Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1 | /* |
Jubin John | 05d6ac1 | 2016-02-14 20:22:17 -0800 | [diff] [blame] | 2 | * Copyright(c) 2015, 2016 Intel Corporation. |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 3 | * |
| 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 Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 9 | * 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 Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 20 | * 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> |
| 63 | |
| 64 | #include "hfi.h" |
| 65 | #include "sdma.h" |
| 66 | #include "user_sdma.h" |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 67 | #include "verbs.h" /* for the headers */ |
| 68 | #include "common.h" /* for struct hfi1_tid_info */ |
| 69 | #include "trace.h" |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 70 | #include "mmu_rb.h" |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 71 | |
| 72 | static uint hfi1_sdma_comp_ring_size = 128; |
| 73 | module_param_named(sdma_comp_size, hfi1_sdma_comp_ring_size, uint, S_IRUGO); |
| 74 | MODULE_PARM_DESC(sdma_comp_size, "Size of User SDMA completion ring. Default: 128"); |
| 75 | |
| 76 | /* The maximum number of Data io vectors per message/request */ |
| 77 | #define MAX_VECTORS_PER_REQ 8 |
| 78 | /* |
| 79 | * Maximum number of packet to send from each message/request |
| 80 | * before moving to the next one. |
| 81 | */ |
| 82 | #define MAX_PKTS_PER_QUEUE 16 |
| 83 | |
| 84 | #define num_pages(x) (1 + ((((x) - 1) & PAGE_MASK) >> PAGE_SHIFT)) |
| 85 | |
| 86 | #define req_opcode(x) \ |
| 87 | (((x) >> HFI1_SDMA_REQ_OPCODE_SHIFT) & HFI1_SDMA_REQ_OPCODE_MASK) |
| 88 | #define req_version(x) \ |
| 89 | (((x) >> HFI1_SDMA_REQ_VERSION_SHIFT) & HFI1_SDMA_REQ_OPCODE_MASK) |
| 90 | #define req_iovcnt(x) \ |
| 91 | (((x) >> HFI1_SDMA_REQ_IOVCNT_SHIFT) & HFI1_SDMA_REQ_IOVCNT_MASK) |
| 92 | |
| 93 | /* Number of BTH.PSN bits used for sequence number in expected rcvs */ |
| 94 | #define BTH_SEQ_MASK 0x7ffull |
| 95 | |
| 96 | /* |
| 97 | * Define fields in the KDETH header so we can update the header |
| 98 | * template. |
| 99 | */ |
| 100 | #define KDETH_OFFSET_SHIFT 0 |
| 101 | #define KDETH_OFFSET_MASK 0x7fff |
| 102 | #define KDETH_OM_SHIFT 15 |
| 103 | #define KDETH_OM_MASK 0x1 |
| 104 | #define KDETH_TID_SHIFT 16 |
| 105 | #define KDETH_TID_MASK 0x3ff |
| 106 | #define KDETH_TIDCTRL_SHIFT 26 |
| 107 | #define KDETH_TIDCTRL_MASK 0x3 |
| 108 | #define KDETH_INTR_SHIFT 28 |
| 109 | #define KDETH_INTR_MASK 0x1 |
| 110 | #define KDETH_SH_SHIFT 29 |
| 111 | #define KDETH_SH_MASK 0x1 |
| 112 | #define KDETH_HCRC_UPPER_SHIFT 16 |
| 113 | #define KDETH_HCRC_UPPER_MASK 0xff |
| 114 | #define KDETH_HCRC_LOWER_SHIFT 24 |
| 115 | #define KDETH_HCRC_LOWER_MASK 0xff |
| 116 | |
| 117 | #define PBC2LRH(x) ((((x) & 0xfff) << 2) - 4) |
| 118 | #define LRH2PBC(x) ((((x) >> 2) + 1) & 0xfff) |
| 119 | |
| 120 | #define KDETH_GET(val, field) \ |
| 121 | (((le32_to_cpu((val))) >> KDETH_##field##_SHIFT) & KDETH_##field##_MASK) |
| 122 | #define KDETH_SET(dw, field, val) do { \ |
| 123 | u32 dwval = le32_to_cpu(dw); \ |
| 124 | dwval &= ~(KDETH_##field##_MASK << KDETH_##field##_SHIFT); \ |
| 125 | dwval |= (((val) & KDETH_##field##_MASK) << \ |
| 126 | KDETH_##field##_SHIFT); \ |
| 127 | dw = cpu_to_le32(dwval); \ |
| 128 | } while (0) |
| 129 | |
| 130 | #define AHG_HEADER_SET(arr, idx, dw, bit, width, value) \ |
| 131 | do { \ |
| 132 | if ((idx) < ARRAY_SIZE((arr))) \ |
| 133 | (arr)[(idx++)] = sdma_build_ahg_descriptor( \ |
| 134 | (__force u16)(value), (dw), (bit), \ |
| 135 | (width)); \ |
| 136 | else \ |
| 137 | return -ERANGE; \ |
| 138 | } while (0) |
| 139 | |
| 140 | /* KDETH OM multipliers and switch over point */ |
| 141 | #define KDETH_OM_SMALL 4 |
| 142 | #define KDETH_OM_LARGE 64 |
| 143 | #define KDETH_OM_MAX_SIZE (1 << ((KDETH_OM_LARGE / KDETH_OM_SMALL) + 1)) |
| 144 | |
| 145 | /* Last packet in the request */ |
Sunny Kumar | cb32649 | 2015-11-06 10:06:43 +0530 | [diff] [blame] | 146 | #define TXREQ_FLAGS_REQ_LAST_PKT BIT(0) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 147 | |
| 148 | #define SDMA_REQ_IN_USE 0 |
| 149 | #define SDMA_REQ_FOR_THREAD 1 |
| 150 | #define SDMA_REQ_SEND_DONE 2 |
| 151 | #define SDMA_REQ_HAVE_AHG 3 |
| 152 | #define SDMA_REQ_HAS_ERROR 4 |
| 153 | #define SDMA_REQ_DONE_ERROR 5 |
| 154 | |
Sunny Kumar | cb32649 | 2015-11-06 10:06:43 +0530 | [diff] [blame] | 155 | #define SDMA_PKT_Q_INACTIVE BIT(0) |
| 156 | #define SDMA_PKT_Q_ACTIVE BIT(1) |
| 157 | #define SDMA_PKT_Q_DEFERRED BIT(2) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 158 | |
| 159 | /* |
| 160 | * Maximum retry attempts to submit a TX request |
| 161 | * before putting the process to sleep. |
| 162 | */ |
| 163 | #define MAX_DEFER_RETRY_COUNT 1 |
| 164 | |
| 165 | static unsigned initial_pkt_count = 8; |
| 166 | |
| 167 | #define SDMA_IOWAIT_TIMEOUT 1000 /* in milliseconds */ |
| 168 | |
Mitko Haralanov | 9565c6a | 2016-05-19 05:21:18 -0700 | [diff] [blame] | 169 | struct sdma_mmu_node; |
| 170 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 171 | struct user_sdma_iovec { |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 172 | struct list_head list; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 173 | struct iovec iov; |
| 174 | /* number of pages in this vector */ |
| 175 | unsigned npages; |
| 176 | /* array of pinned pages for this vector */ |
| 177 | struct page **pages; |
Jubin John | 4d114fd | 2016-02-14 20:21:43 -0800 | [diff] [blame] | 178 | /* |
| 179 | * offset into the virtual address space of the vector at |
| 180 | * which we last left off. |
| 181 | */ |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 182 | u64 offset; |
Mitko Haralanov | 9565c6a | 2016-05-19 05:21:18 -0700 | [diff] [blame] | 183 | struct sdma_mmu_node *node; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 184 | }; |
| 185 | |
Bart Van Assche | d55215c | 2016-06-03 12:10:37 -0700 | [diff] [blame] | 186 | #define SDMA_CACHE_NODE_EVICT 0 |
Mitko Haralanov | e88c927 | 2016-04-12 10:46:53 -0700 | [diff] [blame] | 187 | |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 188 | struct sdma_mmu_node { |
| 189 | struct mmu_rb_node rb; |
Mitko Haralanov | 5511d78 | 2016-03-08 11:15:44 -0800 | [diff] [blame] | 190 | struct list_head list; |
| 191 | struct hfi1_user_sdma_pkt_q *pq; |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 192 | atomic_t refcount; |
| 193 | struct page **pages; |
| 194 | unsigned npages; |
Mitko Haralanov | e88c927 | 2016-04-12 10:46:53 -0700 | [diff] [blame] | 195 | unsigned long flags; |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 196 | }; |
| 197 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 198 | struct user_sdma_request { |
| 199 | struct sdma_req_info info; |
| 200 | struct hfi1_user_sdma_pkt_q *pq; |
| 201 | struct hfi1_user_sdma_comp_q *cq; |
| 202 | /* This is the original header from user space */ |
| 203 | struct hfi1_pkt_header hdr; |
| 204 | /* |
| 205 | * Pointer to the SDMA engine for this request. |
| 206 | * Since different request could be on different VLs, |
| 207 | * each request will need it's own engine pointer. |
| 208 | */ |
| 209 | struct sdma_engine *sde; |
| 210 | u8 ahg_idx; |
| 211 | u32 ahg[9]; |
| 212 | /* |
| 213 | * KDETH.Offset (Eager) field |
| 214 | * We need to remember the initial value so the headers |
| 215 | * can be updated properly. |
| 216 | */ |
| 217 | u32 koffset; |
| 218 | /* |
| 219 | * KDETH.OFFSET (TID) field |
| 220 | * The offset can cover multiple packets, depending on the |
| 221 | * size of the TID entry. |
| 222 | */ |
| 223 | u32 tidoffset; |
| 224 | /* |
| 225 | * KDETH.OM |
| 226 | * Remember this because the header template always sets it |
| 227 | * to 0. |
| 228 | */ |
| 229 | u8 omfactor; |
| 230 | /* |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 231 | * We copy the iovs for this request (based on |
| 232 | * info.iovcnt). These are only the data vectors |
| 233 | */ |
| 234 | unsigned data_iovs; |
| 235 | /* total length of the data in the request */ |
| 236 | u32 data_len; |
| 237 | /* progress index moving along the iovs array */ |
| 238 | unsigned iov_idx; |
| 239 | struct user_sdma_iovec iovs[MAX_VECTORS_PER_REQ]; |
| 240 | /* number of elements copied to the tids array */ |
| 241 | u16 n_tids; |
| 242 | /* TID array values copied from the tid_iov vector */ |
| 243 | u32 *tids; |
| 244 | u16 tididx; |
| 245 | u32 sent; |
| 246 | u64 seqnum; |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 247 | u64 seqcomp; |
Mitko Haralanov | c7cbf2f | 2016-02-03 14:35:23 -0800 | [diff] [blame] | 248 | u64 seqsubmitted; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 249 | struct list_head txps; |
| 250 | unsigned long flags; |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 251 | /* status of the last txreq completed */ |
| 252 | int status; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 253 | }; |
| 254 | |
Mitko Haralanov | b9fb6318 | 2015-10-26 10:28:37 -0400 | [diff] [blame] | 255 | /* |
| 256 | * A single txreq could span up to 3 physical pages when the MTU |
| 257 | * is sufficiently large (> 4K). Each of the IOV pointers also |
| 258 | * needs it's own set of flags so the vector has been handled |
| 259 | * independently of each other. |
| 260 | */ |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 261 | struct user_sdma_txreq { |
| 262 | /* Packet header for the txreq */ |
| 263 | struct hfi1_pkt_header hdr; |
| 264 | struct sdma_txreq txreq; |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 265 | struct list_head list; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 266 | struct user_sdma_request *req; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 267 | u16 flags; |
| 268 | unsigned busycount; |
| 269 | u64 seqnum; |
| 270 | }; |
| 271 | |
| 272 | #define SDMA_DBG(req, fmt, ...) \ |
| 273 | hfi1_cdbg(SDMA, "[%u:%u:%u:%u] " fmt, (req)->pq->dd->unit, \ |
| 274 | (req)->pq->ctxt, (req)->pq->subctxt, (req)->info.comp_idx, \ |
| 275 | ##__VA_ARGS__) |
| 276 | #define SDMA_Q_DBG(pq, fmt, ...) \ |
| 277 | hfi1_cdbg(SDMA, "[%u:%u:%u] " fmt, (pq)->dd->unit, (pq)->ctxt, \ |
| 278 | (pq)->subctxt, ##__VA_ARGS__) |
| 279 | |
| 280 | static int user_sdma_send_pkts(struct user_sdma_request *, unsigned); |
| 281 | static int num_user_pages(const struct iovec *); |
Mike Marciniszyn | a545f53 | 2016-02-14 12:45:53 -0800 | [diff] [blame] | 282 | static void user_sdma_txreq_cb(struct sdma_txreq *, int); |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 283 | static inline void pq_update(struct hfi1_user_sdma_pkt_q *); |
| 284 | static void user_sdma_free_request(struct user_sdma_request *, bool); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 285 | static int pin_vector_pages(struct user_sdma_request *, |
| 286 | struct user_sdma_iovec *); |
Mitko Haralanov | 849e3e9 | 2016-04-12 10:46:16 -0700 | [diff] [blame] | 287 | static void unpin_vector_pages(struct mm_struct *, struct page **, unsigned, |
| 288 | unsigned); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 289 | static int check_header_template(struct user_sdma_request *, |
| 290 | struct hfi1_pkt_header *, u32, u32); |
| 291 | static int set_txreq_header(struct user_sdma_request *, |
| 292 | struct user_sdma_txreq *, u32); |
| 293 | static int set_txreq_header_ahg(struct user_sdma_request *, |
| 294 | struct user_sdma_txreq *, u32); |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 295 | static inline void set_comp_state(struct hfi1_user_sdma_pkt_q *, |
| 296 | struct hfi1_user_sdma_comp_q *, |
| 297 | u16, enum hfi1_sdma_comp_state, int); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 298 | static inline u32 set_pkt_bth_psn(__be32, u8, u32); |
| 299 | static inline u32 get_lrh_len(struct hfi1_pkt_header, u32 len); |
| 300 | |
| 301 | static int defer_packet_queue( |
| 302 | struct sdma_engine *, |
| 303 | struct iowait *, |
| 304 | struct sdma_txreq *, |
| 305 | unsigned seq); |
| 306 | static void activate_packet_queue(struct iowait *, int); |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 307 | static bool sdma_rb_filter(struct mmu_rb_node *, unsigned long, unsigned long); |
| 308 | static int sdma_rb_insert(struct rb_root *, struct mmu_rb_node *); |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 309 | static void sdma_rb_remove(struct rb_root *, struct mmu_rb_node *, |
| 310 | struct mm_struct *); |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 311 | static int sdma_rb_invalidate(struct rb_root *, struct mmu_rb_node *); |
| 312 | |
| 313 | static struct mmu_rb_ops sdma_rb_ops = { |
| 314 | .filter = sdma_rb_filter, |
| 315 | .insert = sdma_rb_insert, |
| 316 | .remove = sdma_rb_remove, |
| 317 | .invalidate = sdma_rb_invalidate |
| 318 | }; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 319 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 320 | static int defer_packet_queue( |
| 321 | struct sdma_engine *sde, |
| 322 | struct iowait *wait, |
| 323 | struct sdma_txreq *txreq, |
| 324 | unsigned seq) |
| 325 | { |
| 326 | struct hfi1_user_sdma_pkt_q *pq = |
| 327 | container_of(wait, struct hfi1_user_sdma_pkt_q, busy); |
| 328 | struct hfi1_ibdev *dev = &pq->dd->verbs_dev; |
| 329 | struct user_sdma_txreq *tx = |
| 330 | container_of(txreq, struct user_sdma_txreq, txreq); |
| 331 | |
| 332 | if (sdma_progress(sde, seq, txreq)) { |
| 333 | if (tx->busycount++ < MAX_DEFER_RETRY_COUNT) |
| 334 | goto eagain; |
| 335 | } |
| 336 | /* |
| 337 | * We are assuming that if the list is enqueued somewhere, it |
| 338 | * is to the dmawait list since that is the only place where |
| 339 | * it is supposed to be enqueued. |
| 340 | */ |
| 341 | xchg(&pq->state, SDMA_PKT_Q_DEFERRED); |
| 342 | write_seqlock(&dev->iowait_lock); |
| 343 | if (list_empty(&pq->busy.list)) |
| 344 | list_add_tail(&pq->busy.list, &sde->dmawait); |
| 345 | write_sequnlock(&dev->iowait_lock); |
| 346 | return -EBUSY; |
| 347 | eagain: |
| 348 | return -EAGAIN; |
| 349 | } |
| 350 | |
| 351 | static void activate_packet_queue(struct iowait *wait, int reason) |
| 352 | { |
| 353 | struct hfi1_user_sdma_pkt_q *pq = |
| 354 | container_of(wait, struct hfi1_user_sdma_pkt_q, busy); |
| 355 | xchg(&pq->state, SDMA_PKT_Q_ACTIVE); |
| 356 | wake_up(&wait->wait_dma); |
| 357 | }; |
| 358 | |
| 359 | static void sdma_kmem_cache_ctor(void *obj) |
| 360 | { |
Janani Ravichandran | 16ccad0 | 2016-02-25 15:08:17 -0500 | [diff] [blame] | 361 | struct user_sdma_txreq *tx = obj; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 362 | |
| 363 | memset(tx, 0, sizeof(*tx)); |
| 364 | } |
| 365 | |
| 366 | int hfi1_user_sdma_alloc_queues(struct hfi1_ctxtdata *uctxt, struct file *fp) |
| 367 | { |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 368 | struct hfi1_filedata *fd; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 369 | int ret = 0; |
| 370 | unsigned memsize; |
| 371 | char buf[64]; |
| 372 | struct hfi1_devdata *dd; |
| 373 | struct hfi1_user_sdma_comp_q *cq; |
| 374 | struct hfi1_user_sdma_pkt_q *pq; |
| 375 | unsigned long flags; |
| 376 | |
| 377 | if (!uctxt || !fp) { |
| 378 | ret = -EBADF; |
| 379 | goto done; |
| 380 | } |
| 381 | |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 382 | fd = fp->private_data; |
| 383 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 384 | if (!hfi1_sdma_comp_ring_size) { |
| 385 | ret = -EINVAL; |
| 386 | goto done; |
| 387 | } |
| 388 | |
| 389 | dd = uctxt->dd; |
| 390 | |
| 391 | pq = kzalloc(sizeof(*pq), GFP_KERNEL); |
Alison Schofield | 806e6e1 | 2015-10-12 14:28:36 -0700 | [diff] [blame] | 392 | if (!pq) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 393 | goto pq_nomem; |
Alison Schofield | 806e6e1 | 2015-10-12 14:28:36 -0700 | [diff] [blame] | 394 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 395 | memsize = sizeof(*pq->reqs) * hfi1_sdma_comp_ring_size; |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 396 | pq->reqs = kzalloc(memsize, GFP_KERNEL); |
Alison Schofield | 806e6e1 | 2015-10-12 14:28:36 -0700 | [diff] [blame] | 397 | if (!pq->reqs) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 398 | goto pq_reqs_nomem; |
Alison Schofield | 806e6e1 | 2015-10-12 14:28:36 -0700 | [diff] [blame] | 399 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 400 | INIT_LIST_HEAD(&pq->list); |
| 401 | pq->dd = dd; |
| 402 | pq->ctxt = uctxt->ctxt; |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 403 | pq->subctxt = fd->subctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 404 | pq->n_max_reqs = hfi1_sdma_comp_ring_size; |
| 405 | pq->state = SDMA_PKT_Q_INACTIVE; |
| 406 | atomic_set(&pq->n_reqs, 0); |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 407 | init_waitqueue_head(&pq->wait); |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 408 | pq->sdma_rb_root = RB_ROOT; |
Mitko Haralanov | 5511d78 | 2016-03-08 11:15:44 -0800 | [diff] [blame] | 409 | INIT_LIST_HEAD(&pq->evict); |
| 410 | spin_lock_init(&pq->evict_lock); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 411 | |
| 412 | iowait_init(&pq->busy, 0, NULL, defer_packet_queue, |
Mike Marciniszyn | a545f53 | 2016-02-14 12:45:53 -0800 | [diff] [blame] | 413 | activate_packet_queue, NULL); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 414 | pq->reqidx = 0; |
| 415 | snprintf(buf, 64, "txreq-kmem-cache-%u-%u-%u", dd->unit, uctxt->ctxt, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 416 | fd->subctxt); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 417 | pq->txreq_cache = kmem_cache_create(buf, |
| 418 | sizeof(struct user_sdma_txreq), |
| 419 | L1_CACHE_BYTES, |
| 420 | SLAB_HWCACHE_ALIGN, |
| 421 | sdma_kmem_cache_ctor); |
| 422 | if (!pq->txreq_cache) { |
| 423 | dd_dev_err(dd, "[%u] Failed to allocate TxReq cache\n", |
| 424 | uctxt->ctxt); |
| 425 | goto pq_txreq_nomem; |
| 426 | } |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 427 | fd->pq = pq; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 428 | cq = kzalloc(sizeof(*cq), GFP_KERNEL); |
Alison Schofield | 806e6e1 | 2015-10-12 14:28:36 -0700 | [diff] [blame] | 429 | if (!cq) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 430 | goto cq_nomem; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 431 | |
Amitoj Kaur Chawla | 8444991 | 2016-03-04 22:30:43 +0530 | [diff] [blame] | 432 | memsize = PAGE_ALIGN(sizeof(*cq->comps) * hfi1_sdma_comp_ring_size); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 433 | cq->comps = vmalloc_user(memsize); |
Alison Schofield | 806e6e1 | 2015-10-12 14:28:36 -0700 | [diff] [blame] | 434 | if (!cq->comps) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 435 | goto cq_comps_nomem; |
Alison Schofield | 806e6e1 | 2015-10-12 14:28:36 -0700 | [diff] [blame] | 436 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 437 | cq->nentries = hfi1_sdma_comp_ring_size; |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 438 | fd->cq = cq; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 439 | |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 440 | ret = hfi1_mmu_rb_register(&pq->sdma_rb_root, &sdma_rb_ops); |
| 441 | if (ret) { |
| 442 | dd_dev_err(dd, "Failed to register with MMU %d", ret); |
| 443 | goto done; |
| 444 | } |
| 445 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 446 | spin_lock_irqsave(&uctxt->sdma_qlock, flags); |
| 447 | list_add(&pq->list, &uctxt->sdma_queues); |
| 448 | spin_unlock_irqrestore(&uctxt->sdma_qlock, flags); |
| 449 | goto done; |
| 450 | |
| 451 | cq_comps_nomem: |
| 452 | kfree(cq); |
| 453 | cq_nomem: |
| 454 | kmem_cache_destroy(pq->txreq_cache); |
| 455 | pq_txreq_nomem: |
| 456 | kfree(pq->reqs); |
| 457 | pq_reqs_nomem: |
| 458 | kfree(pq); |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 459 | fd->pq = NULL; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 460 | pq_nomem: |
| 461 | ret = -ENOMEM; |
| 462 | done: |
| 463 | return ret; |
| 464 | } |
| 465 | |
| 466 | int hfi1_user_sdma_free_queues(struct hfi1_filedata *fd) |
| 467 | { |
| 468 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
| 469 | struct hfi1_user_sdma_pkt_q *pq; |
| 470 | unsigned long flags; |
| 471 | |
| 472 | hfi1_cdbg(SDMA, "[%u:%u:%u] Freeing user SDMA queues", uctxt->dd->unit, |
| 473 | uctxt->ctxt, fd->subctxt); |
| 474 | pq = fd->pq; |
| 475 | if (pq) { |
Ira Weiny | 53445bb | 2016-07-28 15:21:12 -0400 | [diff] [blame] | 476 | hfi1_mmu_rb_unregister(&pq->sdma_rb_root); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 477 | spin_lock_irqsave(&uctxt->sdma_qlock, flags); |
| 478 | if (!list_empty(&pq->list)) |
| 479 | list_del_init(&pq->list); |
| 480 | spin_unlock_irqrestore(&uctxt->sdma_qlock, flags); |
| 481 | iowait_sdma_drain(&pq->busy); |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 482 | /* Wait until all requests have been freed. */ |
| 483 | wait_event_interruptible( |
| 484 | pq->wait, |
| 485 | (ACCESS_ONCE(pq->state) == SDMA_PKT_Q_INACTIVE)); |
| 486 | kfree(pq->reqs); |
Julia Lawall | adad44d | 2015-09-13 14:15:04 +0200 | [diff] [blame] | 487 | kmem_cache_destroy(pq->txreq_cache); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 488 | kfree(pq); |
| 489 | fd->pq = NULL; |
| 490 | } |
| 491 | if (fd->cq) { |
Bhumika Goyal | a4d7d05 | 2016-02-14 20:34:28 +0530 | [diff] [blame] | 492 | vfree(fd->cq->comps); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 493 | kfree(fd->cq); |
| 494 | fd->cq = NULL; |
| 495 | } |
| 496 | return 0; |
| 497 | } |
| 498 | |
Jianxin Xiong | 14833b8 | 2016-07-01 16:01:56 -0700 | [diff] [blame] | 499 | static u8 dlid_to_selector(u16 dlid) |
| 500 | { |
| 501 | static u8 mapping[256]; |
| 502 | static int initialized; |
| 503 | static u8 next; |
| 504 | int hash; |
| 505 | |
| 506 | if (!initialized) { |
| 507 | memset(mapping, 0xFF, 256); |
| 508 | initialized = 1; |
| 509 | } |
| 510 | |
| 511 | hash = ((dlid >> 8) ^ dlid) & 0xFF; |
| 512 | if (mapping[hash] == 0xFF) { |
| 513 | mapping[hash] = next; |
| 514 | next = (next + 1) & 0x7F; |
| 515 | } |
| 516 | |
| 517 | return mapping[hash]; |
| 518 | } |
| 519 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 520 | int hfi1_user_sdma_process_request(struct file *fp, struct iovec *iovec, |
| 521 | unsigned long dim, unsigned long *count) |
| 522 | { |
Dean Luick | ff4ce9b | 2016-07-28 12:27:34 -0400 | [diff] [blame] | 523 | int ret = 0, i; |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 524 | struct hfi1_filedata *fd = fp->private_data; |
| 525 | struct hfi1_ctxtdata *uctxt = fd->uctxt; |
| 526 | struct hfi1_user_sdma_pkt_q *pq = fd->pq; |
| 527 | struct hfi1_user_sdma_comp_q *cq = fd->cq; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 528 | struct hfi1_devdata *dd = pq->dd; |
| 529 | unsigned long idx = 0; |
| 530 | u8 pcount = initial_pkt_count; |
| 531 | struct sdma_req_info info; |
| 532 | struct user_sdma_request *req; |
| 533 | u8 opcode, sc, vl; |
Jianxin Xiong | b583faf | 2016-05-19 05:21:57 -0700 | [diff] [blame] | 534 | int req_queued = 0; |
Jianxin Xiong | 14833b8 | 2016-07-01 16:01:56 -0700 | [diff] [blame] | 535 | u16 dlid; |
| 536 | u8 selector; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 537 | |
| 538 | if (iovec[idx].iov_len < sizeof(info) + sizeof(req->hdr)) { |
| 539 | hfi1_cdbg( |
| 540 | SDMA, |
| 541 | "[%u:%u:%u] First vector not big enough for header %lu/%lu", |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 542 | dd->unit, uctxt->ctxt, fd->subctxt, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 543 | iovec[idx].iov_len, sizeof(info) + sizeof(req->hdr)); |
Mitko Haralanov | faa98b8 | 2015-12-08 17:10:11 -0500 | [diff] [blame] | 544 | return -EINVAL; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 545 | } |
| 546 | ret = copy_from_user(&info, iovec[idx].iov_base, sizeof(info)); |
| 547 | if (ret) { |
| 548 | hfi1_cdbg(SDMA, "[%u:%u:%u] Failed to copy info QW (%d)", |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 549 | dd->unit, uctxt->ctxt, fd->subctxt, ret); |
Mitko Haralanov | faa98b8 | 2015-12-08 17:10:11 -0500 | [diff] [blame] | 550 | return -EFAULT; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 551 | } |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 552 | |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 553 | trace_hfi1_sdma_user_reqinfo(dd, uctxt->ctxt, fd->subctxt, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 554 | (u16 *)&info); |
Dean Luick | 4fa0d22 | 2016-07-28 15:21:14 -0400 | [diff] [blame] | 555 | |
| 556 | if (info.comp_idx >= hfi1_sdma_comp_ring_size) { |
| 557 | hfi1_cdbg(SDMA, |
| 558 | "[%u:%u:%u:%u] Invalid comp index", |
| 559 | dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx); |
| 560 | return -EINVAL; |
| 561 | } |
| 562 | |
Dean Luick | 9ff73c8 | 2016-07-28 15:21:15 -0400 | [diff] [blame] | 563 | /* |
| 564 | * Sanity check the header io vector count. Need at least 1 vector |
| 565 | * (header) and cannot be larger than the actual io vector count. |
| 566 | */ |
| 567 | if (req_iovcnt(info.ctrl) < 1 || req_iovcnt(info.ctrl) > dim) { |
| 568 | hfi1_cdbg(SDMA, |
| 569 | "[%u:%u:%u:%u] Invalid iov count %d, dim %ld", |
| 570 | dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx, |
| 571 | req_iovcnt(info.ctrl), dim); |
| 572 | return -EINVAL; |
| 573 | } |
| 574 | |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 575 | if (cq->comps[info.comp_idx].status == QUEUED || |
| 576 | test_bit(SDMA_REQ_IN_USE, &pq->reqs[info.comp_idx].flags)) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 577 | hfi1_cdbg(SDMA, "[%u:%u:%u] Entry %u is in QUEUED state", |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 578 | dd->unit, uctxt->ctxt, fd->subctxt, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 579 | info.comp_idx); |
Mitko Haralanov | faa98b8 | 2015-12-08 17:10:11 -0500 | [diff] [blame] | 580 | return -EBADSLT; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 581 | } |
| 582 | if (!info.fragsize) { |
| 583 | hfi1_cdbg(SDMA, |
| 584 | "[%u:%u:%u:%u] Request does not specify fragsize", |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 585 | dd->unit, uctxt->ctxt, fd->subctxt, info.comp_idx); |
Mitko Haralanov | faa98b8 | 2015-12-08 17:10:11 -0500 | [diff] [blame] | 586 | return -EINVAL; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 587 | } |
| 588 | /* |
| 589 | * We've done all the safety checks that we can up to this point, |
| 590 | * "allocate" the request entry. |
| 591 | */ |
| 592 | hfi1_cdbg(SDMA, "[%u:%u:%u] Using req/comp entry %u\n", dd->unit, |
Ira Weiny | 9e10af4 | 2015-10-30 18:58:40 -0400 | [diff] [blame] | 593 | uctxt->ctxt, fd->subctxt, info.comp_idx); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 594 | req = pq->reqs + info.comp_idx; |
| 595 | memset(req, 0, sizeof(*req)); |
| 596 | /* Mark the request as IN_USE before we start filling it in. */ |
| 597 | set_bit(SDMA_REQ_IN_USE, &req->flags); |
Dean Luick | 9ff73c8 | 2016-07-28 15:21:15 -0400 | [diff] [blame] | 598 | req->data_iovs = req_iovcnt(info.ctrl) - 1; /* subtract header vector */ |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 599 | req->pq = pq; |
| 600 | req->cq = cq; |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 601 | req->status = -1; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 602 | INIT_LIST_HEAD(&req->txps); |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 603 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 604 | memcpy(&req->info, &info, sizeof(info)); |
| 605 | |
Dean Luick | 9ff73c8 | 2016-07-28 15:21:15 -0400 | [diff] [blame] | 606 | if (req_opcode(info.ctrl) == EXPECTED) { |
| 607 | /* expected must have a TID info and at least one data vector */ |
| 608 | if (req->data_iovs < 2) { |
| 609 | SDMA_DBG(req, |
| 610 | "Not enough vectors for expected request"); |
| 611 | ret = -EINVAL; |
| 612 | goto free_req; |
| 613 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 614 | req->data_iovs--; |
Dean Luick | 9ff73c8 | 2016-07-28 15:21:15 -0400 | [diff] [blame] | 615 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 616 | |
| 617 | if (!info.npkts || req->data_iovs > MAX_VECTORS_PER_REQ) { |
| 618 | SDMA_DBG(req, "Too many vectors (%u/%u)", req->data_iovs, |
| 619 | MAX_VECTORS_PER_REQ); |
Dean Luick | 9da7e9a | 2016-07-28 15:21:17 -0400 | [diff] [blame^] | 620 | ret = -EINVAL; |
| 621 | goto free_req; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 622 | } |
| 623 | /* Copy the header from the user buffer */ |
| 624 | ret = copy_from_user(&req->hdr, iovec[idx].iov_base + sizeof(info), |
| 625 | sizeof(req->hdr)); |
| 626 | if (ret) { |
| 627 | SDMA_DBG(req, "Failed to copy header template (%d)", ret); |
| 628 | ret = -EFAULT; |
| 629 | goto free_req; |
| 630 | } |
| 631 | |
| 632 | /* If Static rate control is not enabled, sanitize the header. */ |
| 633 | if (!HFI1_CAP_IS_USET(STATIC_RATE_CTRL)) |
| 634 | req->hdr.pbc[2] = 0; |
| 635 | |
| 636 | /* Validate the opcode. Do not trust packets from user space blindly. */ |
| 637 | opcode = (be32_to_cpu(req->hdr.bth[0]) >> 24) & 0xff; |
| 638 | if ((opcode & USER_OPCODE_CHECK_MASK) != |
| 639 | USER_OPCODE_CHECK_VAL) { |
| 640 | SDMA_DBG(req, "Invalid opcode (%d)", opcode); |
| 641 | ret = -EINVAL; |
| 642 | goto free_req; |
| 643 | } |
| 644 | /* |
| 645 | * Validate the vl. Do not trust packets from user space blindly. |
| 646 | * VL comes from PBC, SC comes from LRH, and the VL needs to |
| 647 | * match the SC look up. |
| 648 | */ |
| 649 | vl = (le16_to_cpu(req->hdr.pbc[0]) >> 12) & 0xF; |
| 650 | sc = (((be16_to_cpu(req->hdr.lrh[0]) >> 12) & 0xF) | |
| 651 | (((le16_to_cpu(req->hdr.pbc[1]) >> 14) & 0x1) << 4)); |
| 652 | if (vl >= dd->pport->vls_operational || |
| 653 | vl != sc_to_vlt(dd, sc)) { |
| 654 | SDMA_DBG(req, "Invalid SC(%u)/VL(%u)", sc, vl); |
| 655 | ret = -EINVAL; |
| 656 | goto free_req; |
| 657 | } |
| 658 | |
Sebastian Sanchez | e38d1e4 | 2016-04-12 11:22:21 -0700 | [diff] [blame] | 659 | /* Checking P_KEY for requests from user-space */ |
| 660 | if (egress_pkey_check(dd->pport, req->hdr.lrh, req->hdr.bth, sc, |
| 661 | PKEY_CHECK_INVALID)) { |
| 662 | ret = -EINVAL; |
| 663 | goto free_req; |
| 664 | } |
| 665 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 666 | /* |
| 667 | * Also should check the BTH.lnh. If it says the next header is GRH then |
| 668 | * the RXE parsing will be off and will land in the middle of the KDETH |
| 669 | * or miss it entirely. |
| 670 | */ |
| 671 | if ((be16_to_cpu(req->hdr.lrh[0]) & 0x3) == HFI1_LRH_GRH) { |
| 672 | SDMA_DBG(req, "User tried to pass in a GRH"); |
| 673 | ret = -EINVAL; |
| 674 | goto free_req; |
| 675 | } |
| 676 | |
| 677 | req->koffset = le32_to_cpu(req->hdr.kdeth.swdata[6]); |
Jubin John | 4d114fd | 2016-02-14 20:21:43 -0800 | [diff] [blame] | 678 | /* |
| 679 | * Calculate the initial TID offset based on the values of |
| 680 | * KDETH.OFFSET and KDETH.OM that are passed in. |
| 681 | */ |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 682 | req->tidoffset = KDETH_GET(req->hdr.kdeth.ver_tid_offset, OFFSET) * |
| 683 | (KDETH_GET(req->hdr.kdeth.ver_tid_offset, OM) ? |
| 684 | KDETH_OM_LARGE : KDETH_OM_SMALL); |
| 685 | SDMA_DBG(req, "Initial TID offset %u", req->tidoffset); |
| 686 | idx++; |
| 687 | |
| 688 | /* Save all the IO vector structures */ |
Dean Luick | ff4ce9b | 2016-07-28 12:27:34 -0400 | [diff] [blame] | 689 | for (i = 0; i < req->data_iovs; i++) { |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 690 | INIT_LIST_HEAD(&req->iovs[i].list); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 691 | memcpy(&req->iovs[i].iov, iovec + idx++, sizeof(struct iovec)); |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 692 | ret = pin_vector_pages(req, &req->iovs[i]); |
| 693 | if (ret) { |
| 694 | req->status = ret; |
| 695 | goto free_req; |
| 696 | } |
Dean Luick | ff4ce9b | 2016-07-28 12:27:34 -0400 | [diff] [blame] | 697 | req->data_len += req->iovs[i].iov.iov_len; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 698 | } |
| 699 | SDMA_DBG(req, "total data length %u", req->data_len); |
| 700 | |
| 701 | if (pcount > req->info.npkts) |
| 702 | pcount = req->info.npkts; |
| 703 | /* |
| 704 | * Copy any TID info |
| 705 | * User space will provide the TID info only when the |
| 706 | * request type is EXPECTED. This is true even if there is |
| 707 | * only one packet in the request and the header is already |
| 708 | * setup. The reason for the singular TID case is that the |
| 709 | * driver needs to perform safety checks. |
| 710 | */ |
| 711 | if (req_opcode(req->info.ctrl) == EXPECTED) { |
| 712 | u16 ntids = iovec[idx].iov_len / sizeof(*req->tids); |
| 713 | |
| 714 | if (!ntids || ntids > MAX_TID_PAIR_ENTRIES) { |
| 715 | ret = -EINVAL; |
| 716 | goto free_req; |
| 717 | } |
| 718 | req->tids = kcalloc(ntids, sizeof(*req->tids), GFP_KERNEL); |
| 719 | if (!req->tids) { |
| 720 | ret = -ENOMEM; |
| 721 | goto free_req; |
| 722 | } |
| 723 | /* |
| 724 | * We have to copy all of the tids because they may vary |
| 725 | * in size and, therefore, the TID count might not be |
| 726 | * equal to the pkt count. However, there is no way to |
| 727 | * tell at this point. |
| 728 | */ |
| 729 | ret = copy_from_user(req->tids, iovec[idx].iov_base, |
| 730 | ntids * sizeof(*req->tids)); |
| 731 | if (ret) { |
| 732 | SDMA_DBG(req, "Failed to copy %d TIDs (%d)", |
| 733 | ntids, ret); |
| 734 | ret = -EFAULT; |
| 735 | goto free_req; |
| 736 | } |
| 737 | req->n_tids = ntids; |
| 738 | idx++; |
| 739 | } |
| 740 | |
Jianxin Xiong | 14833b8 | 2016-07-01 16:01:56 -0700 | [diff] [blame] | 741 | dlid = be16_to_cpu(req->hdr.lrh[1]); |
| 742 | selector = dlid_to_selector(dlid); |
| 743 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 744 | /* Have to select the engine */ |
| 745 | req->sde = sdma_select_engine_vl(dd, |
Jianxin Xiong | 14833b8 | 2016-07-01 16:01:56 -0700 | [diff] [blame] | 746 | (u32)(uctxt->ctxt + fd->subctxt + |
| 747 | selector), |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 748 | vl); |
| 749 | if (!req->sde || !sdma_running(req->sde)) { |
| 750 | ret = -ECOMM; |
| 751 | goto free_req; |
| 752 | } |
| 753 | |
| 754 | /* We don't need an AHG entry if the request contains only one packet */ |
| 755 | if (req->info.npkts > 1 && HFI1_CAP_IS_USET(SDMA_AHG)) { |
| 756 | int ahg = sdma_ahg_alloc(req->sde); |
| 757 | |
| 758 | if (likely(ahg >= 0)) { |
| 759 | req->ahg_idx = (u8)ahg; |
| 760 | set_bit(SDMA_REQ_HAVE_AHG, &req->flags); |
| 761 | } |
| 762 | } |
| 763 | |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 764 | set_comp_state(pq, cq, info.comp_idx, QUEUED, 0); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 765 | atomic_inc(&pq->n_reqs); |
Jianxin Xiong | b583faf | 2016-05-19 05:21:57 -0700 | [diff] [blame] | 766 | req_queued = 1; |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 767 | /* Send the first N packets in the request to buy us some time */ |
| 768 | ret = user_sdma_send_pkts(req, pcount); |
| 769 | if (unlikely(ret < 0 && ret != -EBUSY)) { |
| 770 | req->status = ret; |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 771 | goto free_req; |
| 772 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 773 | |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 774 | /* |
| 775 | * It is possible that the SDMA engine would have processed all the |
| 776 | * submitted packets by the time we get here. Therefore, only set |
| 777 | * packet queue state to ACTIVE if there are still uncompleted |
| 778 | * requests. |
| 779 | */ |
| 780 | if (atomic_read(&pq->n_reqs)) |
| 781 | xchg(&pq->state, SDMA_PKT_Q_ACTIVE); |
| 782 | |
| 783 | /* |
| 784 | * This is a somewhat blocking send implementation. |
| 785 | * The driver will block the caller until all packets of the |
| 786 | * request have been submitted to the SDMA engine. However, it |
| 787 | * will not wait for send completions. |
| 788 | */ |
| 789 | while (!test_bit(SDMA_REQ_SEND_DONE, &req->flags)) { |
| 790 | ret = user_sdma_send_pkts(req, pcount); |
| 791 | if (ret < 0) { |
| 792 | if (ret != -EBUSY) { |
| 793 | req->status = ret; |
| 794 | set_bit(SDMA_REQ_DONE_ERROR, &req->flags); |
Mitko Haralanov | a402d6a | 2016-02-03 14:37:41 -0800 | [diff] [blame] | 795 | if (ACCESS_ONCE(req->seqcomp) == |
| 796 | req->seqsubmitted - 1) |
| 797 | goto free_req; |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 798 | return ret; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 799 | } |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 800 | wait_event_interruptible_timeout( |
| 801 | pq->busy.wait_dma, |
| 802 | (pq->state == SDMA_PKT_Q_ACTIVE), |
| 803 | msecs_to_jiffies( |
| 804 | SDMA_IOWAIT_TIMEOUT)); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 805 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 806 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 807 | *count += idx; |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 808 | return 0; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 809 | free_req: |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 810 | user_sdma_free_request(req, true); |
Jianxin Xiong | b583faf | 2016-05-19 05:21:57 -0700 | [diff] [blame] | 811 | if (req_queued) |
| 812 | pq_update(pq); |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 813 | set_comp_state(pq, cq, info.comp_idx, ERROR, req->status); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 814 | return ret; |
| 815 | } |
| 816 | |
| 817 | static inline u32 compute_data_length(struct user_sdma_request *req, |
Jubin John | 17fb4f2 | 2016-02-14 20:21:52 -0800 | [diff] [blame] | 818 | struct user_sdma_txreq *tx) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 819 | { |
| 820 | /* |
| 821 | * Determine the proper size of the packet data. |
| 822 | * The size of the data of the first packet is in the header |
| 823 | * template. However, it includes the header and ICRC, which need |
| 824 | * to be subtracted. |
Ira Weiny | c492980 | 2016-07-27 21:08:42 -0400 | [diff] [blame] | 825 | * The minimum representable packet data length in a header is 4 bytes, |
| 826 | * therefore, when the data length request is less than 4 bytes, there's |
| 827 | * only one packet, and the packet data length is equal to that of the |
| 828 | * request data length. |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 829 | * The size of the remaining packets is the minimum of the frag |
| 830 | * size (MTU) or remaining data in the request. |
| 831 | */ |
| 832 | u32 len; |
| 833 | |
| 834 | if (!req->seqnum) { |
Ira Weiny | c492980 | 2016-07-27 21:08:42 -0400 | [diff] [blame] | 835 | if (req->data_len < sizeof(u32)) |
| 836 | len = req->data_len; |
| 837 | else |
| 838 | len = ((be16_to_cpu(req->hdr.lrh[2]) << 2) - |
| 839 | (sizeof(tx->hdr) - 4)); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 840 | } else if (req_opcode(req->info.ctrl) == EXPECTED) { |
| 841 | u32 tidlen = EXP_TID_GET(req->tids[req->tididx], LEN) * |
| 842 | PAGE_SIZE; |
Jubin John | 4d114fd | 2016-02-14 20:21:43 -0800 | [diff] [blame] | 843 | /* |
| 844 | * Get the data length based on the remaining space in the |
| 845 | * TID pair. |
| 846 | */ |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 847 | len = min(tidlen - req->tidoffset, (u32)req->info.fragsize); |
| 848 | /* If we've filled up the TID pair, move to the next one. */ |
| 849 | if (unlikely(!len) && ++req->tididx < req->n_tids && |
| 850 | req->tids[req->tididx]) { |
| 851 | tidlen = EXP_TID_GET(req->tids[req->tididx], |
| 852 | LEN) * PAGE_SIZE; |
| 853 | req->tidoffset = 0; |
| 854 | len = min_t(u32, tidlen, req->info.fragsize); |
| 855 | } |
Jubin John | 4d114fd | 2016-02-14 20:21:43 -0800 | [diff] [blame] | 856 | /* |
| 857 | * Since the TID pairs map entire pages, make sure that we |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 858 | * are not going to try to send more data that we have |
Jubin John | 4d114fd | 2016-02-14 20:21:43 -0800 | [diff] [blame] | 859 | * remaining. |
| 860 | */ |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 861 | len = min(len, req->data_len - req->sent); |
Jubin John | e490974 | 2016-02-14 20:22:00 -0800 | [diff] [blame] | 862 | } else { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 863 | len = min(req->data_len - req->sent, (u32)req->info.fragsize); |
Jubin John | e490974 | 2016-02-14 20:22:00 -0800 | [diff] [blame] | 864 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 865 | SDMA_DBG(req, "Data Length = %u", len); |
| 866 | return len; |
| 867 | } |
| 868 | |
Ira Weiny | c492980 | 2016-07-27 21:08:42 -0400 | [diff] [blame] | 869 | static inline u32 pad_len(u32 len) |
| 870 | { |
| 871 | if (len & (sizeof(u32) - 1)) |
| 872 | len += sizeof(u32) - (len & (sizeof(u32) - 1)); |
| 873 | return len; |
| 874 | } |
| 875 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 876 | static inline u32 get_lrh_len(struct hfi1_pkt_header hdr, u32 len) |
| 877 | { |
| 878 | /* (Size of complete header - size of PBC) + 4B ICRC + data length */ |
| 879 | return ((sizeof(hdr) - sizeof(hdr.pbc)) + 4 + len); |
| 880 | } |
| 881 | |
| 882 | static int user_sdma_send_pkts(struct user_sdma_request *req, unsigned maxpkts) |
| 883 | { |
| 884 | int ret = 0; |
| 885 | unsigned npkts = 0; |
| 886 | struct user_sdma_txreq *tx = NULL; |
| 887 | struct hfi1_user_sdma_pkt_q *pq = NULL; |
| 888 | struct user_sdma_iovec *iovec = NULL; |
| 889 | |
Mitko Haralanov | faa98b8 | 2015-12-08 17:10:11 -0500 | [diff] [blame] | 890 | if (!req->pq) |
| 891 | return -EINVAL; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 892 | |
| 893 | pq = req->pq; |
| 894 | |
Mitko Haralanov | 6a5464f | 2015-12-08 17:10:12 -0500 | [diff] [blame] | 895 | /* If tx completion has reported an error, we are done. */ |
| 896 | if (test_bit(SDMA_REQ_HAS_ERROR, &req->flags)) { |
| 897 | set_bit(SDMA_REQ_DONE_ERROR, &req->flags); |
| 898 | return -EFAULT; |
| 899 | } |
| 900 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 901 | /* |
| 902 | * Check if we might have sent the entire request already |
| 903 | */ |
| 904 | if (unlikely(req->seqnum == req->info.npkts)) { |
| 905 | if (!list_empty(&req->txps)) |
| 906 | goto dosend; |
Mitko Haralanov | faa98b8 | 2015-12-08 17:10:11 -0500 | [diff] [blame] | 907 | return ret; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | if (!maxpkts || maxpkts > req->info.npkts - req->seqnum) |
| 911 | maxpkts = req->info.npkts - req->seqnum; |
| 912 | |
| 913 | while (npkts < maxpkts) { |
| 914 | u32 datalen = 0, queued = 0, data_sent = 0; |
| 915 | u64 iov_offset = 0; |
| 916 | |
| 917 | /* |
| 918 | * Check whether any of the completions have come back |
| 919 | * with errors. If so, we are not going to process any |
| 920 | * more packets from this request. |
| 921 | */ |
| 922 | if (test_bit(SDMA_REQ_HAS_ERROR, &req->flags)) { |
| 923 | set_bit(SDMA_REQ_DONE_ERROR, &req->flags); |
Mitko Haralanov | faa98b8 | 2015-12-08 17:10:11 -0500 | [diff] [blame] | 924 | return -EFAULT; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | tx = kmem_cache_alloc(pq->txreq_cache, GFP_KERNEL); |
Mitko Haralanov | faa98b8 | 2015-12-08 17:10:11 -0500 | [diff] [blame] | 928 | if (!tx) |
| 929 | return -ENOMEM; |
| 930 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 931 | tx->flags = 0; |
| 932 | tx->req = req; |
| 933 | tx->busycount = 0; |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 934 | INIT_LIST_HEAD(&tx->list); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 935 | |
| 936 | if (req->seqnum == req->info.npkts - 1) |
Mitko Haralanov | b9fb6318 | 2015-10-26 10:28:37 -0400 | [diff] [blame] | 937 | tx->flags |= TXREQ_FLAGS_REQ_LAST_PKT; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 938 | |
| 939 | /* |
| 940 | * Calculate the payload size - this is min of the fragment |
| 941 | * (MTU) size or the remaining bytes in the request but only |
| 942 | * if we have payload data. |
| 943 | */ |
| 944 | if (req->data_len) { |
| 945 | iovec = &req->iovs[req->iov_idx]; |
| 946 | if (ACCESS_ONCE(iovec->offset) == iovec->iov.iov_len) { |
| 947 | if (++req->iov_idx == req->data_iovs) { |
| 948 | ret = -EFAULT; |
| 949 | goto free_txreq; |
| 950 | } |
| 951 | iovec = &req->iovs[req->iov_idx]; |
| 952 | WARN_ON(iovec->offset); |
| 953 | } |
| 954 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 955 | datalen = compute_data_length(req, tx); |
| 956 | if (!datalen) { |
| 957 | SDMA_DBG(req, |
| 958 | "Request has data but pkt len is 0"); |
| 959 | ret = -EFAULT; |
| 960 | goto free_tx; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | if (test_bit(SDMA_REQ_HAVE_AHG, &req->flags)) { |
| 965 | if (!req->seqnum) { |
| 966 | u16 pbclen = le16_to_cpu(req->hdr.pbc[0]); |
Ira Weiny | c492980 | 2016-07-27 21:08:42 -0400 | [diff] [blame] | 967 | u32 lrhlen = get_lrh_len(req->hdr, |
| 968 | pad_len(datalen)); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 969 | /* |
| 970 | * Copy the request header into the tx header |
| 971 | * because the HW needs a cacheline-aligned |
| 972 | * address. |
| 973 | * This copy can be optimized out if the hdr |
| 974 | * member of user_sdma_request were also |
| 975 | * cacheline aligned. |
| 976 | */ |
| 977 | memcpy(&tx->hdr, &req->hdr, sizeof(tx->hdr)); |
| 978 | if (PBC2LRH(pbclen) != lrhlen) { |
| 979 | pbclen = (pbclen & 0xf000) | |
| 980 | LRH2PBC(lrhlen); |
| 981 | tx->hdr.pbc[0] = cpu_to_le16(pbclen); |
| 982 | } |
| 983 | ret = sdma_txinit_ahg(&tx->txreq, |
| 984 | SDMA_TXREQ_F_AHG_COPY, |
| 985 | sizeof(tx->hdr) + datalen, |
| 986 | req->ahg_idx, 0, NULL, 0, |
| 987 | user_sdma_txreq_cb); |
| 988 | if (ret) |
| 989 | goto free_tx; |
| 990 | ret = sdma_txadd_kvaddr(pq->dd, &tx->txreq, |
| 991 | &tx->hdr, |
| 992 | sizeof(tx->hdr)); |
| 993 | if (ret) |
| 994 | goto free_txreq; |
| 995 | } else { |
| 996 | int changes; |
| 997 | |
| 998 | changes = set_txreq_header_ahg(req, tx, |
| 999 | datalen); |
| 1000 | if (changes < 0) |
| 1001 | goto free_tx; |
| 1002 | sdma_txinit_ahg(&tx->txreq, |
| 1003 | SDMA_TXREQ_F_USE_AHG, |
| 1004 | datalen, req->ahg_idx, changes, |
| 1005 | req->ahg, sizeof(req->hdr), |
| 1006 | user_sdma_txreq_cb); |
| 1007 | } |
| 1008 | } else { |
| 1009 | ret = sdma_txinit(&tx->txreq, 0, sizeof(req->hdr) + |
| 1010 | datalen, user_sdma_txreq_cb); |
| 1011 | if (ret) |
| 1012 | goto free_tx; |
| 1013 | /* |
| 1014 | * Modify the header for this packet. This only needs |
| 1015 | * to be done if we are not going to use AHG. Otherwise, |
| 1016 | * the HW will do it based on the changes we gave it |
| 1017 | * during sdma_txinit_ahg(). |
| 1018 | */ |
| 1019 | ret = set_txreq_header(req, tx, datalen); |
| 1020 | if (ret) |
| 1021 | goto free_txreq; |
| 1022 | } |
| 1023 | |
| 1024 | /* |
| 1025 | * If the request contains any data vectors, add up to |
| 1026 | * fragsize bytes to the descriptor. |
| 1027 | */ |
| 1028 | while (queued < datalen && |
| 1029 | (req->sent + data_sent) < req->data_len) { |
| 1030 | unsigned long base, offset; |
| 1031 | unsigned pageidx, len; |
| 1032 | |
| 1033 | base = (unsigned long)iovec->iov.iov_base; |
Amitoj Kaur Chawla | 72a5f6a | 2016-02-20 19:08:02 +0530 | [diff] [blame] | 1034 | offset = offset_in_page(base + iovec->offset + |
| 1035 | iov_offset); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1036 | pageidx = (((iovec->offset + iov_offset + |
| 1037 | base) - (base & PAGE_MASK)) >> PAGE_SHIFT); |
| 1038 | len = offset + req->info.fragsize > PAGE_SIZE ? |
| 1039 | PAGE_SIZE - offset : req->info.fragsize; |
| 1040 | len = min((datalen - queued), len); |
| 1041 | ret = sdma_txadd_page(pq->dd, &tx->txreq, |
| 1042 | iovec->pages[pageidx], |
| 1043 | offset, len); |
| 1044 | if (ret) { |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 1045 | SDMA_DBG(req, "SDMA txreq add page failed %d\n", |
| 1046 | ret); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1047 | goto free_txreq; |
| 1048 | } |
| 1049 | iov_offset += len; |
| 1050 | queued += len; |
| 1051 | data_sent += len; |
| 1052 | if (unlikely(queued < datalen && |
| 1053 | pageidx == iovec->npages && |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1054 | req->iov_idx < req->data_iovs - 1)) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1055 | iovec->offset += iov_offset; |
| 1056 | iovec = &req->iovs[++req->iov_idx]; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1057 | iov_offset = 0; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1058 | } |
| 1059 | } |
| 1060 | /* |
| 1061 | * The txreq was submitted successfully so we can update |
| 1062 | * the counters. |
| 1063 | */ |
| 1064 | req->koffset += datalen; |
| 1065 | if (req_opcode(req->info.ctrl) == EXPECTED) |
| 1066 | req->tidoffset += datalen; |
| 1067 | req->sent += data_sent; |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1068 | if (req->data_len) |
| 1069 | iovec->offset += iov_offset; |
Mitko Haralanov | c7cbf2f | 2016-02-03 14:35:23 -0800 | [diff] [blame] | 1070 | list_add_tail(&tx->txreq.list, &req->txps); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1071 | /* |
| 1072 | * It is important to increment this here as it is used to |
| 1073 | * generate the BTH.PSN and, therefore, can't be bulk-updated |
| 1074 | * outside of the loop. |
| 1075 | */ |
| 1076 | tx->seqnum = req->seqnum++; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1077 | npkts++; |
| 1078 | } |
| 1079 | dosend: |
| 1080 | ret = sdma_send_txlist(req->sde, &pq->busy, &req->txps); |
Mitko Haralanov | c7cbf2f | 2016-02-03 14:35:23 -0800 | [diff] [blame] | 1081 | if (list_empty(&req->txps)) { |
| 1082 | req->seqsubmitted = req->seqnum; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1083 | if (req->seqnum == req->info.npkts) { |
| 1084 | set_bit(SDMA_REQ_SEND_DONE, &req->flags); |
| 1085 | /* |
| 1086 | * The txreq has already been submitted to the HW queue |
| 1087 | * so we can free the AHG entry now. Corruption will not |
| 1088 | * happen due to the sequential manner in which |
| 1089 | * descriptors are processed. |
| 1090 | */ |
| 1091 | if (test_bit(SDMA_REQ_HAVE_AHG, &req->flags)) |
| 1092 | sdma_ahg_free(req->sde, req->ahg_idx); |
| 1093 | } |
Mitko Haralanov | c7cbf2f | 2016-02-03 14:35:23 -0800 | [diff] [blame] | 1094 | } else if (ret > 0) { |
| 1095 | req->seqsubmitted += ret; |
| 1096 | ret = 0; |
| 1097 | } |
Mitko Haralanov | faa98b8 | 2015-12-08 17:10:11 -0500 | [diff] [blame] | 1098 | return ret; |
| 1099 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1100 | free_txreq: |
| 1101 | sdma_txclean(pq->dd, &tx->txreq); |
| 1102 | free_tx: |
| 1103 | kmem_cache_free(pq->txreq_cache, tx); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1104 | return ret; |
| 1105 | } |
| 1106 | |
| 1107 | /* |
| 1108 | * How many pages in this iovec element? |
| 1109 | */ |
| 1110 | static inline int num_user_pages(const struct iovec *iov) |
| 1111 | { |
Jubin John | 50e5dcb | 2016-02-14 20:19:41 -0800 | [diff] [blame] | 1112 | const unsigned long addr = (unsigned long)iov->iov_base; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1113 | const unsigned long len = iov->iov_len; |
| 1114 | const unsigned long spage = addr & PAGE_MASK; |
| 1115 | const unsigned long epage = (addr + len - 1) & PAGE_MASK; |
| 1116 | |
| 1117 | return 1 + ((epage - spage) >> PAGE_SHIFT); |
| 1118 | } |
| 1119 | |
Mitko Haralanov | 5511d78 | 2016-03-08 11:15:44 -0800 | [diff] [blame] | 1120 | static u32 sdma_cache_evict(struct hfi1_user_sdma_pkt_q *pq, u32 npages) |
| 1121 | { |
| 1122 | u32 cleared = 0; |
| 1123 | struct sdma_mmu_node *node, *ptr; |
Mitko Haralanov | e88c927 | 2016-04-12 10:46:53 -0700 | [diff] [blame] | 1124 | struct list_head to_evict = LIST_HEAD_INIT(to_evict); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1125 | |
Mitko Haralanov | e88c927 | 2016-04-12 10:46:53 -0700 | [diff] [blame] | 1126 | spin_lock(&pq->evict_lock); |
Mitko Haralanov | 5511d78 | 2016-03-08 11:15:44 -0800 | [diff] [blame] | 1127 | list_for_each_entry_safe_reverse(node, ptr, &pq->evict, list) { |
| 1128 | /* Make sure that no one is still using the node. */ |
| 1129 | if (!atomic_read(&node->refcount)) { |
Mitko Haralanov | e88c927 | 2016-04-12 10:46:53 -0700 | [diff] [blame] | 1130 | set_bit(SDMA_CACHE_NODE_EVICT, &node->flags); |
| 1131 | list_del_init(&node->list); |
| 1132 | list_add(&node->list, &to_evict); |
Mitko Haralanov | 5511d78 | 2016-03-08 11:15:44 -0800 | [diff] [blame] | 1133 | cleared += node->npages; |
Mitko Haralanov | 5511d78 | 2016-03-08 11:15:44 -0800 | [diff] [blame] | 1134 | if (cleared >= npages) |
| 1135 | break; |
| 1136 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1137 | } |
Mitko Haralanov | e88c927 | 2016-04-12 10:46:53 -0700 | [diff] [blame] | 1138 | spin_unlock(&pq->evict_lock); |
| 1139 | |
| 1140 | list_for_each_entry_safe(node, ptr, &to_evict, list) |
| 1141 | hfi1_mmu_rb_remove(&pq->sdma_rb_root, &node->rb); |
| 1142 | |
Mitko Haralanov | 5511d78 | 2016-03-08 11:15:44 -0800 | [diff] [blame] | 1143 | return cleared; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1144 | } |
| 1145 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1146 | static int pin_vector_pages(struct user_sdma_request *req, |
Ira Weiny | 72720dd | 2016-07-28 12:27:25 -0400 | [diff] [blame] | 1147 | struct user_sdma_iovec *iovec) |
| 1148 | { |
Mitko Haralanov | 5511d78 | 2016-03-08 11:15:44 -0800 | [diff] [blame] | 1149 | int ret = 0, pinned, npages, cleared; |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1150 | struct page **pages; |
| 1151 | struct hfi1_user_sdma_pkt_q *pq = req->pq; |
| 1152 | struct sdma_mmu_node *node = NULL; |
| 1153 | struct mmu_rb_node *rb_node; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1154 | |
Mitko Haralanov | f53af85 | 2016-04-12 10:46:47 -0700 | [diff] [blame] | 1155 | rb_node = hfi1_mmu_rb_extract(&pq->sdma_rb_root, |
| 1156 | (unsigned long)iovec->iov.iov_base, |
| 1157 | iovec->iov.iov_len); |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 1158 | if (rb_node && !IS_ERR(rb_node)) |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1159 | node = container_of(rb_node, struct sdma_mmu_node, rb); |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 1160 | else |
| 1161 | rb_node = NULL; |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 1162 | |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1163 | if (!node) { |
| 1164 | node = kzalloc(sizeof(*node), GFP_KERNEL); |
| 1165 | if (!node) |
| 1166 | return -ENOMEM; |
| 1167 | |
| 1168 | node->rb.addr = (unsigned long)iovec->iov.iov_base; |
Mitko Haralanov | 5511d78 | 2016-03-08 11:15:44 -0800 | [diff] [blame] | 1169 | node->pq = pq; |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1170 | atomic_set(&node->refcount, 0); |
Mitko Haralanov | 5511d78 | 2016-03-08 11:15:44 -0800 | [diff] [blame] | 1171 | INIT_LIST_HEAD(&node->list); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1172 | } |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 1173 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1174 | npages = num_user_pages(&iovec->iov); |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1175 | if (node->npages < npages) { |
| 1176 | pages = kcalloc(npages, sizeof(*pages), GFP_KERNEL); |
| 1177 | if (!pages) { |
| 1178 | SDMA_DBG(req, "Failed page array alloc"); |
| 1179 | ret = -ENOMEM; |
| 1180 | goto bail; |
| 1181 | } |
| 1182 | memcpy(pages, node->pages, node->npages * sizeof(*pages)); |
| 1183 | |
| 1184 | npages -= node->npages; |
Mitko Haralanov | e88c927 | 2016-04-12 10:46:53 -0700 | [diff] [blame] | 1185 | |
| 1186 | /* |
| 1187 | * If rb_node is NULL, it means that this is brand new node |
| 1188 | * and, therefore not on the eviction list. |
| 1189 | * If, however, the rb_node is non-NULL, it means that the |
| 1190 | * node is already in RB tree and, therefore on the eviction |
| 1191 | * list (nodes are unconditionally inserted in the eviction |
| 1192 | * list). In that case, we have to remove the node prior to |
| 1193 | * calling the eviction function in order to prevent it from |
| 1194 | * freeing this node. |
| 1195 | */ |
| 1196 | if (rb_node) { |
| 1197 | spin_lock(&pq->evict_lock); |
| 1198 | list_del_init(&node->list); |
| 1199 | spin_unlock(&pq->evict_lock); |
| 1200 | } |
Mitko Haralanov | 5511d78 | 2016-03-08 11:15:44 -0800 | [diff] [blame] | 1201 | retry: |
| 1202 | if (!hfi1_can_pin_pages(pq->dd, pq->n_locked, npages)) { |
Mitko Haralanov | 5511d78 | 2016-03-08 11:15:44 -0800 | [diff] [blame] | 1203 | cleared = sdma_cache_evict(pq, npages); |
Mitko Haralanov | 5511d78 | 2016-03-08 11:15:44 -0800 | [diff] [blame] | 1204 | if (cleared >= npages) |
| 1205 | goto retry; |
| 1206 | } |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1207 | pinned = hfi1_acquire_user_pages( |
| 1208 | ((unsigned long)iovec->iov.iov_base + |
| 1209 | (node->npages * PAGE_SIZE)), npages, 0, |
| 1210 | pages + node->npages); |
| 1211 | if (pinned < 0) { |
| 1212 | kfree(pages); |
| 1213 | ret = pinned; |
| 1214 | goto bail; |
| 1215 | } |
| 1216 | if (pinned != npages) { |
Mitko Haralanov | 849e3e9 | 2016-04-12 10:46:16 -0700 | [diff] [blame] | 1217 | unpin_vector_pages(current->mm, pages, node->npages, |
| 1218 | pinned); |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1219 | ret = -EFAULT; |
| 1220 | goto bail; |
| 1221 | } |
| 1222 | kfree(node->pages); |
Mitko Haralanov | de79093 | 2016-04-12 10:46:41 -0700 | [diff] [blame] | 1223 | node->rb.len = iovec->iov.iov_len; |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1224 | node->pages = pages; |
| 1225 | node->npages += pinned; |
| 1226 | npages = node->npages; |
Mitko Haralanov | 5511d78 | 2016-03-08 11:15:44 -0800 | [diff] [blame] | 1227 | spin_lock(&pq->evict_lock); |
Mitko Haralanov | e88c927 | 2016-04-12 10:46:53 -0700 | [diff] [blame] | 1228 | list_add(&node->list, &pq->evict); |
Mitko Haralanov | 5511d78 | 2016-03-08 11:15:44 -0800 | [diff] [blame] | 1229 | pq->n_locked += pinned; |
| 1230 | spin_unlock(&pq->evict_lock); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1231 | } |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1232 | iovec->pages = node->pages; |
| 1233 | iovec->npages = npages; |
Mitko Haralanov | 9565c6a | 2016-05-19 05:21:18 -0700 | [diff] [blame] | 1234 | iovec->node = node; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1235 | |
Mitko Haralanov | f53af85 | 2016-04-12 10:46:47 -0700 | [diff] [blame] | 1236 | ret = hfi1_mmu_rb_insert(&req->pq->sdma_rb_root, &node->rb); |
| 1237 | if (ret) { |
| 1238 | spin_lock(&pq->evict_lock); |
| 1239 | if (!list_empty(&node->list)) |
| 1240 | list_del(&node->list); |
| 1241 | pq->n_locked -= node->npages; |
| 1242 | spin_unlock(&pq->evict_lock); |
Dean Luick | a383f8e | 2016-07-28 15:21:16 -0400 | [diff] [blame] | 1243 | iovec->node = NULL; |
Mitko Haralanov | f53af85 | 2016-04-12 10:46:47 -0700 | [diff] [blame] | 1244 | goto bail; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1245 | } |
| 1246 | return 0; |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1247 | bail: |
Mitko Haralanov | f53af85 | 2016-04-12 10:46:47 -0700 | [diff] [blame] | 1248 | if (rb_node) |
| 1249 | unpin_vector_pages(current->mm, node->pages, 0, node->npages); |
| 1250 | kfree(node); |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1251 | return ret; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1252 | } |
| 1253 | |
Mitko Haralanov | bd3a894 | 2016-03-08 11:15:33 -0800 | [diff] [blame] | 1254 | static void unpin_vector_pages(struct mm_struct *mm, struct page **pages, |
Mitko Haralanov | 849e3e9 | 2016-04-12 10:46:16 -0700 | [diff] [blame] | 1255 | unsigned start, unsigned npages) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1256 | { |
Ira Weiny | 639297b | 2016-07-28 12:27:33 -0400 | [diff] [blame] | 1257 | hfi1_release_user_pages(mm, pages + start, npages, false); |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1258 | kfree(pages); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | static int check_header_template(struct user_sdma_request *req, |
| 1262 | struct hfi1_pkt_header *hdr, u32 lrhlen, |
| 1263 | u32 datalen) |
| 1264 | { |
| 1265 | /* |
| 1266 | * Perform safety checks for any type of packet: |
| 1267 | * - transfer size is multiple of 64bytes |
Ira Weiny | c492980 | 2016-07-27 21:08:42 -0400 | [diff] [blame] | 1268 | * - packet length is multiple of 4 bytes |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1269 | * - packet length is not larger than MTU size |
| 1270 | * |
| 1271 | * These checks are only done for the first packet of the |
| 1272 | * transfer since the header is "given" to us by user space. |
| 1273 | * For the remainder of the packets we compute the values. |
| 1274 | */ |
Ira Weiny | c492980 | 2016-07-27 21:08:42 -0400 | [diff] [blame] | 1275 | if (req->info.fragsize % PIO_BLOCK_SIZE || lrhlen & 0x3 || |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1276 | lrhlen > get_lrh_len(*hdr, req->info.fragsize)) |
| 1277 | return -EINVAL; |
| 1278 | |
| 1279 | if (req_opcode(req->info.ctrl) == EXPECTED) { |
| 1280 | /* |
| 1281 | * The header is checked only on the first packet. Furthermore, |
| 1282 | * we ensure that at least one TID entry is copied when the |
| 1283 | * request is submitted. Therefore, we don't have to verify that |
| 1284 | * tididx points to something sane. |
| 1285 | */ |
| 1286 | u32 tidval = req->tids[req->tididx], |
| 1287 | tidlen = EXP_TID_GET(tidval, LEN) * PAGE_SIZE, |
| 1288 | tididx = EXP_TID_GET(tidval, IDX), |
| 1289 | tidctrl = EXP_TID_GET(tidval, CTRL), |
| 1290 | tidoff; |
| 1291 | __le32 kval = hdr->kdeth.ver_tid_offset; |
| 1292 | |
| 1293 | tidoff = KDETH_GET(kval, OFFSET) * |
| 1294 | (KDETH_GET(req->hdr.kdeth.ver_tid_offset, OM) ? |
| 1295 | KDETH_OM_LARGE : KDETH_OM_SMALL); |
| 1296 | /* |
| 1297 | * Expected receive packets have the following |
| 1298 | * additional checks: |
| 1299 | * - offset is not larger than the TID size |
| 1300 | * - TIDCtrl values match between header and TID array |
| 1301 | * - TID indexes match between header and TID array |
| 1302 | */ |
| 1303 | if ((tidoff + datalen > tidlen) || |
| 1304 | KDETH_GET(kval, TIDCTRL) != tidctrl || |
| 1305 | KDETH_GET(kval, TID) != tididx) |
| 1306 | return -EINVAL; |
| 1307 | } |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
| 1311 | /* |
| 1312 | * Correctly set the BTH.PSN field based on type of |
| 1313 | * transfer - eager packets can just increment the PSN but |
| 1314 | * expected packets encode generation and sequence in the |
| 1315 | * BTH.PSN field so just incrementing will result in errors. |
| 1316 | */ |
| 1317 | static inline u32 set_pkt_bth_psn(__be32 bthpsn, u8 expct, u32 frags) |
| 1318 | { |
| 1319 | u32 val = be32_to_cpu(bthpsn), |
| 1320 | mask = (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffffull : |
| 1321 | 0xffffffull), |
| 1322 | psn = val & mask; |
| 1323 | if (expct) |
| 1324 | psn = (psn & ~BTH_SEQ_MASK) | ((psn + frags) & BTH_SEQ_MASK); |
| 1325 | else |
| 1326 | psn = psn + frags; |
| 1327 | return psn & mask; |
| 1328 | } |
| 1329 | |
| 1330 | static int set_txreq_header(struct user_sdma_request *req, |
| 1331 | struct user_sdma_txreq *tx, u32 datalen) |
| 1332 | { |
| 1333 | struct hfi1_user_sdma_pkt_q *pq = req->pq; |
| 1334 | struct hfi1_pkt_header *hdr = &tx->hdr; |
| 1335 | u16 pbclen; |
| 1336 | int ret; |
Ira Weiny | c492980 | 2016-07-27 21:08:42 -0400 | [diff] [blame] | 1337 | u32 tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(datalen)); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1338 | |
| 1339 | /* Copy the header template to the request before modification */ |
| 1340 | memcpy(hdr, &req->hdr, sizeof(*hdr)); |
| 1341 | |
| 1342 | /* |
| 1343 | * Check if the PBC and LRH length are mismatched. If so |
| 1344 | * adjust both in the header. |
| 1345 | */ |
| 1346 | pbclen = le16_to_cpu(hdr->pbc[0]); |
| 1347 | if (PBC2LRH(pbclen) != lrhlen) { |
| 1348 | pbclen = (pbclen & 0xf000) | LRH2PBC(lrhlen); |
| 1349 | hdr->pbc[0] = cpu_to_le16(pbclen); |
| 1350 | hdr->lrh[2] = cpu_to_be16(lrhlen >> 2); |
| 1351 | /* |
| 1352 | * Third packet |
| 1353 | * This is the first packet in the sequence that has |
| 1354 | * a "static" size that can be used for the rest of |
| 1355 | * the packets (besides the last one). |
| 1356 | */ |
| 1357 | if (unlikely(req->seqnum == 2)) { |
| 1358 | /* |
| 1359 | * From this point on the lengths in both the |
| 1360 | * PBC and LRH are the same until the last |
| 1361 | * packet. |
| 1362 | * Adjust the template so we don't have to update |
| 1363 | * every packet |
| 1364 | */ |
| 1365 | req->hdr.pbc[0] = hdr->pbc[0]; |
| 1366 | req->hdr.lrh[2] = hdr->lrh[2]; |
| 1367 | } |
| 1368 | } |
| 1369 | /* |
| 1370 | * We only have to modify the header if this is not the |
| 1371 | * first packet in the request. Otherwise, we use the |
| 1372 | * header given to us. |
| 1373 | */ |
| 1374 | if (unlikely(!req->seqnum)) { |
| 1375 | ret = check_header_template(req, hdr, lrhlen, datalen); |
| 1376 | if (ret) |
| 1377 | return ret; |
| 1378 | goto done; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | hdr->bth[2] = cpu_to_be32( |
| 1382 | set_pkt_bth_psn(hdr->bth[2], |
| 1383 | (req_opcode(req->info.ctrl) == EXPECTED), |
| 1384 | req->seqnum)); |
| 1385 | |
| 1386 | /* Set ACK request on last packet */ |
Mitko Haralanov | b9fb6318 | 2015-10-26 10:28:37 -0400 | [diff] [blame] | 1387 | if (unlikely(tx->flags & TXREQ_FLAGS_REQ_LAST_PKT)) |
Jubin John | 8638b77 | 2016-02-14 20:19:24 -0800 | [diff] [blame] | 1388 | hdr->bth[2] |= cpu_to_be32(1UL << 31); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1389 | |
| 1390 | /* Set the new offset */ |
| 1391 | hdr->kdeth.swdata[6] = cpu_to_le32(req->koffset); |
| 1392 | /* Expected packets have to fill in the new TID information */ |
| 1393 | if (req_opcode(req->info.ctrl) == EXPECTED) { |
| 1394 | tidval = req->tids[req->tididx]; |
| 1395 | /* |
| 1396 | * If the offset puts us at the end of the current TID, |
| 1397 | * advance everything. |
| 1398 | */ |
| 1399 | if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) * |
| 1400 | PAGE_SIZE)) { |
| 1401 | req->tidoffset = 0; |
Jubin John | 4d114fd | 2016-02-14 20:21:43 -0800 | [diff] [blame] | 1402 | /* |
| 1403 | * Since we don't copy all the TIDs, all at once, |
| 1404 | * we have to check again. |
| 1405 | */ |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1406 | if (++req->tididx > req->n_tids - 1 || |
| 1407 | !req->tids[req->tididx]) { |
| 1408 | return -EINVAL; |
| 1409 | } |
| 1410 | tidval = req->tids[req->tididx]; |
| 1411 | } |
| 1412 | req->omfactor = EXP_TID_GET(tidval, LEN) * PAGE_SIZE >= |
| 1413 | KDETH_OM_MAX_SIZE ? KDETH_OM_LARGE : KDETH_OM_SMALL; |
| 1414 | /* Set KDETH.TIDCtrl based on value for this TID. */ |
| 1415 | KDETH_SET(hdr->kdeth.ver_tid_offset, TIDCTRL, |
| 1416 | EXP_TID_GET(tidval, CTRL)); |
| 1417 | /* Set KDETH.TID based on value for this TID */ |
| 1418 | KDETH_SET(hdr->kdeth.ver_tid_offset, TID, |
| 1419 | EXP_TID_GET(tidval, IDX)); |
| 1420 | /* Clear KDETH.SH only on the last packet */ |
Mitko Haralanov | b9fb6318 | 2015-10-26 10:28:37 -0400 | [diff] [blame] | 1421 | if (unlikely(tx->flags & TXREQ_FLAGS_REQ_LAST_PKT)) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1422 | KDETH_SET(hdr->kdeth.ver_tid_offset, SH, 0); |
| 1423 | /* |
| 1424 | * Set the KDETH.OFFSET and KDETH.OM based on size of |
| 1425 | * transfer. |
| 1426 | */ |
| 1427 | SDMA_DBG(req, "TID offset %ubytes %uunits om%u", |
| 1428 | req->tidoffset, req->tidoffset / req->omfactor, |
Bart Van Assche | 55c40648 | 2016-06-03 12:11:16 -0700 | [diff] [blame] | 1429 | req->omfactor != KDETH_OM_SMALL); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1430 | KDETH_SET(hdr->kdeth.ver_tid_offset, OFFSET, |
| 1431 | req->tidoffset / req->omfactor); |
| 1432 | KDETH_SET(hdr->kdeth.ver_tid_offset, OM, |
Bart Van Assche | 55c40648 | 2016-06-03 12:11:16 -0700 | [diff] [blame] | 1433 | req->omfactor != KDETH_OM_SMALL); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1434 | } |
| 1435 | done: |
| 1436 | trace_hfi1_sdma_user_header(pq->dd, pq->ctxt, pq->subctxt, |
| 1437 | req->info.comp_idx, hdr, tidval); |
| 1438 | return sdma_txadd_kvaddr(pq->dd, &tx->txreq, hdr, sizeof(*hdr)); |
| 1439 | } |
| 1440 | |
| 1441 | static int set_txreq_header_ahg(struct user_sdma_request *req, |
| 1442 | struct user_sdma_txreq *tx, u32 len) |
| 1443 | { |
| 1444 | int diff = 0; |
| 1445 | struct hfi1_user_sdma_pkt_q *pq = req->pq; |
| 1446 | struct hfi1_pkt_header *hdr = &req->hdr; |
| 1447 | u16 pbclen = le16_to_cpu(hdr->pbc[0]); |
Ira Weiny | c492980 | 2016-07-27 21:08:42 -0400 | [diff] [blame] | 1448 | u32 val32, tidval = 0, lrhlen = get_lrh_len(*hdr, pad_len(len)); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1449 | |
| 1450 | if (PBC2LRH(pbclen) != lrhlen) { |
| 1451 | /* PBC.PbcLengthDWs */ |
| 1452 | AHG_HEADER_SET(req->ahg, diff, 0, 0, 12, |
| 1453 | cpu_to_le16(LRH2PBC(lrhlen))); |
| 1454 | /* LRH.PktLen (we need the full 16 bits due to byte swap) */ |
| 1455 | AHG_HEADER_SET(req->ahg, diff, 3, 0, 16, |
| 1456 | cpu_to_be16(lrhlen >> 2)); |
| 1457 | } |
| 1458 | |
| 1459 | /* |
| 1460 | * Do the common updates |
| 1461 | */ |
| 1462 | /* BTH.PSN and BTH.A */ |
| 1463 | val32 = (be32_to_cpu(hdr->bth[2]) + req->seqnum) & |
| 1464 | (HFI1_CAP_IS_KSET(EXTENDED_PSN) ? 0x7fffffff : 0xffffff); |
Mitko Haralanov | b9fb6318 | 2015-10-26 10:28:37 -0400 | [diff] [blame] | 1465 | if (unlikely(tx->flags & TXREQ_FLAGS_REQ_LAST_PKT)) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1466 | val32 |= 1UL << 31; |
| 1467 | AHG_HEADER_SET(req->ahg, diff, 6, 0, 16, cpu_to_be16(val32 >> 16)); |
| 1468 | AHG_HEADER_SET(req->ahg, diff, 6, 16, 16, cpu_to_be16(val32 & 0xffff)); |
| 1469 | /* KDETH.Offset */ |
| 1470 | AHG_HEADER_SET(req->ahg, diff, 15, 0, 16, |
| 1471 | cpu_to_le16(req->koffset & 0xffff)); |
| 1472 | AHG_HEADER_SET(req->ahg, diff, 15, 16, 16, |
| 1473 | cpu_to_le16(req->koffset >> 16)); |
| 1474 | if (req_opcode(req->info.ctrl) == EXPECTED) { |
| 1475 | __le16 val; |
| 1476 | |
| 1477 | tidval = req->tids[req->tididx]; |
| 1478 | |
| 1479 | /* |
| 1480 | * If the offset puts us at the end of the current TID, |
| 1481 | * advance everything. |
| 1482 | */ |
| 1483 | if ((req->tidoffset) == (EXP_TID_GET(tidval, LEN) * |
| 1484 | PAGE_SIZE)) { |
| 1485 | req->tidoffset = 0; |
Jubin John | 4d114fd | 2016-02-14 20:21:43 -0800 | [diff] [blame] | 1486 | /* |
| 1487 | * Since we don't copy all the TIDs, all at once, |
| 1488 | * we have to check again. |
| 1489 | */ |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1490 | if (++req->tididx > req->n_tids - 1 || |
| 1491 | !req->tids[req->tididx]) { |
| 1492 | return -EINVAL; |
| 1493 | } |
| 1494 | tidval = req->tids[req->tididx]; |
| 1495 | } |
| 1496 | req->omfactor = ((EXP_TID_GET(tidval, LEN) * |
| 1497 | PAGE_SIZE) >= |
| 1498 | KDETH_OM_MAX_SIZE) ? KDETH_OM_LARGE : |
| 1499 | KDETH_OM_SMALL; |
| 1500 | /* KDETH.OM and KDETH.OFFSET (TID) */ |
| 1501 | AHG_HEADER_SET(req->ahg, diff, 7, 0, 16, |
| 1502 | ((!!(req->omfactor - KDETH_OM_SMALL)) << 15 | |
| 1503 | ((req->tidoffset / req->omfactor) & 0x7fff))); |
| 1504 | /* KDETH.TIDCtrl, KDETH.TID */ |
| 1505 | val = cpu_to_le16(((EXP_TID_GET(tidval, CTRL) & 0x3) << 10) | |
| 1506 | (EXP_TID_GET(tidval, IDX) & 0x3ff)); |
| 1507 | /* Clear KDETH.SH on last packet */ |
Mitko Haralanov | b9fb6318 | 2015-10-26 10:28:37 -0400 | [diff] [blame] | 1508 | if (unlikely(tx->flags & TXREQ_FLAGS_REQ_LAST_PKT)) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1509 | val |= cpu_to_le16(KDETH_GET(hdr->kdeth.ver_tid_offset, |
| 1510 | INTR) >> 16); |
| 1511 | val &= cpu_to_le16(~(1U << 13)); |
| 1512 | AHG_HEADER_SET(req->ahg, diff, 7, 16, 14, val); |
Jubin John | e490974 | 2016-02-14 20:22:00 -0800 | [diff] [blame] | 1513 | } else { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1514 | AHG_HEADER_SET(req->ahg, diff, 7, 16, 12, val); |
Jubin John | e490974 | 2016-02-14 20:22:00 -0800 | [diff] [blame] | 1515 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | trace_hfi1_sdma_user_header_ahg(pq->dd, pq->ctxt, pq->subctxt, |
| 1519 | req->info.comp_idx, req->sde->this_idx, |
| 1520 | req->ahg_idx, req->ahg, diff, tidval); |
| 1521 | return diff; |
| 1522 | } |
| 1523 | |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 1524 | /* |
| 1525 | * SDMA tx request completion callback. Called when the SDMA progress |
| 1526 | * state machine gets notification that the SDMA descriptors for this |
| 1527 | * tx request have been processed by the DMA engine. Called in |
| 1528 | * interrupt context. |
| 1529 | */ |
Mike Marciniszyn | a545f53 | 2016-02-14 12:45:53 -0800 | [diff] [blame] | 1530 | static void user_sdma_txreq_cb(struct sdma_txreq *txreq, int status) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1531 | { |
| 1532 | struct user_sdma_txreq *tx = |
| 1533 | container_of(txreq, struct user_sdma_txreq, txreq); |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 1534 | struct user_sdma_request *req; |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 1535 | struct hfi1_user_sdma_pkt_q *pq; |
| 1536 | struct hfi1_user_sdma_comp_q *cq; |
| 1537 | u16 idx; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1538 | |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 1539 | if (!tx->req) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1540 | return; |
| 1541 | |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 1542 | req = tx->req; |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 1543 | pq = req->pq; |
| 1544 | cq = req->cq; |
Mitko Haralanov | b9fb6318 | 2015-10-26 10:28:37 -0400 | [diff] [blame] | 1545 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1546 | if (status != SDMA_TXREQ_S_OK) { |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 1547 | SDMA_DBG(req, "SDMA completion with error %d", |
| 1548 | status); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1549 | set_bit(SDMA_REQ_HAS_ERROR, &req->flags); |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 1550 | } |
| 1551 | |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 1552 | req->seqcomp = tx->seqnum; |
| 1553 | kmem_cache_free(pq->txreq_cache, tx); |
| 1554 | tx = NULL; |
| 1555 | |
| 1556 | idx = req->info.comp_idx; |
| 1557 | if (req->status == -1 && status == SDMA_TXREQ_S_OK) { |
| 1558 | if (req->seqcomp == req->info.npkts - 1) { |
| 1559 | req->status = 0; |
| 1560 | user_sdma_free_request(req, false); |
| 1561 | pq_update(pq); |
| 1562 | set_comp_state(pq, cq, idx, COMPLETE, 0); |
| 1563 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1564 | } else { |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 1565 | if (status != SDMA_TXREQ_S_OK) |
| 1566 | req->status = status; |
Mitko Haralanov | c7cbf2f | 2016-02-03 14:35:23 -0800 | [diff] [blame] | 1567 | if (req->seqcomp == (ACCESS_ONCE(req->seqsubmitted) - 1) && |
| 1568 | (test_bit(SDMA_REQ_SEND_DONE, &req->flags) || |
| 1569 | test_bit(SDMA_REQ_DONE_ERROR, &req->flags))) { |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 1570 | user_sdma_free_request(req, false); |
| 1571 | pq_update(pq); |
| 1572 | set_comp_state(pq, cq, idx, ERROR, req->status); |
| 1573 | } |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 1574 | } |
| 1575 | } |
| 1576 | |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 1577 | static inline void pq_update(struct hfi1_user_sdma_pkt_q *pq) |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 1578 | { |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 1579 | if (atomic_dec_and_test(&pq->n_reqs)) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1580 | xchg(&pq->state, SDMA_PKT_Q_INACTIVE); |
Mitko Haralanov | a0d4069 | 2015-12-08 17:10:13 -0500 | [diff] [blame] | 1581 | wake_up(&pq->wait); |
| 1582 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1583 | } |
| 1584 | |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 1585 | static void user_sdma_free_request(struct user_sdma_request *req, bool unpin) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1586 | { |
| 1587 | if (!list_empty(&req->txps)) { |
| 1588 | struct sdma_txreq *t, *p; |
| 1589 | |
| 1590 | list_for_each_entry_safe(t, p, &req->txps, list) { |
| 1591 | struct user_sdma_txreq *tx = |
| 1592 | container_of(t, struct user_sdma_txreq, txreq); |
| 1593 | list_del_init(&t->list); |
| 1594 | sdma_txclean(req->pq->dd, t); |
| 1595 | kmem_cache_free(req->pq->txreq_cache, tx); |
| 1596 | } |
| 1597 | } |
| 1598 | if (req->data_iovs) { |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1599 | struct sdma_mmu_node *node; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1600 | int i; |
| 1601 | |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1602 | for (i = 0; i < req->data_iovs; i++) { |
Mitko Haralanov | 9565c6a | 2016-05-19 05:21:18 -0700 | [diff] [blame] | 1603 | node = req->iovs[i].node; |
| 1604 | if (!node) |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1605 | continue; |
| 1606 | |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1607 | if (unpin) |
| 1608 | hfi1_mmu_rb_remove(&req->pq->sdma_rb_root, |
| 1609 | &node->rb); |
| 1610 | else |
| 1611 | atomic_dec(&node->refcount); |
| 1612 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1613 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1614 | kfree(req->tids); |
| 1615 | clear_bit(SDMA_REQ_IN_USE, &req->flags); |
| 1616 | } |
| 1617 | |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 1618 | static inline void set_comp_state(struct hfi1_user_sdma_pkt_q *pq, |
| 1619 | struct hfi1_user_sdma_comp_q *cq, |
| 1620 | u16 idx, enum hfi1_sdma_comp_state state, |
| 1621 | int ret) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1622 | { |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 1623 | hfi1_cdbg(SDMA, "[%u:%u:%u:%u] Setting completion status %u %d", |
| 1624 | pq->dd->unit, pq->ctxt, pq->subctxt, idx, state, ret); |
| 1625 | cq->comps[idx].status = state; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1626 | if (state == ERROR) |
Mitko Haralanov | 0f2d87d | 2016-02-03 14:35:06 -0800 | [diff] [blame] | 1627 | cq->comps[idx].errcode = -ret; |
| 1628 | trace_hfi1_sdma_user_completion(pq->dd, pq->ctxt, pq->subctxt, |
| 1629 | idx, state, ret); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1630 | } |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1631 | |
| 1632 | static bool sdma_rb_filter(struct mmu_rb_node *node, unsigned long addr, |
| 1633 | unsigned long len) |
| 1634 | { |
| 1635 | return (bool)(node->addr == addr); |
| 1636 | } |
| 1637 | |
| 1638 | static int sdma_rb_insert(struct rb_root *root, struct mmu_rb_node *mnode) |
| 1639 | { |
| 1640 | struct sdma_mmu_node *node = |
| 1641 | container_of(mnode, struct sdma_mmu_node, rb); |
| 1642 | |
| 1643 | atomic_inc(&node->refcount); |
| 1644 | return 0; |
| 1645 | } |
| 1646 | |
| 1647 | static void sdma_rb_remove(struct rb_root *root, struct mmu_rb_node *mnode, |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 1648 | struct mm_struct *mm) |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1649 | { |
| 1650 | struct sdma_mmu_node *node = |
| 1651 | container_of(mnode, struct sdma_mmu_node, rb); |
| 1652 | |
Mitko Haralanov | 5511d78 | 2016-03-08 11:15:44 -0800 | [diff] [blame] | 1653 | spin_lock(&node->pq->evict_lock); |
Mitko Haralanov | e88c927 | 2016-04-12 10:46:53 -0700 | [diff] [blame] | 1654 | /* |
| 1655 | * We've been called by the MMU notifier but this node has been |
| 1656 | * scheduled for eviction. The eviction function will take care |
| 1657 | * of freeing this node. |
| 1658 | * We have to take the above lock first because we are racing |
| 1659 | * against the setting of the bit in the eviction function. |
| 1660 | */ |
| 1661 | if (mm && test_bit(SDMA_CACHE_NODE_EVICT, &node->flags)) { |
| 1662 | spin_unlock(&node->pq->evict_lock); |
| 1663 | return; |
| 1664 | } |
| 1665 | |
Mitko Haralanov | 4787bc5 | 2016-04-12 10:46:23 -0700 | [diff] [blame] | 1666 | if (!list_empty(&node->list)) |
| 1667 | list_del(&node->list); |
Mitko Haralanov | 5511d78 | 2016-03-08 11:15:44 -0800 | [diff] [blame] | 1668 | node->pq->n_locked -= node->npages; |
| 1669 | spin_unlock(&node->pq->evict_lock); |
| 1670 | |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 1671 | /* |
| 1672 | * If mm is set, we are being called by the MMU notifier and we |
| 1673 | * should not pass a mm_struct to unpin_vector_page(). This is to |
| 1674 | * prevent a deadlock when hfi1_release_user_pages() attempts to |
| 1675 | * take the mmap_sem, which the MMU notifier has already taken. |
| 1676 | */ |
Mitko Haralanov | 849e3e9 | 2016-04-12 10:46:16 -0700 | [diff] [blame] | 1677 | unpin_vector_pages(mm ? NULL : current->mm, node->pages, 0, |
| 1678 | node->npages); |
Mitko Haralanov | bd3a894 | 2016-03-08 11:15:33 -0800 | [diff] [blame] | 1679 | /* |
| 1680 | * If called by the MMU notifier, we have to adjust the pinned |
| 1681 | * page count ourselves. |
| 1682 | */ |
Mitko Haralanov | f19bd64 | 2016-04-12 10:45:57 -0700 | [diff] [blame] | 1683 | if (mm) |
| 1684 | mm->pinned_vm -= node->npages; |
Mitko Haralanov | 5cd3a88d | 2016-03-08 11:15:22 -0800 | [diff] [blame] | 1685 | kfree(node); |
| 1686 | } |
| 1687 | |
| 1688 | static int sdma_rb_invalidate(struct rb_root *root, struct mmu_rb_node *mnode) |
| 1689 | { |
| 1690 | struct sdma_mmu_node *node = |
| 1691 | container_of(mnode, struct sdma_mmu_node, rb); |
| 1692 | |
| 1693 | if (!atomic_read(&node->refcount)) |
| 1694 | return 1; |
| 1695 | return 0; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1696 | } |