1 /* 2 * Copyright (c) 2016 Hisilicon Limited. 3 * 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: 9 * 10 * Redistribution and use in source and binary forms, with or 11 * without modification, are permitted provided that the following 12 * conditions are met: 13 * 14 * - Redistributions of source code must retain the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer. 17 * 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. 22 * 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 30 * SOFTWARE. 31 */ 32 33 #include <linux/platform_device.h> 34 #include <linux/pci.h> 35 #include <uapi/rdma/hns-abi.h> 36 #include "hns_roce_device.h" 37 38 static int hns_roce_pd_alloc(struct hns_roce_dev *hr_dev, unsigned long *pdn) 39 { 40 return hns_roce_bitmap_alloc(&hr_dev->pd_bitmap, pdn) ? -ENOMEM : 0; 41 } 42 43 static void hns_roce_pd_free(struct hns_roce_dev *hr_dev, unsigned long pdn) 44 { 45 hns_roce_bitmap_free(&hr_dev->pd_bitmap, pdn, BITMAP_NO_RR); 46 } 47 48 int hns_roce_init_pd_table(struct hns_roce_dev *hr_dev) 49 { 50 return hns_roce_bitmap_init(&hr_dev->pd_bitmap, hr_dev->caps.num_pds, 51 hr_dev->caps.num_pds - 1, 52 hr_dev->caps.reserved_pds, 0); 53 } 54 55 void hns_roce_cleanup_pd_table(struct hns_roce_dev *hr_dev) 56 { 57 hns_roce_bitmap_cleanup(&hr_dev->pd_bitmap); 58 } 59 60 struct ib_pd *hns_roce_alloc_pd(struct ib_device *ib_dev, 61 struct ib_ucontext *context, 62 struct ib_udata *udata) 63 { 64 struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev); 65 struct device *dev = hr_dev->dev; 66 struct hns_roce_pd *pd; 67 int ret; 68 69 pd = kmalloc(sizeof(*pd), GFP_KERNEL); 70 if (!pd) 71 return ERR_PTR(-ENOMEM); 72 73 ret = hns_roce_pd_alloc(to_hr_dev(ib_dev), &pd->pdn); 74 if (ret) { 75 kfree(pd); 76 dev_err(dev, "[alloc_pd]hns_roce_pd_alloc failed!\n"); 77 return ERR_PTR(ret); 78 } 79 80 if (context) { 81 struct hns_roce_ib_alloc_pd_resp uresp = {.pdn = pd->pdn}; 82 83 if (ib_copy_to_udata(udata, &uresp, sizeof(uresp))) { 84 hns_roce_pd_free(to_hr_dev(ib_dev), pd->pdn); 85 dev_err(dev, "[alloc_pd]ib_copy_to_udata failed!\n"); 86 kfree(pd); 87 return ERR_PTR(-EFAULT); 88 } 89 } 90 91 return &pd->ibpd; 92 } 93 EXPORT_SYMBOL_GPL(hns_roce_alloc_pd); 94 95 int hns_roce_dealloc_pd(struct ib_pd *pd) 96 { 97 hns_roce_pd_free(to_hr_dev(pd->device), to_hr_pd(pd)->pdn); 98 kfree(to_hr_pd(pd)); 99 100 return 0; 101 } 102 EXPORT_SYMBOL_GPL(hns_roce_dealloc_pd); 103 104 int hns_roce_uar_alloc(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar) 105 { 106 struct resource *res; 107 int ret = 0; 108 109 /* Using bitmap to manager UAR index */ 110 ret = hns_roce_bitmap_alloc(&hr_dev->uar_table.bitmap, &uar->logic_idx); 111 if (ret == -1) 112 return -ENOMEM; 113 114 if (uar->logic_idx > 0 && hr_dev->caps.phy_num_uars > 1) 115 uar->index = (uar->logic_idx - 1) % 116 (hr_dev->caps.phy_num_uars - 1) + 1; 117 else 118 uar->index = 0; 119 120 if (!dev_is_pci(hr_dev->dev)) { 121 res = platform_get_resource(hr_dev->pdev, IORESOURCE_MEM, 0); 122 if (!res) { 123 dev_err(&hr_dev->pdev->dev, "memory resource not found!\n"); 124 return -EINVAL; 125 } 126 uar->pfn = ((res->start) >> PAGE_SHIFT) + uar->index; 127 } else { 128 uar->pfn = ((pci_resource_start(hr_dev->pci_dev, 2)) 129 >> PAGE_SHIFT); 130 } 131 132 return 0; 133 } 134 135 void hns_roce_uar_free(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar) 136 { 137 hns_roce_bitmap_free(&hr_dev->uar_table.bitmap, uar->logic_idx, 138 BITMAP_NO_RR); 139 } 140 141 int hns_roce_init_uar_table(struct hns_roce_dev *hr_dev) 142 { 143 return hns_roce_bitmap_init(&hr_dev->uar_table.bitmap, 144 hr_dev->caps.num_uars, 145 hr_dev->caps.num_uars - 1, 146 hr_dev->caps.reserved_uars, 0); 147 } 148 149 void hns_roce_cleanup_uar_table(struct hns_roce_dev *hr_dev) 150 { 151 hns_roce_bitmap_cleanup(&hr_dev->uar_table.bitmap); 152 } 153