GNU Linux-libre 5.15.137-gnu
[releases.git] / net / smc / smc_cdc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Shared Memory Communications over RDMA (SMC-R) and RoCE
4  *
5  * Connection Data Control (CDC)
6  * handles flow control
7  *
8  * Copyright IBM Corp. 2016
9  *
10  * Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
11  */
12
13 #include <linux/spinlock.h>
14
15 #include "smc.h"
16 #include "smc_wr.h"
17 #include "smc_cdc.h"
18 #include "smc_tx.h"
19 #include "smc_rx.h"
20 #include "smc_close.h"
21
22 /********************************** send *************************************/
23
24 /* handler for send/transmission completion of a CDC msg */
25 static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd,
26                                struct smc_link *link,
27                                enum ib_wc_status wc_status)
28 {
29         struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd;
30         struct smc_connection *conn = cdcpend->conn;
31         struct smc_sock *smc;
32         int diff;
33
34         smc = container_of(conn, struct smc_sock, conn);
35         bh_lock_sock(&smc->sk);
36         if (!wc_status) {
37                 diff = smc_curs_diff(cdcpend->conn->sndbuf_desc->len,
38                                      &cdcpend->conn->tx_curs_fin,
39                                      &cdcpend->cursor);
40                 /* sndbuf_space is decreased in smc_sendmsg */
41                 smp_mb__before_atomic();
42                 atomic_add(diff, &cdcpend->conn->sndbuf_space);
43                 /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
44                 smp_mb__after_atomic();
45                 smc_curs_copy(&conn->tx_curs_fin, &cdcpend->cursor, conn);
46                 smc_curs_copy(&conn->local_tx_ctrl_fin, &cdcpend->p_cursor,
47                               conn);
48                 conn->tx_cdc_seq_fin = cdcpend->ctrl_seq;
49         }
50
51         if (atomic_dec_and_test(&conn->cdc_pend_tx_wr) &&
52             unlikely(wq_has_sleeper(&conn->cdc_pend_tx_wq)))
53                 wake_up(&conn->cdc_pend_tx_wq);
54         WARN_ON(atomic_read(&conn->cdc_pend_tx_wr) < 0);
55
56         smc_tx_sndbuf_nonfull(smc);
57         bh_unlock_sock(&smc->sk);
58 }
59
60 int smc_cdc_get_free_slot(struct smc_connection *conn,
61                           struct smc_link *link,
62                           struct smc_wr_buf **wr_buf,
63                           struct smc_rdma_wr **wr_rdma_buf,
64                           struct smc_cdc_tx_pend **pend)
65 {
66         int rc;
67
68         rc = smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf,
69                                      wr_rdma_buf,
70                                      (struct smc_wr_tx_pend_priv **)pend);
71         if (conn->killed) {
72                 /* abnormal termination */
73                 if (!rc)
74                         smc_wr_tx_put_slot(link,
75                                            (struct smc_wr_tx_pend_priv *)(*pend));
76                 rc = -EPIPE;
77         }
78         return rc;
79 }
80
81 static inline void smc_cdc_add_pending_send(struct smc_connection *conn,
82                                             struct smc_cdc_tx_pend *pend)
83 {
84         BUILD_BUG_ON_MSG(
85                 sizeof(struct smc_cdc_msg) > SMC_WR_BUF_SIZE,
86                 "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)");
87         BUILD_BUG_ON_MSG(
88                 offsetofend(struct smc_cdc_msg, reserved) > SMC_WR_TX_SIZE,
89                 "must adapt SMC_WR_TX_SIZE to sizeof(struct smc_cdc_msg); if not all smc_wr upper layer protocols use the same message size any more, must start to set link->wr_tx_sges[i].length on each individual smc_wr_tx_send()");
90         BUILD_BUG_ON_MSG(
91                 sizeof(struct smc_cdc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
92                 "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)");
93         pend->conn = conn;
94         pend->cursor = conn->tx_curs_sent;
95         pend->p_cursor = conn->local_tx_ctrl.prod;
96         pend->ctrl_seq = conn->tx_cdc_seq;
97 }
98
99 int smc_cdc_msg_send(struct smc_connection *conn,
100                      struct smc_wr_buf *wr_buf,
101                      struct smc_cdc_tx_pend *pend)
102 {
103         struct smc_link *link = conn->lnk;
104         union smc_host_cursor cfed;
105         int rc;
106
107         if (unlikely(!READ_ONCE(conn->sndbuf_desc)))
108                 return -ENOBUFS;
109
110         smc_cdc_add_pending_send(conn, pend);
111
112         conn->tx_cdc_seq++;
113         conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
114         smc_host_msg_to_cdc((struct smc_cdc_msg *)wr_buf, conn, &cfed);
115
116         atomic_inc(&conn->cdc_pend_tx_wr);
117         smp_mb__after_atomic(); /* Make sure cdc_pend_tx_wr added before post */
118
119         rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
120         if (!rc) {
121                 smc_curs_copy(&conn->rx_curs_confirmed, &cfed, conn);
122                 conn->local_rx_ctrl.prod_flags.cons_curs_upd_req = 0;
123         } else {
124                 conn->tx_cdc_seq--;
125                 conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
126                 atomic_dec(&conn->cdc_pend_tx_wr);
127         }
128
129         return rc;
130 }
131
132 /* send a validation msg indicating the move of a conn to an other QP link */
133 int smcr_cdc_msg_send_validation(struct smc_connection *conn,
134                                  struct smc_cdc_tx_pend *pend,
135                                  struct smc_wr_buf *wr_buf)
136 {
137         struct smc_host_cdc_msg *local = &conn->local_tx_ctrl;
138         struct smc_link *link = conn->lnk;
139         struct smc_cdc_msg *peer;
140         int rc;
141
142         peer = (struct smc_cdc_msg *)wr_buf;
143         peer->common.type = local->common.type;
144         peer->len = local->len;
145         peer->seqno = htons(conn->tx_cdc_seq_fin); /* seqno last compl. tx */
146         peer->token = htonl(local->token);
147         peer->prod_flags.failover_validation = 1;
148
149         /* We need to set pend->conn here to make sure smc_cdc_tx_handler()
150          * can handle properly
151          */
152         smc_cdc_add_pending_send(conn, pend);
153
154         atomic_inc(&conn->cdc_pend_tx_wr);
155         smp_mb__after_atomic(); /* Make sure cdc_pend_tx_wr added before post */
156
157         rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
158         if (unlikely(rc))
159                 atomic_dec(&conn->cdc_pend_tx_wr);
160
161         return rc;
162 }
163
164 static int smcr_cdc_get_slot_and_msg_send(struct smc_connection *conn)
165 {
166         struct smc_cdc_tx_pend *pend;
167         struct smc_wr_buf *wr_buf;
168         struct smc_link *link;
169         bool again = false;
170         int rc;
171
172 again:
173         link = conn->lnk;
174         if (!smc_wr_tx_link_hold(link))
175                 return -ENOLINK;
176         rc = smc_cdc_get_free_slot(conn, link, &wr_buf, NULL, &pend);
177         if (rc)
178                 goto put_out;
179
180         spin_lock_bh(&conn->send_lock);
181         if (link != conn->lnk) {
182                 /* link of connection changed, try again one time*/
183                 spin_unlock_bh(&conn->send_lock);
184                 smc_wr_tx_put_slot(link,
185                                    (struct smc_wr_tx_pend_priv *)pend);
186                 smc_wr_tx_link_put(link);
187                 if (again)
188                         return -ENOLINK;
189                 again = true;
190                 goto again;
191         }
192         rc = smc_cdc_msg_send(conn, wr_buf, pend);
193         spin_unlock_bh(&conn->send_lock);
194 put_out:
195         smc_wr_tx_link_put(link);
196         return rc;
197 }
198
199 int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn)
200 {
201         int rc;
202
203         if (!conn->lgr || (conn->lgr->is_smcd && conn->lgr->peer_shutdown))
204                 return -EPIPE;
205
206         if (conn->lgr->is_smcd) {
207                 spin_lock_bh(&conn->send_lock);
208                 rc = smcd_cdc_msg_send(conn);
209                 spin_unlock_bh(&conn->send_lock);
210         } else {
211                 rc = smcr_cdc_get_slot_and_msg_send(conn);
212         }
213
214         return rc;
215 }
216
217 void smc_cdc_wait_pend_tx_wr(struct smc_connection *conn)
218 {
219         wait_event(conn->cdc_pend_tx_wq, !atomic_read(&conn->cdc_pend_tx_wr));
220 }
221
222 /* Send a SMC-D CDC header.
223  * This increments the free space available in our send buffer.
224  * Also update the confirmed receive buffer with what was sent to the peer.
225  */
226 int smcd_cdc_msg_send(struct smc_connection *conn)
227 {
228         struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
229         union smc_host_cursor curs;
230         struct smcd_cdc_msg cdc;
231         int rc, diff;
232
233         memset(&cdc, 0, sizeof(cdc));
234         cdc.common.type = SMC_CDC_MSG_TYPE;
235         curs.acurs.counter = atomic64_read(&conn->local_tx_ctrl.prod.acurs);
236         cdc.prod.wrap = curs.wrap;
237         cdc.prod.count = curs.count;
238         curs.acurs.counter = atomic64_read(&conn->local_tx_ctrl.cons.acurs);
239         cdc.cons.wrap = curs.wrap;
240         cdc.cons.count = curs.count;
241         cdc.cons.prod_flags = conn->local_tx_ctrl.prod_flags;
242         cdc.cons.conn_state_flags = conn->local_tx_ctrl.conn_state_flags;
243         rc = smcd_tx_ism_write(conn, &cdc, sizeof(cdc), 0, 1);
244         if (rc)
245                 return rc;
246         smc_curs_copy(&conn->rx_curs_confirmed, &curs, conn);
247         conn->local_rx_ctrl.prod_flags.cons_curs_upd_req = 0;
248         /* Calculate transmitted data and increment free send buffer space */
249         diff = smc_curs_diff(conn->sndbuf_desc->len, &conn->tx_curs_fin,
250                              &conn->tx_curs_sent);
251         /* increased by confirmed number of bytes */
252         smp_mb__before_atomic();
253         atomic_add(diff, &conn->sndbuf_space);
254         /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
255         smp_mb__after_atomic();
256         smc_curs_copy(&conn->tx_curs_fin, &conn->tx_curs_sent, conn);
257
258         smc_tx_sndbuf_nonfull(smc);
259         return rc;
260 }
261
262 /********************************* receive ***********************************/
263
264 static inline bool smc_cdc_before(u16 seq1, u16 seq2)
265 {
266         return (s16)(seq1 - seq2) < 0;
267 }
268
269 static void smc_cdc_handle_urg_data_arrival(struct smc_sock *smc,
270                                             int *diff_prod)
271 {
272         struct smc_connection *conn = &smc->conn;
273         char *base;
274
275         /* new data included urgent business */
276         smc_curs_copy(&conn->urg_curs, &conn->local_rx_ctrl.prod, conn);
277         conn->urg_state = SMC_URG_VALID;
278         if (!sock_flag(&smc->sk, SOCK_URGINLINE))
279                 /* we'll skip the urgent byte, so don't account for it */
280                 (*diff_prod)--;
281         base = (char *)conn->rmb_desc->cpu_addr + conn->rx_off;
282         if (conn->urg_curs.count)
283                 conn->urg_rx_byte = *(base + conn->urg_curs.count - 1);
284         else
285                 conn->urg_rx_byte = *(base + conn->rmb_desc->len - 1);
286         sk_send_sigurg(&smc->sk);
287 }
288
289 static void smc_cdc_msg_validate(struct smc_sock *smc, struct smc_cdc_msg *cdc,
290                                  struct smc_link *link)
291 {
292         struct smc_connection *conn = &smc->conn;
293         u16 recv_seq = ntohs(cdc->seqno);
294         s16 diff;
295
296         /* check that seqnum was seen before */
297         diff = conn->local_rx_ctrl.seqno - recv_seq;
298         if (diff < 0) { /* diff larger than 0x7fff */
299                 /* drop connection */
300                 conn->out_of_sync = 1;  /* prevent any further receives */
301                 spin_lock_bh(&conn->send_lock);
302                 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
303                 conn->lnk = link;
304                 spin_unlock_bh(&conn->send_lock);
305                 sock_hold(&smc->sk); /* sock_put in abort_work */
306                 if (!queue_work(smc_close_wq, &conn->abort_work))
307                         sock_put(&smc->sk);
308         }
309 }
310
311 static void smc_cdc_msg_recv_action(struct smc_sock *smc,
312                                     struct smc_cdc_msg *cdc)
313 {
314         union smc_host_cursor cons_old, prod_old;
315         struct smc_connection *conn = &smc->conn;
316         int diff_cons, diff_prod;
317
318         smc_curs_copy(&prod_old, &conn->local_rx_ctrl.prod, conn);
319         smc_curs_copy(&cons_old, &conn->local_rx_ctrl.cons, conn);
320         smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);
321
322         diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
323                                   &conn->local_rx_ctrl.cons);
324         if (diff_cons) {
325                 /* peer_rmbe_space is decreased during data transfer with RDMA
326                  * write
327                  */
328                 smp_mb__before_atomic();
329                 atomic_add(diff_cons, &conn->peer_rmbe_space);
330                 /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
331                 smp_mb__after_atomic();
332         }
333
334         diff_prod = smc_curs_diff(conn->rmb_desc->len, &prod_old,
335                                   &conn->local_rx_ctrl.prod);
336         if (diff_prod) {
337                 if (conn->local_rx_ctrl.prod_flags.urg_data_present)
338                         smc_cdc_handle_urg_data_arrival(smc, &diff_prod);
339                 /* bytes_to_rcv is decreased in smc_recvmsg */
340                 smp_mb__before_atomic();
341                 atomic_add(diff_prod, &conn->bytes_to_rcv);
342                 /* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */
343                 smp_mb__after_atomic();
344                 smc->sk.sk_data_ready(&smc->sk);
345         } else {
346                 if (conn->local_rx_ctrl.prod_flags.write_blocked)
347                         smc->sk.sk_data_ready(&smc->sk);
348                 if (conn->local_rx_ctrl.prod_flags.urg_data_pending)
349                         conn->urg_state = SMC_URG_NOTYET;
350         }
351
352         /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */
353         if ((diff_cons && smc_tx_prepared_sends(conn)) ||
354             conn->local_rx_ctrl.prod_flags.cons_curs_upd_req ||
355             conn->local_rx_ctrl.prod_flags.urg_data_pending)
356                 smc_tx_sndbuf_nonempty(conn);
357
358         if (diff_cons && conn->urg_tx_pend &&
359             atomic_read(&conn->peer_rmbe_space) == conn->peer_rmbe_size) {
360                 /* urg data confirmed by peer, indicate we're ready for more */
361                 conn->urg_tx_pend = false;
362                 smc->sk.sk_write_space(&smc->sk);
363         }
364
365         if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
366                 smc->sk.sk_err = ECONNRESET;
367                 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
368         }
369         if (smc_cdc_rxed_any_close_or_senddone(conn)) {
370                 smc->sk.sk_shutdown |= RCV_SHUTDOWN;
371                 if (smc->clcsock && smc->clcsock->sk)
372                         smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
373                 sock_set_flag(&smc->sk, SOCK_DONE);
374                 sock_hold(&smc->sk); /* sock_put in close_work */
375                 if (!queue_work(smc_close_wq, &conn->close_work))
376                         sock_put(&smc->sk);
377         }
378 }
379
380 /* called under tasklet context */
381 static void smc_cdc_msg_recv(struct smc_sock *smc, struct smc_cdc_msg *cdc)
382 {
383         sock_hold(&smc->sk);
384         bh_lock_sock(&smc->sk);
385         smc_cdc_msg_recv_action(smc, cdc);
386         bh_unlock_sock(&smc->sk);
387         sock_put(&smc->sk); /* no free sk in softirq-context */
388 }
389
390 /* Schedule a tasklet for this connection. Triggered from the ISM device IRQ
391  * handler to indicate update in the DMBE.
392  *
393  * Context:
394  * - tasklet context
395  */
396 static void smcd_cdc_rx_tsklet(struct tasklet_struct *t)
397 {
398         struct smc_connection *conn = from_tasklet(conn, t, rx_tsklet);
399         struct smcd_cdc_msg *data_cdc;
400         struct smcd_cdc_msg cdc;
401         struct smc_sock *smc;
402
403         if (!conn || conn->killed)
404                 return;
405
406         data_cdc = (struct smcd_cdc_msg *)conn->rmb_desc->cpu_addr;
407         smcd_curs_copy(&cdc.prod, &data_cdc->prod, conn);
408         smcd_curs_copy(&cdc.cons, &data_cdc->cons, conn);
409         smc = container_of(conn, struct smc_sock, conn);
410         smc_cdc_msg_recv(smc, (struct smc_cdc_msg *)&cdc);
411 }
412
413 /* Initialize receive tasklet. Called from ISM device IRQ handler to start
414  * receiver side.
415  */
416 void smcd_cdc_rx_init(struct smc_connection *conn)
417 {
418         tasklet_setup(&conn->rx_tsklet, smcd_cdc_rx_tsklet);
419 }
420
421 /***************************** init, exit, misc ******************************/
422
423 static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
424 {
425         struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
426         struct smc_cdc_msg *cdc = buf;
427         struct smc_connection *conn;
428         struct smc_link_group *lgr;
429         struct smc_sock *smc;
430
431         if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved))
432                 return; /* short message */
433         if (cdc->len != SMC_WR_TX_SIZE)
434                 return; /* invalid message */
435
436         /* lookup connection */
437         lgr = smc_get_lgr(link);
438         read_lock_bh(&lgr->conns_lock);
439         conn = smc_lgr_find_conn(ntohl(cdc->token), lgr);
440         read_unlock_bh(&lgr->conns_lock);
441         if (!conn || conn->out_of_sync)
442                 return;
443         smc = container_of(conn, struct smc_sock, conn);
444
445         if (cdc->prod_flags.failover_validation) {
446                 smc_cdc_msg_validate(smc, cdc, link);
447                 return;
448         }
449         if (smc_cdc_before(ntohs(cdc->seqno),
450                            conn->local_rx_ctrl.seqno))
451                 /* received seqno is old */
452                 return;
453
454         smc_cdc_msg_recv(smc, cdc);
455 }
456
457 static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = {
458         {
459                 .handler        = smc_cdc_rx_handler,
460                 .type           = SMC_CDC_MSG_TYPE
461         },
462         {
463                 .handler        = NULL,
464         }
465 };
466
467 int __init smc_cdc_init(void)
468 {
469         struct smc_wr_rx_handler *handler;
470         int rc = 0;
471
472         for (handler = smc_cdc_rx_handlers; handler->handler; handler++) {
473                 INIT_HLIST_NODE(&handler->list);
474                 rc = smc_wr_rx_register_handler(handler);
475                 if (rc)
476                         break;
477         }
478         return rc;
479 }