Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved. |
Jack Morgenstein | 51a379d | 2008-07-25 10:32:52 -0700 | [diff] [blame] | 3 | * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved. |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 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 "mlx4_ib.h" |
| 35 | |
| 36 | static u32 convert_access(int acc) |
| 37 | { |
| 38 | return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX4_PERM_ATOMIC : 0) | |
| 39 | (acc & IB_ACCESS_REMOTE_WRITE ? MLX4_PERM_REMOTE_WRITE : 0) | |
| 40 | (acc & IB_ACCESS_REMOTE_READ ? MLX4_PERM_REMOTE_READ : 0) | |
| 41 | (acc & IB_ACCESS_LOCAL_WRITE ? MLX4_PERM_LOCAL_WRITE : 0) | |
| 42 | MLX4_PERM_LOCAL_READ; |
| 43 | } |
| 44 | |
| 45 | struct ib_mr *mlx4_ib_get_dma_mr(struct ib_pd *pd, int acc) |
| 46 | { |
| 47 | struct mlx4_ib_mr *mr; |
| 48 | int err; |
| 49 | |
| 50 | mr = kmalloc(sizeof *mr, GFP_KERNEL); |
| 51 | if (!mr) |
| 52 | return ERR_PTR(-ENOMEM); |
| 53 | |
| 54 | err = mlx4_mr_alloc(to_mdev(pd->device)->dev, to_mpd(pd)->pdn, 0, |
| 55 | ~0ull, convert_access(acc), 0, 0, &mr->mmr); |
| 56 | if (err) |
| 57 | goto err_free; |
| 58 | |
| 59 | err = mlx4_mr_enable(to_mdev(pd->device)->dev, &mr->mmr); |
| 60 | if (err) |
| 61 | goto err_mr; |
| 62 | |
| 63 | mr->ibmr.rkey = mr->ibmr.lkey = mr->mmr.key; |
| 64 | mr->umem = NULL; |
| 65 | |
| 66 | return &mr->ibmr; |
| 67 | |
| 68 | err_mr: |
| 69 | mlx4_mr_free(to_mdev(pd->device)->dev, &mr->mmr); |
| 70 | |
| 71 | err_free: |
| 72 | kfree(mr); |
| 73 | |
| 74 | return ERR_PTR(err); |
| 75 | } |
| 76 | |
| 77 | int mlx4_ib_umem_write_mtt(struct mlx4_ib_dev *dev, struct mlx4_mtt *mtt, |
| 78 | struct ib_umem *umem) |
| 79 | { |
| 80 | u64 *pages; |
| 81 | struct ib_umem_chunk *chunk; |
| 82 | int i, j, k; |
| 83 | int n; |
| 84 | int len; |
| 85 | int err = 0; |
| 86 | |
| 87 | pages = (u64 *) __get_free_page(GFP_KERNEL); |
| 88 | if (!pages) |
| 89 | return -ENOMEM; |
| 90 | |
| 91 | i = n = 0; |
| 92 | |
| 93 | list_for_each_entry(chunk, &umem->chunk_list, list) |
| 94 | for (j = 0; j < chunk->nmap; ++j) { |
| 95 | len = sg_dma_len(&chunk->page_list[j]) >> mtt->page_shift; |
| 96 | for (k = 0; k < len; ++k) { |
| 97 | pages[i++] = sg_dma_address(&chunk->page_list[j]) + |
| 98 | umem->page_size * k; |
| 99 | /* |
Jack Morgenstein | d7bb58f | 2007-08-01 12:28:53 +0300 | [diff] [blame] | 100 | * Be friendly to mlx4_write_mtt() and |
| 101 | * pass it chunks of appropriate size. |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 102 | */ |
Jack Morgenstein | d7bb58f | 2007-08-01 12:28:53 +0300 | [diff] [blame] | 103 | if (i == PAGE_SIZE / sizeof (u64)) { |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 104 | err = mlx4_write_mtt(dev->dev, mtt, n, |
| 105 | i, pages); |
| 106 | if (err) |
| 107 | goto out; |
| 108 | n += i; |
| 109 | i = 0; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if (i) |
| 115 | err = mlx4_write_mtt(dev->dev, mtt, n, i, pages); |
| 116 | |
| 117 | out: |
| 118 | free_page((unsigned long) pages); |
| 119 | return err; |
| 120 | } |
| 121 | |
| 122 | struct ib_mr *mlx4_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length, |
| 123 | u64 virt_addr, int access_flags, |
| 124 | struct ib_udata *udata) |
| 125 | { |
| 126 | struct mlx4_ib_dev *dev = to_mdev(pd->device); |
| 127 | struct mlx4_ib_mr *mr; |
| 128 | int shift; |
| 129 | int err; |
| 130 | int n; |
| 131 | |
| 132 | mr = kmalloc(sizeof *mr, GFP_KERNEL); |
| 133 | if (!mr) |
| 134 | return ERR_PTR(-ENOMEM); |
| 135 | |
Arthur Kepner | cb9fbc5 | 2008-04-29 01:00:34 -0700 | [diff] [blame] | 136 | mr->umem = ib_umem_get(pd->uobject->context, start, length, |
| 137 | access_flags, 0); |
Roland Dreier | 225c7b1 | 2007-05-08 18:00:38 -0700 | [diff] [blame] | 138 | if (IS_ERR(mr->umem)) { |
| 139 | err = PTR_ERR(mr->umem); |
| 140 | goto err_free; |
| 141 | } |
| 142 | |
| 143 | n = ib_umem_page_count(mr->umem); |
| 144 | shift = ilog2(mr->umem->page_size); |
| 145 | |
| 146 | err = mlx4_mr_alloc(dev->dev, to_mpd(pd)->pdn, virt_addr, length, |
| 147 | convert_access(access_flags), n, shift, &mr->mmr); |
| 148 | if (err) |
| 149 | goto err_umem; |
| 150 | |
| 151 | err = mlx4_ib_umem_write_mtt(dev, &mr->mmr.mtt, mr->umem); |
| 152 | if (err) |
| 153 | goto err_mr; |
| 154 | |
| 155 | err = mlx4_mr_enable(dev->dev, &mr->mmr); |
| 156 | if (err) |
| 157 | goto err_mr; |
| 158 | |
| 159 | mr->ibmr.rkey = mr->ibmr.lkey = mr->mmr.key; |
| 160 | |
| 161 | return &mr->ibmr; |
| 162 | |
| 163 | err_mr: |
| 164 | mlx4_mr_free(to_mdev(pd->device)->dev, &mr->mmr); |
| 165 | |
| 166 | err_umem: |
| 167 | ib_umem_release(mr->umem); |
| 168 | |
| 169 | err_free: |
| 170 | kfree(mr); |
| 171 | |
| 172 | return ERR_PTR(err); |
| 173 | } |
| 174 | |
| 175 | int mlx4_ib_dereg_mr(struct ib_mr *ibmr) |
| 176 | { |
| 177 | struct mlx4_ib_mr *mr = to_mmr(ibmr); |
| 178 | |
| 179 | mlx4_mr_free(to_mdev(ibmr->device)->dev, &mr->mmr); |
| 180 | if (mr->umem) |
| 181 | ib_umem_release(mr->umem); |
| 182 | kfree(mr); |
| 183 | |
| 184 | return 0; |
| 185 | } |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 186 | |
Roland Dreier | 95d04f0 | 2008-07-23 08:12:26 -0700 | [diff] [blame] | 187 | struct ib_mr *mlx4_ib_alloc_fast_reg_mr(struct ib_pd *pd, |
| 188 | int max_page_list_len) |
| 189 | { |
| 190 | struct mlx4_ib_dev *dev = to_mdev(pd->device); |
| 191 | struct mlx4_ib_mr *mr; |
| 192 | int err; |
| 193 | |
| 194 | mr = kmalloc(sizeof *mr, GFP_KERNEL); |
| 195 | if (!mr) |
| 196 | return ERR_PTR(-ENOMEM); |
| 197 | |
| 198 | err = mlx4_mr_alloc(dev->dev, to_mpd(pd)->pdn, 0, 0, 0, |
| 199 | max_page_list_len, 0, &mr->mmr); |
| 200 | if (err) |
| 201 | goto err_free; |
| 202 | |
| 203 | err = mlx4_mr_enable(dev->dev, &mr->mmr); |
| 204 | if (err) |
| 205 | goto err_mr; |
| 206 | |
Vladimir Sokolovsky | 4c246ed | 2008-08-27 14:29:57 -0700 | [diff] [blame] | 207 | mr->ibmr.rkey = mr->ibmr.lkey = mr->mmr.key; |
| 208 | |
Roland Dreier | 95d04f0 | 2008-07-23 08:12:26 -0700 | [diff] [blame] | 209 | return &mr->ibmr; |
| 210 | |
| 211 | err_mr: |
| 212 | mlx4_mr_free(dev->dev, &mr->mmr); |
| 213 | |
| 214 | err_free: |
| 215 | kfree(mr); |
| 216 | return ERR_PTR(err); |
| 217 | } |
| 218 | |
| 219 | struct ib_fast_reg_page_list *mlx4_ib_alloc_fast_reg_page_list(struct ib_device *ibdev, |
| 220 | int page_list_len) |
| 221 | { |
| 222 | struct mlx4_ib_dev *dev = to_mdev(ibdev); |
| 223 | struct mlx4_ib_fast_reg_page_list *mfrpl; |
| 224 | int size = page_list_len * sizeof (u64); |
| 225 | |
| 226 | if (size > PAGE_SIZE) |
| 227 | return ERR_PTR(-EINVAL); |
| 228 | |
| 229 | mfrpl = kmalloc(sizeof *mfrpl, GFP_KERNEL); |
| 230 | if (!mfrpl) |
| 231 | return ERR_PTR(-ENOMEM); |
| 232 | |
| 233 | mfrpl->ibfrpl.page_list = dma_alloc_coherent(&dev->dev->pdev->dev, |
| 234 | size, &mfrpl->map, |
| 235 | GFP_KERNEL); |
| 236 | if (!mfrpl->ibfrpl.page_list) |
| 237 | goto err_free; |
| 238 | |
| 239 | WARN_ON(mfrpl->map & 0x3f); |
| 240 | |
| 241 | return &mfrpl->ibfrpl; |
| 242 | |
| 243 | err_free: |
| 244 | kfree(mfrpl); |
| 245 | return ERR_PTR(-ENOMEM); |
| 246 | } |
| 247 | |
| 248 | void mlx4_ib_free_fast_reg_page_list(struct ib_fast_reg_page_list *page_list) |
| 249 | { |
| 250 | struct mlx4_ib_dev *dev = to_mdev(page_list->device); |
| 251 | struct mlx4_ib_fast_reg_page_list *mfrpl = to_mfrpl(page_list); |
| 252 | int size = page_list->max_page_list_len * sizeof (u64); |
| 253 | |
| 254 | dma_free_coherent(&dev->dev->pdev->dev, size, page_list->page_list, |
| 255 | mfrpl->map); |
| 256 | kfree(mfrpl); |
| 257 | } |
| 258 | |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 259 | struct ib_fmr *mlx4_ib_fmr_alloc(struct ib_pd *pd, int acc, |
| 260 | struct ib_fmr_attr *fmr_attr) |
| 261 | { |
| 262 | struct mlx4_ib_dev *dev = to_mdev(pd->device); |
| 263 | struct mlx4_ib_fmr *fmr; |
| 264 | int err = -ENOMEM; |
| 265 | |
| 266 | fmr = kmalloc(sizeof *fmr, GFP_KERNEL); |
| 267 | if (!fmr) |
| 268 | return ERR_PTR(-ENOMEM); |
| 269 | |
| 270 | err = mlx4_fmr_alloc(dev->dev, to_mpd(pd)->pdn, convert_access(acc), |
| 271 | fmr_attr->max_pages, fmr_attr->max_maps, |
| 272 | fmr_attr->page_shift, &fmr->mfmr); |
| 273 | if (err) |
| 274 | goto err_free; |
| 275 | |
Jack Morgenstein | e6028c0 | 2008-02-14 10:39:36 -0800 | [diff] [blame] | 276 | err = mlx4_fmr_enable(to_mdev(pd->device)->dev, &fmr->mfmr); |
Jack Morgenstein | 8ad11fb | 2007-08-01 12:29:05 +0300 | [diff] [blame] | 277 | if (err) |
| 278 | goto err_mr; |
| 279 | |
| 280 | fmr->ibfmr.rkey = fmr->ibfmr.lkey = fmr->mfmr.mr.key; |
| 281 | |
| 282 | return &fmr->ibfmr; |
| 283 | |
| 284 | err_mr: |
| 285 | mlx4_mr_free(to_mdev(pd->device)->dev, &fmr->mfmr.mr); |
| 286 | |
| 287 | err_free: |
| 288 | kfree(fmr); |
| 289 | |
| 290 | return ERR_PTR(err); |
| 291 | } |
| 292 | |
| 293 | int mlx4_ib_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list, |
| 294 | int npages, u64 iova) |
| 295 | { |
| 296 | struct mlx4_ib_fmr *ifmr = to_mfmr(ibfmr); |
| 297 | struct mlx4_ib_dev *dev = to_mdev(ifmr->ibfmr.device); |
| 298 | |
| 299 | return mlx4_map_phys_fmr(dev->dev, &ifmr->mfmr, page_list, npages, iova, |
| 300 | &ifmr->ibfmr.lkey, &ifmr->ibfmr.rkey); |
| 301 | } |
| 302 | |
| 303 | int mlx4_ib_unmap_fmr(struct list_head *fmr_list) |
| 304 | { |
| 305 | struct ib_fmr *ibfmr; |
| 306 | int err; |
| 307 | struct mlx4_dev *mdev = NULL; |
| 308 | |
| 309 | list_for_each_entry(ibfmr, fmr_list, list) { |
| 310 | if (mdev && to_mdev(ibfmr->device)->dev != mdev) |
| 311 | return -EINVAL; |
| 312 | mdev = to_mdev(ibfmr->device)->dev; |
| 313 | } |
| 314 | |
| 315 | if (!mdev) |
| 316 | return 0; |
| 317 | |
| 318 | list_for_each_entry(ibfmr, fmr_list, list) { |
| 319 | struct mlx4_ib_fmr *ifmr = to_mfmr(ibfmr); |
| 320 | |
| 321 | mlx4_fmr_unmap(mdev, &ifmr->mfmr, &ifmr->ibfmr.lkey, &ifmr->ibfmr.rkey); |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * Make sure all MPT status updates are visible before issuing |
| 326 | * SYNC_TPT firmware command. |
| 327 | */ |
| 328 | wmb(); |
| 329 | |
| 330 | err = mlx4_SYNC_TPT(mdev); |
| 331 | if (err) |
| 332 | printk(KERN_WARNING "mlx4_ib: SYNC_TPT error %d when " |
| 333 | "unmapping FMRs\n", err); |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | int mlx4_ib_fmr_dealloc(struct ib_fmr *ibfmr) |
| 339 | { |
| 340 | struct mlx4_ib_fmr *ifmr = to_mfmr(ibfmr); |
| 341 | struct mlx4_ib_dev *dev = to_mdev(ibfmr->device); |
| 342 | int err; |
| 343 | |
| 344 | err = mlx4_fmr_free(dev->dev, &ifmr->mfmr); |
| 345 | |
| 346 | if (!err) |
| 347 | kfree(ifmr); |
| 348 | |
| 349 | return err; |
| 350 | } |