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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47#include <linux/mm.h>
48#include <linux/types.h>
49#include <linux/device.h>
50#include <linux/dmapool.h>
51#include <linux/slab.h>
52#include <linux/list.h>
53#include <linux/highmem.h>
54#include <linux/io.h>
55#include <linux/uio.h>
56#include <linux/rbtree.h>
57#include <linux/spinlock.h>
58#include <linux/delay.h>
59#include <linux/kthread.h>
60#include <linux/mmu_context.h>
61#include <linux/module.h>
62#include <linux/vmalloc.h>
63
64#include "hfi.h"
65#include "sdma.h"
66#include "user_sdma.h"
67#include "verbs.h"
68#include "common.h"
69#include "trace.h"
70#include "mmu_rb.h"
71
72static uint hfi1_sdma_comp_ring_size = 128;
73module_param_named(sdma_comp_size, hfi1_sdma_comp_ring_size, uint, S_IRUGO);
74MODULE_PARM_DESC(sdma_comp_size, "Size of User SDMA completion ring. Default: 128");
75
76
77#define MAX_VECTORS_PER_REQ 8
78
79
80
81
82#define MAX_PKTS_PER_QUEUE 16
83
84#define num_pages(x) (1 + ((((x) - 1) & PAGE_MASK) >> PAGE_SHIFT))
85
86#define req_opcode(x) \
87 (((x) >> HFI1_SDMA_REQ_OPCODE_SHIFT) & HFI1_SDMA_REQ_OPCODE_MASK)
88#define req_version(x) \
89 (((x) >> HFI1_SDMA_REQ_VERSION_SHIFT) & HFI1_SDMA_REQ_OPCODE_MASK)
90#define req_iovcnt(x) \
91 (((x) >> HFI1_SDMA_REQ_IOVCNT_SHIFT) & HFI1_SDMA_REQ_IOVCNT_MASK)
92
93
94#define BTH_SEQ_MASK 0x7ffull
95
96
97
98
99
100#define KDETH_OFFSET_SHIFT 0
101#define KDETH_OFFSET_MASK 0x7fff
102#define KDETH_OM_SHIFT 15
103#define KDETH_OM_MASK 0x1
104#define KDETH_TID_SHIFT 16
105#define KDETH_TID_MASK 0x3ff
106#define KDETH_TIDCTRL_SHIFT 26
107#define KDETH_TIDCTRL_MASK 0x3
108#define KDETH_INTR_SHIFT 28
109#define KDETH_INTR_MASK 0x1
110#define KDETH_SH_SHIFT 29
111#define KDETH_SH_MASK 0x1
112#define KDETH_HCRC_UPPER_SHIFT 16
113#define KDETH_HCRC_UPPER_MASK 0xff
114#define KDETH_HCRC_LOWER_SHIFT 24
115#define KDETH_HCRC_LOWER_MASK 0xff
116
117#define AHG_KDETH_INTR_SHIFT 12
118#define AHG_KDETH_SH_SHIFT 13
119
120#define PBC2LRH(x) ((((x) & 0xfff) << 2) - 4)
121#define LRH2PBC(x) ((((x) >> 2) + 1) & 0xfff)
122
123#define KDETH_GET(val, field) \
124 (((le32_to_cpu((val))) >> KDETH_##field##_SHIFT) & KDETH_##field##_MASK)
125#define KDETH_SET(dw, field, val) do { \
126 u32 dwval = le32_to_cpu(dw); \
127 dwval &= ~(KDETH_##field##_MASK << KDETH_##field##_SHIFT); \
128 dwval |= (((val) & KDETH_##field##_MASK) << \
129 KDETH_##field##_SHIFT); \
130 dw = cpu_to_le32(dwval); \
131 } while (0)
132
133#define AHG_HEADER_SET(arr, idx, dw, bit, width, value) \
134 do { \
135 if ((idx) < ARRAY_SIZE((arr))) \
136 (arr)[(idx++)] = sdma_build_ahg_descriptor( \
137 (__force u16)(value), (dw), (bit), \
138 (width)); \
139 else \
140 return -ERANGE; \
141 } while (0)
142
143
144#define KDETH_OM_SMALL 4
145#define KDETH_OM_LARGE 64
146#define KDETH_OM_MAX_SIZE (1 << ((KDETH_OM_LARGE / KDETH_OM_SMALL) + 1))
147
148
149#define TXREQ_FLAGS_REQ_ACK BIT(0)
150#define TXREQ_FLAGS_REQ_DISABLE_SH BIT(1)
151
152
153#define SDMA_REQ_FOR_THREAD 1
154#define SDMA_REQ_SEND_DONE 2
155#define SDMA_REQ_HAVE_AHG 3
156#define SDMA_REQ_HAS_ERROR 4
157#define SDMA_REQ_DONE_ERROR 5
158
159#define SDMA_PKT_Q_INACTIVE BIT(0)
160#define SDMA_PKT_Q_ACTIVE BIT(1)
161#define SDMA_PKT_Q_DEFERRED BIT(2)
162
163
164
165
166
167#define MAX_DEFER_RETRY_COUNT 1
168
169static unsigned initial_pkt_count = 8;
170
171#define SDMA_IOWAIT_TIMEOUT 1000
172
173struct sdma_mmu_node;
174
175struct user_sdma_iovec {
176 struct list_head list;
177 struct iovec iov;
178
179 unsigned npages;
180
181 struct page **pages;
182
183
184
185
186 u64 offset;
187 struct sdma_mmu_node *node;
188};
189
190#define SDMA_CACHE_NODE_EVICT 0
191
192struct sdma_mmu_node {
193 struct mmu_rb_node rb;
194 struct hfi1_user_sdma_pkt_q *pq;
195 atomic_t refcount;
196 struct page **pages;
197 unsigned npages;
198};
199
200
201struct evict_data {
202 u32 cleared;
203 u32 target;
204};
205
206struct user_sdma_request {
207 struct sdma_req_info info;
208 struct hfi1_user_sdma_pkt_q *pq;
209 struct hfi1_user_sdma_comp_q *cq;
210
211 struct hfi1_pkt_header hdr;
212
213
214
215
216
217 struct sdma_engine *sde;
218 u8 ahg_idx;
219 u32 ahg[9];
220
221
222
223
224
225 u32 koffset;
226
227
228
229
230
231 u32 tidoffset;
232
233
234
235
236
237 u8 omfactor;
238
239
240
241
242 unsigned data_iovs;
243
244 u32 data_len;
245
246 unsigned iov_idx;
247 struct user_sdma_iovec iovs[MAX_VECTORS_PER_REQ];
248
249 u16 n_tids;
250
251 u32 *tids;
252 u16 tididx;
253 u32 sent;
254 u64 seqnum;
255 u64 seqcomp;
256 u64 seqsubmitted;
257 struct list_head txps;
258 unsigned long flags;
259
260 int status;
261};
262
263
264
265
266
267
268
269struct user_sdma_txreq {
270
271 struct hfi1_pkt_header hdr;
272 struct sdma_txreq txreq;
273 struct list_head list;
274 struct user_sdma_request *req;
275 u16 flags;
276 unsigned busycount;
277 u64 seqnum;
278};
279
280#define SDMA_DBG(req, fmt, ...) \
281 hfi1_cdbg(SDMA, "[%u:%u:%u:%u] " fmt, (req)->pq->dd->unit, \
282 (req)->pq->ctxt, (req)->pq->subctxt, (req)->info.comp_idx, \
283 ##__VA_ARGS__)
284#define SDMA_Q_DBG(pq, fmt, ...) \
285 hfi1_cdbg(SDMA, "[%u:%u:%u] " fmt, (pq)->dd->unit, (pq)->ctxt, \
286 (pq)->subctxt, ##__VA_ARGS__)
287
288static int user_sdma_send_pkts(struct user_sdma_request *, unsigned);
289static int num_user_pages(const struct iovec *);
290static void user_sdma_txreq_cb(struct sdma_txreq *, int);
291static inline void pq_update(struct hfi1_user_sdma_pkt_q *);
292static void user_sdma_free_request(struct user_sdma_request *, bool);
293static int pin_vector_pages(struct user_sdma_request *,
294 struct user_sdma_iovec *);
295static void unpin_vector_pages(struct mm_struct *, struct page **, unsigned,
296 unsigned);
297static int check_header_template(struct user_sdma_request *,
298 struct hfi1_pkt_header *, u32, u32);
299static int set_txreq_header(struct user_sdma_request *,
300 struct user_sdma_txreq *, u32);
301static int set_txreq_header_ahg(struct user_sdma_request *,
302 struct user_sdma_txreq *, u32);
303static inline void set_comp_state(struct hfi1_user_sdma_pkt_q *,
304 struct hfi1_user_sdma_comp_q *,
305 u16, enum hfi1_sdma_comp_state, int);
306static inline u32 set_pkt_bth_psn(__be32, u8, u32);
307static inline u32 get_lrh_len(struct hfi1_pkt_header, u32 len);
308
309static int defer_packet_queue(
310 struct sdma_engine *,
311 struct iowait *,
312 struct sdma_txreq *,
313 unsigned seq);
314static void activate_packet_queue(struct iowait *, int);
315static bool sdma_rb_filter(struct mmu_rb_node *, unsigned long, unsigned long);
316static int sdma_rb_insert(void *, struct mmu_rb_node *);
317static int sdma_rb_evict(void *arg, struct mmu_rb_node *mnode,
318 void *arg2, bool *stop);
319static void sdma_rb_remove(void *, struct mmu_rb_node *);
320static int sdma_rb_invalidate(void *, struct mmu_rb_node *);
321
322static struct mmu_rb_ops sdma_rb_ops = {
323 .filter = sdma_rb_filter,
324 .insert = sdma_rb_insert,
325 .evict = sdma_rb_evict,
326 .remove = sdma_rb_remove,
327 .invalidate = sdma_rb_invalidate
328};
329
330static int defer_packet_queue(
331 struct sdma_engine *sde,
332 struct iowait *wait,
333 struct sdma_txreq *txreq,
334 unsigned seq)
335{
336 struct hfi1_user_sdma_pkt_q *pq =
337 container_of(wait, struct hfi1_user_sdma_pkt_q, busy);
338 struct hfi1_ibdev *dev = &pq->dd->verbs_dev;
339 struct user_sdma_txreq *tx =
340 container_of(txreq, struct user_sdma_txreq, txreq);
341
342 if (sdma_progress(sde, seq, txreq)) {
343 if (tx->busycount++ < MAX_DEFER_RETRY_COUNT)
344 goto eagain;
345 }
346
347
348
349
350
351 xchg(&pq->state, SDMA_PKT_Q_DEFERRED);
352 write_seqlock(&dev->iowait_lock);
353 if (list_empty(&pq->busy.list))
354 list_add_tail(&pq->busy.list, &sde->dmawait);
355 write_sequnlock(&dev->iowait_lock);
356 return -EBUSY;
357eagain:
358 return -EAGAIN;
359}
360
361static void activate_packet_queue(struct iowait *wait, int reason)
362{
363 struct hfi1_user_sdma_pkt_q *pq =
364 container_of(wait, struct hfi1_user_sdma_pkt_q, busy);
365 xchg(&pq->state, SDMA_PKT_Q_ACTIVE);
366 wake_up(&wait->wait_dma);
367};
368
369static void sdma_kmem_cache_ctor(void *obj)
370{
371 struct user_sdma_txreq *tx = obj;
372
373 memset(tx, 0, sizeof(*tx));
374}
375
376int hfi1_user_sdma_alloc_queues(struct hfi1_ctxtdata *uctxt, struct file *fp)
377{
378 struct hfi1_filedata *fd;
379 int ret = 0;
380 unsigned memsize;
381 char buf[64];
382 struct hfi1_devdata *dd;
383 struct hfi1_user_sdma_comp_q *cq;
384 struct hfi1_user_sdma_pkt_q *pq;
385 unsigned long flags;
386
387 if (!uctxt || !fp) {
388 ret = -EBADF;
389 goto done;
390 }
391
392 fd = fp->private_data;
393
394 if (!hfi1_sdma_comp_ring_size) {
395 ret = -EINVAL;
396 goto done;
397 }
398
399 dd = uctxt->dd;
400
401 pq = kzalloc(sizeof(*pq), GFP_KERNEL);
402 if (!pq)
403 goto pq_nomem;
404
405 memsize = sizeof(*pq->reqs) * hfi1_sdma_comp_ring_size;
406 pq->reqs = kzalloc(memsize, GFP_KERNEL);
407 if (!pq->reqs)
408 goto pq_reqs_nomem;
409
410 memsize = BITS_TO_LONGS(hfi1_sdma_comp_ring_size) * sizeof(long);
411 pq->req_in_use = kzalloc(memsize, GFP_KERNEL);
412 if (!pq->req_in_use)
413 goto pq_reqs_no_in_use;
414
415 INIT_LIST_HEAD(&pq->list);
416 pq->dd = dd;
417 pq->ctxt = uctxt->ctxt;
418 pq->subctxt = fd->subctxt;
419 pq->n_max_reqs = hfi1_sdma_comp_ring_size;
420 pq->state = SDMA_PKT_Q_INACTIVE;
421 atomic_set(&pq->n_reqs, 0);
422 init_waitqueue_head(&pq->wait);
423 atomic_set(&pq->n_locked, 0);
424 pq->mm = fd->mm;
425
426 iowait_init(&pq->busy, 0, NULL, defer_packet_queue,
427 activate_packet_queue, NULL);
428 pq->reqidx = 0;
429 snprintf(buf, 64, "txreq-kmem-cache-%u-%u-%u", dd->unit, uctxt->ctxt,
430 fd->subctxt);
431 pq->txreq_cache = kmem_cache_create(buf,
432 sizeof(struct user_sdma_txreq),
433 L1_CACHE_BYTES,
434 SLAB_HWCACHE_ALIGN,
435 sdma_kmem_cache_ctor);
436 if (!pq->txreq_cache) {
437 dd_dev_err(dd, "[%u] Failed to allocate TxReq cache\n",
438 uctxt->ctxt);
439 goto pq_txreq_nomem;
440 }
441 fd->pq = pq;
442 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
443 if (!cq)
444 goto cq_nomem;
445
446 memsize = PAGE_ALIGN(sizeof(*cq->comps) * hfi1_sdma_comp_ring_size);
447 cq->comps = vmalloc_user(memsize);
448 if (!cq->comps)
449 goto cq_comps_nomem;
450
451 cq->nentries = hfi1_sdma_comp_ring_size;
452 fd->cq = cq;
453
454 ret = hfi1_mmu_rb_register(pq, pq->mm, &sdma_rb_ops, dd->pport->hfi1_wq,
455 &pq->handler);
456 if (ret) {
457 dd_dev_err(dd, "Failed to register with MMU %d", ret);
458 goto done;
459 }
460
461 spin_lock_irqsave(&uctxt->sdma_qlock, flags);
462 list_add(&pq->list, &uctxt->sdma_queues);
463 spin_unlock_irqrestore(&uctxt->sdma_qlock, flags);
464 goto done;
465
466cq_comps_nomem:
467 kfree(cq);
468cq_nomem:
469 kmem_cache_destroy(pq->txreq_cache);
470pq_txreq_nomem:
471 kfree(pq->req_in_use);
472pq_reqs_no_in_use:
473 kfree(pq->reqs);
474pq_reqs_nomem:
475 kfree(pq);
476 fd->pq = NULL;
477pq_nomem:
478 ret = -ENOMEM;
479done:
480 return ret;
481}
482
483int hfi1_user_sdma_free_queues(struct hfi1_filedata *fd)
484{
485 struct hfi1_ctxtdata *uctxt = fd->uctxt;
486 struct hfi1_user_sdma_pkt_q *pq;
487 unsigned long flags;
488
489 hfi1_cdbg(SDMA, "[%u:%u:%u] Freeing user SDMA queues", uctxt->dd->unit,
490 uctxt->ctxt, fd->subctxt);
491 pq = fd->pq;
492 if (pq) {
493 if (pq->handler)
494 hfi1_mmu_rb_unregister(pq->handler);
495 spin_lock_irqsave(&uctxt->sdma_qlock, flags);
496 if (!list_empty(&pq->list))
497 list_del_init(&pq->list);
498 spin_unlock_irqrestore(&uctxt->sdma_qlock, flags);
499 iowait_sdma_drain(&pq->busy);
500
501 wait_event_interruptible(
502 pq->wait,
503 (ACCESS_ONCE(pq->state) == SDMA_PKT_Q_INACTIVE));
504 kfree(pq->reqs);
505 kfree(pq->req_in_use);
506 kmem_cache_destroy(pq->txreq_cache);
507 kfree(pq);
508 fd->pq = NULL;
509 }
510 if (fd->cq) {
511 vfree(fd->cq->comps);
512 kfree(fd->cq);
513 fd->cq = NULL;
514 }
515 return 0;
516}
517
518static u8 dlid_to_selector(u16 dlid)
519{
520 static u8 mapping[256];
521 static int initialized;
522 static u8 next;
523 int hash;
524
525 if (!initialized) {
526 memset(mapping, 0xFF, 256);
527 initialized = 1;
528 }
529
530 hash = ((dlid >> 8) ^ dlid) & 0xFF;
531 if (mapping[hash] == 0xFF) {
532 mapping[hash] = next;
533 next = (next + 1) & 0x7F;
534 }
535
536 return mapping[hash];
537}
538
539int hfi1_user_sdma_process_request(struct file *fp, struct iovec *iovec,
540 unsigned long dim, unsigned long *count)
541{
542 int ret = 0, i;
543 struct hfi1_filedata *fd = fp->private_data;
544 struct hfi1_ctxtdata *uctxt = fd->uctxt;
545 struct hfi1_user_sdma_pkt_q *pq = fd->pq;
546 struct hfi1_user_sdma_comp_q *cq = fd->cq;
547 struct hfi1_devdata *dd = pq->dd;
548 unsigned long idx = 0;
549 u8 pcount = initial_pkt_count;
550 struct sdma_req_info info;
551 struct user_sdma_request *req;
552 u8 opcode, sc, vl;
553 int req_queued = 0;
554 u16 dlid;
555 u32 selector;
556
557 if (iovec[idx].iov_len < sizeof(info) + sizeof(req->hdr)) {
558 hfi1_cdbg(
559 SDMA,
560 "[%u:%u:%u] First vector not big enough for header %lu/%lu",
561 dd->unit, uctxt->ctxt, fd->subctxt,
562 iovec[idx].iov_len, sizeof(info) + sizeof(req->hdr));
563 return -EINVAL;
564 }
565 ret = copy_from_user(&info, iovec[idx].iov_base, sizeof(info));
566 if (ret) {
567 hfi1_cdbg(SDMA, "[%u:%u:%u] Failed to copy info QW (%d)",
568 dd->unit, uctxt->ctxt, fd->subctxt, ret);
569 return -EFAULT;
570 }
571
572 trace_hfi1_sdma_user_reqinfo(dd, uctxt->ctxt, fd->subctxt,
573 (u16 *)&info);
574
575 if (info.comp_idx >= hfi1_sdma_comp_ring_size) {
576 hfi1_cdbg(SDMA,
577 "[%u:%u:%u:%u] Invalid comp index",
578 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx);
579 return -EINVAL;
580 }
581
582
583
584
585
586 if (req_iovcnt(info.ctrl) < 1 || req_iovcnt(info.ctrl) > dim) {
587 hfi1_cdbg(SDMA,
588 "[%u:%u:%u:%u] Invalid iov count %d, dim %ld",
589 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx,
590 req_iovcnt(info.ctrl), dim);
591 return -EINVAL;
592 }
593
594 if (!info.fragsize) {
595 hfi1_cdbg(SDMA,
596 "[%u:%u:%u:%u] Request does not specify fragsize",
597 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx);
598 return -EINVAL;
599 }
600
601
602 if (test_and_set_bit(info.comp_idx, pq->req_in_use)) {
603 hfi1_cdbg(SDMA, "[%u:%u:%u] Entry %u is in use",
604 dd->unit, uctxt->ctxt, fd->subctxt,
605 info.comp_idx);
606 return -EBADSLT;
607 }
608
609
610
611 hfi1_cdbg(SDMA, "[%u:%u:%u] Using req/comp entry %u\n", dd->unit,
612 uctxt->ctxt, fd->subctxt, info.comp_idx);
613 req = pq->reqs + info.comp_idx;
614 memset(req, 0, sizeof(*req));
615 req->data_iovs = req_iovcnt(info.ctrl) - 1;
616 req->pq = pq;
617 req->cq = cq;
618 req->status = -1;
619 INIT_LIST_HEAD(&req->txps);
620
621 memcpy(&req->info, &info, sizeof(info));
622
623 if (req_opcode(info.ctrl) == EXPECTED) {
624
625 if (req->data_iovs < 2) {
626 SDMA_DBG(req,
627 "Not enough vectors for expected request");
628 ret = -EINVAL;
629 goto free_req;
630 }
631 req->data_iovs--;
632 }
633
634 if (!info.npkts || req->data_iovs > MAX_VECTORS_PER_REQ) {
635 SDMA_DBG(req, "Too many vectors (%u/%u)", req->data_iovs,
636 MAX_VECTORS_PER_REQ);
637 ret = -EINVAL;
638 goto free_req;
639 }
640
641 ret = copy_from_user(&req->hdr, iovec[idx].iov_base + sizeof(info),
642 sizeof(req->hdr));
643 if (ret) {
644 SDMA_DBG(req, "Failed to copy header template (%d)", ret);
645 ret = -EFAULT;
646 goto free_req;
647 }
648
649
650 if (!HFI1_CAP_IS_USET(STATIC_RATE_CTRL))
651 req->hdr.pbc[2] = 0;
652
653
654 opcode = (be32_to_cpu(req->hdr.bth[0]) >> 24) & 0xff;
655 if ((opcode & USER_OPCODE_CHECK_MASK) !=
656 USER_OPCODE_CHECK_VAL) {
657 SDMA_DBG(req, "Invalid opcode (%d)", opcode);
658 ret = -EINVAL;
659 goto free_req;
660 }
661
662
663
664
665
666 vl = (le16_to_cpu(req->hdr.pbc[0]) >> 12) & 0xF;
667 sc = (((be16_to_cpu(req->hdr.lrh[0]) >> 12) & 0xF) |
668 (((le16_to_cpu(req->hdr.pbc[1]) >> 14) & 0x1) << 4));
669 if (vl >= dd->pport->vls_operational ||
670 vl != sc_to_vlt(dd, sc)) {
671 SDMA_DBG(req, "Invalid SC(%u)/VL(%u)", sc, vl);
672 ret = -EINVAL;
673 goto free_req;
674 }
675
676
677 if (egress_pkey_check(dd->pport, req->hdr.lrh, req->hdr.bth, sc,
678 PKEY_CHECK_INVALID)) {
679 ret = -EINVAL;
680 goto free_req;
681 }
682
683
684
685
686
687
688 if ((be16_to_cpu(req->hdr.lrh[0]) & 0x3) == HFI1_LRH_GRH) {
689 SDMA_DBG(req, "User tried to pass in a GRH");
690 ret = -EINVAL;
691 goto free_req;
692 }
693
694 req->koffset = le32_to_cpu(req->hdr.kdeth.swdata[6]);
695
696
697
698
699 req->tidoffset = KDETH_GET(req->hdr.kdeth.ver_tid_offset, OFFSET) *
700 (KDETH_GET(req->hdr.kdeth.ver_tid_offset, OM) ?
701 KDETH_OM_LARGE : KDETH_OM_SMALL);
702 SDMA_DBG(req, "Initial TID offset %u", req->tidoffset);
703 idx++;
704
705
706 for (i = 0; i < req->data_iovs; i++) {
707 INIT_LIST_HEAD(&req->iovs[i].list);
708 memcpy(&req->iovs[i].iov, iovec + idx++, sizeof(struct iovec));
709 ret = pin_vector_pages(req, &req->iovs[i]);
710 if (ret) {
711 req->status = ret;
712 goto free_req;
713 }
714 req->data_len += req->iovs[i].iov.iov_len;
715 }
716 SDMA_DBG(req, "total data length %u", req->data_len);
717
718 if (pcount > req->info.npkts)
719 pcount = req->info.npkts;
720
721
722
723
724
725
726
727
728 if (req_opcode(req->info.ctrl) == EXPECTED) {
729 u16 ntids = iovec[idx].iov_len / sizeof(*req->tids);
730
731 if (!ntids || ntids > MAX_TID_PAIR_ENTRIES) {
732 ret = -EINVAL;
733 goto free_req;
734 }
735 req->tids = kcalloc(ntids, sizeof(*req->tids), GFP_KERNEL);
736 if (!req->tids) {
737 ret = -ENOMEM;
738 goto free_req;
739 }
740
741
742
743
744
745
746 ret = copy_from_user(req->tids, iovec[idx].iov_base,
747 ntids * sizeof(*req->tids));
748 if (ret) {
749 SDMA_DBG(req, "Failed to copy %d TIDs (%d)",
750 ntids, ret);
751 ret = -EFAULT;
752 goto free_req;
753 }
754 req->n_tids = ntids;
755 idx++;
756 }
757
758 dlid = be16_to_cpu(req->hdr.lrh[1]);
759 selector = dlid_to_selector(dlid);
760 selector += uctxt->ctxt + fd->subctxt;
761 req->sde = sdma_select_user_engine(dd, selector, vl);
762
763 if (!req->sde || !sdma_running(req->sde)) {
764 ret = -ECOMM;
765 goto free_req;
766 }
767
768
769 if (req->info.npkts > 1 && HFI1_CAP_IS_USET(SDMA_AHG)) {
770 int ahg = sdma_ahg_alloc(req->sde);
771
772 if (likely(ahg >= 0)) {
773 req->ahg_idx = (u8)ahg;
774 set_bit(SDMA_REQ_HAVE_AHG, &req->flags);
775 }
776 }
777
778 set_comp_state(pq, cq, info.comp_idx, QUEUED, 0);
779 atomic_inc(&pq->n_reqs);
780 req_queued = 1;
781
782 ret = user_sdma_send_pkts(req, pcount);
783 if (unlikely(ret < 0 && ret != -EBUSY)) {
784 req->status = ret;
785 goto free_req;
786 }
787
788
789
790
791
792
793
794 if (atomic_read(&pq->n_reqs))
795 xchg(&pq->state, SDMA_PKT_Q_ACTIVE);
796
797
798
799
800
801
802
803 while (!test_bit(SDMA_REQ_SEND_DONE, &req->flags)) {
804 ret = user_sdma_send_pkts(req, pcount);
805 if (ret < 0) {
806 if (ret != -EBUSY) {
807 req->status = ret;
808 set_bit(SDMA_REQ_DONE_ERROR, &req->flags);
809 if (ACCESS_ONCE(req->seqcomp) ==
810 req->seqsubmitted - 1)
811 goto free_req;
812 return ret;
813 }
814 wait_event_interruptible_timeout(
815 pq->busy.wait_dma,
816 (pq->state == SDMA_PKT_Q_ACTIVE),
817 msecs_to_jiffies(
818 SDMA_IOWAIT_TIMEOUT));
819 }
820 }
821 *count += idx;
822 return 0;
823free_req:
824 user_sdma_free_request(req, true);
825 if (req_queued)
826 pq_update(pq);
827 set_comp_state(pq, cq, info.comp_idx, ERROR, req->status);
828 return ret;
829}
830
831static inline u32 compute_data_length(struct user_sdma_request *req,
832 struct user_sdma_txreq *tx)
833{
834
835
836
837
838
839
840
841
842
843
844
845
846 u32 len;
847
848 if (!req->seqnum) {
849 if (req->data_len < sizeof(u32))
850 len = req->data_len;
851 else
852 len = ((be16_to_cpu(req->hdr.lrh[2]) << 2) -
853 (sizeof(tx->hdr) - 4));
854 } else if (req_opcode(req->info.ctrl) == EXPECTED) {
855 u32 tidlen = EXP_TID_GET(req->tids[req->tididx], LEN) *
856 PAGE_SIZE;
857
858
859
860
861 len = min(tidlen - req->tidoffset, (u32)req->info.fragsize);
862
863 if (unlikely(!len) && ++req->tididx < req->n_tids &&
864 req->tids[req->tididx]) {
865 tidlen = EXP_TID_GET(req->tids[req->tididx],
866 LEN) * PAGE_SIZE;
867 req->tidoffset = 0;
868 len = min_t(u32, tidlen, req->info.fragsize);
869 }
870
871
872
873
874
875 len = min(len, req->data_len - req->sent);
876 } else {
877 len = min(req->data_len - req->sent, (u32)req->info.fragsize);
878 }
879 SDMA_DBG(req, "Data Length = %u", len);
880 return len;
881}
882
883static inline u32 pad_len(u32 len)
884{
885 if (len & (sizeof(u32) - 1))
886 len += sizeof(u32) - (len & (sizeof(u32) - 1));
887 return len;
888}
889
890static inline u32 get_lrh_len(struct hfi1_pkt_header hdr, u32 len)
891{
892
893 return ((sizeof(hdr) - sizeof(hdr.pbc)) + 4 + len);
894}
895
896static int user_sdma_send_pkts(struct user_sdma_request *req, unsigned maxpkts)
897{
898 int ret = 0, count;
899 unsigned npkts = 0;
900 struct user_sdma_txreq *tx = NULL;
901 struct hfi1_user_sdma_pkt_q *pq = NULL;
902 struct user_sdma_iovec *iovec = NULL;
903
904 if (!req->pq)
905 return -EINVAL;
906
907 pq = req->pq;
908
909
910 if (test_bit(SDMA_REQ_HAS_ERROR, &req->flags)) {
911 set_bit(SDMA_REQ_DONE_ERROR, &req->flags);
912 return -EFAULT;
913 }
914
915
916
917
918 if (unlikely(req->seqnum == req->info.npkts)) {
919 if (!list_empty(&req->txps))
920 goto dosend;
921 return ret;
922 }
923
924 if (!maxpkts || maxpkts > req->info.npkts - req->seqnum)
925 maxpkts = req->info.npkts - req->seqnum;
926
927 while (npkts < maxpkts) {
928 u32 datalen = 0, queued = 0, data_sent = 0;
929 u64 iov_offset = 0;
930
931
932
933
934
935
936 if (test_bit(SDMA_REQ_HAS_ERROR, &req->flags)) {
937 set_bit(SDMA_REQ_DONE_ERROR, &req->flags);
938 return -EFAULT;
939 }
940
941 tx = kmem_cache_alloc(pq->txreq_cache, GFP_KERNEL);
942 if (!tx)
943 return -ENOMEM;
944
945 tx->flags = 0;
946 tx->req = req;
947 tx->busycount = 0;
948 INIT_LIST_HEAD(&tx->list);
949
950
951
952
953
954 if (req->seqnum == req->info.npkts - 1)
955 tx->flags |= (TXREQ_FLAGS_REQ_ACK |
956 TXREQ_FLAGS_REQ_DISABLE_SH);
957
958
959
960
961
962
963 if (req->data_len) {
964 iovec = &req->iovs[req->iov_idx];
965 if (ACCESS_ONCE(iovec->offset) == iovec->iov.iov_len) {
966 if (++req->iov_idx == req->data_iovs) {
967 ret = -EFAULT;
968 goto free_txreq;
969 }
970 iovec = &req->iovs[req->iov_idx];
971 WARN_ON(iovec->offset);
972 }
973
974 datalen = compute_data_length(req, tx);
975
976
977
978
979
980
981
982
983
984 if (!datalen) {
985 SDMA_DBG(req,
986 "Request has data but pkt len is 0");
987 ret = -EFAULT;
988 goto free_tx;
989 } else if (datalen <= 32) {
990 tx->flags |= TXREQ_FLAGS_REQ_DISABLE_SH;
991 }
992 }
993
994 if (test_bit(SDMA_REQ_HAVE_AHG, &req->flags)) {
995 if (!req->seqnum) {
996 u16 pbclen = le16_to_cpu(req->hdr.pbc[0]);
997 u32 lrhlen = get_lrh_len(req->hdr,
998 pad_len(datalen));
999
1000
1001
1002
1003
1004
1005
1006
1007 memcpy(&tx->hdr, &req->hdr, sizeof(tx->hdr));
1008 if (PBC2LRH(pbclen) != lrhlen) {
1009 pbclen = (pbclen & 0xf000) |
1010 LRH2PBC(lrhlen);
1011 tx->hdr.pbc[0] = cpu_to_le16(pbclen);
1012 }
1013 ret = check_header_template(req, &tx->hdr,
1014 lrhlen, datalen);
1015 if (ret)
1016 goto free_tx;
1017 ret = sdma_txinit_ahg(&tx->txreq,
1018 SDMA_TXREQ_F_AHG_COPY,
1019 sizeof(tx->hdr) + datalen,
1020 req->ahg_idx, 0, NULL, 0,
1021 user_sdma_txreq_cb);
1022 if (ret)
1023 goto free_tx;
1024 ret = sdma_txadd_kvaddr(pq->dd, &tx->txreq,
1025 &tx->hdr,
1026 sizeof(tx->hdr));
1027 if (ret)
1028 goto free_txreq;
1029 } else {
1030 int changes;
1031
1032 changes = set_txreq_header_ahg(req, tx,
1033 datalen);
1034 if (changes < 0)
1035 goto free_tx;
1036 sdma_txinit_ahg(&tx->txreq,
1037 SDMA_TXREQ_F_USE_AHG,
1038 datalen, req->ahg_idx, changes,
1039 req->ahg, sizeof(req->hdr),
1040 user_sdma_txreq_cb);
1041 }
1042 } else {
1043 ret = sdma_txinit(&tx->txreq, 0, sizeof(req->hdr) +
1044 datalen, user_sdma_txreq_cb);
1045 if (ret)
1046 goto free_tx;
1047
1048
1049
1050
1051
1052
1053 ret = set_txreq_header(req, tx, datalen);
1054 if (ret)
1055 goto free_txreq;
1056 }
1057
1058
1059
1060
1061
1062 while (queued < datalen &&
1063 (req->sent + data_sent) < req->data_len) {
1064 unsigned long base, offset;
1065 unsigned pageidx, len;
1066
1067 base = (unsigned long)iovec->iov.iov_base;
1068 offset = offset_in_page(base + iovec->offset +
1069 iov_offset);
1070 pageidx = (((iovec->offset + iov_offset +
1071 base) - (base & PAGE_MASK)) >> PAGE_SHIFT);
1072 len = offset + req->info.fragsize > PAGE_SIZE ?
1073 PAGE_SIZE - offset : req->info.fragsize;
1074 len = min((datalen - queued), len);
1075 ret = sdma_txadd_page(pq->dd, &tx->txreq,
1076 iovec->pages[pageidx],
1077 offset, len);
1078 if (ret) {
1079 SDMA_DBG(req, "SDMA txreq add page failed %d\n",
1080 ret);
1081 goto free_txreq;
1082 }
1083 iov_offset += len;
1084 queued += len;
1085 data_sent += len;
1086 if (unlikely(queued < datalen &&
1087 pageidx == iovec->npages &&
1088 req->iov_idx < req->data_iovs - 1)) {
1089 iovec->offset += iov_offset;
1090 iovec = &req->iovs[++req->iov_idx];
1091 iov_offset = 0;
1092 }
1093 }
1094
1095
1096
1097
1098 req->koffset += datalen;
1099 if (req_opcode(req->info.ctrl) == EXPECTED)
1100 req->tidoffset += datalen;
1101 req->sent += data_sent;
1102 if (req->data_len)
1103 iovec->offset += iov_offset;
1104 list_add_tail(&tx->txreq.list, &req->txps);
1105
1106
1107
1108
1109
1110 tx->seqnum = req->seqnum++;
1111 npkts++;
1112 }
1113dosend:
1114 ret = sdma_send_txlist(req->sde, &pq->busy, &req->txps, &count);
1115 req->seqsubmitted += count;
1116 if (req->seqsubmitted == req->info.npkts) {
1117 set_bit(SDMA_REQ_SEND_DONE, &req->flags);
1118
1119
1120
1121
1122
1123
1124 if (test_bit(SDMA_REQ_HAVE_AHG, &req->flags))
1125 sdma_ahg_free(req->sde, req->ahg_idx);
1126 }
1127 return ret;
1128
1129free_txreq:
1130 sdma_txclean(pq->dd, &tx->txreq);
1131free_tx:
1132 kmem_cache_free(pq->txreq_cache, tx);
1133 return ret;
1134}
1135
1136
1137
1138
1139static inline int num_user_pages(const struct iovec *iov)
1140{
1141 const unsigned long addr = (unsigned long)iov->iov_base;
1142 const unsigned long len = iov->iov_len;
1143 const unsigned long spage = addr & PAGE_MASK;
1144 const unsigned long epage = (addr + len - 1) & PAGE_MASK;
1145
1146 return 1 + ((epage - spage) >> PAGE_SHIFT);
1147}
1148
1149static u32 sdma_cache_evict(struct hfi1_user_sdma_pkt_q *pq, u32 npages)
1150{
1151 struct evict_data evict_data;
1152
1153 evict_data.cleared = 0;
1154 evict_data.target = npages;
1155 hfi1_mmu_rb_evict(pq->handler, &evict_data);
1156 return evict_data.cleared;
1157}
1158
1159static int pin_vector_pages(struct user_sdma_request *req,
1160 struct user_sdma_iovec *iovec)
1161{
1162 int ret = 0, pinned, npages, cleared;
1163 struct page **pages;
1164 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1165 struct sdma_mmu_node *node = NULL;
1166 struct mmu_rb_node *rb_node;
1167
1168 rb_node = hfi1_mmu_rb_extract(pq->handler,
1169 (unsigned long)iovec->iov.iov_base,
1170 iovec->iov.iov_len);
1171 if (rb_node)
1172 node = container_of(rb_node, struct sdma_mmu_node, rb);
1173 else
1174 rb_node = NULL;
1175
1176 if (!node) {
1177 node = kzalloc(sizeof(*node), GFP_KERNEL);
1178 if (!node)
1179 return -ENOMEM;
1180
1181 node->rb.addr = (unsigned long)iovec->iov.iov_base;
1182 node->pq = pq;
1183 atomic_set(&node->refcount, 0);
1184 }
1185
1186 npages = num_user_pages(&iovec->iov);
1187 if (node->npages < npages) {
1188 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
1189 if (!pages) {
1190 SDMA_DBG(req, "Failed page array alloc");
1191 ret = -ENOMEM;
1192 goto bail;
1193 }
1194 memcpy(pages, node->pages, node->npages * sizeof(*pages));
1195
1196 npages -= node->npages;
1197
1198retry:
1199 if (!hfi1_can_pin_pages(pq->dd, pq->mm,
1200 atomic_read(&pq->n_locked), npages)) {
1201 cleared = sdma_cache_evict(pq, npages);
1202 if (cleared >= npages)
1203 goto retry;
1204 }
1205 pinned = hfi1_acquire_user_pages(pq->mm,
1206 ((unsigned long)iovec->iov.iov_base +
1207 (node->npages * PAGE_SIZE)), npages, 0,
1208 pages + node->npages);
1209 if (pinned < 0) {
1210 kfree(pages);
1211 ret = pinned;
1212 goto bail;
1213 }
1214 if (pinned != npages) {
1215 unpin_vector_pages(pq->mm, pages, node->npages,
1216 pinned);
1217 ret = -EFAULT;
1218 goto bail;
1219 }
1220 kfree(node->pages);
1221 node->rb.len = iovec->iov.iov_len;
1222 node->pages = pages;
1223 node->npages += pinned;
1224 npages = node->npages;
1225 atomic_add(pinned, &pq->n_locked);
1226 }
1227 iovec->pages = node->pages;
1228 iovec->npages = npages;
1229 iovec->node = node;
1230
1231 ret = hfi1_mmu_rb_insert(req->pq->handler, &node->rb);
1232 if (ret) {
1233 atomic_sub(node->npages, &pq->n_locked);
1234 iovec->node = NULL;
1235 goto bail;
1236 }
1237 return 0;
1238bail:
1239 if (rb_node)
1240 unpin_vector_pages(pq->mm, node->pages, 0, node->npages);
1241 kfree(node);
1242 return ret;
1243}
1244
1245static void unpin_vector_pages(struct mm_struct *mm, struct page **pages,
1246 unsigned start, unsigned npages)
1247{
1248 hfi1_release_user_pages(mm, pages + start, npages, false);
1249 kfree(pages);
1250}
1251
1252static int check_header_template(struct user_sdma_request *req,
1253 struct hfi1_pkt_header *hdr, u32 lrhlen,
1254 u32 datalen)
1255{
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266 if (req->info.fragsize % PIO_BLOCK_SIZE || lrhlen & 0x3 ||
1267 lrhlen > get_lrh_len(*hdr, req->info.fragsize))
1268 return -EINVAL;
1269
1270 if (req_opcode(req->info.ctrl) == EXPECTED) {
1271
1272
1273
1274
1275
1276
1277 u32 tidval = req->tids[req->tididx],
1278 tidlen = EXP_TID_GET(tidval, LEN) * PAGE_SIZE,
1279 tididx = EXP_TID_GET(tidval, IDX),
1280 tidctrl = EXP_TID_GET(tidval, CTRL),
1281 tidoff;
1282 __le32 kval = hdr->kdeth.ver_tid_offset;
1283
1284 tidoff = KDETH_GET(kval, OFFSET) *
1285 (KDETH_GET(req->hdr.kdeth.ver_tid_offset, OM) ?
1286 KDETH_OM_LARGE : KDETH_OM_SMALL);
1287
1288
1289
1290
1291
1292
1293
1294 if ((tidoff + datalen > tidlen) ||
1295 KDETH_GET(kval, TIDCTRL) != tidctrl ||
1296 KDETH_GET(kval, TID) != tididx)
1297 return -EINVAL;
1298 }
1299 return 0;
1300}
1301
1302
1303
1304
1305
1306
1307
1308static inline u32 set_pkt_bth_psn(__be32 bthpsn, u8 expct, u32 frags)
1309{
1310 u32 val = be32_to_cpu(bthpsn),
1311 mask = (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffffull :
1312 0xffffffull),
1313 psn = val & mask;
1314 if (expct)
1315 psn = (psn & ~BTH_SEQ_MASK) | ((psn + frags) & BTH_SEQ_MASK);
1316 else
1317 psn = psn + frags;
1318 return psn & mask;
1319}
1320
1321static int set_txreq_header(struct user_sdma_request *req,
1322 struct user_sdma_txreq *tx, u32 datalen)
1323{
1324 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1325 struct hfi1_pkt_header *hdr = &tx->hdr;
1326 u16 pbclen;
1327 int ret;
1328 u32 tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(datalen));
1329
1330
1331 memcpy(hdr, &req->hdr, sizeof(*hdr));
1332
1333
1334
1335
1336
1337 pbclen = le16_to_cpu(hdr->pbc[0]);
1338 if (PBC2LRH(pbclen) != lrhlen) {
1339 pbclen = (pbclen & 0xf000) | LRH2PBC(lrhlen);
1340 hdr->pbc[0] = cpu_to_le16(pbclen);
1341 hdr->lrh[2] = cpu_to_be16(lrhlen >> 2);
1342
1343
1344
1345
1346
1347
1348 if (unlikely(req->seqnum == 2)) {
1349
1350
1351
1352
1353
1354
1355
1356 req->hdr.pbc[0] = hdr->pbc[0];
1357 req->hdr.lrh[2] = hdr->lrh[2];
1358 }
1359 }
1360
1361
1362
1363
1364
1365 if (unlikely(!req->seqnum)) {
1366 ret = check_header_template(req, hdr, lrhlen, datalen);
1367 if (ret)
1368 return ret;
1369 goto done;
1370 }
1371
1372 hdr->bth[2] = cpu_to_be32(
1373 set_pkt_bth_psn(hdr->bth[2],
1374 (req_opcode(req->info.ctrl) == EXPECTED),
1375 req->seqnum));
1376
1377
1378 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_ACK))
1379 hdr->bth[2] |= cpu_to_be32(1UL << 31);
1380
1381
1382 hdr->kdeth.swdata[6] = cpu_to_le32(req->koffset);
1383
1384 if (req_opcode(req->info.ctrl) == EXPECTED) {
1385 tidval = req->tids[req->tididx];
1386
1387
1388
1389
1390 if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
1391 PAGE_SIZE)) {
1392 req->tidoffset = 0;
1393
1394
1395
1396
1397 if (++req->tididx > req->n_tids - 1 ||
1398 !req->tids[req->tididx]) {
1399 return -EINVAL;
1400 }
1401 tidval = req->tids[req->tididx];
1402 }
1403 req->omfactor = EXP_TID_GET(tidval, LEN) * PAGE_SIZE >=
1404 KDETH_OM_MAX_SIZE ? KDETH_OM_LARGE : KDETH_OM_SMALL;
1405
1406 KDETH_SET(hdr->kdeth.ver_tid_offset, TIDCTRL,
1407 EXP_TID_GET(tidval, CTRL));
1408
1409 KDETH_SET(hdr->kdeth.ver_tid_offset, TID,
1410 EXP_TID_GET(tidval, IDX));
1411
1412 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_DISABLE_SH))
1413 KDETH_SET(hdr->kdeth.ver_tid_offset, SH, 0);
1414
1415
1416
1417
1418 SDMA_DBG(req, "TID offset %ubytes %uunits om%u",
1419 req->tidoffset, req->tidoffset / req->omfactor,
1420 req->omfactor != KDETH_OM_SMALL);
1421 KDETH_SET(hdr->kdeth.ver_tid_offset, OFFSET,
1422 req->tidoffset / req->omfactor);
1423 KDETH_SET(hdr->kdeth.ver_tid_offset, OM,
1424 req->omfactor != KDETH_OM_SMALL);
1425 }
1426done:
1427 trace_hfi1_sdma_user_header(pq->dd, pq->ctxt, pq->subctxt,
1428 req->info.comp_idx, hdr, tidval);
1429 return sdma_txadd_kvaddr(pq->dd, &tx->txreq, hdr, sizeof(*hdr));
1430}
1431
1432static int set_txreq_header_ahg(struct user_sdma_request *req,
1433 struct user_sdma_txreq *tx, u32 len)
1434{
1435 int diff = 0;
1436 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1437 struct hfi1_pkt_header *hdr = &req->hdr;
1438 u16 pbclen = le16_to_cpu(hdr->pbc[0]);
1439 u32 val32, tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(len));
1440
1441 if (PBC2LRH(pbclen) != lrhlen) {
1442
1443 AHG_HEADER_SET(req->ahg, diff, 0, 0, 12,
1444 cpu_to_le16(LRH2PBC(lrhlen)));
1445
1446 AHG_HEADER_SET(req->ahg, diff, 3, 0, 16,
1447 cpu_to_be16(lrhlen >> 2));
1448 }
1449
1450
1451
1452
1453
1454 val32 = (be32_to_cpu(hdr->bth[2]) + req->seqnum) &
1455 (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffff : 0xffffff);
1456 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_ACK))
1457 val32 |= 1UL << 31;
1458 AHG_HEADER_SET(req->ahg, diff, 6, 0, 16, cpu_to_be16(val32 >> 16));
1459 AHG_HEADER_SET(req->ahg, diff, 6, 16, 16, cpu_to_be16(val32 & 0xffff));
1460
1461 AHG_HEADER_SET(req->ahg, diff, 15, 0, 16,
1462 cpu_to_le16(req->koffset & 0xffff));
1463 AHG_HEADER_SET(req->ahg, diff, 15, 16, 16,
1464 cpu_to_le16(req->koffset >> 16));
1465 if (req_opcode(req->info.ctrl) == EXPECTED) {
1466 __le16 val;
1467
1468 tidval = req->tids[req->tididx];
1469
1470
1471
1472
1473
1474 if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
1475 PAGE_SIZE)) {
1476 req->tidoffset = 0;
1477
1478
1479
1480
1481 if (++req->tididx > req->n_tids - 1 ||
1482 !req->tids[req->tididx]) {
1483 return -EINVAL;
1484 }
1485 tidval = req->tids[req->tididx];
1486 }
1487 req->omfactor = ((EXP_TID_GET(tidval, LEN) *
1488 PAGE_SIZE) >=
1489 KDETH_OM_MAX_SIZE) ? KDETH_OM_LARGE :
1490 KDETH_OM_SMALL;
1491
1492 AHG_HEADER_SET(req->ahg, diff, 7, 0, 16,
1493 ((!!(req->omfactor - KDETH_OM_SMALL)) << 15 |
1494 ((req->tidoffset / req->omfactor) & 0x7fff)));
1495
1496 val = cpu_to_le16(((EXP_TID_GET(tidval, CTRL) & 0x3) << 10) |
1497 (EXP_TID_GET(tidval, IDX) & 0x3ff));
1498
1499 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_DISABLE_SH)) {
1500 val |= cpu_to_le16((KDETH_GET(hdr->kdeth.ver_tid_offset,
1501 INTR) <<
1502 AHG_KDETH_INTR_SHIFT));
1503 } else {
1504 val |= KDETH_GET(hdr->kdeth.ver_tid_offset, SH) ?
1505 cpu_to_le16(0x1 << AHG_KDETH_SH_SHIFT) :
1506 cpu_to_le16((KDETH_GET(hdr->kdeth.ver_tid_offset,
1507 INTR) <<
1508 AHG_KDETH_INTR_SHIFT));
1509 }
1510
1511 AHG_HEADER_SET(req->ahg, diff, 7, 16, 14, val);
1512 }
1513
1514 trace_hfi1_sdma_user_header_ahg(pq->dd, pq->ctxt, pq->subctxt,
1515 req->info.comp_idx, req->sde->this_idx,
1516 req->ahg_idx, req->ahg, diff, tidval);
1517 return diff;
1518}
1519
1520
1521
1522
1523
1524
1525
1526static void user_sdma_txreq_cb(struct sdma_txreq *txreq, int status)
1527{
1528 struct user_sdma_txreq *tx =
1529 container_of(txreq, struct user_sdma_txreq, txreq);
1530 struct user_sdma_request *req;
1531 struct hfi1_user_sdma_pkt_q *pq;
1532 struct hfi1_user_sdma_comp_q *cq;
1533 u16 idx;
1534
1535 if (!tx->req)
1536 return;
1537
1538 req = tx->req;
1539 pq = req->pq;
1540 cq = req->cq;
1541
1542 if (status != SDMA_TXREQ_S_OK) {
1543 SDMA_DBG(req, "SDMA completion with error %d",
1544 status);
1545 set_bit(SDMA_REQ_HAS_ERROR, &req->flags);
1546 }
1547
1548 req->seqcomp = tx->seqnum;
1549 kmem_cache_free(pq->txreq_cache, tx);
1550 tx = NULL;
1551
1552 idx = req->info.comp_idx;
1553 if (req->status == -1 && status == SDMA_TXREQ_S_OK) {
1554 if (req->seqcomp == req->info.npkts - 1) {
1555 req->status = 0;
1556 user_sdma_free_request(req, false);
1557 pq_update(pq);
1558 set_comp_state(pq, cq, idx, COMPLETE, 0);
1559 }
1560 } else {
1561 if (status != SDMA_TXREQ_S_OK)
1562 req->status = status;
1563 if (req->seqcomp == (ACCESS_ONCE(req->seqsubmitted) - 1) &&
1564 (test_bit(SDMA_REQ_SEND_DONE, &req->flags) ||
1565 test_bit(SDMA_REQ_DONE_ERROR, &req->flags))) {
1566 user_sdma_free_request(req, false);
1567 pq_update(pq);
1568 set_comp_state(pq, cq, idx, ERROR, req->status);
1569 }
1570 }
1571}
1572
1573static inline void pq_update(struct hfi1_user_sdma_pkt_q *pq)
1574{
1575 if (atomic_dec_and_test(&pq->n_reqs)) {
1576 xchg(&pq->state, SDMA_PKT_Q_INACTIVE);
1577 wake_up(&pq->wait);
1578 }
1579}
1580
1581static void user_sdma_free_request(struct user_sdma_request *req, bool unpin)
1582{
1583 if (!list_empty(&req->txps)) {
1584 struct sdma_txreq *t, *p;
1585
1586 list_for_each_entry_safe(t, p, &req->txps, list) {
1587 struct user_sdma_txreq *tx =
1588 container_of(t, struct user_sdma_txreq, txreq);
1589 list_del_init(&t->list);
1590 sdma_txclean(req->pq->dd, t);
1591 kmem_cache_free(req->pq->txreq_cache, tx);
1592 }
1593 }
1594 if (req->data_iovs) {
1595 struct sdma_mmu_node *node;
1596 int i;
1597
1598 for (i = 0; i < req->data_iovs; i++) {
1599 node = req->iovs[i].node;
1600 if (!node)
1601 continue;
1602
1603 if (unpin)
1604 hfi1_mmu_rb_remove(req->pq->handler,
1605 &node->rb);
1606 else
1607 atomic_dec(&node->refcount);
1608 }
1609 }
1610 kfree(req->tids);
1611 clear_bit(req->info.comp_idx, req->pq->req_in_use);
1612}
1613
1614static inline void set_comp_state(struct hfi1_user_sdma_pkt_q *pq,
1615 struct hfi1_user_sdma_comp_q *cq,
1616 u16 idx, enum hfi1_sdma_comp_state state,
1617 int ret)
1618{
1619 hfi1_cdbg(SDMA, "[%u:%u:%u:%u] Setting completion status %u %d",
1620 pq->dd->unit, pq->ctxt, pq->subctxt, idx, state, ret);
1621 cq->comps[idx].status = state;
1622 if (state == ERROR)
1623 cq->comps[idx].errcode = -ret;
1624 trace_hfi1_sdma_user_completion(pq->dd, pq->ctxt, pq->subctxt,
1625 idx, state, ret);
1626}
1627
1628static bool sdma_rb_filter(struct mmu_rb_node *node, unsigned long addr,
1629 unsigned long len)
1630{
1631 return (bool)(node->addr == addr);
1632}
1633
1634static int sdma_rb_insert(void *arg, struct mmu_rb_node *mnode)
1635{
1636 struct sdma_mmu_node *node =
1637 container_of(mnode, struct sdma_mmu_node, rb);
1638
1639 atomic_inc(&node->refcount);
1640 return 0;
1641}
1642
1643
1644
1645
1646
1647
1648static int sdma_rb_evict(void *arg, struct mmu_rb_node *mnode,
1649 void *evict_arg, bool *stop)
1650{
1651 struct sdma_mmu_node *node =
1652 container_of(mnode, struct sdma_mmu_node, rb);
1653 struct evict_data *evict_data = evict_arg;
1654
1655
1656 if (atomic_read(&node->refcount))
1657 return 0;
1658
1659
1660 evict_data->cleared += node->npages;
1661
1662
1663 if (evict_data->cleared >= evict_data->target)
1664 *stop = true;
1665
1666 return 1;
1667}
1668
1669static void sdma_rb_remove(void *arg, struct mmu_rb_node *mnode)
1670{
1671 struct sdma_mmu_node *node =
1672 container_of(mnode, struct sdma_mmu_node, rb);
1673
1674 atomic_sub(node->npages, &node->pq->n_locked);
1675
1676 unpin_vector_pages(node->pq->mm, node->pages, 0, node->npages);
1677
1678 kfree(node);
1679}
1680
1681static int sdma_rb_invalidate(void *arg, struct mmu_rb_node *mnode)
1682{
1683 struct sdma_mmu_node *node =
1684 container_of(mnode, struct sdma_mmu_node, rb);
1685
1686 if (!atomic_read(&node->refcount))
1687 return 1;
1688 return 0;
1689}
1690