2 * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include <linux/slab.h>
35 #include <rdma/ib_user_verbs.h>
39 static u32 convert_access(int acc)
41 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX4_PERM_ATOMIC : 0) |
42 (acc & IB_ACCESS_REMOTE_WRITE ? MLX4_PERM_REMOTE_WRITE : 0) |
43 (acc & IB_ACCESS_REMOTE_READ ? MLX4_PERM_REMOTE_READ : 0) |
44 (acc & IB_ACCESS_LOCAL_WRITE ? MLX4_PERM_LOCAL_WRITE : 0) |
45 (acc & IB_ACCESS_MW_BIND ? MLX4_PERM_BIND_MW : 0) |
49 static enum mlx4_mw_type to_mlx4_type(enum ib_mw_type type)
52 case IB_MW_TYPE_1: return MLX4_MW_TYPE_1;
53 case IB_MW_TYPE_2: return MLX4_MW_TYPE_2;
58 struct ib_mr *mlx4_ib_get_dma_mr(struct ib_pd *pd, int acc)
60 struct mlx4_ib_mr *mr;
63 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
65 return ERR_PTR(-ENOMEM);
67 err = mlx4_mr_alloc(to_mdev(pd->device)->dev, to_mpd(pd)->pdn, 0,
68 ~0ull, convert_access(acc), 0, 0, &mr->mmr);
72 err = mlx4_mr_enable(to_mdev(pd->device)->dev, &mr->mmr);
76 mr->ibmr.rkey = mr->ibmr.lkey = mr->mmr.key;
82 (void) mlx4_mr_free(to_mdev(pd->device)->dev, &mr->mmr);
90 int mlx4_ib_umem_write_mtt(struct mlx4_ib_dev *dev, struct mlx4_mtt *mtt,
98 struct scatterlist *sg;
100 pages = (u64 *) __get_free_page(GFP_KERNEL);
106 for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
107 len = sg_dma_len(sg) >> mtt->page_shift;
108 for (k = 0; k < len; ++k) {
109 pages[i++] = sg_dma_address(sg) +
112 * Be friendly to mlx4_write_mtt() and
113 * pass it chunks of appropriate size.
115 if (i == PAGE_SIZE / sizeof (u64)) {
116 err = mlx4_write_mtt(dev->dev, mtt, n,
127 err = mlx4_write_mtt(dev->dev, mtt, n, i, pages);
130 free_page((unsigned long) pages);
134 static struct ib_umem *mlx4_get_umem_mr(struct ib_ucontext *context, u64 start,
135 u64 length, u64 virt_addr,
139 * Force registering the memory as writable if the underlying pages
140 * are writable. This is so rereg can change the access permissions
141 * from readable to writable without having to run through ib_umem_get
144 if (!ib_access_writable(access_flags)) {
145 struct vm_area_struct *vma;
147 down_read(¤t->mm->mmap_sem);
149 * FIXME: Ideally this would iterate over all the vmas that
150 * cover the memory, but for now it requires a single vma to
151 * entirely cover the MR to support RO mappings.
153 vma = find_vma(current->mm, start);
154 if (vma && vma->vm_end >= start + length &&
155 vma->vm_start <= start) {
156 if (vma->vm_flags & VM_WRITE)
157 access_flags |= IB_ACCESS_LOCAL_WRITE;
159 access_flags |= IB_ACCESS_LOCAL_WRITE;
162 up_read(¤t->mm->mmap_sem);
165 return ib_umem_get(context, start, length, access_flags, 0);
168 struct ib_mr *mlx4_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
169 u64 virt_addr, int access_flags,
170 struct ib_udata *udata)
172 struct mlx4_ib_dev *dev = to_mdev(pd->device);
173 struct mlx4_ib_mr *mr;
178 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
180 return ERR_PTR(-ENOMEM);
182 mr->umem = mlx4_get_umem_mr(pd->uobject->context, start, length,
183 virt_addr, access_flags);
184 if (IS_ERR(mr->umem)) {
185 err = PTR_ERR(mr->umem);
189 n = ib_umem_page_count(mr->umem);
190 shift = ilog2(mr->umem->page_size);
192 err = mlx4_mr_alloc(dev->dev, to_mpd(pd)->pdn, virt_addr, length,
193 convert_access(access_flags), n, shift, &mr->mmr);
197 err = mlx4_ib_umem_write_mtt(dev, &mr->mmr.mtt, mr->umem);
201 err = mlx4_mr_enable(dev->dev, &mr->mmr);
205 mr->ibmr.rkey = mr->ibmr.lkey = mr->mmr.key;
210 (void) mlx4_mr_free(to_mdev(pd->device)->dev, &mr->mmr);
213 ib_umem_release(mr->umem);
221 int mlx4_ib_rereg_user_mr(struct ib_mr *mr, int flags,
222 u64 start, u64 length, u64 virt_addr,
223 int mr_access_flags, struct ib_pd *pd,
224 struct ib_udata *udata)
226 struct mlx4_ib_dev *dev = to_mdev(mr->device);
227 struct mlx4_ib_mr *mmr = to_mmr(mr);
228 struct mlx4_mpt_entry *mpt_entry;
229 struct mlx4_mpt_entry **pmpt_entry = &mpt_entry;
232 /* Since we synchronize this call and mlx4_ib_dereg_mr via uverbs,
233 * we assume that the calls can't run concurrently. Otherwise, a
236 err = mlx4_mr_hw_get_mpt(dev->dev, &mmr->mmr, &pmpt_entry);
241 if (flags & IB_MR_REREG_PD) {
242 err = mlx4_mr_hw_change_pd(dev->dev, *pmpt_entry,
246 goto release_mpt_entry;
249 if (flags & IB_MR_REREG_ACCESS) {
250 if (ib_access_writable(mr_access_flags) &&
251 !mmr->umem->writable) {
253 goto release_mpt_entry;
256 err = mlx4_mr_hw_change_access(dev->dev, *pmpt_entry,
257 convert_access(mr_access_flags));
260 goto release_mpt_entry;
263 if (flags & IB_MR_REREG_TRANS) {
267 mlx4_mr_rereg_mem_cleanup(dev->dev, &mmr->mmr);
268 ib_umem_release(mmr->umem);
270 mlx4_get_umem_mr(mr->uobject->context, start, length,
271 virt_addr, mr_access_flags);
272 if (IS_ERR(mmr->umem)) {
273 err = PTR_ERR(mmr->umem);
274 /* Prevent mlx4_ib_dereg_mr from free'ing invalid pointer */
276 goto release_mpt_entry;
278 n = ib_umem_page_count(mmr->umem);
279 shift = ilog2(mmr->umem->page_size);
281 err = mlx4_mr_rereg_mem_write(dev->dev, &mmr->mmr,
282 virt_addr, length, n, shift,
285 ib_umem_release(mmr->umem);
286 goto release_mpt_entry;
288 mmr->mmr.iova = virt_addr;
289 mmr->mmr.size = length;
291 err = mlx4_ib_umem_write_mtt(dev, &mmr->mmr.mtt, mmr->umem);
293 mlx4_mr_rereg_mem_cleanup(dev->dev, &mmr->mmr);
294 ib_umem_release(mmr->umem);
295 goto release_mpt_entry;
299 /* If we couldn't transfer the MR to the HCA, just remember to
300 * return a failure. But dereg_mr will free the resources.
302 err = mlx4_mr_hw_write_mpt(dev->dev, &mmr->mmr, pmpt_entry);
303 if (!err && flags & IB_MR_REREG_ACCESS)
304 mmr->mmr.access = mr_access_flags;
307 mlx4_mr_hw_put_mpt(dev->dev, pmpt_entry);
313 mlx4_alloc_priv_pages(struct ib_device *device,
314 struct mlx4_ib_mr *mr,
319 /* Ensure that size is aligned to DMA cacheline
321 * max_pages is limited to MLX4_MAX_FAST_REG_PAGES
322 * so page_map_size will never cross PAGE_SIZE.
324 mr->page_map_size = roundup(max_pages * sizeof(u64),
325 MLX4_MR_PAGES_ALIGN);
327 /* Prevent cross page boundary allocation. */
328 mr->pages = (__be64 *)get_zeroed_page(GFP_KERNEL);
332 mr->page_map = dma_map_single(device->dma_device, mr->pages,
333 mr->page_map_size, DMA_TO_DEVICE);
335 if (dma_mapping_error(device->dma_device, mr->page_map)) {
343 free_page((unsigned long)mr->pages);
348 mlx4_free_priv_pages(struct mlx4_ib_mr *mr)
351 struct ib_device *device = mr->ibmr.device;
353 dma_unmap_single(device->dma_device, mr->page_map,
354 mr->page_map_size, DMA_TO_DEVICE);
355 free_page((unsigned long)mr->pages);
360 int mlx4_ib_dereg_mr(struct ib_mr *ibmr)
362 struct mlx4_ib_mr *mr = to_mmr(ibmr);
365 mlx4_free_priv_pages(mr);
367 ret = mlx4_mr_free(to_mdev(ibmr->device)->dev, &mr->mmr);
371 ib_umem_release(mr->umem);
377 struct ib_mw *mlx4_ib_alloc_mw(struct ib_pd *pd, enum ib_mw_type type,
378 struct ib_udata *udata)
380 struct mlx4_ib_dev *dev = to_mdev(pd->device);
381 struct mlx4_ib_mw *mw;
384 mw = kmalloc(sizeof(*mw), GFP_KERNEL);
386 return ERR_PTR(-ENOMEM);
388 err = mlx4_mw_alloc(dev->dev, to_mpd(pd)->pdn,
389 to_mlx4_type(type), &mw->mmw);
393 err = mlx4_mw_enable(dev->dev, &mw->mmw);
397 mw->ibmw.rkey = mw->mmw.key;
402 mlx4_mw_free(dev->dev, &mw->mmw);
410 int mlx4_ib_dealloc_mw(struct ib_mw *ibmw)
412 struct mlx4_ib_mw *mw = to_mmw(ibmw);
414 mlx4_mw_free(to_mdev(ibmw->device)->dev, &mw->mmw);
420 struct ib_mr *mlx4_ib_alloc_mr(struct ib_pd *pd,
421 enum ib_mr_type mr_type,
424 struct mlx4_ib_dev *dev = to_mdev(pd->device);
425 struct mlx4_ib_mr *mr;
428 if (mr_type != IB_MR_TYPE_MEM_REG ||
429 max_num_sg > MLX4_MAX_FAST_REG_PAGES)
430 return ERR_PTR(-EINVAL);
432 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
434 return ERR_PTR(-ENOMEM);
436 err = mlx4_mr_alloc(dev->dev, to_mpd(pd)->pdn, 0, 0, 0,
437 max_num_sg, 0, &mr->mmr);
441 err = mlx4_alloc_priv_pages(pd->device, mr, max_num_sg);
445 mr->max_pages = max_num_sg;
446 err = mlx4_mr_enable(dev->dev, &mr->mmr);
450 mr->ibmr.rkey = mr->ibmr.lkey = mr->mmr.key;
456 mr->ibmr.device = pd->device;
457 mlx4_free_priv_pages(mr);
459 (void) mlx4_mr_free(dev->dev, &mr->mmr);
465 struct ib_fmr *mlx4_ib_fmr_alloc(struct ib_pd *pd, int acc,
466 struct ib_fmr_attr *fmr_attr)
468 struct mlx4_ib_dev *dev = to_mdev(pd->device);
469 struct mlx4_ib_fmr *fmr;
472 fmr = kmalloc(sizeof *fmr, GFP_KERNEL);
474 return ERR_PTR(-ENOMEM);
476 err = mlx4_fmr_alloc(dev->dev, to_mpd(pd)->pdn, convert_access(acc),
477 fmr_attr->max_pages, fmr_attr->max_maps,
478 fmr_attr->page_shift, &fmr->mfmr);
482 err = mlx4_fmr_enable(to_mdev(pd->device)->dev, &fmr->mfmr);
486 fmr->ibfmr.rkey = fmr->ibfmr.lkey = fmr->mfmr.mr.key;
491 (void) mlx4_mr_free(to_mdev(pd->device)->dev, &fmr->mfmr.mr);
499 int mlx4_ib_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
500 int npages, u64 iova)
502 struct mlx4_ib_fmr *ifmr = to_mfmr(ibfmr);
503 struct mlx4_ib_dev *dev = to_mdev(ifmr->ibfmr.device);
505 return mlx4_map_phys_fmr(dev->dev, &ifmr->mfmr, page_list, npages, iova,
506 &ifmr->ibfmr.lkey, &ifmr->ibfmr.rkey);
509 int mlx4_ib_unmap_fmr(struct list_head *fmr_list)
511 struct ib_fmr *ibfmr;
513 struct mlx4_dev *mdev = NULL;
515 list_for_each_entry(ibfmr, fmr_list, list) {
516 if (mdev && to_mdev(ibfmr->device)->dev != mdev)
518 mdev = to_mdev(ibfmr->device)->dev;
524 list_for_each_entry(ibfmr, fmr_list, list) {
525 struct mlx4_ib_fmr *ifmr = to_mfmr(ibfmr);
527 mlx4_fmr_unmap(mdev, &ifmr->mfmr, &ifmr->ibfmr.lkey, &ifmr->ibfmr.rkey);
531 * Make sure all MPT status updates are visible before issuing
532 * SYNC_TPT firmware command.
536 err = mlx4_SYNC_TPT(mdev);
538 pr_warn("SYNC_TPT error %d when "
539 "unmapping FMRs\n", err);
544 int mlx4_ib_fmr_dealloc(struct ib_fmr *ibfmr)
546 struct mlx4_ib_fmr *ifmr = to_mfmr(ibfmr);
547 struct mlx4_ib_dev *dev = to_mdev(ibfmr->device);
550 err = mlx4_fmr_free(dev->dev, &ifmr->mfmr);
558 static int mlx4_set_page(struct ib_mr *ibmr, u64 addr)
560 struct mlx4_ib_mr *mr = to_mmr(ibmr);
562 if (unlikely(mr->npages == mr->max_pages))
565 mr->pages[mr->npages++] = cpu_to_be64(addr | MLX4_MTT_FLAG_PRESENT);
570 int mlx4_ib_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
571 unsigned int *sg_offset)
573 struct mlx4_ib_mr *mr = to_mmr(ibmr);
578 ib_dma_sync_single_for_cpu(ibmr->device, mr->page_map,
579 mr->page_map_size, DMA_TO_DEVICE);
581 rc = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, mlx4_set_page);
583 ib_dma_sync_single_for_device(ibmr->device, mr->page_map,
584 mr->page_map_size, DMA_TO_DEVICE);