blob: 0cd6132b75f04548bc1b47ef3a21aeb366f11838 [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>
34#include "hns_roce_device.h"
35
36static int hns_roce_pd_alloc(struct hns_roce_dev *hr_dev, unsigned long *pdn)
37{
Lijun Oua598c6f2016-09-20 17:06:57 +010038 return hns_roce_bitmap_alloc(&hr_dev->pd_bitmap, pdn);
oulijun9a443532016-07-21 19:06:38 +080039}
40
41static void hns_roce_pd_free(struct hns_roce_dev *hr_dev, unsigned long pdn)
42{
43 hns_roce_bitmap_free(&hr_dev->pd_bitmap, pdn);
44}
45
46int hns_roce_init_pd_table(struct hns_roce_dev *hr_dev)
47{
48 return hns_roce_bitmap_init(&hr_dev->pd_bitmap, hr_dev->caps.num_pds,
49 hr_dev->caps.num_pds - 1,
50 hr_dev->caps.reserved_pds, 0);
51}
52
53void hns_roce_cleanup_pd_table(struct hns_roce_dev *hr_dev)
54{
55 hns_roce_bitmap_cleanup(&hr_dev->pd_bitmap);
56}
57
58struct ib_pd *hns_roce_alloc_pd(struct ib_device *ib_dev,
59 struct ib_ucontext *context,
60 struct ib_udata *udata)
61{
62 struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
63 struct device *dev = &hr_dev->pdev->dev;
64 struct hns_roce_pd *pd;
65 int ret;
66
67 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
68 if (!pd)
69 return ERR_PTR(-ENOMEM);
70
71 ret = hns_roce_pd_alloc(to_hr_dev(ib_dev), &pd->pdn);
72 if (ret) {
73 kfree(pd);
74 dev_err(dev, "[alloc_pd]hns_roce_pd_alloc failed!\n");
75 return ERR_PTR(ret);
76 }
77
78 if (context) {
79 if (ib_copy_to_udata(udata, &pd->pdn, sizeof(u64))) {
80 hns_roce_pd_free(to_hr_dev(ib_dev), pd->pdn);
81 dev_err(dev, "[alloc_pd]ib_copy_to_udata failed!\n");
82 kfree(pd);
83 return ERR_PTR(-EFAULT);
84 }
85 }
86
87 return &pd->ibpd;
88}
89
90int hns_roce_dealloc_pd(struct ib_pd *pd)
91{
92 hns_roce_pd_free(to_hr_dev(pd->device), to_hr_pd(pd)->pdn);
93 kfree(to_hr_pd(pd));
94
95 return 0;
96}
97
98int hns_roce_uar_alloc(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar)
99{
100 struct resource *res;
101 int ret = 0;
102
103 /* Using bitmap to manager UAR index */
104 ret = hns_roce_bitmap_alloc(&hr_dev->uar_table.bitmap, &uar->index);
105 if (ret == -1)
106 return -ENOMEM;
107
Lijun Oub280db52016-09-15 23:48:11 +0100108 if (uar->index > 0)
109 uar->index = (uar->index - 1) %
110 (hr_dev->caps.phy_num_uars - 1) + 1;
oulijun9a443532016-07-21 19:06:38 +0800111
112 res = platform_get_resource(hr_dev->pdev, IORESOURCE_MEM, 0);
113 uar->pfn = ((res->start) >> PAGE_SHIFT) + uar->index;
114
115 return 0;
116}
117
118void hns_roce_uar_free(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar)
119{
120 hns_roce_bitmap_free(&hr_dev->uar_table.bitmap, uar->index);
121}
122
123int hns_roce_init_uar_table(struct hns_roce_dev *hr_dev)
124{
125 return hns_roce_bitmap_init(&hr_dev->uar_table.bitmap,
126 hr_dev->caps.num_uars,
127 hr_dev->caps.num_uars - 1,
128 hr_dev->caps.reserved_uars, 0);
129}
130
131void hns_roce_cleanup_uar_table(struct hns_roce_dev *hr_dev)
132{
133 hns_roce_bitmap_cleanup(&hr_dev->uar_table.bitmap);
134}