blob: e88d555389f4ecb6868dda478f0dac3773a6aed6 [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>
63
64#include "hfi.h"
65#include "sdma.h"
66#include "user_sdma.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040067#include "verbs.h" /* for the headers */
68#include "common.h" /* for struct hfi1_tid_info */
69#include "trace.h"
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -080070#include "mmu_rb.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040071
72static uint hfi1_sdma_comp_ring_size = 128;
73module_param_named(sdma_comp_size, hfi1_sdma_comp_ring_size, uint, S_IRUGO);
74MODULE_PARM_DESC(sdma_comp_size, "Size of User SDMA completion ring. Default: 128");
75
76/* The maximum number of Data io vectors per message/request */
77#define MAX_VECTORS_PER_REQ 8
78/*
79 * Maximum number of packet to send from each message/request
80 * before moving to the next one.
81 */
82#define MAX_PKTS_PER_QUEUE 16
83
84#define num_pages(x) (1 + ((((x) - 1) & PAGE_MASK) >> PAGE_SHIFT))
85
86#define req_opcode(x) \
87 (((x) >> HFI1_SDMA_REQ_OPCODE_SHIFT) & HFI1_SDMA_REQ_OPCODE_MASK)
88#define req_version(x) \
89 (((x) >> HFI1_SDMA_REQ_VERSION_SHIFT) & HFI1_SDMA_REQ_OPCODE_MASK)
90#define req_iovcnt(x) \
91 (((x) >> HFI1_SDMA_REQ_IOVCNT_SHIFT) & HFI1_SDMA_REQ_IOVCNT_MASK)
92
93/* Number of BTH.PSN bits used for sequence number in expected rcvs */
94#define BTH_SEQ_MASK 0x7ffull
95
96/*
97 * Define fields in the KDETH header so we can update the header
98 * template.
99 */
100#define KDETH_OFFSET_SHIFT 0
101#define KDETH_OFFSET_MASK 0x7fff
102#define KDETH_OM_SHIFT 15
103#define KDETH_OM_MASK 0x1
104#define KDETH_TID_SHIFT 16
105#define KDETH_TID_MASK 0x3ff
106#define KDETH_TIDCTRL_SHIFT 26
107#define KDETH_TIDCTRL_MASK 0x3
108#define KDETH_INTR_SHIFT 28
109#define KDETH_INTR_MASK 0x1
110#define KDETH_SH_SHIFT 29
111#define KDETH_SH_MASK 0x1
112#define KDETH_HCRC_UPPER_SHIFT 16
113#define KDETH_HCRC_UPPER_MASK 0xff
114#define KDETH_HCRC_LOWER_SHIFT 24
115#define KDETH_HCRC_LOWER_MASK 0xff
116
117#define PBC2LRH(x) ((((x) & 0xfff) << 2) - 4)
118#define LRH2PBC(x) ((((x) >> 2) + 1) & 0xfff)
119
120#define KDETH_GET(val, field) \
121 (((le32_to_cpu((val))) >> KDETH_##field##_SHIFT) & KDETH_##field##_MASK)
122#define KDETH_SET(dw, field, val) do { \
123 u32 dwval = le32_to_cpu(dw); \
124 dwval &= ~(KDETH_##field##_MASK << KDETH_##field##_SHIFT); \
125 dwval |= (((val) & KDETH_##field##_MASK) << \
126 KDETH_##field##_SHIFT); \
127 dw = cpu_to_le32(dwval); \
128 } while (0)
129
130#define AHG_HEADER_SET(arr, idx, dw, bit, width, value) \
131 do { \
132 if ((idx) < ARRAY_SIZE((arr))) \
133 (arr)[(idx++)] = sdma_build_ahg_descriptor( \
134 (__force u16)(value), (dw), (bit), \
135 (width)); \
136 else \
137 return -ERANGE; \
138 } while (0)
139
140/* KDETH OM multipliers and switch over point */
141#define KDETH_OM_SMALL 4
142#define KDETH_OM_LARGE 64
143#define KDETH_OM_MAX_SIZE (1 << ((KDETH_OM_LARGE / KDETH_OM_SMALL) + 1))
144
145/* Last packet in the request */
Sunny Kumarcb326492015-11-06 10:06:43 +0530146#define TXREQ_FLAGS_REQ_LAST_PKT BIT(0)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400147
Dean Luick7b3256e2016-07-28 15:21:18 -0400148/* SDMA request flag bits */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400149#define SDMA_REQ_FOR_THREAD 1
150#define SDMA_REQ_SEND_DONE 2
151#define SDMA_REQ_HAVE_AHG 3
152#define SDMA_REQ_HAS_ERROR 4
153#define SDMA_REQ_DONE_ERROR 5
154
Sunny Kumarcb326492015-11-06 10:06:43 +0530155#define SDMA_PKT_Q_INACTIVE BIT(0)
156#define SDMA_PKT_Q_ACTIVE BIT(1)
157#define SDMA_PKT_Q_DEFERRED BIT(2)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400158
159/*
160 * Maximum retry attempts to submit a TX request
161 * before putting the process to sleep.
162 */
163#define MAX_DEFER_RETRY_COUNT 1
164
165static unsigned initial_pkt_count = 8;
166
167#define SDMA_IOWAIT_TIMEOUT 1000 /* in milliseconds */
168
Mitko Haralanov9565c6a2016-05-19 05:21:18 -0700169struct sdma_mmu_node;
170
Mike Marciniszyn77241052015-07-30 15:17:43 -0400171struct user_sdma_iovec {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800172 struct list_head list;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400173 struct iovec iov;
174 /* number of pages in this vector */
175 unsigned npages;
176 /* array of pinned pages for this vector */
177 struct page **pages;
Jubin John4d114fd2016-02-14 20:21:43 -0800178 /*
179 * offset into the virtual address space of the vector at
180 * which we last left off.
181 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400182 u64 offset;
Mitko Haralanov9565c6a2016-05-19 05:21:18 -0700183 struct sdma_mmu_node *node;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400184};
185
Bart Van Assched55215c2016-06-03 12:10:37 -0700186#define SDMA_CACHE_NODE_EVICT 0
Mitko Haralanove88c9272016-04-12 10:46:53 -0700187
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800188struct sdma_mmu_node {
189 struct mmu_rb_node rb;
Mitko Haralanov5511d782016-03-08 11:15:44 -0800190 struct list_head list;
191 struct hfi1_user_sdma_pkt_q *pq;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800192 atomic_t refcount;
193 struct page **pages;
194 unsigned npages;
Mitko Haralanove88c9272016-04-12 10:46:53 -0700195 unsigned long flags;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800196};
197
Mike Marciniszyn77241052015-07-30 15:17:43 -0400198struct user_sdma_request {
199 struct sdma_req_info info;
200 struct hfi1_user_sdma_pkt_q *pq;
201 struct hfi1_user_sdma_comp_q *cq;
202 /* This is the original header from user space */
203 struct hfi1_pkt_header hdr;
204 /*
205 * Pointer to the SDMA engine for this request.
206 * Since different request could be on different VLs,
207 * each request will need it's own engine pointer.
208 */
209 struct sdma_engine *sde;
210 u8 ahg_idx;
211 u32 ahg[9];
212 /*
213 * KDETH.Offset (Eager) field
214 * We need to remember the initial value so the headers
215 * can be updated properly.
216 */
217 u32 koffset;
218 /*
219 * KDETH.OFFSET (TID) field
220 * The offset can cover multiple packets, depending on the
221 * size of the TID entry.
222 */
223 u32 tidoffset;
224 /*
225 * KDETH.OM
226 * Remember this because the header template always sets it
227 * to 0.
228 */
229 u8 omfactor;
230 /*
Mike Marciniszyn77241052015-07-30 15:17:43 -0400231 * We copy the iovs for this request (based on
232 * info.iovcnt). These are only the data vectors
233 */
234 unsigned data_iovs;
235 /* total length of the data in the request */
236 u32 data_len;
237 /* progress index moving along the iovs array */
238 unsigned iov_idx;
239 struct user_sdma_iovec iovs[MAX_VECTORS_PER_REQ];
240 /* number of elements copied to the tids array */
241 u16 n_tids;
242 /* TID array values copied from the tid_iov vector */
243 u32 *tids;
244 u16 tididx;
245 u32 sent;
246 u64 seqnum;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800247 u64 seqcomp;
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -0800248 u64 seqsubmitted;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400249 struct list_head txps;
250 unsigned long flags;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500251 /* status of the last txreq completed */
252 int status;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400253};
254
Mitko Haralanovb9fb63182015-10-26 10:28:37 -0400255/*
256 * A single txreq could span up to 3 physical pages when the MTU
257 * is sufficiently large (> 4K). Each of the IOV pointers also
258 * needs it's own set of flags so the vector has been handled
259 * independently of each other.
260 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400261struct user_sdma_txreq {
262 /* Packet header for the txreq */
263 struct hfi1_pkt_header hdr;
264 struct sdma_txreq txreq;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500265 struct list_head list;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400266 struct user_sdma_request *req;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400267 u16 flags;
268 unsigned busycount;
269 u64 seqnum;
270};
271
272#define SDMA_DBG(req, fmt, ...) \
273 hfi1_cdbg(SDMA, "[%u:%u:%u:%u] " fmt, (req)->pq->dd->unit, \
274 (req)->pq->ctxt, (req)->pq->subctxt, (req)->info.comp_idx, \
275 ##__VA_ARGS__)
276#define SDMA_Q_DBG(pq, fmt, ...) \
277 hfi1_cdbg(SDMA, "[%u:%u:%u] " fmt, (pq)->dd->unit, (pq)->ctxt, \
278 (pq)->subctxt, ##__VA_ARGS__)
279
280static int user_sdma_send_pkts(struct user_sdma_request *, unsigned);
281static int num_user_pages(const struct iovec *);
Mike Marciniszyna545f532016-02-14 12:45:53 -0800282static void user_sdma_txreq_cb(struct sdma_txreq *, int);
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800283static inline void pq_update(struct hfi1_user_sdma_pkt_q *);
284static void user_sdma_free_request(struct user_sdma_request *, bool);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400285static int pin_vector_pages(struct user_sdma_request *,
286 struct user_sdma_iovec *);
Mitko Haralanov849e3e92016-04-12 10:46:16 -0700287static void unpin_vector_pages(struct mm_struct *, struct page **, unsigned,
288 unsigned);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400289static int check_header_template(struct user_sdma_request *,
290 struct hfi1_pkt_header *, u32, u32);
291static int set_txreq_header(struct user_sdma_request *,
292 struct user_sdma_txreq *, u32);
293static int set_txreq_header_ahg(struct user_sdma_request *,
294 struct user_sdma_txreq *, u32);
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800295static inline void set_comp_state(struct hfi1_user_sdma_pkt_q *,
296 struct hfi1_user_sdma_comp_q *,
297 u16, enum hfi1_sdma_comp_state, int);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400298static inline u32 set_pkt_bth_psn(__be32, u8, u32);
299static inline u32 get_lrh_len(struct hfi1_pkt_header, u32 len);
300
301static int defer_packet_queue(
302 struct sdma_engine *,
303 struct iowait *,
304 struct sdma_txreq *,
305 unsigned seq);
306static void activate_packet_queue(struct iowait *, int);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800307static bool sdma_rb_filter(struct mmu_rb_node *, unsigned long, unsigned long);
308static int sdma_rb_insert(struct rb_root *, struct mmu_rb_node *);
Mitko Haralanovf19bd642016-04-12 10:45:57 -0700309static void sdma_rb_remove(struct rb_root *, struct mmu_rb_node *,
310 struct mm_struct *);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800311static int sdma_rb_invalidate(struct rb_root *, struct mmu_rb_node *);
312
313static struct mmu_rb_ops sdma_rb_ops = {
314 .filter = sdma_rb_filter,
315 .insert = sdma_rb_insert,
316 .remove = sdma_rb_remove,
317 .invalidate = sdma_rb_invalidate
318};
Mike Marciniszyn77241052015-07-30 15:17:43 -0400319
Mike Marciniszyn77241052015-07-30 15:17:43 -0400320static int defer_packet_queue(
321 struct sdma_engine *sde,
322 struct iowait *wait,
323 struct sdma_txreq *txreq,
324 unsigned seq)
325{
326 struct hfi1_user_sdma_pkt_q *pq =
327 container_of(wait, struct hfi1_user_sdma_pkt_q, busy);
328 struct hfi1_ibdev *dev = &pq->dd->verbs_dev;
329 struct user_sdma_txreq *tx =
330 container_of(txreq, struct user_sdma_txreq, txreq);
331
332 if (sdma_progress(sde, seq, txreq)) {
333 if (tx->busycount++ < MAX_DEFER_RETRY_COUNT)
334 goto eagain;
335 }
336 /*
337 * We are assuming that if the list is enqueued somewhere, it
338 * is to the dmawait list since that is the only place where
339 * it is supposed to be enqueued.
340 */
341 xchg(&pq->state, SDMA_PKT_Q_DEFERRED);
342 write_seqlock(&dev->iowait_lock);
343 if (list_empty(&pq->busy.list))
344 list_add_tail(&pq->busy.list, &sde->dmawait);
345 write_sequnlock(&dev->iowait_lock);
346 return -EBUSY;
347eagain:
348 return -EAGAIN;
349}
350
351static void activate_packet_queue(struct iowait *wait, int reason)
352{
353 struct hfi1_user_sdma_pkt_q *pq =
354 container_of(wait, struct hfi1_user_sdma_pkt_q, busy);
355 xchg(&pq->state, SDMA_PKT_Q_ACTIVE);
356 wake_up(&wait->wait_dma);
357};
358
359static void sdma_kmem_cache_ctor(void *obj)
360{
Janani Ravichandran16ccad02016-02-25 15:08:17 -0500361 struct user_sdma_txreq *tx = obj;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400362
363 memset(tx, 0, sizeof(*tx));
364}
365
366int hfi1_user_sdma_alloc_queues(struct hfi1_ctxtdata *uctxt, struct file *fp)
367{
Ira Weiny9e10af42015-10-30 18:58:40 -0400368 struct hfi1_filedata *fd;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400369 int ret = 0;
370 unsigned memsize;
371 char buf[64];
372 struct hfi1_devdata *dd;
373 struct hfi1_user_sdma_comp_q *cq;
374 struct hfi1_user_sdma_pkt_q *pq;
375 unsigned long flags;
376
377 if (!uctxt || !fp) {
378 ret = -EBADF;
379 goto done;
380 }
381
Ira Weiny9e10af42015-10-30 18:58:40 -0400382 fd = fp->private_data;
383
Mike Marciniszyn77241052015-07-30 15:17:43 -0400384 if (!hfi1_sdma_comp_ring_size) {
385 ret = -EINVAL;
386 goto done;
387 }
388
389 dd = uctxt->dd;
390
391 pq = kzalloc(sizeof(*pq), GFP_KERNEL);
Alison Schofield806e6e12015-10-12 14:28:36 -0700392 if (!pq)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400393 goto pq_nomem;
Alison Schofield806e6e12015-10-12 14:28:36 -0700394
Mike Marciniszyn77241052015-07-30 15:17:43 -0400395 memsize = sizeof(*pq->reqs) * hfi1_sdma_comp_ring_size;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800396 pq->reqs = kzalloc(memsize, GFP_KERNEL);
Alison Schofield806e6e12015-10-12 14:28:36 -0700397 if (!pq->reqs)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400398 goto pq_reqs_nomem;
Alison Schofield806e6e12015-10-12 14:28:36 -0700399
Dean Luick7b3256e2016-07-28 15:21:18 -0400400 memsize = BITS_TO_LONGS(hfi1_sdma_comp_ring_size) * sizeof(long);
401 pq->req_in_use = kzalloc(memsize, GFP_KERNEL);
402 if (!pq->req_in_use)
403 goto pq_reqs_no_in_use;
404
Mike Marciniszyn77241052015-07-30 15:17:43 -0400405 INIT_LIST_HEAD(&pq->list);
406 pq->dd = dd;
407 pq->ctxt = uctxt->ctxt;
Ira Weiny9e10af42015-10-30 18:58:40 -0400408 pq->subctxt = fd->subctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400409 pq->n_max_reqs = hfi1_sdma_comp_ring_size;
410 pq->state = SDMA_PKT_Q_INACTIVE;
411 atomic_set(&pq->n_reqs, 0);
Mitko Haralanova0d40692015-12-08 17:10:13 -0500412 init_waitqueue_head(&pq->wait);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800413 pq->sdma_rb_root = RB_ROOT;
Mitko Haralanov5511d782016-03-08 11:15:44 -0800414 INIT_LIST_HEAD(&pq->evict);
415 spin_lock_init(&pq->evict_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400416
417 iowait_init(&pq->busy, 0, NULL, defer_packet_queue,
Mike Marciniszyna545f532016-02-14 12:45:53 -0800418 activate_packet_queue, NULL);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400419 pq->reqidx = 0;
420 snprintf(buf, 64, "txreq-kmem-cache-%u-%u-%u", dd->unit, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -0400421 fd->subctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400422 pq->txreq_cache = kmem_cache_create(buf,
423 sizeof(struct user_sdma_txreq),
424 L1_CACHE_BYTES,
425 SLAB_HWCACHE_ALIGN,
426 sdma_kmem_cache_ctor);
427 if (!pq->txreq_cache) {
428 dd_dev_err(dd, "[%u] Failed to allocate TxReq cache\n",
429 uctxt->ctxt);
430 goto pq_txreq_nomem;
431 }
Ira Weiny9e10af42015-10-30 18:58:40 -0400432 fd->pq = pq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400433 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
Alison Schofield806e6e12015-10-12 14:28:36 -0700434 if (!cq)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400435 goto cq_nomem;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400436
Amitoj Kaur Chawla84449912016-03-04 22:30:43 +0530437 memsize = PAGE_ALIGN(sizeof(*cq->comps) * hfi1_sdma_comp_ring_size);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400438 cq->comps = vmalloc_user(memsize);
Alison Schofield806e6e12015-10-12 14:28:36 -0700439 if (!cq->comps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400440 goto cq_comps_nomem;
Alison Schofield806e6e12015-10-12 14:28:36 -0700441
Mike Marciniszyn77241052015-07-30 15:17:43 -0400442 cq->nentries = hfi1_sdma_comp_ring_size;
Ira Weiny9e10af42015-10-30 18:58:40 -0400443 fd->cq = cq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400444
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800445 ret = hfi1_mmu_rb_register(&pq->sdma_rb_root, &sdma_rb_ops);
446 if (ret) {
447 dd_dev_err(dd, "Failed to register with MMU %d", ret);
448 goto done;
449 }
450
Mike Marciniszyn77241052015-07-30 15:17:43 -0400451 spin_lock_irqsave(&uctxt->sdma_qlock, flags);
452 list_add(&pq->list, &uctxt->sdma_queues);
453 spin_unlock_irqrestore(&uctxt->sdma_qlock, flags);
454 goto done;
455
456cq_comps_nomem:
457 kfree(cq);
458cq_nomem:
459 kmem_cache_destroy(pq->txreq_cache);
460pq_txreq_nomem:
Dean Luick7b3256e2016-07-28 15:21:18 -0400461 kfree(pq->req_in_use);
462pq_reqs_no_in_use:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400463 kfree(pq->reqs);
464pq_reqs_nomem:
465 kfree(pq);
Ira Weiny9e10af42015-10-30 18:58:40 -0400466 fd->pq = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400467pq_nomem:
468 ret = -ENOMEM;
469done:
470 return ret;
471}
472
473int hfi1_user_sdma_free_queues(struct hfi1_filedata *fd)
474{
475 struct hfi1_ctxtdata *uctxt = fd->uctxt;
476 struct hfi1_user_sdma_pkt_q *pq;
477 unsigned long flags;
478
479 hfi1_cdbg(SDMA, "[%u:%u:%u] Freeing user SDMA queues", uctxt->dd->unit,
480 uctxt->ctxt, fd->subctxt);
481 pq = fd->pq;
482 if (pq) {
Ira Weiny53445bb2016-07-28 15:21:12 -0400483 hfi1_mmu_rb_unregister(&pq->sdma_rb_root);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400484 spin_lock_irqsave(&uctxt->sdma_qlock, flags);
485 if (!list_empty(&pq->list))
486 list_del_init(&pq->list);
487 spin_unlock_irqrestore(&uctxt->sdma_qlock, flags);
488 iowait_sdma_drain(&pq->busy);
Mitko Haralanova0d40692015-12-08 17:10:13 -0500489 /* Wait until all requests have been freed. */
490 wait_event_interruptible(
491 pq->wait,
492 (ACCESS_ONCE(pq->state) == SDMA_PKT_Q_INACTIVE));
493 kfree(pq->reqs);
Dean Luick7b3256e2016-07-28 15:21:18 -0400494 kfree(pq->req_in_use);
Julia Lawalladad44d2015-09-13 14:15:04 +0200495 kmem_cache_destroy(pq->txreq_cache);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400496 kfree(pq);
497 fd->pq = NULL;
498 }
499 if (fd->cq) {
Bhumika Goyala4d7d052016-02-14 20:34:28 +0530500 vfree(fd->cq->comps);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400501 kfree(fd->cq);
502 fd->cq = NULL;
503 }
504 return 0;
505}
506
Jianxin Xiong14833b82016-07-01 16:01:56 -0700507static u8 dlid_to_selector(u16 dlid)
508{
509 static u8 mapping[256];
510 static int initialized;
511 static u8 next;
512 int hash;
513
514 if (!initialized) {
515 memset(mapping, 0xFF, 256);
516 initialized = 1;
517 }
518
519 hash = ((dlid >> 8) ^ dlid) & 0xFF;
520 if (mapping[hash] == 0xFF) {
521 mapping[hash] = next;
522 next = (next + 1) & 0x7F;
523 }
524
525 return mapping[hash];
526}
527
Mike Marciniszyn77241052015-07-30 15:17:43 -0400528int hfi1_user_sdma_process_request(struct file *fp, struct iovec *iovec,
529 unsigned long dim, unsigned long *count)
530{
Dean Luickff4ce9b2016-07-28 12:27:34 -0400531 int ret = 0, i;
Ira Weiny9e10af42015-10-30 18:58:40 -0400532 struct hfi1_filedata *fd = fp->private_data;
533 struct hfi1_ctxtdata *uctxt = fd->uctxt;
534 struct hfi1_user_sdma_pkt_q *pq = fd->pq;
535 struct hfi1_user_sdma_comp_q *cq = fd->cq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400536 struct hfi1_devdata *dd = pq->dd;
537 unsigned long idx = 0;
538 u8 pcount = initial_pkt_count;
539 struct sdma_req_info info;
540 struct user_sdma_request *req;
541 u8 opcode, sc, vl;
Jianxin Xiongb583faf2016-05-19 05:21:57 -0700542 int req_queued = 0;
Jianxin Xiong14833b82016-07-01 16:01:56 -0700543 u16 dlid;
544 u8 selector;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400545
546 if (iovec[idx].iov_len < sizeof(info) + sizeof(req->hdr)) {
547 hfi1_cdbg(
548 SDMA,
549 "[%u:%u:%u] First vector not big enough for header %lu/%lu",
Ira Weiny9e10af42015-10-30 18:58:40 -0400550 dd->unit, uctxt->ctxt, fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400551 iovec[idx].iov_len, sizeof(info) + sizeof(req->hdr));
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500552 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400553 }
554 ret = copy_from_user(&info, iovec[idx].iov_base, sizeof(info));
555 if (ret) {
556 hfi1_cdbg(SDMA, "[%u:%u:%u] Failed to copy info QW (%d)",
Ira Weiny9e10af42015-10-30 18:58:40 -0400557 dd->unit, uctxt->ctxt, fd->subctxt, ret);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500558 return -EFAULT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400559 }
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800560
Ira Weiny9e10af42015-10-30 18:58:40 -0400561 trace_hfi1_sdma_user_reqinfo(dd, uctxt->ctxt, fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400562 (u16 *)&info);
Dean Luick4fa0d222016-07-28 15:21:14 -0400563
564 if (info.comp_idx >= hfi1_sdma_comp_ring_size) {
565 hfi1_cdbg(SDMA,
566 "[%u:%u:%u:%u] Invalid comp index",
567 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx);
568 return -EINVAL;
569 }
570
Dean Luick9ff73c82016-07-28 15:21:15 -0400571 /*
572 * Sanity check the header io vector count. Need at least 1 vector
573 * (header) and cannot be larger than the actual io vector count.
574 */
575 if (req_iovcnt(info.ctrl) < 1 || req_iovcnt(info.ctrl) > dim) {
576 hfi1_cdbg(SDMA,
577 "[%u:%u:%u:%u] Invalid iov count %d, dim %ld",
578 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx,
579 req_iovcnt(info.ctrl), dim);
580 return -EINVAL;
581 }
582
Mike Marciniszyn77241052015-07-30 15:17:43 -0400583 if (!info.fragsize) {
584 hfi1_cdbg(SDMA,
585 "[%u:%u:%u:%u] Request does not specify fragsize",
Ira Weiny9e10af42015-10-30 18:58:40 -0400586 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500587 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400588 }
Dean Luick7b3256e2016-07-28 15:21:18 -0400589
590 /* Try to claim the request. */
591 if (test_and_set_bit(info.comp_idx, pq->req_in_use)) {
592 hfi1_cdbg(SDMA, "[%u:%u:%u] Entry %u is in use",
593 dd->unit, uctxt->ctxt, fd->subctxt,
594 info.comp_idx);
595 return -EBADSLT;
596 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400597 /*
Dean Luick7b3256e2016-07-28 15:21:18 -0400598 * All safety checks have been done and this request has been claimed.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400599 */
600 hfi1_cdbg(SDMA, "[%u:%u:%u] Using req/comp entry %u\n", dd->unit,
Ira Weiny9e10af42015-10-30 18:58:40 -0400601 uctxt->ctxt, fd->subctxt, info.comp_idx);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400602 req = pq->reqs + info.comp_idx;
603 memset(req, 0, sizeof(*req));
Dean Luick9ff73c82016-07-28 15:21:15 -0400604 req->data_iovs = req_iovcnt(info.ctrl) - 1; /* subtract header vector */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400605 req->pq = pq;
606 req->cq = cq;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500607 req->status = -1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400608 INIT_LIST_HEAD(&req->txps);
Mitko Haralanova0d40692015-12-08 17:10:13 -0500609
Mike Marciniszyn77241052015-07-30 15:17:43 -0400610 memcpy(&req->info, &info, sizeof(info));
611
Dean Luick9ff73c82016-07-28 15:21:15 -0400612 if (req_opcode(info.ctrl) == EXPECTED) {
613 /* expected must have a TID info and at least one data vector */
614 if (req->data_iovs < 2) {
615 SDMA_DBG(req,
616 "Not enough vectors for expected request");
617 ret = -EINVAL;
618 goto free_req;
619 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400620 req->data_iovs--;
Dean Luick9ff73c82016-07-28 15:21:15 -0400621 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400622
623 if (!info.npkts || req->data_iovs > MAX_VECTORS_PER_REQ) {
624 SDMA_DBG(req, "Too many vectors (%u/%u)", req->data_iovs,
625 MAX_VECTORS_PER_REQ);
Dean Luick9da7e9a2016-07-28 15:21:17 -0400626 ret = -EINVAL;
627 goto free_req;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400628 }
629 /* Copy the header from the user buffer */
630 ret = copy_from_user(&req->hdr, iovec[idx].iov_base + sizeof(info),
631 sizeof(req->hdr));
632 if (ret) {
633 SDMA_DBG(req, "Failed to copy header template (%d)", ret);
634 ret = -EFAULT;
635 goto free_req;
636 }
637
638 /* If Static rate control is not enabled, sanitize the header. */
639 if (!HFI1_CAP_IS_USET(STATIC_RATE_CTRL))
640 req->hdr.pbc[2] = 0;
641
642 /* Validate the opcode. Do not trust packets from user space blindly. */
643 opcode = (be32_to_cpu(req->hdr.bth[0]) >> 24) & 0xff;
644 if ((opcode & USER_OPCODE_CHECK_MASK) !=
645 USER_OPCODE_CHECK_VAL) {
646 SDMA_DBG(req, "Invalid opcode (%d)", opcode);
647 ret = -EINVAL;
648 goto free_req;
649 }
650 /*
651 * Validate the vl. Do not trust packets from user space blindly.
652 * VL comes from PBC, SC comes from LRH, and the VL needs to
653 * match the SC look up.
654 */
655 vl = (le16_to_cpu(req->hdr.pbc[0]) >> 12) & 0xF;
656 sc = (((be16_to_cpu(req->hdr.lrh[0]) >> 12) & 0xF) |
657 (((le16_to_cpu(req->hdr.pbc[1]) >> 14) & 0x1) << 4));
658 if (vl >= dd->pport->vls_operational ||
659 vl != sc_to_vlt(dd, sc)) {
660 SDMA_DBG(req, "Invalid SC(%u)/VL(%u)", sc, vl);
661 ret = -EINVAL;
662 goto free_req;
663 }
664
Sebastian Sancheze38d1e42016-04-12 11:22:21 -0700665 /* Checking P_KEY for requests from user-space */
666 if (egress_pkey_check(dd->pport, req->hdr.lrh, req->hdr.bth, sc,
667 PKEY_CHECK_INVALID)) {
668 ret = -EINVAL;
669 goto free_req;
670 }
671
Mike Marciniszyn77241052015-07-30 15:17:43 -0400672 /*
673 * Also should check the BTH.lnh. If it says the next header is GRH then
674 * the RXE parsing will be off and will land in the middle of the KDETH
675 * or miss it entirely.
676 */
677 if ((be16_to_cpu(req->hdr.lrh[0]) & 0x3) == HFI1_LRH_GRH) {
678 SDMA_DBG(req, "User tried to pass in a GRH");
679 ret = -EINVAL;
680 goto free_req;
681 }
682
683 req->koffset = le32_to_cpu(req->hdr.kdeth.swdata[6]);
Jubin John4d114fd2016-02-14 20:21:43 -0800684 /*
685 * Calculate the initial TID offset based on the values of
686 * KDETH.OFFSET and KDETH.OM that are passed in.
687 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400688 req->tidoffset = KDETH_GET(req->hdr.kdeth.ver_tid_offset, OFFSET) *
689 (KDETH_GET(req->hdr.kdeth.ver_tid_offset, OM) ?
690 KDETH_OM_LARGE : KDETH_OM_SMALL);
691 SDMA_DBG(req, "Initial TID offset %u", req->tidoffset);
692 idx++;
693
694 /* Save all the IO vector structures */
Dean Luickff4ce9b2016-07-28 12:27:34 -0400695 for (i = 0; i < req->data_iovs; i++) {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800696 INIT_LIST_HEAD(&req->iovs[i].list);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400697 memcpy(&req->iovs[i].iov, iovec + idx++, sizeof(struct iovec));
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800698 ret = pin_vector_pages(req, &req->iovs[i]);
699 if (ret) {
700 req->status = ret;
701 goto free_req;
702 }
Dean Luickff4ce9b2016-07-28 12:27:34 -0400703 req->data_len += req->iovs[i].iov.iov_len;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400704 }
705 SDMA_DBG(req, "total data length %u", req->data_len);
706
707 if (pcount > req->info.npkts)
708 pcount = req->info.npkts;
709 /*
710 * Copy any TID info
711 * User space will provide the TID info only when the
712 * request type is EXPECTED. This is true even if there is
713 * only one packet in the request and the header is already
714 * setup. The reason for the singular TID case is that the
715 * driver needs to perform safety checks.
716 */
717 if (req_opcode(req->info.ctrl) == EXPECTED) {
718 u16 ntids = iovec[idx].iov_len / sizeof(*req->tids);
719
720 if (!ntids || ntids > MAX_TID_PAIR_ENTRIES) {
721 ret = -EINVAL;
722 goto free_req;
723 }
724 req->tids = kcalloc(ntids, sizeof(*req->tids), GFP_KERNEL);
725 if (!req->tids) {
726 ret = -ENOMEM;
727 goto free_req;
728 }
729 /*
730 * We have to copy all of the tids because they may vary
731 * in size and, therefore, the TID count might not be
732 * equal to the pkt count. However, there is no way to
733 * tell at this point.
734 */
735 ret = copy_from_user(req->tids, iovec[idx].iov_base,
736 ntids * sizeof(*req->tids));
737 if (ret) {
738 SDMA_DBG(req, "Failed to copy %d TIDs (%d)",
739 ntids, ret);
740 ret = -EFAULT;
741 goto free_req;
742 }
743 req->n_tids = ntids;
744 idx++;
745 }
746
Jianxin Xiong14833b82016-07-01 16:01:56 -0700747 dlid = be16_to_cpu(req->hdr.lrh[1]);
748 selector = dlid_to_selector(dlid);
749
Mike Marciniszyn77241052015-07-30 15:17:43 -0400750 /* Have to select the engine */
751 req->sde = sdma_select_engine_vl(dd,
Jianxin Xiong14833b82016-07-01 16:01:56 -0700752 (u32)(uctxt->ctxt + fd->subctxt +
753 selector),
Mike Marciniszyn77241052015-07-30 15:17:43 -0400754 vl);
755 if (!req->sde || !sdma_running(req->sde)) {
756 ret = -ECOMM;
757 goto free_req;
758 }
759
760 /* We don't need an AHG entry if the request contains only one packet */
761 if (req->info.npkts > 1 && HFI1_CAP_IS_USET(SDMA_AHG)) {
762 int ahg = sdma_ahg_alloc(req->sde);
763
764 if (likely(ahg >= 0)) {
765 req->ahg_idx = (u8)ahg;
766 set_bit(SDMA_REQ_HAVE_AHG, &req->flags);
767 }
768 }
769
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800770 set_comp_state(pq, cq, info.comp_idx, QUEUED, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400771 atomic_inc(&pq->n_reqs);
Jianxin Xiongb583faf2016-05-19 05:21:57 -0700772 req_queued = 1;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800773 /* Send the first N packets in the request to buy us some time */
774 ret = user_sdma_send_pkts(req, pcount);
775 if (unlikely(ret < 0 && ret != -EBUSY)) {
776 req->status = ret;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800777 goto free_req;
778 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400779
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800780 /*
781 * It is possible that the SDMA engine would have processed all the
782 * submitted packets by the time we get here. Therefore, only set
783 * packet queue state to ACTIVE if there are still uncompleted
784 * requests.
785 */
786 if (atomic_read(&pq->n_reqs))
787 xchg(&pq->state, SDMA_PKT_Q_ACTIVE);
788
789 /*
790 * This is a somewhat blocking send implementation.
791 * The driver will block the caller until all packets of the
792 * request have been submitted to the SDMA engine. However, it
793 * will not wait for send completions.
794 */
795 while (!test_bit(SDMA_REQ_SEND_DONE, &req->flags)) {
796 ret = user_sdma_send_pkts(req, pcount);
797 if (ret < 0) {
798 if (ret != -EBUSY) {
799 req->status = ret;
800 set_bit(SDMA_REQ_DONE_ERROR, &req->flags);
Mitko Haralanova402d6a2016-02-03 14:37:41 -0800801 if (ACCESS_ONCE(req->seqcomp) ==
802 req->seqsubmitted - 1)
803 goto free_req;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800804 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400805 }
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800806 wait_event_interruptible_timeout(
807 pq->busy.wait_dma,
808 (pq->state == SDMA_PKT_Q_ACTIVE),
809 msecs_to_jiffies(
810 SDMA_IOWAIT_TIMEOUT));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400811 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400812 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400813 *count += idx;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500814 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400815free_req:
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800816 user_sdma_free_request(req, true);
Jianxin Xiongb583faf2016-05-19 05:21:57 -0700817 if (req_queued)
818 pq_update(pq);
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800819 set_comp_state(pq, cq, info.comp_idx, ERROR, req->status);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400820 return ret;
821}
822
823static inline u32 compute_data_length(struct user_sdma_request *req,
Jubin John17fb4f22016-02-14 20:21:52 -0800824 struct user_sdma_txreq *tx)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400825{
826 /*
827 * Determine the proper size of the packet data.
828 * The size of the data of the first packet is in the header
829 * template. However, it includes the header and ICRC, which need
830 * to be subtracted.
Ira Weinyc4929802016-07-27 21:08:42 -0400831 * The minimum representable packet data length in a header is 4 bytes,
832 * therefore, when the data length request is less than 4 bytes, there's
833 * only one packet, and the packet data length is equal to that of the
834 * request data length.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400835 * The size of the remaining packets is the minimum of the frag
836 * size (MTU) or remaining data in the request.
837 */
838 u32 len;
839
840 if (!req->seqnum) {
Ira Weinyc4929802016-07-27 21:08:42 -0400841 if (req->data_len < sizeof(u32))
842 len = req->data_len;
843 else
844 len = ((be16_to_cpu(req->hdr.lrh[2]) << 2) -
845 (sizeof(tx->hdr) - 4));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400846 } else if (req_opcode(req->info.ctrl) == EXPECTED) {
847 u32 tidlen = EXP_TID_GET(req->tids[req->tididx], LEN) *
848 PAGE_SIZE;
Jubin John4d114fd2016-02-14 20:21:43 -0800849 /*
850 * Get the data length based on the remaining space in the
851 * TID pair.
852 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400853 len = min(tidlen - req->tidoffset, (u32)req->info.fragsize);
854 /* If we've filled up the TID pair, move to the next one. */
855 if (unlikely(!len) && ++req->tididx < req->n_tids &&
856 req->tids[req->tididx]) {
857 tidlen = EXP_TID_GET(req->tids[req->tididx],
858 LEN) * PAGE_SIZE;
859 req->tidoffset = 0;
860 len = min_t(u32, tidlen, req->info.fragsize);
861 }
Jubin John4d114fd2016-02-14 20:21:43 -0800862 /*
863 * Since the TID pairs map entire pages, make sure that we
Mike Marciniszyn77241052015-07-30 15:17:43 -0400864 * are not going to try to send more data that we have
Jubin John4d114fd2016-02-14 20:21:43 -0800865 * remaining.
866 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400867 len = min(len, req->data_len - req->sent);
Jubin Johne4909742016-02-14 20:22:00 -0800868 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400869 len = min(req->data_len - req->sent, (u32)req->info.fragsize);
Jubin Johne4909742016-02-14 20:22:00 -0800870 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400871 SDMA_DBG(req, "Data Length = %u", len);
872 return len;
873}
874
Ira Weinyc4929802016-07-27 21:08:42 -0400875static inline u32 pad_len(u32 len)
876{
877 if (len & (sizeof(u32) - 1))
878 len += sizeof(u32) - (len & (sizeof(u32) - 1));
879 return len;
880}
881
Mike Marciniszyn77241052015-07-30 15:17:43 -0400882static inline u32 get_lrh_len(struct hfi1_pkt_header hdr, u32 len)
883{
884 /* (Size of complete header - size of PBC) + 4B ICRC + data length */
885 return ((sizeof(hdr) - sizeof(hdr.pbc)) + 4 + len);
886}
887
888static int user_sdma_send_pkts(struct user_sdma_request *req, unsigned maxpkts)
889{
890 int ret = 0;
891 unsigned npkts = 0;
892 struct user_sdma_txreq *tx = NULL;
893 struct hfi1_user_sdma_pkt_q *pq = NULL;
894 struct user_sdma_iovec *iovec = NULL;
895
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500896 if (!req->pq)
897 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400898
899 pq = req->pq;
900
Mitko Haralanov6a5464f2015-12-08 17:10:12 -0500901 /* If tx completion has reported an error, we are done. */
902 if (test_bit(SDMA_REQ_HAS_ERROR, &req->flags)) {
903 set_bit(SDMA_REQ_DONE_ERROR, &req->flags);
904 return -EFAULT;
905 }
906
Mike Marciniszyn77241052015-07-30 15:17:43 -0400907 /*
908 * Check if we might have sent the entire request already
909 */
910 if (unlikely(req->seqnum == req->info.npkts)) {
911 if (!list_empty(&req->txps))
912 goto dosend;
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500913 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400914 }
915
916 if (!maxpkts || maxpkts > req->info.npkts - req->seqnum)
917 maxpkts = req->info.npkts - req->seqnum;
918
919 while (npkts < maxpkts) {
920 u32 datalen = 0, queued = 0, data_sent = 0;
921 u64 iov_offset = 0;
922
923 /*
924 * Check whether any of the completions have come back
925 * with errors. If so, we are not going to process any
926 * more packets from this request.
927 */
928 if (test_bit(SDMA_REQ_HAS_ERROR, &req->flags)) {
929 set_bit(SDMA_REQ_DONE_ERROR, &req->flags);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500930 return -EFAULT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400931 }
932
933 tx = kmem_cache_alloc(pq->txreq_cache, GFP_KERNEL);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500934 if (!tx)
935 return -ENOMEM;
936
Mike Marciniszyn77241052015-07-30 15:17:43 -0400937 tx->flags = 0;
938 tx->req = req;
939 tx->busycount = 0;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500940 INIT_LIST_HEAD(&tx->list);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400941
942 if (req->seqnum == req->info.npkts - 1)
Mitko Haralanovb9fb63182015-10-26 10:28:37 -0400943 tx->flags |= TXREQ_FLAGS_REQ_LAST_PKT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400944
945 /*
946 * Calculate the payload size - this is min of the fragment
947 * (MTU) size or the remaining bytes in the request but only
948 * if we have payload data.
949 */
950 if (req->data_len) {
951 iovec = &req->iovs[req->iov_idx];
952 if (ACCESS_ONCE(iovec->offset) == iovec->iov.iov_len) {
953 if (++req->iov_idx == req->data_iovs) {
954 ret = -EFAULT;
955 goto free_txreq;
956 }
957 iovec = &req->iovs[req->iov_idx];
958 WARN_ON(iovec->offset);
959 }
960
Mike Marciniszyn77241052015-07-30 15:17:43 -0400961 datalen = compute_data_length(req, tx);
962 if (!datalen) {
963 SDMA_DBG(req,
964 "Request has data but pkt len is 0");
965 ret = -EFAULT;
966 goto free_tx;
967 }
968 }
969
970 if (test_bit(SDMA_REQ_HAVE_AHG, &req->flags)) {
971 if (!req->seqnum) {
972 u16 pbclen = le16_to_cpu(req->hdr.pbc[0]);
Ira Weinyc4929802016-07-27 21:08:42 -0400973 u32 lrhlen = get_lrh_len(req->hdr,
974 pad_len(datalen));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400975 /*
976 * Copy the request header into the tx header
977 * because the HW needs a cacheline-aligned
978 * address.
979 * This copy can be optimized out if the hdr
980 * member of user_sdma_request were also
981 * cacheline aligned.
982 */
983 memcpy(&tx->hdr, &req->hdr, sizeof(tx->hdr));
984 if (PBC2LRH(pbclen) != lrhlen) {
985 pbclen = (pbclen & 0xf000) |
986 LRH2PBC(lrhlen);
987 tx->hdr.pbc[0] = cpu_to_le16(pbclen);
988 }
989 ret = sdma_txinit_ahg(&tx->txreq,
990 SDMA_TXREQ_F_AHG_COPY,
991 sizeof(tx->hdr) + datalen,
992 req->ahg_idx, 0, NULL, 0,
993 user_sdma_txreq_cb);
994 if (ret)
995 goto free_tx;
996 ret = sdma_txadd_kvaddr(pq->dd, &tx->txreq,
997 &tx->hdr,
998 sizeof(tx->hdr));
999 if (ret)
1000 goto free_txreq;
1001 } else {
1002 int changes;
1003
1004 changes = set_txreq_header_ahg(req, tx,
1005 datalen);
1006 if (changes < 0)
1007 goto free_tx;
1008 sdma_txinit_ahg(&tx->txreq,
1009 SDMA_TXREQ_F_USE_AHG,
1010 datalen, req->ahg_idx, changes,
1011 req->ahg, sizeof(req->hdr),
1012 user_sdma_txreq_cb);
1013 }
1014 } else {
1015 ret = sdma_txinit(&tx->txreq, 0, sizeof(req->hdr) +
1016 datalen, user_sdma_txreq_cb);
1017 if (ret)
1018 goto free_tx;
1019 /*
1020 * Modify the header for this packet. This only needs
1021 * to be done if we are not going to use AHG. Otherwise,
1022 * the HW will do it based on the changes we gave it
1023 * during sdma_txinit_ahg().
1024 */
1025 ret = set_txreq_header(req, tx, datalen);
1026 if (ret)
1027 goto free_txreq;
1028 }
1029
1030 /*
1031 * If the request contains any data vectors, add up to
1032 * fragsize bytes to the descriptor.
1033 */
1034 while (queued < datalen &&
1035 (req->sent + data_sent) < req->data_len) {
1036 unsigned long base, offset;
1037 unsigned pageidx, len;
1038
1039 base = (unsigned long)iovec->iov.iov_base;
Amitoj Kaur Chawla72a5f6a2016-02-20 19:08:02 +05301040 offset = offset_in_page(base + iovec->offset +
1041 iov_offset);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001042 pageidx = (((iovec->offset + iov_offset +
1043 base) - (base & PAGE_MASK)) >> PAGE_SHIFT);
1044 len = offset + req->info.fragsize > PAGE_SIZE ?
1045 PAGE_SIZE - offset : req->info.fragsize;
1046 len = min((datalen - queued), len);
1047 ret = sdma_txadd_page(pq->dd, &tx->txreq,
1048 iovec->pages[pageidx],
1049 offset, len);
1050 if (ret) {
Mitko Haralanova0d40692015-12-08 17:10:13 -05001051 SDMA_DBG(req, "SDMA txreq add page failed %d\n",
1052 ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001053 goto free_txreq;
1054 }
1055 iov_offset += len;
1056 queued += len;
1057 data_sent += len;
1058 if (unlikely(queued < datalen &&
1059 pageidx == iovec->npages &&
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001060 req->iov_idx < req->data_iovs - 1)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001061 iovec->offset += iov_offset;
1062 iovec = &req->iovs[++req->iov_idx];
Mike Marciniszyn77241052015-07-30 15:17:43 -04001063 iov_offset = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001064 }
1065 }
1066 /*
1067 * The txreq was submitted successfully so we can update
1068 * the counters.
1069 */
1070 req->koffset += datalen;
1071 if (req_opcode(req->info.ctrl) == EXPECTED)
1072 req->tidoffset += datalen;
1073 req->sent += data_sent;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001074 if (req->data_len)
1075 iovec->offset += iov_offset;
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001076 list_add_tail(&tx->txreq.list, &req->txps);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001077 /*
1078 * It is important to increment this here as it is used to
1079 * generate the BTH.PSN and, therefore, can't be bulk-updated
1080 * outside of the loop.
1081 */
1082 tx->seqnum = req->seqnum++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001083 npkts++;
1084 }
1085dosend:
1086 ret = sdma_send_txlist(req->sde, &pq->busy, &req->txps);
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001087 if (list_empty(&req->txps)) {
1088 req->seqsubmitted = req->seqnum;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001089 if (req->seqnum == req->info.npkts) {
1090 set_bit(SDMA_REQ_SEND_DONE, &req->flags);
1091 /*
1092 * The txreq has already been submitted to the HW queue
1093 * so we can free the AHG entry now. Corruption will not
1094 * happen due to the sequential manner in which
1095 * descriptors are processed.
1096 */
1097 if (test_bit(SDMA_REQ_HAVE_AHG, &req->flags))
1098 sdma_ahg_free(req->sde, req->ahg_idx);
1099 }
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001100 } else if (ret > 0) {
1101 req->seqsubmitted += ret;
1102 ret = 0;
1103 }
Mitko Haralanovfaa98b82015-12-08 17:10:11 -05001104 return ret;
1105
Mike Marciniszyn77241052015-07-30 15:17:43 -04001106free_txreq:
1107 sdma_txclean(pq->dd, &tx->txreq);
1108free_tx:
1109 kmem_cache_free(pq->txreq_cache, tx);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001110 return ret;
1111}
1112
1113/*
1114 * How many pages in this iovec element?
1115 */
1116static inline int num_user_pages(const struct iovec *iov)
1117{
Jubin John50e5dcb2016-02-14 20:19:41 -08001118 const unsigned long addr = (unsigned long)iov->iov_base;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001119 const unsigned long len = iov->iov_len;
1120 const unsigned long spage = addr & PAGE_MASK;
1121 const unsigned long epage = (addr + len - 1) & PAGE_MASK;
1122
1123 return 1 + ((epage - spage) >> PAGE_SHIFT);
1124}
1125
Mitko Haralanov5511d782016-03-08 11:15:44 -08001126static u32 sdma_cache_evict(struct hfi1_user_sdma_pkt_q *pq, u32 npages)
1127{
1128 u32 cleared = 0;
1129 struct sdma_mmu_node *node, *ptr;
Mitko Haralanove88c9272016-04-12 10:46:53 -07001130 struct list_head to_evict = LIST_HEAD_INIT(to_evict);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001131
Mitko Haralanove88c9272016-04-12 10:46:53 -07001132 spin_lock(&pq->evict_lock);
Mitko Haralanov5511d782016-03-08 11:15:44 -08001133 list_for_each_entry_safe_reverse(node, ptr, &pq->evict, list) {
1134 /* Make sure that no one is still using the node. */
1135 if (!atomic_read(&node->refcount)) {
Mitko Haralanove88c9272016-04-12 10:46:53 -07001136 set_bit(SDMA_CACHE_NODE_EVICT, &node->flags);
1137 list_del_init(&node->list);
1138 list_add(&node->list, &to_evict);
Mitko Haralanov5511d782016-03-08 11:15:44 -08001139 cleared += node->npages;
Mitko Haralanov5511d782016-03-08 11:15:44 -08001140 if (cleared >= npages)
1141 break;
1142 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001143 }
Mitko Haralanove88c9272016-04-12 10:46:53 -07001144 spin_unlock(&pq->evict_lock);
1145
1146 list_for_each_entry_safe(node, ptr, &to_evict, list)
1147 hfi1_mmu_rb_remove(&pq->sdma_rb_root, &node->rb);
1148
Mitko Haralanov5511d782016-03-08 11:15:44 -08001149 return cleared;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001150}
1151
Mike Marciniszyn77241052015-07-30 15:17:43 -04001152static int pin_vector_pages(struct user_sdma_request *req,
Ira Weiny72720dd2016-07-28 12:27:25 -04001153 struct user_sdma_iovec *iovec)
1154{
Mitko Haralanov5511d782016-03-08 11:15:44 -08001155 int ret = 0, pinned, npages, cleared;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001156 struct page **pages;
1157 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1158 struct sdma_mmu_node *node = NULL;
1159 struct mmu_rb_node *rb_node;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001160
Mitko Haralanovf53af852016-04-12 10:46:47 -07001161 rb_node = hfi1_mmu_rb_extract(&pq->sdma_rb_root,
1162 (unsigned long)iovec->iov.iov_base,
1163 iovec->iov.iov_len);
Mitko Haralanovf19bd642016-04-12 10:45:57 -07001164 if (rb_node && !IS_ERR(rb_node))
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001165 node = container_of(rb_node, struct sdma_mmu_node, rb);
Mitko Haralanovf19bd642016-04-12 10:45:57 -07001166 else
1167 rb_node = NULL;
Mitko Haralanova0d40692015-12-08 17:10:13 -05001168
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001169 if (!node) {
1170 node = kzalloc(sizeof(*node), GFP_KERNEL);
1171 if (!node)
1172 return -ENOMEM;
1173
1174 node->rb.addr = (unsigned long)iovec->iov.iov_base;
Mitko Haralanov5511d782016-03-08 11:15:44 -08001175 node->pq = pq;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001176 atomic_set(&node->refcount, 0);
Mitko Haralanov5511d782016-03-08 11:15:44 -08001177 INIT_LIST_HEAD(&node->list);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001178 }
Mitko Haralanova0d40692015-12-08 17:10:13 -05001179
Mike Marciniszyn77241052015-07-30 15:17:43 -04001180 npages = num_user_pages(&iovec->iov);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001181 if (node->npages < npages) {
1182 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
1183 if (!pages) {
1184 SDMA_DBG(req, "Failed page array alloc");
1185 ret = -ENOMEM;
1186 goto bail;
1187 }
1188 memcpy(pages, node->pages, node->npages * sizeof(*pages));
1189
1190 npages -= node->npages;
Mitko Haralanove88c9272016-04-12 10:46:53 -07001191
1192 /*
1193 * If rb_node is NULL, it means that this is brand new node
1194 * and, therefore not on the eviction list.
1195 * If, however, the rb_node is non-NULL, it means that the
1196 * node is already in RB tree and, therefore on the eviction
1197 * list (nodes are unconditionally inserted in the eviction
1198 * list). In that case, we have to remove the node prior to
1199 * calling the eviction function in order to prevent it from
1200 * freeing this node.
1201 */
1202 if (rb_node) {
1203 spin_lock(&pq->evict_lock);
1204 list_del_init(&node->list);
1205 spin_unlock(&pq->evict_lock);
1206 }
Mitko Haralanov5511d782016-03-08 11:15:44 -08001207retry:
1208 if (!hfi1_can_pin_pages(pq->dd, pq->n_locked, npages)) {
Mitko Haralanov5511d782016-03-08 11:15:44 -08001209 cleared = sdma_cache_evict(pq, npages);
Mitko Haralanov5511d782016-03-08 11:15:44 -08001210 if (cleared >= npages)
1211 goto retry;
1212 }
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001213 pinned = hfi1_acquire_user_pages(
1214 ((unsigned long)iovec->iov.iov_base +
1215 (node->npages * PAGE_SIZE)), npages, 0,
1216 pages + node->npages);
1217 if (pinned < 0) {
1218 kfree(pages);
1219 ret = pinned;
1220 goto bail;
1221 }
1222 if (pinned != npages) {
Mitko Haralanov849e3e92016-04-12 10:46:16 -07001223 unpin_vector_pages(current->mm, pages, node->npages,
1224 pinned);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001225 ret = -EFAULT;
1226 goto bail;
1227 }
1228 kfree(node->pages);
Mitko Haralanovde790932016-04-12 10:46:41 -07001229 node->rb.len = iovec->iov.iov_len;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001230 node->pages = pages;
1231 node->npages += pinned;
1232 npages = node->npages;
Mitko Haralanov5511d782016-03-08 11:15:44 -08001233 spin_lock(&pq->evict_lock);
Mitko Haralanove88c9272016-04-12 10:46:53 -07001234 list_add(&node->list, &pq->evict);
Mitko Haralanov5511d782016-03-08 11:15:44 -08001235 pq->n_locked += pinned;
1236 spin_unlock(&pq->evict_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001237 }
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001238 iovec->pages = node->pages;
1239 iovec->npages = npages;
Mitko Haralanov9565c6a2016-05-19 05:21:18 -07001240 iovec->node = node;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001241
Mitko Haralanovf53af852016-04-12 10:46:47 -07001242 ret = hfi1_mmu_rb_insert(&req->pq->sdma_rb_root, &node->rb);
1243 if (ret) {
1244 spin_lock(&pq->evict_lock);
1245 if (!list_empty(&node->list))
1246 list_del(&node->list);
1247 pq->n_locked -= node->npages;
1248 spin_unlock(&pq->evict_lock);
Dean Luicka383f8e2016-07-28 15:21:16 -04001249 iovec->node = NULL;
Mitko Haralanovf53af852016-04-12 10:46:47 -07001250 goto bail;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001251 }
1252 return 0;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001253bail:
Mitko Haralanovf53af852016-04-12 10:46:47 -07001254 if (rb_node)
1255 unpin_vector_pages(current->mm, node->pages, 0, node->npages);
1256 kfree(node);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001257 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001258}
1259
Mitko Haralanovbd3a8942016-03-08 11:15:33 -08001260static void unpin_vector_pages(struct mm_struct *mm, struct page **pages,
Mitko Haralanov849e3e92016-04-12 10:46:16 -07001261 unsigned start, unsigned npages)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001262{
Ira Weiny639297b2016-07-28 12:27:33 -04001263 hfi1_release_user_pages(mm, pages + start, npages, false);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001264 kfree(pages);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001265}
1266
1267static int check_header_template(struct user_sdma_request *req,
1268 struct hfi1_pkt_header *hdr, u32 lrhlen,
1269 u32 datalen)
1270{
1271 /*
1272 * Perform safety checks for any type of packet:
1273 * - transfer size is multiple of 64bytes
Ira Weinyc4929802016-07-27 21:08:42 -04001274 * - packet length is multiple of 4 bytes
Mike Marciniszyn77241052015-07-30 15:17:43 -04001275 * - packet length is not larger than MTU size
1276 *
1277 * These checks are only done for the first packet of the
1278 * transfer since the header is "given" to us by user space.
1279 * For the remainder of the packets we compute the values.
1280 */
Ira Weinyc4929802016-07-27 21:08:42 -04001281 if (req->info.fragsize % PIO_BLOCK_SIZE || lrhlen & 0x3 ||
Mike Marciniszyn77241052015-07-30 15:17:43 -04001282 lrhlen > get_lrh_len(*hdr, req->info.fragsize))
1283 return -EINVAL;
1284
1285 if (req_opcode(req->info.ctrl) == EXPECTED) {
1286 /*
1287 * The header is checked only on the first packet. Furthermore,
1288 * we ensure that at least one TID entry is copied when the
1289 * request is submitted. Therefore, we don't have to verify that
1290 * tididx points to something sane.
1291 */
1292 u32 tidval = req->tids[req->tididx],
1293 tidlen = EXP_TID_GET(tidval, LEN) * PAGE_SIZE,
1294 tididx = EXP_TID_GET(tidval, IDX),
1295 tidctrl = EXP_TID_GET(tidval, CTRL),
1296 tidoff;
1297 __le32 kval = hdr->kdeth.ver_tid_offset;
1298
1299 tidoff = KDETH_GET(kval, OFFSET) *
1300 (KDETH_GET(req->hdr.kdeth.ver_tid_offset, OM) ?
1301 KDETH_OM_LARGE : KDETH_OM_SMALL);
1302 /*
1303 * Expected receive packets have the following
1304 * additional checks:
1305 * - offset is not larger than the TID size
1306 * - TIDCtrl values match between header and TID array
1307 * - TID indexes match between header and TID array
1308 */
1309 if ((tidoff + datalen > tidlen) ||
1310 KDETH_GET(kval, TIDCTRL) != tidctrl ||
1311 KDETH_GET(kval, TID) != tididx)
1312 return -EINVAL;
1313 }
1314 return 0;
1315}
1316
1317/*
1318 * Correctly set the BTH.PSN field based on type of
1319 * transfer - eager packets can just increment the PSN but
1320 * expected packets encode generation and sequence in the
1321 * BTH.PSN field so just incrementing will result in errors.
1322 */
1323static inline u32 set_pkt_bth_psn(__be32 bthpsn, u8 expct, u32 frags)
1324{
1325 u32 val = be32_to_cpu(bthpsn),
1326 mask = (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffffull :
1327 0xffffffull),
1328 psn = val & mask;
1329 if (expct)
1330 psn = (psn & ~BTH_SEQ_MASK) | ((psn + frags) & BTH_SEQ_MASK);
1331 else
1332 psn = psn + frags;
1333 return psn & mask;
1334}
1335
1336static int set_txreq_header(struct user_sdma_request *req,
1337 struct user_sdma_txreq *tx, u32 datalen)
1338{
1339 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1340 struct hfi1_pkt_header *hdr = &tx->hdr;
1341 u16 pbclen;
1342 int ret;
Ira Weinyc4929802016-07-27 21:08:42 -04001343 u32 tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(datalen));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001344
1345 /* Copy the header template to the request before modification */
1346 memcpy(hdr, &req->hdr, sizeof(*hdr));
1347
1348 /*
1349 * Check if the PBC and LRH length are mismatched. If so
1350 * adjust both in the header.
1351 */
1352 pbclen = le16_to_cpu(hdr->pbc[0]);
1353 if (PBC2LRH(pbclen) != lrhlen) {
1354 pbclen = (pbclen & 0xf000) | LRH2PBC(lrhlen);
1355 hdr->pbc[0] = cpu_to_le16(pbclen);
1356 hdr->lrh[2] = cpu_to_be16(lrhlen >> 2);
1357 /*
1358 * Third packet
1359 * This is the first packet in the sequence that has
1360 * a "static" size that can be used for the rest of
1361 * the packets (besides the last one).
1362 */
1363 if (unlikely(req->seqnum == 2)) {
1364 /*
1365 * From this point on the lengths in both the
1366 * PBC and LRH are the same until the last
1367 * packet.
1368 * Adjust the template so we don't have to update
1369 * every packet
1370 */
1371 req->hdr.pbc[0] = hdr->pbc[0];
1372 req->hdr.lrh[2] = hdr->lrh[2];
1373 }
1374 }
1375 /*
1376 * We only have to modify the header if this is not the
1377 * first packet in the request. Otherwise, we use the
1378 * header given to us.
1379 */
1380 if (unlikely(!req->seqnum)) {
1381 ret = check_header_template(req, hdr, lrhlen, datalen);
1382 if (ret)
1383 return ret;
1384 goto done;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001385 }
1386
1387 hdr->bth[2] = cpu_to_be32(
1388 set_pkt_bth_psn(hdr->bth[2],
1389 (req_opcode(req->info.ctrl) == EXPECTED),
1390 req->seqnum));
1391
1392 /* Set ACK request on last packet */
Mitko Haralanovb9fb63182015-10-26 10:28:37 -04001393 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_LAST_PKT))
Jubin John8638b772016-02-14 20:19:24 -08001394 hdr->bth[2] |= cpu_to_be32(1UL << 31);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001395
1396 /* Set the new offset */
1397 hdr->kdeth.swdata[6] = cpu_to_le32(req->koffset);
1398 /* Expected packets have to fill in the new TID information */
1399 if (req_opcode(req->info.ctrl) == EXPECTED) {
1400 tidval = req->tids[req->tididx];
1401 /*
1402 * If the offset puts us at the end of the current TID,
1403 * advance everything.
1404 */
1405 if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
1406 PAGE_SIZE)) {
1407 req->tidoffset = 0;
Jubin John4d114fd2016-02-14 20:21:43 -08001408 /*
1409 * Since we don't copy all the TIDs, all at once,
1410 * we have to check again.
1411 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001412 if (++req->tididx > req->n_tids - 1 ||
1413 !req->tids[req->tididx]) {
1414 return -EINVAL;
1415 }
1416 tidval = req->tids[req->tididx];
1417 }
1418 req->omfactor = EXP_TID_GET(tidval, LEN) * PAGE_SIZE >=
1419 KDETH_OM_MAX_SIZE ? KDETH_OM_LARGE : KDETH_OM_SMALL;
1420 /* Set KDETH.TIDCtrl based on value for this TID. */
1421 KDETH_SET(hdr->kdeth.ver_tid_offset, TIDCTRL,
1422 EXP_TID_GET(tidval, CTRL));
1423 /* Set KDETH.TID based on value for this TID */
1424 KDETH_SET(hdr->kdeth.ver_tid_offset, TID,
1425 EXP_TID_GET(tidval, IDX));
1426 /* Clear KDETH.SH only on the last packet */
Mitko Haralanovb9fb63182015-10-26 10:28:37 -04001427 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_LAST_PKT))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001428 KDETH_SET(hdr->kdeth.ver_tid_offset, SH, 0);
1429 /*
1430 * Set the KDETH.OFFSET and KDETH.OM based on size of
1431 * transfer.
1432 */
1433 SDMA_DBG(req, "TID offset %ubytes %uunits om%u",
1434 req->tidoffset, req->tidoffset / req->omfactor,
Bart Van Assche55c406482016-06-03 12:11:16 -07001435 req->omfactor != KDETH_OM_SMALL);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001436 KDETH_SET(hdr->kdeth.ver_tid_offset, OFFSET,
1437 req->tidoffset / req->omfactor);
1438 KDETH_SET(hdr->kdeth.ver_tid_offset, OM,
Bart Van Assche55c406482016-06-03 12:11:16 -07001439 req->omfactor != KDETH_OM_SMALL);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001440 }
1441done:
1442 trace_hfi1_sdma_user_header(pq->dd, pq->ctxt, pq->subctxt,
1443 req->info.comp_idx, hdr, tidval);
1444 return sdma_txadd_kvaddr(pq->dd, &tx->txreq, hdr, sizeof(*hdr));
1445}
1446
1447static int set_txreq_header_ahg(struct user_sdma_request *req,
1448 struct user_sdma_txreq *tx, u32 len)
1449{
1450 int diff = 0;
1451 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1452 struct hfi1_pkt_header *hdr = &req->hdr;
1453 u16 pbclen = le16_to_cpu(hdr->pbc[0]);
Ira Weinyc4929802016-07-27 21:08:42 -04001454 u32 val32, tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(len));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001455
1456 if (PBC2LRH(pbclen) != lrhlen) {
1457 /* PBC.PbcLengthDWs */
1458 AHG_HEADER_SET(req->ahg, diff, 0, 0, 12,
1459 cpu_to_le16(LRH2PBC(lrhlen)));
1460 /* LRH.PktLen (we need the full 16 bits due to byte swap) */
1461 AHG_HEADER_SET(req->ahg, diff, 3, 0, 16,
1462 cpu_to_be16(lrhlen >> 2));
1463 }
1464
1465 /*
1466 * Do the common updates
1467 */
1468 /* BTH.PSN and BTH.A */
1469 val32 = (be32_to_cpu(hdr->bth[2]) + req->seqnum) &
1470 (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffff : 0xffffff);
Mitko Haralanovb9fb63182015-10-26 10:28:37 -04001471 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_LAST_PKT))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001472 val32 |= 1UL << 31;
1473 AHG_HEADER_SET(req->ahg, diff, 6, 0, 16, cpu_to_be16(val32 >> 16));
1474 AHG_HEADER_SET(req->ahg, diff, 6, 16, 16, cpu_to_be16(val32 & 0xffff));
1475 /* KDETH.Offset */
1476 AHG_HEADER_SET(req->ahg, diff, 15, 0, 16,
1477 cpu_to_le16(req->koffset & 0xffff));
1478 AHG_HEADER_SET(req->ahg, diff, 15, 16, 16,
1479 cpu_to_le16(req->koffset >> 16));
1480 if (req_opcode(req->info.ctrl) == EXPECTED) {
1481 __le16 val;
1482
1483 tidval = req->tids[req->tididx];
1484
1485 /*
1486 * If the offset puts us at the end of the current TID,
1487 * advance everything.
1488 */
1489 if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
1490 PAGE_SIZE)) {
1491 req->tidoffset = 0;
Jubin John4d114fd2016-02-14 20:21:43 -08001492 /*
1493 * Since we don't copy all the TIDs, all at once,
1494 * we have to check again.
1495 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001496 if (++req->tididx > req->n_tids - 1 ||
1497 !req->tids[req->tididx]) {
1498 return -EINVAL;
1499 }
1500 tidval = req->tids[req->tididx];
1501 }
1502 req->omfactor = ((EXP_TID_GET(tidval, LEN) *
1503 PAGE_SIZE) >=
1504 KDETH_OM_MAX_SIZE) ? KDETH_OM_LARGE :
1505 KDETH_OM_SMALL;
1506 /* KDETH.OM and KDETH.OFFSET (TID) */
1507 AHG_HEADER_SET(req->ahg, diff, 7, 0, 16,
1508 ((!!(req->omfactor - KDETH_OM_SMALL)) << 15 |
1509 ((req->tidoffset / req->omfactor) & 0x7fff)));
1510 /* KDETH.TIDCtrl, KDETH.TID */
1511 val = cpu_to_le16(((EXP_TID_GET(tidval, CTRL) & 0x3) << 10) |
1512 (EXP_TID_GET(tidval, IDX) & 0x3ff));
1513 /* Clear KDETH.SH on last packet */
Mitko Haralanovb9fb63182015-10-26 10:28:37 -04001514 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_LAST_PKT)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001515 val |= cpu_to_le16(KDETH_GET(hdr->kdeth.ver_tid_offset,
1516 INTR) >> 16);
1517 val &= cpu_to_le16(~(1U << 13));
1518 AHG_HEADER_SET(req->ahg, diff, 7, 16, 14, val);
Jubin Johne4909742016-02-14 20:22:00 -08001519 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001520 AHG_HEADER_SET(req->ahg, diff, 7, 16, 12, val);
Jubin Johne4909742016-02-14 20:22:00 -08001521 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001522 }
1523
1524 trace_hfi1_sdma_user_header_ahg(pq->dd, pq->ctxt, pq->subctxt,
1525 req->info.comp_idx, req->sde->this_idx,
1526 req->ahg_idx, req->ahg, diff, tidval);
1527 return diff;
1528}
1529
Mitko Haralanova0d40692015-12-08 17:10:13 -05001530/*
1531 * SDMA tx request completion callback. Called when the SDMA progress
1532 * state machine gets notification that the SDMA descriptors for this
1533 * tx request have been processed by the DMA engine. Called in
1534 * interrupt context.
1535 */
Mike Marciniszyna545f532016-02-14 12:45:53 -08001536static void user_sdma_txreq_cb(struct sdma_txreq *txreq, int status)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001537{
1538 struct user_sdma_txreq *tx =
1539 container_of(txreq, struct user_sdma_txreq, txreq);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001540 struct user_sdma_request *req;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001541 struct hfi1_user_sdma_pkt_q *pq;
1542 struct hfi1_user_sdma_comp_q *cq;
1543 u16 idx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001544
Mitko Haralanova0d40692015-12-08 17:10:13 -05001545 if (!tx->req)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001546 return;
1547
Mitko Haralanova0d40692015-12-08 17:10:13 -05001548 req = tx->req;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001549 pq = req->pq;
1550 cq = req->cq;
Mitko Haralanovb9fb63182015-10-26 10:28:37 -04001551
Mike Marciniszyn77241052015-07-30 15:17:43 -04001552 if (status != SDMA_TXREQ_S_OK) {
Mitko Haralanova0d40692015-12-08 17:10:13 -05001553 SDMA_DBG(req, "SDMA completion with error %d",
1554 status);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001555 set_bit(SDMA_REQ_HAS_ERROR, &req->flags);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001556 }
1557
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001558 req->seqcomp = tx->seqnum;
1559 kmem_cache_free(pq->txreq_cache, tx);
1560 tx = NULL;
1561
1562 idx = req->info.comp_idx;
1563 if (req->status == -1 && status == SDMA_TXREQ_S_OK) {
1564 if (req->seqcomp == req->info.npkts - 1) {
1565 req->status = 0;
1566 user_sdma_free_request(req, false);
1567 pq_update(pq);
1568 set_comp_state(pq, cq, idx, COMPLETE, 0);
1569 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001570 } else {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001571 if (status != SDMA_TXREQ_S_OK)
1572 req->status = status;
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001573 if (req->seqcomp == (ACCESS_ONCE(req->seqsubmitted) - 1) &&
1574 (test_bit(SDMA_REQ_SEND_DONE, &req->flags) ||
1575 test_bit(SDMA_REQ_DONE_ERROR, &req->flags))) {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001576 user_sdma_free_request(req, false);
1577 pq_update(pq);
1578 set_comp_state(pq, cq, idx, ERROR, req->status);
1579 }
Mitko Haralanova0d40692015-12-08 17:10:13 -05001580 }
1581}
1582
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001583static inline void pq_update(struct hfi1_user_sdma_pkt_q *pq)
Mitko Haralanova0d40692015-12-08 17:10:13 -05001584{
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001585 if (atomic_dec_and_test(&pq->n_reqs)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001586 xchg(&pq->state, SDMA_PKT_Q_INACTIVE);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001587 wake_up(&pq->wait);
1588 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001589}
1590
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001591static void user_sdma_free_request(struct user_sdma_request *req, bool unpin)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001592{
1593 if (!list_empty(&req->txps)) {
1594 struct sdma_txreq *t, *p;
1595
1596 list_for_each_entry_safe(t, p, &req->txps, list) {
1597 struct user_sdma_txreq *tx =
1598 container_of(t, struct user_sdma_txreq, txreq);
1599 list_del_init(&t->list);
1600 sdma_txclean(req->pq->dd, t);
1601 kmem_cache_free(req->pq->txreq_cache, tx);
1602 }
1603 }
1604 if (req->data_iovs) {
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001605 struct sdma_mmu_node *node;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001606 int i;
1607
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001608 for (i = 0; i < req->data_iovs; i++) {
Mitko Haralanov9565c6a2016-05-19 05:21:18 -07001609 node = req->iovs[i].node;
1610 if (!node)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001611 continue;
1612
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001613 if (unpin)
1614 hfi1_mmu_rb_remove(&req->pq->sdma_rb_root,
1615 &node->rb);
1616 else
1617 atomic_dec(&node->refcount);
1618 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001619 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001620 kfree(req->tids);
Dean Luick7b3256e2016-07-28 15:21:18 -04001621 clear_bit(req->info.comp_idx, req->pq->req_in_use);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001622}
1623
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001624static inline void set_comp_state(struct hfi1_user_sdma_pkt_q *pq,
1625 struct hfi1_user_sdma_comp_q *cq,
1626 u16 idx, enum hfi1_sdma_comp_state state,
1627 int ret)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001628{
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001629 hfi1_cdbg(SDMA, "[%u:%u:%u:%u] Setting completion status %u %d",
1630 pq->dd->unit, pq->ctxt, pq->subctxt, idx, state, ret);
1631 cq->comps[idx].status = state;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001632 if (state == ERROR)
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001633 cq->comps[idx].errcode = -ret;
1634 trace_hfi1_sdma_user_completion(pq->dd, pq->ctxt, pq->subctxt,
1635 idx, state, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001636}
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001637
1638static bool sdma_rb_filter(struct mmu_rb_node *node, unsigned long addr,
1639 unsigned long len)
1640{
1641 return (bool)(node->addr == addr);
1642}
1643
1644static int sdma_rb_insert(struct rb_root *root, struct mmu_rb_node *mnode)
1645{
1646 struct sdma_mmu_node *node =
1647 container_of(mnode, struct sdma_mmu_node, rb);
1648
1649 atomic_inc(&node->refcount);
1650 return 0;
1651}
1652
1653static void sdma_rb_remove(struct rb_root *root, struct mmu_rb_node *mnode,
Mitko Haralanovf19bd642016-04-12 10:45:57 -07001654 struct mm_struct *mm)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001655{
1656 struct sdma_mmu_node *node =
1657 container_of(mnode, struct sdma_mmu_node, rb);
1658
Mitko Haralanov5511d782016-03-08 11:15:44 -08001659 spin_lock(&node->pq->evict_lock);
Mitko Haralanove88c9272016-04-12 10:46:53 -07001660 /*
1661 * We've been called by the MMU notifier but this node has been
1662 * scheduled for eviction. The eviction function will take care
1663 * of freeing this node.
1664 * We have to take the above lock first because we are racing
1665 * against the setting of the bit in the eviction function.
1666 */
1667 if (mm && test_bit(SDMA_CACHE_NODE_EVICT, &node->flags)) {
1668 spin_unlock(&node->pq->evict_lock);
1669 return;
1670 }
1671
Mitko Haralanov4787bc52016-04-12 10:46:23 -07001672 if (!list_empty(&node->list))
1673 list_del(&node->list);
Mitko Haralanov5511d782016-03-08 11:15:44 -08001674 node->pq->n_locked -= node->npages;
1675 spin_unlock(&node->pq->evict_lock);
1676
Mitko Haralanovf19bd642016-04-12 10:45:57 -07001677 /*
1678 * If mm is set, we are being called by the MMU notifier and we
1679 * should not pass a mm_struct to unpin_vector_page(). This is to
1680 * prevent a deadlock when hfi1_release_user_pages() attempts to
1681 * take the mmap_sem, which the MMU notifier has already taken.
1682 */
Mitko Haralanov849e3e92016-04-12 10:46:16 -07001683 unpin_vector_pages(mm ? NULL : current->mm, node->pages, 0,
1684 node->npages);
Mitko Haralanovbd3a8942016-03-08 11:15:33 -08001685 /*
1686 * If called by the MMU notifier, we have to adjust the pinned
1687 * page count ourselves.
1688 */
Mitko Haralanovf19bd642016-04-12 10:45:57 -07001689 if (mm)
1690 mm->pinned_vm -= node->npages;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001691 kfree(node);
1692}
1693
1694static int sdma_rb_invalidate(struct rb_root *root, struct mmu_rb_node *mnode)
1695{
1696 struct sdma_mmu_node *node =
1697 container_of(mnode, struct sdma_mmu_node, rb);
1698
1699 if (!atomic_read(&node->refcount))
1700 return 1;
1701 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001702}