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