blob: 9b89df584943ace330adf27eab009289c7c24f12 [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
Jubin Johnaf534932016-08-31 07:24:27 -070097#define AHG_KDETH_INTR_SHIFT 12
Jakub Pawlake7301392016-12-07 19:32:41 -080098#define AHG_KDETH_SH_SHIFT 13
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -070099#define AHG_KDETH_ARRAY_SIZE 9
Jubin Johnaf534932016-08-31 07:24:27 -0700100
Mike Marciniszyn77241052015-07-30 15:17:43 -0400101#define PBC2LRH(x) ((((x) & 0xfff) << 2) - 4)
102#define LRH2PBC(x) ((((x) >> 2) + 1) & 0xfff)
103
Mike Marciniszyn77241052015-07-30 15:17:43 -0400104#define AHG_HEADER_SET(arr, idx, dw, bit, width, value) \
105 do { \
106 if ((idx) < ARRAY_SIZE((arr))) \
107 (arr)[(idx++)] = sdma_build_ahg_descriptor( \
108 (__force u16)(value), (dw), (bit), \
109 (width)); \
110 else \
111 return -ERANGE; \
112 } while (0)
113
Jakub Pawlake7301392016-12-07 19:32:41 -0800114/* Tx request flag bits */
115#define TXREQ_FLAGS_REQ_ACK BIT(0) /* Set the ACK bit in the header */
116#define TXREQ_FLAGS_REQ_DISABLE_SH BIT(1) /* Disable header suppression */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400117
Sunny Kumarcb326492015-11-06 10:06:43 +0530118#define SDMA_PKT_Q_INACTIVE BIT(0)
119#define SDMA_PKT_Q_ACTIVE BIT(1)
120#define SDMA_PKT_Q_DEFERRED BIT(2)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400121
122/*
123 * Maximum retry attempts to submit a TX request
124 * before putting the process to sleep.
125 */
126#define MAX_DEFER_RETRY_COUNT 1
127
128static unsigned initial_pkt_count = 8;
129
130#define SDMA_IOWAIT_TIMEOUT 1000 /* in milliseconds */
131
Mitko Haralanov9565c6a2016-05-19 05:21:18 -0700132struct sdma_mmu_node;
133
Mike Marciniszyn77241052015-07-30 15:17:43 -0400134struct user_sdma_iovec {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800135 struct list_head list;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400136 struct iovec iov;
137 /* number of pages in this vector */
138 unsigned npages;
139 /* array of pinned pages for this vector */
140 struct page **pages;
Jubin John4d114fd2016-02-14 20:21:43 -0800141 /*
142 * offset into the virtual address space of the vector at
143 * which we last left off.
144 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400145 u64 offset;
Mitko Haralanov9565c6a2016-05-19 05:21:18 -0700146 struct sdma_mmu_node *node;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400147};
148
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800149struct sdma_mmu_node {
150 struct mmu_rb_node rb;
Mitko Haralanov5511d782016-03-08 11:15:44 -0800151 struct hfi1_user_sdma_pkt_q *pq;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800152 atomic_t refcount;
153 struct page **pages;
154 unsigned npages;
Dean Luickb7df1922016-07-28 15:21:23 -0400155};
156
157/* evict operation argument */
158struct evict_data {
159 u32 cleared; /* count evicted so far */
160 u32 target; /* target count to evict */
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800161};
162
Mike Marciniszyn77241052015-07-30 15:17:43 -0400163struct user_sdma_request {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400164 /* This is the original header from user space */
165 struct hfi1_pkt_header hdr;
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -0700166
167 /* Read mostly fields */
168 struct hfi1_user_sdma_pkt_q *pq ____cacheline_aligned_in_smp;
169 struct hfi1_user_sdma_comp_q *cq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400170 /*
171 * Pointer to the SDMA engine for this request.
172 * Since different request could be on different VLs,
173 * each request will need it's own engine pointer.
174 */
175 struct sdma_engine *sde;
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -0700176 struct sdma_req_info info;
177 /* TID array values copied from the tid_iov vector */
178 u32 *tids;
179 /* total length of the data in the request */
180 u32 data_len;
181 /* number of elements copied to the tids array */
182 u16 n_tids;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400183 /*
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -0700184 * We copy the iovs for this request (based on
185 * info.iovcnt). These are only the data vectors
Mike Marciniszyn77241052015-07-30 15:17:43 -0400186 */
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -0700187 u8 data_iovs;
188 s8 ahg_idx;
189
190 /* Writeable fields shared with interrupt */
191 u64 seqcomp ____cacheline_aligned_in_smp;
192 u64 seqsubmitted;
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -0700193 /* status of the last txreq completed */
194 int status;
195
196 /* Send side fields */
197 struct list_head txps ____cacheline_aligned_in_smp;
198 u64 seqnum;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400199 /*
200 * KDETH.OFFSET (TID) field
201 * The offset can cover multiple packets, depending on the
202 * size of the TID entry.
203 */
204 u32 tidoffset;
205 /*
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -0700206 * KDETH.Offset (Eager) field
207 * We need to remember the initial value so the headers
208 * can be updated properly.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400209 */
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -0700210 u32 koffset;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400211 u32 sent;
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -0700212 /* TID index copied from the tid_iov vector */
213 u16 tididx;
214 /* progress index moving along the iovs array */
215 u8 iov_idx;
Sebastian Sanchezb8884292017-05-26 05:35:44 -0700216 u8 done;
Sebastian Sancheze9c48eb2017-05-26 05:35:50 -0700217 u8 has_error;
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -0700218
219 struct user_sdma_iovec iovs[MAX_VECTORS_PER_REQ];
220} ____cacheline_aligned_in_smp;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400221
Mitko Haralanovb9fb63182015-10-26 10:28:37 -0400222/*
223 * A single txreq could span up to 3 physical pages when the MTU
224 * is sufficiently large (> 4K). Each of the IOV pointers also
225 * needs it's own set of flags so the vector has been handled
226 * independently of each other.
227 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400228struct user_sdma_txreq {
229 /* Packet header for the txreq */
230 struct hfi1_pkt_header hdr;
231 struct sdma_txreq txreq;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500232 struct list_head list;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400233 struct user_sdma_request *req;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400234 u16 flags;
235 unsigned busycount;
236 u64 seqnum;
237};
238
239#define SDMA_DBG(req, fmt, ...) \
240 hfi1_cdbg(SDMA, "[%u:%u:%u:%u] " fmt, (req)->pq->dd->unit, \
241 (req)->pq->ctxt, (req)->pq->subctxt, (req)->info.comp_idx, \
242 ##__VA_ARGS__)
243#define SDMA_Q_DBG(pq, fmt, ...) \
244 hfi1_cdbg(SDMA, "[%u:%u:%u] " fmt, (pq)->dd->unit, (pq)->ctxt, \
245 (pq)->subctxt, ##__VA_ARGS__)
246
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -0700247static int user_sdma_send_pkts(struct user_sdma_request *req,
248 unsigned maxpkts);
249static int num_user_pages(const struct iovec *iov);
250static void user_sdma_txreq_cb(struct sdma_txreq *txreq, int status);
251static inline void pq_update(struct hfi1_user_sdma_pkt_q *pq);
252static void user_sdma_free_request(struct user_sdma_request *req, bool unpin);
253static int pin_vector_pages(struct user_sdma_request *req,
254 struct user_sdma_iovec *iovec);
255static void unpin_vector_pages(struct mm_struct *mm, struct page **pages,
256 unsigned start, unsigned npages);
257static int check_header_template(struct user_sdma_request *req,
258 struct hfi1_pkt_header *hdr, u32 lrhlen,
259 u32 datalen);
260static int set_txreq_header(struct user_sdma_request *req,
261 struct user_sdma_txreq *tx, u32 datalen);
262static int set_txreq_header_ahg(struct user_sdma_request *req,
263 struct user_sdma_txreq *tx, u32 len);
264static inline void set_comp_state(struct hfi1_user_sdma_pkt_q *pq,
265 struct hfi1_user_sdma_comp_q *cq,
266 u16 idx, enum hfi1_sdma_comp_state state,
267 int ret);
268static inline u32 set_pkt_bth_psn(__be32 bthpsn, u8 expct, u32 frags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400269static inline u32 get_lrh_len(struct hfi1_pkt_header, u32 len);
270
271static int defer_packet_queue(
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -0700272 struct sdma_engine *sde,
273 struct iowait *wait,
274 struct sdma_txreq *txreq,
Kaike Wanbcad2912017-07-24 07:45:37 -0700275 uint seq,
276 bool pkts_sent);
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -0700277static void activate_packet_queue(struct iowait *wait, int reason);
278static bool sdma_rb_filter(struct mmu_rb_node *node, unsigned long addr,
279 unsigned long len);
280static int sdma_rb_insert(void *arg, struct mmu_rb_node *mnode);
Dean Luickb7df1922016-07-28 15:21:23 -0400281static int sdma_rb_evict(void *arg, struct mmu_rb_node *mnode,
282 void *arg2, bool *stop);
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -0700283static void sdma_rb_remove(void *arg, struct mmu_rb_node *mnode);
284static int sdma_rb_invalidate(void *arg, struct mmu_rb_node *mnode);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800285
286static struct mmu_rb_ops sdma_rb_ops = {
287 .filter = sdma_rb_filter,
288 .insert = sdma_rb_insert,
Dean Luickb7df1922016-07-28 15:21:23 -0400289 .evict = sdma_rb_evict,
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800290 .remove = sdma_rb_remove,
291 .invalidate = sdma_rb_invalidate
292};
Mike Marciniszyn77241052015-07-30 15:17:43 -0400293
Mike Marciniszyn77241052015-07-30 15:17:43 -0400294static int defer_packet_queue(
295 struct sdma_engine *sde,
296 struct iowait *wait,
297 struct sdma_txreq *txreq,
Kaike Wanbcad2912017-07-24 07:45:37 -0700298 uint seq,
299 bool pkts_sent)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400300{
301 struct hfi1_user_sdma_pkt_q *pq =
302 container_of(wait, struct hfi1_user_sdma_pkt_q, busy);
303 struct hfi1_ibdev *dev = &pq->dd->verbs_dev;
304 struct user_sdma_txreq *tx =
305 container_of(txreq, struct user_sdma_txreq, txreq);
306
307 if (sdma_progress(sde, seq, txreq)) {
308 if (tx->busycount++ < MAX_DEFER_RETRY_COUNT)
309 goto eagain;
310 }
311 /*
312 * We are assuming that if the list is enqueued somewhere, it
313 * is to the dmawait list since that is the only place where
314 * it is supposed to be enqueued.
315 */
316 xchg(&pq->state, SDMA_PKT_Q_DEFERRED);
317 write_seqlock(&dev->iowait_lock);
318 if (list_empty(&pq->busy.list))
Kaike Wanbcad2912017-07-24 07:45:37 -0700319 iowait_queue(pkts_sent, &pq->busy, &sde->dmawait);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400320 write_sequnlock(&dev->iowait_lock);
321 return -EBUSY;
322eagain:
323 return -EAGAIN;
324}
325
326static void activate_packet_queue(struct iowait *wait, int reason)
327{
328 struct hfi1_user_sdma_pkt_q *pq =
329 container_of(wait, struct hfi1_user_sdma_pkt_q, busy);
330 xchg(&pq->state, SDMA_PKT_Q_ACTIVE);
331 wake_up(&wait->wait_dma);
332};
333
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700334int hfi1_user_sdma_alloc_queues(struct hfi1_ctxtdata *uctxt,
335 struct hfi1_filedata *fd)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400336{
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700337 int ret = -ENOMEM;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400338 char buf[64];
339 struct hfi1_devdata *dd;
340 struct hfi1_user_sdma_comp_q *cq;
341 struct hfi1_user_sdma_pkt_q *pq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400342
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700343 if (!uctxt || !fd)
344 return -EBADF;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400345
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700346 if (!hfi1_sdma_comp_ring_size)
347 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400348
349 dd = uctxt->dd;
350
351 pq = kzalloc(sizeof(*pq), GFP_KERNEL);
Alison Schofield806e6e12015-10-12 14:28:36 -0700352 if (!pq)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700353 return -ENOMEM;
Dean Luick7b3256e2016-07-28 15:21:18 -0400354
Mike Marciniszyn77241052015-07-30 15:17:43 -0400355 pq->dd = dd;
356 pq->ctxt = uctxt->ctxt;
Ira Weiny9e10af42015-10-30 18:58:40 -0400357 pq->subctxt = fd->subctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400358 pq->n_max_reqs = hfi1_sdma_comp_ring_size;
359 pq->state = SDMA_PKT_Q_INACTIVE;
360 atomic_set(&pq->n_reqs, 0);
Mitko Haralanova0d40692015-12-08 17:10:13 -0500361 init_waitqueue_head(&pq->wait);
Dean Luickb7df1922016-07-28 15:21:23 -0400362 atomic_set(&pq->n_locked, 0);
Ira Weiny3faa3d92016-07-28 15:21:19 -0400363 pq->mm = fd->mm;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400364
365 iowait_init(&pq->busy, 0, NULL, defer_packet_queue,
Mike Marciniszyna545f532016-02-14 12:45:53 -0800366 activate_packet_queue, NULL);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400367 pq->reqidx = 0;
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700368
369 pq->reqs = kcalloc(hfi1_sdma_comp_ring_size,
370 sizeof(*pq->reqs),
371 GFP_KERNEL);
372 if (!pq->reqs)
373 goto pq_reqs_nomem;
374
375 pq->req_in_use = kcalloc(BITS_TO_LONGS(hfi1_sdma_comp_ring_size),
376 sizeof(*pq->req_in_use),
377 GFP_KERNEL);
378 if (!pq->req_in_use)
379 goto pq_reqs_no_in_use;
380
Mike Marciniszyn77241052015-07-30 15:17:43 -0400381 snprintf(buf, 64, "txreq-kmem-cache-%u-%u-%u", dd->unit, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -0400382 fd->subctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400383 pq->txreq_cache = kmem_cache_create(buf,
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700384 sizeof(struct user_sdma_txreq),
Mike Marciniszyn77241052015-07-30 15:17:43 -0400385 L1_CACHE_BYTES,
386 SLAB_HWCACHE_ALIGN,
Michael J. Ruhl79563712017-08-21 18:26:45 -0700387 NULL);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400388 if (!pq->txreq_cache) {
389 dd_dev_err(dd, "[%u] Failed to allocate TxReq cache\n",
390 uctxt->ctxt);
391 goto pq_txreq_nomem;
392 }
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700393
Mike Marciniszyn77241052015-07-30 15:17:43 -0400394 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
Alison Schofield806e6e12015-10-12 14:28:36 -0700395 if (!cq)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400396 goto cq_nomem;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400397
Markus Elfringe036c202017-02-10 08:50:45 +0100398 cq->comps = vmalloc_user(PAGE_ALIGN(sizeof(*cq->comps)
399 * hfi1_sdma_comp_ring_size));
Alison Schofield806e6e12015-10-12 14:28:36 -0700400 if (!cq->comps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400401 goto cq_comps_nomem;
Alison Schofield806e6e12015-10-12 14:28:36 -0700402
Mike Marciniszyn77241052015-07-30 15:17:43 -0400403 cq->nentries = hfi1_sdma_comp_ring_size;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400404
Dean Luickb85ced92016-07-28 15:21:24 -0400405 ret = hfi1_mmu_rb_register(pq, pq->mm, &sdma_rb_ops, dd->pport->hfi1_wq,
406 &pq->handler);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800407 if (ret) {
408 dd_dev_err(dd, "Failed to register with MMU %d", ret);
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700409 goto pq_mmu_fail;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800410 }
411
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700412 fd->pq = pq;
413 fd->cq = cq;
414
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700415 return 0;
416
417pq_mmu_fail:
418 vfree(cq->comps);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400419cq_comps_nomem:
420 kfree(cq);
421cq_nomem:
422 kmem_cache_destroy(pq->txreq_cache);
423pq_txreq_nomem:
Dean Luick7b3256e2016-07-28 15:21:18 -0400424 kfree(pq->req_in_use);
425pq_reqs_no_in_use:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400426 kfree(pq->reqs);
427pq_reqs_nomem:
428 kfree(pq);
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700429
Mike Marciniszyn77241052015-07-30 15:17:43 -0400430 return ret;
431}
432
Michael J. Ruhle87473b2017-07-29 08:43:32 -0700433int hfi1_user_sdma_free_queues(struct hfi1_filedata *fd,
434 struct hfi1_ctxtdata *uctxt)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400435{
Mike Marciniszyn77241052015-07-30 15:17:43 -0400436 struct hfi1_user_sdma_pkt_q *pq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400437
438 hfi1_cdbg(SDMA, "[%u:%u:%u] Freeing user SDMA queues", uctxt->dd->unit,
439 uctxt->ctxt, fd->subctxt);
440 pq = fd->pq;
441 if (pq) {
Dean Luicke0b09ac2016-07-28 15:21:20 -0400442 if (pq->handler)
443 hfi1_mmu_rb_unregister(pq->handler);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400444 iowait_sdma_drain(&pq->busy);
Mitko Haralanova0d40692015-12-08 17:10:13 -0500445 /* Wait until all requests have been freed. */
446 wait_event_interruptible(
447 pq->wait,
448 (ACCESS_ONCE(pq->state) == SDMA_PKT_Q_INACTIVE));
449 kfree(pq->reqs);
Dean Luick7b3256e2016-07-28 15:21:18 -0400450 kfree(pq->req_in_use);
Julia Lawalladad44d2015-09-13 14:15:04 +0200451 kmem_cache_destroy(pq->txreq_cache);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400452 kfree(pq);
453 fd->pq = NULL;
454 }
455 if (fd->cq) {
Bhumika Goyala4d7d052016-02-14 20:34:28 +0530456 vfree(fd->cq->comps);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400457 kfree(fd->cq);
458 fd->cq = NULL;
459 }
460 return 0;
461}
462
Jianxin Xiong14833b82016-07-01 16:01:56 -0700463static u8 dlid_to_selector(u16 dlid)
464{
465 static u8 mapping[256];
466 static int initialized;
467 static u8 next;
468 int hash;
469
470 if (!initialized) {
471 memset(mapping, 0xFF, 256);
472 initialized = 1;
473 }
474
475 hash = ((dlid >> 8) ^ dlid) & 0xFF;
476 if (mapping[hash] == 0xFF) {
477 mapping[hash] = next;
478 next = (next + 1) & 0x7F;
479 }
480
481 return mapping[hash];
482}
483
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700484int hfi1_user_sdma_process_request(struct hfi1_filedata *fd,
485 struct iovec *iovec, unsigned long dim,
486 unsigned long *count)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400487{
Dean Luickff4ce9b2016-07-28 12:27:34 -0400488 int ret = 0, i;
Ira Weiny9e10af42015-10-30 18:58:40 -0400489 struct hfi1_ctxtdata *uctxt = fd->uctxt;
490 struct hfi1_user_sdma_pkt_q *pq = fd->pq;
491 struct hfi1_user_sdma_comp_q *cq = fd->cq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400492 struct hfi1_devdata *dd = pq->dd;
493 unsigned long idx = 0;
494 u8 pcount = initial_pkt_count;
495 struct sdma_req_info info;
496 struct user_sdma_request *req;
497 u8 opcode, sc, vl;
Don Hiatt566d53a2017-08-04 13:54:47 -0700498 u16 pkey;
499 u32 slid;
Jianxin Xiongb583faf2016-05-19 05:21:57 -0700500 int req_queued = 0;
Jianxin Xiong14833b82016-07-01 16:01:56 -0700501 u16 dlid;
Tadeusz Struk0cb2aa62016-09-25 07:44:23 -0700502 u32 selector;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400503
504 if (iovec[idx].iov_len < sizeof(info) + sizeof(req->hdr)) {
505 hfi1_cdbg(
506 SDMA,
507 "[%u:%u:%u] First vector not big enough for header %lu/%lu",
Ira Weiny9e10af42015-10-30 18:58:40 -0400508 dd->unit, uctxt->ctxt, fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400509 iovec[idx].iov_len, sizeof(info) + sizeof(req->hdr));
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500510 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400511 }
512 ret = copy_from_user(&info, iovec[idx].iov_base, sizeof(info));
513 if (ret) {
514 hfi1_cdbg(SDMA, "[%u:%u:%u] Failed to copy info QW (%d)",
Ira Weiny9e10af42015-10-30 18:58:40 -0400515 dd->unit, uctxt->ctxt, fd->subctxt, ret);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500516 return -EFAULT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400517 }
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800518
Ira Weiny9e10af42015-10-30 18:58:40 -0400519 trace_hfi1_sdma_user_reqinfo(dd, uctxt->ctxt, fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400520 (u16 *)&info);
Dean Luick4fa0d222016-07-28 15:21:14 -0400521
522 if (info.comp_idx >= hfi1_sdma_comp_ring_size) {
523 hfi1_cdbg(SDMA,
524 "[%u:%u:%u:%u] Invalid comp index",
525 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx);
526 return -EINVAL;
527 }
528
Dean Luick9ff73c82016-07-28 15:21:15 -0400529 /*
530 * Sanity check the header io vector count. Need at least 1 vector
531 * (header) and cannot be larger than the actual io vector count.
532 */
533 if (req_iovcnt(info.ctrl) < 1 || req_iovcnt(info.ctrl) > dim) {
534 hfi1_cdbg(SDMA,
535 "[%u:%u:%u:%u] Invalid iov count %d, dim %ld",
536 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx,
537 req_iovcnt(info.ctrl), dim);
538 return -EINVAL;
539 }
540
Mike Marciniszyn77241052015-07-30 15:17:43 -0400541 if (!info.fragsize) {
542 hfi1_cdbg(SDMA,
543 "[%u:%u:%u:%u] Request does not specify fragsize",
Ira Weiny9e10af42015-10-30 18:58:40 -0400544 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500545 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400546 }
Dean Luick7b3256e2016-07-28 15:21:18 -0400547
548 /* Try to claim the request. */
549 if (test_and_set_bit(info.comp_idx, pq->req_in_use)) {
550 hfi1_cdbg(SDMA, "[%u:%u:%u] Entry %u is in use",
551 dd->unit, uctxt->ctxt, fd->subctxt,
552 info.comp_idx);
553 return -EBADSLT;
554 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400555 /*
Dean Luick7b3256e2016-07-28 15:21:18 -0400556 * All safety checks have been done and this request has been claimed.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400557 */
558 hfi1_cdbg(SDMA, "[%u:%u:%u] Using req/comp entry %u\n", dd->unit,
Ira Weiny9e10af42015-10-30 18:58:40 -0400559 uctxt->ctxt, fd->subctxt, info.comp_idx);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400560 req = pq->reqs + info.comp_idx;
Dean Luick9ff73c82016-07-28 15:21:15 -0400561 req->data_iovs = req_iovcnt(info.ctrl) - 1; /* subtract header vector */
Sebastian Sanchezceb26562017-05-12 09:19:36 -0700562 req->data_len = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400563 req->pq = pq;
564 req->cq = cq;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500565 req->status = -1;
Sebastian Sanchez780a4c12017-05-04 05:14:51 -0700566 req->ahg_idx = -1;
Sebastian Sanchezceb26562017-05-12 09:19:36 -0700567 req->iov_idx = 0;
568 req->sent = 0;
569 req->seqnum = 0;
570 req->seqcomp = 0;
571 req->seqsubmitted = 0;
Sebastian Sanchezceb26562017-05-12 09:19:36 -0700572 req->tids = NULL;
Sebastian Sanchezb8884292017-05-26 05:35:44 -0700573 req->done = 0;
Sebastian Sancheze9c48eb2017-05-26 05:35:50 -0700574 req->has_error = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400575 INIT_LIST_HEAD(&req->txps);
Mitko Haralanova0d40692015-12-08 17:10:13 -0500576
Mike Marciniszyn77241052015-07-30 15:17:43 -0400577 memcpy(&req->info, &info, sizeof(info));
578
Dean Luick9ff73c82016-07-28 15:21:15 -0400579 if (req_opcode(info.ctrl) == EXPECTED) {
580 /* expected must have a TID info and at least one data vector */
581 if (req->data_iovs < 2) {
582 SDMA_DBG(req,
583 "Not enough vectors for expected request");
584 ret = -EINVAL;
585 goto free_req;
586 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400587 req->data_iovs--;
Dean Luick9ff73c82016-07-28 15:21:15 -0400588 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400589
590 if (!info.npkts || req->data_iovs > MAX_VECTORS_PER_REQ) {
591 SDMA_DBG(req, "Too many vectors (%u/%u)", req->data_iovs,
592 MAX_VECTORS_PER_REQ);
Dean Luick9da7e9a2016-07-28 15:21:17 -0400593 ret = -EINVAL;
594 goto free_req;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400595 }
596 /* Copy the header from the user buffer */
597 ret = copy_from_user(&req->hdr, iovec[idx].iov_base + sizeof(info),
598 sizeof(req->hdr));
599 if (ret) {
600 SDMA_DBG(req, "Failed to copy header template (%d)", ret);
601 ret = -EFAULT;
602 goto free_req;
603 }
604
605 /* If Static rate control is not enabled, sanitize the header. */
606 if (!HFI1_CAP_IS_USET(STATIC_RATE_CTRL))
607 req->hdr.pbc[2] = 0;
608
609 /* Validate the opcode. Do not trust packets from user space blindly. */
610 opcode = (be32_to_cpu(req->hdr.bth[0]) >> 24) & 0xff;
611 if ((opcode & USER_OPCODE_CHECK_MASK) !=
612 USER_OPCODE_CHECK_VAL) {
613 SDMA_DBG(req, "Invalid opcode (%d)", opcode);
614 ret = -EINVAL;
615 goto free_req;
616 }
617 /*
618 * Validate the vl. Do not trust packets from user space blindly.
619 * VL comes from PBC, SC comes from LRH, and the VL needs to
620 * match the SC look up.
621 */
622 vl = (le16_to_cpu(req->hdr.pbc[0]) >> 12) & 0xF;
623 sc = (((be16_to_cpu(req->hdr.lrh[0]) >> 12) & 0xF) |
624 (((le16_to_cpu(req->hdr.pbc[1]) >> 14) & 0x1) << 4));
625 if (vl >= dd->pport->vls_operational ||
626 vl != sc_to_vlt(dd, sc)) {
627 SDMA_DBG(req, "Invalid SC(%u)/VL(%u)", sc, vl);
628 ret = -EINVAL;
629 goto free_req;
630 }
631
Sebastian Sancheze38d1e42016-04-12 11:22:21 -0700632 /* Checking P_KEY for requests from user-space */
Don Hiatt566d53a2017-08-04 13:54:47 -0700633 pkey = (u16)be32_to_cpu(req->hdr.bth[0]);
634 slid = be16_to_cpu(req->hdr.lrh[3]);
635 if (egress_pkey_check(dd->pport, slid, pkey, sc, PKEY_CHECK_INVALID)) {
Sebastian Sancheze38d1e42016-04-12 11:22:21 -0700636 ret = -EINVAL;
637 goto free_req;
638 }
639
Mike Marciniszyn77241052015-07-30 15:17:43 -0400640 /*
641 * Also should check the BTH.lnh. If it says the next header is GRH then
642 * the RXE parsing will be off and will land in the middle of the KDETH
643 * or miss it entirely.
644 */
645 if ((be16_to_cpu(req->hdr.lrh[0]) & 0x3) == HFI1_LRH_GRH) {
646 SDMA_DBG(req, "User tried to pass in a GRH");
647 ret = -EINVAL;
648 goto free_req;
649 }
650
651 req->koffset = le32_to_cpu(req->hdr.kdeth.swdata[6]);
Jubin John4d114fd2016-02-14 20:21:43 -0800652 /*
653 * Calculate the initial TID offset based on the values of
654 * KDETH.OFFSET and KDETH.OM that are passed in.
655 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400656 req->tidoffset = KDETH_GET(req->hdr.kdeth.ver_tid_offset, OFFSET) *
657 (KDETH_GET(req->hdr.kdeth.ver_tid_offset, OM) ?
658 KDETH_OM_LARGE : KDETH_OM_SMALL);
659 SDMA_DBG(req, "Initial TID offset %u", req->tidoffset);
660 idx++;
661
662 /* Save all the IO vector structures */
Dean Luickff4ce9b2016-07-28 12:27:34 -0400663 for (i = 0; i < req->data_iovs; i++) {
Sebastian Sanchezceb26562017-05-12 09:19:36 -0700664 req->iovs[i].offset = 0;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800665 INIT_LIST_HEAD(&req->iovs[i].list);
Markus Elfringdb6f0282017-02-10 21:45:38 +0100666 memcpy(&req->iovs[i].iov,
667 iovec + idx++,
668 sizeof(req->iovs[i].iov));
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800669 ret = pin_vector_pages(req, &req->iovs[i]);
670 if (ret) {
Sebastian Sanchezceb26562017-05-12 09:19:36 -0700671 req->data_iovs = i;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800672 req->status = ret;
673 goto free_req;
674 }
Dean Luickff4ce9b2016-07-28 12:27:34 -0400675 req->data_len += req->iovs[i].iov.iov_len;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400676 }
677 SDMA_DBG(req, "total data length %u", req->data_len);
678
679 if (pcount > req->info.npkts)
680 pcount = req->info.npkts;
681 /*
682 * Copy any TID info
683 * User space will provide the TID info only when the
684 * request type is EXPECTED. This is true even if there is
685 * only one packet in the request and the header is already
686 * setup. The reason for the singular TID case is that the
687 * driver needs to perform safety checks.
688 */
689 if (req_opcode(req->info.ctrl) == EXPECTED) {
690 u16 ntids = iovec[idx].iov_len / sizeof(*req->tids);
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800691 u32 *tmp;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400692
693 if (!ntids || ntids > MAX_TID_PAIR_ENTRIES) {
694 ret = -EINVAL;
695 goto free_req;
696 }
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800697
Mike Marciniszyn77241052015-07-30 15:17:43 -0400698 /*
699 * We have to copy all of the tids because they may vary
700 * in size and, therefore, the TID count might not be
701 * equal to the pkt count. However, there is no way to
702 * tell at this point.
703 */
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800704 tmp = memdup_user(iovec[idx].iov_base,
705 ntids * sizeof(*req->tids));
706 if (IS_ERR(tmp)) {
707 ret = PTR_ERR(tmp);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400708 SDMA_DBG(req, "Failed to copy %d TIDs (%d)",
709 ntids, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400710 goto free_req;
711 }
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800712 req->tids = tmp;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400713 req->n_tids = ntids;
Sebastian Sanchezceb26562017-05-12 09:19:36 -0700714 req->tididx = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400715 idx++;
716 }
717
Jianxin Xiong14833b82016-07-01 16:01:56 -0700718 dlid = be16_to_cpu(req->hdr.lrh[1]);
719 selector = dlid_to_selector(dlid);
Tadeusz Struk0cb2aa62016-09-25 07:44:23 -0700720 selector += uctxt->ctxt + fd->subctxt;
721 req->sde = sdma_select_user_engine(dd, selector, vl);
Jianxin Xiong14833b82016-07-01 16:01:56 -0700722
Mike Marciniszyn77241052015-07-30 15:17:43 -0400723 if (!req->sde || !sdma_running(req->sde)) {
724 ret = -ECOMM;
725 goto free_req;
726 }
727
728 /* We don't need an AHG entry if the request contains only one packet */
Sebastian Sanchez780a4c12017-05-04 05:14:51 -0700729 if (req->info.npkts > 1 && HFI1_CAP_IS_USET(SDMA_AHG))
730 req->ahg_idx = sdma_ahg_alloc(req->sde);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400731
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800732 set_comp_state(pq, cq, info.comp_idx, QUEUED, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400733 atomic_inc(&pq->n_reqs);
Jianxin Xiongb583faf2016-05-19 05:21:57 -0700734 req_queued = 1;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800735 /* Send the first N packets in the request to buy us some time */
736 ret = user_sdma_send_pkts(req, pcount);
737 if (unlikely(ret < 0 && ret != -EBUSY)) {
738 req->status = ret;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800739 goto free_req;
740 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400741
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800742 /*
743 * It is possible that the SDMA engine would have processed all the
744 * submitted packets by the time we get here. Therefore, only set
745 * packet queue state to ACTIVE if there are still uncompleted
746 * requests.
747 */
748 if (atomic_read(&pq->n_reqs))
749 xchg(&pq->state, SDMA_PKT_Q_ACTIVE);
750
751 /*
752 * This is a somewhat blocking send implementation.
753 * The driver will block the caller until all packets of the
754 * request have been submitted to the SDMA engine. However, it
755 * will not wait for send completions.
756 */
Sebastian Sanchezb8884292017-05-26 05:35:44 -0700757 while (req->seqsubmitted != req->info.npkts) {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800758 ret = user_sdma_send_pkts(req, pcount);
759 if (ret < 0) {
760 if (ret != -EBUSY) {
761 req->status = ret;
Sebastian Sancheze9c48eb2017-05-26 05:35:50 -0700762 WRITE_ONCE(req->has_error, 1);
Mitko Haralanova402d6a2016-02-03 14:37:41 -0800763 if (ACCESS_ONCE(req->seqcomp) ==
764 req->seqsubmitted - 1)
765 goto free_req;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800766 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400767 }
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800768 wait_event_interruptible_timeout(
769 pq->busy.wait_dma,
770 (pq->state == SDMA_PKT_Q_ACTIVE),
771 msecs_to_jiffies(
772 SDMA_IOWAIT_TIMEOUT));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400773 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400774 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400775 *count += idx;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500776 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400777free_req:
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800778 user_sdma_free_request(req, true);
Jianxin Xiongb583faf2016-05-19 05:21:57 -0700779 if (req_queued)
780 pq_update(pq);
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800781 set_comp_state(pq, cq, info.comp_idx, ERROR, req->status);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400782 return ret;
783}
784
785static inline u32 compute_data_length(struct user_sdma_request *req,
Jubin John17fb4f22016-02-14 20:21:52 -0800786 struct user_sdma_txreq *tx)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400787{
788 /*
789 * Determine the proper size of the packet data.
790 * The size of the data of the first packet is in the header
791 * template. However, it includes the header and ICRC, which need
792 * to be subtracted.
Ira Weinyc4929802016-07-27 21:08:42 -0400793 * The minimum representable packet data length in a header is 4 bytes,
794 * therefore, when the data length request is less than 4 bytes, there's
795 * only one packet, and the packet data length is equal to that of the
796 * request data length.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400797 * The size of the remaining packets is the minimum of the frag
798 * size (MTU) or remaining data in the request.
799 */
800 u32 len;
801
802 if (!req->seqnum) {
Ira Weinyc4929802016-07-27 21:08:42 -0400803 if (req->data_len < sizeof(u32))
804 len = req->data_len;
805 else
806 len = ((be16_to_cpu(req->hdr.lrh[2]) << 2) -
807 (sizeof(tx->hdr) - 4));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400808 } else if (req_opcode(req->info.ctrl) == EXPECTED) {
809 u32 tidlen = EXP_TID_GET(req->tids[req->tididx], LEN) *
810 PAGE_SIZE;
Jubin John4d114fd2016-02-14 20:21:43 -0800811 /*
812 * Get the data length based on the remaining space in the
813 * TID pair.
814 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400815 len = min(tidlen - req->tidoffset, (u32)req->info.fragsize);
816 /* If we've filled up the TID pair, move to the next one. */
817 if (unlikely(!len) && ++req->tididx < req->n_tids &&
818 req->tids[req->tididx]) {
819 tidlen = EXP_TID_GET(req->tids[req->tididx],
820 LEN) * PAGE_SIZE;
821 req->tidoffset = 0;
822 len = min_t(u32, tidlen, req->info.fragsize);
823 }
Jubin John4d114fd2016-02-14 20:21:43 -0800824 /*
825 * Since the TID pairs map entire pages, make sure that we
Mike Marciniszyn77241052015-07-30 15:17:43 -0400826 * are not going to try to send more data that we have
Jubin John4d114fd2016-02-14 20:21:43 -0800827 * remaining.
828 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400829 len = min(len, req->data_len - req->sent);
Jubin Johne4909742016-02-14 20:22:00 -0800830 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400831 len = min(req->data_len - req->sent, (u32)req->info.fragsize);
Jubin Johne4909742016-02-14 20:22:00 -0800832 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400833 SDMA_DBG(req, "Data Length = %u", len);
834 return len;
835}
836
Ira Weinyc4929802016-07-27 21:08:42 -0400837static inline u32 pad_len(u32 len)
838{
839 if (len & (sizeof(u32) - 1))
840 len += sizeof(u32) - (len & (sizeof(u32) - 1));
841 return len;
842}
843
Mike Marciniszyn77241052015-07-30 15:17:43 -0400844static inline u32 get_lrh_len(struct hfi1_pkt_header hdr, u32 len)
845{
846 /* (Size of complete header - size of PBC) + 4B ICRC + data length */
847 return ((sizeof(hdr) - sizeof(hdr.pbc)) + 4 + len);
848}
849
850static int user_sdma_send_pkts(struct user_sdma_request *req, unsigned maxpkts)
851{
Harish Chegondi0b115ef2016-09-06 04:35:37 -0700852 int ret = 0, count;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400853 unsigned npkts = 0;
854 struct user_sdma_txreq *tx = NULL;
855 struct hfi1_user_sdma_pkt_q *pq = NULL;
856 struct user_sdma_iovec *iovec = NULL;
857
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500858 if (!req->pq)
859 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400860
861 pq = req->pq;
862
Mitko Haralanov6a5464f2015-12-08 17:10:12 -0500863 /* If tx completion has reported an error, we are done. */
Sebastian Sancheze9c48eb2017-05-26 05:35:50 -0700864 if (READ_ONCE(req->has_error))
Mitko Haralanov6a5464f2015-12-08 17:10:12 -0500865 return -EFAULT;
Mitko Haralanov6a5464f2015-12-08 17:10:12 -0500866
Mike Marciniszyn77241052015-07-30 15:17:43 -0400867 /*
868 * Check if we might have sent the entire request already
869 */
870 if (unlikely(req->seqnum == req->info.npkts)) {
871 if (!list_empty(&req->txps))
872 goto dosend;
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500873 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400874 }
875
876 if (!maxpkts || maxpkts > req->info.npkts - req->seqnum)
877 maxpkts = req->info.npkts - req->seqnum;
878
879 while (npkts < maxpkts) {
880 u32 datalen = 0, queued = 0, data_sent = 0;
881 u64 iov_offset = 0;
882
883 /*
884 * Check whether any of the completions have come back
885 * with errors. If so, we are not going to process any
886 * more packets from this request.
887 */
Sebastian Sancheze9c48eb2017-05-26 05:35:50 -0700888 if (READ_ONCE(req->has_error))
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500889 return -EFAULT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400890
891 tx = kmem_cache_alloc(pq->txreq_cache, GFP_KERNEL);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500892 if (!tx)
893 return -ENOMEM;
894
Mike Marciniszyn77241052015-07-30 15:17:43 -0400895 tx->flags = 0;
896 tx->req = req;
897 tx->busycount = 0;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500898 INIT_LIST_HEAD(&tx->list);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400899
Jakub Pawlake7301392016-12-07 19:32:41 -0800900 /*
901 * For the last packet set the ACK request
902 * and disable header suppression.
903 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400904 if (req->seqnum == req->info.npkts - 1)
Jakub Pawlake7301392016-12-07 19:32:41 -0800905 tx->flags |= (TXREQ_FLAGS_REQ_ACK |
906 TXREQ_FLAGS_REQ_DISABLE_SH);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400907
908 /*
909 * Calculate the payload size - this is min of the fragment
910 * (MTU) size or the remaining bytes in the request but only
911 * if we have payload data.
912 */
913 if (req->data_len) {
914 iovec = &req->iovs[req->iov_idx];
915 if (ACCESS_ONCE(iovec->offset) == iovec->iov.iov_len) {
916 if (++req->iov_idx == req->data_iovs) {
917 ret = -EFAULT;
918 goto free_txreq;
919 }
920 iovec = &req->iovs[req->iov_idx];
921 WARN_ON(iovec->offset);
922 }
923
Mike Marciniszyn77241052015-07-30 15:17:43 -0400924 datalen = compute_data_length(req, tx);
Jakub Pawlake7301392016-12-07 19:32:41 -0800925
926 /*
927 * Disable header suppression for the payload <= 8DWS.
928 * If there is an uncorrectable error in the receive
929 * data FIFO when the received payload size is less than
930 * or equal to 8DWS then the RxDmaDataFifoRdUncErr is
931 * not reported.There is set RHF.EccErr if the header
932 * is not suppressed.
933 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400934 if (!datalen) {
935 SDMA_DBG(req,
936 "Request has data but pkt len is 0");
937 ret = -EFAULT;
938 goto free_tx;
Jakub Pawlake7301392016-12-07 19:32:41 -0800939 } else if (datalen <= 32) {
940 tx->flags |= TXREQ_FLAGS_REQ_DISABLE_SH;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400941 }
942 }
943
Sebastian Sanchez780a4c12017-05-04 05:14:51 -0700944 if (req->ahg_idx >= 0) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400945 if (!req->seqnum) {
946 u16 pbclen = le16_to_cpu(req->hdr.pbc[0]);
Ira Weinyc4929802016-07-27 21:08:42 -0400947 u32 lrhlen = get_lrh_len(req->hdr,
948 pad_len(datalen));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400949 /*
950 * Copy the request header into the tx header
951 * because the HW needs a cacheline-aligned
952 * address.
953 * This copy can be optimized out if the hdr
954 * member of user_sdma_request were also
955 * cacheline aligned.
956 */
957 memcpy(&tx->hdr, &req->hdr, sizeof(tx->hdr));
958 if (PBC2LRH(pbclen) != lrhlen) {
959 pbclen = (pbclen & 0xf000) |
960 LRH2PBC(lrhlen);
961 tx->hdr.pbc[0] = cpu_to_le16(pbclen);
962 }
Jakub Pawlake7301392016-12-07 19:32:41 -0800963 ret = check_header_template(req, &tx->hdr,
964 lrhlen, datalen);
965 if (ret)
966 goto free_tx;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400967 ret = sdma_txinit_ahg(&tx->txreq,
968 SDMA_TXREQ_F_AHG_COPY,
969 sizeof(tx->hdr) + datalen,
970 req->ahg_idx, 0, NULL, 0,
971 user_sdma_txreq_cb);
972 if (ret)
973 goto free_tx;
974 ret = sdma_txadd_kvaddr(pq->dd, &tx->txreq,
975 &tx->hdr,
976 sizeof(tx->hdr));
977 if (ret)
978 goto free_txreq;
979 } else {
980 int changes;
981
982 changes = set_txreq_header_ahg(req, tx,
983 datalen);
984 if (changes < 0)
985 goto free_tx;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400986 }
987 } else {
988 ret = sdma_txinit(&tx->txreq, 0, sizeof(req->hdr) +
989 datalen, user_sdma_txreq_cb);
990 if (ret)
991 goto free_tx;
992 /*
993 * Modify the header for this packet. This only needs
994 * to be done if we are not going to use AHG. Otherwise,
995 * the HW will do it based on the changes we gave it
996 * during sdma_txinit_ahg().
997 */
998 ret = set_txreq_header(req, tx, datalen);
999 if (ret)
1000 goto free_txreq;
1001 }
1002
1003 /*
1004 * If the request contains any data vectors, add up to
1005 * fragsize bytes to the descriptor.
1006 */
1007 while (queued < datalen &&
1008 (req->sent + data_sent) < req->data_len) {
1009 unsigned long base, offset;
1010 unsigned pageidx, len;
1011
1012 base = (unsigned long)iovec->iov.iov_base;
Amitoj Kaur Chawla72a5f6a2016-02-20 19:08:02 +05301013 offset = offset_in_page(base + iovec->offset +
1014 iov_offset);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001015 pageidx = (((iovec->offset + iov_offset +
1016 base) - (base & PAGE_MASK)) >> PAGE_SHIFT);
1017 len = offset + req->info.fragsize > PAGE_SIZE ?
1018 PAGE_SIZE - offset : req->info.fragsize;
1019 len = min((datalen - queued), len);
1020 ret = sdma_txadd_page(pq->dd, &tx->txreq,
1021 iovec->pages[pageidx],
1022 offset, len);
1023 if (ret) {
Mitko Haralanova0d40692015-12-08 17:10:13 -05001024 SDMA_DBG(req, "SDMA txreq add page failed %d\n",
1025 ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001026 goto free_txreq;
1027 }
1028 iov_offset += len;
1029 queued += len;
1030 data_sent += len;
1031 if (unlikely(queued < datalen &&
1032 pageidx == iovec->npages &&
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001033 req->iov_idx < req->data_iovs - 1)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001034 iovec->offset += iov_offset;
1035 iovec = &req->iovs[++req->iov_idx];
Mike Marciniszyn77241052015-07-30 15:17:43 -04001036 iov_offset = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001037 }
1038 }
1039 /*
1040 * The txreq was submitted successfully so we can update
1041 * the counters.
1042 */
1043 req->koffset += datalen;
1044 if (req_opcode(req->info.ctrl) == EXPECTED)
1045 req->tidoffset += datalen;
1046 req->sent += data_sent;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001047 if (req->data_len)
1048 iovec->offset += iov_offset;
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001049 list_add_tail(&tx->txreq.list, &req->txps);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001050 /*
1051 * It is important to increment this here as it is used to
1052 * generate the BTH.PSN and, therefore, can't be bulk-updated
1053 * outside of the loop.
1054 */
1055 tx->seqnum = req->seqnum++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001056 npkts++;
1057 }
1058dosend:
Harish Chegondi0b115ef2016-09-06 04:35:37 -07001059 ret = sdma_send_txlist(req->sde, &pq->busy, &req->txps, &count);
1060 req->seqsubmitted += count;
1061 if (req->seqsubmitted == req->info.npkts) {
Sebastian Sanchezb8884292017-05-26 05:35:44 -07001062 WRITE_ONCE(req->done, 1);
Harish Chegondi0b115ef2016-09-06 04:35:37 -07001063 /*
1064 * The txreq has already been submitted to the HW queue
1065 * so we can free the AHG entry now. Corruption will not
1066 * happen due to the sequential manner in which
1067 * descriptors are processed.
1068 */
Sebastian Sanchez780a4c12017-05-04 05:14:51 -07001069 if (req->ahg_idx >= 0)
Harish Chegondi0b115ef2016-09-06 04:35:37 -07001070 sdma_ahg_free(req->sde, req->ahg_idx);
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001071 }
Mitko Haralanovfaa98b82015-12-08 17:10:11 -05001072 return ret;
1073
Mike Marciniszyn77241052015-07-30 15:17:43 -04001074free_txreq:
1075 sdma_txclean(pq->dd, &tx->txreq);
1076free_tx:
1077 kmem_cache_free(pq->txreq_cache, tx);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001078 return ret;
1079}
1080
1081/*
1082 * How many pages in this iovec element?
1083 */
1084static inline int num_user_pages(const struct iovec *iov)
1085{
Jubin John50e5dcb2016-02-14 20:19:41 -08001086 const unsigned long addr = (unsigned long)iov->iov_base;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001087 const unsigned long len = iov->iov_len;
1088 const unsigned long spage = addr & PAGE_MASK;
1089 const unsigned long epage = (addr + len - 1) & PAGE_MASK;
1090
1091 return 1 + ((epage - spage) >> PAGE_SHIFT);
1092}
1093
Mitko Haralanov5511d782016-03-08 11:15:44 -08001094static u32 sdma_cache_evict(struct hfi1_user_sdma_pkt_q *pq, u32 npages)
1095{
Dean Luickb7df1922016-07-28 15:21:23 -04001096 struct evict_data evict_data;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001097
Dean Luickb7df1922016-07-28 15:21:23 -04001098 evict_data.cleared = 0;
1099 evict_data.target = npages;
1100 hfi1_mmu_rb_evict(pq->handler, &evict_data);
1101 return evict_data.cleared;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001102}
1103
Mike Marciniszyn77241052015-07-30 15:17:43 -04001104static int pin_vector_pages(struct user_sdma_request *req,
Ira Weiny72720dd2016-07-28 12:27:25 -04001105 struct user_sdma_iovec *iovec)
1106{
Mitko Haralanov5511d782016-03-08 11:15:44 -08001107 int ret = 0, pinned, npages, cleared;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001108 struct page **pages;
1109 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1110 struct sdma_mmu_node *node = NULL;
1111 struct mmu_rb_node *rb_node;
Sebastian Sanchez7be85672017-05-26 05:35:12 -07001112 bool extracted;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001113
Sebastian Sanchez7be85672017-05-26 05:35:12 -07001114 extracted =
1115 hfi1_mmu_rb_remove_unless_exact(pq->handler,
1116 (unsigned long)
1117 iovec->iov.iov_base,
1118 iovec->iov.iov_len, &rb_node);
1119 if (rb_node) {
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001120 node = container_of(rb_node, struct sdma_mmu_node, rb);
Sebastian Sanchez7be85672017-05-26 05:35:12 -07001121 if (!extracted) {
1122 atomic_inc(&node->refcount);
1123 iovec->pages = node->pages;
1124 iovec->npages = node->npages;
1125 iovec->node = node;
1126 return 0;
1127 }
1128 }
Mitko Haralanova0d40692015-12-08 17:10:13 -05001129
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001130 if (!node) {
1131 node = kzalloc(sizeof(*node), GFP_KERNEL);
1132 if (!node)
1133 return -ENOMEM;
1134
1135 node->rb.addr = (unsigned long)iovec->iov.iov_base;
Mitko Haralanov5511d782016-03-08 11:15:44 -08001136 node->pq = pq;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001137 atomic_set(&node->refcount, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001138 }
Mitko Haralanova0d40692015-12-08 17:10:13 -05001139
Mike Marciniszyn77241052015-07-30 15:17:43 -04001140 npages = num_user_pages(&iovec->iov);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001141 if (node->npages < npages) {
1142 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
1143 if (!pages) {
1144 SDMA_DBG(req, "Failed page array alloc");
1145 ret = -ENOMEM;
1146 goto bail;
1147 }
1148 memcpy(pages, node->pages, node->npages * sizeof(*pages));
1149
1150 npages -= node->npages;
Mitko Haralanove88c9272016-04-12 10:46:53 -07001151
Mitko Haralanov5511d782016-03-08 11:15:44 -08001152retry:
Dean Luickb7df1922016-07-28 15:21:23 -04001153 if (!hfi1_can_pin_pages(pq->dd, pq->mm,
1154 atomic_read(&pq->n_locked), npages)) {
Mitko Haralanov5511d782016-03-08 11:15:44 -08001155 cleared = sdma_cache_evict(pq, npages);
Mitko Haralanov5511d782016-03-08 11:15:44 -08001156 if (cleared >= npages)
1157 goto retry;
1158 }
Ira Weiny3faa3d92016-07-28 15:21:19 -04001159 pinned = hfi1_acquire_user_pages(pq->mm,
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001160 ((unsigned long)iovec->iov.iov_base +
1161 (node->npages * PAGE_SIZE)), npages, 0,
1162 pages + node->npages);
1163 if (pinned < 0) {
1164 kfree(pages);
1165 ret = pinned;
1166 goto bail;
1167 }
1168 if (pinned != npages) {
Ira Weiny3faa3d92016-07-28 15:21:19 -04001169 unpin_vector_pages(pq->mm, pages, node->npages,
Mitko Haralanov849e3e92016-04-12 10:46:16 -07001170 pinned);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001171 ret = -EFAULT;
1172 goto bail;
1173 }
1174 kfree(node->pages);
Mitko Haralanovde790932016-04-12 10:46:41 -07001175 node->rb.len = iovec->iov.iov_len;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001176 node->pages = pages;
1177 node->npages += pinned;
1178 npages = node->npages;
Dean Luickb7df1922016-07-28 15:21:23 -04001179 atomic_add(pinned, &pq->n_locked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001180 }
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001181 iovec->pages = node->pages;
1182 iovec->npages = npages;
Mitko Haralanov9565c6a2016-05-19 05:21:18 -07001183 iovec->node = node;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001184
Dean Luicke0b09ac2016-07-28 15:21:20 -04001185 ret = hfi1_mmu_rb_insert(req->pq->handler, &node->rb);
Mitko Haralanovf53af852016-04-12 10:46:47 -07001186 if (ret) {
Dean Luickb7df1922016-07-28 15:21:23 -04001187 atomic_sub(node->npages, &pq->n_locked);
Dean Luicka383f8e2016-07-28 15:21:16 -04001188 iovec->node = NULL;
Mitko Haralanovf53af852016-04-12 10:46:47 -07001189 goto bail;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001190 }
1191 return 0;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001192bail:
Mitko Haralanovf53af852016-04-12 10:46:47 -07001193 if (rb_node)
Ira Weiny3faa3d92016-07-28 15:21:19 -04001194 unpin_vector_pages(pq->mm, node->pages, 0, node->npages);
Mitko Haralanovf53af852016-04-12 10:46:47 -07001195 kfree(node);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001196 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001197}
1198
Mitko Haralanovbd3a8942016-03-08 11:15:33 -08001199static void unpin_vector_pages(struct mm_struct *mm, struct page **pages,
Mitko Haralanov849e3e92016-04-12 10:46:16 -07001200 unsigned start, unsigned npages)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001201{
Ira Weiny639297b2016-07-28 12:27:33 -04001202 hfi1_release_user_pages(mm, pages + start, npages, false);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001203 kfree(pages);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001204}
1205
1206static int check_header_template(struct user_sdma_request *req,
1207 struct hfi1_pkt_header *hdr, u32 lrhlen,
1208 u32 datalen)
1209{
1210 /*
1211 * Perform safety checks for any type of packet:
1212 * - transfer size is multiple of 64bytes
Ira Weinyc4929802016-07-27 21:08:42 -04001213 * - packet length is multiple of 4 bytes
Mike Marciniszyn77241052015-07-30 15:17:43 -04001214 * - packet length is not larger than MTU size
1215 *
1216 * These checks are only done for the first packet of the
1217 * transfer since the header is "given" to us by user space.
1218 * For the remainder of the packets we compute the values.
1219 */
Ira Weinyc4929802016-07-27 21:08:42 -04001220 if (req->info.fragsize % PIO_BLOCK_SIZE || lrhlen & 0x3 ||
Mike Marciniszyn77241052015-07-30 15:17:43 -04001221 lrhlen > get_lrh_len(*hdr, req->info.fragsize))
1222 return -EINVAL;
1223
1224 if (req_opcode(req->info.ctrl) == EXPECTED) {
1225 /*
1226 * The header is checked only on the first packet. Furthermore,
1227 * we ensure that at least one TID entry is copied when the
1228 * request is submitted. Therefore, we don't have to verify that
1229 * tididx points to something sane.
1230 */
1231 u32 tidval = req->tids[req->tididx],
1232 tidlen = EXP_TID_GET(tidval, LEN) * PAGE_SIZE,
1233 tididx = EXP_TID_GET(tidval, IDX),
1234 tidctrl = EXP_TID_GET(tidval, CTRL),
1235 tidoff;
1236 __le32 kval = hdr->kdeth.ver_tid_offset;
1237
1238 tidoff = KDETH_GET(kval, OFFSET) *
1239 (KDETH_GET(req->hdr.kdeth.ver_tid_offset, OM) ?
1240 KDETH_OM_LARGE : KDETH_OM_SMALL);
1241 /*
1242 * Expected receive packets have the following
1243 * additional checks:
1244 * - offset is not larger than the TID size
1245 * - TIDCtrl values match between header and TID array
1246 * - TID indexes match between header and TID array
1247 */
1248 if ((tidoff + datalen > tidlen) ||
1249 KDETH_GET(kval, TIDCTRL) != tidctrl ||
1250 KDETH_GET(kval, TID) != tididx)
1251 return -EINVAL;
1252 }
1253 return 0;
1254}
1255
1256/*
1257 * Correctly set the BTH.PSN field based on type of
1258 * transfer - eager packets can just increment the PSN but
1259 * expected packets encode generation and sequence in the
1260 * BTH.PSN field so just incrementing will result in errors.
1261 */
1262static inline u32 set_pkt_bth_psn(__be32 bthpsn, u8 expct, u32 frags)
1263{
1264 u32 val = be32_to_cpu(bthpsn),
1265 mask = (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffffull :
1266 0xffffffull),
1267 psn = val & mask;
1268 if (expct)
1269 psn = (psn & ~BTH_SEQ_MASK) | ((psn + frags) & BTH_SEQ_MASK);
1270 else
1271 psn = psn + frags;
1272 return psn & mask;
1273}
1274
1275static int set_txreq_header(struct user_sdma_request *req,
1276 struct user_sdma_txreq *tx, u32 datalen)
1277{
1278 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1279 struct hfi1_pkt_header *hdr = &tx->hdr;
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001280 u8 omfactor; /* KDETH.OM */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001281 u16 pbclen;
1282 int ret;
Ira Weinyc4929802016-07-27 21:08:42 -04001283 u32 tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(datalen));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001284
1285 /* Copy the header template to the request before modification */
1286 memcpy(hdr, &req->hdr, sizeof(*hdr));
1287
1288 /*
1289 * Check if the PBC and LRH length are mismatched. If so
1290 * adjust both in the header.
1291 */
1292 pbclen = le16_to_cpu(hdr->pbc[0]);
1293 if (PBC2LRH(pbclen) != lrhlen) {
1294 pbclen = (pbclen & 0xf000) | LRH2PBC(lrhlen);
1295 hdr->pbc[0] = cpu_to_le16(pbclen);
1296 hdr->lrh[2] = cpu_to_be16(lrhlen >> 2);
1297 /*
1298 * Third packet
1299 * This is the first packet in the sequence that has
1300 * a "static" size that can be used for the rest of
1301 * the packets (besides the last one).
1302 */
1303 if (unlikely(req->seqnum == 2)) {
1304 /*
1305 * From this point on the lengths in both the
1306 * PBC and LRH are the same until the last
1307 * packet.
1308 * Adjust the template so we don't have to update
1309 * every packet
1310 */
1311 req->hdr.pbc[0] = hdr->pbc[0];
1312 req->hdr.lrh[2] = hdr->lrh[2];
1313 }
1314 }
1315 /*
1316 * We only have to modify the header if this is not the
1317 * first packet in the request. Otherwise, we use the
1318 * header given to us.
1319 */
1320 if (unlikely(!req->seqnum)) {
1321 ret = check_header_template(req, hdr, lrhlen, datalen);
1322 if (ret)
1323 return ret;
1324 goto done;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001325 }
1326
1327 hdr->bth[2] = cpu_to_be32(
1328 set_pkt_bth_psn(hdr->bth[2],
1329 (req_opcode(req->info.ctrl) == EXPECTED),
1330 req->seqnum));
1331
1332 /* Set ACK request on last packet */
Jakub Pawlake7301392016-12-07 19:32:41 -08001333 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_ACK))
Jubin John8638b772016-02-14 20:19:24 -08001334 hdr->bth[2] |= cpu_to_be32(1UL << 31);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001335
1336 /* Set the new offset */
1337 hdr->kdeth.swdata[6] = cpu_to_le32(req->koffset);
1338 /* Expected packets have to fill in the new TID information */
1339 if (req_opcode(req->info.ctrl) == EXPECTED) {
1340 tidval = req->tids[req->tididx];
1341 /*
1342 * If the offset puts us at the end of the current TID,
1343 * advance everything.
1344 */
1345 if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
1346 PAGE_SIZE)) {
1347 req->tidoffset = 0;
Jubin John4d114fd2016-02-14 20:21:43 -08001348 /*
1349 * Since we don't copy all the TIDs, all at once,
1350 * we have to check again.
1351 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001352 if (++req->tididx > req->n_tids - 1 ||
1353 !req->tids[req->tididx]) {
1354 return -EINVAL;
1355 }
1356 tidval = req->tids[req->tididx];
1357 }
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001358 omfactor = EXP_TID_GET(tidval, LEN) * PAGE_SIZE >=
1359 KDETH_OM_MAX_SIZE ? KDETH_OM_LARGE_SHIFT :
1360 KDETH_OM_SMALL_SHIFT;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001361 /* Set KDETH.TIDCtrl based on value for this TID. */
1362 KDETH_SET(hdr->kdeth.ver_tid_offset, TIDCTRL,
1363 EXP_TID_GET(tidval, CTRL));
1364 /* Set KDETH.TID based on value for this TID */
1365 KDETH_SET(hdr->kdeth.ver_tid_offset, TID,
1366 EXP_TID_GET(tidval, IDX));
Jakub Pawlake7301392016-12-07 19:32:41 -08001367 /* Clear KDETH.SH when DISABLE_SH flag is set */
1368 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_DISABLE_SH))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001369 KDETH_SET(hdr->kdeth.ver_tid_offset, SH, 0);
1370 /*
1371 * Set the KDETH.OFFSET and KDETH.OM based on size of
1372 * transfer.
1373 */
1374 SDMA_DBG(req, "TID offset %ubytes %uunits om%u",
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001375 req->tidoffset, req->tidoffset >> omfactor,
1376 omfactor != KDETH_OM_SMALL_SHIFT);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001377 KDETH_SET(hdr->kdeth.ver_tid_offset, OFFSET,
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001378 req->tidoffset >> omfactor);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001379 KDETH_SET(hdr->kdeth.ver_tid_offset, OM,
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001380 omfactor != KDETH_OM_SMALL_SHIFT);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001381 }
1382done:
1383 trace_hfi1_sdma_user_header(pq->dd, pq->ctxt, pq->subctxt,
1384 req->info.comp_idx, hdr, tidval);
1385 return sdma_txadd_kvaddr(pq->dd, &tx->txreq, hdr, sizeof(*hdr));
1386}
1387
1388static int set_txreq_header_ahg(struct user_sdma_request *req,
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001389 struct user_sdma_txreq *tx, u32 datalen)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001390{
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001391 u32 ahg[AHG_KDETH_ARRAY_SIZE];
Mike Marciniszyn77241052015-07-30 15:17:43 -04001392 int diff = 0;
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001393 u8 omfactor; /* KDETH.OM */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001394 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1395 struct hfi1_pkt_header *hdr = &req->hdr;
1396 u16 pbclen = le16_to_cpu(hdr->pbc[0]);
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001397 u32 val32, tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(datalen));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001398
1399 if (PBC2LRH(pbclen) != lrhlen) {
1400 /* PBC.PbcLengthDWs */
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001401 AHG_HEADER_SET(ahg, diff, 0, 0, 12,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001402 cpu_to_le16(LRH2PBC(lrhlen)));
1403 /* LRH.PktLen (we need the full 16 bits due to byte swap) */
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001404 AHG_HEADER_SET(ahg, diff, 3, 0, 16,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001405 cpu_to_be16(lrhlen >> 2));
1406 }
1407
1408 /*
1409 * Do the common updates
1410 */
1411 /* BTH.PSN and BTH.A */
1412 val32 = (be32_to_cpu(hdr->bth[2]) + req->seqnum) &
1413 (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffff : 0xffffff);
Jakub Pawlake7301392016-12-07 19:32:41 -08001414 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_ACK))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001415 val32 |= 1UL << 31;
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001416 AHG_HEADER_SET(ahg, diff, 6, 0, 16, cpu_to_be16(val32 >> 16));
1417 AHG_HEADER_SET(ahg, diff, 6, 16, 16, cpu_to_be16(val32 & 0xffff));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001418 /* KDETH.Offset */
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001419 AHG_HEADER_SET(ahg, diff, 15, 0, 16,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001420 cpu_to_le16(req->koffset & 0xffff));
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001421 AHG_HEADER_SET(ahg, diff, 15, 16, 16, cpu_to_le16(req->koffset >> 16));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001422 if (req_opcode(req->info.ctrl) == EXPECTED) {
1423 __le16 val;
1424
1425 tidval = req->tids[req->tididx];
1426
1427 /*
1428 * If the offset puts us at the end of the current TID,
1429 * advance everything.
1430 */
1431 if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
1432 PAGE_SIZE)) {
1433 req->tidoffset = 0;
Jubin John4d114fd2016-02-14 20:21:43 -08001434 /*
1435 * Since we don't copy all the TIDs, all at once,
1436 * we have to check again.
1437 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001438 if (++req->tididx > req->n_tids - 1 ||
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001439 !req->tids[req->tididx])
Mike Marciniszyn77241052015-07-30 15:17:43 -04001440 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001441 tidval = req->tids[req->tididx];
1442 }
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001443 omfactor = ((EXP_TID_GET(tidval, LEN) *
Mike Marciniszyn77241052015-07-30 15:17:43 -04001444 PAGE_SIZE) >=
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001445 KDETH_OM_MAX_SIZE) ? KDETH_OM_LARGE_SHIFT :
1446 KDETH_OM_SMALL_SHIFT;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001447 /* KDETH.OM and KDETH.OFFSET (TID) */
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001448 AHG_HEADER_SET(ahg, diff, 7, 0, 16,
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001449 ((!!(omfactor - KDETH_OM_SMALL_SHIFT)) << 15 |
1450 ((req->tidoffset >> omfactor)
1451 & 0x7fff)));
Jakub Pawlake7301392016-12-07 19:32:41 -08001452 /* KDETH.TIDCtrl, KDETH.TID, KDETH.Intr, KDETH.SH */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001453 val = cpu_to_le16(((EXP_TID_GET(tidval, CTRL) & 0x3) << 10) |
Jakub Pawlake7301392016-12-07 19:32:41 -08001454 (EXP_TID_GET(tidval, IDX) & 0x3ff));
1455
1456 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_DISABLE_SH)) {
1457 val |= cpu_to_le16((KDETH_GET(hdr->kdeth.ver_tid_offset,
1458 INTR) <<
1459 AHG_KDETH_INTR_SHIFT));
Jubin Johne4909742016-02-14 20:22:00 -08001460 } else {
Jakub Pawlake7301392016-12-07 19:32:41 -08001461 val |= KDETH_GET(hdr->kdeth.ver_tid_offset, SH) ?
1462 cpu_to_le16(0x1 << AHG_KDETH_SH_SHIFT) :
1463 cpu_to_le16((KDETH_GET(hdr->kdeth.ver_tid_offset,
1464 INTR) <<
1465 AHG_KDETH_INTR_SHIFT));
Jubin Johne4909742016-02-14 20:22:00 -08001466 }
Jakub Pawlake7301392016-12-07 19:32:41 -08001467
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001468 AHG_HEADER_SET(ahg, diff, 7, 16, 14, val);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001469 }
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001470 if (diff < 0)
1471 return diff;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001472
1473 trace_hfi1_sdma_user_header_ahg(pq->dd, pq->ctxt, pq->subctxt,
1474 req->info.comp_idx, req->sde->this_idx,
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001475 req->ahg_idx, ahg, diff, tidval);
1476 sdma_txinit_ahg(&tx->txreq,
1477 SDMA_TXREQ_F_USE_AHG,
1478 datalen, req->ahg_idx, diff,
1479 ahg, sizeof(req->hdr),
1480 user_sdma_txreq_cb);
1481
Mike Marciniszyn77241052015-07-30 15:17:43 -04001482 return diff;
1483}
1484
Mitko Haralanova0d40692015-12-08 17:10:13 -05001485/*
1486 * SDMA tx request completion callback. Called when the SDMA progress
1487 * state machine gets notification that the SDMA descriptors for this
1488 * tx request have been processed by the DMA engine. Called in
1489 * interrupt context.
1490 */
Mike Marciniszyna545f532016-02-14 12:45:53 -08001491static void user_sdma_txreq_cb(struct sdma_txreq *txreq, int status)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001492{
1493 struct user_sdma_txreq *tx =
1494 container_of(txreq, struct user_sdma_txreq, txreq);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001495 struct user_sdma_request *req;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001496 struct hfi1_user_sdma_pkt_q *pq;
1497 struct hfi1_user_sdma_comp_q *cq;
1498 u16 idx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001499
Mitko Haralanova0d40692015-12-08 17:10:13 -05001500 if (!tx->req)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001501 return;
1502
Mitko Haralanova0d40692015-12-08 17:10:13 -05001503 req = tx->req;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001504 pq = req->pq;
1505 cq = req->cq;
Mitko Haralanovb9fb63182015-10-26 10:28:37 -04001506
Mike Marciniszyn77241052015-07-30 15:17:43 -04001507 if (status != SDMA_TXREQ_S_OK) {
Mitko Haralanova0d40692015-12-08 17:10:13 -05001508 SDMA_DBG(req, "SDMA completion with error %d",
1509 status);
Sebastian Sancheze9c48eb2017-05-26 05:35:50 -07001510 WRITE_ONCE(req->has_error, 1);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001511 }
1512
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001513 req->seqcomp = tx->seqnum;
1514 kmem_cache_free(pq->txreq_cache, tx);
1515 tx = NULL;
1516
1517 idx = req->info.comp_idx;
1518 if (req->status == -1 && status == SDMA_TXREQ_S_OK) {
1519 if (req->seqcomp == req->info.npkts - 1) {
1520 req->status = 0;
1521 user_sdma_free_request(req, false);
1522 pq_update(pq);
1523 set_comp_state(pq, cq, idx, COMPLETE, 0);
1524 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001525 } else {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001526 if (status != SDMA_TXREQ_S_OK)
1527 req->status = status;
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001528 if (req->seqcomp == (ACCESS_ONCE(req->seqsubmitted) - 1) &&
Sebastian Sanchezb8884292017-05-26 05:35:44 -07001529 (READ_ONCE(req->done) ||
Sebastian Sancheze9c48eb2017-05-26 05:35:50 -07001530 READ_ONCE(req->has_error))) {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001531 user_sdma_free_request(req, false);
1532 pq_update(pq);
1533 set_comp_state(pq, cq, idx, ERROR, req->status);
1534 }
Mitko Haralanova0d40692015-12-08 17:10:13 -05001535 }
1536}
1537
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001538static inline void pq_update(struct hfi1_user_sdma_pkt_q *pq)
Mitko Haralanova0d40692015-12-08 17:10:13 -05001539{
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001540 if (atomic_dec_and_test(&pq->n_reqs)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001541 xchg(&pq->state, SDMA_PKT_Q_INACTIVE);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001542 wake_up(&pq->wait);
1543 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001544}
1545
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001546static void user_sdma_free_request(struct user_sdma_request *req, bool unpin)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001547{
1548 if (!list_empty(&req->txps)) {
1549 struct sdma_txreq *t, *p;
1550
1551 list_for_each_entry_safe(t, p, &req->txps, list) {
1552 struct user_sdma_txreq *tx =
1553 container_of(t, struct user_sdma_txreq, txreq);
1554 list_del_init(&t->list);
1555 sdma_txclean(req->pq->dd, t);
1556 kmem_cache_free(req->pq->txreq_cache, tx);
1557 }
1558 }
1559 if (req->data_iovs) {
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001560 struct sdma_mmu_node *node;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001561 int i;
1562
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001563 for (i = 0; i < req->data_iovs; i++) {
Mitko Haralanov9565c6a2016-05-19 05:21:18 -07001564 node = req->iovs[i].node;
1565 if (!node)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001566 continue;
1567
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001568 if (unpin)
Dean Luicke0b09ac2016-07-28 15:21:20 -04001569 hfi1_mmu_rb_remove(req->pq->handler,
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001570 &node->rb);
1571 else
1572 atomic_dec(&node->refcount);
1573 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001574 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001575 kfree(req->tids);
Dean Luick7b3256e2016-07-28 15:21:18 -04001576 clear_bit(req->info.comp_idx, req->pq->req_in_use);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001577}
1578
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001579static inline void set_comp_state(struct hfi1_user_sdma_pkt_q *pq,
1580 struct hfi1_user_sdma_comp_q *cq,
1581 u16 idx, enum hfi1_sdma_comp_state state,
1582 int ret)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001583{
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001584 hfi1_cdbg(SDMA, "[%u:%u:%u:%u] Setting completion status %u %d",
1585 pq->dd->unit, pq->ctxt, pq->subctxt, idx, state, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001586 if (state == ERROR)
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001587 cq->comps[idx].errcode = -ret;
Michael J. Ruhl0519c522017-03-20 17:24:45 -07001588 smp_wmb(); /* make sure errcode is visible first */
1589 cq->comps[idx].status = state;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001590 trace_hfi1_sdma_user_completion(pq->dd, pq->ctxt, pq->subctxt,
1591 idx, state, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001592}
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001593
1594static bool sdma_rb_filter(struct mmu_rb_node *node, unsigned long addr,
1595 unsigned long len)
1596{
1597 return (bool)(node->addr == addr);
1598}
1599
Dean Luicke0b09ac2016-07-28 15:21:20 -04001600static int sdma_rb_insert(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001601{
1602 struct sdma_mmu_node *node =
1603 container_of(mnode, struct sdma_mmu_node, rb);
1604
1605 atomic_inc(&node->refcount);
1606 return 0;
1607}
1608
Dean Luickb7df1922016-07-28 15:21:23 -04001609/*
1610 * Return 1 to remove the node from the rb tree and call the remove op.
1611 *
1612 * Called with the rb tree lock held.
1613 */
1614static int sdma_rb_evict(void *arg, struct mmu_rb_node *mnode,
1615 void *evict_arg, bool *stop)
1616{
1617 struct sdma_mmu_node *node =
1618 container_of(mnode, struct sdma_mmu_node, rb);
1619 struct evict_data *evict_data = evict_arg;
1620
1621 /* is this node still being used? */
1622 if (atomic_read(&node->refcount))
1623 return 0; /* keep this node */
1624
1625 /* this node will be evicted, add its pages to our count */
1626 evict_data->cleared += node->npages;
1627
1628 /* have enough pages been cleared? */
1629 if (evict_data->cleared >= evict_data->target)
1630 *stop = true;
1631
1632 return 1; /* remove this node */
1633}
1634
Dean Luick082b3532016-07-28 15:21:25 -04001635static void sdma_rb_remove(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001636{
1637 struct sdma_mmu_node *node =
1638 container_of(mnode, struct sdma_mmu_node, rb);
1639
Dean Luickb7df1922016-07-28 15:21:23 -04001640 atomic_sub(node->npages, &node->pq->n_locked);
Mitko Haralanov5511d782016-03-08 11:15:44 -08001641
Dean Luickb85ced92016-07-28 15:21:24 -04001642 unpin_vector_pages(node->pq->mm, node->pages, 0, node->npages);
1643
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001644 kfree(node);
1645}
1646
Dean Luicke0b09ac2016-07-28 15:21:20 -04001647static int sdma_rb_invalidate(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001648{
1649 struct sdma_mmu_node *node =
1650 container_of(mnode, struct sdma_mmu_node, rb);
1651
1652 if (!atomic_read(&node->refcount))
1653 return 1;
1654 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001655}