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