GNU Linux-libre 4.19.286-gnu1
[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 struct smc_cdc_tx_pend {
25         struct smc_connection   *conn;          /* socket connection */
26         union smc_host_cursor   cursor; /* tx sndbuf cursor sent */
27         union smc_host_cursor   p_cursor;       /* rx RMBE cursor produced */
28         u16                     ctrl_seq;       /* conn. tx sequence # */
29 };
30
31 /* handler for send/transmission completion of a CDC msg */
32 static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd,
33                                struct smc_link *link,
34                                enum ib_wc_status wc_status)
35 {
36         struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd;
37         struct smc_connection *conn = cdcpend->conn;
38         struct smc_sock *smc;
39         int diff;
40
41         if (!conn)
42                 /* already dismissed */
43                 return;
44
45         smc = container_of(conn, struct smc_sock, conn);
46         bh_lock_sock(&smc->sk);
47         if (!wc_status) {
48                 diff = smc_curs_diff(cdcpend->conn->sndbuf_desc->len,
49                                      &cdcpend->conn->tx_curs_fin,
50                                      &cdcpend->cursor);
51                 /* sndbuf_space is decreased in smc_sendmsg */
52                 smp_mb__before_atomic();
53                 atomic_add(diff, &cdcpend->conn->sndbuf_space);
54                 /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
55                 smp_mb__after_atomic();
56                 smc_curs_copy(&conn->tx_curs_fin, &cdcpend->cursor, conn);
57         }
58         smc_tx_sndbuf_nonfull(smc);
59         bh_unlock_sock(&smc->sk);
60 }
61
62 int smc_cdc_get_free_slot(struct smc_connection *conn,
63                           struct smc_wr_buf **wr_buf,
64                           struct smc_cdc_tx_pend **pend)
65 {
66         struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
67         int rc;
68
69         rc = smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf,
70                                      (struct smc_wr_tx_pend_priv **)pend);
71         if (!conn->alert_token_local)
72                 /* abnormal termination */
73                 rc = -EPIPE;
74         return rc;
75 }
76
77 static inline void smc_cdc_add_pending_send(struct smc_connection *conn,
78                                             struct smc_cdc_tx_pend *pend)
79 {
80         BUILD_BUG_ON_MSG(
81                 sizeof(struct smc_cdc_msg) > SMC_WR_BUF_SIZE,
82                 "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)");
83         BUILD_BUG_ON_MSG(
84                 sizeof(struct smc_cdc_msg) != SMC_WR_TX_SIZE,
85                 "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()");
86         BUILD_BUG_ON_MSG(
87                 sizeof(struct smc_cdc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
88                 "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)");
89         pend->conn = conn;
90         pend->cursor = conn->tx_curs_sent;
91         pend->p_cursor = conn->local_tx_ctrl.prod;
92         pend->ctrl_seq = conn->tx_cdc_seq;
93 }
94
95 int smc_cdc_msg_send(struct smc_connection *conn,
96                      struct smc_wr_buf *wr_buf,
97                      struct smc_cdc_tx_pend *pend)
98 {
99         union smc_host_cursor cfed;
100         struct smc_link *link;
101         int rc;
102
103         link = &conn->lgr->lnk[SMC_SINGLE_LINK];
104
105         smc_cdc_add_pending_send(conn, pend);
106
107         conn->tx_cdc_seq++;
108         conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
109         smc_host_msg_to_cdc((struct smc_cdc_msg *)wr_buf, conn, &cfed);
110         rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
111         if (!rc)
112                 smc_curs_copy(&conn->rx_curs_confirmed, &cfed, conn);
113
114         return rc;
115 }
116
117 static int smcr_cdc_get_slot_and_msg_send(struct smc_connection *conn)
118 {
119         struct smc_cdc_tx_pend *pend;
120         struct smc_wr_buf *wr_buf;
121         int rc;
122
123         rc = smc_cdc_get_free_slot(conn, &wr_buf, &pend);
124         if (rc)
125                 return rc;
126
127         return smc_cdc_msg_send(conn, wr_buf, pend);
128 }
129
130 int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn)
131 {
132         int rc;
133
134         if (conn->lgr->is_smcd) {
135                 spin_lock_bh(&conn->send_lock);
136                 rc = smcd_cdc_msg_send(conn);
137                 spin_unlock_bh(&conn->send_lock);
138         } else {
139                 rc = smcr_cdc_get_slot_and_msg_send(conn);
140         }
141
142         return rc;
143 }
144
145 static bool smc_cdc_tx_filter(struct smc_wr_tx_pend_priv *tx_pend,
146                               unsigned long data)
147 {
148         struct smc_connection *conn = (struct smc_connection *)data;
149         struct smc_cdc_tx_pend *cdc_pend =
150                 (struct smc_cdc_tx_pend *)tx_pend;
151
152         return cdc_pend->conn == conn;
153 }
154
155 static void smc_cdc_tx_dismisser(struct smc_wr_tx_pend_priv *tx_pend)
156 {
157         struct smc_cdc_tx_pend *cdc_pend =
158                 (struct smc_cdc_tx_pend *)tx_pend;
159
160         cdc_pend->conn = NULL;
161 }
162
163 void smc_cdc_tx_dismiss_slots(struct smc_connection *conn)
164 {
165         struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
166
167         smc_wr_tx_dismiss_slots(link, SMC_CDC_MSG_TYPE,
168                                 smc_cdc_tx_filter, smc_cdc_tx_dismisser,
169                                 (unsigned long)conn);
170 }
171
172 /* Send a SMC-D CDC header.
173  * This increments the free space available in our send buffer.
174  * Also update the confirmed receive buffer with what was sent to the peer.
175  */
176 int smcd_cdc_msg_send(struct smc_connection *conn)
177 {
178         struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
179         struct smcd_cdc_msg cdc;
180         int rc, diff;
181
182         memset(&cdc, 0, sizeof(cdc));
183         cdc.common.type = SMC_CDC_MSG_TYPE;
184         cdc.prod_wrap = conn->local_tx_ctrl.prod.wrap;
185         cdc.prod_count = conn->local_tx_ctrl.prod.count;
186
187         cdc.cons_wrap = conn->local_tx_ctrl.cons.wrap;
188         cdc.cons_count = conn->local_tx_ctrl.cons.count;
189         cdc.prod_flags = conn->local_tx_ctrl.prod_flags;
190         cdc.conn_state_flags = conn->local_tx_ctrl.conn_state_flags;
191         rc = smcd_tx_ism_write(conn, &cdc, sizeof(cdc), 0, 1);
192         if (rc)
193                 return rc;
194         smc_curs_copy(&conn->rx_curs_confirmed, &conn->local_tx_ctrl.cons,
195                       conn);
196         /* Calculate transmitted data and increment free send buffer space */
197         diff = smc_curs_diff(conn->sndbuf_desc->len, &conn->tx_curs_fin,
198                              &conn->tx_curs_sent);
199         /* increased by confirmed number of bytes */
200         smp_mb__before_atomic();
201         atomic_add(diff, &conn->sndbuf_space);
202         /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
203         smp_mb__after_atomic();
204         smc_curs_copy(&conn->tx_curs_fin, &conn->tx_curs_sent, conn);
205
206         smc_tx_sndbuf_nonfull(smc);
207         return rc;
208 }
209
210 /********************************* receive ***********************************/
211
212 static inline bool smc_cdc_before(u16 seq1, u16 seq2)
213 {
214         return (s16)(seq1 - seq2) < 0;
215 }
216
217 static void smc_cdc_handle_urg_data_arrival(struct smc_sock *smc,
218                                             int *diff_prod)
219 {
220         struct smc_connection *conn = &smc->conn;
221         char *base;
222
223         /* new data included urgent business */
224         smc_curs_copy(&conn->urg_curs, &conn->local_rx_ctrl.prod, conn);
225         conn->urg_state = SMC_URG_VALID;
226         if (!sock_flag(&smc->sk, SOCK_URGINLINE))
227                 /* we'll skip the urgent byte, so don't account for it */
228                 (*diff_prod)--;
229         base = (char *)conn->rmb_desc->cpu_addr + conn->rx_off;
230         if (conn->urg_curs.count)
231                 conn->urg_rx_byte = *(base + conn->urg_curs.count - 1);
232         else
233                 conn->urg_rx_byte = *(base + conn->rmb_desc->len - 1);
234         sk_send_sigurg(&smc->sk);
235 }
236
237 static void smc_cdc_msg_recv_action(struct smc_sock *smc,
238                                     struct smc_cdc_msg *cdc)
239 {
240         union smc_host_cursor cons_old, prod_old;
241         struct smc_connection *conn = &smc->conn;
242         int diff_cons, diff_prod;
243
244         smc_curs_copy(&prod_old, &conn->local_rx_ctrl.prod, conn);
245         smc_curs_copy(&cons_old, &conn->local_rx_ctrl.cons, conn);
246         smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);
247
248         diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
249                                   &conn->local_rx_ctrl.cons);
250         if (diff_cons) {
251                 /* peer_rmbe_space is decreased during data transfer with RDMA
252                  * write
253                  */
254                 smp_mb__before_atomic();
255                 atomic_add(diff_cons, &conn->peer_rmbe_space);
256                 /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
257                 smp_mb__after_atomic();
258         }
259
260         diff_prod = smc_curs_diff(conn->rmb_desc->len, &prod_old,
261                                   &conn->local_rx_ctrl.prod);
262         if (diff_prod) {
263                 if (conn->local_rx_ctrl.prod_flags.urg_data_present)
264                         smc_cdc_handle_urg_data_arrival(smc, &diff_prod);
265                 /* bytes_to_rcv is decreased in smc_recvmsg */
266                 smp_mb__before_atomic();
267                 atomic_add(diff_prod, &conn->bytes_to_rcv);
268                 /* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */
269                 smp_mb__after_atomic();
270                 smc->sk.sk_data_ready(&smc->sk);
271         } else {
272                 if (conn->local_rx_ctrl.prod_flags.write_blocked ||
273                     conn->local_rx_ctrl.prod_flags.cons_curs_upd_req ||
274                     conn->local_rx_ctrl.prod_flags.urg_data_pending) {
275                         if (conn->local_rx_ctrl.prod_flags.urg_data_pending)
276                                 conn->urg_state = SMC_URG_NOTYET;
277                         /* force immediate tx of current consumer cursor, but
278                          * under send_lock to guarantee arrival in seqno-order
279                          */
280                         if (smc->sk.sk_state != SMC_INIT)
281                                 smc_tx_sndbuf_nonempty(conn);
282                 }
283         }
284
285         /* piggy backed tx info */
286         /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */
287         if (diff_cons && smc_tx_prepared_sends(conn)) {
288                 smc_tx_sndbuf_nonempty(conn);
289                 /* trigger socket release if connection closed */
290                 smc_close_wake_tx_prepared(smc);
291         }
292         if (diff_cons && conn->urg_tx_pend &&
293             atomic_read(&conn->peer_rmbe_space) == conn->peer_rmbe_size) {
294                 /* urg data confirmed by peer, indicate we're ready for more */
295                 conn->urg_tx_pend = false;
296                 smc->sk.sk_write_space(&smc->sk);
297         }
298
299         if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
300                 smc->sk.sk_err = ECONNRESET;
301                 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
302         }
303         if (smc_cdc_rxed_any_close_or_senddone(conn)) {
304                 smc->sk.sk_shutdown |= RCV_SHUTDOWN;
305                 if (smc->clcsock && smc->clcsock->sk)
306                         smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
307                 sock_set_flag(&smc->sk, SOCK_DONE);
308                 sock_hold(&smc->sk); /* sock_put in close_work */
309                 if (!schedule_work(&conn->close_work))
310                         sock_put(&smc->sk);
311         }
312 }
313
314 /* called under tasklet context */
315 static void smc_cdc_msg_recv(struct smc_sock *smc, struct smc_cdc_msg *cdc)
316 {
317         sock_hold(&smc->sk);
318         bh_lock_sock(&smc->sk);
319         smc_cdc_msg_recv_action(smc, cdc);
320         bh_unlock_sock(&smc->sk);
321         sock_put(&smc->sk); /* no free sk in softirq-context */
322 }
323
324 /* Schedule a tasklet for this connection. Triggered from the ISM device IRQ
325  * handler to indicate update in the DMBE.
326  *
327  * Context:
328  * - tasklet context
329  */
330 static void smcd_cdc_rx_tsklet(unsigned long data)
331 {
332         struct smc_connection *conn = (struct smc_connection *)data;
333         struct smcd_cdc_msg cdc;
334         struct smc_sock *smc;
335
336         if (!conn)
337                 return;
338
339         memcpy(&cdc, conn->rmb_desc->cpu_addr, sizeof(cdc));
340         smc = container_of(conn, struct smc_sock, conn);
341         smc_cdc_msg_recv(smc, (struct smc_cdc_msg *)&cdc);
342 }
343
344 /* Initialize receive tasklet. Called from ISM device IRQ handler to start
345  * receiver side.
346  */
347 void smcd_cdc_rx_init(struct smc_connection *conn)
348 {
349         tasklet_init(&conn->rx_tsklet, smcd_cdc_rx_tsklet, (unsigned long)conn);
350 }
351
352 /***************************** init, exit, misc ******************************/
353
354 static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
355 {
356         struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
357         struct smc_cdc_msg *cdc = buf;
358         struct smc_connection *conn;
359         struct smc_link_group *lgr;
360         struct smc_sock *smc;
361
362         if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved))
363                 return; /* short message */
364         if (cdc->len != SMC_WR_TX_SIZE)
365                 return; /* invalid message */
366
367         /* lookup connection */
368         lgr = smc_get_lgr(link);
369         read_lock_bh(&lgr->conns_lock);
370         conn = smc_lgr_find_conn(ntohl(cdc->token), lgr);
371         read_unlock_bh(&lgr->conns_lock);
372         if (!conn)
373                 return;
374         smc = container_of(conn, struct smc_sock, conn);
375
376         if (!cdc->prod_flags.failover_validation) {
377                 if (smc_cdc_before(ntohs(cdc->seqno),
378                                    conn->local_rx_ctrl.seqno))
379                         /* received seqno is old */
380                         return;
381         }
382         smc_cdc_msg_recv(smc, cdc);
383 }
384
385 static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = {
386         {
387                 .handler        = smc_cdc_rx_handler,
388                 .type           = SMC_CDC_MSG_TYPE
389         },
390         {
391                 .handler        = NULL,
392         }
393 };
394
395 int __init smc_cdc_init(void)
396 {
397         struct smc_wr_rx_handler *handler;
398         int rc = 0;
399
400         for (handler = smc_cdc_rx_handlers; handler->handler; handler++) {
401                 INIT_HLIST_NODE(&handler->list);
402                 rc = smc_wr_rx_register_handler(handler);
403                 if (rc)
404                         break;
405         }
406         return rc;
407 }