blob: be241d53020f1efad9700077c1b7952c5b4f0cb7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ursula Braune6727f32017-01-09 16:55:23 +01002/*
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
4 *
5 * Manage send buffer.
6 * Producer:
7 * Copy user space data into send buffer, if send buffer space available.
8 * Consumer:
9 * Trigger RDMA write into RMBE of peer and send CDC, if RMBE space available.
10 *
11 * Copyright IBM Corp. 2016
12 *
13 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
14 */
15
16#include <linux/net.h>
17#include <linux/rcupdate.h>
18#include <linux/workqueue.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010019#include <linux/sched/signal.h>
20
Ursula Braune6727f32017-01-09 16:55:23 +010021#include <net/sock.h>
Ursula Braun01d2f7e2018-04-26 17:18:22 +020022#include <net/tcp.h>
Ursula Braune6727f32017-01-09 16:55:23 +010023
24#include "smc.h"
25#include "smc_wr.h"
26#include "smc_cdc.h"
Karsten Graul5bc056d2019-02-12 16:29:51 +010027#include "smc_close.h"
Hans Wippelbe244f22018-06-28 19:05:10 +020028#include "smc_ism.h"
Ursula Braune6727f32017-01-09 16:55:23 +010029#include "smc_tx.h"
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +020030#include "smc_stats.h"
Tony Luaff30832021-11-01 15:39:14 +080031#include "smc_tracepoint.h"
Ursula Braune6727f32017-01-09 16:55:23 +010032
Karsten Graul16297d12019-02-12 16:29:52 +010033#define SMC_TX_WORK_DELAY 0
Ursula Braun01d2f7e2018-04-26 17:18:22 +020034#define SMC_TX_CORK_DELAY (HZ >> 2) /* 250 ms */
Ursula Braun18e537c2017-09-21 09:16:33 +020035
Ursula Braune6727f32017-01-09 16:55:23 +010036/***************************** sndbuf producer *******************************/
37
38/* callback implementation for sk.sk_write_space()
Stefan Rasplde8474e2018-05-23 16:38:11 +020039 * to wakeup sndbuf producers that blocked with smc_tx_wait().
Ursula Braune6727f32017-01-09 16:55:23 +010040 * called under sk_socket lock.
41 */
42static void smc_tx_write_space(struct sock *sk)
43{
44 struct socket *sock = sk->sk_socket;
45 struct smc_sock *smc = smc_sk(sk);
46 struct socket_wq *wq;
47
48 /* similar to sk_stream_write_space */
49 if (atomic_read(&smc->conn.sndbuf_space) && sock) {
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +020050 if (test_bit(SOCK_NOSPACE, &sock->flags))
Guvenc Gulce194730a2021-06-16 16:52:58 +020051 SMC_STAT_RMB_TX_FULL(smc, !smc->conn.lnk);
Ursula Braune6727f32017-01-09 16:55:23 +010052 clear_bit(SOCK_NOSPACE, &sock->flags);
53 rcu_read_lock();
54 wq = rcu_dereference(sk->sk_wq);
55 if (skwq_has_sleeper(wq))
56 wake_up_interruptible_poll(&wq->wait,
Linus Torvaldsa9a08842018-02-11 14:34:03 -080057 EPOLLOUT | EPOLLWRNORM |
58 EPOLLWRBAND);
Ursula Braune6727f32017-01-09 16:55:23 +010059 if (wq && wq->fasync_list && !(sk->sk_shutdown & SEND_SHUTDOWN))
60 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
61 rcu_read_unlock();
62 }
63}
64
Stefan Rasplde8474e2018-05-23 16:38:11 +020065/* Wakeup sndbuf producers that blocked with smc_tx_wait().
Ursula Braune6727f32017-01-09 16:55:23 +010066 * Cf. tcp_data_snd_check()=>tcp_check_space()=>tcp_new_space().
67 */
68void smc_tx_sndbuf_nonfull(struct smc_sock *smc)
69{
70 if (smc->sk.sk_socket &&
71 test_bit(SOCK_NOSPACE, &smc->sk.sk_socket->flags))
72 smc->sk.sk_write_space(&smc->sk);
73}
74
Stefan Rasplde8474e2018-05-23 16:38:11 +020075/* blocks sndbuf producer until at least one byte of free space available
76 * or urgent Byte was consumed
77 */
78static int smc_tx_wait(struct smc_sock *smc, int flags)
Ursula Braune6727f32017-01-09 16:55:23 +010079{
80 DEFINE_WAIT_FUNC(wait, woken_wake_function);
81 struct smc_connection *conn = &smc->conn;
82 struct sock *sk = &smc->sk;
Ursula Braune6727f32017-01-09 16:55:23 +010083 long timeo;
84 int rc = 0;
85
86 /* similar to sk_stream_wait_memory */
87 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
Ursula Braune6727f32017-01-09 16:55:23 +010088 add_wait_queue(sk_sleep(sk), &wait);
89 while (1) {
90 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
91 if (sk->sk_err ||
92 (sk->sk_shutdown & SEND_SHUTDOWN) ||
Ursula Braunb2900982019-10-21 16:13:08 +020093 conn->killed ||
Ursula Braune6727f32017-01-09 16:55:23 +010094 conn->local_tx_ctrl.conn_state_flags.peer_done_writing) {
95 rc = -EPIPE;
96 break;
97 }
Ursula Braunaa377e62018-01-24 10:28:17 +010098 if (smc_cdc_rxed_any_close(conn)) {
Ursula Braune6727f32017-01-09 16:55:23 +010099 rc = -ECONNRESET;
100 break;
101 }
102 if (!timeo) {
Jason Baron4651d182019-08-19 14:36:01 -0400103 /* ensure EPOLLOUT is subsequently generated */
104 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Ursula Braune6727f32017-01-09 16:55:23 +0100105 rc = -EAGAIN;
106 break;
107 }
108 if (signal_pending(current)) {
109 rc = sock_intr_errno(timeo);
110 break;
111 }
112 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +0200113 if (atomic_read(&conn->sndbuf_space) && !conn->urg_tx_pend)
114 break; /* at least 1 byte of free & no urgent data */
Ursula Braune6727f32017-01-09 16:55:23 +0100115 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Ursula Braune6727f32017-01-09 16:55:23 +0100116 sk_wait_event(sk, &timeo,
117 sk->sk_err ||
118 (sk->sk_shutdown & SEND_SHUTDOWN) ||
Ursula Braunaa377e62018-01-24 10:28:17 +0100119 smc_cdc_rxed_any_close(conn) ||
Stefan Rasplde8474e2018-05-23 16:38:11 +0200120 (atomic_read(&conn->sndbuf_space) &&
121 !conn->urg_tx_pend),
Ursula Braune6727f32017-01-09 16:55:23 +0100122 &wait);
Ursula Braune6727f32017-01-09 16:55:23 +0100123 }
124 remove_wait_queue(sk_sleep(sk), &wait);
125 return rc;
126}
127
Ursula Braun01d2f7e2018-04-26 17:18:22 +0200128static bool smc_tx_is_corked(struct smc_sock *smc)
129{
130 struct tcp_sock *tp = tcp_sk(smc->clcsock->sk);
131
132 return (tp->nonagle & TCP_NAGLE_CORK) ? true : false;
133}
134
Ursula Braune6727f32017-01-09 16:55:23 +0100135/* sndbuf producer: main API called by socket layer.
136 * called under sock lock.
137 */
138int smc_tx_sendmsg(struct smc_sock *smc, struct msghdr *msg, size_t len)
139{
140 size_t copylen, send_done = 0, send_remaining = len;
141 size_t chunk_len, chunk_off, chunk_len_sum;
142 struct smc_connection *conn = &smc->conn;
143 union smc_host_cursor prep;
144 struct sock *sk = &smc->sk;
145 char *sndbuf_base;
146 int tx_cnt_prep;
147 int writespace;
148 int rc, chunk;
149
150 /* This should be in poll */
151 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
152
153 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) {
154 rc = -EPIPE;
155 goto out_err;
156 }
157
Guvenc Gulce17081632021-06-25 17:11:02 +0200158 if (sk->sk_state == SMC_INIT)
159 return -ENOTCONN;
160
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200161 if (len > conn->sndbuf_desc->len)
Guvenc Gulce194730a2021-06-16 16:52:58 +0200162 SMC_STAT_RMB_TX_SIZE_SMALL(smc, !conn->lnk);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200163
164 if (len > conn->peer_rmbe_size)
Guvenc Gulce194730a2021-06-16 16:52:58 +0200165 SMC_STAT_RMB_TX_PEER_SIZE_SMALL(smc, !conn->lnk);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200166
167 if (msg->msg_flags & MSG_OOB)
Guvenc Gulce194730a2021-06-16 16:52:58 +0200168 SMC_STAT_INC(smc, urg_data_cnt);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200169
Ursula Braune6727f32017-01-09 16:55:23 +0100170 while (msg_data_left(msg)) {
Ursula Braune6727f32017-01-09 16:55:23 +0100171 if (smc->sk.sk_shutdown & SEND_SHUTDOWN ||
Ursula Braunb38d7322017-01-09 16:55:25 +0100172 (smc->sk.sk_err == ECONNABORTED) ||
Ursula Braunb2900982019-10-21 16:13:08 +0200173 conn->killed)
Ursula Braune6727f32017-01-09 16:55:23 +0100174 return -EPIPE;
175 if (smc_cdc_rxed_any_close(conn))
176 return send_done ?: -ECONNRESET;
177
Stefan Rasplde8474e2018-05-23 16:38:11 +0200178 if (msg->msg_flags & MSG_OOB)
179 conn->local_tx_ctrl.prod_flags.urg_data_pending = 1;
180
181 if (!atomic_read(&conn->sndbuf_space) || conn->urg_tx_pend) {
Karsten Graul6889b362019-01-30 18:51:03 +0100182 if (send_done)
183 return send_done;
Stefan Rasplde8474e2018-05-23 16:38:11 +0200184 rc = smc_tx_wait(smc, msg->msg_flags);
Karsten Graul6889b362019-01-30 18:51:03 +0100185 if (rc)
Ursula Braune6727f32017-01-09 16:55:23 +0100186 goto out_err;
Ursula Braune6727f32017-01-09 16:55:23 +0100187 continue;
188 }
189
190 /* initialize variables for 1st iteration of subsequent loop */
Stefan Rasplde8474e2018-05-23 16:38:11 +0200191 /* could be just 1 byte, even after smc_tx_wait above */
Ursula Braune6727f32017-01-09 16:55:23 +0100192 writespace = atomic_read(&conn->sndbuf_space);
193 /* not more than what user space asked for */
194 copylen = min_t(size_t, send_remaining, writespace);
195 /* determine start of sndbuf */
196 sndbuf_base = conn->sndbuf_desc->cpu_addr;
Stefan Rasplbac6de72018-07-23 13:53:09 +0200197 smc_curs_copy(&prep, &conn->tx_curs_prep, conn);
Ursula Braune6727f32017-01-09 16:55:23 +0100198 tx_cnt_prep = prep.count;
199 /* determine chunks where to write into sndbuf */
200 /* either unwrapped case, or 1st chunk of wrapped case */
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200201 chunk_len = min_t(size_t, copylen, conn->sndbuf_desc->len -
202 tx_cnt_prep);
Ursula Braune6727f32017-01-09 16:55:23 +0100203 chunk_len_sum = chunk_len;
204 chunk_off = tx_cnt_prep;
Ursula Braun10428dd2017-07-28 13:56:22 +0200205 smc_sndbuf_sync_sg_for_cpu(conn);
Ursula Braune6727f32017-01-09 16:55:23 +0100206 for (chunk = 0; chunk < 2; chunk++) {
207 rc = memcpy_from_msg(sndbuf_base + chunk_off,
208 msg, chunk_len);
209 if (rc) {
Ursula Braun10428dd2017-07-28 13:56:22 +0200210 smc_sndbuf_sync_sg_for_device(conn);
Ursula Braune6727f32017-01-09 16:55:23 +0100211 if (send_done)
212 return send_done;
213 goto out_err;
214 }
215 send_done += chunk_len;
216 send_remaining -= chunk_len;
217
218 if (chunk_len_sum == copylen)
219 break; /* either on 1st or 2nd iteration */
220 /* prepare next (== 2nd) iteration */
221 chunk_len = copylen - chunk_len; /* remainder */
222 chunk_len_sum += chunk_len;
223 chunk_off = 0; /* modulo offset in send ring buffer */
224 }
Ursula Braun10428dd2017-07-28 13:56:22 +0200225 smc_sndbuf_sync_sg_for_device(conn);
Ursula Braune6727f32017-01-09 16:55:23 +0100226 /* update cursors */
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200227 smc_curs_add(conn->sndbuf_desc->len, &prep, copylen);
Stefan Rasplbac6de72018-07-23 13:53:09 +0200228 smc_curs_copy(&conn->tx_curs_prep, &prep, conn);
Ursula Braune6727f32017-01-09 16:55:23 +0100229 /* increased in send tasklet smc_cdc_tx_handler() */
230 smp_mb__before_atomic();
231 atomic_sub(copylen, &conn->sndbuf_space);
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200232 /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
Ursula Braune6727f32017-01-09 16:55:23 +0100233 smp_mb__after_atomic();
234 /* since we just produced more new data into sndbuf,
235 * trigger sndbuf consumer: RDMA write into peer RMBE and CDC
236 */
Stefan Rasplde8474e2018-05-23 16:38:11 +0200237 if ((msg->msg_flags & MSG_OOB) && !send_remaining)
238 conn->urg_tx_pend = true;
Ursula Braun01d2f7e2018-04-26 17:18:22 +0200239 if ((msg->msg_flags & MSG_MORE || smc_tx_is_corked(smc)) &&
240 (atomic_read(&conn->sndbuf_space) >
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200241 (conn->sndbuf_desc->len >> 1)))
Ursula Braun01d2f7e2018-04-26 17:18:22 +0200242 /* for a corked socket defer the RDMA writes if there
243 * is still sufficient sndbuf_space available
244 */
Karsten Graul22ef4732020-09-10 18:48:29 +0200245 queue_delayed_work(conn->lgr->tx_wq, &conn->tx_work,
246 SMC_TX_CORK_DELAY);
Ursula Braun01d2f7e2018-04-26 17:18:22 +0200247 else
248 smc_tx_sndbuf_nonempty(conn);
Tony Luaff30832021-11-01 15:39:14 +0800249
250 trace_smc_tx_sendmsg(smc, copylen);
Ursula Braune6727f32017-01-09 16:55:23 +0100251 } /* while (msg_data_left(msg)) */
252
253 return send_done;
254
255out_err:
256 rc = sk_stream_error(sk, msg->msg_flags, rc);
257 /* make sure we wake any epoll edge trigger waiter */
258 if (unlikely(rc == -EAGAIN))
259 sk->sk_write_space(sk);
260 return rc;
261}
262
263/***************************** sndbuf consumer *******************************/
264
Hans Wippelbe244f22018-06-28 19:05:10 +0200265/* sndbuf consumer: actual data transfer of one target chunk with ISM write */
266int smcd_tx_ism_write(struct smc_connection *conn, void *data, size_t len,
267 u32 offset, int signal)
268{
269 struct smc_ism_position pos;
270 int rc;
271
272 memset(&pos, 0, sizeof(pos));
273 pos.token = conn->peer_token;
274 pos.index = conn->peer_rmbe_idx;
275 pos.offset = conn->tx_off + offset;
276 pos.signal = signal;
277 rc = smc_ism_write(conn->lgr->smcd, &pos, data, len);
278 if (rc)
279 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
280 return rc;
281}
282
Ursula Braune6727f32017-01-09 16:55:23 +0100283/* sndbuf consumer: actual data transfer of one target chunk with RDMA write */
284static int smc_tx_rdma_write(struct smc_connection *conn, int peer_rmbe_offset,
Ursula Braunad6f3172019-02-04 13:44:44 +0100285 int num_sges, struct ib_rdma_wr *rdma_wr)
Ursula Braune6727f32017-01-09 16:55:23 +0100286{
287 struct smc_link_group *lgr = conn->lgr;
Karsten Graul387707f2020-04-29 17:10:40 +0200288 struct smc_link *link = conn->lnk;
Ursula Braune6727f32017-01-09 16:55:23 +0100289 int rc;
290
Ursula Braunad6f3172019-02-04 13:44:44 +0100291 rdma_wr->wr.wr_id = smc_wr_tx_get_next_wr_id(link);
292 rdma_wr->wr.num_sge = num_sges;
293 rdma_wr->remote_addr =
Karsten Graul387707f2020-04-29 17:10:40 +0200294 lgr->rtokens[conn->rtoken_idx][link->link_idx].dma_addr +
Ursula Braune6727f32017-01-09 16:55:23 +0100295 /* RMBE within RMB */
Hans Wippel95d8d2632018-05-18 09:34:13 +0200296 conn->tx_off +
Ursula Braune6727f32017-01-09 16:55:23 +0100297 /* offset within RMBE */
298 peer_rmbe_offset;
Karsten Graul387707f2020-04-29 17:10:40 +0200299 rdma_wr->rkey = lgr->rtokens[conn->rtoken_idx][link->link_idx].rkey;
Ursula Braunad6f3172019-02-04 13:44:44 +0100300 rc = ib_post_send(link->roce_qp, &rdma_wr->wr, NULL);
Ursula Braunb2900982019-10-21 16:13:08 +0200301 if (rc)
Karsten Graul87523932020-05-01 12:48:09 +0200302 smcr_link_down_cond_sched(link);
Ursula Braune6727f32017-01-09 16:55:23 +0100303 return rc;
304}
305
306/* sndbuf consumer */
307static inline void smc_tx_advance_cursors(struct smc_connection *conn,
308 union smc_host_cursor *prod,
309 union smc_host_cursor *sent,
310 size_t len)
311{
312 smc_curs_add(conn->peer_rmbe_size, prod, len);
313 /* increased in recv tasklet smc_cdc_msg_rcv() */
314 smp_mb__before_atomic();
315 /* data in flight reduces usable snd_wnd */
316 atomic_sub(len, &conn->peer_rmbe_space);
317 /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
318 smp_mb__after_atomic();
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200319 smc_curs_add(conn->sndbuf_desc->len, sent, len);
Ursula Braune6727f32017-01-09 16:55:23 +0100320}
321
Hans Wippelbe244f22018-06-28 19:05:10 +0200322/* SMC-R helper for smc_tx_rdma_writes() */
323static int smcr_tx_rdma_writes(struct smc_connection *conn, size_t len,
324 size_t src_off, size_t src_len,
Ursula Braunad6f3172019-02-04 13:44:44 +0100325 size_t dst_off, size_t dst_len,
326 struct smc_rdma_wr *wr_rdma_buf)
Ursula Braune6727f32017-01-09 16:55:23 +0100327{
Karsten Graul387707f2020-04-29 17:10:40 +0200328 struct smc_link *link = conn->lnk;
329
Hans Wippelbe244f22018-06-28 19:05:10 +0200330 dma_addr_t dma_addr =
Karsten Graul387707f2020-04-29 17:10:40 +0200331 sg_dma_address(conn->sndbuf_desc->sgt[link->link_idx].sgl);
Hans Wippelbe244f22018-06-28 19:05:10 +0200332 int src_len_sum = src_len, dst_len_sum = dst_len;
Hans Wippelbe244f22018-06-28 19:05:10 +0200333 int sent_count = src_off;
334 int srcchunk, dstchunk;
Ursula Braune6727f32017-01-09 16:55:23 +0100335 int num_sges;
336 int rc;
337
Ursula Braune6727f32017-01-09 16:55:23 +0100338 for (dstchunk = 0; dstchunk < 2; dstchunk++) {
Ursula Braunad6f3172019-02-04 13:44:44 +0100339 struct ib_sge *sge =
340 wr_rdma_buf->wr_tx_rdma[dstchunk].wr.sg_list;
341
Ursula Braune6727f32017-01-09 16:55:23 +0100342 num_sges = 0;
343 for (srcchunk = 0; srcchunk < 2; srcchunk++) {
Ursula Braunad6f3172019-02-04 13:44:44 +0100344 sge[srcchunk].addr = dma_addr + src_off;
345 sge[srcchunk].length = src_len;
Ursula Braune6727f32017-01-09 16:55:23 +0100346 num_sges++;
Hans Wippelbe244f22018-06-28 19:05:10 +0200347
Ursula Braune6727f32017-01-09 16:55:23 +0100348 src_off += src_len;
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200349 if (src_off >= conn->sndbuf_desc->len)
350 src_off -= conn->sndbuf_desc->len;
Ursula Braune6727f32017-01-09 16:55:23 +0100351 /* modulo in send ring */
352 if (src_len_sum == dst_len)
353 break; /* either on 1st or 2nd iteration */
354 /* prepare next (== 2nd) iteration */
355 src_len = dst_len - src_len; /* remainder */
356 src_len_sum += src_len;
357 }
Ursula Braunad6f3172019-02-04 13:44:44 +0100358 rc = smc_tx_rdma_write(conn, dst_off, num_sges,
359 &wr_rdma_buf->wr_tx_rdma[dstchunk]);
Ursula Braune6727f32017-01-09 16:55:23 +0100360 if (rc)
361 return rc;
362 if (dst_len_sum == len)
363 break; /* either on 1st or 2nd iteration */
364 /* prepare next (== 2nd) iteration */
365 dst_off = 0; /* modulo offset in RMBE ring buffer */
366 dst_len = len - dst_len; /* remainder */
367 dst_len_sum += dst_len;
Hans Wippelbe244f22018-06-28 19:05:10 +0200368 src_len = min_t(int, dst_len, conn->sndbuf_desc->len -
369 sent_count);
Ursula Braune6727f32017-01-09 16:55:23 +0100370 src_len_sum = src_len;
371 }
Hans Wippelbe244f22018-06-28 19:05:10 +0200372 return 0;
373}
374
375/* SMC-D helper for smc_tx_rdma_writes() */
376static int smcd_tx_rdma_writes(struct smc_connection *conn, size_t len,
377 size_t src_off, size_t src_len,
378 size_t dst_off, size_t dst_len)
379{
380 int src_len_sum = src_len, dst_len_sum = dst_len;
381 int srcchunk, dstchunk;
382 int rc;
383
384 for (dstchunk = 0; dstchunk < 2; dstchunk++) {
385 for (srcchunk = 0; srcchunk < 2; srcchunk++) {
386 void *data = conn->sndbuf_desc->cpu_addr + src_off;
387
388 rc = smcd_tx_ism_write(conn, data, src_len, dst_off +
389 sizeof(struct smcd_cdc_msg), 0);
390 if (rc)
391 return rc;
392 dst_off += src_len;
393 src_off += src_len;
394 if (src_off >= conn->sndbuf_desc->len)
395 src_off -= conn->sndbuf_desc->len;
396 /* modulo in send ring */
397 if (src_len_sum == dst_len)
398 break; /* either on 1st or 2nd iteration */
399 /* prepare next (== 2nd) iteration */
400 src_len = dst_len - src_len; /* remainder */
401 src_len_sum += src_len;
402 }
403 if (dst_len_sum == len)
404 break; /* either on 1st or 2nd iteration */
405 /* prepare next (== 2nd) iteration */
406 dst_off = 0; /* modulo offset in RMBE ring buffer */
407 dst_len = len - dst_len; /* remainder */
408 dst_len_sum += dst_len;
409 src_len = min_t(int, dst_len, conn->sndbuf_desc->len - src_off);
410 src_len_sum = src_len;
411 }
412 return 0;
413}
414
Ursula Braune6727f32017-01-09 16:55:23 +0100415/* sndbuf consumer: prepare all necessary (src&dst) chunks of data transmit;
416 * usable snd_wnd as max transmit
417 */
Ursula Braunad6f3172019-02-04 13:44:44 +0100418static int smc_tx_rdma_writes(struct smc_connection *conn,
419 struct smc_rdma_wr *wr_rdma_buf)
Ursula Braune6727f32017-01-09 16:55:23 +0100420{
Hans Wippelbe244f22018-06-28 19:05:10 +0200421 size_t len, src_len, dst_off, dst_len; /* current chunk values */
Ursula Braune6727f32017-01-09 16:55:23 +0100422 union smc_host_cursor sent, prep, prod, cons;
Ursula Braune6727f32017-01-09 16:55:23 +0100423 struct smc_cdc_producer_flags *pflags;
424 int to_send, rmbespace;
Ursula Braune6727f32017-01-09 16:55:23 +0100425 int rc;
426
427 /* source: sndbuf */
Stefan Rasplbac6de72018-07-23 13:53:09 +0200428 smc_curs_copy(&sent, &conn->tx_curs_sent, conn);
429 smc_curs_copy(&prep, &conn->tx_curs_prep, conn);
Ursula Braune6727f32017-01-09 16:55:23 +0100430 /* cf. wmem_alloc - (snd_max - snd_una) */
431 to_send = smc_curs_diff(conn->sndbuf_desc->len, &sent, &prep);
432 if (to_send <= 0)
433 return 0;
434
435 /* destination: RMBE */
436 /* cf. snd_wnd */
437 rmbespace = atomic_read(&conn->peer_rmbe_space);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200438 if (rmbespace <= 0) {
Guvenc Gulce194730a2021-06-16 16:52:58 +0200439 struct smc_sock *smc = container_of(conn, struct smc_sock,
440 conn);
441 SMC_STAT_RMB_TX_PEER_FULL(smc, !conn->lnk);
Ursula Braune6727f32017-01-09 16:55:23 +0100442 return 0;
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200443 }
Stefan Rasplbac6de72018-07-23 13:53:09 +0200444 smc_curs_copy(&prod, &conn->local_tx_ctrl.prod, conn);
445 smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
Ursula Braune6727f32017-01-09 16:55:23 +0100446
447 /* if usable snd_wnd closes ask peer to advertise once it opens again */
448 pflags = &conn->local_tx_ctrl.prod_flags;
449 pflags->write_blocked = (to_send >= rmbespace);
450 /* cf. usable snd_wnd */
451 len = min(to_send, rmbespace);
452
453 /* initialize variables for first iteration of subsequent nested loop */
Ursula Braune6727f32017-01-09 16:55:23 +0100454 dst_off = prod.count;
455 if (prod.wrap == cons.wrap) {
456 /* the filled destination area is unwrapped,
457 * hence the available free destination space is wrapped
458 * and we need 2 destination chunks of sum len; start with 1st
459 * which is limited by what's available in sndbuf
460 */
461 dst_len = min_t(size_t,
462 conn->peer_rmbe_size - prod.count, len);
463 } else {
464 /* the filled destination area is wrapped,
465 * hence the available free destination space is unwrapped
466 * and we need a single destination chunk of entire len
467 */
468 dst_len = len;
469 }
Ursula Braune6727f32017-01-09 16:55:23 +0100470 /* dst_len determines the maximum src_len */
471 if (sent.count + dst_len <= conn->sndbuf_desc->len) {
472 /* unwrapped src case: single chunk of entire dst_len */
473 src_len = dst_len;
474 } else {
475 /* wrapped src case: 2 chunks of sum dst_len; start with 1st: */
476 src_len = conn->sndbuf_desc->len - sent.count;
477 }
Hans Wippelbe244f22018-06-28 19:05:10 +0200478
479 if (conn->lgr->is_smcd)
480 rc = smcd_tx_rdma_writes(conn, len, sent.count, src_len,
481 dst_off, dst_len);
482 else
483 rc = smcr_tx_rdma_writes(conn, len, sent.count, src_len,
Ursula Braunad6f3172019-02-04 13:44:44 +0100484 dst_off, dst_len, wr_rdma_buf);
Hans Wippelbe244f22018-06-28 19:05:10 +0200485 if (rc)
486 return rc;
Ursula Braune6727f32017-01-09 16:55:23 +0100487
Stefan Rasplde8474e2018-05-23 16:38:11 +0200488 if (conn->urg_tx_pend && len == to_send)
489 pflags->urg_data_present = 1;
Ursula Braune6727f32017-01-09 16:55:23 +0100490 smc_tx_advance_cursors(conn, &prod, &sent, len);
491 /* update connection's cursors with advanced local cursors */
Stefan Rasplbac6de72018-07-23 13:53:09 +0200492 smc_curs_copy(&conn->local_tx_ctrl.prod, &prod, conn);
Ursula Braune6727f32017-01-09 16:55:23 +0100493 /* dst: peer RMBE */
Stefan Rasplbac6de72018-07-23 13:53:09 +0200494 smc_curs_copy(&conn->tx_curs_sent, &sent, conn);/* src: local sndbuf */
Ursula Braune6727f32017-01-09 16:55:23 +0100495
496 return 0;
497}
498
499/* Wakeup sndbuf consumers from any context (IRQ or process)
500 * since there is more data to transmit; usable snd_wnd as max transmit
501 */
Karsten Graul95f7f3e2021-10-07 16:14:40 +0200502static int smcr_tx_sndbuf_nonempty(struct smc_connection *conn)
Ursula Braune6727f32017-01-09 16:55:23 +0100503{
Ursula Brauncecc7a32019-02-21 13:00:58 +0100504 struct smc_cdc_producer_flags *pflags = &conn->local_tx_ctrl.prod_flags;
Karsten Graulc6f02eb2020-05-04 14:18:38 +0200505 struct smc_link *link = conn->lnk;
Ursula Braunad6f3172019-02-04 13:44:44 +0100506 struct smc_rdma_wr *wr_rdma_buf;
Ursula Braune6727f32017-01-09 16:55:23 +0100507 struct smc_cdc_tx_pend *pend;
508 struct smc_wr_buf *wr_buf;
509 int rc;
510
Karsten Graul95f7f3e2021-10-07 16:14:40 +0200511 if (!link || !smc_wr_tx_link_hold(link))
512 return -ENOLINK;
Karsten Graulc6f02eb2020-05-04 14:18:38 +0200513 rc = smc_cdc_get_free_slot(conn, link, &wr_buf, &wr_rdma_buf, &pend);
Ursula Braune6727f32017-01-09 16:55:23 +0100514 if (rc < 0) {
Karsten Graul95f7f3e2021-10-07 16:14:40 +0200515 smc_wr_tx_link_put(link);
Ursula Braune6727f32017-01-09 16:55:23 +0100516 if (rc == -EBUSY) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100517 struct smc_sock *smc =
518 container_of(conn, struct smc_sock, conn);
519
Karsten Graul33f3fcc2019-01-30 18:51:05 +0100520 if (smc->sk.sk_err == ECONNABORTED)
521 return sock_error(&smc->sk);
Ursula Braunb2900982019-10-21 16:13:08 +0200522 if (conn->killed)
523 return -EPIPE;
Ursula Braune6727f32017-01-09 16:55:23 +0100524 rc = 0;
Karsten Graul22ef4732020-09-10 18:48:29 +0200525 mod_delayed_work(conn->lgr->tx_wq, &conn->tx_work,
Ursula Braunb2900982019-10-21 16:13:08 +0200526 SMC_TX_WORK_DELAY);
Ursula Braune6727f32017-01-09 16:55:23 +0100527 }
Karsten Graul33f3fcc2019-01-30 18:51:05 +0100528 return rc;
Ursula Braune6727f32017-01-09 16:55:23 +0100529 }
530
Karsten Graul33f3fcc2019-01-30 18:51:05 +0100531 spin_lock_bh(&conn->send_lock);
Karsten Graulc6f02eb2020-05-04 14:18:38 +0200532 if (link != conn->lnk) {
533 /* link of connection changed, tx_work will restart */
534 smc_wr_tx_put_slot(link,
535 (struct smc_wr_tx_pend_priv *)pend);
536 rc = -ENOLINK;
537 goto out_unlock;
538 }
Ursula Brauncecc7a32019-02-21 13:00:58 +0100539 if (!pflags->urg_data_present) {
Ursula Braunad6f3172019-02-04 13:44:44 +0100540 rc = smc_tx_rdma_writes(conn, wr_rdma_buf);
Stefan Rasplde8474e2018-05-23 16:38:11 +0200541 if (rc) {
Karsten Graulc6f02eb2020-05-04 14:18:38 +0200542 smc_wr_tx_put_slot(link,
Stefan Rasplde8474e2018-05-23 16:38:11 +0200543 (struct smc_wr_tx_pend_priv *)pend);
544 goto out_unlock;
545 }
Ursula Braune6727f32017-01-09 16:55:23 +0100546 }
547
548 rc = smc_cdc_msg_send(conn, wr_buf, pend);
Stefan Rasplde8474e2018-05-23 16:38:11 +0200549 if (!rc && pflags->urg_data_present) {
550 pflags->urg_data_pending = 0;
551 pflags->urg_data_present = 0;
552 }
Ursula Braune6727f32017-01-09 16:55:23 +0100553
554out_unlock:
555 spin_unlock_bh(&conn->send_lock);
Karsten Graul95f7f3e2021-10-07 16:14:40 +0200556 smc_wr_tx_link_put(link);
Karsten Graul8f3d65c2021-08-09 11:05:56 +0200557 return rc;
558}
559
Hans Wippelbe244f22018-06-28 19:05:10 +0200560static int smcd_tx_sndbuf_nonempty(struct smc_connection *conn)
561{
562 struct smc_cdc_producer_flags *pflags = &conn->local_tx_ctrl.prod_flags;
563 int rc = 0;
564
565 spin_lock_bh(&conn->send_lock);
566 if (!pflags->urg_data_present)
Ursula Braunad6f3172019-02-04 13:44:44 +0100567 rc = smc_tx_rdma_writes(conn, NULL);
Hans Wippelbe244f22018-06-28 19:05:10 +0200568 if (!rc)
569 rc = smcd_cdc_msg_send(conn);
570
571 if (!rc && pflags->urg_data_present) {
572 pflags->urg_data_pending = 0;
573 pflags->urg_data_present = 0;
574 }
575 spin_unlock_bh(&conn->send_lock);
576 return rc;
577}
578
579int smc_tx_sndbuf_nonempty(struct smc_connection *conn)
580{
581 int rc;
582
Ursula Braunb2900982019-10-21 16:13:08 +0200583 if (conn->killed ||
584 conn->local_rx_ctrl.conn_state_flags.peer_conn_abort)
585 return -EPIPE; /* connection being aborted */
Hans Wippelbe244f22018-06-28 19:05:10 +0200586 if (conn->lgr->is_smcd)
587 rc = smcd_tx_sndbuf_nonempty(conn);
588 else
589 rc = smcr_tx_sndbuf_nonempty(conn);
590
Karsten Graul5bc056d2019-02-12 16:29:51 +0100591 if (!rc) {
592 /* trigger socket release if connection is closing */
593 struct smc_sock *smc = container_of(conn, struct smc_sock,
594 conn);
595 smc_close_wake_tx_prepared(smc);
596 }
Hans Wippelbe244f22018-06-28 19:05:10 +0200597 return rc;
598}
599
Ursula Braune6727f32017-01-09 16:55:23 +0100600/* Wakeup sndbuf consumers from process context
601 * since there is more data to transmit
602 */
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700603void smc_tx_work(struct work_struct *work)
Ursula Braune6727f32017-01-09 16:55:23 +0100604{
Ursula Braun18e537c2017-09-21 09:16:33 +0200605 struct smc_connection *conn = container_of(to_delayed_work(work),
Ursula Braune6727f32017-01-09 16:55:23 +0100606 struct smc_connection,
607 tx_work);
608 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
Ursula Braun90cacb22017-04-10 14:57:59 +0200609 int rc;
Ursula Braune6727f32017-01-09 16:55:23 +0100610
611 lock_sock(&smc->sk);
Ursula Braunb2900982019-10-21 16:13:08 +0200612 if (smc->sk.sk_err)
Ursula Braun1a0a04c2018-01-25 11:15:36 +0100613 goto out;
614
Ursula Braun90cacb22017-04-10 14:57:59 +0200615 rc = smc_tx_sndbuf_nonempty(conn);
616 if (!rc && conn->local_rx_ctrl.prod_flags.write_blocked &&
617 !atomic_read(&conn->bytes_to_rcv))
618 conn->local_rx_ctrl.prod_flags.write_blocked = 0;
Ursula Braun1a0a04c2018-01-25 11:15:36 +0100619
620out:
Ursula Braune6727f32017-01-09 16:55:23 +0100621 release_sock(&smc->sk);
622}
623
Stefan Rasplde8474e2018-05-23 16:38:11 +0200624void smc_tx_consumer_update(struct smc_connection *conn, bool force)
Ursula Braun952310c2017-01-09 16:55:24 +0100625{
Ursula Braun99be51f2018-07-18 15:22:49 +0200626 union smc_host_cursor cfed, cons, prod;
627 int sender_free = conn->rmb_desc->len;
Ursula Braun6b5771a2017-12-07 13:38:48 +0100628 int to_confirm;
Ursula Braun952310c2017-01-09 16:55:24 +0100629
Stefan Rasplbac6de72018-07-23 13:53:09 +0200630 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
631 smc_curs_copy(&cfed, &conn->rx_curs_confirmed, conn);
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200632 to_confirm = smc_curs_diff(conn->rmb_desc->len, &cfed, &cons);
Ursula Braun99be51f2018-07-18 15:22:49 +0200633 if (to_confirm > conn->rmbe_update_limit) {
Stefan Rasplbac6de72018-07-23 13:53:09 +0200634 smc_curs_copy(&prod, &conn->local_rx_ctrl.prod, conn);
Ursula Braun99be51f2018-07-18 15:22:49 +0200635 sender_free = conn->rmb_desc->len -
Ursula Braunb8649ef2019-02-04 13:44:45 +0100636 smc_curs_diff_large(conn->rmb_desc->len,
637 &cfed, &prod);
Ursula Braun99be51f2018-07-18 15:22:49 +0200638 }
Ursula Braun952310c2017-01-09 16:55:24 +0100639
640 if (conn->local_rx_ctrl.prod_flags.cons_curs_upd_req ||
Stefan Rasplde8474e2018-05-23 16:38:11 +0200641 force ||
Ursula Braun952310c2017-01-09 16:55:24 +0100642 ((to_confirm > conn->rmbe_update_limit) &&
Ursula Braun99be51f2018-07-18 15:22:49 +0200643 ((sender_free <= (conn->rmb_desc->len / 2)) ||
Ursula Braun952310c2017-01-09 16:55:24 +0100644 conn->local_rx_ctrl.prod_flags.write_blocked))) {
Ursula Braunb2900982019-10-21 16:13:08 +0200645 if (conn->killed ||
646 conn->local_rx_ctrl.conn_state_flags.peer_conn_abort)
647 return;
Ursula Braun1a0a04c2018-01-25 11:15:36 +0100648 if ((smc_cdc_get_slot_and_msg_send(conn) < 0) &&
Ursula Braunb2900982019-10-21 16:13:08 +0200649 !conn->killed) {
Karsten Graul22ef4732020-09-10 18:48:29 +0200650 queue_delayed_work(conn->lgr->tx_wq, &conn->tx_work,
651 SMC_TX_WORK_DELAY);
Ursula Braun952310c2017-01-09 16:55:24 +0100652 return;
653 }
Ursula Braun952310c2017-01-09 16:55:24 +0100654 }
655 if (conn->local_rx_ctrl.prod_flags.write_blocked &&
656 !atomic_read(&conn->bytes_to_rcv))
657 conn->local_rx_ctrl.prod_flags.write_blocked = 0;
658}
659
Ursula Braune6727f32017-01-09 16:55:23 +0100660/***************************** send initialize *******************************/
661
662/* Initialize send properties on connection establishment. NB: not __init! */
663void smc_tx_init(struct smc_sock *smc)
664{
665 smc->sk.sk_write_space = smc_tx_write_space;
Ursula Braune6727f32017-01-09 16:55:23 +0100666}