blob: 0749689d76439f7ac95efdfceb04c5299bd80f26 [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001/*
Jubin John05d6ac12016-02-14 20:22:17 -08002 * Copyright(c) 2015, 2016 Intel Corporation.
Mike Marciniszyn77241052015-07-30 15:17:43 -04003 *
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
6 *
7 * GPL LICENSE SUMMARY
8 *
Mike Marciniszyn77241052015-07-30 15:17:43 -04009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * BSD LICENSE
19 *
Mike Marciniszyn77241052015-07-30 15:17:43 -040020 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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>
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -080063#include <linux/string.h>
Mike Marciniszyn77241052015-07-30 15:17:43 -040064
65#include "hfi.h"
66#include "sdma.h"
67#include "user_sdma.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040068#include "verbs.h" /* for the headers */
69#include "common.h" /* for struct hfi1_tid_info */
70#include "trace.h"
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -080071#include "mmu_rb.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040072
73static uint hfi1_sdma_comp_ring_size = 128;
74module_param_named(sdma_comp_size, hfi1_sdma_comp_ring_size, uint, S_IRUGO);
75MODULE_PARM_DESC(sdma_comp_size, "Size of User SDMA completion ring. Default: 128");
76
77/* The maximum number of Data io vectors per message/request */
78#define MAX_VECTORS_PER_REQ 8
79/*
80 * Maximum number of packet to send from each message/request
81 * before moving to the next one.
82 */
83#define MAX_PKTS_PER_QUEUE 16
84
85#define num_pages(x) (1 + ((((x) - 1) & PAGE_MASK) >> PAGE_SHIFT))
86
87#define req_opcode(x) \
88 (((x) >> HFI1_SDMA_REQ_OPCODE_SHIFT) & HFI1_SDMA_REQ_OPCODE_MASK)
89#define req_version(x) \
90 (((x) >> HFI1_SDMA_REQ_VERSION_SHIFT) & HFI1_SDMA_REQ_OPCODE_MASK)
91#define req_iovcnt(x) \
92 (((x) >> HFI1_SDMA_REQ_IOVCNT_SHIFT) & HFI1_SDMA_REQ_IOVCNT_MASK)
93
94/* Number of BTH.PSN bits used for sequence number in expected rcvs */
95#define BTH_SEQ_MASK 0x7ffull
96
97/*
98 * Define fields in the KDETH header so we can update the header
99 * template.
100 */
101#define KDETH_OFFSET_SHIFT 0
102#define KDETH_OFFSET_MASK 0x7fff
103#define KDETH_OM_SHIFT 15
104#define KDETH_OM_MASK 0x1
105#define KDETH_TID_SHIFT 16
106#define KDETH_TID_MASK 0x3ff
107#define KDETH_TIDCTRL_SHIFT 26
108#define KDETH_TIDCTRL_MASK 0x3
109#define KDETH_INTR_SHIFT 28
110#define KDETH_INTR_MASK 0x1
111#define KDETH_SH_SHIFT 29
112#define KDETH_SH_MASK 0x1
113#define KDETH_HCRC_UPPER_SHIFT 16
114#define KDETH_HCRC_UPPER_MASK 0xff
115#define KDETH_HCRC_LOWER_SHIFT 24
116#define KDETH_HCRC_LOWER_MASK 0xff
117
Jubin Johnaf534932016-08-31 07:24:27 -0700118#define AHG_KDETH_INTR_SHIFT 12
Jakub Pawlake7301392016-12-07 19:32:41 -0800119#define AHG_KDETH_SH_SHIFT 13
Jubin Johnaf534932016-08-31 07:24:27 -0700120
Mike Marciniszyn77241052015-07-30 15:17:43 -0400121#define PBC2LRH(x) ((((x) & 0xfff) << 2) - 4)
122#define LRH2PBC(x) ((((x) >> 2) + 1) & 0xfff)
123
124#define KDETH_GET(val, field) \
125 (((le32_to_cpu((val))) >> KDETH_##field##_SHIFT) & KDETH_##field##_MASK)
126#define KDETH_SET(dw, field, val) do { \
127 u32 dwval = le32_to_cpu(dw); \
128 dwval &= ~(KDETH_##field##_MASK << KDETH_##field##_SHIFT); \
129 dwval |= (((val) & KDETH_##field##_MASK) << \
130 KDETH_##field##_SHIFT); \
131 dw = cpu_to_le32(dwval); \
132 } while (0)
133
134#define AHG_HEADER_SET(arr, idx, dw, bit, width, value) \
135 do { \
136 if ((idx) < ARRAY_SIZE((arr))) \
137 (arr)[(idx++)] = sdma_build_ahg_descriptor( \
138 (__force u16)(value), (dw), (bit), \
139 (width)); \
140 else \
141 return -ERANGE; \
142 } while (0)
143
144/* KDETH OM multipliers and switch over point */
145#define KDETH_OM_SMALL 4
146#define KDETH_OM_LARGE 64
147#define KDETH_OM_MAX_SIZE (1 << ((KDETH_OM_LARGE / KDETH_OM_SMALL) + 1))
148
Jakub Pawlake7301392016-12-07 19:32:41 -0800149/* Tx request flag bits */
150#define TXREQ_FLAGS_REQ_ACK BIT(0) /* Set the ACK bit in the header */
151#define TXREQ_FLAGS_REQ_DISABLE_SH BIT(1) /* Disable header suppression */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400152
Dean Luick7b3256e2016-07-28 15:21:18 -0400153/* SDMA request flag bits */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400154#define SDMA_REQ_FOR_THREAD 1
155#define SDMA_REQ_SEND_DONE 2
156#define SDMA_REQ_HAVE_AHG 3
157#define SDMA_REQ_HAS_ERROR 4
158#define SDMA_REQ_DONE_ERROR 5
159
Sunny Kumarcb326492015-11-06 10:06:43 +0530160#define SDMA_PKT_Q_INACTIVE BIT(0)
161#define SDMA_PKT_Q_ACTIVE BIT(1)
162#define SDMA_PKT_Q_DEFERRED BIT(2)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400163
164/*
165 * Maximum retry attempts to submit a TX request
166 * before putting the process to sleep.
167 */
168#define MAX_DEFER_RETRY_COUNT 1
169
170static unsigned initial_pkt_count = 8;
171
172#define SDMA_IOWAIT_TIMEOUT 1000 /* in milliseconds */
173
Mitko Haralanov9565c6a2016-05-19 05:21:18 -0700174struct sdma_mmu_node;
175
Mike Marciniszyn77241052015-07-30 15:17:43 -0400176struct user_sdma_iovec {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800177 struct list_head list;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400178 struct iovec iov;
179 /* number of pages in this vector */
180 unsigned npages;
181 /* array of pinned pages for this vector */
182 struct page **pages;
Jubin John4d114fd2016-02-14 20:21:43 -0800183 /*
184 * offset into the virtual address space of the vector at
185 * which we last left off.
186 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400187 u64 offset;
Mitko Haralanov9565c6a2016-05-19 05:21:18 -0700188 struct sdma_mmu_node *node;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400189};
190
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800191struct sdma_mmu_node {
192 struct mmu_rb_node rb;
Mitko Haralanov5511d782016-03-08 11:15:44 -0800193 struct hfi1_user_sdma_pkt_q *pq;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800194 atomic_t refcount;
195 struct page **pages;
196 unsigned npages;
Dean Luickb7df1922016-07-28 15:21:23 -0400197};
198
199/* evict operation argument */
200struct evict_data {
201 u32 cleared; /* count evicted so far */
202 u32 target; /* target count to evict */
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800203};
204
Mike Marciniszyn77241052015-07-30 15:17:43 -0400205struct user_sdma_request {
206 struct sdma_req_info info;
207 struct hfi1_user_sdma_pkt_q *pq;
208 struct hfi1_user_sdma_comp_q *cq;
209 /* This is the original header from user space */
210 struct hfi1_pkt_header hdr;
211 /*
212 * Pointer to the SDMA engine for this request.
213 * Since different request could be on different VLs,
214 * each request will need it's own engine pointer.
215 */
216 struct sdma_engine *sde;
217 u8 ahg_idx;
218 u32 ahg[9];
219 /*
220 * KDETH.Offset (Eager) field
221 * We need to remember the initial value so the headers
222 * can be updated properly.
223 */
224 u32 koffset;
225 /*
226 * KDETH.OFFSET (TID) field
227 * The offset can cover multiple packets, depending on the
228 * size of the TID entry.
229 */
230 u32 tidoffset;
231 /*
232 * KDETH.OM
233 * Remember this because the header template always sets it
234 * to 0.
235 */
236 u8 omfactor;
237 /*
Mike Marciniszyn77241052015-07-30 15:17:43 -0400238 * We copy the iovs for this request (based on
239 * info.iovcnt). These are only the data vectors
240 */
241 unsigned data_iovs;
242 /* total length of the data in the request */
243 u32 data_len;
244 /* progress index moving along the iovs array */
245 unsigned iov_idx;
246 struct user_sdma_iovec iovs[MAX_VECTORS_PER_REQ];
247 /* number of elements copied to the tids array */
248 u16 n_tids;
249 /* TID array values copied from the tid_iov vector */
250 u32 *tids;
251 u16 tididx;
252 u32 sent;
253 u64 seqnum;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800254 u64 seqcomp;
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -0800255 u64 seqsubmitted;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400256 struct list_head txps;
257 unsigned long flags;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500258 /* status of the last txreq completed */
259 int status;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400260};
261
Mitko Haralanovb9fb63182015-10-26 10:28:37 -0400262/*
263 * A single txreq could span up to 3 physical pages when the MTU
264 * is sufficiently large (> 4K). Each of the IOV pointers also
265 * needs it's own set of flags so the vector has been handled
266 * independently of each other.
267 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400268struct user_sdma_txreq {
269 /* Packet header for the txreq */
270 struct hfi1_pkt_header hdr;
271 struct sdma_txreq txreq;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500272 struct list_head list;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400273 struct user_sdma_request *req;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400274 u16 flags;
275 unsigned busycount;
276 u64 seqnum;
277};
278
279#define SDMA_DBG(req, fmt, ...) \
280 hfi1_cdbg(SDMA, "[%u:%u:%u:%u] " fmt, (req)->pq->dd->unit, \
281 (req)->pq->ctxt, (req)->pq->subctxt, (req)->info.comp_idx, \
282 ##__VA_ARGS__)
283#define SDMA_Q_DBG(pq, fmt, ...) \
284 hfi1_cdbg(SDMA, "[%u:%u:%u] " fmt, (pq)->dd->unit, (pq)->ctxt, \
285 (pq)->subctxt, ##__VA_ARGS__)
286
287static int user_sdma_send_pkts(struct user_sdma_request *, unsigned);
288static int num_user_pages(const struct iovec *);
Mike Marciniszyna545f532016-02-14 12:45:53 -0800289static void user_sdma_txreq_cb(struct sdma_txreq *, int);
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800290static inline void pq_update(struct hfi1_user_sdma_pkt_q *);
291static void user_sdma_free_request(struct user_sdma_request *, bool);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400292static int pin_vector_pages(struct user_sdma_request *,
293 struct user_sdma_iovec *);
Mitko Haralanov849e3e92016-04-12 10:46:16 -0700294static void unpin_vector_pages(struct mm_struct *, struct page **, unsigned,
295 unsigned);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400296static int check_header_template(struct user_sdma_request *,
297 struct hfi1_pkt_header *, u32, u32);
298static int set_txreq_header(struct user_sdma_request *,
299 struct user_sdma_txreq *, u32);
300static int set_txreq_header_ahg(struct user_sdma_request *,
301 struct user_sdma_txreq *, u32);
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800302static inline void set_comp_state(struct hfi1_user_sdma_pkt_q *,
303 struct hfi1_user_sdma_comp_q *,
304 u16, enum hfi1_sdma_comp_state, int);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400305static inline u32 set_pkt_bth_psn(__be32, u8, u32);
306static inline u32 get_lrh_len(struct hfi1_pkt_header, u32 len);
307
308static int defer_packet_queue(
309 struct sdma_engine *,
310 struct iowait *,
311 struct sdma_txreq *,
312 unsigned seq);
313static void activate_packet_queue(struct iowait *, int);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800314static bool sdma_rb_filter(struct mmu_rb_node *, unsigned long, unsigned long);
Dean Luicke0b09ac2016-07-28 15:21:20 -0400315static int sdma_rb_insert(void *, struct mmu_rb_node *);
Dean Luickb7df1922016-07-28 15:21:23 -0400316static int sdma_rb_evict(void *arg, struct mmu_rb_node *mnode,
317 void *arg2, bool *stop);
Dean Luick082b3532016-07-28 15:21:25 -0400318static void sdma_rb_remove(void *, struct mmu_rb_node *);
Dean Luicke0b09ac2016-07-28 15:21:20 -0400319static int sdma_rb_invalidate(void *, struct mmu_rb_node *);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800320
321static struct mmu_rb_ops sdma_rb_ops = {
322 .filter = sdma_rb_filter,
323 .insert = sdma_rb_insert,
Dean Luickb7df1922016-07-28 15:21:23 -0400324 .evict = sdma_rb_evict,
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800325 .remove = sdma_rb_remove,
326 .invalidate = sdma_rb_invalidate
327};
Mike Marciniszyn77241052015-07-30 15:17:43 -0400328
Mike Marciniszyn77241052015-07-30 15:17:43 -0400329static int defer_packet_queue(
330 struct sdma_engine *sde,
331 struct iowait *wait,
332 struct sdma_txreq *txreq,
333 unsigned seq)
334{
335 struct hfi1_user_sdma_pkt_q *pq =
336 container_of(wait, struct hfi1_user_sdma_pkt_q, busy);
337 struct hfi1_ibdev *dev = &pq->dd->verbs_dev;
338 struct user_sdma_txreq *tx =
339 container_of(txreq, struct user_sdma_txreq, txreq);
340
341 if (sdma_progress(sde, seq, txreq)) {
342 if (tx->busycount++ < MAX_DEFER_RETRY_COUNT)
343 goto eagain;
344 }
345 /*
346 * We are assuming that if the list is enqueued somewhere, it
347 * is to the dmawait list since that is the only place where
348 * it is supposed to be enqueued.
349 */
350 xchg(&pq->state, SDMA_PKT_Q_DEFERRED);
351 write_seqlock(&dev->iowait_lock);
352 if (list_empty(&pq->busy.list))
353 list_add_tail(&pq->busy.list, &sde->dmawait);
354 write_sequnlock(&dev->iowait_lock);
355 return -EBUSY;
356eagain:
357 return -EAGAIN;
358}
359
360static void activate_packet_queue(struct iowait *wait, int reason)
361{
362 struct hfi1_user_sdma_pkt_q *pq =
363 container_of(wait, struct hfi1_user_sdma_pkt_q, busy);
364 xchg(&pq->state, SDMA_PKT_Q_ACTIVE);
365 wake_up(&wait->wait_dma);
366};
367
368static void sdma_kmem_cache_ctor(void *obj)
369{
Janani Ravichandran16ccad02016-02-25 15:08:17 -0500370 struct user_sdma_txreq *tx = obj;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400371
372 memset(tx, 0, sizeof(*tx));
373}
374
375int hfi1_user_sdma_alloc_queues(struct hfi1_ctxtdata *uctxt, struct file *fp)
376{
Ira Weiny9e10af42015-10-30 18:58:40 -0400377 struct hfi1_filedata *fd;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400378 int ret = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400379 char buf[64];
380 struct hfi1_devdata *dd;
381 struct hfi1_user_sdma_comp_q *cq;
382 struct hfi1_user_sdma_pkt_q *pq;
383 unsigned long flags;
384
385 if (!uctxt || !fp) {
386 ret = -EBADF;
387 goto done;
388 }
389
Ira Weiny9e10af42015-10-30 18:58:40 -0400390 fd = fp->private_data;
391
Mike Marciniszyn77241052015-07-30 15:17:43 -0400392 if (!hfi1_sdma_comp_ring_size) {
393 ret = -EINVAL;
394 goto done;
395 }
396
397 dd = uctxt->dd;
398
399 pq = kzalloc(sizeof(*pq), GFP_KERNEL);
Alison Schofield806e6e12015-10-12 14:28:36 -0700400 if (!pq)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400401 goto pq_nomem;
Alison Schofield806e6e12015-10-12 14:28:36 -0700402
Markus Elfring147d84e2017-02-09 16:06:12 +0100403 pq->reqs = kcalloc(hfi1_sdma_comp_ring_size,
404 sizeof(*pq->reqs),
405 GFP_KERNEL);
Alison Schofield806e6e12015-10-12 14:28:36 -0700406 if (!pq->reqs)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400407 goto pq_reqs_nomem;
Alison Schofield806e6e12015-10-12 14:28:36 -0700408
Markus Elfring147d84e2017-02-09 16:06:12 +0100409 pq->req_in_use = kcalloc(BITS_TO_LONGS(hfi1_sdma_comp_ring_size),
410 sizeof(*pq->req_in_use),
411 GFP_KERNEL);
Dean Luick7b3256e2016-07-28 15:21:18 -0400412 if (!pq->req_in_use)
413 goto pq_reqs_no_in_use;
414
Mike Marciniszyn77241052015-07-30 15:17:43 -0400415 INIT_LIST_HEAD(&pq->list);
416 pq->dd = dd;
417 pq->ctxt = uctxt->ctxt;
Ira Weiny9e10af42015-10-30 18:58:40 -0400418 pq->subctxt = fd->subctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400419 pq->n_max_reqs = hfi1_sdma_comp_ring_size;
420 pq->state = SDMA_PKT_Q_INACTIVE;
421 atomic_set(&pq->n_reqs, 0);
Mitko Haralanova0d40692015-12-08 17:10:13 -0500422 init_waitqueue_head(&pq->wait);
Dean Luickb7df1922016-07-28 15:21:23 -0400423 atomic_set(&pq->n_locked, 0);
Ira Weiny3faa3d92016-07-28 15:21:19 -0400424 pq->mm = fd->mm;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400425
426 iowait_init(&pq->busy, 0, NULL, defer_packet_queue,
Mike Marciniszyna545f532016-02-14 12:45:53 -0800427 activate_packet_queue, NULL);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400428 pq->reqidx = 0;
429 snprintf(buf, 64, "txreq-kmem-cache-%u-%u-%u", dd->unit, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -0400430 fd->subctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400431 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 }
Ira Weiny9e10af42015-10-30 18:58:40 -0400441 fd->pq = pq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400442 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
Alison Schofield806e6e12015-10-12 14:28:36 -0700443 if (!cq)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400444 goto cq_nomem;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400445
Markus Elfringe036c202017-02-10 08:50:45 +0100446 cq->comps = vmalloc_user(PAGE_ALIGN(sizeof(*cq->comps)
447 * hfi1_sdma_comp_ring_size));
Alison Schofield806e6e12015-10-12 14:28:36 -0700448 if (!cq->comps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400449 goto cq_comps_nomem;
Alison Schofield806e6e12015-10-12 14:28:36 -0700450
Mike Marciniszyn77241052015-07-30 15:17:43 -0400451 cq->nentries = hfi1_sdma_comp_ring_size;
Ira Weiny9e10af42015-10-30 18:58:40 -0400452 fd->cq = cq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400453
Dean Luickb85ced92016-07-28 15:21:24 -0400454 ret = hfi1_mmu_rb_register(pq, pq->mm, &sdma_rb_ops, dd->pport->hfi1_wq,
455 &pq->handler);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800456 if (ret) {
457 dd_dev_err(dd, "Failed to register with MMU %d", ret);
458 goto done;
459 }
460
Mike Marciniszyn77241052015-07-30 15:17:43 -0400461 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:
Dean Luick7b3256e2016-07-28 15:21:18 -0400471 kfree(pq->req_in_use);
472pq_reqs_no_in_use:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400473 kfree(pq->reqs);
474pq_reqs_nomem:
475 kfree(pq);
Ira Weiny9e10af42015-10-30 18:58:40 -0400476 fd->pq = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400477pq_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) {
Dean Luicke0b09ac2016-07-28 15:21:20 -0400493 if (pq->handler)
494 hfi1_mmu_rb_unregister(pq->handler);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400495 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);
Mitko Haralanova0d40692015-12-08 17:10:13 -0500500 /* Wait until all requests have been freed. */
501 wait_event_interruptible(
502 pq->wait,
503 (ACCESS_ONCE(pq->state) == SDMA_PKT_Q_INACTIVE));
504 kfree(pq->reqs);
Dean Luick7b3256e2016-07-28 15:21:18 -0400505 kfree(pq->req_in_use);
Julia Lawalladad44d2015-09-13 14:15:04 +0200506 kmem_cache_destroy(pq->txreq_cache);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400507 kfree(pq);
508 fd->pq = NULL;
509 }
510 if (fd->cq) {
Bhumika Goyala4d7d052016-02-14 20:34:28 +0530511 vfree(fd->cq->comps);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400512 kfree(fd->cq);
513 fd->cq = NULL;
514 }
515 return 0;
516}
517
Jianxin Xiong14833b82016-07-01 16:01:56 -0700518static 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
Mike Marciniszyn77241052015-07-30 15:17:43 -0400539int hfi1_user_sdma_process_request(struct file *fp, struct iovec *iovec,
540 unsigned long dim, unsigned long *count)
541{
Dean Luickff4ce9b2016-07-28 12:27:34 -0400542 int ret = 0, i;
Ira Weiny9e10af42015-10-30 18:58:40 -0400543 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;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400547 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;
Jianxin Xiongb583faf2016-05-19 05:21:57 -0700553 int req_queued = 0;
Jianxin Xiong14833b82016-07-01 16:01:56 -0700554 u16 dlid;
Tadeusz Struk0cb2aa62016-09-25 07:44:23 -0700555 u32 selector;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400556
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",
Ira Weiny9e10af42015-10-30 18:58:40 -0400561 dd->unit, uctxt->ctxt, fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400562 iovec[idx].iov_len, sizeof(info) + sizeof(req->hdr));
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500563 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400564 }
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)",
Ira Weiny9e10af42015-10-30 18:58:40 -0400568 dd->unit, uctxt->ctxt, fd->subctxt, ret);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500569 return -EFAULT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400570 }
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800571
Ira Weiny9e10af42015-10-30 18:58:40 -0400572 trace_hfi1_sdma_user_reqinfo(dd, uctxt->ctxt, fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400573 (u16 *)&info);
Dean Luick4fa0d222016-07-28 15:21:14 -0400574
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
Dean Luick9ff73c82016-07-28 15:21:15 -0400582 /*
583 * Sanity check the header io vector count. Need at least 1 vector
584 * (header) and cannot be larger than the actual io vector count.
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
Mike Marciniszyn77241052015-07-30 15:17:43 -0400594 if (!info.fragsize) {
595 hfi1_cdbg(SDMA,
596 "[%u:%u:%u:%u] Request does not specify fragsize",
Ira Weiny9e10af42015-10-30 18:58:40 -0400597 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500598 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400599 }
Dean Luick7b3256e2016-07-28 15:21:18 -0400600
601 /* Try to claim the request. */
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 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400608 /*
Dean Luick7b3256e2016-07-28 15:21:18 -0400609 * All safety checks have been done and this request has been claimed.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400610 */
611 hfi1_cdbg(SDMA, "[%u:%u:%u] Using req/comp entry %u\n", dd->unit,
Ira Weiny9e10af42015-10-30 18:58:40 -0400612 uctxt->ctxt, fd->subctxt, info.comp_idx);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400613 req = pq->reqs + info.comp_idx;
614 memset(req, 0, sizeof(*req));
Dean Luick9ff73c82016-07-28 15:21:15 -0400615 req->data_iovs = req_iovcnt(info.ctrl) - 1; /* subtract header vector */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400616 req->pq = pq;
617 req->cq = cq;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500618 req->status = -1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400619 INIT_LIST_HEAD(&req->txps);
Mitko Haralanova0d40692015-12-08 17:10:13 -0500620
Mike Marciniszyn77241052015-07-30 15:17:43 -0400621 memcpy(&req->info, &info, sizeof(info));
622
Dean Luick9ff73c82016-07-28 15:21:15 -0400623 if (req_opcode(info.ctrl) == EXPECTED) {
624 /* expected must have a TID info and at least one data vector */
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 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400631 req->data_iovs--;
Dean Luick9ff73c82016-07-28 15:21:15 -0400632 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400633
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);
Dean Luick9da7e9a2016-07-28 15:21:17 -0400637 ret = -EINVAL;
638 goto free_req;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400639 }
640 /* Copy the header from the user buffer */
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 /* If Static rate control is not enabled, sanitize the header. */
650 if (!HFI1_CAP_IS_USET(STATIC_RATE_CTRL))
651 req->hdr.pbc[2] = 0;
652
653 /* Validate the opcode. Do not trust packets from user space blindly. */
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 * Validate the vl. Do not trust packets from user space blindly.
663 * VL comes from PBC, SC comes from LRH, and the VL needs to
664 * match the SC look up.
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
Sebastian Sancheze38d1e42016-04-12 11:22:21 -0700676 /* Checking P_KEY for requests from user-space */
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
Mike Marciniszyn77241052015-07-30 15:17:43 -0400683 /*
684 * Also should check the BTH.lnh. If it says the next header is GRH then
685 * the RXE parsing will be off and will land in the middle of the KDETH
686 * or miss it entirely.
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]);
Jubin John4d114fd2016-02-14 20:21:43 -0800695 /*
696 * Calculate the initial TID offset based on the values of
697 * KDETH.OFFSET and KDETH.OM that are passed in.
698 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400699 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 /* Save all the IO vector structures */
Dean Luickff4ce9b2016-07-28 12:27:34 -0400706 for (i = 0; i < req->data_iovs; i++) {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800707 INIT_LIST_HEAD(&req->iovs[i].list);
Markus Elfringdb6f0282017-02-10 21:45:38 +0100708 memcpy(&req->iovs[i].iov,
709 iovec + idx++,
710 sizeof(req->iovs[i].iov));
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800711 ret = pin_vector_pages(req, &req->iovs[i]);
712 if (ret) {
713 req->status = ret;
714 goto free_req;
715 }
Dean Luickff4ce9b2016-07-28 12:27:34 -0400716 req->data_len += req->iovs[i].iov.iov_len;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400717 }
718 SDMA_DBG(req, "total data length %u", req->data_len);
719
720 if (pcount > req->info.npkts)
721 pcount = req->info.npkts;
722 /*
723 * Copy any TID info
724 * User space will provide the TID info only when the
725 * request type is EXPECTED. This is true even if there is
726 * only one packet in the request and the header is already
727 * setup. The reason for the singular TID case is that the
728 * driver needs to perform safety checks.
729 */
730 if (req_opcode(req->info.ctrl) == EXPECTED) {
731 u16 ntids = iovec[idx].iov_len / sizeof(*req->tids);
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800732 u32 *tmp;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400733
734 if (!ntids || ntids > MAX_TID_PAIR_ENTRIES) {
735 ret = -EINVAL;
736 goto free_req;
737 }
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800738
Mike Marciniszyn77241052015-07-30 15:17:43 -0400739 /*
740 * We have to copy all of the tids because they may vary
741 * in size and, therefore, the TID count might not be
742 * equal to the pkt count. However, there is no way to
743 * tell at this point.
744 */
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800745 tmp = memdup_user(iovec[idx].iov_base,
746 ntids * sizeof(*req->tids));
747 if (IS_ERR(tmp)) {
748 ret = PTR_ERR(tmp);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400749 SDMA_DBG(req, "Failed to copy %d TIDs (%d)",
750 ntids, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400751 goto free_req;
752 }
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800753 req->tids = tmp;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400754 req->n_tids = ntids;
755 idx++;
756 }
757
Jianxin Xiong14833b82016-07-01 16:01:56 -0700758 dlid = be16_to_cpu(req->hdr.lrh[1]);
759 selector = dlid_to_selector(dlid);
Tadeusz Struk0cb2aa62016-09-25 07:44:23 -0700760 selector += uctxt->ctxt + fd->subctxt;
761 req->sde = sdma_select_user_engine(dd, selector, vl);
Jianxin Xiong14833b82016-07-01 16:01:56 -0700762
Mike Marciniszyn77241052015-07-30 15:17:43 -0400763 if (!req->sde || !sdma_running(req->sde)) {
764 ret = -ECOMM;
765 goto free_req;
766 }
767
768 /* We don't need an AHG entry if the request contains only one packet */
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
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800778 set_comp_state(pq, cq, info.comp_idx, QUEUED, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400779 atomic_inc(&pq->n_reqs);
Jianxin Xiongb583faf2016-05-19 05:21:57 -0700780 req_queued = 1;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800781 /* Send the first N packets in the request to buy us some time */
782 ret = user_sdma_send_pkts(req, pcount);
783 if (unlikely(ret < 0 && ret != -EBUSY)) {
784 req->status = ret;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800785 goto free_req;
786 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400787
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800788 /*
789 * It is possible that the SDMA engine would have processed all the
790 * submitted packets by the time we get here. Therefore, only set
791 * packet queue state to ACTIVE if there are still uncompleted
792 * requests.
793 */
794 if (atomic_read(&pq->n_reqs))
795 xchg(&pq->state, SDMA_PKT_Q_ACTIVE);
796
797 /*
798 * This is a somewhat blocking send implementation.
799 * The driver will block the caller until all packets of the
800 * request have been submitted to the SDMA engine. However, it
801 * will not wait for send completions.
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);
Mitko Haralanova402d6a2016-02-03 14:37:41 -0800809 if (ACCESS_ONCE(req->seqcomp) ==
810 req->seqsubmitted - 1)
811 goto free_req;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800812 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400813 }
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800814 wait_event_interruptible_timeout(
815 pq->busy.wait_dma,
816 (pq->state == SDMA_PKT_Q_ACTIVE),
817 msecs_to_jiffies(
818 SDMA_IOWAIT_TIMEOUT));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400819 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400820 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400821 *count += idx;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500822 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400823free_req:
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800824 user_sdma_free_request(req, true);
Jianxin Xiongb583faf2016-05-19 05:21:57 -0700825 if (req_queued)
826 pq_update(pq);
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800827 set_comp_state(pq, cq, info.comp_idx, ERROR, req->status);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400828 return ret;
829}
830
831static inline u32 compute_data_length(struct user_sdma_request *req,
Jubin John17fb4f22016-02-14 20:21:52 -0800832 struct user_sdma_txreq *tx)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400833{
834 /*
835 * Determine the proper size of the packet data.
836 * The size of the data of the first packet is in the header
837 * template. However, it includes the header and ICRC, which need
838 * to be subtracted.
Ira Weinyc4929802016-07-27 21:08:42 -0400839 * The minimum representable packet data length in a header is 4 bytes,
840 * therefore, when the data length request is less than 4 bytes, there's
841 * only one packet, and the packet data length is equal to that of the
842 * request data length.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400843 * The size of the remaining packets is the minimum of the frag
844 * size (MTU) or remaining data in the request.
845 */
846 u32 len;
847
848 if (!req->seqnum) {
Ira Weinyc4929802016-07-27 21:08:42 -0400849 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));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400854 } else if (req_opcode(req->info.ctrl) == EXPECTED) {
855 u32 tidlen = EXP_TID_GET(req->tids[req->tididx], LEN) *
856 PAGE_SIZE;
Jubin John4d114fd2016-02-14 20:21:43 -0800857 /*
858 * Get the data length based on the remaining space in the
859 * TID pair.
860 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400861 len = min(tidlen - req->tidoffset, (u32)req->info.fragsize);
862 /* If we've filled up the TID pair, move to the next one. */
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 }
Jubin John4d114fd2016-02-14 20:21:43 -0800870 /*
871 * Since the TID pairs map entire pages, make sure that we
Mike Marciniszyn77241052015-07-30 15:17:43 -0400872 * are not going to try to send more data that we have
Jubin John4d114fd2016-02-14 20:21:43 -0800873 * remaining.
874 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400875 len = min(len, req->data_len - req->sent);
Jubin Johne4909742016-02-14 20:22:00 -0800876 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400877 len = min(req->data_len - req->sent, (u32)req->info.fragsize);
Jubin Johne4909742016-02-14 20:22:00 -0800878 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400879 SDMA_DBG(req, "Data Length = %u", len);
880 return len;
881}
882
Ira Weinyc4929802016-07-27 21:08:42 -0400883static 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
Mike Marciniszyn77241052015-07-30 15:17:43 -0400890static inline u32 get_lrh_len(struct hfi1_pkt_header hdr, u32 len)
891{
892 /* (Size of complete header - size of PBC) + 4B ICRC + data length */
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{
Harish Chegondi0b115ef2016-09-06 04:35:37 -0700898 int ret = 0, count;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400899 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
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500904 if (!req->pq)
905 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400906
907 pq = req->pq;
908
Mitko Haralanov6a5464f2015-12-08 17:10:12 -0500909 /* If tx completion has reported an error, we are done. */
910 if (test_bit(SDMA_REQ_HAS_ERROR, &req->flags)) {
911 set_bit(SDMA_REQ_DONE_ERROR, &req->flags);
912 return -EFAULT;
913 }
914
Mike Marciniszyn77241052015-07-30 15:17:43 -0400915 /*
916 * Check if we might have sent the entire request already
917 */
918 if (unlikely(req->seqnum == req->info.npkts)) {
919 if (!list_empty(&req->txps))
920 goto dosend;
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500921 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400922 }
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 * Check whether any of the completions have come back
933 * with errors. If so, we are not going to process any
934 * more packets from this request.
935 */
936 if (test_bit(SDMA_REQ_HAS_ERROR, &req->flags)) {
937 set_bit(SDMA_REQ_DONE_ERROR, &req->flags);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500938 return -EFAULT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400939 }
940
941 tx = kmem_cache_alloc(pq->txreq_cache, GFP_KERNEL);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500942 if (!tx)
943 return -ENOMEM;
944
Mike Marciniszyn77241052015-07-30 15:17:43 -0400945 tx->flags = 0;
946 tx->req = req;
947 tx->busycount = 0;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500948 INIT_LIST_HEAD(&tx->list);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400949
Jakub Pawlake7301392016-12-07 19:32:41 -0800950 /*
951 * For the last packet set the ACK request
952 * and disable header suppression.
953 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400954 if (req->seqnum == req->info.npkts - 1)
Jakub Pawlake7301392016-12-07 19:32:41 -0800955 tx->flags |= (TXREQ_FLAGS_REQ_ACK |
956 TXREQ_FLAGS_REQ_DISABLE_SH);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400957
958 /*
959 * Calculate the payload size - this is min of the fragment
960 * (MTU) size or the remaining bytes in the request but only
961 * if we have payload data.
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
Mike Marciniszyn77241052015-07-30 15:17:43 -0400974 datalen = compute_data_length(req, tx);
Jakub Pawlake7301392016-12-07 19:32:41 -0800975
976 /*
977 * Disable header suppression for the payload <= 8DWS.
978 * If there is an uncorrectable error in the receive
979 * data FIFO when the received payload size is less than
980 * or equal to 8DWS then the RxDmaDataFifoRdUncErr is
981 * not reported.There is set RHF.EccErr if the header
982 * is not suppressed.
983 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400984 if (!datalen) {
985 SDMA_DBG(req,
986 "Request has data but pkt len is 0");
987 ret = -EFAULT;
988 goto free_tx;
Jakub Pawlake7301392016-12-07 19:32:41 -0800989 } else if (datalen <= 32) {
990 tx->flags |= TXREQ_FLAGS_REQ_DISABLE_SH;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400991 }
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]);
Ira Weinyc4929802016-07-27 21:08:42 -0400997 u32 lrhlen = get_lrh_len(req->hdr,
998 pad_len(datalen));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400999 /*
1000 * Copy the request header into the tx header
1001 * because the HW needs a cacheline-aligned
1002 * address.
1003 * This copy can be optimized out if the hdr
1004 * member of user_sdma_request were also
1005 * cacheline aligned.
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 }
Jakub Pawlake7301392016-12-07 19:32:41 -08001013 ret = check_header_template(req, &tx->hdr,
1014 lrhlen, datalen);
1015 if (ret)
1016 goto free_tx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001017 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 * Modify the header for this packet. This only needs
1049 * to be done if we are not going to use AHG. Otherwise,
1050 * the HW will do it based on the changes we gave it
1051 * during sdma_txinit_ahg().
1052 */
1053 ret = set_txreq_header(req, tx, datalen);
1054 if (ret)
1055 goto free_txreq;
1056 }
1057
1058 /*
1059 * If the request contains any data vectors, add up to
1060 * fragsize bytes to the descriptor.
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;
Amitoj Kaur Chawla72a5f6a2016-02-20 19:08:02 +05301068 offset = offset_in_page(base + iovec->offset +
1069 iov_offset);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001070 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) {
Mitko Haralanova0d40692015-12-08 17:10:13 -05001079 SDMA_DBG(req, "SDMA txreq add page failed %d\n",
1080 ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001081 goto free_txreq;
1082 }
1083 iov_offset += len;
1084 queued += len;
1085 data_sent += len;
1086 if (unlikely(queued < datalen &&
1087 pageidx == iovec->npages &&
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001088 req->iov_idx < req->data_iovs - 1)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001089 iovec->offset += iov_offset;
1090 iovec = &req->iovs[++req->iov_idx];
Mike Marciniszyn77241052015-07-30 15:17:43 -04001091 iov_offset = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001092 }
1093 }
1094 /*
1095 * The txreq was submitted successfully so we can update
1096 * the counters.
1097 */
1098 req->koffset += datalen;
1099 if (req_opcode(req->info.ctrl) == EXPECTED)
1100 req->tidoffset += datalen;
1101 req->sent += data_sent;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001102 if (req->data_len)
1103 iovec->offset += iov_offset;
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001104 list_add_tail(&tx->txreq.list, &req->txps);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001105 /*
1106 * It is important to increment this here as it is used to
1107 * generate the BTH.PSN and, therefore, can't be bulk-updated
1108 * outside of the loop.
1109 */
1110 tx->seqnum = req->seqnum++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001111 npkts++;
1112 }
1113dosend:
Harish Chegondi0b115ef2016-09-06 04:35:37 -07001114 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 * The txreq has already been submitted to the HW queue
1120 * so we can free the AHG entry now. Corruption will not
1121 * happen due to the sequential manner in which
1122 * descriptors are processed.
1123 */
1124 if (test_bit(SDMA_REQ_HAVE_AHG, &req->flags))
1125 sdma_ahg_free(req->sde, req->ahg_idx);
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001126 }
Mitko Haralanovfaa98b82015-12-08 17:10:11 -05001127 return ret;
1128
Mike Marciniszyn77241052015-07-30 15:17:43 -04001129free_txreq:
1130 sdma_txclean(pq->dd, &tx->txreq);
1131free_tx:
1132 kmem_cache_free(pq->txreq_cache, tx);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001133 return ret;
1134}
1135
1136/*
1137 * How many pages in this iovec element?
1138 */
1139static inline int num_user_pages(const struct iovec *iov)
1140{
Jubin John50e5dcb2016-02-14 20:19:41 -08001141 const unsigned long addr = (unsigned long)iov->iov_base;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001142 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
Mitko Haralanov5511d782016-03-08 11:15:44 -08001149static u32 sdma_cache_evict(struct hfi1_user_sdma_pkt_q *pq, u32 npages)
1150{
Dean Luickb7df1922016-07-28 15:21:23 -04001151 struct evict_data evict_data;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001152
Dean Luickb7df1922016-07-28 15:21:23 -04001153 evict_data.cleared = 0;
1154 evict_data.target = npages;
1155 hfi1_mmu_rb_evict(pq->handler, &evict_data);
1156 return evict_data.cleared;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001157}
1158
Mike Marciniszyn77241052015-07-30 15:17:43 -04001159static int pin_vector_pages(struct user_sdma_request *req,
Ira Weiny72720dd2016-07-28 12:27:25 -04001160 struct user_sdma_iovec *iovec)
1161{
Mitko Haralanov5511d782016-03-08 11:15:44 -08001162 int ret = 0, pinned, npages, cleared;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001163 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;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001167
Dean Luicke0b09ac2016-07-28 15:21:20 -04001168 rb_node = hfi1_mmu_rb_extract(pq->handler,
Mitko Haralanovf53af852016-04-12 10:46:47 -07001169 (unsigned long)iovec->iov.iov_base,
1170 iovec->iov.iov_len);
Dennis Dalessandro2b160562016-10-25 13:12:46 -07001171 if (rb_node)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001172 node = container_of(rb_node, struct sdma_mmu_node, rb);
Mitko Haralanovf19bd642016-04-12 10:45:57 -07001173 else
1174 rb_node = NULL;
Mitko Haralanova0d40692015-12-08 17:10:13 -05001175
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001176 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;
Mitko Haralanov5511d782016-03-08 11:15:44 -08001182 node->pq = pq;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001183 atomic_set(&node->refcount, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001184 }
Mitko Haralanova0d40692015-12-08 17:10:13 -05001185
Mike Marciniszyn77241052015-07-30 15:17:43 -04001186 npages = num_user_pages(&iovec->iov);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001187 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;
Mitko Haralanove88c9272016-04-12 10:46:53 -07001197
Mitko Haralanov5511d782016-03-08 11:15:44 -08001198retry:
Dean Luickb7df1922016-07-28 15:21:23 -04001199 if (!hfi1_can_pin_pages(pq->dd, pq->mm,
1200 atomic_read(&pq->n_locked), npages)) {
Mitko Haralanov5511d782016-03-08 11:15:44 -08001201 cleared = sdma_cache_evict(pq, npages);
Mitko Haralanov5511d782016-03-08 11:15:44 -08001202 if (cleared >= npages)
1203 goto retry;
1204 }
Ira Weiny3faa3d92016-07-28 15:21:19 -04001205 pinned = hfi1_acquire_user_pages(pq->mm,
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001206 ((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) {
Ira Weiny3faa3d92016-07-28 15:21:19 -04001215 unpin_vector_pages(pq->mm, pages, node->npages,
Mitko Haralanov849e3e92016-04-12 10:46:16 -07001216 pinned);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001217 ret = -EFAULT;
1218 goto bail;
1219 }
1220 kfree(node->pages);
Mitko Haralanovde790932016-04-12 10:46:41 -07001221 node->rb.len = iovec->iov.iov_len;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001222 node->pages = pages;
1223 node->npages += pinned;
1224 npages = node->npages;
Dean Luickb7df1922016-07-28 15:21:23 -04001225 atomic_add(pinned, &pq->n_locked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001226 }
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001227 iovec->pages = node->pages;
1228 iovec->npages = npages;
Mitko Haralanov9565c6a2016-05-19 05:21:18 -07001229 iovec->node = node;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001230
Dean Luicke0b09ac2016-07-28 15:21:20 -04001231 ret = hfi1_mmu_rb_insert(req->pq->handler, &node->rb);
Mitko Haralanovf53af852016-04-12 10:46:47 -07001232 if (ret) {
Dean Luickb7df1922016-07-28 15:21:23 -04001233 atomic_sub(node->npages, &pq->n_locked);
Dean Luicka383f8e2016-07-28 15:21:16 -04001234 iovec->node = NULL;
Mitko Haralanovf53af852016-04-12 10:46:47 -07001235 goto bail;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001236 }
1237 return 0;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001238bail:
Mitko Haralanovf53af852016-04-12 10:46:47 -07001239 if (rb_node)
Ira Weiny3faa3d92016-07-28 15:21:19 -04001240 unpin_vector_pages(pq->mm, node->pages, 0, node->npages);
Mitko Haralanovf53af852016-04-12 10:46:47 -07001241 kfree(node);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001242 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001243}
1244
Mitko Haralanovbd3a8942016-03-08 11:15:33 -08001245static void unpin_vector_pages(struct mm_struct *mm, struct page **pages,
Mitko Haralanov849e3e92016-04-12 10:46:16 -07001246 unsigned start, unsigned npages)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001247{
Ira Weiny639297b2016-07-28 12:27:33 -04001248 hfi1_release_user_pages(mm, pages + start, npages, false);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001249 kfree(pages);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001250}
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 * Perform safety checks for any type of packet:
1258 * - transfer size is multiple of 64bytes
Ira Weinyc4929802016-07-27 21:08:42 -04001259 * - packet length is multiple of 4 bytes
Mike Marciniszyn77241052015-07-30 15:17:43 -04001260 * - packet length is not larger than MTU size
1261 *
1262 * These checks are only done for the first packet of the
1263 * transfer since the header is "given" to us by user space.
1264 * For the remainder of the packets we compute the values.
1265 */
Ira Weinyc4929802016-07-27 21:08:42 -04001266 if (req->info.fragsize % PIO_BLOCK_SIZE || lrhlen & 0x3 ||
Mike Marciniszyn77241052015-07-30 15:17:43 -04001267 lrhlen > get_lrh_len(*hdr, req->info.fragsize))
1268 return -EINVAL;
1269
1270 if (req_opcode(req->info.ctrl) == EXPECTED) {
1271 /*
1272 * The header is checked only on the first packet. Furthermore,
1273 * we ensure that at least one TID entry is copied when the
1274 * request is submitted. Therefore, we don't have to verify that
1275 * tididx points to something sane.
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 * Expected receive packets have the following
1289 * additional checks:
1290 * - offset is not larger than the TID size
1291 * - TIDCtrl values match between header and TID array
1292 * - TID indexes match between header and TID array
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 * Correctly set the BTH.PSN field based on type of
1304 * transfer - eager packets can just increment the PSN but
1305 * expected packets encode generation and sequence in the
1306 * BTH.PSN field so just incrementing will result in errors.
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;
Ira Weinyc4929802016-07-27 21:08:42 -04001328 u32 tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(datalen));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001329
1330 /* Copy the header template to the request before modification */
1331 memcpy(hdr, &req->hdr, sizeof(*hdr));
1332
1333 /*
1334 * Check if the PBC and LRH length are mismatched. If so
1335 * adjust both in the header.
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 * Third packet
1344 * This is the first packet in the sequence that has
1345 * a "static" size that can be used for the rest of
1346 * the packets (besides the last one).
1347 */
1348 if (unlikely(req->seqnum == 2)) {
1349 /*
1350 * From this point on the lengths in both the
1351 * PBC and LRH are the same until the last
1352 * packet.
1353 * Adjust the template so we don't have to update
1354 * every packet
1355 */
1356 req->hdr.pbc[0] = hdr->pbc[0];
1357 req->hdr.lrh[2] = hdr->lrh[2];
1358 }
1359 }
1360 /*
1361 * We only have to modify the header if this is not the
1362 * first packet in the request. Otherwise, we use the
1363 * header given to us.
1364 */
1365 if (unlikely(!req->seqnum)) {
1366 ret = check_header_template(req, hdr, lrhlen, datalen);
1367 if (ret)
1368 return ret;
1369 goto done;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001370 }
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 /* Set ACK request on last packet */
Jakub Pawlake7301392016-12-07 19:32:41 -08001378 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_ACK))
Jubin John8638b772016-02-14 20:19:24 -08001379 hdr->bth[2] |= cpu_to_be32(1UL << 31);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001380
1381 /* Set the new offset */
1382 hdr->kdeth.swdata[6] = cpu_to_le32(req->koffset);
1383 /* Expected packets have to fill in the new TID information */
1384 if (req_opcode(req->info.ctrl) == EXPECTED) {
1385 tidval = req->tids[req->tididx];
1386 /*
1387 * If the offset puts us at the end of the current TID,
1388 * advance everything.
1389 */
1390 if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
1391 PAGE_SIZE)) {
1392 req->tidoffset = 0;
Jubin John4d114fd2016-02-14 20:21:43 -08001393 /*
1394 * Since we don't copy all the TIDs, all at once,
1395 * we have to check again.
1396 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001397 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 /* Set KDETH.TIDCtrl based on value for this TID. */
1406 KDETH_SET(hdr->kdeth.ver_tid_offset, TIDCTRL,
1407 EXP_TID_GET(tidval, CTRL));
1408 /* Set KDETH.TID based on value for this TID */
1409 KDETH_SET(hdr->kdeth.ver_tid_offset, TID,
1410 EXP_TID_GET(tidval, IDX));
Jakub Pawlake7301392016-12-07 19:32:41 -08001411 /* Clear KDETH.SH when DISABLE_SH flag is set */
1412 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_DISABLE_SH))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001413 KDETH_SET(hdr->kdeth.ver_tid_offset, SH, 0);
1414 /*
1415 * Set the KDETH.OFFSET and KDETH.OM based on size of
1416 * transfer.
1417 */
1418 SDMA_DBG(req, "TID offset %ubytes %uunits om%u",
1419 req->tidoffset, req->tidoffset / req->omfactor,
Bart Van Assche55c406482016-06-03 12:11:16 -07001420 req->omfactor != KDETH_OM_SMALL);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001421 KDETH_SET(hdr->kdeth.ver_tid_offset, OFFSET,
1422 req->tidoffset / req->omfactor);
1423 KDETH_SET(hdr->kdeth.ver_tid_offset, OM,
Bart Van Assche55c406482016-06-03 12:11:16 -07001424 req->omfactor != KDETH_OM_SMALL);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001425 }
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]);
Ira Weinyc4929802016-07-27 21:08:42 -04001439 u32 val32, tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(len));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001440
1441 if (PBC2LRH(pbclen) != lrhlen) {
1442 /* PBC.PbcLengthDWs */
1443 AHG_HEADER_SET(req->ahg, diff, 0, 0, 12,
1444 cpu_to_le16(LRH2PBC(lrhlen)));
1445 /* LRH.PktLen (we need the full 16 bits due to byte swap) */
1446 AHG_HEADER_SET(req->ahg, diff, 3, 0, 16,
1447 cpu_to_be16(lrhlen >> 2));
1448 }
1449
1450 /*
1451 * Do the common updates
1452 */
1453 /* BTH.PSN and BTH.A */
1454 val32 = (be32_to_cpu(hdr->bth[2]) + req->seqnum) &
1455 (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffff : 0xffffff);
Jakub Pawlake7301392016-12-07 19:32:41 -08001456 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_ACK))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001457 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 /* KDETH.Offset */
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 * If the offset puts us at the end of the current TID,
1472 * advance everything.
1473 */
1474 if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
1475 PAGE_SIZE)) {
1476 req->tidoffset = 0;
Jubin John4d114fd2016-02-14 20:21:43 -08001477 /*
1478 * Since we don't copy all the TIDs, all at once,
1479 * we have to check again.
1480 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001481 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 /* KDETH.OM and KDETH.OFFSET (TID) */
1492 AHG_HEADER_SET(req->ahg, diff, 7, 0, 16,
1493 ((!!(req->omfactor - KDETH_OM_SMALL)) << 15 |
1494 ((req->tidoffset / req->omfactor) & 0x7fff)));
Jakub Pawlake7301392016-12-07 19:32:41 -08001495 /* KDETH.TIDCtrl, KDETH.TID, KDETH.Intr, KDETH.SH */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001496 val = cpu_to_le16(((EXP_TID_GET(tidval, CTRL) & 0x3) << 10) |
Jakub Pawlake7301392016-12-07 19:32:41 -08001497 (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));
Jubin Johne4909742016-02-14 20:22:00 -08001503 } else {
Jakub Pawlake7301392016-12-07 19:32:41 -08001504 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));
Jubin Johne4909742016-02-14 20:22:00 -08001509 }
Jakub Pawlake7301392016-12-07 19:32:41 -08001510
1511 AHG_HEADER_SET(req->ahg, diff, 7, 16, 14, val);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001512 }
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
Mitko Haralanova0d40692015-12-08 17:10:13 -05001520/*
1521 * SDMA tx request completion callback. Called when the SDMA progress
1522 * state machine gets notification that the SDMA descriptors for this
1523 * tx request have been processed by the DMA engine. Called in
1524 * interrupt context.
1525 */
Mike Marciniszyna545f532016-02-14 12:45:53 -08001526static void user_sdma_txreq_cb(struct sdma_txreq *txreq, int status)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001527{
1528 struct user_sdma_txreq *tx =
1529 container_of(txreq, struct user_sdma_txreq, txreq);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001530 struct user_sdma_request *req;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001531 struct hfi1_user_sdma_pkt_q *pq;
1532 struct hfi1_user_sdma_comp_q *cq;
1533 u16 idx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001534
Mitko Haralanova0d40692015-12-08 17:10:13 -05001535 if (!tx->req)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001536 return;
1537
Mitko Haralanova0d40692015-12-08 17:10:13 -05001538 req = tx->req;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001539 pq = req->pq;
1540 cq = req->cq;
Mitko Haralanovb9fb63182015-10-26 10:28:37 -04001541
Mike Marciniszyn77241052015-07-30 15:17:43 -04001542 if (status != SDMA_TXREQ_S_OK) {
Mitko Haralanova0d40692015-12-08 17:10:13 -05001543 SDMA_DBG(req, "SDMA completion with error %d",
1544 status);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001545 set_bit(SDMA_REQ_HAS_ERROR, &req->flags);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001546 }
1547
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001548 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 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001560 } else {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001561 if (status != SDMA_TXREQ_S_OK)
1562 req->status = status;
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001563 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))) {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001566 user_sdma_free_request(req, false);
1567 pq_update(pq);
1568 set_comp_state(pq, cq, idx, ERROR, req->status);
1569 }
Mitko Haralanova0d40692015-12-08 17:10:13 -05001570 }
1571}
1572
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001573static inline void pq_update(struct hfi1_user_sdma_pkt_q *pq)
Mitko Haralanova0d40692015-12-08 17:10:13 -05001574{
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001575 if (atomic_dec_and_test(&pq->n_reqs)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001576 xchg(&pq->state, SDMA_PKT_Q_INACTIVE);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001577 wake_up(&pq->wait);
1578 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001579}
1580
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001581static void user_sdma_free_request(struct user_sdma_request *req, bool unpin)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001582{
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) {
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001595 struct sdma_mmu_node *node;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001596 int i;
1597
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001598 for (i = 0; i < req->data_iovs; i++) {
Mitko Haralanov9565c6a2016-05-19 05:21:18 -07001599 node = req->iovs[i].node;
1600 if (!node)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001601 continue;
1602
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001603 if (unpin)
Dean Luicke0b09ac2016-07-28 15:21:20 -04001604 hfi1_mmu_rb_remove(req->pq->handler,
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001605 &node->rb);
1606 else
1607 atomic_dec(&node->refcount);
1608 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001609 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001610 kfree(req->tids);
Dean Luick7b3256e2016-07-28 15:21:18 -04001611 clear_bit(req->info.comp_idx, req->pq->req_in_use);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001612}
1613
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001614static 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)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001618{
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001619 hfi1_cdbg(SDMA, "[%u:%u:%u:%u] Setting completion status %u %d",
1620 pq->dd->unit, pq->ctxt, pq->subctxt, idx, state, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001621 if (state == ERROR)
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001622 cq->comps[idx].errcode = -ret;
Michael J. Ruhl0519c522017-03-20 17:24:45 -07001623 smp_wmb(); /* make sure errcode is visible first */
1624 cq->comps[idx].status = state;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001625 trace_hfi1_sdma_user_completion(pq->dd, pq->ctxt, pq->subctxt,
1626 idx, state, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001627}
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001628
1629static bool sdma_rb_filter(struct mmu_rb_node *node, unsigned long addr,
1630 unsigned long len)
1631{
1632 return (bool)(node->addr == addr);
1633}
1634
Dean Luicke0b09ac2016-07-28 15:21:20 -04001635static int sdma_rb_insert(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001636{
1637 struct sdma_mmu_node *node =
1638 container_of(mnode, struct sdma_mmu_node, rb);
1639
1640 atomic_inc(&node->refcount);
1641 return 0;
1642}
1643
Dean Luickb7df1922016-07-28 15:21:23 -04001644/*
1645 * Return 1 to remove the node from the rb tree and call the remove op.
1646 *
1647 * Called with the rb tree lock held.
1648 */
1649static int sdma_rb_evict(void *arg, struct mmu_rb_node *mnode,
1650 void *evict_arg, bool *stop)
1651{
1652 struct sdma_mmu_node *node =
1653 container_of(mnode, struct sdma_mmu_node, rb);
1654 struct evict_data *evict_data = evict_arg;
1655
1656 /* is this node still being used? */
1657 if (atomic_read(&node->refcount))
1658 return 0; /* keep this node */
1659
1660 /* this node will be evicted, add its pages to our count */
1661 evict_data->cleared += node->npages;
1662
1663 /* have enough pages been cleared? */
1664 if (evict_data->cleared >= evict_data->target)
1665 *stop = true;
1666
1667 return 1; /* remove this node */
1668}
1669
Dean Luick082b3532016-07-28 15:21:25 -04001670static void sdma_rb_remove(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001671{
1672 struct sdma_mmu_node *node =
1673 container_of(mnode, struct sdma_mmu_node, rb);
1674
Dean Luickb7df1922016-07-28 15:21:23 -04001675 atomic_sub(node->npages, &node->pq->n_locked);
Mitko Haralanov5511d782016-03-08 11:15:44 -08001676
Dean Luickb85ced92016-07-28 15:21:24 -04001677 unpin_vector_pages(node->pq->mm, node->pages, 0, node->npages);
1678
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001679 kfree(node);
1680}
1681
Dean Luicke0b09ac2016-07-28 15:21:20 -04001682static int sdma_rb_invalidate(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001683{
1684 struct sdma_mmu_node *node =
1685 container_of(mnode, struct sdma_mmu_node, rb);
1686
1687 if (!atomic_read(&node->refcount))
1688 return 1;
1689 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001690}