blob: a3a9925f408aa57cbe5af33c12e0a0a9b796b370 [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
Harish Chegondi624b9ac2017-08-21 18:26:57 -0700850static int user_sdma_txadd_ahg(struct user_sdma_request *req,
851 struct user_sdma_txreq *tx,
852 u32 datalen)
853{
854 int ret;
855 u16 pbclen = le16_to_cpu(req->hdr.pbc[0]);
856 u32 lrhlen = get_lrh_len(req->hdr, pad_len(datalen));
857 struct hfi1_user_sdma_pkt_q *pq = req->pq;
858
859 /*
860 * Copy the request header into the tx header
861 * because the HW needs a cacheline-aligned
862 * address.
863 * This copy can be optimized out if the hdr
864 * member of user_sdma_request were also
865 * cacheline aligned.
866 */
867 memcpy(&tx->hdr, &req->hdr, sizeof(tx->hdr));
868 if (PBC2LRH(pbclen) != lrhlen) {
869 pbclen = (pbclen & 0xf000) | LRH2PBC(lrhlen);
870 tx->hdr.pbc[0] = cpu_to_le16(pbclen);
871 }
872 ret = check_header_template(req, &tx->hdr, lrhlen, datalen);
873 if (ret)
874 return ret;
875 ret = sdma_txinit_ahg(&tx->txreq, SDMA_TXREQ_F_AHG_COPY,
876 sizeof(tx->hdr) + datalen, req->ahg_idx,
877 0, NULL, 0, user_sdma_txreq_cb);
878 if (ret)
879 return ret;
880 ret = sdma_txadd_kvaddr(pq->dd, &tx->txreq, &tx->hdr, sizeof(tx->hdr));
881 if (ret)
882 sdma_txclean(pq->dd, &tx->txreq);
883 return ret;
884}
885
886static int user_sdma_txadd(struct user_sdma_request *req,
887 struct user_sdma_txreq *tx,
888 struct user_sdma_iovec *iovec, u32 datalen,
889 u32 *queued_ptr, u32 *data_sent_ptr,
890 u64 *iov_offset_ptr)
891{
892 int ret;
893 unsigned int pageidx, len;
894 unsigned long base, offset;
895 u64 iov_offset = *iov_offset_ptr;
896 u32 queued = *queued_ptr, data_sent = *data_sent_ptr;
897 struct hfi1_user_sdma_pkt_q *pq = req->pq;
898
899 base = (unsigned long)iovec->iov.iov_base;
900 offset = offset_in_page(base + iovec->offset + iov_offset);
901 pageidx = (((iovec->offset + iov_offset + base) - (base & PAGE_MASK)) >>
902 PAGE_SHIFT);
903 len = offset + req->info.fragsize > PAGE_SIZE ?
904 PAGE_SIZE - offset : req->info.fragsize;
905 len = min((datalen - queued), len);
906 ret = sdma_txadd_page(pq->dd, &tx->txreq, iovec->pages[pageidx],
907 offset, len);
908 if (ret) {
909 SDMA_DBG(req, "SDMA txreq add page failed %d\n", ret);
910 return ret;
911 }
912 iov_offset += len;
913 queued += len;
914 data_sent += len;
915 if (unlikely(queued < datalen && pageidx == iovec->npages &&
916 req->iov_idx < req->data_iovs - 1)) {
917 iovec->offset += iov_offset;
918 iovec = &req->iovs[++req->iov_idx];
919 iov_offset = 0;
920 }
921
922 *queued_ptr = queued;
923 *data_sent_ptr = data_sent;
924 *iov_offset_ptr = iov_offset;
925 return ret;
926}
927
Mike Marciniszyn77241052015-07-30 15:17:43 -0400928static int user_sdma_send_pkts(struct user_sdma_request *req, unsigned maxpkts)
929{
Harish Chegondi0b115ef2016-09-06 04:35:37 -0700930 int ret = 0, count;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400931 unsigned npkts = 0;
932 struct user_sdma_txreq *tx = NULL;
933 struct hfi1_user_sdma_pkt_q *pq = NULL;
934 struct user_sdma_iovec *iovec = NULL;
935
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500936 if (!req->pq)
937 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400938
939 pq = req->pq;
940
Mitko Haralanov6a5464f2015-12-08 17:10:12 -0500941 /* If tx completion has reported an error, we are done. */
Sebastian Sancheze9c48eb2017-05-26 05:35:50 -0700942 if (READ_ONCE(req->has_error))
Mitko Haralanov6a5464f2015-12-08 17:10:12 -0500943 return -EFAULT;
Mitko Haralanov6a5464f2015-12-08 17:10:12 -0500944
Mike Marciniszyn77241052015-07-30 15:17:43 -0400945 /*
946 * Check if we might have sent the entire request already
947 */
948 if (unlikely(req->seqnum == req->info.npkts)) {
949 if (!list_empty(&req->txps))
950 goto dosend;
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500951 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400952 }
953
954 if (!maxpkts || maxpkts > req->info.npkts - req->seqnum)
955 maxpkts = req->info.npkts - req->seqnum;
956
957 while (npkts < maxpkts) {
958 u32 datalen = 0, queued = 0, data_sent = 0;
959 u64 iov_offset = 0;
960
961 /*
962 * Check whether any of the completions have come back
963 * with errors. If so, we are not going to process any
964 * more packets from this request.
965 */
Sebastian Sancheze9c48eb2017-05-26 05:35:50 -0700966 if (READ_ONCE(req->has_error))
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500967 return -EFAULT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400968
969 tx = kmem_cache_alloc(pq->txreq_cache, GFP_KERNEL);
Mitko Haralanovfaa98b82015-12-08 17:10:11 -0500970 if (!tx)
971 return -ENOMEM;
972
Mike Marciniszyn77241052015-07-30 15:17:43 -0400973 tx->flags = 0;
974 tx->req = req;
975 tx->busycount = 0;
Mitko Haralanova0d40692015-12-08 17:10:13 -0500976 INIT_LIST_HEAD(&tx->list);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400977
Jakub Pawlake7301392016-12-07 19:32:41 -0800978 /*
979 * For the last packet set the ACK request
980 * and disable header suppression.
981 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400982 if (req->seqnum == req->info.npkts - 1)
Jakub Pawlake7301392016-12-07 19:32:41 -0800983 tx->flags |= (TXREQ_FLAGS_REQ_ACK |
984 TXREQ_FLAGS_REQ_DISABLE_SH);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400985
986 /*
987 * Calculate the payload size - this is min of the fragment
988 * (MTU) size or the remaining bytes in the request but only
989 * if we have payload data.
990 */
991 if (req->data_len) {
992 iovec = &req->iovs[req->iov_idx];
993 if (ACCESS_ONCE(iovec->offset) == iovec->iov.iov_len) {
994 if (++req->iov_idx == req->data_iovs) {
995 ret = -EFAULT;
996 goto free_txreq;
997 }
998 iovec = &req->iovs[req->iov_idx];
999 WARN_ON(iovec->offset);
1000 }
1001
Mike Marciniszyn77241052015-07-30 15:17:43 -04001002 datalen = compute_data_length(req, tx);
Jakub Pawlake7301392016-12-07 19:32:41 -08001003
1004 /*
1005 * Disable header suppression for the payload <= 8DWS.
1006 * If there is an uncorrectable error in the receive
1007 * data FIFO when the received payload size is less than
1008 * or equal to 8DWS then the RxDmaDataFifoRdUncErr is
1009 * not reported.There is set RHF.EccErr if the header
1010 * is not suppressed.
1011 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001012 if (!datalen) {
1013 SDMA_DBG(req,
1014 "Request has data but pkt len is 0");
1015 ret = -EFAULT;
1016 goto free_tx;
Jakub Pawlake7301392016-12-07 19:32:41 -08001017 } else if (datalen <= 32) {
1018 tx->flags |= TXREQ_FLAGS_REQ_DISABLE_SH;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001019 }
1020 }
1021
Sebastian Sanchez780a4c12017-05-04 05:14:51 -07001022 if (req->ahg_idx >= 0) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001023 if (!req->seqnum) {
Harish Chegondi624b9ac2017-08-21 18:26:57 -07001024 ret = user_sdma_txadd_ahg(req, tx, datalen);
Jakub Pawlake7301392016-12-07 19:32:41 -08001025 if (ret)
1026 goto free_tx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001027 } else {
1028 int changes;
1029
1030 changes = set_txreq_header_ahg(req, tx,
1031 datalen);
1032 if (changes < 0)
1033 goto free_tx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001034 }
1035 } else {
1036 ret = sdma_txinit(&tx->txreq, 0, sizeof(req->hdr) +
1037 datalen, user_sdma_txreq_cb);
1038 if (ret)
1039 goto free_tx;
1040 /*
1041 * Modify the header for this packet. This only needs
1042 * to be done if we are not going to use AHG. Otherwise,
1043 * the HW will do it based on the changes we gave it
1044 * during sdma_txinit_ahg().
1045 */
1046 ret = set_txreq_header(req, tx, datalen);
1047 if (ret)
1048 goto free_txreq;
1049 }
1050
1051 /*
1052 * If the request contains any data vectors, add up to
1053 * fragsize bytes to the descriptor.
1054 */
1055 while (queued < datalen &&
1056 (req->sent + data_sent) < req->data_len) {
Harish Chegondi624b9ac2017-08-21 18:26:57 -07001057 ret = user_sdma_txadd(req, tx, iovec, datalen,
1058 &queued, &data_sent, &iov_offset);
1059 if (ret)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001060 goto free_txreq;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001061 }
1062 /*
1063 * The txreq was submitted successfully so we can update
1064 * the counters.
1065 */
1066 req->koffset += datalen;
1067 if (req_opcode(req->info.ctrl) == EXPECTED)
1068 req->tidoffset += datalen;
1069 req->sent += data_sent;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001070 if (req->data_len)
1071 iovec->offset += iov_offset;
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001072 list_add_tail(&tx->txreq.list, &req->txps);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001073 /*
1074 * It is important to increment this here as it is used to
1075 * generate the BTH.PSN and, therefore, can't be bulk-updated
1076 * outside of the loop.
1077 */
1078 tx->seqnum = req->seqnum++;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001079 npkts++;
1080 }
1081dosend:
Harish Chegondi0b115ef2016-09-06 04:35:37 -07001082 ret = sdma_send_txlist(req->sde, &pq->busy, &req->txps, &count);
1083 req->seqsubmitted += count;
1084 if (req->seqsubmitted == req->info.npkts) {
Sebastian Sanchezb8884292017-05-26 05:35:44 -07001085 WRITE_ONCE(req->done, 1);
Harish Chegondi0b115ef2016-09-06 04:35:37 -07001086 /*
1087 * The txreq has already been submitted to the HW queue
1088 * so we can free the AHG entry now. Corruption will not
1089 * happen due to the sequential manner in which
1090 * descriptors are processed.
1091 */
Sebastian Sanchez780a4c12017-05-04 05:14:51 -07001092 if (req->ahg_idx >= 0)
Harish Chegondi0b115ef2016-09-06 04:35:37 -07001093 sdma_ahg_free(req->sde, req->ahg_idx);
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001094 }
Mitko Haralanovfaa98b82015-12-08 17:10:11 -05001095 return ret;
1096
Mike Marciniszyn77241052015-07-30 15:17:43 -04001097free_txreq:
1098 sdma_txclean(pq->dd, &tx->txreq);
1099free_tx:
1100 kmem_cache_free(pq->txreq_cache, tx);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001101 return ret;
1102}
1103
1104/*
1105 * How many pages in this iovec element?
1106 */
1107static inline int num_user_pages(const struct iovec *iov)
1108{
Jubin John50e5dcb2016-02-14 20:19:41 -08001109 const unsigned long addr = (unsigned long)iov->iov_base;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001110 const unsigned long len = iov->iov_len;
1111 const unsigned long spage = addr & PAGE_MASK;
1112 const unsigned long epage = (addr + len - 1) & PAGE_MASK;
1113
1114 return 1 + ((epage - spage) >> PAGE_SHIFT);
1115}
1116
Mitko Haralanov5511d782016-03-08 11:15:44 -08001117static u32 sdma_cache_evict(struct hfi1_user_sdma_pkt_q *pq, u32 npages)
1118{
Dean Luickb7df1922016-07-28 15:21:23 -04001119 struct evict_data evict_data;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001120
Dean Luickb7df1922016-07-28 15:21:23 -04001121 evict_data.cleared = 0;
1122 evict_data.target = npages;
1123 hfi1_mmu_rb_evict(pq->handler, &evict_data);
1124 return evict_data.cleared;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001125}
1126
Harish Chegondi4c6c9aa2017-08-21 18:27:03 -07001127static int pin_sdma_pages(struct user_sdma_request *req,
1128 struct user_sdma_iovec *iovec,
1129 struct sdma_mmu_node *node,
1130 int npages)
1131{
1132 int pinned, cleared;
1133 struct page **pages;
1134 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1135
1136 pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL);
1137 if (!pages) {
1138 SDMA_DBG(req, "Failed page array alloc");
1139 return -ENOMEM;
1140 }
1141 memcpy(pages, node->pages, node->npages * sizeof(*pages));
1142
1143 npages -= node->npages;
1144retry:
1145 if (!hfi1_can_pin_pages(pq->dd, pq->mm,
1146 atomic_read(&pq->n_locked), npages)) {
1147 cleared = sdma_cache_evict(pq, npages);
1148 if (cleared >= npages)
1149 goto retry;
1150 }
1151 pinned = hfi1_acquire_user_pages(pq->mm,
1152 ((unsigned long)iovec->iov.iov_base +
1153 (node->npages * PAGE_SIZE)), npages, 0,
1154 pages + node->npages);
1155 if (pinned < 0) {
1156 kfree(pages);
1157 return pinned;
1158 }
1159 if (pinned != npages) {
1160 unpin_vector_pages(pq->mm, pages, node->npages, pinned);
1161 return -EFAULT;
1162 }
1163 kfree(node->pages);
1164 node->rb.len = iovec->iov.iov_len;
1165 node->pages = pages;
1166 atomic_add(pinned, &pq->n_locked);
1167 return pinned;
1168}
1169
Harish Chegondi04a646d2017-08-21 18:27:09 -07001170static void unpin_sdma_pages(struct sdma_mmu_node *node)
1171{
1172 if (node->npages) {
1173 unpin_vector_pages(node->pq->mm, node->pages, 0, node->npages);
1174 atomic_sub(node->npages, &node->pq->n_locked);
1175 }
1176}
1177
Mike Marciniszyn77241052015-07-30 15:17:43 -04001178static int pin_vector_pages(struct user_sdma_request *req,
Ira Weiny72720dd2016-07-28 12:27:25 -04001179 struct user_sdma_iovec *iovec)
1180{
Harish Chegondi4c6c9aa2017-08-21 18:27:03 -07001181 int ret = 0, pinned, npages;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001182 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1183 struct sdma_mmu_node *node = NULL;
1184 struct mmu_rb_node *rb_node;
Sebastian Sanchez7be85672017-05-26 05:35:12 -07001185 bool extracted;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001186
Sebastian Sanchez7be85672017-05-26 05:35:12 -07001187 extracted =
1188 hfi1_mmu_rb_remove_unless_exact(pq->handler,
1189 (unsigned long)
1190 iovec->iov.iov_base,
1191 iovec->iov.iov_len, &rb_node);
1192 if (rb_node) {
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001193 node = container_of(rb_node, struct sdma_mmu_node, rb);
Sebastian Sanchez7be85672017-05-26 05:35:12 -07001194 if (!extracted) {
1195 atomic_inc(&node->refcount);
1196 iovec->pages = node->pages;
1197 iovec->npages = node->npages;
1198 iovec->node = node;
1199 return 0;
1200 }
1201 }
Mitko Haralanova0d40692015-12-08 17:10:13 -05001202
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001203 if (!node) {
1204 node = kzalloc(sizeof(*node), GFP_KERNEL);
1205 if (!node)
1206 return -ENOMEM;
1207
1208 node->rb.addr = (unsigned long)iovec->iov.iov_base;
Mitko Haralanov5511d782016-03-08 11:15:44 -08001209 node->pq = pq;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001210 atomic_set(&node->refcount, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001211 }
Mitko Haralanova0d40692015-12-08 17:10:13 -05001212
Mike Marciniszyn77241052015-07-30 15:17:43 -04001213 npages = num_user_pages(&iovec->iov);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001214 if (node->npages < npages) {
Harish Chegondi4c6c9aa2017-08-21 18:27:03 -07001215 pinned = pin_sdma_pages(req, iovec, node, npages);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001216 if (pinned < 0) {
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001217 ret = pinned;
1218 goto bail;
1219 }
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001220 node->npages += pinned;
1221 npages = node->npages;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001222 }
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001223 iovec->pages = node->pages;
1224 iovec->npages = npages;
Mitko Haralanov9565c6a2016-05-19 05:21:18 -07001225 iovec->node = node;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001226
Dean Luicke0b09ac2016-07-28 15:21:20 -04001227 ret = hfi1_mmu_rb_insert(req->pq->handler, &node->rb);
Mitko Haralanovf53af852016-04-12 10:46:47 -07001228 if (ret) {
Dean Luicka383f8e2016-07-28 15:21:16 -04001229 iovec->node = NULL;
Mitko Haralanovf53af852016-04-12 10:46:47 -07001230 goto bail;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001231 }
1232 return 0;
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001233bail:
Harish Chegondi04a646d2017-08-21 18:27:09 -07001234 unpin_sdma_pages(node);
Mitko Haralanovf53af852016-04-12 10:46:47 -07001235 kfree(node);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001236 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001237}
1238
Mitko Haralanovbd3a8942016-03-08 11:15:33 -08001239static void unpin_vector_pages(struct mm_struct *mm, struct page **pages,
Mitko Haralanov849e3e92016-04-12 10:46:16 -07001240 unsigned start, unsigned npages)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001241{
Ira Weiny639297b2016-07-28 12:27:33 -04001242 hfi1_release_user_pages(mm, pages + start, npages, false);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001243 kfree(pages);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001244}
1245
1246static int check_header_template(struct user_sdma_request *req,
1247 struct hfi1_pkt_header *hdr, u32 lrhlen,
1248 u32 datalen)
1249{
1250 /*
1251 * Perform safety checks for any type of packet:
1252 * - transfer size is multiple of 64bytes
Ira Weinyc4929802016-07-27 21:08:42 -04001253 * - packet length is multiple of 4 bytes
Mike Marciniszyn77241052015-07-30 15:17:43 -04001254 * - packet length is not larger than MTU size
1255 *
1256 * These checks are only done for the first packet of the
1257 * transfer since the header is "given" to us by user space.
1258 * For the remainder of the packets we compute the values.
1259 */
Ira Weinyc4929802016-07-27 21:08:42 -04001260 if (req->info.fragsize % PIO_BLOCK_SIZE || lrhlen & 0x3 ||
Mike Marciniszyn77241052015-07-30 15:17:43 -04001261 lrhlen > get_lrh_len(*hdr, req->info.fragsize))
1262 return -EINVAL;
1263
1264 if (req_opcode(req->info.ctrl) == EXPECTED) {
1265 /*
1266 * The header is checked only on the first packet. Furthermore,
1267 * we ensure that at least one TID entry is copied when the
1268 * request is submitted. Therefore, we don't have to verify that
1269 * tididx points to something sane.
1270 */
1271 u32 tidval = req->tids[req->tididx],
1272 tidlen = EXP_TID_GET(tidval, LEN) * PAGE_SIZE,
1273 tididx = EXP_TID_GET(tidval, IDX),
1274 tidctrl = EXP_TID_GET(tidval, CTRL),
1275 tidoff;
1276 __le32 kval = hdr->kdeth.ver_tid_offset;
1277
1278 tidoff = KDETH_GET(kval, OFFSET) *
1279 (KDETH_GET(req->hdr.kdeth.ver_tid_offset, OM) ?
1280 KDETH_OM_LARGE : KDETH_OM_SMALL);
1281 /*
1282 * Expected receive packets have the following
1283 * additional checks:
1284 * - offset is not larger than the TID size
1285 * - TIDCtrl values match between header and TID array
1286 * - TID indexes match between header and TID array
1287 */
1288 if ((tidoff + datalen > tidlen) ||
1289 KDETH_GET(kval, TIDCTRL) != tidctrl ||
1290 KDETH_GET(kval, TID) != tididx)
1291 return -EINVAL;
1292 }
1293 return 0;
1294}
1295
1296/*
1297 * Correctly set the BTH.PSN field based on type of
1298 * transfer - eager packets can just increment the PSN but
1299 * expected packets encode generation and sequence in the
1300 * BTH.PSN field so just incrementing will result in errors.
1301 */
1302static inline u32 set_pkt_bth_psn(__be32 bthpsn, u8 expct, u32 frags)
1303{
1304 u32 val = be32_to_cpu(bthpsn),
1305 mask = (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffffull :
1306 0xffffffull),
1307 psn = val & mask;
1308 if (expct)
1309 psn = (psn & ~BTH_SEQ_MASK) | ((psn + frags) & BTH_SEQ_MASK);
1310 else
1311 psn = psn + frags;
1312 return psn & mask;
1313}
1314
1315static int set_txreq_header(struct user_sdma_request *req,
1316 struct user_sdma_txreq *tx, u32 datalen)
1317{
1318 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1319 struct hfi1_pkt_header *hdr = &tx->hdr;
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001320 u8 omfactor; /* KDETH.OM */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001321 u16 pbclen;
1322 int ret;
Ira Weinyc4929802016-07-27 21:08:42 -04001323 u32 tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(datalen));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001324
1325 /* Copy the header template to the request before modification */
1326 memcpy(hdr, &req->hdr, sizeof(*hdr));
1327
1328 /*
1329 * Check if the PBC and LRH length are mismatched. If so
1330 * adjust both in the header.
1331 */
1332 pbclen = le16_to_cpu(hdr->pbc[0]);
1333 if (PBC2LRH(pbclen) != lrhlen) {
1334 pbclen = (pbclen & 0xf000) | LRH2PBC(lrhlen);
1335 hdr->pbc[0] = cpu_to_le16(pbclen);
1336 hdr->lrh[2] = cpu_to_be16(lrhlen >> 2);
1337 /*
1338 * Third packet
1339 * This is the first packet in the sequence that has
1340 * a "static" size that can be used for the rest of
1341 * the packets (besides the last one).
1342 */
1343 if (unlikely(req->seqnum == 2)) {
1344 /*
1345 * From this point on the lengths in both the
1346 * PBC and LRH are the same until the last
1347 * packet.
1348 * Adjust the template so we don't have to update
1349 * every packet
1350 */
1351 req->hdr.pbc[0] = hdr->pbc[0];
1352 req->hdr.lrh[2] = hdr->lrh[2];
1353 }
1354 }
1355 /*
1356 * We only have to modify the header if this is not the
1357 * first packet in the request. Otherwise, we use the
1358 * header given to us.
1359 */
1360 if (unlikely(!req->seqnum)) {
1361 ret = check_header_template(req, hdr, lrhlen, datalen);
1362 if (ret)
1363 return ret;
1364 goto done;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001365 }
1366
1367 hdr->bth[2] = cpu_to_be32(
1368 set_pkt_bth_psn(hdr->bth[2],
1369 (req_opcode(req->info.ctrl) == EXPECTED),
1370 req->seqnum));
1371
1372 /* Set ACK request on last packet */
Jakub Pawlake7301392016-12-07 19:32:41 -08001373 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_ACK))
Jubin John8638b772016-02-14 20:19:24 -08001374 hdr->bth[2] |= cpu_to_be32(1UL << 31);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001375
1376 /* Set the new offset */
1377 hdr->kdeth.swdata[6] = cpu_to_le32(req->koffset);
1378 /* Expected packets have to fill in the new TID information */
1379 if (req_opcode(req->info.ctrl) == EXPECTED) {
1380 tidval = req->tids[req->tididx];
1381 /*
1382 * If the offset puts us at the end of the current TID,
1383 * advance everything.
1384 */
1385 if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
1386 PAGE_SIZE)) {
1387 req->tidoffset = 0;
Jubin John4d114fd2016-02-14 20:21:43 -08001388 /*
1389 * Since we don't copy all the TIDs, all at once,
1390 * we have to check again.
1391 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001392 if (++req->tididx > req->n_tids - 1 ||
1393 !req->tids[req->tididx]) {
1394 return -EINVAL;
1395 }
1396 tidval = req->tids[req->tididx];
1397 }
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001398 omfactor = EXP_TID_GET(tidval, LEN) * PAGE_SIZE >=
1399 KDETH_OM_MAX_SIZE ? KDETH_OM_LARGE_SHIFT :
1400 KDETH_OM_SMALL_SHIFT;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001401 /* Set KDETH.TIDCtrl based on value for this TID. */
1402 KDETH_SET(hdr->kdeth.ver_tid_offset, TIDCTRL,
1403 EXP_TID_GET(tidval, CTRL));
1404 /* Set KDETH.TID based on value for this TID */
1405 KDETH_SET(hdr->kdeth.ver_tid_offset, TID,
1406 EXP_TID_GET(tidval, IDX));
Jakub Pawlake7301392016-12-07 19:32:41 -08001407 /* Clear KDETH.SH when DISABLE_SH flag is set */
1408 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_DISABLE_SH))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001409 KDETH_SET(hdr->kdeth.ver_tid_offset, SH, 0);
1410 /*
1411 * Set the KDETH.OFFSET and KDETH.OM based on size of
1412 * transfer.
1413 */
1414 SDMA_DBG(req, "TID offset %ubytes %uunits om%u",
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001415 req->tidoffset, req->tidoffset >> omfactor,
1416 omfactor != KDETH_OM_SMALL_SHIFT);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001417 KDETH_SET(hdr->kdeth.ver_tid_offset, OFFSET,
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001418 req->tidoffset >> omfactor);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001419 KDETH_SET(hdr->kdeth.ver_tid_offset, OM,
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001420 omfactor != KDETH_OM_SMALL_SHIFT);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001421 }
1422done:
1423 trace_hfi1_sdma_user_header(pq->dd, pq->ctxt, pq->subctxt,
1424 req->info.comp_idx, hdr, tidval);
1425 return sdma_txadd_kvaddr(pq->dd, &tx->txreq, hdr, sizeof(*hdr));
1426}
1427
1428static int set_txreq_header_ahg(struct user_sdma_request *req,
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001429 struct user_sdma_txreq *tx, u32 datalen)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001430{
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001431 u32 ahg[AHG_KDETH_ARRAY_SIZE];
Mike Marciniszyn77241052015-07-30 15:17:43 -04001432 int diff = 0;
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001433 u8 omfactor; /* KDETH.OM */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001434 struct hfi1_user_sdma_pkt_q *pq = req->pq;
1435 struct hfi1_pkt_header *hdr = &req->hdr;
1436 u16 pbclen = le16_to_cpu(hdr->pbc[0]);
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001437 u32 val32, tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(datalen));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001438
1439 if (PBC2LRH(pbclen) != lrhlen) {
1440 /* PBC.PbcLengthDWs */
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001441 AHG_HEADER_SET(ahg, diff, 0, 0, 12,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001442 cpu_to_le16(LRH2PBC(lrhlen)));
1443 /* LRH.PktLen (we need the full 16 bits due to byte swap) */
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001444 AHG_HEADER_SET(ahg, diff, 3, 0, 16,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001445 cpu_to_be16(lrhlen >> 2));
1446 }
1447
1448 /*
1449 * Do the common updates
1450 */
1451 /* BTH.PSN and BTH.A */
1452 val32 = (be32_to_cpu(hdr->bth[2]) + req->seqnum) &
1453 (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffff : 0xffffff);
Jakub Pawlake7301392016-12-07 19:32:41 -08001454 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_ACK))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001455 val32 |= 1UL << 31;
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001456 AHG_HEADER_SET(ahg, diff, 6, 0, 16, cpu_to_be16(val32 >> 16));
1457 AHG_HEADER_SET(ahg, diff, 6, 16, 16, cpu_to_be16(val32 & 0xffff));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001458 /* KDETH.Offset */
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001459 AHG_HEADER_SET(ahg, diff, 15, 0, 16,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001460 cpu_to_le16(req->koffset & 0xffff));
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001461 AHG_HEADER_SET(ahg, diff, 15, 16, 16, cpu_to_le16(req->koffset >> 16));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001462 if (req_opcode(req->info.ctrl) == EXPECTED) {
1463 __le16 val;
1464
1465 tidval = req->tids[req->tididx];
1466
1467 /*
1468 * If the offset puts us at the end of the current TID,
1469 * advance everything.
1470 */
1471 if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) *
1472 PAGE_SIZE)) {
1473 req->tidoffset = 0;
Jubin John4d114fd2016-02-14 20:21:43 -08001474 /*
1475 * Since we don't copy all the TIDs, all at once,
1476 * we have to check again.
1477 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001478 if (++req->tididx > req->n_tids - 1 ||
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001479 !req->tids[req->tididx])
Mike Marciniszyn77241052015-07-30 15:17:43 -04001480 return -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001481 tidval = req->tids[req->tididx];
1482 }
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001483 omfactor = ((EXP_TID_GET(tidval, LEN) *
Mike Marciniszyn77241052015-07-30 15:17:43 -04001484 PAGE_SIZE) >=
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001485 KDETH_OM_MAX_SIZE) ? KDETH_OM_LARGE_SHIFT :
1486 KDETH_OM_SMALL_SHIFT;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001487 /* KDETH.OM and KDETH.OFFSET (TID) */
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001488 AHG_HEADER_SET(ahg, diff, 7, 0, 16,
Sebastian Sanchezade6f8a2017-05-04 05:14:16 -07001489 ((!!(omfactor - KDETH_OM_SMALL_SHIFT)) << 15 |
1490 ((req->tidoffset >> omfactor)
1491 & 0x7fff)));
Jakub Pawlake7301392016-12-07 19:32:41 -08001492 /* KDETH.TIDCtrl, KDETH.TID, KDETH.Intr, KDETH.SH */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001493 val = cpu_to_le16(((EXP_TID_GET(tidval, CTRL) & 0x3) << 10) |
Jakub Pawlake7301392016-12-07 19:32:41 -08001494 (EXP_TID_GET(tidval, IDX) & 0x3ff));
1495
1496 if (unlikely(tx->flags & TXREQ_FLAGS_REQ_DISABLE_SH)) {
1497 val |= cpu_to_le16((KDETH_GET(hdr->kdeth.ver_tid_offset,
1498 INTR) <<
1499 AHG_KDETH_INTR_SHIFT));
Jubin Johne4909742016-02-14 20:22:00 -08001500 } else {
Jakub Pawlake7301392016-12-07 19:32:41 -08001501 val |= KDETH_GET(hdr->kdeth.ver_tid_offset, SH) ?
1502 cpu_to_le16(0x1 << AHG_KDETH_SH_SHIFT) :
1503 cpu_to_le16((KDETH_GET(hdr->kdeth.ver_tid_offset,
1504 INTR) <<
1505 AHG_KDETH_INTR_SHIFT));
Jubin Johne4909742016-02-14 20:22:00 -08001506 }
Jakub Pawlake7301392016-12-07 19:32:41 -08001507
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001508 AHG_HEADER_SET(ahg, diff, 7, 16, 14, val);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001509 }
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001510 if (diff < 0)
1511 return diff;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001512
1513 trace_hfi1_sdma_user_header_ahg(pq->dd, pq->ctxt, pq->subctxt,
1514 req->info.comp_idx, req->sde->this_idx,
Sebastian Sancheze3304b7cc42017-05-26 05:35:18 -07001515 req->ahg_idx, ahg, diff, tidval);
1516 sdma_txinit_ahg(&tx->txreq,
1517 SDMA_TXREQ_F_USE_AHG,
1518 datalen, req->ahg_idx, diff,
1519 ahg, sizeof(req->hdr),
1520 user_sdma_txreq_cb);
1521
Mike Marciniszyn77241052015-07-30 15:17:43 -04001522 return diff;
1523}
1524
Mitko Haralanova0d40692015-12-08 17:10:13 -05001525/*
1526 * SDMA tx request completion callback. Called when the SDMA progress
1527 * state machine gets notification that the SDMA descriptors for this
1528 * tx request have been processed by the DMA engine. Called in
1529 * interrupt context.
1530 */
Mike Marciniszyna545f532016-02-14 12:45:53 -08001531static void user_sdma_txreq_cb(struct sdma_txreq *txreq, int status)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001532{
1533 struct user_sdma_txreq *tx =
1534 container_of(txreq, struct user_sdma_txreq, txreq);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001535 struct user_sdma_request *req;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001536 struct hfi1_user_sdma_pkt_q *pq;
1537 struct hfi1_user_sdma_comp_q *cq;
1538 u16 idx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001539
Mitko Haralanova0d40692015-12-08 17:10:13 -05001540 if (!tx->req)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001541 return;
1542
Mitko Haralanova0d40692015-12-08 17:10:13 -05001543 req = tx->req;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001544 pq = req->pq;
1545 cq = req->cq;
Mitko Haralanovb9fb63182015-10-26 10:28:37 -04001546
Mike Marciniszyn77241052015-07-30 15:17:43 -04001547 if (status != SDMA_TXREQ_S_OK) {
Mitko Haralanova0d40692015-12-08 17:10:13 -05001548 SDMA_DBG(req, "SDMA completion with error %d",
1549 status);
Sebastian Sancheze9c48eb2017-05-26 05:35:50 -07001550 WRITE_ONCE(req->has_error, 1);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001551 }
1552
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001553 req->seqcomp = tx->seqnum;
1554 kmem_cache_free(pq->txreq_cache, tx);
1555 tx = NULL;
1556
1557 idx = req->info.comp_idx;
1558 if (req->status == -1 && status == SDMA_TXREQ_S_OK) {
1559 if (req->seqcomp == req->info.npkts - 1) {
1560 req->status = 0;
1561 user_sdma_free_request(req, false);
1562 pq_update(pq);
1563 set_comp_state(pq, cq, idx, COMPLETE, 0);
1564 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001565 } else {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001566 if (status != SDMA_TXREQ_S_OK)
1567 req->status = status;
Mitko Haralanovc7cbf2f2016-02-03 14:35:23 -08001568 if (req->seqcomp == (ACCESS_ONCE(req->seqsubmitted) - 1) &&
Sebastian Sanchezb8884292017-05-26 05:35:44 -07001569 (READ_ONCE(req->done) ||
Sebastian Sancheze9c48eb2017-05-26 05:35:50 -07001570 READ_ONCE(req->has_error))) {
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001571 user_sdma_free_request(req, false);
1572 pq_update(pq);
1573 set_comp_state(pq, cq, idx, ERROR, req->status);
1574 }
Mitko Haralanova0d40692015-12-08 17:10:13 -05001575 }
1576}
1577
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001578static inline void pq_update(struct hfi1_user_sdma_pkt_q *pq)
Mitko Haralanova0d40692015-12-08 17:10:13 -05001579{
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001580 if (atomic_dec_and_test(&pq->n_reqs)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001581 xchg(&pq->state, SDMA_PKT_Q_INACTIVE);
Mitko Haralanova0d40692015-12-08 17:10:13 -05001582 wake_up(&pq->wait);
1583 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001584}
1585
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001586static void user_sdma_free_request(struct user_sdma_request *req, bool unpin)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001587{
1588 if (!list_empty(&req->txps)) {
1589 struct sdma_txreq *t, *p;
1590
1591 list_for_each_entry_safe(t, p, &req->txps, list) {
1592 struct user_sdma_txreq *tx =
1593 container_of(t, struct user_sdma_txreq, txreq);
1594 list_del_init(&t->list);
1595 sdma_txclean(req->pq->dd, t);
1596 kmem_cache_free(req->pq->txreq_cache, tx);
1597 }
1598 }
1599 if (req->data_iovs) {
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001600 struct sdma_mmu_node *node;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001601 int i;
1602
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001603 for (i = 0; i < req->data_iovs; i++) {
Mitko Haralanov9565c6a2016-05-19 05:21:18 -07001604 node = req->iovs[i].node;
1605 if (!node)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001606 continue;
1607
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001608 if (unpin)
Dean Luicke0b09ac2016-07-28 15:21:20 -04001609 hfi1_mmu_rb_remove(req->pq->handler,
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001610 &node->rb);
1611 else
1612 atomic_dec(&node->refcount);
1613 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001614 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001615 kfree(req->tids);
Dean Luick7b3256e2016-07-28 15:21:18 -04001616 clear_bit(req->info.comp_idx, req->pq->req_in_use);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001617}
1618
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001619static inline void set_comp_state(struct hfi1_user_sdma_pkt_q *pq,
1620 struct hfi1_user_sdma_comp_q *cq,
1621 u16 idx, enum hfi1_sdma_comp_state state,
1622 int ret)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001623{
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001624 hfi1_cdbg(SDMA, "[%u:%u:%u:%u] Setting completion status %u %d",
1625 pq->dd->unit, pq->ctxt, pq->subctxt, idx, state, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001626 if (state == ERROR)
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001627 cq->comps[idx].errcode = -ret;
Michael J. Ruhl0519c522017-03-20 17:24:45 -07001628 smp_wmb(); /* make sure errcode is visible first */
1629 cq->comps[idx].status = state;
Mitko Haralanov0f2d87d2016-02-03 14:35:06 -08001630 trace_hfi1_sdma_user_completion(pq->dd, pq->ctxt, pq->subctxt,
1631 idx, state, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001632}
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001633
1634static bool sdma_rb_filter(struct mmu_rb_node *node, unsigned long addr,
1635 unsigned long len)
1636{
1637 return (bool)(node->addr == addr);
1638}
1639
Dean Luicke0b09ac2016-07-28 15:21:20 -04001640static int sdma_rb_insert(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001641{
1642 struct sdma_mmu_node *node =
1643 container_of(mnode, struct sdma_mmu_node, rb);
1644
1645 atomic_inc(&node->refcount);
1646 return 0;
1647}
1648
Dean Luickb7df1922016-07-28 15:21:23 -04001649/*
1650 * Return 1 to remove the node from the rb tree and call the remove op.
1651 *
1652 * Called with the rb tree lock held.
1653 */
1654static int sdma_rb_evict(void *arg, struct mmu_rb_node *mnode,
1655 void *evict_arg, bool *stop)
1656{
1657 struct sdma_mmu_node *node =
1658 container_of(mnode, struct sdma_mmu_node, rb);
1659 struct evict_data *evict_data = evict_arg;
1660
1661 /* is this node still being used? */
1662 if (atomic_read(&node->refcount))
1663 return 0; /* keep this node */
1664
1665 /* this node will be evicted, add its pages to our count */
1666 evict_data->cleared += node->npages;
1667
1668 /* have enough pages been cleared? */
1669 if (evict_data->cleared >= evict_data->target)
1670 *stop = true;
1671
1672 return 1; /* remove this node */
1673}
1674
Dean Luick082b3532016-07-28 15:21:25 -04001675static void sdma_rb_remove(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001676{
1677 struct sdma_mmu_node *node =
1678 container_of(mnode, struct sdma_mmu_node, rb);
1679
Harish Chegondi04a646d2017-08-21 18:27:09 -07001680 unpin_sdma_pages(node);
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001681 kfree(node);
1682}
1683
Dean Luicke0b09ac2016-07-28 15:21:20 -04001684static int sdma_rb_invalidate(void *arg, struct mmu_rb_node *mnode)
Mitko Haralanov5cd3a88d2016-03-08 11:15:22 -08001685{
1686 struct sdma_mmu_node *node =
1687 container_of(mnode, struct sdma_mmu_node, rb);
1688
1689 if (!atomic_read(&node->refcount))
1690 return 1;
1691 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001692}