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