Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
e6727f39 UB |
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> | |
c3edc401 IM |
19 | #include <linux/sched/signal.h> |
20 | ||
e6727f39 | 21 | #include <net/sock.h> |
01d2f7e2 | 22 | #include <net/tcp.h> |
e6727f39 UB |
23 | |
24 | #include "smc.h" | |
25 | #include "smc_wr.h" | |
26 | #include "smc_cdc.h" | |
27 | #include "smc_tx.h" | |
28 | ||
18e537cd | 29 | #define SMC_TX_WORK_DELAY HZ |
01d2f7e2 | 30 | #define SMC_TX_CORK_DELAY (HZ >> 2) /* 250 ms */ |
18e537cd | 31 | |
e6727f39 UB |
32 | /***************************** sndbuf producer *******************************/ |
33 | ||
34 | /* callback implementation for sk.sk_write_space() | |
de8474eb | 35 | * to wakeup sndbuf producers that blocked with smc_tx_wait(). |
e6727f39 UB |
36 | * called under sk_socket lock. |
37 | */ | |
38 | static void smc_tx_write_space(struct sock *sk) | |
39 | { | |
40 | struct socket *sock = sk->sk_socket; | |
41 | struct smc_sock *smc = smc_sk(sk); | |
42 | struct socket_wq *wq; | |
43 | ||
44 | /* similar to sk_stream_write_space */ | |
45 | if (atomic_read(&smc->conn.sndbuf_space) && sock) { | |
46 | clear_bit(SOCK_NOSPACE, &sock->flags); | |
47 | rcu_read_lock(); | |
48 | wq = rcu_dereference(sk->sk_wq); | |
49 | if (skwq_has_sleeper(wq)) | |
50 | wake_up_interruptible_poll(&wq->wait, | |
a9a08845 LT |
51 | EPOLLOUT | EPOLLWRNORM | |
52 | EPOLLWRBAND); | |
e6727f39 UB |
53 | if (wq && wq->fasync_list && !(sk->sk_shutdown & SEND_SHUTDOWN)) |
54 | sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT); | |
55 | rcu_read_unlock(); | |
56 | } | |
57 | } | |
58 | ||
de8474eb | 59 | /* Wakeup sndbuf producers that blocked with smc_tx_wait(). |
e6727f39 UB |
60 | * Cf. tcp_data_snd_check()=>tcp_check_space()=>tcp_new_space(). |
61 | */ | |
62 | void smc_tx_sndbuf_nonfull(struct smc_sock *smc) | |
63 | { | |
64 | if (smc->sk.sk_socket && | |
65 | test_bit(SOCK_NOSPACE, &smc->sk.sk_socket->flags)) | |
66 | smc->sk.sk_write_space(&smc->sk); | |
67 | } | |
68 | ||
de8474eb SR |
69 | /* blocks sndbuf producer until at least one byte of free space available |
70 | * or urgent Byte was consumed | |
71 | */ | |
72 | static int smc_tx_wait(struct smc_sock *smc, int flags) | |
e6727f39 UB |
73 | { |
74 | DEFINE_WAIT_FUNC(wait, woken_wake_function); | |
75 | struct smc_connection *conn = &smc->conn; | |
76 | struct sock *sk = &smc->sk; | |
77 | bool noblock; | |
78 | long timeo; | |
79 | int rc = 0; | |
80 | ||
81 | /* similar to sk_stream_wait_memory */ | |
82 | timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT); | |
83 | noblock = timeo ? false : true; | |
84 | 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) || | |
89 | conn->local_tx_ctrl.conn_state_flags.peer_done_writing) { | |
90 | rc = -EPIPE; | |
91 | break; | |
92 | } | |
aa377e68 | 93 | if (smc_cdc_rxed_any_close(conn)) { |
e6727f39 UB |
94 | rc = -ECONNRESET; |
95 | break; | |
96 | } | |
97 | if (!timeo) { | |
98 | if (noblock) | |
99 | set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); | |
100 | rc = -EAGAIN; | |
101 | break; | |
102 | } | |
103 | if (signal_pending(current)) { | |
104 | rc = sock_intr_errno(timeo); | |
105 | break; | |
106 | } | |
107 | sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); | |
de8474eb SR |
108 | if (atomic_read(&conn->sndbuf_space) && !conn->urg_tx_pend) |
109 | break; /* at least 1 byte of free & no urgent data */ | |
e6727f39 | 110 | set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
e6727f39 UB |
111 | sk_wait_event(sk, &timeo, |
112 | sk->sk_err || | |
113 | (sk->sk_shutdown & SEND_SHUTDOWN) || | |
aa377e68 | 114 | smc_cdc_rxed_any_close(conn) || |
de8474eb SR |
115 | (atomic_read(&conn->sndbuf_space) && |
116 | !conn->urg_tx_pend), | |
e6727f39 | 117 | &wait); |
e6727f39 UB |
118 | } |
119 | remove_wait_queue(sk_sleep(sk), &wait); | |
120 | return rc; | |
121 | } | |
122 | ||
01d2f7e2 UB |
123 | static bool smc_tx_is_corked(struct smc_sock *smc) |
124 | { | |
125 | struct tcp_sock *tp = tcp_sk(smc->clcsock->sk); | |
126 | ||
127 | return (tp->nonagle & TCP_NAGLE_CORK) ? true : false; | |
128 | } | |
129 | ||
e6727f39 UB |
130 | /* sndbuf producer: main API called by socket layer. |
131 | * called under sock lock. | |
132 | */ | |
133 | int smc_tx_sendmsg(struct smc_sock *smc, struct msghdr *msg, size_t len) | |
134 | { | |
135 | size_t copylen, send_done = 0, send_remaining = len; | |
136 | size_t chunk_len, chunk_off, chunk_len_sum; | |
137 | struct smc_connection *conn = &smc->conn; | |
138 | union smc_host_cursor prep; | |
139 | struct sock *sk = &smc->sk; | |
140 | char *sndbuf_base; | |
141 | int tx_cnt_prep; | |
142 | int writespace; | |
143 | int rc, chunk; | |
144 | ||
145 | /* This should be in poll */ | |
146 | sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); | |
147 | ||
148 | if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN)) { | |
149 | rc = -EPIPE; | |
150 | goto out_err; | |
151 | } | |
152 | ||
153 | while (msg_data_left(msg)) { | |
154 | if (sk->sk_state == SMC_INIT) | |
155 | return -ENOTCONN; | |
156 | if (smc->sk.sk_shutdown & SEND_SHUTDOWN || | |
b38d7324 | 157 | (smc->sk.sk_err == ECONNABORTED) || |
e6727f39 UB |
158 | conn->local_tx_ctrl.conn_state_flags.peer_conn_abort) |
159 | return -EPIPE; | |
160 | if (smc_cdc_rxed_any_close(conn)) | |
161 | return send_done ?: -ECONNRESET; | |
162 | ||
de8474eb SR |
163 | if (msg->msg_flags & MSG_OOB) |
164 | conn->local_tx_ctrl.prod_flags.urg_data_pending = 1; | |
165 | ||
166 | if (!atomic_read(&conn->sndbuf_space) || conn->urg_tx_pend) { | |
167 | rc = smc_tx_wait(smc, msg->msg_flags); | |
e6727f39 UB |
168 | if (rc) { |
169 | if (send_done) | |
170 | return send_done; | |
171 | goto out_err; | |
172 | } | |
173 | continue; | |
174 | } | |
175 | ||
176 | /* initialize variables for 1st iteration of subsequent loop */ | |
de8474eb | 177 | /* could be just 1 byte, even after smc_tx_wait above */ |
e6727f39 UB |
178 | 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; | |
183 | smc_curs_write(&prep, | |
184 | smc_curs_read(&conn->tx_curs_prep, conn), | |
185 | conn); | |
186 | tx_cnt_prep = prep.count; | |
187 | /* determine chunks where to write into sndbuf */ | |
188 | /* either unwrapped case, or 1st chunk of wrapped case */ | |
69cb7dc0 HW |
189 | chunk_len = min_t(size_t, copylen, conn->sndbuf_desc->len - |
190 | tx_cnt_prep); | |
e6727f39 UB |
191 | chunk_len_sum = chunk_len; |
192 | chunk_off = tx_cnt_prep; | |
10428dd8 | 193 | smc_sndbuf_sync_sg_for_cpu(conn); |
e6727f39 UB |
194 | for (chunk = 0; chunk < 2; chunk++) { |
195 | rc = memcpy_from_msg(sndbuf_base + chunk_off, | |
196 | msg, chunk_len); | |
197 | if (rc) { | |
10428dd8 | 198 | smc_sndbuf_sync_sg_for_device(conn); |
e6727f39 UB |
199 | if (send_done) |
200 | return send_done; | |
201 | goto out_err; | |
202 | } | |
203 | send_done += chunk_len; | |
204 | send_remaining -= chunk_len; | |
205 | ||
206 | if (chunk_len_sum == copylen) | |
207 | break; /* either on 1st or 2nd iteration */ | |
208 | /* prepare next (== 2nd) iteration */ | |
209 | chunk_len = copylen - chunk_len; /* remainder */ | |
210 | chunk_len_sum += chunk_len; | |
211 | chunk_off = 0; /* modulo offset in send ring buffer */ | |
212 | } | |
10428dd8 | 213 | smc_sndbuf_sync_sg_for_device(conn); |
e6727f39 | 214 | /* update cursors */ |
69cb7dc0 | 215 | smc_curs_add(conn->sndbuf_desc->len, &prep, copylen); |
e6727f39 UB |
216 | smc_curs_write(&conn->tx_curs_prep, |
217 | smc_curs_read(&prep, conn), | |
218 | conn); | |
219 | /* increased in send tasklet smc_cdc_tx_handler() */ | |
220 | smp_mb__before_atomic(); | |
221 | atomic_sub(copylen, &conn->sndbuf_space); | |
69cb7dc0 | 222 | /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */ |
e6727f39 UB |
223 | smp_mb__after_atomic(); |
224 | /* since we just produced more new data into sndbuf, | |
225 | * trigger sndbuf consumer: RDMA write into peer RMBE and CDC | |
226 | */ | |
de8474eb SR |
227 | if ((msg->msg_flags & MSG_OOB) && !send_remaining) |
228 | conn->urg_tx_pend = true; | |
01d2f7e2 UB |
229 | if ((msg->msg_flags & MSG_MORE || smc_tx_is_corked(smc)) && |
230 | (atomic_read(&conn->sndbuf_space) > | |
69cb7dc0 | 231 | (conn->sndbuf_desc->len >> 1))) |
01d2f7e2 UB |
232 | /* for a corked socket defer the RDMA writes if there |
233 | * is still sufficient sndbuf_space available | |
234 | */ | |
235 | schedule_delayed_work(&conn->tx_work, | |
236 | SMC_TX_CORK_DELAY); | |
237 | else | |
238 | smc_tx_sndbuf_nonempty(conn); | |
e6727f39 UB |
239 | } /* while (msg_data_left(msg)) */ |
240 | ||
241 | return send_done; | |
242 | ||
243 | out_err: | |
244 | rc = sk_stream_error(sk, msg->msg_flags, rc); | |
245 | /* make sure we wake any epoll edge trigger waiter */ | |
246 | if (unlikely(rc == -EAGAIN)) | |
247 | sk->sk_write_space(sk); | |
248 | return rc; | |
249 | } | |
250 | ||
251 | /***************************** sndbuf consumer *******************************/ | |
252 | ||
253 | /* sndbuf consumer: actual data transfer of one target chunk with RDMA write */ | |
254 | static int smc_tx_rdma_write(struct smc_connection *conn, int peer_rmbe_offset, | |
255 | int num_sges, struct ib_sge sges[]) | |
256 | { | |
257 | struct smc_link_group *lgr = conn->lgr; | |
258 | struct ib_send_wr *failed_wr = NULL; | |
259 | struct ib_rdma_wr rdma_wr; | |
260 | struct smc_link *link; | |
261 | int rc; | |
262 | ||
263 | memset(&rdma_wr, 0, sizeof(rdma_wr)); | |
264 | link = &lgr->lnk[SMC_SINGLE_LINK]; | |
265 | rdma_wr.wr.wr_id = smc_wr_tx_get_next_wr_id(link); | |
266 | rdma_wr.wr.sg_list = sges; | |
267 | rdma_wr.wr.num_sge = num_sges; | |
268 | rdma_wr.wr.opcode = IB_WR_RDMA_WRITE; | |
269 | rdma_wr.remote_addr = | |
270 | lgr->rtokens[conn->rtoken_idx][SMC_SINGLE_LINK].dma_addr + | |
271 | /* RMBE within RMB */ | |
95d8d263 | 272 | conn->tx_off + |
e6727f39 UB |
273 | /* offset within RMBE */ |
274 | peer_rmbe_offset; | |
275 | rdma_wr.rkey = lgr->rtokens[conn->rtoken_idx][SMC_SINGLE_LINK].rkey; | |
276 | rc = ib_post_send(link->roce_qp, &rdma_wr.wr, &failed_wr); | |
b4772b3a | 277 | if (rc) { |
e6727f39 | 278 | conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1; |
b4772b3a UB |
279 | smc_lgr_terminate(lgr); |
280 | } | |
e6727f39 UB |
281 | return rc; |
282 | } | |
283 | ||
284 | /* sndbuf consumer */ | |
285 | static inline void smc_tx_advance_cursors(struct smc_connection *conn, | |
286 | union smc_host_cursor *prod, | |
287 | union smc_host_cursor *sent, | |
288 | size_t len) | |
289 | { | |
290 | smc_curs_add(conn->peer_rmbe_size, prod, len); | |
291 | /* increased in recv tasklet smc_cdc_msg_rcv() */ | |
292 | smp_mb__before_atomic(); | |
293 | /* data in flight reduces usable snd_wnd */ | |
294 | atomic_sub(len, &conn->peer_rmbe_space); | |
295 | /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */ | |
296 | smp_mb__after_atomic(); | |
69cb7dc0 | 297 | smc_curs_add(conn->sndbuf_desc->len, sent, len); |
e6727f39 UB |
298 | } |
299 | ||
300 | /* sndbuf consumer: prepare all necessary (src&dst) chunks of data transmit; | |
301 | * usable snd_wnd as max transmit | |
302 | */ | |
303 | static int smc_tx_rdma_writes(struct smc_connection *conn) | |
304 | { | |
305 | size_t src_off, src_len, dst_off, dst_len; /* current chunk values */ | |
306 | size_t len, dst_len_sum, src_len_sum, dstchunk, srcchunk; | |
307 | union smc_host_cursor sent, prep, prod, cons; | |
308 | struct ib_sge sges[SMC_IB_MAX_SEND_SGE]; | |
309 | struct smc_link_group *lgr = conn->lgr; | |
de8474eb | 310 | struct smc_cdc_producer_flags *pflags; |
e6727f39 UB |
311 | int to_send, rmbespace; |
312 | struct smc_link *link; | |
9d8fb617 | 313 | dma_addr_t dma_addr; |
e6727f39 UB |
314 | int num_sges; |
315 | int rc; | |
316 | ||
317 | /* source: sndbuf */ | |
318 | smc_curs_write(&sent, smc_curs_read(&conn->tx_curs_sent, conn), conn); | |
319 | smc_curs_write(&prep, smc_curs_read(&conn->tx_curs_prep, conn), conn); | |
320 | /* cf. wmem_alloc - (snd_max - snd_una) */ | |
69cb7dc0 | 321 | to_send = smc_curs_diff(conn->sndbuf_desc->len, &sent, &prep); |
e6727f39 UB |
322 | if (to_send <= 0) |
323 | return 0; | |
324 | ||
325 | /* destination: RMBE */ | |
326 | /* cf. snd_wnd */ | |
327 | rmbespace = atomic_read(&conn->peer_rmbe_space); | |
328 | if (rmbespace <= 0) | |
329 | return 0; | |
330 | smc_curs_write(&prod, | |
331 | smc_curs_read(&conn->local_tx_ctrl.prod, conn), | |
332 | conn); | |
333 | smc_curs_write(&cons, | |
334 | smc_curs_read(&conn->local_rx_ctrl.cons, conn), | |
335 | conn); | |
336 | ||
337 | /* if usable snd_wnd closes ask peer to advertise once it opens again */ | |
de8474eb SR |
338 | pflags = &conn->local_tx_ctrl.prod_flags; |
339 | pflags->write_blocked = (to_send >= rmbespace); | |
e6727f39 UB |
340 | /* cf. usable snd_wnd */ |
341 | len = min(to_send, rmbespace); | |
342 | ||
343 | /* initialize variables for first iteration of subsequent nested loop */ | |
344 | link = &lgr->lnk[SMC_SINGLE_LINK]; | |
345 | dst_off = prod.count; | |
346 | if (prod.wrap == cons.wrap) { | |
347 | /* the filled destination area is unwrapped, | |
348 | * hence the available free destination space is wrapped | |
349 | * and we need 2 destination chunks of sum len; start with 1st | |
350 | * which is limited by what's available in sndbuf | |
351 | */ | |
352 | dst_len = min_t(size_t, | |
353 | conn->peer_rmbe_size - prod.count, len); | |
354 | } else { | |
355 | /* the filled destination area is wrapped, | |
356 | * hence the available free destination space is unwrapped | |
357 | * and we need a single destination chunk of entire len | |
358 | */ | |
359 | dst_len = len; | |
360 | } | |
361 | dst_len_sum = dst_len; | |
362 | src_off = sent.count; | |
363 | /* dst_len determines the maximum src_len */ | |
69cb7dc0 | 364 | if (sent.count + dst_len <= conn->sndbuf_desc->len) { |
e6727f39 UB |
365 | /* unwrapped src case: single chunk of entire dst_len */ |
366 | src_len = dst_len; | |
367 | } else { | |
368 | /* wrapped src case: 2 chunks of sum dst_len; start with 1st: */ | |
69cb7dc0 | 369 | src_len = conn->sndbuf_desc->len - sent.count; |
e6727f39 UB |
370 | } |
371 | src_len_sum = src_len; | |
9d8fb617 | 372 | dma_addr = sg_dma_address(conn->sndbuf_desc->sgt[SMC_SINGLE_LINK].sgl); |
e6727f39 UB |
373 | for (dstchunk = 0; dstchunk < 2; dstchunk++) { |
374 | num_sges = 0; | |
375 | for (srcchunk = 0; srcchunk < 2; srcchunk++) { | |
9d8fb617 | 376 | sges[srcchunk].addr = dma_addr + src_off; |
e6727f39 UB |
377 | sges[srcchunk].length = src_len; |
378 | sges[srcchunk].lkey = link->roce_pd->local_dma_lkey; | |
379 | num_sges++; | |
380 | src_off += src_len; | |
69cb7dc0 HW |
381 | if (src_off >= conn->sndbuf_desc->len) |
382 | src_off -= conn->sndbuf_desc->len; | |
e6727f39 UB |
383 | /* modulo in send ring */ |
384 | if (src_len_sum == dst_len) | |
385 | break; /* either on 1st or 2nd iteration */ | |
386 | /* prepare next (== 2nd) iteration */ | |
387 | src_len = dst_len - src_len; /* remainder */ | |
388 | src_len_sum += src_len; | |
389 | } | |
390 | rc = smc_tx_rdma_write(conn, dst_off, num_sges, sges); | |
391 | if (rc) | |
392 | return rc; | |
393 | if (dst_len_sum == len) | |
394 | break; /* either on 1st or 2nd iteration */ | |
395 | /* prepare next (== 2nd) iteration */ | |
396 | dst_off = 0; /* modulo offset in RMBE ring buffer */ | |
397 | dst_len = len - dst_len; /* remainder */ | |
398 | dst_len_sum += dst_len; | |
399 | src_len = min_t(int, | |
69cb7dc0 | 400 | dst_len, conn->sndbuf_desc->len - sent.count); |
e6727f39 UB |
401 | src_len_sum = src_len; |
402 | } | |
403 | ||
de8474eb SR |
404 | if (conn->urg_tx_pend && len == to_send) |
405 | pflags->urg_data_present = 1; | |
e6727f39 UB |
406 | smc_tx_advance_cursors(conn, &prod, &sent, len); |
407 | /* update connection's cursors with advanced local cursors */ | |
408 | smc_curs_write(&conn->local_tx_ctrl.prod, | |
409 | smc_curs_read(&prod, conn), | |
410 | conn); | |
411 | /* dst: peer RMBE */ | |
412 | smc_curs_write(&conn->tx_curs_sent, | |
413 | smc_curs_read(&sent, conn), | |
414 | conn); | |
415 | /* src: local sndbuf */ | |
416 | ||
417 | return 0; | |
418 | } | |
419 | ||
420 | /* Wakeup sndbuf consumers from any context (IRQ or process) | |
421 | * since there is more data to transmit; usable snd_wnd as max transmit | |
422 | */ | |
423 | int smc_tx_sndbuf_nonempty(struct smc_connection *conn) | |
424 | { | |
de8474eb | 425 | struct smc_cdc_producer_flags *pflags; |
e6727f39 UB |
426 | struct smc_cdc_tx_pend *pend; |
427 | struct smc_wr_buf *wr_buf; | |
428 | int rc; | |
429 | ||
430 | spin_lock_bh(&conn->send_lock); | |
51957bc5 | 431 | rc = smc_cdc_get_free_slot(conn, &wr_buf, &pend); |
e6727f39 UB |
432 | if (rc < 0) { |
433 | if (rc == -EBUSY) { | |
b38d7324 UB |
434 | struct smc_sock *smc = |
435 | container_of(conn, struct smc_sock, conn); | |
436 | ||
437 | if (smc->sk.sk_err == ECONNABORTED) { | |
438 | rc = sock_error(&smc->sk); | |
439 | goto out_unlock; | |
440 | } | |
e6727f39 | 441 | rc = 0; |
1a0a04c7 | 442 | if (conn->alert_token_local) /* connection healthy */ |
01d2f7e2 UB |
443 | mod_delayed_work(system_wq, &conn->tx_work, |
444 | SMC_TX_WORK_DELAY); | |
e6727f39 UB |
445 | } |
446 | goto out_unlock; | |
447 | } | |
448 | ||
de8474eb SR |
449 | if (!conn->local_tx_ctrl.prod_flags.urg_data_present) { |
450 | rc = smc_tx_rdma_writes(conn); | |
451 | if (rc) { | |
452 | smc_wr_tx_put_slot(&conn->lgr->lnk[SMC_SINGLE_LINK], | |
453 | (struct smc_wr_tx_pend_priv *)pend); | |
454 | goto out_unlock; | |
455 | } | |
e6727f39 UB |
456 | } |
457 | ||
458 | rc = smc_cdc_msg_send(conn, wr_buf, pend); | |
de8474eb SR |
459 | pflags = &conn->local_tx_ctrl.prod_flags; |
460 | if (!rc && pflags->urg_data_present) { | |
461 | pflags->urg_data_pending = 0; | |
462 | pflags->urg_data_present = 0; | |
463 | } | |
e6727f39 UB |
464 | |
465 | out_unlock: | |
466 | spin_unlock_bh(&conn->send_lock); | |
467 | return rc; | |
468 | } | |
469 | ||
470 | /* Wakeup sndbuf consumers from process context | |
471 | * since there is more data to transmit | |
472 | */ | |
be7f3e59 | 473 | void smc_tx_work(struct work_struct *work) |
e6727f39 | 474 | { |
18e537cd | 475 | struct smc_connection *conn = container_of(to_delayed_work(work), |
e6727f39 UB |
476 | struct smc_connection, |
477 | tx_work); | |
478 | struct smc_sock *smc = container_of(conn, struct smc_sock, conn); | |
90cacb2e | 479 | int rc; |
e6727f39 UB |
480 | |
481 | lock_sock(&smc->sk); | |
1a0a04c7 UB |
482 | if (smc->sk.sk_err || |
483 | !conn->alert_token_local || | |
484 | conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) | |
485 | goto out; | |
486 | ||
90cacb2e UB |
487 | rc = smc_tx_sndbuf_nonempty(conn); |
488 | if (!rc && conn->local_rx_ctrl.prod_flags.write_blocked && | |
489 | !atomic_read(&conn->bytes_to_rcv)) | |
490 | conn->local_rx_ctrl.prod_flags.write_blocked = 0; | |
1a0a04c7 UB |
491 | |
492 | out: | |
e6727f39 UB |
493 | release_sock(&smc->sk); |
494 | } | |
495 | ||
de8474eb | 496 | void smc_tx_consumer_update(struct smc_connection *conn, bool force) |
952310cc | 497 | { |
99be51f1 UB |
498 | union smc_host_cursor cfed, cons, prod; |
499 | int sender_free = conn->rmb_desc->len; | |
6b5771aa | 500 | int to_confirm; |
952310cc UB |
501 | |
502 | smc_curs_write(&cons, | |
503 | smc_curs_read(&conn->local_tx_ctrl.cons, conn), | |
504 | conn); | |
505 | smc_curs_write(&cfed, | |
506 | smc_curs_read(&conn->rx_curs_confirmed, conn), | |
507 | conn); | |
69cb7dc0 | 508 | to_confirm = smc_curs_diff(conn->rmb_desc->len, &cfed, &cons); |
99be51f1 UB |
509 | if (to_confirm > conn->rmbe_update_limit) { |
510 | smc_curs_write(&prod, | |
511 | smc_curs_read(&conn->local_rx_ctrl.prod, conn), | |
512 | conn); | |
513 | sender_free = conn->rmb_desc->len - | |
514 | smc_curs_diff(conn->rmb_desc->len, &prod, &cfed); | |
515 | } | |
952310cc UB |
516 | |
517 | if (conn->local_rx_ctrl.prod_flags.cons_curs_upd_req || | |
de8474eb | 518 | force || |
952310cc | 519 | ((to_confirm > conn->rmbe_update_limit) && |
99be51f1 | 520 | ((sender_free <= (conn->rmb_desc->len / 2)) || |
952310cc | 521 | conn->local_rx_ctrl.prod_flags.write_blocked))) { |
1a0a04c7 UB |
522 | if ((smc_cdc_get_slot_and_msg_send(conn) < 0) && |
523 | conn->alert_token_local) { /* connection healthy */ | |
18e537cd UB |
524 | schedule_delayed_work(&conn->tx_work, |
525 | SMC_TX_WORK_DELAY); | |
952310cc UB |
526 | return; |
527 | } | |
528 | smc_curs_write(&conn->rx_curs_confirmed, | |
529 | smc_curs_read(&conn->local_tx_ctrl.cons, conn), | |
530 | conn); | |
531 | conn->local_rx_ctrl.prod_flags.cons_curs_upd_req = 0; | |
532 | } | |
533 | if (conn->local_rx_ctrl.prod_flags.write_blocked && | |
534 | !atomic_read(&conn->bytes_to_rcv)) | |
535 | conn->local_rx_ctrl.prod_flags.write_blocked = 0; | |
536 | } | |
537 | ||
e6727f39 UB |
538 | /***************************** send initialize *******************************/ |
539 | ||
540 | /* Initialize send properties on connection establishment. NB: not __init! */ | |
541 | void smc_tx_init(struct smc_sock *smc) | |
542 | { | |
543 | smc->sk.sk_write_space = smc_tx_write_space; | |
e6727f39 | 544 | } |