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
10 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
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;
35 /* already dismissed */
38 smc = container_of(conn, struct smc_sock, conn);
39 bh_lock_sock(&smc->sk);
41 diff = smc_curs_diff(cdcpend->conn->sndbuf_desc->len,
42 &cdcpend->conn->tx_curs_fin,
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,
52 conn->tx_cdc_seq_fin = cdcpend->ctrl_seq;
54 smc_tx_sndbuf_nonfull(smc);
55 bh_unlock_sock(&smc->sk);
58 int 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)
66 rc = smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf,
68 (struct smc_wr_tx_pend_priv **)pend);
70 /* abnormal termination */
75 static inline void smc_cdc_add_pending_send(struct smc_connection *conn,
76 struct smc_cdc_tx_pend *pend)
79 sizeof(struct smc_cdc_msg) > SMC_WR_BUF_SIZE,
80 "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)");
82 offsetofend(struct smc_cdc_msg, reserved) > SMC_WR_TX_SIZE,
83 "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()");
85 sizeof(struct smc_cdc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
86 "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)");
88 pend->cursor = conn->tx_curs_sent;
89 pend->p_cursor = conn->local_tx_ctrl.prod;
90 pend->ctrl_seq = conn->tx_cdc_seq;
93 int smc_cdc_msg_send(struct smc_connection *conn,
94 struct smc_wr_buf *wr_buf,
95 struct smc_cdc_tx_pend *pend)
97 struct smc_link *link = conn->lnk;
98 union smc_host_cursor cfed;
101 smc_cdc_add_pending_send(conn, pend);
104 conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
105 smc_host_msg_to_cdc((struct smc_cdc_msg *)wr_buf, conn, &cfed);
106 rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
108 smc_curs_copy(&conn->rx_curs_confirmed, &cfed, conn);
109 conn->local_rx_ctrl.prod_flags.cons_curs_upd_req = 0;
112 conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
118 /* send a validation msg indicating the move of a conn to an other QP link */
119 int smcr_cdc_msg_send_validation(struct smc_connection *conn)
121 struct smc_host_cdc_msg *local = &conn->local_tx_ctrl;
122 struct smc_link *link = conn->lnk;
123 struct smc_cdc_tx_pend *pend;
124 struct smc_wr_buf *wr_buf;
125 struct smc_cdc_msg *peer;
128 rc = smc_cdc_get_free_slot(conn, link, &wr_buf, NULL, &pend);
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;
139 rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
143 static int smcr_cdc_get_slot_and_msg_send(struct smc_connection *conn)
145 struct smc_cdc_tx_pend *pend;
146 struct smc_wr_buf *wr_buf;
147 struct smc_link *link;
153 rc = smc_cdc_get_free_slot(conn, link, &wr_buf, NULL, &pend);
157 spin_lock_bh(&conn->send_lock);
158 if (link != conn->lnk) {
159 /* link of connection changed, try again one time*/
160 spin_unlock_bh(&conn->send_lock);
161 smc_wr_tx_put_slot(link,
162 (struct smc_wr_tx_pend_priv *)pend);
168 rc = smc_cdc_msg_send(conn, wr_buf, pend);
169 spin_unlock_bh(&conn->send_lock);
173 int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn)
177 if (!conn->lgr || (conn->lgr->is_smcd && conn->lgr->peer_shutdown))
180 if (conn->lgr->is_smcd) {
181 spin_lock_bh(&conn->send_lock);
182 rc = smcd_cdc_msg_send(conn);
183 spin_unlock_bh(&conn->send_lock);
185 rc = smcr_cdc_get_slot_and_msg_send(conn);
191 static bool smc_cdc_tx_filter(struct smc_wr_tx_pend_priv *tx_pend,
194 struct smc_connection *conn = (struct smc_connection *)data;
195 struct smc_cdc_tx_pend *cdc_pend =
196 (struct smc_cdc_tx_pend *)tx_pend;
198 return cdc_pend->conn == conn;
201 static void smc_cdc_tx_dismisser(struct smc_wr_tx_pend_priv *tx_pend)
203 struct smc_cdc_tx_pend *cdc_pend =
204 (struct smc_cdc_tx_pend *)tx_pend;
206 cdc_pend->conn = NULL;
209 void smc_cdc_tx_dismiss_slots(struct smc_connection *conn)
211 struct smc_link *link = conn->lnk;
213 smc_wr_tx_dismiss_slots(link, SMC_CDC_MSG_TYPE,
214 smc_cdc_tx_filter, smc_cdc_tx_dismisser,
215 (unsigned long)conn);
218 /* Send a SMC-D CDC header.
219 * This increments the free space available in our send buffer.
220 * Also update the confirmed receive buffer with what was sent to the peer.
222 int smcd_cdc_msg_send(struct smc_connection *conn)
224 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
225 union smc_host_cursor curs;
226 struct smcd_cdc_msg cdc;
229 memset(&cdc, 0, sizeof(cdc));
230 cdc.common.type = SMC_CDC_MSG_TYPE;
231 curs.acurs.counter = atomic64_read(&conn->local_tx_ctrl.prod.acurs);
232 cdc.prod.wrap = curs.wrap;
233 cdc.prod.count = curs.count;
234 curs.acurs.counter = atomic64_read(&conn->local_tx_ctrl.cons.acurs);
235 cdc.cons.wrap = curs.wrap;
236 cdc.cons.count = curs.count;
237 cdc.cons.prod_flags = conn->local_tx_ctrl.prod_flags;
238 cdc.cons.conn_state_flags = conn->local_tx_ctrl.conn_state_flags;
239 rc = smcd_tx_ism_write(conn, &cdc, sizeof(cdc), 0, 1);
242 smc_curs_copy(&conn->rx_curs_confirmed, &curs, conn);
243 conn->local_rx_ctrl.prod_flags.cons_curs_upd_req = 0;
244 /* Calculate transmitted data and increment free send buffer space */
245 diff = smc_curs_diff(conn->sndbuf_desc->len, &conn->tx_curs_fin,
246 &conn->tx_curs_sent);
247 /* increased by confirmed number of bytes */
248 smp_mb__before_atomic();
249 atomic_add(diff, &conn->sndbuf_space);
250 /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
251 smp_mb__after_atomic();
252 smc_curs_copy(&conn->tx_curs_fin, &conn->tx_curs_sent, conn);
254 smc_tx_sndbuf_nonfull(smc);
258 /********************************* receive ***********************************/
260 static inline bool smc_cdc_before(u16 seq1, u16 seq2)
262 return (s16)(seq1 - seq2) < 0;
265 static void smc_cdc_handle_urg_data_arrival(struct smc_sock *smc,
268 struct smc_connection *conn = &smc->conn;
271 /* new data included urgent business */
272 smc_curs_copy(&conn->urg_curs, &conn->local_rx_ctrl.prod, conn);
273 conn->urg_state = SMC_URG_VALID;
274 if (!sock_flag(&smc->sk, SOCK_URGINLINE))
275 /* we'll skip the urgent byte, so don't account for it */
277 base = (char *)conn->rmb_desc->cpu_addr + conn->rx_off;
278 if (conn->urg_curs.count)
279 conn->urg_rx_byte = *(base + conn->urg_curs.count - 1);
281 conn->urg_rx_byte = *(base + conn->rmb_desc->len - 1);
282 sk_send_sigurg(&smc->sk);
285 static void smc_cdc_msg_recv_action(struct smc_sock *smc,
286 struct smc_cdc_msg *cdc)
288 union smc_host_cursor cons_old, prod_old;
289 struct smc_connection *conn = &smc->conn;
290 int diff_cons, diff_prod;
292 smc_curs_copy(&prod_old, &conn->local_rx_ctrl.prod, conn);
293 smc_curs_copy(&cons_old, &conn->local_rx_ctrl.cons, conn);
294 smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);
296 diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
297 &conn->local_rx_ctrl.cons);
299 /* peer_rmbe_space is decreased during data transfer with RDMA
302 smp_mb__before_atomic();
303 atomic_add(diff_cons, &conn->peer_rmbe_space);
304 /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
305 smp_mb__after_atomic();
308 diff_prod = smc_curs_diff(conn->rmb_desc->len, &prod_old,
309 &conn->local_rx_ctrl.prod);
311 if (conn->local_rx_ctrl.prod_flags.urg_data_present)
312 smc_cdc_handle_urg_data_arrival(smc, &diff_prod);
313 /* bytes_to_rcv is decreased in smc_recvmsg */
314 smp_mb__before_atomic();
315 atomic_add(diff_prod, &conn->bytes_to_rcv);
316 /* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */
317 smp_mb__after_atomic();
318 smc->sk.sk_data_ready(&smc->sk);
320 if (conn->local_rx_ctrl.prod_flags.write_blocked)
321 smc->sk.sk_data_ready(&smc->sk);
322 if (conn->local_rx_ctrl.prod_flags.urg_data_pending)
323 conn->urg_state = SMC_URG_NOTYET;
326 /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */
327 if ((diff_cons && smc_tx_prepared_sends(conn)) ||
328 conn->local_rx_ctrl.prod_flags.cons_curs_upd_req ||
329 conn->local_rx_ctrl.prod_flags.urg_data_pending)
330 smc_tx_sndbuf_nonempty(conn);
332 if (diff_cons && conn->urg_tx_pend &&
333 atomic_read(&conn->peer_rmbe_space) == conn->peer_rmbe_size) {
334 /* urg data confirmed by peer, indicate we're ready for more */
335 conn->urg_tx_pend = false;
336 smc->sk.sk_write_space(&smc->sk);
339 if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
340 smc->sk.sk_err = ECONNRESET;
341 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
343 if (smc_cdc_rxed_any_close_or_senddone(conn)) {
344 smc->sk.sk_shutdown |= RCV_SHUTDOWN;
345 if (smc->clcsock && smc->clcsock->sk)
346 smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
347 sock_set_flag(&smc->sk, SOCK_DONE);
348 sock_hold(&smc->sk); /* sock_put in close_work */
349 if (!schedule_work(&conn->close_work))
354 /* called under tasklet context */
355 static void smc_cdc_msg_recv(struct smc_sock *smc, struct smc_cdc_msg *cdc)
358 bh_lock_sock(&smc->sk);
359 smc_cdc_msg_recv_action(smc, cdc);
360 bh_unlock_sock(&smc->sk);
361 sock_put(&smc->sk); /* no free sk in softirq-context */
364 /* Schedule a tasklet for this connection. Triggered from the ISM device IRQ
365 * handler to indicate update in the DMBE.
370 static void smcd_cdc_rx_tsklet(unsigned long data)
372 struct smc_connection *conn = (struct smc_connection *)data;
373 struct smcd_cdc_msg *data_cdc;
374 struct smcd_cdc_msg cdc;
375 struct smc_sock *smc;
377 if (!conn || conn->killed)
380 data_cdc = (struct smcd_cdc_msg *)conn->rmb_desc->cpu_addr;
381 smcd_curs_copy(&cdc.prod, &data_cdc->prod, conn);
382 smcd_curs_copy(&cdc.cons, &data_cdc->cons, conn);
383 smc = container_of(conn, struct smc_sock, conn);
384 smc_cdc_msg_recv(smc, (struct smc_cdc_msg *)&cdc);
387 /* Initialize receive tasklet. Called from ISM device IRQ handler to start
390 void smcd_cdc_rx_init(struct smc_connection *conn)
392 tasklet_init(&conn->rx_tsklet, smcd_cdc_rx_tsklet, (unsigned long)conn);
395 /***************************** init, exit, misc ******************************/
397 static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
399 struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
400 struct smc_cdc_msg *cdc = buf;
401 struct smc_connection *conn;
402 struct smc_link_group *lgr;
403 struct smc_sock *smc;
405 if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved))
406 return; /* short message */
407 if (cdc->len != SMC_WR_TX_SIZE)
408 return; /* invalid message */
410 /* lookup connection */
411 lgr = smc_get_lgr(link);
412 read_lock_bh(&lgr->conns_lock);
413 conn = smc_lgr_find_conn(ntohl(cdc->token), lgr);
414 read_unlock_bh(&lgr->conns_lock);
417 smc = container_of(conn, struct smc_sock, conn);
419 if (!cdc->prod_flags.failover_validation) {
420 if (smc_cdc_before(ntohs(cdc->seqno),
421 conn->local_rx_ctrl.seqno))
422 /* received seqno is old */
425 smc_cdc_msg_recv(smc, cdc);
428 static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = {
430 .handler = smc_cdc_rx_handler,
431 .type = SMC_CDC_MSG_TYPE
438 int __init smc_cdc_init(void)
440 struct smc_wr_rx_handler *handler;
443 for (handler = smc_cdc_rx_handlers; handler->handler; handler++) {
444 INIT_HLIST_NODE(&handler->list);
445 rc = smc_wr_rx_register_handler(handler);