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