1 // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
2 
3 /* Authors: Bernard Metzler <bmt@zurich.ibm.com> */
4 /* Copyright (c) 2008-2019, IBM Corporation */
5 
6 #include <linux/errno.h>
7 #include <linux/types.h>
8 #include <linux/net.h>
9 #include <linux/scatterlist.h>
10 #include <linux/highmem.h>
11 #include <net/tcp.h>
12 
13 #include <rdma/iw_cm.h>
14 #include <rdma/ib_verbs.h>
15 #include <rdma/ib_user_verbs.h>
16 
17 #include "siw.h"
18 #include "siw_verbs.h"
19 #include "siw_mem.h"
20 
21 #define MAX_HDR_INLINE					\
22 	(((uint32_t)(sizeof(struct siw_rreq_pkt) -	\
23 		     sizeof(struct iwarp_send))) & 0xF8)
24 
25 static struct page *siw_get_pblpage(struct siw_mem *mem, u64 addr, int *idx)
26 {
27 	struct siw_pbl *pbl = mem->pbl;
28 	u64 offset = addr - mem->va;
29 	dma_addr_t paddr = siw_pbl_get_buffer(pbl, offset, NULL, idx);
30 
31 	if (paddr)
32 		return ib_virt_dma_to_page(paddr);
33 
34 	return NULL;
35 }
36 
37 /*
38  * Copy short payload at provided destination payload address
39  */
40 static int siw_try_1seg(struct siw_iwarp_tx *c_tx, void *paddr)
41 {
42 	struct siw_wqe *wqe = &c_tx->wqe_active;
43 	struct siw_sge *sge = &wqe->sqe.sge[0];
44 	u32 bytes = sge->length;
45 
46 	if (bytes > MAX_HDR_INLINE || wqe->sqe.num_sge != 1)
47 		return MAX_HDR_INLINE + 1;
48 
49 	if (!bytes)
50 		return 0;
51 
52 	if (tx_flags(wqe) & SIW_WQE_INLINE) {
53 		memcpy(paddr, &wqe->sqe.sge[1], bytes);
54 	} else {
55 		struct siw_mem *mem = wqe->mem[0];
56 
57 		if (!mem->mem_obj) {
58 			/* Kernel client using kva */
59 			memcpy(paddr, ib_virt_dma_to_ptr(sge->laddr), bytes);
60 		} else if (c_tx->in_syscall) {
61 			if (copy_from_user(paddr, u64_to_user_ptr(sge->laddr),
62 					   bytes))
63 				return -EFAULT;
64 		} else {
65 			unsigned int off = sge->laddr & ~PAGE_MASK;
66 			struct page *p;
67 			char *buffer;
68 			int pbl_idx = 0;
69 
70 			if (!mem->is_pbl)
71 				p = siw_get_upage(mem->umem, sge->laddr);
72 			else
73 				p = siw_get_pblpage(mem, sge->laddr, &pbl_idx);
74 
75 			if (unlikely(!p))
76 				return -EFAULT;
77 
78 			buffer = kmap_local_page(p);
79 
80 			if (likely(PAGE_SIZE - off >= bytes)) {
81 				memcpy(paddr, buffer + off, bytes);
82 			} else {
83 				unsigned long part = bytes - (PAGE_SIZE - off);
84 
85 				memcpy(paddr, buffer + off, part);
86 				kunmap_local(buffer);
87 
88 				if (!mem->is_pbl)
89 					p = siw_get_upage(mem->umem,
90 							  sge->laddr + part);
91 				else
92 					p = siw_get_pblpage(mem,
93 							    sge->laddr + part,
94 							    &pbl_idx);
95 				if (unlikely(!p))
96 					return -EFAULT;
97 
98 				buffer = kmap_local_page(p);
99 				memcpy(paddr + part, buffer, bytes - part);
100 			}
101 			kunmap_local(buffer);
102 		}
103 	}
104 	return (int)bytes;
105 }
106 
107 #define PKT_FRAGMENTED 1
108 #define PKT_COMPLETE 0
109 
110 /*
111  * siw_qp_prepare_tx()
112  *
113  * Prepare tx state for sending out one fpdu. Builds complete pkt
114  * if no user data or only immediate data are present.
115  *
116  * returns PKT_COMPLETE if complete pkt built, PKT_FRAGMENTED otherwise.
117  */
118 static int siw_qp_prepare_tx(struct siw_iwarp_tx *c_tx)
119 {
120 	struct siw_wqe *wqe = &c_tx->wqe_active;
121 	char *crc = NULL;
122 	int data = 0;
123 
124 	switch (tx_type(wqe)) {
125 	case SIW_OP_READ:
126 	case SIW_OP_READ_LOCAL_INV:
127 		memcpy(&c_tx->pkt.ctrl,
128 		       &iwarp_pktinfo[RDMAP_RDMA_READ_REQ].ctrl,
129 		       sizeof(struct iwarp_ctrl));
130 
131 		c_tx->pkt.rreq.rsvd = 0;
132 		c_tx->pkt.rreq.ddp_qn = htonl(RDMAP_UNTAGGED_QN_RDMA_READ);
133 		c_tx->pkt.rreq.ddp_msn =
134 			htonl(++c_tx->ddp_msn[RDMAP_UNTAGGED_QN_RDMA_READ]);
135 		c_tx->pkt.rreq.ddp_mo = 0;
136 		c_tx->pkt.rreq.sink_stag = htonl(wqe->sqe.sge[0].lkey);
137 		c_tx->pkt.rreq.sink_to =
138 			cpu_to_be64(wqe->sqe.sge[0].laddr);
139 		c_tx->pkt.rreq.source_stag = htonl(wqe->sqe.rkey);
140 		c_tx->pkt.rreq.source_to = cpu_to_be64(wqe->sqe.raddr);
141 		c_tx->pkt.rreq.read_size = htonl(wqe->sqe.sge[0].length);
142 
143 		c_tx->ctrl_len = sizeof(struct iwarp_rdma_rreq);
144 		crc = (char *)&c_tx->pkt.rreq_pkt.crc;
145 		break;
146 
147 	case SIW_OP_SEND:
148 		if (tx_flags(wqe) & SIW_WQE_SOLICITED)
149 			memcpy(&c_tx->pkt.ctrl,
150 			       &iwarp_pktinfo[RDMAP_SEND_SE].ctrl,
151 			       sizeof(struct iwarp_ctrl));
152 		else
153 			memcpy(&c_tx->pkt.ctrl, &iwarp_pktinfo[RDMAP_SEND].ctrl,
154 			       sizeof(struct iwarp_ctrl));
155 
156 		c_tx->pkt.send.ddp_qn = RDMAP_UNTAGGED_QN_SEND;
157 		c_tx->pkt.send.ddp_msn =
158 			htonl(++c_tx->ddp_msn[RDMAP_UNTAGGED_QN_SEND]);
159 		c_tx->pkt.send.ddp_mo = 0;
160 
161 		c_tx->pkt.send_inv.inval_stag = 0;
162 
163 		c_tx->ctrl_len = sizeof(struct iwarp_send);
164 
165 		crc = (char *)&c_tx->pkt.send_pkt.crc;
166 		data = siw_try_1seg(c_tx, crc);
167 		break;
168 
169 	case SIW_OP_SEND_REMOTE_INV:
170 		if (tx_flags(wqe) & SIW_WQE_SOLICITED)
171 			memcpy(&c_tx->pkt.ctrl,
172 			       &iwarp_pktinfo[RDMAP_SEND_SE_INVAL].ctrl,
173 			       sizeof(struct iwarp_ctrl));
174 		else
175 			memcpy(&c_tx->pkt.ctrl,
176 			       &iwarp_pktinfo[RDMAP_SEND_INVAL].ctrl,
177 			       sizeof(struct iwarp_ctrl));
178 
179 		c_tx->pkt.send.ddp_qn = RDMAP_UNTAGGED_QN_SEND;
180 		c_tx->pkt.send.ddp_msn =
181 			htonl(++c_tx->ddp_msn[RDMAP_UNTAGGED_QN_SEND]);
182 		c_tx->pkt.send.ddp_mo = 0;
183 
184 		c_tx->pkt.send_inv.inval_stag = cpu_to_be32(wqe->sqe.rkey);
185 
186 		c_tx->ctrl_len = sizeof(struct iwarp_send_inv);
187 
188 		crc = (char *)&c_tx->pkt.send_pkt.crc;
189 		data = siw_try_1seg(c_tx, crc);
190 		break;
191 
192 	case SIW_OP_WRITE:
193 		memcpy(&c_tx->pkt.ctrl, &iwarp_pktinfo[RDMAP_RDMA_WRITE].ctrl,
194 		       sizeof(struct iwarp_ctrl));
195 
196 		c_tx->pkt.rwrite.sink_stag = htonl(wqe->sqe.rkey);
197 		c_tx->pkt.rwrite.sink_to = cpu_to_be64(wqe->sqe.raddr);
198 		c_tx->ctrl_len = sizeof(struct iwarp_rdma_write);
199 
200 		crc = (char *)&c_tx->pkt.write_pkt.crc;
201 		data = siw_try_1seg(c_tx, crc);
202 		break;
203 
204 	case SIW_OP_READ_RESPONSE:
205 		memcpy(&c_tx->pkt.ctrl,
206 		       &iwarp_pktinfo[RDMAP_RDMA_READ_RESP].ctrl,
207 		       sizeof(struct iwarp_ctrl));
208 
209 		/* NBO */
210 		c_tx->pkt.rresp.sink_stag = cpu_to_be32(wqe->sqe.rkey);
211 		c_tx->pkt.rresp.sink_to = cpu_to_be64(wqe->sqe.raddr);
212 
213 		c_tx->ctrl_len = sizeof(struct iwarp_rdma_rresp);
214 
215 		crc = (char *)&c_tx->pkt.write_pkt.crc;
216 		data = siw_try_1seg(c_tx, crc);
217 		break;
218 
219 	default:
220 		siw_dbg_qp(tx_qp(c_tx), "stale wqe type %d\n", tx_type(wqe));
221 		return -EOPNOTSUPP;
222 	}
223 	if (unlikely(data < 0))
224 		return data;
225 
226 	c_tx->ctrl_sent = 0;
227 
228 	if (data <= MAX_HDR_INLINE) {
229 		if (data) {
230 			wqe->processed = data;
231 
232 			c_tx->pkt.ctrl.mpa_len =
233 				htons(c_tx->ctrl_len + data - MPA_HDR_SIZE);
234 
235 			/* Add pad, if needed */
236 			data += -(int)data & 0x3;
237 			/* advance CRC location after payload */
238 			crc += data;
239 			c_tx->ctrl_len += data;
240 
241 			if (!(c_tx->pkt.ctrl.ddp_rdmap_ctrl & DDP_FLAG_TAGGED))
242 				c_tx->pkt.c_untagged.ddp_mo = 0;
243 			else
244 				c_tx->pkt.c_tagged.ddp_to =
245 					cpu_to_be64(wqe->sqe.raddr);
246 		}
247 
248 		*(u32 *)crc = 0;
249 		/*
250 		 * Do complete CRC if enabled and short packet
251 		 */
252 		if (c_tx->mpa_crc_hd) {
253 			crypto_shash_init(c_tx->mpa_crc_hd);
254 			if (crypto_shash_update(c_tx->mpa_crc_hd,
255 						(u8 *)&c_tx->pkt,
256 						c_tx->ctrl_len))
257 				return -EINVAL;
258 			crypto_shash_final(c_tx->mpa_crc_hd, (u8 *)crc);
259 		}
260 		c_tx->ctrl_len += MPA_CRC_SIZE;
261 
262 		return PKT_COMPLETE;
263 	}
264 	c_tx->ctrl_len += MPA_CRC_SIZE;
265 	c_tx->sge_idx = 0;
266 	c_tx->sge_off = 0;
267 	c_tx->pbl_idx = 0;
268 
269 	/*
270 	 * Allow direct sending out of user buffer if WR is non signalled
271 	 * and payload is over threshold.
272 	 * Per RDMA verbs, the application should not change the send buffer
273 	 * until the work completed. In iWarp, work completion is only
274 	 * local delivery to TCP. TCP may reuse the buffer for
275 	 * retransmission. Changing unsent data also breaks the CRC,
276 	 * if applied.
277 	 */
278 	if (c_tx->zcopy_tx && wqe->bytes >= SENDPAGE_THRESH &&
279 	    !(tx_flags(wqe) & SIW_WQE_SIGNALLED))
280 		c_tx->use_sendpage = 1;
281 	else
282 		c_tx->use_sendpage = 0;
283 
284 	return PKT_FRAGMENTED;
285 }
286 
287 /*
288  * Send out one complete control type FPDU, or header of FPDU carrying
289  * data. Used for fixed sized packets like Read.Requests or zero length
290  * SENDs, WRITEs, READ.Responses, or header only.
291  */
292 static int siw_tx_ctrl(struct siw_iwarp_tx *c_tx, struct socket *s,
293 			      int flags)
294 {
295 	struct msghdr msg = { .msg_flags = flags };
296 	struct kvec iov = { .iov_base =
297 				    (char *)&c_tx->pkt.ctrl + c_tx->ctrl_sent,
298 			    .iov_len = c_tx->ctrl_len - c_tx->ctrl_sent };
299 
300 	int rv = kernel_sendmsg(s, &msg, &iov, 1,
301 				c_tx->ctrl_len - c_tx->ctrl_sent);
302 
303 	if (rv >= 0) {
304 		c_tx->ctrl_sent += rv;
305 
306 		if (c_tx->ctrl_sent == c_tx->ctrl_len)
307 			rv = 0;
308 		else
309 			rv = -EAGAIN;
310 	}
311 	return rv;
312 }
313 
314 /*
315  * 0copy TCP transmit interface: Use MSG_SPLICE_PAGES.
316  *
317  * Using sendpage to push page by page appears to be less efficient
318  * than using sendmsg, even if data are copied.
319  *
320  * A general performance limitation might be the extra four bytes
321  * trailer checksum segment to be pushed after user data.
322  */
323 static int siw_tcp_sendpages(struct socket *s, struct page **page, int offset,
324 			     size_t size)
325 {
326 	struct bio_vec bvec;
327 	struct msghdr msg = {
328 		.msg_flags = (MSG_MORE | MSG_DONTWAIT | MSG_SENDPAGE_NOTLAST |
329 			      MSG_SPLICE_PAGES),
330 	};
331 	struct sock *sk = s->sk;
332 	int i = 0, rv = 0, sent = 0;
333 
334 	while (size) {
335 		size_t bytes = min_t(size_t, PAGE_SIZE - offset, size);
336 
337 		if (size + offset <= PAGE_SIZE)
338 			msg.msg_flags &= ~MSG_SENDPAGE_NOTLAST;
339 
340 		tcp_rate_check_app_limited(sk);
341 		bvec_set_page(&bvec, page[i], bytes, offset);
342 		iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, size);
343 
344 try_page_again:
345 		lock_sock(sk);
346 		rv = tcp_sendmsg_locked(sk, &msg, size);
347 		release_sock(sk);
348 
349 		if (rv > 0) {
350 			size -= rv;
351 			sent += rv;
352 			if (rv != bytes) {
353 				offset += rv;
354 				bytes -= rv;
355 				goto try_page_again;
356 			}
357 			offset = 0;
358 		} else {
359 			if (rv == -EAGAIN || rv == 0)
360 				break;
361 			return rv;
362 		}
363 		i++;
364 	}
365 	return sent;
366 }
367 
368 /*
369  * siw_0copy_tx()
370  *
371  * Pushes list of pages to TCP socket. If pages from multiple
372  * SGE's, all referenced pages of each SGE are pushed in one
373  * shot.
374  */
375 static int siw_0copy_tx(struct socket *s, struct page **page,
376 			struct siw_sge *sge, unsigned int offset,
377 			unsigned int size)
378 {
379 	int i = 0, sent = 0, rv;
380 	int sge_bytes = min(sge->length - offset, size);
381 
382 	offset = (sge->laddr + offset) & ~PAGE_MASK;
383 
384 	while (sent != size) {
385 		rv = siw_tcp_sendpages(s, &page[i], offset, sge_bytes);
386 		if (rv >= 0) {
387 			sent += rv;
388 			if (size == sent || sge_bytes > rv)
389 				break;
390 
391 			i += PAGE_ALIGN(sge_bytes + offset) >> PAGE_SHIFT;
392 			sge++;
393 			sge_bytes = min(sge->length, size - sent);
394 			offset = sge->laddr & ~PAGE_MASK;
395 		} else {
396 			sent = rv;
397 			break;
398 		}
399 	}
400 	return sent;
401 }
402 
403 #define MAX_TRAILER (MPA_CRC_SIZE + 4)
404 
405 static void siw_unmap_pages(struct kvec *iov, unsigned long kmap_mask, int len)
406 {
407 	int i;
408 
409 	/*
410 	 * Work backwards through the array to honor the kmap_local_page()
411 	 * ordering requirements.
412 	 */
413 	for (i = (len-1); i >= 0; i--) {
414 		if (kmap_mask & BIT(i)) {
415 			unsigned long addr = (unsigned long)iov[i].iov_base;
416 
417 			kunmap_local((void *)(addr & PAGE_MASK));
418 		}
419 	}
420 }
421 
422 /*
423  * siw_tx_hdt() tries to push a complete packet to TCP where all
424  * packet fragments are referenced by the elements of one iovec.
425  * For the data portion, each involved page must be referenced by
426  * one extra element. All sge's data can be non-aligned to page
427  * boundaries. Two more elements are referencing iWARP header
428  * and trailer:
429  * MAX_ARRAY = 64KB/PAGE_SIZE + 1 + (2 * (SIW_MAX_SGE - 1) + HDR + TRL
430  */
431 #define MAX_ARRAY ((0xffff / PAGE_SIZE) + 1 + (2 * (SIW_MAX_SGE - 1) + 2))
432 
433 /*
434  * Write out iov referencing hdr, data and trailer of current FPDU.
435  * Update transmit state dependent on write return status
436  */
437 static int siw_tx_hdt(struct siw_iwarp_tx *c_tx, struct socket *s)
438 {
439 	struct siw_wqe *wqe = &c_tx->wqe_active;
440 	struct siw_sge *sge = &wqe->sqe.sge[c_tx->sge_idx];
441 	struct kvec iov[MAX_ARRAY];
442 	struct page *page_array[MAX_ARRAY];
443 	struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_EOR };
444 
445 	int seg = 0, do_crc = c_tx->do_crc, is_kva = 0, rv;
446 	unsigned int data_len = c_tx->bytes_unsent, hdr_len = 0, trl_len = 0,
447 		     sge_off = c_tx->sge_off, sge_idx = c_tx->sge_idx,
448 		     pbl_idx = c_tx->pbl_idx;
449 	unsigned long kmap_mask = 0L;
450 
451 	if (c_tx->state == SIW_SEND_HDR) {
452 		if (c_tx->use_sendpage) {
453 			rv = siw_tx_ctrl(c_tx, s, MSG_DONTWAIT | MSG_MORE);
454 			if (rv)
455 				goto done;
456 
457 			c_tx->state = SIW_SEND_DATA;
458 		} else {
459 			iov[0].iov_base =
460 				(char *)&c_tx->pkt.ctrl + c_tx->ctrl_sent;
461 			iov[0].iov_len = hdr_len =
462 				c_tx->ctrl_len - c_tx->ctrl_sent;
463 			seg = 1;
464 		}
465 	}
466 
467 	wqe->processed += data_len;
468 
469 	while (data_len) { /* walk the list of SGE's */
470 		unsigned int sge_len = min(sge->length - sge_off, data_len);
471 		unsigned int fp_off = (sge->laddr + sge_off) & ~PAGE_MASK;
472 		struct siw_mem *mem;
473 
474 		if (!(tx_flags(wqe) & SIW_WQE_INLINE)) {
475 			mem = wqe->mem[sge_idx];
476 			is_kva = mem->mem_obj == NULL ? 1 : 0;
477 		} else {
478 			is_kva = 1;
479 		}
480 		if (is_kva && !c_tx->use_sendpage) {
481 			/*
482 			 * tx from kernel virtual address: either inline data
483 			 * or memory region with assigned kernel buffer
484 			 */
485 			iov[seg].iov_base =
486 				ib_virt_dma_to_ptr(sge->laddr + sge_off);
487 			iov[seg].iov_len = sge_len;
488 
489 			if (do_crc)
490 				crypto_shash_update(c_tx->mpa_crc_hd,
491 						    iov[seg].iov_base,
492 						    sge_len);
493 			sge_off += sge_len;
494 			data_len -= sge_len;
495 			seg++;
496 			goto sge_done;
497 		}
498 
499 		while (sge_len) {
500 			size_t plen = min((int)PAGE_SIZE - fp_off, sge_len);
501 			void *kaddr;
502 
503 			if (!is_kva) {
504 				struct page *p;
505 
506 				if (mem->is_pbl)
507 					p = siw_get_pblpage(
508 						mem, sge->laddr + sge_off,
509 						&pbl_idx);
510 				else
511 					p = siw_get_upage(mem->umem,
512 							  sge->laddr + sge_off);
513 				if (unlikely(!p)) {
514 					siw_unmap_pages(iov, kmap_mask, seg);
515 					wqe->processed -= c_tx->bytes_unsent;
516 					rv = -EFAULT;
517 					goto done_crc;
518 				}
519 				page_array[seg] = p;
520 
521 				if (!c_tx->use_sendpage) {
522 					void *kaddr = kmap_local_page(p);
523 
524 					/* Remember for later kunmap() */
525 					kmap_mask |= BIT(seg);
526 					iov[seg].iov_base = kaddr + fp_off;
527 					iov[seg].iov_len = plen;
528 
529 					if (do_crc)
530 						crypto_shash_update(
531 							c_tx->mpa_crc_hd,
532 							iov[seg].iov_base,
533 							plen);
534 				} else if (do_crc) {
535 					kaddr = kmap_local_page(p);
536 					crypto_shash_update(c_tx->mpa_crc_hd,
537 							    kaddr + fp_off,
538 							    plen);
539 					kunmap_local(kaddr);
540 				}
541 			} else {
542 				/*
543 				 * Cast to an uintptr_t to preserve all 64 bits
544 				 * in sge->laddr.
545 				 */
546 				u64 va = sge->laddr + sge_off;
547 
548 				page_array[seg] = ib_virt_dma_to_page(va);
549 				if (do_crc)
550 					crypto_shash_update(
551 						c_tx->mpa_crc_hd,
552 						ib_virt_dma_to_ptr(va),
553 						plen);
554 			}
555 
556 			sge_len -= plen;
557 			sge_off += plen;
558 			data_len -= plen;
559 			fp_off = 0;
560 
561 			if (++seg >= (int)MAX_ARRAY) {
562 				siw_dbg_qp(tx_qp(c_tx), "to many fragments\n");
563 				siw_unmap_pages(iov, kmap_mask, seg-1);
564 				wqe->processed -= c_tx->bytes_unsent;
565 				rv = -EMSGSIZE;
566 				goto done_crc;
567 			}
568 		}
569 sge_done:
570 		/* Update SGE variables at end of SGE */
571 		if (sge_off == sge->length &&
572 		    (data_len != 0 || wqe->processed < wqe->bytes)) {
573 			sge_idx++;
574 			sge++;
575 			sge_off = 0;
576 		}
577 	}
578 	/* trailer */
579 	if (likely(c_tx->state != SIW_SEND_TRAILER)) {
580 		iov[seg].iov_base = &c_tx->trailer.pad[4 - c_tx->pad];
581 		iov[seg].iov_len = trl_len = MAX_TRAILER - (4 - c_tx->pad);
582 	} else {
583 		iov[seg].iov_base = &c_tx->trailer.pad[c_tx->ctrl_sent];
584 		iov[seg].iov_len = trl_len = MAX_TRAILER - c_tx->ctrl_sent;
585 	}
586 
587 	if (c_tx->pad) {
588 		*(u32 *)c_tx->trailer.pad = 0;
589 		if (do_crc)
590 			crypto_shash_update(c_tx->mpa_crc_hd,
591 				(u8 *)&c_tx->trailer.crc - c_tx->pad,
592 				c_tx->pad);
593 	}
594 	if (!c_tx->mpa_crc_hd)
595 		c_tx->trailer.crc = 0;
596 	else if (do_crc)
597 		crypto_shash_final(c_tx->mpa_crc_hd, (u8 *)&c_tx->trailer.crc);
598 
599 	data_len = c_tx->bytes_unsent;
600 
601 	if (c_tx->use_sendpage) {
602 		rv = siw_0copy_tx(s, page_array, &wqe->sqe.sge[c_tx->sge_idx],
603 				  c_tx->sge_off, data_len);
604 		if (rv == data_len) {
605 			rv = kernel_sendmsg(s, &msg, &iov[seg], 1, trl_len);
606 			if (rv > 0)
607 				rv += data_len;
608 			else
609 				rv = data_len;
610 		}
611 	} else {
612 		rv = kernel_sendmsg(s, &msg, iov, seg + 1,
613 				    hdr_len + data_len + trl_len);
614 		siw_unmap_pages(iov, kmap_mask, seg);
615 	}
616 	if (rv < (int)hdr_len) {
617 		/* Not even complete hdr pushed or negative rv */
618 		wqe->processed -= data_len;
619 		if (rv >= 0) {
620 			c_tx->ctrl_sent += rv;
621 			rv = -EAGAIN;
622 		}
623 		goto done_crc;
624 	}
625 	rv -= hdr_len;
626 
627 	if (rv >= (int)data_len) {
628 		/* all user data pushed to TCP or no data to push */
629 		if (data_len > 0 && wqe->processed < wqe->bytes) {
630 			/* Save the current state for next tx */
631 			c_tx->sge_idx = sge_idx;
632 			c_tx->sge_off = sge_off;
633 			c_tx->pbl_idx = pbl_idx;
634 		}
635 		rv -= data_len;
636 
637 		if (rv == trl_len) /* all pushed */
638 			rv = 0;
639 		else {
640 			c_tx->state = SIW_SEND_TRAILER;
641 			c_tx->ctrl_len = MAX_TRAILER;
642 			c_tx->ctrl_sent = rv + 4 - c_tx->pad;
643 			c_tx->bytes_unsent = 0;
644 			rv = -EAGAIN;
645 		}
646 
647 	} else if (data_len > 0) {
648 		/* Maybe some user data pushed to TCP */
649 		c_tx->state = SIW_SEND_DATA;
650 		wqe->processed -= data_len - rv;
651 
652 		if (rv) {
653 			/*
654 			 * Some bytes out. Recompute tx state based
655 			 * on old state and bytes pushed
656 			 */
657 			unsigned int sge_unsent;
658 
659 			c_tx->bytes_unsent -= rv;
660 			sge = &wqe->sqe.sge[c_tx->sge_idx];
661 			sge_unsent = sge->length - c_tx->sge_off;
662 
663 			while (sge_unsent <= rv) {
664 				rv -= sge_unsent;
665 				c_tx->sge_idx++;
666 				c_tx->sge_off = 0;
667 				sge++;
668 				sge_unsent = sge->length;
669 			}
670 			c_tx->sge_off += rv;
671 		}
672 		rv = -EAGAIN;
673 	}
674 done_crc:
675 	c_tx->do_crc = 0;
676 done:
677 	return rv;
678 }
679 
680 static void siw_update_tcpseg(struct siw_iwarp_tx *c_tx,
681 				     struct socket *s)
682 {
683 	struct tcp_sock *tp = tcp_sk(s->sk);
684 
685 	if (tp->gso_segs) {
686 		if (c_tx->gso_seg_limit == 0)
687 			c_tx->tcp_seglen = tp->mss_cache * tp->gso_segs;
688 		else
689 			c_tx->tcp_seglen =
690 				tp->mss_cache *
691 				min_t(u16, c_tx->gso_seg_limit, tp->gso_segs);
692 	} else {
693 		c_tx->tcp_seglen = tp->mss_cache;
694 	}
695 	/* Loopback may give odd numbers */
696 	c_tx->tcp_seglen &= 0xfffffff8;
697 }
698 
699 /*
700  * siw_prepare_fpdu()
701  *
702  * Prepares transmit context to send out one FPDU if FPDU will contain
703  * user data and user data are not immediate data.
704  * Computes maximum FPDU length to fill up TCP MSS if possible.
705  *
706  * @qp:		QP from which to transmit
707  * @wqe:	Current WQE causing transmission
708  *
709  * TODO: Take into account real available sendspace on socket
710  *       to avoid header misalignment due to send pausing within
711  *       fpdu transmission
712  */
713 static void siw_prepare_fpdu(struct siw_qp *qp, struct siw_wqe *wqe)
714 {
715 	struct siw_iwarp_tx *c_tx = &qp->tx_ctx;
716 	int data_len;
717 
718 	c_tx->ctrl_len =
719 		iwarp_pktinfo[__rdmap_get_opcode(&c_tx->pkt.ctrl)].hdr_len;
720 	c_tx->ctrl_sent = 0;
721 
722 	/*
723 	 * Update target buffer offset if any
724 	 */
725 	if (!(c_tx->pkt.ctrl.ddp_rdmap_ctrl & DDP_FLAG_TAGGED))
726 		/* Untagged message */
727 		c_tx->pkt.c_untagged.ddp_mo = cpu_to_be32(wqe->processed);
728 	else /* Tagged message */
729 		c_tx->pkt.c_tagged.ddp_to =
730 			cpu_to_be64(wqe->sqe.raddr + wqe->processed);
731 
732 	data_len = wqe->bytes - wqe->processed;
733 	if (data_len + c_tx->ctrl_len + MPA_CRC_SIZE > c_tx->tcp_seglen) {
734 		/* Trim DDP payload to fit into current TCP segment */
735 		data_len = c_tx->tcp_seglen - (c_tx->ctrl_len + MPA_CRC_SIZE);
736 		c_tx->pkt.ctrl.ddp_rdmap_ctrl &= ~DDP_FLAG_LAST;
737 		c_tx->pad = 0;
738 	} else {
739 		c_tx->pkt.ctrl.ddp_rdmap_ctrl |= DDP_FLAG_LAST;
740 		c_tx->pad = -data_len & 0x3;
741 	}
742 	c_tx->bytes_unsent = data_len;
743 
744 	c_tx->pkt.ctrl.mpa_len =
745 		htons(c_tx->ctrl_len + data_len - MPA_HDR_SIZE);
746 
747 	/*
748 	 * Init MPA CRC computation
749 	 */
750 	if (c_tx->mpa_crc_hd) {
751 		crypto_shash_init(c_tx->mpa_crc_hd);
752 		crypto_shash_update(c_tx->mpa_crc_hd, (u8 *)&c_tx->pkt,
753 				    c_tx->ctrl_len);
754 		c_tx->do_crc = 1;
755 	}
756 }
757 
758 /*
759  * siw_check_sgl_tx()
760  *
761  * Check permissions for a list of SGE's (SGL).
762  * A successful check will have all memory referenced
763  * for transmission resolved and assigned to the WQE.
764  *
765  * @pd:		Protection Domain SGL should belong to
766  * @wqe:	WQE to be checked
767  * @perms:	requested access permissions
768  *
769  */
770 
771 static int siw_check_sgl_tx(struct ib_pd *pd, struct siw_wqe *wqe,
772 			    enum ib_access_flags perms)
773 {
774 	struct siw_sge *sge = &wqe->sqe.sge[0];
775 	int i, len, num_sge = wqe->sqe.num_sge;
776 
777 	if (unlikely(num_sge > SIW_MAX_SGE))
778 		return -EINVAL;
779 
780 	for (i = 0, len = 0; num_sge; num_sge--, i++, sge++) {
781 		/*
782 		 * rdma verbs: do not check stag for a zero length sge
783 		 */
784 		if (sge->length) {
785 			int rv = siw_check_sge(pd, sge, &wqe->mem[i], perms, 0,
786 					       sge->length);
787 
788 			if (unlikely(rv != E_ACCESS_OK))
789 				return rv;
790 		}
791 		len += sge->length;
792 	}
793 	return len;
794 }
795 
796 /*
797  * siw_qp_sq_proc_tx()
798  *
799  * Process one WQE which needs transmission on the wire.
800  */
801 static int siw_qp_sq_proc_tx(struct siw_qp *qp, struct siw_wqe *wqe)
802 {
803 	struct siw_iwarp_tx *c_tx = &qp->tx_ctx;
804 	struct socket *s = qp->attrs.sk;
805 	int rv = 0, burst_len = qp->tx_ctx.burst;
806 	enum rdmap_ecode ecode = RDMAP_ECODE_CATASTROPHIC_STREAM;
807 
808 	if (unlikely(wqe->wr_status == SIW_WR_IDLE))
809 		return 0;
810 
811 	if (!burst_len)
812 		burst_len = SQ_USER_MAXBURST;
813 
814 	if (wqe->wr_status == SIW_WR_QUEUED) {
815 		if (!(wqe->sqe.flags & SIW_WQE_INLINE)) {
816 			if (tx_type(wqe) == SIW_OP_READ_RESPONSE)
817 				wqe->sqe.num_sge = 1;
818 
819 			if (tx_type(wqe) != SIW_OP_READ &&
820 			    tx_type(wqe) != SIW_OP_READ_LOCAL_INV) {
821 				/*
822 				 * Reference memory to be tx'd w/o checking
823 				 * access for LOCAL_READ permission, since
824 				 * not defined in RDMA core.
825 				 */
826 				rv = siw_check_sgl_tx(qp->pd, wqe, 0);
827 				if (rv < 0) {
828 					if (tx_type(wqe) ==
829 					    SIW_OP_READ_RESPONSE)
830 						ecode = siw_rdmap_error(-rv);
831 					rv = -EINVAL;
832 					goto tx_error;
833 				}
834 				wqe->bytes = rv;
835 			} else {
836 				wqe->bytes = 0;
837 			}
838 		} else {
839 			wqe->bytes = wqe->sqe.sge[0].length;
840 			if (!rdma_is_kernel_res(&qp->base_qp.res)) {
841 				if (wqe->bytes > SIW_MAX_INLINE) {
842 					rv = -EINVAL;
843 					goto tx_error;
844 				}
845 				wqe->sqe.sge[0].laddr =
846 					(u64)(uintptr_t)&wqe->sqe.sge[1];
847 			}
848 		}
849 		wqe->wr_status = SIW_WR_INPROGRESS;
850 		wqe->processed = 0;
851 
852 		siw_update_tcpseg(c_tx, s);
853 
854 		rv = siw_qp_prepare_tx(c_tx);
855 		if (rv == PKT_FRAGMENTED) {
856 			c_tx->state = SIW_SEND_HDR;
857 			siw_prepare_fpdu(qp, wqe);
858 		} else if (rv == PKT_COMPLETE) {
859 			c_tx->state = SIW_SEND_SHORT_FPDU;
860 		} else {
861 			goto tx_error;
862 		}
863 	}
864 
865 next_segment:
866 	siw_dbg_qp(qp, "wr type %d, state %d, data %u, sent %u, id %llx\n",
867 		   tx_type(wqe), wqe->wr_status, wqe->bytes, wqe->processed,
868 		   wqe->sqe.id);
869 
870 	if (--burst_len == 0) {
871 		rv = -EINPROGRESS;
872 		goto tx_done;
873 	}
874 	if (c_tx->state == SIW_SEND_SHORT_FPDU) {
875 		enum siw_opcode tx_type = tx_type(wqe);
876 		unsigned int msg_flags;
877 
878 		if (siw_sq_empty(qp) || !siw_tcp_nagle || burst_len == 1)
879 			/*
880 			 * End current TCP segment, if SQ runs empty,
881 			 * or siw_tcp_nagle is not set, or we bail out
882 			 * soon due to no burst credit left.
883 			 */
884 			msg_flags = MSG_DONTWAIT;
885 		else
886 			msg_flags = MSG_DONTWAIT | MSG_MORE;
887 
888 		rv = siw_tx_ctrl(c_tx, s, msg_flags);
889 
890 		if (!rv && tx_type != SIW_OP_READ &&
891 		    tx_type != SIW_OP_READ_LOCAL_INV)
892 			wqe->processed = wqe->bytes;
893 
894 		goto tx_done;
895 
896 	} else {
897 		rv = siw_tx_hdt(c_tx, s);
898 	}
899 	if (!rv) {
900 		/*
901 		 * One segment sent. Processing completed if last
902 		 * segment, Do next segment otherwise.
903 		 */
904 		if (unlikely(c_tx->tx_suspend)) {
905 			/*
906 			 * Verbs, 6.4.: Try stopping sending after a full
907 			 * DDP segment if the connection goes down
908 			 * (== peer halfclose)
909 			 */
910 			rv = -ECONNABORTED;
911 			goto tx_done;
912 		}
913 		if (c_tx->pkt.ctrl.ddp_rdmap_ctrl & DDP_FLAG_LAST) {
914 			siw_dbg_qp(qp, "WQE completed\n");
915 			goto tx_done;
916 		}
917 		c_tx->state = SIW_SEND_HDR;
918 
919 		siw_update_tcpseg(c_tx, s);
920 
921 		siw_prepare_fpdu(qp, wqe);
922 		goto next_segment;
923 	}
924 tx_done:
925 	qp->tx_ctx.burst = burst_len;
926 	return rv;
927 
928 tx_error:
929 	if (ecode != RDMAP_ECODE_CATASTROPHIC_STREAM)
930 		siw_init_terminate(qp, TERM_ERROR_LAYER_RDMAP,
931 				   RDMAP_ETYPE_REMOTE_PROTECTION, ecode, 1);
932 	else
933 		siw_init_terminate(qp, TERM_ERROR_LAYER_RDMAP,
934 				   RDMAP_ETYPE_CATASTROPHIC,
935 				   RDMAP_ECODE_UNSPECIFIED, 1);
936 	return rv;
937 }
938 
939 static int siw_fastreg_mr(struct ib_pd *pd, struct siw_sqe *sqe)
940 {
941 	struct ib_mr *base_mr = (struct ib_mr *)(uintptr_t)sqe->base_mr;
942 	struct siw_device *sdev = to_siw_dev(pd->device);
943 	struct siw_mem *mem;
944 	int rv = 0;
945 
946 	siw_dbg_pd(pd, "STag 0x%08x\n", sqe->rkey);
947 
948 	if (unlikely(!base_mr)) {
949 		pr_warn("siw: fastreg: STag 0x%08x unknown\n", sqe->rkey);
950 		return -EINVAL;
951 	}
952 
953 	if (unlikely(base_mr->rkey >> 8 != sqe->rkey  >> 8)) {
954 		pr_warn("siw: fastreg: STag 0x%08x: bad MR\n", sqe->rkey);
955 		return -EINVAL;
956 	}
957 
958 	mem = siw_mem_id2obj(sdev, sqe->rkey  >> 8);
959 	if (unlikely(!mem)) {
960 		pr_warn("siw: fastreg: STag 0x%08x unknown\n", sqe->rkey);
961 		return -EINVAL;
962 	}
963 
964 	if (unlikely(mem->pd != pd)) {
965 		pr_warn("siw: fastreg: PD mismatch\n");
966 		rv = -EINVAL;
967 		goto out;
968 	}
969 	if (unlikely(mem->stag_valid)) {
970 		pr_warn("siw: fastreg: STag 0x%08x already valid\n", sqe->rkey);
971 		rv = -EINVAL;
972 		goto out;
973 	}
974 	/* Refresh STag since user may have changed key part */
975 	mem->stag = sqe->rkey;
976 	mem->perms = sqe->access;
977 
978 	siw_dbg_mem(mem, "STag 0x%08x now valid\n", sqe->rkey);
979 	mem->va = base_mr->iova;
980 	mem->stag_valid = 1;
981 out:
982 	siw_mem_put(mem);
983 	return rv;
984 }
985 
986 static int siw_qp_sq_proc_local(struct siw_qp *qp, struct siw_wqe *wqe)
987 {
988 	int rv;
989 
990 	switch (tx_type(wqe)) {
991 	case SIW_OP_REG_MR:
992 		rv = siw_fastreg_mr(qp->pd, &wqe->sqe);
993 		break;
994 
995 	case SIW_OP_INVAL_STAG:
996 		rv = siw_invalidate_stag(qp->pd, wqe->sqe.rkey);
997 		break;
998 
999 	default:
1000 		rv = -EINVAL;
1001 	}
1002 	return rv;
1003 }
1004 
1005 /*
1006  * siw_qp_sq_process()
1007  *
1008  * Core TX path routine for RDMAP/DDP/MPA using a TCP kernel socket.
1009  * Sends RDMAP payload for the current SQ WR @wqe of @qp in one or more
1010  * MPA FPDUs, each containing a DDP segment.
1011  *
1012  * SQ processing may occur in user context as a result of posting
1013  * new WQE's or from siw_sq_work_handler() context. Processing in
1014  * user context is limited to non-kernel verbs users.
1015  *
1016  * SQ processing may get paused anytime, possibly in the middle of a WR
1017  * or FPDU, if insufficient send space is available. SQ processing
1018  * gets resumed from siw_sq_work_handler(), if send space becomes
1019  * available again.
1020  *
1021  * Must be called with the QP state read-locked.
1022  *
1023  * Note:
1024  * An outbound RREQ can be satisfied by the corresponding RRESP
1025  * _before_ it gets assigned to the ORQ. This happens regularly
1026  * in RDMA READ via loopback case. Since both outbound RREQ and
1027  * inbound RRESP can be handled by the same CPU, locking the ORQ
1028  * is dead-lock prone and thus not an option. With that, the
1029  * RREQ gets assigned to the ORQ _before_ being sent - see
1030  * siw_activate_tx() - and pulled back in case of send failure.
1031  */
1032 int siw_qp_sq_process(struct siw_qp *qp)
1033 {
1034 	struct siw_wqe *wqe = tx_wqe(qp);
1035 	enum siw_opcode tx_type;
1036 	unsigned long flags;
1037 	int rv = 0;
1038 
1039 	siw_dbg_qp(qp, "enter for type %d\n", tx_type(wqe));
1040 
1041 next_wqe:
1042 	/*
1043 	 * Stop QP processing if SQ state changed
1044 	 */
1045 	if (unlikely(qp->tx_ctx.tx_suspend)) {
1046 		siw_dbg_qp(qp, "tx suspended\n");
1047 		goto done;
1048 	}
1049 	tx_type = tx_type(wqe);
1050 
1051 	if (tx_type <= SIW_OP_READ_RESPONSE)
1052 		rv = siw_qp_sq_proc_tx(qp, wqe);
1053 	else
1054 		rv = siw_qp_sq_proc_local(qp, wqe);
1055 
1056 	if (!rv) {
1057 		/*
1058 		 * WQE processing done
1059 		 */
1060 		switch (tx_type) {
1061 		case SIW_OP_SEND:
1062 		case SIW_OP_SEND_REMOTE_INV:
1063 		case SIW_OP_WRITE:
1064 			siw_wqe_put_mem(wqe, tx_type);
1065 			fallthrough;
1066 
1067 		case SIW_OP_INVAL_STAG:
1068 		case SIW_OP_REG_MR:
1069 			if (tx_flags(wqe) & SIW_WQE_SIGNALLED)
1070 				siw_sqe_complete(qp, &wqe->sqe, wqe->bytes,
1071 						 SIW_WC_SUCCESS);
1072 			break;
1073 
1074 		case SIW_OP_READ:
1075 		case SIW_OP_READ_LOCAL_INV:
1076 			/*
1077 			 * already enqueued to ORQ queue
1078 			 */
1079 			break;
1080 
1081 		case SIW_OP_READ_RESPONSE:
1082 			siw_wqe_put_mem(wqe, tx_type);
1083 			break;
1084 
1085 		default:
1086 			WARN(1, "undefined WQE type %d\n", tx_type);
1087 			rv = -EINVAL;
1088 			goto done;
1089 		}
1090 
1091 		spin_lock_irqsave(&qp->sq_lock, flags);
1092 		wqe->wr_status = SIW_WR_IDLE;
1093 		rv = siw_activate_tx(qp);
1094 		spin_unlock_irqrestore(&qp->sq_lock, flags);
1095 
1096 		if (rv <= 0)
1097 			goto done;
1098 
1099 		goto next_wqe;
1100 
1101 	} else if (rv == -EAGAIN) {
1102 		siw_dbg_qp(qp, "sq paused: hd/tr %d of %d, data %d\n",
1103 			   qp->tx_ctx.ctrl_sent, qp->tx_ctx.ctrl_len,
1104 			   qp->tx_ctx.bytes_unsent);
1105 		rv = 0;
1106 		goto done;
1107 	} else if (rv == -EINPROGRESS) {
1108 		rv = siw_sq_start(qp);
1109 		goto done;
1110 	} else {
1111 		/*
1112 		 * WQE processing failed.
1113 		 * Verbs 8.3.2:
1114 		 * o It turns any WQE into a signalled WQE.
1115 		 * o Local catastrophic error must be surfaced
1116 		 * o QP must be moved into Terminate state: done by code
1117 		 *   doing socket state change processing
1118 		 *
1119 		 * o TODO: Termination message must be sent.
1120 		 * o TODO: Implement more precise work completion errors,
1121 		 *         see enum ib_wc_status in ib_verbs.h
1122 		 */
1123 		siw_dbg_qp(qp, "wqe type %d processing failed: %d\n",
1124 			   tx_type(wqe), rv);
1125 
1126 		spin_lock_irqsave(&qp->sq_lock, flags);
1127 		/*
1128 		 * RREQ may have already been completed by inbound RRESP!
1129 		 */
1130 		if ((tx_type == SIW_OP_READ ||
1131 		     tx_type == SIW_OP_READ_LOCAL_INV) && qp->attrs.orq_size) {
1132 			/* Cleanup pending entry in ORQ */
1133 			qp->orq_put--;
1134 			qp->orq[qp->orq_put % qp->attrs.orq_size].flags = 0;
1135 		}
1136 		spin_unlock_irqrestore(&qp->sq_lock, flags);
1137 		/*
1138 		 * immediately suspends further TX processing
1139 		 */
1140 		if (!qp->tx_ctx.tx_suspend)
1141 			siw_qp_cm_drop(qp, 0);
1142 
1143 		switch (tx_type) {
1144 		case SIW_OP_SEND:
1145 		case SIW_OP_SEND_REMOTE_INV:
1146 		case SIW_OP_SEND_WITH_IMM:
1147 		case SIW_OP_WRITE:
1148 		case SIW_OP_READ:
1149 		case SIW_OP_READ_LOCAL_INV:
1150 			siw_wqe_put_mem(wqe, tx_type);
1151 			fallthrough;
1152 
1153 		case SIW_OP_INVAL_STAG:
1154 		case SIW_OP_REG_MR:
1155 			siw_sqe_complete(qp, &wqe->sqe, wqe->bytes,
1156 					 SIW_WC_LOC_QP_OP_ERR);
1157 
1158 			siw_qp_event(qp, IB_EVENT_QP_FATAL);
1159 
1160 			break;
1161 
1162 		case SIW_OP_READ_RESPONSE:
1163 			siw_dbg_qp(qp, "proc. read.response failed: %d\n", rv);
1164 
1165 			siw_qp_event(qp, IB_EVENT_QP_REQ_ERR);
1166 
1167 			siw_wqe_put_mem(wqe, SIW_OP_READ_RESPONSE);
1168 
1169 			break;
1170 
1171 		default:
1172 			WARN(1, "undefined WQE type %d\n", tx_type);
1173 			rv = -EINVAL;
1174 		}
1175 		wqe->wr_status = SIW_WR_IDLE;
1176 	}
1177 done:
1178 	return rv;
1179 }
1180 
1181 static void siw_sq_resume(struct siw_qp *qp)
1182 {
1183 	if (down_read_trylock(&qp->state_lock)) {
1184 		if (likely(qp->attrs.state == SIW_QP_STATE_RTS &&
1185 			   !qp->tx_ctx.tx_suspend)) {
1186 			int rv = siw_qp_sq_process(qp);
1187 
1188 			up_read(&qp->state_lock);
1189 
1190 			if (unlikely(rv < 0)) {
1191 				siw_dbg_qp(qp, "SQ task failed: err %d\n", rv);
1192 
1193 				if (!qp->tx_ctx.tx_suspend)
1194 					siw_qp_cm_drop(qp, 0);
1195 			}
1196 		} else {
1197 			up_read(&qp->state_lock);
1198 		}
1199 	} else {
1200 		siw_dbg_qp(qp, "Resume SQ while QP locked\n");
1201 	}
1202 	siw_qp_put(qp);
1203 }
1204 
1205 struct tx_task_t {
1206 	struct llist_head active;
1207 	wait_queue_head_t waiting;
1208 };
1209 
1210 static DEFINE_PER_CPU(struct tx_task_t, siw_tx_task_g);
1211 
1212 void siw_stop_tx_thread(int nr_cpu)
1213 {
1214 	kthread_stop(siw_tx_thread[nr_cpu]);
1215 	wake_up(&per_cpu(siw_tx_task_g, nr_cpu).waiting);
1216 }
1217 
1218 int siw_run_sq(void *data)
1219 {
1220 	const int nr_cpu = (unsigned int)(long)data;
1221 	struct llist_node *active;
1222 	struct siw_qp *qp;
1223 	struct tx_task_t *tx_task = &per_cpu(siw_tx_task_g, nr_cpu);
1224 
1225 	init_llist_head(&tx_task->active);
1226 	init_waitqueue_head(&tx_task->waiting);
1227 
1228 	while (1) {
1229 		struct llist_node *fifo_list = NULL;
1230 
1231 		wait_event_interruptible(tx_task->waiting,
1232 					 !llist_empty(&tx_task->active) ||
1233 						 kthread_should_stop());
1234 
1235 		if (kthread_should_stop())
1236 			break;
1237 
1238 		active = llist_del_all(&tx_task->active);
1239 		/*
1240 		 * llist_del_all returns a list with newest entry first.
1241 		 * Re-order list for fairness among QP's.
1242 		 */
1243 		while (active) {
1244 			struct llist_node *tmp = active;
1245 
1246 			active = llist_next(active);
1247 			tmp->next = fifo_list;
1248 			fifo_list = tmp;
1249 		}
1250 		while (fifo_list) {
1251 			qp = container_of(fifo_list, struct siw_qp, tx_list);
1252 			fifo_list = llist_next(fifo_list);
1253 			qp->tx_list.next = NULL;
1254 
1255 			siw_sq_resume(qp);
1256 		}
1257 	}
1258 	active = llist_del_all(&tx_task->active);
1259 	if (active) {
1260 		llist_for_each_entry(qp, active, tx_list) {
1261 			qp->tx_list.next = NULL;
1262 			siw_sq_resume(qp);
1263 		}
1264 	}
1265 	return 0;
1266 }
1267 
1268 int siw_sq_start(struct siw_qp *qp)
1269 {
1270 	if (tx_wqe(qp)->wr_status == SIW_WR_IDLE)
1271 		return 0;
1272 
1273 	if (unlikely(!cpu_online(qp->tx_cpu))) {
1274 		siw_put_tx_cpu(qp->tx_cpu);
1275 		qp->tx_cpu = siw_get_tx_cpu(qp->sdev);
1276 		if (qp->tx_cpu < 0) {
1277 			pr_warn("siw: no tx cpu available\n");
1278 
1279 			return -EIO;
1280 		}
1281 	}
1282 	siw_qp_get(qp);
1283 
1284 	llist_add(&qp->tx_list, &per_cpu(siw_tx_task_g, qp->tx_cpu).active);
1285 
1286 	wake_up(&per_cpu(siw_tx_task_g, qp->tx_cpu).waiting);
1287 
1288 	return 0;
1289 }
1290