blob: 6b72267df9e722b8dac7ae46c1d7986e3a00e259 [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001/*
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -07002 * Copyright(c) 2015 - 2017 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
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -0700146#define KDETH_OM_SMALL_SHIFT 2
Mike Marciniszyn77241052015-07-30 15:17:43 -0400147#define KDETH_OM_LARGE 64
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -0700148#define KDETH_OM_LARGE_SHIFT 6
Mike Marciniszyn77241052015-07-30 15:17:43 -0400149#define KDETH_OM_MAX_SIZE (1 << ((KDETH_OM_LARGE / KDETH_OM_SMALL) + 1))
150
Jakub Pawlake7301392016-12-07 19:32:41 -0800151/* Tx request flag bits */
152#define TXREQ_FLAGS_REQ_ACK BIT(0) /* Set the ACK bit in the header */
153#define TXREQ_FLAGS_REQ_DISABLE_SH BIT(1) /* Disable header suppression */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400154
Dean Luick7b3256e2016-07-28 15:21:18 -0400155/* SDMA request flag bits */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400156#define SDMA_REQ_FOR_THREAD 1
157#define SDMA_REQ_SEND_DONE 2
Sebastian Sanchez780a4c12017-05-04 05:14:51 -0700158#define SDMA_REQ_HAS_ERROR 3
159#define SDMA_REQ_DONE_ERROR 4
Mike Marciniszyn77241052015-07-30 15:17:43 -0400160
Sunny Kumarcb326492015-11-06 10:06:43 +0530161#define SDMA_PKT_Q_INACTIVE BIT(0)
162#define SDMA_PKT_Q_ACTIVE BIT(1)
163#define SDMA_PKT_Q_DEFERRED BIT(2)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400164
165/*
166 * Maximum retry attempts to submit a TX request
167 * before putting the process to sleep.
168 */
169#define MAX_DEFER_RETRY_COUNT 1
170
171static unsigned initial_pkt_count = 8;
172
173#define SDMA_IOWAIT_TIMEOUT 1000 /* in milliseconds */
174
Mitko Haralanov9565c6a2016-05-19 05:21:18 -0700175struct sdma_mmu_node;
176
Mike Marciniszyn77241052015-07-30 15:17:43 -0400177struct user_sdma_iovec {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800178 struct list_head list;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400179 struct iovec iov;
180 /* number of pages in this vector */
181 unsigned npages;
182 /* array of pinned pages for this vector */
183 struct page **pages;
Jubin John4d114fd2016-02-14 20:21:43 -0800184 /*
185 * offset into the virtual address space of the vector at
186 * which we last left off.
187 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400188 u64 offset;
Mitko Haralanov9565c6a2016-05-19 05:21:18 -0700189 struct sdma_mmu_node *node;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400190};
191
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800192struct sdma_mmu_node {
193 struct mmu_rb_node rb;
Mitko Haralanov5511d782016-03-08 11:15:44 -0800194 struct hfi1_user_sdma_pkt_q *pq;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800195 atomic_t refcount;
196 struct page **pages;
197 unsigned npages;
Dean Luickb7df1922016-07-28 15:21:23 -0400198};
199
200/* evict operation argument */
201struct evict_data {
202 u32 cleared; /* count evicted so far */
203 u32 target; /* target count to evict */
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800204};
205
Mike Marciniszyn77241052015-07-30 15:17:43 -0400206struct user_sdma_request {
207 struct sdma_req_info info;
208 struct hfi1_user_sdma_pkt_q *pq;
209 struct hfi1_user_sdma_comp_q *cq;
210 /* This is the original header from user space */
211 struct hfi1_pkt_header hdr;
212 /*
213 * Pointer to the SDMA engine for this request.
214 * Since different request could be on different VLs,
215 * each request will need it's own engine pointer.
216 */
217 struct sdma_engine *sde;
Sebastian Sanchez780a4c12017-05-04 05:14:51 -0700218 s8 ahg_idx;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400219 u32 ahg[9];
220 /*
221 * KDETH.Offset (Eager) field
222 * We need to remember the initial value so the headers
223 * can be updated properly.
224 */
225 u32 koffset;
226 /*
227 * KDETH.OFFSET (TID) field
228 * The offset can cover multiple packets, depending on the
229 * size of the TID entry.
230 */
231 u32 tidoffset;
232 /*
Mike Marciniszyn77241052015-07-30 15:17:43 -0400233 * We copy the iovs for this request (based on
234 * info.iovcnt). These are only the data vectors
235 */
236 unsigned data_iovs;
237 /* total length of the data in the request */
238 u32 data_len;
239 /* progress index moving along the iovs array */
240 unsigned iov_idx;
241 struct user_sdma_iovec iovs[MAX_VECTORS_PER_REQ];
242 /* number of elements copied to the tids array */
243 u16 n_tids;
244 /* TID array values copied from the tid_iov vector */
245 u32 *tids;
246 u16 tididx;
247 u32 sent;
248 u64 seqnum;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800249 u64 seqcomp;
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -0800250 u64 seqsubmitted;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400251 struct list_head txps;
252 unsigned long flags;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500253 /* status of the last txreq completed */
254 int status;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400255};
256
Mitko Haralanovb9fb63182015-10-26 10:28:37 -0400257/*
258 * A single txreq could span up to 3 physical pages when the MTU
259 * is sufficiently large (> 4K). Each of the IOV pointers also
260 * needs it's own set of flags so the vector has been handled
261 * independently of each other.
262 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400263struct user_sdma_txreq {
264 /* Packet header for the txreq */
265 struct hfi1_pkt_header hdr;
266 struct sdma_txreq txreq;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500267 struct list_head list;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400268 struct user_sdma_request *req;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400269 u16 flags;
270 unsigned busycount;
271 u64 seqnum;
272};
273
274#define SDMA_DBG(req, fmt, ...) \
275 hfi1_cdbg(SDMA, "[%u:%u:%u:%u] " fmt, (req)->pq->dd->unit, \
276 (req)->pq->ctxt, (req)->pq->subctxt, (req)->info.comp_idx, \
277 ##__VA_ARGS__)
278#define SDMA_Q_DBG(pq, fmt, ...) \
279 hfi1_cdbg(SDMA, "[%u:%u:%u] " fmt, (pq)->dd->unit, (pq)->ctxt, \
280 (pq)->subctxt, ##__VA_ARGS__)
281
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -0700282static int user_sdma_send_pkts(struct user_sdma_request *req,
283 unsigned maxpkts);
284static int num_user_pages(const struct iovec *iov);
285static void user_sdma_txreq_cb(struct sdma_txreq *txreq, int status);
286static inline void pq_update(struct hfi1_user_sdma_pkt_q *pq);
287static void user_sdma_free_request(struct user_sdma_request *req, bool unpin);
288static int pin_vector_pages(struct user_sdma_request *req,
289 struct user_sdma_iovec *iovec);
290static void unpin_vector_pages(struct mm_struct *mm, struct page **pages,
291 unsigned start, unsigned npages);
292static int check_header_template(struct user_sdma_request *req,
293 struct hfi1_pkt_header *hdr, u32 lrhlen,
294 u32 datalen);
295static int set_txreq_header(struct user_sdma_request *req,
296 struct user_sdma_txreq *tx, u32 datalen);
297static int set_txreq_header_ahg(struct user_sdma_request *req,
298 struct user_sdma_txreq *tx, u32 len);
299static inline void set_comp_state(struct hfi1_user_sdma_pkt_q *pq,
300 struct hfi1_user_sdma_comp_q *cq,
301 u16 idx, enum hfi1_sdma_comp_state state,
302 int ret);
303static inline u32 set_pkt_bth_psn(__be32 bthpsn, u8 expct, u32 frags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400304static inline u32 get_lrh_len(struct hfi1_pkt_header, u32 len);
305
306static int defer_packet_queue(
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -0700307 struct sdma_engine *sde,
308 struct iowait *wait,
309 struct sdma_txreq *txreq,
310 unsigned int seq);
311static void activate_packet_queue(struct iowait *wait, int reason);
312static bool sdma_rb_filter(struct mmu_rb_node *node, unsigned long addr,
313 unsigned long len);
314static int sdma_rb_insert(void *arg, struct mmu_rb_node *mnode);
Dean Luickb7df1922016-07-28 15:21:23 -0400315static int sdma_rb_evict(void *arg, struct mmu_rb_node *mnode,
316 void *arg2, bool *stop);
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -0700317static void sdma_rb_remove(void *arg, struct mmu_rb_node *mnode);
318static int sdma_rb_invalidate(void *arg, struct mmu_rb_node *mnode);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800319
320static struct mmu_rb_ops sdma_rb_ops = {
321 .filter = sdma_rb_filter,
322 .insert = sdma_rb_insert,
Dean Luickb7df1922016-07-28 15:21:23 -0400323 .evict = sdma_rb_evict,
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800324 .remove = sdma_rb_remove,
325 .invalidate = sdma_rb_invalidate
326};
Mike Marciniszyn77241052015-07-30 15:17:43 -0400327
Mike Marciniszyn77241052015-07-30 15:17:43 -0400328static int defer_packet_queue(
329 struct sdma_engine *sde,
330 struct iowait *wait,
331 struct sdma_txreq *txreq,
332 unsigned seq)
333{
334 struct hfi1_user_sdma_pkt_q *pq =
335 container_of(wait, struct hfi1_user_sdma_pkt_q, busy);
336 struct hfi1_ibdev *dev = &pq->dd->verbs_dev;
337 struct user_sdma_txreq *tx =
338 container_of(txreq, struct user_sdma_txreq, txreq);
339
340 if (sdma_progress(sde, seq, txreq)) {
341 if (tx->busycount++ < MAX_DEFER_RETRY_COUNT)
342 goto eagain;
343 }
344 /*
345 * We are assuming that if the list is enqueued somewhere, it
346 * is to the dmawait list since that is the only place where
347 * it is supposed to be enqueued.
348 */
349 xchg(&pq->state, SDMA_PKT_Q_DEFERRED);
350 write_seqlock(&dev->iowait_lock);
351 if (list_empty(&pq->busy.list))
352 list_add_tail(&pq->busy.list, &sde->dmawait);
353 write_sequnlock(&dev->iowait_lock);
354 return -EBUSY;
355eagain:
356 return -EAGAIN;
357}
358
359static void activate_packet_queue(struct iowait *wait, int reason)
360{
361 struct hfi1_user_sdma_pkt_q *pq =
362 container_of(wait, struct hfi1_user_sdma_pkt_q, busy);
363 xchg(&pq->state, SDMA_PKT_Q_ACTIVE);
364 wake_up(&wait->wait_dma);
365};
366
367static void sdma_kmem_cache_ctor(void *obj)
368{
Janani Ravichandran16ccad02016-02-25 15:08:17 -0500369 struct user_sdma_txreq *tx = obj;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400370
371 memset(tx, 0, sizeof(*tx));
372}
373
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700374int hfi1_user_sdma_alloc_queues(struct hfi1_ctxtdata *uctxt,
375 struct hfi1_filedata *fd)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400376{
377 int ret = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400378 char buf[64];
379 struct hfi1_devdata *dd;
380 struct hfi1_user_sdma_comp_q *cq;
381 struct hfi1_user_sdma_pkt_q *pq;
382 unsigned long flags;
383
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700384 if (!uctxt || !fd) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400385 ret = -EBADF;
386 goto done;
387 }
388
389 if (!hfi1_sdma_comp_ring_size) {
390 ret = -EINVAL;
391 goto done;
392 }
393
394 dd = uctxt->dd;
395
396 pq = kzalloc(sizeof(*pq), GFP_KERNEL);
Alison Schofield806e6e12015-10-12 14:28:36 -0700397 if (!pq)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400398 goto pq_nomem;
Alison Schofield806e6e12015-10-12 14:28:36 -0700399
Markus Elfring147d84e2017-02-09 16:06:12 +0100400 pq->reqs = kcalloc(hfi1_sdma_comp_ring_size,
401 sizeof(*pq->reqs),
402 GFP_KERNEL);
Alison Schofield806e6e12015-10-12 14:28:36 -0700403 if (!pq->reqs)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400404 goto pq_reqs_nomem;
Alison Schofield806e6e12015-10-12 14:28:36 -0700405
Markus Elfring147d84e2017-02-09 16:06:12 +0100406 pq->req_in_use = kcalloc(BITS_TO_LONGS(hfi1_sdma_comp_ring_size),
407 sizeof(*pq->req_in_use),
408 GFP_KERNEL);
Dean Luick7b3256e2016-07-28 15:21:18 -0400409 if (!pq->req_in_use)
410 goto pq_reqs_no_in_use;
411
Mike Marciniszyn77241052015-07-30 15:17:43 -0400412 INIT_LIST_HEAD(&pq->list);
413 pq->dd = dd;
414 pq->ctxt = uctxt->ctxt;
Ira Weiny9e10af42015-10-30 18:58:40 -0400415 pq->subctxt = fd->subctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400416 pq->n_max_reqs = hfi1_sdma_comp_ring_size;
417 pq->state = SDMA_PKT_Q_INACTIVE;
418 atomic_set(&pq->n_reqs, 0);
Mitko Haralanova0d40692015-12-08 17:10:13 -0500419 init_waitqueue_head(&pq->wait);
Dean Luickb7df1922016-07-28 15:21:23 -0400420 atomic_set(&pq->n_locked, 0);
Ira Weiny3faa3d92016-07-28 15:21:19 -0400421 pq->mm = fd->mm;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400422
423 iowait_init(&pq->busy, 0, NULL, defer_packet_queue,
Mike Marciniszyna545f532016-02-14 12:45:53 -0800424 activate_packet_queue, NULL);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400425 pq->reqidx = 0;
426 snprintf(buf, 64, "txreq-kmem-cache-%u-%u-%u", dd->unit, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -0400427 fd->subctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400428 pq->txreq_cache = kmem_cache_create(buf,
429 sizeof(struct user_sdma_txreq),
430 L1_CACHE_BYTES,
431 SLAB_HWCACHE_ALIGN,
432 sdma_kmem_cache_ctor);
433 if (!pq->txreq_cache) {
434 dd_dev_err(dd, "[%u] Failed to allocate TxReq cache\n",
435 uctxt->ctxt);
436 goto pq_txreq_nomem;
437 }
Ira Weiny9e10af42015-10-30 18:58:40 -0400438 fd->pq = pq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400439 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
Alison Schofield806e6e12015-10-12 14:28:36 -0700440 if (!cq)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400441 goto cq_nomem;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400442
Markus Elfringe036c202017-02-10 08:50:45 +0100443 cq->comps = vmalloc_user(PAGE_ALIGN(sizeof(*cq->comps)
444 * hfi1_sdma_comp_ring_size));
Alison Schofield806e6e12015-10-12 14:28:36 -0700445 if (!cq->comps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400446 goto cq_comps_nomem;
Alison Schofield806e6e12015-10-12 14:28:36 -0700447
Mike Marciniszyn77241052015-07-30 15:17:43 -0400448 cq->nentries = hfi1_sdma_comp_ring_size;
Ira Weiny9e10af42015-10-30 18:58:40 -0400449 fd->cq = cq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400450
Dean Luickb85ced92016-07-28 15:21:24 -0400451 ret = hfi1_mmu_rb_register(pq, pq->mm, &sdma_rb_ops, dd->pport->hfi1_wq,
452 &pq->handler);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800453 if (ret) {
454 dd_dev_err(dd, "Failed to register with MMU %d", ret);
455 goto done;
456 }
457
Mike Marciniszyn77241052015-07-30 15:17:43 -0400458 spin_lock_irqsave(&uctxt->sdma_qlock, flags);
459 list_add(&pq->list, &uctxt->sdma_queues);
460 spin_unlock_irqrestore(&uctxt->sdma_qlock, flags);
461 goto done;
462
463cq_comps_nomem:
464 kfree(cq);
465cq_nomem:
466 kmem_cache_destroy(pq->txreq_cache);
467pq_txreq_nomem:
Dean Luick7b3256e2016-07-28 15:21:18 -0400468 kfree(pq->req_in_use);
469pq_reqs_no_in_use:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400470 kfree(pq->reqs);
471pq_reqs_nomem:
472 kfree(pq);
Ira Weiny9e10af42015-10-30 18:58:40 -0400473 fd->pq = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400474pq_nomem:
475 ret = -ENOMEM;
476done:
477 return ret;
478}
479
480int hfi1_user_sdma_free_queues(struct hfi1_filedata *fd)
481{
482 struct hfi1_ctxtdata *uctxt = fd->uctxt;
483 struct hfi1_user_sdma_pkt_q *pq;
484 unsigned long flags;
485
486 hfi1_cdbg(SDMA, "[%u:%u:%u] Freeing user SDMA queues", uctxt->dd->unit,
487 uctxt->ctxt, fd->subctxt);
488 pq = fd->pq;
489 if (pq) {
Dean Luicke0b09ac2016-07-28 15:21:20 -0400490 if (pq->handler)
491 hfi1_mmu_rb_unregister(pq->handler);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400492 spin_lock_irqsave(&uctxt->sdma_qlock, flags);
493 if (!list_empty(&pq->list))
494 list_del_init(&pq->list);
495 spin_unlock_irqrestore(&uctxt->sdma_qlock, flags);
496 iowait_sdma_drain(&pq->busy);
Mitko Haralanova0d40692015-12-08 17:10:13 -0500497 /* Wait until all requests have been freed. */
498 wait_event_interruptible(
499 pq->wait,
500 (ACCESS_ONCE(pq->state) == SDMA_PKT_Q_INACTIVE));
501 kfree(pq->reqs);
Dean Luick7b3256e2016-07-28 15:21:18 -0400502 kfree(pq->req_in_use);
Julia Lawalladad44d2015-09-13 14:15:04 +0200503 kmem_cache_destroy(pq->txreq_cache);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400504 kfree(pq);
505 fd->pq = NULL;
506 }
507 if (fd->cq) {
Bhumika Goyala4d7d052016-02-14 20:34:28 +0530508 vfree(fd->cq->comps);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400509 kfree(fd->cq);
510 fd->cq = NULL;
511 }
512 return 0;
513}
514
Jianxin Xiong14833b82016-07-01 16:01:56 -0700515static u8 dlid_to_selector(u16 dlid)
516{
517 static u8 mapping[256];
518 static int initialized;
519 static u8 next;
520 int hash;
521
522 if (!initialized) {
523 memset(mapping, 0xFF, 256);
524 initialized = 1;
525 }
526
527 hash = ((dlid >> 8) ^ dlid) & 0xFF;
528 if (mapping[hash] == 0xFF) {
529 mapping[hash] = next;
530 next = (next + 1) & 0x7F;
531 }
532
533 return mapping[hash];
534}
535
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700536int hfi1_user_sdma_process_request(struct hfi1_filedata *fd,
537 struct iovec *iovec, unsigned long dim,
538 unsigned long *count)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400539{
Dean Luickff4ce9b2016-07-28 12:27:34 -0400540 int ret = 0, i;
Ira Weiny9e10af42015-10-30 18:58:40 -0400541 struct hfi1_ctxtdata *uctxt = fd->uctxt;
542 struct hfi1_user_sdma_pkt_q *pq = fd->pq;
543 struct hfi1_user_sdma_comp_q *cq = fd->cq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400544 struct hfi1_devdata *dd = pq->dd;
545 unsigned long idx = 0;
546 u8 pcount = initial_pkt_count;
547 struct sdma_req_info info;
548 struct user_sdma_request *req;
549 u8 opcode, sc, vl;
Jianxin Xiongb583faf2016-05-19 05:21:57 -0700550 int req_queued = 0;
Jianxin Xiong14833b82016-07-01 16:01:56 -0700551 u16 dlid;
Tadeusz Struk0cb2aa62016-09-25 07:44:23 -0700552 u32 selector;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400553
554 if (iovec[idx].iov_len < sizeof(info) + sizeof(req->hdr)) {
555 hfi1_cdbg(
556 SDMA,
557 "[%u:%u:%u] First vector not big enough for header %lu/%lu",
Ira Weiny9e10af42015-10-30 18:58:40 -0400558 dd->unit, uctxt->ctxt, fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400559 iovec[idx].iov_len, sizeof(info) + sizeof(req->hdr));
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500560 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400561 }
562 ret = copy_from_user(&info, iovec[idx].iov_base, sizeof(info));
563 if (ret) {
564 hfi1_cdbg(SDMA, "[%u:%u:%u] Failed to copy info QW (%d)",
Ira Weiny9e10af42015-10-30 18:58:40 -0400565 dd->unit, uctxt->ctxt, fd->subctxt, ret);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500566 return -EFAULT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400567 }
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800568
Ira Weiny9e10af42015-10-30 18:58:40 -0400569 trace_hfi1_sdma_user_reqinfo(dd, uctxt->ctxt, fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400570 (u16 *)&info);
Dean Luick4fa0d222016-07-28 15:21:14 -0400571
572 if (info.comp_idx >= hfi1_sdma_comp_ring_size) {
573 hfi1_cdbg(SDMA,
574 "[%u:%u:%u:%u] Invalid comp index",
575 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx);
576 return -EINVAL;
577 }
578
Dean Luick9ff73c82016-07-28 15:21:15 -0400579 /*
580 * Sanity check the header io vector count. Need at least 1 vector
581 * (header) and cannot be larger than the actual io vector count.
582 */
583 if (req_iovcnt(info.ctrl) < 1 || req_iovcnt(info.ctrl) > dim) {
584 hfi1_cdbg(SDMA,
585 "[%u:%u:%u:%u] Invalid iov count %d, dim %ld",
586 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx,
587 req_iovcnt(info.ctrl), dim);
588 return -EINVAL;
589 }
590
Mike Marciniszyn77241052015-07-30 15:17:43 -0400591 if (!info.fragsize) {
592 hfi1_cdbg(SDMA,
593 "[%u:%u:%u:%u] Request does not specify fragsize",
Ira Weiny9e10af42015-10-30 18:58:40 -0400594 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500595 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400596 }
Dean Luick7b3256e2016-07-28 15:21:18 -0400597
598 /* Try to claim the request. */
599 if (test_and_set_bit(info.comp_idx, pq->req_in_use)) {
600 hfi1_cdbg(SDMA, "[%u:%u:%u] Entry %u is in use",
601 dd->unit, uctxt->ctxt, fd->subctxt,
602 info.comp_idx);
603 return -EBADSLT;
604 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400605 /*
Dean Luick7b3256e2016-07-28 15:21:18 -0400606 * All safety checks have been done and this request has been claimed.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400607 */
608 hfi1_cdbg(SDMA, "[%u:%u:%u] Using req/comp entry %u\n", dd->unit,
Ira Weiny9e10af42015-10-30 18:58:40 -0400609 uctxt->ctxt, fd->subctxt, info.comp_idx);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400610 req = pq->reqs + info.comp_idx;
611 memset(req, 0, sizeof(*req));
Dean Luick9ff73c82016-07-28 15:21:15 -0400612 req->data_iovs = req_iovcnt(info.ctrl) - 1; /* subtract header vector */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400613 req->pq = pq;
614 req->cq = cq;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500615 req->status = -1;
Sebastian Sanchez780a4c12017-05-04 05:14:51 -0700616 req->ahg_idx = -1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400617 INIT_LIST_HEAD(&req->txps);
Mitko Haralanova0d40692015-12-08 17:10:13 -0500618
Mike Marciniszyn77241052015-07-30 15:17:43 -0400619 memcpy(&req->info, &info, sizeof(info));
620
Dean Luick9ff73c82016-07-28 15:21:15 -0400621 if (req_opcode(info.ctrl) == EXPECTED) {
622 /* expected must have a TID info and at least one data vector */
623 if (req->data_iovs < 2) {
624 SDMA_DBG(req,
625 "Not enough vectors for expected request");
626 ret = -EINVAL;
627 goto free_req;
628 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400629 req->data_iovs--;
Dean Luick9ff73c82016-07-28 15:21:15 -0400630 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400631
632 if (!info.npkts || req->data_iovs > MAX_VECTORS_PER_REQ) {
633 SDMA_DBG(req, "Too many vectors (%u/%u)", req->data_iovs,
634 MAX_VECTORS_PER_REQ);
Dean Luick9da7e9a2016-07-28 15:21:17 -0400635 ret = -EINVAL;
636 goto free_req;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400637 }
638 /* Copy the header from the user buffer */
639 ret = copy_from_user(&req->hdr, iovec[idx].iov_base + sizeof(info),
640 sizeof(req->hdr));
641 if (ret) {
642 SDMA_DBG(req, "Failed to copy header template (%d)", ret);
643 ret = -EFAULT;
644 goto free_req;
645 }
646
647 /* If Static rate control is not enabled, sanitize the header. */
648 if (!HFI1_CAP_IS_USET(STATIC_RATE_CTRL))
649 req->hdr.pbc[2] = 0;
650
651 /* Validate the opcode. Do not trust packets from user space blindly. */
652 opcode = (be32_to_cpu(req->hdr.bth[0]) >> 24) & 0xff;
653 if ((opcode & USER_OPCODE_CHECK_MASK) !=
654 USER_OPCODE_CHECK_VAL) {
655 SDMA_DBG(req, "Invalid opcode (%d)", opcode);
656 ret = -EINVAL;
657 goto free_req;
658 }
659 /*
660 * Validate the vl. Do not trust packets from user space blindly.
661 * VL comes from PBC, SC comes from LRH, and the VL needs to
662 * match the SC look up.
663 */
664 vl = (le16_to_cpu(req->hdr.pbc[0]) >> 12) & 0xF;
665 sc = (((be16_to_cpu(req->hdr.lrh[0]) >> 12) & 0xF) |
666 (((le16_to_cpu(req->hdr.pbc[1]) >> 14) & 0x1) << 4));
667 if (vl >= dd->pport->vls_operational ||
668 vl != sc_to_vlt(dd, sc)) {
669 SDMA_DBG(req, "Invalid SC(%u)/VL(%u)", sc, vl);
670 ret = -EINVAL;
671 goto free_req;
672 }
673
Sebastian Sancheze38d1e42016-04-12 11:22:21 -0700674 /* Checking P_KEY for requests from user-space */
675 if (egress_pkey_check(dd->pport, req->hdr.lrh, req->hdr.bth, sc,
676 PKEY_CHECK_INVALID)) {
677 ret = -EINVAL;
678 goto free_req;
679 }
680
Mike Marciniszyn77241052015-07-30 15:17:43 -0400681 /*
682 * Also should check the BTH.lnh. If it says the next header is GRH then
683 * the RXE parsing will be off and will land in the middle of the KDETH
684 * or miss it entirely.
685 */
686 if ((be16_to_cpu(req->hdr.lrh[0]) & 0x3) == HFI1_LRH_GRH) {
687 SDMA_DBG(req, "User tried to pass in a GRH");
688 ret = -EINVAL;
689 goto free_req;
690 }
691
692 req->koffset = le32_to_cpu(req->hdr.kdeth.swdata[6]);
Jubin John4d114fd2016-02-14 20:21:43 -0800693 /*
694 * Calculate the initial TID offset based on the values of
695 * KDETH.OFFSET and KDETH.OM that are passed in.
696 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400697 req->tidoffset = KDETH_GET(req->hdr.kdeth.ver_tid_offset, OFFSET) *
698 (KDETH_GET(req->hdr.kdeth.ver_tid_offset, OM) ?
699 KDETH_OM_LARGE : KDETH_OM_SMALL);
700 SDMA_DBG(req, "Initial TID offset %u", req->tidoffset);
701 idx++;
702
703 /* Save all the IO vector structures */
Dean Luickff4ce9b2016-07-28 12:27:34 -0400704 for (i = 0; i < req->data_iovs; i++) {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800705 INIT_LIST_HEAD(&req->iovs[i].list);
Markus Elfringdb6f0282017-02-10 21:45:38 +0100706 memcpy(&req->iovs[i].iov,
707 iovec + idx++,
708 sizeof(req->iovs[i].iov));
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800709 ret = pin_vector_pages(req, &req->iovs[i]);
710 if (ret) {
711 req->status = ret;
712 goto free_req;
713 }
Dean Luickff4ce9b2016-07-28 12:27:34 -0400714 req->data_len += req->iovs[i].iov.iov_len;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400715 }
716 SDMA_DBG(req, "total data length %u", req->data_len);
717
718 if (pcount > req->info.npkts)
719 pcount = req->info.npkts;
720 /*
721 * Copy any TID info
722 * User space will provide the TID info only when the
723 * request type is EXPECTED. This is true even if there is
724 * only one packet in the request and the header is already
725 * setup. The reason for the singular TID case is that the
726 * driver needs to perform safety checks.
727 */
728 if (req_opcode(req->info.ctrl) == EXPECTED) {
729 u16 ntids = iovec[idx].iov_len / sizeof(*req->tids);
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800730 u32 *tmp;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400731
732 if (!ntids || ntids > MAX_TID_PAIR_ENTRIES) {
733 ret = -EINVAL;
734 goto free_req;
735 }
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800736
Mike Marciniszyn77241052015-07-30 15:17:43 -0400737 /*
738 * We have to copy all of the tids because they may vary
739 * in size and, therefore, the TID count might not be
740 * equal to the pkt count. However, there is no way to
741 * tell at this point.
742 */
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800743 tmp = memdup_user(iovec[idx].iov_base,
744 ntids * sizeof(*req->tids));
745 if (IS_ERR(tmp)) {
746 ret = PTR_ERR(tmp);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400747 SDMA_DBG(req, "Failed to copy %d TIDs (%d)",
748 ntids, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400749 goto free_req;
750 }
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800751 req->tids = tmp;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400752 req->n_tids = ntids;
753 idx++;
754 }
755
Jianxin Xiong14833b82016-07-01 16:01:56 -0700756 dlid = be16_to_cpu(req->hdr.lrh[1]);
757 selector = dlid_to_selector(dlid);
Tadeusz Struk0cb2aa62016-09-25 07:44:23 -0700758 selector += uctxt->ctxt + fd->subctxt;
759 req->sde = sdma_select_user_engine(dd, selector, vl);
Jianxin Xiong14833b82016-07-01 16:01:56 -0700760
Mike Marciniszyn77241052015-07-30 15:17:43 -0400761 if (!req->sde || !sdma_running(req->sde)) {
762 ret = -ECOMM;
763 goto free_req;
764 }
765
766 /* We don't need an AHG entry if the request contains only one packet */
Sebastian Sanchez780a4c12017-05-04 05:14:51 -0700767 if (req->info.npkts > 1 && HFI1_CAP_IS_USET(SDMA_AHG))
768 req->ahg_idx = sdma_ahg_alloc(req->sde);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400769
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{
Harish Chegondi0b115ef2016-09-06 04:35:37 -0700890 int ret = 0, count;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400891 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
Jakub Pawlake7301392016-12-07 19:32:41 -0800942 /*
943 * For the last packet set the ACK request
944 * and disable header suppression.
945 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400946 if (req->seqnum == req->info.npkts - 1)
Jakub Pawlake7301392016-12-07 19:32:41 -0800947 tx->flags |= (TXREQ_FLAGS_REQ_ACK |
948 TXREQ_FLAGS_REQ_DISABLE_SH);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400949
950 /*
951 * Calculate the payload size - this is min of the fragment
952 * (MTU) size or the remaining bytes in the request but only
953 * if we have payload data.
954 */
955 if (req->data_len) {
956 iovec = &req->iovs[req->iov_idx];
957 if (ACCESS_ONCE(iovec->offset) == iovec->iov.iov_len) {
958 if (++req->iov_idx == req->data_iovs) {
959 ret = -EFAULT;
960 goto free_txreq;
961 }
962 iovec = &req->iovs[req->iov_idx];
963 WARN_ON(iovec->offset);
964 }
965
Mike Marciniszyn77241052015-07-30 15:17:43 -0400966 datalen = compute_data_length(req, tx);
Jakub Pawlake7301392016-12-07 19:32:41 -0800967
968 /*
969 * Disable header suppression for the payload <= 8DWS.
970 * If there is an uncorrectable error in the receive
971 * data FIFO when the received payload size is less than
972 * or equal to 8DWS then the RxDmaDataFifoRdUncErr is
973 * not reported.There is set RHF.EccErr if the header
974 * is not suppressed.
975 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400976 if (!datalen) {
977 SDMA_DBG(req,
978 "Request has data but pkt len is 0");
979 ret = -EFAULT;
980 goto free_tx;
Jakub Pawlake7301392016-12-07 19:32:41 -0800981 } else if (datalen <= 32) {
982 tx->flags |= TXREQ_FLAGS_REQ_DISABLE_SH;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400983 }
984 }
985
Sebastian Sanchez780a4c12017-05-04 05:14:51 -0700986 if (req->ahg_idx >= 0) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400987 if (!req->seqnum) {
988 u16 pbclen = le16_to_cpu(req->hdr.pbc[0]);
Ira Weinyc4929802016-07-27 21:08:42 -0400989 u32 lrhlen = get_lrh_len(req->hdr,
990 pad_len(datalen));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400991 /*
992 * Copy the request header into the tx header
993 * because the HW needs a cacheline-aligned
994 * address.
995 * This copy can be optimized out if the hdr
996 * member of user_sdma_request were also
997 * cacheline aligned.
998 */
999 memcpy(&tx->hdr, &req->hdr, sizeof(tx->hdr));
1000 if (PBC2LRH(pbclen) != lrhlen) {
1001 pbclen = (pbclen & 0xf000) |
1002 LRH2PBC(lrhlen);
1003 tx->hdr.pbc[0] = cpu_to_le16(pbclen);
1004 }
Jakub Pawlake7301392016-12-07 19:32:41 -08001005 ret = check_header_template(req, &tx->hdr,
1006 lrhlen, datalen);
1007 if (ret)
1008 goto free_tx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001009 ret = sdma_txinit_ahg(&tx->txreq,
1010 SDMA_TXREQ_F_AHG_COPY,
1011 sizeof(tx->hdr) + datalen,
1012 req->ahg_idx, 0, NULL, 0,
1013 user_sdma_txreq_cb);
1014 if (ret)
1015 goto free_tx;
1016 ret = sdma_txadd_kvaddr(pq->dd, &tx->txreq,
1017 &tx->hdr,
1018 sizeof(tx->hdr));
1019 if (ret)
1020 goto free_txreq;
1021 } else {
1022 int changes;
1023
1024 changes = set_txreq_header_ahg(req, tx,
1025 datalen);
1026 if (changes < 0)
1027 goto free_tx;
1028 sdma_txinit_ahg(&tx->txreq,
1029 SDMA_TXREQ_F_USE_AHG,
1030 datalen, req->ahg_idx, changes,
1031 req->ahg, sizeof(req->hdr),
1032 user_sdma_txreq_cb);
1033 }
1034 } else {
1035 ret = sdma_txinit(&tx->txreq, 0, sizeof(req->hdr) +
1036 datalen, user_sdma_txreq_cb);
1037 if (ret)
1038 goto free_tx;
1039 /*
1040 * Modify the header for this packet. This only needs
1041 * to be done if we are not going to use AHG. Otherwise,
1042 * the HW will do it based on the changes we gave it
1043 * during sdma_txinit_ahg().
1044 */
1045 ret = set_txreq_header(req, tx, datalen);
1046 if (ret)
1047 goto free_txreq;
1048 }
1049
1050 /*
1051 * If the request contains any data vectors, add up to
1052 * fragsize bytes to the descriptor.
1053 */
1054 while (queued < datalen &&
1055 (req->sent + data_sent) < req->data_len) {
1056 unsigned long base, offset;
1057 unsigned pageidx, len;
1058
1059 base = (unsigned long)iovec->iov.iov_base;
Amitoj Kaur Chawla72a5f6a2016-02-20 19:08:02 +05301060 offset = offset_in_page(base + iovec->offset +
1061 iov_offset);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001062 pageidx = (((iovec->offset + iov_offset +
1063 base) - (base & PAGE_MASK)) >> PAGE_SHIFT);
1064 len = offset + req->info.fragsize > PAGE_SIZE ?
1065 PAGE_SIZE - offset : req->info.fragsize;
1066 len = min((datalen - queued), len);
1067 ret = sdma_txadd_page(pq->dd, &tx->txreq,
1068 iovec->pages[pageidx],
1069 offset, len);
1070 if (ret) {
Mitko Haralanova0d40692015-12-08 17:10:13 -05001071 SDMA_DBG(req, "SDMA txreq add page failed %d\n",
1072 ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001073 goto free_txreq;
1074 }
1075 iov_offset += len;
1076 queued += len;
1077 data_sent += len;
1078 if (unlikely(queued < datalen &&
1079 pageidx == iovec->npages &&
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001080 req->iov_idx < req->data_iovs - 1)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001081 iovec->offset += iov_offset;
1082 iovec = &req->iovs[++req->iov_idx];
Mike Marciniszyn77241052015-07-30 15:17:43 -04001083 iov_offset = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001084 }
1085 }
1086 /*
1087 * The txreq was submitted successfully so we can update
1088 * the counters.
1089 */
1090 req->koffset += datalen;
1091 if (req_opcode(req->info.ctrl) == EXPECTED)
1092 req->tidoffset += datalen;
1093 req->sent += data_sent;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001094 if (req->data_len)
1095 iovec->offset += iov_offset;
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001096 list_add_tail(&tx->txreq.list, &req->txps);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001097 /*
1098 * It is important to increment this here as it is used to
1099 * generate the BTH.PSN and, therefore, can't be bulk-updated
1100 * outside of the loop.
1101 */
1102 tx->seqnum = req->seqnum++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001103 npkts++;
1104 }
1105dosend:
Harish Chegondi0b115ef2016-09-06 04:35:37 -07001106 ret = sdma_send_txlist(req->sde, &pq->busy, &req->txps, &count);
1107 req->seqsubmitted += count;
1108 if (req->seqsubmitted == req->info.npkts) {
1109 set_bit(SDMA_REQ_SEND_DONE, &req->flags);
1110 /*
1111 * The txreq has already been submitted to the HW queue
1112 * so we can free the AHG entry now. Corruption will not
1113 * happen due to the sequential manner in which
1114 * descriptors are processed.
1115 */
Sebastian Sanchez780a4c12017-05-04 05:14:51 -07001116 if (req->ahg_idx >= 0)
Harish Chegondi0b115ef2016-09-06 04:35:37 -07001117 sdma_ahg_free(req->sde, req->ahg_idx);
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001118 }
Mitko Haralanovfaa98b82015-12-08 17:10:11 -05001119 return ret;
1120
Mike Marciniszyn77241052015-07-30 15:17:43 -04001121free_txreq:
1122 sdma_txclean(pq->dd, &tx->txreq);
1123free_tx:
1124 kmem_cache_free(pq->txreq_cache, tx);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001125 return ret;
1126}
1127
1128/*
1129 * How many pages in this iovec element?
1130 */
1131static inline int num_user_pages(const struct iovec *iov)
1132{
Jubin John50e5dcb2016-02-14 20:19:41 -08001133 const unsigned long addr = (unsigned long)iov->iov_base;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001134 const unsigned long len = iov->iov_len;
1135 const unsigned long spage = addr & PAGE_MASK;
1136 const unsigned long epage = (addr + len - 1) & PAGE_MASK;
1137
1138 return 1 + ((epage - spage) >> PAGE_SHIFT);
1139}
1140
Mitko Haralanov5511d782016-03-08 11:15:44 -08001141static u32 sdma_cache_evict(struct hfi1_user_sdma_pkt_q *pq, u32 npages)
1142{
Dean Luickb7df1922016-07-28 15:21:23 -04001143 struct evict_data evict_data;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001144
Dean Luickb7df1922016-07-28 15:21:23 -04001145 evict_data.cleared = 0;
1146 evict_data.target = npages;
1147 hfi1_mmu_rb_evict(pq->handler, &evict_data);
1148 return evict_data.cleared;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001149}
1150
Mike Marciniszyn77241052015-07-30 15:17:43 -04001151static int pin_vector_pages(struct user_sdma_request *req,
Ira Weiny72720dd2016-07-28 12:27:25 -04001152 struct user_sdma_iovec *iovec)
1153{
Mitko Haralanov5511d782016-03-08 11:15:44 -08001154 int ret = 0, pinned, npages, cleared;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001155 struct page **pages;
1156 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1157 struct sdma_mmu_node *node = NULL;
1158 struct mmu_rb_node *rb_node;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001159
Dean Luicke0b09ac2016-07-28 15:21:20 -04001160 rb_node = hfi1_mmu_rb_extract(pq->handler,
Mitko Haralanovf53af852016-04-12 10:46:47 -07001161 (unsigned long)iovec->iov.iov_base,
1162 iovec->iov.iov_len);
Dennis Dalessandro2b160562016-10-25 13:12:46 -07001163 if (rb_node)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001164 node = container_of(rb_node, struct sdma_mmu_node, rb);
Mitko Haralanovf19bd642016-04-12 10:45:57 -07001165 else
1166 rb_node = NULL;
Mitko Haralanova0d40692015-12-08 17:10:13 -05001167
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001168 if (!node) {
1169 node = kzalloc(sizeof(*node), GFP_KERNEL);
1170 if (!node)
1171 return -ENOMEM;
1172
1173 node->rb.addr = (unsigned long)iovec->iov.iov_base;
Mitko Haralanov5511d782016-03-08 11:15:44 -08001174 node->pq = pq;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001175 atomic_set(&node->refcount, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001176 }
Mitko Haralanova0d40692015-12-08 17:10:13 -05001177
Mike Marciniszyn77241052015-07-30 15:17:43 -04001178 npages = num_user_pages(&iovec->iov);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001179 if (node->npages < npages) {
1180 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
1181 if (!pages) {
1182 SDMA_DBG(req, "Failed page array alloc");
1183 ret = -ENOMEM;
1184 goto bail;
1185 }
1186 memcpy(pages, node->pages, node->npages * sizeof(*pages));
1187
1188 npages -= node->npages;
Mitko Haralanove88c9272016-04-12 10:46:53 -07001189
Mitko Haralanov5511d782016-03-08 11:15:44 -08001190retry:
Dean Luickb7df1922016-07-28 15:21:23 -04001191 if (!hfi1_can_pin_pages(pq->dd, pq->mm,
1192 atomic_read(&pq->n_locked), npages)) {
Mitko Haralanov5511d782016-03-08 11:15:44 -08001193 cleared = sdma_cache_evict(pq, npages);
Mitko Haralanov5511d782016-03-08 11:15:44 -08001194 if (cleared >= npages)
1195 goto retry;
1196 }
Ira Weiny3faa3d92016-07-28 15:21:19 -04001197 pinned = hfi1_acquire_user_pages(pq->mm,
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001198 ((unsigned long)iovec->iov.iov_base +
1199 (node->npages * PAGE_SIZE)), npages, 0,
1200 pages + node->npages);
1201 if (pinned < 0) {
1202 kfree(pages);
1203 ret = pinned;
1204 goto bail;
1205 }
1206 if (pinned != npages) {
Ira Weiny3faa3d92016-07-28 15:21:19 -04001207 unpin_vector_pages(pq->mm, pages, node->npages,
Mitko Haralanov849e3e92016-04-12 10:46:16 -07001208 pinned);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001209 ret = -EFAULT;
1210 goto bail;
1211 }
1212 kfree(node->pages);
Mitko Haralanovde790932016-04-12 10:46:41 -07001213 node->rb.len = iovec->iov.iov_len;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001214 node->pages = pages;
1215 node->npages += pinned;
1216 npages = node->npages;
Dean Luickb7df1922016-07-28 15:21:23 -04001217 atomic_add(pinned, &pq->n_locked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001218 }
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001219 iovec->pages = node->pages;
1220 iovec->npages = npages;
Mitko Haralanov9565c6a2016-05-19 05:21:18 -07001221 iovec->node = node;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001222
Dean Luicke0b09ac2016-07-28 15:21:20 -04001223 ret = hfi1_mmu_rb_insert(req->pq->handler, &node->rb);
Mitko Haralanovf53af852016-04-12 10:46:47 -07001224 if (ret) {
Dean Luickb7df1922016-07-28 15:21:23 -04001225 atomic_sub(node->npages, &pq->n_locked);
Dean Luicka383f8e2016-07-28 15:21:16 -04001226 iovec->node = NULL;
Mitko Haralanovf53af852016-04-12 10:46:47 -07001227 goto bail;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001228 }
1229 return 0;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001230bail:
Mitko Haralanovf53af852016-04-12 10:46:47 -07001231 if (rb_node)
Ira Weiny3faa3d92016-07-28 15:21:19 -04001232 unpin_vector_pages(pq->mm, node->pages, 0, node->npages);
Mitko Haralanovf53af852016-04-12 10:46:47 -07001233 kfree(node);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001234 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001235}
1236
Mitko Haralanovbd3a8942016-03-08 11:15:33 -08001237static void unpin_vector_pages(struct mm_struct *mm, struct page **pages,
Mitko Haralanov849e3e92016-04-12 10:46:16 -07001238 unsigned start, unsigned npages)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001239{
Ira Weiny639297b2016-07-28 12:27:33 -04001240 hfi1_release_user_pages(mm, pages + start, npages, false);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001241 kfree(pages);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001242}
1243
1244static int check_header_template(struct user_sdma_request *req,
1245 struct hfi1_pkt_header *hdr, u32 lrhlen,
1246 u32 datalen)
1247{
1248 /*
1249 * Perform safety checks for any type of packet:
1250 * - transfer size is multiple of 64bytes
Ira Weinyc4929802016-07-27 21:08:42 -04001251 * - packet length is multiple of 4 bytes
Mike Marciniszyn77241052015-07-30 15:17:43 -04001252 * - packet length is not larger than MTU size
1253 *
1254 * These checks are only done for the first packet of the
1255 * transfer since the header is "given" to us by user space.
1256 * For the remainder of the packets we compute the values.
1257 */
Ira Weinyc4929802016-07-27 21:08:42 -04001258 if (req->info.fragsize % PIO_BLOCK_SIZE || lrhlen & 0x3 ||
Mike Marciniszyn77241052015-07-30 15:17:43 -04001259 lrhlen > get_lrh_len(*hdr, req->info.fragsize))
1260 return -EINVAL;
1261
1262 if (req_opcode(req->info.ctrl) == EXPECTED) {
1263 /*
1264 * The header is checked only on the first packet. Furthermore,
1265 * we ensure that at least one TID entry is copied when the
1266 * request is submitted. Therefore, we don't have to verify that
1267 * tididx points to something sane.
1268 */
1269 u32 tidval = req->tids[req->tididx],
1270 tidlen = EXP_TID_GET(tidval, LEN) * PAGE_SIZE,
1271 tididx = EXP_TID_GET(tidval, IDX),
1272 tidctrl = EXP_TID_GET(tidval, CTRL),
1273 tidoff;
1274 __le32 kval = hdr->kdeth.ver_tid_offset;
1275
1276 tidoff = KDETH_GET(kval, OFFSET) *
1277 (KDETH_GET(req->hdr.kdeth.ver_tid_offset, OM) ?
1278 KDETH_OM_LARGE : KDETH_OM_SMALL);
1279 /*
1280 * Expected receive packets have the following
1281 * additional checks:
1282 * - offset is not larger than the TID size
1283 * - TIDCtrl values match between header and TID array
1284 * - TID indexes match between header and TID array
1285 */
1286 if ((tidoff + datalen > tidlen) ||
1287 KDETH_GET(kval, TIDCTRL) != tidctrl ||
1288 KDETH_GET(kval, TID) != tididx)
1289 return -EINVAL;
1290 }
1291 return 0;
1292}
1293
1294/*
1295 * Correctly set the BTH.PSN field based on type of
1296 * transfer - eager packets can just increment the PSN but
1297 * expected packets encode generation and sequence in the
1298 * BTH.PSN field so just incrementing will result in errors.
1299 */
1300static inline u32 set_pkt_bth_psn(__be32 bthpsn, u8 expct, u32 frags)
1301{
1302 u32 val = be32_to_cpu(bthpsn),
1303 mask = (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffffull :
1304 0xffffffull),
1305 psn = val & mask;
1306 if (expct)
1307 psn = (psn & ~BTH_SEQ_MASK) | ((psn + frags) & BTH_SEQ_MASK);
1308 else
1309 psn = psn + frags;
1310 return psn & mask;
1311}
1312
1313static int set_txreq_header(struct user_sdma_request *req,
1314 struct user_sdma_txreq *tx, u32 datalen)
1315{
1316 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1317 struct hfi1_pkt_header *hdr = &tx->hdr;
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001318 u8 omfactor; /* KDETH.OM */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001319 u16 pbclen;
1320 int ret;
Ira Weinyc4929802016-07-27 21:08:42 -04001321 u32 tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(datalen));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001322
1323 /* Copy the header template to the request before modification */
1324 memcpy(hdr, &req->hdr, sizeof(*hdr));
1325
1326 /*
1327 * Check if the PBC and LRH length are mismatched. If so
1328 * adjust both in the header.
1329 */
1330 pbclen = le16_to_cpu(hdr->pbc[0]);
1331 if (PBC2LRH(pbclen) != lrhlen) {
1332 pbclen = (pbclen & 0xf000) | LRH2PBC(lrhlen);
1333 hdr->pbc[0] = cpu_to_le16(pbclen);
1334 hdr->lrh[2] = cpu_to_be16(lrhlen >> 2);
1335 /*
1336 * Third packet
1337 * This is the first packet in the sequence that has
1338 * a "static" size that can be used for the rest of
1339 * the packets (besides the last one).
1340 */
1341 if (unlikely(req->seqnum == 2)) {
1342 /*
1343 * From this point on the lengths in both the
1344 * PBC and LRH are the same until the last
1345 * packet.
1346 * Adjust the template so we don't have to update
1347 * every packet
1348 */
1349 req->hdr.pbc[0] = hdr->pbc[0];
1350 req->hdr.lrh[2] = hdr->lrh[2];
1351 }
1352 }
1353 /*
1354 * We only have to modify the header if this is not the
1355 * first packet in the request. Otherwise, we use the
1356 * header given to us.
1357 */
1358 if (unlikely(!req->seqnum)) {
1359 ret = check_header_template(req, hdr, lrhlen, datalen);
1360 if (ret)
1361 return ret;
1362 goto done;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001363 }
1364
1365 hdr->bth[2] = cpu_to_be32(
1366 set_pkt_bth_psn(hdr->bth[2],
1367 (req_opcode(req->info.ctrl) == EXPECTED),
1368 req->seqnum));
1369
1370 /* Set ACK request on last packet */
Jakub Pawlake7301392016-12-07 19:32:41 -08001371 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_ACK))
Jubin John8638b772016-02-14 20:19:24 -08001372 hdr->bth[2] |= cpu_to_be32(1UL << 31);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001373
1374 /* Set the new offset */
1375 hdr->kdeth.swdata[6] = cpu_to_le32(req->koffset);
1376 /* Expected packets have to fill in the new TID information */
1377 if (req_opcode(req->info.ctrl) == EXPECTED) {
1378 tidval = req->tids[req->tididx];
1379 /*
1380 * If the offset puts us at the end of the current TID,
1381 * advance everything.
1382 */
1383 if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
1384 PAGE_SIZE)) {
1385 req->tidoffset = 0;
Jubin John4d114fd2016-02-14 20:21:43 -08001386 /*
1387 * Since we don't copy all the TIDs, all at once,
1388 * we have to check again.
1389 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001390 if (++req->tididx > req->n_tids - 1 ||
1391 !req->tids[req->tididx]) {
1392 return -EINVAL;
1393 }
1394 tidval = req->tids[req->tididx];
1395 }
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001396 omfactor = EXP_TID_GET(tidval, LEN) * PAGE_SIZE >=
1397 KDETH_OM_MAX_SIZE ? KDETH_OM_LARGE_SHIFT :
1398 KDETH_OM_SMALL_SHIFT;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001399 /* Set KDETH.TIDCtrl based on value for this TID. */
1400 KDETH_SET(hdr->kdeth.ver_tid_offset, TIDCTRL,
1401 EXP_TID_GET(tidval, CTRL));
1402 /* Set KDETH.TID based on value for this TID */
1403 KDETH_SET(hdr->kdeth.ver_tid_offset, TID,
1404 EXP_TID_GET(tidval, IDX));
Jakub Pawlake7301392016-12-07 19:32:41 -08001405 /* Clear KDETH.SH when DISABLE_SH flag is set */
1406 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_DISABLE_SH))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001407 KDETH_SET(hdr->kdeth.ver_tid_offset, SH, 0);
1408 /*
1409 * Set the KDETH.OFFSET and KDETH.OM based on size of
1410 * transfer.
1411 */
1412 SDMA_DBG(req, "TID offset %ubytes %uunits om%u",
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001413 req->tidoffset, req->tidoffset >> omfactor,
1414 omfactor != KDETH_OM_SMALL_SHIFT);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001415 KDETH_SET(hdr->kdeth.ver_tid_offset, OFFSET,
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001416 req->tidoffset >> omfactor);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001417 KDETH_SET(hdr->kdeth.ver_tid_offset, OM,
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001418 omfactor != KDETH_OM_SMALL_SHIFT);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001419 }
1420done:
1421 trace_hfi1_sdma_user_header(pq->dd, pq->ctxt, pq->subctxt,
1422 req->info.comp_idx, hdr, tidval);
1423 return sdma_txadd_kvaddr(pq->dd, &tx->txreq, hdr, sizeof(*hdr));
1424}
1425
1426static int set_txreq_header_ahg(struct user_sdma_request *req,
1427 struct user_sdma_txreq *tx, u32 len)
1428{
1429 int diff = 0;
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001430 u8 omfactor; /* KDETH.OM */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001431 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1432 struct hfi1_pkt_header *hdr = &req->hdr;
1433 u16 pbclen = le16_to_cpu(hdr->pbc[0]);
Ira Weinyc4929802016-07-27 21:08:42 -04001434 u32 val32, tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(len));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001435
1436 if (PBC2LRH(pbclen) != lrhlen) {
1437 /* PBC.PbcLengthDWs */
1438 AHG_HEADER_SET(req->ahg, diff, 0, 0, 12,
1439 cpu_to_le16(LRH2PBC(lrhlen)));
1440 /* LRH.PktLen (we need the full 16 bits due to byte swap) */
1441 AHG_HEADER_SET(req->ahg, diff, 3, 0, 16,
1442 cpu_to_be16(lrhlen >> 2));
1443 }
1444
1445 /*
1446 * Do the common updates
1447 */
1448 /* BTH.PSN and BTH.A */
1449 val32 = (be32_to_cpu(hdr->bth[2]) + req->seqnum) &
1450 (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffff : 0xffffff);
Jakub Pawlake7301392016-12-07 19:32:41 -08001451 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_ACK))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001452 val32 |= 1UL << 31;
1453 AHG_HEADER_SET(req->ahg, diff, 6, 0, 16, cpu_to_be16(val32 >> 16));
1454 AHG_HEADER_SET(req->ahg, diff, 6, 16, 16, cpu_to_be16(val32 & 0xffff));
1455 /* KDETH.Offset */
1456 AHG_HEADER_SET(req->ahg, diff, 15, 0, 16,
1457 cpu_to_le16(req->koffset & 0xffff));
1458 AHG_HEADER_SET(req->ahg, diff, 15, 16, 16,
1459 cpu_to_le16(req->koffset >> 16));
1460 if (req_opcode(req->info.ctrl) == EXPECTED) {
1461 __le16 val;
1462
1463 tidval = req->tids[req->tididx];
1464
1465 /*
1466 * If the offset puts us at the end of the current TID,
1467 * advance everything.
1468 */
1469 if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
1470 PAGE_SIZE)) {
1471 req->tidoffset = 0;
Jubin John4d114fd2016-02-14 20:21:43 -08001472 /*
1473 * Since we don't copy all the TIDs, all at once,
1474 * we have to check again.
1475 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001476 if (++req->tididx > req->n_tids - 1 ||
1477 !req->tids[req->tididx]) {
1478 return -EINVAL;
1479 }
1480 tidval = req->tids[req->tididx];
1481 }
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001482 omfactor = ((EXP_TID_GET(tidval, LEN) *
Mike Marciniszyn77241052015-07-30 15:17:43 -04001483 PAGE_SIZE) >=
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001484 KDETH_OM_MAX_SIZE) ? KDETH_OM_LARGE_SHIFT :
1485 KDETH_OM_SMALL_SHIFT;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001486 /* KDETH.OM and KDETH.OFFSET (TID) */
1487 AHG_HEADER_SET(req->ahg, diff, 7, 0, 16,
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001488 ((!!(omfactor - KDETH_OM_SMALL_SHIFT)) << 15 |
1489 ((req->tidoffset >> omfactor)
1490 & 0x7fff)));
Jakub Pawlake7301392016-12-07 19:32:41 -08001491 /* KDETH.TIDCtrl, KDETH.TID, KDETH.Intr, KDETH.SH */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001492 val = cpu_to_le16(((EXP_TID_GET(tidval, CTRL) & 0x3) << 10) |
Jakub Pawlake7301392016-12-07 19:32:41 -08001493 (EXP_TID_GET(tidval, IDX) & 0x3ff));
1494
1495 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_DISABLE_SH)) {
1496 val |= cpu_to_le16((KDETH_GET(hdr->kdeth.ver_tid_offset,
1497 INTR) <<
1498 AHG_KDETH_INTR_SHIFT));
Jubin Johne4909742016-02-14 20:22:00 -08001499 } else {
Jakub Pawlake7301392016-12-07 19:32:41 -08001500 val |= KDETH_GET(hdr->kdeth.ver_tid_offset, SH) ?
1501 cpu_to_le16(0x1 << AHG_KDETH_SH_SHIFT) :
1502 cpu_to_le16((KDETH_GET(hdr->kdeth.ver_tid_offset,
1503 INTR) <<
1504 AHG_KDETH_INTR_SHIFT));
Jubin Johne4909742016-02-14 20:22:00 -08001505 }
Jakub Pawlake7301392016-12-07 19:32:41 -08001506
1507 AHG_HEADER_SET(req->ahg, diff, 7, 16, 14, val);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001508 }
1509
1510 trace_hfi1_sdma_user_header_ahg(pq->dd, pq->ctxt, pq->subctxt,
1511 req->info.comp_idx, req->sde->this_idx,
1512 req->ahg_idx, req->ahg, diff, tidval);
1513 return diff;
1514}
1515
Mitko Haralanova0d40692015-12-08 17:10:13 -05001516/*
1517 * SDMA tx request completion callback. Called when the SDMA progress
1518 * state machine gets notification that the SDMA descriptors for this
1519 * tx request have been processed by the DMA engine. Called in
1520 * interrupt context.
1521 */
Mike Marciniszyna545f532016-02-14 12:45:53 -08001522static void user_sdma_txreq_cb(struct sdma_txreq *txreq, int status)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001523{
1524 struct user_sdma_txreq *tx =
1525 container_of(txreq, struct user_sdma_txreq, txreq);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001526 struct user_sdma_request *req;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001527 struct hfi1_user_sdma_pkt_q *pq;
1528 struct hfi1_user_sdma_comp_q *cq;
1529 u16 idx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001530
Mitko Haralanova0d40692015-12-08 17:10:13 -05001531 if (!tx->req)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001532 return;
1533
Mitko Haralanova0d40692015-12-08 17:10:13 -05001534 req = tx->req;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001535 pq = req->pq;
1536 cq = req->cq;
Mitko Haralanovb9fb63182015-10-26 10:28:37 -04001537
Mike Marciniszyn77241052015-07-30 15:17:43 -04001538 if (status != SDMA_TXREQ_S_OK) {
Mitko Haralanova0d40692015-12-08 17:10:13 -05001539 SDMA_DBG(req, "SDMA completion with error %d",
1540 status);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001541 set_bit(SDMA_REQ_HAS_ERROR, &req->flags);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001542 }
1543
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001544 req->seqcomp = tx->seqnum;
1545 kmem_cache_free(pq->txreq_cache, tx);
1546 tx = NULL;
1547
1548 idx = req->info.comp_idx;
1549 if (req->status == -1 && status == SDMA_TXREQ_S_OK) {
1550 if (req->seqcomp == req->info.npkts - 1) {
1551 req->status = 0;
1552 user_sdma_free_request(req, false);
1553 pq_update(pq);
1554 set_comp_state(pq, cq, idx, COMPLETE, 0);
1555 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001556 } else {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001557 if (status != SDMA_TXREQ_S_OK)
1558 req->status = status;
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001559 if (req->seqcomp == (ACCESS_ONCE(req->seqsubmitted) - 1) &&
1560 (test_bit(SDMA_REQ_SEND_DONE, &req->flags) ||
1561 test_bit(SDMA_REQ_DONE_ERROR, &req->flags))) {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001562 user_sdma_free_request(req, false);
1563 pq_update(pq);
1564 set_comp_state(pq, cq, idx, ERROR, req->status);
1565 }
Mitko Haralanova0d40692015-12-08 17:10:13 -05001566 }
1567}
1568
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001569static inline void pq_update(struct hfi1_user_sdma_pkt_q *pq)
Mitko Haralanova0d40692015-12-08 17:10:13 -05001570{
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001571 if (atomic_dec_and_test(&pq->n_reqs)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001572 xchg(&pq->state, SDMA_PKT_Q_INACTIVE);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001573 wake_up(&pq->wait);
1574 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001575}
1576
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001577static void user_sdma_free_request(struct user_sdma_request *req, bool unpin)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001578{
1579 if (!list_empty(&req->txps)) {
1580 struct sdma_txreq *t, *p;
1581
1582 list_for_each_entry_safe(t, p, &req->txps, list) {
1583 struct user_sdma_txreq *tx =
1584 container_of(t, struct user_sdma_txreq, txreq);
1585 list_del_init(&t->list);
1586 sdma_txclean(req->pq->dd, t);
1587 kmem_cache_free(req->pq->txreq_cache, tx);
1588 }
1589 }
1590 if (req->data_iovs) {
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001591 struct sdma_mmu_node *node;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001592 int i;
1593
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001594 for (i = 0; i < req->data_iovs; i++) {
Mitko Haralanov9565c6a2016-05-19 05:21:18 -07001595 node = req->iovs[i].node;
1596 if (!node)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001597 continue;
1598
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001599 if (unpin)
Dean Luicke0b09ac2016-07-28 15:21:20 -04001600 hfi1_mmu_rb_remove(req->pq->handler,
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001601 &node->rb);
1602 else
1603 atomic_dec(&node->refcount);
1604 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001605 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001606 kfree(req->tids);
Dean Luick7b3256e2016-07-28 15:21:18 -04001607 clear_bit(req->info.comp_idx, req->pq->req_in_use);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001608}
1609
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001610static inline void set_comp_state(struct hfi1_user_sdma_pkt_q *pq,
1611 struct hfi1_user_sdma_comp_q *cq,
1612 u16 idx, enum hfi1_sdma_comp_state state,
1613 int ret)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001614{
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001615 hfi1_cdbg(SDMA, "[%u:%u:%u:%u] Setting completion status %u %d",
1616 pq->dd->unit, pq->ctxt, pq->subctxt, idx, state, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001617 if (state == ERROR)
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001618 cq->comps[idx].errcode = -ret;
Michael J. Ruhl0519c522017-03-20 17:24:45 -07001619 smp_wmb(); /* make sure errcode is visible first */
1620 cq->comps[idx].status = state;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001621 trace_hfi1_sdma_user_completion(pq->dd, pq->ctxt, pq->subctxt,
1622 idx, state, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001623}
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001624
1625static bool sdma_rb_filter(struct mmu_rb_node *node, unsigned long addr,
1626 unsigned long len)
1627{
1628 return (bool)(node->addr == addr);
1629}
1630
Dean Luicke0b09ac2016-07-28 15:21:20 -04001631static int sdma_rb_insert(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001632{
1633 struct sdma_mmu_node *node =
1634 container_of(mnode, struct sdma_mmu_node, rb);
1635
1636 atomic_inc(&node->refcount);
1637 return 0;
1638}
1639
Dean Luickb7df1922016-07-28 15:21:23 -04001640/*
1641 * Return 1 to remove the node from the rb tree and call the remove op.
1642 *
1643 * Called with the rb tree lock held.
1644 */
1645static int sdma_rb_evict(void *arg, struct mmu_rb_node *mnode,
1646 void *evict_arg, bool *stop)
1647{
1648 struct sdma_mmu_node *node =
1649 container_of(mnode, struct sdma_mmu_node, rb);
1650 struct evict_data *evict_data = evict_arg;
1651
1652 /* is this node still being used? */
1653 if (atomic_read(&node->refcount))
1654 return 0; /* keep this node */
1655
1656 /* this node will be evicted, add its pages to our count */
1657 evict_data->cleared += node->npages;
1658
1659 /* have enough pages been cleared? */
1660 if (evict_data->cleared >= evict_data->target)
1661 *stop = true;
1662
1663 return 1; /* remove this node */
1664}
1665
Dean Luick082b3532016-07-28 15:21:25 -04001666static void sdma_rb_remove(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001667{
1668 struct sdma_mmu_node *node =
1669 container_of(mnode, struct sdma_mmu_node, rb);
1670
Dean Luickb7df1922016-07-28 15:21:23 -04001671 atomic_sub(node->npages, &node->pq->n_locked);
Mitko Haralanov5511d782016-03-08 11:15:44 -08001672
Dean Luickb85ced92016-07-28 15:21:24 -04001673 unpin_vector_pages(node->pq->mm, node->pages, 0, node->npages);
1674
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001675 kfree(node);
1676}
1677
Dean Luicke0b09ac2016-07-28 15:21:20 -04001678static int sdma_rb_invalidate(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001679{
1680 struct sdma_mmu_node *node =
1681 container_of(mnode, struct sdma_mmu_node, rb);
1682
1683 if (!atomic_read(&node->refcount))
1684 return 1;
1685 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001686}