1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#include <linux/mm.h>
33#include <linux/types.h>
34#include <linux/device.h>
35#include <linux/dmapool.h>
36#include <linux/slab.h>
37#include <linux/list.h>
38#include <linux/highmem.h>
39#include <linux/io.h>
40#include <linux/uio.h>
41#include <linux/rbtree.h>
42#include <linux/spinlock.h>
43#include <linux/delay.h>
44
45#include "qib.h"
46#include "qib_user_sdma.h"
47
48
49#define QIB_USER_SDMA_MIN_HEADER_LENGTH 64
50
51#define QIB_USER_SDMA_EXP_HEADER_LENGTH 64
52
53#define QIB_USER_SDMA_DRAIN_TIMEOUT 250
54
55
56
57
58static struct rb_root qib_user_sdma_rb_root = RB_ROOT;
59
60struct qib_user_sdma_rb_node {
61 struct rb_node node;
62 int refcount;
63 pid_t pid;
64};
65
66struct qib_user_sdma_pkt {
67 struct list_head list;
68
69 u8 tiddma;
70 u8 largepkt;
71 u16 frag_size;
72 u16 index;
73 u16 naddr;
74 u16 addrlimit;
75 u16 tidsmidx;
76 u16 tidsmcount;
77 u16 payload_size;
78 u32 bytes_togo;
79 u32 counter;
80 struct qib_tid_session_member *tidsm;
81 struct qib_user_sdma_queue *pq;
82 u64 added;
83
84 struct {
85 u16 offset;
86 u16 length;
87 u16 first_desc;
88 u16 last_desc;
89 u16 put_page;
90 u16 dma_mapped;
91 u16 dma_length;
92 u16 padding;
93 struct page *page;
94 void *kvaddr;
95 dma_addr_t addr;
96 } addr[4];
97};
98
99struct qib_user_sdma_queue {
100
101
102
103
104
105 struct list_head sent;
106
107
108
109
110
111 spinlock_t sent_lock ____cacheline_aligned_in_smp;
112
113
114 char header_cache_name[64];
115 struct dma_pool *header_cache;
116
117
118 char pkt_slab_name[64];
119 struct kmem_cache *pkt_slab;
120
121
122 u32 counter;
123 u32 sent_counter;
124
125 u32 num_pending;
126
127 u32 num_sending;
128
129 u64 added;
130
131
132 struct rb_root dma_pages_root;
133
134 struct qib_user_sdma_rb_node *sdma_rb_node;
135
136
137 struct mutex lock;
138};
139
140static struct qib_user_sdma_rb_node *
141qib_user_sdma_rb_search(struct rb_root *root, pid_t pid)
142{
143 struct qib_user_sdma_rb_node *sdma_rb_node;
144 struct rb_node *node = root->rb_node;
145
146 while (node) {
147 sdma_rb_node = container_of(node,
148 struct qib_user_sdma_rb_node, node);
149 if (pid < sdma_rb_node->pid)
150 node = node->rb_left;
151 else if (pid > sdma_rb_node->pid)
152 node = node->rb_right;
153 else
154 return sdma_rb_node;
155 }
156 return NULL;
157}
158
159static int
160qib_user_sdma_rb_insert(struct rb_root *root, struct qib_user_sdma_rb_node *new)
161{
162 struct rb_node **node = &(root->rb_node);
163 struct rb_node *parent = NULL;
164 struct qib_user_sdma_rb_node *got;
165
166 while (*node) {
167 got = container_of(*node, struct qib_user_sdma_rb_node, node);
168 parent = *node;
169 if (new->pid < got->pid)
170 node = &((*node)->rb_left);
171 else if (new->pid > got->pid)
172 node = &((*node)->rb_right);
173 else
174 return 0;
175 }
176
177 rb_link_node(&new->node, parent, node);
178 rb_insert_color(&new->node, root);
179 return 1;
180}
181
182struct qib_user_sdma_queue *
183qib_user_sdma_queue_create(struct device *dev, int unit, int ctxt, int sctxt)
184{
185 struct qib_user_sdma_queue *pq =
186 kmalloc(sizeof(struct qib_user_sdma_queue), GFP_KERNEL);
187 struct qib_user_sdma_rb_node *sdma_rb_node;
188
189 if (!pq)
190 goto done;
191
192 pq->counter = 0;
193 pq->sent_counter = 0;
194 pq->num_pending = 0;
195 pq->num_sending = 0;
196 pq->added = 0;
197 pq->sdma_rb_node = NULL;
198
199 INIT_LIST_HEAD(&pq->sent);
200 spin_lock_init(&pq->sent_lock);
201 mutex_init(&pq->lock);
202
203 snprintf(pq->pkt_slab_name, sizeof(pq->pkt_slab_name),
204 "qib-user-sdma-pkts-%u-%02u.%02u", unit, ctxt, sctxt);
205 pq->pkt_slab = kmem_cache_create(pq->pkt_slab_name,
206 sizeof(struct qib_user_sdma_pkt),
207 0, 0, NULL);
208
209 if (!pq->pkt_slab)
210 goto err_kfree;
211
212 snprintf(pq->header_cache_name, sizeof(pq->header_cache_name),
213 "qib-user-sdma-headers-%u-%02u.%02u", unit, ctxt, sctxt);
214 pq->header_cache = dma_pool_create(pq->header_cache_name,
215 dev,
216 QIB_USER_SDMA_EXP_HEADER_LENGTH,
217 4, 0);
218 if (!pq->header_cache)
219 goto err_slab;
220
221 pq->dma_pages_root = RB_ROOT;
222
223 sdma_rb_node = qib_user_sdma_rb_search(&qib_user_sdma_rb_root,
224 current->pid);
225 if (sdma_rb_node) {
226 sdma_rb_node->refcount++;
227 } else {
228 int ret;
229
230 sdma_rb_node = kmalloc(sizeof(
231 struct qib_user_sdma_rb_node), GFP_KERNEL);
232 if (!sdma_rb_node)
233 goto err_rb;
234
235 sdma_rb_node->refcount = 1;
236 sdma_rb_node->pid = current->pid;
237
238 ret = qib_user_sdma_rb_insert(&qib_user_sdma_rb_root,
239 sdma_rb_node);
240 BUG_ON(ret == 0);
241 }
242 pq->sdma_rb_node = sdma_rb_node;
243
244 goto done;
245
246err_rb:
247 dma_pool_destroy(pq->header_cache);
248err_slab:
249 kmem_cache_destroy(pq->pkt_slab);
250err_kfree:
251 kfree(pq);
252 pq = NULL;
253
254done:
255 return pq;
256}
257
258static void qib_user_sdma_init_frag(struct qib_user_sdma_pkt *pkt,
259 int i, u16 offset, u16 len,
260 u16 first_desc, u16 last_desc,
261 u16 put_page, u16 dma_mapped,
262 struct page *page, void *kvaddr,
263 dma_addr_t dma_addr, u16 dma_length)
264{
265 pkt->addr[i].offset = offset;
266 pkt->addr[i].length = len;
267 pkt->addr[i].first_desc = first_desc;
268 pkt->addr[i].last_desc = last_desc;
269 pkt->addr[i].put_page = put_page;
270 pkt->addr[i].dma_mapped = dma_mapped;
271 pkt->addr[i].page = page;
272 pkt->addr[i].kvaddr = kvaddr;
273 pkt->addr[i].addr = dma_addr;
274 pkt->addr[i].dma_length = dma_length;
275}
276
277static void *qib_user_sdma_alloc_header(struct qib_user_sdma_queue *pq,
278 size_t len, dma_addr_t *dma_addr)
279{
280 void *hdr;
281
282 if (len == QIB_USER_SDMA_EXP_HEADER_LENGTH)
283 hdr = dma_pool_alloc(pq->header_cache, GFP_KERNEL,
284 dma_addr);
285 else
286 hdr = NULL;
287
288 if (!hdr) {
289 hdr = kmalloc(len, GFP_KERNEL);
290 if (!hdr)
291 return NULL;
292
293 *dma_addr = 0;
294 }
295
296 return hdr;
297}
298
299static int qib_user_sdma_page_to_frags(const struct qib_devdata *dd,
300 struct qib_user_sdma_queue *pq,
301 struct qib_user_sdma_pkt *pkt,
302 struct page *page, u16 put,
303 u16 offset, u16 len, void *kvaddr)
304{
305 __le16 *pbc16;
306 void *pbcvaddr;
307 struct qib_message_header *hdr;
308 u16 newlen, pbclen, lastdesc, dma_mapped;
309 u32 vcto;
310 union qib_seqnum seqnum;
311 dma_addr_t pbcdaddr;
312 dma_addr_t dma_addr =
313 dma_map_page(&dd->pcidev->dev,
314 page, offset, len, DMA_TO_DEVICE);
315 int ret = 0;
316
317 if (dma_mapping_error(&dd->pcidev->dev, dma_addr)) {
318
319
320
321
322
323 if (put) {
324 put_page(page);
325 } else {
326
327 kunmap(page);
328 __free_page(page);
329 }
330 ret = -ENOMEM;
331 goto done;
332 }
333 offset = 0;
334 dma_mapped = 1;
335
336
337next_fragment:
338
339
340
341
342
343 if (pkt->tiddma && len > pkt->tidsm[pkt->tidsmidx].length)
344 newlen = pkt->tidsm[pkt->tidsmidx].length;
345 else
346 newlen = len;
347
348
349
350
351
352
353
354
355 lastdesc = 0;
356 if ((pkt->payload_size + newlen) >= pkt->frag_size) {
357 newlen = pkt->frag_size - pkt->payload_size;
358 lastdesc = 1;
359 } else if (pkt->tiddma) {
360 if (newlen == pkt->tidsm[pkt->tidsmidx].length)
361 lastdesc = 1;
362 } else {
363 if (newlen == pkt->bytes_togo)
364 lastdesc = 1;
365 }
366
367
368 qib_user_sdma_init_frag(pkt, pkt->naddr,
369 offset, newlen,
370 0, lastdesc,
371 put, dma_mapped,
372 page, kvaddr,
373 dma_addr, len);
374 pkt->bytes_togo -= newlen;
375 pkt->payload_size += newlen;
376 pkt->naddr++;
377 if (pkt->naddr == pkt->addrlimit) {
378 ret = -EFAULT;
379 goto done;
380 }
381
382
383 if (pkt->bytes_togo == 0) {
384
385
386 if (!pkt->addr[pkt->index].addr) {
387 pkt->addr[pkt->index].addr =
388 dma_map_single(&dd->pcidev->dev,
389 pkt->addr[pkt->index].kvaddr,
390 pkt->addr[pkt->index].dma_length,
391 DMA_TO_DEVICE);
392 if (dma_mapping_error(&dd->pcidev->dev,
393 pkt->addr[pkt->index].addr)) {
394 ret = -ENOMEM;
395 goto done;
396 }
397 pkt->addr[pkt->index].dma_mapped = 1;
398 }
399
400 goto done;
401 }
402
403
404 if (pkt->tiddma) {
405 pkt->tidsm[pkt->tidsmidx].length -= newlen;
406 if (pkt->tidsm[pkt->tidsmidx].length) {
407 pkt->tidsm[pkt->tidsmidx].offset += newlen;
408 } else {
409 pkt->tidsmidx++;
410 if (pkt->tidsmidx == pkt->tidsmcount) {
411 ret = -EFAULT;
412 goto done;
413 }
414 }
415 }
416
417
418
419
420
421
422 if (lastdesc == 0)
423 goto done;
424
425
426
427
428
429
430
431
432
433
434
435
436 pbclen = pkt->addr[pkt->index].length;
437 pbcvaddr = qib_user_sdma_alloc_header(pq, pbclen, &pbcdaddr);
438 if (!pbcvaddr) {
439 ret = -ENOMEM;
440 goto done;
441 }
442
443 pbc16 = (__le16 *)pkt->addr[pkt->index].kvaddr;
444 memcpy(pbcvaddr, pbc16, pbclen);
445
446
447 hdr = (struct qib_message_header *)&pbc16[4];
448
449
450 pbc16[0] = cpu_to_le16(le16_to_cpu(pbc16[0])-(pkt->bytes_togo>>2));
451
452
453 hdr->lrh[2] = cpu_to_be16(le16_to_cpu(pbc16[0]));
454
455 if (pkt->tiddma) {
456
457 hdr->iph.pkt_flags =
458 cpu_to_le16(le16_to_cpu(hdr->iph.pkt_flags)|0x2);
459
460 hdr->flags &= ~(0x04|0x20);
461 } else {
462
463 hdr->bth[0] = cpu_to_be32(be32_to_cpu(hdr->bth[0])&0xFFCFFFFF);
464
465 hdr->flags &= ~(0x04);
466 }
467
468
469 vcto = le32_to_cpu(hdr->iph.ver_ctxt_tid_offset);
470 hdr->iph.chksum = cpu_to_le16(QIB_LRH_BTH +
471 be16_to_cpu(hdr->lrh[2]) -
472 ((vcto>>16)&0xFFFF) - (vcto&0xFFFF) -
473 le16_to_cpu(hdr->iph.pkt_flags));
474
475
476
477 if (!pkt->addr[pkt->index].addr) {
478 pkt->addr[pkt->index].addr =
479 dma_map_single(&dd->pcidev->dev,
480 pkt->addr[pkt->index].kvaddr,
481 pkt->addr[pkt->index].dma_length,
482 DMA_TO_DEVICE);
483 if (dma_mapping_error(&dd->pcidev->dev,
484 pkt->addr[pkt->index].addr)) {
485 ret = -ENOMEM;
486 goto done;
487 }
488 pkt->addr[pkt->index].dma_mapped = 1;
489 }
490
491
492 pbc16 = (__le16 *)pbcvaddr;
493 hdr = (struct qib_message_header *)&pbc16[4];
494
495
496 pbc16[0] = cpu_to_le16(le16_to_cpu(pbc16[0])-(pkt->payload_size>>2));
497
498
499 hdr->lrh[2] = cpu_to_be16(le16_to_cpu(pbc16[0]));
500
501 if (pkt->tiddma) {
502
503 hdr->iph.ver_ctxt_tid_offset = cpu_to_le32(
504 (le32_to_cpu(hdr->iph.ver_ctxt_tid_offset)&0xFF000000) +
505 (pkt->tidsm[pkt->tidsmidx].tid<<QLOGIC_IB_I_TID_SHIFT) +
506 (pkt->tidsm[pkt->tidsmidx].offset>>2));
507 } else {
508
509 hdr->uwords[2] += pkt->payload_size;
510 }
511
512
513 vcto = le32_to_cpu(hdr->iph.ver_ctxt_tid_offset);
514 hdr->iph.chksum = cpu_to_le16(QIB_LRH_BTH +
515 be16_to_cpu(hdr->lrh[2]) -
516 ((vcto>>16)&0xFFFF) - (vcto&0xFFFF) -
517 le16_to_cpu(hdr->iph.pkt_flags));
518
519
520 seqnum.val = be32_to_cpu(hdr->bth[2]);
521 if (pkt->tiddma)
522 seqnum.seq++;
523 else
524 seqnum.pkt++;
525 hdr->bth[2] = cpu_to_be32(seqnum.val);
526
527
528 qib_user_sdma_init_frag(pkt, pkt->naddr,
529 0, pbclen,
530 1, 0,
531 0, 0,
532 NULL, pbcvaddr,
533 pbcdaddr, pbclen);
534 pkt->index = pkt->naddr;
535 pkt->payload_size = 0;
536 pkt->naddr++;
537 if (pkt->naddr == pkt->addrlimit) {
538 ret = -EFAULT;
539 goto done;
540 }
541
542
543 if (newlen != len) {
544 if (dma_mapped) {
545 put = 0;
546 dma_mapped = 0;
547 page = NULL;
548 kvaddr = NULL;
549 }
550 len -= newlen;
551 offset += newlen;
552
553 goto next_fragment;
554 }
555
556done:
557 return ret;
558}
559
560
561static int qib_user_sdma_coalesce(const struct qib_devdata *dd,
562 struct qib_user_sdma_queue *pq,
563 struct qib_user_sdma_pkt *pkt,
564 const struct iovec *iov,
565 unsigned long niov)
566{
567 int ret = 0;
568 struct page *page = alloc_page(GFP_KERNEL);
569 void *mpage_save;
570 char *mpage;
571 int i;
572 int len = 0;
573
574 if (!page) {
575 ret = -ENOMEM;
576 goto done;
577 }
578
579 mpage = kmap(page);
580 mpage_save = mpage;
581 for (i = 0; i < niov; i++) {
582 int cfur;
583
584 cfur = copy_from_user(mpage,
585 iov[i].iov_base, iov[i].iov_len);
586 if (cfur) {
587 ret = -EFAULT;
588 goto free_unmap;
589 }
590
591 mpage += iov[i].iov_len;
592 len += iov[i].iov_len;
593 }
594
595 ret = qib_user_sdma_page_to_frags(dd, pq, pkt,
596 page, 0, 0, len, mpage_save);
597 goto done;
598
599free_unmap:
600 kunmap(page);
601 __free_page(page);
602done:
603 return ret;
604}
605
606
607
608
609static int qib_user_sdma_num_pages(const struct iovec *iov)
610{
611 const unsigned long addr = (unsigned long) iov->iov_base;
612 const unsigned long len = iov->iov_len;
613 const unsigned long spage = addr & PAGE_MASK;
614 const unsigned long epage = (addr + len - 1) & PAGE_MASK;
615
616 return 1 + ((epage - spage) >> PAGE_SHIFT);
617}
618
619static void qib_user_sdma_free_pkt_frag(struct device *dev,
620 struct qib_user_sdma_queue *pq,
621 struct qib_user_sdma_pkt *pkt,
622 int frag)
623{
624 const int i = frag;
625
626 if (pkt->addr[i].page) {
627
628 if (pkt->addr[i].dma_mapped)
629 dma_unmap_page(dev,
630 pkt->addr[i].addr,
631 pkt->addr[i].dma_length,
632 DMA_TO_DEVICE);
633
634 if (pkt->addr[i].kvaddr)
635 kunmap(pkt->addr[i].page);
636
637 if (pkt->addr[i].put_page)
638 put_page(pkt->addr[i].page);
639 else
640 __free_page(pkt->addr[i].page);
641 } else if (pkt->addr[i].kvaddr) {
642
643 if (pkt->addr[i].dma_mapped) {
644
645 dma_unmap_single(dev,
646 pkt->addr[i].addr,
647 pkt->addr[i].dma_length,
648 DMA_TO_DEVICE);
649 kfree(pkt->addr[i].kvaddr);
650 } else if (pkt->addr[i].addr) {
651
652 dma_pool_free(pq->header_cache,
653 pkt->addr[i].kvaddr, pkt->addr[i].addr);
654 } else {
655
656 kfree(pkt->addr[i].kvaddr);
657 }
658 }
659}
660
661
662static int qib_user_sdma_pin_pages(const struct qib_devdata *dd,
663 struct qib_user_sdma_queue *pq,
664 struct qib_user_sdma_pkt *pkt,
665 unsigned long addr, int tlen, int npages)
666{
667 struct page *pages[8];
668 int i, j;
669 int ret = 0;
670
671 while (npages) {
672 if (npages > 8)
673 j = 8;
674 else
675 j = npages;
676
677 ret = get_user_pages_fast(addr, j, 0, pages);
678 if (ret != j) {
679 i = 0;
680 j = ret;
681 ret = -ENOMEM;
682 goto free_pages;
683 }
684
685 for (i = 0; i < j; i++) {
686
687 unsigned long fofs = addr & ~PAGE_MASK;
688 int flen = ((fofs + tlen) > PAGE_SIZE) ?
689 (PAGE_SIZE - fofs) : tlen;
690
691 ret = qib_user_sdma_page_to_frags(dd, pq, pkt,
692 pages[i], 1, fofs, flen, NULL);
693 if (ret < 0) {
694
695
696
697 i++;
698 goto free_pages;
699 }
700
701 addr += flen;
702 tlen -= flen;
703 }
704
705 npages -= j;
706 }
707
708 goto done;
709
710
711free_pages:
712 while (i < j)
713 put_page(pages[i++]);
714
715done:
716 return ret;
717}
718
719static int qib_user_sdma_pin_pkt(const struct qib_devdata *dd,
720 struct qib_user_sdma_queue *pq,
721 struct qib_user_sdma_pkt *pkt,
722 const struct iovec *iov,
723 unsigned long niov)
724{
725 int ret = 0;
726 unsigned long idx;
727
728 for (idx = 0; idx < niov; idx++) {
729 const int npages = qib_user_sdma_num_pages(iov + idx);
730 const unsigned long addr = (unsigned long) iov[idx].iov_base;
731
732 ret = qib_user_sdma_pin_pages(dd, pq, pkt, addr,
733 iov[idx].iov_len, npages);
734 if (ret < 0)
735 goto free_pkt;
736 }
737
738 goto done;
739
740free_pkt:
741
742 for (idx = 1; idx < pkt->naddr; idx++)
743 qib_user_sdma_free_pkt_frag(&dd->pcidev->dev, pq, pkt, idx);
744
745
746
747
748 if (pkt->addr[0].dma_mapped) {
749 dma_unmap_single(&dd->pcidev->dev,
750 pkt->addr[0].addr,
751 pkt->addr[0].dma_length,
752 DMA_TO_DEVICE);
753 pkt->addr[0].addr = 0;
754 pkt->addr[0].dma_mapped = 0;
755 }
756
757done:
758 return ret;
759}
760
761static int qib_user_sdma_init_payload(const struct qib_devdata *dd,
762 struct qib_user_sdma_queue *pq,
763 struct qib_user_sdma_pkt *pkt,
764 const struct iovec *iov,
765 unsigned long niov, int npages)
766{
767 int ret = 0;
768
769 if (pkt->frag_size == pkt->bytes_togo &&
770 npages >= ARRAY_SIZE(pkt->addr))
771 ret = qib_user_sdma_coalesce(dd, pq, pkt, iov, niov);
772 else
773 ret = qib_user_sdma_pin_pkt(dd, pq, pkt, iov, niov);
774
775 return ret;
776}
777
778
779static void qib_user_sdma_free_pkt_list(struct device *dev,
780 struct qib_user_sdma_queue *pq,
781 struct list_head *list)
782{
783 struct qib_user_sdma_pkt *pkt, *pkt_next;
784
785 list_for_each_entry_safe(pkt, pkt_next, list, list) {
786 int i;
787
788 for (i = 0; i < pkt->naddr; i++)
789 qib_user_sdma_free_pkt_frag(dev, pq, pkt, i);
790
791 if (pkt->largepkt)
792 kfree(pkt);
793 else
794 kmem_cache_free(pq->pkt_slab, pkt);
795 }
796 INIT_LIST_HEAD(list);
797}
798
799
800
801
802
803
804
805
806static int qib_user_sdma_queue_pkts(const struct qib_devdata *dd,
807 struct qib_pportdata *ppd,
808 struct qib_user_sdma_queue *pq,
809 const struct iovec *iov,
810 unsigned long niov,
811 struct list_head *list,
812 int *maxpkts, int *ndesc)
813{
814 unsigned long idx = 0;
815 int ret = 0;
816 int npkts = 0;
817 __le32 *pbc;
818 dma_addr_t dma_addr;
819 struct qib_user_sdma_pkt *pkt = NULL;
820 size_t len;
821 size_t nw;
822 u32 counter = pq->counter;
823 u16 frag_size;
824
825 while (idx < niov && npkts < *maxpkts) {
826 const unsigned long addr = (unsigned long) iov[idx].iov_base;
827 const unsigned long idx_save = idx;
828 unsigned pktnw;
829 unsigned pktnwc;
830 int nfrags = 0;
831 int npages = 0;
832 int bytes_togo = 0;
833 int tiddma = 0;
834 int cfur;
835
836 len = iov[idx].iov_len;
837 nw = len >> 2;
838
839 if (len < QIB_USER_SDMA_MIN_HEADER_LENGTH ||
840 len > PAGE_SIZE || len & 3 || addr & 3) {
841 ret = -EINVAL;
842 goto free_list;
843 }
844
845 pbc = qib_user_sdma_alloc_header(pq, len, &dma_addr);
846 if (!pbc) {
847 ret = -ENOMEM;
848 goto free_list;
849 }
850
851 cfur = copy_from_user(pbc, iov[idx].iov_base, len);
852 if (cfur) {
853 ret = -EFAULT;
854 goto free_pbc;
855 }
856
857
858
859
860
861
862 pktnwc = nw - 1;
863
864
865
866
867
868
869
870
871
872
873 pktnw = le32_to_cpu(*pbc) & 0xFFFF;
874 if (pktnw < pktnwc) {
875 ret = -EINVAL;
876 goto free_pbc;
877 }
878
879 idx++;
880 while (pktnwc < pktnw && idx < niov) {
881 const size_t slen = iov[idx].iov_len;
882 const unsigned long faddr =
883 (unsigned long) iov[idx].iov_base;
884
885 if (slen & 3 || faddr & 3 || !slen) {
886 ret = -EINVAL;
887 goto free_pbc;
888 }
889
890 npages += qib_user_sdma_num_pages(&iov[idx]);
891
892 bytes_togo += slen;
893 pktnwc += slen >> 2;
894 idx++;
895 nfrags++;
896 }
897
898 if (pktnwc != pktnw) {
899 ret = -EINVAL;
900 goto free_pbc;
901 }
902
903 frag_size = ((le32_to_cpu(*pbc))>>16) & 0xFFFF;
904 if (((frag_size ? frag_size : bytes_togo) + len) >
905 ppd->ibmaxlen) {
906 ret = -EINVAL;
907 goto free_pbc;
908 }
909
910 if (frag_size) {
911 int pktsize, tidsmsize, n;
912
913 n = npages*((2*PAGE_SIZE/frag_size)+1);
914 pktsize = sizeof(*pkt) + sizeof(pkt->addr[0])*n;
915
916
917
918
919 tiddma = (((le32_to_cpu(pbc[7])>>
920 QLOGIC_IB_I_TID_SHIFT)&
921 QLOGIC_IB_I_TID_MASK) !=
922 QLOGIC_IB_I_TID_MASK);
923
924 if (tiddma)
925 tidsmsize = iov[idx].iov_len;
926 else
927 tidsmsize = 0;
928
929 pkt = kmalloc(pktsize+tidsmsize, GFP_KERNEL);
930 if (!pkt) {
931 ret = -ENOMEM;
932 goto free_pbc;
933 }
934 pkt->largepkt = 1;
935 pkt->frag_size = frag_size;
936 pkt->addrlimit = n + ARRAY_SIZE(pkt->addr);
937
938 if (tiddma) {
939 char *tidsm = (char *)pkt + pktsize;
940
941 cfur = copy_from_user(tidsm,
942 iov[idx].iov_base, tidsmsize);
943 if (cfur) {
944 ret = -EFAULT;
945 goto free_pkt;
946 }
947 pkt->tidsm =
948 (struct qib_tid_session_member *)tidsm;
949 pkt->tidsmcount = tidsmsize/
950 sizeof(struct qib_tid_session_member);
951 pkt->tidsmidx = 0;
952 idx++;
953 }
954
955
956
957
958
959
960 *pbc = cpu_to_le32(le32_to_cpu(*pbc) & 0x0000FFFF);
961 } else {
962 pkt = kmem_cache_alloc(pq->pkt_slab, GFP_KERNEL);
963 if (!pkt) {
964 ret = -ENOMEM;
965 goto free_pbc;
966 }
967 pkt->largepkt = 0;
968 pkt->frag_size = bytes_togo;
969 pkt->addrlimit = ARRAY_SIZE(pkt->addr);
970 }
971 pkt->bytes_togo = bytes_togo;
972 pkt->payload_size = 0;
973 pkt->counter = counter;
974 pkt->tiddma = tiddma;
975
976
977 qib_user_sdma_init_frag(pkt, 0,
978 0, len,
979 1, 0,
980 0, 0,
981 NULL, pbc,
982 dma_addr, len);
983 pkt->index = 0;
984 pkt->naddr = 1;
985
986 if (nfrags) {
987 ret = qib_user_sdma_init_payload(dd, pq, pkt,
988 iov + idx_save + 1,
989 nfrags, npages);
990 if (ret < 0)
991 goto free_pkt;
992 } else {
993
994
995 pkt->addr[0].last_desc = 1;
996
997 if (dma_addr == 0) {
998
999
1000
1001
1002 dma_addr = dma_map_single(&dd->pcidev->dev,
1003 pbc, len, DMA_TO_DEVICE);
1004 if (dma_mapping_error(&dd->pcidev->dev,
1005 dma_addr)) {
1006 ret = -ENOMEM;
1007 goto free_pkt;
1008 }
1009 pkt->addr[0].addr = dma_addr;
1010 pkt->addr[0].dma_mapped = 1;
1011 }
1012 }
1013
1014 counter++;
1015 npkts++;
1016 pkt->pq = pq;
1017 pkt->index = 0;
1018 *ndesc += pkt->naddr;
1019
1020 list_add_tail(&pkt->list, list);
1021 }
1022
1023 *maxpkts = npkts;
1024 ret = idx;
1025 goto done;
1026
1027free_pkt:
1028 if (pkt->largepkt)
1029 kfree(pkt);
1030 else
1031 kmem_cache_free(pq->pkt_slab, pkt);
1032free_pbc:
1033 if (dma_addr)
1034 dma_pool_free(pq->header_cache, pbc, dma_addr);
1035 else
1036 kfree(pbc);
1037free_list:
1038 qib_user_sdma_free_pkt_list(&dd->pcidev->dev, pq, list);
1039done:
1040 return ret;
1041}
1042
1043static void qib_user_sdma_set_complete_counter(struct qib_user_sdma_queue *pq,
1044 u32 c)
1045{
1046 pq->sent_counter = c;
1047}
1048
1049
1050static int qib_user_sdma_queue_clean(struct qib_pportdata *ppd,
1051 struct qib_user_sdma_queue *pq)
1052{
1053 struct qib_devdata *dd = ppd->dd;
1054 struct list_head free_list;
1055 struct qib_user_sdma_pkt *pkt;
1056 struct qib_user_sdma_pkt *pkt_prev;
1057 unsigned long flags;
1058 int ret = 0;
1059
1060 if (!pq->num_sending)
1061 return 0;
1062
1063 INIT_LIST_HEAD(&free_list);
1064
1065
1066
1067
1068
1069
1070 spin_lock_irqsave(&pq->sent_lock, flags);
1071 list_for_each_entry_safe(pkt, pkt_prev, &pq->sent, list) {
1072 s64 descd = ppd->sdma_descq_removed - pkt->added;
1073
1074 if (descd < 0)
1075 break;
1076
1077 list_move_tail(&pkt->list, &free_list);
1078
1079
1080 ret++;
1081 pq->num_sending--;
1082 }
1083 spin_unlock_irqrestore(&pq->sent_lock, flags);
1084
1085 if (!list_empty(&free_list)) {
1086 u32 counter;
1087
1088 pkt = list_entry(free_list.prev,
1089 struct qib_user_sdma_pkt, list);
1090 counter = pkt->counter;
1091
1092 qib_user_sdma_free_pkt_list(&dd->pcidev->dev, pq, &free_list);
1093 qib_user_sdma_set_complete_counter(pq, counter);
1094 }
1095
1096 return ret;
1097}
1098
1099void qib_user_sdma_queue_destroy(struct qib_user_sdma_queue *pq)
1100{
1101 if (!pq)
1102 return;
1103
1104 pq->sdma_rb_node->refcount--;
1105 if (pq->sdma_rb_node->refcount == 0) {
1106 rb_erase(&pq->sdma_rb_node->node, &qib_user_sdma_rb_root);
1107 kfree(pq->sdma_rb_node);
1108 }
1109 dma_pool_destroy(pq->header_cache);
1110 kmem_cache_destroy(pq->pkt_slab);
1111 kfree(pq);
1112}
1113
1114
1115static int qib_user_sdma_hwqueue_clean(struct qib_pportdata *ppd)
1116{
1117 int ret;
1118 unsigned long flags;
1119
1120 spin_lock_irqsave(&ppd->sdma_lock, flags);
1121 ret = qib_sdma_make_progress(ppd);
1122 spin_unlock_irqrestore(&ppd->sdma_lock, flags);
1123
1124 return ret;
1125}
1126
1127
1128void qib_user_sdma_queue_drain(struct qib_pportdata *ppd,
1129 struct qib_user_sdma_queue *pq)
1130{
1131 struct qib_devdata *dd = ppd->dd;
1132 unsigned long flags;
1133 int i;
1134
1135 if (!pq)
1136 return;
1137
1138 for (i = 0; i < QIB_USER_SDMA_DRAIN_TIMEOUT; i++) {
1139 mutex_lock(&pq->lock);
1140 if (!pq->num_pending && !pq->num_sending) {
1141 mutex_unlock(&pq->lock);
1142 break;
1143 }
1144 qib_user_sdma_hwqueue_clean(ppd);
1145 qib_user_sdma_queue_clean(ppd, pq);
1146 mutex_unlock(&pq->lock);
1147 msleep(20);
1148 }
1149
1150 if (pq->num_pending || pq->num_sending) {
1151 struct qib_user_sdma_pkt *pkt;
1152 struct qib_user_sdma_pkt *pkt_prev;
1153 struct list_head free_list;
1154
1155 mutex_lock(&pq->lock);
1156 spin_lock_irqsave(&ppd->sdma_lock, flags);
1157
1158
1159
1160 if (pq->num_pending) {
1161 list_for_each_entry_safe(pkt, pkt_prev,
1162 &ppd->sdma_userpending, list) {
1163 if (pkt->pq == pq) {
1164 list_move_tail(&pkt->list, &pq->sent);
1165 pq->num_pending--;
1166 pq->num_sending++;
1167 }
1168 }
1169 }
1170 spin_unlock_irqrestore(&ppd->sdma_lock, flags);
1171
1172 qib_dev_err(dd, "user sdma lists not empty: forcing!\n");
1173 INIT_LIST_HEAD(&free_list);
1174 list_splice_init(&pq->sent, &free_list);
1175 pq->num_sending = 0;
1176 qib_user_sdma_free_pkt_list(&dd->pcidev->dev, pq, &free_list);
1177 mutex_unlock(&pq->lock);
1178 }
1179}
1180
1181static inline __le64 qib_sdma_make_desc0(u8 gen,
1182 u64 addr, u64 dwlen, u64 dwoffset)
1183{
1184 return cpu_to_le64(
1185 ((addr & 0xfffffffcULL) << 32) |
1186
1187 ((gen & 3ULL) << 30) |
1188
1189 ((dwlen & 0x7ffULL) << 16) |
1190
1191 (dwoffset & 0x7ffULL));
1192}
1193
1194static inline __le64 qib_sdma_make_first_desc0(__le64 descq)
1195{
1196 return descq | cpu_to_le64(1ULL << 12);
1197}
1198
1199static inline __le64 qib_sdma_make_last_desc0(__le64 descq)
1200{
1201
1202 return descq | cpu_to_le64(1ULL << 11 | 1ULL << 13);
1203}
1204
1205static inline __le64 qib_sdma_make_desc1(u64 addr)
1206{
1207
1208 return cpu_to_le64(addr >> 32);
1209}
1210
1211static void qib_user_sdma_send_frag(struct qib_pportdata *ppd,
1212 struct qib_user_sdma_pkt *pkt, int idx,
1213 unsigned ofs, u16 tail, u8 gen)
1214{
1215 const u64 addr = (u64) pkt->addr[idx].addr +
1216 (u64) pkt->addr[idx].offset;
1217 const u64 dwlen = (u64) pkt->addr[idx].length / 4;
1218 __le64 *descqp;
1219 __le64 descq0;
1220
1221 descqp = &ppd->sdma_descq[tail].qw[0];
1222
1223 descq0 = qib_sdma_make_desc0(gen, addr, dwlen, ofs);
1224 if (pkt->addr[idx].first_desc)
1225 descq0 = qib_sdma_make_first_desc0(descq0);
1226 if (pkt->addr[idx].last_desc) {
1227 descq0 = qib_sdma_make_last_desc0(descq0);
1228 if (ppd->sdma_intrequest) {
1229 descq0 |= cpu_to_le64(1ULL << 15);
1230 ppd->sdma_intrequest = 0;
1231 }
1232 }
1233
1234 descqp[0] = descq0;
1235 descqp[1] = qib_sdma_make_desc1(addr);
1236}
1237
1238void qib_user_sdma_send_desc(struct qib_pportdata *ppd,
1239 struct list_head *pktlist)
1240{
1241 struct qib_devdata *dd = ppd->dd;
1242 u16 nfree, nsent;
1243 u16 tail, tail_c;
1244 u8 gen, gen_c;
1245
1246 nfree = qib_sdma_descq_freecnt(ppd);
1247 if (!nfree)
1248 return;
1249
1250retry:
1251 nsent = 0;
1252 tail_c = tail = ppd->sdma_descq_tail;
1253 gen_c = gen = ppd->sdma_generation;
1254 while (!list_empty(pktlist)) {
1255 struct qib_user_sdma_pkt *pkt =
1256 list_entry(pktlist->next, struct qib_user_sdma_pkt,
1257 list);
1258 int i, j, c = 0;
1259 unsigned ofs = 0;
1260 u16 dtail = tail;
1261
1262 for (i = pkt->index; i < pkt->naddr && nfree; i++) {
1263 qib_user_sdma_send_frag(ppd, pkt, i, ofs, tail, gen);
1264 ofs += pkt->addr[i].length >> 2;
1265
1266 if (++tail == ppd->sdma_descq_cnt) {
1267 tail = 0;
1268 ++gen;
1269 ppd->sdma_intrequest = 1;
1270 } else if (tail == (ppd->sdma_descq_cnt>>1)) {
1271 ppd->sdma_intrequest = 1;
1272 }
1273 nfree--;
1274 if (pkt->addr[i].last_desc == 0)
1275 continue;
1276
1277
1278
1279
1280
1281
1282
1283 if (ofs > dd->piosize2kmax_dwords) {
1284 for (j = pkt->index; j <= i; j++) {
1285 ppd->sdma_descq[dtail].qw[0] |=
1286 cpu_to_le64(1ULL << 14);
1287 if (++dtail == ppd->sdma_descq_cnt)
1288 dtail = 0;
1289 }
1290 }
1291 c += i + 1 - pkt->index;
1292 pkt->index = i + 1;
1293 tail_c = dtail = tail;
1294 gen_c = gen;
1295 ofs = 0;
1296 }
1297
1298 ppd->sdma_descq_added += c;
1299 nsent += c;
1300 if (pkt->index == pkt->naddr) {
1301 pkt->added = ppd->sdma_descq_added;
1302 pkt->pq->added = pkt->added;
1303 pkt->pq->num_pending--;
1304 spin_lock(&pkt->pq->sent_lock);
1305 pkt->pq->num_sending++;
1306 list_move_tail(&pkt->list, &pkt->pq->sent);
1307 spin_unlock(&pkt->pq->sent_lock);
1308 }
1309 if (!nfree || (nsent<<2) > ppd->sdma_descq_cnt)
1310 break;
1311 }
1312
1313
1314 if (ppd->sdma_descq_tail != tail_c) {
1315 ppd->sdma_generation = gen_c;
1316 dd->f_sdma_update_tail(ppd, tail_c);
1317 }
1318
1319 if (nfree && !list_empty(pktlist))
1320 goto retry;
1321}
1322
1323
1324static int qib_user_sdma_push_pkts(struct qib_pportdata *ppd,
1325 struct qib_user_sdma_queue *pq,
1326 struct list_head *pktlist, int count)
1327{
1328 unsigned long flags;
1329
1330 if (unlikely(!(ppd->lflags & QIBL_LINKACTIVE)))
1331 return -ECOMM;
1332
1333
1334 if (pq->sdma_rb_node->refcount > 1) {
1335 spin_lock_irqsave(&ppd->sdma_lock, flags);
1336 if (unlikely(!__qib_sdma_running(ppd))) {
1337 spin_unlock_irqrestore(&ppd->sdma_lock, flags);
1338 return -ECOMM;
1339 }
1340 pq->num_pending += count;
1341 list_splice_tail_init(pktlist, &ppd->sdma_userpending);
1342 qib_user_sdma_send_desc(ppd, &ppd->sdma_userpending);
1343 spin_unlock_irqrestore(&ppd->sdma_lock, flags);
1344 return 0;
1345 }
1346
1347
1348
1349
1350
1351
1352
1353
1354 pq->num_pending += count;
1355
1356
1357
1358
1359
1360
1361 do {
1362 spin_lock_irqsave(&ppd->sdma_lock, flags);
1363 if (unlikely(!__qib_sdma_running(ppd))) {
1364 spin_unlock_irqrestore(&ppd->sdma_lock, flags);
1365 return -ECOMM;
1366 }
1367 qib_user_sdma_send_desc(ppd, pktlist);
1368 if (!list_empty(pktlist))
1369 qib_sdma_make_progress(ppd);
1370 spin_unlock_irqrestore(&ppd->sdma_lock, flags);
1371 } while (!list_empty(pktlist));
1372
1373 return 0;
1374}
1375
1376int qib_user_sdma_writev(struct qib_ctxtdata *rcd,
1377 struct qib_user_sdma_queue *pq,
1378 const struct iovec *iov,
1379 unsigned long dim)
1380{
1381 struct qib_devdata *dd = rcd->dd;
1382 struct qib_pportdata *ppd = rcd->ppd;
1383 int ret = 0;
1384 struct list_head list;
1385 int npkts = 0;
1386
1387 INIT_LIST_HEAD(&list);
1388
1389 mutex_lock(&pq->lock);
1390
1391
1392 if (!qib_sdma_running(ppd))
1393 goto done_unlock;
1394
1395
1396 if (pq->added > ppd->sdma_descq_removed)
1397 qib_user_sdma_hwqueue_clean(ppd);
1398
1399 if (pq->num_sending)
1400 qib_user_sdma_queue_clean(ppd, pq);
1401
1402 while (dim) {
1403 int mxp = 1;
1404 int ndesc = 0;
1405
1406 ret = qib_user_sdma_queue_pkts(dd, ppd, pq,
1407 iov, dim, &list, &mxp, &ndesc);
1408 if (ret < 0)
1409 goto done_unlock;
1410 else {
1411 dim -= ret;
1412 iov += ret;
1413 }
1414
1415
1416 if (!list_empty(&list)) {
1417
1418
1419
1420 if (qib_sdma_descq_freecnt(ppd) < ndesc) {
1421 qib_user_sdma_hwqueue_clean(ppd);
1422 if (pq->num_sending)
1423 qib_user_sdma_queue_clean(ppd, pq);
1424 }
1425
1426 ret = qib_user_sdma_push_pkts(ppd, pq, &list, mxp);
1427 if (ret < 0)
1428 goto done_unlock;
1429 else {
1430 npkts += mxp;
1431 pq->counter += mxp;
1432 }
1433 }
1434 }
1435
1436done_unlock:
1437 if (!list_empty(&list))
1438 qib_user_sdma_free_pkt_list(&dd->pcidev->dev, pq, &list);
1439 mutex_unlock(&pq->lock);
1440
1441 return (ret < 0) ? ret : npkts;
1442}
1443
1444int qib_user_sdma_make_progress(struct qib_pportdata *ppd,
1445 struct qib_user_sdma_queue *pq)
1446{
1447 int ret = 0;
1448
1449 mutex_lock(&pq->lock);
1450 qib_user_sdma_hwqueue_clean(ppd);
1451 ret = qib_user_sdma_queue_clean(ppd, pq);
1452 mutex_unlock(&pq->lock);
1453
1454 return ret;
1455}
1456
1457u32 qib_user_sdma_complete_counter(const struct qib_user_sdma_queue *pq)
1458{
1459 return pq ? pq->sent_counter : 0;
1460}
1461
1462u32 qib_user_sdma_inflight_counter(struct qib_user_sdma_queue *pq)
1463{
1464 return pq ? pq->counter : 0;
1465}
1466