blob: 824f096ee7dee07e314ac985314849a7b0d9f531 [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"
30
Karsten Graul16297d12019-02-12 16:29:52 +010031#define SMC_TX_WORK_DELAY 0
Ursula Braun01d2f7e2018-04-26 17:18:22 +020032#define SMC_TX_CORK_DELAY (HZ >> 2) /* 250 ms */
Ursula Braun18e537c2017-09-21 09:16:33 +020033
Ursula Braune6727f32017-01-09 16:55:23 +010034/***************************** sndbuf producer *******************************/
35
36/* callback implementation for sk.sk_write_space()
Stefan Rasplde8474e2018-05-23 16:38:11 +020037 * to wakeup sndbuf producers that blocked with smc_tx_wait().
Ursula Braune6727f32017-01-09 16:55:23 +010038 * called under sk_socket lock.
39 */
40static void smc_tx_write_space(struct sock *sk)
41{
42 struct socket *sock = sk->sk_socket;
43 struct smc_sock *smc = smc_sk(sk);
44 struct socket_wq *wq;
45
46 /* similar to sk_stream_write_space */
47 if (atomic_read(&smc->conn.sndbuf_space) && sock) {
48 clear_bit(SOCK_NOSPACE, &sock->flags);
49 rcu_read_lock();
50 wq = rcu_dereference(sk->sk_wq);
51 if (skwq_has_sleeper(wq))
52 wake_up_interruptible_poll(&wq->wait,
Linus Torvaldsa9a08842018-02-11 14:34:03 -080053 EPOLLOUT | EPOLLWRNORM |
54 EPOLLWRBAND);
Ursula Braune6727f32017-01-09 16:55:23 +010055 if (wq && wq->fasync_list && !(sk->sk_shutdown & SEND_SHUTDOWN))
56 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
57 rcu_read_unlock();
58 }
59}
60
Stefan Rasplde8474e2018-05-23 16:38:11 +020061/* Wakeup sndbuf producers that blocked with smc_tx_wait().
Ursula Braune6727f32017-01-09 16:55:23 +010062 * Cf. tcp_data_snd_check()=>tcp_check_space()=>tcp_new_space().
63 */
64void smc_tx_sndbuf_nonfull(struct smc_sock *smc)
65{
66 if (smc->sk.sk_socket &&
67 test_bit(SOCK_NOSPACE, &smc->sk.sk_socket->flags))
68 smc->sk.sk_write_space(&smc->sk);
69}
70
Stefan Rasplde8474e2018-05-23 16:38:11 +020071/* blocks sndbuf producer until at least one byte of free space available
72 * or urgent Byte was consumed
73 */
74static int smc_tx_wait(struct smc_sock *smc, int flags)
Ursula Braune6727f32017-01-09 16:55:23 +010075{
76 DEFINE_WAIT_FUNC(wait, woken_wake_function);
77 struct smc_connection *conn = &smc->conn;
78 struct sock *sk = &smc->sk;
Ursula Braune6727f32017-01-09 16:55:23 +010079 long timeo;
80 int rc = 0;
81
82 /* similar to sk_stream_wait_memory */
83 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
Ursula Braune6727f32017-01-09 16:55:23 +010084 add_wait_queue(sk_sleep(sk), &wait);
85 while (1) {
86 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
87 if (sk->sk_err ||
88 (sk->sk_shutdown & SEND_SHUTDOWN) ||
Ursula Braunb2900982019-10-21 16:13:08 +020089 conn->killed ||
Ursula Braune6727f32017-01-09 16:55:23 +010090 conn->local_tx_ctrl.conn_state_flags.peer_done_writing) {
91 rc = -EPIPE;
92 break;
93 }
Ursula Braunaa377e62018-01-24 10:28:17 +010094 if (smc_cdc_rxed_any_close(conn)) {
Ursula Braune6727f32017-01-09 16:55:23 +010095 rc = -ECONNRESET;
96 break;
97 }
98 if (!timeo) {
Jason Baron4651d182019-08-19 14:36:01 -040099 /* ensure EPOLLOUT is subsequently generated */
100 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Ursula Braune6727f32017-01-09 16:55:23 +0100101 rc = -EAGAIN;
102 break;
103 }
104 if (signal_pending(current)) {
105 rc = sock_intr_errno(timeo);
106 break;
107 }
108 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +0200109 if (atomic_read(&conn->sndbuf_space) && !conn->urg_tx_pend)
110 break; /* at least 1 byte of free & no urgent data */
Ursula Braune6727f32017-01-09 16:55:23 +0100111 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Ursula Braune6727f32017-01-09 16:55:23 +0100112 sk_wait_event(sk, &timeo,
113 sk->sk_err ||
114 (sk->sk_shutdown & SEND_SHUTDOWN) ||
Ursula Braunaa377e62018-01-24 10:28:17 +0100115 smc_cdc_rxed_any_close(conn) ||
Stefan Rasplde8474e2018-05-23 16:38:11 +0200116 (atomic_read(&conn->sndbuf_space) &&
117 !conn->urg_tx_pend),
Ursula Braune6727f32017-01-09 16:55:23 +0100118 &wait);
Ursula Braune6727f32017-01-09 16:55:23 +0100119 }
120 remove_wait_queue(sk_sleep(sk), &wait);
121 return rc;
122}
123
Ursula Braun01d2f7e2018-04-26 17:18:22 +0200124static bool smc_tx_is_corked(struct smc_sock *smc)
125{
126 struct tcp_sock *tp = tcp_sk(smc->clcsock->sk);
127
128 return (tp->nonagle & TCP_NAGLE_CORK) ? true : false;
129}
130
Ursula Braune6727f32017-01-09 16:55:23 +0100131/* sndbuf producer: main API called by socket layer.
132 * called under sock lock.
133 */
134int smc_tx_sendmsg(struct smc_sock *smc, struct msghdr *msg, size_t len)
135{
136 size_t copylen, send_done = 0, send_remaining = len;
137 size_t chunk_len, chunk_off, chunk_len_sum;
138 struct smc_connection *conn = &smc->conn;
139 union smc_host_cursor prep;
140 struct sock *sk = &smc->sk;
141 char *sndbuf_base;
142 int tx_cnt_prep;
143 int writespace;
144 int rc, chunk;
145
146 /* This should be in poll */
147 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
148
149 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) {
150 rc = -EPIPE;
151 goto out_err;
152 }
153
154 while (msg_data_left(msg)) {
155 if (sk->sk_state == SMC_INIT)
156 return -ENOTCONN;
157 if (smc->sk.sk_shutdown & SEND_SHUTDOWN ||
Ursula Braunb38d7322017-01-09 16:55:25 +0100158 (smc->sk.sk_err == ECONNABORTED) ||
Ursula Braunb2900982019-10-21 16:13:08 +0200159 conn->killed)
Ursula Braune6727f32017-01-09 16:55:23 +0100160 return -EPIPE;
161 if (smc_cdc_rxed_any_close(conn))
162 return send_done ?: -ECONNRESET;
163
Stefan Rasplde8474e2018-05-23 16:38:11 +0200164 if (msg->msg_flags & MSG_OOB)
165 conn->local_tx_ctrl.prod_flags.urg_data_pending = 1;
166
167 if (!atomic_read(&conn->sndbuf_space) || conn->urg_tx_pend) {
Karsten Graul6889b362019-01-30 18:51:03 +0100168 if (send_done)
169 return send_done;
Stefan Rasplde8474e2018-05-23 16:38:11 +0200170 rc = smc_tx_wait(smc, msg->msg_flags);
Karsten Graul6889b362019-01-30 18:51:03 +0100171 if (rc)
Ursula Braune6727f32017-01-09 16:55:23 +0100172 goto out_err;
Ursula Braune6727f32017-01-09 16:55:23 +0100173 continue;
174 }
175
176 /* initialize variables for 1st iteration of subsequent loop */
Stefan Rasplde8474e2018-05-23 16:38:11 +0200177 /* could be just 1 byte, even after smc_tx_wait above */
Ursula Braune6727f32017-01-09 16:55:23 +0100178 writespace = atomic_read(&conn->sndbuf_space);
179 /* not more than what user space asked for */
180 copylen = min_t(size_t, send_remaining, writespace);
181 /* determine start of sndbuf */
182 sndbuf_base = conn->sndbuf_desc->cpu_addr;
Stefan Rasplbac6de72018-07-23 13:53:09 +0200183 smc_curs_copy(&prep, &conn->tx_curs_prep, conn);
Ursula Braune6727f32017-01-09 16:55:23 +0100184 tx_cnt_prep = prep.count;
185 /* determine chunks where to write into sndbuf */
186 /* either unwrapped case, or 1st chunk of wrapped case */
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200187 chunk_len = min_t(size_t, copylen, conn->sndbuf_desc->len -
188 tx_cnt_prep);
Ursula Braune6727f32017-01-09 16:55:23 +0100189 chunk_len_sum = chunk_len;
190 chunk_off = tx_cnt_prep;
Ursula Braun10428dd2017-07-28 13:56:22 +0200191 smc_sndbuf_sync_sg_for_cpu(conn);
Ursula Braune6727f32017-01-09 16:55:23 +0100192 for (chunk = 0; chunk < 2; chunk++) {
193 rc = memcpy_from_msg(sndbuf_base + chunk_off,
194 msg, chunk_len);
195 if (rc) {
Ursula Braun10428dd2017-07-28 13:56:22 +0200196 smc_sndbuf_sync_sg_for_device(conn);
Ursula Braune6727f32017-01-09 16:55:23 +0100197 if (send_done)
198 return send_done;
199 goto out_err;
200 }
201 send_done += chunk_len;
202 send_remaining -= chunk_len;
203
204 if (chunk_len_sum == copylen)
205 break; /* either on 1st or 2nd iteration */
206 /* prepare next (== 2nd) iteration */
207 chunk_len = copylen - chunk_len; /* remainder */
208 chunk_len_sum += chunk_len;
209 chunk_off = 0; /* modulo offset in send ring buffer */
210 }
Ursula Braun10428dd2017-07-28 13:56:22 +0200211 smc_sndbuf_sync_sg_for_device(conn);
Ursula Braune6727f32017-01-09 16:55:23 +0100212 /* update cursors */
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200213 smc_curs_add(conn->sndbuf_desc->len, &prep, copylen);
Stefan Rasplbac6de72018-07-23 13:53:09 +0200214 smc_curs_copy(&conn->tx_curs_prep, &prep, conn);
Ursula Braune6727f32017-01-09 16:55:23 +0100215 /* increased in send tasklet smc_cdc_tx_handler() */
216 smp_mb__before_atomic();
217 atomic_sub(copylen, &conn->sndbuf_space);
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200218 /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
Ursula Braune6727f32017-01-09 16:55:23 +0100219 smp_mb__after_atomic();
220 /* since we just produced more new data into sndbuf,
221 * trigger sndbuf consumer: RDMA write into peer RMBE and CDC
222 */
Stefan Rasplde8474e2018-05-23 16:38:11 +0200223 if ((msg->msg_flags & MSG_OOB) && !send_remaining)
224 conn->urg_tx_pend = true;
Ursula Braun01d2f7e2018-04-26 17:18:22 +0200225 if ((msg->msg_flags & MSG_MORE || smc_tx_is_corked(smc)) &&
226 (atomic_read(&conn->sndbuf_space) >
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200227 (conn->sndbuf_desc->len >> 1)))
Ursula Braun01d2f7e2018-04-26 17:18:22 +0200228 /* for a corked socket defer the RDMA writes if there
229 * is still sufficient sndbuf_space available
230 */
231 schedule_delayed_work(&conn->tx_work,
232 SMC_TX_CORK_DELAY);
233 else
234 smc_tx_sndbuf_nonempty(conn);
Ursula Braune6727f32017-01-09 16:55:23 +0100235 } /* while (msg_data_left(msg)) */
236
237 return send_done;
238
239out_err:
240 rc = sk_stream_error(sk, msg->msg_flags, rc);
241 /* make sure we wake any epoll edge trigger waiter */
242 if (unlikely(rc == -EAGAIN))
243 sk->sk_write_space(sk);
244 return rc;
245}
246
247/***************************** sndbuf consumer *******************************/
248
Hans Wippelbe244f22018-06-28 19:05:10 +0200249/* sndbuf consumer: actual data transfer of one target chunk with ISM write */
250int smcd_tx_ism_write(struct smc_connection *conn, void *data, size_t len,
251 u32 offset, int signal)
252{
253 struct smc_ism_position pos;
254 int rc;
255
256 memset(&pos, 0, sizeof(pos));
257 pos.token = conn->peer_token;
258 pos.index = conn->peer_rmbe_idx;
259 pos.offset = conn->tx_off + offset;
260 pos.signal = signal;
261 rc = smc_ism_write(conn->lgr->smcd, &pos, data, len);
262 if (rc)
263 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
264 return rc;
265}
266
Ursula Braune6727f32017-01-09 16:55:23 +0100267/* sndbuf consumer: actual data transfer of one target chunk with RDMA write */
268static int smc_tx_rdma_write(struct smc_connection *conn, int peer_rmbe_offset,
Ursula Braunad6f3172019-02-04 13:44:44 +0100269 int num_sges, struct ib_rdma_wr *rdma_wr)
Ursula Braune6727f32017-01-09 16:55:23 +0100270{
271 struct smc_link_group *lgr = conn->lgr;
Ursula Braune6727f32017-01-09 16:55:23 +0100272 struct smc_link *link;
273 int rc;
274
Ursula Braune6727f32017-01-09 16:55:23 +0100275 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braunad6f3172019-02-04 13:44:44 +0100276 rdma_wr->wr.wr_id = smc_wr_tx_get_next_wr_id(link);
277 rdma_wr->wr.num_sge = num_sges;
278 rdma_wr->remote_addr =
Ursula Braune6727f32017-01-09 16:55:23 +0100279 lgr->rtokens[conn->rtoken_idx][SMC_SINGLE_LINK].dma_addr +
280 /* RMBE within RMB */
Hans Wippel95d8d2632018-05-18 09:34:13 +0200281 conn->tx_off +
Ursula Braune6727f32017-01-09 16:55:23 +0100282 /* offset within RMBE */
283 peer_rmbe_offset;
Ursula Braunad6f3172019-02-04 13:44:44 +0100284 rdma_wr->rkey = lgr->rtokens[conn->rtoken_idx][SMC_SINGLE_LINK].rkey;
285 rc = ib_post_send(link->roce_qp, &rdma_wr->wr, NULL);
Ursula Braunb2900982019-10-21 16:13:08 +0200286 if (rc)
Ursula Braunb4772b32018-01-25 11:15:33 +0100287 smc_lgr_terminate(lgr);
Ursula Braune6727f32017-01-09 16:55:23 +0100288 return rc;
289}
290
291/* sndbuf consumer */
292static inline void smc_tx_advance_cursors(struct smc_connection *conn,
293 union smc_host_cursor *prod,
294 union smc_host_cursor *sent,
295 size_t len)
296{
297 smc_curs_add(conn->peer_rmbe_size, prod, len);
298 /* increased in recv tasklet smc_cdc_msg_rcv() */
299 smp_mb__before_atomic();
300 /* data in flight reduces usable snd_wnd */
301 atomic_sub(len, &conn->peer_rmbe_space);
302 /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
303 smp_mb__after_atomic();
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200304 smc_curs_add(conn->sndbuf_desc->len, sent, len);
Ursula Braune6727f32017-01-09 16:55:23 +0100305}
306
Hans Wippelbe244f22018-06-28 19:05:10 +0200307/* SMC-R helper for smc_tx_rdma_writes() */
308static int smcr_tx_rdma_writes(struct smc_connection *conn, size_t len,
309 size_t src_off, size_t src_len,
Ursula Braunad6f3172019-02-04 13:44:44 +0100310 size_t dst_off, size_t dst_len,
311 struct smc_rdma_wr *wr_rdma_buf)
Ursula Braune6727f32017-01-09 16:55:23 +0100312{
Hans Wippelbe244f22018-06-28 19:05:10 +0200313 dma_addr_t dma_addr =
314 sg_dma_address(conn->sndbuf_desc->sgt[SMC_SINGLE_LINK].sgl);
Hans Wippelbe244f22018-06-28 19:05:10 +0200315 int src_len_sum = src_len, dst_len_sum = dst_len;
Hans Wippelbe244f22018-06-28 19:05:10 +0200316 int sent_count = src_off;
317 int srcchunk, dstchunk;
Ursula Braune6727f32017-01-09 16:55:23 +0100318 int num_sges;
319 int rc;
320
Ursula Braune6727f32017-01-09 16:55:23 +0100321 for (dstchunk = 0; dstchunk < 2; dstchunk++) {
Ursula Braunad6f3172019-02-04 13:44:44 +0100322 struct ib_sge *sge =
323 wr_rdma_buf->wr_tx_rdma[dstchunk].wr.sg_list;
324
Ursula Braune6727f32017-01-09 16:55:23 +0100325 num_sges = 0;
326 for (srcchunk = 0; srcchunk < 2; srcchunk++) {
Ursula Braunad6f3172019-02-04 13:44:44 +0100327 sge[srcchunk].addr = dma_addr + src_off;
328 sge[srcchunk].length = src_len;
Ursula Braune6727f32017-01-09 16:55:23 +0100329 num_sges++;
Hans Wippelbe244f22018-06-28 19:05:10 +0200330
Ursula Braune6727f32017-01-09 16:55:23 +0100331 src_off += src_len;
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200332 if (src_off >= conn->sndbuf_desc->len)
333 src_off -= conn->sndbuf_desc->len;
Ursula Braune6727f32017-01-09 16:55:23 +0100334 /* modulo in send ring */
335 if (src_len_sum == dst_len)
336 break; /* either on 1st or 2nd iteration */
337 /* prepare next (== 2nd) iteration */
338 src_len = dst_len - src_len; /* remainder */
339 src_len_sum += src_len;
340 }
Ursula Braunad6f3172019-02-04 13:44:44 +0100341 rc = smc_tx_rdma_write(conn, dst_off, num_sges,
342 &wr_rdma_buf->wr_tx_rdma[dstchunk]);
Ursula Braune6727f32017-01-09 16:55:23 +0100343 if (rc)
344 return rc;
345 if (dst_len_sum == len)
346 break; /* either on 1st or 2nd iteration */
347 /* prepare next (== 2nd) iteration */
348 dst_off = 0; /* modulo offset in RMBE ring buffer */
349 dst_len = len - dst_len; /* remainder */
350 dst_len_sum += dst_len;
Hans Wippelbe244f22018-06-28 19:05:10 +0200351 src_len = min_t(int, dst_len, conn->sndbuf_desc->len -
352 sent_count);
Ursula Braune6727f32017-01-09 16:55:23 +0100353 src_len_sum = src_len;
354 }
Hans Wippelbe244f22018-06-28 19:05:10 +0200355 return 0;
356}
357
358/* SMC-D helper for smc_tx_rdma_writes() */
359static int smcd_tx_rdma_writes(struct smc_connection *conn, size_t len,
360 size_t src_off, size_t src_len,
361 size_t dst_off, size_t dst_len)
362{
363 int src_len_sum = src_len, dst_len_sum = dst_len;
364 int srcchunk, dstchunk;
365 int rc;
366
367 for (dstchunk = 0; dstchunk < 2; dstchunk++) {
368 for (srcchunk = 0; srcchunk < 2; srcchunk++) {
369 void *data = conn->sndbuf_desc->cpu_addr + src_off;
370
371 rc = smcd_tx_ism_write(conn, data, src_len, dst_off +
372 sizeof(struct smcd_cdc_msg), 0);
373 if (rc)
374 return rc;
375 dst_off += src_len;
376 src_off += src_len;
377 if (src_off >= conn->sndbuf_desc->len)
378 src_off -= conn->sndbuf_desc->len;
379 /* modulo in send ring */
380 if (src_len_sum == dst_len)
381 break; /* either on 1st or 2nd iteration */
382 /* prepare next (== 2nd) iteration */
383 src_len = dst_len - src_len; /* remainder */
384 src_len_sum += src_len;
385 }
386 if (dst_len_sum == len)
387 break; /* either on 1st or 2nd iteration */
388 /* prepare next (== 2nd) iteration */
389 dst_off = 0; /* modulo offset in RMBE ring buffer */
390 dst_len = len - dst_len; /* remainder */
391 dst_len_sum += dst_len;
392 src_len = min_t(int, dst_len, conn->sndbuf_desc->len - src_off);
393 src_len_sum = src_len;
394 }
395 return 0;
396}
397
Ursula Braune6727f32017-01-09 16:55:23 +0100398/* sndbuf consumer: prepare all necessary (src&dst) chunks of data transmit;
399 * usable snd_wnd as max transmit
400 */
Ursula Braunad6f3172019-02-04 13:44:44 +0100401static int smc_tx_rdma_writes(struct smc_connection *conn,
402 struct smc_rdma_wr *wr_rdma_buf)
Ursula Braune6727f32017-01-09 16:55:23 +0100403{
Hans Wippelbe244f22018-06-28 19:05:10 +0200404 size_t len, src_len, dst_off, dst_len; /* current chunk values */
Ursula Braune6727f32017-01-09 16:55:23 +0100405 union smc_host_cursor sent, prep, prod, cons;
Ursula Braune6727f32017-01-09 16:55:23 +0100406 struct smc_cdc_producer_flags *pflags;
407 int to_send, rmbespace;
Ursula Braune6727f32017-01-09 16:55:23 +0100408 int rc;
409
410 /* source: sndbuf */
Stefan Rasplbac6de72018-07-23 13:53:09 +0200411 smc_curs_copy(&sent, &conn->tx_curs_sent, conn);
412 smc_curs_copy(&prep, &conn->tx_curs_prep, conn);
Ursula Braune6727f32017-01-09 16:55:23 +0100413 /* cf. wmem_alloc - (snd_max - snd_una) */
414 to_send = smc_curs_diff(conn->sndbuf_desc->len, &sent, &prep);
415 if (to_send <= 0)
416 return 0;
417
418 /* destination: RMBE */
419 /* cf. snd_wnd */
420 rmbespace = atomic_read(&conn->peer_rmbe_space);
421 if (rmbespace <= 0)
422 return 0;
Stefan Rasplbac6de72018-07-23 13:53:09 +0200423 smc_curs_copy(&prod, &conn->local_tx_ctrl.prod, conn);
424 smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
Ursula Braune6727f32017-01-09 16:55:23 +0100425
426 /* if usable snd_wnd closes ask peer to advertise once it opens again */
427 pflags = &conn->local_tx_ctrl.prod_flags;
428 pflags->write_blocked = (to_send >= rmbespace);
429 /* cf. usable snd_wnd */
430 len = min(to_send, rmbespace);
431
432 /* initialize variables for first iteration of subsequent nested loop */
Ursula Braune6727f32017-01-09 16:55:23 +0100433 dst_off = prod.count;
434 if (prod.wrap == cons.wrap) {
435 /* the filled destination area is unwrapped,
436 * hence the available free destination space is wrapped
437 * and we need 2 destination chunks of sum len; start with 1st
438 * which is limited by what's available in sndbuf
439 */
440 dst_len = min_t(size_t,
441 conn->peer_rmbe_size - prod.count, len);
442 } else {
443 /* the filled destination area is wrapped,
444 * hence the available free destination space is unwrapped
445 * and we need a single destination chunk of entire len
446 */
447 dst_len = len;
448 }
Ursula Braune6727f32017-01-09 16:55:23 +0100449 /* dst_len determines the maximum src_len */
450 if (sent.count + dst_len <= conn->sndbuf_desc->len) {
451 /* unwrapped src case: single chunk of entire dst_len */
452 src_len = dst_len;
453 } else {
454 /* wrapped src case: 2 chunks of sum dst_len; start with 1st: */
455 src_len = conn->sndbuf_desc->len - sent.count;
456 }
Hans Wippelbe244f22018-06-28 19:05:10 +0200457
458 if (conn->lgr->is_smcd)
459 rc = smcd_tx_rdma_writes(conn, len, sent.count, src_len,
460 dst_off, dst_len);
461 else
462 rc = smcr_tx_rdma_writes(conn, len, sent.count, src_len,
Ursula Braunad6f3172019-02-04 13:44:44 +0100463 dst_off, dst_len, wr_rdma_buf);
Hans Wippelbe244f22018-06-28 19:05:10 +0200464 if (rc)
465 return rc;
Ursula Braune6727f32017-01-09 16:55:23 +0100466
Stefan Rasplde8474e2018-05-23 16:38:11 +0200467 if (conn->urg_tx_pend && len == to_send)
468 pflags->urg_data_present = 1;
Ursula Braune6727f32017-01-09 16:55:23 +0100469 smc_tx_advance_cursors(conn, &prod, &sent, len);
470 /* update connection's cursors with advanced local cursors */
Stefan Rasplbac6de72018-07-23 13:53:09 +0200471 smc_curs_copy(&conn->local_tx_ctrl.prod, &prod, conn);
Ursula Braune6727f32017-01-09 16:55:23 +0100472 /* dst: peer RMBE */
Stefan Rasplbac6de72018-07-23 13:53:09 +0200473 smc_curs_copy(&conn->tx_curs_sent, &sent, conn);/* src: local sndbuf */
Ursula Braune6727f32017-01-09 16:55:23 +0100474
475 return 0;
476}
477
478/* Wakeup sndbuf consumers from any context (IRQ or process)
479 * since there is more data to transmit; usable snd_wnd as max transmit
480 */
Hans Wippelbe244f22018-06-28 19:05:10 +0200481static int smcr_tx_sndbuf_nonempty(struct smc_connection *conn)
Ursula Braune6727f32017-01-09 16:55:23 +0100482{
Ursula Brauncecc7a32019-02-21 13:00:58 +0100483 struct smc_cdc_producer_flags *pflags = &conn->local_tx_ctrl.prod_flags;
Ursula Braunad6f3172019-02-04 13:44:44 +0100484 struct smc_rdma_wr *wr_rdma_buf;
Ursula Braune6727f32017-01-09 16:55:23 +0100485 struct smc_cdc_tx_pend *pend;
486 struct smc_wr_buf *wr_buf;
487 int rc;
488
Ursula Braunad6f3172019-02-04 13:44:44 +0100489 rc = smc_cdc_get_free_slot(conn, &wr_buf, &wr_rdma_buf, &pend);
Ursula Braune6727f32017-01-09 16:55:23 +0100490 if (rc < 0) {
491 if (rc == -EBUSY) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100492 struct smc_sock *smc =
493 container_of(conn, struct smc_sock, conn);
494
Karsten Graul33f3fcc2019-01-30 18:51:05 +0100495 if (smc->sk.sk_err == ECONNABORTED)
496 return sock_error(&smc->sk);
Ursula Braunb2900982019-10-21 16:13:08 +0200497 if (conn->killed)
498 return -EPIPE;
Ursula Braune6727f32017-01-09 16:55:23 +0100499 rc = 0;
Ursula Braunb2900982019-10-21 16:13:08 +0200500 mod_delayed_work(system_wq, &conn->tx_work,
501 SMC_TX_WORK_DELAY);
Ursula Braune6727f32017-01-09 16:55:23 +0100502 }
Karsten Graul33f3fcc2019-01-30 18:51:05 +0100503 return rc;
Ursula Braune6727f32017-01-09 16:55:23 +0100504 }
505
Karsten Graul33f3fcc2019-01-30 18:51:05 +0100506 spin_lock_bh(&conn->send_lock);
Ursula Brauncecc7a32019-02-21 13:00:58 +0100507 if (!pflags->urg_data_present) {
Ursula Braunad6f3172019-02-04 13:44:44 +0100508 rc = smc_tx_rdma_writes(conn, wr_rdma_buf);
Stefan Rasplde8474e2018-05-23 16:38:11 +0200509 if (rc) {
510 smc_wr_tx_put_slot(&conn->lgr->lnk[SMC_SINGLE_LINK],
511 (struct smc_wr_tx_pend_priv *)pend);
512 goto out_unlock;
513 }
Ursula Braune6727f32017-01-09 16:55:23 +0100514 }
515
516 rc = smc_cdc_msg_send(conn, wr_buf, pend);
Stefan Rasplde8474e2018-05-23 16:38:11 +0200517 if (!rc && pflags->urg_data_present) {
518 pflags->urg_data_pending = 0;
519 pflags->urg_data_present = 0;
520 }
Ursula Braune6727f32017-01-09 16:55:23 +0100521
522out_unlock:
523 spin_unlock_bh(&conn->send_lock);
524 return rc;
525}
526
Hans Wippelbe244f22018-06-28 19:05:10 +0200527static int smcd_tx_sndbuf_nonempty(struct smc_connection *conn)
528{
529 struct smc_cdc_producer_flags *pflags = &conn->local_tx_ctrl.prod_flags;
530 int rc = 0;
531
532 spin_lock_bh(&conn->send_lock);
533 if (!pflags->urg_data_present)
Ursula Braunad6f3172019-02-04 13:44:44 +0100534 rc = smc_tx_rdma_writes(conn, NULL);
Hans Wippelbe244f22018-06-28 19:05:10 +0200535 if (!rc)
536 rc = smcd_cdc_msg_send(conn);
537
538 if (!rc && pflags->urg_data_present) {
539 pflags->urg_data_pending = 0;
540 pflags->urg_data_present = 0;
541 }
542 spin_unlock_bh(&conn->send_lock);
543 return rc;
544}
545
546int smc_tx_sndbuf_nonempty(struct smc_connection *conn)
547{
548 int rc;
549
Ursula Braunb2900982019-10-21 16:13:08 +0200550 if (conn->killed ||
551 conn->local_rx_ctrl.conn_state_flags.peer_conn_abort)
552 return -EPIPE; /* connection being aborted */
Hans Wippelbe244f22018-06-28 19:05:10 +0200553 if (conn->lgr->is_smcd)
554 rc = smcd_tx_sndbuf_nonempty(conn);
555 else
556 rc = smcr_tx_sndbuf_nonempty(conn);
557
Karsten Graul5bc056d2019-02-12 16:29:51 +0100558 if (!rc) {
559 /* trigger socket release if connection is closing */
560 struct smc_sock *smc = container_of(conn, struct smc_sock,
561 conn);
562 smc_close_wake_tx_prepared(smc);
563 }
Hans Wippelbe244f22018-06-28 19:05:10 +0200564 return rc;
565}
566
Ursula Braune6727f32017-01-09 16:55:23 +0100567/* Wakeup sndbuf consumers from process context
568 * since there is more data to transmit
569 */
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700570void smc_tx_work(struct work_struct *work)
Ursula Braune6727f32017-01-09 16:55:23 +0100571{
Ursula Braun18e537c2017-09-21 09:16:33 +0200572 struct smc_connection *conn = container_of(to_delayed_work(work),
Ursula Braune6727f32017-01-09 16:55:23 +0100573 struct smc_connection,
574 tx_work);
575 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
Ursula Braun90cacb22017-04-10 14:57:59 +0200576 int rc;
Ursula Braune6727f32017-01-09 16:55:23 +0100577
578 lock_sock(&smc->sk);
Ursula Braunb2900982019-10-21 16:13:08 +0200579 if (smc->sk.sk_err)
Ursula Braun1a0a04c2018-01-25 11:15:36 +0100580 goto out;
581
Ursula Braun90cacb22017-04-10 14:57:59 +0200582 rc = smc_tx_sndbuf_nonempty(conn);
583 if (!rc && conn->local_rx_ctrl.prod_flags.write_blocked &&
584 !atomic_read(&conn->bytes_to_rcv))
585 conn->local_rx_ctrl.prod_flags.write_blocked = 0;
Ursula Braun1a0a04c2018-01-25 11:15:36 +0100586
587out:
Ursula Braune6727f32017-01-09 16:55:23 +0100588 release_sock(&smc->sk);
589}
590
Stefan Rasplde8474e2018-05-23 16:38:11 +0200591void smc_tx_consumer_update(struct smc_connection *conn, bool force)
Ursula Braun952310c2017-01-09 16:55:24 +0100592{
Ursula Braun99be51f2018-07-18 15:22:49 +0200593 union smc_host_cursor cfed, cons, prod;
594 int sender_free = conn->rmb_desc->len;
Ursula Braun6b5771a2017-12-07 13:38:48 +0100595 int to_confirm;
Ursula Braun952310c2017-01-09 16:55:24 +0100596
Stefan Rasplbac6de72018-07-23 13:53:09 +0200597 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
598 smc_curs_copy(&cfed, &conn->rx_curs_confirmed, conn);
Hans Wippel69cb7dc2018-05-18 09:34:10 +0200599 to_confirm = smc_curs_diff(conn->rmb_desc->len, &cfed, &cons);
Ursula Braun99be51f2018-07-18 15:22:49 +0200600 if (to_confirm > conn->rmbe_update_limit) {
Stefan Rasplbac6de72018-07-23 13:53:09 +0200601 smc_curs_copy(&prod, &conn->local_rx_ctrl.prod, conn);
Ursula Braun99be51f2018-07-18 15:22:49 +0200602 sender_free = conn->rmb_desc->len -
Ursula Braunb8649ef2019-02-04 13:44:45 +0100603 smc_curs_diff_large(conn->rmb_desc->len,
604 &cfed, &prod);
Ursula Braun99be51f2018-07-18 15:22:49 +0200605 }
Ursula Braun952310c2017-01-09 16:55:24 +0100606
607 if (conn->local_rx_ctrl.prod_flags.cons_curs_upd_req ||
Stefan Rasplde8474e2018-05-23 16:38:11 +0200608 force ||
Ursula Braun952310c2017-01-09 16:55:24 +0100609 ((to_confirm > conn->rmbe_update_limit) &&
Ursula Braun99be51f2018-07-18 15:22:49 +0200610 ((sender_free <= (conn->rmb_desc->len / 2)) ||
Ursula Braun952310c2017-01-09 16:55:24 +0100611 conn->local_rx_ctrl.prod_flags.write_blocked))) {
Ursula Braunb2900982019-10-21 16:13:08 +0200612 if (conn->killed ||
613 conn->local_rx_ctrl.conn_state_flags.peer_conn_abort)
614 return;
Ursula Braun1a0a04c2018-01-25 11:15:36 +0100615 if ((smc_cdc_get_slot_and_msg_send(conn) < 0) &&
Ursula Braunb2900982019-10-21 16:13:08 +0200616 !conn->killed) {
Ursula Braun18e537c2017-09-21 09:16:33 +0200617 schedule_delayed_work(&conn->tx_work,
618 SMC_TX_WORK_DELAY);
Ursula Braun952310c2017-01-09 16:55:24 +0100619 return;
620 }
Ursula Braun952310c2017-01-09 16:55:24 +0100621 }
622 if (conn->local_rx_ctrl.prod_flags.write_blocked &&
623 !atomic_read(&conn->bytes_to_rcv))
624 conn->local_rx_ctrl.prod_flags.write_blocked = 0;
625}
626
Ursula Braune6727f32017-01-09 16:55:23 +0100627/***************************** send initialize *******************************/
628
629/* Initialize send properties on connection establishment. NB: not __init! */
630void smc_tx_init(struct smc_sock *smc)
631{
632 smc->sk.sk_write_space = smc_tx_write_space;
Ursula Braune6727f32017-01-09 16:55:23 +0100633}