Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 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> |
Ingo Molnar | c3edc40 | 2017-02-02 08:35:14 +0100 | [diff] [blame] | 19 | #include <linux/sched/signal.h> |
| 20 | |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 21 | #include <net/sock.h> |
Ursula Braun | 01d2f7e | 2018-04-26 17:18:22 +0200 | [diff] [blame] | 22 | #include <net/tcp.h> |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 23 | |
| 24 | #include "smc.h" |
| 25 | #include "smc_wr.h" |
| 26 | #include "smc_cdc.h" |
Karsten Graul | 5bc056d | 2019-02-12 16:29:51 +0100 | [diff] [blame] | 27 | #include "smc_close.h" |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 28 | #include "smc_ism.h" |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 29 | #include "smc_tx.h" |
Guvenc Gulce | e0e4b8f | 2021-06-16 16:52:55 +0200 | [diff] [blame] | 30 | #include "smc_stats.h" |
Tony Lu | aff3083 | 2021-11-01 15:39:14 +0800 | [diff] [blame] | 31 | #include "smc_tracepoint.h" |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 32 | |
Karsten Graul | 16297d1 | 2019-02-12 16:29:52 +0100 | [diff] [blame] | 33 | #define SMC_TX_WORK_DELAY 0 |
Ursula Braun | 01d2f7e | 2018-04-26 17:18:22 +0200 | [diff] [blame] | 34 | #define SMC_TX_CORK_DELAY (HZ >> 2) /* 250 ms */ |
Ursula Braun | 18e537c | 2017-09-21 09:16:33 +0200 | [diff] [blame] | 35 | |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 36 | /***************************** sndbuf producer *******************************/ |
| 37 | |
| 38 | /* callback implementation for sk.sk_write_space() |
Stefan Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 39 | * to wakeup sndbuf producers that blocked with smc_tx_wait(). |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 40 | * called under sk_socket lock. |
| 41 | */ |
| 42 | static 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 Gulce | e0e4b8f | 2021-06-16 16:52:55 +0200 | [diff] [blame] | 50 | if (test_bit(SOCK_NOSPACE, &sock->flags)) |
Guvenc Gulce | 194730a | 2021-06-16 16:52:58 +0200 | [diff] [blame] | 51 | SMC_STAT_RMB_TX_FULL(smc, !smc->conn.lnk); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 52 | 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 Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 57 | EPOLLOUT | EPOLLWRNORM | |
| 58 | EPOLLWRBAND); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 59 | 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 Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 65 | /* Wakeup sndbuf producers that blocked with smc_tx_wait(). |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 66 | * Cf. tcp_data_snd_check()=>tcp_check_space()=>tcp_new_space(). |
| 67 | */ |
| 68 | void 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 Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 75 | /* blocks sndbuf producer until at least one byte of free space available |
| 76 | * or urgent Byte was consumed |
| 77 | */ |
| 78 | static int smc_tx_wait(struct smc_sock *smc, int flags) |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 79 | { |
| 80 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
| 81 | struct smc_connection *conn = &smc->conn; |
| 82 | struct sock *sk = &smc->sk; |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 83 | long timeo; |
| 84 | int rc = 0; |
| 85 | |
| 86 | /* similar to sk_stream_wait_memory */ |
| 87 | timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 88 | 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 Braun | b290098 | 2019-10-21 16:13:08 +0200 | [diff] [blame] | 93 | conn->killed || |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 94 | conn->local_tx_ctrl.conn_state_flags.peer_done_writing) { |
| 95 | rc = -EPIPE; |
| 96 | break; |
| 97 | } |
Ursula Braun | aa377e6 | 2018-01-24 10:28:17 +0100 | [diff] [blame] | 98 | if (smc_cdc_rxed_any_close(conn)) { |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 99 | rc = -ECONNRESET; |
| 100 | break; |
| 101 | } |
| 102 | if (!timeo) { |
Jason Baron | 4651d18 | 2019-08-19 14:36:01 -0400 | [diff] [blame] | 103 | /* ensure EPOLLOUT is subsequently generated */ |
| 104 | set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 105 | 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 Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 113 | if (atomic_read(&conn->sndbuf_space) && !conn->urg_tx_pend) |
| 114 | break; /* at least 1 byte of free & no urgent data */ |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 115 | set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 116 | sk_wait_event(sk, &timeo, |
| 117 | sk->sk_err || |
| 118 | (sk->sk_shutdown & SEND_SHUTDOWN) || |
Ursula Braun | aa377e6 | 2018-01-24 10:28:17 +0100 | [diff] [blame] | 119 | smc_cdc_rxed_any_close(conn) || |
Stefan Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 120 | (atomic_read(&conn->sndbuf_space) && |
| 121 | !conn->urg_tx_pend), |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 122 | &wait); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 123 | } |
| 124 | remove_wait_queue(sk_sleep(sk), &wait); |
| 125 | return rc; |
| 126 | } |
| 127 | |
Ursula Braun | 01d2f7e | 2018-04-26 17:18:22 +0200 | [diff] [blame] | 128 | static 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 Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 135 | /* sndbuf producer: main API called by socket layer. |
| 136 | * called under sock lock. |
| 137 | */ |
| 138 | int 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 Gulce | 1708163 | 2021-06-25 17:11:02 +0200 | [diff] [blame] | 158 | if (sk->sk_state == SMC_INIT) |
| 159 | return -ENOTCONN; |
| 160 | |
Guvenc Gulce | e0e4b8f | 2021-06-16 16:52:55 +0200 | [diff] [blame] | 161 | if (len > conn->sndbuf_desc->len) |
Guvenc Gulce | 194730a | 2021-06-16 16:52:58 +0200 | [diff] [blame] | 162 | SMC_STAT_RMB_TX_SIZE_SMALL(smc, !conn->lnk); |
Guvenc Gulce | e0e4b8f | 2021-06-16 16:52:55 +0200 | [diff] [blame] | 163 | |
| 164 | if (len > conn->peer_rmbe_size) |
Guvenc Gulce | 194730a | 2021-06-16 16:52:58 +0200 | [diff] [blame] | 165 | SMC_STAT_RMB_TX_PEER_SIZE_SMALL(smc, !conn->lnk); |
Guvenc Gulce | e0e4b8f | 2021-06-16 16:52:55 +0200 | [diff] [blame] | 166 | |
| 167 | if (msg->msg_flags & MSG_OOB) |
Guvenc Gulce | 194730a | 2021-06-16 16:52:58 +0200 | [diff] [blame] | 168 | SMC_STAT_INC(smc, urg_data_cnt); |
Guvenc Gulce | e0e4b8f | 2021-06-16 16:52:55 +0200 | [diff] [blame] | 169 | |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 170 | while (msg_data_left(msg)) { |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 171 | if (smc->sk.sk_shutdown & SEND_SHUTDOWN || |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 172 | (smc->sk.sk_err == ECONNABORTED) || |
Ursula Braun | b290098 | 2019-10-21 16:13:08 +0200 | [diff] [blame] | 173 | conn->killed) |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 174 | return -EPIPE; |
| 175 | if (smc_cdc_rxed_any_close(conn)) |
| 176 | return send_done ?: -ECONNRESET; |
| 177 | |
Stefan Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 178 | 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 Graul | 6889b36 | 2019-01-30 18:51:03 +0100 | [diff] [blame] | 182 | if (send_done) |
| 183 | return send_done; |
Stefan Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 184 | rc = smc_tx_wait(smc, msg->msg_flags); |
Karsten Graul | 6889b36 | 2019-01-30 18:51:03 +0100 | [diff] [blame] | 185 | if (rc) |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 186 | goto out_err; |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 187 | continue; |
| 188 | } |
| 189 | |
| 190 | /* initialize variables for 1st iteration of subsequent loop */ |
Stefan Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 191 | /* could be just 1 byte, even after smc_tx_wait above */ |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 192 | 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 Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 197 | smc_curs_copy(&prep, &conn->tx_curs_prep, conn); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 198 | tx_cnt_prep = prep.count; |
| 199 | /* determine chunks where to write into sndbuf */ |
| 200 | /* either unwrapped case, or 1st chunk of wrapped case */ |
Hans Wippel | 69cb7dc | 2018-05-18 09:34:10 +0200 | [diff] [blame] | 201 | chunk_len = min_t(size_t, copylen, conn->sndbuf_desc->len - |
| 202 | tx_cnt_prep); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 203 | chunk_len_sum = chunk_len; |
| 204 | chunk_off = tx_cnt_prep; |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 205 | smc_sndbuf_sync_sg_for_cpu(conn); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 206 | for (chunk = 0; chunk < 2; chunk++) { |
| 207 | rc = memcpy_from_msg(sndbuf_base + chunk_off, |
| 208 | msg, chunk_len); |
| 209 | if (rc) { |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 210 | smc_sndbuf_sync_sg_for_device(conn); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 211 | 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 Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 225 | smc_sndbuf_sync_sg_for_device(conn); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 226 | /* update cursors */ |
Hans Wippel | 69cb7dc | 2018-05-18 09:34:10 +0200 | [diff] [blame] | 227 | smc_curs_add(conn->sndbuf_desc->len, &prep, copylen); |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 228 | smc_curs_copy(&conn->tx_curs_prep, &prep, conn); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 229 | /* increased in send tasklet smc_cdc_tx_handler() */ |
| 230 | smp_mb__before_atomic(); |
| 231 | atomic_sub(copylen, &conn->sndbuf_space); |
Hans Wippel | 69cb7dc | 2018-05-18 09:34:10 +0200 | [diff] [blame] | 232 | /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */ |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 233 | 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 Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 237 | if ((msg->msg_flags & MSG_OOB) && !send_remaining) |
| 238 | conn->urg_tx_pend = true; |
Ursula Braun | 01d2f7e | 2018-04-26 17:18:22 +0200 | [diff] [blame] | 239 | if ((msg->msg_flags & MSG_MORE || smc_tx_is_corked(smc)) && |
| 240 | (atomic_read(&conn->sndbuf_space) > |
Hans Wippel | 69cb7dc | 2018-05-18 09:34:10 +0200 | [diff] [blame] | 241 | (conn->sndbuf_desc->len >> 1))) |
Ursula Braun | 01d2f7e | 2018-04-26 17:18:22 +0200 | [diff] [blame] | 242 | /* for a corked socket defer the RDMA writes if there |
| 243 | * is still sufficient sndbuf_space available |
| 244 | */ |
Karsten Graul | 22ef473 | 2020-09-10 18:48:29 +0200 | [diff] [blame] | 245 | queue_delayed_work(conn->lgr->tx_wq, &conn->tx_work, |
| 246 | SMC_TX_CORK_DELAY); |
Ursula Braun | 01d2f7e | 2018-04-26 17:18:22 +0200 | [diff] [blame] | 247 | else |
| 248 | smc_tx_sndbuf_nonempty(conn); |
Tony Lu | aff3083 | 2021-11-01 15:39:14 +0800 | [diff] [blame] | 249 | |
| 250 | trace_smc_tx_sendmsg(smc, copylen); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 251 | } /* while (msg_data_left(msg)) */ |
| 252 | |
| 253 | return send_done; |
| 254 | |
| 255 | out_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 Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 265 | /* sndbuf consumer: actual data transfer of one target chunk with ISM write */ |
| 266 | int 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 Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 283 | /* sndbuf consumer: actual data transfer of one target chunk with RDMA write */ |
| 284 | static int smc_tx_rdma_write(struct smc_connection *conn, int peer_rmbe_offset, |
Ursula Braun | ad6f317 | 2019-02-04 13:44:44 +0100 | [diff] [blame] | 285 | int num_sges, struct ib_rdma_wr *rdma_wr) |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 286 | { |
| 287 | struct smc_link_group *lgr = conn->lgr; |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 288 | struct smc_link *link = conn->lnk; |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 289 | int rc; |
| 290 | |
Ursula Braun | ad6f317 | 2019-02-04 13:44:44 +0100 | [diff] [blame] | 291 | 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 Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 294 | lgr->rtokens[conn->rtoken_idx][link->link_idx].dma_addr + |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 295 | /* RMBE within RMB */ |
Hans Wippel | 95d8d263 | 2018-05-18 09:34:13 +0200 | [diff] [blame] | 296 | conn->tx_off + |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 297 | /* offset within RMBE */ |
| 298 | peer_rmbe_offset; |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 299 | rdma_wr->rkey = lgr->rtokens[conn->rtoken_idx][link->link_idx].rkey; |
Ursula Braun | ad6f317 | 2019-02-04 13:44:44 +0100 | [diff] [blame] | 300 | rc = ib_post_send(link->roce_qp, &rdma_wr->wr, NULL); |
Ursula Braun | b290098 | 2019-10-21 16:13:08 +0200 | [diff] [blame] | 301 | if (rc) |
Karsten Graul | 8752393 | 2020-05-01 12:48:09 +0200 | [diff] [blame] | 302 | smcr_link_down_cond_sched(link); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 303 | return rc; |
| 304 | } |
| 305 | |
| 306 | /* sndbuf consumer */ |
| 307 | static 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 Wippel | 69cb7dc | 2018-05-18 09:34:10 +0200 | [diff] [blame] | 319 | smc_curs_add(conn->sndbuf_desc->len, sent, len); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 320 | } |
| 321 | |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 322 | /* SMC-R helper for smc_tx_rdma_writes() */ |
| 323 | static int smcr_tx_rdma_writes(struct smc_connection *conn, size_t len, |
| 324 | size_t src_off, size_t src_len, |
Ursula Braun | ad6f317 | 2019-02-04 13:44:44 +0100 | [diff] [blame] | 325 | size_t dst_off, size_t dst_len, |
| 326 | struct smc_rdma_wr *wr_rdma_buf) |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 327 | { |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 328 | struct smc_link *link = conn->lnk; |
| 329 | |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 330 | dma_addr_t dma_addr = |
Karsten Graul | 387707f | 2020-04-29 17:10:40 +0200 | [diff] [blame] | 331 | sg_dma_address(conn->sndbuf_desc->sgt[link->link_idx].sgl); |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 332 | int src_len_sum = src_len, dst_len_sum = dst_len; |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 333 | int sent_count = src_off; |
| 334 | int srcchunk, dstchunk; |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 335 | int num_sges; |
| 336 | int rc; |
| 337 | |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 338 | for (dstchunk = 0; dstchunk < 2; dstchunk++) { |
Ursula Braun | ad6f317 | 2019-02-04 13:44:44 +0100 | [diff] [blame] | 339 | struct ib_sge *sge = |
| 340 | wr_rdma_buf->wr_tx_rdma[dstchunk].wr.sg_list; |
| 341 | |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 342 | num_sges = 0; |
| 343 | for (srcchunk = 0; srcchunk < 2; srcchunk++) { |
Ursula Braun | ad6f317 | 2019-02-04 13:44:44 +0100 | [diff] [blame] | 344 | sge[srcchunk].addr = dma_addr + src_off; |
| 345 | sge[srcchunk].length = src_len; |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 346 | num_sges++; |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 347 | |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 348 | src_off += src_len; |
Hans Wippel | 69cb7dc | 2018-05-18 09:34:10 +0200 | [diff] [blame] | 349 | if (src_off >= conn->sndbuf_desc->len) |
| 350 | src_off -= conn->sndbuf_desc->len; |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 351 | /* 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 Braun | ad6f317 | 2019-02-04 13:44:44 +0100 | [diff] [blame] | 358 | rc = smc_tx_rdma_write(conn, dst_off, num_sges, |
| 359 | &wr_rdma_buf->wr_tx_rdma[dstchunk]); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 360 | 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 Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 368 | src_len = min_t(int, dst_len, conn->sndbuf_desc->len - |
| 369 | sent_count); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 370 | src_len_sum = src_len; |
| 371 | } |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | /* SMC-D helper for smc_tx_rdma_writes() */ |
| 376 | static 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 Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 415 | /* sndbuf consumer: prepare all necessary (src&dst) chunks of data transmit; |
| 416 | * usable snd_wnd as max transmit |
| 417 | */ |
Ursula Braun | ad6f317 | 2019-02-04 13:44:44 +0100 | [diff] [blame] | 418 | static int smc_tx_rdma_writes(struct smc_connection *conn, |
| 419 | struct smc_rdma_wr *wr_rdma_buf) |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 420 | { |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 421 | size_t len, src_len, dst_off, dst_len; /* current chunk values */ |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 422 | union smc_host_cursor sent, prep, prod, cons; |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 423 | struct smc_cdc_producer_flags *pflags; |
| 424 | int to_send, rmbespace; |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 425 | int rc; |
| 426 | |
| 427 | /* source: sndbuf */ |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 428 | smc_curs_copy(&sent, &conn->tx_curs_sent, conn); |
| 429 | smc_curs_copy(&prep, &conn->tx_curs_prep, conn); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 430 | /* 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 Gulce | e0e4b8f | 2021-06-16 16:52:55 +0200 | [diff] [blame] | 438 | if (rmbespace <= 0) { |
Guvenc Gulce | 194730a | 2021-06-16 16:52:58 +0200 | [diff] [blame] | 439 | struct smc_sock *smc = container_of(conn, struct smc_sock, |
| 440 | conn); |
| 441 | SMC_STAT_RMB_TX_PEER_FULL(smc, !conn->lnk); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 442 | return 0; |
Guvenc Gulce | e0e4b8f | 2021-06-16 16:52:55 +0200 | [diff] [blame] | 443 | } |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 444 | smc_curs_copy(&prod, &conn->local_tx_ctrl.prod, conn); |
| 445 | smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 446 | |
| 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 Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 454 | 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 Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 470 | /* 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 Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 478 | |
| 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 Braun | ad6f317 | 2019-02-04 13:44:44 +0100 | [diff] [blame] | 484 | dst_off, dst_len, wr_rdma_buf); |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 485 | if (rc) |
| 486 | return rc; |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 487 | |
Stefan Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 488 | if (conn->urg_tx_pend && len == to_send) |
| 489 | pflags->urg_data_present = 1; |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 490 | smc_tx_advance_cursors(conn, &prod, &sent, len); |
| 491 | /* update connection's cursors with advanced local cursors */ |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 492 | smc_curs_copy(&conn->local_tx_ctrl.prod, &prod, conn); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 493 | /* dst: peer RMBE */ |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 494 | smc_curs_copy(&conn->tx_curs_sent, &sent, conn);/* src: local sndbuf */ |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 495 | |
| 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 Graul | 95f7f3e | 2021-10-07 16:14:40 +0200 | [diff] [blame] | 502 | static int smcr_tx_sndbuf_nonempty(struct smc_connection *conn) |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 503 | { |
Ursula Braun | cecc7a3 | 2019-02-21 13:00:58 +0100 | [diff] [blame] | 504 | struct smc_cdc_producer_flags *pflags = &conn->local_tx_ctrl.prod_flags; |
Karsten Graul | c6f02eb | 2020-05-04 14:18:38 +0200 | [diff] [blame] | 505 | struct smc_link *link = conn->lnk; |
Ursula Braun | ad6f317 | 2019-02-04 13:44:44 +0100 | [diff] [blame] | 506 | struct smc_rdma_wr *wr_rdma_buf; |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 507 | struct smc_cdc_tx_pend *pend; |
| 508 | struct smc_wr_buf *wr_buf; |
| 509 | int rc; |
| 510 | |
Karsten Graul | 95f7f3e | 2021-10-07 16:14:40 +0200 | [diff] [blame] | 511 | if (!link || !smc_wr_tx_link_hold(link)) |
| 512 | return -ENOLINK; |
Karsten Graul | c6f02eb | 2020-05-04 14:18:38 +0200 | [diff] [blame] | 513 | rc = smc_cdc_get_free_slot(conn, link, &wr_buf, &wr_rdma_buf, &pend); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 514 | if (rc < 0) { |
Karsten Graul | 95f7f3e | 2021-10-07 16:14:40 +0200 | [diff] [blame] | 515 | smc_wr_tx_link_put(link); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 516 | if (rc == -EBUSY) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 517 | struct smc_sock *smc = |
| 518 | container_of(conn, struct smc_sock, conn); |
| 519 | |
Karsten Graul | 33f3fcc | 2019-01-30 18:51:05 +0100 | [diff] [blame] | 520 | if (smc->sk.sk_err == ECONNABORTED) |
| 521 | return sock_error(&smc->sk); |
Ursula Braun | b290098 | 2019-10-21 16:13:08 +0200 | [diff] [blame] | 522 | if (conn->killed) |
| 523 | return -EPIPE; |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 524 | rc = 0; |
Karsten Graul | 22ef473 | 2020-09-10 18:48:29 +0200 | [diff] [blame] | 525 | mod_delayed_work(conn->lgr->tx_wq, &conn->tx_work, |
Ursula Braun | b290098 | 2019-10-21 16:13:08 +0200 | [diff] [blame] | 526 | SMC_TX_WORK_DELAY); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 527 | } |
Karsten Graul | 33f3fcc | 2019-01-30 18:51:05 +0100 | [diff] [blame] | 528 | return rc; |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 529 | } |
| 530 | |
Karsten Graul | 33f3fcc | 2019-01-30 18:51:05 +0100 | [diff] [blame] | 531 | spin_lock_bh(&conn->send_lock); |
Karsten Graul | c6f02eb | 2020-05-04 14:18:38 +0200 | [diff] [blame] | 532 | 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 Braun | cecc7a3 | 2019-02-21 13:00:58 +0100 | [diff] [blame] | 539 | if (!pflags->urg_data_present) { |
Ursula Braun | ad6f317 | 2019-02-04 13:44:44 +0100 | [diff] [blame] | 540 | rc = smc_tx_rdma_writes(conn, wr_rdma_buf); |
Stefan Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 541 | if (rc) { |
Karsten Graul | c6f02eb | 2020-05-04 14:18:38 +0200 | [diff] [blame] | 542 | smc_wr_tx_put_slot(link, |
Stefan Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 543 | (struct smc_wr_tx_pend_priv *)pend); |
| 544 | goto out_unlock; |
| 545 | } |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | rc = smc_cdc_msg_send(conn, wr_buf, pend); |
Stefan Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 549 | if (!rc && pflags->urg_data_present) { |
| 550 | pflags->urg_data_pending = 0; |
| 551 | pflags->urg_data_present = 0; |
| 552 | } |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 553 | |
| 554 | out_unlock: |
| 555 | spin_unlock_bh(&conn->send_lock); |
Karsten Graul | 95f7f3e | 2021-10-07 16:14:40 +0200 | [diff] [blame] | 556 | smc_wr_tx_link_put(link); |
Karsten Graul | 8f3d65c | 2021-08-09 11:05:56 +0200 | [diff] [blame] | 557 | return rc; |
| 558 | } |
| 559 | |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 560 | static 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 Braun | ad6f317 | 2019-02-04 13:44:44 +0100 | [diff] [blame] | 567 | rc = smc_tx_rdma_writes(conn, NULL); |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 568 | 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 | |
| 579 | int smc_tx_sndbuf_nonempty(struct smc_connection *conn) |
| 580 | { |
| 581 | int rc; |
| 582 | |
Ursula Braun | b290098 | 2019-10-21 16:13:08 +0200 | [diff] [blame] | 583 | if (conn->killed || |
| 584 | conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) |
| 585 | return -EPIPE; /* connection being aborted */ |
Hans Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 586 | if (conn->lgr->is_smcd) |
| 587 | rc = smcd_tx_sndbuf_nonempty(conn); |
| 588 | else |
| 589 | rc = smcr_tx_sndbuf_nonempty(conn); |
| 590 | |
Karsten Graul | 5bc056d | 2019-02-12 16:29:51 +0100 | [diff] [blame] | 591 | 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 Wippel | be244f2 | 2018-06-28 19:05:10 +0200 | [diff] [blame] | 597 | return rc; |
| 598 | } |
| 599 | |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 600 | /* Wakeup sndbuf consumers from process context |
| 601 | * since there is more data to transmit |
| 602 | */ |
Eric Dumazet | be7f3e5 | 2018-05-17 03:54:21 -0700 | [diff] [blame] | 603 | void smc_tx_work(struct work_struct *work) |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 604 | { |
Ursula Braun | 18e537c | 2017-09-21 09:16:33 +0200 | [diff] [blame] | 605 | struct smc_connection *conn = container_of(to_delayed_work(work), |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 606 | struct smc_connection, |
| 607 | tx_work); |
| 608 | struct smc_sock *smc = container_of(conn, struct smc_sock, conn); |
Ursula Braun | 90cacb2 | 2017-04-10 14:57:59 +0200 | [diff] [blame] | 609 | int rc; |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 610 | |
| 611 | lock_sock(&smc->sk); |
Ursula Braun | b290098 | 2019-10-21 16:13:08 +0200 | [diff] [blame] | 612 | if (smc->sk.sk_err) |
Ursula Braun | 1a0a04c | 2018-01-25 11:15:36 +0100 | [diff] [blame] | 613 | goto out; |
| 614 | |
Ursula Braun | 90cacb2 | 2017-04-10 14:57:59 +0200 | [diff] [blame] | 615 | 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 Braun | 1a0a04c | 2018-01-25 11:15:36 +0100 | [diff] [blame] | 619 | |
| 620 | out: |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 621 | release_sock(&smc->sk); |
| 622 | } |
| 623 | |
Stefan Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 624 | void smc_tx_consumer_update(struct smc_connection *conn, bool force) |
Ursula Braun | 952310c | 2017-01-09 16:55:24 +0100 | [diff] [blame] | 625 | { |
Ursula Braun | 99be51f | 2018-07-18 15:22:49 +0200 | [diff] [blame] | 626 | union smc_host_cursor cfed, cons, prod; |
| 627 | int sender_free = conn->rmb_desc->len; |
Ursula Braun | 6b5771a | 2017-12-07 13:38:48 +0100 | [diff] [blame] | 628 | int to_confirm; |
Ursula Braun | 952310c | 2017-01-09 16:55:24 +0100 | [diff] [blame] | 629 | |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 630 | smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn); |
| 631 | smc_curs_copy(&cfed, &conn->rx_curs_confirmed, conn); |
Hans Wippel | 69cb7dc | 2018-05-18 09:34:10 +0200 | [diff] [blame] | 632 | to_confirm = smc_curs_diff(conn->rmb_desc->len, &cfed, &cons); |
Ursula Braun | 99be51f | 2018-07-18 15:22:49 +0200 | [diff] [blame] | 633 | if (to_confirm > conn->rmbe_update_limit) { |
Stefan Raspl | bac6de7 | 2018-07-23 13:53:09 +0200 | [diff] [blame] | 634 | smc_curs_copy(&prod, &conn->local_rx_ctrl.prod, conn); |
Ursula Braun | 99be51f | 2018-07-18 15:22:49 +0200 | [diff] [blame] | 635 | sender_free = conn->rmb_desc->len - |
Ursula Braun | b8649ef | 2019-02-04 13:44:45 +0100 | [diff] [blame] | 636 | smc_curs_diff_large(conn->rmb_desc->len, |
| 637 | &cfed, &prod); |
Ursula Braun | 99be51f | 2018-07-18 15:22:49 +0200 | [diff] [blame] | 638 | } |
Ursula Braun | 952310c | 2017-01-09 16:55:24 +0100 | [diff] [blame] | 639 | |
| 640 | if (conn->local_rx_ctrl.prod_flags.cons_curs_upd_req || |
Stefan Raspl | de8474e | 2018-05-23 16:38:11 +0200 | [diff] [blame] | 641 | force || |
Ursula Braun | 952310c | 2017-01-09 16:55:24 +0100 | [diff] [blame] | 642 | ((to_confirm > conn->rmbe_update_limit) && |
Ursula Braun | 99be51f | 2018-07-18 15:22:49 +0200 | [diff] [blame] | 643 | ((sender_free <= (conn->rmb_desc->len / 2)) || |
Ursula Braun | 952310c | 2017-01-09 16:55:24 +0100 | [diff] [blame] | 644 | conn->local_rx_ctrl.prod_flags.write_blocked))) { |
Ursula Braun | b290098 | 2019-10-21 16:13:08 +0200 | [diff] [blame] | 645 | if (conn->killed || |
| 646 | conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) |
| 647 | return; |
Ursula Braun | 1a0a04c | 2018-01-25 11:15:36 +0100 | [diff] [blame] | 648 | if ((smc_cdc_get_slot_and_msg_send(conn) < 0) && |
Ursula Braun | b290098 | 2019-10-21 16:13:08 +0200 | [diff] [blame] | 649 | !conn->killed) { |
Karsten Graul | 22ef473 | 2020-09-10 18:48:29 +0200 | [diff] [blame] | 650 | queue_delayed_work(conn->lgr->tx_wq, &conn->tx_work, |
| 651 | SMC_TX_WORK_DELAY); |
Ursula Braun | 952310c | 2017-01-09 16:55:24 +0100 | [diff] [blame] | 652 | return; |
| 653 | } |
Ursula Braun | 952310c | 2017-01-09 16:55:24 +0100 | [diff] [blame] | 654 | } |
| 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 Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 660 | /***************************** send initialize *******************************/ |
| 661 | |
| 662 | /* Initialize send properties on connection establishment. NB: not __init! */ |
| 663 | void smc_tx_init(struct smc_sock *smc) |
| 664 | { |
| 665 | smc->sk.sk_write_space = smc_tx_write_space; |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 666 | } |