blob: b80ef104ab4e525a9cb1a0a5bf37e9ae0d53439f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ursula Braun5f083182017-01-09 16:55:22 +01002/*
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"
Ursula Braune6727f32017-01-09 16:55:23 +010018#include "smc_tx.h"
Ursula Braun952310c2017-01-09 16:55:24 +010019#include "smc_rx.h"
Ursula Braunb38d7322017-01-09 16:55:25 +010020#include "smc_close.h"
Ursula Braun5f083182017-01-09 16:55:22 +010021
22/********************************** send *************************************/
23
Ursula Braun5f083182017-01-09 16:55:22 +010024/* handler for send/transmission completion of a CDC msg */
25static 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;
Stefan Rasplbac6de72018-07-23 13:53:09 +020030 struct smc_connection *conn = cdcpend->conn;
Ursula Braun5f083182017-01-09 16:55:22 +010031 struct smc_sock *smc;
32 int diff;
33
Stefan Rasplbac6de72018-07-23 13:53:09 +020034 if (!conn)
Ursula Braun5f083182017-01-09 16:55:22 +010035 /* already dismissed */
36 return;
37
Stefan Rasplbac6de72018-07-23 13:53:09 +020038 smc = container_of(conn, struct smc_sock, conn);
Ursula Braun5f083182017-01-09 16:55:22 +010039 bh_lock_sock(&smc->sk);
40 if (!wc_status) {
Hans Wippel69cb7dc2018-05-18 09:34:10 +020041 diff = smc_curs_diff(cdcpend->conn->sndbuf_desc->len,
Ursula Braun5f083182017-01-09 16:55:22 +010042 &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);
Hans Wippel69cb7dc2018-05-18 09:34:10 +020047 /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
Ursula Braun5f083182017-01-09 16:55:22 +010048 smp_mb__after_atomic();
Stefan Rasplbac6de72018-07-23 13:53:09 +020049 smc_curs_copy(&conn->tx_curs_fin, &cdcpend->cursor, conn);
Ursula Braun5f083182017-01-09 16:55:22 +010050 }
Ursula Braune6727f32017-01-09 16:55:23 +010051 smc_tx_sndbuf_nonfull(smc);
Ursula Braun5f083182017-01-09 16:55:22 +010052 bh_unlock_sock(&smc->sk);
53}
54
Ursula Braun51957bc2017-09-21 09:17:34 +020055int smc_cdc_get_free_slot(struct smc_connection *conn,
Ursula Braun5f083182017-01-09 16:55:22 +010056 struct smc_wr_buf **wr_buf,
Ursula Braunad6f3172019-02-04 13:44:44 +010057 struct smc_rdma_wr **wr_rdma_buf,
Ursula Braun5f083182017-01-09 16:55:22 +010058 struct smc_cdc_tx_pend **pend)
59{
Ursula Braun51957bc2017-09-21 09:17:34 +020060 struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun1a0a04c2018-01-25 11:15:36 +010061 int rc;
Ursula Braun51957bc2017-09-21 09:17:34 +020062
Ursula Braun1a0a04c2018-01-25 11:15:36 +010063 rc = smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf,
Ursula Braunad6f3172019-02-04 13:44:44 +010064 wr_rdma_buf,
Ursula Braun1a0a04c2018-01-25 11:15:36 +010065 (struct smc_wr_tx_pend_priv **)pend);
66 if (!conn->alert_token_local)
67 /* abnormal termination */
68 rc = -EPIPE;
69 return rc;
Ursula Braun5f083182017-01-09 16:55:22 +010070}
71
72static inline void smc_cdc_add_pending_send(struct smc_connection *conn,
73 struct smc_cdc_tx_pend *pend)
74{
75 BUILD_BUG_ON_MSG(
76 sizeof(struct smc_cdc_msg) > SMC_WR_BUF_SIZE,
77 "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)");
78 BUILD_BUG_ON_MSG(
Ursula Braunb9a22dd2018-11-20 16:46:42 +010079 offsetofend(struct smc_cdc_msg, reserved) > SMC_WR_TX_SIZE,
Ursula Braun5f083182017-01-09 16:55:22 +010080 "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()");
81 BUILD_BUG_ON_MSG(
82 sizeof(struct smc_cdc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
83 "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)");
84 pend->conn = conn;
85 pend->cursor = conn->tx_curs_sent;
86 pend->p_cursor = conn->local_tx_ctrl.prod;
87 pend->ctrl_seq = conn->tx_cdc_seq;
88}
89
90int smc_cdc_msg_send(struct smc_connection *conn,
91 struct smc_wr_buf *wr_buf,
92 struct smc_cdc_tx_pend *pend)
93{
94 struct smc_link *link;
95 int rc;
96
97 link = &conn->lgr->lnk[SMC_SINGLE_LINK];
98
99 smc_cdc_add_pending_send(conn, pend);
100
101 conn->tx_cdc_seq++;
102 conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
103 smc_host_msg_to_cdc((struct smc_cdc_msg *)wr_buf,
104 &conn->local_tx_ctrl, conn);
105 rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
106 if (!rc)
Stefan Rasplbac6de72018-07-23 13:53:09 +0200107 smc_curs_copy(&conn->rx_curs_confirmed,
108 &conn->local_tx_ctrl.cons, conn);
Ursula Braun5f083182017-01-09 16:55:22 +0100109
110 return rc;
111}
112
Hans Wippelbe244f22018-06-28 19:05:10 +0200113static int smcr_cdc_get_slot_and_msg_send(struct smc_connection *conn)
Ursula Braun5f083182017-01-09 16:55:22 +0100114{
115 struct smc_cdc_tx_pend *pend;
116 struct smc_wr_buf *wr_buf;
117 int rc;
118
Ursula Braunad6f3172019-02-04 13:44:44 +0100119 rc = smc_cdc_get_free_slot(conn, &wr_buf, NULL, &pend);
Ursula Braun5f083182017-01-09 16:55:22 +0100120 if (rc)
121 return rc;
122
Karsten Graul2dee25a2019-01-30 18:51:06 +0100123 spin_lock_bh(&conn->send_lock);
124 rc = smc_cdc_msg_send(conn, wr_buf, pend);
125 spin_unlock_bh(&conn->send_lock);
126 return rc;
Ursula Braun5f083182017-01-09 16:55:22 +0100127}
128
Hans Wippelbe244f22018-06-28 19:05:10 +0200129int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn)
130{
131 int rc;
132
133 if (conn->lgr->is_smcd) {
134 spin_lock_bh(&conn->send_lock);
135 rc = smcd_cdc_msg_send(conn);
136 spin_unlock_bh(&conn->send_lock);
137 } else {
138 rc = smcr_cdc_get_slot_and_msg_send(conn);
139 }
140
141 return rc;
142}
143
Ursula Braun5f083182017-01-09 16:55:22 +0100144static bool smc_cdc_tx_filter(struct smc_wr_tx_pend_priv *tx_pend,
145 unsigned long data)
146{
147 struct smc_connection *conn = (struct smc_connection *)data;
148 struct smc_cdc_tx_pend *cdc_pend =
149 (struct smc_cdc_tx_pend *)tx_pend;
150
151 return cdc_pend->conn == conn;
152}
153
154static void smc_cdc_tx_dismisser(struct smc_wr_tx_pend_priv *tx_pend)
155{
156 struct smc_cdc_tx_pend *cdc_pend =
157 (struct smc_cdc_tx_pend *)tx_pend;
158
159 cdc_pend->conn = NULL;
160}
161
162void smc_cdc_tx_dismiss_slots(struct smc_connection *conn)
163{
164 struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
165
166 smc_wr_tx_dismiss_slots(link, SMC_CDC_MSG_TYPE,
167 smc_cdc_tx_filter, smc_cdc_tx_dismisser,
168 (unsigned long)conn);
169}
170
Hans Wippelbe244f22018-06-28 19:05:10 +0200171/* Send a SMC-D CDC header.
172 * This increments the free space available in our send buffer.
173 * Also update the confirmed receive buffer with what was sent to the peer.
174 */
175int smcd_cdc_msg_send(struct smc_connection *conn)
176{
177 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
Ursula Braunb9a22dd2018-11-20 16:46:42 +0100178 union smc_host_cursor curs;
Hans Wippelbe244f22018-06-28 19:05:10 +0200179 struct smcd_cdc_msg cdc;
180 int rc, diff;
181
182 memset(&cdc, 0, sizeof(cdc));
183 cdc.common.type = SMC_CDC_MSG_TYPE;
Ursula Braunb9a22dd2018-11-20 16:46:42 +0100184 curs.acurs.counter = atomic64_read(&conn->local_tx_ctrl.prod.acurs);
185 cdc.prod.wrap = curs.wrap;
186 cdc.prod.count = curs.count;
187 curs.acurs.counter = atomic64_read(&conn->local_tx_ctrl.cons.acurs);
188 cdc.cons.wrap = curs.wrap;
189 cdc.cons.count = curs.count;
190 cdc.cons.prod_flags = conn->local_tx_ctrl.prod_flags;
191 cdc.cons.conn_state_flags = conn->local_tx_ctrl.conn_state_flags;
Hans Wippelbe244f22018-06-28 19:05:10 +0200192 rc = smcd_tx_ism_write(conn, &cdc, sizeof(cdc), 0, 1);
193 if (rc)
194 return rc;
Ursula Braunb9a22dd2018-11-20 16:46:42 +0100195 smc_curs_copy(&conn->rx_curs_confirmed, &curs, conn);
Hans Wippelbe244f22018-06-28 19:05:10 +0200196 /* Calculate transmitted data and increment free send buffer space */
197 diff = smc_curs_diff(conn->sndbuf_desc->len, &conn->tx_curs_fin,
198 &conn->tx_curs_sent);
199 /* increased by confirmed number of bytes */
200 smp_mb__before_atomic();
201 atomic_add(diff, &conn->sndbuf_space);
202 /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
203 smp_mb__after_atomic();
Stefan Rasplbac6de72018-07-23 13:53:09 +0200204 smc_curs_copy(&conn->tx_curs_fin, &conn->tx_curs_sent, conn);
Hans Wippelbe244f22018-06-28 19:05:10 +0200205
206 smc_tx_sndbuf_nonfull(smc);
207 return rc;
208}
209
Ursula Braun5f083182017-01-09 16:55:22 +0100210/********************************* receive ***********************************/
211
212static inline bool smc_cdc_before(u16 seq1, u16 seq2)
213{
214 return (s16)(seq1 - seq2) < 0;
215}
216
Stefan Rasplde8474e2018-05-23 16:38:11 +0200217static void smc_cdc_handle_urg_data_arrival(struct smc_sock *smc,
218 int *diff_prod)
219{
220 struct smc_connection *conn = &smc->conn;
221 char *base;
222
223 /* new data included urgent business */
Stefan Rasplbac6de72018-07-23 13:53:09 +0200224 smc_curs_copy(&conn->urg_curs, &conn->local_rx_ctrl.prod, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +0200225 conn->urg_state = SMC_URG_VALID;
226 if (!sock_flag(&smc->sk, SOCK_URGINLINE))
227 /* we'll skip the urgent byte, so don't account for it */
228 (*diff_prod)--;
Hans Wippelbe244f22018-06-28 19:05:10 +0200229 base = (char *)conn->rmb_desc->cpu_addr + conn->rx_off;
Stefan Rasplde8474e2018-05-23 16:38:11 +0200230 if (conn->urg_curs.count)
231 conn->urg_rx_byte = *(base + conn->urg_curs.count - 1);
232 else
233 conn->urg_rx_byte = *(base + conn->rmb_desc->len - 1);
234 sk_send_sigurg(&smc->sk);
235}
236
Ursula Braun5f083182017-01-09 16:55:22 +0100237static void smc_cdc_msg_recv_action(struct smc_sock *smc,
Ursula Braun5f083182017-01-09 16:55:22 +0100238 struct smc_cdc_msg *cdc)
239{
240 union smc_host_cursor cons_old, prod_old;
241 struct smc_connection *conn = &smc->conn;
242 int diff_cons, diff_prod;
243
Stefan Rasplbac6de72018-07-23 13:53:09 +0200244 smc_curs_copy(&prod_old, &conn->local_rx_ctrl.prod, conn);
245 smc_curs_copy(&cons_old, &conn->local_rx_ctrl.cons, conn);
Ursula Braun5f083182017-01-09 16:55:22 +0100246 smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);
247
248 diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
249 &conn->local_rx_ctrl.cons);
250 if (diff_cons) {
251 /* peer_rmbe_space is decreased during data transfer with RDMA
252 * write
253 */
254 smp_mb__before_atomic();
255 atomic_add(diff_cons, &conn->peer_rmbe_space);
256 /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
257 smp_mb__after_atomic();
258 }
259
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200260 diff_prod = smc_curs_diff(conn->rmb_desc->len, &prod_old,
Ursula Braun5f083182017-01-09 16:55:22 +0100261 &conn->local_rx_ctrl.prod);
262 if (diff_prod) {
Stefan Rasplde8474e2018-05-23 16:38:11 +0200263 if (conn->local_rx_ctrl.prod_flags.urg_data_present)
264 smc_cdc_handle_urg_data_arrival(smc, &diff_prod);
Ursula Braun5f083182017-01-09 16:55:22 +0100265 /* bytes_to_rcv is decreased in smc_recvmsg */
266 smp_mb__before_atomic();
267 atomic_add(diff_prod, &conn->bytes_to_rcv);
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200268 /* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */
Ursula Braun5f083182017-01-09 16:55:22 +0100269 smp_mb__after_atomic();
Ursula Braun952310c2017-01-09 16:55:24 +0100270 smc->sk.sk_data_ready(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +0200271 } else {
272 if (conn->local_rx_ctrl.prod_flags.write_blocked ||
273 conn->local_rx_ctrl.prod_flags.cons_curs_upd_req ||
274 conn->local_rx_ctrl.prod_flags.urg_data_pending) {
275 if (conn->local_rx_ctrl.prod_flags.urg_data_pending)
276 conn->urg_state = SMC_URG_NOTYET;
277 /* force immediate tx of current consumer cursor, but
278 * under send_lock to guarantee arrival in seqno-order
279 */
Ursula Braun56070162018-08-03 10:38:33 +0200280 if (smc->sk.sk_state != SMC_INIT)
281 smc_tx_sndbuf_nonempty(conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +0200282 }
Ursula Braun5f083182017-01-09 16:55:22 +0100283 }
284
Ursula Braun51f1de72018-01-26 09:28:48 +0100285 /* piggy backed tx info */
286 /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */
287 if (diff_cons && smc_tx_prepared_sends(conn)) {
288 smc_tx_sndbuf_nonempty(conn);
289 /* trigger socket release if connection closed */
290 smc_close_wake_tx_prepared(smc);
291 }
Stefan Rasplde8474e2018-05-23 16:38:11 +0200292 if (diff_cons && conn->urg_tx_pend &&
293 atomic_read(&conn->peer_rmbe_space) == conn->peer_rmbe_size) {
294 /* urg data confirmed by peer, indicate we're ready for more */
295 conn->urg_tx_pend = false;
296 smc->sk.sk_write_space(&smc->sk);
297 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100298
Ursula Braunb38d7322017-01-09 16:55:25 +0100299 if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
Ursula Braun5f083182017-01-09 16:55:22 +0100300 smc->sk.sk_err = ECONNRESET;
Ursula Braunb38d7322017-01-09 16:55:25 +0100301 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
302 }
Ursula Braun46c28db2017-04-10 14:58:01 +0200303 if (smc_cdc_rxed_any_close_or_senddone(conn)) {
304 smc->sk.sk_shutdown |= RCV_SHUTDOWN;
305 if (smc->clcsock && smc->clcsock->sk)
306 smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
307 sock_set_flag(&smc->sk, SOCK_DONE);
Ursula Braun51f1de72018-01-26 09:28:48 +0100308 sock_hold(&smc->sk); /* sock_put in close_work */
309 if (!schedule_work(&conn->close_work))
310 sock_put(&smc->sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100311 }
Ursula Braun5f083182017-01-09 16:55:22 +0100312}
313
314/* called under tasklet context */
Hans Wippeld7b0e372018-05-18 09:34:15 +0200315static void smc_cdc_msg_recv(struct smc_sock *smc, struct smc_cdc_msg *cdc)
Ursula Braun5f083182017-01-09 16:55:22 +0100316{
Ursula Braun5f083182017-01-09 16:55:22 +0100317 sock_hold(&smc->sk);
Ursula Braun5f083182017-01-09 16:55:22 +0100318 bh_lock_sock(&smc->sk);
Hans Wippeld7b0e372018-05-18 09:34:15 +0200319 smc_cdc_msg_recv_action(smc, cdc);
Ursula Braun5f083182017-01-09 16:55:22 +0100320 bh_unlock_sock(&smc->sk);
321 sock_put(&smc->sk); /* no free sk in softirq-context */
322}
323
Hans Wippelbe244f22018-06-28 19:05:10 +0200324/* Schedule a tasklet for this connection. Triggered from the ISM device IRQ
325 * handler to indicate update in the DMBE.
326 *
327 * Context:
328 * - tasklet context
329 */
330static void smcd_cdc_rx_tsklet(unsigned long data)
331{
332 struct smc_connection *conn = (struct smc_connection *)data;
Ursula Braunb9a22dd2018-11-20 16:46:42 +0100333 struct smcd_cdc_msg *data_cdc;
Hans Wippelbe244f22018-06-28 19:05:10 +0200334 struct smcd_cdc_msg cdc;
335 struct smc_sock *smc;
336
337 if (!conn)
338 return;
339
Ursula Braunb9a22dd2018-11-20 16:46:42 +0100340 data_cdc = (struct smcd_cdc_msg *)conn->rmb_desc->cpu_addr;
341 smcd_curs_copy(&cdc.prod, &data_cdc->prod, conn);
342 smcd_curs_copy(&cdc.cons, &data_cdc->cons, conn);
Hans Wippelbe244f22018-06-28 19:05:10 +0200343 smc = container_of(conn, struct smc_sock, conn);
344 smc_cdc_msg_recv(smc, (struct smc_cdc_msg *)&cdc);
345}
346
347/* Initialize receive tasklet. Called from ISM device IRQ handler to start
348 * receiver side.
349 */
350void smcd_cdc_rx_init(struct smc_connection *conn)
351{
352 tasklet_init(&conn->rx_tsklet, smcd_cdc_rx_tsklet, (unsigned long)conn);
353}
354
Ursula Braun5f083182017-01-09 16:55:22 +0100355/***************************** init, exit, misc ******************************/
356
357static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
358{
359 struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
360 struct smc_cdc_msg *cdc = buf;
Hans Wippeld7b0e372018-05-18 09:34:15 +0200361 struct smc_connection *conn;
362 struct smc_link_group *lgr;
363 struct smc_sock *smc;
Ursula Braun5f083182017-01-09 16:55:22 +0100364
365 if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved))
366 return; /* short message */
Karsten Graulcbba07a2018-02-28 12:44:07 +0100367 if (cdc->len != SMC_WR_TX_SIZE)
Ursula Braun5f083182017-01-09 16:55:22 +0100368 return; /* invalid message */
Hans Wippeld7b0e372018-05-18 09:34:15 +0200369
370 /* lookup connection */
Stefan Raspl00e5fb22018-07-23 13:53:10 +0200371 lgr = smc_get_lgr(link);
Hans Wippeld7b0e372018-05-18 09:34:15 +0200372 read_lock_bh(&lgr->conns_lock);
373 conn = smc_lgr_find_conn(ntohl(cdc->token), lgr);
374 read_unlock_bh(&lgr->conns_lock);
375 if (!conn)
376 return;
377 smc = container_of(conn, struct smc_sock, conn);
378
379 if (!cdc->prod_flags.failover_validation) {
380 if (smc_cdc_before(ntohs(cdc->seqno),
381 conn->local_rx_ctrl.seqno))
382 /* received seqno is old */
383 return;
384 }
385 smc_cdc_msg_recv(smc, cdc);
Ursula Braun5f083182017-01-09 16:55:22 +0100386}
387
388static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = {
389 {
390 .handler = smc_cdc_rx_handler,
391 .type = SMC_CDC_MSG_TYPE
392 },
393 {
394 .handler = NULL,
395 }
396};
397
398int __init smc_cdc_init(void)
399{
400 struct smc_wr_rx_handler *handler;
401 int rc = 0;
402
403 for (handler = smc_cdc_rx_handlers; handler->handler; handler++) {
404 INIT_HLIST_NODE(&handler->list);
405 rc = smc_wr_rx_register_handler(handler);
406 if (rc)
407 break;
408 }
409 return rc;
410}