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