xref: /openbmc/linux/drivers/infiniband/hw/usnic/usnic_vnic.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1e3cf00d0SUpinder Malhi /*
2e3cf00d0SUpinder Malhi  * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved.
3e3cf00d0SUpinder Malhi  *
43805eadeSJeff Squyres  * This software is available to you under a choice of one of two
53805eadeSJeff Squyres  * licenses.  You may choose to be licensed under the terms of the GNU
63805eadeSJeff Squyres  * General Public License (GPL) Version 2, available from the file
73805eadeSJeff Squyres  * COPYING in the main directory of this source tree, or the
83805eadeSJeff Squyres  * BSD license below:
93805eadeSJeff Squyres  *
103805eadeSJeff Squyres  *     Redistribution and use in source and binary forms, with or
113805eadeSJeff Squyres  *     without modification, are permitted provided that the following
123805eadeSJeff Squyres  *     conditions are met:
133805eadeSJeff Squyres  *
143805eadeSJeff Squyres  *      - Redistributions of source code must retain the above
153805eadeSJeff Squyres  *        copyright notice, this list of conditions and the following
163805eadeSJeff Squyres  *        disclaimer.
173805eadeSJeff Squyres  *
183805eadeSJeff Squyres  *      - Redistributions in binary form must reproduce the above
193805eadeSJeff Squyres  *        copyright notice, this list of conditions and the following
203805eadeSJeff Squyres  *        disclaimer in the documentation and/or other materials
213805eadeSJeff Squyres  *        provided with the distribution.
22e3cf00d0SUpinder Malhi  *
23e3cf00d0SUpinder Malhi  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24e3cf00d0SUpinder Malhi  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25e3cf00d0SUpinder Malhi  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26e3cf00d0SUpinder Malhi  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27e3cf00d0SUpinder Malhi  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28e3cf00d0SUpinder Malhi  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29e3cf00d0SUpinder Malhi  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30e3cf00d0SUpinder Malhi  * SOFTWARE.
31e3cf00d0SUpinder Malhi  *
32e3cf00d0SUpinder Malhi  */
33e3cf00d0SUpinder Malhi #include <linux/errno.h>
34e3cf00d0SUpinder Malhi #include <linux/pci.h>
35e3cf00d0SUpinder Malhi 
36e3cf00d0SUpinder Malhi #include "usnic_ib.h"
37e3cf00d0SUpinder Malhi #include "vnic_resource.h"
38e3cf00d0SUpinder Malhi #include "usnic_log.h"
39e3cf00d0SUpinder Malhi #include "usnic_vnic.h"
40e3cf00d0SUpinder Malhi 
41e3cf00d0SUpinder Malhi struct usnic_vnic {
42e3cf00d0SUpinder Malhi 	struct vnic_dev			*vdev;
43e3cf00d0SUpinder Malhi 	struct vnic_dev_bar		bar[PCI_NUM_RESOURCES];
44e3cf00d0SUpinder Malhi 	struct usnic_vnic_res_chunk	chunks[USNIC_VNIC_RES_TYPE_MAX];
45e3cf00d0SUpinder Malhi 	spinlock_t			res_lock;
46e3cf00d0SUpinder Malhi };
47e3cf00d0SUpinder Malhi 
_to_vnic_res_type(enum usnic_vnic_res_type res_type)48e3cf00d0SUpinder Malhi static enum vnic_res_type _to_vnic_res_type(enum usnic_vnic_res_type res_type)
49e3cf00d0SUpinder Malhi {
50e3cf00d0SUpinder Malhi #define DEFINE_USNIC_VNIC_RES_AT(usnic_vnic_res_t, vnic_res_type, desc, val) \
51e3cf00d0SUpinder Malhi 		vnic_res_type,
52e3cf00d0SUpinder Malhi #define DEFINE_USNIC_VNIC_RES(usnic_vnic_res_t, vnic_res_type, desc) \
53e3cf00d0SUpinder Malhi 		vnic_res_type,
54e3cf00d0SUpinder Malhi 	static enum vnic_res_type usnic_vnic_type_2_vnic_type[] = {
55e3cf00d0SUpinder Malhi 						USNIC_VNIC_RES_TYPES};
56e3cf00d0SUpinder Malhi #undef DEFINE_USNIC_VNIC_RES
57e3cf00d0SUpinder Malhi #undef DEFINE_USNIC_VNIC_RES_AT
58e3cf00d0SUpinder Malhi 
59e3cf00d0SUpinder Malhi 	if (res_type >= USNIC_VNIC_RES_TYPE_MAX)
60e3cf00d0SUpinder Malhi 		return RES_TYPE_MAX;
61e3cf00d0SUpinder Malhi 
62e3cf00d0SUpinder Malhi 	return usnic_vnic_type_2_vnic_type[res_type];
63e3cf00d0SUpinder Malhi }
64e3cf00d0SUpinder Malhi 
usnic_vnic_res_type_to_str(enum usnic_vnic_res_type res_type)65e3cf00d0SUpinder Malhi const char *usnic_vnic_res_type_to_str(enum usnic_vnic_res_type res_type)
66e3cf00d0SUpinder Malhi {
67e3cf00d0SUpinder Malhi #define DEFINE_USNIC_VNIC_RES_AT(usnic_vnic_res_t, vnic_res_type, desc, val) \
68e3cf00d0SUpinder Malhi 		desc,
69e3cf00d0SUpinder Malhi #define DEFINE_USNIC_VNIC_RES(usnic_vnic_res_t, vnic_res_type, desc) \
70e3cf00d0SUpinder Malhi 		desc,
71e3cf00d0SUpinder Malhi 	static const char * const usnic_vnic_res_type_desc[] = {
72e3cf00d0SUpinder Malhi 						USNIC_VNIC_RES_TYPES};
73e3cf00d0SUpinder Malhi #undef DEFINE_USNIC_VNIC_RES
74e3cf00d0SUpinder Malhi #undef DEFINE_USNIC_VNIC_RES_AT
75e3cf00d0SUpinder Malhi 
76e3cf00d0SUpinder Malhi 	if (res_type >= USNIC_VNIC_RES_TYPE_MAX)
77e3cf00d0SUpinder Malhi 		return "unknown";
78e3cf00d0SUpinder Malhi 
79e3cf00d0SUpinder Malhi 	return usnic_vnic_res_type_desc[res_type];
80e3cf00d0SUpinder Malhi 
81e3cf00d0SUpinder Malhi }
82e3cf00d0SUpinder Malhi 
usnic_vnic_pci_name(struct usnic_vnic * vnic)83e3cf00d0SUpinder Malhi const char *usnic_vnic_pci_name(struct usnic_vnic *vnic)
84e3cf00d0SUpinder Malhi {
85e3cf00d0SUpinder Malhi 	return pci_name(usnic_vnic_get_pdev(vnic));
86e3cf00d0SUpinder Malhi }
87e3cf00d0SUpinder Malhi 
usnic_vnic_dump(struct usnic_vnic * vnic,char * buf,int buf_sz,void * hdr_obj,int (* printtitle)(void *,char *,int),int (* printcols)(char *,int),int (* printrow)(void *,char *,int))88e3cf00d0SUpinder Malhi int usnic_vnic_dump(struct usnic_vnic *vnic, char *buf,
89e3cf00d0SUpinder Malhi 			int buf_sz,
90e3cf00d0SUpinder Malhi 			void *hdr_obj,
91e3cf00d0SUpinder Malhi 			int (*printtitle)(void *, char*, int),
92e3cf00d0SUpinder Malhi 			int (*printcols)(char *, int),
93e3cf00d0SUpinder Malhi 			int (*printrow)(void *, char *, int))
94e3cf00d0SUpinder Malhi {
95e3cf00d0SUpinder Malhi 	struct usnic_vnic_res_chunk *chunk;
96e3cf00d0SUpinder Malhi 	struct usnic_vnic_res *res;
97e3cf00d0SUpinder Malhi 	struct vnic_dev_bar *bar0;
98e3cf00d0SUpinder Malhi 	int i, j, offset;
99e3cf00d0SUpinder Malhi 
100e3cf00d0SUpinder Malhi 	offset = 0;
101e3cf00d0SUpinder Malhi 	bar0 = usnic_vnic_get_bar(vnic, 0);
102e3cf00d0SUpinder Malhi 	offset += scnprintf(buf + offset, buf_sz - offset,
103e3cf00d0SUpinder Malhi 			"VF:%hu BAR0 bus_addr=%pa vaddr=0x%p size=%ld ",
104e3cf00d0SUpinder Malhi 			usnic_vnic_get_index(vnic),
105e3cf00d0SUpinder Malhi 			&bar0->bus_addr,
106e3cf00d0SUpinder Malhi 			bar0->vaddr, bar0->len);
107e3cf00d0SUpinder Malhi 	if (printtitle)
108e3cf00d0SUpinder Malhi 		offset += printtitle(hdr_obj, buf + offset, buf_sz - offset);
109e3cf00d0SUpinder Malhi 	offset += scnprintf(buf + offset, buf_sz - offset, "\n");
110e3cf00d0SUpinder Malhi 	offset += scnprintf(buf + offset, buf_sz - offset,
111e3cf00d0SUpinder Malhi 			"|RES\t|CTRL_PIN\t\t|IN_USE\t");
112e3cf00d0SUpinder Malhi 	if (printcols)
113e3cf00d0SUpinder Malhi 		offset += printcols(buf + offset, buf_sz - offset);
114e3cf00d0SUpinder Malhi 	offset += scnprintf(buf + offset, buf_sz - offset, "\n");
115e3cf00d0SUpinder Malhi 
116e3cf00d0SUpinder Malhi 	spin_lock(&vnic->res_lock);
117e3cf00d0SUpinder Malhi 	for (i = 0; i < ARRAY_SIZE(vnic->chunks); i++) {
118e3cf00d0SUpinder Malhi 		chunk = &vnic->chunks[i];
119e3cf00d0SUpinder Malhi 		for (j = 0; j < chunk->cnt; j++) {
120e3cf00d0SUpinder Malhi 			res = chunk->res[j];
121e3cf00d0SUpinder Malhi 			offset += scnprintf(buf + offset, buf_sz - offset,
122e3cf00d0SUpinder Malhi 					"|%s[%u]\t|0x%p\t|%u\t",
123e3cf00d0SUpinder Malhi 					usnic_vnic_res_type_to_str(res->type),
124e3cf00d0SUpinder Malhi 					res->vnic_idx, res->ctrl, !!res->owner);
125e3cf00d0SUpinder Malhi 			if (printrow) {
126e3cf00d0SUpinder Malhi 				offset += printrow(res->owner, buf + offset,
127e3cf00d0SUpinder Malhi 							buf_sz - offset);
128e3cf00d0SUpinder Malhi 			}
129e3cf00d0SUpinder Malhi 			offset += scnprintf(buf + offset, buf_sz - offset,
130e3cf00d0SUpinder Malhi 						"\n");
131e3cf00d0SUpinder Malhi 		}
132e3cf00d0SUpinder Malhi 	}
133e3cf00d0SUpinder Malhi 	spin_unlock(&vnic->res_lock);
134e3cf00d0SUpinder Malhi 	return offset;
135e3cf00d0SUpinder Malhi }
136e3cf00d0SUpinder Malhi 
usnic_vnic_res_spec_update(struct usnic_vnic_res_spec * spec,enum usnic_vnic_res_type trgt_type,u16 cnt)137e3cf00d0SUpinder Malhi void usnic_vnic_res_spec_update(struct usnic_vnic_res_spec *spec,
138e3cf00d0SUpinder Malhi 				enum usnic_vnic_res_type trgt_type,
139e3cf00d0SUpinder Malhi 				u16 cnt)
140e3cf00d0SUpinder Malhi {
141e3cf00d0SUpinder Malhi 	int i;
142e3cf00d0SUpinder Malhi 
143e3cf00d0SUpinder Malhi 	for (i = 0; i < USNIC_VNIC_RES_TYPE_MAX; i++) {
144e3cf00d0SUpinder Malhi 		if (spec->resources[i].type == trgt_type) {
145e3cf00d0SUpinder Malhi 			spec->resources[i].cnt = cnt;
146e3cf00d0SUpinder Malhi 			return;
147e3cf00d0SUpinder Malhi 		}
148e3cf00d0SUpinder Malhi 	}
149e3cf00d0SUpinder Malhi 
150e3cf00d0SUpinder Malhi 	WARN_ON(1);
151e3cf00d0SUpinder Malhi }
152e3cf00d0SUpinder Malhi 
usnic_vnic_res_spec_satisfied(const struct usnic_vnic_res_spec * min_spec,struct usnic_vnic_res_spec * res_spec)153e3cf00d0SUpinder Malhi int usnic_vnic_res_spec_satisfied(const struct usnic_vnic_res_spec *min_spec,
154e3cf00d0SUpinder Malhi 					struct usnic_vnic_res_spec *res_spec)
155e3cf00d0SUpinder Malhi {
156e3cf00d0SUpinder Malhi 	int found, i, j;
157e3cf00d0SUpinder Malhi 
158e3cf00d0SUpinder Malhi 	for (i = 0; i < USNIC_VNIC_RES_TYPE_MAX; i++) {
159e3cf00d0SUpinder Malhi 		found = 0;
160e3cf00d0SUpinder Malhi 
161e3cf00d0SUpinder Malhi 		for (j = 0; j < USNIC_VNIC_RES_TYPE_MAX; j++) {
162e3cf00d0SUpinder Malhi 			if (res_spec->resources[i].type !=
163e3cf00d0SUpinder Malhi 				min_spec->resources[i].type)
164e3cf00d0SUpinder Malhi 				continue;
165e3cf00d0SUpinder Malhi 			found = 1;
166e3cf00d0SUpinder Malhi 			if (min_spec->resources[i].cnt >
167e3cf00d0SUpinder Malhi 					res_spec->resources[i].cnt)
168e3cf00d0SUpinder Malhi 				return -EINVAL;
169e3cf00d0SUpinder Malhi 			break;
170e3cf00d0SUpinder Malhi 		}
171e3cf00d0SUpinder Malhi 
172e3cf00d0SUpinder Malhi 		if (!found)
173e3cf00d0SUpinder Malhi 			return -EINVAL;
174e3cf00d0SUpinder Malhi 	}
175e3cf00d0SUpinder Malhi 	return 0;
176e3cf00d0SUpinder Malhi }
177e3cf00d0SUpinder Malhi 
usnic_vnic_spec_dump(char * buf,int buf_sz,struct usnic_vnic_res_spec * res_spec)178e3cf00d0SUpinder Malhi int usnic_vnic_spec_dump(char *buf, int buf_sz,
179e3cf00d0SUpinder Malhi 				struct usnic_vnic_res_spec *res_spec)
180e3cf00d0SUpinder Malhi {
181e3cf00d0SUpinder Malhi 	enum usnic_vnic_res_type res_type;
182e3cf00d0SUpinder Malhi 	int res_cnt;
183e3cf00d0SUpinder Malhi 	int i;
184e3cf00d0SUpinder Malhi 	int offset = 0;
185e3cf00d0SUpinder Malhi 
186e3cf00d0SUpinder Malhi 	for (i = 0; i < USNIC_VNIC_RES_TYPE_MAX; i++) {
187e3cf00d0SUpinder Malhi 		res_type = res_spec->resources[i].type;
188e3cf00d0SUpinder Malhi 		res_cnt = res_spec->resources[i].cnt;
189e3cf00d0SUpinder Malhi 		offset += scnprintf(buf + offset, buf_sz - offset,
190e3cf00d0SUpinder Malhi 				"Res: %s Cnt: %d ",
191e3cf00d0SUpinder Malhi 				usnic_vnic_res_type_to_str(res_type),
192e3cf00d0SUpinder Malhi 				res_cnt);
193e3cf00d0SUpinder Malhi 	}
194e3cf00d0SUpinder Malhi 
195e3cf00d0SUpinder Malhi 	return offset;
196e3cf00d0SUpinder Malhi }
197e3cf00d0SUpinder Malhi 
usnic_vnic_check_room(struct usnic_vnic * vnic,struct usnic_vnic_res_spec * res_spec)198e3cf00d0SUpinder Malhi int usnic_vnic_check_room(struct usnic_vnic *vnic,
199e3cf00d0SUpinder Malhi 				struct usnic_vnic_res_spec *res_spec)
200e3cf00d0SUpinder Malhi {
201e3cf00d0SUpinder Malhi 	int i;
202e3cf00d0SUpinder Malhi 	enum usnic_vnic_res_type res_type;
203e3cf00d0SUpinder Malhi 	int res_cnt;
204e3cf00d0SUpinder Malhi 
205e3cf00d0SUpinder Malhi 	for (i = 0; i < USNIC_VNIC_RES_TYPE_MAX; i++) {
206e3cf00d0SUpinder Malhi 		res_type = res_spec->resources[i].type;
207e3cf00d0SUpinder Malhi 		res_cnt = res_spec->resources[i].cnt;
208e3cf00d0SUpinder Malhi 
209e3cf00d0SUpinder Malhi 		if (res_type == USNIC_VNIC_RES_TYPE_EOL)
210e3cf00d0SUpinder Malhi 			break;
211e3cf00d0SUpinder Malhi 
212e3cf00d0SUpinder Malhi 		if (res_cnt > usnic_vnic_res_free_cnt(vnic, res_type))
213e3cf00d0SUpinder Malhi 			return -EBUSY;
214e3cf00d0SUpinder Malhi 	}
215e3cf00d0SUpinder Malhi 
216e3cf00d0SUpinder Malhi 	return 0;
217e3cf00d0SUpinder Malhi }
218e3cf00d0SUpinder Malhi 
usnic_vnic_res_cnt(struct usnic_vnic * vnic,enum usnic_vnic_res_type type)219e3cf00d0SUpinder Malhi int usnic_vnic_res_cnt(struct usnic_vnic *vnic,
220e3cf00d0SUpinder Malhi 				enum usnic_vnic_res_type type)
221e3cf00d0SUpinder Malhi {
222e3cf00d0SUpinder Malhi 	return vnic->chunks[type].cnt;
223e3cf00d0SUpinder Malhi }
224e3cf00d0SUpinder Malhi 
usnic_vnic_res_free_cnt(struct usnic_vnic * vnic,enum usnic_vnic_res_type type)225e3cf00d0SUpinder Malhi int usnic_vnic_res_free_cnt(struct usnic_vnic *vnic,
226e3cf00d0SUpinder Malhi 				enum usnic_vnic_res_type type)
227e3cf00d0SUpinder Malhi {
228e3cf00d0SUpinder Malhi 	return vnic->chunks[type].free_cnt;
229e3cf00d0SUpinder Malhi }
230e3cf00d0SUpinder Malhi 
231e3cf00d0SUpinder Malhi struct usnic_vnic_res_chunk *
usnic_vnic_get_resources(struct usnic_vnic * vnic,enum usnic_vnic_res_type type,int cnt,void * owner)232e3cf00d0SUpinder Malhi usnic_vnic_get_resources(struct usnic_vnic *vnic, enum usnic_vnic_res_type type,
233e3cf00d0SUpinder Malhi 				int cnt, void *owner)
234e3cf00d0SUpinder Malhi {
235e3cf00d0SUpinder Malhi 	struct usnic_vnic_res_chunk *src, *ret;
236e3cf00d0SUpinder Malhi 	struct usnic_vnic_res *res;
237e3cf00d0SUpinder Malhi 	int i;
238e3cf00d0SUpinder Malhi 
23996390f62SNelson Escobar 	if (usnic_vnic_res_free_cnt(vnic, type) < cnt || cnt < 0 || !owner)
240e3cf00d0SUpinder Malhi 		return ERR_PTR(-EINVAL);
241e3cf00d0SUpinder Malhi 
242e3cf00d0SUpinder Malhi 	ret = kzalloc(sizeof(*ret), GFP_ATOMIC);
24302d93f8eSLeon Romanovsky 	if (!ret)
244e3cf00d0SUpinder Malhi 		return ERR_PTR(-ENOMEM);
245e3cf00d0SUpinder Malhi 
24696390f62SNelson Escobar 	if (cnt > 0) {
24796390f62SNelson Escobar 		ret->res = kcalloc(cnt, sizeof(*(ret->res)), GFP_ATOMIC);
248e3cf00d0SUpinder Malhi 		if (!ret->res) {
249e3cf00d0SUpinder Malhi 			kfree(ret);
250e3cf00d0SUpinder Malhi 			return ERR_PTR(-ENOMEM);
251e3cf00d0SUpinder Malhi 		}
252e3cf00d0SUpinder Malhi 
253e3cf00d0SUpinder Malhi 		spin_lock(&vnic->res_lock);
254e3cf00d0SUpinder Malhi 		src = &vnic->chunks[type];
255e3cf00d0SUpinder Malhi 		for (i = 0; i < src->cnt && ret->cnt < cnt; i++) {
256e3cf00d0SUpinder Malhi 			res = src->res[i];
257e3cf00d0SUpinder Malhi 			if (!res->owner) {
258e3cf00d0SUpinder Malhi 				src->free_cnt--;
259e3cf00d0SUpinder Malhi 				res->owner = owner;
260e3cf00d0SUpinder Malhi 				ret->res[ret->cnt++] = res;
261e3cf00d0SUpinder Malhi 			}
262e3cf00d0SUpinder Malhi 		}
263e3cf00d0SUpinder Malhi 
264e3cf00d0SUpinder Malhi 		spin_unlock(&vnic->res_lock);
26596390f62SNelson Escobar 	}
266e3cf00d0SUpinder Malhi 	ret->type = type;
267e3cf00d0SUpinder Malhi 	ret->vnic = vnic;
268e3cf00d0SUpinder Malhi 	WARN_ON(ret->cnt != cnt);
269e3cf00d0SUpinder Malhi 
270e3cf00d0SUpinder Malhi 	return ret;
271e3cf00d0SUpinder Malhi }
272e3cf00d0SUpinder Malhi 
usnic_vnic_put_resources(struct usnic_vnic_res_chunk * chunk)273e3cf00d0SUpinder Malhi void usnic_vnic_put_resources(struct usnic_vnic_res_chunk *chunk)
274e3cf00d0SUpinder Malhi {
275e3cf00d0SUpinder Malhi 
276e3cf00d0SUpinder Malhi 	struct usnic_vnic_res *res;
277e3cf00d0SUpinder Malhi 	int i;
278e3cf00d0SUpinder Malhi 	struct usnic_vnic *vnic = chunk->vnic;
279e3cf00d0SUpinder Malhi 
28096390f62SNelson Escobar 	if (chunk->cnt > 0) {
281e3cf00d0SUpinder Malhi 		spin_lock(&vnic->res_lock);
282e3cf00d0SUpinder Malhi 		while ((i = --chunk->cnt) >= 0) {
283e3cf00d0SUpinder Malhi 			res = chunk->res[i];
284e3cf00d0SUpinder Malhi 			chunk->res[i] = NULL;
285e3cf00d0SUpinder Malhi 			res->owner = NULL;
286e3cf00d0SUpinder Malhi 			vnic->chunks[res->type].free_cnt++;
287e3cf00d0SUpinder Malhi 		}
288e3cf00d0SUpinder Malhi 		spin_unlock(&vnic->res_lock);
28996390f62SNelson Escobar 	}
290e3cf00d0SUpinder Malhi 
291e3cf00d0SUpinder Malhi 	kfree(chunk->res);
292e3cf00d0SUpinder Malhi 	kfree(chunk);
293e3cf00d0SUpinder Malhi }
294e3cf00d0SUpinder Malhi 
usnic_vnic_get_index(struct usnic_vnic * vnic)295e3cf00d0SUpinder Malhi u16 usnic_vnic_get_index(struct usnic_vnic *vnic)
296e3cf00d0SUpinder Malhi {
297e3cf00d0SUpinder Malhi 	return usnic_vnic_get_pdev(vnic)->devfn - 1;
298e3cf00d0SUpinder Malhi }
299e3cf00d0SUpinder Malhi 
usnic_vnic_alloc_res_chunk(struct usnic_vnic * vnic,enum usnic_vnic_res_type type,struct usnic_vnic_res_chunk * chunk)300e3cf00d0SUpinder Malhi static int usnic_vnic_alloc_res_chunk(struct usnic_vnic *vnic,
301e3cf00d0SUpinder Malhi 					enum usnic_vnic_res_type type,
302e3cf00d0SUpinder Malhi 					struct usnic_vnic_res_chunk *chunk)
303e3cf00d0SUpinder Malhi {
304e3cf00d0SUpinder Malhi 	int cnt, err, i;
305e3cf00d0SUpinder Malhi 	struct usnic_vnic_res *res;
306e3cf00d0SUpinder Malhi 
307e3cf00d0SUpinder Malhi 	cnt = vnic_dev_get_res_count(vnic->vdev, _to_vnic_res_type(type));
30802d93f8eSLeon Romanovsky 	if (cnt < 1) {
30902d93f8eSLeon Romanovsky 		usnic_err("Wrong res count with cnt %d\n", cnt);
310e3cf00d0SUpinder Malhi 		return -EINVAL;
31102d93f8eSLeon Romanovsky 	}
312e3cf00d0SUpinder Malhi 
313e3cf00d0SUpinder Malhi 	chunk->cnt = chunk->free_cnt = cnt;
314*6396bb22SKees Cook 	chunk->res = kcalloc(cnt, sizeof(*(chunk->res)), GFP_KERNEL);
315e3cf00d0SUpinder Malhi 	if (!chunk->res)
316e3cf00d0SUpinder Malhi 		return -ENOMEM;
317e3cf00d0SUpinder Malhi 
318e3cf00d0SUpinder Malhi 	for (i = 0; i < cnt; i++) {
319e3cf00d0SUpinder Malhi 		res = kzalloc(sizeof(*res), GFP_KERNEL);
320e3cf00d0SUpinder Malhi 		if (!res) {
321e3cf00d0SUpinder Malhi 			err = -ENOMEM;
322e3cf00d0SUpinder Malhi 			goto fail;
323e3cf00d0SUpinder Malhi 		}
324e3cf00d0SUpinder Malhi 		res->type = type;
325e3cf00d0SUpinder Malhi 		res->vnic_idx = i;
326e3cf00d0SUpinder Malhi 		res->vnic = vnic;
327e3cf00d0SUpinder Malhi 		res->ctrl = vnic_dev_get_res(vnic->vdev,
328e3cf00d0SUpinder Malhi 						_to_vnic_res_type(type), i);
329e3cf00d0SUpinder Malhi 		chunk->res[i] = res;
330e3cf00d0SUpinder Malhi 	}
331e3cf00d0SUpinder Malhi 
332e3cf00d0SUpinder Malhi 	chunk->vnic = vnic;
333e3cf00d0SUpinder Malhi 	return 0;
334e3cf00d0SUpinder Malhi fail:
335e3cf00d0SUpinder Malhi 	for (i--; i >= 0; i--)
336e3cf00d0SUpinder Malhi 		kfree(chunk->res[i]);
337e3cf00d0SUpinder Malhi 	kfree(chunk->res);
338e3cf00d0SUpinder Malhi 	return err;
339e3cf00d0SUpinder Malhi }
340e3cf00d0SUpinder Malhi 
usnic_vnic_free_res_chunk(struct usnic_vnic_res_chunk * chunk)341e3cf00d0SUpinder Malhi static void usnic_vnic_free_res_chunk(struct usnic_vnic_res_chunk *chunk)
342e3cf00d0SUpinder Malhi {
343e3cf00d0SUpinder Malhi 	int i;
344e3cf00d0SUpinder Malhi 	for (i = 0; i < chunk->cnt; i++)
345e3cf00d0SUpinder Malhi 		kfree(chunk->res[i]);
346e3cf00d0SUpinder Malhi 	kfree(chunk->res);
347e3cf00d0SUpinder Malhi }
348e3cf00d0SUpinder Malhi 
usnic_vnic_discover_resources(struct pci_dev * pdev,struct usnic_vnic * vnic)349e3cf00d0SUpinder Malhi static int usnic_vnic_discover_resources(struct pci_dev *pdev,
350e3cf00d0SUpinder Malhi 						struct usnic_vnic *vnic)
351e3cf00d0SUpinder Malhi {
352e3cf00d0SUpinder Malhi 	enum usnic_vnic_res_type res_type;
353e3cf00d0SUpinder Malhi 	int i;
354e3cf00d0SUpinder Malhi 	int err = 0;
355e3cf00d0SUpinder Malhi 
356e3cf00d0SUpinder Malhi 	for (i = 0; i < ARRAY_SIZE(vnic->bar); i++) {
357e3cf00d0SUpinder Malhi 		if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
358e3cf00d0SUpinder Malhi 			continue;
359e3cf00d0SUpinder Malhi 		vnic->bar[i].len = pci_resource_len(pdev, i);
360e3cf00d0SUpinder Malhi 		vnic->bar[i].vaddr = pci_iomap(pdev, i, vnic->bar[i].len);
361e3cf00d0SUpinder Malhi 		if (!vnic->bar[i].vaddr) {
362e3cf00d0SUpinder Malhi 			usnic_err("Cannot memory-map BAR %d, aborting\n",
363e3cf00d0SUpinder Malhi 					i);
364e3cf00d0SUpinder Malhi 			err = -ENODEV;
365e3cf00d0SUpinder Malhi 			goto out_clean_bar;
366e3cf00d0SUpinder Malhi 		}
367e3cf00d0SUpinder Malhi 		vnic->bar[i].bus_addr = pci_resource_start(pdev, i);
368e3cf00d0SUpinder Malhi 	}
369e3cf00d0SUpinder Malhi 
370e3cf00d0SUpinder Malhi 	vnic->vdev = vnic_dev_register(NULL, pdev, pdev, vnic->bar,
371e3cf00d0SUpinder Malhi 			ARRAY_SIZE(vnic->bar));
372e3cf00d0SUpinder Malhi 	if (!vnic->vdev) {
373e3cf00d0SUpinder Malhi 		usnic_err("Failed to register device %s\n",
374e3cf00d0SUpinder Malhi 				pci_name(pdev));
375e3cf00d0SUpinder Malhi 		err = -EINVAL;
376e3cf00d0SUpinder Malhi 		goto out_clean_bar;
377e3cf00d0SUpinder Malhi 	}
378e3cf00d0SUpinder Malhi 
379e3cf00d0SUpinder Malhi 	for (res_type = USNIC_VNIC_RES_TYPE_EOL + 1;
380e3cf00d0SUpinder Malhi 			res_type < USNIC_VNIC_RES_TYPE_MAX; res_type++) {
381e3cf00d0SUpinder Malhi 		err = usnic_vnic_alloc_res_chunk(vnic, res_type,
382e3cf00d0SUpinder Malhi 						&vnic->chunks[res_type]);
38302d93f8eSLeon Romanovsky 		if (err)
384e3cf00d0SUpinder Malhi 			goto out_clean_chunks;
385e3cf00d0SUpinder Malhi 	}
386e3cf00d0SUpinder Malhi 
387e3cf00d0SUpinder Malhi 	return 0;
388e3cf00d0SUpinder Malhi 
389e3cf00d0SUpinder Malhi out_clean_chunks:
390e3cf00d0SUpinder Malhi 	for (res_type--; res_type > USNIC_VNIC_RES_TYPE_EOL; res_type--)
391e3cf00d0SUpinder Malhi 		usnic_vnic_free_res_chunk(&vnic->chunks[res_type]);
392e3cf00d0SUpinder Malhi 	vnic_dev_unregister(vnic->vdev);
393e3cf00d0SUpinder Malhi out_clean_bar:
394e3cf00d0SUpinder Malhi 	for (i = 0; i < ARRAY_SIZE(vnic->bar); i++) {
395e3cf00d0SUpinder Malhi 		if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
396e3cf00d0SUpinder Malhi 			continue;
397e3cf00d0SUpinder Malhi 		if (!vnic->bar[i].vaddr)
398e3cf00d0SUpinder Malhi 			break;
399e3cf00d0SUpinder Malhi 
400e3cf00d0SUpinder Malhi 		iounmap(vnic->bar[i].vaddr);
401e3cf00d0SUpinder Malhi 	}
402e3cf00d0SUpinder Malhi 
403e3cf00d0SUpinder Malhi 	return err;
404e3cf00d0SUpinder Malhi }
405e3cf00d0SUpinder Malhi 
usnic_vnic_get_pdev(struct usnic_vnic * vnic)406e3cf00d0SUpinder Malhi struct pci_dev *usnic_vnic_get_pdev(struct usnic_vnic *vnic)
407e3cf00d0SUpinder Malhi {
408e3cf00d0SUpinder Malhi 	return vnic_dev_get_pdev(vnic->vdev);
409e3cf00d0SUpinder Malhi }
410e3cf00d0SUpinder Malhi 
usnic_vnic_get_bar(struct usnic_vnic * vnic,int bar_num)411e3cf00d0SUpinder Malhi struct vnic_dev_bar *usnic_vnic_get_bar(struct usnic_vnic *vnic,
412e3cf00d0SUpinder Malhi 				int bar_num)
413e3cf00d0SUpinder Malhi {
414e3cf00d0SUpinder Malhi 	return (bar_num < ARRAY_SIZE(vnic->bar)) ? &vnic->bar[bar_num] : NULL;
415e3cf00d0SUpinder Malhi }
416e3cf00d0SUpinder Malhi 
usnic_vnic_release_resources(struct usnic_vnic * vnic)417e3cf00d0SUpinder Malhi static void usnic_vnic_release_resources(struct usnic_vnic *vnic)
418e3cf00d0SUpinder Malhi {
419e3cf00d0SUpinder Malhi 	int i;
420e3cf00d0SUpinder Malhi 	struct pci_dev *pdev;
421e3cf00d0SUpinder Malhi 	enum usnic_vnic_res_type res_type;
422e3cf00d0SUpinder Malhi 
423e3cf00d0SUpinder Malhi 	pdev = usnic_vnic_get_pdev(vnic);
424e3cf00d0SUpinder Malhi 
425e3cf00d0SUpinder Malhi 	for (res_type = USNIC_VNIC_RES_TYPE_EOL + 1;
426e3cf00d0SUpinder Malhi 			res_type < USNIC_VNIC_RES_TYPE_MAX; res_type++)
427e3cf00d0SUpinder Malhi 		usnic_vnic_free_res_chunk(&vnic->chunks[res_type]);
428e3cf00d0SUpinder Malhi 
429e3cf00d0SUpinder Malhi 	vnic_dev_unregister(vnic->vdev);
430e3cf00d0SUpinder Malhi 
431e3cf00d0SUpinder Malhi 	for (i = 0; i < ARRAY_SIZE(vnic->bar); i++) {
432e3cf00d0SUpinder Malhi 		if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
433e3cf00d0SUpinder Malhi 			continue;
434e3cf00d0SUpinder Malhi 		iounmap(vnic->bar[i].vaddr);
435e3cf00d0SUpinder Malhi 	}
436e3cf00d0SUpinder Malhi }
437e3cf00d0SUpinder Malhi 
usnic_vnic_alloc(struct pci_dev * pdev)438e3cf00d0SUpinder Malhi struct usnic_vnic *usnic_vnic_alloc(struct pci_dev *pdev)
439e3cf00d0SUpinder Malhi {
440e3cf00d0SUpinder Malhi 	struct usnic_vnic *vnic;
441e3cf00d0SUpinder Malhi 	int err = 0;
442e3cf00d0SUpinder Malhi 
443e3cf00d0SUpinder Malhi 	if (!pci_is_enabled(pdev)) {
444e3cf00d0SUpinder Malhi 		usnic_err("PCI dev %s is disabled\n", pci_name(pdev));
445e3cf00d0SUpinder Malhi 		return ERR_PTR(-EINVAL);
446e3cf00d0SUpinder Malhi 	}
447e3cf00d0SUpinder Malhi 
448e3cf00d0SUpinder Malhi 	vnic = kzalloc(sizeof(*vnic), GFP_KERNEL);
44902d93f8eSLeon Romanovsky 	if (!vnic)
450e3cf00d0SUpinder Malhi 		return ERR_PTR(-ENOMEM);
451e3cf00d0SUpinder Malhi 
452e3cf00d0SUpinder Malhi 	spin_lock_init(&vnic->res_lock);
453e3cf00d0SUpinder Malhi 
454e3cf00d0SUpinder Malhi 	err = usnic_vnic_discover_resources(pdev, vnic);
455e3cf00d0SUpinder Malhi 	if (err) {
456e3cf00d0SUpinder Malhi 		usnic_err("Failed to discover %s resources with err %d\n",
457e3cf00d0SUpinder Malhi 				pci_name(pdev), err);
458e3cf00d0SUpinder Malhi 		goto out_free_vnic;
459e3cf00d0SUpinder Malhi 	}
460e3cf00d0SUpinder Malhi 
461e3cf00d0SUpinder Malhi 	usnic_dbg("Allocated vnic for %s\n", usnic_vnic_pci_name(vnic));
462e3cf00d0SUpinder Malhi 
463e3cf00d0SUpinder Malhi 	return vnic;
464e3cf00d0SUpinder Malhi 
465e3cf00d0SUpinder Malhi out_free_vnic:
466e3cf00d0SUpinder Malhi 	kfree(vnic);
467e3cf00d0SUpinder Malhi 
468e3cf00d0SUpinder Malhi 	return ERR_PTR(err);
469e3cf00d0SUpinder Malhi }
470e3cf00d0SUpinder Malhi 
usnic_vnic_free(struct usnic_vnic * vnic)471e3cf00d0SUpinder Malhi void usnic_vnic_free(struct usnic_vnic *vnic)
472e3cf00d0SUpinder Malhi {
473e3cf00d0SUpinder Malhi 	usnic_vnic_release_resources(vnic);
474e3cf00d0SUpinder Malhi 	kfree(vnic);
475e3cf00d0SUpinder Malhi }
476