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