Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
5f08318f UB |
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" | |
e6727f39 | 18 | #include "smc_tx.h" |
952310cc | 19 | #include "smc_rx.h" |
b38d7324 | 20 | #include "smc_close.h" |
5f08318f UB |
21 | |
22 | /********************************** send *************************************/ | |
23 | ||
24 | struct smc_cdc_tx_pend { | |
25 | struct smc_connection *conn; /* socket connection */ | |
26 | union smc_host_cursor cursor; /* tx sndbuf cursor sent */ | |
27 | union smc_host_cursor p_cursor; /* rx RMBE cursor produced */ | |
28 | u16 ctrl_seq; /* conn. tx sequence # */ | |
29 | }; | |
30 | ||
31 | /* handler for send/transmission completion of a CDC msg */ | |
32 | static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd, | |
33 | struct smc_link *link, | |
34 | enum ib_wc_status wc_status) | |
35 | { | |
36 | struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd; | |
37 | struct smc_sock *smc; | |
38 | int diff; | |
39 | ||
40 | if (!cdcpend->conn) | |
41 | /* already dismissed */ | |
42 | return; | |
43 | ||
44 | smc = container_of(cdcpend->conn, struct smc_sock, conn); | |
45 | bh_lock_sock(&smc->sk); | |
46 | if (!wc_status) { | |
69cb7dc0 | 47 | diff = smc_curs_diff(cdcpend->conn->sndbuf_desc->len, |
5f08318f UB |
48 | &cdcpend->conn->tx_curs_fin, |
49 | &cdcpend->cursor); | |
50 | /* sndbuf_space is decreased in smc_sendmsg */ | |
51 | smp_mb__before_atomic(); | |
52 | atomic_add(diff, &cdcpend->conn->sndbuf_space); | |
69cb7dc0 | 53 | /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */ |
5f08318f UB |
54 | smp_mb__after_atomic(); |
55 | smc_curs_write(&cdcpend->conn->tx_curs_fin, | |
56 | smc_curs_read(&cdcpend->cursor, cdcpend->conn), | |
57 | cdcpend->conn); | |
58 | } | |
e6727f39 | 59 | smc_tx_sndbuf_nonfull(smc); |
5f08318f UB |
60 | bh_unlock_sock(&smc->sk); |
61 | } | |
62 | ||
51957bc5 | 63 | int smc_cdc_get_free_slot(struct smc_connection *conn, |
5f08318f UB |
64 | struct smc_wr_buf **wr_buf, |
65 | struct smc_cdc_tx_pend **pend) | |
66 | { | |
51957bc5 | 67 | struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK]; |
1a0a04c7 | 68 | int rc; |
51957bc5 | 69 | |
1a0a04c7 UB |
70 | rc = smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf, |
71 | (struct smc_wr_tx_pend_priv **)pend); | |
72 | if (!conn->alert_token_local) | |
73 | /* abnormal termination */ | |
74 | rc = -EPIPE; | |
75 | return rc; | |
5f08318f UB |
76 | } |
77 | ||
78 | static inline void smc_cdc_add_pending_send(struct smc_connection *conn, | |
79 | struct smc_cdc_tx_pend *pend) | |
80 | { | |
81 | BUILD_BUG_ON_MSG( | |
82 | sizeof(struct smc_cdc_msg) > SMC_WR_BUF_SIZE, | |
83 | "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)"); | |
84 | BUILD_BUG_ON_MSG( | |
33825761 | 85 | sizeof(struct smc_cdc_msg) != SMC_WR_TX_SIZE, |
5f08318f UB |
86 | "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()"); |
87 | BUILD_BUG_ON_MSG( | |
88 | sizeof(struct smc_cdc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE, | |
89 | "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)"); | |
90 | pend->conn = conn; | |
91 | pend->cursor = conn->tx_curs_sent; | |
92 | pend->p_cursor = conn->local_tx_ctrl.prod; | |
93 | pend->ctrl_seq = conn->tx_cdc_seq; | |
94 | } | |
95 | ||
96 | int smc_cdc_msg_send(struct smc_connection *conn, | |
97 | struct smc_wr_buf *wr_buf, | |
98 | struct smc_cdc_tx_pend *pend) | |
99 | { | |
100 | struct smc_link *link; | |
101 | int rc; | |
102 | ||
103 | link = &conn->lgr->lnk[SMC_SINGLE_LINK]; | |
104 | ||
105 | smc_cdc_add_pending_send(conn, pend); | |
106 | ||
107 | conn->tx_cdc_seq++; | |
108 | conn->local_tx_ctrl.seqno = conn->tx_cdc_seq; | |
109 | smc_host_msg_to_cdc((struct smc_cdc_msg *)wr_buf, | |
110 | &conn->local_tx_ctrl, conn); | |
111 | rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend); | |
112 | if (!rc) | |
113 | smc_curs_write(&conn->rx_curs_confirmed, | |
114 | smc_curs_read(&conn->local_tx_ctrl.cons, conn), | |
115 | conn); | |
116 | ||
117 | return rc; | |
118 | } | |
119 | ||
120 | int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn) | |
121 | { | |
122 | struct smc_cdc_tx_pend *pend; | |
123 | struct smc_wr_buf *wr_buf; | |
124 | int rc; | |
125 | ||
51957bc5 | 126 | rc = smc_cdc_get_free_slot(conn, &wr_buf, &pend); |
5f08318f UB |
127 | if (rc) |
128 | return rc; | |
129 | ||
130 | return smc_cdc_msg_send(conn, wr_buf, pend); | |
131 | } | |
132 | ||
133 | static bool smc_cdc_tx_filter(struct smc_wr_tx_pend_priv *tx_pend, | |
134 | unsigned long data) | |
135 | { | |
136 | struct smc_connection *conn = (struct smc_connection *)data; | |
137 | struct smc_cdc_tx_pend *cdc_pend = | |
138 | (struct smc_cdc_tx_pend *)tx_pend; | |
139 | ||
140 | return cdc_pend->conn == conn; | |
141 | } | |
142 | ||
143 | static void smc_cdc_tx_dismisser(struct smc_wr_tx_pend_priv *tx_pend) | |
144 | { | |
145 | struct smc_cdc_tx_pend *cdc_pend = | |
146 | (struct smc_cdc_tx_pend *)tx_pend; | |
147 | ||
148 | cdc_pend->conn = NULL; | |
149 | } | |
150 | ||
151 | void smc_cdc_tx_dismiss_slots(struct smc_connection *conn) | |
152 | { | |
153 | struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK]; | |
154 | ||
155 | smc_wr_tx_dismiss_slots(link, SMC_CDC_MSG_TYPE, | |
156 | smc_cdc_tx_filter, smc_cdc_tx_dismisser, | |
157 | (unsigned long)conn); | |
158 | } | |
159 | ||
160 | /********************************* receive ***********************************/ | |
161 | ||
162 | static inline bool smc_cdc_before(u16 seq1, u16 seq2) | |
163 | { | |
164 | return (s16)(seq1 - seq2) < 0; | |
165 | } | |
166 | ||
de8474eb SR |
167 | static void smc_cdc_handle_urg_data_arrival(struct smc_sock *smc, |
168 | int *diff_prod) | |
169 | { | |
170 | struct smc_connection *conn = &smc->conn; | |
171 | char *base; | |
172 | ||
173 | /* new data included urgent business */ | |
174 | smc_curs_write(&conn->urg_curs, | |
175 | smc_curs_read(&conn->local_rx_ctrl.prod, conn), | |
176 | conn); | |
177 | conn->urg_state = SMC_URG_VALID; | |
178 | if (!sock_flag(&smc->sk, SOCK_URGINLINE)) | |
179 | /* we'll skip the urgent byte, so don't account for it */ | |
180 | (*diff_prod)--; | |
181 | base = (char *)conn->rmb_desc->cpu_addr; | |
182 | if (conn->urg_curs.count) | |
183 | conn->urg_rx_byte = *(base + conn->urg_curs.count - 1); | |
184 | else | |
185 | conn->urg_rx_byte = *(base + conn->rmb_desc->len - 1); | |
186 | sk_send_sigurg(&smc->sk); | |
187 | } | |
188 | ||
5f08318f | 189 | static void smc_cdc_msg_recv_action(struct smc_sock *smc, |
5f08318f UB |
190 | struct smc_cdc_msg *cdc) |
191 | { | |
192 | union smc_host_cursor cons_old, prod_old; | |
193 | struct smc_connection *conn = &smc->conn; | |
194 | int diff_cons, diff_prod; | |
195 | ||
5f08318f UB |
196 | smc_curs_write(&prod_old, |
197 | smc_curs_read(&conn->local_rx_ctrl.prod, conn), | |
198 | conn); | |
199 | smc_curs_write(&cons_old, | |
200 | smc_curs_read(&conn->local_rx_ctrl.cons, conn), | |
201 | conn); | |
202 | smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn); | |
203 | ||
204 | diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old, | |
205 | &conn->local_rx_ctrl.cons); | |
206 | if (diff_cons) { | |
207 | /* peer_rmbe_space is decreased during data transfer with RDMA | |
208 | * write | |
209 | */ | |
210 | smp_mb__before_atomic(); | |
211 | atomic_add(diff_cons, &conn->peer_rmbe_space); | |
212 | /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */ | |
213 | smp_mb__after_atomic(); | |
214 | } | |
215 | ||
69cb7dc0 | 216 | diff_prod = smc_curs_diff(conn->rmb_desc->len, &prod_old, |
5f08318f UB |
217 | &conn->local_rx_ctrl.prod); |
218 | if (diff_prod) { | |
de8474eb SR |
219 | if (conn->local_rx_ctrl.prod_flags.urg_data_present) |
220 | smc_cdc_handle_urg_data_arrival(smc, &diff_prod); | |
5f08318f UB |
221 | /* bytes_to_rcv is decreased in smc_recvmsg */ |
222 | smp_mb__before_atomic(); | |
223 | atomic_add(diff_prod, &conn->bytes_to_rcv); | |
69cb7dc0 | 224 | /* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */ |
5f08318f | 225 | smp_mb__after_atomic(); |
952310cc | 226 | smc->sk.sk_data_ready(&smc->sk); |
de8474eb SR |
227 | } else { |
228 | if (conn->local_rx_ctrl.prod_flags.write_blocked || | |
229 | conn->local_rx_ctrl.prod_flags.cons_curs_upd_req || | |
230 | conn->local_rx_ctrl.prod_flags.urg_data_pending) { | |
231 | if (conn->local_rx_ctrl.prod_flags.urg_data_pending) | |
232 | conn->urg_state = SMC_URG_NOTYET; | |
233 | /* force immediate tx of current consumer cursor, but | |
234 | * under send_lock to guarantee arrival in seqno-order | |
235 | */ | |
5607016c UB |
236 | if (smc->sk.sk_state != SMC_INIT) |
237 | smc_tx_sndbuf_nonempty(conn); | |
de8474eb | 238 | } |
5f08318f UB |
239 | } |
240 | ||
51f1de79 UB |
241 | /* piggy backed tx info */ |
242 | /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */ | |
243 | if (diff_cons && smc_tx_prepared_sends(conn)) { | |
244 | smc_tx_sndbuf_nonempty(conn); | |
245 | /* trigger socket release if connection closed */ | |
246 | smc_close_wake_tx_prepared(smc); | |
247 | } | |
de8474eb SR |
248 | if (diff_cons && conn->urg_tx_pend && |
249 | atomic_read(&conn->peer_rmbe_space) == conn->peer_rmbe_size) { | |
250 | /* urg data confirmed by peer, indicate we're ready for more */ | |
251 | conn->urg_tx_pend = false; | |
252 | smc->sk.sk_write_space(&smc->sk); | |
253 | } | |
51f1de79 | 254 | |
b38d7324 | 255 | if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) { |
5f08318f | 256 | smc->sk.sk_err = ECONNRESET; |
b38d7324 UB |
257 | conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1; |
258 | } | |
46c28dbd UB |
259 | if (smc_cdc_rxed_any_close_or_senddone(conn)) { |
260 | smc->sk.sk_shutdown |= RCV_SHUTDOWN; | |
261 | if (smc->clcsock && smc->clcsock->sk) | |
262 | smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN; | |
263 | sock_set_flag(&smc->sk, SOCK_DONE); | |
51f1de79 UB |
264 | sock_hold(&smc->sk); /* sock_put in close_work */ |
265 | if (!schedule_work(&conn->close_work)) | |
266 | sock_put(&smc->sk); | |
b38d7324 | 267 | } |
5f08318f UB |
268 | } |
269 | ||
270 | /* called under tasklet context */ | |
d7b0e37c | 271 | static void smc_cdc_msg_recv(struct smc_sock *smc, struct smc_cdc_msg *cdc) |
5f08318f | 272 | { |
5f08318f | 273 | sock_hold(&smc->sk); |
5f08318f | 274 | bh_lock_sock(&smc->sk); |
d7b0e37c | 275 | smc_cdc_msg_recv_action(smc, cdc); |
5f08318f UB |
276 | bh_unlock_sock(&smc->sk); |
277 | sock_put(&smc->sk); /* no free sk in softirq-context */ | |
278 | } | |
279 | ||
280 | /***************************** init, exit, misc ******************************/ | |
281 | ||
282 | static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf) | |
283 | { | |
284 | struct smc_link *link = (struct smc_link *)wc->qp->qp_context; | |
285 | struct smc_cdc_msg *cdc = buf; | |
d7b0e37c HW |
286 | struct smc_connection *conn; |
287 | struct smc_link_group *lgr; | |
288 | struct smc_sock *smc; | |
5f08318f UB |
289 | |
290 | if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved)) | |
291 | return; /* short message */ | |
cbba07a7 | 292 | if (cdc->len != SMC_WR_TX_SIZE) |
5f08318f | 293 | return; /* invalid message */ |
d7b0e37c HW |
294 | |
295 | /* lookup connection */ | |
296 | lgr = container_of(link, struct smc_link_group, lnk[SMC_SINGLE_LINK]); | |
297 | read_lock_bh(&lgr->conns_lock); | |
298 | conn = smc_lgr_find_conn(ntohl(cdc->token), lgr); | |
299 | read_unlock_bh(&lgr->conns_lock); | |
300 | if (!conn) | |
301 | return; | |
302 | smc = container_of(conn, struct smc_sock, conn); | |
303 | ||
304 | if (!cdc->prod_flags.failover_validation) { | |
305 | if (smc_cdc_before(ntohs(cdc->seqno), | |
306 | conn->local_rx_ctrl.seqno)) | |
307 | /* received seqno is old */ | |
308 | return; | |
309 | } | |
310 | smc_cdc_msg_recv(smc, cdc); | |
5f08318f UB |
311 | } |
312 | ||
313 | static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = { | |
314 | { | |
315 | .handler = smc_cdc_rx_handler, | |
316 | .type = SMC_CDC_MSG_TYPE | |
317 | }, | |
318 | { | |
319 | .handler = NULL, | |
320 | } | |
321 | }; | |
322 | ||
323 | int __init smc_cdc_init(void) | |
324 | { | |
325 | struct smc_wr_rx_handler *handler; | |
326 | int rc = 0; | |
327 | ||
328 | for (handler = smc_cdc_rx_handlers; handler->handler; handler++) { | |
329 | INIT_HLIST_NODE(&handler->list); | |
330 | rc = smc_wr_rx_register_handler(handler); | |
331 | if (rc) | |
332 | break; | |
333 | } | |
334 | return rc; | |
335 | } |