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