1 // SPDX-License-Identifier: GPL-2.0
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
5 * Connection Data Control (CDC)
8 * Copyright IBM Corp. 2016
13 #include <linux/spinlock.h>
20 #include "smc_close.h"
22 /********************************** send *************************************/
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)
29 struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd;
30 struct smc_connection *conn = cdcpend->conn;
34 smc = container_of(conn, struct smc_sock, conn);
35 bh_lock_sock(&smc->sk);
37 diff = smc_curs_diff(cdcpend->conn->sndbuf_desc->len,
38 &cdcpend->conn->tx_curs_fin,
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,
48 conn->tx_cdc_seq_fin = cdcpend->ctrl_seq;
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
56 if (sock_owned_by_user(&smc->sk))
57 conn->tx_in_release_sock = true;
61 if (unlikely(wq_has_sleeper(&conn->cdc_pend_tx_wq)))
62 wake_up(&conn->cdc_pend_tx_wq);
64 WARN_ON(atomic_read(&conn->cdc_pend_tx_wr) < 0);
66 smc_tx_sndbuf_nonfull(smc);
67 bh_unlock_sock(&smc->sk);
70 int 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)
78 rc = smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf,
80 (struct smc_wr_tx_pend_priv **)pend);
82 /* abnormal termination */
84 smc_wr_tx_put_slot(link,
85 (struct smc_wr_tx_pend_priv *)(*pend));
91 static inline void smc_cdc_add_pending_send(struct smc_connection *conn,
92 struct smc_cdc_tx_pend *pend)
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)");
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()");
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)");
104 pend->cursor = conn->tx_curs_sent;
105 pend->p_cursor = conn->local_tx_ctrl.prod;
106 pend->ctrl_seq = conn->tx_cdc_seq;
109 int smc_cdc_msg_send(struct smc_connection *conn,
110 struct smc_wr_buf *wr_buf,
111 struct smc_cdc_tx_pend *pend)
113 struct smc_link *link = conn->lnk;
114 union smc_host_cursor cfed;
117 if (unlikely(!READ_ONCE(conn->sndbuf_desc)))
120 smc_cdc_add_pending_send(conn, pend);
123 conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
124 smc_host_msg_to_cdc((struct smc_cdc_msg *)wr_buf, conn, &cfed);
126 atomic_inc(&conn->cdc_pend_tx_wr);
127 smp_mb__after_atomic(); /* Make sure cdc_pend_tx_wr added before post */
129 rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
131 smc_curs_copy(&conn->rx_curs_confirmed, &cfed, conn);
132 conn->local_rx_ctrl.prod_flags.cons_curs_upd_req = 0;
135 conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
136 atomic_dec(&conn->cdc_pend_tx_wr);
142 /* send a validation msg indicating the move of a conn to an other QP link */
143 int smcr_cdc_msg_send_validation(struct smc_connection *conn,
144 struct smc_cdc_tx_pend *pend,
145 struct smc_wr_buf *wr_buf)
147 struct smc_host_cdc_msg *local = &conn->local_tx_ctrl;
148 struct smc_link *link = conn->lnk;
149 struct smc_cdc_msg *peer;
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;
159 /* We need to set pend->conn here to make sure smc_cdc_tx_handler()
160 * can handle properly
162 smc_cdc_add_pending_send(conn, pend);
164 atomic_inc(&conn->cdc_pend_tx_wr);
165 smp_mb__after_atomic(); /* Make sure cdc_pend_tx_wr added before post */
167 rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
169 atomic_dec(&conn->cdc_pend_tx_wr);
174 static int smcr_cdc_get_slot_and_msg_send(struct smc_connection *conn)
176 struct smc_cdc_tx_pend *pend;
177 struct smc_wr_buf *wr_buf;
178 struct smc_link *link;
184 if (!smc_wr_tx_link_hold(link))
186 rc = smc_cdc_get_free_slot(conn, link, &wr_buf, NULL, &pend);
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);
202 rc = smc_cdc_msg_send(conn, wr_buf, pend);
203 spin_unlock_bh(&conn->send_lock);
205 smc_wr_tx_link_put(link);
209 int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn)
213 if (!smc_conn_lgr_valid(conn) ||
214 (conn->lgr->is_smcd && conn->lgr->peer_shutdown))
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);
222 rc = smcr_cdc_get_slot_and_msg_send(conn);
228 void smc_cdc_wait_pend_tx_wr(struct smc_connection *conn)
230 wait_event(conn->cdc_pend_tx_wq, !atomic_read(&conn->cdc_pend_tx_wr));
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.
237 int smcd_cdc_msg_send(struct smc_connection *conn)
239 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
240 union smc_host_cursor curs;
241 struct smcd_cdc_msg cdc;
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);
257 smc_curs_copy(&conn->rx_curs_confirmed, &curs, conn);
258 conn->local_rx_ctrl.prod_flags.cons_curs_upd_req = 0;
259 /* Calculate transmitted data and increment free send buffer space */
260 diff = smc_curs_diff(conn->sndbuf_desc->len, &conn->tx_curs_fin,
261 &conn->tx_curs_sent);
262 /* increased by confirmed number of bytes */
263 smp_mb__before_atomic();
264 atomic_add(diff, &conn->sndbuf_space);
265 /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
266 smp_mb__after_atomic();
267 smc_curs_copy(&conn->tx_curs_fin, &conn->tx_curs_sent, conn);
269 smc_tx_sndbuf_nonfull(smc);
273 /********************************* receive ***********************************/
275 static inline bool smc_cdc_before(u16 seq1, u16 seq2)
277 return (s16)(seq1 - seq2) < 0;
280 static void smc_cdc_handle_urg_data_arrival(struct smc_sock *smc,
283 struct smc_connection *conn = &smc->conn;
286 /* new data included urgent business */
287 smc_curs_copy(&conn->urg_curs, &conn->local_rx_ctrl.prod, conn);
288 conn->urg_state = SMC_URG_VALID;
289 if (!sock_flag(&smc->sk, SOCK_URGINLINE))
290 /* we'll skip the urgent byte, so don't account for it */
292 base = (char *)conn->rmb_desc->cpu_addr + conn->rx_off;
293 if (conn->urg_curs.count)
294 conn->urg_rx_byte = *(base + conn->urg_curs.count - 1);
296 conn->urg_rx_byte = *(base + conn->rmb_desc->len - 1);
297 sk_send_sigurg(&smc->sk);
300 static void smc_cdc_msg_validate(struct smc_sock *smc, struct smc_cdc_msg *cdc,
301 struct smc_link *link)
303 struct smc_connection *conn = &smc->conn;
304 u16 recv_seq = ntohs(cdc->seqno);
307 /* check that seqnum was seen before */
308 diff = conn->local_rx_ctrl.seqno - recv_seq;
309 if (diff < 0) { /* diff larger than 0x7fff */
310 /* drop connection */
311 conn->out_of_sync = 1; /* prevent any further receives */
312 spin_lock_bh(&conn->send_lock);
313 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
315 spin_unlock_bh(&conn->send_lock);
316 sock_hold(&smc->sk); /* sock_put in abort_work */
317 if (!queue_work(smc_close_wq, &conn->abort_work))
322 static void smc_cdc_msg_recv_action(struct smc_sock *smc,
323 struct smc_cdc_msg *cdc)
325 union smc_host_cursor cons_old, prod_old;
326 struct smc_connection *conn = &smc->conn;
327 int diff_cons, diff_prod;
329 smc_curs_copy(&prod_old, &conn->local_rx_ctrl.prod, conn);
330 smc_curs_copy(&cons_old, &conn->local_rx_ctrl.cons, conn);
331 smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);
333 diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
334 &conn->local_rx_ctrl.cons);
336 /* peer_rmbe_space is decreased during data transfer with RDMA
339 smp_mb__before_atomic();
340 atomic_add(diff_cons, &conn->peer_rmbe_space);
341 /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
342 smp_mb__after_atomic();
345 diff_prod = smc_curs_diff(conn->rmb_desc->len, &prod_old,
346 &conn->local_rx_ctrl.prod);
348 if (conn->local_rx_ctrl.prod_flags.urg_data_present)
349 smc_cdc_handle_urg_data_arrival(smc, &diff_prod);
350 /* bytes_to_rcv is decreased in smc_recvmsg */
351 smp_mb__before_atomic();
352 atomic_add(diff_prod, &conn->bytes_to_rcv);
353 /* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */
354 smp_mb__after_atomic();
355 smc->sk.sk_data_ready(&smc->sk);
357 if (conn->local_rx_ctrl.prod_flags.write_blocked)
358 smc->sk.sk_data_ready(&smc->sk);
359 if (conn->local_rx_ctrl.prod_flags.urg_data_pending)
360 conn->urg_state = SMC_URG_NOTYET;
363 /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */
364 if ((diff_cons && smc_tx_prepared_sends(conn)) ||
365 conn->local_rx_ctrl.prod_flags.cons_curs_upd_req ||
366 conn->local_rx_ctrl.prod_flags.urg_data_pending) {
367 if (!sock_owned_by_user(&smc->sk))
368 smc_tx_pending(conn);
370 conn->tx_in_release_sock = true;
373 if (diff_cons && conn->urg_tx_pend &&
374 atomic_read(&conn->peer_rmbe_space) == conn->peer_rmbe_size) {
375 /* urg data confirmed by peer, indicate we're ready for more */
376 conn->urg_tx_pend = false;
377 smc->sk.sk_write_space(&smc->sk);
380 if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
381 smc->sk.sk_err = ECONNRESET;
382 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
384 if (smc_cdc_rxed_any_close_or_senddone(conn)) {
385 smc->sk.sk_shutdown |= RCV_SHUTDOWN;
386 if (smc->clcsock && smc->clcsock->sk)
387 smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
388 sock_set_flag(&smc->sk, SOCK_DONE);
389 sock_hold(&smc->sk); /* sock_put in close_work */
390 if (!queue_work(smc_close_wq, &conn->close_work))
395 /* called under tasklet context */
396 static void smc_cdc_msg_recv(struct smc_sock *smc, struct smc_cdc_msg *cdc)
399 bh_lock_sock(&smc->sk);
400 smc_cdc_msg_recv_action(smc, cdc);
401 bh_unlock_sock(&smc->sk);
402 sock_put(&smc->sk); /* no free sk in softirq-context */
405 /* Schedule a tasklet for this connection. Triggered from the ISM device IRQ
406 * handler to indicate update in the DMBE.
411 static void smcd_cdc_rx_tsklet(struct tasklet_struct *t)
413 struct smc_connection *conn = from_tasklet(conn, t, rx_tsklet);
414 struct smcd_cdc_msg *data_cdc;
415 struct smcd_cdc_msg cdc;
416 struct smc_sock *smc;
418 if (!conn || conn->killed)
421 data_cdc = (struct smcd_cdc_msg *)conn->rmb_desc->cpu_addr;
422 smcd_curs_copy(&cdc.prod, &data_cdc->prod, conn);
423 smcd_curs_copy(&cdc.cons, &data_cdc->cons, conn);
424 smc = container_of(conn, struct smc_sock, conn);
425 smc_cdc_msg_recv(smc, (struct smc_cdc_msg *)&cdc);
428 /* Initialize receive tasklet. Called from ISM device IRQ handler to start
431 void smcd_cdc_rx_init(struct smc_connection *conn)
433 tasklet_setup(&conn->rx_tsklet, smcd_cdc_rx_tsklet);
436 /***************************** init, exit, misc ******************************/
438 static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
440 struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
441 struct smc_cdc_msg *cdc = buf;
442 struct smc_connection *conn;
443 struct smc_link_group *lgr;
444 struct smc_sock *smc;
446 if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved))
447 return; /* short message */
448 if (cdc->len != SMC_WR_TX_SIZE)
449 return; /* invalid message */
451 /* lookup connection */
452 lgr = smc_get_lgr(link);
453 read_lock_bh(&lgr->conns_lock);
454 conn = smc_lgr_find_conn(ntohl(cdc->token), lgr);
455 read_unlock_bh(&lgr->conns_lock);
456 if (!conn || conn->out_of_sync)
458 smc = container_of(conn, struct smc_sock, conn);
460 if (cdc->prod_flags.failover_validation) {
461 smc_cdc_msg_validate(smc, cdc, link);
464 if (smc_cdc_before(ntohs(cdc->seqno),
465 conn->local_rx_ctrl.seqno))
466 /* received seqno is old */
469 smc_cdc_msg_recv(smc, cdc);
472 static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = {
474 .handler = smc_cdc_rx_handler,
475 .type = SMC_CDC_MSG_TYPE
482 int __init smc_cdc_init(void)
484 struct smc_wr_rx_handler *handler;
487 for (handler = smc_cdc_rx_handlers; handler->handler; handler++) {
488 INIT_HLIST_NODE(&handler->list);
489 rc = smc_wr_rx_register_handler(handler);