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