blob: bdab2188c04a9ab6277dcb84cb231be5b6388c90 [file] [log] [blame]
oulijun9a443532016-07-21 19:06:38 +08001/*
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>
Wei Hu(Xavier)7afddaf2017-08-30 17:23:11 +080034#include <linux/pci.h>
oulijun9a443532016-07-21 19:06:38 +080035#include "hns_roce_device.h"
36
37static int hns_roce_pd_alloc(struct hns_roce_dev *hr_dev, unsigned long *pdn)
38{
Lijun Oua598c6f2016-09-20 17:06:57 +010039 return hns_roce_bitmap_alloc(&hr_dev->pd_bitmap, pdn);
oulijun9a443532016-07-21 19:06:38 +080040}
41
42static void hns_roce_pd_free(struct hns_roce_dev *hr_dev, unsigned long pdn)
43{
Wei Hu (Xavier)5e6ff782016-11-23 19:41:07 +000044 hns_roce_bitmap_free(&hr_dev->pd_bitmap, pdn, BITMAP_NO_RR);
oulijun9a443532016-07-21 19:06:38 +080045}
46
47int hns_roce_init_pd_table(struct hns_roce_dev *hr_dev)
48{
49 return hns_roce_bitmap_init(&hr_dev->pd_bitmap, hr_dev->caps.num_pds,
50 hr_dev->caps.num_pds - 1,
51 hr_dev->caps.reserved_pds, 0);
52}
53
54void hns_roce_cleanup_pd_table(struct hns_roce_dev *hr_dev)
55{
56 hns_roce_bitmap_cleanup(&hr_dev->pd_bitmap);
57}
58
59struct ib_pd *hns_roce_alloc_pd(struct ib_device *ib_dev,
60 struct ib_ucontext *context,
61 struct ib_udata *udata)
62{
63 struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
Wei Hu(Xavier)13ca9702017-08-30 17:23:02 +080064 struct device *dev = hr_dev->dev;
oulijun9a443532016-07-21 19:06:38 +080065 struct hns_roce_pd *pd;
66 int ret;
67
68 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
69 if (!pd)
70 return ERR_PTR(-ENOMEM);
71
72 ret = hns_roce_pd_alloc(to_hr_dev(ib_dev), &pd->pdn);
73 if (ret) {
74 kfree(pd);
75 dev_err(dev, "[alloc_pd]hns_roce_pd_alloc failed!\n");
76 return ERR_PTR(ret);
77 }
78
79 if (context) {
80 if (ib_copy_to_udata(udata, &pd->pdn, sizeof(u64))) {
81 hns_roce_pd_free(to_hr_dev(ib_dev), pd->pdn);
82 dev_err(dev, "[alloc_pd]ib_copy_to_udata failed!\n");
83 kfree(pd);
84 return ERR_PTR(-EFAULT);
85 }
86 }
87
88 return &pd->ibpd;
89}
Wei Hu(Xavier)08805fd2017-08-30 17:22:59 +080090EXPORT_SYMBOL_GPL(hns_roce_alloc_pd);
oulijun9a443532016-07-21 19:06:38 +080091
92int hns_roce_dealloc_pd(struct ib_pd *pd)
93{
94 hns_roce_pd_free(to_hr_dev(pd->device), to_hr_pd(pd)->pdn);
95 kfree(to_hr_pd(pd));
96
97 return 0;
98}
Wei Hu(Xavier)08805fd2017-08-30 17:22:59 +080099EXPORT_SYMBOL_GPL(hns_roce_dealloc_pd);
oulijun9a443532016-07-21 19:06:38 +0800100
101int hns_roce_uar_alloc(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar)
102{
103 struct resource *res;
104 int ret = 0;
105
106 /* Using bitmap to manager UAR index */
107 ret = hns_roce_bitmap_alloc(&hr_dev->uar_table.bitmap, &uar->index);
108 if (ret == -1)
109 return -ENOMEM;
110
Lijun Oub280db52016-09-15 23:48:11 +0100111 if (uar->index > 0)
112 uar->index = (uar->index - 1) %
113 (hr_dev->caps.phy_num_uars - 1) + 1;
oulijun9a443532016-07-21 19:06:38 +0800114
Wei Hu(Xavier)7afddaf2017-08-30 17:23:11 +0800115 if (!dev_is_pci(hr_dev->dev)) {
116 res = platform_get_resource(hr_dev->pdev, IORESOURCE_MEM, 0);
117 if (!res) {
118 dev_err(&hr_dev->pdev->dev, "memory resource not found!\n");
119 return -EINVAL;
120 }
121 uar->pfn = ((res->start) >> PAGE_SHIFT) + uar->index;
122 } else {
123 uar->pfn = ((pci_resource_start(hr_dev->pci_dev, 2))
124 >> PAGE_SHIFT);
Lijun Ou3e413872016-09-20 17:07:10 +0100125 }
oulijun9a443532016-07-21 19:06:38 +0800126
127 return 0;
128}
129
130void hns_roce_uar_free(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar)
131{
Wei Hu (Xavier)5e6ff782016-11-23 19:41:07 +0000132 hns_roce_bitmap_free(&hr_dev->uar_table.bitmap, uar->index,
133 BITMAP_NO_RR);
oulijun9a443532016-07-21 19:06:38 +0800134}
135
136int hns_roce_init_uar_table(struct hns_roce_dev *hr_dev)
137{
138 return hns_roce_bitmap_init(&hr_dev->uar_table.bitmap,
139 hr_dev->caps.num_uars,
140 hr_dev->caps.num_uars - 1,
141 hr_dev->caps.reserved_uars, 0);
142}
143
144void hns_roce_cleanup_uar_table(struct hns_roce_dev *hr_dev)
145{
146 hns_roce_bitmap_cleanup(&hr_dev->uar_table.bitmap);
147}