diff options
Diffstat (limited to 'drivers/infiniband/hw/hns/hns_roce_pd.c')
| -rw-r--r-- | drivers/infiniband/hw/hns/hns_roce_pd.c | 167 |
1 files changed, 94 insertions, 73 deletions
diff --git a/drivers/infiniband/hw/hns/hns_roce_pd.c b/drivers/infiniband/hw/hns/hns_roce_pd.c index e11c149da04d..225c3e328e0e 100644 --- a/drivers/infiniband/hw/hns/hns_roce_pd.c +++ b/drivers/infiniband/hw/hns/hns_roce_pd.c @@ -30,86 +30,70 @@ * SOFTWARE. */ -#include <linux/platform_device.h> -#include <linux/pci.h> -#include <uapi/rdma/hns-abi.h> #include "hns_roce_device.h" -static int hns_roce_pd_alloc(struct hns_roce_dev *hr_dev, unsigned long *pdn) +void hns_roce_init_pd_table(struct hns_roce_dev *hr_dev) { - return hns_roce_bitmap_alloc(&hr_dev->pd_bitmap, pdn) ? -ENOMEM : 0; -} - -static void hns_roce_pd_free(struct hns_roce_dev *hr_dev, unsigned long pdn) -{ - hns_roce_bitmap_free(&hr_dev->pd_bitmap, pdn, BITMAP_NO_RR); -} - -int hns_roce_init_pd_table(struct hns_roce_dev *hr_dev) -{ - return hns_roce_bitmap_init(&hr_dev->pd_bitmap, hr_dev->caps.num_pds, - hr_dev->caps.num_pds - 1, - hr_dev->caps.reserved_pds, 0); -} + struct hns_roce_ida *pd_ida = &hr_dev->pd_ida; -void hns_roce_cleanup_pd_table(struct hns_roce_dev *hr_dev) -{ - hns_roce_bitmap_cleanup(&hr_dev->pd_bitmap); + ida_init(&pd_ida->ida); + pd_ida->max = hr_dev->caps.num_pds - 1; + pd_ida->min = hr_dev->caps.reserved_pds; } -struct ib_pd *hns_roce_alloc_pd(struct ib_device *ib_dev, - struct ib_ucontext *context, - struct ib_udata *udata) +int hns_roce_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) { + struct ib_device *ib_dev = ibpd->device; struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev); - struct device *dev = hr_dev->dev; - struct hns_roce_pd *pd; - int ret; - - pd = kmalloc(sizeof(*pd), GFP_KERNEL); - if (!pd) - return ERR_PTR(-ENOMEM); + struct hns_roce_ida *pd_ida = &hr_dev->pd_ida; + struct hns_roce_pd *pd = to_hr_pd(ibpd); + int ret = 0; + int id; - ret = hns_roce_pd_alloc(to_hr_dev(ib_dev), &pd->pdn); - if (ret) { - kfree(pd); - dev_err(dev, "[alloc_pd]hns_roce_pd_alloc failed!\n"); - return ERR_PTR(ret); + id = ida_alloc_range(&pd_ida->ida, pd_ida->min, pd_ida->max, + GFP_KERNEL); + if (id < 0) { + ibdev_err(ib_dev, "failed to alloc pd, id = %d.\n", id); + return -ENOMEM; } + pd->pdn = (unsigned long)id; - if (context) { - struct hns_roce_ib_alloc_pd_resp uresp = {.pdn = pd->pdn}; + if (udata) { + struct hns_roce_ib_alloc_pd_resp resp = {.pdn = pd->pdn}; - if (ib_copy_to_udata(udata, &uresp, sizeof(uresp))) { - hns_roce_pd_free(to_hr_dev(ib_dev), pd->pdn); - dev_err(dev, "[alloc_pd]ib_copy_to_udata failed!\n"); - kfree(pd); - return ERR_PTR(-EFAULT); + ret = ib_copy_to_udata(udata, &resp, + min(udata->outlen, sizeof(resp))); + if (ret) { + ida_free(&pd_ida->ida, id); + ibdev_err(ib_dev, "failed to copy to udata, ret = %d\n", ret); } } - return &pd->ibpd; + return ret; } -EXPORT_SYMBOL_GPL(hns_roce_alloc_pd); -int hns_roce_dealloc_pd(struct ib_pd *pd) +int hns_roce_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata) { - hns_roce_pd_free(to_hr_dev(pd->device), to_hr_pd(pd)->pdn); - kfree(to_hr_pd(pd)); + struct hns_roce_dev *hr_dev = to_hr_dev(pd->device); + + ida_free(&hr_dev->pd_ida.ida, (int)to_hr_pd(pd)->pdn); return 0; } -EXPORT_SYMBOL_GPL(hns_roce_dealloc_pd); int hns_roce_uar_alloc(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar) { - struct resource *res; - int ret = 0; + struct hns_roce_ida *uar_ida = &hr_dev->uar_ida; + int id; /* Using bitmap to manager UAR index */ - ret = hns_roce_bitmap_alloc(&hr_dev->uar_table.bitmap, &uar->logic_idx); - if (ret == -1) + id = ida_alloc_range(&uar_ida->ida, uar_ida->min, uar_ida->max, + GFP_KERNEL); + if (id < 0) { + ibdev_err(&hr_dev->ib_dev, "failed to alloc uar id(%d).\n", id); return -ENOMEM; + } + uar->logic_idx = (unsigned long)id; if (uar->logic_idx > 0 && hr_dev->caps.phy_num_uars > 1) uar->index = (uar->logic_idx - 1) % @@ -117,36 +101,73 @@ int hns_roce_uar_alloc(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar) else uar->index = 0; - if (!dev_is_pci(hr_dev->dev)) { - res = platform_get_resource(hr_dev->pdev, IORESOURCE_MEM, 0); - if (!res) { - dev_err(&hr_dev->pdev->dev, "memory resource not found!\n"); - return -EINVAL; - } - uar->pfn = ((res->start) >> PAGE_SHIFT) + uar->index; - } else { - uar->pfn = ((pci_resource_start(hr_dev->pci_dev, 2)) - >> PAGE_SHIFT); + uar->pfn = ((pci_resource_start(hr_dev->pci_dev, 2)) >> PAGE_SHIFT); + if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_DIRECT_WQE) + hr_dev->dwqe_page = pci_resource_start(hr_dev->pci_dev, 4); + + return 0; +} + +void hns_roce_init_uar_table(struct hns_roce_dev *hr_dev) +{ + struct hns_roce_ida *uar_ida = &hr_dev->uar_ida; + + ida_init(&uar_ida->ida); + uar_ida->max = hr_dev->caps.num_uars - 1; + uar_ida->min = hr_dev->caps.reserved_uars; +} + +static int hns_roce_xrcd_alloc(struct hns_roce_dev *hr_dev, u32 *xrcdn) +{ + struct hns_roce_ida *xrcd_ida = &hr_dev->xrcd_ida; + int id; + + id = ida_alloc_range(&xrcd_ida->ida, xrcd_ida->min, xrcd_ida->max, + GFP_KERNEL); + if (id < 0) { + ibdev_err(&hr_dev->ib_dev, "failed to alloc xrcdn(%d).\n", id); + return -ENOMEM; } + *xrcdn = (u32)id; return 0; } -void hns_roce_uar_free(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar) +void hns_roce_init_xrcd_table(struct hns_roce_dev *hr_dev) { - hns_roce_bitmap_free(&hr_dev->uar_table.bitmap, uar->logic_idx, - BITMAP_NO_RR); + struct hns_roce_ida *xrcd_ida = &hr_dev->xrcd_ida; + + ida_init(&xrcd_ida->ida); + xrcd_ida->max = hr_dev->caps.num_xrcds - 1; + xrcd_ida->min = hr_dev->caps.reserved_xrcds; } -int hns_roce_init_uar_table(struct hns_roce_dev *hr_dev) +int hns_roce_alloc_xrcd(struct ib_xrcd *ib_xrcd, struct ib_udata *udata) { - return hns_roce_bitmap_init(&hr_dev->uar_table.bitmap, - hr_dev->caps.num_uars, - hr_dev->caps.num_uars - 1, - hr_dev->caps.reserved_uars, 0); + struct hns_roce_dev *hr_dev = to_hr_dev(ib_xrcd->device); + struct hns_roce_xrcd *xrcd = to_hr_xrcd(ib_xrcd); + int ret; + + if (!(hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_XRC)) { + ret = -EOPNOTSUPP; + goto err_out; + } + + ret = hns_roce_xrcd_alloc(hr_dev, &xrcd->xrcdn); + +err_out: + if (ret) + atomic64_inc(&hr_dev->dfx_cnt[HNS_ROCE_DFX_XRCD_ALLOC_ERR_CNT]); + + return ret; } -void hns_roce_cleanup_uar_table(struct hns_roce_dev *hr_dev) +int hns_roce_dealloc_xrcd(struct ib_xrcd *ib_xrcd, struct ib_udata *udata) { - hns_roce_bitmap_cleanup(&hr_dev->uar_table.bitmap); + struct hns_roce_dev *hr_dev = to_hr_dev(ib_xrcd->device); + u32 xrcdn = to_hr_xrcd(ib_xrcd)->xrcdn; + + ida_free(&hr_dev->xrcd_ida.ida, (int)xrcdn); + + return 0; } |
