Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
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>
19#include <linux/sched/signal.h>
20
21#include <net/sock.h>
22#include <net/tcp.h>
23
24#include "smc.h"
25#include "smc_wr.h"
26#include "smc_cdc.h"
27#include "smc_close.h"
28#include "smc_ism.h"
29#include "smc_tx.h"
30#include "smc_stats.h"
31#include "smc_tracepoint.h"
32
33#define SMC_TX_WORK_DELAY 0
34
35/***************************** sndbuf producer *******************************/
36
37/* callback implementation for sk.sk_write_space()
38 * to wakeup sndbuf producers that blocked with smc_tx_wait().
39 * called under sk_socket lock.
40 */
41static void smc_tx_write_space(struct sock *sk)
42{
43 struct socket *sock = sk->sk_socket;
44 struct smc_sock *smc = smc_sk(sk);
45 struct socket_wq *wq;
46
47 /* similar to sk_stream_write_space */
48 if (atomic_read(&smc->conn.sndbuf_space) && sock) {
49 if (test_bit(SOCK_NOSPACE, &sock->flags))
50 SMC_STAT_RMB_TX_FULL(smc, !smc->conn.lnk);
51 clear_bit(SOCK_NOSPACE, &sock->flags);
52 rcu_read_lock();
53 wq = rcu_dereference(sk->sk_wq);
54 if (skwq_has_sleeper(wq))
55 wake_up_interruptible_poll(&wq->wait,
56 EPOLLOUT | EPOLLWRNORM |
57 EPOLLWRBAND);
58 if (wq && wq->fasync_list && !(sk->sk_shutdown & SEND_SHUTDOWN))
59 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
60 rcu_read_unlock();
61 }
62}
63
64/* Wakeup sndbuf producers that blocked with smc_tx_wait().
65 * Cf. tcp_data_snd_check()=>tcp_check_space()=>tcp_new_space().
66 */
67void smc_tx_sndbuf_nonfull(struct smc_sock *smc)
68{
69 if (smc->sk.sk_socket &&
70 test_bit(SOCK_NOSPACE, &smc->sk.sk_socket->flags))
71 smc->sk.sk_write_space(&smc->sk);
72}
73
74/* blocks sndbuf producer until at least one byte of free space available
75 * or urgent Byte was consumed
76 */
77static int smc_tx_wait(struct smc_sock *smc, int flags)
78{
79 DEFINE_WAIT_FUNC(wait, woken_wake_function);
80 struct smc_connection *conn = &smc->conn;
81 struct sock *sk = &smc->sk;
82 long timeo;
83 int rc = 0;
84
85 /* similar to sk_stream_wait_memory */
86 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
87 add_wait_queue(sk_sleep(sk), &wait);
88 while (1) {
89 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
90 if (sk->sk_err ||
91 (sk->sk_shutdown & SEND_SHUTDOWN) ||
92 conn->killed ||
93 conn->local_tx_ctrl.conn_state_flags.peer_done_writing) {
94 rc = -EPIPE;
95 break;
96 }
97 if (smc_cdc_rxed_any_close(conn)) {
98 rc = -ECONNRESET;
99 break;
100 }
101 if (!timeo) {
102 /* ensure EPOLLOUT is subsequently generated */
103 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
104 rc = -EAGAIN;
105 break;
106 }
107 if (signal_pending(current)) {
108 rc = sock_intr_errno(timeo);
109 break;
110 }
111 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
112 if (atomic_read(&conn->sndbuf_space) && !conn->urg_tx_pend)
113 break; /* at least 1 byte of free & no urgent data */
114 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
115 sk_wait_event(sk, &timeo,
116 sk->sk_err ||
117 (sk->sk_shutdown & SEND_SHUTDOWN) ||
118 smc_cdc_rxed_any_close(conn) ||
119 (atomic_read(&conn->sndbuf_space) &&
120 !conn->urg_tx_pend),
121 &wait);
122 }
123 remove_wait_queue(sk_sleep(sk), &wait);
124 return rc;
125}
126
127static bool smc_tx_is_corked(struct smc_sock *smc)
128{
129 struct tcp_sock *tp = tcp_sk(smc->clcsock->sk);
130
131 return (tp->nonagle & TCP_NAGLE_CORK) ? true : false;
132}
133
134/* If we have pending CDC messages, do not send:
135 * Because CQE of this CDC message will happen shortly, it gives
136 * a chance to coalesce future sendmsg() payload in to one RDMA Write,
137 * without need for a timer, and with no latency trade off.
138 * Algorithm here:
139 * 1. First message should never cork
140 * 2. If we have pending Tx CDC messages, wait for the first CDC
141 * message's completion
142 * 3. Don't cork to much data in a single RDMA Write to prevent burst
143 * traffic, total corked message should not exceed sendbuf/2
144 */
145static bool smc_should_autocork(struct smc_sock *smc)
146{
147 struct smc_connection *conn = &smc->conn;
148 int corking_size;
149
150 corking_size = min_t(unsigned int, conn->sndbuf_desc->len >> 1,
151 sock_net(&smc->sk)->smc.sysctl_autocorking_size);
152
153 if (atomic_read(&conn->cdc_pend_tx_wr) == 0 ||
154 smc_tx_prepared_sends(conn) > corking_size)
155 return false;
156 return true;
157}
158
159static bool smc_tx_should_cork(struct smc_sock *smc, struct msghdr *msg)
160{
161 struct smc_connection *conn = &smc->conn;
162
163 if (smc_should_autocork(smc))
164 return true;
165
166 /* for a corked socket defer the RDMA writes if
167 * sndbuf_space is still available. The applications
168 * should known how/when to uncork it.
169 */
170 if ((msg->msg_flags & MSG_MORE ||
171 smc_tx_is_corked(smc) ||
172 msg->msg_flags & MSG_SENDPAGE_NOTLAST) &&
173 atomic_read(&conn->sndbuf_space))
174 return true;
175
176 return false;
177}
178
179/* sndbuf producer: main API called by socket layer.
180 * called under sock lock.
181 */
182int smc_tx_sendmsg(struct smc_sock *smc, struct msghdr *msg, size_t len)
183{
184 size_t copylen, send_done = 0, send_remaining = len;
185 size_t chunk_len, chunk_off, chunk_len_sum;
186 struct smc_connection *conn = &smc->conn;
187 union smc_host_cursor prep;
188 struct sock *sk = &smc->sk;
189 char *sndbuf_base;
190 int tx_cnt_prep;
191 int writespace;
192 int rc, chunk;
193
194 /* This should be in poll */
195 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
196
197 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) {
198 rc = -EPIPE;
199 goto out_err;
200 }
201
202 if (sk->sk_state == SMC_INIT)
203 return -ENOTCONN;
204
205 if (len > conn->sndbuf_desc->len)
206 SMC_STAT_RMB_TX_SIZE_SMALL(smc, !conn->lnk);
207
208 if (len > conn->peer_rmbe_size)
209 SMC_STAT_RMB_TX_PEER_SIZE_SMALL(smc, !conn->lnk);
210
211 if (msg->msg_flags & MSG_OOB)
212 SMC_STAT_INC(smc, urg_data_cnt);
213
214 while (msg_data_left(msg)) {
215 if (smc->sk.sk_shutdown & SEND_SHUTDOWN ||
216 (smc->sk.sk_err == ECONNABORTED) ||
217 conn->killed)
218 return -EPIPE;
219 if (smc_cdc_rxed_any_close(conn))
220 return send_done ?: -ECONNRESET;
221
222 if (msg->msg_flags & MSG_OOB)
223 conn->local_tx_ctrl.prod_flags.urg_data_pending = 1;
224
225 if (!atomic_read(&conn->sndbuf_space) || conn->urg_tx_pend) {
226 if (send_done)
227 return send_done;
228 rc = smc_tx_wait(smc, msg->msg_flags);
229 if (rc)
230 goto out_err;
231 continue;
232 }
233
234 /* initialize variables for 1st iteration of subsequent loop */
235 /* could be just 1 byte, even after smc_tx_wait above */
236 writespace = atomic_read(&conn->sndbuf_space);
237 /* not more than what user space asked for */
238 copylen = min_t(size_t, send_remaining, writespace);
239 /* determine start of sndbuf */
240 sndbuf_base = conn->sndbuf_desc->cpu_addr;
241 smc_curs_copy(&prep, &conn->tx_curs_prep, conn);
242 tx_cnt_prep = prep.count;
243 /* determine chunks where to write into sndbuf */
244 /* either unwrapped case, or 1st chunk of wrapped case */
245 chunk_len = min_t(size_t, copylen, conn->sndbuf_desc->len -
246 tx_cnt_prep);
247 chunk_len_sum = chunk_len;
248 chunk_off = tx_cnt_prep;
249 for (chunk = 0; chunk < 2; chunk++) {
250 rc = memcpy_from_msg(sndbuf_base + chunk_off,
251 msg, chunk_len);
252 if (rc) {
253 smc_sndbuf_sync_sg_for_device(conn);
254 if (send_done)
255 return send_done;
256 goto out_err;
257 }
258 send_done += chunk_len;
259 send_remaining -= chunk_len;
260
261 if (chunk_len_sum == copylen)
262 break; /* either on 1st or 2nd iteration */
263 /* prepare next (== 2nd) iteration */
264 chunk_len = copylen - chunk_len; /* remainder */
265 chunk_len_sum += chunk_len;
266 chunk_off = 0; /* modulo offset in send ring buffer */
267 }
268 smc_sndbuf_sync_sg_for_device(conn);
269 /* update cursors */
270 smc_curs_add(conn->sndbuf_desc->len, &prep, copylen);
271 smc_curs_copy(&conn->tx_curs_prep, &prep, conn);
272 /* increased in send tasklet smc_cdc_tx_handler() */
273 smp_mb__before_atomic();
274 atomic_sub(copylen, &conn->sndbuf_space);
275 /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
276 smp_mb__after_atomic();
277 /* since we just produced more new data into sndbuf,
278 * trigger sndbuf consumer: RDMA write into peer RMBE and CDC
279 */
280 if ((msg->msg_flags & MSG_OOB) && !send_remaining)
281 conn->urg_tx_pend = true;
282 /* If we need to cork, do nothing and wait for the next
283 * sendmsg() call or push on tx completion
284 */
285 if (!smc_tx_should_cork(smc, msg))
286 smc_tx_sndbuf_nonempty(conn);
287
288 trace_smc_tx_sendmsg(smc, copylen);
289 } /* while (msg_data_left(msg)) */
290
291 return send_done;
292
293out_err:
294 rc = sk_stream_error(sk, msg->msg_flags, rc);
295 /* make sure we wake any epoll edge trigger waiter */
296 if (unlikely(rc == -EAGAIN))
297 sk->sk_write_space(sk);
298 return rc;
299}
300
301int smc_tx_sendpage(struct smc_sock *smc, struct page *page, int offset,
302 size_t size, int flags)
303{
304 struct msghdr msg = {.msg_flags = flags};
305 char *kaddr = kmap(page);
306 struct kvec iov;
307 int rc;
308
309 iov.iov_base = kaddr + offset;
310 iov.iov_len = size;
311 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &iov, 1, size);
312 rc = smc_tx_sendmsg(smc, &msg, size);
313 kunmap(page);
314 return rc;
315}
316
317/***************************** sndbuf consumer *******************************/
318
319/* sndbuf consumer: actual data transfer of one target chunk with ISM write */
320int smcd_tx_ism_write(struct smc_connection *conn, void *data, size_t len,
321 u32 offset, int signal)
322{
323 int rc;
324
325 rc = smc_ism_write(conn->lgr->smcd, conn->peer_token,
326 conn->peer_rmbe_idx, signal, conn->tx_off + offset,
327 data, len);
328 if (rc)
329 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
330 return rc;
331}
332
333/* sndbuf consumer: actual data transfer of one target chunk with RDMA write */
334static int smc_tx_rdma_write(struct smc_connection *conn, int peer_rmbe_offset,
335 int num_sges, struct ib_rdma_wr *rdma_wr)
336{
337 struct smc_link_group *lgr = conn->lgr;
338 struct smc_link *link = conn->lnk;
339 int rc;
340
341 rdma_wr->wr.wr_id = smc_wr_tx_get_next_wr_id(link);
342 rdma_wr->wr.num_sge = num_sges;
343 rdma_wr->remote_addr =
344 lgr->rtokens[conn->rtoken_idx][link->link_idx].dma_addr +
345 /* RMBE within RMB */
346 conn->tx_off +
347 /* offset within RMBE */
348 peer_rmbe_offset;
349 rdma_wr->rkey = lgr->rtokens[conn->rtoken_idx][link->link_idx].rkey;
350 rc = ib_post_send(link->roce_qp, &rdma_wr->wr, NULL);
351 if (rc)
352 smcr_link_down_cond_sched(link);
353 return rc;
354}
355
356/* sndbuf consumer */
357static inline void smc_tx_advance_cursors(struct smc_connection *conn,
358 union smc_host_cursor *prod,
359 union smc_host_cursor *sent,
360 size_t len)
361{
362 smc_curs_add(conn->peer_rmbe_size, prod, len);
363 /* increased in recv tasklet smc_cdc_msg_rcv() */
364 smp_mb__before_atomic();
365 /* data in flight reduces usable snd_wnd */
366 atomic_sub(len, &conn->peer_rmbe_space);
367 /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
368 smp_mb__after_atomic();
369 smc_curs_add(conn->sndbuf_desc->len, sent, len);
370}
371
372/* SMC-R helper for smc_tx_rdma_writes() */
373static int smcr_tx_rdma_writes(struct smc_connection *conn, size_t len,
374 size_t src_off, size_t src_len,
375 size_t dst_off, size_t dst_len,
376 struct smc_rdma_wr *wr_rdma_buf)
377{
378 struct smc_link *link = conn->lnk;
379
380 dma_addr_t dma_addr =
381 sg_dma_address(conn->sndbuf_desc->sgt[link->link_idx].sgl);
382 u64 virt_addr = (uintptr_t)conn->sndbuf_desc->cpu_addr;
383 int src_len_sum = src_len, dst_len_sum = dst_len;
384 int sent_count = src_off;
385 int srcchunk, dstchunk;
386 int num_sges;
387 int rc;
388
389 for (dstchunk = 0; dstchunk < 2; dstchunk++) {
390 struct ib_rdma_wr *wr = &wr_rdma_buf->wr_tx_rdma[dstchunk];
391 struct ib_sge *sge = wr->wr.sg_list;
392 u64 base_addr = dma_addr;
393
394 if (dst_len < link->qp_attr.cap.max_inline_data) {
395 base_addr = virt_addr;
396 wr->wr.send_flags |= IB_SEND_INLINE;
397 } else {
398 wr->wr.send_flags &= ~IB_SEND_INLINE;
399 }
400
401 num_sges = 0;
402 for (srcchunk = 0; srcchunk < 2; srcchunk++) {
403 sge[srcchunk].addr = conn->sndbuf_desc->is_vm ?
404 (virt_addr + src_off) : (base_addr + src_off);
405 sge[srcchunk].length = src_len;
406 if (conn->sndbuf_desc->is_vm)
407 sge[srcchunk].lkey =
408 conn->sndbuf_desc->mr[link->link_idx]->lkey;
409 num_sges++;
410
411 src_off += src_len;
412 if (src_off >= conn->sndbuf_desc->len)
413 src_off -= conn->sndbuf_desc->len;
414 /* modulo in send ring */
415 if (src_len_sum == dst_len)
416 break; /* either on 1st or 2nd iteration */
417 /* prepare next (== 2nd) iteration */
418 src_len = dst_len - src_len; /* remainder */
419 src_len_sum += src_len;
420 }
421 rc = smc_tx_rdma_write(conn, dst_off, num_sges, wr);
422 if (rc)
423 return rc;
424 if (dst_len_sum == len)
425 break; /* either on 1st or 2nd iteration */
426 /* prepare next (== 2nd) iteration */
427 dst_off = 0; /* modulo offset in RMBE ring buffer */
428 dst_len = len - dst_len; /* remainder */
429 dst_len_sum += dst_len;
430 src_len = min_t(int, dst_len, conn->sndbuf_desc->len -
431 sent_count);
432 src_len_sum = src_len;
433 }
434 return 0;
435}
436
437/* SMC-D helper for smc_tx_rdma_writes() */
438static int smcd_tx_rdma_writes(struct smc_connection *conn, size_t len,
439 size_t src_off, size_t src_len,
440 size_t dst_off, size_t dst_len)
441{
442 int src_len_sum = src_len, dst_len_sum = dst_len;
443 int srcchunk, dstchunk;
444 int rc;
445
446 for (dstchunk = 0; dstchunk < 2; dstchunk++) {
447 for (srcchunk = 0; srcchunk < 2; srcchunk++) {
448 void *data = conn->sndbuf_desc->cpu_addr + src_off;
449
450 rc = smcd_tx_ism_write(conn, data, src_len, dst_off +
451 sizeof(struct smcd_cdc_msg), 0);
452 if (rc)
453 return rc;
454 dst_off += src_len;
455 src_off += src_len;
456 if (src_off >= conn->sndbuf_desc->len)
457 src_off -= conn->sndbuf_desc->len;
458 /* modulo in send ring */
459 if (src_len_sum == dst_len)
460 break; /* either on 1st or 2nd iteration */
461 /* prepare next (== 2nd) iteration */
462 src_len = dst_len - src_len; /* remainder */
463 src_len_sum += src_len;
464 }
465 if (dst_len_sum == len)
466 break; /* either on 1st or 2nd iteration */
467 /* prepare next (== 2nd) iteration */
468 dst_off = 0; /* modulo offset in RMBE ring buffer */
469 dst_len = len - dst_len; /* remainder */
470 dst_len_sum += dst_len;
471 src_len = min_t(int, dst_len, conn->sndbuf_desc->len - src_off);
472 src_len_sum = src_len;
473 }
474 return 0;
475}
476
477/* sndbuf consumer: prepare all necessary (src&dst) chunks of data transmit;
478 * usable snd_wnd as max transmit
479 */
480static int smc_tx_rdma_writes(struct smc_connection *conn,
481 struct smc_rdma_wr *wr_rdma_buf)
482{
483 size_t len, src_len, dst_off, dst_len; /* current chunk values */
484 union smc_host_cursor sent, prep, prod, cons;
485 struct smc_cdc_producer_flags *pflags;
486 int to_send, rmbespace;
487 int rc;
488
489 /* source: sndbuf */
490 smc_curs_copy(&sent, &conn->tx_curs_sent, conn);
491 smc_curs_copy(&prep, &conn->tx_curs_prep, conn);
492 /* cf. wmem_alloc - (snd_max - snd_una) */
493 to_send = smc_curs_diff(conn->sndbuf_desc->len, &sent, &prep);
494 if (to_send <= 0)
495 return 0;
496
497 /* destination: RMBE */
498 /* cf. snd_wnd */
499 rmbespace = atomic_read(&conn->peer_rmbe_space);
500 if (rmbespace <= 0) {
501 struct smc_sock *smc = container_of(conn, struct smc_sock,
502 conn);
503 SMC_STAT_RMB_TX_PEER_FULL(smc, !conn->lnk);
504 return 0;
505 }
506 smc_curs_copy(&prod, &conn->local_tx_ctrl.prod, conn);
507 smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
508
509 /* if usable snd_wnd closes ask peer to advertise once it opens again */
510 pflags = &conn->local_tx_ctrl.prod_flags;
511 pflags->write_blocked = (to_send >= rmbespace);
512 /* cf. usable snd_wnd */
513 len = min(to_send, rmbespace);
514
515 /* initialize variables for first iteration of subsequent nested loop */
516 dst_off = prod.count;
517 if (prod.wrap == cons.wrap) {
518 /* the filled destination area is unwrapped,
519 * hence the available free destination space is wrapped
520 * and we need 2 destination chunks of sum len; start with 1st
521 * which is limited by what's available in sndbuf
522 */
523 dst_len = min_t(size_t,
524 conn->peer_rmbe_size - prod.count, len);
525 } else {
526 /* the filled destination area is wrapped,
527 * hence the available free destination space is unwrapped
528 * and we need a single destination chunk of entire len
529 */
530 dst_len = len;
531 }
532 /* dst_len determines the maximum src_len */
533 if (sent.count + dst_len <= conn->sndbuf_desc->len) {
534 /* unwrapped src case: single chunk of entire dst_len */
535 src_len = dst_len;
536 } else {
537 /* wrapped src case: 2 chunks of sum dst_len; start with 1st: */
538 src_len = conn->sndbuf_desc->len - sent.count;
539 }
540
541 if (conn->lgr->is_smcd)
542 rc = smcd_tx_rdma_writes(conn, len, sent.count, src_len,
543 dst_off, dst_len);
544 else
545 rc = smcr_tx_rdma_writes(conn, len, sent.count, src_len,
546 dst_off, dst_len, wr_rdma_buf);
547 if (rc)
548 return rc;
549
550 if (conn->urg_tx_pend && len == to_send)
551 pflags->urg_data_present = 1;
552 smc_tx_advance_cursors(conn, &prod, &sent, len);
553 /* update connection's cursors with advanced local cursors */
554 smc_curs_copy(&conn->local_tx_ctrl.prod, &prod, conn);
555 /* dst: peer RMBE */
556 smc_curs_copy(&conn->tx_curs_sent, &sent, conn);/* src: local sndbuf */
557
558 return 0;
559}
560
561/* Wakeup sndbuf consumers from any context (IRQ or process)
562 * since there is more data to transmit; usable snd_wnd as max transmit
563 */
564static int smcr_tx_sndbuf_nonempty(struct smc_connection *conn)
565{
566 struct smc_cdc_producer_flags *pflags = &conn->local_tx_ctrl.prod_flags;
567 struct smc_link *link = conn->lnk;
568 struct smc_rdma_wr *wr_rdma_buf;
569 struct smc_cdc_tx_pend *pend;
570 struct smc_wr_buf *wr_buf;
571 int rc;
572
573 if (!link || !smc_wr_tx_link_hold(link))
574 return -ENOLINK;
575 rc = smc_cdc_get_free_slot(conn, link, &wr_buf, &wr_rdma_buf, &pend);
576 if (rc < 0) {
577 smc_wr_tx_link_put(link);
578 if (rc == -EBUSY) {
579 struct smc_sock *smc =
580 container_of(conn, struct smc_sock, conn);
581
582 if (smc->sk.sk_err == ECONNABORTED)
583 return sock_error(&smc->sk);
584 if (conn->killed)
585 return -EPIPE;
586 rc = 0;
587 mod_delayed_work(conn->lgr->tx_wq, &conn->tx_work,
588 SMC_TX_WORK_DELAY);
589 }
590 return rc;
591 }
592
593 spin_lock_bh(&conn->send_lock);
594 if (link != conn->lnk) {
595 /* link of connection changed, tx_work will restart */
596 smc_wr_tx_put_slot(link,
597 (struct smc_wr_tx_pend_priv *)pend);
598 rc = -ENOLINK;
599 goto out_unlock;
600 }
601 if (!pflags->urg_data_present) {
602 rc = smc_tx_rdma_writes(conn, wr_rdma_buf);
603 if (rc) {
604 smc_wr_tx_put_slot(link,
605 (struct smc_wr_tx_pend_priv *)pend);
606 goto out_unlock;
607 }
608 }
609
610 rc = smc_cdc_msg_send(conn, wr_buf, pend);
611 if (!rc && pflags->urg_data_present) {
612 pflags->urg_data_pending = 0;
613 pflags->urg_data_present = 0;
614 }
615
616out_unlock:
617 spin_unlock_bh(&conn->send_lock);
618 smc_wr_tx_link_put(link);
619 return rc;
620}
621
622static int smcd_tx_sndbuf_nonempty(struct smc_connection *conn)
623{
624 struct smc_cdc_producer_flags *pflags = &conn->local_tx_ctrl.prod_flags;
625 int rc = 0;
626
627 spin_lock_bh(&conn->send_lock);
628 if (!pflags->urg_data_present)
629 rc = smc_tx_rdma_writes(conn, NULL);
630 if (!rc)
631 rc = smcd_cdc_msg_send(conn);
632
633 if (!rc && pflags->urg_data_present) {
634 pflags->urg_data_pending = 0;
635 pflags->urg_data_present = 0;
636 }
637 spin_unlock_bh(&conn->send_lock);
638 return rc;
639}
640
641static int __smc_tx_sndbuf_nonempty(struct smc_connection *conn)
642{
643 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
644 int rc = 0;
645
646 /* No data in the send queue */
647 if (unlikely(smc_tx_prepared_sends(conn) <= 0))
648 goto out;
649
650 /* Peer don't have RMBE space */
651 if (unlikely(atomic_read(&conn->peer_rmbe_space) <= 0)) {
652 SMC_STAT_RMB_TX_PEER_FULL(smc, !conn->lnk);
653 goto out;
654 }
655
656 if (conn->killed ||
657 conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
658 rc = -EPIPE; /* connection being aborted */
659 goto out;
660 }
661 if (conn->lgr->is_smcd)
662 rc = smcd_tx_sndbuf_nonempty(conn);
663 else
664 rc = smcr_tx_sndbuf_nonempty(conn);
665
666 if (!rc) {
667 /* trigger socket release if connection is closing */
668 smc_close_wake_tx_prepared(smc);
669 }
670
671out:
672 return rc;
673}
674
675int smc_tx_sndbuf_nonempty(struct smc_connection *conn)
676{
677 int rc;
678
679 /* This make sure only one can send simultaneously to prevent wasting
680 * of CPU and CDC slot.
681 * Record whether someone has tried to push while we are pushing.
682 */
683 if (atomic_inc_return(&conn->tx_pushing) > 1)
684 return 0;
685
686again:
687 atomic_set(&conn->tx_pushing, 1);
688 smp_wmb(); /* Make sure tx_pushing is 1 before real send */
689 rc = __smc_tx_sndbuf_nonempty(conn);
690
691 /* We need to check whether someone else have added some data into
692 * the send queue and tried to push but failed after the atomic_set()
693 * when we are pushing.
694 * If so, we need to push again to prevent those data hang in the send
695 * queue.
696 */
697 if (unlikely(!atomic_dec_and_test(&conn->tx_pushing)))
698 goto again;
699
700 return rc;
701}
702
703/* Wakeup sndbuf consumers from process context
704 * since there is more data to transmit. The caller
705 * must hold sock lock.
706 */
707void smc_tx_pending(struct smc_connection *conn)
708{
709 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
710 int rc;
711
712 if (smc->sk.sk_err)
713 return;
714
715 rc = smc_tx_sndbuf_nonempty(conn);
716 if (!rc && conn->local_rx_ctrl.prod_flags.write_blocked &&
717 !atomic_read(&conn->bytes_to_rcv))
718 conn->local_rx_ctrl.prod_flags.write_blocked = 0;
719}
720
721/* Wakeup sndbuf consumers from process context
722 * since there is more data to transmit in locked
723 * sock.
724 */
725void smc_tx_work(struct work_struct *work)
726{
727 struct smc_connection *conn = container_of(to_delayed_work(work),
728 struct smc_connection,
729 tx_work);
730 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
731
732 lock_sock(&smc->sk);
733 smc_tx_pending(conn);
734 release_sock(&smc->sk);
735}
736
737void smc_tx_consumer_update(struct smc_connection *conn, bool force)
738{
739 union smc_host_cursor cfed, cons, prod;
740 int sender_free = conn->rmb_desc->len;
741 int to_confirm;
742
743 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
744 smc_curs_copy(&cfed, &conn->rx_curs_confirmed, conn);
745 to_confirm = smc_curs_diff(conn->rmb_desc->len, &cfed, &cons);
746 if (to_confirm > conn->rmbe_update_limit) {
747 smc_curs_copy(&prod, &conn->local_rx_ctrl.prod, conn);
748 sender_free = conn->rmb_desc->len -
749 smc_curs_diff_large(conn->rmb_desc->len,
750 &cfed, &prod);
751 }
752
753 if (conn->local_rx_ctrl.prod_flags.cons_curs_upd_req ||
754 force ||
755 ((to_confirm > conn->rmbe_update_limit) &&
756 ((sender_free <= (conn->rmb_desc->len / 2)) ||
757 conn->local_rx_ctrl.prod_flags.write_blocked))) {
758 if (conn->killed ||
759 conn->local_rx_ctrl.conn_state_flags.peer_conn_abort)
760 return;
761 if ((smc_cdc_get_slot_and_msg_send(conn) < 0) &&
762 !conn->killed) {
763 queue_delayed_work(conn->lgr->tx_wq, &conn->tx_work,
764 SMC_TX_WORK_DELAY);
765 return;
766 }
767 }
768 if (conn->local_rx_ctrl.prod_flags.write_blocked &&
769 !atomic_read(&conn->bytes_to_rcv))
770 conn->local_rx_ctrl.prod_flags.write_blocked = 0;
771}
772
773/***************************** send initialize *******************************/
774
775/* Initialize send properties on connection establishment. NB: not __init! */
776void smc_tx_init(struct smc_sock *smc)
777{
778 smc->sk.sk_write_space = smc_tx_write_space;
779}
1// SPDX-License-Identifier: GPL-2.0
2/*
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>
19#include <linux/sched/signal.h>
20
21#include <net/sock.h>
22#include <net/tcp.h>
23
24#include "smc.h"
25#include "smc_wr.h"
26#include "smc_cdc.h"
27#include "smc_close.h"
28#include "smc_ism.h"
29#include "smc_tx.h"
30#include "smc_stats.h"
31
32#define SMC_TX_WORK_DELAY 0
33#define SMC_TX_CORK_DELAY (HZ >> 2) /* 250 ms */
34
35/***************************** sndbuf producer *******************************/
36
37/* callback implementation for sk.sk_write_space()
38 * to wakeup sndbuf producers that blocked with smc_tx_wait().
39 * called under sk_socket lock.
40 */
41static void smc_tx_write_space(struct sock *sk)
42{
43 struct socket *sock = sk->sk_socket;
44 struct smc_sock *smc = smc_sk(sk);
45 struct socket_wq *wq;
46
47 /* similar to sk_stream_write_space */
48 if (atomic_read(&smc->conn.sndbuf_space) && sock) {
49 if (test_bit(SOCK_NOSPACE, &sock->flags))
50 SMC_STAT_RMB_TX_FULL(smc, !smc->conn.lnk);
51 clear_bit(SOCK_NOSPACE, &sock->flags);
52 rcu_read_lock();
53 wq = rcu_dereference(sk->sk_wq);
54 if (skwq_has_sleeper(wq))
55 wake_up_interruptible_poll(&wq->wait,
56 EPOLLOUT | EPOLLWRNORM |
57 EPOLLWRBAND);
58 if (wq && wq->fasync_list && !(sk->sk_shutdown & SEND_SHUTDOWN))
59 sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
60 rcu_read_unlock();
61 }
62}
63
64/* Wakeup sndbuf producers that blocked with smc_tx_wait().
65 * Cf. tcp_data_snd_check()=>tcp_check_space()=>tcp_new_space().
66 */
67void smc_tx_sndbuf_nonfull(struct smc_sock *smc)
68{
69 if (smc->sk.sk_socket &&
70 test_bit(SOCK_NOSPACE, &smc->sk.sk_socket->flags))
71 smc->sk.sk_write_space(&smc->sk);
72}
73
74/* blocks sndbuf producer until at least one byte of free space available
75 * or urgent Byte was consumed
76 */
77static int smc_tx_wait(struct smc_sock *smc, int flags)
78{
79 DEFINE_WAIT_FUNC(wait, woken_wake_function);
80 struct smc_connection *conn = &smc->conn;
81 struct sock *sk = &smc->sk;
82 long timeo;
83 int rc = 0;
84
85 /* similar to sk_stream_wait_memory */
86 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
87 add_wait_queue(sk_sleep(sk), &wait);
88 while (1) {
89 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
90 if (sk->sk_err ||
91 (sk->sk_shutdown & SEND_SHUTDOWN) ||
92 conn->killed ||
93 conn->local_tx_ctrl.conn_state_flags.peer_done_writing) {
94 rc = -EPIPE;
95 break;
96 }
97 if (smc_cdc_rxed_any_close(conn)) {
98 rc = -ECONNRESET;
99 break;
100 }
101 if (!timeo) {
102 /* ensure EPOLLOUT is subsequently generated */
103 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
104 rc = -EAGAIN;
105 break;
106 }
107 if (signal_pending(current)) {
108 rc = sock_intr_errno(timeo);
109 break;
110 }
111 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
112 if (atomic_read(&conn->sndbuf_space) && !conn->urg_tx_pend)
113 break; /* at least 1 byte of free & no urgent data */
114 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
115 sk_wait_event(sk, &timeo,
116 sk->sk_err ||
117 (sk->sk_shutdown & SEND_SHUTDOWN) ||
118 smc_cdc_rxed_any_close(conn) ||
119 (atomic_read(&conn->sndbuf_space) &&
120 !conn->urg_tx_pend),
121 &wait);
122 }
123 remove_wait_queue(sk_sleep(sk), &wait);
124 return rc;
125}
126
127static bool smc_tx_is_corked(struct smc_sock *smc)
128{
129 struct tcp_sock *tp = tcp_sk(smc->clcsock->sk);
130
131 return (tp->nonagle & TCP_NAGLE_CORK) ? true : false;
132}
133
134/* sndbuf producer: main API called by socket layer.
135 * called under sock lock.
136 */
137int smc_tx_sendmsg(struct smc_sock *smc, struct msghdr *msg, size_t len)
138{
139 size_t copylen, send_done = 0, send_remaining = len;
140 size_t chunk_len, chunk_off, chunk_len_sum;
141 struct smc_connection *conn = &smc->conn;
142 union smc_host_cursor prep;
143 struct sock *sk = &smc->sk;
144 char *sndbuf_base;
145 int tx_cnt_prep;
146 int writespace;
147 int rc, chunk;
148
149 /* This should be in poll */
150 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
151
152 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) {
153 rc = -EPIPE;
154 goto out_err;
155 }
156
157 if (sk->sk_state == SMC_INIT)
158 return -ENOTCONN;
159
160 if (len > conn->sndbuf_desc->len)
161 SMC_STAT_RMB_TX_SIZE_SMALL(smc, !conn->lnk);
162
163 if (len > conn->peer_rmbe_size)
164 SMC_STAT_RMB_TX_PEER_SIZE_SMALL(smc, !conn->lnk);
165
166 if (msg->msg_flags & MSG_OOB)
167 SMC_STAT_INC(smc, urg_data_cnt);
168
169 while (msg_data_left(msg)) {
170 if (smc->sk.sk_shutdown & SEND_SHUTDOWN ||
171 (smc->sk.sk_err == ECONNABORTED) ||
172 conn->killed)
173 return -EPIPE;
174 if (smc_cdc_rxed_any_close(conn))
175 return send_done ?: -ECONNRESET;
176
177 if (msg->msg_flags & MSG_OOB)
178 conn->local_tx_ctrl.prod_flags.urg_data_pending = 1;
179
180 if (!atomic_read(&conn->sndbuf_space) || conn->urg_tx_pend) {
181 if (send_done)
182 return send_done;
183 rc = smc_tx_wait(smc, msg->msg_flags);
184 if (rc)
185 goto out_err;
186 continue;
187 }
188
189 /* initialize variables for 1st iteration of subsequent loop */
190 /* could be just 1 byte, even after smc_tx_wait above */
191 writespace = atomic_read(&conn->sndbuf_space);
192 /* not more than what user space asked for */
193 copylen = min_t(size_t, send_remaining, writespace);
194 /* determine start of sndbuf */
195 sndbuf_base = conn->sndbuf_desc->cpu_addr;
196 smc_curs_copy(&prep, &conn->tx_curs_prep, conn);
197 tx_cnt_prep = prep.count;
198 /* determine chunks where to write into sndbuf */
199 /* either unwrapped case, or 1st chunk of wrapped case */
200 chunk_len = min_t(size_t, copylen, conn->sndbuf_desc->len -
201 tx_cnt_prep);
202 chunk_len_sum = chunk_len;
203 chunk_off = tx_cnt_prep;
204 smc_sndbuf_sync_sg_for_cpu(conn);
205 for (chunk = 0; chunk < 2; chunk++) {
206 rc = memcpy_from_msg(sndbuf_base + chunk_off,
207 msg, chunk_len);
208 if (rc) {
209 smc_sndbuf_sync_sg_for_device(conn);
210 if (send_done)
211 return send_done;
212 goto out_err;
213 }
214 send_done += chunk_len;
215 send_remaining -= chunk_len;
216
217 if (chunk_len_sum == copylen)
218 break; /* either on 1st or 2nd iteration */
219 /* prepare next (== 2nd) iteration */
220 chunk_len = copylen - chunk_len; /* remainder */
221 chunk_len_sum += chunk_len;
222 chunk_off = 0; /* modulo offset in send ring buffer */
223 }
224 smc_sndbuf_sync_sg_for_device(conn);
225 /* update cursors */
226 smc_curs_add(conn->sndbuf_desc->len, &prep, copylen);
227 smc_curs_copy(&conn->tx_curs_prep, &prep, conn);
228 /* increased in send tasklet smc_cdc_tx_handler() */
229 smp_mb__before_atomic();
230 atomic_sub(copylen, &conn->sndbuf_space);
231 /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
232 smp_mb__after_atomic();
233 /* since we just produced more new data into sndbuf,
234 * trigger sndbuf consumer: RDMA write into peer RMBE and CDC
235 */
236 if ((msg->msg_flags & MSG_OOB) && !send_remaining)
237 conn->urg_tx_pend = true;
238 if ((msg->msg_flags & MSG_MORE || smc_tx_is_corked(smc)) &&
239 (atomic_read(&conn->sndbuf_space) >
240 (conn->sndbuf_desc->len >> 1)))
241 /* for a corked socket defer the RDMA writes if there
242 * is still sufficient sndbuf_space available
243 */
244 queue_delayed_work(conn->lgr->tx_wq, &conn->tx_work,
245 SMC_TX_CORK_DELAY);
246 else
247 smc_tx_sndbuf_nonempty(conn);
248 } /* while (msg_data_left(msg)) */
249
250 return send_done;
251
252out_err:
253 rc = sk_stream_error(sk, msg->msg_flags, rc);
254 /* make sure we wake any epoll edge trigger waiter */
255 if (unlikely(rc == -EAGAIN))
256 sk->sk_write_space(sk);
257 return rc;
258}
259
260/***************************** sndbuf consumer *******************************/
261
262/* sndbuf consumer: actual data transfer of one target chunk with ISM write */
263int smcd_tx_ism_write(struct smc_connection *conn, void *data, size_t len,
264 u32 offset, int signal)
265{
266 struct smc_ism_position pos;
267 int rc;
268
269 memset(&pos, 0, sizeof(pos));
270 pos.token = conn->peer_token;
271 pos.index = conn->peer_rmbe_idx;
272 pos.offset = conn->tx_off + offset;
273 pos.signal = signal;
274 rc = smc_ism_write(conn->lgr->smcd, &pos, data, len);
275 if (rc)
276 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
277 return rc;
278}
279
280/* sndbuf consumer: actual data transfer of one target chunk with RDMA write */
281static int smc_tx_rdma_write(struct smc_connection *conn, int peer_rmbe_offset,
282 int num_sges, struct ib_rdma_wr *rdma_wr)
283{
284 struct smc_link_group *lgr = conn->lgr;
285 struct smc_link *link = conn->lnk;
286 int rc;
287
288 rdma_wr->wr.wr_id = smc_wr_tx_get_next_wr_id(link);
289 rdma_wr->wr.num_sge = num_sges;
290 rdma_wr->remote_addr =
291 lgr->rtokens[conn->rtoken_idx][link->link_idx].dma_addr +
292 /* RMBE within RMB */
293 conn->tx_off +
294 /* offset within RMBE */
295 peer_rmbe_offset;
296 rdma_wr->rkey = lgr->rtokens[conn->rtoken_idx][link->link_idx].rkey;
297 rc = ib_post_send(link->roce_qp, &rdma_wr->wr, NULL);
298 if (rc)
299 smcr_link_down_cond_sched(link);
300 return rc;
301}
302
303/* sndbuf consumer */
304static inline void smc_tx_advance_cursors(struct smc_connection *conn,
305 union smc_host_cursor *prod,
306 union smc_host_cursor *sent,
307 size_t len)
308{
309 smc_curs_add(conn->peer_rmbe_size, prod, len);
310 /* increased in recv tasklet smc_cdc_msg_rcv() */
311 smp_mb__before_atomic();
312 /* data in flight reduces usable snd_wnd */
313 atomic_sub(len, &conn->peer_rmbe_space);
314 /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
315 smp_mb__after_atomic();
316 smc_curs_add(conn->sndbuf_desc->len, sent, len);
317}
318
319/* SMC-R helper for smc_tx_rdma_writes() */
320static int smcr_tx_rdma_writes(struct smc_connection *conn, size_t len,
321 size_t src_off, size_t src_len,
322 size_t dst_off, size_t dst_len,
323 struct smc_rdma_wr *wr_rdma_buf)
324{
325 struct smc_link *link = conn->lnk;
326
327 dma_addr_t dma_addr =
328 sg_dma_address(conn->sndbuf_desc->sgt[link->link_idx].sgl);
329 int src_len_sum = src_len, dst_len_sum = dst_len;
330 int sent_count = src_off;
331 int srcchunk, dstchunk;
332 int num_sges;
333 int rc;
334
335 for (dstchunk = 0; dstchunk < 2; dstchunk++) {
336 struct ib_sge *sge =
337 wr_rdma_buf->wr_tx_rdma[dstchunk].wr.sg_list;
338
339 num_sges = 0;
340 for (srcchunk = 0; srcchunk < 2; srcchunk++) {
341 sge[srcchunk].addr = dma_addr + src_off;
342 sge[srcchunk].length = src_len;
343 num_sges++;
344
345 src_off += src_len;
346 if (src_off >= conn->sndbuf_desc->len)
347 src_off -= conn->sndbuf_desc->len;
348 /* modulo in send ring */
349 if (src_len_sum == dst_len)
350 break; /* either on 1st or 2nd iteration */
351 /* prepare next (== 2nd) iteration */
352 src_len = dst_len - src_len; /* remainder */
353 src_len_sum += src_len;
354 }
355 rc = smc_tx_rdma_write(conn, dst_off, num_sges,
356 &wr_rdma_buf->wr_tx_rdma[dstchunk]);
357 if (rc)
358 return rc;
359 if (dst_len_sum == len)
360 break; /* either on 1st or 2nd iteration */
361 /* prepare next (== 2nd) iteration */
362 dst_off = 0; /* modulo offset in RMBE ring buffer */
363 dst_len = len - dst_len; /* remainder */
364 dst_len_sum += dst_len;
365 src_len = min_t(int, dst_len, conn->sndbuf_desc->len -
366 sent_count);
367 src_len_sum = src_len;
368 }
369 return 0;
370}
371
372/* SMC-D helper for smc_tx_rdma_writes() */
373static int smcd_tx_rdma_writes(struct smc_connection *conn, size_t len,
374 size_t src_off, size_t src_len,
375 size_t dst_off, size_t dst_len)
376{
377 int src_len_sum = src_len, dst_len_sum = dst_len;
378 int srcchunk, dstchunk;
379 int rc;
380
381 for (dstchunk = 0; dstchunk < 2; dstchunk++) {
382 for (srcchunk = 0; srcchunk < 2; srcchunk++) {
383 void *data = conn->sndbuf_desc->cpu_addr + src_off;
384
385 rc = smcd_tx_ism_write(conn, data, src_len, dst_off +
386 sizeof(struct smcd_cdc_msg), 0);
387 if (rc)
388 return rc;
389 dst_off += src_len;
390 src_off += src_len;
391 if (src_off >= conn->sndbuf_desc->len)
392 src_off -= conn->sndbuf_desc->len;
393 /* modulo in send ring */
394 if (src_len_sum == dst_len)
395 break; /* either on 1st or 2nd iteration */
396 /* prepare next (== 2nd) iteration */
397 src_len = dst_len - src_len; /* remainder */
398 src_len_sum += src_len;
399 }
400 if (dst_len_sum == len)
401 break; /* either on 1st or 2nd iteration */
402 /* prepare next (== 2nd) iteration */
403 dst_off = 0; /* modulo offset in RMBE ring buffer */
404 dst_len = len - dst_len; /* remainder */
405 dst_len_sum += dst_len;
406 src_len = min_t(int, dst_len, conn->sndbuf_desc->len - src_off);
407 src_len_sum = src_len;
408 }
409 return 0;
410}
411
412/* sndbuf consumer: prepare all necessary (src&dst) chunks of data transmit;
413 * usable snd_wnd as max transmit
414 */
415static int smc_tx_rdma_writes(struct smc_connection *conn,
416 struct smc_rdma_wr *wr_rdma_buf)
417{
418 size_t len, src_len, dst_off, dst_len; /* current chunk values */
419 union smc_host_cursor sent, prep, prod, cons;
420 struct smc_cdc_producer_flags *pflags;
421 int to_send, rmbespace;
422 int rc;
423
424 /* source: sndbuf */
425 smc_curs_copy(&sent, &conn->tx_curs_sent, conn);
426 smc_curs_copy(&prep, &conn->tx_curs_prep, conn);
427 /* cf. wmem_alloc - (snd_max - snd_una) */
428 to_send = smc_curs_diff(conn->sndbuf_desc->len, &sent, &prep);
429 if (to_send <= 0)
430 return 0;
431
432 /* destination: RMBE */
433 /* cf. snd_wnd */
434 rmbespace = atomic_read(&conn->peer_rmbe_space);
435 if (rmbespace <= 0) {
436 struct smc_sock *smc = container_of(conn, struct smc_sock,
437 conn);
438 SMC_STAT_RMB_TX_PEER_FULL(smc, !conn->lnk);
439 return 0;
440 }
441 smc_curs_copy(&prod, &conn->local_tx_ctrl.prod, conn);
442 smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
443
444 /* if usable snd_wnd closes ask peer to advertise once it opens again */
445 pflags = &conn->local_tx_ctrl.prod_flags;
446 pflags->write_blocked = (to_send >= rmbespace);
447 /* cf. usable snd_wnd */
448 len = min(to_send, rmbespace);
449
450 /* initialize variables for first iteration of subsequent nested loop */
451 dst_off = prod.count;
452 if (prod.wrap == cons.wrap) {
453 /* the filled destination area is unwrapped,
454 * hence the available free destination space is wrapped
455 * and we need 2 destination chunks of sum len; start with 1st
456 * which is limited by what's available in sndbuf
457 */
458 dst_len = min_t(size_t,
459 conn->peer_rmbe_size - prod.count, len);
460 } else {
461 /* the filled destination area is wrapped,
462 * hence the available free destination space is unwrapped
463 * and we need a single destination chunk of entire len
464 */
465 dst_len = len;
466 }
467 /* dst_len determines the maximum src_len */
468 if (sent.count + dst_len <= conn->sndbuf_desc->len) {
469 /* unwrapped src case: single chunk of entire dst_len */
470 src_len = dst_len;
471 } else {
472 /* wrapped src case: 2 chunks of sum dst_len; start with 1st: */
473 src_len = conn->sndbuf_desc->len - sent.count;
474 }
475
476 if (conn->lgr->is_smcd)
477 rc = smcd_tx_rdma_writes(conn, len, sent.count, src_len,
478 dst_off, dst_len);
479 else
480 rc = smcr_tx_rdma_writes(conn, len, sent.count, src_len,
481 dst_off, dst_len, wr_rdma_buf);
482 if (rc)
483 return rc;
484
485 if (conn->urg_tx_pend && len == to_send)
486 pflags->urg_data_present = 1;
487 smc_tx_advance_cursors(conn, &prod, &sent, len);
488 /* update connection's cursors with advanced local cursors */
489 smc_curs_copy(&conn->local_tx_ctrl.prod, &prod, conn);
490 /* dst: peer RMBE */
491 smc_curs_copy(&conn->tx_curs_sent, &sent, conn);/* src: local sndbuf */
492
493 return 0;
494}
495
496/* Wakeup sndbuf consumers from any context (IRQ or process)
497 * since there is more data to transmit; usable snd_wnd as max transmit
498 */
499static int smcr_tx_sndbuf_nonempty(struct smc_connection *conn)
500{
501 struct smc_cdc_producer_flags *pflags = &conn->local_tx_ctrl.prod_flags;
502 struct smc_link *link = conn->lnk;
503 struct smc_rdma_wr *wr_rdma_buf;
504 struct smc_cdc_tx_pend *pend;
505 struct smc_wr_buf *wr_buf;
506 int rc;
507
508 if (!link || !smc_wr_tx_link_hold(link))
509 return -ENOLINK;
510 rc = smc_cdc_get_free_slot(conn, link, &wr_buf, &wr_rdma_buf, &pend);
511 if (rc < 0) {
512 smc_wr_tx_link_put(link);
513 if (rc == -EBUSY) {
514 struct smc_sock *smc =
515 container_of(conn, struct smc_sock, conn);
516
517 if (smc->sk.sk_err == ECONNABORTED)
518 return sock_error(&smc->sk);
519 if (conn->killed)
520 return -EPIPE;
521 rc = 0;
522 mod_delayed_work(conn->lgr->tx_wq, &conn->tx_work,
523 SMC_TX_WORK_DELAY);
524 }
525 return rc;
526 }
527
528 spin_lock_bh(&conn->send_lock);
529 if (link != conn->lnk) {
530 /* link of connection changed, tx_work will restart */
531 smc_wr_tx_put_slot(link,
532 (struct smc_wr_tx_pend_priv *)pend);
533 rc = -ENOLINK;
534 goto out_unlock;
535 }
536 if (!pflags->urg_data_present) {
537 rc = smc_tx_rdma_writes(conn, wr_rdma_buf);
538 if (rc) {
539 smc_wr_tx_put_slot(link,
540 (struct smc_wr_tx_pend_priv *)pend);
541 goto out_unlock;
542 }
543 }
544
545 rc = smc_cdc_msg_send(conn, wr_buf, pend);
546 if (!rc && pflags->urg_data_present) {
547 pflags->urg_data_pending = 0;
548 pflags->urg_data_present = 0;
549 }
550
551out_unlock:
552 spin_unlock_bh(&conn->send_lock);
553 smc_wr_tx_link_put(link);
554 return rc;
555}
556
557static int smcd_tx_sndbuf_nonempty(struct smc_connection *conn)
558{
559 struct smc_cdc_producer_flags *pflags = &conn->local_tx_ctrl.prod_flags;
560 int rc = 0;
561
562 spin_lock_bh(&conn->send_lock);
563 if (!pflags->urg_data_present)
564 rc = smc_tx_rdma_writes(conn, NULL);
565 if (!rc)
566 rc = smcd_cdc_msg_send(conn);
567
568 if (!rc && pflags->urg_data_present) {
569 pflags->urg_data_pending = 0;
570 pflags->urg_data_present = 0;
571 }
572 spin_unlock_bh(&conn->send_lock);
573 return rc;
574}
575
576int smc_tx_sndbuf_nonempty(struct smc_connection *conn)
577{
578 int rc;
579
580 if (conn->killed ||
581 conn->local_rx_ctrl.conn_state_flags.peer_conn_abort)
582 return -EPIPE; /* connection being aborted */
583 if (conn->lgr->is_smcd)
584 rc = smcd_tx_sndbuf_nonempty(conn);
585 else
586 rc = smcr_tx_sndbuf_nonempty(conn);
587
588 if (!rc) {
589 /* trigger socket release if connection is closing */
590 struct smc_sock *smc = container_of(conn, struct smc_sock,
591 conn);
592 smc_close_wake_tx_prepared(smc);
593 }
594 return rc;
595}
596
597/* Wakeup sndbuf consumers from process context
598 * since there is more data to transmit
599 */
600void smc_tx_work(struct work_struct *work)
601{
602 struct smc_connection *conn = container_of(to_delayed_work(work),
603 struct smc_connection,
604 tx_work);
605 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
606 int rc;
607
608 lock_sock(&smc->sk);
609 if (smc->sk.sk_err)
610 goto out;
611
612 rc = smc_tx_sndbuf_nonempty(conn);
613 if (!rc && conn->local_rx_ctrl.prod_flags.write_blocked &&
614 !atomic_read(&conn->bytes_to_rcv))
615 conn->local_rx_ctrl.prod_flags.write_blocked = 0;
616
617out:
618 release_sock(&smc->sk);
619}
620
621void smc_tx_consumer_update(struct smc_connection *conn, bool force)
622{
623 union smc_host_cursor cfed, cons, prod;
624 int sender_free = conn->rmb_desc->len;
625 int to_confirm;
626
627 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
628 smc_curs_copy(&cfed, &conn->rx_curs_confirmed, conn);
629 to_confirm = smc_curs_diff(conn->rmb_desc->len, &cfed, &cons);
630 if (to_confirm > conn->rmbe_update_limit) {
631 smc_curs_copy(&prod, &conn->local_rx_ctrl.prod, conn);
632 sender_free = conn->rmb_desc->len -
633 smc_curs_diff_large(conn->rmb_desc->len,
634 &cfed, &prod);
635 }
636
637 if (conn->local_rx_ctrl.prod_flags.cons_curs_upd_req ||
638 force ||
639 ((to_confirm > conn->rmbe_update_limit) &&
640 ((sender_free <= (conn->rmb_desc->len / 2)) ||
641 conn->local_rx_ctrl.prod_flags.write_blocked))) {
642 if (conn->killed ||
643 conn->local_rx_ctrl.conn_state_flags.peer_conn_abort)
644 return;
645 if ((smc_cdc_get_slot_and_msg_send(conn) < 0) &&
646 !conn->killed) {
647 queue_delayed_work(conn->lgr->tx_wq, &conn->tx_work,
648 SMC_TX_WORK_DELAY);
649 return;
650 }
651 }
652 if (conn->local_rx_ctrl.prod_flags.write_blocked &&
653 !atomic_read(&conn->bytes_to_rcv))
654 conn->local_rx_ctrl.prod_flags.write_blocked = 0;
655}
656
657/***************************** send initialize *******************************/
658
659/* Initialize send properties on connection establishment. NB: not __init! */
660void smc_tx_init(struct smc_sock *smc)
661{
662 smc->sk.sk_write_space = smc_tx_write_space;
663}