OSDN Git Service

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