1 /*
2 * Copyright(c) 2020 - Cornelis Networks, Inc.
3 * Copyright(c) 2015 - 2018 Intel Corporation.
4 *
5 * This file is provided under a dual BSD/GPLv2 license. When using or
6 * redistributing this file, you may do so under either license.
7 *
8 * GPL LICENSE SUMMARY
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * BSD LICENSE
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 *
25 * - Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * - Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in
29 * the documentation and/or other materials provided with the
30 * distribution.
31 * - Neither the name of Intel Corporation nor the names of its
32 * contributors may be used to endorse or promote products derived
33 * from this software without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 *
47 */
48 #include <linux/mm.h>
49 #include <linux/types.h>
50 #include <linux/device.h>
51 #include <linux/dmapool.h>
52 #include <linux/slab.h>
53 #include <linux/list.h>
54 #include <linux/highmem.h>
55 #include <linux/io.h>
56 #include <linux/uio.h>
57 #include <linux/rbtree.h>
58 #include <linux/spinlock.h>
59 #include <linux/delay.h>
60 #include <linux/kthread.h>
61 #include <linux/mmu_context.h>
62 #include <linux/module.h>
63 #include <linux/vmalloc.h>
64 #include <linux/string.h>
65
66 #include "hfi.h"
67 #include "sdma.h"
68 #include "user_sdma.h"
69 #include "verbs.h" /* for the headers */
70 #include "common.h" /* for struct hfi1_tid_info */
71 #include "trace.h"
72
73 static uint hfi1_sdma_comp_ring_size = 128;
74 module_param_named(sdma_comp_size, hfi1_sdma_comp_ring_size, uint, S_IRUGO);
75 MODULE_PARM_DESC(sdma_comp_size, "Size of User SDMA completion ring. Default: 128");
76
77 static unsigned initial_pkt_count = 8;
78
79 static int user_sdma_send_pkts(struct user_sdma_request *req, u16 maxpkts);
80 static void user_sdma_txreq_cb(struct sdma_txreq *txreq, int status);
81 static inline void pq_update(struct hfi1_user_sdma_pkt_q *pq);
82 static void user_sdma_free_request(struct user_sdma_request *req);
83 static int check_header_template(struct user_sdma_request *req,
84 struct hfi1_pkt_header *hdr, u32 lrhlen,
85 u32 datalen);
86 static int set_txreq_header(struct user_sdma_request *req,
87 struct user_sdma_txreq *tx, u32 datalen);
88 static int set_txreq_header_ahg(struct user_sdma_request *req,
89 struct user_sdma_txreq *tx, u32 len);
90 static inline void set_comp_state(struct hfi1_user_sdma_pkt_q *pq,
91 struct hfi1_user_sdma_comp_q *cq,
92 u16 idx, enum hfi1_sdma_comp_state state,
93 int ret);
94 static inline u32 set_pkt_bth_psn(__be32 bthpsn, u8 expct, u32 frags);
95 static inline u32 get_lrh_len(struct hfi1_pkt_header, u32 len);
96
97 static int defer_packet_queue(
98 struct sdma_engine *sde,
99 struct iowait_work *wait,
100 struct sdma_txreq *txreq,
101 uint seq,
102 bool pkts_sent);
103 static void activate_packet_queue(struct iowait *wait, int reason);
104 static bool sdma_rb_filter(struct mmu_rb_node *node, unsigned long addr,
105 unsigned long len);
106 static int sdma_rb_insert(void *arg, struct mmu_rb_node *mnode);
107 static int sdma_rb_evict(void *arg, struct mmu_rb_node *mnode,
108 void *arg2, bool *stop);
109 static void sdma_rb_remove(void *arg, struct mmu_rb_node *mnode);
110 static int sdma_rb_invalidate(void *arg, struct mmu_rb_node *mnode);
111
112 static struct mmu_rb_ops sdma_rb_ops = {
113 .filter = sdma_rb_filter,
114 .insert = sdma_rb_insert,
115 .evict = sdma_rb_evict,
116 .remove = sdma_rb_remove,
117 .invalidate = sdma_rb_invalidate
118 };
119
120 static int add_system_pages_to_sdma_packet(struct user_sdma_request *req,
121 struct user_sdma_txreq *tx,
122 struct user_sdma_iovec *iovec,
123 u32 *pkt_remaining);
124
defer_packet_queue(struct sdma_engine * sde,struct iowait_work * wait,struct sdma_txreq * txreq,uint seq,bool pkts_sent)125 static int defer_packet_queue(
126 struct sdma_engine *sde,
127 struct iowait_work *wait,
128 struct sdma_txreq *txreq,
129 uint seq,
130 bool pkts_sent)
131 {
132 struct hfi1_user_sdma_pkt_q *pq =
133 container_of(wait->iow, struct hfi1_user_sdma_pkt_q, busy);
134
135 write_seqlock(&sde->waitlock);
136 trace_hfi1_usdma_defer(pq, sde, &pq->busy);
137 if (sdma_progress(sde, seq, txreq))
138 goto eagain;
139 /*
140 * We are assuming that if the list is enqueued somewhere, it
141 * is to the dmawait list since that is the only place where
142 * it is supposed to be enqueued.
143 */
144 xchg(&pq->state, SDMA_PKT_Q_DEFERRED);
145 if (list_empty(&pq->busy.list)) {
146 pq->busy.lock = &sde->waitlock;
147 iowait_get_priority(&pq->busy);
148 iowait_queue(pkts_sent, &pq->busy, &sde->dmawait);
149 }
150 write_sequnlock(&sde->waitlock);
151 return -EBUSY;
152 eagain:
153 write_sequnlock(&sde->waitlock);
154 return -EAGAIN;
155 }
156
activate_packet_queue(struct iowait * wait,int reason)157 static void activate_packet_queue(struct iowait *wait, int reason)
158 {
159 struct hfi1_user_sdma_pkt_q *pq =
160 container_of(wait, struct hfi1_user_sdma_pkt_q, busy);
161
162 trace_hfi1_usdma_activate(pq, wait, reason);
163 xchg(&pq->state, SDMA_PKT_Q_ACTIVE);
164 wake_up(&wait->wait_dma);
165 };
166
hfi1_user_sdma_alloc_queues(struct hfi1_ctxtdata * uctxt,struct hfi1_filedata * fd)167 int hfi1_user_sdma_alloc_queues(struct hfi1_ctxtdata *uctxt,
168 struct hfi1_filedata *fd)
169 {
170 int ret = -ENOMEM;
171 char buf[64];
172 struct hfi1_devdata *dd;
173 struct hfi1_user_sdma_comp_q *cq;
174 struct hfi1_user_sdma_pkt_q *pq;
175
176 if (!uctxt || !fd)
177 return -EBADF;
178
179 if (!hfi1_sdma_comp_ring_size)
180 return -EINVAL;
181
182 dd = uctxt->dd;
183
184 pq = kzalloc(sizeof(*pq), GFP_KERNEL);
185 if (!pq)
186 return -ENOMEM;
187 pq->dd = dd;
188 pq->ctxt = uctxt->ctxt;
189 pq->subctxt = fd->subctxt;
190 pq->n_max_reqs = hfi1_sdma_comp_ring_size;
191 atomic_set(&pq->n_reqs, 0);
192 init_waitqueue_head(&pq->wait);
193 atomic_set(&pq->n_locked, 0);
194
195 iowait_init(&pq->busy, 0, NULL, NULL, defer_packet_queue,
196 activate_packet_queue, NULL, NULL);
197 pq->reqidx = 0;
198
199 pq->reqs = kcalloc(hfi1_sdma_comp_ring_size,
200 sizeof(*pq->reqs),
201 GFP_KERNEL);
202 if (!pq->reqs)
203 goto pq_reqs_nomem;
204
205 pq->req_in_use = kcalloc(BITS_TO_LONGS(hfi1_sdma_comp_ring_size),
206 sizeof(*pq->req_in_use),
207 GFP_KERNEL);
208 if (!pq->req_in_use)
209 goto pq_reqs_no_in_use;
210
211 snprintf(buf, 64, "txreq-kmem-cache-%u-%u-%u", dd->unit, uctxt->ctxt,
212 fd->subctxt);
213 pq->txreq_cache = kmem_cache_create(buf,
214 sizeof(struct user_sdma_txreq),
215 L1_CACHE_BYTES,
216 SLAB_HWCACHE_ALIGN,
217 NULL);
218 if (!pq->txreq_cache) {
219 dd_dev_err(dd, "[%u] Failed to allocate TxReq cache\n",
220 uctxt->ctxt);
221 goto pq_txreq_nomem;
222 }
223
224 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
225 if (!cq)
226 goto cq_nomem;
227
228 cq->comps = vmalloc_user(PAGE_ALIGN(sizeof(*cq->comps)
229 * hfi1_sdma_comp_ring_size));
230 if (!cq->comps)
231 goto cq_comps_nomem;
232
233 cq->nentries = hfi1_sdma_comp_ring_size;
234
235 ret = hfi1_mmu_rb_register(pq, &sdma_rb_ops, dd->pport->hfi1_wq,
236 &pq->handler);
237 if (ret) {
238 dd_dev_err(dd, "Failed to register with MMU %d", ret);
239 goto pq_mmu_fail;
240 }
241
242 rcu_assign_pointer(fd->pq, pq);
243 fd->cq = cq;
244
245 return 0;
246
247 pq_mmu_fail:
248 vfree(cq->comps);
249 cq_comps_nomem:
250 kfree(cq);
251 cq_nomem:
252 kmem_cache_destroy(pq->txreq_cache);
253 pq_txreq_nomem:
254 kfree(pq->req_in_use);
255 pq_reqs_no_in_use:
256 kfree(pq->reqs);
257 pq_reqs_nomem:
258 kfree(pq);
259
260 return ret;
261 }
262
flush_pq_iowait(struct hfi1_user_sdma_pkt_q * pq)263 static void flush_pq_iowait(struct hfi1_user_sdma_pkt_q *pq)
264 {
265 unsigned long flags;
266 seqlock_t *lock = pq->busy.lock;
267
268 if (!lock)
269 return;
270 write_seqlock_irqsave(lock, flags);
271 if (!list_empty(&pq->busy.list)) {
272 list_del_init(&pq->busy.list);
273 pq->busy.lock = NULL;
274 }
275 write_sequnlock_irqrestore(lock, flags);
276 }
277
hfi1_user_sdma_free_queues(struct hfi1_filedata * fd,struct hfi1_ctxtdata * uctxt)278 int hfi1_user_sdma_free_queues(struct hfi1_filedata *fd,
279 struct hfi1_ctxtdata *uctxt)
280 {
281 struct hfi1_user_sdma_pkt_q *pq;
282
283 trace_hfi1_sdma_user_free_queues(uctxt->dd, uctxt->ctxt, fd->subctxt);
284
285 spin_lock(&fd->pq_rcu_lock);
286 pq = srcu_dereference_check(fd->pq, &fd->pq_srcu,
287 lockdep_is_held(&fd->pq_rcu_lock));
288 if (pq) {
289 rcu_assign_pointer(fd->pq, NULL);
290 spin_unlock(&fd->pq_rcu_lock);
291 synchronize_srcu(&fd->pq_srcu);
292 /* at this point there can be no more new requests */
293 if (pq->handler)
294 hfi1_mmu_rb_unregister(pq->handler);
295 iowait_sdma_drain(&pq->busy);
296 /* Wait until all requests have been freed. */
297 wait_event_interruptible(
298 pq->wait,
299 !atomic_read(&pq->n_reqs));
300 kfree(pq->reqs);
301 kfree(pq->req_in_use);
302 kmem_cache_destroy(pq->txreq_cache);
303 flush_pq_iowait(pq);
304 kfree(pq);
305 } else {
306 spin_unlock(&fd->pq_rcu_lock);
307 }
308 if (fd->cq) {
309 vfree(fd->cq->comps);
310 kfree(fd->cq);
311 fd->cq = NULL;
312 }
313 return 0;
314 }
315
dlid_to_selector(u16 dlid)316 static u8 dlid_to_selector(u16 dlid)
317 {
318 static u8 mapping[256];
319 static int initialized;
320 static u8 next;
321 int hash;
322
323 if (!initialized) {
324 memset(mapping, 0xFF, 256);
325 initialized = 1;
326 }
327
328 hash = ((dlid >> 8) ^ dlid) & 0xFF;
329 if (mapping[hash] == 0xFF) {
330 mapping[hash] = next;
331 next = (next + 1) & 0x7F;
332 }
333
334 return mapping[hash];
335 }
336
337 /**
338 * hfi1_user_sdma_process_request() - Process and start a user sdma request
339 * @fd: valid file descriptor
340 * @iovec: array of io vectors to process
341 * @dim: overall iovec array size
342 * @count: number of io vector array entries processed
343 */
hfi1_user_sdma_process_request(struct hfi1_filedata * fd,struct iovec * iovec,unsigned long dim,unsigned long * count)344 int hfi1_user_sdma_process_request(struct hfi1_filedata *fd,
345 struct iovec *iovec, unsigned long dim,
346 unsigned long *count)
347 {
348 int ret = 0, i;
349 struct hfi1_ctxtdata *uctxt = fd->uctxt;
350 struct hfi1_user_sdma_pkt_q *pq =
351 srcu_dereference(fd->pq, &fd->pq_srcu);
352 struct hfi1_user_sdma_comp_q *cq = fd->cq;
353 struct hfi1_devdata *dd = pq->dd;
354 unsigned long idx = 0;
355 u8 pcount = initial_pkt_count;
356 struct sdma_req_info info;
357 struct user_sdma_request *req;
358 u8 opcode, sc, vl;
359 u16 pkey;
360 u32 slid;
361 u16 dlid;
362 u32 selector;
363
364 if (iovec[idx].iov_len < sizeof(info) + sizeof(req->hdr)) {
365 hfi1_cdbg(
366 SDMA,
367 "[%u:%u:%u] First vector not big enough for header %lu/%lu",
368 dd->unit, uctxt->ctxt, fd->subctxt,
369 iovec[idx].iov_len, sizeof(info) + sizeof(req->hdr));
370 return -EINVAL;
371 }
372 ret = copy_from_user(&info, iovec[idx].iov_base, sizeof(info));
373 if (ret) {
374 hfi1_cdbg(SDMA, "[%u:%u:%u] Failed to copy info QW (%d)",
375 dd->unit, uctxt->ctxt, fd->subctxt, ret);
376 return -EFAULT;
377 }
378
379 trace_hfi1_sdma_user_reqinfo(dd, uctxt->ctxt, fd->subctxt,
380 (u16 *)&info);
381 if (info.comp_idx >= hfi1_sdma_comp_ring_size) {
382 hfi1_cdbg(SDMA,
383 "[%u:%u:%u:%u] Invalid comp index",
384 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx);
385 return -EINVAL;
386 }
387
388 /*
389 * Sanity check the header io vector count. Need at least 1 vector
390 * (header) and cannot be larger than the actual io vector count.
391 */
392 if (req_iovcnt(info.ctrl) < 1 || req_iovcnt(info.ctrl) > dim) {
393 hfi1_cdbg(SDMA,
394 "[%u:%u:%u:%u] Invalid iov count %d, dim %ld",
395 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx,
396 req_iovcnt(info.ctrl), dim);
397 return -EINVAL;
398 }
399
400 if (!info.fragsize) {
401 hfi1_cdbg(SDMA,
402 "[%u:%u:%u:%u] Request does not specify fragsize",
403 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx);
404 return -EINVAL;
405 }
406
407 /* Try to claim the request. */
408 if (test_and_set_bit(info.comp_idx, pq->req_in_use)) {
409 hfi1_cdbg(SDMA, "[%u:%u:%u] Entry %u is in use",
410 dd->unit, uctxt->ctxt, fd->subctxt,
411 info.comp_idx);
412 return -EBADSLT;
413 }
414 /*
415 * All safety checks have been done and this request has been claimed.
416 */
417 trace_hfi1_sdma_user_process_request(dd, uctxt->ctxt, fd->subctxt,
418 info.comp_idx);
419 req = pq->reqs + info.comp_idx;
420 req->data_iovs = req_iovcnt(info.ctrl) - 1; /* subtract header vector */
421 req->data_len = 0;
422 req->pq = pq;
423 req->cq = cq;
424 req->ahg_idx = -1;
425 req->iov_idx = 0;
426 req->sent = 0;
427 req->seqnum = 0;
428 req->seqcomp = 0;
429 req->seqsubmitted = 0;
430 req->tids = NULL;
431 req->has_error = 0;
432 INIT_LIST_HEAD(&req->txps);
433
434 memcpy(&req->info, &info, sizeof(info));
435
436 /* The request is initialized, count it */
437 atomic_inc(&pq->n_reqs);
438
439 if (req_opcode(info.ctrl) == EXPECTED) {
440 /* expected must have a TID info and at least one data vector */
441 if (req->data_iovs < 2) {
442 SDMA_DBG(req,
443 "Not enough vectors for expected request");
444 ret = -EINVAL;
445 goto free_req;
446 }
447 req->data_iovs--;
448 }
449
450 if (!info.npkts || req->data_iovs > MAX_VECTORS_PER_REQ) {
451 SDMA_DBG(req, "Too many vectors (%u/%u)", req->data_iovs,
452 MAX_VECTORS_PER_REQ);
453 ret = -EINVAL;
454 goto free_req;
455 }
456
457 /* Copy the header from the user buffer */
458 ret = copy_from_user(&req->hdr, iovec[idx].iov_base + sizeof(info),
459 sizeof(req->hdr));
460 if (ret) {
461 SDMA_DBG(req, "Failed to copy header template (%d)", ret);
462 ret = -EFAULT;
463 goto free_req;
464 }
465
466 /* If Static rate control is not enabled, sanitize the header. */
467 if (!HFI1_CAP_IS_USET(STATIC_RATE_CTRL))
468 req->hdr.pbc[2] = 0;
469
470 /* Validate the opcode. Do not trust packets from user space blindly. */
471 opcode = (be32_to_cpu(req->hdr.bth[0]) >> 24) & 0xff;
472 if ((opcode & USER_OPCODE_CHECK_MASK) !=
473 USER_OPCODE_CHECK_VAL) {
474 SDMA_DBG(req, "Invalid opcode (%d)", opcode);
475 ret = -EINVAL;
476 goto free_req;
477 }
478 /*
479 * Validate the vl. Do not trust packets from user space blindly.
480 * VL comes from PBC, SC comes from LRH, and the VL needs to
481 * match the SC look up.
482 */
483 vl = (le16_to_cpu(req->hdr.pbc[0]) >> 12) & 0xF;
484 sc = (((be16_to_cpu(req->hdr.lrh[0]) >> 12) & 0xF) |
485 (((le16_to_cpu(req->hdr.pbc[1]) >> 14) & 0x1) << 4));
486 if (vl >= dd->pport->vls_operational ||
487 vl != sc_to_vlt(dd, sc)) {
488 SDMA_DBG(req, "Invalid SC(%u)/VL(%u)", sc, vl);
489 ret = -EINVAL;
490 goto free_req;
491 }
492
493 /* Checking P_KEY for requests from user-space */
494 pkey = (u16)be32_to_cpu(req->hdr.bth[0]);
495 slid = be16_to_cpu(req->hdr.lrh[3]);
496 if (egress_pkey_check(dd->pport, slid, pkey, sc, PKEY_CHECK_INVALID)) {
497 ret = -EINVAL;
498 goto free_req;
499 }
500
501 /*
502 * Also should check the BTH.lnh. If it says the next header is GRH then
503 * the RXE parsing will be off and will land in the middle of the KDETH
504 * or miss it entirely.
505 */
506 if ((be16_to_cpu(req->hdr.lrh[0]) & 0x3) == HFI1_LRH_GRH) {
507 SDMA_DBG(req, "User tried to pass in a GRH");
508 ret = -EINVAL;
509 goto free_req;
510 }
511
512 req->koffset = le32_to_cpu(req->hdr.kdeth.swdata[6]);
513 /*
514 * Calculate the initial TID offset based on the values of
515 * KDETH.OFFSET and KDETH.OM that are passed in.
516 */
517 req->tidoffset = KDETH_GET(req->hdr.kdeth.ver_tid_offset, OFFSET) *
518 (KDETH_GET(req->hdr.kdeth.ver_tid_offset, OM) ?
519 KDETH_OM_LARGE : KDETH_OM_SMALL);
520 trace_hfi1_sdma_user_initial_tidoffset(dd, uctxt->ctxt, fd->subctxt,
521 info.comp_idx, req->tidoffset);
522 idx++;
523
524 /* Save all the IO vector structures */
525 for (i = 0; i < req->data_iovs; i++) {
526 req->iovs[i].offset = 0;
527 INIT_LIST_HEAD(&req->iovs[i].list);
528 memcpy(&req->iovs[i].iov,
529 iovec + idx++,
530 sizeof(req->iovs[i].iov));
531 if (req->iovs[i].iov.iov_len == 0) {
532 ret = -EINVAL;
533 goto free_req;
534 }
535 req->data_len += req->iovs[i].iov.iov_len;
536 }
537 trace_hfi1_sdma_user_data_length(dd, uctxt->ctxt, fd->subctxt,
538 info.comp_idx, req->data_len);
539 if (pcount > req->info.npkts)
540 pcount = req->info.npkts;
541 /*
542 * Copy any TID info
543 * User space will provide the TID info only when the
544 * request type is EXPECTED. This is true even if there is
545 * only one packet in the request and the header is already
546 * setup. The reason for the singular TID case is that the
547 * driver needs to perform safety checks.
548 */
549 if (req_opcode(req->info.ctrl) == EXPECTED) {
550 u16 ntids = iovec[idx].iov_len / sizeof(*req->tids);
551 u32 *tmp;
552
553 if (!ntids || ntids > MAX_TID_PAIR_ENTRIES) {
554 ret = -EINVAL;
555 goto free_req;
556 }
557
558 /*
559 * We have to copy all of the tids because they may vary
560 * in size and, therefore, the TID count might not be
561 * equal to the pkt count. However, there is no way to
562 * tell at this point.
563 */
564 tmp = memdup_user(iovec[idx].iov_base,
565 ntids * sizeof(*req->tids));
566 if (IS_ERR(tmp)) {
567 ret = PTR_ERR(tmp);
568 SDMA_DBG(req, "Failed to copy %d TIDs (%d)",
569 ntids, ret);
570 goto free_req;
571 }
572 req->tids = tmp;
573 req->n_tids = ntids;
574 req->tididx = 0;
575 idx++;
576 }
577
578 dlid = be16_to_cpu(req->hdr.lrh[1]);
579 selector = dlid_to_selector(dlid);
580 selector += uctxt->ctxt + fd->subctxt;
581 req->sde = sdma_select_user_engine(dd, selector, vl);
582
583 if (!req->sde || !sdma_running(req->sde)) {
584 ret = -ECOMM;
585 goto free_req;
586 }
587
588 /* We don't need an AHG entry if the request contains only one packet */
589 if (req->info.npkts > 1 && HFI1_CAP_IS_USET(SDMA_AHG))
590 req->ahg_idx = sdma_ahg_alloc(req->sde);
591
592 set_comp_state(pq, cq, info.comp_idx, QUEUED, 0);
593 pq->state = SDMA_PKT_Q_ACTIVE;
594
595 /*
596 * This is a somewhat blocking send implementation.
597 * The driver will block the caller until all packets of the
598 * request have been submitted to the SDMA engine. However, it
599 * will not wait for send completions.
600 */
601 while (req->seqsubmitted != req->info.npkts) {
602 ret = user_sdma_send_pkts(req, pcount);
603 if (ret < 0) {
604 int we_ret;
605
606 if (ret != -EBUSY)
607 goto free_req;
608 we_ret = wait_event_interruptible_timeout(
609 pq->busy.wait_dma,
610 pq->state == SDMA_PKT_Q_ACTIVE,
611 msecs_to_jiffies(
612 SDMA_IOWAIT_TIMEOUT));
613 trace_hfi1_usdma_we(pq, we_ret);
614 if (we_ret <= 0)
615 flush_pq_iowait(pq);
616 }
617 }
618 *count += idx;
619 return 0;
620 free_req:
621 /*
622 * If the submitted seqsubmitted == npkts, the completion routine
623 * controls the final state. If sequbmitted < npkts, wait for any
624 * outstanding packets to finish before cleaning up.
625 */
626 if (req->seqsubmitted < req->info.npkts) {
627 if (req->seqsubmitted)
628 wait_event(pq->busy.wait_dma,
629 (req->seqcomp == req->seqsubmitted - 1));
630 user_sdma_free_request(req);
631 pq_update(pq);
632 set_comp_state(pq, cq, info.comp_idx, ERROR, ret);
633 }
634 return ret;
635 }
636
compute_data_length(struct user_sdma_request * req,struct user_sdma_txreq * tx)637 static inline u32 compute_data_length(struct user_sdma_request *req,
638 struct user_sdma_txreq *tx)
639 {
640 /*
641 * Determine the proper size of the packet data.
642 * The size of the data of the first packet is in the header
643 * template. However, it includes the header and ICRC, which need
644 * to be subtracted.
645 * The minimum representable packet data length in a header is 4 bytes,
646 * therefore, when the data length request is less than 4 bytes, there's
647 * only one packet, and the packet data length is equal to that of the
648 * request data length.
649 * The size of the remaining packets is the minimum of the frag
650 * size (MTU) or remaining data in the request.
651 */
652 u32 len;
653
654 if (!req->seqnum) {
655 if (req->data_len < sizeof(u32))
656 len = req->data_len;
657 else
658 len = ((be16_to_cpu(req->hdr.lrh[2]) << 2) -
659 (sizeof(tx->hdr) - 4));
660 } else if (req_opcode(req->info.ctrl) == EXPECTED) {
661 u32 tidlen = EXP_TID_GET(req->tids[req->tididx], LEN) *
662 PAGE_SIZE;
663 /*
664 * Get the data length based on the remaining space in the
665 * TID pair.
666 */
667 len = min(tidlen - req->tidoffset, (u32)req->info.fragsize);
668 /* If we've filled up the TID pair, move to the next one. */
669 if (unlikely(!len) && ++req->tididx < req->n_tids &&
670 req->tids[req->tididx]) {
671 tidlen = EXP_TID_GET(req->tids[req->tididx],
672 LEN) * PAGE_SIZE;
673 req->tidoffset = 0;
674 len = min_t(u32, tidlen, req->info.fragsize);
675 }
676 /*
677 * Since the TID pairs map entire pages, make sure that we
678 * are not going to try to send more data that we have
679 * remaining.
680 */
681 len = min(len, req->data_len - req->sent);
682 } else {
683 len = min(req->data_len - req->sent, (u32)req->info.fragsize);
684 }
685 trace_hfi1_sdma_user_compute_length(req->pq->dd,
686 req->pq->ctxt,
687 req->pq->subctxt,
688 req->info.comp_idx,
689 len);
690 return len;
691 }
692
pad_len(u32 len)693 static inline u32 pad_len(u32 len)
694 {
695 if (len & (sizeof(u32) - 1))
696 len += sizeof(u32) - (len & (sizeof(u32) - 1));
697 return len;
698 }
699
get_lrh_len(struct hfi1_pkt_header hdr,u32 len)700 static inline u32 get_lrh_len(struct hfi1_pkt_header hdr, u32 len)
701 {
702 /* (Size of complete header - size of PBC) + 4B ICRC + data length */
703 return ((sizeof(hdr) - sizeof(hdr.pbc)) + 4 + len);
704 }
705
user_sdma_txadd_ahg(struct user_sdma_request * req,struct user_sdma_txreq * tx,u32 datalen)706 static int user_sdma_txadd_ahg(struct user_sdma_request *req,
707 struct user_sdma_txreq *tx,
708 u32 datalen)
709 {
710 int ret;
711 u16 pbclen = le16_to_cpu(req->hdr.pbc[0]);
712 u32 lrhlen = get_lrh_len(req->hdr, pad_len(datalen));
713 struct hfi1_user_sdma_pkt_q *pq = req->pq;
714
715 /*
716 * Copy the request header into the tx header
717 * because the HW needs a cacheline-aligned
718 * address.
719 * This copy can be optimized out if the hdr
720 * member of user_sdma_request were also
721 * cacheline aligned.
722 */
723 memcpy(&tx->hdr, &req->hdr, sizeof(tx->hdr));
724 if (PBC2LRH(pbclen) != lrhlen) {
725 pbclen = (pbclen & 0xf000) | LRH2PBC(lrhlen);
726 tx->hdr.pbc[0] = cpu_to_le16(pbclen);
727 }
728 ret = check_header_template(req, &tx->hdr, lrhlen, datalen);
729 if (ret)
730 return ret;
731 ret = sdma_txinit_ahg(&tx->txreq, SDMA_TXREQ_F_AHG_COPY,
732 sizeof(tx->hdr) + datalen, req->ahg_idx,
733 0, NULL, 0, user_sdma_txreq_cb);
734 if (ret)
735 return ret;
736 ret = sdma_txadd_kvaddr(pq->dd, &tx->txreq, &tx->hdr, sizeof(tx->hdr));
737 if (ret)
738 sdma_txclean(pq->dd, &tx->txreq);
739 return ret;
740 }
741
user_sdma_send_pkts(struct user_sdma_request * req,u16 maxpkts)742 static int user_sdma_send_pkts(struct user_sdma_request *req, u16 maxpkts)
743 {
744 int ret = 0;
745 u16 count;
746 unsigned npkts = 0;
747 struct user_sdma_txreq *tx = NULL;
748 struct hfi1_user_sdma_pkt_q *pq = NULL;
749 struct user_sdma_iovec *iovec = NULL;
750
751 if (!req->pq)
752 return -EINVAL;
753
754 pq = req->pq;
755
756 /* If tx completion has reported an error, we are done. */
757 if (READ_ONCE(req->has_error))
758 return -EFAULT;
759
760 /*
761 * Check if we might have sent the entire request already
762 */
763 if (unlikely(req->seqnum == req->info.npkts)) {
764 if (!list_empty(&req->txps))
765 goto dosend;
766 return ret;
767 }
768
769 if (!maxpkts || maxpkts > req->info.npkts - req->seqnum)
770 maxpkts = req->info.npkts - req->seqnum;
771
772 while (npkts < maxpkts) {
773 u32 datalen = 0;
774
775 /*
776 * Check whether any of the completions have come back
777 * with errors. If so, we are not going to process any
778 * more packets from this request.
779 */
780 if (READ_ONCE(req->has_error))
781 return -EFAULT;
782
783 tx = kmem_cache_alloc(pq->txreq_cache, GFP_KERNEL);
784 if (!tx)
785 return -ENOMEM;
786
787 tx->flags = 0;
788 tx->req = req;
789 INIT_LIST_HEAD(&tx->list);
790
791 /*
792 * For the last packet set the ACK request
793 * and disable header suppression.
794 */
795 if (req->seqnum == req->info.npkts - 1)
796 tx->flags |= (TXREQ_FLAGS_REQ_ACK |
797 TXREQ_FLAGS_REQ_DISABLE_SH);
798
799 /*
800 * Calculate the payload size - this is min of the fragment
801 * (MTU) size or the remaining bytes in the request but only
802 * if we have payload data.
803 */
804 if (req->data_len) {
805 iovec = &req->iovs[req->iov_idx];
806 if (READ_ONCE(iovec->offset) == iovec->iov.iov_len) {
807 if (++req->iov_idx == req->data_iovs) {
808 ret = -EFAULT;
809 goto free_tx;
810 }
811 iovec = &req->iovs[req->iov_idx];
812 WARN_ON(iovec->offset);
813 }
814
815 datalen = compute_data_length(req, tx);
816
817 /*
818 * Disable header suppression for the payload <= 8DWS.
819 * If there is an uncorrectable error in the receive
820 * data FIFO when the received payload size is less than
821 * or equal to 8DWS then the RxDmaDataFifoRdUncErr is
822 * not reported.There is set RHF.EccErr if the header
823 * is not suppressed.
824 */
825 if (!datalen) {
826 SDMA_DBG(req,
827 "Request has data but pkt len is 0");
828 ret = -EFAULT;
829 goto free_tx;
830 } else if (datalen <= 32) {
831 tx->flags |= TXREQ_FLAGS_REQ_DISABLE_SH;
832 }
833 }
834
835 if (req->ahg_idx >= 0) {
836 if (!req->seqnum) {
837 ret = user_sdma_txadd_ahg(req, tx, datalen);
838 if (ret)
839 goto free_tx;
840 } else {
841 int changes;
842
843 changes = set_txreq_header_ahg(req, tx,
844 datalen);
845 if (changes < 0) {
846 ret = changes;
847 goto free_tx;
848 }
849 }
850 } else {
851 ret = sdma_txinit(&tx->txreq, 0, sizeof(req->hdr) +
852 datalen, user_sdma_txreq_cb);
853 if (ret)
854 goto free_tx;
855 /*
856 * Modify the header for this packet. This only needs
857 * to be done if we are not going to use AHG. Otherwise,
858 * the HW will do it based on the changes we gave it
859 * during sdma_txinit_ahg().
860 */
861 ret = set_txreq_header(req, tx, datalen);
862 if (ret)
863 goto free_txreq;
864 }
865
866 req->koffset += datalen;
867 if (req_opcode(req->info.ctrl) == EXPECTED)
868 req->tidoffset += datalen;
869 req->sent += datalen;
870 while (datalen) {
871 ret = add_system_pages_to_sdma_packet(req, tx, iovec,
872 &datalen);
873 if (ret)
874 goto free_txreq;
875 iovec = &req->iovs[req->iov_idx];
876 }
877 list_add_tail(&tx->txreq.list, &req->txps);
878 /*
879 * It is important to increment this here as it is used to
880 * generate the BTH.PSN and, therefore, can't be bulk-updated
881 * outside of the loop.
882 */
883 tx->seqnum = req->seqnum++;
884 npkts++;
885 }
886 dosend:
887 ret = sdma_send_txlist(req->sde,
888 iowait_get_ib_work(&pq->busy),
889 &req->txps, &count);
890 req->seqsubmitted += count;
891 if (req->seqsubmitted == req->info.npkts) {
892 /*
893 * The txreq has already been submitted to the HW queue
894 * so we can free the AHG entry now. Corruption will not
895 * happen due to the sequential manner in which
896 * descriptors are processed.
897 */
898 if (req->ahg_idx >= 0)
899 sdma_ahg_free(req->sde, req->ahg_idx);
900 }
901 return ret;
902
903 free_txreq:
904 sdma_txclean(pq->dd, &tx->txreq);
905 free_tx:
906 kmem_cache_free(pq->txreq_cache, tx);
907 return ret;
908 }
909
sdma_cache_evict(struct hfi1_user_sdma_pkt_q * pq,u32 npages)910 static u32 sdma_cache_evict(struct hfi1_user_sdma_pkt_q *pq, u32 npages)
911 {
912 struct evict_data evict_data;
913 struct mmu_rb_handler *handler = pq->handler;
914
915 evict_data.cleared = 0;
916 evict_data.target = npages;
917 hfi1_mmu_rb_evict(handler, &evict_data);
918 return evict_data.cleared;
919 }
920
check_header_template(struct user_sdma_request * req,struct hfi1_pkt_header * hdr,u32 lrhlen,u32 datalen)921 static int check_header_template(struct user_sdma_request *req,
922 struct hfi1_pkt_header *hdr, u32 lrhlen,
923 u32 datalen)
924 {
925 /*
926 * Perform safety checks for any type of packet:
927 * - transfer size is multiple of 64bytes
928 * - packet length is multiple of 4 bytes
929 * - packet length is not larger than MTU size
930 *
931 * These checks are only done for the first packet of the
932 * transfer since the header is "given" to us by user space.
933 * For the remainder of the packets we compute the values.
934 */
935 if (req->info.fragsize % PIO_BLOCK_SIZE || lrhlen & 0x3 ||
936 lrhlen > get_lrh_len(*hdr, req->info.fragsize))
937 return -EINVAL;
938
939 if (req_opcode(req->info.ctrl) == EXPECTED) {
940 /*
941 * The header is checked only on the first packet. Furthermore,
942 * we ensure that at least one TID entry is copied when the
943 * request is submitted. Therefore, we don't have to verify that
944 * tididx points to something sane.
945 */
946 u32 tidval = req->tids[req->tididx],
947 tidlen = EXP_TID_GET(tidval, LEN) * PAGE_SIZE,
948 tididx = EXP_TID_GET(tidval, IDX),
949 tidctrl = EXP_TID_GET(tidval, CTRL),
950 tidoff;
951 __le32 kval = hdr->kdeth.ver_tid_offset;
952
953 tidoff = KDETH_GET(kval, OFFSET) *
954 (KDETH_GET(req->hdr.kdeth.ver_tid_offset, OM) ?
955 KDETH_OM_LARGE : KDETH_OM_SMALL);
956 /*
957 * Expected receive packets have the following
958 * additional checks:
959 * - offset is not larger than the TID size
960 * - TIDCtrl values match between header and TID array
961 * - TID indexes match between header and TID array
962 */
963 if ((tidoff + datalen > tidlen) ||
964 KDETH_GET(kval, TIDCTRL) != tidctrl ||
965 KDETH_GET(kval, TID) != tididx)
966 return -EINVAL;
967 }
968 return 0;
969 }
970
971 /*
972 * Correctly set the BTH.PSN field based on type of
973 * transfer - eager packets can just increment the PSN but
974 * expected packets encode generation and sequence in the
975 * BTH.PSN field so just incrementing will result in errors.
976 */
set_pkt_bth_psn(__be32 bthpsn,u8 expct,u32 frags)977 static inline u32 set_pkt_bth_psn(__be32 bthpsn, u8 expct, u32 frags)
978 {
979 u32 val = be32_to_cpu(bthpsn),
980 mask = (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffffull :
981 0xffffffull),
982 psn = val & mask;
983 if (expct)
984 psn = (psn & ~HFI1_KDETH_BTH_SEQ_MASK) |
985 ((psn + frags) & HFI1_KDETH_BTH_SEQ_MASK);
986 else
987 psn = psn + frags;
988 return psn & mask;
989 }
990
set_txreq_header(struct user_sdma_request * req,struct user_sdma_txreq * tx,u32 datalen)991 static int set_txreq_header(struct user_sdma_request *req,
992 struct user_sdma_txreq *tx, u32 datalen)
993 {
994 struct hfi1_user_sdma_pkt_q *pq = req->pq;
995 struct hfi1_pkt_header *hdr = &tx->hdr;
996 u8 omfactor; /* KDETH.OM */
997 u16 pbclen;
998 int ret;
999 u32 tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(datalen));
1000
1001 /* Copy the header template to the request before modification */
1002 memcpy(hdr, &req->hdr, sizeof(*hdr));
1003
1004 /*
1005 * Check if the PBC and LRH length are mismatched. If so
1006 * adjust both in the header.
1007 */
1008 pbclen = le16_to_cpu(hdr->pbc[0]);
1009 if (PBC2LRH(pbclen) != lrhlen) {
1010 pbclen = (pbclen & 0xf000) | LRH2PBC(lrhlen);
1011 hdr->pbc[0] = cpu_to_le16(pbclen);
1012 hdr->lrh[2] = cpu_to_be16(lrhlen >> 2);
1013 /*
1014 * Third packet
1015 * This is the first packet in the sequence that has
1016 * a "static" size that can be used for the rest of
1017 * the packets (besides the last one).
1018 */
1019 if (unlikely(req->seqnum == 2)) {
1020 /*
1021 * From this point on the lengths in both the
1022 * PBC and LRH are the same until the last
1023 * packet.
1024 * Adjust the template so we don't have to update
1025 * every packet
1026 */
1027 req->hdr.pbc[0] = hdr->pbc[0];
1028 req->hdr.lrh[2] = hdr->lrh[2];
1029 }
1030 }
1031 /*
1032 * We only have to modify the header if this is not the
1033 * first packet in the request. Otherwise, we use the
1034 * header given to us.
1035 */
1036 if (unlikely(!req->seqnum)) {
1037 ret = check_header_template(req, hdr, lrhlen, datalen);
1038 if (ret)
1039 return ret;
1040 goto done;
1041 }
1042
1043 hdr->bth[2] = cpu_to_be32(
1044 set_pkt_bth_psn(hdr->bth[2],
1045 (req_opcode(req->info.ctrl) == EXPECTED),
1046 req->seqnum));
1047
1048 /* Set ACK request on last packet */
1049 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_ACK))
1050 hdr->bth[2] |= cpu_to_be32(1UL << 31);
1051
1052 /* Set the new offset */
1053 hdr->kdeth.swdata[6] = cpu_to_le32(req->koffset);
1054 /* Expected packets have to fill in the new TID information */
1055 if (req_opcode(req->info.ctrl) == EXPECTED) {
1056 tidval = req->tids[req->tididx];
1057 /*
1058 * If the offset puts us at the end of the current TID,
1059 * advance everything.
1060 */
1061 if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
1062 PAGE_SIZE)) {
1063 req->tidoffset = 0;
1064 /*
1065 * Since we don't copy all the TIDs, all at once,
1066 * we have to check again.
1067 */
1068 if (++req->tididx > req->n_tids - 1 ||
1069 !req->tids[req->tididx]) {
1070 return -EINVAL;
1071 }
1072 tidval = req->tids[req->tididx];
1073 }
1074 omfactor = EXP_TID_GET(tidval, LEN) * PAGE_SIZE >=
1075 KDETH_OM_MAX_SIZE ? KDETH_OM_LARGE_SHIFT :
1076 KDETH_OM_SMALL_SHIFT;
1077 /* Set KDETH.TIDCtrl based on value for this TID. */
1078 KDETH_SET(hdr->kdeth.ver_tid_offset, TIDCTRL,
1079 EXP_TID_GET(tidval, CTRL));
1080 /* Set KDETH.TID based on value for this TID */
1081 KDETH_SET(hdr->kdeth.ver_tid_offset, TID,
1082 EXP_TID_GET(tidval, IDX));
1083 /* Clear KDETH.SH when DISABLE_SH flag is set */
1084 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_DISABLE_SH))
1085 KDETH_SET(hdr->kdeth.ver_tid_offset, SH, 0);
1086 /*
1087 * Set the KDETH.OFFSET and KDETH.OM based on size of
1088 * transfer.
1089 */
1090 trace_hfi1_sdma_user_tid_info(
1091 pq->dd, pq->ctxt, pq->subctxt, req->info.comp_idx,
1092 req->tidoffset, req->tidoffset >> omfactor,
1093 omfactor != KDETH_OM_SMALL_SHIFT);
1094 KDETH_SET(hdr->kdeth.ver_tid_offset, OFFSET,
1095 req->tidoffset >> omfactor);
1096 KDETH_SET(hdr->kdeth.ver_tid_offset, OM,
1097 omfactor != KDETH_OM_SMALL_SHIFT);
1098 }
1099 done:
1100 trace_hfi1_sdma_user_header(pq->dd, pq->ctxt, pq->subctxt,
1101 req->info.comp_idx, hdr, tidval);
1102 return sdma_txadd_kvaddr(pq->dd, &tx->txreq, hdr, sizeof(*hdr));
1103 }
1104
set_txreq_header_ahg(struct user_sdma_request * req,struct user_sdma_txreq * tx,u32 datalen)1105 static int set_txreq_header_ahg(struct user_sdma_request *req,
1106 struct user_sdma_txreq *tx, u32 datalen)
1107 {
1108 u32 ahg[AHG_KDETH_ARRAY_SIZE];
1109 int idx = 0;
1110 u8 omfactor; /* KDETH.OM */
1111 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1112 struct hfi1_pkt_header *hdr = &req->hdr;
1113 u16 pbclen = le16_to_cpu(hdr->pbc[0]);
1114 u32 val32, tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(datalen));
1115 size_t array_size = ARRAY_SIZE(ahg);
1116
1117 if (PBC2LRH(pbclen) != lrhlen) {
1118 /* PBC.PbcLengthDWs */
1119 idx = ahg_header_set(ahg, idx, array_size, 0, 0, 12,
1120 (__force u16)cpu_to_le16(LRH2PBC(lrhlen)));
1121 if (idx < 0)
1122 return idx;
1123 /* LRH.PktLen (we need the full 16 bits due to byte swap) */
1124 idx = ahg_header_set(ahg, idx, array_size, 3, 0, 16,
1125 (__force u16)cpu_to_be16(lrhlen >> 2));
1126 if (idx < 0)
1127 return idx;
1128 }
1129
1130 /*
1131 * Do the common updates
1132 */
1133 /* BTH.PSN and BTH.A */
1134 val32 = (be32_to_cpu(hdr->bth[2]) + req->seqnum) &
1135 (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffff : 0xffffff);
1136 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_ACK))
1137 val32 |= 1UL << 31;
1138 idx = ahg_header_set(ahg, idx, array_size, 6, 0, 16,
1139 (__force u16)cpu_to_be16(val32 >> 16));
1140 if (idx < 0)
1141 return idx;
1142 idx = ahg_header_set(ahg, idx, array_size, 6, 16, 16,
1143 (__force u16)cpu_to_be16(val32 & 0xffff));
1144 if (idx < 0)
1145 return idx;
1146 /* KDETH.Offset */
1147 idx = ahg_header_set(ahg, idx, array_size, 15, 0, 16,
1148 (__force u16)cpu_to_le16(req->koffset & 0xffff));
1149 if (idx < 0)
1150 return idx;
1151 idx = ahg_header_set(ahg, idx, array_size, 15, 16, 16,
1152 (__force u16)cpu_to_le16(req->koffset >> 16));
1153 if (idx < 0)
1154 return idx;
1155 if (req_opcode(req->info.ctrl) == EXPECTED) {
1156 __le16 val;
1157
1158 tidval = req->tids[req->tididx];
1159
1160 /*
1161 * If the offset puts us at the end of the current TID,
1162 * advance everything.
1163 */
1164 if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
1165 PAGE_SIZE)) {
1166 req->tidoffset = 0;
1167 /*
1168 * Since we don't copy all the TIDs, all at once,
1169 * we have to check again.
1170 */
1171 if (++req->tididx > req->n_tids - 1 ||
1172 !req->tids[req->tididx])
1173 return -EINVAL;
1174 tidval = req->tids[req->tididx];
1175 }
1176 omfactor = ((EXP_TID_GET(tidval, LEN) *
1177 PAGE_SIZE) >=
1178 KDETH_OM_MAX_SIZE) ? KDETH_OM_LARGE_SHIFT :
1179 KDETH_OM_SMALL_SHIFT;
1180 /* KDETH.OM and KDETH.OFFSET (TID) */
1181 idx = ahg_header_set(
1182 ahg, idx, array_size, 7, 0, 16,
1183 ((!!(omfactor - KDETH_OM_SMALL_SHIFT)) << 15 |
1184 ((req->tidoffset >> omfactor)
1185 & 0x7fff)));
1186 if (idx < 0)
1187 return idx;
1188 /* KDETH.TIDCtrl, KDETH.TID, KDETH.Intr, KDETH.SH */
1189 val = cpu_to_le16(((EXP_TID_GET(tidval, CTRL) & 0x3) << 10) |
1190 (EXP_TID_GET(tidval, IDX) & 0x3ff));
1191
1192 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_DISABLE_SH)) {
1193 val |= cpu_to_le16((KDETH_GET(hdr->kdeth.ver_tid_offset,
1194 INTR) <<
1195 AHG_KDETH_INTR_SHIFT));
1196 } else {
1197 val |= KDETH_GET(hdr->kdeth.ver_tid_offset, SH) ?
1198 cpu_to_le16(0x1 << AHG_KDETH_SH_SHIFT) :
1199 cpu_to_le16((KDETH_GET(hdr->kdeth.ver_tid_offset,
1200 INTR) <<
1201 AHG_KDETH_INTR_SHIFT));
1202 }
1203
1204 idx = ahg_header_set(ahg, idx, array_size,
1205 7, 16, 14, (__force u16)val);
1206 if (idx < 0)
1207 return idx;
1208 }
1209
1210 trace_hfi1_sdma_user_header_ahg(pq->dd, pq->ctxt, pq->subctxt,
1211 req->info.comp_idx, req->sde->this_idx,
1212 req->ahg_idx, ahg, idx, tidval);
1213 sdma_txinit_ahg(&tx->txreq,
1214 SDMA_TXREQ_F_USE_AHG,
1215 datalen, req->ahg_idx, idx,
1216 ahg, sizeof(req->hdr),
1217 user_sdma_txreq_cb);
1218
1219 return idx;
1220 }
1221
1222 /**
1223 * user_sdma_txreq_cb() - SDMA tx request completion callback.
1224 * @txreq: valid sdma tx request
1225 * @status: success/failure of request
1226 *
1227 * Called when the SDMA progress state machine gets notification that
1228 * the SDMA descriptors for this tx request have been processed by the
1229 * DMA engine. Called in interrupt context.
1230 * Only do work on completed sequences.
1231 */
user_sdma_txreq_cb(struct sdma_txreq * txreq,int status)1232 static void user_sdma_txreq_cb(struct sdma_txreq *txreq, int status)
1233 {
1234 struct user_sdma_txreq *tx =
1235 container_of(txreq, struct user_sdma_txreq, txreq);
1236 struct user_sdma_request *req;
1237 struct hfi1_user_sdma_pkt_q *pq;
1238 struct hfi1_user_sdma_comp_q *cq;
1239 enum hfi1_sdma_comp_state state = COMPLETE;
1240
1241 if (!tx->req)
1242 return;
1243
1244 req = tx->req;
1245 pq = req->pq;
1246 cq = req->cq;
1247
1248 if (status != SDMA_TXREQ_S_OK) {
1249 SDMA_DBG(req, "SDMA completion with error %d",
1250 status);
1251 WRITE_ONCE(req->has_error, 1);
1252 state = ERROR;
1253 }
1254
1255 req->seqcomp = tx->seqnum;
1256 kmem_cache_free(pq->txreq_cache, tx);
1257
1258 /* sequence isn't complete? We are done */
1259 if (req->seqcomp != req->info.npkts - 1)
1260 return;
1261
1262 user_sdma_free_request(req);
1263 set_comp_state(pq, cq, req->info.comp_idx, state, status);
1264 pq_update(pq);
1265 }
1266
pq_update(struct hfi1_user_sdma_pkt_q * pq)1267 static inline void pq_update(struct hfi1_user_sdma_pkt_q *pq)
1268 {
1269 if (atomic_dec_and_test(&pq->n_reqs))
1270 wake_up(&pq->wait);
1271 }
1272
user_sdma_free_request(struct user_sdma_request * req)1273 static void user_sdma_free_request(struct user_sdma_request *req)
1274 {
1275 if (!list_empty(&req->txps)) {
1276 struct sdma_txreq *t, *p;
1277
1278 list_for_each_entry_safe(t, p, &req->txps, list) {
1279 struct user_sdma_txreq *tx =
1280 container_of(t, struct user_sdma_txreq, txreq);
1281 list_del_init(&t->list);
1282 sdma_txclean(req->pq->dd, t);
1283 kmem_cache_free(req->pq->txreq_cache, tx);
1284 }
1285 }
1286
1287 kfree(req->tids);
1288 clear_bit(req->info.comp_idx, req->pq->req_in_use);
1289 }
1290
set_comp_state(struct hfi1_user_sdma_pkt_q * pq,struct hfi1_user_sdma_comp_q * cq,u16 idx,enum hfi1_sdma_comp_state state,int ret)1291 static inline void set_comp_state(struct hfi1_user_sdma_pkt_q *pq,
1292 struct hfi1_user_sdma_comp_q *cq,
1293 u16 idx, enum hfi1_sdma_comp_state state,
1294 int ret)
1295 {
1296 if (state == ERROR)
1297 cq->comps[idx].errcode = -ret;
1298 smp_wmb(); /* make sure errcode is visible first */
1299 cq->comps[idx].status = state;
1300 trace_hfi1_sdma_user_completion(pq->dd, pq->ctxt, pq->subctxt,
1301 idx, state, ret);
1302 }
1303
unpin_vector_pages(struct mm_struct * mm,struct page ** pages,unsigned int start,unsigned int npages)1304 static void unpin_vector_pages(struct mm_struct *mm, struct page **pages,
1305 unsigned int start, unsigned int npages)
1306 {
1307 hfi1_release_user_pages(mm, pages + start, npages, false);
1308 kfree(pages);
1309 }
1310
free_system_node(struct sdma_mmu_node * node)1311 static void free_system_node(struct sdma_mmu_node *node)
1312 {
1313 if (node->npages) {
1314 unpin_vector_pages(mm_from_sdma_node(node), node->pages, 0,
1315 node->npages);
1316 atomic_sub(node->npages, &node->pq->n_locked);
1317 }
1318 kfree(node);
1319 }
1320
acquire_node(struct sdma_mmu_node * node)1321 static inline void acquire_node(struct sdma_mmu_node *node)
1322 {
1323 atomic_inc(&node->refcount);
1324 WARN_ON(atomic_read(&node->refcount) < 0);
1325 }
1326
release_node(struct mmu_rb_handler * handler,struct sdma_mmu_node * node)1327 static inline void release_node(struct mmu_rb_handler *handler,
1328 struct sdma_mmu_node *node)
1329 {
1330 atomic_dec(&node->refcount);
1331 WARN_ON(atomic_read(&node->refcount) < 0);
1332 }
1333
find_system_node(struct mmu_rb_handler * handler,unsigned long start,unsigned long end)1334 static struct sdma_mmu_node *find_system_node(struct mmu_rb_handler *handler,
1335 unsigned long start,
1336 unsigned long end)
1337 {
1338 struct mmu_rb_node *rb_node;
1339 struct sdma_mmu_node *node;
1340 unsigned long flags;
1341
1342 spin_lock_irqsave(&handler->lock, flags);
1343 rb_node = hfi1_mmu_rb_get_first(handler, start, (end - start));
1344 if (!rb_node) {
1345 spin_unlock_irqrestore(&handler->lock, flags);
1346 return NULL;
1347 }
1348 node = container_of(rb_node, struct sdma_mmu_node, rb);
1349 acquire_node(node);
1350 spin_unlock_irqrestore(&handler->lock, flags);
1351
1352 return node;
1353 }
1354
pin_system_pages(struct user_sdma_request * req,uintptr_t start_address,size_t length,struct sdma_mmu_node * node,int npages)1355 static int pin_system_pages(struct user_sdma_request *req,
1356 uintptr_t start_address, size_t length,
1357 struct sdma_mmu_node *node, int npages)
1358 {
1359 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1360 int pinned, cleared;
1361 struct page **pages;
1362
1363 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
1364 if (!pages)
1365 return -ENOMEM;
1366
1367 retry:
1368 if (!hfi1_can_pin_pages(pq->dd, current->mm, atomic_read(&pq->n_locked),
1369 npages)) {
1370 SDMA_DBG(req, "Evicting: nlocked %u npages %u",
1371 atomic_read(&pq->n_locked), npages);
1372 cleared = sdma_cache_evict(pq, npages);
1373 if (cleared >= npages)
1374 goto retry;
1375 }
1376
1377 SDMA_DBG(req, "Acquire user pages start_address %lx node->npages %u npages %u",
1378 start_address, node->npages, npages);
1379 pinned = hfi1_acquire_user_pages(current->mm, start_address, npages, 0,
1380 pages);
1381
1382 if (pinned < 0) {
1383 kfree(pages);
1384 SDMA_DBG(req, "pinned %d", pinned);
1385 return pinned;
1386 }
1387 if (pinned != npages) {
1388 unpin_vector_pages(current->mm, pages, node->npages, pinned);
1389 SDMA_DBG(req, "npages %u pinned %d", npages, pinned);
1390 return -EFAULT;
1391 }
1392 node->rb.addr = start_address;
1393 node->rb.len = length;
1394 node->pages = pages;
1395 node->npages = npages;
1396 atomic_add(pinned, &pq->n_locked);
1397 SDMA_DBG(req, "done. pinned %d", pinned);
1398 return 0;
1399 }
1400
add_system_pinning(struct user_sdma_request * req,struct sdma_mmu_node ** node_p,unsigned long start,unsigned long len)1401 static int add_system_pinning(struct user_sdma_request *req,
1402 struct sdma_mmu_node **node_p,
1403 unsigned long start, unsigned long len)
1404
1405 {
1406 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1407 struct sdma_mmu_node *node;
1408 int ret;
1409
1410 node = kzalloc(sizeof(*node), GFP_KERNEL);
1411 if (!node)
1412 return -ENOMEM;
1413
1414 node->pq = pq;
1415 ret = pin_system_pages(req, start, len, node, PFN_DOWN(len));
1416 if (ret == 0) {
1417 ret = hfi1_mmu_rb_insert(pq->handler, &node->rb);
1418 if (ret)
1419 free_system_node(node);
1420 else
1421 *node_p = node;
1422
1423 return ret;
1424 }
1425
1426 kfree(node);
1427 return ret;
1428 }
1429
get_system_cache_entry(struct user_sdma_request * req,struct sdma_mmu_node ** node_p,size_t req_start,size_t req_len)1430 static int get_system_cache_entry(struct user_sdma_request *req,
1431 struct sdma_mmu_node **node_p,
1432 size_t req_start, size_t req_len)
1433 {
1434 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1435 u64 start = ALIGN_DOWN(req_start, PAGE_SIZE);
1436 u64 end = PFN_ALIGN(req_start + req_len);
1437 struct mmu_rb_handler *handler = pq->handler;
1438 int ret;
1439
1440 if ((end - start) == 0) {
1441 SDMA_DBG(req,
1442 "Request for empty cache entry req_start %lx req_len %lx start %llx end %llx",
1443 req_start, req_len, start, end);
1444 return -EINVAL;
1445 }
1446
1447 SDMA_DBG(req, "req_start %lx req_len %lu", req_start, req_len);
1448
1449 while (1) {
1450 struct sdma_mmu_node *node =
1451 find_system_node(handler, start, end);
1452 u64 prepend_len = 0;
1453
1454 SDMA_DBG(req, "node %p start %llx end %llu", node, start, end);
1455 if (!node) {
1456 ret = add_system_pinning(req, node_p, start,
1457 end - start);
1458 if (ret == -EEXIST) {
1459 /*
1460 * Another execution context has inserted a
1461 * conficting entry first.
1462 */
1463 continue;
1464 }
1465 return ret;
1466 }
1467
1468 if (node->rb.addr <= start) {
1469 /*
1470 * This entry covers at least part of the region. If it doesn't extend
1471 * to the end, then this will be called again for the next segment.
1472 */
1473 *node_p = node;
1474 return 0;
1475 }
1476
1477 SDMA_DBG(req, "prepend: node->rb.addr %lx, node->refcount %d",
1478 node->rb.addr, atomic_read(&node->refcount));
1479 prepend_len = node->rb.addr - start;
1480
1481 /*
1482 * This node will not be returned, instead a new node
1483 * will be. So release the reference.
1484 */
1485 release_node(handler, node);
1486
1487 /* Prepend a node to cover the beginning of the allocation */
1488 ret = add_system_pinning(req, node_p, start, prepend_len);
1489 if (ret == -EEXIST) {
1490 /* Another execution context has inserted a conficting entry first. */
1491 continue;
1492 }
1493 return ret;
1494 }
1495 }
1496
add_mapping_to_sdma_packet(struct user_sdma_request * req,struct user_sdma_txreq * tx,struct sdma_mmu_node * cache_entry,size_t start,size_t from_this_cache_entry)1497 static int add_mapping_to_sdma_packet(struct user_sdma_request *req,
1498 struct user_sdma_txreq *tx,
1499 struct sdma_mmu_node *cache_entry,
1500 size_t start,
1501 size_t from_this_cache_entry)
1502 {
1503 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1504 unsigned int page_offset;
1505 unsigned int from_this_page;
1506 size_t page_index;
1507 void *ctx;
1508 int ret;
1509
1510 /*
1511 * Because the cache may be more fragmented than the memory that is being accessed,
1512 * it's not strictly necessary to have a descriptor per cache entry.
1513 */
1514
1515 while (from_this_cache_entry) {
1516 page_index = PFN_DOWN(start - cache_entry->rb.addr);
1517
1518 if (page_index >= cache_entry->npages) {
1519 SDMA_DBG(req,
1520 "Request for page_index %zu >= cache_entry->npages %u",
1521 page_index, cache_entry->npages);
1522 return -EINVAL;
1523 }
1524
1525 page_offset = start - ALIGN_DOWN(start, PAGE_SIZE);
1526 from_this_page = PAGE_SIZE - page_offset;
1527
1528 if (from_this_page < from_this_cache_entry) {
1529 ctx = NULL;
1530 } else {
1531 /*
1532 * In the case they are equal the next line has no practical effect,
1533 * but it's better to do a register to register copy than a conditional
1534 * branch.
1535 */
1536 from_this_page = from_this_cache_entry;
1537 ctx = cache_entry;
1538 }
1539
1540 ret = sdma_txadd_page(pq->dd, ctx, &tx->txreq,
1541 cache_entry->pages[page_index],
1542 page_offset, from_this_page);
1543 if (ret) {
1544 /*
1545 * When there's a failure, the entire request is freed by
1546 * user_sdma_send_pkts().
1547 */
1548 SDMA_DBG(req,
1549 "sdma_txadd_page failed %d page_index %lu page_offset %u from_this_page %u",
1550 ret, page_index, page_offset, from_this_page);
1551 return ret;
1552 }
1553 start += from_this_page;
1554 from_this_cache_entry -= from_this_page;
1555 }
1556 return 0;
1557 }
1558
add_system_iovec_to_sdma_packet(struct user_sdma_request * req,struct user_sdma_txreq * tx,struct user_sdma_iovec * iovec,size_t from_this_iovec)1559 static int add_system_iovec_to_sdma_packet(struct user_sdma_request *req,
1560 struct user_sdma_txreq *tx,
1561 struct user_sdma_iovec *iovec,
1562 size_t from_this_iovec)
1563 {
1564 struct mmu_rb_handler *handler = req->pq->handler;
1565
1566 while (from_this_iovec > 0) {
1567 struct sdma_mmu_node *cache_entry;
1568 size_t from_this_cache_entry;
1569 size_t start;
1570 int ret;
1571
1572 start = (uintptr_t)iovec->iov.iov_base + iovec->offset;
1573 ret = get_system_cache_entry(req, &cache_entry, start,
1574 from_this_iovec);
1575 if (ret) {
1576 SDMA_DBG(req, "pin system segment failed %d", ret);
1577 return ret;
1578 }
1579
1580 from_this_cache_entry = cache_entry->rb.len - (start - cache_entry->rb.addr);
1581 if (from_this_cache_entry > from_this_iovec)
1582 from_this_cache_entry = from_this_iovec;
1583
1584 ret = add_mapping_to_sdma_packet(req, tx, cache_entry, start,
1585 from_this_cache_entry);
1586 if (ret) {
1587 /*
1588 * We're guaranteed that there will be no descriptor
1589 * completion callback that releases this node
1590 * because only the last descriptor referencing it
1591 * has a context attached, and a failure means the
1592 * last descriptor was never added.
1593 */
1594 release_node(handler, cache_entry);
1595 SDMA_DBG(req, "add system segment failed %d", ret);
1596 return ret;
1597 }
1598
1599 iovec->offset += from_this_cache_entry;
1600 from_this_iovec -= from_this_cache_entry;
1601 }
1602
1603 return 0;
1604 }
1605
add_system_pages_to_sdma_packet(struct user_sdma_request * req,struct user_sdma_txreq * tx,struct user_sdma_iovec * iovec,u32 * pkt_data_remaining)1606 static int add_system_pages_to_sdma_packet(struct user_sdma_request *req,
1607 struct user_sdma_txreq *tx,
1608 struct user_sdma_iovec *iovec,
1609 u32 *pkt_data_remaining)
1610 {
1611 size_t remaining_to_add = *pkt_data_remaining;
1612 /*
1613 * Walk through iovec entries, ensure the associated pages
1614 * are pinned and mapped, add data to the packet until no more
1615 * data remains to be added.
1616 */
1617 while (remaining_to_add > 0) {
1618 struct user_sdma_iovec *cur_iovec;
1619 size_t from_this_iovec;
1620 int ret;
1621
1622 cur_iovec = iovec;
1623 from_this_iovec = iovec->iov.iov_len - iovec->offset;
1624
1625 if (from_this_iovec > remaining_to_add) {
1626 from_this_iovec = remaining_to_add;
1627 } else {
1628 /* The current iovec entry will be consumed by this pass. */
1629 req->iov_idx++;
1630 iovec++;
1631 }
1632
1633 ret = add_system_iovec_to_sdma_packet(req, tx, cur_iovec,
1634 from_this_iovec);
1635 if (ret)
1636 return ret;
1637
1638 remaining_to_add -= from_this_iovec;
1639 }
1640 *pkt_data_remaining = remaining_to_add;
1641
1642 return 0;
1643 }
1644
system_descriptor_complete(struct hfi1_devdata * dd,struct sdma_desc * descp)1645 void system_descriptor_complete(struct hfi1_devdata *dd,
1646 struct sdma_desc *descp)
1647 {
1648 switch (sdma_mapping_type(descp)) {
1649 case SDMA_MAP_SINGLE:
1650 dma_unmap_single(&dd->pcidev->dev, sdma_mapping_addr(descp),
1651 sdma_mapping_len(descp), DMA_TO_DEVICE);
1652 break;
1653 case SDMA_MAP_PAGE:
1654 dma_unmap_page(&dd->pcidev->dev, sdma_mapping_addr(descp),
1655 sdma_mapping_len(descp), DMA_TO_DEVICE);
1656 break;
1657 }
1658
1659 if (descp->pinning_ctx) {
1660 struct sdma_mmu_node *node = descp->pinning_ctx;
1661
1662 release_node(node->rb.handler, node);
1663 }
1664 }
1665
sdma_rb_filter(struct mmu_rb_node * node,unsigned long addr,unsigned long len)1666 static bool sdma_rb_filter(struct mmu_rb_node *node, unsigned long addr,
1667 unsigned long len)
1668 {
1669 return (bool)(node->addr == addr);
1670 }
1671
sdma_rb_insert(void * arg,struct mmu_rb_node * mnode)1672 static int sdma_rb_insert(void *arg, struct mmu_rb_node *mnode)
1673 {
1674 struct sdma_mmu_node *node =
1675 container_of(mnode, struct sdma_mmu_node, rb);
1676
1677 atomic_inc(&node->refcount);
1678 return 0;
1679 }
1680
1681 /*
1682 * Return 1 to remove the node from the rb tree and call the remove op.
1683 *
1684 * Called with the rb tree lock held.
1685 */
sdma_rb_evict(void * arg,struct mmu_rb_node * mnode,void * evict_arg,bool * stop)1686 static int sdma_rb_evict(void *arg, struct mmu_rb_node *mnode,
1687 void *evict_arg, bool *stop)
1688 {
1689 struct sdma_mmu_node *node =
1690 container_of(mnode, struct sdma_mmu_node, rb);
1691 struct evict_data *evict_data = evict_arg;
1692
1693 /* is this node still being used? */
1694 if (atomic_read(&node->refcount))
1695 return 0; /* keep this node */
1696
1697 /* this node will be evicted, add its pages to our count */
1698 evict_data->cleared += node->npages;
1699
1700 /* have enough pages been cleared? */
1701 if (evict_data->cleared >= evict_data->target)
1702 *stop = true;
1703
1704 return 1; /* remove this node */
1705 }
1706
sdma_rb_remove(void * arg,struct mmu_rb_node * mnode)1707 static void sdma_rb_remove(void *arg, struct mmu_rb_node *mnode)
1708 {
1709 struct sdma_mmu_node *node =
1710 container_of(mnode, struct sdma_mmu_node, rb);
1711
1712 free_system_node(node);
1713 }
1714
sdma_rb_invalidate(void * arg,struct mmu_rb_node * mnode)1715 static int sdma_rb_invalidate(void *arg, struct mmu_rb_node *mnode)
1716 {
1717 struct sdma_mmu_node *node =
1718 container_of(mnode, struct sdma_mmu_node, rb);
1719
1720 if (!atomic_read(&node->refcount))
1721 return 1;
1722 return 0;
1723 }
1724