blob: 89105e95b4523f0a0d197e7167f27914430f482e [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 smc = container_of(conn, struct smc_sock, conn);
Ursula Braun5f083182017-01-09 16:55:22 +010035 bh_lock_sock(&smc->sk);
36 if (!wc_status) {
Hans Wippel69cb7dc2018-05-18 09:34:10 +020037 diff = smc_curs_diff(cdcpend->conn->sndbuf_desc->len,
Ursula Braun5f083182017-01-09 16:55:22 +010038 &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);
Hans Wippel69cb7dc2018-05-18 09:34:10 +020043 /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
Ursula Braun5f083182017-01-09 16:55:22 +010044 smp_mb__after_atomic();
Stefan Rasplbac6de72018-07-23 13:53:09 +020045 smc_curs_copy(&conn->tx_curs_fin, &cdcpend->cursor, conn);
Karsten Graulf0ec4f12020-05-04 14:18:37 +020046 smc_curs_copy(&conn->local_tx_ctrl_fin, &cdcpend->p_cursor,
47 conn);
48 conn->tx_cdc_seq_fin = cdcpend->ctrl_seq;
Ursula Braun5f083182017-01-09 16:55:22 +010049 }
Dust Li349d4312021-12-28 17:03:25 +080050
Dust Lidcd2cf52022-03-01 17:43:57 +080051 if (atomic_dec_and_test(&conn->cdc_pend_tx_wr)) {
Dust Li6b88af82022-03-01 17:44:02 +080052 /* If user owns the sock_lock, mark the connection need sending.
53 * User context will later try to send when it release sock_lock
54 * in smc_release_cb()
Dust Lidcd2cf52022-03-01 17:43:57 +080055 */
Dust Li6b88af82022-03-01 17:44:02 +080056 if (sock_owned_by_user(&smc->sk))
57 conn->tx_in_release_sock = true;
58 else
59 smc_tx_pending(conn);
60
Dust Lidcd2cf52022-03-01 17:43:57 +080061 if (unlikely(wq_has_sleeper(&conn->cdc_pend_tx_wq)))
62 wake_up(&conn->cdc_pend_tx_wq);
63 }
Dust Li349d4312021-12-28 17:03:25 +080064 WARN_ON(atomic_read(&conn->cdc_pend_tx_wr) < 0);
65
Ursula Braune6727f32017-01-09 16:55:23 +010066 smc_tx_sndbuf_nonfull(smc);
Ursula Braun5f083182017-01-09 16:55:22 +010067 bh_unlock_sock(&smc->sk);
68}
69
Ursula Braun51957bc2017-09-21 09:17:34 +020070int smc_cdc_get_free_slot(struct smc_connection *conn,
Karsten Graulc6f02eb2020-05-04 14:18:38 +020071 struct smc_link *link,
Ursula Braun5f083182017-01-09 16:55:22 +010072 struct smc_wr_buf **wr_buf,
Ursula Braunad6f3172019-02-04 13:44:44 +010073 struct smc_rdma_wr **wr_rdma_buf,
Ursula Braun5f083182017-01-09 16:55:22 +010074 struct smc_cdc_tx_pend **pend)
75{
Ursula Braun1a0a04c2018-01-25 11:15:36 +010076 int rc;
Ursula Braun51957bc2017-09-21 09:17:34 +020077
Ursula Braun1a0a04c2018-01-25 11:15:36 +010078 rc = smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf,
Ursula Braunad6f3172019-02-04 13:44:44 +010079 wr_rdma_buf,
Ursula Braun1a0a04c2018-01-25 11:15:36 +010080 (struct smc_wr_tx_pend_priv **)pend);
Karsten Graul2bced6a2020-07-20 16:24:28 +020081 if (conn->killed) {
Ursula Braun1a0a04c2018-01-25 11:15:36 +010082 /* abnormal termination */
Karsten Graul2bced6a2020-07-20 16:24:28 +020083 if (!rc)
84 smc_wr_tx_put_slot(link,
Guangguan Wange225c9a2022-05-28 14:54:57 +080085 (struct smc_wr_tx_pend_priv *)(*pend));
Ursula Braun1a0a04c2018-01-25 11:15:36 +010086 rc = -EPIPE;
Karsten Graul2bced6a2020-07-20 16:24:28 +020087 }
Ursula Braun1a0a04c2018-01-25 11:15:36 +010088 return rc;
Ursula Braun5f083182017-01-09 16:55:22 +010089}
90
91static inline void smc_cdc_add_pending_send(struct smc_connection *conn,
92 struct smc_cdc_tx_pend *pend)
93{
94 BUILD_BUG_ON_MSG(
95 sizeof(struct smc_cdc_msg) > SMC_WR_BUF_SIZE,
96 "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)");
97 BUILD_BUG_ON_MSG(
Ursula Braunb9a22dd2018-11-20 16:46:42 +010098 offsetofend(struct smc_cdc_msg, reserved) > SMC_WR_TX_SIZE,
Ursula Braun5f083182017-01-09 16:55:22 +010099 "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()");
100 BUILD_BUG_ON_MSG(
101 sizeof(struct smc_cdc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
102 "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)");
103 pend->conn = conn;
104 pend->cursor = conn->tx_curs_sent;
105 pend->p_cursor = conn->local_tx_ctrl.prod;
106 pend->ctrl_seq = conn->tx_cdc_seq;
107}
108
109int smc_cdc_msg_send(struct smc_connection *conn,
110 struct smc_wr_buf *wr_buf,
111 struct smc_cdc_tx_pend *pend)
112{
Karsten Graul387707f2020-04-29 17:10:40 +0200113 struct smc_link *link = conn->lnk;
Ursula Braunb8649ef2019-02-04 13:44:45 +0100114 union smc_host_cursor cfed;
Ursula Braun5f083182017-01-09 16:55:22 +0100115 int rc;
116
D. Wythe22a825c2023-03-08 16:17:12 +0800117 if (unlikely(!READ_ONCE(conn->sndbuf_desc)))
118 return -ENOBUFS;
119
Ursula Braun5f083182017-01-09 16:55:22 +0100120 smc_cdc_add_pending_send(conn, pend);
121
122 conn->tx_cdc_seq++;
123 conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
Ursula Braunccc8ca92019-02-07 14:52:54 +0100124 smc_host_msg_to_cdc((struct smc_cdc_msg *)wr_buf, conn, &cfed);
Dust Li349d4312021-12-28 17:03:25 +0800125
126 atomic_inc(&conn->cdc_pend_tx_wr);
127 smp_mb__after_atomic(); /* Make sure cdc_pend_tx_wr added before post */
128
Ursula Braun5f083182017-01-09 16:55:22 +0100129 rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
Karsten Graul4dff63c2019-02-12 16:29:50 +0100130 if (!rc) {
Ursula Braunb8649ef2019-02-04 13:44:45 +0100131 smc_curs_copy(&conn->rx_curs_confirmed, &cfed, conn);
Karsten Graul4dff63c2019-02-12 16:29:50 +0100132 conn->local_rx_ctrl.prod_flags.cons_curs_upd_req = 0;
Karsten Graulf0ec4f12020-05-04 14:18:37 +0200133 } else {
134 conn->tx_cdc_seq--;
135 conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
Dust Li349d4312021-12-28 17:03:25 +0800136 atomic_dec(&conn->cdc_pend_tx_wr);
Karsten Graul4dff63c2019-02-12 16:29:50 +0100137 }
Ursula Braun5f083182017-01-09 16:55:22 +0100138
139 return rc;
140}
141
Karsten Graul29bd73d2020-05-04 14:18:39 +0200142/* send a validation msg indicating the move of a conn to an other QP link */
Karsten Graulb8ded9d2020-05-30 16:42:37 +0200143int smcr_cdc_msg_send_validation(struct smc_connection *conn,
144 struct smc_cdc_tx_pend *pend,
145 struct smc_wr_buf *wr_buf)
Karsten Graul29bd73d2020-05-04 14:18:39 +0200146{
147 struct smc_host_cdc_msg *local = &conn->local_tx_ctrl;
148 struct smc_link *link = conn->lnk;
Karsten Graul29bd73d2020-05-04 14:18:39 +0200149 struct smc_cdc_msg *peer;
150 int rc;
151
Karsten Graul29bd73d2020-05-04 14:18:39 +0200152 peer = (struct smc_cdc_msg *)wr_buf;
153 peer->common.type = local->common.type;
154 peer->len = local->len;
155 peer->seqno = htons(conn->tx_cdc_seq_fin); /* seqno last compl. tx */
156 peer->token = htonl(local->token);
157 peer->prod_flags.failover_validation = 1;
158
Dust Li349d4312021-12-28 17:03:25 +0800159 /* We need to set pend->conn here to make sure smc_cdc_tx_handler()
160 * can handle properly
161 */
162 smc_cdc_add_pending_send(conn, pend);
163
164 atomic_inc(&conn->cdc_pend_tx_wr);
165 smp_mb__after_atomic(); /* Make sure cdc_pend_tx_wr added before post */
166
Karsten Graul29bd73d2020-05-04 14:18:39 +0200167 rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
Dust Li349d4312021-12-28 17:03:25 +0800168 if (unlikely(rc))
169 atomic_dec(&conn->cdc_pend_tx_wr);
170
Karsten Graul29bd73d2020-05-04 14:18:39 +0200171 return rc;
172}
173
Hans Wippelbe244f22018-06-28 19:05:10 +0200174static int smcr_cdc_get_slot_and_msg_send(struct smc_connection *conn)
Ursula Braun5f083182017-01-09 16:55:22 +0100175{
176 struct smc_cdc_tx_pend *pend;
177 struct smc_wr_buf *wr_buf;
Karsten Graulc6f02eb2020-05-04 14:18:38 +0200178 struct smc_link *link;
179 bool again = false;
Ursula Braun5f083182017-01-09 16:55:22 +0100180 int rc;
181
Karsten Graulc6f02eb2020-05-04 14:18:38 +0200182again:
183 link = conn->lnk;
Karsten Graul95f7f3e2021-10-07 16:14:40 +0200184 if (!smc_wr_tx_link_hold(link))
185 return -ENOLINK;
Karsten Graulc6f02eb2020-05-04 14:18:38 +0200186 rc = smc_cdc_get_free_slot(conn, link, &wr_buf, NULL, &pend);
Ursula Braun5f083182017-01-09 16:55:22 +0100187 if (rc)
Karsten Graul95f7f3e2021-10-07 16:14:40 +0200188 goto put_out;
Ursula Braun5f083182017-01-09 16:55:22 +0100189
Karsten Graul2dee25a2019-01-30 18:51:06 +0100190 spin_lock_bh(&conn->send_lock);
Karsten Graulc6f02eb2020-05-04 14:18:38 +0200191 if (link != conn->lnk) {
192 /* link of connection changed, try again one time*/
193 spin_unlock_bh(&conn->send_lock);
194 smc_wr_tx_put_slot(link,
195 (struct smc_wr_tx_pend_priv *)pend);
Karsten Graul95f7f3e2021-10-07 16:14:40 +0200196 smc_wr_tx_link_put(link);
Karsten Graulc6f02eb2020-05-04 14:18:38 +0200197 if (again)
198 return -ENOLINK;
199 again = true;
200 goto again;
201 }
Karsten Graul2dee25a2019-01-30 18:51:06 +0100202 rc = smc_cdc_msg_send(conn, wr_buf, pend);
203 spin_unlock_bh(&conn->send_lock);
Karsten Graul95f7f3e2021-10-07 16:14:40 +0200204put_out:
205 smc_wr_tx_link_put(link);
Karsten Graul2dee25a2019-01-30 18:51:06 +0100206 return rc;
Ursula Braun5f083182017-01-09 16:55:22 +0100207}
208
Hans Wippelbe244f22018-06-28 19:05:10 +0200209int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn)
210{
211 int rc;
212
Wen Guea89c6c2022-01-13 16:36:41 +0800213 if (!smc_conn_lgr_valid(conn) ||
214 (conn->lgr->is_smcd && conn->lgr->peer_shutdown))
Ursula Braun50c6b202019-11-14 13:02:40 +0100215 return -EPIPE;
216
Hans Wippelbe244f22018-06-28 19:05:10 +0200217 if (conn->lgr->is_smcd) {
218 spin_lock_bh(&conn->send_lock);
219 rc = smcd_cdc_msg_send(conn);
220 spin_unlock_bh(&conn->send_lock);
221 } else {
222 rc = smcr_cdc_get_slot_and_msg_send(conn);
223 }
224
225 return rc;
226}
227
Dust Li349d4312021-12-28 17:03:25 +0800228void smc_cdc_wait_pend_tx_wr(struct smc_connection *conn)
Ursula Braun5f083182017-01-09 16:55:22 +0100229{
Dust Li349d4312021-12-28 17:03:25 +0800230 wait_event(conn->cdc_pend_tx_wq, !atomic_read(&conn->cdc_pend_tx_wr));
Ursula Braun5f083182017-01-09 16:55:22 +0100231}
232
Hans Wippelbe244f22018-06-28 19:05:10 +0200233/* Send a SMC-D CDC header.
234 * This increments the free space available in our send buffer.
235 * Also update the confirmed receive buffer with what was sent to the peer.
236 */
237int smcd_cdc_msg_send(struct smc_connection *conn)
238{
239 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
Ursula Braunb9a22dd2018-11-20 16:46:42 +0100240 union smc_host_cursor curs;
Hans Wippelbe244f22018-06-28 19:05:10 +0200241 struct smcd_cdc_msg cdc;
242 int rc, diff;
243
244 memset(&cdc, 0, sizeof(cdc));
245 cdc.common.type = SMC_CDC_MSG_TYPE;
Ursula Braunb9a22dd2018-11-20 16:46:42 +0100246 curs.acurs.counter = atomic64_read(&conn->local_tx_ctrl.prod.acurs);
247 cdc.prod.wrap = curs.wrap;
248 cdc.prod.count = curs.count;
249 curs.acurs.counter = atomic64_read(&conn->local_tx_ctrl.cons.acurs);
250 cdc.cons.wrap = curs.wrap;
251 cdc.cons.count = curs.count;
252 cdc.cons.prod_flags = conn->local_tx_ctrl.prod_flags;
253 cdc.cons.conn_state_flags = conn->local_tx_ctrl.conn_state_flags;
Hans Wippelbe244f22018-06-28 19:05:10 +0200254 rc = smcd_tx_ism_write(conn, &cdc, sizeof(cdc), 0, 1);
255 if (rc)
256 return rc;
Ursula Braunb9a22dd2018-11-20 16:46:42 +0100257 smc_curs_copy(&conn->rx_curs_confirmed, &curs, conn);
Karsten Graul4dff63c2019-02-12 16:29:50 +0100258 conn->local_rx_ctrl.prod_flags.cons_curs_upd_req = 0;
Hans Wippelbe244f22018-06-28 19:05:10 +0200259 /* Calculate transmitted data and increment free send buffer space */
260 diff = smc_curs_diff(conn->sndbuf_desc->len, &conn->tx_curs_fin,
261 &conn->tx_curs_sent);
262 /* increased by confirmed number of bytes */
263 smp_mb__before_atomic();
264 atomic_add(diff, &conn->sndbuf_space);
265 /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
266 smp_mb__after_atomic();
Stefan Rasplbac6de72018-07-23 13:53:09 +0200267 smc_curs_copy(&conn->tx_curs_fin, &conn->tx_curs_sent, conn);
Hans Wippelbe244f22018-06-28 19:05:10 +0200268
269 smc_tx_sndbuf_nonfull(smc);
270 return rc;
271}
272
Ursula Braun5f083182017-01-09 16:55:22 +0100273/********************************* receive ***********************************/
274
275static inline bool smc_cdc_before(u16 seq1, u16 seq2)
276{
277 return (s16)(seq1 - seq2) < 0;
278}
279
Stefan Rasplde8474eb2018-05-23 16:38:11 +0200280static void smc_cdc_handle_urg_data_arrival(struct smc_sock *smc,
281 int *diff_prod)
282{
283 struct smc_connection *conn = &smc->conn;
284 char *base;
285
286 /* new data included urgent business */
Stefan Rasplbac6de72018-07-23 13:53:09 +0200287 smc_curs_copy(&conn->urg_curs, &conn->local_rx_ctrl.prod, conn);
Stefan Rasplde8474eb2018-05-23 16:38:11 +0200288 conn->urg_state = SMC_URG_VALID;
289 if (!sock_flag(&smc->sk, SOCK_URGINLINE))
290 /* we'll skip the urgent byte, so don't account for it */
291 (*diff_prod)--;
Hans Wippelbe244f22018-06-28 19:05:10 +0200292 base = (char *)conn->rmb_desc->cpu_addr + conn->rx_off;
Stefan Rasplde8474eb2018-05-23 16:38:11 +0200293 if (conn->urg_curs.count)
294 conn->urg_rx_byte = *(base + conn->urg_curs.count - 1);
295 else
296 conn->urg_rx_byte = *(base + conn->rmb_desc->len - 1);
297 sk_send_sigurg(&smc->sk);
298}
299
Karsten Graulb286a062020-05-04 14:18:40 +0200300static void smc_cdc_msg_validate(struct smc_sock *smc, struct smc_cdc_msg *cdc,
301 struct smc_link *link)
302{
303 struct smc_connection *conn = &smc->conn;
304 u16 recv_seq = ntohs(cdc->seqno);
305 s16 diff;
306
307 /* check that seqnum was seen before */
308 diff = conn->local_rx_ctrl.seqno - recv_seq;
309 if (diff < 0) { /* diff larger than 0x7fff */
310 /* drop connection */
311 conn->out_of_sync = 1; /* prevent any further receives */
312 spin_lock_bh(&conn->send_lock);
313 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
314 conn->lnk = link;
315 spin_unlock_bh(&conn->send_lock);
316 sock_hold(&smc->sk); /* sock_put in abort_work */
Karsten Graul22ef4732020-09-10 18:48:29 +0200317 if (!queue_work(smc_close_wq, &conn->abort_work))
Karsten Graulb286a062020-05-04 14:18:40 +0200318 sock_put(&smc->sk);
319 }
320}
321
Ursula Braun5f083182017-01-09 16:55:22 +0100322static void smc_cdc_msg_recv_action(struct smc_sock *smc,
Ursula Braun5f083182017-01-09 16:55:22 +0100323 struct smc_cdc_msg *cdc)
324{
325 union smc_host_cursor cons_old, prod_old;
326 struct smc_connection *conn = &smc->conn;
327 int diff_cons, diff_prod;
328
Stefan Rasplbac6de72018-07-23 13:53:09 +0200329 smc_curs_copy(&prod_old, &conn->local_rx_ctrl.prod, conn);
330 smc_curs_copy(&cons_old, &conn->local_rx_ctrl.cons, conn);
Ursula Braun5f083182017-01-09 16:55:22 +0100331 smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);
332
333 diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
334 &conn->local_rx_ctrl.cons);
335 if (diff_cons) {
336 /* peer_rmbe_space is decreased during data transfer with RDMA
337 * write
338 */
339 smp_mb__before_atomic();
340 atomic_add(diff_cons, &conn->peer_rmbe_space);
341 /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
342 smp_mb__after_atomic();
343 }
344
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200345 diff_prod = smc_curs_diff(conn->rmb_desc->len, &prod_old,
Ursula Braun5f083182017-01-09 16:55:22 +0100346 &conn->local_rx_ctrl.prod);
347 if (diff_prod) {
Stefan Rasplde8474eb2018-05-23 16:38:11 +0200348 if (conn->local_rx_ctrl.prod_flags.urg_data_present)
349 smc_cdc_handle_urg_data_arrival(smc, &diff_prod);
Ursula Braun5f083182017-01-09 16:55:22 +0100350 /* bytes_to_rcv is decreased in smc_recvmsg */
351 smp_mb__before_atomic();
352 atomic_add(diff_prod, &conn->bytes_to_rcv);
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200353 /* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */
Ursula Braun5f083182017-01-09 16:55:22 +0100354 smp_mb__after_atomic();
Ursula Braun952310c2017-01-09 16:55:24 +0100355 smc->sk.sk_data_ready(&smc->sk);
Stefan Rasplde8474eb2018-05-23 16:38:11 +0200356 } else {
Karsten Graulcf0cfe52019-02-12 16:29:53 +0100357 if (conn->local_rx_ctrl.prod_flags.write_blocked)
358 smc->sk.sk_data_ready(&smc->sk);
359 if (conn->local_rx_ctrl.prod_flags.urg_data_pending)
360 conn->urg_state = SMC_URG_NOTYET;
Ursula Braun5f083182017-01-09 16:55:22 +0100361 }
362
Ursula Braun51f1de72018-01-26 09:28:48 +0100363 /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */
Karsten Graulcf0cfe52019-02-12 16:29:53 +0100364 if ((diff_cons && smc_tx_prepared_sends(conn)) ||
365 conn->local_rx_ctrl.prod_flags.cons_curs_upd_req ||
Dust Li6b88af82022-03-01 17:44:02 +0800366 conn->local_rx_ctrl.prod_flags.urg_data_pending) {
367 if (!sock_owned_by_user(&smc->sk))
368 smc_tx_pending(conn);
369 else
370 conn->tx_in_release_sock = true;
371 }
Karsten Graulcf0cfe52019-02-12 16:29:53 +0100372
Stefan Rasplde8474eb2018-05-23 16:38:11 +0200373 if (diff_cons && conn->urg_tx_pend &&
374 atomic_read(&conn->peer_rmbe_space) == conn->peer_rmbe_size) {
375 /* urg data confirmed by peer, indicate we're ready for more */
376 conn->urg_tx_pend = false;
377 smc->sk.sk_write_space(&smc->sk);
378 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100379
Ursula Braunb38d7322017-01-09 16:55:25 +0100380 if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
Ursula Braun5f083182017-01-09 16:55:22 +0100381 smc->sk.sk_err = ECONNRESET;
Ursula Braunb38d7322017-01-09 16:55:25 +0100382 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
383 }
Ursula Braun46c28db2017-04-10 14:58:01 +0200384 if (smc_cdc_rxed_any_close_or_senddone(conn)) {
385 smc->sk.sk_shutdown |= RCV_SHUTDOWN;
386 if (smc->clcsock && smc->clcsock->sk)
387 smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
388 sock_set_flag(&smc->sk, SOCK_DONE);
Ursula Braun51f1de72018-01-26 09:28:48 +0100389 sock_hold(&smc->sk); /* sock_put in close_work */
Karsten Graul22ef4732020-09-10 18:48:29 +0200390 if (!queue_work(smc_close_wq, &conn->close_work))
Ursula Braun51f1de72018-01-26 09:28:48 +0100391 sock_put(&smc->sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100392 }
Ursula Braun5f083182017-01-09 16:55:22 +0100393}
394
395/* called under tasklet context */
Hans Wippeld7b0e372018-05-18 09:34:15 +0200396static void smc_cdc_msg_recv(struct smc_sock *smc, struct smc_cdc_msg *cdc)
Ursula Braun5f083182017-01-09 16:55:22 +0100397{
Ursula Braun5f083182017-01-09 16:55:22 +0100398 sock_hold(&smc->sk);
Ursula Braun5f083182017-01-09 16:55:22 +0100399 bh_lock_sock(&smc->sk);
Hans Wippeld7b0e372018-05-18 09:34:15 +0200400 smc_cdc_msg_recv_action(smc, cdc);
Ursula Braun5f083182017-01-09 16:55:22 +0100401 bh_unlock_sock(&smc->sk);
402 sock_put(&smc->sk); /* no free sk in softirq-context */
403}
404
Hans Wippelbe244f22018-06-28 19:05:10 +0200405/* Schedule a tasklet for this connection. Triggered from the ISM device IRQ
406 * handler to indicate update in the DMBE.
407 *
408 * Context:
409 * - tasklet context
410 */
Allen Paisfcb8e3a2020-11-03 14:48:22 +0530411static void smcd_cdc_rx_tsklet(struct tasklet_struct *t)
Hans Wippelbe244f22018-06-28 19:05:10 +0200412{
Allen Paisfcb8e3a2020-11-03 14:48:22 +0530413 struct smc_connection *conn = from_tasklet(conn, t, rx_tsklet);
Ursula Braunb9a22dd2018-11-20 16:46:42 +0100414 struct smcd_cdc_msg *data_cdc;
Hans Wippelbe244f22018-06-28 19:05:10 +0200415 struct smcd_cdc_msg cdc;
416 struct smc_sock *smc;
417
Ursula Braunb2900982019-10-21 16:13:08 +0200418 if (!conn || conn->killed)
Hans Wippelbe244f22018-06-28 19:05:10 +0200419 return;
420
Ursula Braunb9a22dd2018-11-20 16:46:42 +0100421 data_cdc = (struct smcd_cdc_msg *)conn->rmb_desc->cpu_addr;
422 smcd_curs_copy(&cdc.prod, &data_cdc->prod, conn);
423 smcd_curs_copy(&cdc.cons, &data_cdc->cons, conn);
Hans Wippelbe244f22018-06-28 19:05:10 +0200424 smc = container_of(conn, struct smc_sock, conn);
425 smc_cdc_msg_recv(smc, (struct smc_cdc_msg *)&cdc);
426}
427
428/* Initialize receive tasklet. Called from ISM device IRQ handler to start
429 * receiver side.
430 */
431void smcd_cdc_rx_init(struct smc_connection *conn)
432{
Allen Paisfcb8e3a2020-11-03 14:48:22 +0530433 tasklet_setup(&conn->rx_tsklet, smcd_cdc_rx_tsklet);
Hans Wippelbe244f22018-06-28 19:05:10 +0200434}
435
Ursula Braun5f083182017-01-09 16:55:22 +0100436/***************************** init, exit, misc ******************************/
437
438static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
439{
440 struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
441 struct smc_cdc_msg *cdc = buf;
Hans Wippeld7b0e372018-05-18 09:34:15 +0200442 struct smc_connection *conn;
443 struct smc_link_group *lgr;
444 struct smc_sock *smc;
Ursula Braun5f083182017-01-09 16:55:22 +0100445
446 if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved))
447 return; /* short message */
Karsten Graulcbba07a2018-02-28 12:44:07 +0100448 if (cdc->len != SMC_WR_TX_SIZE)
Ursula Braun5f083182017-01-09 16:55:22 +0100449 return; /* invalid message */
Hans Wippeld7b0e372018-05-18 09:34:15 +0200450
451 /* lookup connection */
Stefan Raspl00e5fb22018-07-23 13:53:10 +0200452 lgr = smc_get_lgr(link);
Hans Wippeld7b0e372018-05-18 09:34:15 +0200453 read_lock_bh(&lgr->conns_lock);
454 conn = smc_lgr_find_conn(ntohl(cdc->token), lgr);
455 read_unlock_bh(&lgr->conns_lock);
Karsten Graulb286a062020-05-04 14:18:40 +0200456 if (!conn || conn->out_of_sync)
Hans Wippeld7b0e372018-05-18 09:34:15 +0200457 return;
458 smc = container_of(conn, struct smc_sock, conn);
459
Karsten Graulb286a062020-05-04 14:18:40 +0200460 if (cdc->prod_flags.failover_validation) {
461 smc_cdc_msg_validate(smc, cdc, link);
462 return;
Hans Wippeld7b0e372018-05-18 09:34:15 +0200463 }
Karsten Graulb286a062020-05-04 14:18:40 +0200464 if (smc_cdc_before(ntohs(cdc->seqno),
465 conn->local_rx_ctrl.seqno))
466 /* received seqno is old */
467 return;
468
Hans Wippeld7b0e372018-05-18 09:34:15 +0200469 smc_cdc_msg_recv(smc, cdc);
Ursula Braun5f083182017-01-09 16:55:22 +0100470}
471
472static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = {
473 {
474 .handler = smc_cdc_rx_handler,
475 .type = SMC_CDC_MSG_TYPE
476 },
477 {
478 .handler = NULL,
479 }
480};
481
482int __init smc_cdc_init(void)
483{
484 struct smc_wr_rx_handler *handler;
485 int rc = 0;
486
487 for (handler = smc_cdc_rx_handlers; handler->handler; handler++) {
488 INIT_HLIST_NODE(&handler->list);
489 rc = smc_wr_rx_register_handler(handler);
490 if (rc)
491 break;
492 }
493 return rc;
494}