xref: /linux/net/smc/smc_tx.c (revision f8a11425075ff11b4b5784f077cb84f3d2dfb3f0)
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  */
41 static 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  */
67 void 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  */
77 static 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 
127 static 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  */
137 int 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 (len > conn->sndbuf_desc->len)
158 		SMC_STAT_RMB_TX_SIZE_SMALL(smc, !conn->lnk);
159 
160 	if (len > conn->peer_rmbe_size)
161 		SMC_STAT_RMB_TX_PEER_SIZE_SMALL(smc, !conn->lnk);
162 
163 	if (msg->msg_flags & MSG_OOB)
164 		SMC_STAT_INC(smc, urg_data_cnt);
165 
166 	while (msg_data_left(msg)) {
167 		if (sk->sk_state == SMC_INIT)
168 			return -ENOTCONN;
169 		if (smc->sk.sk_shutdown & SEND_SHUTDOWN ||
170 		    (smc->sk.sk_err == ECONNABORTED) ||
171 		    conn->killed)
172 			return -EPIPE;
173 		if (smc_cdc_rxed_any_close(conn))
174 			return send_done ?: -ECONNRESET;
175 
176 		if (msg->msg_flags & MSG_OOB)
177 			conn->local_tx_ctrl.prod_flags.urg_data_pending = 1;
178 
179 		if (!atomic_read(&conn->sndbuf_space) || conn->urg_tx_pend) {
180 			if (send_done)
181 				return send_done;
182 			rc = smc_tx_wait(smc, msg->msg_flags);
183 			if (rc)
184 				goto out_err;
185 			continue;
186 		}
187 
188 		/* initialize variables for 1st iteration of subsequent loop */
189 		/* could be just 1 byte, even after smc_tx_wait above */
190 		writespace = atomic_read(&conn->sndbuf_space);
191 		/* not more than what user space asked for */
192 		copylen = min_t(size_t, send_remaining, writespace);
193 		/* determine start of sndbuf */
194 		sndbuf_base = conn->sndbuf_desc->cpu_addr;
195 		smc_curs_copy(&prep, &conn->tx_curs_prep, conn);
196 		tx_cnt_prep = prep.count;
197 		/* determine chunks where to write into sndbuf */
198 		/* either unwrapped case, or 1st chunk of wrapped case */
199 		chunk_len = min_t(size_t, copylen, conn->sndbuf_desc->len -
200 				  tx_cnt_prep);
201 		chunk_len_sum = chunk_len;
202 		chunk_off = tx_cnt_prep;
203 		smc_sndbuf_sync_sg_for_cpu(conn);
204 		for (chunk = 0; chunk < 2; chunk++) {
205 			rc = memcpy_from_msg(sndbuf_base + chunk_off,
206 					     msg, chunk_len);
207 			if (rc) {
208 				smc_sndbuf_sync_sg_for_device(conn);
209 				if (send_done)
210 					return send_done;
211 				goto out_err;
212 			}
213 			send_done += chunk_len;
214 			send_remaining -= chunk_len;
215 
216 			if (chunk_len_sum == copylen)
217 				break; /* either on 1st or 2nd iteration */
218 			/* prepare next (== 2nd) iteration */
219 			chunk_len = copylen - chunk_len; /* remainder */
220 			chunk_len_sum += chunk_len;
221 			chunk_off = 0; /* modulo offset in send ring buffer */
222 		}
223 		smc_sndbuf_sync_sg_for_device(conn);
224 		/* update cursors */
225 		smc_curs_add(conn->sndbuf_desc->len, &prep, copylen);
226 		smc_curs_copy(&conn->tx_curs_prep, &prep, conn);
227 		/* increased in send tasklet smc_cdc_tx_handler() */
228 		smp_mb__before_atomic();
229 		atomic_sub(copylen, &conn->sndbuf_space);
230 		/* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
231 		smp_mb__after_atomic();
232 		/* since we just produced more new data into sndbuf,
233 		 * trigger sndbuf consumer: RDMA write into peer RMBE and CDC
234 		 */
235 		if ((msg->msg_flags & MSG_OOB) && !send_remaining)
236 			conn->urg_tx_pend = true;
237 		if ((msg->msg_flags & MSG_MORE || smc_tx_is_corked(smc)) &&
238 		    (atomic_read(&conn->sndbuf_space) >
239 						(conn->sndbuf_desc->len >> 1)))
240 			/* for a corked socket defer the RDMA writes if there
241 			 * is still sufficient sndbuf_space available
242 			 */
243 			queue_delayed_work(conn->lgr->tx_wq, &conn->tx_work,
244 					   SMC_TX_CORK_DELAY);
245 		else
246 			smc_tx_sndbuf_nonempty(conn);
247 	} /* while (msg_data_left(msg)) */
248 
249 	return send_done;
250 
251 out_err:
252 	rc = sk_stream_error(sk, msg->msg_flags, rc);
253 	/* make sure we wake any epoll edge trigger waiter */
254 	if (unlikely(rc == -EAGAIN))
255 		sk->sk_write_space(sk);
256 	return rc;
257 }
258 
259 /***************************** sndbuf consumer *******************************/
260 
261 /* sndbuf consumer: actual data transfer of one target chunk with ISM write */
262 int smcd_tx_ism_write(struct smc_connection *conn, void *data, size_t len,
263 		      u32 offset, int signal)
264 {
265 	struct smc_ism_position pos;
266 	int rc;
267 
268 	memset(&pos, 0, sizeof(pos));
269 	pos.token = conn->peer_token;
270 	pos.index = conn->peer_rmbe_idx;
271 	pos.offset = conn->tx_off + offset;
272 	pos.signal = signal;
273 	rc = smc_ism_write(conn->lgr->smcd, &pos, data, len);
274 	if (rc)
275 		conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
276 	return rc;
277 }
278 
279 /* sndbuf consumer: actual data transfer of one target chunk with RDMA write */
280 static int smc_tx_rdma_write(struct smc_connection *conn, int peer_rmbe_offset,
281 			     int num_sges, struct ib_rdma_wr *rdma_wr)
282 {
283 	struct smc_link_group *lgr = conn->lgr;
284 	struct smc_link *link = conn->lnk;
285 	int rc;
286 
287 	rdma_wr->wr.wr_id = smc_wr_tx_get_next_wr_id(link);
288 	rdma_wr->wr.num_sge = num_sges;
289 	rdma_wr->remote_addr =
290 		lgr->rtokens[conn->rtoken_idx][link->link_idx].dma_addr +
291 		/* RMBE within RMB */
292 		conn->tx_off +
293 		/* offset within RMBE */
294 		peer_rmbe_offset;
295 	rdma_wr->rkey = lgr->rtokens[conn->rtoken_idx][link->link_idx].rkey;
296 	rc = ib_post_send(link->roce_qp, &rdma_wr->wr, NULL);
297 	if (rc)
298 		smcr_link_down_cond_sched(link);
299 	return rc;
300 }
301 
302 /* sndbuf consumer */
303 static inline void smc_tx_advance_cursors(struct smc_connection *conn,
304 					  union smc_host_cursor *prod,
305 					  union smc_host_cursor *sent,
306 					  size_t len)
307 {
308 	smc_curs_add(conn->peer_rmbe_size, prod, len);
309 	/* increased in recv tasklet smc_cdc_msg_rcv() */
310 	smp_mb__before_atomic();
311 	/* data in flight reduces usable snd_wnd */
312 	atomic_sub(len, &conn->peer_rmbe_space);
313 	/* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
314 	smp_mb__after_atomic();
315 	smc_curs_add(conn->sndbuf_desc->len, sent, len);
316 }
317 
318 /* SMC-R helper for smc_tx_rdma_writes() */
319 static int smcr_tx_rdma_writes(struct smc_connection *conn, size_t len,
320 			       size_t src_off, size_t src_len,
321 			       size_t dst_off, size_t dst_len,
322 			       struct smc_rdma_wr *wr_rdma_buf)
323 {
324 	struct smc_link *link = conn->lnk;
325 
326 	dma_addr_t dma_addr =
327 		sg_dma_address(conn->sndbuf_desc->sgt[link->link_idx].sgl);
328 	int src_len_sum = src_len, dst_len_sum = dst_len;
329 	int sent_count = src_off;
330 	int srcchunk, dstchunk;
331 	int num_sges;
332 	int rc;
333 
334 	for (dstchunk = 0; dstchunk < 2; dstchunk++) {
335 		struct ib_sge *sge =
336 			wr_rdma_buf->wr_tx_rdma[dstchunk].wr.sg_list;
337 
338 		num_sges = 0;
339 		for (srcchunk = 0; srcchunk < 2; srcchunk++) {
340 			sge[srcchunk].addr = dma_addr + src_off;
341 			sge[srcchunk].length = src_len;
342 			num_sges++;
343 
344 			src_off += src_len;
345 			if (src_off >= conn->sndbuf_desc->len)
346 				src_off -= conn->sndbuf_desc->len;
347 						/* modulo in send ring */
348 			if (src_len_sum == dst_len)
349 				break; /* either on 1st or 2nd iteration */
350 			/* prepare next (== 2nd) iteration */
351 			src_len = dst_len - src_len; /* remainder */
352 			src_len_sum += src_len;
353 		}
354 		rc = smc_tx_rdma_write(conn, dst_off, num_sges,
355 				       &wr_rdma_buf->wr_tx_rdma[dstchunk]);
356 		if (rc)
357 			return rc;
358 		if (dst_len_sum == len)
359 			break; /* either on 1st or 2nd iteration */
360 		/* prepare next (== 2nd) iteration */
361 		dst_off = 0; /* modulo offset in RMBE ring buffer */
362 		dst_len = len - dst_len; /* remainder */
363 		dst_len_sum += dst_len;
364 		src_len = min_t(int, dst_len, conn->sndbuf_desc->len -
365 				sent_count);
366 		src_len_sum = src_len;
367 	}
368 	return 0;
369 }
370 
371 /* SMC-D helper for smc_tx_rdma_writes() */
372 static int smcd_tx_rdma_writes(struct smc_connection *conn, size_t len,
373 			       size_t src_off, size_t src_len,
374 			       size_t dst_off, size_t dst_len)
375 {
376 	int src_len_sum = src_len, dst_len_sum = dst_len;
377 	int srcchunk, dstchunk;
378 	int rc;
379 
380 	for (dstchunk = 0; dstchunk < 2; dstchunk++) {
381 		for (srcchunk = 0; srcchunk < 2; srcchunk++) {
382 			void *data = conn->sndbuf_desc->cpu_addr + src_off;
383 
384 			rc = smcd_tx_ism_write(conn, data, src_len, dst_off +
385 					       sizeof(struct smcd_cdc_msg), 0);
386 			if (rc)
387 				return rc;
388 			dst_off += src_len;
389 			src_off += src_len;
390 			if (src_off >= conn->sndbuf_desc->len)
391 				src_off -= conn->sndbuf_desc->len;
392 						/* modulo in send ring */
393 			if (src_len_sum == dst_len)
394 				break; /* either on 1st or 2nd iteration */
395 			/* prepare next (== 2nd) iteration */
396 			src_len = dst_len - src_len; /* remainder */
397 			src_len_sum += src_len;
398 		}
399 		if (dst_len_sum == len)
400 			break; /* either on 1st or 2nd iteration */
401 		/* prepare next (== 2nd) iteration */
402 		dst_off = 0; /* modulo offset in RMBE ring buffer */
403 		dst_len = len - dst_len; /* remainder */
404 		dst_len_sum += dst_len;
405 		src_len = min_t(int, dst_len, conn->sndbuf_desc->len - src_off);
406 		src_len_sum = src_len;
407 	}
408 	return 0;
409 }
410 
411 /* sndbuf consumer: prepare all necessary (src&dst) chunks of data transmit;
412  * usable snd_wnd as max transmit
413  */
414 static int smc_tx_rdma_writes(struct smc_connection *conn,
415 			      struct smc_rdma_wr *wr_rdma_buf)
416 {
417 	size_t len, src_len, dst_off, dst_len; /* current chunk values */
418 	union smc_host_cursor sent, prep, prod, cons;
419 	struct smc_cdc_producer_flags *pflags;
420 	int to_send, rmbespace;
421 	int rc;
422 
423 	/* source: sndbuf */
424 	smc_curs_copy(&sent, &conn->tx_curs_sent, conn);
425 	smc_curs_copy(&prep, &conn->tx_curs_prep, conn);
426 	/* cf. wmem_alloc - (snd_max - snd_una) */
427 	to_send = smc_curs_diff(conn->sndbuf_desc->len, &sent, &prep);
428 	if (to_send <= 0)
429 		return 0;
430 
431 	/* destination: RMBE */
432 	/* cf. snd_wnd */
433 	rmbespace = atomic_read(&conn->peer_rmbe_space);
434 	if (rmbespace <= 0) {
435 		struct smc_sock *smc = container_of(conn, struct smc_sock,
436 						    conn);
437 		SMC_STAT_RMB_TX_PEER_FULL(smc, !conn->lnk);
438 		return 0;
439 	}
440 	smc_curs_copy(&prod, &conn->local_tx_ctrl.prod, conn);
441 	smc_curs_copy(&cons, &conn->local_rx_ctrl.cons, conn);
442 
443 	/* if usable snd_wnd closes ask peer to advertise once it opens again */
444 	pflags = &conn->local_tx_ctrl.prod_flags;
445 	pflags->write_blocked = (to_send >= rmbespace);
446 	/* cf. usable snd_wnd */
447 	len = min(to_send, rmbespace);
448 
449 	/* initialize variables for first iteration of subsequent nested loop */
450 	dst_off = prod.count;
451 	if (prod.wrap == cons.wrap) {
452 		/* the filled destination area is unwrapped,
453 		 * hence the available free destination space is wrapped
454 		 * and we need 2 destination chunks of sum len; start with 1st
455 		 * which is limited by what's available in sndbuf
456 		 */
457 		dst_len = min_t(size_t,
458 				conn->peer_rmbe_size - prod.count, len);
459 	} else {
460 		/* the filled destination area is wrapped,
461 		 * hence the available free destination space is unwrapped
462 		 * and we need a single destination chunk of entire len
463 		 */
464 		dst_len = len;
465 	}
466 	/* dst_len determines the maximum src_len */
467 	if (sent.count + dst_len <= conn->sndbuf_desc->len) {
468 		/* unwrapped src case: single chunk of entire dst_len */
469 		src_len = dst_len;
470 	} else {
471 		/* wrapped src case: 2 chunks of sum dst_len; start with 1st: */
472 		src_len = conn->sndbuf_desc->len - sent.count;
473 	}
474 
475 	if (conn->lgr->is_smcd)
476 		rc = smcd_tx_rdma_writes(conn, len, sent.count, src_len,
477 					 dst_off, dst_len);
478 	else
479 		rc = smcr_tx_rdma_writes(conn, len, sent.count, src_len,
480 					 dst_off, dst_len, wr_rdma_buf);
481 	if (rc)
482 		return rc;
483 
484 	if (conn->urg_tx_pend && len == to_send)
485 		pflags->urg_data_present = 1;
486 	smc_tx_advance_cursors(conn, &prod, &sent, len);
487 	/* update connection's cursors with advanced local cursors */
488 	smc_curs_copy(&conn->local_tx_ctrl.prod, &prod, conn);
489 							/* dst: peer RMBE */
490 	smc_curs_copy(&conn->tx_curs_sent, &sent, conn);/* src: local sndbuf */
491 
492 	return 0;
493 }
494 
495 /* Wakeup sndbuf consumers from any context (IRQ or process)
496  * since there is more data to transmit; usable snd_wnd as max transmit
497  */
498 static int smcr_tx_sndbuf_nonempty(struct smc_connection *conn)
499 {
500 	struct smc_cdc_producer_flags *pflags = &conn->local_tx_ctrl.prod_flags;
501 	struct smc_link *link = conn->lnk;
502 	struct smc_rdma_wr *wr_rdma_buf;
503 	struct smc_cdc_tx_pend *pend;
504 	struct smc_wr_buf *wr_buf;
505 	int rc;
506 
507 	rc = smc_cdc_get_free_slot(conn, link, &wr_buf, &wr_rdma_buf, &pend);
508 	if (rc < 0) {
509 		if (rc == -EBUSY) {
510 			struct smc_sock *smc =
511 				container_of(conn, struct smc_sock, conn);
512 
513 			if (smc->sk.sk_err == ECONNABORTED)
514 				return sock_error(&smc->sk);
515 			if (conn->killed)
516 				return -EPIPE;
517 			rc = 0;
518 			mod_delayed_work(conn->lgr->tx_wq, &conn->tx_work,
519 					 SMC_TX_WORK_DELAY);
520 		}
521 		return rc;
522 	}
523 
524 	spin_lock_bh(&conn->send_lock);
525 	if (link != conn->lnk) {
526 		/* link of connection changed, tx_work will restart */
527 		smc_wr_tx_put_slot(link,
528 				   (struct smc_wr_tx_pend_priv *)pend);
529 		rc = -ENOLINK;
530 		goto out_unlock;
531 	}
532 	if (!pflags->urg_data_present) {
533 		rc = smc_tx_rdma_writes(conn, wr_rdma_buf);
534 		if (rc) {
535 			smc_wr_tx_put_slot(link,
536 					   (struct smc_wr_tx_pend_priv *)pend);
537 			goto out_unlock;
538 		}
539 	}
540 
541 	rc = smc_cdc_msg_send(conn, wr_buf, pend);
542 	if (!rc && pflags->urg_data_present) {
543 		pflags->urg_data_pending = 0;
544 		pflags->urg_data_present = 0;
545 	}
546 
547 out_unlock:
548 	spin_unlock_bh(&conn->send_lock);
549 	return rc;
550 }
551 
552 static int smcd_tx_sndbuf_nonempty(struct smc_connection *conn)
553 {
554 	struct smc_cdc_producer_flags *pflags = &conn->local_tx_ctrl.prod_flags;
555 	int rc = 0;
556 
557 	spin_lock_bh(&conn->send_lock);
558 	if (!pflags->urg_data_present)
559 		rc = smc_tx_rdma_writes(conn, NULL);
560 	if (!rc)
561 		rc = smcd_cdc_msg_send(conn);
562 
563 	if (!rc && pflags->urg_data_present) {
564 		pflags->urg_data_pending = 0;
565 		pflags->urg_data_present = 0;
566 	}
567 	spin_unlock_bh(&conn->send_lock);
568 	return rc;
569 }
570 
571 int smc_tx_sndbuf_nonempty(struct smc_connection *conn)
572 {
573 	int rc;
574 
575 	if (conn->killed ||
576 	    conn->local_rx_ctrl.conn_state_flags.peer_conn_abort)
577 		return -EPIPE;	/* connection being aborted */
578 	if (conn->lgr->is_smcd)
579 		rc = smcd_tx_sndbuf_nonempty(conn);
580 	else
581 		rc = smcr_tx_sndbuf_nonempty(conn);
582 
583 	if (!rc) {
584 		/* trigger socket release if connection is closing */
585 		struct smc_sock *smc = container_of(conn, struct smc_sock,
586 						    conn);
587 		smc_close_wake_tx_prepared(smc);
588 	}
589 	return rc;
590 }
591 
592 /* Wakeup sndbuf consumers from process context
593  * since there is more data to transmit
594  */
595 void smc_tx_work(struct work_struct *work)
596 {
597 	struct smc_connection *conn = container_of(to_delayed_work(work),
598 						   struct smc_connection,
599 						   tx_work);
600 	struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
601 	int rc;
602 
603 	lock_sock(&smc->sk);
604 	if (smc->sk.sk_err)
605 		goto out;
606 
607 	rc = smc_tx_sndbuf_nonempty(conn);
608 	if (!rc && conn->local_rx_ctrl.prod_flags.write_blocked &&
609 	    !atomic_read(&conn->bytes_to_rcv))
610 		conn->local_rx_ctrl.prod_flags.write_blocked = 0;
611 
612 out:
613 	release_sock(&smc->sk);
614 }
615 
616 void smc_tx_consumer_update(struct smc_connection *conn, bool force)
617 {
618 	union smc_host_cursor cfed, cons, prod;
619 	int sender_free = conn->rmb_desc->len;
620 	int to_confirm;
621 
622 	smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
623 	smc_curs_copy(&cfed, &conn->rx_curs_confirmed, conn);
624 	to_confirm = smc_curs_diff(conn->rmb_desc->len, &cfed, &cons);
625 	if (to_confirm > conn->rmbe_update_limit) {
626 		smc_curs_copy(&prod, &conn->local_rx_ctrl.prod, conn);
627 		sender_free = conn->rmb_desc->len -
628 			      smc_curs_diff_large(conn->rmb_desc->len,
629 						  &cfed, &prod);
630 	}
631 
632 	if (conn->local_rx_ctrl.prod_flags.cons_curs_upd_req ||
633 	    force ||
634 	    ((to_confirm > conn->rmbe_update_limit) &&
635 	     ((sender_free <= (conn->rmb_desc->len / 2)) ||
636 	      conn->local_rx_ctrl.prod_flags.write_blocked))) {
637 		if (conn->killed ||
638 		    conn->local_rx_ctrl.conn_state_flags.peer_conn_abort)
639 			return;
640 		if ((smc_cdc_get_slot_and_msg_send(conn) < 0) &&
641 		    !conn->killed) {
642 			queue_delayed_work(conn->lgr->tx_wq, &conn->tx_work,
643 					   SMC_TX_WORK_DELAY);
644 			return;
645 		}
646 	}
647 	if (conn->local_rx_ctrl.prod_flags.write_blocked &&
648 	    !atomic_read(&conn->bytes_to_rcv))
649 		conn->local_rx_ctrl.prod_flags.write_blocked = 0;
650 }
651 
652 /***************************** send initialize *******************************/
653 
654 /* Initialize send properties on connection establishment. NB: not __init! */
655 void smc_tx_init(struct smc_sock *smc)
656 {
657 	smc->sk.sk_write_space = smc_tx_write_space;
658 }
659