blob: 79450cf2a3d5417d9c20c70e1417b5715ae29002 [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{
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700377 int ret = -ENOMEM;
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. Ruhl62239fc2017-05-04 05:15:21 -0700384 if (!uctxt || !fd)
385 return -EBADF;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400386
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700387 if (!hfi1_sdma_comp_ring_size)
388 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400389
390 dd = uctxt->dd;
391
392 pq = kzalloc(sizeof(*pq), GFP_KERNEL);
Alison Schofield806e6e12015-10-12 14:28:36 -0700393 if (!pq)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700394 return -ENOMEM;
Dean Luick7b3256e2016-07-28 15:21:18 -0400395
Mike Marciniszyn77241052015-07-30 15:17:43 -0400396 INIT_LIST_HEAD(&pq->list);
397 pq->dd = dd;
398 pq->ctxt = uctxt->ctxt;
Ira Weiny9e10af42015-10-30 18:58:40 -0400399 pq->subctxt = fd->subctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400400 pq->n_max_reqs = hfi1_sdma_comp_ring_size;
401 pq->state = SDMA_PKT_Q_INACTIVE;
402 atomic_set(&pq->n_reqs, 0);
Mitko Haralanova0d40692015-12-08 17:10:13 -0500403 init_waitqueue_head(&pq->wait);
Dean Luickb7df1922016-07-28 15:21:23 -0400404 atomic_set(&pq->n_locked, 0);
Ira Weiny3faa3d92016-07-28 15:21:19 -0400405 pq->mm = fd->mm;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400406
407 iowait_init(&pq->busy, 0, NULL, defer_packet_queue,
Mike Marciniszyna545f532016-02-14 12:45:53 -0800408 activate_packet_queue, NULL);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400409 pq->reqidx = 0;
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700410
411 pq->reqs = kcalloc(hfi1_sdma_comp_ring_size,
412 sizeof(*pq->reqs),
413 GFP_KERNEL);
414 if (!pq->reqs)
415 goto pq_reqs_nomem;
416
417 pq->req_in_use = kcalloc(BITS_TO_LONGS(hfi1_sdma_comp_ring_size),
418 sizeof(*pq->req_in_use),
419 GFP_KERNEL);
420 if (!pq->req_in_use)
421 goto pq_reqs_no_in_use;
422
Mike Marciniszyn77241052015-07-30 15:17:43 -0400423 snprintf(buf, 64, "txreq-kmem-cache-%u-%u-%u", dd->unit, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -0400424 fd->subctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400425 pq->txreq_cache = kmem_cache_create(buf,
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700426 sizeof(struct user_sdma_txreq),
Mike Marciniszyn77241052015-07-30 15:17:43 -0400427 L1_CACHE_BYTES,
428 SLAB_HWCACHE_ALIGN,
429 sdma_kmem_cache_ctor);
430 if (!pq->txreq_cache) {
431 dd_dev_err(dd, "[%u] Failed to allocate TxReq cache\n",
432 uctxt->ctxt);
433 goto pq_txreq_nomem;
434 }
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700435
Mike Marciniszyn77241052015-07-30 15:17:43 -0400436 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
Alison Schofield806e6e12015-10-12 14:28:36 -0700437 if (!cq)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400438 goto cq_nomem;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400439
Markus Elfringe036c202017-02-10 08:50:45 +0100440 cq->comps = vmalloc_user(PAGE_ALIGN(sizeof(*cq->comps)
441 * hfi1_sdma_comp_ring_size));
Alison Schofield806e6e12015-10-12 14:28:36 -0700442 if (!cq->comps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400443 goto cq_comps_nomem;
Alison Schofield806e6e12015-10-12 14:28:36 -0700444
Mike Marciniszyn77241052015-07-30 15:17:43 -0400445 cq->nentries = hfi1_sdma_comp_ring_size;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400446
Dean Luickb85ced92016-07-28 15:21:24 -0400447 ret = hfi1_mmu_rb_register(pq, pq->mm, &sdma_rb_ops, dd->pport->hfi1_wq,
448 &pq->handler);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800449 if (ret) {
450 dd_dev_err(dd, "Failed to register with MMU %d", ret);
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700451 goto pq_mmu_fail;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800452 }
453
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700454 fd->pq = pq;
455 fd->cq = cq;
456
Mike Marciniszyn77241052015-07-30 15:17:43 -0400457 spin_lock_irqsave(&uctxt->sdma_qlock, flags);
458 list_add(&pq->list, &uctxt->sdma_queues);
459 spin_unlock_irqrestore(&uctxt->sdma_qlock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400460
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700461 return 0;
462
463pq_mmu_fail:
464 vfree(cq->comps);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400465cq_comps_nomem:
466 kfree(cq);
467cq_nomem:
468 kmem_cache_destroy(pq->txreq_cache);
469pq_txreq_nomem:
Dean Luick7b3256e2016-07-28 15:21:18 -0400470 kfree(pq->req_in_use);
471pq_reqs_no_in_use:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400472 kfree(pq->reqs);
473pq_reqs_nomem:
474 kfree(pq);
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700475
Mike Marciniszyn77241052015-07-30 15:17:43 -0400476 return ret;
477}
478
479int hfi1_user_sdma_free_queues(struct hfi1_filedata *fd)
480{
481 struct hfi1_ctxtdata *uctxt = fd->uctxt;
482 struct hfi1_user_sdma_pkt_q *pq;
483 unsigned long flags;
484
485 hfi1_cdbg(SDMA, "[%u:%u:%u] Freeing user SDMA queues", uctxt->dd->unit,
486 uctxt->ctxt, fd->subctxt);
487 pq = fd->pq;
488 if (pq) {
Dean Luicke0b09ac2016-07-28 15:21:20 -0400489 if (pq->handler)
490 hfi1_mmu_rb_unregister(pq->handler);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400491 spin_lock_irqsave(&uctxt->sdma_qlock, flags);
492 if (!list_empty(&pq->list))
493 list_del_init(&pq->list);
494 spin_unlock_irqrestore(&uctxt->sdma_qlock, flags);
495 iowait_sdma_drain(&pq->busy);
Mitko Haralanova0d40692015-12-08 17:10:13 -0500496 /* Wait until all requests have been freed. */
497 wait_event_interruptible(
498 pq->wait,
499 (ACCESS_ONCE(pq->state) == SDMA_PKT_Q_INACTIVE));
500 kfree(pq->reqs);
Dean Luick7b3256e2016-07-28 15:21:18 -0400501 kfree(pq->req_in_use);
Julia Lawalladad44d2015-09-13 14:15:04 +0200502 kmem_cache_destroy(pq->txreq_cache);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400503 kfree(pq);
504 fd->pq = NULL;
505 }
506 if (fd->cq) {
Bhumika Goyala4d7d052016-02-14 20:34:28 +0530507 vfree(fd->cq->comps);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400508 kfree(fd->cq);
509 fd->cq = NULL;
510 }
511 return 0;
512}
513
Jianxin Xiong14833b82016-07-01 16:01:56 -0700514static u8 dlid_to_selector(u16 dlid)
515{
516 static u8 mapping[256];
517 static int initialized;
518 static u8 next;
519 int hash;
520
521 if (!initialized) {
522 memset(mapping, 0xFF, 256);
523 initialized = 1;
524 }
525
526 hash = ((dlid >> 8) ^ dlid) & 0xFF;
527 if (mapping[hash] == 0xFF) {
528 mapping[hash] = next;
529 next = (next + 1) & 0x7F;
530 }
531
532 return mapping[hash];
533}
534
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700535int hfi1_user_sdma_process_request(struct hfi1_filedata *fd,
536 struct iovec *iovec, unsigned long dim,
537 unsigned long *count)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400538{
Dean Luickff4ce9b2016-07-28 12:27:34 -0400539 int ret = 0, i;
Ira Weiny9e10af42015-10-30 18:58:40 -0400540 struct hfi1_ctxtdata *uctxt = fd->uctxt;
541 struct hfi1_user_sdma_pkt_q *pq = fd->pq;
542 struct hfi1_user_sdma_comp_q *cq = fd->cq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400543 struct hfi1_devdata *dd = pq->dd;
544 unsigned long idx = 0;
545 u8 pcount = initial_pkt_count;
546 struct sdma_req_info info;
547 struct user_sdma_request *req;
548 u8 opcode, sc, vl;
Jianxin Xiongb583faf2016-05-19 05:21:57 -0700549 int req_queued = 0;
Jianxin Xiong14833b82016-07-01 16:01:56 -0700550 u16 dlid;
Tadeusz Struk0cb2aa62016-09-25 07:44:23 -0700551 u32 selector;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400552
553 if (iovec[idx].iov_len < sizeof(info) + sizeof(req->hdr)) {
554 hfi1_cdbg(
555 SDMA,
556 "[%u:%u:%u] First vector not big enough for header %lu/%lu",
Ira Weiny9e10af42015-10-30 18:58:40 -0400557 dd->unit, uctxt->ctxt, fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400558 iovec[idx].iov_len, sizeof(info) + sizeof(req->hdr));
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500559 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400560 }
561 ret = copy_from_user(&info, iovec[idx].iov_base, sizeof(info));
562 if (ret) {
563 hfi1_cdbg(SDMA, "[%u:%u:%u] Failed to copy info QW (%d)",
Ira Weiny9e10af42015-10-30 18:58:40 -0400564 dd->unit, uctxt->ctxt, fd->subctxt, ret);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500565 return -EFAULT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400566 }
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800567
Ira Weiny9e10af42015-10-30 18:58:40 -0400568 trace_hfi1_sdma_user_reqinfo(dd, uctxt->ctxt, fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400569 (u16 *)&info);
Dean Luick4fa0d222016-07-28 15:21:14 -0400570
571 if (info.comp_idx >= hfi1_sdma_comp_ring_size) {
572 hfi1_cdbg(SDMA,
573 "[%u:%u:%u:%u] Invalid comp index",
574 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx);
575 return -EINVAL;
576 }
577
Dean Luick9ff73c82016-07-28 15:21:15 -0400578 /*
579 * Sanity check the header io vector count. Need at least 1 vector
580 * (header) and cannot be larger than the actual io vector count.
581 */
582 if (req_iovcnt(info.ctrl) < 1 || req_iovcnt(info.ctrl) > dim) {
583 hfi1_cdbg(SDMA,
584 "[%u:%u:%u:%u] Invalid iov count %d, dim %ld",
585 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx,
586 req_iovcnt(info.ctrl), dim);
587 return -EINVAL;
588 }
589
Mike Marciniszyn77241052015-07-30 15:17:43 -0400590 if (!info.fragsize) {
591 hfi1_cdbg(SDMA,
592 "[%u:%u:%u:%u] Request does not specify fragsize",
Ira Weiny9e10af42015-10-30 18:58:40 -0400593 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500594 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400595 }
Dean Luick7b3256e2016-07-28 15:21:18 -0400596
597 /* Try to claim the request. */
598 if (test_and_set_bit(info.comp_idx, pq->req_in_use)) {
599 hfi1_cdbg(SDMA, "[%u:%u:%u] Entry %u is in use",
600 dd->unit, uctxt->ctxt, fd->subctxt,
601 info.comp_idx);
602 return -EBADSLT;
603 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400604 /*
Dean Luick7b3256e2016-07-28 15:21:18 -0400605 * All safety checks have been done and this request has been claimed.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400606 */
607 hfi1_cdbg(SDMA, "[%u:%u:%u] Using req/comp entry %u\n", dd->unit,
Ira Weiny9e10af42015-10-30 18:58:40 -0400608 uctxt->ctxt, fd->subctxt, info.comp_idx);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400609 req = pq->reqs + info.comp_idx;
Dean Luick9ff73c82016-07-28 15:21:15 -0400610 req->data_iovs = req_iovcnt(info.ctrl) - 1; /* subtract header vector */
Sebastian Sanchezceb26562017-05-12 09:19:36 -0700611 req->data_len = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400612 req->pq = pq;
613 req->cq = cq;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500614 req->status = -1;
Sebastian Sanchez780a4c12017-05-04 05:14:51 -0700615 req->ahg_idx = -1;
Sebastian Sanchezceb26562017-05-12 09:19:36 -0700616 req->iov_idx = 0;
617 req->sent = 0;
618 req->seqnum = 0;
619 req->seqcomp = 0;
620 req->seqsubmitted = 0;
621 req->flags = 0;
622 req->tids = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400623 INIT_LIST_HEAD(&req->txps);
Mitko Haralanova0d40692015-12-08 17:10:13 -0500624
Mike Marciniszyn77241052015-07-30 15:17:43 -0400625 memcpy(&req->info, &info, sizeof(info));
626
Dean Luick9ff73c82016-07-28 15:21:15 -0400627 if (req_opcode(info.ctrl) == EXPECTED) {
628 /* expected must have a TID info and at least one data vector */
629 if (req->data_iovs < 2) {
630 SDMA_DBG(req,
631 "Not enough vectors for expected request");
632 ret = -EINVAL;
633 goto free_req;
634 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400635 req->data_iovs--;
Dean Luick9ff73c82016-07-28 15:21:15 -0400636 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400637
638 if (!info.npkts || req->data_iovs > MAX_VECTORS_PER_REQ) {
639 SDMA_DBG(req, "Too many vectors (%u/%u)", req->data_iovs,
640 MAX_VECTORS_PER_REQ);
Dean Luick9da7e9a2016-07-28 15:21:17 -0400641 ret = -EINVAL;
642 goto free_req;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400643 }
644 /* Copy the header from the user buffer */
645 ret = copy_from_user(&req->hdr, iovec[idx].iov_base + sizeof(info),
646 sizeof(req->hdr));
647 if (ret) {
648 SDMA_DBG(req, "Failed to copy header template (%d)", ret);
649 ret = -EFAULT;
650 goto free_req;
651 }
652
653 /* If Static rate control is not enabled, sanitize the header. */
654 if (!HFI1_CAP_IS_USET(STATIC_RATE_CTRL))
655 req->hdr.pbc[2] = 0;
656
657 /* Validate the opcode. Do not trust packets from user space blindly. */
658 opcode = (be32_to_cpu(req->hdr.bth[0]) >> 24) & 0xff;
659 if ((opcode & USER_OPCODE_CHECK_MASK) !=
660 USER_OPCODE_CHECK_VAL) {
661 SDMA_DBG(req, "Invalid opcode (%d)", opcode);
662 ret = -EINVAL;
663 goto free_req;
664 }
665 /*
666 * Validate the vl. Do not trust packets from user space blindly.
667 * VL comes from PBC, SC comes from LRH, and the VL needs to
668 * match the SC look up.
669 */
670 vl = (le16_to_cpu(req->hdr.pbc[0]) >> 12) & 0xF;
671 sc = (((be16_to_cpu(req->hdr.lrh[0]) >> 12) & 0xF) |
672 (((le16_to_cpu(req->hdr.pbc[1]) >> 14) & 0x1) << 4));
673 if (vl >= dd->pport->vls_operational ||
674 vl != sc_to_vlt(dd, sc)) {
675 SDMA_DBG(req, "Invalid SC(%u)/VL(%u)", sc, vl);
676 ret = -EINVAL;
677 goto free_req;
678 }
679
Sebastian Sancheze38d1e42016-04-12 11:22:21 -0700680 /* Checking P_KEY for requests from user-space */
681 if (egress_pkey_check(dd->pport, req->hdr.lrh, req->hdr.bth, sc,
682 PKEY_CHECK_INVALID)) {
683 ret = -EINVAL;
684 goto free_req;
685 }
686
Mike Marciniszyn77241052015-07-30 15:17:43 -0400687 /*
688 * Also should check the BTH.lnh. If it says the next header is GRH then
689 * the RXE parsing will be off and will land in the middle of the KDETH
690 * or miss it entirely.
691 */
692 if ((be16_to_cpu(req->hdr.lrh[0]) & 0x3) == HFI1_LRH_GRH) {
693 SDMA_DBG(req, "User tried to pass in a GRH");
694 ret = -EINVAL;
695 goto free_req;
696 }
697
698 req->koffset = le32_to_cpu(req->hdr.kdeth.swdata[6]);
Jubin John4d114fd2016-02-14 20:21:43 -0800699 /*
700 * Calculate the initial TID offset based on the values of
701 * KDETH.OFFSET and KDETH.OM that are passed in.
702 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400703 req->tidoffset = KDETH_GET(req->hdr.kdeth.ver_tid_offset, OFFSET) *
704 (KDETH_GET(req->hdr.kdeth.ver_tid_offset, OM) ?
705 KDETH_OM_LARGE : KDETH_OM_SMALL);
706 SDMA_DBG(req, "Initial TID offset %u", req->tidoffset);
707 idx++;
708
709 /* Save all the IO vector structures */
Dean Luickff4ce9b2016-07-28 12:27:34 -0400710 for (i = 0; i < req->data_iovs; i++) {
Sebastian Sanchezceb26562017-05-12 09:19:36 -0700711 req->iovs[i].offset = 0;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800712 INIT_LIST_HEAD(&req->iovs[i].list);
Markus Elfringdb6f0282017-02-10 21:45:38 +0100713 memcpy(&req->iovs[i].iov,
714 iovec + idx++,
715 sizeof(req->iovs[i].iov));
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800716 ret = pin_vector_pages(req, &req->iovs[i]);
717 if (ret) {
Sebastian Sanchezceb26562017-05-12 09:19:36 -0700718 req->data_iovs = i;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800719 req->status = ret;
720 goto free_req;
721 }
Dean Luickff4ce9b2016-07-28 12:27:34 -0400722 req->data_len += req->iovs[i].iov.iov_len;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400723 }
724 SDMA_DBG(req, "total data length %u", req->data_len);
725
726 if (pcount > req->info.npkts)
727 pcount = req->info.npkts;
728 /*
729 * Copy any TID info
730 * User space will provide the TID info only when the
731 * request type is EXPECTED. This is true even if there is
732 * only one packet in the request and the header is already
733 * setup. The reason for the singular TID case is that the
734 * driver needs to perform safety checks.
735 */
736 if (req_opcode(req->info.ctrl) == EXPECTED) {
737 u16 ntids = iovec[idx].iov_len / sizeof(*req->tids);
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800738 u32 *tmp;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400739
740 if (!ntids || ntids > MAX_TID_PAIR_ENTRIES) {
741 ret = -EINVAL;
742 goto free_req;
743 }
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800744
Mike Marciniszyn77241052015-07-30 15:17:43 -0400745 /*
746 * We have to copy all of the tids because they may vary
747 * in size and, therefore, the TID count might not be
748 * equal to the pkt count. However, there is no way to
749 * tell at this point.
750 */
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800751 tmp = memdup_user(iovec[idx].iov_base,
752 ntids * sizeof(*req->tids));
753 if (IS_ERR(tmp)) {
754 ret = PTR_ERR(tmp);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400755 SDMA_DBG(req, "Failed to copy %d TIDs (%d)",
756 ntids, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400757 goto free_req;
758 }
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800759 req->tids = tmp;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400760 req->n_tids = ntids;
Sebastian Sanchezceb26562017-05-12 09:19:36 -0700761 req->tididx = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400762 idx++;
763 }
764
Jianxin Xiong14833b82016-07-01 16:01:56 -0700765 dlid = be16_to_cpu(req->hdr.lrh[1]);
766 selector = dlid_to_selector(dlid);
Tadeusz Struk0cb2aa62016-09-25 07:44:23 -0700767 selector += uctxt->ctxt + fd->subctxt;
768 req->sde = sdma_select_user_engine(dd, selector, vl);
Jianxin Xiong14833b82016-07-01 16:01:56 -0700769
Mike Marciniszyn77241052015-07-30 15:17:43 -0400770 if (!req->sde || !sdma_running(req->sde)) {
771 ret = -ECOMM;
772 goto free_req;
773 }
774
775 /* We don't need an AHG entry if the request contains only one packet */
Sebastian Sanchez780a4c12017-05-04 05:14:51 -0700776 if (req->info.npkts > 1 && HFI1_CAP_IS_USET(SDMA_AHG))
777 req->ahg_idx = sdma_ahg_alloc(req->sde);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400778
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800779 set_comp_state(pq, cq, info.comp_idx, QUEUED, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400780 atomic_inc(&pq->n_reqs);
Jianxin Xiongb583faf2016-05-19 05:21:57 -0700781 req_queued = 1;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800782 /* Send the first N packets in the request to buy us some time */
783 ret = user_sdma_send_pkts(req, pcount);
784 if (unlikely(ret < 0 && ret != -EBUSY)) {
785 req->status = ret;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800786 goto free_req;
787 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400788
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800789 /*
790 * It is possible that the SDMA engine would have processed all the
791 * submitted packets by the time we get here. Therefore, only set
792 * packet queue state to ACTIVE if there are still uncompleted
793 * requests.
794 */
795 if (atomic_read(&pq->n_reqs))
796 xchg(&pq->state, SDMA_PKT_Q_ACTIVE);
797
798 /*
799 * This is a somewhat blocking send implementation.
800 * The driver will block the caller until all packets of the
801 * request have been submitted to the SDMA engine. However, it
802 * will not wait for send completions.
803 */
804 while (!test_bit(SDMA_REQ_SEND_DONE, &req->flags)) {
805 ret = user_sdma_send_pkts(req, pcount);
806 if (ret < 0) {
807 if (ret != -EBUSY) {
808 req->status = ret;
809 set_bit(SDMA_REQ_DONE_ERROR, &req->flags);
Mitko Haralanova402d6a2016-02-03 14:37:41 -0800810 if (ACCESS_ONCE(req->seqcomp) ==
811 req->seqsubmitted - 1)
812 goto free_req;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800813 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400814 }
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800815 wait_event_interruptible_timeout(
816 pq->busy.wait_dma,
817 (pq->state == SDMA_PKT_Q_ACTIVE),
818 msecs_to_jiffies(
819 SDMA_IOWAIT_TIMEOUT));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400820 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400821 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400822 *count += idx;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500823 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400824free_req:
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800825 user_sdma_free_request(req, true);
Jianxin Xiongb583faf2016-05-19 05:21:57 -0700826 if (req_queued)
827 pq_update(pq);
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800828 set_comp_state(pq, cq, info.comp_idx, ERROR, req->status);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400829 return ret;
830}
831
832static inline u32 compute_data_length(struct user_sdma_request *req,
Jubin John17fb4f22016-02-14 20:21:52 -0800833 struct user_sdma_txreq *tx)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400834{
835 /*
836 * Determine the proper size of the packet data.
837 * The size of the data of the first packet is in the header
838 * template. However, it includes the header and ICRC, which need
839 * to be subtracted.
Ira Weinyc4929802016-07-27 21:08:42 -0400840 * The minimum representable packet data length in a header is 4 bytes,
841 * therefore, when the data length request is less than 4 bytes, there's
842 * only one packet, and the packet data length is equal to that of the
843 * request data length.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400844 * The size of the remaining packets is the minimum of the frag
845 * size (MTU) or remaining data in the request.
846 */
847 u32 len;
848
849 if (!req->seqnum) {
Ira Weinyc4929802016-07-27 21:08:42 -0400850 if (req->data_len < sizeof(u32))
851 len = req->data_len;
852 else
853 len = ((be16_to_cpu(req->hdr.lrh[2]) << 2) -
854 (sizeof(tx->hdr) - 4));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400855 } else if (req_opcode(req->info.ctrl) == EXPECTED) {
856 u32 tidlen = EXP_TID_GET(req->tids[req->tididx], LEN) *
857 PAGE_SIZE;
Jubin John4d114fd2016-02-14 20:21:43 -0800858 /*
859 * Get the data length based on the remaining space in the
860 * TID pair.
861 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400862 len = min(tidlen - req->tidoffset, (u32)req->info.fragsize);
863 /* If we've filled up the TID pair, move to the next one. */
864 if (unlikely(!len) && ++req->tididx < req->n_tids &&
865 req->tids[req->tididx]) {
866 tidlen = EXP_TID_GET(req->tids[req->tididx],
867 LEN) * PAGE_SIZE;
868 req->tidoffset = 0;
869 len = min_t(u32, tidlen, req->info.fragsize);
870 }
Jubin John4d114fd2016-02-14 20:21:43 -0800871 /*
872 * Since the TID pairs map entire pages, make sure that we
Mike Marciniszyn77241052015-07-30 15:17:43 -0400873 * are not going to try to send more data that we have
Jubin John4d114fd2016-02-14 20:21:43 -0800874 * remaining.
875 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400876 len = min(len, req->data_len - req->sent);
Jubin Johne4909742016-02-14 20:22:00 -0800877 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400878 len = min(req->data_len - req->sent, (u32)req->info.fragsize);
Jubin Johne4909742016-02-14 20:22:00 -0800879 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400880 SDMA_DBG(req, "Data Length = %u", len);
881 return len;
882}
883
Ira Weinyc4929802016-07-27 21:08:42 -0400884static inline u32 pad_len(u32 len)
885{
886 if (len & (sizeof(u32) - 1))
887 len += sizeof(u32) - (len & (sizeof(u32) - 1));
888 return len;
889}
890
Mike Marciniszyn77241052015-07-30 15:17:43 -0400891static inline u32 get_lrh_len(struct hfi1_pkt_header hdr, u32 len)
892{
893 /* (Size of complete header - size of PBC) + 4B ICRC + data length */
894 return ((sizeof(hdr) - sizeof(hdr.pbc)) + 4 + len);
895}
896
897static int user_sdma_send_pkts(struct user_sdma_request *req, unsigned maxpkts)
898{
Harish Chegondi0b115ef2016-09-06 04:35:37 -0700899 int ret = 0, count;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400900 unsigned npkts = 0;
901 struct user_sdma_txreq *tx = NULL;
902 struct hfi1_user_sdma_pkt_q *pq = NULL;
903 struct user_sdma_iovec *iovec = NULL;
904
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500905 if (!req->pq)
906 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400907
908 pq = req->pq;
909
Mitko Haralanov6a5464f2015-12-08 17:10:12 -0500910 /* If tx completion has reported an error, we are done. */
911 if (test_bit(SDMA_REQ_HAS_ERROR, &req->flags)) {
912 set_bit(SDMA_REQ_DONE_ERROR, &req->flags);
913 return -EFAULT;
914 }
915
Mike Marciniszyn77241052015-07-30 15:17:43 -0400916 /*
917 * Check if we might have sent the entire request already
918 */
919 if (unlikely(req->seqnum == req->info.npkts)) {
920 if (!list_empty(&req->txps))
921 goto dosend;
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500922 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400923 }
924
925 if (!maxpkts || maxpkts > req->info.npkts - req->seqnum)
926 maxpkts = req->info.npkts - req->seqnum;
927
928 while (npkts < maxpkts) {
929 u32 datalen = 0, queued = 0, data_sent = 0;
930 u64 iov_offset = 0;
931
932 /*
933 * Check whether any of the completions have come back
934 * with errors. If so, we are not going to process any
935 * more packets from this request.
936 */
937 if (test_bit(SDMA_REQ_HAS_ERROR, &req->flags)) {
938 set_bit(SDMA_REQ_DONE_ERROR, &req->flags);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500939 return -EFAULT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400940 }
941
942 tx = kmem_cache_alloc(pq->txreq_cache, GFP_KERNEL);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500943 if (!tx)
944 return -ENOMEM;
945
Mike Marciniszyn77241052015-07-30 15:17:43 -0400946 tx->flags = 0;
947 tx->req = req;
948 tx->busycount = 0;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500949 INIT_LIST_HEAD(&tx->list);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400950
Jakub Pawlake7301392016-12-07 19:32:41 -0800951 /*
952 * For the last packet set the ACK request
953 * and disable header suppression.
954 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400955 if (req->seqnum == req->info.npkts - 1)
Jakub Pawlake7301392016-12-07 19:32:41 -0800956 tx->flags |= (TXREQ_FLAGS_REQ_ACK |
957 TXREQ_FLAGS_REQ_DISABLE_SH);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400958
959 /*
960 * Calculate the payload size - this is min of the fragment
961 * (MTU) size or the remaining bytes in the request but only
962 * if we have payload data.
963 */
964 if (req->data_len) {
965 iovec = &req->iovs[req->iov_idx];
966 if (ACCESS_ONCE(iovec->offset) == iovec->iov.iov_len) {
967 if (++req->iov_idx == req->data_iovs) {
968 ret = -EFAULT;
969 goto free_txreq;
970 }
971 iovec = &req->iovs[req->iov_idx];
972 WARN_ON(iovec->offset);
973 }
974
Mike Marciniszyn77241052015-07-30 15:17:43 -0400975 datalen = compute_data_length(req, tx);
Jakub Pawlake7301392016-12-07 19:32:41 -0800976
977 /*
978 * Disable header suppression for the payload <= 8DWS.
979 * If there is an uncorrectable error in the receive
980 * data FIFO when the received payload size is less than
981 * or equal to 8DWS then the RxDmaDataFifoRdUncErr is
982 * not reported.There is set RHF.EccErr if the header
983 * is not suppressed.
984 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400985 if (!datalen) {
986 SDMA_DBG(req,
987 "Request has data but pkt len is 0");
988 ret = -EFAULT;
989 goto free_tx;
Jakub Pawlake7301392016-12-07 19:32:41 -0800990 } else if (datalen <= 32) {
991 tx->flags |= TXREQ_FLAGS_REQ_DISABLE_SH;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400992 }
993 }
994
Sebastian Sanchez780a4c12017-05-04 05:14:51 -0700995 if (req->ahg_idx >= 0) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400996 if (!req->seqnum) {
997 u16 pbclen = le16_to_cpu(req->hdr.pbc[0]);
Ira Weinyc4929802016-07-27 21:08:42 -0400998 u32 lrhlen = get_lrh_len(req->hdr,
999 pad_len(datalen));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001000 /*
1001 * Copy the request header into the tx header
1002 * because the HW needs a cacheline-aligned
1003 * address.
1004 * This copy can be optimized out if the hdr
1005 * member of user_sdma_request were also
1006 * cacheline aligned.
1007 */
1008 memcpy(&tx->hdr, &req->hdr, sizeof(tx->hdr));
1009 if (PBC2LRH(pbclen) != lrhlen) {
1010 pbclen = (pbclen & 0xf000) |
1011 LRH2PBC(lrhlen);
1012 tx->hdr.pbc[0] = cpu_to_le16(pbclen);
1013 }
Jakub Pawlake7301392016-12-07 19:32:41 -08001014 ret = check_header_template(req, &tx->hdr,
1015 lrhlen, datalen);
1016 if (ret)
1017 goto free_tx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001018 ret = sdma_txinit_ahg(&tx->txreq,
1019 SDMA_TXREQ_F_AHG_COPY,
1020 sizeof(tx->hdr) + datalen,
1021 req->ahg_idx, 0, NULL, 0,
1022 user_sdma_txreq_cb);
1023 if (ret)
1024 goto free_tx;
1025 ret = sdma_txadd_kvaddr(pq->dd, &tx->txreq,
1026 &tx->hdr,
1027 sizeof(tx->hdr));
1028 if (ret)
1029 goto free_txreq;
1030 } else {
1031 int changes;
1032
1033 changes = set_txreq_header_ahg(req, tx,
1034 datalen);
1035 if (changes < 0)
1036 goto free_tx;
1037 sdma_txinit_ahg(&tx->txreq,
1038 SDMA_TXREQ_F_USE_AHG,
1039 datalen, req->ahg_idx, changes,
1040 req->ahg, sizeof(req->hdr),
1041 user_sdma_txreq_cb);
1042 }
1043 } else {
1044 ret = sdma_txinit(&tx->txreq, 0, sizeof(req->hdr) +
1045 datalen, user_sdma_txreq_cb);
1046 if (ret)
1047 goto free_tx;
1048 /*
1049 * Modify the header for this packet. This only needs
1050 * to be done if we are not going to use AHG. Otherwise,
1051 * the HW will do it based on the changes we gave it
1052 * during sdma_txinit_ahg().
1053 */
1054 ret = set_txreq_header(req, tx, datalen);
1055 if (ret)
1056 goto free_txreq;
1057 }
1058
1059 /*
1060 * If the request contains any data vectors, add up to
1061 * fragsize bytes to the descriptor.
1062 */
1063 while (queued < datalen &&
1064 (req->sent + data_sent) < req->data_len) {
1065 unsigned long base, offset;
1066 unsigned pageidx, len;
1067
1068 base = (unsigned long)iovec->iov.iov_base;
Amitoj Kaur Chawla72a5f6a2016-02-20 19:08:02 +05301069 offset = offset_in_page(base + iovec->offset +
1070 iov_offset);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001071 pageidx = (((iovec->offset + iov_offset +
1072 base) - (base & PAGE_MASK)) >> PAGE_SHIFT);
1073 len = offset + req->info.fragsize > PAGE_SIZE ?
1074 PAGE_SIZE - offset : req->info.fragsize;
1075 len = min((datalen - queued), len);
1076 ret = sdma_txadd_page(pq->dd, &tx->txreq,
1077 iovec->pages[pageidx],
1078 offset, len);
1079 if (ret) {
Mitko Haralanova0d40692015-12-08 17:10:13 -05001080 SDMA_DBG(req, "SDMA txreq add page failed %d\n",
1081 ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001082 goto free_txreq;
1083 }
1084 iov_offset += len;
1085 queued += len;
1086 data_sent += len;
1087 if (unlikely(queued < datalen &&
1088 pageidx == iovec->npages &&
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001089 req->iov_idx < req->data_iovs - 1)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001090 iovec->offset += iov_offset;
1091 iovec = &req->iovs[++req->iov_idx];
Mike Marciniszyn77241052015-07-30 15:17:43 -04001092 iov_offset = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001093 }
1094 }
1095 /*
1096 * The txreq was submitted successfully so we can update
1097 * the counters.
1098 */
1099 req->koffset += datalen;
1100 if (req_opcode(req->info.ctrl) == EXPECTED)
1101 req->tidoffset += datalen;
1102 req->sent += data_sent;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001103 if (req->data_len)
1104 iovec->offset += iov_offset;
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001105 list_add_tail(&tx->txreq.list, &req->txps);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001106 /*
1107 * It is important to increment this here as it is used to
1108 * generate the BTH.PSN and, therefore, can't be bulk-updated
1109 * outside of the loop.
1110 */
1111 tx->seqnum = req->seqnum++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001112 npkts++;
1113 }
1114dosend:
Harish Chegondi0b115ef2016-09-06 04:35:37 -07001115 ret = sdma_send_txlist(req->sde, &pq->busy, &req->txps, &count);
1116 req->seqsubmitted += count;
1117 if (req->seqsubmitted == req->info.npkts) {
1118 set_bit(SDMA_REQ_SEND_DONE, &req->flags);
1119 /*
1120 * The txreq has already been submitted to the HW queue
1121 * so we can free the AHG entry now. Corruption will not
1122 * happen due to the sequential manner in which
1123 * descriptors are processed.
1124 */
Sebastian Sanchez780a4c12017-05-04 05:14:51 -07001125 if (req->ahg_idx >= 0)
Harish Chegondi0b115ef2016-09-06 04:35:37 -07001126 sdma_ahg_free(req->sde, req->ahg_idx);
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001127 }
Mitko Haralanovfaa98b82015-12-08 17:10:11 -05001128 return ret;
1129
Mike Marciniszyn77241052015-07-30 15:17:43 -04001130free_txreq:
1131 sdma_txclean(pq->dd, &tx->txreq);
1132free_tx:
1133 kmem_cache_free(pq->txreq_cache, tx);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001134 return ret;
1135}
1136
1137/*
1138 * How many pages in this iovec element?
1139 */
1140static inline int num_user_pages(const struct iovec *iov)
1141{
Jubin John50e5dcb2016-02-14 20:19:41 -08001142 const unsigned long addr = (unsigned long)iov->iov_base;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001143 const unsigned long len = iov->iov_len;
1144 const unsigned long spage = addr & PAGE_MASK;
1145 const unsigned long epage = (addr + len - 1) & PAGE_MASK;
1146
1147 return 1 + ((epage - spage) >> PAGE_SHIFT);
1148}
1149
Mitko Haralanov5511d782016-03-08 11:15:44 -08001150static u32 sdma_cache_evict(struct hfi1_user_sdma_pkt_q *pq, u32 npages)
1151{
Dean Luickb7df1922016-07-28 15:21:23 -04001152 struct evict_data evict_data;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001153
Dean Luickb7df1922016-07-28 15:21:23 -04001154 evict_data.cleared = 0;
1155 evict_data.target = npages;
1156 hfi1_mmu_rb_evict(pq->handler, &evict_data);
1157 return evict_data.cleared;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001158}
1159
Mike Marciniszyn77241052015-07-30 15:17:43 -04001160static int pin_vector_pages(struct user_sdma_request *req,
Ira Weiny72720dd2016-07-28 12:27:25 -04001161 struct user_sdma_iovec *iovec)
1162{
Mitko Haralanov5511d782016-03-08 11:15:44 -08001163 int ret = 0, pinned, npages, cleared;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001164 struct page **pages;
1165 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1166 struct sdma_mmu_node *node = NULL;
1167 struct mmu_rb_node *rb_node;
Sebastian Sanchez7be85672017-05-26 05:35:12 -07001168 bool extracted;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001169
Sebastian Sanchez7be85672017-05-26 05:35:12 -07001170 extracted =
1171 hfi1_mmu_rb_remove_unless_exact(pq->handler,
1172 (unsigned long)
1173 iovec->iov.iov_base,
1174 iovec->iov.iov_len, &rb_node);
1175 if (rb_node) {
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001176 node = container_of(rb_node, struct sdma_mmu_node, rb);
Sebastian Sanchez7be85672017-05-26 05:35:12 -07001177 if (!extracted) {
1178 atomic_inc(&node->refcount);
1179 iovec->pages = node->pages;
1180 iovec->npages = node->npages;
1181 iovec->node = node;
1182 return 0;
1183 }
1184 }
Mitko Haralanova0d40692015-12-08 17:10:13 -05001185
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001186 if (!node) {
1187 node = kzalloc(sizeof(*node), GFP_KERNEL);
1188 if (!node)
1189 return -ENOMEM;
1190
1191 node->rb.addr = (unsigned long)iovec->iov.iov_base;
Mitko Haralanov5511d782016-03-08 11:15:44 -08001192 node->pq = pq;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001193 atomic_set(&node->refcount, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001194 }
Mitko Haralanova0d40692015-12-08 17:10:13 -05001195
Mike Marciniszyn77241052015-07-30 15:17:43 -04001196 npages = num_user_pages(&iovec->iov);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001197 if (node->npages < npages) {
1198 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
1199 if (!pages) {
1200 SDMA_DBG(req, "Failed page array alloc");
1201 ret = -ENOMEM;
1202 goto bail;
1203 }
1204 memcpy(pages, node->pages, node->npages * sizeof(*pages));
1205
1206 npages -= node->npages;
Mitko Haralanove88c9272016-04-12 10:46:53 -07001207
Mitko Haralanov5511d782016-03-08 11:15:44 -08001208retry:
Dean Luickb7df1922016-07-28 15:21:23 -04001209 if (!hfi1_can_pin_pages(pq->dd, pq->mm,
1210 atomic_read(&pq->n_locked), npages)) {
Mitko Haralanov5511d782016-03-08 11:15:44 -08001211 cleared = sdma_cache_evict(pq, npages);
Mitko Haralanov5511d782016-03-08 11:15:44 -08001212 if (cleared >= npages)
1213 goto retry;
1214 }
Ira Weiny3faa3d92016-07-28 15:21:19 -04001215 pinned = hfi1_acquire_user_pages(pq->mm,
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001216 ((unsigned long)iovec->iov.iov_base +
1217 (node->npages * PAGE_SIZE)), npages, 0,
1218 pages + node->npages);
1219 if (pinned < 0) {
1220 kfree(pages);
1221 ret = pinned;
1222 goto bail;
1223 }
1224 if (pinned != npages) {
Ira Weiny3faa3d92016-07-28 15:21:19 -04001225 unpin_vector_pages(pq->mm, pages, node->npages,
Mitko Haralanov849e3e92016-04-12 10:46:16 -07001226 pinned);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001227 ret = -EFAULT;
1228 goto bail;
1229 }
1230 kfree(node->pages);
Mitko Haralanovde790932016-04-12 10:46:41 -07001231 node->rb.len = iovec->iov.iov_len;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001232 node->pages = pages;
1233 node->npages += pinned;
1234 npages = node->npages;
Dean Luickb7df1922016-07-28 15:21:23 -04001235 atomic_add(pinned, &pq->n_locked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001236 }
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001237 iovec->pages = node->pages;
1238 iovec->npages = npages;
Mitko Haralanov9565c6a2016-05-19 05:21:18 -07001239 iovec->node = node;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001240
Dean Luicke0b09ac2016-07-28 15:21:20 -04001241 ret = hfi1_mmu_rb_insert(req->pq->handler, &node->rb);
Mitko Haralanovf53af852016-04-12 10:46:47 -07001242 if (ret) {
Dean Luickb7df1922016-07-28 15:21:23 -04001243 atomic_sub(node->npages, &pq->n_locked);
Dean Luicka383f8e2016-07-28 15:21:16 -04001244 iovec->node = NULL;
Mitko Haralanovf53af852016-04-12 10:46:47 -07001245 goto bail;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001246 }
1247 return 0;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001248bail:
Mitko Haralanovf53af852016-04-12 10:46:47 -07001249 if (rb_node)
Ira Weiny3faa3d92016-07-28 15:21:19 -04001250 unpin_vector_pages(pq->mm, node->pages, 0, node->npages);
Mitko Haralanovf53af852016-04-12 10:46:47 -07001251 kfree(node);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001252 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001253}
1254
Mitko Haralanovbd3a8942016-03-08 11:15:33 -08001255static void unpin_vector_pages(struct mm_struct *mm, struct page **pages,
Mitko Haralanov849e3e92016-04-12 10:46:16 -07001256 unsigned start, unsigned npages)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001257{
Ira Weiny639297b2016-07-28 12:27:33 -04001258 hfi1_release_user_pages(mm, pages + start, npages, false);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001259 kfree(pages);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001260}
1261
1262static int check_header_template(struct user_sdma_request *req,
1263 struct hfi1_pkt_header *hdr, u32 lrhlen,
1264 u32 datalen)
1265{
1266 /*
1267 * Perform safety checks for any type of packet:
1268 * - transfer size is multiple of 64bytes
Ira Weinyc4929802016-07-27 21:08:42 -04001269 * - packet length is multiple of 4 bytes
Mike Marciniszyn77241052015-07-30 15:17:43 -04001270 * - packet length is not larger than MTU size
1271 *
1272 * These checks are only done for the first packet of the
1273 * transfer since the header is "given" to us by user space.
1274 * For the remainder of the packets we compute the values.
1275 */
Ira Weinyc4929802016-07-27 21:08:42 -04001276 if (req->info.fragsize % PIO_BLOCK_SIZE || lrhlen & 0x3 ||
Mike Marciniszyn77241052015-07-30 15:17:43 -04001277 lrhlen > get_lrh_len(*hdr, req->info.fragsize))
1278 return -EINVAL;
1279
1280 if (req_opcode(req->info.ctrl) == EXPECTED) {
1281 /*
1282 * The header is checked only on the first packet. Furthermore,
1283 * we ensure that at least one TID entry is copied when the
1284 * request is submitted. Therefore, we don't have to verify that
1285 * tididx points to something sane.
1286 */
1287 u32 tidval = req->tids[req->tididx],
1288 tidlen = EXP_TID_GET(tidval, LEN) * PAGE_SIZE,
1289 tididx = EXP_TID_GET(tidval, IDX),
1290 tidctrl = EXP_TID_GET(tidval, CTRL),
1291 tidoff;
1292 __le32 kval = hdr->kdeth.ver_tid_offset;
1293
1294 tidoff = KDETH_GET(kval, OFFSET) *
1295 (KDETH_GET(req->hdr.kdeth.ver_tid_offset, OM) ?
1296 KDETH_OM_LARGE : KDETH_OM_SMALL);
1297 /*
1298 * Expected receive packets have the following
1299 * additional checks:
1300 * - offset is not larger than the TID size
1301 * - TIDCtrl values match between header and TID array
1302 * - TID indexes match between header and TID array
1303 */
1304 if ((tidoff + datalen > tidlen) ||
1305 KDETH_GET(kval, TIDCTRL) != tidctrl ||
1306 KDETH_GET(kval, TID) != tididx)
1307 return -EINVAL;
1308 }
1309 return 0;
1310}
1311
1312/*
1313 * Correctly set the BTH.PSN field based on type of
1314 * transfer - eager packets can just increment the PSN but
1315 * expected packets encode generation and sequence in the
1316 * BTH.PSN field so just incrementing will result in errors.
1317 */
1318static inline u32 set_pkt_bth_psn(__be32 bthpsn, u8 expct, u32 frags)
1319{
1320 u32 val = be32_to_cpu(bthpsn),
1321 mask = (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffffull :
1322 0xffffffull),
1323 psn = val & mask;
1324 if (expct)
1325 psn = (psn & ~BTH_SEQ_MASK) | ((psn + frags) & BTH_SEQ_MASK);
1326 else
1327 psn = psn + frags;
1328 return psn & mask;
1329}
1330
1331static int set_txreq_header(struct user_sdma_request *req,
1332 struct user_sdma_txreq *tx, u32 datalen)
1333{
1334 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1335 struct hfi1_pkt_header *hdr = &tx->hdr;
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001336 u8 omfactor; /* KDETH.OM */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001337 u16 pbclen;
1338 int ret;
Ira Weinyc4929802016-07-27 21:08:42 -04001339 u32 tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(datalen));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001340
1341 /* Copy the header template to the request before modification */
1342 memcpy(hdr, &req->hdr, sizeof(*hdr));
1343
1344 /*
1345 * Check if the PBC and LRH length are mismatched. If so
1346 * adjust both in the header.
1347 */
1348 pbclen = le16_to_cpu(hdr->pbc[0]);
1349 if (PBC2LRH(pbclen) != lrhlen) {
1350 pbclen = (pbclen & 0xf000) | LRH2PBC(lrhlen);
1351 hdr->pbc[0] = cpu_to_le16(pbclen);
1352 hdr->lrh[2] = cpu_to_be16(lrhlen >> 2);
1353 /*
1354 * Third packet
1355 * This is the first packet in the sequence that has
1356 * a "static" size that can be used for the rest of
1357 * the packets (besides the last one).
1358 */
1359 if (unlikely(req->seqnum == 2)) {
1360 /*
1361 * From this point on the lengths in both the
1362 * PBC and LRH are the same until the last
1363 * packet.
1364 * Adjust the template so we don't have to update
1365 * every packet
1366 */
1367 req->hdr.pbc[0] = hdr->pbc[0];
1368 req->hdr.lrh[2] = hdr->lrh[2];
1369 }
1370 }
1371 /*
1372 * We only have to modify the header if this is not the
1373 * first packet in the request. Otherwise, we use the
1374 * header given to us.
1375 */
1376 if (unlikely(!req->seqnum)) {
1377 ret = check_header_template(req, hdr, lrhlen, datalen);
1378 if (ret)
1379 return ret;
1380 goto done;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001381 }
1382
1383 hdr->bth[2] = cpu_to_be32(
1384 set_pkt_bth_psn(hdr->bth[2],
1385 (req_opcode(req->info.ctrl) == EXPECTED),
1386 req->seqnum));
1387
1388 /* Set ACK request on last packet */
Jakub Pawlake7301392016-12-07 19:32:41 -08001389 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_ACK))
Jubin John8638b772016-02-14 20:19:24 -08001390 hdr->bth[2] |= cpu_to_be32(1UL << 31);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001391
1392 /* Set the new offset */
1393 hdr->kdeth.swdata[6] = cpu_to_le32(req->koffset);
1394 /* Expected packets have to fill in the new TID information */
1395 if (req_opcode(req->info.ctrl) == EXPECTED) {
1396 tidval = req->tids[req->tididx];
1397 /*
1398 * If the offset puts us at the end of the current TID,
1399 * advance everything.
1400 */
1401 if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
1402 PAGE_SIZE)) {
1403 req->tidoffset = 0;
Jubin John4d114fd2016-02-14 20:21:43 -08001404 /*
1405 * Since we don't copy all the TIDs, all at once,
1406 * we have to check again.
1407 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001408 if (++req->tididx > req->n_tids - 1 ||
1409 !req->tids[req->tididx]) {
1410 return -EINVAL;
1411 }
1412 tidval = req->tids[req->tididx];
1413 }
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001414 omfactor = EXP_TID_GET(tidval, LEN) * PAGE_SIZE >=
1415 KDETH_OM_MAX_SIZE ? KDETH_OM_LARGE_SHIFT :
1416 KDETH_OM_SMALL_SHIFT;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001417 /* Set KDETH.TIDCtrl based on value for this TID. */
1418 KDETH_SET(hdr->kdeth.ver_tid_offset, TIDCTRL,
1419 EXP_TID_GET(tidval, CTRL));
1420 /* Set KDETH.TID based on value for this TID */
1421 KDETH_SET(hdr->kdeth.ver_tid_offset, TID,
1422 EXP_TID_GET(tidval, IDX));
Jakub Pawlake7301392016-12-07 19:32:41 -08001423 /* Clear KDETH.SH when DISABLE_SH flag is set */
1424 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_DISABLE_SH))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001425 KDETH_SET(hdr->kdeth.ver_tid_offset, SH, 0);
1426 /*
1427 * Set the KDETH.OFFSET and KDETH.OM based on size of
1428 * transfer.
1429 */
1430 SDMA_DBG(req, "TID offset %ubytes %uunits om%u",
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001431 req->tidoffset, req->tidoffset >> omfactor,
1432 omfactor != KDETH_OM_SMALL_SHIFT);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001433 KDETH_SET(hdr->kdeth.ver_tid_offset, OFFSET,
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001434 req->tidoffset >> omfactor);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001435 KDETH_SET(hdr->kdeth.ver_tid_offset, OM,
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001436 omfactor != KDETH_OM_SMALL_SHIFT);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001437 }
1438done:
1439 trace_hfi1_sdma_user_header(pq->dd, pq->ctxt, pq->subctxt,
1440 req->info.comp_idx, hdr, tidval);
1441 return sdma_txadd_kvaddr(pq->dd, &tx->txreq, hdr, sizeof(*hdr));
1442}
1443
1444static int set_txreq_header_ahg(struct user_sdma_request *req,
1445 struct user_sdma_txreq *tx, u32 len)
1446{
1447 int diff = 0;
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001448 u8 omfactor; /* KDETH.OM */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001449 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1450 struct hfi1_pkt_header *hdr = &req->hdr;
1451 u16 pbclen = le16_to_cpu(hdr->pbc[0]);
Ira Weinyc4929802016-07-27 21:08:42 -04001452 u32 val32, tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(len));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001453
1454 if (PBC2LRH(pbclen) != lrhlen) {
1455 /* PBC.PbcLengthDWs */
1456 AHG_HEADER_SET(req->ahg, diff, 0, 0, 12,
1457 cpu_to_le16(LRH2PBC(lrhlen)));
1458 /* LRH.PktLen (we need the full 16 bits due to byte swap) */
1459 AHG_HEADER_SET(req->ahg, diff, 3, 0, 16,
1460 cpu_to_be16(lrhlen >> 2));
1461 }
1462
1463 /*
1464 * Do the common updates
1465 */
1466 /* BTH.PSN and BTH.A */
1467 val32 = (be32_to_cpu(hdr->bth[2]) + req->seqnum) &
1468 (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffff : 0xffffff);
Jakub Pawlake7301392016-12-07 19:32:41 -08001469 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_ACK))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001470 val32 |= 1UL << 31;
1471 AHG_HEADER_SET(req->ahg, diff, 6, 0, 16, cpu_to_be16(val32 >> 16));
1472 AHG_HEADER_SET(req->ahg, diff, 6, 16, 16, cpu_to_be16(val32 & 0xffff));
1473 /* KDETH.Offset */
1474 AHG_HEADER_SET(req->ahg, diff, 15, 0, 16,
1475 cpu_to_le16(req->koffset & 0xffff));
1476 AHG_HEADER_SET(req->ahg, diff, 15, 16, 16,
1477 cpu_to_le16(req->koffset >> 16));
1478 if (req_opcode(req->info.ctrl) == EXPECTED) {
1479 __le16 val;
1480
1481 tidval = req->tids[req->tididx];
1482
1483 /*
1484 * If the offset puts us at the end of the current TID,
1485 * advance everything.
1486 */
1487 if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
1488 PAGE_SIZE)) {
1489 req->tidoffset = 0;
Jubin John4d114fd2016-02-14 20:21:43 -08001490 /*
1491 * Since we don't copy all the TIDs, all at once,
1492 * we have to check again.
1493 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001494 if (++req->tididx > req->n_tids - 1 ||
1495 !req->tids[req->tididx]) {
1496 return -EINVAL;
1497 }
1498 tidval = req->tids[req->tididx];
1499 }
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001500 omfactor = ((EXP_TID_GET(tidval, LEN) *
Mike Marciniszyn77241052015-07-30 15:17:43 -04001501 PAGE_SIZE) >=
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001502 KDETH_OM_MAX_SIZE) ? KDETH_OM_LARGE_SHIFT :
1503 KDETH_OM_SMALL_SHIFT;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001504 /* KDETH.OM and KDETH.OFFSET (TID) */
1505 AHG_HEADER_SET(req->ahg, diff, 7, 0, 16,
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001506 ((!!(omfactor - KDETH_OM_SMALL_SHIFT)) << 15 |
1507 ((req->tidoffset >> omfactor)
1508 & 0x7fff)));
Jakub Pawlake7301392016-12-07 19:32:41 -08001509 /* KDETH.TIDCtrl, KDETH.TID, KDETH.Intr, KDETH.SH */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001510 val = cpu_to_le16(((EXP_TID_GET(tidval, CTRL) & 0x3) << 10) |
Jakub Pawlake7301392016-12-07 19:32:41 -08001511 (EXP_TID_GET(tidval, IDX) & 0x3ff));
1512
1513 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_DISABLE_SH)) {
1514 val |= cpu_to_le16((KDETH_GET(hdr->kdeth.ver_tid_offset,
1515 INTR) <<
1516 AHG_KDETH_INTR_SHIFT));
Jubin Johne4909742016-02-14 20:22:00 -08001517 } else {
Jakub Pawlake7301392016-12-07 19:32:41 -08001518 val |= KDETH_GET(hdr->kdeth.ver_tid_offset, SH) ?
1519 cpu_to_le16(0x1 << AHG_KDETH_SH_SHIFT) :
1520 cpu_to_le16((KDETH_GET(hdr->kdeth.ver_tid_offset,
1521 INTR) <<
1522 AHG_KDETH_INTR_SHIFT));
Jubin Johne4909742016-02-14 20:22:00 -08001523 }
Jakub Pawlake7301392016-12-07 19:32:41 -08001524
1525 AHG_HEADER_SET(req->ahg, diff, 7, 16, 14, val);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001526 }
1527
1528 trace_hfi1_sdma_user_header_ahg(pq->dd, pq->ctxt, pq->subctxt,
1529 req->info.comp_idx, req->sde->this_idx,
1530 req->ahg_idx, req->ahg, diff, tidval);
1531 return diff;
1532}
1533
Mitko Haralanova0d40692015-12-08 17:10:13 -05001534/*
1535 * SDMA tx request completion callback. Called when the SDMA progress
1536 * state machine gets notification that the SDMA descriptors for this
1537 * tx request have been processed by the DMA engine. Called in
1538 * interrupt context.
1539 */
Mike Marciniszyna545f532016-02-14 12:45:53 -08001540static void user_sdma_txreq_cb(struct sdma_txreq *txreq, int status)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001541{
1542 struct user_sdma_txreq *tx =
1543 container_of(txreq, struct user_sdma_txreq, txreq);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001544 struct user_sdma_request *req;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001545 struct hfi1_user_sdma_pkt_q *pq;
1546 struct hfi1_user_sdma_comp_q *cq;
1547 u16 idx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001548
Mitko Haralanova0d40692015-12-08 17:10:13 -05001549 if (!tx->req)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001550 return;
1551
Mitko Haralanova0d40692015-12-08 17:10:13 -05001552 req = tx->req;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001553 pq = req->pq;
1554 cq = req->cq;
Mitko Haralanovb9fb63182015-10-26 10:28:37 -04001555
Mike Marciniszyn77241052015-07-30 15:17:43 -04001556 if (status != SDMA_TXREQ_S_OK) {
Mitko Haralanova0d40692015-12-08 17:10:13 -05001557 SDMA_DBG(req, "SDMA completion with error %d",
1558 status);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001559 set_bit(SDMA_REQ_HAS_ERROR, &req->flags);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001560 }
1561
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001562 req->seqcomp = tx->seqnum;
1563 kmem_cache_free(pq->txreq_cache, tx);
1564 tx = NULL;
1565
1566 idx = req->info.comp_idx;
1567 if (req->status == -1 && status == SDMA_TXREQ_S_OK) {
1568 if (req->seqcomp == req->info.npkts - 1) {
1569 req->status = 0;
1570 user_sdma_free_request(req, false);
1571 pq_update(pq);
1572 set_comp_state(pq, cq, idx, COMPLETE, 0);
1573 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001574 } else {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001575 if (status != SDMA_TXREQ_S_OK)
1576 req->status = status;
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001577 if (req->seqcomp == (ACCESS_ONCE(req->seqsubmitted) - 1) &&
1578 (test_bit(SDMA_REQ_SEND_DONE, &req->flags) ||
1579 test_bit(SDMA_REQ_DONE_ERROR, &req->flags))) {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001580 user_sdma_free_request(req, false);
1581 pq_update(pq);
1582 set_comp_state(pq, cq, idx, ERROR, req->status);
1583 }
Mitko Haralanova0d40692015-12-08 17:10:13 -05001584 }
1585}
1586
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001587static inline void pq_update(struct hfi1_user_sdma_pkt_q *pq)
Mitko Haralanova0d40692015-12-08 17:10:13 -05001588{
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001589 if (atomic_dec_and_test(&pq->n_reqs)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001590 xchg(&pq->state, SDMA_PKT_Q_INACTIVE);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001591 wake_up(&pq->wait);
1592 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001593}
1594
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001595static void user_sdma_free_request(struct user_sdma_request *req, bool unpin)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001596{
1597 if (!list_empty(&req->txps)) {
1598 struct sdma_txreq *t, *p;
1599
1600 list_for_each_entry_safe(t, p, &req->txps, list) {
1601 struct user_sdma_txreq *tx =
1602 container_of(t, struct user_sdma_txreq, txreq);
1603 list_del_init(&t->list);
1604 sdma_txclean(req->pq->dd, t);
1605 kmem_cache_free(req->pq->txreq_cache, tx);
1606 }
1607 }
1608 if (req->data_iovs) {
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001609 struct sdma_mmu_node *node;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001610 int i;
1611
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001612 for (i = 0; i < req->data_iovs; i++) {
Mitko Haralanov9565c6a2016-05-19 05:21:18 -07001613 node = req->iovs[i].node;
1614 if (!node)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001615 continue;
1616
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001617 if (unpin)
Dean Luicke0b09ac2016-07-28 15:21:20 -04001618 hfi1_mmu_rb_remove(req->pq->handler,
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001619 &node->rb);
1620 else
1621 atomic_dec(&node->refcount);
1622 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001623 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001624 kfree(req->tids);
Dean Luick7b3256e2016-07-28 15:21:18 -04001625 clear_bit(req->info.comp_idx, req->pq->req_in_use);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001626}
1627
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001628static inline void set_comp_state(struct hfi1_user_sdma_pkt_q *pq,
1629 struct hfi1_user_sdma_comp_q *cq,
1630 u16 idx, enum hfi1_sdma_comp_state state,
1631 int ret)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001632{
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001633 hfi1_cdbg(SDMA, "[%u:%u:%u:%u] Setting completion status %u %d",
1634 pq->dd->unit, pq->ctxt, pq->subctxt, idx, state, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001635 if (state == ERROR)
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001636 cq->comps[idx].errcode = -ret;
Michael J. Ruhl0519c522017-03-20 17:24:45 -07001637 smp_wmb(); /* make sure errcode is visible first */
1638 cq->comps[idx].status = state;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001639 trace_hfi1_sdma_user_completion(pq->dd, pq->ctxt, pq->subctxt,
1640 idx, state, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001641}
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001642
1643static bool sdma_rb_filter(struct mmu_rb_node *node, unsigned long addr,
1644 unsigned long len)
1645{
1646 return (bool)(node->addr == addr);
1647}
1648
Dean Luicke0b09ac2016-07-28 15:21:20 -04001649static int sdma_rb_insert(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001650{
1651 struct sdma_mmu_node *node =
1652 container_of(mnode, struct sdma_mmu_node, rb);
1653
1654 atomic_inc(&node->refcount);
1655 return 0;
1656}
1657
Dean Luickb7df1922016-07-28 15:21:23 -04001658/*
1659 * Return 1 to remove the node from the rb tree and call the remove op.
1660 *
1661 * Called with the rb tree lock held.
1662 */
1663static int sdma_rb_evict(void *arg, struct mmu_rb_node *mnode,
1664 void *evict_arg, bool *stop)
1665{
1666 struct sdma_mmu_node *node =
1667 container_of(mnode, struct sdma_mmu_node, rb);
1668 struct evict_data *evict_data = evict_arg;
1669
1670 /* is this node still being used? */
1671 if (atomic_read(&node->refcount))
1672 return 0; /* keep this node */
1673
1674 /* this node will be evicted, add its pages to our count */
1675 evict_data->cleared += node->npages;
1676
1677 /* have enough pages been cleared? */
1678 if (evict_data->cleared >= evict_data->target)
1679 *stop = true;
1680
1681 return 1; /* remove this node */
1682}
1683
Dean Luick082b3532016-07-28 15:21:25 -04001684static void sdma_rb_remove(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001685{
1686 struct sdma_mmu_node *node =
1687 container_of(mnode, struct sdma_mmu_node, rb);
1688
Dean Luickb7df1922016-07-28 15:21:23 -04001689 atomic_sub(node->npages, &node->pq->n_locked);
Mitko Haralanov5511d782016-03-08 11:15:44 -08001690
Dean Luickb85ced92016-07-28 15:21:24 -04001691 unpin_vector_pages(node->pq->mm, node->pages, 0, node->npages);
1692
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001693 kfree(node);
1694}
1695
Dean Luicke0b09ac2016-07-28 15:21:20 -04001696static int sdma_rb_invalidate(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001697{
1698 struct sdma_mmu_node *node =
1699 container_of(mnode, struct sdma_mmu_node, rb);
1700
1701 if (!atomic_read(&node->refcount))
1702 return 1;
1703 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001704}