2 * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 static u32 convert_access(int acc)
37 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX4_PERM_ATOMIC : 0) |
38 (acc & IB_ACCESS_REMOTE_WRITE ? MLX4_PERM_REMOTE_WRITE : 0) |
39 (acc & IB_ACCESS_REMOTE_READ ? MLX4_PERM_REMOTE_READ : 0) |
40 (acc & IB_ACCESS_LOCAL_WRITE ? MLX4_PERM_LOCAL_WRITE : 0) |
44 struct ib_mr *mlx4_ib_get_dma_mr(struct ib_pd *pd, int acc)
46 struct mlx4_ib_mr *mr;
49 mr = kmalloc(sizeof *mr, GFP_KERNEL);
51 return ERR_PTR(-ENOMEM);
53 err = mlx4_mr_alloc(to_mdev(pd->device)->dev, to_mpd(pd)->pdn, 0,
54 ~0ull, convert_access(acc), 0, 0, &mr->mmr);
58 err = mlx4_mr_enable(to_mdev(pd->device)->dev, &mr->mmr);
62 mr->ibmr.rkey = mr->ibmr.lkey = mr->mmr.key;
68 mlx4_mr_free(to_mdev(pd->device)->dev, &mr->mmr);
76 int mlx4_ib_umem_write_mtt(struct mlx4_ib_dev *dev, struct mlx4_mtt *mtt,
80 struct ib_umem_chunk *chunk;
86 pages = (u64 *) __get_free_page(GFP_KERNEL);
92 list_for_each_entry(chunk, &umem->chunk_list, list)
93 for (j = 0; j < chunk->nmap; ++j) {
94 len = sg_dma_len(&chunk->page_list[j]) >> mtt->page_shift;
95 for (k = 0; k < len; ++k) {
96 pages[i++] = sg_dma_address(&chunk->page_list[j]) +
99 * Be friendly to mlx4_write_mtt() and
100 * pass it chunks of appropriate size.
102 if (i == PAGE_SIZE / sizeof (u64)) {
103 err = mlx4_write_mtt(dev->dev, mtt, n,
114 err = mlx4_write_mtt(dev->dev, mtt, n, i, pages);
117 free_page((unsigned long) pages);
121 struct ib_mr *mlx4_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
122 u64 virt_addr, int access_flags,
123 struct ib_udata *udata)
125 struct mlx4_ib_dev *dev = to_mdev(pd->device);
126 struct mlx4_ib_mr *mr;
131 mr = kmalloc(sizeof *mr, GFP_KERNEL);
133 return ERR_PTR(-ENOMEM);
135 mr->umem = ib_umem_get(pd->uobject->context, start, length,
137 if (IS_ERR(mr->umem)) {
138 err = PTR_ERR(mr->umem);
142 n = ib_umem_page_count(mr->umem);
143 shift = ilog2(mr->umem->page_size);
145 err = mlx4_mr_alloc(dev->dev, to_mpd(pd)->pdn, virt_addr, length,
146 convert_access(access_flags), n, shift, &mr->mmr);
150 err = mlx4_ib_umem_write_mtt(dev, &mr->mmr.mtt, mr->umem);
154 err = mlx4_mr_enable(dev->dev, &mr->mmr);
158 mr->ibmr.rkey = mr->ibmr.lkey = mr->mmr.key;
163 mlx4_mr_free(to_mdev(pd->device)->dev, &mr->mmr);
166 ib_umem_release(mr->umem);
174 int mlx4_ib_dereg_mr(struct ib_mr *ibmr)
176 struct mlx4_ib_mr *mr = to_mmr(ibmr);
178 mlx4_mr_free(to_mdev(ibmr->device)->dev, &mr->mmr);
180 ib_umem_release(mr->umem);
186 struct ib_mr *mlx4_ib_alloc_fast_reg_mr(struct ib_pd *pd,
187 int max_page_list_len)
189 struct mlx4_ib_dev *dev = to_mdev(pd->device);
190 struct mlx4_ib_mr *mr;
193 mr = kmalloc(sizeof *mr, GFP_KERNEL);
195 return ERR_PTR(-ENOMEM);
197 err = mlx4_mr_alloc(dev->dev, to_mpd(pd)->pdn, 0, 0, 0,
198 max_page_list_len, 0, &mr->mmr);
202 err = mlx4_mr_enable(dev->dev, &mr->mmr);
209 mlx4_mr_free(dev->dev, &mr->mmr);
216 struct ib_fast_reg_page_list *mlx4_ib_alloc_fast_reg_page_list(struct ib_device *ibdev,
219 struct mlx4_ib_dev *dev = to_mdev(ibdev);
220 struct mlx4_ib_fast_reg_page_list *mfrpl;
221 int size = page_list_len * sizeof (u64);
223 if (size > PAGE_SIZE)
224 return ERR_PTR(-EINVAL);
226 mfrpl = kmalloc(sizeof *mfrpl, GFP_KERNEL);
228 return ERR_PTR(-ENOMEM);
230 mfrpl->ibfrpl.page_list = dma_alloc_coherent(&dev->dev->pdev->dev,
233 if (!mfrpl->ibfrpl.page_list)
236 WARN_ON(mfrpl->map & 0x3f);
238 return &mfrpl->ibfrpl;
242 return ERR_PTR(-ENOMEM);
245 void mlx4_ib_free_fast_reg_page_list(struct ib_fast_reg_page_list *page_list)
247 struct mlx4_ib_dev *dev = to_mdev(page_list->device);
248 struct mlx4_ib_fast_reg_page_list *mfrpl = to_mfrpl(page_list);
249 int size = page_list->max_page_list_len * sizeof (u64);
251 dma_free_coherent(&dev->dev->pdev->dev, size, page_list->page_list,
256 struct ib_fmr *mlx4_ib_fmr_alloc(struct ib_pd *pd, int acc,
257 struct ib_fmr_attr *fmr_attr)
259 struct mlx4_ib_dev *dev = to_mdev(pd->device);
260 struct mlx4_ib_fmr *fmr;
263 fmr = kmalloc(sizeof *fmr, GFP_KERNEL);
265 return ERR_PTR(-ENOMEM);
267 err = mlx4_fmr_alloc(dev->dev, to_mpd(pd)->pdn, convert_access(acc),
268 fmr_attr->max_pages, fmr_attr->max_maps,
269 fmr_attr->page_shift, &fmr->mfmr);
273 err = mlx4_fmr_enable(to_mdev(pd->device)->dev, &fmr->mfmr);
277 fmr->ibfmr.rkey = fmr->ibfmr.lkey = fmr->mfmr.mr.key;
282 mlx4_mr_free(to_mdev(pd->device)->dev, &fmr->mfmr.mr);
290 int mlx4_ib_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
291 int npages, u64 iova)
293 struct mlx4_ib_fmr *ifmr = to_mfmr(ibfmr);
294 struct mlx4_ib_dev *dev = to_mdev(ifmr->ibfmr.device);
296 return mlx4_map_phys_fmr(dev->dev, &ifmr->mfmr, page_list, npages, iova,
297 &ifmr->ibfmr.lkey, &ifmr->ibfmr.rkey);
300 int mlx4_ib_unmap_fmr(struct list_head *fmr_list)
302 struct ib_fmr *ibfmr;
304 struct mlx4_dev *mdev = NULL;
306 list_for_each_entry(ibfmr, fmr_list, list) {
307 if (mdev && to_mdev(ibfmr->device)->dev != mdev)
309 mdev = to_mdev(ibfmr->device)->dev;
315 list_for_each_entry(ibfmr, fmr_list, list) {
316 struct mlx4_ib_fmr *ifmr = to_mfmr(ibfmr);
318 mlx4_fmr_unmap(mdev, &ifmr->mfmr, &ifmr->ibfmr.lkey, &ifmr->ibfmr.rkey);
322 * Make sure all MPT status updates are visible before issuing
323 * SYNC_TPT firmware command.
327 err = mlx4_SYNC_TPT(mdev);
329 printk(KERN_WARNING "mlx4_ib: SYNC_TPT error %d when "
330 "unmapping FMRs\n", err);
335 int mlx4_ib_fmr_dealloc(struct ib_fmr *ibfmr)
337 struct mlx4_ib_fmr *ifmr = to_mfmr(ibfmr);
338 struct mlx4_ib_dev *dev = to_mdev(ibfmr->device);
341 err = mlx4_fmr_free(dev->dev, &ifmr->mfmr);