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 
siw_get_pblpage(struct siw_mem * mem,u64 addr,int * idx)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  */
siw_try_1seg(struct siw_iwarp_tx * c_tx,void * paddr)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  */
siw_qp_prepare_tx(struct siw_iwarp_tx * c_tx)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  */
siw_tx_ctrl(struct siw_iwarp_tx * c_tx,struct socket * s,int flags)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  */
siw_tcp_sendpages(struct socket * s,struct page ** page,int offset,size_t size)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  */
siw_0copy_tx(struct socket * s,struct page ** page,struct siw_sge * sge,unsigned int offset,unsigned int size)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 
siw_unmap_pages(struct kvec * iov,unsigned long kmap_mask,int len)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  */
siw_tx_hdt(struct siw_iwarp_tx * c_tx,struct socket * s)438 static int siw_tx_hdt(struct siw_iwarp_tx *c_tx, struct socket *s)
439 {
440 	struct siw_wqe *wqe = &c_tx->wqe_active;
441 	struct siw_sge *sge = &wqe->sqe.sge[c_tx->sge_idx];
442 	struct kvec iov[MAX_ARRAY];
443 	struct page *page_array[MAX_ARRAY];
444 	struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_EOR };
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 				crypto_shash_update(c_tx->mpa_crc_hd,
492 						    iov[seg].iov_base,
493 						    sge_len);
494 			sge_off += sge_len;
495 			data_len -= sge_len;
496 			seg++;
497 			goto sge_done;
498 		}
499 
500 		while (sge_len) {
501 			size_t plen = min((int)PAGE_SIZE - fp_off, sge_len);
502 			void *kaddr;
503 
504 			if (!is_kva) {
505 				struct page *p;
506 
507 				if (mem->is_pbl)
508 					p = siw_get_pblpage(
509 						mem, sge->laddr + sge_off,
510 						&pbl_idx);
511 				else
512 					p = siw_get_upage(mem->umem,
513 							  sge->laddr + sge_off);
514 				if (unlikely(!p)) {
515 					siw_unmap_pages(iov, kmap_mask, seg);
516 					wqe->processed -= c_tx->bytes_unsent;
517 					rv = -EFAULT;
518 					goto done_crc;
519 				}
520 				page_array[seg] = p;
521 
522 				if (!c_tx->use_sendpage) {
523 					void *kaddr = kmap_local_page(p);
524 
525 					/* Remember for later kunmap() */
526 					kmap_mask |= BIT(seg);
527 					iov[seg].iov_base = kaddr + fp_off;
528 					iov[seg].iov_len = plen;
529 
530 					if (do_crc)
531 						crypto_shash_update(
532 							c_tx->mpa_crc_hd,
533 							iov[seg].iov_base,
534 							plen);
535 				} else if (do_crc) {
536 					kaddr = kmap_local_page(p);
537 					crypto_shash_update(c_tx->mpa_crc_hd,
538 							    kaddr + fp_off,
539 							    plen);
540 					kunmap_local(kaddr);
541 				}
542 			} else {
543 				/*
544 				 * Cast to an uintptr_t to preserve all 64 bits
545 				 * in sge->laddr.
546 				 */
547 				u64 va = sge->laddr + sge_off;
548 
549 				page_array[seg] = ib_virt_dma_to_page(va);
550 				if (do_crc)
551 					crypto_shash_update(
552 						c_tx->mpa_crc_hd,
553 						ib_virt_dma_to_ptr(va),
554 						plen);
555 			}
556 
557 			sge_len -= plen;
558 			sge_off += plen;
559 			data_len -= plen;
560 			fp_off = 0;
561 
562 			if (++seg >= (int)MAX_ARRAY) {
563 				siw_dbg_qp(tx_qp(c_tx), "to many fragments\n");
564 				siw_unmap_pages(iov, kmap_mask, seg-1);
565 				wqe->processed -= c_tx->bytes_unsent;
566 				rv = -EMSGSIZE;
567 				goto done_crc;
568 			}
569 		}
570 sge_done:
571 		/* Update SGE variables at end of SGE */
572 		if (sge_off == sge->length &&
573 		    (data_len != 0 || wqe->processed < wqe->bytes)) {
574 			sge_idx++;
575 			sge++;
576 			sge_off = 0;
577 		}
578 	}
579 	/* trailer */
580 	if (likely(c_tx->state != SIW_SEND_TRAILER)) {
581 		iov[seg].iov_base = &c_tx->trailer.pad[4 - c_tx->pad];
582 		iov[seg].iov_len = trl_len = MAX_TRAILER - (4 - c_tx->pad);
583 	} else {
584 		iov[seg].iov_base = &c_tx->trailer.pad[c_tx->ctrl_sent];
585 		iov[seg].iov_len = trl_len = MAX_TRAILER - c_tx->ctrl_sent;
586 	}
587 
588 	if (c_tx->pad) {
589 		*(u32 *)c_tx->trailer.pad = 0;
590 		if (do_crc)
591 			crypto_shash_update(c_tx->mpa_crc_hd,
592 				(u8 *)&c_tx->trailer.crc - c_tx->pad,
593 				c_tx->pad);
594 	}
595 	if (!c_tx->mpa_crc_hd)
596 		c_tx->trailer.crc = 0;
597 	else if (do_crc)
598 		crypto_shash_final(c_tx->mpa_crc_hd, (u8 *)&c_tx->trailer.crc);
599 
600 	data_len = c_tx->bytes_unsent;
601 
602 	if (c_tx->use_sendpage) {
603 		rv = siw_0copy_tx(s, page_array, &wqe->sqe.sge[c_tx->sge_idx],
604 				  c_tx->sge_off, data_len);
605 		if (rv == data_len) {
606 			rv = kernel_sendmsg(s, &msg, &iov[seg], 1, trl_len);
607 			if (rv > 0)
608 				rv += data_len;
609 			else
610 				rv = data_len;
611 		}
612 	} else {
613 		rv = kernel_sendmsg(s, &msg, iov, seg + 1,
614 				    hdr_len + data_len + trl_len);
615 		siw_unmap_pages(iov, kmap_mask, seg);
616 	}
617 	if (rv < (int)hdr_len) {
618 		/* Not even complete hdr pushed or negative rv */
619 		wqe->processed -= data_len;
620 		if (rv >= 0) {
621 			c_tx->ctrl_sent += rv;
622 			rv = -EAGAIN;
623 		}
624 		goto done_crc;
625 	}
626 	rv -= hdr_len;
627 
628 	if (rv >= (int)data_len) {
629 		/* all user data pushed to TCP or no data to push */
630 		if (data_len > 0 && wqe->processed < wqe->bytes) {
631 			/* Save the current state for next tx */
632 			c_tx->sge_idx = sge_idx;
633 			c_tx->sge_off = sge_off;
634 			c_tx->pbl_idx = pbl_idx;
635 		}
636 		rv -= data_len;
637 
638 		if (rv == trl_len) /* all pushed */
639 			rv = 0;
640 		else {
641 			c_tx->state = SIW_SEND_TRAILER;
642 			c_tx->ctrl_len = MAX_TRAILER;
643 			c_tx->ctrl_sent = rv + 4 - c_tx->pad;
644 			c_tx->bytes_unsent = 0;
645 			rv = -EAGAIN;
646 		}
647 
648 	} else if (data_len > 0) {
649 		/* Maybe some user data pushed to TCP */
650 		c_tx->state = SIW_SEND_DATA;
651 		wqe->processed -= data_len - rv;
652 
653 		if (rv) {
654 			/*
655 			 * Some bytes out. Recompute tx state based
656 			 * on old state and bytes pushed
657 			 */
658 			unsigned int sge_unsent;
659 
660 			c_tx->bytes_unsent -= rv;
661 			sge = &wqe->sqe.sge[c_tx->sge_idx];
662 			sge_unsent = sge->length - c_tx->sge_off;
663 
664 			while (sge_unsent <= rv) {
665 				rv -= sge_unsent;
666 				c_tx->sge_idx++;
667 				c_tx->sge_off = 0;
668 				sge++;
669 				sge_unsent = sge->length;
670 			}
671 			c_tx->sge_off += rv;
672 		}
673 		rv = -EAGAIN;
674 	}
675 done_crc:
676 	c_tx->do_crc = 0;
677 done:
678 	return rv;
679 }
680 
siw_update_tcpseg(struct siw_iwarp_tx * c_tx,struct socket * s)681 static void siw_update_tcpseg(struct siw_iwarp_tx *c_tx,
682 				     struct socket *s)
683 {
684 	struct tcp_sock *tp = tcp_sk(s->sk);
685 
686 	if (tp->gso_segs) {
687 		if (c_tx->gso_seg_limit == 0)
688 			c_tx->tcp_seglen = tp->mss_cache * tp->gso_segs;
689 		else
690 			c_tx->tcp_seglen =
691 				tp->mss_cache *
692 				min_t(u16, c_tx->gso_seg_limit, tp->gso_segs);
693 	} else {
694 		c_tx->tcp_seglen = tp->mss_cache;
695 	}
696 	/* Loopback may give odd numbers */
697 	c_tx->tcp_seglen &= 0xfffffff8;
698 }
699 
700 /*
701  * siw_prepare_fpdu()
702  *
703  * Prepares transmit context to send out one FPDU if FPDU will contain
704  * user data and user data are not immediate data.
705  * Computes maximum FPDU length to fill up TCP MSS if possible.
706  *
707  * @qp:		QP from which to transmit
708  * @wqe:	Current WQE causing transmission
709  *
710  * TODO: Take into account real available sendspace on socket
711  *       to avoid header misalignment due to send pausing within
712  *       fpdu transmission
713  */
siw_prepare_fpdu(struct siw_qp * qp,struct siw_wqe * wqe)714 static void siw_prepare_fpdu(struct siw_qp *qp, struct siw_wqe *wqe)
715 {
716 	struct siw_iwarp_tx *c_tx = &qp->tx_ctx;
717 	int data_len;
718 
719 	c_tx->ctrl_len =
720 		iwarp_pktinfo[__rdmap_get_opcode(&c_tx->pkt.ctrl)].hdr_len;
721 	c_tx->ctrl_sent = 0;
722 
723 	/*
724 	 * Update target buffer offset if any
725 	 */
726 	if (!(c_tx->pkt.ctrl.ddp_rdmap_ctrl & DDP_FLAG_TAGGED))
727 		/* Untagged message */
728 		c_tx->pkt.c_untagged.ddp_mo = cpu_to_be32(wqe->processed);
729 	else /* Tagged message */
730 		c_tx->pkt.c_tagged.ddp_to =
731 			cpu_to_be64(wqe->sqe.raddr + wqe->processed);
732 
733 	data_len = wqe->bytes - wqe->processed;
734 	if (data_len + c_tx->ctrl_len + MPA_CRC_SIZE > c_tx->tcp_seglen) {
735 		/* Trim DDP payload to fit into current TCP segment */
736 		data_len = c_tx->tcp_seglen - (c_tx->ctrl_len + MPA_CRC_SIZE);
737 		c_tx->pkt.ctrl.ddp_rdmap_ctrl &= ~DDP_FLAG_LAST;
738 		c_tx->pad = 0;
739 	} else {
740 		c_tx->pkt.ctrl.ddp_rdmap_ctrl |= DDP_FLAG_LAST;
741 		c_tx->pad = -data_len & 0x3;
742 	}
743 	c_tx->bytes_unsent = data_len;
744 
745 	c_tx->pkt.ctrl.mpa_len =
746 		htons(c_tx->ctrl_len + data_len - MPA_HDR_SIZE);
747 
748 	/*
749 	 * Init MPA CRC computation
750 	 */
751 	if (c_tx->mpa_crc_hd) {
752 		crypto_shash_init(c_tx->mpa_crc_hd);
753 		crypto_shash_update(c_tx->mpa_crc_hd, (u8 *)&c_tx->pkt,
754 				    c_tx->ctrl_len);
755 		c_tx->do_crc = 1;
756 	}
757 }
758 
759 /*
760  * siw_check_sgl_tx()
761  *
762  * Check permissions for a list of SGE's (SGL).
763  * A successful check will have all memory referenced
764  * for transmission resolved and assigned to the WQE.
765  *
766  * @pd:		Protection Domain SGL should belong to
767  * @wqe:	WQE to be checked
768  * @perms:	requested access permissions
769  *
770  */
771 
siw_check_sgl_tx(struct ib_pd * pd,struct siw_wqe * wqe,enum ib_access_flags perms)772 static int siw_check_sgl_tx(struct ib_pd *pd, struct siw_wqe *wqe,
773 			    enum ib_access_flags perms)
774 {
775 	struct siw_sge *sge = &wqe->sqe.sge[0];
776 	int i, len, num_sge = wqe->sqe.num_sge;
777 
778 	if (unlikely(num_sge > SIW_MAX_SGE))
779 		return -EINVAL;
780 
781 	for (i = 0, len = 0; num_sge; num_sge--, i++, sge++) {
782 		/*
783 		 * rdma verbs: do not check stag for a zero length sge
784 		 */
785 		if (sge->length) {
786 			int rv = siw_check_sge(pd, sge, &wqe->mem[i], perms, 0,
787 					       sge->length);
788 
789 			if (unlikely(rv != E_ACCESS_OK))
790 				return rv;
791 		}
792 		len += sge->length;
793 	}
794 	return len;
795 }
796 
797 /*
798  * siw_qp_sq_proc_tx()
799  *
800  * Process one WQE which needs transmission on the wire.
801  */
siw_qp_sq_proc_tx(struct siw_qp * qp,struct siw_wqe * wqe)802 static int siw_qp_sq_proc_tx(struct siw_qp *qp, struct siw_wqe *wqe)
803 {
804 	struct siw_iwarp_tx *c_tx = &qp->tx_ctx;
805 	struct socket *s = qp->attrs.sk;
806 	int rv = 0, burst_len = qp->tx_ctx.burst;
807 	enum rdmap_ecode ecode = RDMAP_ECODE_CATASTROPHIC_STREAM;
808 
809 	if (unlikely(wqe->wr_status == SIW_WR_IDLE))
810 		return 0;
811 
812 	if (!burst_len)
813 		burst_len = SQ_USER_MAXBURST;
814 
815 	if (wqe->wr_status == SIW_WR_QUEUED) {
816 		if (!(wqe->sqe.flags & SIW_WQE_INLINE)) {
817 			if (tx_type(wqe) == SIW_OP_READ_RESPONSE)
818 				wqe->sqe.num_sge = 1;
819 
820 			if (tx_type(wqe) != SIW_OP_READ &&
821 			    tx_type(wqe) != SIW_OP_READ_LOCAL_INV) {
822 				/*
823 				 * Reference memory to be tx'd w/o checking
824 				 * access for LOCAL_READ permission, since
825 				 * not defined in RDMA core.
826 				 */
827 				rv = siw_check_sgl_tx(qp->pd, wqe, 0);
828 				if (rv < 0) {
829 					if (tx_type(wqe) ==
830 					    SIW_OP_READ_RESPONSE)
831 						ecode = siw_rdmap_error(-rv);
832 					rv = -EINVAL;
833 					goto tx_error;
834 				}
835 				wqe->bytes = rv;
836 			} else {
837 				wqe->bytes = 0;
838 			}
839 		} else {
840 			wqe->bytes = wqe->sqe.sge[0].length;
841 			if (!rdma_is_kernel_res(&qp->base_qp.res)) {
842 				if (wqe->bytes > SIW_MAX_INLINE) {
843 					rv = -EINVAL;
844 					goto tx_error;
845 				}
846 				wqe->sqe.sge[0].laddr =
847 					(u64)(uintptr_t)&wqe->sqe.sge[1];
848 			}
849 		}
850 		wqe->wr_status = SIW_WR_INPROGRESS;
851 		wqe->processed = 0;
852 
853 		siw_update_tcpseg(c_tx, s);
854 
855 		rv = siw_qp_prepare_tx(c_tx);
856 		if (rv == PKT_FRAGMENTED) {
857 			c_tx->state = SIW_SEND_HDR;
858 			siw_prepare_fpdu(qp, wqe);
859 		} else if (rv == PKT_COMPLETE) {
860 			c_tx->state = SIW_SEND_SHORT_FPDU;
861 		} else {
862 			goto tx_error;
863 		}
864 	}
865 
866 next_segment:
867 	siw_dbg_qp(qp, "wr type %d, state %d, data %u, sent %u, id %llx\n",
868 		   tx_type(wqe), wqe->wr_status, wqe->bytes, wqe->processed,
869 		   wqe->sqe.id);
870 
871 	if (--burst_len == 0) {
872 		rv = -EINPROGRESS;
873 		goto tx_done;
874 	}
875 	if (c_tx->state == SIW_SEND_SHORT_FPDU) {
876 		enum siw_opcode tx_type = tx_type(wqe);
877 		unsigned int msg_flags;
878 
879 		if (siw_sq_empty(qp) || !siw_tcp_nagle || burst_len == 1)
880 			/*
881 			 * End current TCP segment, if SQ runs empty,
882 			 * or siw_tcp_nagle is not set, or we bail out
883 			 * soon due to no burst credit left.
884 			 */
885 			msg_flags = MSG_DONTWAIT;
886 		else
887 			msg_flags = MSG_DONTWAIT | MSG_MORE;
888 
889 		rv = siw_tx_ctrl(c_tx, s, msg_flags);
890 
891 		if (!rv && tx_type != SIW_OP_READ &&
892 		    tx_type != SIW_OP_READ_LOCAL_INV)
893 			wqe->processed = wqe->bytes;
894 
895 		goto tx_done;
896 
897 	} else {
898 		rv = siw_tx_hdt(c_tx, s);
899 	}
900 	if (!rv) {
901 		/*
902 		 * One segment sent. Processing completed if last
903 		 * segment, Do next segment otherwise.
904 		 */
905 		if (unlikely(c_tx->tx_suspend)) {
906 			/*
907 			 * Verbs, 6.4.: Try stopping sending after a full
908 			 * DDP segment if the connection goes down
909 			 * (== peer halfclose)
910 			 */
911 			rv = -ECONNABORTED;
912 			goto tx_done;
913 		}
914 		if (c_tx->pkt.ctrl.ddp_rdmap_ctrl & DDP_FLAG_LAST) {
915 			siw_dbg_qp(qp, "WQE completed\n");
916 			goto tx_done;
917 		}
918 		c_tx->state = SIW_SEND_HDR;
919 
920 		siw_update_tcpseg(c_tx, s);
921 
922 		siw_prepare_fpdu(qp, wqe);
923 		goto next_segment;
924 	}
925 tx_done:
926 	qp->tx_ctx.burst = burst_len;
927 	return rv;
928 
929 tx_error:
930 	if (ecode != RDMAP_ECODE_CATASTROPHIC_STREAM)
931 		siw_init_terminate(qp, TERM_ERROR_LAYER_RDMAP,
932 				   RDMAP_ETYPE_REMOTE_PROTECTION, ecode, 1);
933 	else
934 		siw_init_terminate(qp, TERM_ERROR_LAYER_RDMAP,
935 				   RDMAP_ETYPE_CATASTROPHIC,
936 				   RDMAP_ECODE_UNSPECIFIED, 1);
937 	return rv;
938 }
939 
siw_fastreg_mr(struct ib_pd * pd,struct siw_sqe * sqe)940 static int siw_fastreg_mr(struct ib_pd *pd, struct siw_sqe *sqe)
941 {
942 	struct ib_mr *base_mr = (struct ib_mr *)(uintptr_t)sqe->base_mr;
943 	struct siw_device *sdev = to_siw_dev(pd->device);
944 	struct siw_mem *mem;
945 	int rv = 0;
946 
947 	siw_dbg_pd(pd, "STag 0x%08x\n", sqe->rkey);
948 
949 	if (unlikely(!base_mr)) {
950 		pr_warn("siw: fastreg: STag 0x%08x unknown\n", sqe->rkey);
951 		return -EINVAL;
952 	}
953 
954 	if (unlikely(base_mr->rkey >> 8 != sqe->rkey  >> 8)) {
955 		pr_warn("siw: fastreg: STag 0x%08x: bad MR\n", sqe->rkey);
956 		return -EINVAL;
957 	}
958 
959 	mem = siw_mem_id2obj(sdev, sqe->rkey  >> 8);
960 	if (unlikely(!mem)) {
961 		pr_warn("siw: fastreg: STag 0x%08x unknown\n", sqe->rkey);
962 		return -EINVAL;
963 	}
964 
965 	if (unlikely(mem->pd != pd)) {
966 		pr_warn("siw: fastreg: PD mismatch\n");
967 		rv = -EINVAL;
968 		goto out;
969 	}
970 	if (unlikely(mem->stag_valid)) {
971 		pr_warn("siw: fastreg: STag 0x%08x already valid\n", sqe->rkey);
972 		rv = -EINVAL;
973 		goto out;
974 	}
975 	/* Refresh STag since user may have changed key part */
976 	mem->stag = sqe->rkey;
977 	mem->perms = sqe->access;
978 
979 	siw_dbg_mem(mem, "STag 0x%08x now valid\n", sqe->rkey);
980 	mem->va = base_mr->iova;
981 	mem->stag_valid = 1;
982 out:
983 	siw_mem_put(mem);
984 	return rv;
985 }
986 
siw_qp_sq_proc_local(struct siw_qp * qp,struct siw_wqe * wqe)987 static int siw_qp_sq_proc_local(struct siw_qp *qp, struct siw_wqe *wqe)
988 {
989 	int rv;
990 
991 	switch (tx_type(wqe)) {
992 	case SIW_OP_REG_MR:
993 		rv = siw_fastreg_mr(qp->pd, &wqe->sqe);
994 		break;
995 
996 	case SIW_OP_INVAL_STAG:
997 		rv = siw_invalidate_stag(qp->pd, wqe->sqe.rkey);
998 		break;
999 
1000 	default:
1001 		rv = -EINVAL;
1002 	}
1003 	return rv;
1004 }
1005 
1006 /*
1007  * siw_qp_sq_process()
1008  *
1009  * Core TX path routine for RDMAP/DDP/MPA using a TCP kernel socket.
1010  * Sends RDMAP payload for the current SQ WR @wqe of @qp in one or more
1011  * MPA FPDUs, each containing a DDP segment.
1012  *
1013  * SQ processing may occur in user context as a result of posting
1014  * new WQE's or from siw_sq_work_handler() context. Processing in
1015  * user context is limited to non-kernel verbs users.
1016  *
1017  * SQ processing may get paused anytime, possibly in the middle of a WR
1018  * or FPDU, if insufficient send space is available. SQ processing
1019  * gets resumed from siw_sq_work_handler(), if send space becomes
1020  * available again.
1021  *
1022  * Must be called with the QP state read-locked.
1023  *
1024  * Note:
1025  * An outbound RREQ can be satisfied by the corresponding RRESP
1026  * _before_ it gets assigned to the ORQ. This happens regularly
1027  * in RDMA READ via loopback case. Since both outbound RREQ and
1028  * inbound RRESP can be handled by the same CPU, locking the ORQ
1029  * is dead-lock prone and thus not an option. With that, the
1030  * RREQ gets assigned to the ORQ _before_ being sent - see
1031  * siw_activate_tx() - and pulled back in case of send failure.
1032  */
siw_qp_sq_process(struct siw_qp * qp)1033 int siw_qp_sq_process(struct siw_qp *qp)
1034 {
1035 	struct siw_wqe *wqe = tx_wqe(qp);
1036 	enum siw_opcode tx_type;
1037 	unsigned long flags;
1038 	int rv = 0;
1039 
1040 	siw_dbg_qp(qp, "enter for type %d\n", tx_type(wqe));
1041 
1042 next_wqe:
1043 	/*
1044 	 * Stop QP processing if SQ state changed
1045 	 */
1046 	if (unlikely(qp->tx_ctx.tx_suspend)) {
1047 		siw_dbg_qp(qp, "tx suspended\n");
1048 		goto done;
1049 	}
1050 	tx_type = tx_type(wqe);
1051 
1052 	if (tx_type <= SIW_OP_READ_RESPONSE)
1053 		rv = siw_qp_sq_proc_tx(qp, wqe);
1054 	else
1055 		rv = siw_qp_sq_proc_local(qp, wqe);
1056 
1057 	if (!rv) {
1058 		/*
1059 		 * WQE processing done
1060 		 */
1061 		switch (tx_type) {
1062 		case SIW_OP_SEND:
1063 		case SIW_OP_SEND_REMOTE_INV:
1064 		case SIW_OP_WRITE:
1065 			siw_wqe_put_mem(wqe, tx_type);
1066 			fallthrough;
1067 
1068 		case SIW_OP_INVAL_STAG:
1069 		case SIW_OP_REG_MR:
1070 			if (tx_flags(wqe) & SIW_WQE_SIGNALLED)
1071 				siw_sqe_complete(qp, &wqe->sqe, wqe->bytes,
1072 						 SIW_WC_SUCCESS);
1073 			break;
1074 
1075 		case SIW_OP_READ:
1076 		case SIW_OP_READ_LOCAL_INV:
1077 			/*
1078 			 * already enqueued to ORQ queue
1079 			 */
1080 			break;
1081 
1082 		case SIW_OP_READ_RESPONSE:
1083 			siw_wqe_put_mem(wqe, tx_type);
1084 			break;
1085 
1086 		default:
1087 			WARN(1, "undefined WQE type %d\n", tx_type);
1088 			rv = -EINVAL;
1089 			goto done;
1090 		}
1091 
1092 		spin_lock_irqsave(&qp->sq_lock, flags);
1093 		wqe->wr_status = SIW_WR_IDLE;
1094 		rv = siw_activate_tx(qp);
1095 		spin_unlock_irqrestore(&qp->sq_lock, flags);
1096 
1097 		if (rv <= 0)
1098 			goto done;
1099 
1100 		goto next_wqe;
1101 
1102 	} else if (rv == -EAGAIN) {
1103 		siw_dbg_qp(qp, "sq paused: hd/tr %d of %d, data %d\n",
1104 			   qp->tx_ctx.ctrl_sent, qp->tx_ctx.ctrl_len,
1105 			   qp->tx_ctx.bytes_unsent);
1106 		rv = 0;
1107 		goto done;
1108 	} else if (rv == -EINPROGRESS) {
1109 		rv = siw_sq_start(qp);
1110 		goto done;
1111 	} else {
1112 		/*
1113 		 * WQE processing failed.
1114 		 * Verbs 8.3.2:
1115 		 * o It turns any WQE into a signalled WQE.
1116 		 * o Local catastrophic error must be surfaced
1117 		 * o QP must be moved into Terminate state: done by code
1118 		 *   doing socket state change processing
1119 		 *
1120 		 * o TODO: Termination message must be sent.
1121 		 * o TODO: Implement more precise work completion errors,
1122 		 *         see enum ib_wc_status in ib_verbs.h
1123 		 */
1124 		siw_dbg_qp(qp, "wqe type %d processing failed: %d\n",
1125 			   tx_type(wqe), rv);
1126 
1127 		spin_lock_irqsave(&qp->sq_lock, flags);
1128 		/*
1129 		 * RREQ may have already been completed by inbound RRESP!
1130 		 */
1131 		if ((tx_type == SIW_OP_READ ||
1132 		     tx_type == SIW_OP_READ_LOCAL_INV) && qp->attrs.orq_size) {
1133 			/* Cleanup pending entry in ORQ */
1134 			qp->orq_put--;
1135 			qp->orq[qp->orq_put % qp->attrs.orq_size].flags = 0;
1136 		}
1137 		spin_unlock_irqrestore(&qp->sq_lock, flags);
1138 		/*
1139 		 * immediately suspends further TX processing
1140 		 */
1141 		if (!qp->tx_ctx.tx_suspend)
1142 			siw_qp_cm_drop(qp, 0);
1143 
1144 		switch (tx_type) {
1145 		case SIW_OP_SEND:
1146 		case SIW_OP_SEND_REMOTE_INV:
1147 		case SIW_OP_SEND_WITH_IMM:
1148 		case SIW_OP_WRITE:
1149 		case SIW_OP_READ:
1150 		case SIW_OP_READ_LOCAL_INV:
1151 			siw_wqe_put_mem(wqe, tx_type);
1152 			fallthrough;
1153 
1154 		case SIW_OP_INVAL_STAG:
1155 		case SIW_OP_REG_MR:
1156 			siw_sqe_complete(qp, &wqe->sqe, wqe->bytes,
1157 					 SIW_WC_LOC_QP_OP_ERR);
1158 
1159 			siw_qp_event(qp, IB_EVENT_QP_FATAL);
1160 
1161 			break;
1162 
1163 		case SIW_OP_READ_RESPONSE:
1164 			siw_dbg_qp(qp, "proc. read.response failed: %d\n", rv);
1165 
1166 			siw_qp_event(qp, IB_EVENT_QP_REQ_ERR);
1167 
1168 			siw_wqe_put_mem(wqe, SIW_OP_READ_RESPONSE);
1169 
1170 			break;
1171 
1172 		default:
1173 			WARN(1, "undefined WQE type %d\n", tx_type);
1174 			rv = -EINVAL;
1175 		}
1176 		wqe->wr_status = SIW_WR_IDLE;
1177 	}
1178 done:
1179 	return rv;
1180 }
1181 
siw_sq_resume(struct siw_qp * qp)1182 static void siw_sq_resume(struct siw_qp *qp)
1183 {
1184 	if (down_read_trylock(&qp->state_lock)) {
1185 		if (likely(qp->attrs.state == SIW_QP_STATE_RTS &&
1186 			   !qp->tx_ctx.tx_suspend)) {
1187 			int rv = siw_qp_sq_process(qp);
1188 
1189 			up_read(&qp->state_lock);
1190 
1191 			if (unlikely(rv < 0)) {
1192 				siw_dbg_qp(qp, "SQ task failed: err %d\n", rv);
1193 
1194 				if (!qp->tx_ctx.tx_suspend)
1195 					siw_qp_cm_drop(qp, 0);
1196 			}
1197 		} else {
1198 			up_read(&qp->state_lock);
1199 		}
1200 	} else {
1201 		siw_dbg_qp(qp, "Resume SQ while QP locked\n");
1202 	}
1203 	siw_qp_put(qp);
1204 }
1205 
1206 struct tx_task_t {
1207 	struct llist_head active;
1208 	wait_queue_head_t waiting;
1209 };
1210 
1211 static DEFINE_PER_CPU(struct tx_task_t, siw_tx_task_g);
1212 
siw_create_tx_threads(void)1213 int siw_create_tx_threads(void)
1214 {
1215 	int cpu, assigned = 0;
1216 
1217 	for_each_online_cpu(cpu) {
1218 		struct tx_task_t *tx_task;
1219 
1220 		/* Skip HT cores */
1221 		if (cpu % cpumask_weight(topology_sibling_cpumask(cpu)))
1222 			continue;
1223 
1224 		tx_task = &per_cpu(siw_tx_task_g, cpu);
1225 		init_llist_head(&tx_task->active);
1226 		init_waitqueue_head(&tx_task->waiting);
1227 
1228 		siw_tx_thread[cpu] =
1229 			kthread_run_on_cpu(siw_run_sq,
1230 					   (unsigned long *)(long)cpu,
1231 					   cpu, "siw_tx/%u");
1232 		if (IS_ERR(siw_tx_thread[cpu])) {
1233 			siw_tx_thread[cpu] = NULL;
1234 			continue;
1235 		}
1236 		assigned++;
1237 	}
1238 	return assigned;
1239 }
1240 
siw_stop_tx_threads(void)1241 void siw_stop_tx_threads(void)
1242 {
1243 	int cpu;
1244 
1245 	for_each_possible_cpu(cpu) {
1246 		if (siw_tx_thread[cpu]) {
1247 			kthread_stop(siw_tx_thread[cpu]);
1248 			wake_up(&per_cpu(siw_tx_task_g, cpu).waiting);
1249 			siw_tx_thread[cpu] = NULL;
1250 		}
1251 	}
1252 }
1253 
siw_run_sq(void * data)1254 int siw_run_sq(void *data)
1255 {
1256 	const int nr_cpu = (unsigned int)(long)data;
1257 	struct llist_node *active;
1258 	struct siw_qp *qp;
1259 	struct tx_task_t *tx_task = &per_cpu(siw_tx_task_g, nr_cpu);
1260 
1261 	while (1) {
1262 		struct llist_node *fifo_list = NULL;
1263 
1264 		wait_event_interruptible(tx_task->waiting,
1265 					 !llist_empty(&tx_task->active) ||
1266 						 kthread_should_stop());
1267 
1268 		if (kthread_should_stop())
1269 			break;
1270 
1271 		active = llist_del_all(&tx_task->active);
1272 		/*
1273 		 * llist_del_all returns a list with newest entry first.
1274 		 * Re-order list for fairness among QP's.
1275 		 */
1276 		fifo_list = llist_reverse_order(active);
1277 		while (fifo_list) {
1278 			qp = container_of(fifo_list, struct siw_qp, tx_list);
1279 			fifo_list = llist_next(fifo_list);
1280 			qp->tx_list.next = NULL;
1281 
1282 			siw_sq_resume(qp);
1283 		}
1284 	}
1285 	active = llist_del_all(&tx_task->active);
1286 	if (active) {
1287 		llist_for_each_entry(qp, active, tx_list) {
1288 			qp->tx_list.next = NULL;
1289 			siw_sq_resume(qp);
1290 		}
1291 	}
1292 	return 0;
1293 }
1294 
siw_sq_start(struct siw_qp * qp)1295 int siw_sq_start(struct siw_qp *qp)
1296 {
1297 	if (tx_wqe(qp)->wr_status == SIW_WR_IDLE)
1298 		return 0;
1299 
1300 	if (unlikely(!cpu_online(qp->tx_cpu))) {
1301 		siw_put_tx_cpu(qp->tx_cpu);
1302 		qp->tx_cpu = siw_get_tx_cpu(qp->sdev);
1303 		if (qp->tx_cpu < 0) {
1304 			pr_warn("siw: no tx cpu available\n");
1305 
1306 			return -EIO;
1307 		}
1308 	}
1309 	siw_qp_get(qp);
1310 
1311 	llist_add(&qp->tx_list, &per_cpu(siw_tx_task_g, qp->tx_cpu).active);
1312 
1313 	wake_up(&per_cpu(siw_tx_task_g, qp->tx_cpu).waiting);
1314 
1315 	return 0;
1316 }
1317