1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
38static 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
43static 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
48int 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
55void hns_roce_cleanup_pd_table(struct hns_roce_dev *hr_dev)
56{
57 hns_roce_bitmap_cleanup(&hr_dev->pd_bitmap);
58}
59
60int hns_roce_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
61{
62 struct ib_device *ib_dev = ibpd->device;
63 struct hns_roce_pd *pd = to_hr_pd(ibpd);
64 int ret;
65
66 ret = hns_roce_pd_alloc(to_hr_dev(ib_dev), &pd->pdn);
67 if (ret) {
68 ibdev_err(ib_dev, "failed to alloc pd, ret = %d\n", ret);
69 return ret;
70 }
71
72 if (udata) {
73 struct hns_roce_ib_alloc_pd_resp uresp = {.pdn = pd->pdn};
74
75 if (ib_copy_to_udata(udata, &uresp, sizeof(uresp))) {
76 hns_roce_pd_free(to_hr_dev(ib_dev), pd->pdn);
77 ibdev_err(ib_dev, "failed to copy to udata\n");
78 return -EFAULT;
79 }
80 }
81
82 return 0;
83}
84
85int hns_roce_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
86{
87 hns_roce_pd_free(to_hr_dev(pd->device), to_hr_pd(pd)->pdn);
88 return 0;
89}
90
91int hns_roce_uar_alloc(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar)
92{
93 struct resource *res;
94 int ret;
95
96
97 ret = hns_roce_bitmap_alloc(&hr_dev->uar_table.bitmap, &uar->logic_idx);
98 if (ret)
99 return -ENOMEM;
100
101 if (uar->logic_idx > 0 && hr_dev->caps.phy_num_uars > 1)
102 uar->index = (uar->logic_idx - 1) %
103 (hr_dev->caps.phy_num_uars - 1) + 1;
104 else
105 uar->index = 0;
106
107 if (!dev_is_pci(hr_dev->dev)) {
108 res = platform_get_resource(hr_dev->pdev, IORESOURCE_MEM, 0);
109 if (!res) {
110 dev_err(&hr_dev->pdev->dev, "memory resource not found!\n");
111 return -EINVAL;
112 }
113 uar->pfn = ((res->start) >> PAGE_SHIFT) + uar->index;
114 } else {
115 uar->pfn = ((pci_resource_start(hr_dev->pci_dev, 2))
116 >> PAGE_SHIFT);
117 }
118
119 return 0;
120}
121
122void hns_roce_uar_free(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar)
123{
124 hns_roce_bitmap_free(&hr_dev->uar_table.bitmap, uar->logic_idx,
125 BITMAP_NO_RR);
126}
127
128int hns_roce_init_uar_table(struct hns_roce_dev *hr_dev)
129{
130 return hns_roce_bitmap_init(&hr_dev->uar_table.bitmap,
131 hr_dev->caps.num_uars,
132 hr_dev->caps.num_uars - 1,
133 hr_dev->caps.reserved_uars, 0);
134}
135
136void hns_roce_cleanup_uar_table(struct hns_roce_dev *hr_dev)
137{
138 hns_roce_bitmap_cleanup(&hr_dev->uar_table.bitmap);
139}
140