2 * Copyright (c) 2016 Hisilicon Limited.
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
33 #include <linux/platform_device.h>
34 #include "hns_roce_device.h"
36 static int hns_roce_pd_alloc(struct hns_roce_dev *hr_dev, unsigned long *pdn)
38 struct device *dev = &hr_dev->pdev->dev;
39 unsigned long pd_number;
42 ret = hns_roce_bitmap_alloc(&hr_dev->pd_bitmap, &pd_number);
44 dev_err(dev, "alloc pdn from pdbitmap failed\n");
53 static void hns_roce_pd_free(struct hns_roce_dev *hr_dev, unsigned long pdn)
55 hns_roce_bitmap_free(&hr_dev->pd_bitmap, pdn);
58 int hns_roce_init_pd_table(struct hns_roce_dev *hr_dev)
60 return hns_roce_bitmap_init(&hr_dev->pd_bitmap, hr_dev->caps.num_pds,
61 hr_dev->caps.num_pds - 1,
62 hr_dev->caps.reserved_pds, 0);
65 void hns_roce_cleanup_pd_table(struct hns_roce_dev *hr_dev)
67 hns_roce_bitmap_cleanup(&hr_dev->pd_bitmap);
70 struct ib_pd *hns_roce_alloc_pd(struct ib_device *ib_dev,
71 struct ib_ucontext *context,
72 struct ib_udata *udata)
74 struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
75 struct device *dev = &hr_dev->pdev->dev;
76 struct hns_roce_pd *pd;
79 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
81 return ERR_PTR(-ENOMEM);
83 ret = hns_roce_pd_alloc(to_hr_dev(ib_dev), &pd->pdn);
86 dev_err(dev, "[alloc_pd]hns_roce_pd_alloc failed!\n");
91 if (ib_copy_to_udata(udata, &pd->pdn, sizeof(u64))) {
92 hns_roce_pd_free(to_hr_dev(ib_dev), pd->pdn);
93 dev_err(dev, "[alloc_pd]ib_copy_to_udata failed!\n");
95 return ERR_PTR(-EFAULT);
102 int hns_roce_dealloc_pd(struct ib_pd *pd)
104 hns_roce_pd_free(to_hr_dev(pd->device), to_hr_pd(pd)->pdn);
110 int hns_roce_uar_alloc(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar)
112 struct resource *res;
115 /* Using bitmap to manager UAR index */
116 ret = hns_roce_bitmap_alloc(&hr_dev->uar_table.bitmap, &uar->index);
120 uar->index = (uar->index - 1) % hr_dev->caps.phy_num_uars + 1;
122 res = platform_get_resource(hr_dev->pdev, IORESOURCE_MEM, 0);
123 uar->pfn = ((res->start) >> PAGE_SHIFT) + uar->index;
128 void hns_roce_uar_free(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar)
130 hns_roce_bitmap_free(&hr_dev->uar_table.bitmap, uar->index);
133 int hns_roce_init_uar_table(struct hns_roce_dev *hr_dev)
135 return hns_roce_bitmap_init(&hr_dev->uar_table.bitmap,
136 hr_dev->caps.num_uars,
137 hr_dev->caps.num_uars - 1,
138 hr_dev->caps.reserved_uars, 0);
141 void hns_roce_cleanup_uar_table(struct hns_roce_dev *hr_dev)
143 hns_roce_bitmap_cleanup(&hr_dev->uar_table.bitmap);