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