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 do_tcp_sendpages.
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 sock *sk = s->sk;
327 	int i = 0, rv = 0, sent = 0,
328 	    flags = MSG_MORE | MSG_DONTWAIT | MSG_SENDPAGE_NOTLAST;
329 
330 	while (size) {
331 		size_t bytes = min_t(size_t, PAGE_SIZE - offset, size);
332 
333 		if (size + offset <= PAGE_SIZE)
334 			flags = MSG_MORE | MSG_DONTWAIT;
335 
336 		tcp_rate_check_app_limited(sk);
337 try_page_again:
338 		lock_sock(sk);
339 		rv = do_tcp_sendpages(sk, page[i], offset, bytes, flags);
340 		release_sock(sk);
341 
342 		if (rv > 0) {
343 			size -= rv;
344 			sent += rv;
345 			if (rv != bytes) {
346 				offset += rv;
347 				bytes -= rv;
348 				goto try_page_again;
349 			}
350 			offset = 0;
351 		} else {
352 			if (rv == -EAGAIN || rv == 0)
353 				break;
354 			return rv;
355 		}
356 		i++;
357 	}
358 	return sent;
359 }
360 
361 /*
362  * siw_0copy_tx()
363  *
364  * Pushes list of pages to TCP socket. If pages from multiple
365  * SGE's, all referenced pages of each SGE are pushed in one
366  * shot.
367  */
368 static int siw_0copy_tx(struct socket *s, struct page **page,
369 			struct siw_sge *sge, unsigned int offset,
370 			unsigned int size)
371 {
372 	int i = 0, sent = 0, rv;
373 	int sge_bytes = min(sge->length - offset, size);
374 
375 	offset = (sge->laddr + offset) & ~PAGE_MASK;
376 
377 	while (sent != size) {
378 		rv = siw_tcp_sendpages(s, &page[i], offset, sge_bytes);
379 		if (rv >= 0) {
380 			sent += rv;
381 			if (size == sent || sge_bytes > rv)
382 				break;
383 
384 			i += PAGE_ALIGN(sge_bytes + offset) >> PAGE_SHIFT;
385 			sge++;
386 			sge_bytes = min(sge->length, size - sent);
387 			offset = sge->laddr & ~PAGE_MASK;
388 		} else {
389 			sent = rv;
390 			break;
391 		}
392 	}
393 	return sent;
394 }
395 
396 #define MAX_TRAILER (MPA_CRC_SIZE + 4)
397 
398 static void siw_unmap_pages(struct kvec *iov, unsigned long kmap_mask, int len)
399 {
400 	int i;
401 
402 	/*
403 	 * Work backwards through the array to honor the kmap_local_page()
404 	 * ordering requirements.
405 	 */
406 	for (i = (len-1); i >= 0; i--) {
407 		if (kmap_mask & BIT(i)) {
408 			unsigned long addr = (unsigned long)iov[i].iov_base;
409 
410 			kunmap_local((void *)(addr & PAGE_MASK));
411 		}
412 	}
413 }
414 
415 /*
416  * siw_tx_hdt() tries to push a complete packet to TCP where all
417  * packet fragments are referenced by the elements of one iovec.
418  * For the data portion, each involved page must be referenced by
419  * one extra element. All sge's data can be non-aligned to page
420  * boundaries. Two more elements are referencing iWARP header
421  * and trailer:
422  * MAX_ARRAY = 64KB/PAGE_SIZE + 1 + (2 * (SIW_MAX_SGE - 1) + HDR + TRL
423  */
424 #define MAX_ARRAY ((0xffff / PAGE_SIZE) + 1 + (2 * (SIW_MAX_SGE - 1) + 2))
425 
426 /*
427  * Write out iov referencing hdr, data and trailer of current FPDU.
428  * Update transmit state dependent on write return status
429  */
430 static int siw_tx_hdt(struct siw_iwarp_tx *c_tx, struct socket *s)
431 {
432 	struct siw_wqe *wqe = &c_tx->wqe_active;
433 	struct siw_sge *sge = &wqe->sqe.sge[c_tx->sge_idx];
434 	struct kvec iov[MAX_ARRAY];
435 	struct page *page_array[MAX_ARRAY];
436 	struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_EOR };
437 
438 	int seg = 0, do_crc = c_tx->do_crc, is_kva = 0, rv;
439 	unsigned int data_len = c_tx->bytes_unsent, hdr_len = 0, trl_len = 0,
440 		     sge_off = c_tx->sge_off, sge_idx = c_tx->sge_idx,
441 		     pbl_idx = c_tx->pbl_idx;
442 	unsigned long kmap_mask = 0L;
443 
444 	if (c_tx->state == SIW_SEND_HDR) {
445 		if (c_tx->use_sendpage) {
446 			rv = siw_tx_ctrl(c_tx, s, MSG_DONTWAIT | MSG_MORE);
447 			if (rv)
448 				goto done;
449 
450 			c_tx->state = SIW_SEND_DATA;
451 		} else {
452 			iov[0].iov_base =
453 				(char *)&c_tx->pkt.ctrl + c_tx->ctrl_sent;
454 			iov[0].iov_len = hdr_len =
455 				c_tx->ctrl_len - c_tx->ctrl_sent;
456 			seg = 1;
457 		}
458 	}
459 
460 	wqe->processed += data_len;
461 
462 	while (data_len) { /* walk the list of SGE's */
463 		unsigned int sge_len = min(sge->length - sge_off, data_len);
464 		unsigned int fp_off = (sge->laddr + sge_off) & ~PAGE_MASK;
465 		struct siw_mem *mem;
466 
467 		if (!(tx_flags(wqe) & SIW_WQE_INLINE)) {
468 			mem = wqe->mem[sge_idx];
469 			is_kva = mem->mem_obj == NULL ? 1 : 0;
470 		} else {
471 			is_kva = 1;
472 		}
473 		if (is_kva && !c_tx->use_sendpage) {
474 			/*
475 			 * tx from kernel virtual address: either inline data
476 			 * or memory region with assigned kernel buffer
477 			 */
478 			iov[seg].iov_base =
479 				ib_virt_dma_to_ptr(sge->laddr + sge_off);
480 			iov[seg].iov_len = sge_len;
481 
482 			if (do_crc)
483 				crypto_shash_update(c_tx->mpa_crc_hd,
484 						    iov[seg].iov_base,
485 						    sge_len);
486 			sge_off += sge_len;
487 			data_len -= sge_len;
488 			seg++;
489 			goto sge_done;
490 		}
491 
492 		while (sge_len) {
493 			size_t plen = min((int)PAGE_SIZE - fp_off, sge_len);
494 			void *kaddr;
495 
496 			if (!is_kva) {
497 				struct page *p;
498 
499 				if (mem->is_pbl)
500 					p = siw_get_pblpage(
501 						mem, sge->laddr + sge_off,
502 						&pbl_idx);
503 				else
504 					p = siw_get_upage(mem->umem,
505 							  sge->laddr + sge_off);
506 				if (unlikely(!p)) {
507 					siw_unmap_pages(iov, kmap_mask, seg);
508 					wqe->processed -= c_tx->bytes_unsent;
509 					rv = -EFAULT;
510 					goto done_crc;
511 				}
512 				page_array[seg] = p;
513 
514 				if (!c_tx->use_sendpage) {
515 					void *kaddr = kmap_local_page(p);
516 
517 					/* Remember for later kunmap() */
518 					kmap_mask |= BIT(seg);
519 					iov[seg].iov_base = kaddr + fp_off;
520 					iov[seg].iov_len = plen;
521 
522 					if (do_crc)
523 						crypto_shash_update(
524 							c_tx->mpa_crc_hd,
525 							iov[seg].iov_base,
526 							plen);
527 				} else if (do_crc) {
528 					kaddr = kmap_local_page(p);
529 					crypto_shash_update(c_tx->mpa_crc_hd,
530 							    kaddr + fp_off,
531 							    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 					crypto_shash_update(
544 						c_tx->mpa_crc_hd,
545 						ib_virt_dma_to_ptr(va),
546 						plen);
547 			}
548 
549 			sge_len -= plen;
550 			sge_off += plen;
551 			data_len -= plen;
552 			fp_off = 0;
553 
554 			if (++seg >= (int)MAX_ARRAY) {
555 				siw_dbg_qp(tx_qp(c_tx), "to many fragments\n");
556 				siw_unmap_pages(iov, kmap_mask, seg-1);
557 				wqe->processed -= c_tx->bytes_unsent;
558 				rv = -EMSGSIZE;
559 				goto done_crc;
560 			}
561 		}
562 sge_done:
563 		/* Update SGE variables at end of SGE */
564 		if (sge_off == sge->length &&
565 		    (data_len != 0 || wqe->processed < wqe->bytes)) {
566 			sge_idx++;
567 			sge++;
568 			sge_off = 0;
569 		}
570 	}
571 	/* trailer */
572 	if (likely(c_tx->state != SIW_SEND_TRAILER)) {
573 		iov[seg].iov_base = &c_tx->trailer.pad[4 - c_tx->pad];
574 		iov[seg].iov_len = trl_len = MAX_TRAILER - (4 - c_tx->pad);
575 	} else {
576 		iov[seg].iov_base = &c_tx->trailer.pad[c_tx->ctrl_sent];
577 		iov[seg].iov_len = trl_len = MAX_TRAILER - c_tx->ctrl_sent;
578 	}
579 
580 	if (c_tx->pad) {
581 		*(u32 *)c_tx->trailer.pad = 0;
582 		if (do_crc)
583 			crypto_shash_update(c_tx->mpa_crc_hd,
584 				(u8 *)&c_tx->trailer.crc - c_tx->pad,
585 				c_tx->pad);
586 	}
587 	if (!c_tx->mpa_crc_hd)
588 		c_tx->trailer.crc = 0;
589 	else if (do_crc)
590 		crypto_shash_final(c_tx->mpa_crc_hd, (u8 *)&c_tx->trailer.crc);
591 
592 	data_len = c_tx->bytes_unsent;
593 
594 	if (c_tx->use_sendpage) {
595 		rv = siw_0copy_tx(s, page_array, &wqe->sqe.sge[c_tx->sge_idx],
596 				  c_tx->sge_off, data_len);
597 		if (rv == data_len) {
598 			rv = kernel_sendmsg(s, &msg, &iov[seg], 1, trl_len);
599 			if (rv > 0)
600 				rv += data_len;
601 			else
602 				rv = data_len;
603 		}
604 	} else {
605 		rv = kernel_sendmsg(s, &msg, iov, seg + 1,
606 				    hdr_len + data_len + trl_len);
607 		siw_unmap_pages(iov, kmap_mask, seg);
608 	}
609 	if (rv < (int)hdr_len) {
610 		/* Not even complete hdr pushed or negative rv */
611 		wqe->processed -= data_len;
612 		if (rv >= 0) {
613 			c_tx->ctrl_sent += rv;
614 			rv = -EAGAIN;
615 		}
616 		goto done_crc;
617 	}
618 	rv -= hdr_len;
619 
620 	if (rv >= (int)data_len) {
621 		/* all user data pushed to TCP or no data to push */
622 		if (data_len > 0 && wqe->processed < wqe->bytes) {
623 			/* Save the current state for next tx */
624 			c_tx->sge_idx = sge_idx;
625 			c_tx->sge_off = sge_off;
626 			c_tx->pbl_idx = pbl_idx;
627 		}
628 		rv -= data_len;
629 
630 		if (rv == trl_len) /* all pushed */
631 			rv = 0;
632 		else {
633 			c_tx->state = SIW_SEND_TRAILER;
634 			c_tx->ctrl_len = MAX_TRAILER;
635 			c_tx->ctrl_sent = rv + 4 - c_tx->pad;
636 			c_tx->bytes_unsent = 0;
637 			rv = -EAGAIN;
638 		}
639 
640 	} else if (data_len > 0) {
641 		/* Maybe some user data pushed to TCP */
642 		c_tx->state = SIW_SEND_DATA;
643 		wqe->processed -= data_len - rv;
644 
645 		if (rv) {
646 			/*
647 			 * Some bytes out. Recompute tx state based
648 			 * on old state and bytes pushed
649 			 */
650 			unsigned int sge_unsent;
651 
652 			c_tx->bytes_unsent -= rv;
653 			sge = &wqe->sqe.sge[c_tx->sge_idx];
654 			sge_unsent = sge->length - c_tx->sge_off;
655 
656 			while (sge_unsent <= rv) {
657 				rv -= sge_unsent;
658 				c_tx->sge_idx++;
659 				c_tx->sge_off = 0;
660 				sge++;
661 				sge_unsent = sge->length;
662 			}
663 			c_tx->sge_off += rv;
664 		}
665 		rv = -EAGAIN;
666 	}
667 done_crc:
668 	c_tx->do_crc = 0;
669 done:
670 	return rv;
671 }
672 
673 static void siw_update_tcpseg(struct siw_iwarp_tx *c_tx,
674 				     struct socket *s)
675 {
676 	struct tcp_sock *tp = tcp_sk(s->sk);
677 
678 	if (tp->gso_segs) {
679 		if (c_tx->gso_seg_limit == 0)
680 			c_tx->tcp_seglen = tp->mss_cache * tp->gso_segs;
681 		else
682 			c_tx->tcp_seglen =
683 				tp->mss_cache *
684 				min_t(u16, c_tx->gso_seg_limit, tp->gso_segs);
685 	} else {
686 		c_tx->tcp_seglen = tp->mss_cache;
687 	}
688 	/* Loopback may give odd numbers */
689 	c_tx->tcp_seglen &= 0xfffffff8;
690 }
691 
692 /*
693  * siw_prepare_fpdu()
694  *
695  * Prepares transmit context to send out one FPDU if FPDU will contain
696  * user data and user data are not immediate data.
697  * Computes maximum FPDU length to fill up TCP MSS if possible.
698  *
699  * @qp:		QP from which to transmit
700  * @wqe:	Current WQE causing transmission
701  *
702  * TODO: Take into account real available sendspace on socket
703  *       to avoid header misalignment due to send pausing within
704  *       fpdu transmission
705  */
706 static void siw_prepare_fpdu(struct siw_qp *qp, struct siw_wqe *wqe)
707 {
708 	struct siw_iwarp_tx *c_tx = &qp->tx_ctx;
709 	int data_len;
710 
711 	c_tx->ctrl_len =
712 		iwarp_pktinfo[__rdmap_get_opcode(&c_tx->pkt.ctrl)].hdr_len;
713 	c_tx->ctrl_sent = 0;
714 
715 	/*
716 	 * Update target buffer offset if any
717 	 */
718 	if (!(c_tx->pkt.ctrl.ddp_rdmap_ctrl & DDP_FLAG_TAGGED))
719 		/* Untagged message */
720 		c_tx->pkt.c_untagged.ddp_mo = cpu_to_be32(wqe->processed);
721 	else /* Tagged message */
722 		c_tx->pkt.c_tagged.ddp_to =
723 			cpu_to_be64(wqe->sqe.raddr + wqe->processed);
724 
725 	data_len = wqe->bytes - wqe->processed;
726 	if (data_len + c_tx->ctrl_len + MPA_CRC_SIZE > c_tx->tcp_seglen) {
727 		/* Trim DDP payload to fit into current TCP segment */
728 		data_len = c_tx->tcp_seglen - (c_tx->ctrl_len + MPA_CRC_SIZE);
729 		c_tx->pkt.ctrl.ddp_rdmap_ctrl &= ~DDP_FLAG_LAST;
730 		c_tx->pad = 0;
731 	} else {
732 		c_tx->pkt.ctrl.ddp_rdmap_ctrl |= DDP_FLAG_LAST;
733 		c_tx->pad = -data_len & 0x3;
734 	}
735 	c_tx->bytes_unsent = data_len;
736 
737 	c_tx->pkt.ctrl.mpa_len =
738 		htons(c_tx->ctrl_len + data_len - MPA_HDR_SIZE);
739 
740 	/*
741 	 * Init MPA CRC computation
742 	 */
743 	if (c_tx->mpa_crc_hd) {
744 		crypto_shash_init(c_tx->mpa_crc_hd);
745 		crypto_shash_update(c_tx->mpa_crc_hd, (u8 *)&c_tx->pkt,
746 				    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_sq_work_handler() 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_sq_work_handler(), if send space becomes
1012  * available again.
1013  *
1014  * Must be called with the QP state read-locked.
1015  *
1016  * Note:
1017  * An outbound RREQ can be satisfied by the corresponding RRESP
1018  * _before_ it gets assigned to the ORQ. This happens regularly
1019  * in RDMA READ via loopback case. Since both outbound RREQ and
1020  * inbound RRESP can be handled by the same CPU, locking the ORQ
1021  * is dead-lock prone and thus not an option. With that, the
1022  * RREQ gets assigned to the ORQ _before_ being sent - see
1023  * siw_activate_tx() - and pulled back in case of send failure.
1024  */
1025 int siw_qp_sq_process(struct siw_qp *qp)
1026 {
1027 	struct siw_wqe *wqe = tx_wqe(qp);
1028 	enum siw_opcode tx_type;
1029 	unsigned long flags;
1030 	int rv = 0;
1031 
1032 	siw_dbg_qp(qp, "enter for type %d\n", tx_type(wqe));
1033 
1034 next_wqe:
1035 	/*
1036 	 * Stop QP processing if SQ state changed
1037 	 */
1038 	if (unlikely(qp->tx_ctx.tx_suspend)) {
1039 		siw_dbg_qp(qp, "tx suspended\n");
1040 		goto done;
1041 	}
1042 	tx_type = tx_type(wqe);
1043 
1044 	if (tx_type <= SIW_OP_READ_RESPONSE)
1045 		rv = siw_qp_sq_proc_tx(qp, wqe);
1046 	else
1047 		rv = siw_qp_sq_proc_local(qp, wqe);
1048 
1049 	if (!rv) {
1050 		/*
1051 		 * WQE processing done
1052 		 */
1053 		switch (tx_type) {
1054 		case SIW_OP_SEND:
1055 		case SIW_OP_SEND_REMOTE_INV:
1056 		case SIW_OP_WRITE:
1057 			siw_wqe_put_mem(wqe, tx_type);
1058 			fallthrough;
1059 
1060 		case SIW_OP_INVAL_STAG:
1061 		case SIW_OP_REG_MR:
1062 			if (tx_flags(wqe) & SIW_WQE_SIGNALLED)
1063 				siw_sqe_complete(qp, &wqe->sqe, wqe->bytes,
1064 						 SIW_WC_SUCCESS);
1065 			break;
1066 
1067 		case SIW_OP_READ:
1068 		case SIW_OP_READ_LOCAL_INV:
1069 			/*
1070 			 * already enqueued to ORQ queue
1071 			 */
1072 			break;
1073 
1074 		case SIW_OP_READ_RESPONSE:
1075 			siw_wqe_put_mem(wqe, tx_type);
1076 			break;
1077 
1078 		default:
1079 			WARN(1, "undefined WQE type %d\n", tx_type);
1080 			rv = -EINVAL;
1081 			goto done;
1082 		}
1083 
1084 		spin_lock_irqsave(&qp->sq_lock, flags);
1085 		wqe->wr_status = SIW_WR_IDLE;
1086 		rv = siw_activate_tx(qp);
1087 		spin_unlock_irqrestore(&qp->sq_lock, flags);
1088 
1089 		if (rv <= 0)
1090 			goto done;
1091 
1092 		goto next_wqe;
1093 
1094 	} else if (rv == -EAGAIN) {
1095 		siw_dbg_qp(qp, "sq paused: hd/tr %d of %d, data %d\n",
1096 			   qp->tx_ctx.ctrl_sent, qp->tx_ctx.ctrl_len,
1097 			   qp->tx_ctx.bytes_unsent);
1098 		rv = 0;
1099 		goto done;
1100 	} else if (rv == -EINPROGRESS) {
1101 		rv = siw_sq_start(qp);
1102 		goto done;
1103 	} else {
1104 		/*
1105 		 * WQE processing failed.
1106 		 * Verbs 8.3.2:
1107 		 * o It turns any WQE into a signalled WQE.
1108 		 * o Local catastrophic error must be surfaced
1109 		 * o QP must be moved into Terminate state: done by code
1110 		 *   doing socket state change processing
1111 		 *
1112 		 * o TODO: Termination message must be sent.
1113 		 * o TODO: Implement more precise work completion errors,
1114 		 *         see enum ib_wc_status in ib_verbs.h
1115 		 */
1116 		siw_dbg_qp(qp, "wqe type %d processing failed: %d\n",
1117 			   tx_type(wqe), rv);
1118 
1119 		spin_lock_irqsave(&qp->sq_lock, flags);
1120 		/*
1121 		 * RREQ may have already been completed by inbound RRESP!
1122 		 */
1123 		if ((tx_type == SIW_OP_READ ||
1124 		     tx_type == SIW_OP_READ_LOCAL_INV) && qp->attrs.orq_size) {
1125 			/* Cleanup pending entry in ORQ */
1126 			qp->orq_put--;
1127 			qp->orq[qp->orq_put % qp->attrs.orq_size].flags = 0;
1128 		}
1129 		spin_unlock_irqrestore(&qp->sq_lock, flags);
1130 		/*
1131 		 * immediately suspends further TX processing
1132 		 */
1133 		if (!qp->tx_ctx.tx_suspend)
1134 			siw_qp_cm_drop(qp, 0);
1135 
1136 		switch (tx_type) {
1137 		case SIW_OP_SEND:
1138 		case SIW_OP_SEND_REMOTE_INV:
1139 		case SIW_OP_SEND_WITH_IMM:
1140 		case SIW_OP_WRITE:
1141 		case SIW_OP_READ:
1142 		case SIW_OP_READ_LOCAL_INV:
1143 			siw_wqe_put_mem(wqe, tx_type);
1144 			fallthrough;
1145 
1146 		case SIW_OP_INVAL_STAG:
1147 		case SIW_OP_REG_MR:
1148 			siw_sqe_complete(qp, &wqe->sqe, wqe->bytes,
1149 					 SIW_WC_LOC_QP_OP_ERR);
1150 
1151 			siw_qp_event(qp, IB_EVENT_QP_FATAL);
1152 
1153 			break;
1154 
1155 		case SIW_OP_READ_RESPONSE:
1156 			siw_dbg_qp(qp, "proc. read.response failed: %d\n", rv);
1157 
1158 			siw_qp_event(qp, IB_EVENT_QP_REQ_ERR);
1159 
1160 			siw_wqe_put_mem(wqe, SIW_OP_READ_RESPONSE);
1161 
1162 			break;
1163 
1164 		default:
1165 			WARN(1, "undefined WQE type %d\n", tx_type);
1166 			rv = -EINVAL;
1167 		}
1168 		wqe->wr_status = SIW_WR_IDLE;
1169 	}
1170 done:
1171 	return rv;
1172 }
1173 
1174 static void siw_sq_resume(struct siw_qp *qp)
1175 {
1176 	if (down_read_trylock(&qp->state_lock)) {
1177 		if (likely(qp->attrs.state == SIW_QP_STATE_RTS &&
1178 			   !qp->tx_ctx.tx_suspend)) {
1179 			int rv = siw_qp_sq_process(qp);
1180 
1181 			up_read(&qp->state_lock);
1182 
1183 			if (unlikely(rv < 0)) {
1184 				siw_dbg_qp(qp, "SQ task failed: err %d\n", rv);
1185 
1186 				if (!qp->tx_ctx.tx_suspend)
1187 					siw_qp_cm_drop(qp, 0);
1188 			}
1189 		} else {
1190 			up_read(&qp->state_lock);
1191 		}
1192 	} else {
1193 		siw_dbg_qp(qp, "Resume SQ while QP locked\n");
1194 	}
1195 	siw_qp_put(qp);
1196 }
1197 
1198 struct tx_task_t {
1199 	struct llist_head active;
1200 	wait_queue_head_t waiting;
1201 };
1202 
1203 static DEFINE_PER_CPU(struct tx_task_t, siw_tx_task_g);
1204 
1205 void siw_stop_tx_thread(int nr_cpu)
1206 {
1207 	kthread_stop(siw_tx_thread[nr_cpu]);
1208 	wake_up(&per_cpu(siw_tx_task_g, nr_cpu).waiting);
1209 }
1210 
1211 int siw_run_sq(void *data)
1212 {
1213 	const int nr_cpu = (unsigned int)(long)data;
1214 	struct llist_node *active;
1215 	struct siw_qp *qp;
1216 	struct tx_task_t *tx_task = &per_cpu(siw_tx_task_g, nr_cpu);
1217 
1218 	init_llist_head(&tx_task->active);
1219 	init_waitqueue_head(&tx_task->waiting);
1220 
1221 	while (1) {
1222 		struct llist_node *fifo_list = NULL;
1223 
1224 		wait_event_interruptible(tx_task->waiting,
1225 					 !llist_empty(&tx_task->active) ||
1226 						 kthread_should_stop());
1227 
1228 		if (kthread_should_stop())
1229 			break;
1230 
1231 		active = llist_del_all(&tx_task->active);
1232 		/*
1233 		 * llist_del_all returns a list with newest entry first.
1234 		 * Re-order list for fairness among QP's.
1235 		 */
1236 		while (active) {
1237 			struct llist_node *tmp = active;
1238 
1239 			active = llist_next(active);
1240 			tmp->next = fifo_list;
1241 			fifo_list = tmp;
1242 		}
1243 		while (fifo_list) {
1244 			qp = container_of(fifo_list, struct siw_qp, tx_list);
1245 			fifo_list = llist_next(fifo_list);
1246 			qp->tx_list.next = NULL;
1247 
1248 			siw_sq_resume(qp);
1249 		}
1250 	}
1251 	active = llist_del_all(&tx_task->active);
1252 	if (active) {
1253 		llist_for_each_entry(qp, active, tx_list) {
1254 			qp->tx_list.next = NULL;
1255 			siw_sq_resume(qp);
1256 		}
1257 	}
1258 	return 0;
1259 }
1260 
1261 int siw_sq_start(struct siw_qp *qp)
1262 {
1263 	if (tx_wqe(qp)->wr_status == SIW_WR_IDLE)
1264 		return 0;
1265 
1266 	if (unlikely(!cpu_online(qp->tx_cpu))) {
1267 		siw_put_tx_cpu(qp->tx_cpu);
1268 		qp->tx_cpu = siw_get_tx_cpu(qp->sdev);
1269 		if (qp->tx_cpu < 0) {
1270 			pr_warn("siw: no tx cpu available\n");
1271 
1272 			return -EIO;
1273 		}
1274 	}
1275 	siw_qp_get(qp);
1276 
1277 	llist_add(&qp->tx_list, &per_cpu(siw_tx_task_g, qp->tx_cpu).active);
1278 
1279 	wake_up(&per_cpu(siw_tx_task_g, qp->tx_cpu).waiting);
1280 
1281 	return 0;
1282 }
1283