Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006, 2007, 2009 QLogic Corporation. All rights reserved. |
| 3 | * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved. |
| 4 | * |
| 5 | * This software is available to you under a choice of one of two |
| 6 | * licenses. You may choose to be licensed under the terms of the GNU |
| 7 | * General Public License (GPL) Version 2, available from the file |
| 8 | * COPYING in the main directory of this source tree, or the |
| 9 | * OpenIB.org BSD license below: |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or |
| 12 | * without modification, are permitted provided that the following |
| 13 | * conditions are met: |
| 14 | * |
| 15 | * - Redistributions of source code must retain the above |
| 16 | * copyright notice, this list of conditions and the following |
| 17 | * disclaimer. |
| 18 | * |
| 19 | * - Redistributions in binary form must reproduce the above |
| 20 | * copyright notice, this list of conditions and the following |
| 21 | * disclaimer in the documentation and/or other materials |
| 22 | * provided with the distribution. |
| 23 | * |
| 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 25 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 26 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 27 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 28 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 29 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 30 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 31 | * SOFTWARE. |
| 32 | */ |
| 33 | |
| 34 | #include "qib.h" |
| 35 | |
| 36 | /** |
| 37 | * qib_alloc_lkey - allocate an lkey |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 38 | * @mr: memory region that this lkey protects |
Mike Marciniszyn | 6a82649 | 2012-06-27 18:33:12 -0400 | [diff] [blame] | 39 | * @dma_region: 0->normal key, 1->restricted DMA key |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 40 | * |
Mike Marciniszyn | 6a82649 | 2012-06-27 18:33:12 -0400 | [diff] [blame] | 41 | * Returns 0 if successful, otherwise returns -errno. |
| 42 | * |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 43 | * Increments mr reference count as required. |
Mike Marciniszyn | 6a82649 | 2012-06-27 18:33:12 -0400 | [diff] [blame] | 44 | * |
| 45 | * Sets the lkey field mr for non-dma regions. |
| 46 | * |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 47 | */ |
| 48 | |
Mike Marciniszyn | 6a82649 | 2012-06-27 18:33:12 -0400 | [diff] [blame] | 49 | int qib_alloc_lkey(struct qib_mregion *mr, int dma_region) |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 50 | { |
| 51 | unsigned long flags; |
| 52 | u32 r; |
| 53 | u32 n; |
Mike Marciniszyn | 6a82649 | 2012-06-27 18:33:12 -0400 | [diff] [blame] | 54 | int ret = 0; |
| 55 | struct qib_ibdev *dev = to_idev(mr->pd->device); |
| 56 | struct qib_lkey_table *rkt = &dev->lk_table; |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 57 | |
| 58 | spin_lock_irqsave(&rkt->lock, flags); |
| 59 | |
Mike Marciniszyn | 6a82649 | 2012-06-27 18:33:12 -0400 | [diff] [blame] | 60 | /* special case for dma_mr lkey == 0 */ |
| 61 | if (dma_region) { |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 62 | struct qib_mregion *tmr; |
| 63 | |
Mike Marciniszyn | f3bdf34 | 2013-05-17 12:40:32 +0000 | [diff] [blame] | 64 | tmr = rcu_access_pointer(dev->dma_mr); |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 65 | if (!tmr) { |
Mike Marciniszyn | 6a82649 | 2012-06-27 18:33:12 -0400 | [diff] [blame] | 66 | qib_get_mr(mr); |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 67 | rcu_assign_pointer(dev->dma_mr, mr); |
Mike Marciniszyn | 6a82649 | 2012-06-27 18:33:12 -0400 | [diff] [blame] | 68 | mr->lkey_published = 1; |
| 69 | } |
| 70 | goto success; |
| 71 | } |
| 72 | |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 73 | /* Find the next available LKEY */ |
| 74 | r = rkt->next; |
| 75 | n = r; |
| 76 | for (;;) { |
| 77 | if (rkt->table[r] == NULL) |
| 78 | break; |
| 79 | r = (r + 1) & (rkt->max - 1); |
Mike Marciniszyn | 6a82649 | 2012-06-27 18:33:12 -0400 | [diff] [blame] | 80 | if (r == n) |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 81 | goto bail; |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 82 | } |
| 83 | rkt->next = (r + 1) & (rkt->max - 1); |
| 84 | /* |
| 85 | * Make sure lkey is never zero which is reserved to indicate an |
| 86 | * unrestricted LKEY. |
| 87 | */ |
| 88 | rkt->gen++; |
Mike Marciniszyn | d6f1c17 | 2015-07-21 08:36:07 -0400 | [diff] [blame] | 89 | /* |
| 90 | * bits are capped in qib_verbs.c to insure enough bits |
| 91 | * for generation number |
| 92 | */ |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 93 | mr->lkey = (r << (32 - ib_qib_lkey_table_size)) | |
| 94 | ((((1 << (24 - ib_qib_lkey_table_size)) - 1) & rkt->gen) |
| 95 | << 8); |
| 96 | if (mr->lkey == 0) { |
| 97 | mr->lkey |= 1 << 8; |
| 98 | rkt->gen++; |
| 99 | } |
Mike Marciniszyn | 6a82649 | 2012-06-27 18:33:12 -0400 | [diff] [blame] | 100 | qib_get_mr(mr); |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 101 | rcu_assign_pointer(rkt->table[r], mr); |
Mike Marciniszyn | 6a82649 | 2012-06-27 18:33:12 -0400 | [diff] [blame] | 102 | mr->lkey_published = 1; |
| 103 | success: |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 104 | spin_unlock_irqrestore(&rkt->lock, flags); |
Mike Marciniszyn | 6a82649 | 2012-06-27 18:33:12 -0400 | [diff] [blame] | 105 | out: |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 106 | return ret; |
Mike Marciniszyn | 6a82649 | 2012-06-27 18:33:12 -0400 | [diff] [blame] | 107 | bail: |
| 108 | spin_unlock_irqrestore(&rkt->lock, flags); |
| 109 | ret = -ENOMEM; |
| 110 | goto out; |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /** |
| 114 | * qib_free_lkey - free an lkey |
Mike Marciniszyn | 6a82649 | 2012-06-27 18:33:12 -0400 | [diff] [blame] | 115 | * @mr: mr to free from tables |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 116 | */ |
Mike Marciniszyn | 6a82649 | 2012-06-27 18:33:12 -0400 | [diff] [blame] | 117 | void qib_free_lkey(struct qib_mregion *mr) |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 118 | { |
| 119 | unsigned long flags; |
| 120 | u32 lkey = mr->lkey; |
| 121 | u32 r; |
Mike Marciniszyn | 6a82649 | 2012-06-27 18:33:12 -0400 | [diff] [blame] | 122 | struct qib_ibdev *dev = to_idev(mr->pd->device); |
| 123 | struct qib_lkey_table *rkt = &dev->lk_table; |
| 124 | |
| 125 | spin_lock_irqsave(&rkt->lock, flags); |
| 126 | if (!mr->lkey_published) |
| 127 | goto out; |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 128 | if (lkey == 0) |
Andreea-Cristina Bernat | 590c3fe | 2015-01-16 10:19:59 -0500 | [diff] [blame] | 129 | RCU_INIT_POINTER(dev->dma_mr, NULL); |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 130 | else { |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 131 | r = lkey >> (32 - ib_qib_lkey_table_size); |
Andreea-Cristina Bernat | 590c3fe | 2015-01-16 10:19:59 -0500 | [diff] [blame] | 132 | RCU_INIT_POINTER(rkt->table[r], NULL); |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 133 | } |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 134 | qib_put_mr(mr); |
| 135 | mr->lkey_published = 0; |
Mike Marciniszyn | 6a82649 | 2012-06-27 18:33:12 -0400 | [diff] [blame] | 136 | out: |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 137 | spin_unlock_irqrestore(&rkt->lock, flags); |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
| 141 | * qib_lkey_ok - check IB SGE for validity and initialize |
| 142 | * @rkt: table containing lkey to check SGE against |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 143 | * @pd: protection domain |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 144 | * @isge: outgoing internal SGE |
| 145 | * @sge: SGE to check |
| 146 | * @acc: access flags |
| 147 | * |
| 148 | * Return 1 if valid and successful, otherwise returns 0. |
| 149 | * |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 150 | * increments the reference count upon success |
| 151 | * |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 152 | * Check the IB SGE for validity and initialize our internal version |
| 153 | * of it. |
| 154 | */ |
| 155 | int qib_lkey_ok(struct qib_lkey_table *rkt, struct qib_pd *pd, |
| 156 | struct qib_sge *isge, struct ib_sge *sge, int acc) |
| 157 | { |
| 158 | struct qib_mregion *mr; |
| 159 | unsigned n, m; |
| 160 | size_t off; |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 161 | |
| 162 | /* |
| 163 | * We use LKEY == zero for kernel virtual addresses |
| 164 | * (see qib_get_dma_mr and qib_dma.c). |
| 165 | */ |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 166 | rcu_read_lock(); |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 167 | if (sge->lkey == 0) { |
| 168 | struct qib_ibdev *dev = to_idev(pd->ibpd.device); |
| 169 | |
| 170 | if (pd->user) |
| 171 | goto bail; |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 172 | mr = rcu_dereference(dev->dma_mr); |
| 173 | if (!mr) |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 174 | goto bail; |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 175 | if (unlikely(!atomic_inc_not_zero(&mr->refcount))) |
| 176 | goto bail; |
| 177 | rcu_read_unlock(); |
Mike Marciniszyn | 4db62d4 | 2011-01-10 17:42:23 -0800 | [diff] [blame] | 178 | |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 179 | isge->mr = mr; |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 180 | isge->vaddr = (void *) sge->addr; |
| 181 | isge->length = sge->length; |
| 182 | isge->sge_length = sge->length; |
| 183 | isge->m = 0; |
| 184 | isge->n = 0; |
| 185 | goto ok; |
| 186 | } |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 187 | mr = rcu_dereference( |
| 188 | rkt->table[(sge->lkey >> (32 - ib_qib_lkey_table_size))]); |
| 189 | if (unlikely(!mr || mr->lkey != sge->lkey || mr->pd != &pd->ibpd)) |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 190 | goto bail; |
| 191 | |
| 192 | off = sge->addr - mr->user_base; |
Mike Marciniszyn | c00aaa1 | 2012-09-28 17:33:52 +0000 | [diff] [blame] | 193 | if (unlikely(sge->addr < mr->user_base || |
| 194 | off + sge->length > mr->length || |
| 195 | (mr->access_flags & acc) != acc)) |
Mike Marciniszyn | 4db62d4 | 2011-01-10 17:42:23 -0800 | [diff] [blame] | 196 | goto bail; |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 197 | if (unlikely(!atomic_inc_not_zero(&mr->refcount))) |
| 198 | goto bail; |
| 199 | rcu_read_unlock(); |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 200 | |
| 201 | off += mr->offset; |
Mike Marciniszyn | 2a600f1 | 2011-01-10 17:42:22 -0800 | [diff] [blame] | 202 | if (mr->page_shift) { |
| 203 | /* |
| 204 | page sizes are uniform power of 2 so no loop is necessary |
| 205 | entries_spanned_by_off is the number of times the loop below |
| 206 | would have executed. |
| 207 | */ |
| 208 | size_t entries_spanned_by_off; |
| 209 | |
| 210 | entries_spanned_by_off = off >> mr->page_shift; |
| 211 | off -= (entries_spanned_by_off << mr->page_shift); |
| 212 | m = entries_spanned_by_off/QIB_SEGSZ; |
| 213 | n = entries_spanned_by_off%QIB_SEGSZ; |
| 214 | } else { |
| 215 | m = 0; |
| 216 | n = 0; |
| 217 | while (off >= mr->map[m]->segs[n].length) { |
| 218 | off -= mr->map[m]->segs[n].length; |
| 219 | n++; |
| 220 | if (n >= QIB_SEGSZ) { |
| 221 | m++; |
| 222 | n = 0; |
| 223 | } |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 226 | isge->mr = mr; |
| 227 | isge->vaddr = mr->map[m]->segs[n].vaddr + off; |
| 228 | isge->length = mr->map[m]->segs[n].length - off; |
| 229 | isge->sge_length = sge->length; |
| 230 | isge->m = m; |
| 231 | isge->n = n; |
| 232 | ok: |
Mike Marciniszyn | 4db62d4 | 2011-01-10 17:42:23 -0800 | [diff] [blame] | 233 | return 1; |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 234 | bail: |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 235 | rcu_read_unlock(); |
Mike Marciniszyn | 4db62d4 | 2011-01-10 17:42:23 -0800 | [diff] [blame] | 236 | return 0; |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
| 240 | * qib_rkey_ok - check the IB virtual address, length, and RKEY |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 241 | * @qp: qp for validation |
| 242 | * @sge: SGE state |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 243 | * @len: length of data |
| 244 | * @vaddr: virtual address to place data |
| 245 | * @rkey: rkey to check |
| 246 | * @acc: access flags |
| 247 | * |
| 248 | * Return 1 if successful, otherwise 0. |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 249 | * |
| 250 | * increments the reference count upon success |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 251 | */ |
| 252 | int qib_rkey_ok(struct qib_qp *qp, struct qib_sge *sge, |
| 253 | u32 len, u64 vaddr, u32 rkey, int acc) |
| 254 | { |
| 255 | struct qib_lkey_table *rkt = &to_idev(qp->ibqp.device)->lk_table; |
| 256 | struct qib_mregion *mr; |
| 257 | unsigned n, m; |
| 258 | size_t off; |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 259 | |
| 260 | /* |
| 261 | * We use RKEY == zero for kernel virtual addresses |
| 262 | * (see qib_get_dma_mr and qib_dma.c). |
| 263 | */ |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 264 | rcu_read_lock(); |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 265 | if (rkey == 0) { |
| 266 | struct qib_pd *pd = to_ipd(qp->ibqp.pd); |
| 267 | struct qib_ibdev *dev = to_idev(pd->ibpd.device); |
| 268 | |
| 269 | if (pd->user) |
| 270 | goto bail; |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 271 | mr = rcu_dereference(dev->dma_mr); |
| 272 | if (!mr) |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 273 | goto bail; |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 274 | if (unlikely(!atomic_inc_not_zero(&mr->refcount))) |
| 275 | goto bail; |
| 276 | rcu_read_unlock(); |
Mike Marciniszyn | 4db62d4 | 2011-01-10 17:42:23 -0800 | [diff] [blame] | 277 | |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 278 | sge->mr = mr; |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 279 | sge->vaddr = (void *) vaddr; |
| 280 | sge->length = len; |
| 281 | sge->sge_length = len; |
| 282 | sge->m = 0; |
| 283 | sge->n = 0; |
| 284 | goto ok; |
| 285 | } |
| 286 | |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 287 | mr = rcu_dereference( |
| 288 | rkt->table[(rkey >> (32 - ib_qib_lkey_table_size))]); |
| 289 | if (unlikely(!mr || mr->lkey != rkey || qp->ibqp.pd != mr->pd)) |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 290 | goto bail; |
| 291 | |
| 292 | off = vaddr - mr->iova; |
| 293 | if (unlikely(vaddr < mr->iova || off + len > mr->length || |
| 294 | (mr->access_flags & acc) == 0)) |
Mike Marciniszyn | 4db62d4 | 2011-01-10 17:42:23 -0800 | [diff] [blame] | 295 | goto bail; |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 296 | if (unlikely(!atomic_inc_not_zero(&mr->refcount))) |
| 297 | goto bail; |
| 298 | rcu_read_unlock(); |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 299 | |
| 300 | off += mr->offset; |
Mike Marciniszyn | 2a600f1 | 2011-01-10 17:42:22 -0800 | [diff] [blame] | 301 | if (mr->page_shift) { |
| 302 | /* |
| 303 | page sizes are uniform power of 2 so no loop is necessary |
| 304 | entries_spanned_by_off is the number of times the loop below |
| 305 | would have executed. |
| 306 | */ |
| 307 | size_t entries_spanned_by_off; |
| 308 | |
| 309 | entries_spanned_by_off = off >> mr->page_shift; |
| 310 | off -= (entries_spanned_by_off << mr->page_shift); |
| 311 | m = entries_spanned_by_off/QIB_SEGSZ; |
| 312 | n = entries_spanned_by_off%QIB_SEGSZ; |
| 313 | } else { |
| 314 | m = 0; |
| 315 | n = 0; |
| 316 | while (off >= mr->map[m]->segs[n].length) { |
| 317 | off -= mr->map[m]->segs[n].length; |
| 318 | n++; |
| 319 | if (n >= QIB_SEGSZ) { |
| 320 | m++; |
| 321 | n = 0; |
| 322 | } |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 323 | } |
| 324 | } |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 325 | sge->mr = mr; |
| 326 | sge->vaddr = mr->map[m]->segs[n].vaddr + off; |
| 327 | sge->length = mr->map[m]->segs[n].length - off; |
| 328 | sge->sge_length = len; |
| 329 | sge->m = m; |
| 330 | sge->n = n; |
| 331 | ok: |
Mike Marciniszyn | 4db62d4 | 2011-01-10 17:42:23 -0800 | [diff] [blame] | 332 | return 1; |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 333 | bail: |
Mike Marciniszyn | 8aac4cc | 2012-06-27 18:33:19 -0400 | [diff] [blame] | 334 | rcu_read_unlock(); |
Mike Marciniszyn | 4db62d4 | 2011-01-10 17:42:23 -0800 | [diff] [blame] | 335 | return 0; |
Ralph Campbell | f931551 | 2010-05-23 21:44:54 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /* |
Sagi Grimberg | 38071a4 | 2015-10-13 19:11:31 +0300 | [diff] [blame] | 339 | * Initialize the memory region specified by the work request. |
| 340 | */ |
| 341 | int qib_reg_mr(struct qib_qp *qp, struct ib_reg_wr *wr) |
| 342 | { |
| 343 | struct qib_lkey_table *rkt = &to_idev(qp->ibqp.device)->lk_table; |
| 344 | struct qib_pd *pd = to_ipd(qp->ibqp.pd); |
| 345 | struct qib_mr *mr = to_imr(wr->mr); |
| 346 | struct qib_mregion *mrg; |
| 347 | u32 key = wr->key; |
| 348 | unsigned i, n, m; |
| 349 | int ret = -EINVAL; |
| 350 | unsigned long flags; |
| 351 | u64 *page_list; |
| 352 | size_t ps; |
| 353 | |
| 354 | spin_lock_irqsave(&rkt->lock, flags); |
| 355 | if (pd->user || key == 0) |
| 356 | goto bail; |
| 357 | |
| 358 | mrg = rcu_dereference_protected( |
| 359 | rkt->table[(key >> (32 - ib_qib_lkey_table_size))], |
| 360 | lockdep_is_held(&rkt->lock)); |
| 361 | if (unlikely(mrg == NULL || qp->ibqp.pd != mrg->pd)) |
| 362 | goto bail; |
| 363 | |
| 364 | if (mr->npages > mrg->max_segs) |
| 365 | goto bail; |
| 366 | |
| 367 | ps = mr->ibmr.page_size; |
| 368 | if (mr->ibmr.length > ps * mr->npages) |
| 369 | goto bail; |
| 370 | |
| 371 | mrg->user_base = mr->ibmr.iova; |
| 372 | mrg->iova = mr->ibmr.iova; |
| 373 | mrg->lkey = key; |
| 374 | mrg->length = mr->ibmr.length; |
| 375 | mrg->access_flags = wr->access; |
| 376 | page_list = mr->pages; |
| 377 | m = 0; |
| 378 | n = 0; |
| 379 | for (i = 0; i < mr->npages; i++) { |
| 380 | mrg->map[m]->segs[n].vaddr = (void *) page_list[i]; |
| 381 | mrg->map[m]->segs[n].length = ps; |
| 382 | if (++n == QIB_SEGSZ) { |
| 383 | m++; |
| 384 | n = 0; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | ret = 0; |
| 389 | bail: |
| 390 | spin_unlock_irqrestore(&rkt->lock, flags); |
| 391 | return ret; |
| 392 | } |