xref: /openbmc/linux/net/smc/smc_cdc.c (revision 52c291a3)
1 /*
2  * Shared Memory Communications over RDMA (SMC-R) and RoCE
3  *
4  * Connection Data Control (CDC)
5  * handles flow control
6  *
7  * Copyright IBM Corp. 2016
8  *
9  * Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
10  */
11 
12 #include <linux/spinlock.h>
13 
14 #include "smc.h"
15 #include "smc_wr.h"
16 #include "smc_cdc.h"
17 #include "smc_tx.h"
18 #include "smc_rx.h"
19 #include "smc_close.h"
20 
21 /********************************** send *************************************/
22 
23 struct smc_cdc_tx_pend {
24 	struct smc_connection	*conn;		/* socket connection */
25 	union smc_host_cursor	cursor;	/* tx sndbuf cursor sent */
26 	union smc_host_cursor	p_cursor;	/* rx RMBE cursor produced */
27 	u16			ctrl_seq;	/* conn. tx sequence # */
28 };
29 
30 /* handler for send/transmission completion of a CDC msg */
31 static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd,
32 			       struct smc_link *link,
33 			       enum ib_wc_status wc_status)
34 {
35 	struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd;
36 	struct smc_sock *smc;
37 	int diff;
38 
39 	if (!cdcpend->conn)
40 		/* already dismissed */
41 		return;
42 
43 	smc = container_of(cdcpend->conn, struct smc_sock, conn);
44 	bh_lock_sock(&smc->sk);
45 	if (!wc_status) {
46 		diff = smc_curs_diff(cdcpend->conn->sndbuf_size,
47 				     &cdcpend->conn->tx_curs_fin,
48 				     &cdcpend->cursor);
49 		/* sndbuf_space is decreased in smc_sendmsg */
50 		smp_mb__before_atomic();
51 		atomic_add(diff, &cdcpend->conn->sndbuf_space);
52 		/* guarantee 0 <= sndbuf_space <= sndbuf_size */
53 		smp_mb__after_atomic();
54 		smc_curs_write(&cdcpend->conn->tx_curs_fin,
55 			       smc_curs_read(&cdcpend->cursor, cdcpend->conn),
56 			       cdcpend->conn);
57 	}
58 	smc_tx_sndbuf_nonfull(smc);
59 	if (smc->sk.sk_state != SMC_ACTIVE)
60 		/* wake up smc_close_wait_tx_pends() */
61 		smc->sk.sk_state_change(&smc->sk);
62 	bh_unlock_sock(&smc->sk);
63 }
64 
65 int smc_cdc_get_free_slot(struct smc_link *link,
66 			  struct smc_wr_buf **wr_buf,
67 			  struct smc_cdc_tx_pend **pend)
68 {
69 	return smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf,
70 				       (struct smc_wr_tx_pend_priv **)pend);
71 }
72 
73 static inline void smc_cdc_add_pending_send(struct smc_connection *conn,
74 					    struct smc_cdc_tx_pend *pend)
75 {
76 	BUILD_BUG_ON_MSG(
77 		sizeof(struct smc_cdc_msg) > SMC_WR_BUF_SIZE,
78 		"must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)");
79 	BUILD_BUG_ON_MSG(
80 		offsetof(struct smc_cdc_msg, reserved) > SMC_WR_TX_SIZE,
81 		"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()");
82 	BUILD_BUG_ON_MSG(
83 		sizeof(struct smc_cdc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
84 		"must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)");
85 	pend->conn = conn;
86 	pend->cursor = conn->tx_curs_sent;
87 	pend->p_cursor = conn->local_tx_ctrl.prod;
88 	pend->ctrl_seq = conn->tx_cdc_seq;
89 }
90 
91 int smc_cdc_msg_send(struct smc_connection *conn,
92 		     struct smc_wr_buf *wr_buf,
93 		     struct smc_cdc_tx_pend *pend)
94 {
95 	struct smc_link *link;
96 	int rc;
97 
98 	link = &conn->lgr->lnk[SMC_SINGLE_LINK];
99 
100 	smc_cdc_add_pending_send(conn, pend);
101 
102 	conn->tx_cdc_seq++;
103 	conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
104 	smc_host_msg_to_cdc((struct smc_cdc_msg *)wr_buf,
105 			    &conn->local_tx_ctrl, conn);
106 	rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
107 	if (!rc)
108 		smc_curs_write(&conn->rx_curs_confirmed,
109 			       smc_curs_read(&conn->local_tx_ctrl.cons, conn),
110 			       conn);
111 
112 	return rc;
113 }
114 
115 int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn)
116 {
117 	struct smc_cdc_tx_pend *pend;
118 	struct smc_wr_buf *wr_buf;
119 	int rc;
120 
121 	rc = smc_cdc_get_free_slot(&conn->lgr->lnk[SMC_SINGLE_LINK], &wr_buf,
122 				   &pend);
123 	if (rc)
124 		return rc;
125 
126 	return smc_cdc_msg_send(conn, wr_buf, pend);
127 }
128 
129 static bool smc_cdc_tx_filter(struct smc_wr_tx_pend_priv *tx_pend,
130 			      unsigned long data)
131 {
132 	struct smc_connection *conn = (struct smc_connection *)data;
133 	struct smc_cdc_tx_pend *cdc_pend =
134 		(struct smc_cdc_tx_pend *)tx_pend;
135 
136 	return cdc_pend->conn == conn;
137 }
138 
139 static void smc_cdc_tx_dismisser(struct smc_wr_tx_pend_priv *tx_pend)
140 {
141 	struct smc_cdc_tx_pend *cdc_pend =
142 		(struct smc_cdc_tx_pend *)tx_pend;
143 
144 	cdc_pend->conn = NULL;
145 }
146 
147 void smc_cdc_tx_dismiss_slots(struct smc_connection *conn)
148 {
149 	struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
150 
151 	smc_wr_tx_dismiss_slots(link, SMC_CDC_MSG_TYPE,
152 				smc_cdc_tx_filter, smc_cdc_tx_dismisser,
153 				(unsigned long)conn);
154 }
155 
156 bool smc_cdc_tx_has_pending(struct smc_connection *conn)
157 {
158 	struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
159 
160 	return smc_wr_tx_has_pending(link, SMC_CDC_MSG_TYPE,
161 				     smc_cdc_tx_filter, (unsigned long)conn);
162 }
163 
164 /********************************* receive ***********************************/
165 
166 static inline bool smc_cdc_before(u16 seq1, u16 seq2)
167 {
168 	return (s16)(seq1 - seq2) < 0;
169 }
170 
171 static void smc_cdc_msg_recv_action(struct smc_sock *smc,
172 				    struct smc_link *link,
173 				    struct smc_cdc_msg *cdc)
174 {
175 	union smc_host_cursor cons_old, prod_old;
176 	struct smc_connection *conn = &smc->conn;
177 	int diff_cons, diff_prod;
178 
179 	if (!cdc->prod_flags.failover_validation) {
180 		if (smc_cdc_before(ntohs(cdc->seqno),
181 				   conn->local_rx_ctrl.seqno))
182 			/* received seqno is old */
183 			return;
184 	}
185 	smc_curs_write(&prod_old,
186 		       smc_curs_read(&conn->local_rx_ctrl.prod, conn),
187 		       conn);
188 	smc_curs_write(&cons_old,
189 		       smc_curs_read(&conn->local_rx_ctrl.cons, conn),
190 		       conn);
191 	smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);
192 
193 	diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
194 				  &conn->local_rx_ctrl.cons);
195 	if (diff_cons) {
196 		/* peer_rmbe_space is decreased during data transfer with RDMA
197 		 * write
198 		 */
199 		smp_mb__before_atomic();
200 		atomic_add(diff_cons, &conn->peer_rmbe_space);
201 		/* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
202 		smp_mb__after_atomic();
203 	}
204 
205 	diff_prod = smc_curs_diff(conn->rmbe_size, &prod_old,
206 				  &conn->local_rx_ctrl.prod);
207 	if (diff_prod) {
208 		/* bytes_to_rcv is decreased in smc_recvmsg */
209 		smp_mb__before_atomic();
210 		atomic_add(diff_prod, &conn->bytes_to_rcv);
211 		/* guarantee 0 <= bytes_to_rcv <= rmbe_size */
212 		smp_mb__after_atomic();
213 		smc->sk.sk_data_ready(&smc->sk);
214 	}
215 
216 	if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
217 		smc->sk.sk_err = ECONNRESET;
218 		conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
219 	}
220 	if (smc_cdc_rxed_any_close_or_senddone(conn)) {
221 		smc->sk.sk_shutdown |= RCV_SHUTDOWN;
222 		if (smc->clcsock && smc->clcsock->sk)
223 			smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
224 		sock_set_flag(&smc->sk, SOCK_DONE);
225 		schedule_work(&conn->close_work);
226 	}
227 
228 	/* piggy backed tx info */
229 	/* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */
230 	if (diff_cons && smc_tx_prepared_sends(conn)) {
231 		smc_tx_sndbuf_nonempty(conn);
232 		/* trigger socket release if connection closed */
233 		smc_close_wake_tx_prepared(smc);
234 	}
235 
236 	/* socket connected but not accepted */
237 	if (!smc->sk.sk_socket)
238 		return;
239 
240 	/* data available */
241 	if ((conn->local_rx_ctrl.prod_flags.write_blocked) ||
242 	    (conn->local_rx_ctrl.prod_flags.cons_curs_upd_req))
243 		smc_tx_consumer_update(conn);
244 }
245 
246 /* called under tasklet context */
247 static inline void smc_cdc_msg_recv(struct smc_cdc_msg *cdc,
248 				    struct smc_link *link, u64 wr_id)
249 {
250 	struct smc_link_group *lgr = container_of(link, struct smc_link_group,
251 						  lnk[SMC_SINGLE_LINK]);
252 	struct smc_connection *connection;
253 	struct smc_sock *smc;
254 
255 	/* lookup connection */
256 	read_lock_bh(&lgr->conns_lock);
257 	connection = smc_lgr_find_conn(ntohl(cdc->token), lgr);
258 	if (!connection) {
259 		read_unlock_bh(&lgr->conns_lock);
260 		return;
261 	}
262 	smc = container_of(connection, struct smc_sock, conn);
263 	sock_hold(&smc->sk);
264 	read_unlock_bh(&lgr->conns_lock);
265 	bh_lock_sock(&smc->sk);
266 	smc_cdc_msg_recv_action(smc, link, cdc);
267 	bh_unlock_sock(&smc->sk);
268 	sock_put(&smc->sk); /* no free sk in softirq-context */
269 }
270 
271 /***************************** init, exit, misc ******************************/
272 
273 static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
274 {
275 	struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
276 	struct smc_cdc_msg *cdc = buf;
277 
278 	if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved))
279 		return; /* short message */
280 	if (cdc->len != sizeof(*cdc))
281 		return; /* invalid message */
282 	smc_cdc_msg_recv(cdc, link, wc->wr_id);
283 }
284 
285 static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = {
286 	{
287 		.handler	= smc_cdc_rx_handler,
288 		.type		= SMC_CDC_MSG_TYPE
289 	},
290 	{
291 		.handler	= NULL,
292 	}
293 };
294 
295 int __init smc_cdc_init(void)
296 {
297 	struct smc_wr_rx_handler *handler;
298 	int rc = 0;
299 
300 	for (handler = smc_cdc_rx_handlers; handler->handler; handler++) {
301 		INIT_HLIST_NODE(&handler->list);
302 		rc = smc_wr_rx_register_handler(handler);
303 		if (rc)
304 			break;
305 	}
306 	return rc;
307 }
308