xref: /freebsd/sys/dev/pci/pci_iov.c (revision 3481a9cdc4dc26ef583bb0f46a04d5d059cde466)
1 /*-
2  * Copyright (c) 2013-2015 Sandvine Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #include "opt_bus.h"
29 
30 #include <sys/param.h>
31 #include <sys/conf.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/fcntl.h>
36 #include <sys/ioccom.h>
37 #include <sys/iov.h>
38 #include <sys/linker.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/pciio.h>
44 #include <sys/queue.h>
45 #include <sys/rman.h>
46 #include <sys/stdarg.h>
47 #include <sys/sysctl.h>
48 
49 #include <machine/bus.h>
50 
51 #include <sys/nv.h>
52 #include <sys/iov_schema.h>
53 
54 #include <dev/pci/pcireg.h>
55 #include <dev/pci/pcivar.h>
56 #include <dev/pci/pci_iov.h>
57 #include <dev/pci/pci_private.h>
58 #include <dev/pci/pci_iov_private.h>
59 #include <dev/pci/schema_private.h>
60 
61 #include "pcib_if.h"
62 
63 static MALLOC_DEFINE(M_SRIOV, "sr_iov", "PCI SR-IOV allocations");
64 
65 static d_ioctl_t pci_iov_ioctl;
66 
67 static struct cdevsw iov_cdevsw = {
68 	.d_version = D_VERSION,
69 	.d_name = "iov",
70 	.d_ioctl = pci_iov_ioctl
71 };
72 
73 SYSCTL_DECL(_hw_pci);
74 
75 /*
76  * The maximum amount of memory we will allocate for user configuration of an
77  * SR-IOV device.  1MB ought to be enough for anyone, but leave this
78  * configurable just in case.
79  */
80 static u_long pci_iov_max_config = 1024 * 1024;
81 SYSCTL_ULONG(_hw_pci, OID_AUTO, iov_max_config, CTLFLAG_RWTUN,
82     &pci_iov_max_config, 0, "Maximum allowed size of SR-IOV configuration.");
83 
84 #define IOV_READ(d, r, w) \
85 	pci_read_config((d)->cfg.dev, (d)->cfg.iov->iov_pos + r, w)
86 
87 #define IOV_WRITE(d, r, v, w) \
88 	pci_write_config((d)->cfg.dev, (d)->cfg.iov->iov_pos + r, v, w)
89 
90 static nvlist_t	*pci_iov_build_schema(nvlist_t **pf_schema,
91 		    nvlist_t **vf_schema);
92 static void	pci_iov_build_pf_schema(nvlist_t *schema,
93 		    nvlist_t **driver_schema);
94 static void	pci_iov_build_vf_schema(nvlist_t *schema,
95 		    nvlist_t **driver_schema);
96 static int	pci_iov_delete_iov_children(struct pci_devinfo *dinfo);
97 static nvlist_t	*pci_iov_get_pf_subsystem_schema(void);
98 static nvlist_t	*pci_iov_get_vf_subsystem_schema(void);
99 
100 int
pci_iov_attach_name(device_t dev,struct nvlist * pf_schema,struct nvlist * vf_schema,const char * fmt,...)101 pci_iov_attach_name(device_t dev, struct nvlist *pf_schema,
102     struct nvlist *vf_schema, const char *fmt, ...)
103 {
104 	char buf[NAME_MAX + 1];
105 	va_list ap;
106 
107 	va_start(ap, fmt);
108 	vsnprintf(buf, sizeof(buf), fmt, ap);
109 	va_end(ap);
110 	return (PCI_IOV_ATTACH(device_get_parent(dev), dev, pf_schema,
111 	    vf_schema, buf));
112 }
113 
114 int
pci_iov_attach_method(device_t bus,device_t dev,nvlist_t * pf_schema,nvlist_t * vf_schema,const char * name)115 pci_iov_attach_method(device_t bus, device_t dev, nvlist_t *pf_schema,
116     nvlist_t *vf_schema, const char *name)
117 {
118 	struct pci_devinfo *dinfo;
119 	struct pcicfg_iov *iov;
120 	nvlist_t *schema;
121 	uint32_t version;
122 	int error;
123 	int iov_pos;
124 
125 	dinfo = device_get_ivars(dev);
126 	schema = NULL;
127 
128 	error = pci_find_extcap(dev, PCIZ_SRIOV, &iov_pos);
129 
130 	if (error != 0)
131 		return (error);
132 
133 	version = pci_read_config(dev, iov_pos, 4);
134 	if (PCI_EXTCAP_VER(version) != 1) {
135 		if (bootverbose)
136 			device_printf(dev,
137 			    "Unsupported version of SR-IOV (%d) detected\n",
138 			    PCI_EXTCAP_VER(version));
139 
140 		return (ENXIO);
141 	}
142 
143 	iov = malloc(sizeof(*dinfo->cfg.iov), M_SRIOV, M_WAITOK | M_ZERO);
144 
145 	mtx_lock(&Giant);
146 	if (dinfo->cfg.iov != NULL) {
147 		error = EBUSY;
148 		goto cleanup;
149 	}
150 	iov->iov_pf = dev;
151 	iov->iov_pos = iov_pos;
152 
153 	schema = pci_iov_build_schema(&pf_schema, &vf_schema);
154 	if (schema == NULL) {
155 		error = ENOMEM;
156 		goto cleanup;
157 	}
158 
159 	error = pci_iov_validate_schema(schema);
160 	if (error != 0)
161 		goto cleanup;
162 	iov->iov_schema = schema;
163 
164 	iov->iov_cdev = make_dev(&iov_cdevsw, device_get_unit(dev),
165 	    UID_ROOT, GID_WHEEL, 0600, "iov/%s", name);
166 
167 	if (iov->iov_cdev == NULL) {
168 		error = ENOMEM;
169 		goto cleanup;
170 	}
171 
172 	dinfo->cfg.iov = iov;
173 	iov->iov_cdev->si_drv1 = dinfo;
174 	mtx_unlock(&Giant);
175 
176 	return (0);
177 
178 cleanup:
179 	nvlist_destroy(schema);
180 	nvlist_destroy(pf_schema);
181 	nvlist_destroy(vf_schema);
182 	free(iov, M_SRIOV);
183 	mtx_unlock(&Giant);
184 	return (error);
185 }
186 
187 int
pci_iov_detach_method(device_t bus,device_t dev)188 pci_iov_detach_method(device_t bus, device_t dev)
189 {
190 	struct pci_devinfo *dinfo;
191 	struct pcicfg_iov *iov;
192 	int error;
193 
194 	mtx_lock(&Giant);
195 	dinfo = device_get_ivars(dev);
196 	iov = dinfo->cfg.iov;
197 
198 	if (iov == NULL) {
199 		mtx_unlock(&Giant);
200 		return (0);
201 	}
202 
203 	if ((iov->iov_flags & IOV_BUSY) != 0) {
204 		mtx_unlock(&Giant);
205 		return (EBUSY);
206 	}
207 
208 	error = pci_iov_delete_iov_children(dinfo);
209 	if (error != 0) {
210 		mtx_unlock(&Giant);
211 		return (error);
212 	}
213 
214 	dinfo->cfg.iov = NULL;
215 
216 	if (iov->iov_cdev) {
217 		destroy_dev(iov->iov_cdev);
218 		iov->iov_cdev = NULL;
219 	}
220 	nvlist_destroy(iov->iov_schema);
221 
222 	free(iov, M_SRIOV);
223 	mtx_unlock(&Giant);
224 
225 	return (0);
226 }
227 
228 static nvlist_t *
pci_iov_build_schema(nvlist_t ** pf,nvlist_t ** vf)229 pci_iov_build_schema(nvlist_t **pf, nvlist_t **vf)
230 {
231 	nvlist_t *schema, *pf_driver, *vf_driver;
232 
233 	/* We always take ownership of the schemas. */
234 	pf_driver = *pf;
235 	*pf = NULL;
236 	vf_driver = *vf;
237 	*vf = NULL;
238 
239 	schema = pci_iov_schema_alloc_node();
240 	if (schema == NULL)
241 		goto cleanup;
242 
243 	pci_iov_build_pf_schema(schema, &pf_driver);
244 	pci_iov_build_vf_schema(schema, &vf_driver);
245 
246 	if (nvlist_error(schema) != 0)
247 		goto cleanup;
248 
249 	return (schema);
250 
251 cleanup:
252 	nvlist_destroy(schema);
253 	nvlist_destroy(pf_driver);
254 	nvlist_destroy(vf_driver);
255 	return (NULL);
256 }
257 
258 static void
pci_iov_build_pf_schema(nvlist_t * schema,nvlist_t ** driver_schema)259 pci_iov_build_pf_schema(nvlist_t *schema, nvlist_t **driver_schema)
260 {
261 	nvlist_t *pf_schema, *iov_schema;
262 
263 	pf_schema = pci_iov_schema_alloc_node();
264 	if (pf_schema == NULL) {
265 		nvlist_set_error(schema, ENOMEM);
266 		return;
267 	}
268 
269 	iov_schema = pci_iov_get_pf_subsystem_schema();
270 
271 	/*
272 	 * Note that if either *driver_schema or iov_schema is NULL, then
273 	 * nvlist_move_nvlist will put the schema in the error state and
274 	 * SR-IOV will fail to initialize later, so we don't have to explicitly
275 	 * handle that case.
276 	 */
277 	nvlist_move_nvlist(pf_schema, DRIVER_CONFIG_NAME, *driver_schema);
278 	nvlist_move_nvlist(pf_schema, IOV_CONFIG_NAME, iov_schema);
279 	nvlist_move_nvlist(schema, PF_CONFIG_NAME, pf_schema);
280 	*driver_schema = NULL;
281 }
282 
283 static void
pci_iov_build_vf_schema(nvlist_t * schema,nvlist_t ** driver_schema)284 pci_iov_build_vf_schema(nvlist_t *schema, nvlist_t **driver_schema)
285 {
286 	nvlist_t *vf_schema, *iov_schema;
287 
288 	vf_schema = pci_iov_schema_alloc_node();
289 	if (vf_schema == NULL) {
290 		nvlist_set_error(schema, ENOMEM);
291 		return;
292 	}
293 
294 	iov_schema = pci_iov_get_vf_subsystem_schema();
295 
296 	/*
297 	 * Note that if either *driver_schema or iov_schema is NULL, then
298 	 * nvlist_move_nvlist will put the schema in the error state and
299 	 * SR-IOV will fail to initialize later, so we don't have to explicitly
300 	 * handle that case.
301 	 */
302 	nvlist_move_nvlist(vf_schema, DRIVER_CONFIG_NAME, *driver_schema);
303 	nvlist_move_nvlist(vf_schema, IOV_CONFIG_NAME, iov_schema);
304 	nvlist_move_nvlist(schema, VF_SCHEMA_NAME, vf_schema);
305 	*driver_schema = NULL;
306 }
307 
308 static nvlist_t *
pci_iov_get_pf_subsystem_schema(void)309 pci_iov_get_pf_subsystem_schema(void)
310 {
311 	nvlist_t *pf;
312 
313 	pf = pci_iov_schema_alloc_node();
314 	if (pf == NULL)
315 		return (NULL);
316 
317 	pci_iov_schema_add_uint16(pf, "num_vfs", IOV_SCHEMA_REQUIRED, -1);
318 	pci_iov_schema_add_string(pf, "device", IOV_SCHEMA_REQUIRED, NULL);
319 
320 	return (pf);
321 }
322 
323 static nvlist_t *
pci_iov_get_vf_subsystem_schema(void)324 pci_iov_get_vf_subsystem_schema(void)
325 {
326 	nvlist_t *vf;
327 
328 	vf = pci_iov_schema_alloc_node();
329 	if (vf == NULL)
330 		return (NULL);
331 
332 	pci_iov_schema_add_bool(vf, "passthrough", IOV_SCHEMA_HASDEFAULT, 0);
333 
334 	return (vf);
335 }
336 
337 static int
pci_iov_alloc_bar(struct pci_devinfo * dinfo,int bar,pci_addr_t bar_shift)338 pci_iov_alloc_bar(struct pci_devinfo *dinfo, int bar, pci_addr_t bar_shift)
339 {
340 	struct resource *res;
341 	struct pcicfg_iov *iov;
342 	device_t dev, bus;
343 	rman_res_t start, end;
344 	pci_addr_t bar_size;
345 	int rid;
346 
347 	iov = dinfo->cfg.iov;
348 	dev = dinfo->cfg.dev;
349 	bus = device_get_parent(dev);
350 	rid = iov->iov_pos + PCIR_SRIOV_BAR(bar);
351 	bar_size = 1 << bar_shift;
352 
353 	res = pci_alloc_multi_resource(bus, dev, SYS_RES_MEMORY, rid, 0,
354 	    ~0, 1, iov->iov_num_vfs, RF_ACTIVE);
355 
356 	if (res == NULL)
357 		return (ENXIO);
358 
359 	iov->iov_bar[bar].res = res;
360 	iov->iov_bar[bar].bar_size = bar_size;
361 	iov->iov_bar[bar].bar_shift = bar_shift;
362 
363 	start = rman_get_start(res);
364 	end = rman_get_end(res);
365 	return (rman_manage_region(&iov->rman, start, end));
366 }
367 
368 static void
pci_iov_add_bars(struct pcicfg_iov * iov,struct pci_devinfo * dinfo)369 pci_iov_add_bars(struct pcicfg_iov *iov, struct pci_devinfo *dinfo)
370 {
371 	struct pci_iov_bar *bar;
372 	uint64_t bar_start;
373 	int i;
374 
375 	for (i = 0; i <= PCIR_MAX_BAR_0; i++) {
376 		bar = &iov->iov_bar[i];
377 		if (bar->res != NULL) {
378 			bar_start = rman_get_start(bar->res) +
379 			    dinfo->cfg.vf.index * bar->bar_size;
380 
381 			pci_add_bar(dinfo->cfg.dev, PCIR_BAR(i), bar_start,
382 			    bar->bar_shift);
383 		}
384 	}
385 }
386 
387 static int
pci_iov_parse_config(struct pcicfg_iov * iov,struct pci_iov_arg * arg,nvlist_t ** ret)388 pci_iov_parse_config(struct pcicfg_iov *iov, struct pci_iov_arg *arg,
389     nvlist_t **ret)
390 {
391 	void *packed_config;
392 	nvlist_t *config;
393 	int error;
394 
395 	config = NULL;
396 	packed_config = NULL;
397 
398 	if (arg->len > pci_iov_max_config) {
399 		error = EMSGSIZE;
400 		goto out;
401 	}
402 
403 	packed_config = malloc(arg->len, M_SRIOV, M_WAITOK);
404 
405 	error = copyin(arg->config, packed_config, arg->len);
406 	if (error != 0)
407 		goto out;
408 
409 	config = nvlist_unpack(packed_config, arg->len, NV_FLAG_IGNORE_CASE);
410 	if (config == NULL) {
411 		error = EINVAL;
412 		goto out;
413 	}
414 
415 	error = pci_iov_schema_validate_config(iov->iov_schema, config);
416 	if (error != 0)
417 		goto out;
418 
419 	error = nvlist_error(config);
420 	if (error != 0)
421 		goto out;
422 
423 	*ret = config;
424 	config = NULL;
425 
426 out:
427 	nvlist_destroy(config);
428 	free(packed_config, M_SRIOV);
429 	return (error);
430 }
431 
432 /*
433  * Set the ARI_EN bit in the lowest-numbered PCI function with the SR-IOV
434  * capability.  This bit is only writeable on the lowest-numbered PF but
435  * affects all PFs on the device.
436  */
437 static int
pci_iov_set_ari(device_t bus,bool * ari_enabled)438 pci_iov_set_ari(device_t bus, bool *ari_enabled)
439 {
440 	device_t lowest;
441 	device_t *devlist;
442 	int i, error, devcount, lowest_func, lowest_pos, iov_pos, dev_func;
443 	uint16_t iov_ctl;
444 
445 	/* If ARI is disabled on the downstream port there is nothing to do. */
446 	if (!PCIB_ARI_ENABLED(device_get_parent(bus))) {
447 		*ari_enabled = false;
448 		return (0);
449 	}
450 
451 	error = device_get_children(bus, &devlist, &devcount);
452 
453 	if (error != 0)
454 		return (error);
455 
456 	lowest = NULL;
457 	for (i = 0; i < devcount; i++) {
458 		if (pci_find_extcap(devlist[i], PCIZ_SRIOV, &iov_pos) == 0) {
459 			dev_func = pci_get_function(devlist[i]);
460 			if (lowest == NULL || dev_func < lowest_func) {
461 				lowest = devlist[i];
462 				lowest_func = dev_func;
463 				lowest_pos = iov_pos;
464 			}
465 		}
466 	}
467 	free(devlist, M_TEMP);
468 
469 	/*
470 	 * If we called this function some device must have the SR-IOV
471 	 * capability.
472 	 */
473 	KASSERT(lowest != NULL,
474 	    ("Could not find child of %s with SR-IOV capability",
475 	    device_get_nameunit(bus)));
476 
477 	iov_ctl = pci_read_config(lowest, lowest_pos + PCIR_SRIOV_CTL, 2);
478 	iov_ctl |= PCIM_SRIOV_ARI_EN;
479 	pci_write_config(lowest, lowest_pos + PCIR_SRIOV_CTL, iov_ctl, 2);
480 	if ((pci_read_config(lowest, lowest_pos + PCIR_SRIOV_CTL, 2) &
481 	    PCIM_SRIOV_ARI_EN) == 0) {
482 		device_printf(lowest, "failed to enable ARI\n");
483 		return (ENXIO);
484 	}
485 	*ari_enabled = true;
486 	return (0);
487 }
488 
489 static int
pci_iov_config_page_size(struct pci_devinfo * dinfo)490 pci_iov_config_page_size(struct pci_devinfo *dinfo)
491 {
492 	uint32_t page_cap, page_size;
493 
494 	page_cap = IOV_READ(dinfo, PCIR_SRIOV_PAGE_CAP, 4);
495 
496 	/*
497 	 * If the system page size is less than the smallest SR-IOV page size
498 	 * then round up to the smallest SR-IOV page size.
499 	 */
500 	if (PAGE_SHIFT < PCI_SRIOV_BASE_PAGE_SHIFT)
501 		page_size = (1 << 0);
502 	else
503 		page_size = (1 << (PAGE_SHIFT - PCI_SRIOV_BASE_PAGE_SHIFT));
504 
505 	/* Check that the device supports the system page size. */
506 	if (!(page_size & page_cap))
507 		return (ENXIO);
508 
509 	IOV_WRITE(dinfo, PCIR_SRIOV_PAGE_SIZE, page_size, 4);
510 	return (0);
511 }
512 
513 static int
pci_iov_init(device_t dev,uint16_t num_vfs,const nvlist_t * config)514 pci_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *config)
515 {
516 	const nvlist_t *device, *driver_config;
517 
518 	device = nvlist_get_nvlist(config, PF_CONFIG_NAME);
519 	driver_config = nvlist_get_nvlist(device, DRIVER_CONFIG_NAME);
520 	return (PCI_IOV_INIT(dev, num_vfs, driver_config));
521 }
522 
523 static int
pci_iov_init_rman(device_t pf,struct pcicfg_iov * iov)524 pci_iov_init_rman(device_t pf, struct pcicfg_iov *iov)
525 {
526 	int error;
527 
528 	iov->rman.rm_start = 0;
529 	iov->rman.rm_end = ~0;
530 	iov->rman.rm_type = RMAN_ARRAY;
531 	snprintf(iov->rman_name, sizeof(iov->rman_name), "%s VF I/O memory",
532 	    device_get_nameunit(pf));
533 	iov->rman.rm_descr = iov->rman_name;
534 
535 	error = rman_init(&iov->rman);
536 	if (error != 0)
537 		return (error);
538 
539 	iov->iov_flags |= IOV_RMAN_INITED;
540 	return (0);
541 }
542 
543 static int
pci_iov_alloc_bar_ea(struct pci_devinfo * dinfo,int bar)544 pci_iov_alloc_bar_ea(struct pci_devinfo *dinfo, int bar)
545 {
546 	struct pcicfg_iov *iov;
547 	rman_res_t start, end;
548 	struct resource *res;
549 	struct resource_list *rl;
550 	struct resource_list_entry *rle;
551 
552 	rl = &dinfo->resources;
553 	iov = dinfo->cfg.iov;
554 
555 	rle = resource_list_find(rl, SYS_RES_MEMORY,
556 	    iov->iov_pos + PCIR_SRIOV_BAR(bar));
557 	if (rle == NULL)
558 		rle = resource_list_find(rl, SYS_RES_IOPORT,
559 		    iov->iov_pos + PCIR_SRIOV_BAR(bar));
560 	if (rle == NULL)
561 		return (ENXIO);
562 	res = rle->res;
563 
564 	iov->iov_bar[bar].res = res;
565 	iov->iov_bar[bar].bar_size = rman_get_size(res) / iov->iov_num_vfs;
566 	iov->iov_bar[bar].bar_shift = pci_mapsize(iov->iov_bar[bar].bar_size);
567 
568 	start = rman_get_start(res);
569 	end = rman_get_end(res);
570 
571 	return (rman_manage_region(&iov->rman, start, end));
572 }
573 
574 static int
pci_iov_setup_bars(struct pci_devinfo * dinfo)575 pci_iov_setup_bars(struct pci_devinfo *dinfo)
576 {
577 	device_t dev;
578 	struct pcicfg_iov *iov;
579 	pci_addr_t bar_value, testval;
580 	int i, last_64, error;
581 
582 	iov = dinfo->cfg.iov;
583 	dev = dinfo->cfg.dev;
584 	last_64 = 0;
585 
586 	pci_add_resources_ea(device_get_parent(dev), dev, 1);
587 
588 	for (i = 0; i <= PCIR_MAX_BAR_0; i++) {
589 		/* First, try to use BARs allocated with EA */
590 		error = pci_iov_alloc_bar_ea(dinfo, i);
591 		if (error == 0)
592 			continue;
593 
594 		/* Allocate legacy-BAR only if EA is not enabled */
595 		if (pci_ea_is_enabled(dev, iov->iov_pos + PCIR_SRIOV_BAR(i)))
596 			continue;
597 
598 		/*
599 		 * If a PCI BAR is a 64-bit wide BAR, then it spans two
600 		 * consecutive registers.  Therefore if the last BAR that
601 		 * we looked at was a 64-bit BAR, we need to skip this
602 		 * register as it's the second half of the last BAR.
603 		 */
604 		if (!last_64) {
605 			pci_read_bar(dev,
606 			    iov->iov_pos + PCIR_SRIOV_BAR(i),
607 			    &bar_value, &testval, &last_64);
608 
609 			if (testval != 0) {
610 				error = pci_iov_alloc_bar(dinfo, i,
611 				   pci_mapsize(testval));
612 				if (error != 0)
613 					return (error);
614 			}
615 		} else
616 			last_64 = 0;
617 	}
618 
619 	return (0);
620 }
621 
622 static int
pci_iov_enumerate_vfs(struct pci_devinfo * dinfo,const nvlist_t * config,uint16_t first_rid,uint16_t rid_stride)623 pci_iov_enumerate_vfs(struct pci_devinfo *dinfo, const nvlist_t *config,
624     uint16_t first_rid, uint16_t rid_stride)
625 {
626 	char device_name[VF_MAX_NAME];
627 	const nvlist_t *device, *driver_config, *iov_config;
628 	device_t bus, dev, vf, *vfs;
629 	struct pcicfg_iov *iov;
630 	struct pci_devinfo *vfinfo;
631 	int error, i, ncreated;
632 	uint16_t vid, did, next_rid;
633 
634 	iov = dinfo->cfg.iov;
635 	dev = dinfo->cfg.dev;
636 	bus = device_get_parent(dev);
637 	next_rid = first_rid;
638 	vid = pci_get_vendor(dev);
639 	did = IOV_READ(dinfo, PCIR_SRIOV_VF_DID, 2);
640 	vfs = mallocarray(iov->iov_num_vfs, sizeof(*vfs), M_SRIOV,
641 	    M_WAITOK | M_ZERO);
642 	ncreated = 0;
643 
644 	for (i = 0; i < iov->iov_num_vfs; i++, next_rid += rid_stride) {
645 		snprintf(device_name, sizeof(device_name), VF_PREFIX"%d", i);
646 		device = nvlist_get_nvlist(config, device_name);
647 		iov_config = nvlist_get_nvlist(device, IOV_CONFIG_NAME);
648 		driver_config = nvlist_get_nvlist(device, DRIVER_CONFIG_NAME);
649 
650 		vf = PCI_CREATE_IOV_CHILD(bus, dev, next_rid, vid, did);
651 		if (vf == NULL) {
652 			error = ENXIO;
653 			goto fail;
654 		}
655 		vfs[ncreated++] = vf;
656 
657 		/*
658 		 * If we are creating passthrough devices then force the ppt
659 		 * driver to attach to prevent a VF driver from claiming the
660 		 * VFs.
661 		 */
662 		if (nvlist_get_bool(iov_config, "passthrough"))
663 			device_set_devclass_fixed(vf, "ppt");
664 
665 		vfinfo = device_get_ivars(vf);
666 
667 		vfinfo->cfg.vf.index = i;
668 
669 		pci_iov_add_bars(iov, vfinfo);
670 
671 		error = PCI_IOV_ADD_VF(dev, i, driver_config);
672 		if (error != 0) {
673 			device_printf(dev, "Failed to add VF %d\n", i);
674 			goto fail;
675 		}
676 	}
677 
678 	bus_attach_children(bus);
679 	free(vfs, M_SRIOV);
680 	return (0);
681 
682 fail:
683 	for (i = 0; i < ncreated; i++)
684 		device_delete_child(bus, vfs[i]);
685 	free(vfs, M_SRIOV);
686 	return (error);
687 }
688 
689 static int
pci_iov_config(struct cdev * cdev,struct pci_iov_arg * arg)690 pci_iov_config(struct cdev *cdev, struct pci_iov_arg *arg)
691 {
692 	device_t bus, dev;
693 	struct pci_devinfo *dinfo;
694 	struct pcicfg_iov *iov;
695 	nvlist_t *config;
696 	int i, error;
697 	uint16_t rid_off, rid_stride;
698 	uint16_t first_rid, last_rid;
699 	uint16_t iov_ctl;
700 	uint16_t num_vfs, total_vfs;
701 	int iov_inited;
702 	bool ari_enabled;
703 
704 	mtx_lock(&Giant);
705 	dinfo = cdev->si_drv1;
706 	iov = dinfo->cfg.iov;
707 	dev = dinfo->cfg.dev;
708 	bus = device_get_parent(dev);
709 	iov_inited = 0;
710 	config = NULL;
711 
712 	if ((iov->iov_flags & IOV_BUSY) || iov->iov_num_vfs != 0) {
713 		mtx_unlock(&Giant);
714 		return (EBUSY);
715 	}
716 	iov->iov_flags |= IOV_BUSY;
717 
718 	error = pci_iov_parse_config(iov, arg, &config);
719 	if (error != 0)
720 		goto out;
721 
722 	num_vfs = pci_iov_config_get_num_vfs(config);
723 	total_vfs = IOV_READ(dinfo, PCIR_SRIOV_TOTAL_VFS, 2);
724 	if (num_vfs > total_vfs) {
725 		error = EINVAL;
726 		goto out;
727 	}
728 
729 	error = pci_iov_config_page_size(dinfo);
730 	if (error != 0)
731 		goto out;
732 
733 	error = pci_iov_set_ari(bus, &ari_enabled);
734 	if (error != 0)
735 		goto out;
736 
737 	error = pci_iov_init(dev, num_vfs, config);
738 	if (error != 0)
739 		goto out;
740 	iov_inited = 1;
741 
742 	IOV_WRITE(dinfo, PCIR_SRIOV_NUM_VFS, num_vfs, 2);
743 
744 	rid_off = IOV_READ(dinfo, PCIR_SRIOV_VF_OFF, 2);
745 	rid_stride = IOV_READ(dinfo, PCIR_SRIOV_VF_STRIDE, 2);
746 
747 	first_rid = pci_get_rid(dev) + rid_off;
748 	last_rid = first_rid + (num_vfs - 1) * rid_stride;
749 
750 	if (pci_get_bus(dev) != PCI_RID2BUS(last_rid)) {
751 		device_t pcib = device_get_parent(bus);
752 		uint8_t secbus = pci_read_config(pcib, PCIR_SECBUS_1, 1);
753 		uint8_t subbus = pci_read_config(pcib, PCIR_SUBBUS_1, 1);
754 		uint16_t vf_bus = PCI_RID2BUS(last_rid);
755 
756 		/*
757 		 * XXX: This should not be directly accessing the bridge registers and does
758 		 * nothing to prevent some other device from releasing this bus number while
759 		 * another PF is using it.
760 		 */
761 		if (secbus == 0 || vf_bus < secbus || vf_bus > subbus) {
762 			int rid = 0;
763 
764 			iov->iov_bus_res = bus_alloc_resource(bus, PCI_RES_BUS, &rid,
765 							      vf_bus, vf_bus, 1, RF_ACTIVE);
766 			if (iov->iov_bus_res == NULL) {
767 				device_printf(dev,
768 				    "failed to allocate PCIe bus number for VFs\n");
769 				error = ENOSPC;
770 				goto out;
771 			}
772 		}
773 	}
774 
775 	if (!ari_enabled) {
776 		uint16_t vf_rid;
777 
778 		/*
779 		 * Without ARI, VFs on the PF's bus must use device 0.
780 		 * VFs on another bus use the conventional device/function
781 		 * encoding and may have a non-zero device number.  For
782 		 * example, Intel 82576 and I350 VFs start at device 16 on
783 		 * the next bus when ARI is unavailable.
784 		 */
785 		vf_rid = first_rid;
786 		for (i = 0; i < num_vfs; i++, vf_rid += rid_stride) {
787 			if (PCI_RID2BUS(vf_rid) == pci_get_bus(dev) &&
788 			    PCI_RID2SLOT(vf_rid) != 0) {
789 				error = ENOSPC;
790 				goto out;
791 			}
792 		}
793 	}
794 
795 	iov_ctl = IOV_READ(dinfo, PCIR_SRIOV_CTL, 2);
796 	iov_ctl &= ~(PCIM_SRIOV_VF_EN | PCIM_SRIOV_VF_MSE);
797 	IOV_WRITE(dinfo, PCIR_SRIOV_CTL, iov_ctl, 2);
798 
799 	error = pci_iov_init_rman(dev, iov);
800 	if (error != 0)
801 		goto out;
802 
803 	iov->iov_num_vfs = num_vfs;
804 
805 	error = pci_iov_setup_bars(dinfo);
806 	if (error != 0)
807 		goto out;
808 
809 	iov_ctl = IOV_READ(dinfo, PCIR_SRIOV_CTL, 2);
810 	iov_ctl |= PCIM_SRIOV_VF_EN | PCIM_SRIOV_VF_MSE;
811 	IOV_WRITE(dinfo, PCIR_SRIOV_CTL, iov_ctl, 2);
812 
813 	/* Per specification, we must wait 100ms before accessing VFs. */
814 	pause("iov", roundup(hz, 10));
815 	error = pci_iov_enumerate_vfs(dinfo, config, first_rid, rid_stride);
816 	if (error != 0)
817 		goto out;
818 
819 	nvlist_destroy(config);
820 	iov->iov_flags &= ~IOV_BUSY;
821 	mtx_unlock(&Giant);
822 
823 	return (0);
824 out:
825 	if (iov_inited) {
826 		PCI_IOV_UNINIT(dev);
827 		iov_ctl = IOV_READ(dinfo, PCIR_SRIOV_CTL, 2);
828 		iov_ctl &= ~(PCIM_SRIOV_VF_EN | PCIM_SRIOV_VF_MSE);
829 		IOV_WRITE(dinfo, PCIR_SRIOV_CTL, iov_ctl, 2);
830 		IOV_WRITE(dinfo, PCIR_SRIOV_NUM_VFS, 0, 2);
831 	}
832 
833 	for (i = 0; i <= PCIR_MAX_BAR_0; i++) {
834 		if (iov->iov_bar[i].res != NULL) {
835 			pci_release_resource(bus, dev, iov->iov_bar[i].res);
836 			pci_delete_resource(bus, dev, SYS_RES_MEMORY,
837 			    iov->iov_pos + PCIR_SRIOV_BAR(i));
838 			iov->iov_bar[i].res = NULL;
839 		}
840 	}
841 
842 	if (iov->iov_bus_res != NULL) {
843 		bus_release_resource(bus, iov->iov_bus_res);
844 		iov->iov_bus_res = NULL;
845 	}
846 
847 	if (iov->iov_flags & IOV_RMAN_INITED) {
848 		rman_fini(&iov->rman);
849 		iov->iov_flags &= ~IOV_RMAN_INITED;
850 	}
851 
852 	nvlist_destroy(config);
853 	iov->iov_num_vfs = 0;
854 	iov->iov_flags &= ~IOV_BUSY;
855 	mtx_unlock(&Giant);
856 	return (error);
857 }
858 
859 void
pci_iov_cfg_restore(device_t dev,struct pci_devinfo * dinfo)860 pci_iov_cfg_restore(device_t dev, struct pci_devinfo *dinfo)
861 {
862 	struct pcicfg_iov *iov;
863 
864 	iov = dinfo->cfg.iov;
865 
866 	IOV_WRITE(dinfo, PCIR_SRIOV_PAGE_SIZE, iov->iov_page_size, 4);
867 	IOV_WRITE(dinfo, PCIR_SRIOV_NUM_VFS, iov->iov_num_vfs, 2);
868 	IOV_WRITE(dinfo, PCIR_SRIOV_CTL, iov->iov_ctl, 2);
869 }
870 
871 void
pci_iov_cfg_save(device_t dev,struct pci_devinfo * dinfo)872 pci_iov_cfg_save(device_t dev, struct pci_devinfo *dinfo)
873 {
874 	struct pcicfg_iov *iov;
875 
876 	iov = dinfo->cfg.iov;
877 
878 	iov->iov_page_size = IOV_READ(dinfo, PCIR_SRIOV_PAGE_SIZE, 4);
879 	iov->iov_ctl = IOV_READ(dinfo, PCIR_SRIOV_CTL, 2);
880 }
881 
882 /* Return true if child is a VF of the given PF. */
883 static int
pci_iov_is_child_vf(struct pcicfg_iov * pf,device_t child)884 pci_iov_is_child_vf(struct pcicfg_iov *pf, device_t child)
885 {
886 	struct pci_devinfo *vfinfo;
887 
888 	vfinfo = device_get_ivars(child);
889 
890 	if (!(vfinfo->cfg.flags & PCICFG_VF))
891 		return (0);
892 
893 	return (pf == vfinfo->cfg.iov);
894 }
895 
896 static int
pci_iov_build_status(struct pci_devinfo * dinfo,nvlist_t ** statusp)897 pci_iov_build_status(struct pci_devinfo *dinfo, nvlist_t **statusp)
898 {
899 	const char *driver;
900 	device_t bus, child, dev, pcib, *devlist, *vfdevs;
901 	nvlist_t *pf, *status, **vfs;
902 	struct pcicfg_iov *iov;
903 	struct pci_devinfo *vfinfo;
904 	bool attached, passthrough;
905 	int busno, devcount, error, func, i, slot;
906 	uint16_t rid_off, rid_stride, vf_rid;
907 
908 	mtx_assert(&Giant, MA_OWNED);
909 
910 	iov = dinfo->cfg.iov;
911 	dev = dinfo->cfg.dev;
912 	bus = device_get_parent(dev);
913 	pcib = device_get_parent(bus);
914 	devlist = NULL;
915 	vfdevs = NULL;
916 	vfs = NULL;
917 	status = NULL;
918 	pf = NULL;
919 	error = 0;
920 
921 	if (iov->iov_num_vfs != 0) {
922 		vfdevs = mallocarray(iov->iov_num_vfs, sizeof(*vfdevs),
923 		    M_SRIOV, M_WAITOK | M_ZERO);
924 		error = device_get_children(bus, &devlist, &devcount);
925 		if (error != 0)
926 			goto out;
927 		for (i = 0; i < devcount; i++) {
928 			child = devlist[i];
929 			if (!pci_iov_is_child_vf(iov, child))
930 				continue;
931 			vfinfo = device_get_ivars(child);
932 			if (vfinfo->cfg.vf.index < iov->iov_num_vfs)
933 				vfdevs[vfinfo->cfg.vf.index] = child;
934 		}
935 	}
936 
937 	status = nvlist_create(0);
938 	pf = nvlist_create(0);
939 	if (status == NULL || pf == NULL) {
940 		error = ENOMEM;
941 		goto out;
942 	}
943 	nvlist_add_number(status, IOV_STATUS_VERSION_NAME, IOV_STATUS_VERSION);
944 	nvlist_add_string(pf, IOV_STATUS_DEVICE_NAME, device_get_nameunit(dev));
945 	nvlist_add_stringf(pf, IOV_STATUS_PCI_LOCATION_NAME, "pci%u:%u:%u:%u",
946 	    (u_int)pci_get_domain(dev), (u_int)pci_get_bus(dev),
947 	    (u_int)pci_get_slot(dev), (u_int)pci_get_function(dev));
948 	nvlist_add_bool(pf, IOV_STATUS_ENABLED_NAME,
949 	    (IOV_READ(dinfo, PCIR_SRIOV_CTL, 2) & PCIM_SRIOV_VF_EN) != 0);
950 	nvlist_add_number(pf, IOV_STATUS_NUM_VFS_NAME, iov->iov_num_vfs);
951 	nvlist_add_number(pf, IOV_STATUS_TOTAL_VFS_NAME,
952 	    IOV_READ(dinfo, PCIR_SRIOV_TOTAL_VFS, 2));
953 	error = nvlist_error(pf);
954 	if (error != 0)
955 		goto out;
956 	nvlist_move_nvlist(status, IOV_STATUS_PF_NAME, pf);
957 	pf = NULL;
958 
959 	if (iov->iov_num_vfs != 0)
960 		vfs = mallocarray(iov->iov_num_vfs, sizeof(*vfs), M_SRIOV,
961 		    M_WAITOK | M_ZERO);
962 	rid_off = IOV_READ(dinfo, PCIR_SRIOV_VF_OFF, 2);
963 	rid_stride = IOV_READ(dinfo, PCIR_SRIOV_VF_STRIDE, 2);
964 	vf_rid = pci_get_rid(dev) + rid_off;
965 	for (i = 0; i < iov->iov_num_vfs; i++, vf_rid += rid_stride) {
966 		vfs[i] = nvlist_create(0);
967 		if (vfs[i] == NULL) {
968 			error = ENOMEM;
969 			goto out;
970 		}
971 		nvlist_add_number(vfs[i], IOV_STATUS_VF_INDEX_NAME, i);
972 		child = vfdevs[i];
973 		if (child != NULL) {
974 			busno = pci_get_bus(child);
975 			slot = pci_get_slot(child);
976 			func = pci_get_function(child);
977 		} else
978 			PCIB_DECODE_RID(pcib, vf_rid, &busno, &slot, &func);
979 		nvlist_add_stringf(vfs[i], IOV_STATUS_PCI_LOCATION_NAME,
980 		    "pci%u:%u:%u:%u", (u_int)pci_get_domain(dev),
981 		    (u_int)busno, (u_int)slot, (u_int)func);
982 		attached = child != NULL && device_is_attached(child);
983 		passthrough = child != NULL && device_get_name(child) != NULL &&
984 		    strcmp(device_get_name(child), "ppt") == 0;
985 		nvlist_add_bool(vfs[i], IOV_STATUS_ATTACHED_NAME, attached);
986 		nvlist_add_bool(vfs[i], IOV_STATUS_PASSTHROUGH_NAME,
987 		    passthrough);
988 		if (attached) {
989 			driver = device_get_nameunit(child);
990 			if (driver != NULL)
991 				nvlist_add_string(vfs[i],
992 				    IOV_STATUS_BOUND_DRIVER_NAME, driver);
993 		}
994 		error = nvlist_error(vfs[i]);
995 		if (error != 0)
996 			goto out;
997 	}
998 	if (iov->iov_num_vfs != 0)
999 		nvlist_add_nvlist_array(status, IOV_STATUS_VFS_NAME,
1000 		    (const nvlist_t * const *)vfs, iov->iov_num_vfs);
1001 	error = nvlist_error(status);
1002 	if (error != 0)
1003 		goto out;
1004 	*statusp = status;
1005 	status = NULL;
1006 out:
1007 	if (vfs != NULL) {
1008 		for (i = 0; i < iov->iov_num_vfs; i++)
1009 			nvlist_destroy(vfs[i]);
1010 		free(vfs, M_SRIOV);
1011 	}
1012 	nvlist_destroy(pf);
1013 	nvlist_destroy(status);
1014 	free(vfdevs, M_SRIOV);
1015 	free(devlist, M_TEMP);
1016 	return (error);
1017 }
1018 
1019 static int
pci_iov_delete_iov_children(struct pci_devinfo * dinfo)1020 pci_iov_delete_iov_children(struct pci_devinfo *dinfo)
1021 {
1022 	device_t bus, dev, vf, *devlist;
1023 	struct pcicfg_iov *iov;
1024 	int i, error, devcount;
1025 	uint32_t iov_ctl;
1026 
1027 	mtx_assert(&Giant, MA_OWNED);
1028 
1029 	iov = dinfo->cfg.iov;
1030 	dev = dinfo->cfg.dev;
1031 	bus = device_get_parent(dev);
1032 	devlist = NULL;
1033 
1034 	iov->iov_flags |= IOV_BUSY;
1035 
1036 	error = device_get_children(bus, &devlist, &devcount);
1037 
1038 	if (error != 0)
1039 		goto out;
1040 
1041 	for (i = 0; i < devcount; i++) {
1042 		vf = devlist[i];
1043 
1044 		if (!pci_iov_is_child_vf(iov, vf))
1045 			continue;
1046 
1047 		error = device_detach(vf);
1048 		if (error != 0) {
1049 			device_printf(dev,
1050 			   "Could not disable SR-IOV: failed to detach VF %s\n",
1051 			    device_get_nameunit(vf));
1052 			goto out;
1053 		}
1054 	}
1055 
1056 	for (i = 0; i < devcount; i++) {
1057 		vf = devlist[i];
1058 
1059 		if (pci_iov_is_child_vf(iov, vf))
1060 			device_delete_child(bus, vf);
1061 	}
1062 	PCI_IOV_UNINIT(dev);
1063 
1064 	iov_ctl = IOV_READ(dinfo, PCIR_SRIOV_CTL, 2);
1065 	iov_ctl &= ~(PCIM_SRIOV_VF_EN | PCIM_SRIOV_VF_MSE);
1066 	IOV_WRITE(dinfo, PCIR_SRIOV_CTL, iov_ctl, 2);
1067 	IOV_WRITE(dinfo, PCIR_SRIOV_NUM_VFS, 0, 2);
1068 
1069 	iov->iov_num_vfs = 0;
1070 
1071 	for (i = 0; i <= PCIR_MAX_BAR_0; i++) {
1072 		if (iov->iov_bar[i].res != NULL) {
1073 			pci_release_resource(bus, dev, iov->iov_bar[i].res);
1074 			pci_delete_resource(bus, dev, SYS_RES_MEMORY,
1075 			    iov->iov_pos + PCIR_SRIOV_BAR(i));
1076 			iov->iov_bar[i].res = NULL;
1077 		}
1078 	}
1079 
1080 	if (iov->iov_bus_res != NULL) {
1081 		bus_release_resource(bus, iov->iov_bus_res);
1082 		iov->iov_bus_res = NULL;
1083 	}
1084 
1085 	if (iov->iov_flags & IOV_RMAN_INITED) {
1086 		rman_fini(&iov->rman);
1087 		iov->iov_flags &= ~IOV_RMAN_INITED;
1088 	}
1089 
1090 	error = 0;
1091 out:
1092 	free(devlist, M_TEMP);
1093 	iov->iov_flags &= ~IOV_BUSY;
1094 	return (error);
1095 }
1096 
1097 static int
pci_iov_delete(struct cdev * cdev)1098 pci_iov_delete(struct cdev *cdev)
1099 {
1100 	struct pci_devinfo *dinfo;
1101 	struct pcicfg_iov *iov;
1102 	int error;
1103 
1104 	mtx_lock(&Giant);
1105 	dinfo = cdev->si_drv1;
1106 	iov = dinfo->cfg.iov;
1107 
1108 	if ((iov->iov_flags & IOV_BUSY) != 0) {
1109 		error = EBUSY;
1110 		goto out;
1111 	}
1112 	if (iov->iov_num_vfs == 0) {
1113 		error = ECHILD;
1114 		goto out;
1115 	}
1116 
1117 	error = pci_iov_delete_iov_children(dinfo);
1118 
1119 out:
1120 	mtx_unlock(&Giant);
1121 	return (error);
1122 }
1123 
1124 static int
pci_iov_get_schema_ioctl(struct cdev * cdev,struct pci_iov_schema * output)1125 pci_iov_get_schema_ioctl(struct cdev *cdev, struct pci_iov_schema *output)
1126 {
1127 	struct pci_devinfo *dinfo;
1128 	void *packed;
1129 	size_t output_len, size;
1130 	int error;
1131 
1132 	packed = NULL;
1133 
1134 	mtx_lock(&Giant);
1135 	dinfo = cdev->si_drv1;
1136 	packed = nvlist_pack(dinfo->cfg.iov->iov_schema, &size);
1137 	mtx_unlock(&Giant);
1138 
1139 	if (packed == NULL) {
1140 		error = ENOMEM;
1141 		goto fail;
1142 	}
1143 
1144 	output_len = output->len;
1145 	output->len = size;
1146 	if (size <= output_len) {
1147 		error = copyout(packed, output->schema, size);
1148 
1149 		if (error != 0)
1150 			goto fail;
1151 
1152 		output->error = 0;
1153 	} else
1154 		/*
1155 		 * If we return an error then the ioctl code won't copyout
1156 		 * output back to userland, so we flag the error in the struct
1157 		 * instead.
1158 		 */
1159 		output->error = EMSGSIZE;
1160 
1161 	error = 0;
1162 
1163 fail:
1164 	free(packed, M_NVLIST);
1165 
1166 	return (error);
1167 }
1168 
1169 static int
pci_iov_get_status_ioctl(struct cdev * cdev,struct pci_iov_status * output)1170 pci_iov_get_status_ioctl(struct cdev *cdev, struct pci_iov_status *output)
1171 {
1172 	struct pci_devinfo *dinfo;
1173 	nvlist_t *status;
1174 	void *packed;
1175 	size_t output_len, size;
1176 	int error;
1177 
1178 	status = NULL;
1179 	packed = NULL;
1180 	mtx_lock(&Giant);
1181 	dinfo = cdev->si_drv1;
1182 	error = pci_iov_build_status(dinfo, &status);
1183 	mtx_unlock(&Giant);
1184 	if (error != 0)
1185 		goto out;
1186 
1187 	packed = nvlist_pack(status, &size);
1188 	if (packed == NULL) {
1189 		error = ENOMEM;
1190 		goto out;
1191 	}
1192 
1193 	output_len = output->len;
1194 	output->len = size;
1195 	if (size <= output_len) {
1196 		error = copyout(packed, output->status, size);
1197 		if (error != 0)
1198 			goto out;
1199 		output->error = 0;
1200 	} else {
1201 		/* Keep the ioctl successful so the required size is copied out. */
1202 		output->error = EMSGSIZE;
1203 	}
1204 	error = 0;
1205 out:
1206 	free(packed, M_NVLIST);
1207 	nvlist_destroy(status);
1208 	return (error);
1209 }
1210 
1211 static int
pci_iov_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)1212 pci_iov_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
1213     struct thread *td)
1214 {
1215 
1216 	switch (cmd) {
1217 	case IOV_CONFIG:
1218 		return (pci_iov_config(dev, (struct pci_iov_arg *)data));
1219 	case IOV_DELETE:
1220 		return (pci_iov_delete(dev));
1221 	case IOV_GET_SCHEMA:
1222 		return (pci_iov_get_schema_ioctl(dev,
1223 		    (struct pci_iov_schema *)data));
1224 	case IOV_GET_STATUS:
1225 		return (pci_iov_get_status_ioctl(dev,
1226 		    (struct pci_iov_status *)data));
1227 	default:
1228 		return (EINVAL);
1229 	}
1230 }
1231 
1232 struct resource *
pci_vf_alloc_mem_resource(device_t dev,device_t child,int rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)1233 pci_vf_alloc_mem_resource(device_t dev, device_t child, int rid,
1234     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
1235 {
1236 	struct pci_devinfo *dinfo;
1237 	struct pcicfg_iov *iov;
1238 	struct pci_map *map;
1239 	struct resource *res;
1240 	struct resource_list_entry *rle;
1241 	rman_res_t bar_start, bar_end;
1242 	pci_addr_t bar_length;
1243 	int error;
1244 
1245 	dinfo = device_get_ivars(child);
1246 	iov = dinfo->cfg.iov;
1247 
1248 	map = pci_find_bar(child, rid);
1249 	if (map == NULL)
1250 		return (NULL);
1251 
1252 	bar_length = 1 << map->pm_size;
1253 	bar_start = map->pm_value;
1254 	bar_end = bar_start + bar_length - 1;
1255 
1256 	/* Make sure that the resource fits the constraints. */
1257 	if (bar_start >= end || bar_end <= bar_start || count != 1)
1258 		return (NULL);
1259 
1260 	/* Clamp the resource to the constraints if necessary. */
1261 	if (bar_start < start)
1262 		bar_start = start;
1263 	if (bar_end > end)
1264 		bar_end = end;
1265 	bar_length = bar_end - bar_start + 1;
1266 
1267 	res = rman_reserve_resource(&iov->rman, bar_start, bar_end,
1268 	    bar_length, flags, child);
1269 	if (res == NULL)
1270 		return (NULL);
1271 
1272 	rle = resource_list_add(&dinfo->resources, SYS_RES_MEMORY, rid,
1273 	    bar_start, bar_end, 1);
1274 	if (rle == NULL) {
1275 		rman_release_resource(res);
1276 		return (NULL);
1277 	}
1278 
1279 	rman_set_rid(res, rid);
1280 	rman_set_type(res, SYS_RES_MEMORY);
1281 
1282 	if (flags & RF_ACTIVE) {
1283 		error = bus_activate_resource(child, SYS_RES_MEMORY, rid, res);
1284 		if (error != 0) {
1285 			resource_list_delete(&dinfo->resources, SYS_RES_MEMORY,
1286 			    rid);
1287 			rman_release_resource(res);
1288 			return (NULL);
1289 		}
1290 	}
1291 	rle->res = res;
1292 
1293 	return (res);
1294 }
1295 
1296 int
pci_vf_release_mem_resource(device_t dev,device_t child,struct resource * r)1297 pci_vf_release_mem_resource(device_t dev, device_t child, struct resource *r)
1298 {
1299 	struct pci_devinfo *dinfo;
1300 	struct resource_list_entry *rle;
1301 	int error, rid;
1302 
1303 	dinfo = device_get_ivars(child);
1304 
1305 	KASSERT(rman_get_type(r) == SYS_RES_MEMORY,
1306 	    ("%s: invalid resource %p", __func__, r));
1307 	KASSERT(rman_is_region_manager(r, &dinfo->cfg.iov->rman),
1308 	    ("%s: rman %p doesn't match for resource %p", __func__,
1309 	    &dinfo->cfg.iov->rman, r));
1310 
1311 	if (rman_get_flags(r) & RF_ACTIVE) {
1312 		error = bus_deactivate_resource(child, r);
1313 		if (error != 0)
1314 			return (error);
1315 	}
1316 
1317 	rid = rman_get_rid(r);
1318 	rle = resource_list_find(&dinfo->resources, SYS_RES_MEMORY, rid);
1319 	if (rle != NULL) {
1320 		rle->res = NULL;
1321 		resource_list_delete(&dinfo->resources, SYS_RES_MEMORY,
1322 		    rid);
1323 	}
1324 
1325 	return (rman_release_resource(r));
1326 }
1327 
1328 int
pci_vf_activate_mem_resource(device_t dev,device_t child,struct resource * r)1329 pci_vf_activate_mem_resource(device_t dev, device_t child, struct resource *r)
1330 {
1331 #ifdef INVARIANTS
1332 	struct pci_devinfo *dinfo = device_get_ivars(child);
1333 #endif
1334 	struct resource_map map;
1335 	int error;
1336 
1337 	KASSERT(rman_get_type(r) == SYS_RES_MEMORY,
1338 	    ("%s: invalid resource %p", __func__, r));
1339 	KASSERT(rman_is_region_manager(r, &dinfo->cfg.iov->rman),
1340 	    ("%s: rman %p doesn't match for resource %p", __func__,
1341 	    &dinfo->cfg.iov->rman, r));
1342 
1343 	error = rman_activate_resource(r);
1344 	if (error != 0)
1345 		return (error);
1346 
1347 	if ((rman_get_flags(r) & RF_UNMAPPED) == 0) {
1348 		error = BUS_MAP_RESOURCE(dev, child, r, NULL, &map);
1349 		if (error != 0) {
1350 			rman_deactivate_resource(r);
1351 			return (error);
1352 		}
1353 
1354 		rman_set_mapping(r, &map);
1355 	}
1356 	return (0);
1357 }
1358 
1359 int
pci_vf_deactivate_mem_resource(device_t dev,device_t child,struct resource * r)1360 pci_vf_deactivate_mem_resource(device_t dev, device_t child, struct resource *r)
1361 {
1362 #ifdef INVARIANTS
1363 	struct pci_devinfo *dinfo = device_get_ivars(child);
1364 #endif
1365 	struct resource_map map;
1366 	int error;
1367 
1368 	KASSERT(rman_get_type(r) == SYS_RES_MEMORY,
1369 	    ("%s: invalid resource %p", __func__, r));
1370 	KASSERT(rman_is_region_manager(r, &dinfo->cfg.iov->rman),
1371 	    ("%s: rman %p doesn't match for resource %p", __func__,
1372 	    &dinfo->cfg.iov->rman, r));
1373 
1374 	error = rman_deactivate_resource(r);
1375 	if (error != 0)
1376 		return (error);
1377 
1378 	if ((rman_get_flags(r) & RF_UNMAPPED) == 0) {
1379 		rman_get_mapping(r, &map);
1380 		BUS_UNMAP_RESOURCE(dev, child, r, &map);
1381 	}
1382 	return (0);
1383 }
1384 
1385 int
pci_vf_adjust_mem_resource(device_t dev,device_t child,struct resource * r,rman_res_t start,rman_res_t end)1386 pci_vf_adjust_mem_resource(device_t dev, device_t child, struct resource *r,
1387     rman_res_t start, rman_res_t end)
1388 {
1389 #ifdef INVARIANTS
1390 	struct pci_devinfo *dinfo = device_get_ivars(child);
1391 #endif
1392 
1393 	KASSERT(rman_get_type(r) == SYS_RES_MEMORY,
1394 	    ("%s: invalid resource %p", __func__, r));
1395 	KASSERT(rman_is_region_manager(r, &dinfo->cfg.iov->rman),
1396 	    ("%s: rman %p doesn't match for resource %p", __func__,
1397 	    &dinfo->cfg.iov->rman, r));
1398 
1399 	return (rman_adjust_resource(r, start, end));
1400 }
1401 
1402 static struct resource *
pci_vf_find_parent_resource(struct pcicfg_iov * iov,struct resource * r)1403 pci_vf_find_parent_resource(struct pcicfg_iov *iov, struct resource *r)
1404 {
1405 	struct resource *pres;
1406 
1407 	for (u_int i = 0; i <= PCIR_MAX_BAR_0; i++) {
1408 		pres = iov->iov_bar[i].res;
1409 		if (pres != NULL) {
1410 			if (rman_get_start(pres) <= rman_get_start(r) &&
1411 			    rman_get_end(pres) >= rman_get_end(r))
1412 				return (pres);
1413 		}
1414 	}
1415 	return (NULL);
1416 }
1417 
1418 int
pci_vf_map_mem_resource(device_t dev,device_t child,struct resource * r,struct resource_map_request * argsp,struct resource_map * map)1419 pci_vf_map_mem_resource(device_t dev, device_t child, struct resource *r,
1420     struct resource_map_request *argsp, struct resource_map *map)
1421 {
1422 	struct pci_devinfo *dinfo = device_get_ivars(child);
1423 	struct pcicfg_iov *iov = dinfo->cfg.iov;
1424 	struct resource_map_request args;
1425 	struct resource *pres;
1426 	rman_res_t length, start;
1427 	int error;
1428 
1429 	KASSERT(rman_get_type(r) == SYS_RES_MEMORY,
1430 	    ("%s: invalid resource %p", __func__, r));
1431 	KASSERT(rman_is_region_manager(r, &iov->rman),
1432 	    ("%s: rman %p doesn't match for resource %p", __func__,
1433 	    &dinfo->cfg.iov->rman, r));
1434 
1435 	/* Resources must be active to be mapped. */
1436 	if (!(rman_get_flags(r) & RF_ACTIVE))
1437 		return (ENXIO);
1438 
1439 	resource_init_map_request(&args);
1440 	error = resource_validate_map_request(r, argsp, &args, &start, &length);
1441 	if (error)
1442 		return (error);
1443 
1444 	pres = pci_vf_find_parent_resource(dinfo->cfg.iov, r);
1445 	if (pres == NULL)
1446 		return (ENOENT);
1447 
1448 	args.offset = start - rman_get_start(pres);
1449 	args.length = length;
1450 	return (bus_map_resource(iov->iov_pf, pres, &args, map));
1451 }
1452 
1453 int
pci_vf_unmap_mem_resource(device_t dev,device_t child,struct resource * r,struct resource_map * map)1454 pci_vf_unmap_mem_resource(device_t dev, device_t child, struct resource *r,
1455     struct resource_map *map)
1456 {
1457 	struct pci_devinfo *dinfo = device_get_ivars(child);
1458 	struct pcicfg_iov *iov = dinfo->cfg.iov;
1459 	struct resource *pres;
1460 
1461 	KASSERT(rman_get_type(r) == SYS_RES_MEMORY,
1462 	    ("%s: invalid resource %p", __func__, r));
1463 	KASSERT(rman_is_region_manager(r, &iov->rman),
1464 	    ("%s: rman %p doesn't match for resource %p", __func__,
1465 	    &dinfo->cfg.iov->rman, r));
1466 
1467 	pres = pci_vf_find_parent_resource(iov, r);
1468 	if (pres == NULL)
1469 		return (ENOENT);
1470 	return (bus_unmap_resource(iov->iov_pf, pres, map));
1471 }
1472