Bryan O'Sullivan | cef1cce | 2006-03-29 15:23:36 -0800 | [diff] [blame] | 1 | /* |
Bryan O'Sullivan | 759d576 | 2006-07-01 04:35:49 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2006 QLogic, Inc. All rights reserved. |
Bryan O'Sullivan | cef1cce | 2006-03-29 15:23:36 -0800 | [diff] [blame] | 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 <rdma/ib_pack.h> |
| 35 | #include <rdma/ib_smi.h> |
| 36 | |
| 37 | #include "ipath_verbs.h" |
| 38 | |
| 39 | /** |
| 40 | * ipath_get_dma_mr - get a DMA memory region |
| 41 | * @pd: protection domain for this memory region |
| 42 | * @acc: access flags |
| 43 | * |
| 44 | * Returns the memory region on success, otherwise returns an errno. |
| 45 | */ |
| 46 | struct ib_mr *ipath_get_dma_mr(struct ib_pd *pd, int acc) |
| 47 | { |
| 48 | struct ipath_mr *mr; |
| 49 | struct ib_mr *ret; |
| 50 | |
| 51 | mr = kzalloc(sizeof *mr, GFP_KERNEL); |
| 52 | if (!mr) { |
| 53 | ret = ERR_PTR(-ENOMEM); |
| 54 | goto bail; |
| 55 | } |
| 56 | |
| 57 | mr->mr.access_flags = acc; |
| 58 | ret = &mr->ibmr; |
| 59 | |
| 60 | bail: |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | static struct ipath_mr *alloc_mr(int count, |
| 65 | struct ipath_lkey_table *lk_table) |
| 66 | { |
| 67 | struct ipath_mr *mr; |
| 68 | int m, i = 0; |
| 69 | |
| 70 | /* Allocate struct plus pointers to first level page tables. */ |
| 71 | m = (count + IPATH_SEGSZ - 1) / IPATH_SEGSZ; |
| 72 | mr = kmalloc(sizeof *mr + m * sizeof mr->mr.map[0], GFP_KERNEL); |
| 73 | if (!mr) |
| 74 | goto done; |
| 75 | |
| 76 | /* Allocate first level page tables. */ |
| 77 | for (; i < m; i++) { |
| 78 | mr->mr.map[i] = kmalloc(sizeof *mr->mr.map[0], GFP_KERNEL); |
| 79 | if (!mr->mr.map[i]) |
| 80 | goto bail; |
| 81 | } |
| 82 | mr->mr.mapsz = m; |
| 83 | |
| 84 | /* |
| 85 | * ib_reg_phys_mr() will initialize mr->ibmr except for |
| 86 | * lkey and rkey. |
| 87 | */ |
| 88 | if (!ipath_alloc_lkey(lk_table, &mr->mr)) |
| 89 | goto bail; |
| 90 | mr->ibmr.rkey = mr->ibmr.lkey = mr->mr.lkey; |
| 91 | |
| 92 | goto done; |
| 93 | |
| 94 | bail: |
| 95 | while (i) { |
| 96 | i--; |
| 97 | kfree(mr->mr.map[i]); |
| 98 | } |
| 99 | kfree(mr); |
| 100 | mr = NULL; |
| 101 | |
| 102 | done: |
| 103 | return mr; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * ipath_reg_phys_mr - register a physical memory region |
| 108 | * @pd: protection domain for this memory region |
| 109 | * @buffer_list: pointer to the list of physical buffers to register |
| 110 | * @num_phys_buf: the number of physical buffers to register |
| 111 | * @iova_start: the starting address passed over IB which maps to this MR |
| 112 | * |
| 113 | * Returns the memory region on success, otherwise returns an errno. |
| 114 | */ |
| 115 | struct ib_mr *ipath_reg_phys_mr(struct ib_pd *pd, |
| 116 | struct ib_phys_buf *buffer_list, |
| 117 | int num_phys_buf, int acc, u64 *iova_start) |
| 118 | { |
| 119 | struct ipath_mr *mr; |
| 120 | int n, m, i; |
| 121 | struct ib_mr *ret; |
| 122 | |
| 123 | mr = alloc_mr(num_phys_buf, &to_idev(pd->device)->lk_table); |
| 124 | if (mr == NULL) { |
| 125 | ret = ERR_PTR(-ENOMEM); |
| 126 | goto bail; |
| 127 | } |
| 128 | |
| 129 | mr->mr.user_base = *iova_start; |
| 130 | mr->mr.iova = *iova_start; |
| 131 | mr->mr.length = 0; |
| 132 | mr->mr.offset = 0; |
| 133 | mr->mr.access_flags = acc; |
| 134 | mr->mr.max_segs = num_phys_buf; |
| 135 | |
| 136 | m = 0; |
| 137 | n = 0; |
| 138 | for (i = 0; i < num_phys_buf; i++) { |
| 139 | mr->mr.map[m]->segs[n].vaddr = |
| 140 | phys_to_virt(buffer_list[i].addr); |
| 141 | mr->mr.map[m]->segs[n].length = buffer_list[i].size; |
| 142 | mr->mr.length += buffer_list[i].size; |
| 143 | n++; |
| 144 | if (n == IPATH_SEGSZ) { |
| 145 | m++; |
| 146 | n = 0; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | ret = &mr->ibmr; |
| 151 | |
| 152 | bail: |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * ipath_reg_user_mr - register a userspace memory region |
| 158 | * @pd: protection domain for this memory region |
| 159 | * @region: the user memory region |
| 160 | * @mr_access_flags: access flags for this memory region |
| 161 | * @udata: unused by the InfiniPath driver |
| 162 | * |
| 163 | * Returns the memory region on success, otherwise returns an errno. |
| 164 | */ |
| 165 | struct ib_mr *ipath_reg_user_mr(struct ib_pd *pd, struct ib_umem *region, |
| 166 | int mr_access_flags, struct ib_udata *udata) |
| 167 | { |
| 168 | struct ipath_mr *mr; |
| 169 | struct ib_umem_chunk *chunk; |
| 170 | int n, m, i; |
| 171 | struct ib_mr *ret; |
| 172 | |
| 173 | n = 0; |
| 174 | list_for_each_entry(chunk, ®ion->chunk_list, list) |
| 175 | n += chunk->nents; |
| 176 | |
| 177 | mr = alloc_mr(n, &to_idev(pd->device)->lk_table); |
| 178 | if (!mr) { |
| 179 | ret = ERR_PTR(-ENOMEM); |
| 180 | goto bail; |
| 181 | } |
| 182 | |
| 183 | mr->mr.user_base = region->user_base; |
| 184 | mr->mr.iova = region->virt_base; |
| 185 | mr->mr.length = region->length; |
| 186 | mr->mr.offset = region->offset; |
| 187 | mr->mr.access_flags = mr_access_flags; |
| 188 | mr->mr.max_segs = n; |
| 189 | |
| 190 | m = 0; |
| 191 | n = 0; |
| 192 | list_for_each_entry(chunk, ®ion->chunk_list, list) { |
| 193 | for (i = 0; i < chunk->nmap; i++) { |
| 194 | mr->mr.map[m]->segs[n].vaddr = |
| 195 | page_address(chunk->page_list[i].page); |
| 196 | mr->mr.map[m]->segs[n].length = region->page_size; |
| 197 | n++; |
| 198 | if (n == IPATH_SEGSZ) { |
| 199 | m++; |
| 200 | n = 0; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | ret = &mr->ibmr; |
| 205 | |
| 206 | bail: |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * ipath_dereg_mr - unregister and free a memory region |
| 212 | * @ibmr: the memory region to free |
| 213 | * |
| 214 | * Returns 0 on success. |
| 215 | * |
| 216 | * Note that this is called to free MRs created by ipath_get_dma_mr() |
| 217 | * or ipath_reg_user_mr(). |
| 218 | */ |
| 219 | int ipath_dereg_mr(struct ib_mr *ibmr) |
| 220 | { |
| 221 | struct ipath_mr *mr = to_imr(ibmr); |
| 222 | int i; |
| 223 | |
| 224 | ipath_free_lkey(&to_idev(ibmr->device)->lk_table, ibmr->lkey); |
| 225 | i = mr->mr.mapsz; |
| 226 | while (i) { |
| 227 | i--; |
| 228 | kfree(mr->mr.map[i]); |
| 229 | } |
| 230 | kfree(mr); |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * ipath_alloc_fmr - allocate a fast memory region |
| 236 | * @pd: the protection domain for this memory region |
| 237 | * @mr_access_flags: access flags for this memory region |
| 238 | * @fmr_attr: fast memory region attributes |
| 239 | * |
| 240 | * Returns the memory region on success, otherwise returns an errno. |
| 241 | */ |
| 242 | struct ib_fmr *ipath_alloc_fmr(struct ib_pd *pd, int mr_access_flags, |
| 243 | struct ib_fmr_attr *fmr_attr) |
| 244 | { |
| 245 | struct ipath_fmr *fmr; |
| 246 | int m, i = 0; |
| 247 | struct ib_fmr *ret; |
| 248 | |
| 249 | /* Allocate struct plus pointers to first level page tables. */ |
| 250 | m = (fmr_attr->max_pages + IPATH_SEGSZ - 1) / IPATH_SEGSZ; |
| 251 | fmr = kmalloc(sizeof *fmr + m * sizeof fmr->mr.map[0], GFP_KERNEL); |
| 252 | if (!fmr) |
| 253 | goto bail; |
| 254 | |
| 255 | /* Allocate first level page tables. */ |
| 256 | for (; i < m; i++) { |
| 257 | fmr->mr.map[i] = kmalloc(sizeof *fmr->mr.map[0], |
| 258 | GFP_KERNEL); |
| 259 | if (!fmr->mr.map[i]) |
| 260 | goto bail; |
| 261 | } |
| 262 | fmr->mr.mapsz = m; |
| 263 | |
| 264 | /* |
| 265 | * ib_alloc_fmr() will initialize fmr->ibfmr except for lkey & |
| 266 | * rkey. |
| 267 | */ |
| 268 | if (!ipath_alloc_lkey(&to_idev(pd->device)->lk_table, &fmr->mr)) |
| 269 | goto bail; |
| 270 | fmr->ibfmr.rkey = fmr->ibfmr.lkey = fmr->mr.lkey; |
| 271 | /* |
| 272 | * Resources are allocated but no valid mapping (RKEY can't be |
| 273 | * used). |
| 274 | */ |
| 275 | fmr->mr.user_base = 0; |
| 276 | fmr->mr.iova = 0; |
| 277 | fmr->mr.length = 0; |
| 278 | fmr->mr.offset = 0; |
| 279 | fmr->mr.access_flags = mr_access_flags; |
| 280 | fmr->mr.max_segs = fmr_attr->max_pages; |
| 281 | fmr->page_shift = fmr_attr->page_shift; |
| 282 | |
| 283 | ret = &fmr->ibfmr; |
| 284 | goto done; |
| 285 | |
| 286 | bail: |
| 287 | while (i) |
| 288 | kfree(fmr->mr.map[--i]); |
| 289 | kfree(fmr); |
| 290 | ret = ERR_PTR(-ENOMEM); |
| 291 | |
| 292 | done: |
| 293 | return ret; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * ipath_map_phys_fmr - set up a fast memory region |
| 298 | * @ibmfr: the fast memory region to set up |
| 299 | * @page_list: the list of pages to associate with the fast memory region |
| 300 | * @list_len: the number of pages to associate with the fast memory region |
| 301 | * @iova: the virtual address of the start of the fast memory region |
| 302 | * |
| 303 | * This may be called from interrupt context. |
| 304 | */ |
| 305 | |
| 306 | int ipath_map_phys_fmr(struct ib_fmr *ibfmr, u64 * page_list, |
| 307 | int list_len, u64 iova) |
| 308 | { |
| 309 | struct ipath_fmr *fmr = to_ifmr(ibfmr); |
| 310 | struct ipath_lkey_table *rkt; |
| 311 | unsigned long flags; |
| 312 | int m, n, i; |
| 313 | u32 ps; |
| 314 | int ret; |
| 315 | |
| 316 | if (list_len > fmr->mr.max_segs) { |
| 317 | ret = -EINVAL; |
| 318 | goto bail; |
| 319 | } |
| 320 | rkt = &to_idev(ibfmr->device)->lk_table; |
| 321 | spin_lock_irqsave(&rkt->lock, flags); |
| 322 | fmr->mr.user_base = iova; |
| 323 | fmr->mr.iova = iova; |
| 324 | ps = 1 << fmr->page_shift; |
| 325 | fmr->mr.length = list_len * ps; |
| 326 | m = 0; |
| 327 | n = 0; |
| 328 | ps = 1 << fmr->page_shift; |
| 329 | for (i = 0; i < list_len; i++) { |
| 330 | fmr->mr.map[m]->segs[n].vaddr = phys_to_virt(page_list[i]); |
| 331 | fmr->mr.map[m]->segs[n].length = ps; |
| 332 | if (++n == IPATH_SEGSZ) { |
| 333 | m++; |
| 334 | n = 0; |
| 335 | } |
| 336 | } |
| 337 | spin_unlock_irqrestore(&rkt->lock, flags); |
| 338 | ret = 0; |
| 339 | |
| 340 | bail: |
| 341 | return ret; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * ipath_unmap_fmr - unmap fast memory regions |
| 346 | * @fmr_list: the list of fast memory regions to unmap |
| 347 | * |
| 348 | * Returns 0 on success. |
| 349 | */ |
| 350 | int ipath_unmap_fmr(struct list_head *fmr_list) |
| 351 | { |
| 352 | struct ipath_fmr *fmr; |
| 353 | struct ipath_lkey_table *rkt; |
| 354 | unsigned long flags; |
| 355 | |
| 356 | list_for_each_entry(fmr, fmr_list, ibfmr.list) { |
| 357 | rkt = &to_idev(fmr->ibfmr.device)->lk_table; |
| 358 | spin_lock_irqsave(&rkt->lock, flags); |
| 359 | fmr->mr.user_base = 0; |
| 360 | fmr->mr.iova = 0; |
| 361 | fmr->mr.length = 0; |
| 362 | spin_unlock_irqrestore(&rkt->lock, flags); |
| 363 | } |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * ipath_dealloc_fmr - deallocate a fast memory region |
| 369 | * @ibfmr: the fast memory region to deallocate |
| 370 | * |
| 371 | * Returns 0 on success. |
| 372 | */ |
| 373 | int ipath_dealloc_fmr(struct ib_fmr *ibfmr) |
| 374 | { |
| 375 | struct ipath_fmr *fmr = to_ifmr(ibfmr); |
| 376 | int i; |
| 377 | |
| 378 | ipath_free_lkey(&to_idev(ibfmr->device)->lk_table, ibfmr->lkey); |
| 379 | i = fmr->mr.mapsz; |
| 380 | while (i) |
| 381 | kfree(fmr->mr.map[--i]); |
| 382 | kfree(fmr); |
| 383 | return 0; |
| 384 | } |