Path: blob/master/drivers/infiniband/hw/ipath/ipath_mr.c
15112 views
/*1* Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.2* Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.3*4* This software is available to you under a choice of one of two5* licenses. You may choose to be licensed under the terms of the GNU6* General Public License (GPL) Version 2, available from the file7* COPYING in the main directory of this source tree, or the8* OpenIB.org BSD license below:9*10* Redistribution and use in source and binary forms, with or11* without modification, are permitted provided that the following12* conditions are met:13*14* - Redistributions of source code must retain the above15* copyright notice, this list of conditions and the following16* disclaimer.17*18* - Redistributions in binary form must reproduce the above19* copyright notice, this list of conditions and the following20* disclaimer in the documentation and/or other materials21* provided with the distribution.22*23* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,24* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF25* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND26* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS27* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN28* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN29* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE30* SOFTWARE.31*/3233#include <linux/slab.h>3435#include <rdma/ib_umem.h>36#include <rdma/ib_pack.h>37#include <rdma/ib_smi.h>3839#include "ipath_verbs.h"4041/* Fast memory region */42struct ipath_fmr {43struct ib_fmr ibfmr;44u8 page_shift;45struct ipath_mregion mr; /* must be last */46};4748static inline struct ipath_fmr *to_ifmr(struct ib_fmr *ibfmr)49{50return container_of(ibfmr, struct ipath_fmr, ibfmr);51}5253/**54* ipath_get_dma_mr - get a DMA memory region55* @pd: protection domain for this memory region56* @acc: access flags57*58* Returns the memory region on success, otherwise returns an errno.59* Note that all DMA addresses should be created via the60* struct ib_dma_mapping_ops functions (see ipath_dma.c).61*/62struct ib_mr *ipath_get_dma_mr(struct ib_pd *pd, int acc)63{64struct ipath_mr *mr;65struct ib_mr *ret;6667mr = kzalloc(sizeof *mr, GFP_KERNEL);68if (!mr) {69ret = ERR_PTR(-ENOMEM);70goto bail;71}7273mr->mr.access_flags = acc;74ret = &mr->ibmr;7576bail:77return ret;78}7980static struct ipath_mr *alloc_mr(int count,81struct ipath_lkey_table *lk_table)82{83struct ipath_mr *mr;84int m, i = 0;8586/* Allocate struct plus pointers to first level page tables. */87m = (count + IPATH_SEGSZ - 1) / IPATH_SEGSZ;88mr = kmalloc(sizeof *mr + m * sizeof mr->mr.map[0], GFP_KERNEL);89if (!mr)90goto done;9192/* Allocate first level page tables. */93for (; i < m; i++) {94mr->mr.map[i] = kmalloc(sizeof *mr->mr.map[0], GFP_KERNEL);95if (!mr->mr.map[i])96goto bail;97}98mr->mr.mapsz = m;99100/*101* ib_reg_phys_mr() will initialize mr->ibmr except for102* lkey and rkey.103*/104if (!ipath_alloc_lkey(lk_table, &mr->mr))105goto bail;106mr->ibmr.rkey = mr->ibmr.lkey = mr->mr.lkey;107108goto done;109110bail:111while (i) {112i--;113kfree(mr->mr.map[i]);114}115kfree(mr);116mr = NULL;117118done:119return mr;120}121122/**123* ipath_reg_phys_mr - register a physical memory region124* @pd: protection domain for this memory region125* @buffer_list: pointer to the list of physical buffers to register126* @num_phys_buf: the number of physical buffers to register127* @iova_start: the starting address passed over IB which maps to this MR128*129* Returns the memory region on success, otherwise returns an errno.130*/131struct ib_mr *ipath_reg_phys_mr(struct ib_pd *pd,132struct ib_phys_buf *buffer_list,133int num_phys_buf, int acc, u64 *iova_start)134{135struct ipath_mr *mr;136int n, m, i;137struct ib_mr *ret;138139mr = alloc_mr(num_phys_buf, &to_idev(pd->device)->lk_table);140if (mr == NULL) {141ret = ERR_PTR(-ENOMEM);142goto bail;143}144145mr->mr.pd = pd;146mr->mr.user_base = *iova_start;147mr->mr.iova = *iova_start;148mr->mr.length = 0;149mr->mr.offset = 0;150mr->mr.access_flags = acc;151mr->mr.max_segs = num_phys_buf;152mr->umem = NULL;153154m = 0;155n = 0;156for (i = 0; i < num_phys_buf; i++) {157mr->mr.map[m]->segs[n].vaddr = (void *) buffer_list[i].addr;158mr->mr.map[m]->segs[n].length = buffer_list[i].size;159mr->mr.length += buffer_list[i].size;160n++;161if (n == IPATH_SEGSZ) {162m++;163n = 0;164}165}166167ret = &mr->ibmr;168169bail:170return ret;171}172173/**174* ipath_reg_user_mr - register a userspace memory region175* @pd: protection domain for this memory region176* @start: starting userspace address177* @length: length of region to register178* @virt_addr: virtual address to use (from HCA's point of view)179* @mr_access_flags: access flags for this memory region180* @udata: unused by the InfiniPath driver181*182* Returns the memory region on success, otherwise returns an errno.183*/184struct ib_mr *ipath_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,185u64 virt_addr, int mr_access_flags,186struct ib_udata *udata)187{188struct ipath_mr *mr;189struct ib_umem *umem;190struct ib_umem_chunk *chunk;191int n, m, i;192struct ib_mr *ret;193194if (length == 0) {195ret = ERR_PTR(-EINVAL);196goto bail;197}198199umem = ib_umem_get(pd->uobject->context, start, length,200mr_access_flags, 0);201if (IS_ERR(umem))202return (void *) umem;203204n = 0;205list_for_each_entry(chunk, &umem->chunk_list, list)206n += chunk->nents;207208mr = alloc_mr(n, &to_idev(pd->device)->lk_table);209if (!mr) {210ret = ERR_PTR(-ENOMEM);211ib_umem_release(umem);212goto bail;213}214215mr->mr.pd = pd;216mr->mr.user_base = start;217mr->mr.iova = virt_addr;218mr->mr.length = length;219mr->mr.offset = umem->offset;220mr->mr.access_flags = mr_access_flags;221mr->mr.max_segs = n;222mr->umem = umem;223224m = 0;225n = 0;226list_for_each_entry(chunk, &umem->chunk_list, list) {227for (i = 0; i < chunk->nents; i++) {228void *vaddr;229230vaddr = page_address(sg_page(&chunk->page_list[i]));231if (!vaddr) {232ret = ERR_PTR(-EINVAL);233goto bail;234}235mr->mr.map[m]->segs[n].vaddr = vaddr;236mr->mr.map[m]->segs[n].length = umem->page_size;237n++;238if (n == IPATH_SEGSZ) {239m++;240n = 0;241}242}243}244ret = &mr->ibmr;245246bail:247return ret;248}249250/**251* ipath_dereg_mr - unregister and free a memory region252* @ibmr: the memory region to free253*254* Returns 0 on success.255*256* Note that this is called to free MRs created by ipath_get_dma_mr()257* or ipath_reg_user_mr().258*/259int ipath_dereg_mr(struct ib_mr *ibmr)260{261struct ipath_mr *mr = to_imr(ibmr);262int i;263264ipath_free_lkey(&to_idev(ibmr->device)->lk_table, ibmr->lkey);265i = mr->mr.mapsz;266while (i) {267i--;268kfree(mr->mr.map[i]);269}270271if (mr->umem)272ib_umem_release(mr->umem);273274kfree(mr);275return 0;276}277278/**279* ipath_alloc_fmr - allocate a fast memory region280* @pd: the protection domain for this memory region281* @mr_access_flags: access flags for this memory region282* @fmr_attr: fast memory region attributes283*284* Returns the memory region on success, otherwise returns an errno.285*/286struct ib_fmr *ipath_alloc_fmr(struct ib_pd *pd, int mr_access_flags,287struct ib_fmr_attr *fmr_attr)288{289struct ipath_fmr *fmr;290int m, i = 0;291struct ib_fmr *ret;292293/* Allocate struct plus pointers to first level page tables. */294m = (fmr_attr->max_pages + IPATH_SEGSZ - 1) / IPATH_SEGSZ;295fmr = kmalloc(sizeof *fmr + m * sizeof fmr->mr.map[0], GFP_KERNEL);296if (!fmr)297goto bail;298299/* Allocate first level page tables. */300for (; i < m; i++) {301fmr->mr.map[i] = kmalloc(sizeof *fmr->mr.map[0],302GFP_KERNEL);303if (!fmr->mr.map[i])304goto bail;305}306fmr->mr.mapsz = m;307308/*309* ib_alloc_fmr() will initialize fmr->ibfmr except for lkey &310* rkey.311*/312if (!ipath_alloc_lkey(&to_idev(pd->device)->lk_table, &fmr->mr))313goto bail;314fmr->ibfmr.rkey = fmr->ibfmr.lkey = fmr->mr.lkey;315/*316* Resources are allocated but no valid mapping (RKEY can't be317* used).318*/319fmr->mr.pd = pd;320fmr->mr.user_base = 0;321fmr->mr.iova = 0;322fmr->mr.length = 0;323fmr->mr.offset = 0;324fmr->mr.access_flags = mr_access_flags;325fmr->mr.max_segs = fmr_attr->max_pages;326fmr->page_shift = fmr_attr->page_shift;327328ret = &fmr->ibfmr;329goto done;330331bail:332while (i)333kfree(fmr->mr.map[--i]);334kfree(fmr);335ret = ERR_PTR(-ENOMEM);336337done:338return ret;339}340341/**342* ipath_map_phys_fmr - set up a fast memory region343* @ibmfr: the fast memory region to set up344* @page_list: the list of pages to associate with the fast memory region345* @list_len: the number of pages to associate with the fast memory region346* @iova: the virtual address of the start of the fast memory region347*348* This may be called from interrupt context.349*/350351int ipath_map_phys_fmr(struct ib_fmr *ibfmr, u64 * page_list,352int list_len, u64 iova)353{354struct ipath_fmr *fmr = to_ifmr(ibfmr);355struct ipath_lkey_table *rkt;356unsigned long flags;357int m, n, i;358u32 ps;359int ret;360361if (list_len > fmr->mr.max_segs) {362ret = -EINVAL;363goto bail;364}365rkt = &to_idev(ibfmr->device)->lk_table;366spin_lock_irqsave(&rkt->lock, flags);367fmr->mr.user_base = iova;368fmr->mr.iova = iova;369ps = 1 << fmr->page_shift;370fmr->mr.length = list_len * ps;371m = 0;372n = 0;373ps = 1 << fmr->page_shift;374for (i = 0; i < list_len; i++) {375fmr->mr.map[m]->segs[n].vaddr = (void *) page_list[i];376fmr->mr.map[m]->segs[n].length = ps;377if (++n == IPATH_SEGSZ) {378m++;379n = 0;380}381}382spin_unlock_irqrestore(&rkt->lock, flags);383ret = 0;384385bail:386return ret;387}388389/**390* ipath_unmap_fmr - unmap fast memory regions391* @fmr_list: the list of fast memory regions to unmap392*393* Returns 0 on success.394*/395int ipath_unmap_fmr(struct list_head *fmr_list)396{397struct ipath_fmr *fmr;398struct ipath_lkey_table *rkt;399unsigned long flags;400401list_for_each_entry(fmr, fmr_list, ibfmr.list) {402rkt = &to_idev(fmr->ibfmr.device)->lk_table;403spin_lock_irqsave(&rkt->lock, flags);404fmr->mr.user_base = 0;405fmr->mr.iova = 0;406fmr->mr.length = 0;407spin_unlock_irqrestore(&rkt->lock, flags);408}409return 0;410}411412/**413* ipath_dealloc_fmr - deallocate a fast memory region414* @ibfmr: the fast memory region to deallocate415*416* Returns 0 on success.417*/418int ipath_dealloc_fmr(struct ib_fmr *ibfmr)419{420struct ipath_fmr *fmr = to_ifmr(ibfmr);421int i;422423ipath_free_lkey(&to_idev(ibfmr->device)->lk_table, ibfmr->lkey);424i = fmr->mr.mapsz;425while (i)426kfree(fmr->mr.map[--i]);427kfree(fmr);428return 0;429}430431432