1
2
3
4
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
25static 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 virt_to_page(paddr);
33
34 return NULL;
35}
36
37
38
39
40static 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
59 memcpy(paddr,
60 (const void *)(uintptr_t)sge->laddr, bytes);
61 } else if (c_tx->in_syscall) {
62 if (copy_from_user(paddr, u64_to_user_ptr(sge->laddr),
63 bytes))
64 return -EFAULT;
65 } else {
66 unsigned int off = sge->laddr & ~PAGE_MASK;
67 struct page *p;
68 char *buffer;
69 int pbl_idx = 0;
70
71 if (!mem->is_pbl)
72 p = siw_get_upage(mem->umem, sge->laddr);
73 else
74 p = siw_get_pblpage(mem, sge->laddr, &pbl_idx);
75
76 if (unlikely(!p))
77 return -EFAULT;
78
79 buffer = kmap(p);
80
81 if (likely(PAGE_SIZE - off >= bytes)) {
82 memcpy(paddr, buffer + off, bytes);
83 } else {
84 unsigned long part = bytes - (PAGE_SIZE - off);
85
86 memcpy(paddr, buffer + off, part);
87 kunmap(p);
88
89 if (!mem->is_pbl)
90 p = siw_get_upage(mem->umem,
91 sge->laddr + part);
92 else
93 p = siw_get_pblpage(mem,
94 sge->laddr + part,
95 &pbl_idx);
96 if (unlikely(!p))
97 return -EFAULT;
98
99 buffer = kmap(p);
100 memcpy(paddr + part, buffer, bytes - part);
101 }
102 kunmap(p);
103 }
104 }
105 return (int)bytes;
106}
107
108#define PKT_FRAGMENTED 1
109#define PKT_COMPLETE 0
110
111
112
113
114
115
116
117
118
119static int siw_qp_prepare_tx(struct siw_iwarp_tx *c_tx)
120{
121 struct siw_wqe *wqe = &c_tx->wqe_active;
122 char *crc = NULL;
123 int data = 0;
124
125 switch (tx_type(wqe)) {
126 case SIW_OP_READ:
127 case SIW_OP_READ_LOCAL_INV:
128 memcpy(&c_tx->pkt.ctrl,
129 &iwarp_pktinfo[RDMAP_RDMA_READ_REQ].ctrl,
130 sizeof(struct iwarp_ctrl));
131
132 c_tx->pkt.rreq.rsvd = 0;
133 c_tx->pkt.rreq.ddp_qn = htonl(RDMAP_UNTAGGED_QN_RDMA_READ);
134 c_tx->pkt.rreq.ddp_msn =
135 htonl(++c_tx->ddp_msn[RDMAP_UNTAGGED_QN_RDMA_READ]);
136 c_tx->pkt.rreq.ddp_mo = 0;
137 c_tx->pkt.rreq.sink_stag = htonl(wqe->sqe.sge[0].lkey);
138 c_tx->pkt.rreq.sink_to =
139 cpu_to_be64(wqe->sqe.sge[0].laddr);
140 c_tx->pkt.rreq.source_stag = htonl(wqe->sqe.rkey);
141 c_tx->pkt.rreq.source_to = cpu_to_be64(wqe->sqe.raddr);
142 c_tx->pkt.rreq.read_size = htonl(wqe->sqe.sge[0].length);
143
144 c_tx->ctrl_len = sizeof(struct iwarp_rdma_rreq);
145 crc = (char *)&c_tx->pkt.rreq_pkt.crc;
146 break;
147
148 case SIW_OP_SEND:
149 if (tx_flags(wqe) & SIW_WQE_SOLICITED)
150 memcpy(&c_tx->pkt.ctrl,
151 &iwarp_pktinfo[RDMAP_SEND_SE].ctrl,
152 sizeof(struct iwarp_ctrl));
153 else
154 memcpy(&c_tx->pkt.ctrl, &iwarp_pktinfo[RDMAP_SEND].ctrl,
155 sizeof(struct iwarp_ctrl));
156
157 c_tx->pkt.send.ddp_qn = RDMAP_UNTAGGED_QN_SEND;
158 c_tx->pkt.send.ddp_msn =
159 htonl(++c_tx->ddp_msn[RDMAP_UNTAGGED_QN_SEND]);
160 c_tx->pkt.send.ddp_mo = 0;
161
162 c_tx->pkt.send_inv.inval_stag = 0;
163
164 c_tx->ctrl_len = sizeof(struct iwarp_send);
165
166 crc = (char *)&c_tx->pkt.send_pkt.crc;
167 data = siw_try_1seg(c_tx, crc);
168 break;
169
170 case SIW_OP_SEND_REMOTE_INV:
171 if (tx_flags(wqe) & SIW_WQE_SOLICITED)
172 memcpy(&c_tx->pkt.ctrl,
173 &iwarp_pktinfo[RDMAP_SEND_SE_INVAL].ctrl,
174 sizeof(struct iwarp_ctrl));
175 else
176 memcpy(&c_tx->pkt.ctrl,
177 &iwarp_pktinfo[RDMAP_SEND_INVAL].ctrl,
178 sizeof(struct iwarp_ctrl));
179
180 c_tx->pkt.send.ddp_qn = RDMAP_UNTAGGED_QN_SEND;
181 c_tx->pkt.send.ddp_msn =
182 htonl(++c_tx->ddp_msn[RDMAP_UNTAGGED_QN_SEND]);
183 c_tx->pkt.send.ddp_mo = 0;
184
185 c_tx->pkt.send_inv.inval_stag = cpu_to_be32(wqe->sqe.rkey);
186
187 c_tx->ctrl_len = sizeof(struct iwarp_send_inv);
188
189 crc = (char *)&c_tx->pkt.send_pkt.crc;
190 data = siw_try_1seg(c_tx, crc);
191 break;
192
193 case SIW_OP_WRITE:
194 memcpy(&c_tx->pkt.ctrl, &iwarp_pktinfo[RDMAP_RDMA_WRITE].ctrl,
195 sizeof(struct iwarp_ctrl));
196
197 c_tx->pkt.rwrite.sink_stag = htonl(wqe->sqe.rkey);
198 c_tx->pkt.rwrite.sink_to = cpu_to_be64(wqe->sqe.raddr);
199 c_tx->ctrl_len = sizeof(struct iwarp_rdma_write);
200
201 crc = (char *)&c_tx->pkt.write_pkt.crc;
202 data = siw_try_1seg(c_tx, crc);
203 break;
204
205 case SIW_OP_READ_RESPONSE:
206 memcpy(&c_tx->pkt.ctrl,
207 &iwarp_pktinfo[RDMAP_RDMA_READ_RESP].ctrl,
208 sizeof(struct iwarp_ctrl));
209
210
211 c_tx->pkt.rresp.sink_stag = cpu_to_be32(wqe->sqe.rkey);
212 c_tx->pkt.rresp.sink_to = cpu_to_be64(wqe->sqe.raddr);
213
214 c_tx->ctrl_len = sizeof(struct iwarp_rdma_rresp);
215
216 crc = (char *)&c_tx->pkt.write_pkt.crc;
217 data = siw_try_1seg(c_tx, crc);
218 break;
219
220 default:
221 siw_dbg_qp(tx_qp(c_tx), "stale wqe type %d\n", tx_type(wqe));
222 return -EOPNOTSUPP;
223 }
224 if (unlikely(data < 0))
225 return data;
226
227 c_tx->ctrl_sent = 0;
228
229 if (data <= MAX_HDR_INLINE) {
230 if (data) {
231 wqe->processed = data;
232
233 c_tx->pkt.ctrl.mpa_len =
234 htons(c_tx->ctrl_len + data - MPA_HDR_SIZE);
235
236
237 data += -(int)data & 0x3;
238
239 crc += data;
240 c_tx->ctrl_len += data;
241
242 if (!(c_tx->pkt.ctrl.ddp_rdmap_ctrl & DDP_FLAG_TAGGED))
243 c_tx->pkt.c_untagged.ddp_mo = 0;
244 else
245 c_tx->pkt.c_tagged.ddp_to =
246 cpu_to_be64(wqe->sqe.raddr);
247 }
248
249 *(u32 *)crc = 0;
250
251
252
253 if (c_tx->mpa_crc_hd) {
254 crypto_shash_init(c_tx->mpa_crc_hd);
255 if (crypto_shash_update(c_tx->mpa_crc_hd,
256 (u8 *)&c_tx->pkt,
257 c_tx->ctrl_len))
258 return -EINVAL;
259 crypto_shash_final(c_tx->mpa_crc_hd, (u8 *)crc);
260 }
261 c_tx->ctrl_len += MPA_CRC_SIZE;
262
263 return PKT_COMPLETE;
264 }
265 c_tx->ctrl_len += MPA_CRC_SIZE;
266 c_tx->sge_idx = 0;
267 c_tx->sge_off = 0;
268 c_tx->pbl_idx = 0;
269
270
271
272
273
274
275
276
277
278
279 if (c_tx->zcopy_tx && wqe->bytes >= SENDPAGE_THRESH &&
280 !(tx_flags(wqe) & SIW_WQE_SIGNALLED))
281 c_tx->use_sendpage = 1;
282 else
283 c_tx->use_sendpage = 0;
284
285 return PKT_FRAGMENTED;
286}
287
288
289
290
291
292
293static int siw_tx_ctrl(struct siw_iwarp_tx *c_tx, struct socket *s,
294 int flags)
295{
296 struct msghdr msg = { .msg_flags = flags };
297 struct kvec iov = { .iov_base =
298 (char *)&c_tx->pkt.ctrl + c_tx->ctrl_sent,
299 .iov_len = c_tx->ctrl_len - c_tx->ctrl_sent };
300
301 int rv = kernel_sendmsg(s, &msg, &iov, 1,
302 c_tx->ctrl_len - c_tx->ctrl_sent);
303
304 if (rv >= 0) {
305 c_tx->ctrl_sent += rv;
306
307 if (c_tx->ctrl_sent == c_tx->ctrl_len)
308 rv = 0;
309 else
310 rv = -EAGAIN;
311 }
312 return rv;
313}
314
315
316
317
318
319
320
321
322
323
324static int siw_tcp_sendpages(struct socket *s, struct page **page, int offset,
325 size_t size)
326{
327 struct sock *sk = s->sk;
328 int i = 0, rv = 0, sent = 0,
329 flags = MSG_MORE | MSG_DONTWAIT | MSG_SENDPAGE_NOTLAST;
330
331 while (size) {
332 size_t bytes = min_t(size_t, PAGE_SIZE - offset, size);
333
334 if (size + offset <= PAGE_SIZE)
335 flags = MSG_MORE | MSG_DONTWAIT;
336
337 tcp_rate_check_app_limited(sk);
338try_page_again:
339 lock_sock(sk);
340 rv = do_tcp_sendpages(sk, page[i], offset, bytes, flags);
341 release_sock(sk);
342
343 if (rv > 0) {
344 size -= rv;
345 sent += rv;
346 if (rv != bytes) {
347 offset += rv;
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
364
365
366
367
368
369static 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
399static void siw_unmap_pages(struct page **pp, unsigned long kmap_mask)
400{
401 while (kmap_mask) {
402 if (kmap_mask & BIT(0))
403 kunmap(*pp);
404 pp++;
405 kmap_mask >>= 1;
406 }
407}
408
409
410
411
412
413
414
415
416
417
418#define MAX_ARRAY ((0xffff / PAGE_SIZE) + 1 + (2 * (SIW_MAX_SGE - 1) + 2))
419
420
421
422
423
424static int siw_tx_hdt(struct siw_iwarp_tx *c_tx, struct socket *s)
425{
426 struct siw_wqe *wqe = &c_tx->wqe_active;
427 struct siw_sge *sge = &wqe->sqe.sge[c_tx->sge_idx];
428 struct kvec iov[MAX_ARRAY];
429 struct page *page_array[MAX_ARRAY];
430 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_EOR };
431
432 int seg = 0, do_crc = c_tx->do_crc, is_kva = 0, rv;
433 unsigned int data_len = c_tx->bytes_unsent, hdr_len = 0, trl_len = 0,
434 sge_off = c_tx->sge_off, sge_idx = c_tx->sge_idx,
435 pbl_idx = c_tx->pbl_idx;
436 unsigned long kmap_mask = 0L;
437
438 if (c_tx->state == SIW_SEND_HDR) {
439 if (c_tx->use_sendpage) {
440 rv = siw_tx_ctrl(c_tx, s, MSG_DONTWAIT | MSG_MORE);
441 if (rv)
442 goto done;
443
444 c_tx->state = SIW_SEND_DATA;
445 } else {
446 iov[0].iov_base =
447 (char *)&c_tx->pkt.ctrl + c_tx->ctrl_sent;
448 iov[0].iov_len = hdr_len =
449 c_tx->ctrl_len - c_tx->ctrl_sent;
450 seg = 1;
451 }
452 }
453
454 wqe->processed += data_len;
455
456 while (data_len) {
457 unsigned int sge_len = min(sge->length - sge_off, data_len);
458 unsigned int fp_off = (sge->laddr + sge_off) & ~PAGE_MASK;
459 struct siw_mem *mem;
460
461 if (!(tx_flags(wqe) & SIW_WQE_INLINE)) {
462 mem = wqe->mem[sge_idx];
463 is_kva = mem->mem_obj == NULL ? 1 : 0;
464 } else {
465 is_kva = 1;
466 }
467 if (is_kva && !c_tx->use_sendpage) {
468
469
470
471
472 iov[seg].iov_base =
473 (void *)(uintptr_t)(sge->laddr + sge_off);
474 iov[seg].iov_len = sge_len;
475
476 if (do_crc)
477 crypto_shash_update(c_tx->mpa_crc_hd,
478 iov[seg].iov_base,
479 sge_len);
480 sge_off += sge_len;
481 data_len -= sge_len;
482 seg++;
483 goto sge_done;
484 }
485
486 while (sge_len) {
487 size_t plen = min((int)PAGE_SIZE - fp_off, sge_len);
488
489 if (!is_kva) {
490 struct page *p;
491
492 if (mem->is_pbl)
493 p = siw_get_pblpage(
494 mem, sge->laddr + sge_off,
495 &pbl_idx);
496 else
497 p = siw_get_upage(mem->umem,
498 sge->laddr + sge_off);
499 if (unlikely(!p)) {
500 siw_unmap_pages(page_array, kmap_mask);
501 wqe->processed -= c_tx->bytes_unsent;
502 rv = -EFAULT;
503 goto done_crc;
504 }
505 page_array[seg] = p;
506
507 if (!c_tx->use_sendpage) {
508 iov[seg].iov_base = kmap(p) + fp_off;
509 iov[seg].iov_len = plen;
510
511
512 kmap_mask |= BIT(seg);
513
514 if (do_crc)
515 crypto_shash_update(
516 c_tx->mpa_crc_hd,
517 iov[seg].iov_base,
518 plen);
519 } else if (do_crc) {
520 crypto_shash_update(c_tx->mpa_crc_hd,
521 kmap(p) + fp_off,
522 plen);
523 kunmap(p);
524 }
525 } else {
526 u64 va = sge->laddr + sge_off;
527
528 page_array[seg] = virt_to_page(va & PAGE_MASK);
529 if (do_crc)
530 crypto_shash_update(
531 c_tx->mpa_crc_hd,
532 (void *)(uintptr_t)va,
533 plen);
534 }
535
536 sge_len -= plen;
537 sge_off += plen;
538 data_len -= plen;
539 fp_off = 0;
540
541 if (++seg > (int)MAX_ARRAY) {
542 siw_dbg_qp(tx_qp(c_tx), "to many fragments\n");
543 siw_unmap_pages(page_array, kmap_mask);
544 wqe->processed -= c_tx->bytes_unsent;
545 rv = -EMSGSIZE;
546 goto done_crc;
547 }
548 }
549sge_done:
550
551 if (sge_off == sge->length &&
552 (data_len != 0 || wqe->processed < wqe->bytes)) {
553 sge_idx++;
554 sge++;
555 sge_off = 0;
556 }
557 }
558
559 if (likely(c_tx->state != SIW_SEND_TRAILER)) {
560 iov[seg].iov_base = &c_tx->trailer.pad[4 - c_tx->pad];
561 iov[seg].iov_len = trl_len = MAX_TRAILER - (4 - c_tx->pad);
562 } else {
563 iov[seg].iov_base = &c_tx->trailer.pad[c_tx->ctrl_sent];
564 iov[seg].iov_len = trl_len = MAX_TRAILER - c_tx->ctrl_sent;
565 }
566
567 if (c_tx->pad) {
568 *(u32 *)c_tx->trailer.pad = 0;
569 if (do_crc)
570 crypto_shash_update(c_tx->mpa_crc_hd,
571 (u8 *)&c_tx->trailer.crc - c_tx->pad,
572 c_tx->pad);
573 }
574 if (!c_tx->mpa_crc_hd)
575 c_tx->trailer.crc = 0;
576 else if (do_crc)
577 crypto_shash_final(c_tx->mpa_crc_hd, (u8 *)&c_tx->trailer.crc);
578
579 data_len = c_tx->bytes_unsent;
580
581 if (c_tx->use_sendpage) {
582 rv = siw_0copy_tx(s, page_array, &wqe->sqe.sge[c_tx->sge_idx],
583 c_tx->sge_off, data_len);
584 if (rv == data_len) {
585 rv = kernel_sendmsg(s, &msg, &iov[seg], 1, trl_len);
586 if (rv > 0)
587 rv += data_len;
588 else
589 rv = data_len;
590 }
591 } else {
592 rv = kernel_sendmsg(s, &msg, iov, seg + 1,
593 hdr_len + data_len + trl_len);
594 siw_unmap_pages(page_array, kmap_mask);
595 }
596 if (rv < (int)hdr_len) {
597
598 wqe->processed -= data_len;
599 if (rv >= 0) {
600 c_tx->ctrl_sent += rv;
601 rv = -EAGAIN;
602 }
603 goto done_crc;
604 }
605 rv -= hdr_len;
606
607 if (rv >= (int)data_len) {
608
609 if (data_len > 0 && wqe->processed < wqe->bytes) {
610
611 c_tx->sge_idx = sge_idx;
612 c_tx->sge_off = sge_off;
613 c_tx->pbl_idx = pbl_idx;
614 }
615 rv -= data_len;
616
617 if (rv == trl_len)
618 rv = 0;
619 else {
620 c_tx->state = SIW_SEND_TRAILER;
621 c_tx->ctrl_len = MAX_TRAILER;
622 c_tx->ctrl_sent = rv + 4 - c_tx->pad;
623 c_tx->bytes_unsent = 0;
624 rv = -EAGAIN;
625 }
626
627 } else if (data_len > 0) {
628
629 c_tx->state = SIW_SEND_DATA;
630 wqe->processed -= data_len - rv;
631
632 if (rv) {
633
634
635
636
637 unsigned int sge_unsent;
638
639 c_tx->bytes_unsent -= rv;
640 sge = &wqe->sqe.sge[c_tx->sge_idx];
641 sge_unsent = sge->length - c_tx->sge_off;
642
643 while (sge_unsent <= rv) {
644 rv -= sge_unsent;
645 c_tx->sge_idx++;
646 c_tx->sge_off = 0;
647 sge++;
648 sge_unsent = sge->length;
649 }
650 c_tx->sge_off += rv;
651 }
652 rv = -EAGAIN;
653 }
654done_crc:
655 c_tx->do_crc = 0;
656done:
657 return rv;
658}
659
660static void siw_update_tcpseg(struct siw_iwarp_tx *c_tx,
661 struct socket *s)
662{
663 struct tcp_sock *tp = tcp_sk(s->sk);
664
665 if (tp->gso_segs) {
666 if (c_tx->gso_seg_limit == 0)
667 c_tx->tcp_seglen = tp->mss_cache * tp->gso_segs;
668 else
669 c_tx->tcp_seglen =
670 tp->mss_cache *
671 min_t(u16, c_tx->gso_seg_limit, tp->gso_segs);
672 } else {
673 c_tx->tcp_seglen = tp->mss_cache;
674 }
675
676 c_tx->tcp_seglen &= 0xfffffff8;
677}
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693static void siw_prepare_fpdu(struct siw_qp *qp, struct siw_wqe *wqe)
694{
695 struct siw_iwarp_tx *c_tx = &qp->tx_ctx;
696 int data_len;
697
698 c_tx->ctrl_len =
699 iwarp_pktinfo[__rdmap_get_opcode(&c_tx->pkt.ctrl)].hdr_len;
700 c_tx->ctrl_sent = 0;
701
702
703
704
705 if (!(c_tx->pkt.ctrl.ddp_rdmap_ctrl & DDP_FLAG_TAGGED))
706
707 c_tx->pkt.c_untagged.ddp_mo = cpu_to_be32(wqe->processed);
708 else
709 c_tx->pkt.c_tagged.ddp_to =
710 cpu_to_be64(wqe->sqe.raddr + wqe->processed);
711
712 data_len = wqe->bytes - wqe->processed;
713 if (data_len + c_tx->ctrl_len + MPA_CRC_SIZE > c_tx->tcp_seglen) {
714
715 data_len = c_tx->tcp_seglen - (c_tx->ctrl_len + MPA_CRC_SIZE);
716 c_tx->pkt.ctrl.ddp_rdmap_ctrl &= ~DDP_FLAG_LAST;
717 c_tx->pad = 0;
718 } else {
719 c_tx->pkt.ctrl.ddp_rdmap_ctrl |= DDP_FLAG_LAST;
720 c_tx->pad = -data_len & 0x3;
721 }
722 c_tx->bytes_unsent = data_len;
723
724 c_tx->pkt.ctrl.mpa_len =
725 htons(c_tx->ctrl_len + data_len - MPA_HDR_SIZE);
726
727
728
729
730 if (c_tx->mpa_crc_hd) {
731 crypto_shash_init(c_tx->mpa_crc_hd);
732 crypto_shash_update(c_tx->mpa_crc_hd, (u8 *)&c_tx->pkt,
733 c_tx->ctrl_len);
734 c_tx->do_crc = 1;
735 }
736}
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751static int siw_check_sgl_tx(struct ib_pd *pd, struct siw_wqe *wqe,
752 enum ib_access_flags perms)
753{
754 struct siw_sge *sge = &wqe->sqe.sge[0];
755 int i, len, num_sge = wqe->sqe.num_sge;
756
757 if (unlikely(num_sge > SIW_MAX_SGE))
758 return -EINVAL;
759
760 for (i = 0, len = 0; num_sge; num_sge--, i++, sge++) {
761
762
763
764 if (sge->length) {
765 int rv = siw_check_sge(pd, sge, &wqe->mem[i], perms, 0,
766 sge->length);
767
768 if (unlikely(rv != E_ACCESS_OK))
769 return rv;
770 }
771 len += sge->length;
772 }
773 return len;
774}
775
776
777
778
779
780
781static int siw_qp_sq_proc_tx(struct siw_qp *qp, struct siw_wqe *wqe)
782{
783 struct siw_iwarp_tx *c_tx = &qp->tx_ctx;
784 struct socket *s = qp->attrs.sk;
785 int rv = 0, burst_len = qp->tx_ctx.burst;
786 enum rdmap_ecode ecode = RDMAP_ECODE_CATASTROPHIC_STREAM;
787
788 if (unlikely(wqe->wr_status == SIW_WR_IDLE))
789 return 0;
790
791 if (!burst_len)
792 burst_len = SQ_USER_MAXBURST;
793
794 if (wqe->wr_status == SIW_WR_QUEUED) {
795 if (!(wqe->sqe.flags & SIW_WQE_INLINE)) {
796 if (tx_type(wqe) == SIW_OP_READ_RESPONSE)
797 wqe->sqe.num_sge = 1;
798
799 if (tx_type(wqe) != SIW_OP_READ &&
800 tx_type(wqe) != SIW_OP_READ_LOCAL_INV) {
801
802
803
804
805
806 rv = siw_check_sgl_tx(qp->pd, wqe, 0);
807 if (rv < 0) {
808 if (tx_type(wqe) ==
809 SIW_OP_READ_RESPONSE)
810 ecode = siw_rdmap_error(-rv);
811 rv = -EINVAL;
812 goto tx_error;
813 }
814 wqe->bytes = rv;
815 } else {
816 wqe->bytes = 0;
817 }
818 } else {
819 wqe->bytes = wqe->sqe.sge[0].length;
820 if (!qp->kernel_verbs) {
821 if (wqe->bytes > SIW_MAX_INLINE) {
822 rv = -EINVAL;
823 goto tx_error;
824 }
825 wqe->sqe.sge[0].laddr =
826 (u64)(uintptr_t)&wqe->sqe.sge[1];
827 }
828 }
829 wqe->wr_status = SIW_WR_INPROGRESS;
830 wqe->processed = 0;
831
832 siw_update_tcpseg(c_tx, s);
833
834 rv = siw_qp_prepare_tx(c_tx);
835 if (rv == PKT_FRAGMENTED) {
836 c_tx->state = SIW_SEND_HDR;
837 siw_prepare_fpdu(qp, wqe);
838 } else if (rv == PKT_COMPLETE) {
839 c_tx->state = SIW_SEND_SHORT_FPDU;
840 } else {
841 goto tx_error;
842 }
843 }
844
845next_segment:
846 siw_dbg_qp(qp, "wr type %d, state %d, data %u, sent %u, id %llx\n",
847 tx_type(wqe), wqe->wr_status, wqe->bytes, wqe->processed,
848 wqe->sqe.id);
849
850 if (--burst_len == 0) {
851 rv = -EINPROGRESS;
852 goto tx_done;
853 }
854 if (c_tx->state == SIW_SEND_SHORT_FPDU) {
855 enum siw_opcode tx_type = tx_type(wqe);
856 unsigned int msg_flags;
857
858 if (siw_sq_empty(qp) || !siw_tcp_nagle || burst_len == 1)
859
860
861
862
863
864 msg_flags = MSG_DONTWAIT;
865 else
866 msg_flags = MSG_DONTWAIT | MSG_MORE;
867
868 rv = siw_tx_ctrl(c_tx, s, msg_flags);
869
870 if (!rv && tx_type != SIW_OP_READ &&
871 tx_type != SIW_OP_READ_LOCAL_INV)
872 wqe->processed = wqe->bytes;
873
874 goto tx_done;
875
876 } else {
877 rv = siw_tx_hdt(c_tx, s);
878 }
879 if (!rv) {
880
881
882
883
884 if (unlikely(c_tx->tx_suspend)) {
885
886
887
888
889
890 rv = -ECONNABORTED;
891 goto tx_done;
892 }
893 if (c_tx->pkt.ctrl.ddp_rdmap_ctrl & DDP_FLAG_LAST) {
894 siw_dbg_qp(qp, "WQE completed\n");
895 goto tx_done;
896 }
897 c_tx->state = SIW_SEND_HDR;
898
899 siw_update_tcpseg(c_tx, s);
900
901 siw_prepare_fpdu(qp, wqe);
902 goto next_segment;
903 }
904tx_done:
905 qp->tx_ctx.burst = burst_len;
906 return rv;
907
908tx_error:
909 if (ecode != RDMAP_ECODE_CATASTROPHIC_STREAM)
910 siw_init_terminate(qp, TERM_ERROR_LAYER_RDMAP,
911 RDMAP_ETYPE_REMOTE_PROTECTION, ecode, 1);
912 else
913 siw_init_terminate(qp, TERM_ERROR_LAYER_RDMAP,
914 RDMAP_ETYPE_CATASTROPHIC,
915 RDMAP_ECODE_UNSPECIFIED, 1);
916 return rv;
917}
918
919static int siw_fastreg_mr(struct ib_pd *pd, struct siw_sqe *sqe)
920{
921 struct ib_mr *base_mr = (struct ib_mr *)(uintptr_t)sqe->base_mr;
922 struct siw_device *sdev = to_siw_dev(pd->device);
923 struct siw_mem *mem = siw_mem_id2obj(sdev, sqe->rkey >> 8);
924 int rv = 0;
925
926 siw_dbg_pd(pd, "STag 0x%08x\n", sqe->rkey);
927
928 if (unlikely(!mem || !base_mr)) {
929 pr_warn("siw: fastreg: STag 0x%08x unknown\n", sqe->rkey);
930 return -EINVAL;
931 }
932 if (unlikely(base_mr->rkey >> 8 != sqe->rkey >> 8)) {
933 pr_warn("siw: fastreg: STag 0x%08x: bad MR\n", sqe->rkey);
934 rv = -EINVAL;
935 goto out;
936 }
937 if (unlikely(mem->pd != pd)) {
938 pr_warn("siw: fastreg: PD mismatch\n");
939 rv = -EINVAL;
940 goto out;
941 }
942 if (unlikely(mem->stag_valid)) {
943 pr_warn("siw: fastreg: STag 0x%08x already valid\n", sqe->rkey);
944 rv = -EINVAL;
945 goto out;
946 }
947
948 mem->stag = sqe->rkey;
949 mem->perms = sqe->access;
950
951 siw_dbg_mem(mem, "STag 0x%08x now valid\n", sqe->rkey);
952 mem->va = base_mr->iova;
953 mem->stag_valid = 1;
954out:
955 siw_mem_put(mem);
956 return rv;
957}
958
959static int siw_qp_sq_proc_local(struct siw_qp *qp, struct siw_wqe *wqe)
960{
961 int rv;
962
963 switch (tx_type(wqe)) {
964 case SIW_OP_REG_MR:
965 rv = siw_fastreg_mr(qp->pd, &wqe->sqe);
966 break;
967
968 case SIW_OP_INVAL_STAG:
969 rv = siw_invalidate_stag(qp->pd, wqe->sqe.rkey);
970 break;
971
972 default:
973 rv = -EINVAL;
974 }
975 return rv;
976}
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005int siw_qp_sq_process(struct siw_qp *qp)
1006{
1007 struct siw_wqe *wqe = tx_wqe(qp);
1008 enum siw_opcode tx_type;
1009 unsigned long flags;
1010 int rv = 0;
1011
1012 siw_dbg_qp(qp, "enter for type %d\n", tx_type(wqe));
1013
1014next_wqe:
1015
1016
1017
1018 if (unlikely(qp->tx_ctx.tx_suspend)) {
1019 siw_dbg_qp(qp, "tx suspended\n");
1020 goto done;
1021 }
1022 tx_type = tx_type(wqe);
1023
1024 if (tx_type <= SIW_OP_READ_RESPONSE)
1025 rv = siw_qp_sq_proc_tx(qp, wqe);
1026 else
1027 rv = siw_qp_sq_proc_local(qp, wqe);
1028
1029 if (!rv) {
1030
1031
1032
1033 switch (tx_type) {
1034 case SIW_OP_SEND:
1035 case SIW_OP_SEND_REMOTE_INV:
1036 case SIW_OP_WRITE:
1037 siw_wqe_put_mem(wqe, tx_type);
1038
1039
1040 case SIW_OP_INVAL_STAG:
1041 case SIW_OP_REG_MR:
1042 if (tx_flags(wqe) & SIW_WQE_SIGNALLED)
1043 siw_sqe_complete(qp, &wqe->sqe, wqe->bytes,
1044 SIW_WC_SUCCESS);
1045 break;
1046
1047 case SIW_OP_READ:
1048 case SIW_OP_READ_LOCAL_INV:
1049
1050
1051
1052 break;
1053
1054 case SIW_OP_READ_RESPONSE:
1055 siw_wqe_put_mem(wqe, tx_type);
1056 break;
1057
1058 default:
1059 WARN(1, "undefined WQE type %d\n", tx_type);
1060 rv = -EINVAL;
1061 goto done;
1062 }
1063
1064 spin_lock_irqsave(&qp->sq_lock, flags);
1065 wqe->wr_status = SIW_WR_IDLE;
1066 rv = siw_activate_tx(qp);
1067 spin_unlock_irqrestore(&qp->sq_lock, flags);
1068
1069 if (rv <= 0)
1070 goto done;
1071
1072 goto next_wqe;
1073
1074 } else if (rv == -EAGAIN) {
1075 siw_dbg_qp(qp, "sq paused: hd/tr %d of %d, data %d\n",
1076 qp->tx_ctx.ctrl_sent, qp->tx_ctx.ctrl_len,
1077 qp->tx_ctx.bytes_unsent);
1078 rv = 0;
1079 goto done;
1080 } else if (rv == -EINPROGRESS) {
1081 rv = siw_sq_start(qp);
1082 goto done;
1083 } else {
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096 siw_dbg_qp(qp, "wqe type %d processing failed: %d\n",
1097 tx_type(wqe), rv);
1098
1099 spin_lock_irqsave(&qp->sq_lock, flags);
1100
1101
1102
1103 if (tx_type == SIW_OP_READ ||
1104 tx_type == SIW_OP_READ_LOCAL_INV) {
1105
1106 qp->orq_put--;
1107 qp->orq[qp->orq_put % qp->attrs.orq_size].flags = 0;
1108 }
1109 spin_unlock_irqrestore(&qp->sq_lock, flags);
1110
1111
1112
1113 if (!qp->tx_ctx.tx_suspend)
1114 siw_qp_cm_drop(qp, 0);
1115
1116 switch (tx_type) {
1117 case SIW_OP_SEND:
1118 case SIW_OP_SEND_REMOTE_INV:
1119 case SIW_OP_SEND_WITH_IMM:
1120 case SIW_OP_WRITE:
1121 case SIW_OP_READ:
1122 case SIW_OP_READ_LOCAL_INV:
1123 siw_wqe_put_mem(wqe, tx_type);
1124
1125
1126 case SIW_OP_INVAL_STAG:
1127 case SIW_OP_REG_MR:
1128 siw_sqe_complete(qp, &wqe->sqe, wqe->bytes,
1129 SIW_WC_LOC_QP_OP_ERR);
1130
1131 siw_qp_event(qp, IB_EVENT_QP_FATAL);
1132
1133 break;
1134
1135 case SIW_OP_READ_RESPONSE:
1136 siw_dbg_qp(qp, "proc. read.response failed: %d\n", rv);
1137
1138 siw_qp_event(qp, IB_EVENT_QP_REQ_ERR);
1139
1140 siw_wqe_put_mem(wqe, SIW_OP_READ_RESPONSE);
1141
1142 break;
1143
1144 default:
1145 WARN(1, "undefined WQE type %d\n", tx_type);
1146 rv = -EINVAL;
1147 }
1148 wqe->wr_status = SIW_WR_IDLE;
1149 }
1150done:
1151 return rv;
1152}
1153
1154static void siw_sq_resume(struct siw_qp *qp)
1155{
1156 if (down_read_trylock(&qp->state_lock)) {
1157 if (likely(qp->attrs.state == SIW_QP_STATE_RTS &&
1158 !qp->tx_ctx.tx_suspend)) {
1159 int rv = siw_qp_sq_process(qp);
1160
1161 up_read(&qp->state_lock);
1162
1163 if (unlikely(rv < 0)) {
1164 siw_dbg_qp(qp, "SQ task failed: err %d\n", rv);
1165
1166 if (!qp->tx_ctx.tx_suspend)
1167 siw_qp_cm_drop(qp, 0);
1168 }
1169 } else {
1170 up_read(&qp->state_lock);
1171 }
1172 } else {
1173 siw_dbg_qp(qp, "Resume SQ while QP locked\n");
1174 }
1175 siw_qp_put(qp);
1176}
1177
1178struct tx_task_t {
1179 struct llist_head active;
1180 wait_queue_head_t waiting;
1181};
1182
1183static DEFINE_PER_CPU(struct tx_task_t, siw_tx_task_g);
1184
1185void siw_stop_tx_thread(int nr_cpu)
1186{
1187 kthread_stop(siw_tx_thread[nr_cpu]);
1188 wake_up(&per_cpu(siw_tx_task_g, nr_cpu).waiting);
1189}
1190
1191int siw_run_sq(void *data)
1192{
1193 const int nr_cpu = (unsigned int)(long)data;
1194 struct llist_node *active;
1195 struct siw_qp *qp;
1196 struct tx_task_t *tx_task = &per_cpu(siw_tx_task_g, nr_cpu);
1197
1198 init_llist_head(&tx_task->active);
1199 init_waitqueue_head(&tx_task->waiting);
1200
1201 while (1) {
1202 struct llist_node *fifo_list = NULL;
1203
1204 wait_event_interruptible(tx_task->waiting,
1205 !llist_empty(&tx_task->active) ||
1206 kthread_should_stop());
1207
1208 if (kthread_should_stop())
1209 break;
1210
1211 active = llist_del_all(&tx_task->active);
1212
1213
1214
1215
1216 while (active) {
1217 struct llist_node *tmp = active;
1218
1219 active = llist_next(active);
1220 tmp->next = fifo_list;
1221 fifo_list = tmp;
1222 }
1223 while (fifo_list) {
1224 qp = container_of(fifo_list, struct siw_qp, tx_list);
1225 fifo_list = llist_next(fifo_list);
1226 qp->tx_list.next = NULL;
1227
1228 siw_sq_resume(qp);
1229 }
1230 }
1231 active = llist_del_all(&tx_task->active);
1232 if (active) {
1233 llist_for_each_entry(qp, active, tx_list) {
1234 qp->tx_list.next = NULL;
1235 siw_sq_resume(qp);
1236 }
1237 }
1238 return 0;
1239}
1240
1241int siw_sq_start(struct siw_qp *qp)
1242{
1243 if (tx_wqe(qp)->wr_status == SIW_WR_IDLE)
1244 return 0;
1245
1246 if (unlikely(!cpu_online(qp->tx_cpu))) {
1247 siw_put_tx_cpu(qp->tx_cpu);
1248 qp->tx_cpu = siw_get_tx_cpu(qp->sdev);
1249 if (qp->tx_cpu < 0) {
1250 pr_warn("siw: no tx cpu available\n");
1251
1252 return -EIO;
1253 }
1254 }
1255 siw_qp_get(qp);
1256
1257 llist_add(&qp->tx_list, &per_cpu(siw_tx_task_g, qp->tx_cpu).active);
1258
1259 wake_up(&per_cpu(siw_tx_task_g, qp->tx_cpu).waiting);
1260
1261 return 0;
1262}
1263