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