blob: aae1f40016e40bcf856fb45c8697a5ca7ec84de3 [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
334static void sdma_kmem_cache_ctor(void *obj)
335{
Janani Ravichandran16ccad02016-02-25 15:08:17 -0500336 struct user_sdma_txreq *tx = obj;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400337
338 memset(tx, 0, sizeof(*tx));
339}
340
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700341int hfi1_user_sdma_alloc_queues(struct hfi1_ctxtdata *uctxt,
342 struct hfi1_filedata *fd)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400343{
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700344 int ret = -ENOMEM;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400345 char buf[64];
346 struct hfi1_devdata *dd;
347 struct hfi1_user_sdma_comp_q *cq;
348 struct hfi1_user_sdma_pkt_q *pq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400349
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700350 if (!uctxt || !fd)
351 return -EBADF;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400352
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700353 if (!hfi1_sdma_comp_ring_size)
354 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400355
356 dd = uctxt->dd;
357
358 pq = kzalloc(sizeof(*pq), GFP_KERNEL);
Alison Schofield806e6e12015-10-12 14:28:36 -0700359 if (!pq)
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700360 return -ENOMEM;
Dean Luick7b3256e2016-07-28 15:21:18 -0400361
Mike Marciniszyn77241052015-07-30 15:17:43 -0400362 pq->dd = dd;
363 pq->ctxt = uctxt->ctxt;
Ira Weiny9e10af42015-10-30 18:58:40 -0400364 pq->subctxt = fd->subctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400365 pq->n_max_reqs = hfi1_sdma_comp_ring_size;
366 pq->state = SDMA_PKT_Q_INACTIVE;
367 atomic_set(&pq->n_reqs, 0);
Mitko Haralanova0d40692015-12-08 17:10:13 -0500368 init_waitqueue_head(&pq->wait);
Dean Luickb7df1922016-07-28 15:21:23 -0400369 atomic_set(&pq->n_locked, 0);
Ira Weiny3faa3d92016-07-28 15:21:19 -0400370 pq->mm = fd->mm;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400371
372 iowait_init(&pq->busy, 0, NULL, defer_packet_queue,
Mike Marciniszyna545f532016-02-14 12:45:53 -0800373 activate_packet_queue, NULL);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400374 pq->reqidx = 0;
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700375
376 pq->reqs = kcalloc(hfi1_sdma_comp_ring_size,
377 sizeof(*pq->reqs),
378 GFP_KERNEL);
379 if (!pq->reqs)
380 goto pq_reqs_nomem;
381
382 pq->req_in_use = kcalloc(BITS_TO_LONGS(hfi1_sdma_comp_ring_size),
383 sizeof(*pq->req_in_use),
384 GFP_KERNEL);
385 if (!pq->req_in_use)
386 goto pq_reqs_no_in_use;
387
Mike Marciniszyn77241052015-07-30 15:17:43 -0400388 snprintf(buf, 64, "txreq-kmem-cache-%u-%u-%u", dd->unit, uctxt->ctxt,
Ira Weiny9e10af42015-10-30 18:58:40 -0400389 fd->subctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400390 pq->txreq_cache = kmem_cache_create(buf,
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700391 sizeof(struct user_sdma_txreq),
Mike Marciniszyn77241052015-07-30 15:17:43 -0400392 L1_CACHE_BYTES,
393 SLAB_HWCACHE_ALIGN,
394 sdma_kmem_cache_ctor);
395 if (!pq->txreq_cache) {
396 dd_dev_err(dd, "[%u] Failed to allocate TxReq cache\n",
397 uctxt->ctxt);
398 goto pq_txreq_nomem;
399 }
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700400
Mike Marciniszyn77241052015-07-30 15:17:43 -0400401 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
Alison Schofield806e6e12015-10-12 14:28:36 -0700402 if (!cq)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400403 goto cq_nomem;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400404
Markus Elfringe036c202017-02-10 08:50:45 +0100405 cq->comps = vmalloc_user(PAGE_ALIGN(sizeof(*cq->comps)
406 * hfi1_sdma_comp_ring_size));
Alison Schofield806e6e12015-10-12 14:28:36 -0700407 if (!cq->comps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400408 goto cq_comps_nomem;
Alison Schofield806e6e12015-10-12 14:28:36 -0700409
Mike Marciniszyn77241052015-07-30 15:17:43 -0400410 cq->nentries = hfi1_sdma_comp_ring_size;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400411
Dean Luickb85ced92016-07-28 15:21:24 -0400412 ret = hfi1_mmu_rb_register(pq, pq->mm, &sdma_rb_ops, dd->pport->hfi1_wq,
413 &pq->handler);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800414 if (ret) {
415 dd_dev_err(dd, "Failed to register with MMU %d", ret);
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700416 goto pq_mmu_fail;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800417 }
418
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700419 fd->pq = pq;
420 fd->cq = cq;
421
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700422 return 0;
423
424pq_mmu_fail:
425 vfree(cq->comps);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400426cq_comps_nomem:
427 kfree(cq);
428cq_nomem:
429 kmem_cache_destroy(pq->txreq_cache);
430pq_txreq_nomem:
Dean Luick7b3256e2016-07-28 15:21:18 -0400431 kfree(pq->req_in_use);
432pq_reqs_no_in_use:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400433 kfree(pq->reqs);
434pq_reqs_nomem:
435 kfree(pq);
Michael J. Ruhl62239fc2017-05-04 05:15:21 -0700436
Mike Marciniszyn77241052015-07-30 15:17:43 -0400437 return ret;
438}
439
Michael J. Ruhle87473b2017-07-29 08:43:32 -0700440int hfi1_user_sdma_free_queues(struct hfi1_filedata *fd,
441 struct hfi1_ctxtdata *uctxt)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400442{
Mike Marciniszyn77241052015-07-30 15:17:43 -0400443 struct hfi1_user_sdma_pkt_q *pq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400444
445 hfi1_cdbg(SDMA, "[%u:%u:%u] Freeing user SDMA queues", uctxt->dd->unit,
446 uctxt->ctxt, fd->subctxt);
447 pq = fd->pq;
448 if (pq) {
Dean Luicke0b09ac2016-07-28 15:21:20 -0400449 if (pq->handler)
450 hfi1_mmu_rb_unregister(pq->handler);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400451 iowait_sdma_drain(&pq->busy);
Mitko Haralanova0d40692015-12-08 17:10:13 -0500452 /* Wait until all requests have been freed. */
453 wait_event_interruptible(
454 pq->wait,
455 (ACCESS_ONCE(pq->state) == SDMA_PKT_Q_INACTIVE));
456 kfree(pq->reqs);
Dean Luick7b3256e2016-07-28 15:21:18 -0400457 kfree(pq->req_in_use);
Julia Lawalladad44d2015-09-13 14:15:04 +0200458 kmem_cache_destroy(pq->txreq_cache);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400459 kfree(pq);
460 fd->pq = NULL;
461 }
462 if (fd->cq) {
Bhumika Goyala4d7d052016-02-14 20:34:28 +0530463 vfree(fd->cq->comps);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400464 kfree(fd->cq);
465 fd->cq = NULL;
466 }
467 return 0;
468}
469
Jianxin Xiong14833b82016-07-01 16:01:56 -0700470static u8 dlid_to_selector(u16 dlid)
471{
472 static u8 mapping[256];
473 static int initialized;
474 static u8 next;
475 int hash;
476
477 if (!initialized) {
478 memset(mapping, 0xFF, 256);
479 initialized = 1;
480 }
481
482 hash = ((dlid >> 8) ^ dlid) & 0xFF;
483 if (mapping[hash] == 0xFF) {
484 mapping[hash] = next;
485 next = (next + 1) & 0x7F;
486 }
487
488 return mapping[hash];
489}
490
Michael J. Ruhl5042cdd2017-05-04 05:14:45 -0700491int hfi1_user_sdma_process_request(struct hfi1_filedata *fd,
492 struct iovec *iovec, unsigned long dim,
493 unsigned long *count)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400494{
Dean Luickff4ce9b2016-07-28 12:27:34 -0400495 int ret = 0, i;
Ira Weiny9e10af42015-10-30 18:58:40 -0400496 struct hfi1_ctxtdata *uctxt = fd->uctxt;
497 struct hfi1_user_sdma_pkt_q *pq = fd->pq;
498 struct hfi1_user_sdma_comp_q *cq = fd->cq;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400499 struct hfi1_devdata *dd = pq->dd;
500 unsigned long idx = 0;
501 u8 pcount = initial_pkt_count;
502 struct sdma_req_info info;
503 struct user_sdma_request *req;
504 u8 opcode, sc, vl;
Don Hiatt566d53a2017-08-04 13:54:47 -0700505 u16 pkey;
506 u32 slid;
Jianxin Xiongb583faf2016-05-19 05:21:57 -0700507 int req_queued = 0;
Jianxin Xiong14833b82016-07-01 16:01:56 -0700508 u16 dlid;
Tadeusz Struk0cb2aa62016-09-25 07:44:23 -0700509 u32 selector;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400510
511 if (iovec[idx].iov_len < sizeof(info) + sizeof(req->hdr)) {
512 hfi1_cdbg(
513 SDMA,
514 "[%u:%u:%u] First vector not big enough for header %lu/%lu",
Ira Weiny9e10af42015-10-30 18:58:40 -0400515 dd->unit, uctxt->ctxt, fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400516 iovec[idx].iov_len, sizeof(info) + sizeof(req->hdr));
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500517 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400518 }
519 ret = copy_from_user(&info, iovec[idx].iov_base, sizeof(info));
520 if (ret) {
521 hfi1_cdbg(SDMA, "[%u:%u:%u] Failed to copy info QW (%d)",
Ira Weiny9e10af42015-10-30 18:58:40 -0400522 dd->unit, uctxt->ctxt, fd->subctxt, ret);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500523 return -EFAULT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400524 }
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800525
Ira Weiny9e10af42015-10-30 18:58:40 -0400526 trace_hfi1_sdma_user_reqinfo(dd, uctxt->ctxt, fd->subctxt,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400527 (u16 *)&info);
Dean Luick4fa0d222016-07-28 15:21:14 -0400528
529 if (info.comp_idx >= hfi1_sdma_comp_ring_size) {
530 hfi1_cdbg(SDMA,
531 "[%u:%u:%u:%u] Invalid comp index",
532 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx);
533 return -EINVAL;
534 }
535
Dean Luick9ff73c82016-07-28 15:21:15 -0400536 /*
537 * Sanity check the header io vector count. Need at least 1 vector
538 * (header) and cannot be larger than the actual io vector count.
539 */
540 if (req_iovcnt(info.ctrl) < 1 || req_iovcnt(info.ctrl) > dim) {
541 hfi1_cdbg(SDMA,
542 "[%u:%u:%u:%u] Invalid iov count %d, dim %ld",
543 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx,
544 req_iovcnt(info.ctrl), dim);
545 return -EINVAL;
546 }
547
Mike Marciniszyn77241052015-07-30 15:17:43 -0400548 if (!info.fragsize) {
549 hfi1_cdbg(SDMA,
550 "[%u:%u:%u:%u] Request does not specify fragsize",
Ira Weiny9e10af42015-10-30 18:58:40 -0400551 dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500552 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400553 }
Dean Luick7b3256e2016-07-28 15:21:18 -0400554
555 /* Try to claim the request. */
556 if (test_and_set_bit(info.comp_idx, pq->req_in_use)) {
557 hfi1_cdbg(SDMA, "[%u:%u:%u] Entry %u is in use",
558 dd->unit, uctxt->ctxt, fd->subctxt,
559 info.comp_idx);
560 return -EBADSLT;
561 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400562 /*
Dean Luick7b3256e2016-07-28 15:21:18 -0400563 * All safety checks have been done and this request has been claimed.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400564 */
565 hfi1_cdbg(SDMA, "[%u:%u:%u] Using req/comp entry %u\n", dd->unit,
Ira Weiny9e10af42015-10-30 18:58:40 -0400566 uctxt->ctxt, fd->subctxt, info.comp_idx);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400567 req = pq->reqs + info.comp_idx;
Dean Luick9ff73c82016-07-28 15:21:15 -0400568 req->data_iovs = req_iovcnt(info.ctrl) - 1; /* subtract header vector */
Sebastian Sanchezceb26562017-05-12 09:19:36 -0700569 req->data_len = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400570 req->pq = pq;
571 req->cq = cq;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500572 req->status = -1;
Sebastian Sanchez780a4c12017-05-04 05:14:51 -0700573 req->ahg_idx = -1;
Sebastian Sanchezceb26562017-05-12 09:19:36 -0700574 req->iov_idx = 0;
575 req->sent = 0;
576 req->seqnum = 0;
577 req->seqcomp = 0;
578 req->seqsubmitted = 0;
Sebastian Sanchezceb26562017-05-12 09:19:36 -0700579 req->tids = NULL;
Sebastian Sanchezb8884292017-05-26 05:35:44 -0700580 req->done = 0;
Sebastian Sancheze9c48eb2017-05-26 05:35:50 -0700581 req->has_error = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400582 INIT_LIST_HEAD(&req->txps);
Mitko Haralanova0d40692015-12-08 17:10:13 -0500583
Mike Marciniszyn77241052015-07-30 15:17:43 -0400584 memcpy(&req->info, &info, sizeof(info));
585
Dean Luick9ff73c82016-07-28 15:21:15 -0400586 if (req_opcode(info.ctrl) == EXPECTED) {
587 /* expected must have a TID info and at least one data vector */
588 if (req->data_iovs < 2) {
589 SDMA_DBG(req,
590 "Not enough vectors for expected request");
591 ret = -EINVAL;
592 goto free_req;
593 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400594 req->data_iovs--;
Dean Luick9ff73c82016-07-28 15:21:15 -0400595 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400596
597 if (!info.npkts || req->data_iovs > MAX_VECTORS_PER_REQ) {
598 SDMA_DBG(req, "Too many vectors (%u/%u)", req->data_iovs,
599 MAX_VECTORS_PER_REQ);
Dean Luick9da7e9a2016-07-28 15:21:17 -0400600 ret = -EINVAL;
601 goto free_req;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400602 }
603 /* Copy the header from the user buffer */
604 ret = copy_from_user(&req->hdr, iovec[idx].iov_base + sizeof(info),
605 sizeof(req->hdr));
606 if (ret) {
607 SDMA_DBG(req, "Failed to copy header template (%d)", ret);
608 ret = -EFAULT;
609 goto free_req;
610 }
611
612 /* If Static rate control is not enabled, sanitize the header. */
613 if (!HFI1_CAP_IS_USET(STATIC_RATE_CTRL))
614 req->hdr.pbc[2] = 0;
615
616 /* Validate the opcode. Do not trust packets from user space blindly. */
617 opcode = (be32_to_cpu(req->hdr.bth[0]) >> 24) & 0xff;
618 if ((opcode & USER_OPCODE_CHECK_MASK) !=
619 USER_OPCODE_CHECK_VAL) {
620 SDMA_DBG(req, "Invalid opcode (%d)", opcode);
621 ret = -EINVAL;
622 goto free_req;
623 }
624 /*
625 * Validate the vl. Do not trust packets from user space blindly.
626 * VL comes from PBC, SC comes from LRH, and the VL needs to
627 * match the SC look up.
628 */
629 vl = (le16_to_cpu(req->hdr.pbc[0]) >> 12) & 0xF;
630 sc = (((be16_to_cpu(req->hdr.lrh[0]) >> 12) & 0xF) |
631 (((le16_to_cpu(req->hdr.pbc[1]) >> 14) & 0x1) << 4));
632 if (vl >= dd->pport->vls_operational ||
633 vl != sc_to_vlt(dd, sc)) {
634 SDMA_DBG(req, "Invalid SC(%u)/VL(%u)", sc, vl);
635 ret = -EINVAL;
636 goto free_req;
637 }
638
Sebastian Sancheze38d1e42016-04-12 11:22:21 -0700639 /* Checking P_KEY for requests from user-space */
Don Hiatt566d53a2017-08-04 13:54:47 -0700640 pkey = (u16)be32_to_cpu(req->hdr.bth[0]);
641 slid = be16_to_cpu(req->hdr.lrh[3]);
642 if (egress_pkey_check(dd->pport, slid, pkey, sc, PKEY_CHECK_INVALID)) {
Sebastian Sancheze38d1e42016-04-12 11:22:21 -0700643 ret = -EINVAL;
644 goto free_req;
645 }
646
Mike Marciniszyn77241052015-07-30 15:17:43 -0400647 /*
648 * Also should check the BTH.lnh. If it says the next header is GRH then
649 * the RXE parsing will be off and will land in the middle of the KDETH
650 * or miss it entirely.
651 */
652 if ((be16_to_cpu(req->hdr.lrh[0]) & 0x3) == HFI1_LRH_GRH) {
653 SDMA_DBG(req, "User tried to pass in a GRH");
654 ret = -EINVAL;
655 goto free_req;
656 }
657
658 req->koffset = le32_to_cpu(req->hdr.kdeth.swdata[6]);
Jubin John4d114fd2016-02-14 20:21:43 -0800659 /*
660 * Calculate the initial TID offset based on the values of
661 * KDETH.OFFSET and KDETH.OM that are passed in.
662 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400663 req->tidoffset = KDETH_GET(req->hdr.kdeth.ver_tid_offset, OFFSET) *
664 (KDETH_GET(req->hdr.kdeth.ver_tid_offset, OM) ?
665 KDETH_OM_LARGE : KDETH_OM_SMALL);
666 SDMA_DBG(req, "Initial TID offset %u", req->tidoffset);
667 idx++;
668
669 /* Save all the IO vector structures */
Dean Luickff4ce9b2016-07-28 12:27:34 -0400670 for (i = 0; i < req->data_iovs; i++) {
Sebastian Sanchezceb26562017-05-12 09:19:36 -0700671 req->iovs[i].offset = 0;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800672 INIT_LIST_HEAD(&req->iovs[i].list);
Markus Elfringdb6f0282017-02-10 21:45:38 +0100673 memcpy(&req->iovs[i].iov,
674 iovec + idx++,
675 sizeof(req->iovs[i].iov));
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800676 ret = pin_vector_pages(req, &req->iovs[i]);
677 if (ret) {
Sebastian Sanchezceb26562017-05-12 09:19:36 -0700678 req->data_iovs = i;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -0800679 req->status = ret;
680 goto free_req;
681 }
Dean Luickff4ce9b2016-07-28 12:27:34 -0400682 req->data_len += req->iovs[i].iov.iov_len;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400683 }
684 SDMA_DBG(req, "total data length %u", req->data_len);
685
686 if (pcount > req->info.npkts)
687 pcount = req->info.npkts;
688 /*
689 * Copy any TID info
690 * User space will provide the TID info only when the
691 * request type is EXPECTED. This is true even if there is
692 * only one packet in the request and the header is already
693 * setup. The reason for the singular TID case is that the
694 * driver needs to perform safety checks.
695 */
696 if (req_opcode(req->info.ctrl) == EXPECTED) {
697 u16 ntids = iovec[idx].iov_len / sizeof(*req->tids);
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800698 u32 *tmp;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400699
700 if (!ntids || ntids > MAX_TID_PAIR_ENTRIES) {
701 ret = -EINVAL;
702 goto free_req;
703 }
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800704
Mike Marciniszyn77241052015-07-30 15:17:43 -0400705 /*
706 * We have to copy all of the tids because they may vary
707 * in size and, therefore, the TID count might not be
708 * equal to the pkt count. However, there is no way to
709 * tell at this point.
710 */
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800711 tmp = memdup_user(iovec[idx].iov_base,
712 ntids * sizeof(*req->tids));
713 if (IS_ERR(tmp)) {
714 ret = PTR_ERR(tmp);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400715 SDMA_DBG(req, "Failed to copy %d TIDs (%d)",
716 ntids, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400717 goto free_req;
718 }
Michael J. Ruhl1bb0d7b2017-02-08 05:28:31 -0800719 req->tids = tmp;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400720 req->n_tids = ntids;
Sebastian Sanchezceb26562017-05-12 09:19:36 -0700721 req->tididx = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400722 idx++;
723 }
724
Jianxin Xiong14833b82016-07-01 16:01:56 -0700725 dlid = be16_to_cpu(req->hdr.lrh[1]);
726 selector = dlid_to_selector(dlid);
Tadeusz Struk0cb2aa62016-09-25 07:44:23 -0700727 selector += uctxt->ctxt + fd->subctxt;
728 req->sde = sdma_select_user_engine(dd, selector, vl);
Jianxin Xiong14833b82016-07-01 16:01:56 -0700729
Mike Marciniszyn77241052015-07-30 15:17:43 -0400730 if (!req->sde || !sdma_running(req->sde)) {
731 ret = -ECOMM;
732 goto free_req;
733 }
734
735 /* We don't need an AHG entry if the request contains only one packet */
Sebastian Sanchez780a4c12017-05-04 05:14:51 -0700736 if (req->info.npkts > 1 && HFI1_CAP_IS_USET(SDMA_AHG))
737 req->ahg_idx = sdma_ahg_alloc(req->sde);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400738
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800739 set_comp_state(pq, cq, info.comp_idx, QUEUED, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400740 atomic_inc(&pq->n_reqs);
Jianxin Xiongb583faf2016-05-19 05:21:57 -0700741 req_queued = 1;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800742 /* Send the first N packets in the request to buy us some time */
743 ret = user_sdma_send_pkts(req, pcount);
744 if (unlikely(ret < 0 && ret != -EBUSY)) {
745 req->status = ret;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800746 goto free_req;
747 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400748
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800749 /*
750 * It is possible that the SDMA engine would have processed all the
751 * submitted packets by the time we get here. Therefore, only set
752 * packet queue state to ACTIVE if there are still uncompleted
753 * requests.
754 */
755 if (atomic_read(&pq->n_reqs))
756 xchg(&pq->state, SDMA_PKT_Q_ACTIVE);
757
758 /*
759 * This is a somewhat blocking send implementation.
760 * The driver will block the caller until all packets of the
761 * request have been submitted to the SDMA engine. However, it
762 * will not wait for send completions.
763 */
Sebastian Sanchezb8884292017-05-26 05:35:44 -0700764 while (req->seqsubmitted != req->info.npkts) {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800765 ret = user_sdma_send_pkts(req, pcount);
766 if (ret < 0) {
767 if (ret != -EBUSY) {
768 req->status = ret;
Sebastian Sancheze9c48eb2017-05-26 05:35:50 -0700769 WRITE_ONCE(req->has_error, 1);
Mitko Haralanova402d6a2016-02-03 14:37:41 -0800770 if (ACCESS_ONCE(req->seqcomp) ==
771 req->seqsubmitted - 1)
772 goto free_req;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800773 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400774 }
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800775 wait_event_interruptible_timeout(
776 pq->busy.wait_dma,
777 (pq->state == SDMA_PKT_Q_ACTIVE),
778 msecs_to_jiffies(
779 SDMA_IOWAIT_TIMEOUT));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400780 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400781 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400782 *count += idx;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500783 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400784free_req:
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800785 user_sdma_free_request(req, true);
Jianxin Xiongb583faf2016-05-19 05:21:57 -0700786 if (req_queued)
787 pq_update(pq);
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -0800788 set_comp_state(pq, cq, info.comp_idx, ERROR, req->status);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400789 return ret;
790}
791
792static inline u32 compute_data_length(struct user_sdma_request *req,
Jubin John17fb4f22016-02-14 20:21:52 -0800793 struct user_sdma_txreq *tx)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400794{
795 /*
796 * Determine the proper size of the packet data.
797 * The size of the data of the first packet is in the header
798 * template. However, it includes the header and ICRC, which need
799 * to be subtracted.
Ira Weinyc4929802016-07-27 21:08:42 -0400800 * The minimum representable packet data length in a header is 4 bytes,
801 * therefore, when the data length request is less than 4 bytes, there's
802 * only one packet, and the packet data length is equal to that of the
803 * request data length.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400804 * The size of the remaining packets is the minimum of the frag
805 * size (MTU) or remaining data in the request.
806 */
807 u32 len;
808
809 if (!req->seqnum) {
Ira Weinyc4929802016-07-27 21:08:42 -0400810 if (req->data_len < sizeof(u32))
811 len = req->data_len;
812 else
813 len = ((be16_to_cpu(req->hdr.lrh[2]) << 2) -
814 (sizeof(tx->hdr) - 4));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400815 } else if (req_opcode(req->info.ctrl) == EXPECTED) {
816 u32 tidlen = EXP_TID_GET(req->tids[req->tididx], LEN) *
817 PAGE_SIZE;
Jubin John4d114fd2016-02-14 20:21:43 -0800818 /*
819 * Get the data length based on the remaining space in the
820 * TID pair.
821 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400822 len = min(tidlen - req->tidoffset, (u32)req->info.fragsize);
823 /* If we've filled up the TID pair, move to the next one. */
824 if (unlikely(!len) && ++req->tididx < req->n_tids &&
825 req->tids[req->tididx]) {
826 tidlen = EXP_TID_GET(req->tids[req->tididx],
827 LEN) * PAGE_SIZE;
828 req->tidoffset = 0;
829 len = min_t(u32, tidlen, req->info.fragsize);
830 }
Jubin John4d114fd2016-02-14 20:21:43 -0800831 /*
832 * Since the TID pairs map entire pages, make sure that we
Mike Marciniszyn77241052015-07-30 15:17:43 -0400833 * are not going to try to send more data that we have
Jubin John4d114fd2016-02-14 20:21:43 -0800834 * remaining.
835 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400836 len = min(len, req->data_len - req->sent);
Jubin Johne4909742016-02-14 20:22:00 -0800837 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400838 len = min(req->data_len - req->sent, (u32)req->info.fragsize);
Jubin Johne4909742016-02-14 20:22:00 -0800839 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400840 SDMA_DBG(req, "Data Length = %u", len);
841 return len;
842}
843
Ira Weinyc4929802016-07-27 21:08:42 -0400844static inline u32 pad_len(u32 len)
845{
846 if (len & (sizeof(u32) - 1))
847 len += sizeof(u32) - (len & (sizeof(u32) - 1));
848 return len;
849}
850
Mike Marciniszyn77241052015-07-30 15:17:43 -0400851static inline u32 get_lrh_len(struct hfi1_pkt_header hdr, u32 len)
852{
853 /* (Size of complete header - size of PBC) + 4B ICRC + data length */
854 return ((sizeof(hdr) - sizeof(hdr.pbc)) + 4 + len);
855}
856
857static int user_sdma_send_pkts(struct user_sdma_request *req, unsigned maxpkts)
858{
Harish Chegondi0b115ef2016-09-06 04:35:37 -0700859 int ret = 0, count;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400860 unsigned npkts = 0;
861 struct user_sdma_txreq *tx = NULL;
862 struct hfi1_user_sdma_pkt_q *pq = NULL;
863 struct user_sdma_iovec *iovec = NULL;
864
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500865 if (!req->pq)
866 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400867
868 pq = req->pq;
869
Mitko Haralanov6a5464f2015-12-08 17:10:12 -0500870 /* If tx completion has reported an error, we are done. */
Sebastian Sancheze9c48eb2017-05-26 05:35:50 -0700871 if (READ_ONCE(req->has_error))
Mitko Haralanov6a5464f2015-12-08 17:10:12 -0500872 return -EFAULT;
Mitko Haralanov6a5464f2015-12-08 17:10:12 -0500873
Mike Marciniszyn77241052015-07-30 15:17:43 -0400874 /*
875 * Check if we might have sent the entire request already
876 */
877 if (unlikely(req->seqnum == req->info.npkts)) {
878 if (!list_empty(&req->txps))
879 goto dosend;
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500880 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400881 }
882
883 if (!maxpkts || maxpkts > req->info.npkts - req->seqnum)
884 maxpkts = req->info.npkts - req->seqnum;
885
886 while (npkts < maxpkts) {
887 u32 datalen = 0, queued = 0, data_sent = 0;
888 u64 iov_offset = 0;
889
890 /*
891 * Check whether any of the completions have come back
892 * with errors. If so, we are not going to process any
893 * more packets from this request.
894 */
Sebastian Sancheze9c48eb2017-05-26 05:35:50 -0700895 if (READ_ONCE(req->has_error))
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500896 return -EFAULT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400897
898 tx = kmem_cache_alloc(pq->txreq_cache, GFP_KERNEL);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500899 if (!tx)
900 return -ENOMEM;
901
Mike Marciniszyn77241052015-07-30 15:17:43 -0400902 tx->flags = 0;
903 tx->req = req;
904 tx->busycount = 0;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500905 INIT_LIST_HEAD(&tx->list);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400906
Jakub Pawlake7301392016-12-07 19:32:41 -0800907 /*
908 * For the last packet set the ACK request
909 * and disable header suppression.
910 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400911 if (req->seqnum == req->info.npkts - 1)
Jakub Pawlake7301392016-12-07 19:32:41 -0800912 tx->flags |= (TXREQ_FLAGS_REQ_ACK |
913 TXREQ_FLAGS_REQ_DISABLE_SH);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400914
915 /*
916 * Calculate the payload size - this is min of the fragment
917 * (MTU) size or the remaining bytes in the request but only
918 * if we have payload data.
919 */
920 if (req->data_len) {
921 iovec = &req->iovs[req->iov_idx];
922 if (ACCESS_ONCE(iovec->offset) == iovec->iov.iov_len) {
923 if (++req->iov_idx == req->data_iovs) {
924 ret = -EFAULT;
925 goto free_txreq;
926 }
927 iovec = &req->iovs[req->iov_idx];
928 WARN_ON(iovec->offset);
929 }
930
Mike Marciniszyn77241052015-07-30 15:17:43 -0400931 datalen = compute_data_length(req, tx);
Jakub Pawlake7301392016-12-07 19:32:41 -0800932
933 /*
934 * Disable header suppression for the payload <= 8DWS.
935 * If there is an uncorrectable error in the receive
936 * data FIFO when the received payload size is less than
937 * or equal to 8DWS then the RxDmaDataFifoRdUncErr is
938 * not reported.There is set RHF.EccErr if the header
939 * is not suppressed.
940 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400941 if (!datalen) {
942 SDMA_DBG(req,
943 "Request has data but pkt len is 0");
944 ret = -EFAULT;
945 goto free_tx;
Jakub Pawlake7301392016-12-07 19:32:41 -0800946 } else if (datalen <= 32) {
947 tx->flags |= TXREQ_FLAGS_REQ_DISABLE_SH;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400948 }
949 }
950
Sebastian Sanchez780a4c12017-05-04 05:14:51 -0700951 if (req->ahg_idx >= 0) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400952 if (!req->seqnum) {
953 u16 pbclen = le16_to_cpu(req->hdr.pbc[0]);
Ira Weinyc4929802016-07-27 21:08:42 -0400954 u32 lrhlen = get_lrh_len(req->hdr,
955 pad_len(datalen));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400956 /*
957 * Copy the request header into the tx header
958 * because the HW needs a cacheline-aligned
959 * address.
960 * This copy can be optimized out if the hdr
961 * member of user_sdma_request were also
962 * cacheline aligned.
963 */
964 memcpy(&tx->hdr, &req->hdr, sizeof(tx->hdr));
965 if (PBC2LRH(pbclen) != lrhlen) {
966 pbclen = (pbclen & 0xf000) |
967 LRH2PBC(lrhlen);
968 tx->hdr.pbc[0] = cpu_to_le16(pbclen);
969 }
Jakub Pawlake7301392016-12-07 19:32:41 -0800970 ret = check_header_template(req, &tx->hdr,
971 lrhlen, datalen);
972 if (ret)
973 goto free_tx;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400974 ret = sdma_txinit_ahg(&tx->txreq,
975 SDMA_TXREQ_F_AHG_COPY,
976 sizeof(tx->hdr) + datalen,
977 req->ahg_idx, 0, NULL, 0,
978 user_sdma_txreq_cb);
979 if (ret)
980 goto free_tx;
981 ret = sdma_txadd_kvaddr(pq->dd, &tx->txreq,
982 &tx->hdr,
983 sizeof(tx->hdr));
984 if (ret)
985 goto free_txreq;
986 } else {
987 int changes;
988
989 changes = set_txreq_header_ahg(req, tx,
990 datalen);
991 if (changes < 0)
992 goto free_tx;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400993 }
994 } else {
995 ret = sdma_txinit(&tx->txreq, 0, sizeof(req->hdr) +
996 datalen, user_sdma_txreq_cb);
997 if (ret)
998 goto free_tx;
999 /*
1000 * Modify the header for this packet. This only needs
1001 * to be done if we are not going to use AHG. Otherwise,
1002 * the HW will do it based on the changes we gave it
1003 * during sdma_txinit_ahg().
1004 */
1005 ret = set_txreq_header(req, tx, datalen);
1006 if (ret)
1007 goto free_txreq;
1008 }
1009
1010 /*
1011 * If the request contains any data vectors, add up to
1012 * fragsize bytes to the descriptor.
1013 */
1014 while (queued < datalen &&
1015 (req->sent + data_sent) < req->data_len) {
1016 unsigned long base, offset;
1017 unsigned pageidx, len;
1018
1019 base = (unsigned long)iovec->iov.iov_base;
Amitoj Kaur Chawla72a5f6a2016-02-20 19:08:02 +05301020 offset = offset_in_page(base + iovec->offset +
1021 iov_offset);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001022 pageidx = (((iovec->offset + iov_offset +
1023 base) - (base & PAGE_MASK)) >> PAGE_SHIFT);
1024 len = offset + req->info.fragsize > PAGE_SIZE ?
1025 PAGE_SIZE - offset : req->info.fragsize;
1026 len = min((datalen - queued), len);
1027 ret = sdma_txadd_page(pq->dd, &tx->txreq,
1028 iovec->pages[pageidx],
1029 offset, len);
1030 if (ret) {
Mitko Haralanova0d40692015-12-08 17:10:13 -05001031 SDMA_DBG(req, "SDMA txreq add page failed %d\n",
1032 ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001033 goto free_txreq;
1034 }
1035 iov_offset += len;
1036 queued += len;
1037 data_sent += len;
1038 if (unlikely(queued < datalen &&
1039 pageidx == iovec->npages &&
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001040 req->iov_idx < req->data_iovs - 1)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001041 iovec->offset += iov_offset;
1042 iovec = &req->iovs[++req->iov_idx];
Mike Marciniszyn77241052015-07-30 15:17:43 -04001043 iov_offset = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001044 }
1045 }
1046 /*
1047 * The txreq was submitted successfully so we can update
1048 * the counters.
1049 */
1050 req->koffset += datalen;
1051 if (req_opcode(req->info.ctrl) == EXPECTED)
1052 req->tidoffset += datalen;
1053 req->sent += data_sent;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001054 if (req->data_len)
1055 iovec->offset += iov_offset;
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001056 list_add_tail(&tx->txreq.list, &req->txps);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001057 /*
1058 * It is important to increment this here as it is used to
1059 * generate the BTH.PSN and, therefore, can't be bulk-updated
1060 * outside of the loop.
1061 */
1062 tx->seqnum = req->seqnum++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001063 npkts++;
1064 }
1065dosend:
Harish Chegondi0b115ef2016-09-06 04:35:37 -07001066 ret = sdma_send_txlist(req->sde, &pq->busy, &req->txps, &count);
1067 req->seqsubmitted += count;
1068 if (req->seqsubmitted == req->info.npkts) {
Sebastian Sanchezb8884292017-05-26 05:35:44 -07001069 WRITE_ONCE(req->done, 1);
Harish Chegondi0b115ef2016-09-06 04:35:37 -07001070 /*
1071 * The txreq has already been submitted to the HW queue
1072 * so we can free the AHG entry now. Corruption will not
1073 * happen due to the sequential manner in which
1074 * descriptors are processed.
1075 */
Sebastian Sanchez780a4c12017-05-04 05:14:51 -07001076 if (req->ahg_idx >= 0)
Harish Chegondi0b115ef2016-09-06 04:35:37 -07001077 sdma_ahg_free(req->sde, req->ahg_idx);
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001078 }
Mitko Haralanovfaa98b82015-12-08 17:10:11 -05001079 return ret;
1080
Mike Marciniszyn77241052015-07-30 15:17:43 -04001081free_txreq:
1082 sdma_txclean(pq->dd, &tx->txreq);
1083free_tx:
1084 kmem_cache_free(pq->txreq_cache, tx);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001085 return ret;
1086}
1087
1088/*
1089 * How many pages in this iovec element?
1090 */
1091static inline int num_user_pages(const struct iovec *iov)
1092{
Jubin John50e5dcb2016-02-14 20:19:41 -08001093 const unsigned long addr = (unsigned long)iov->iov_base;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001094 const unsigned long len = iov->iov_len;
1095 const unsigned long spage = addr & PAGE_MASK;
1096 const unsigned long epage = (addr + len - 1) & PAGE_MASK;
1097
1098 return 1 + ((epage - spage) >> PAGE_SHIFT);
1099}
1100
Mitko Haralanov5511d782016-03-08 11:15:44 -08001101static u32 sdma_cache_evict(struct hfi1_user_sdma_pkt_q *pq, u32 npages)
1102{
Dean Luickb7df1922016-07-28 15:21:23 -04001103 struct evict_data evict_data;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001104
Dean Luickb7df1922016-07-28 15:21:23 -04001105 evict_data.cleared = 0;
1106 evict_data.target = npages;
1107 hfi1_mmu_rb_evict(pq->handler, &evict_data);
1108 return evict_data.cleared;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001109}
1110
Mike Marciniszyn77241052015-07-30 15:17:43 -04001111static int pin_vector_pages(struct user_sdma_request *req,
Ira Weiny72720dd2016-07-28 12:27:25 -04001112 struct user_sdma_iovec *iovec)
1113{
Mitko Haralanov5511d782016-03-08 11:15:44 -08001114 int ret = 0, pinned, npages, cleared;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001115 struct page **pages;
1116 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1117 struct sdma_mmu_node *node = NULL;
1118 struct mmu_rb_node *rb_node;
Sebastian Sanchez7be85672017-05-26 05:35:12 -07001119 bool extracted;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001120
Sebastian Sanchez7be85672017-05-26 05:35:12 -07001121 extracted =
1122 hfi1_mmu_rb_remove_unless_exact(pq->handler,
1123 (unsigned long)
1124 iovec->iov.iov_base,
1125 iovec->iov.iov_len, &rb_node);
1126 if (rb_node) {
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001127 node = container_of(rb_node, struct sdma_mmu_node, rb);
Sebastian Sanchez7be85672017-05-26 05:35:12 -07001128 if (!extracted) {
1129 atomic_inc(&node->refcount);
1130 iovec->pages = node->pages;
1131 iovec->npages = node->npages;
1132 iovec->node = node;
1133 return 0;
1134 }
1135 }
Mitko Haralanova0d40692015-12-08 17:10:13 -05001136
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001137 if (!node) {
1138 node = kzalloc(sizeof(*node), GFP_KERNEL);
1139 if (!node)
1140 return -ENOMEM;
1141
1142 node->rb.addr = (unsigned long)iovec->iov.iov_base;
Mitko Haralanov5511d782016-03-08 11:15:44 -08001143 node->pq = pq;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001144 atomic_set(&node->refcount, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001145 }
Mitko Haralanova0d40692015-12-08 17:10:13 -05001146
Mike Marciniszyn77241052015-07-30 15:17:43 -04001147 npages = num_user_pages(&iovec->iov);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001148 if (node->npages < npages) {
1149 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
1150 if (!pages) {
1151 SDMA_DBG(req, "Failed page array alloc");
1152 ret = -ENOMEM;
1153 goto bail;
1154 }
1155 memcpy(pages, node->pages, node->npages * sizeof(*pages));
1156
1157 npages -= node->npages;
Mitko Haralanove88c9272016-04-12 10:46:53 -07001158
Mitko Haralanov5511d782016-03-08 11:15:44 -08001159retry:
Dean Luickb7df1922016-07-28 15:21:23 -04001160 if (!hfi1_can_pin_pages(pq->dd, pq->mm,
1161 atomic_read(&pq->n_locked), npages)) {
Mitko Haralanov5511d782016-03-08 11:15:44 -08001162 cleared = sdma_cache_evict(pq, npages);
Mitko Haralanov5511d782016-03-08 11:15:44 -08001163 if (cleared >= npages)
1164 goto retry;
1165 }
Ira Weiny3faa3d92016-07-28 15:21:19 -04001166 pinned = hfi1_acquire_user_pages(pq->mm,
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001167 ((unsigned long)iovec->iov.iov_base +
1168 (node->npages * PAGE_SIZE)), npages, 0,
1169 pages + node->npages);
1170 if (pinned < 0) {
1171 kfree(pages);
1172 ret = pinned;
1173 goto bail;
1174 }
1175 if (pinned != npages) {
Ira Weiny3faa3d92016-07-28 15:21:19 -04001176 unpin_vector_pages(pq->mm, pages, node->npages,
Mitko Haralanov849e3e92016-04-12 10:46:16 -07001177 pinned);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001178 ret = -EFAULT;
1179 goto bail;
1180 }
1181 kfree(node->pages);
Mitko Haralanovde790932016-04-12 10:46:41 -07001182 node->rb.len = iovec->iov.iov_len;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001183 node->pages = pages;
1184 node->npages += pinned;
1185 npages = node->npages;
Dean Luickb7df1922016-07-28 15:21:23 -04001186 atomic_add(pinned, &pq->n_locked);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001187 }
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001188 iovec->pages = node->pages;
1189 iovec->npages = npages;
Mitko Haralanov9565c6a2016-05-19 05:21:18 -07001190 iovec->node = node;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001191
Dean Luicke0b09ac2016-07-28 15:21:20 -04001192 ret = hfi1_mmu_rb_insert(req->pq->handler, &node->rb);
Mitko Haralanovf53af852016-04-12 10:46:47 -07001193 if (ret) {
Dean Luickb7df1922016-07-28 15:21:23 -04001194 atomic_sub(node->npages, &pq->n_locked);
Dean Luicka383f8e2016-07-28 15:21:16 -04001195 iovec->node = NULL;
Mitko Haralanovf53af852016-04-12 10:46:47 -07001196 goto bail;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001197 }
1198 return 0;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001199bail:
Mitko Haralanovf53af852016-04-12 10:46:47 -07001200 if (rb_node)
Ira Weiny3faa3d92016-07-28 15:21:19 -04001201 unpin_vector_pages(pq->mm, node->pages, 0, node->npages);
Mitko Haralanovf53af852016-04-12 10:46:47 -07001202 kfree(node);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001203 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001204}
1205
Mitko Haralanovbd3a8942016-03-08 11:15:33 -08001206static void unpin_vector_pages(struct mm_struct *mm, struct page **pages,
Mitko Haralanov849e3e92016-04-12 10:46:16 -07001207 unsigned start, unsigned npages)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001208{
Ira Weiny639297b2016-07-28 12:27:33 -04001209 hfi1_release_user_pages(mm, pages + start, npages, false);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001210 kfree(pages);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001211}
1212
1213static int check_header_template(struct user_sdma_request *req,
1214 struct hfi1_pkt_header *hdr, u32 lrhlen,
1215 u32 datalen)
1216{
1217 /*
1218 * Perform safety checks for any type of packet:
1219 * - transfer size is multiple of 64bytes
Ira Weinyc4929802016-07-27 21:08:42 -04001220 * - packet length is multiple of 4 bytes
Mike Marciniszyn77241052015-07-30 15:17:43 -04001221 * - packet length is not larger than MTU size
1222 *
1223 * These checks are only done for the first packet of the
1224 * transfer since the header is "given" to us by user space.
1225 * For the remainder of the packets we compute the values.
1226 */
Ira Weinyc4929802016-07-27 21:08:42 -04001227 if (req->info.fragsize % PIO_BLOCK_SIZE || lrhlen & 0x3 ||
Mike Marciniszyn77241052015-07-30 15:17:43 -04001228 lrhlen > get_lrh_len(*hdr, req->info.fragsize))
1229 return -EINVAL;
1230
1231 if (req_opcode(req->info.ctrl) == EXPECTED) {
1232 /*
1233 * The header is checked only on the first packet. Furthermore,
1234 * we ensure that at least one TID entry is copied when the
1235 * request is submitted. Therefore, we don't have to verify that
1236 * tididx points to something sane.
1237 */
1238 u32 tidval = req->tids[req->tididx],
1239 tidlen = EXP_TID_GET(tidval, LEN) * PAGE_SIZE,
1240 tididx = EXP_TID_GET(tidval, IDX),
1241 tidctrl = EXP_TID_GET(tidval, CTRL),
1242 tidoff;
1243 __le32 kval = hdr->kdeth.ver_tid_offset;
1244
1245 tidoff = KDETH_GET(kval, OFFSET) *
1246 (KDETH_GET(req->hdr.kdeth.ver_tid_offset, OM) ?
1247 KDETH_OM_LARGE : KDETH_OM_SMALL);
1248 /*
1249 * Expected receive packets have the following
1250 * additional checks:
1251 * - offset is not larger than the TID size
1252 * - TIDCtrl values match between header and TID array
1253 * - TID indexes match between header and TID array
1254 */
1255 if ((tidoff + datalen > tidlen) ||
1256 KDETH_GET(kval, TIDCTRL) != tidctrl ||
1257 KDETH_GET(kval, TID) != tididx)
1258 return -EINVAL;
1259 }
1260 return 0;
1261}
1262
1263/*
1264 * Correctly set the BTH.PSN field based on type of
1265 * transfer - eager packets can just increment the PSN but
1266 * expected packets encode generation and sequence in the
1267 * BTH.PSN field so just incrementing will result in errors.
1268 */
1269static inline u32 set_pkt_bth_psn(__be32 bthpsn, u8 expct, u32 frags)
1270{
1271 u32 val = be32_to_cpu(bthpsn),
1272 mask = (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffffull :
1273 0xffffffull),
1274 psn = val & mask;
1275 if (expct)
1276 psn = (psn & ~BTH_SEQ_MASK) | ((psn + frags) & BTH_SEQ_MASK);
1277 else
1278 psn = psn + frags;
1279 return psn & mask;
1280}
1281
1282static int set_txreq_header(struct user_sdma_request *req,
1283 struct user_sdma_txreq *tx, u32 datalen)
1284{
1285 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1286 struct hfi1_pkt_header *hdr = &tx->hdr;
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001287 u8 omfactor; /* KDETH.OM */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001288 u16 pbclen;
1289 int ret;
Ira Weinyc4929802016-07-27 21:08:42 -04001290 u32 tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(datalen));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001291
1292 /* Copy the header template to the request before modification */
1293 memcpy(hdr, &req->hdr, sizeof(*hdr));
1294
1295 /*
1296 * Check if the PBC and LRH length are mismatched. If so
1297 * adjust both in the header.
1298 */
1299 pbclen = le16_to_cpu(hdr->pbc[0]);
1300 if (PBC2LRH(pbclen) != lrhlen) {
1301 pbclen = (pbclen & 0xf000) | LRH2PBC(lrhlen);
1302 hdr->pbc[0] = cpu_to_le16(pbclen);
1303 hdr->lrh[2] = cpu_to_be16(lrhlen >> 2);
1304 /*
1305 * Third packet
1306 * This is the first packet in the sequence that has
1307 * a "static" size that can be used for the rest of
1308 * the packets (besides the last one).
1309 */
1310 if (unlikely(req->seqnum == 2)) {
1311 /*
1312 * From this point on the lengths in both the
1313 * PBC and LRH are the same until the last
1314 * packet.
1315 * Adjust the template so we don't have to update
1316 * every packet
1317 */
1318 req->hdr.pbc[0] = hdr->pbc[0];
1319 req->hdr.lrh[2] = hdr->lrh[2];
1320 }
1321 }
1322 /*
1323 * We only have to modify the header if this is not the
1324 * first packet in the request. Otherwise, we use the
1325 * header given to us.
1326 */
1327 if (unlikely(!req->seqnum)) {
1328 ret = check_header_template(req, hdr, lrhlen, datalen);
1329 if (ret)
1330 return ret;
1331 goto done;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001332 }
1333
1334 hdr->bth[2] = cpu_to_be32(
1335 set_pkt_bth_psn(hdr->bth[2],
1336 (req_opcode(req->info.ctrl) == EXPECTED),
1337 req->seqnum));
1338
1339 /* Set ACK request on last packet */
Jakub Pawlake7301392016-12-07 19:32:41 -08001340 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_ACK))
Jubin John8638b772016-02-14 20:19:24 -08001341 hdr->bth[2] |= cpu_to_be32(1UL << 31);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001342
1343 /* Set the new offset */
1344 hdr->kdeth.swdata[6] = cpu_to_le32(req->koffset);
1345 /* Expected packets have to fill in the new TID information */
1346 if (req_opcode(req->info.ctrl) == EXPECTED) {
1347 tidval = req->tids[req->tididx];
1348 /*
1349 * If the offset puts us at the end of the current TID,
1350 * advance everything.
1351 */
1352 if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
1353 PAGE_SIZE)) {
1354 req->tidoffset = 0;
Jubin John4d114fd2016-02-14 20:21:43 -08001355 /*
1356 * Since we don't copy all the TIDs, all at once,
1357 * we have to check again.
1358 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001359 if (++req->tididx > req->n_tids - 1 ||
1360 !req->tids[req->tididx]) {
1361 return -EINVAL;
1362 }
1363 tidval = req->tids[req->tididx];
1364 }
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001365 omfactor = EXP_TID_GET(tidval, LEN) * PAGE_SIZE >=
1366 KDETH_OM_MAX_SIZE ? KDETH_OM_LARGE_SHIFT :
1367 KDETH_OM_SMALL_SHIFT;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001368 /* Set KDETH.TIDCtrl based on value for this TID. */
1369 KDETH_SET(hdr->kdeth.ver_tid_offset, TIDCTRL,
1370 EXP_TID_GET(tidval, CTRL));
1371 /* Set KDETH.TID based on value for this TID */
1372 KDETH_SET(hdr->kdeth.ver_tid_offset, TID,
1373 EXP_TID_GET(tidval, IDX));
Jakub Pawlake7301392016-12-07 19:32:41 -08001374 /* Clear KDETH.SH when DISABLE_SH flag is set */
1375 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_DISABLE_SH))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001376 KDETH_SET(hdr->kdeth.ver_tid_offset, SH, 0);
1377 /*
1378 * Set the KDETH.OFFSET and KDETH.OM based on size of
1379 * transfer.
1380 */
1381 SDMA_DBG(req, "TID offset %ubytes %uunits om%u",
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001382 req->tidoffset, req->tidoffset >> omfactor,
1383 omfactor != KDETH_OM_SMALL_SHIFT);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001384 KDETH_SET(hdr->kdeth.ver_tid_offset, OFFSET,
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001385 req->tidoffset >> omfactor);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001386 KDETH_SET(hdr->kdeth.ver_tid_offset, OM,
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001387 omfactor != KDETH_OM_SMALL_SHIFT);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001388 }
1389done:
1390 trace_hfi1_sdma_user_header(pq->dd, pq->ctxt, pq->subctxt,
1391 req->info.comp_idx, hdr, tidval);
1392 return sdma_txadd_kvaddr(pq->dd, &tx->txreq, hdr, sizeof(*hdr));
1393}
1394
1395static int set_txreq_header_ahg(struct user_sdma_request *req,
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001396 struct user_sdma_txreq *tx, u32 datalen)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001397{
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001398 u32 ahg[AHG_KDETH_ARRAY_SIZE];
Mike Marciniszyn77241052015-07-30 15:17:43 -04001399 int diff = 0;
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001400 u8 omfactor; /* KDETH.OM */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001401 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1402 struct hfi1_pkt_header *hdr = &req->hdr;
1403 u16 pbclen = le16_to_cpu(hdr->pbc[0]);
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001404 u32 val32, tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(datalen));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001405
1406 if (PBC2LRH(pbclen) != lrhlen) {
1407 /* PBC.PbcLengthDWs */
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001408 AHG_HEADER_SET(ahg, diff, 0, 0, 12,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001409 cpu_to_le16(LRH2PBC(lrhlen)));
1410 /* LRH.PktLen (we need the full 16 bits due to byte swap) */
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001411 AHG_HEADER_SET(ahg, diff, 3, 0, 16,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001412 cpu_to_be16(lrhlen >> 2));
1413 }
1414
1415 /*
1416 * Do the common updates
1417 */
1418 /* BTH.PSN and BTH.A */
1419 val32 = (be32_to_cpu(hdr->bth[2]) + req->seqnum) &
1420 (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffff : 0xffffff);
Jakub Pawlake7301392016-12-07 19:32:41 -08001421 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_ACK))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001422 val32 |= 1UL << 31;
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001423 AHG_HEADER_SET(ahg, diff, 6, 0, 16, cpu_to_be16(val32 >> 16));
1424 AHG_HEADER_SET(ahg, diff, 6, 16, 16, cpu_to_be16(val32 & 0xffff));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001425 /* KDETH.Offset */
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001426 AHG_HEADER_SET(ahg, diff, 15, 0, 16,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001427 cpu_to_le16(req->koffset & 0xffff));
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001428 AHG_HEADER_SET(ahg, diff, 15, 16, 16, cpu_to_le16(req->koffset >> 16));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001429 if (req_opcode(req->info.ctrl) == EXPECTED) {
1430 __le16 val;
1431
1432 tidval = req->tids[req->tididx];
1433
1434 /*
1435 * If the offset puts us at the end of the current TID,
1436 * advance everything.
1437 */
1438 if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
1439 PAGE_SIZE)) {
1440 req->tidoffset = 0;
Jubin John4d114fd2016-02-14 20:21:43 -08001441 /*
1442 * Since we don't copy all the TIDs, all at once,
1443 * we have to check again.
1444 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001445 if (++req->tididx > req->n_tids - 1 ||
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001446 !req->tids[req->tididx])
Mike Marciniszyn77241052015-07-30 15:17:43 -04001447 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001448 tidval = req->tids[req->tididx];
1449 }
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001450 omfactor = ((EXP_TID_GET(tidval, LEN) *
Mike Marciniszyn77241052015-07-30 15:17:43 -04001451 PAGE_SIZE) >=
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001452 KDETH_OM_MAX_SIZE) ? KDETH_OM_LARGE_SHIFT :
1453 KDETH_OM_SMALL_SHIFT;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001454 /* KDETH.OM and KDETH.OFFSET (TID) */
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001455 AHG_HEADER_SET(ahg, diff, 7, 0, 16,
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001456 ((!!(omfactor - KDETH_OM_SMALL_SHIFT)) << 15 |
1457 ((req->tidoffset >> omfactor)
1458 & 0x7fff)));
Jakub Pawlake7301392016-12-07 19:32:41 -08001459 /* KDETH.TIDCtrl, KDETH.TID, KDETH.Intr, KDETH.SH */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001460 val = cpu_to_le16(((EXP_TID_GET(tidval, CTRL) & 0x3) << 10) |
Jakub Pawlake7301392016-12-07 19:32:41 -08001461 (EXP_TID_GET(tidval, IDX) & 0x3ff));
1462
1463 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_DISABLE_SH)) {
1464 val |= cpu_to_le16((KDETH_GET(hdr->kdeth.ver_tid_offset,
1465 INTR) <<
1466 AHG_KDETH_INTR_SHIFT));
Jubin Johne4909742016-02-14 20:22:00 -08001467 } else {
Jakub Pawlake7301392016-12-07 19:32:41 -08001468 val |= KDETH_GET(hdr->kdeth.ver_tid_offset, SH) ?
1469 cpu_to_le16(0x1 << AHG_KDETH_SH_SHIFT) :
1470 cpu_to_le16((KDETH_GET(hdr->kdeth.ver_tid_offset,
1471 INTR) <<
1472 AHG_KDETH_INTR_SHIFT));
Jubin Johne4909742016-02-14 20:22:00 -08001473 }
Jakub Pawlake7301392016-12-07 19:32:41 -08001474
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001475 AHG_HEADER_SET(ahg, diff, 7, 16, 14, val);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001476 }
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001477 if (diff < 0)
1478 return diff;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001479
1480 trace_hfi1_sdma_user_header_ahg(pq->dd, pq->ctxt, pq->subctxt,
1481 req->info.comp_idx, req->sde->this_idx,
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001482 req->ahg_idx, ahg, diff, tidval);
1483 sdma_txinit_ahg(&tx->txreq,
1484 SDMA_TXREQ_F_USE_AHG,
1485 datalen, req->ahg_idx, diff,
1486 ahg, sizeof(req->hdr),
1487 user_sdma_txreq_cb);
1488
Mike Marciniszyn77241052015-07-30 15:17:43 -04001489 return diff;
1490}
1491
Mitko Haralanova0d40692015-12-08 17:10:13 -05001492/*
1493 * SDMA tx request completion callback. Called when the SDMA progress
1494 * state machine gets notification that the SDMA descriptors for this
1495 * tx request have been processed by the DMA engine. Called in
1496 * interrupt context.
1497 */
Mike Marciniszyna545f532016-02-14 12:45:53 -08001498static void user_sdma_txreq_cb(struct sdma_txreq *txreq, int status)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001499{
1500 struct user_sdma_txreq *tx =
1501 container_of(txreq, struct user_sdma_txreq, txreq);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001502 struct user_sdma_request *req;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001503 struct hfi1_user_sdma_pkt_q *pq;
1504 struct hfi1_user_sdma_comp_q *cq;
1505 u16 idx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001506
Mitko Haralanova0d40692015-12-08 17:10:13 -05001507 if (!tx->req)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001508 return;
1509
Mitko Haralanova0d40692015-12-08 17:10:13 -05001510 req = tx->req;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001511 pq = req->pq;
1512 cq = req->cq;
Mitko Haralanovb9fb63182015-10-26 10:28:37 -04001513
Mike Marciniszyn77241052015-07-30 15:17:43 -04001514 if (status != SDMA_TXREQ_S_OK) {
Mitko Haralanova0d40692015-12-08 17:10:13 -05001515 SDMA_DBG(req, "SDMA completion with error %d",
1516 status);
Sebastian Sancheze9c48eb2017-05-26 05:35:50 -07001517 WRITE_ONCE(req->has_error, 1);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001518 }
1519
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001520 req->seqcomp = tx->seqnum;
1521 kmem_cache_free(pq->txreq_cache, tx);
1522 tx = NULL;
1523
1524 idx = req->info.comp_idx;
1525 if (req->status == -1 && status == SDMA_TXREQ_S_OK) {
1526 if (req->seqcomp == req->info.npkts - 1) {
1527 req->status = 0;
1528 user_sdma_free_request(req, false);
1529 pq_update(pq);
1530 set_comp_state(pq, cq, idx, COMPLETE, 0);
1531 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001532 } else {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001533 if (status != SDMA_TXREQ_S_OK)
1534 req->status = status;
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001535 if (req->seqcomp == (ACCESS_ONCE(req->seqsubmitted) - 1) &&
Sebastian Sanchezb8884292017-05-26 05:35:44 -07001536 (READ_ONCE(req->done) ||
Sebastian Sancheze9c48eb2017-05-26 05:35:50 -07001537 READ_ONCE(req->has_error))) {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001538 user_sdma_free_request(req, false);
1539 pq_update(pq);
1540 set_comp_state(pq, cq, idx, ERROR, req->status);
1541 }
Mitko Haralanova0d40692015-12-08 17:10:13 -05001542 }
1543}
1544
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001545static inline void pq_update(struct hfi1_user_sdma_pkt_q *pq)
Mitko Haralanova0d40692015-12-08 17:10:13 -05001546{
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001547 if (atomic_dec_and_test(&pq->n_reqs)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001548 xchg(&pq->state, SDMA_PKT_Q_INACTIVE);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001549 wake_up(&pq->wait);
1550 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001551}
1552
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001553static void user_sdma_free_request(struct user_sdma_request *req, bool unpin)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001554{
1555 if (!list_empty(&req->txps)) {
1556 struct sdma_txreq *t, *p;
1557
1558 list_for_each_entry_safe(t, p, &req->txps, list) {
1559 struct user_sdma_txreq *tx =
1560 container_of(t, struct user_sdma_txreq, txreq);
1561 list_del_init(&t->list);
1562 sdma_txclean(req->pq->dd, t);
1563 kmem_cache_free(req->pq->txreq_cache, tx);
1564 }
1565 }
1566 if (req->data_iovs) {
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001567 struct sdma_mmu_node *node;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001568 int i;
1569
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001570 for (i = 0; i < req->data_iovs; i++) {
Mitko Haralanov9565c6a2016-05-19 05:21:18 -07001571 node = req->iovs[i].node;
1572 if (!node)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001573 continue;
1574
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001575 if (unpin)
Dean Luicke0b09ac2016-07-28 15:21:20 -04001576 hfi1_mmu_rb_remove(req->pq->handler,
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001577 &node->rb);
1578 else
1579 atomic_dec(&node->refcount);
1580 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001581 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001582 kfree(req->tids);
Dean Luick7b3256e2016-07-28 15:21:18 -04001583 clear_bit(req->info.comp_idx, req->pq->req_in_use);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001584}
1585
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001586static inline void set_comp_state(struct hfi1_user_sdma_pkt_q *pq,
1587 struct hfi1_user_sdma_comp_q *cq,
1588 u16 idx, enum hfi1_sdma_comp_state state,
1589 int ret)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001590{
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001591 hfi1_cdbg(SDMA, "[%u:%u:%u:%u] Setting completion status %u %d",
1592 pq->dd->unit, pq->ctxt, pq->subctxt, idx, state, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001593 if (state == ERROR)
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001594 cq->comps[idx].errcode = -ret;
Michael J. Ruhl0519c522017-03-20 17:24:45 -07001595 smp_wmb(); /* make sure errcode is visible first */
1596 cq->comps[idx].status = state;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001597 trace_hfi1_sdma_user_completion(pq->dd, pq->ctxt, pq->subctxt,
1598 idx, state, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001599}
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001600
1601static bool sdma_rb_filter(struct mmu_rb_node *node, unsigned long addr,
1602 unsigned long len)
1603{
1604 return (bool)(node->addr == addr);
1605}
1606
Dean Luicke0b09ac2016-07-28 15:21:20 -04001607static int sdma_rb_insert(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001608{
1609 struct sdma_mmu_node *node =
1610 container_of(mnode, struct sdma_mmu_node, rb);
1611
1612 atomic_inc(&node->refcount);
1613 return 0;
1614}
1615
Dean Luickb7df1922016-07-28 15:21:23 -04001616/*
1617 * Return 1 to remove the node from the rb tree and call the remove op.
1618 *
1619 * Called with the rb tree lock held.
1620 */
1621static int sdma_rb_evict(void *arg, struct mmu_rb_node *mnode,
1622 void *evict_arg, bool *stop)
1623{
1624 struct sdma_mmu_node *node =
1625 container_of(mnode, struct sdma_mmu_node, rb);
1626 struct evict_data *evict_data = evict_arg;
1627
1628 /* is this node still being used? */
1629 if (atomic_read(&node->refcount))
1630 return 0; /* keep this node */
1631
1632 /* this node will be evicted, add its pages to our count */
1633 evict_data->cleared += node->npages;
1634
1635 /* have enough pages been cleared? */
1636 if (evict_data->cleared >= evict_data->target)
1637 *stop = true;
1638
1639 return 1; /* remove this node */
1640}
1641
Dean Luick082b3532016-07-28 15:21:25 -04001642static void sdma_rb_remove(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001643{
1644 struct sdma_mmu_node *node =
1645 container_of(mnode, struct sdma_mmu_node, rb);
1646
Dean Luickb7df1922016-07-28 15:21:23 -04001647 atomic_sub(node->npages, &node->pq->n_locked);
Mitko Haralanov5511d782016-03-08 11:15:44 -08001648
Dean Luickb85ced92016-07-28 15:21:24 -04001649 unpin_vector_pages(node->pq->mm, node->pages, 0, node->npages);
1650
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001651 kfree(node);
1652}
1653
Dean Luicke0b09ac2016-07-28 15:21:20 -04001654static int sdma_rb_invalidate(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001655{
1656 struct sdma_mmu_node *node =
1657 container_of(mnode, struct sdma_mmu_node, rb);
1658
1659 if (!atomic_read(&node->refcount))
1660 return 1;
1661 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001662}