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