hns_roce_pd.c 5.0 KB
Newer Older
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
/*
 * Copyright (c) 2016 Hisilicon Limited.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <linux/platform_device.h>
34
#include <linux/pci.h>
35 36
#include "hns_roce_device.h"

37
void hns_roce_init_pd_table(struct hns_roce_dev *hr_dev)
38
{
39
	struct hns_roce_ida *pd_ida = &hr_dev->pd_ida;
40

41 42 43
	ida_init(&pd_ida->ida);
	pd_ida->max = hr_dev->caps.num_pds - 1;
	pd_ida->min = hr_dev->caps.reserved_pds;
44 45
}

46
int hns_roce_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
47
{
48
	struct ib_device *ib_dev = ibpd->device;
49 50
	struct hns_roce_dev *hr_dev = to_hr_dev(ib_dev);
	struct hns_roce_ida *pd_ida = &hr_dev->pd_ida;
51
	struct hns_roce_pd *pd = to_hr_pd(ibpd);
52 53
	int ret = 0;
	int id;
54

55 56 57 58 59
	id = ida_alloc_range(&pd_ida->ida, pd_ida->min, pd_ida->max,
			     GFP_KERNEL);
	if (id < 0) {
		ibdev_err(ib_dev, "failed to alloc pd, id = %d.\n", id);
		return -ENOMEM;
60
	}
61
	pd->pdn = (unsigned long)id;
62

63
	if (udata) {
64
		struct hns_roce_ib_alloc_pd_resp resp = {.pdn = pd->pdn};
65

66 67 68
		ret = ib_copy_to_udata(udata, &resp,
				       min(udata->outlen, sizeof(resp)));
		if (ret) {
69
			ida_free(&pd_ida->ida, id);
70
			ibdev_err(ib_dev, "failed to copy to udata, ret = %d\n", ret);
71 72 73
		}
	}

74
	return ret;
75 76
}

77
int hns_roce_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
78
{
79 80 81 82
	struct hns_roce_dev *hr_dev = to_hr_dev(pd->device);

	ida_free(&hr_dev->pd_ida.ida, (int)to_hr_pd(pd)->pdn);

83
	return 0;
84 85 86 87
}

int hns_roce_uar_alloc(struct hns_roce_dev *hr_dev, struct hns_roce_uar *uar)
{
88
	struct hns_roce_ida *uar_ida = &hr_dev->uar_ida;
89
	struct resource *res;
90
	int id;
91 92

	/* Using bitmap to manager UAR index */
93 94 95 96
	id = ida_alloc_range(&uar_ida->ida, uar_ida->min, uar_ida->max,
			     GFP_KERNEL);
	if (id < 0) {
		ibdev_err(&hr_dev->ib_dev, "failed to alloc uar id(%d).\n", id);
97
		return -ENOMEM;
98 99
	}
	uar->logic_idx = (unsigned long)id;
100

101 102
	if (uar->logic_idx > 0 && hr_dev->caps.phy_num_uars > 1)
		uar->index = (uar->logic_idx - 1) %
103
			     (hr_dev->caps.phy_num_uars - 1) + 1;
104 105
	else
		uar->index = 0;
106

107 108 109
	if (!dev_is_pci(hr_dev->dev)) {
		res = platform_get_resource(hr_dev->pdev, IORESOURCE_MEM, 0);
		if (!res) {
110
			ida_free(&uar_ida->ida, id);
111 112 113 114 115 116 117
			dev_err(&hr_dev->pdev->dev, "memory resource not found!\n");
			return -EINVAL;
		}
		uar->pfn = ((res->start) >> PAGE_SHIFT) + uar->index;
	} else {
		uar->pfn = ((pci_resource_start(hr_dev->pci_dev, 2))
			   >> PAGE_SHIFT);
118
	}
119 120 121 122

	return 0;
}

123
void hns_roce_init_uar_table(struct hns_roce_dev *hr_dev)
124
{
125
	struct hns_roce_ida *uar_ida = &hr_dev->uar_ida;
126

127 128 129
	ida_init(&uar_ida->ida);
	uar_ida->max = hr_dev->caps.num_uars - 1;
	uar_ida->min = hr_dev->caps.reserved_uars;
130
}
131 132 133

static int hns_roce_xrcd_alloc(struct hns_roce_dev *hr_dev, u32 *xrcdn)
{
134 135
	struct hns_roce_ida *xrcd_ida = &hr_dev->xrcd_ida;
	int id;
136

137 138 139 140 141 142 143
	id = ida_alloc_range(&xrcd_ida->ida, xrcd_ida->min, xrcd_ida->max,
			     GFP_KERNEL);
	if (id < 0) {
		ibdev_err(&hr_dev->ib_dev, "failed to alloc xrcdn(%d).\n", id);
		return -ENOMEM;
	}
	*xrcdn = (u32)id;
144 145

	return 0;
146 147
}

148
void hns_roce_init_xrcd_table(struct hns_roce_dev *hr_dev)
149
{
150
	struct hns_roce_ida *xrcd_ida = &hr_dev->xrcd_ida;
151

152 153 154
	ida_init(&xrcd_ida->ida);
	xrcd_ida->max = hr_dev->caps.num_xrcds - 1;
	xrcd_ida->min = hr_dev->caps.reserved_xrcds;
155 156 157 158 159 160 161 162 163 164 165 166
}

int hns_roce_alloc_xrcd(struct ib_xrcd *ib_xrcd, struct ib_udata *udata)
{
	struct hns_roce_dev *hr_dev = to_hr_dev(ib_xrcd->device);
	struct hns_roce_xrcd *xrcd = to_hr_xrcd(ib_xrcd);
	int ret;

	if (!(hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_XRC))
		return -EINVAL;

	ret = hns_roce_xrcd_alloc(hr_dev, &xrcd->xrcdn);
167
	if (ret)
168 169 170 171 172 173 174
		return ret;

	return 0;
}

int hns_roce_dealloc_xrcd(struct ib_xrcd *ib_xrcd, struct ib_udata *udata)
{
175 176 177 178
	struct hns_roce_dev *hr_dev = to_hr_dev(ib_xrcd->device);
	u32 xrcdn = to_hr_xrcd(ib_xrcd)->xrcdn;

	ida_free(&hr_dev->xrcd_ida.ida, (int)xrcdn);
179 180 181

	return 0;
}