xref: /freebsd/sys/dev/pci/pci_iov.c (revision 8ef24a0d4b28fe230e20637f56869cc4148cd2ca)
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 __FBSDID("$FreeBSD$");
29 
30 #include "opt_bus.h"
31 
32 #include <sys/param.h>
33 #include <sys/conf.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/fcntl.h>
38 #include <sys/ioccom.h>
39 #include <sys/iov.h>
40 #include <sys/linker.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/pciio.h>
44 #include <sys/queue.h>
45 #include <sys/rman.h>
46 #include <sys/sysctl.h>
47 
48 #include <machine/bus.h>
49 #include <machine/stdarg.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 
85 #define IOV_READ(d, r, w) \
86 	pci_read_config((d)->cfg.dev, (d)->cfg.iov->iov_pos + r, w)
87 
88 #define IOV_WRITE(d, r, v, w) \
89 	pci_write_config((d)->cfg.dev, (d)->cfg.iov->iov_pos + r, v, w)
90 
91 static nvlist_t	*pci_iov_build_schema(nvlist_t **pf_schema,
92 		    nvlist_t **vf_schema);
93 static void	pci_iov_build_pf_schema(nvlist_t *schema,
94 		    nvlist_t **driver_schema);
95 static void	pci_iov_build_vf_schema(nvlist_t *schema,
96 		    nvlist_t **driver_schema);
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
101 pci_iov_attach_method(device_t bus, device_t dev, nvlist_t *pf_schema,
102     nvlist_t *vf_schema)
103 {
104 	device_t pcib;
105 	struct pci_devinfo *dinfo;
106 	struct pcicfg_iov *iov;
107 	nvlist_t *schema;
108 	uint32_t version;
109 	int error;
110 	int iov_pos;
111 
112 	dinfo = device_get_ivars(dev);
113 	pcib = device_get_parent(bus);
114 	schema = NULL;
115 
116 	error = pci_find_extcap(dev, PCIZ_SRIOV, &iov_pos);
117 
118 	if (error != 0)
119 		return (error);
120 
121 	version = pci_read_config(dev, iov_pos, 4);
122 	if (PCI_EXTCAP_VER(version) != 1) {
123 		if (bootverbose)
124 			device_printf(dev,
125 			    "Unsupported version of SR-IOV (%d) detected\n",
126 			    PCI_EXTCAP_VER(version));
127 
128 		return (ENXIO);
129 	}
130 
131 	iov = malloc(sizeof(*dinfo->cfg.iov), M_SRIOV, M_WAITOK | M_ZERO);
132 
133 	mtx_lock(&Giant);
134 	if (dinfo->cfg.iov != NULL) {
135 		error = EBUSY;
136 		goto cleanup;
137 	}
138 	iov->iov_pos = iov_pos;
139 
140 	schema = pci_iov_build_schema(&pf_schema, &vf_schema);
141 	if (schema == NULL) {
142 		error = ENOMEM;
143 		goto cleanup;
144 	}
145 
146 	error = pci_iov_validate_schema(schema);
147 	if (error != 0)
148 		goto cleanup;
149 	iov->iov_schema = schema;
150 
151 	iov->iov_cdev = make_dev(&iov_cdevsw, device_get_unit(dev),
152 	    UID_ROOT, GID_WHEEL, 0600, "iov/%s", device_get_nameunit(dev));
153 
154 	if (iov->iov_cdev == NULL) {
155 		error = ENOMEM;
156 		goto cleanup;
157 	}
158 
159 	dinfo->cfg.iov = iov;
160 	iov->iov_cdev->si_drv1 = dinfo;
161 	mtx_unlock(&Giant);
162 
163 	return (0);
164 
165 cleanup:
166 	nvlist_destroy(schema);
167 	nvlist_destroy(pf_schema);
168 	nvlist_destroy(vf_schema);
169 	free(iov, M_SRIOV);
170 	mtx_unlock(&Giant);
171 	return (error);
172 }
173 
174 int
175 pci_iov_detach_method(device_t bus, device_t dev)
176 {
177 	struct pci_devinfo *dinfo;
178 	struct pcicfg_iov *iov;
179 
180 	mtx_lock(&Giant);
181 	dinfo = device_get_ivars(dev);
182 	iov = dinfo->cfg.iov;
183 
184 	if (iov == NULL) {
185 		mtx_unlock(&Giant);
186 		return (0);
187 	}
188 
189 	if (iov->iov_num_vfs != 0 || iov->iov_flags & IOV_BUSY) {
190 		mtx_unlock(&Giant);
191 		return (EBUSY);
192 	}
193 
194 	dinfo->cfg.iov = NULL;
195 
196 	if (iov->iov_cdev) {
197 		destroy_dev(iov->iov_cdev);
198 		iov->iov_cdev = NULL;
199 	}
200 	nvlist_destroy(iov->iov_schema);
201 
202 	free(iov, M_SRIOV);
203 	mtx_unlock(&Giant);
204 
205 	return (0);
206 }
207 
208 static nvlist_t *
209 pci_iov_build_schema(nvlist_t **pf, nvlist_t **vf)
210 {
211 	nvlist_t *schema, *pf_driver, *vf_driver;
212 
213 	/* We always take ownership of the schemas. */
214 	pf_driver = *pf;
215 	*pf = NULL;
216 	vf_driver = *vf;
217 	*vf = NULL;
218 
219 	schema = pci_iov_schema_alloc_node();
220 	if (schema == NULL)
221 		goto cleanup;
222 
223 	pci_iov_build_pf_schema(schema, &pf_driver);
224 	pci_iov_build_vf_schema(schema, &vf_driver);
225 
226 	if (nvlist_error(schema) != 0)
227 		goto cleanup;
228 
229 	return (schema);
230 
231 cleanup:
232 	nvlist_destroy(schema);
233 	nvlist_destroy(pf_driver);
234 	nvlist_destroy(vf_driver);
235 	return (NULL);
236 }
237 
238 static void
239 pci_iov_build_pf_schema(nvlist_t *schema, nvlist_t **driver_schema)
240 {
241 	nvlist_t *pf_schema, *iov_schema;
242 
243 	pf_schema = pci_iov_schema_alloc_node();
244 	if (pf_schema == NULL) {
245 		nvlist_set_error(schema, ENOMEM);
246 		return;
247 	}
248 
249 	iov_schema = pci_iov_get_pf_subsystem_schema();
250 
251 	/*
252 	 * Note that if either *driver_schema or iov_schema is NULL, then
253 	 * nvlist_move_nvlist will put the schema in the error state and
254 	 * SR-IOV will fail to initialize later, so we don't have to explicitly
255 	 * handle that case.
256 	 */
257 	nvlist_move_nvlist(pf_schema, DRIVER_CONFIG_NAME, *driver_schema);
258 	nvlist_move_nvlist(pf_schema, IOV_CONFIG_NAME, iov_schema);
259 	nvlist_move_nvlist(schema, PF_CONFIG_NAME, pf_schema);
260 	*driver_schema = NULL;
261 }
262 
263 static void
264 pci_iov_build_vf_schema(nvlist_t *schema, nvlist_t **driver_schema)
265 {
266 	nvlist_t *vf_schema, *iov_schema;
267 
268 	vf_schema = pci_iov_schema_alloc_node();
269 	if (vf_schema == NULL) {
270 		nvlist_set_error(schema, ENOMEM);
271 		return;
272 	}
273 
274 	iov_schema = pci_iov_get_vf_subsystem_schema();
275 
276 	/*
277 	 * Note that if either *driver_schema or iov_schema is NULL, then
278 	 * nvlist_move_nvlist will put the schema in the error state and
279 	 * SR-IOV will fail to initialize later, so we don't have to explicitly
280 	 * handle that case.
281 	 */
282 	nvlist_move_nvlist(vf_schema, DRIVER_CONFIG_NAME, *driver_schema);
283 	nvlist_move_nvlist(vf_schema, IOV_CONFIG_NAME, iov_schema);
284 	nvlist_move_nvlist(schema, VF_SCHEMA_NAME, vf_schema);
285 	*driver_schema = NULL;
286 }
287 
288 static nvlist_t *
289 pci_iov_get_pf_subsystem_schema(void)
290 {
291 	nvlist_t *pf;
292 
293 	pf = pci_iov_schema_alloc_node();
294 	if (pf == NULL)
295 		return (NULL);
296 
297 	pci_iov_schema_add_uint16(pf, "num_vfs", IOV_SCHEMA_REQUIRED, -1);
298 	pci_iov_schema_add_string(pf, "device", IOV_SCHEMA_REQUIRED, NULL);
299 
300 	return (pf);
301 }
302 
303 static nvlist_t *
304 pci_iov_get_vf_subsystem_schema(void)
305 {
306 	nvlist_t *vf;
307 
308 	vf = pci_iov_schema_alloc_node();
309 	if (vf == NULL)
310 		return (NULL);
311 
312 	pci_iov_schema_add_bool(vf, "passthrough", IOV_SCHEMA_HASDEFAULT, 0);
313 
314 	return (vf);
315 }
316 
317 static int
318 pci_iov_alloc_bar(struct pci_devinfo *dinfo, int bar, pci_addr_t bar_shift)
319 {
320 	struct resource *res;
321 	struct pcicfg_iov *iov;
322 	device_t dev, bus;
323 	rman_res_t start, end;
324 	pci_addr_t bar_size;
325 	int rid;
326 
327 	iov = dinfo->cfg.iov;
328 	dev = dinfo->cfg.dev;
329 	bus = device_get_parent(dev);
330 	rid = iov->iov_pos + PCIR_SRIOV_BAR(bar);
331 	bar_size = 1 << bar_shift;
332 
333 	res = pci_alloc_multi_resource(bus, dev, SYS_RES_MEMORY, &rid, 0,
334 	    ~0, 1, iov->iov_num_vfs, RF_ACTIVE);
335 
336 	if (res == NULL)
337 		return (ENXIO);
338 
339 	iov->iov_bar[bar].res = res;
340 	iov->iov_bar[bar].bar_size = bar_size;
341 	iov->iov_bar[bar].bar_shift = bar_shift;
342 
343 	start = rman_get_start(res);
344 	end = rman_get_end(res);
345 	return (rman_manage_region(&iov->rman, start, end));
346 }
347 
348 static void
349 pci_iov_add_bars(struct pcicfg_iov *iov, struct pci_devinfo *dinfo)
350 {
351 	struct pci_iov_bar *bar;
352 	uint64_t bar_start;
353 	int i;
354 
355 	for (i = 0; i <= PCIR_MAX_BAR_0; i++) {
356 		bar = &iov->iov_bar[i];
357 		if (bar->res != NULL) {
358 			bar_start = rman_get_start(bar->res) +
359 			    dinfo->cfg.vf.index * bar->bar_size;
360 
361 			pci_add_bar(dinfo->cfg.dev, PCIR_BAR(i), bar_start,
362 			    bar->bar_shift);
363 		}
364 	}
365 }
366 
367 static int
368 pci_iov_parse_config(struct pcicfg_iov *iov, struct pci_iov_arg *arg,
369     nvlist_t **ret)
370 {
371 	void *packed_config;
372 	nvlist_t *config;
373 	int error;
374 
375 	config = NULL;
376 	packed_config = NULL;
377 
378 	if (arg->len > pci_iov_max_config) {
379 		error = EMSGSIZE;
380 		goto out;
381 	}
382 
383 	packed_config = malloc(arg->len, M_SRIOV, M_WAITOK);
384 
385 	error = copyin(arg->config, packed_config, arg->len);
386 	if (error != 0)
387 		goto out;
388 
389 	config = nvlist_unpack(packed_config, arg->len, NV_FLAG_IGNORE_CASE);
390 	if (config == NULL) {
391 		error = EINVAL;
392 		goto out;
393 	}
394 
395 	error = pci_iov_schema_validate_config(iov->iov_schema, config);
396 	if (error != 0)
397 		goto out;
398 
399 	error = nvlist_error(config);
400 	if (error != 0)
401 		goto out;
402 
403 	*ret = config;
404 	config = NULL;
405 
406 out:
407 	nvlist_destroy(config);
408 	free(packed_config, M_SRIOV);
409 	return (error);
410 }
411 
412 /*
413  * Set the ARI_EN bit in the lowest-numbered PCI function with the SR-IOV
414  * capability.  This bit is only writeable on the lowest-numbered PF but
415  * affects all PFs on the device.
416  */
417 static int
418 pci_iov_set_ari(device_t bus)
419 {
420 	device_t lowest;
421 	device_t *devlist;
422 	int i, error, devcount, lowest_func, lowest_pos, iov_pos, dev_func;
423 	uint16_t iov_ctl;
424 
425 	/* If ARI is disabled on the downstream port there is nothing to do. */
426 	if (!PCIB_ARI_ENABLED(device_get_parent(bus)))
427 		return (0);
428 
429 	error = device_get_children(bus, &devlist, &devcount);
430 
431 	if (error != 0)
432 		return (error);
433 
434 	lowest = NULL;
435 	for (i = 0; i < devcount; i++) {
436 		if (pci_find_extcap(devlist[i], PCIZ_SRIOV, &iov_pos) == 0) {
437 			dev_func = pci_get_function(devlist[i]);
438 			if (lowest == NULL || dev_func < lowest_func) {
439 				lowest = devlist[i];
440 				lowest_func = dev_func;
441 				lowest_pos = iov_pos;
442 			}
443 		}
444 	}
445 	free(devlist, M_TEMP);
446 
447 	/*
448 	 * If we called this function some device must have the SR-IOV
449 	 * capability.
450 	 */
451 	KASSERT(lowest != NULL,
452 	    ("Could not find child of %s with SR-IOV capability",
453 	    device_get_nameunit(bus)));
454 
455 	iov_ctl = pci_read_config(lowest, lowest_pos + PCIR_SRIOV_CTL, 2);
456 	iov_ctl |= PCIM_SRIOV_ARI_EN;
457 	pci_write_config(lowest, lowest_pos + PCIR_SRIOV_CTL, iov_ctl, 2);
458 	if ((pci_read_config(lowest, lowest_pos + PCIR_SRIOV_CTL, 2) &
459 	    PCIM_SRIOV_ARI_EN) == 0) {
460 		device_printf(lowest, "failed to enable ARI\n");
461 		return (ENXIO);
462 	}
463 	return (0);
464 }
465 
466 static int
467 pci_iov_config_page_size(struct pci_devinfo *dinfo)
468 {
469 	uint32_t page_cap, page_size;
470 
471 	page_cap = IOV_READ(dinfo, PCIR_SRIOV_PAGE_CAP, 4);
472 
473 	/*
474 	 * If the system page size is less than the smallest SR-IOV page size
475 	 * then round up to the smallest SR-IOV page size.
476 	 */
477 	if (PAGE_SHIFT < PCI_SRIOV_BASE_PAGE_SHIFT)
478 		page_size = (1 << 0);
479 	else
480 		page_size = (1 << (PAGE_SHIFT - PCI_SRIOV_BASE_PAGE_SHIFT));
481 
482 	/* Check that the device supports the system page size. */
483 	if (!(page_size & page_cap))
484 		return (ENXIO);
485 
486 	IOV_WRITE(dinfo, PCIR_SRIOV_PAGE_SIZE, page_size, 4);
487 	return (0);
488 }
489 
490 static int
491 pci_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *config)
492 {
493 	const nvlist_t *device, *driver_config;
494 
495 	device = nvlist_get_nvlist(config, PF_CONFIG_NAME);
496 	driver_config = nvlist_get_nvlist(device, DRIVER_CONFIG_NAME);
497 	return (PCI_IOV_INIT(dev, num_vfs, driver_config));
498 }
499 
500 static int
501 pci_iov_init_rman(device_t pf, struct pcicfg_iov *iov)
502 {
503 	int error;
504 
505 	iov->rman.rm_start = 0;
506 	iov->rman.rm_end = ~0;
507 	iov->rman.rm_type = RMAN_ARRAY;
508 	snprintf(iov->rman_name, sizeof(iov->rman_name), "%s VF I/O memory",
509 	    device_get_nameunit(pf));
510 	iov->rman.rm_descr = iov->rman_name;
511 
512 	error = rman_init(&iov->rman);
513 	if (error != 0)
514 		return (error);
515 
516 	iov->iov_flags |= IOV_RMAN_INITED;
517 	return (0);
518 }
519 
520 static int
521 pci_iov_alloc_bar_ea(struct pci_devinfo *dinfo, int bar)
522 {
523 	struct pcicfg_iov *iov;
524 	rman_res_t start, end;
525 	struct resource *res;
526 	struct resource_list *rl;
527 	struct resource_list_entry *rle;
528 
529 	rl = &dinfo->resources;
530 	iov = dinfo->cfg.iov;
531 
532 	rle = resource_list_find(rl, SYS_RES_MEMORY,
533 	    iov->iov_pos + PCIR_SRIOV_BAR(bar));
534 	if (rle == NULL)
535 		rle = resource_list_find(rl, SYS_RES_IOPORT,
536 		    iov->iov_pos + PCIR_SRIOV_BAR(bar));
537 	if (rle == NULL)
538 		return (ENXIO);
539 	res = rle->res;
540 
541 	iov->iov_bar[bar].res = res;
542 	iov->iov_bar[bar].bar_size = rman_get_size(res) / iov->iov_num_vfs;
543 	iov->iov_bar[bar].bar_shift = pci_mapsize(iov->iov_bar[bar].bar_size);
544 
545 	start = rman_get_start(res);
546 	end = rman_get_end(res);
547 
548 	return (rman_manage_region(&iov->rman, start, end));
549 }
550 
551 static int
552 pci_iov_setup_bars(struct pci_devinfo *dinfo)
553 {
554 	device_t dev;
555 	struct pcicfg_iov *iov;
556 	pci_addr_t bar_value, testval;
557 	int i, last_64, error;
558 
559 	iov = dinfo->cfg.iov;
560 	dev = dinfo->cfg.dev;
561 	last_64 = 0;
562 
563 	pci_add_resources_ea(device_get_parent(dev), dev, 1);
564 
565 	for (i = 0; i <= PCIR_MAX_BAR_0; i++) {
566 		/* First, try to use BARs allocated with EA */
567 		error = pci_iov_alloc_bar_ea(dinfo, i);
568 		if (error == 0)
569 			continue;
570 
571 		/* Allocate legacy-BAR only if EA is not enabled */
572 		if (pci_ea_is_enabled(dev, iov->iov_pos + PCIR_SRIOV_BAR(i)))
573 			continue;
574 
575 		/*
576 		 * If a PCI BAR is a 64-bit wide BAR, then it spans two
577 		 * consecutive registers.  Therefore if the last BAR that
578 		 * we looked at was a 64-bit BAR, we need to skip this
579 		 * register as it's the second half of the last BAR.
580 		 */
581 		if (!last_64) {
582 			pci_read_bar(dev,
583 			    iov->iov_pos + PCIR_SRIOV_BAR(i),
584 			    &bar_value, &testval, &last_64);
585 
586 			if (testval != 0) {
587 				error = pci_iov_alloc_bar(dinfo, i,
588 				   pci_mapsize(testval));
589 				if (error != 0)
590 					return (error);
591 			}
592 		} else
593 			last_64 = 0;
594 	}
595 
596 	return (0);
597 }
598 
599 static void
600 pci_iov_enumerate_vfs(struct pci_devinfo *dinfo, const nvlist_t *config,
601     uint16_t first_rid, uint16_t rid_stride)
602 {
603 	char device_name[VF_MAX_NAME];
604 	const nvlist_t *device, *driver_config, *iov_config;
605 	device_t bus, dev, vf;
606 	struct pcicfg_iov *iov;
607 	struct pci_devinfo *vfinfo;
608 	int i, error;
609 	uint16_t vid, did, next_rid;
610 
611 	iov = dinfo->cfg.iov;
612 	dev = dinfo->cfg.dev;
613 	bus = device_get_parent(dev);
614 	next_rid = first_rid;
615 	vid = pci_get_vendor(dev);
616 	did = IOV_READ(dinfo, PCIR_SRIOV_VF_DID, 2);
617 
618 	for (i = 0; i < iov->iov_num_vfs; i++, next_rid += rid_stride) {
619 		snprintf(device_name, sizeof(device_name), VF_PREFIX"%d", i);
620 		device = nvlist_get_nvlist(config, device_name);
621 		iov_config = nvlist_get_nvlist(device, IOV_CONFIG_NAME);
622 		driver_config = nvlist_get_nvlist(device, DRIVER_CONFIG_NAME);
623 
624 		vf = PCI_CREATE_IOV_CHILD(bus, dev, next_rid, vid, did);
625 		if (vf == NULL)
626 			break;
627 
628 		/*
629 		 * If we are creating passthrough devices then force the ppt
630 		 * driver to attach to prevent a VF driver from claiming the
631 		 * VFs.
632 		 */
633 		if (nvlist_get_bool(iov_config, "passthrough"))
634 			device_set_devclass_fixed(vf, "ppt");
635 
636 		vfinfo = device_get_ivars(vf);
637 
638 		vfinfo->cfg.iov = iov;
639 		vfinfo->cfg.vf.index = i;
640 
641 		pci_iov_add_bars(iov, vfinfo);
642 
643 		error = PCI_IOV_ADD_VF(dev, i, driver_config);
644 		if (error != 0) {
645 			device_printf(dev, "Failed to add VF %d\n", i);
646 			device_delete_child(bus, vf);
647 		}
648 	}
649 
650 	bus_generic_attach(bus);
651 }
652 
653 static int
654 pci_iov_config(struct cdev *cdev, struct pci_iov_arg *arg)
655 {
656 	device_t bus, dev;
657 	struct pci_devinfo *dinfo;
658 	struct pcicfg_iov *iov;
659 	nvlist_t *config;
660 	int i, error;
661 	uint16_t rid_off, rid_stride;
662 	uint16_t first_rid, last_rid;
663 	uint16_t iov_ctl;
664 	uint16_t num_vfs, total_vfs;
665 	int iov_inited;
666 
667 	mtx_lock(&Giant);
668 	dinfo = cdev->si_drv1;
669 	iov = dinfo->cfg.iov;
670 	dev = dinfo->cfg.dev;
671 	bus = device_get_parent(dev);
672 	iov_inited = 0;
673 	config = NULL;
674 
675 	if ((iov->iov_flags & IOV_BUSY) || iov->iov_num_vfs != 0) {
676 		mtx_unlock(&Giant);
677 		return (EBUSY);
678 	}
679 	iov->iov_flags |= IOV_BUSY;
680 
681 	error = pci_iov_parse_config(iov, arg, &config);
682 	if (error != 0)
683 		goto out;
684 
685 	num_vfs = pci_iov_config_get_num_vfs(config);
686 	total_vfs = IOV_READ(dinfo, PCIR_SRIOV_TOTAL_VFS, 2);
687 	if (num_vfs > total_vfs) {
688 		error = EINVAL;
689 		goto out;
690 	}
691 
692 	error = pci_iov_config_page_size(dinfo);
693 	if (error != 0)
694 		goto out;
695 
696 	error = pci_iov_set_ari(bus);
697 	if (error != 0)
698 		goto out;
699 
700 	error = pci_iov_init(dev, num_vfs, config);
701 	if (error != 0)
702 		goto out;
703 	iov_inited = 1;
704 
705 	IOV_WRITE(dinfo, PCIR_SRIOV_NUM_VFS, num_vfs, 2);
706 
707 	rid_off = IOV_READ(dinfo, PCIR_SRIOV_VF_OFF, 2);
708 	rid_stride = IOV_READ(dinfo, PCIR_SRIOV_VF_STRIDE, 2);
709 
710 	first_rid = pci_get_rid(dev) + rid_off;
711 	last_rid = first_rid + (num_vfs - 1) * rid_stride;
712 
713 	/* We don't yet support allocating extra bus numbers for VFs. */
714 	if (pci_get_bus(dev) != PCI_RID2BUS(last_rid)) {
715 		error = ENOSPC;
716 		goto out;
717 	}
718 
719 	iov_ctl = IOV_READ(dinfo, PCIR_SRIOV_CTL, 2);
720 	iov_ctl &= ~(PCIM_SRIOV_VF_EN | PCIM_SRIOV_VF_MSE);
721 	IOV_WRITE(dinfo, PCIR_SRIOV_CTL, iov_ctl, 2);
722 
723 	error = pci_iov_init_rman(dev, iov);
724 	if (error != 0)
725 		goto out;
726 
727 	iov->iov_num_vfs = num_vfs;
728 
729 	error = pci_iov_setup_bars(dinfo);
730 	if (error != 0)
731 		goto out;
732 
733 	iov_ctl = IOV_READ(dinfo, PCIR_SRIOV_CTL, 2);
734 	iov_ctl |= PCIM_SRIOV_VF_EN | PCIM_SRIOV_VF_MSE;
735 	IOV_WRITE(dinfo, PCIR_SRIOV_CTL, iov_ctl, 2);
736 
737 	/* Per specification, we must wait 100ms before accessing VFs. */
738 	pause("iov", roundup(hz, 10));
739 	pci_iov_enumerate_vfs(dinfo, config, first_rid, rid_stride);
740 
741 	nvlist_destroy(config);
742 	iov->iov_flags &= ~IOV_BUSY;
743 	mtx_unlock(&Giant);
744 
745 	return (0);
746 out:
747 	if (iov_inited)
748 		PCI_IOV_UNINIT(dev);
749 
750 	for (i = 0; i <= PCIR_MAX_BAR_0; i++) {
751 		if (iov->iov_bar[i].res != NULL) {
752 			pci_release_resource(bus, dev, SYS_RES_MEMORY,
753 			    iov->iov_pos + PCIR_SRIOV_BAR(i),
754 			    iov->iov_bar[i].res);
755 			pci_delete_resource(bus, dev, SYS_RES_MEMORY,
756 			    iov->iov_pos + PCIR_SRIOV_BAR(i));
757 			iov->iov_bar[i].res = NULL;
758 		}
759 	}
760 
761 	if (iov->iov_flags & IOV_RMAN_INITED) {
762 		rman_fini(&iov->rman);
763 		iov->iov_flags &= ~IOV_RMAN_INITED;
764 	}
765 
766 	nvlist_destroy(config);
767 	iov->iov_num_vfs = 0;
768 	iov->iov_flags &= ~IOV_BUSY;
769 	mtx_unlock(&Giant);
770 	return (error);
771 }
772 
773 void
774 pci_iov_cfg_restore(device_t dev, struct pci_devinfo *dinfo)
775 {
776 	struct pcicfg_iov *iov;
777 
778 	iov = dinfo->cfg.iov;
779 
780 	IOV_WRITE(dinfo, PCIR_SRIOV_PAGE_SIZE, iov->iov_page_size, 4);
781 	IOV_WRITE(dinfo, PCIR_SRIOV_NUM_VFS, iov->iov_num_vfs, 2);
782 	IOV_WRITE(dinfo, PCIR_SRIOV_CTL, iov->iov_ctl, 2);
783 }
784 
785 void
786 pci_iov_cfg_save(device_t dev, struct pci_devinfo *dinfo)
787 {
788 	struct pcicfg_iov *iov;
789 
790 	iov = dinfo->cfg.iov;
791 
792 	iov->iov_page_size = IOV_READ(dinfo, PCIR_SRIOV_PAGE_SIZE, 4);
793 	iov->iov_ctl = IOV_READ(dinfo, PCIR_SRIOV_CTL, 2);
794 }
795 
796 /* Return true if child is a VF of the given PF. */
797 static int
798 pci_iov_is_child_vf(struct pcicfg_iov *pf, device_t child)
799 {
800 	struct pci_devinfo *vfinfo;
801 
802 	vfinfo = device_get_ivars(child);
803 
804 	if (!(vfinfo->cfg.flags & PCICFG_VF))
805 		return (0);
806 
807 	return (pf == vfinfo->cfg.iov);
808 }
809 
810 static int
811 pci_iov_delete(struct cdev *cdev)
812 {
813 	device_t bus, dev, vf, *devlist;
814 	struct pci_devinfo *dinfo;
815 	struct pcicfg_iov *iov;
816 	int i, error, devcount;
817 	uint32_t iov_ctl;
818 
819 	mtx_lock(&Giant);
820 	dinfo = cdev->si_drv1;
821 	iov = dinfo->cfg.iov;
822 	dev = dinfo->cfg.dev;
823 	bus = device_get_parent(dev);
824 	devlist = NULL;
825 
826 	if (iov->iov_flags & IOV_BUSY) {
827 		mtx_unlock(&Giant);
828 		return (EBUSY);
829 	}
830 
831 	if (iov->iov_num_vfs == 0) {
832 		mtx_unlock(&Giant);
833 		return (ECHILD);
834 	}
835 
836 	iov->iov_flags |= IOV_BUSY;
837 
838 	error = device_get_children(bus, &devlist, &devcount);
839 
840 	if (error != 0)
841 		goto out;
842 
843 	for (i = 0; i < devcount; i++) {
844 		vf = devlist[i];
845 
846 		if (!pci_iov_is_child_vf(iov, vf))
847 			continue;
848 
849 		error = device_detach(vf);
850 		if (error != 0) {
851 			device_printf(dev,
852 			   "Could not disable SR-IOV: failed to detach VF %s\n",
853 			    device_get_nameunit(vf));
854 			goto out;
855 		}
856 	}
857 
858 	for (i = 0; i < devcount; i++) {
859 		vf = devlist[i];
860 
861 		if (pci_iov_is_child_vf(iov, vf))
862 			device_delete_child(bus, vf);
863 	}
864 	PCI_IOV_UNINIT(dev);
865 
866 	iov_ctl = IOV_READ(dinfo, PCIR_SRIOV_CTL, 2);
867 	iov_ctl &= ~(PCIM_SRIOV_VF_EN | PCIM_SRIOV_VF_MSE);
868 	IOV_WRITE(dinfo, PCIR_SRIOV_CTL, iov_ctl, 2);
869 	IOV_WRITE(dinfo, PCIR_SRIOV_NUM_VFS, 0, 2);
870 
871 	iov->iov_num_vfs = 0;
872 
873 	for (i = 0; i <= PCIR_MAX_BAR_0; i++) {
874 		if (iov->iov_bar[i].res != NULL) {
875 			pci_release_resource(bus, dev, SYS_RES_MEMORY,
876 			    iov->iov_pos + PCIR_SRIOV_BAR(i),
877 			    iov->iov_bar[i].res);
878 			pci_delete_resource(bus, dev, SYS_RES_MEMORY,
879 			    iov->iov_pos + PCIR_SRIOV_BAR(i));
880 			iov->iov_bar[i].res = NULL;
881 		}
882 	}
883 
884 	if (iov->iov_flags & IOV_RMAN_INITED) {
885 		rman_fini(&iov->rman);
886 		iov->iov_flags &= ~IOV_RMAN_INITED;
887 	}
888 
889 	error = 0;
890 out:
891 	free(devlist, M_TEMP);
892 	iov->iov_flags &= ~IOV_BUSY;
893 	mtx_unlock(&Giant);
894 	return (error);
895 }
896 
897 static int
898 pci_iov_get_schema_ioctl(struct cdev *cdev, struct pci_iov_schema *output)
899 {
900 	struct pci_devinfo *dinfo;
901 	void *packed;
902 	size_t output_len, size;
903 	int error;
904 
905 	packed = NULL;
906 
907 	mtx_lock(&Giant);
908 	dinfo = cdev->si_drv1;
909 	packed = nvlist_pack(dinfo->cfg.iov->iov_schema, &size);
910 	mtx_unlock(&Giant);
911 
912 	if (packed == NULL) {
913 		error = ENOMEM;
914 		goto fail;
915 	}
916 
917 	output_len = output->len;
918 	output->len = size;
919 	if (size <= output_len) {
920 		error = copyout(packed, output->schema, size);
921 
922 		if (error != 0)
923 			goto fail;
924 
925 		output->error = 0;
926 	} else
927 		/*
928 		 * If we return an error then the ioctl code won't copyout
929 		 * output back to userland, so we flag the error in the struct
930 		 * instead.
931 		 */
932 		output->error = EMSGSIZE;
933 
934 	error = 0;
935 
936 fail:
937 	free(packed, M_NVLIST);
938 
939 	return (error);
940 }
941 
942 static int
943 pci_iov_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
944     struct thread *td)
945 {
946 
947 	switch (cmd) {
948 	case IOV_CONFIG:
949 		return (pci_iov_config(dev, (struct pci_iov_arg *)data));
950 	case IOV_DELETE:
951 		return (pci_iov_delete(dev));
952 	case IOV_GET_SCHEMA:
953 		return (pci_iov_get_schema_ioctl(dev,
954 		    (struct pci_iov_schema *)data));
955 	default:
956 		return (EINVAL);
957 	}
958 }
959 
960 struct resource *
961 pci_vf_alloc_mem_resource(device_t dev, device_t child, int *rid,
962     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
963 {
964 	struct pci_devinfo *dinfo;
965 	struct pcicfg_iov *iov;
966 	struct pci_map *map;
967 	struct resource *res;
968 	struct resource_list_entry *rle;
969 	rman_res_t bar_start, bar_end;
970 	pci_addr_t bar_length;
971 	int error;
972 
973 	dinfo = device_get_ivars(child);
974 	iov = dinfo->cfg.iov;
975 
976 	map = pci_find_bar(child, *rid);
977 	if (map == NULL)
978 		return (NULL);
979 
980 	bar_length = 1 << map->pm_size;
981 	bar_start = map->pm_value;
982 	bar_end = bar_start + bar_length - 1;
983 
984 	/* Make sure that the resource fits the constraints. */
985 	if (bar_start >= end || bar_end <= bar_start || count != 1)
986 		return (NULL);
987 
988 	/* Clamp the resource to the constraints if necessary. */
989 	if (bar_start < start)
990 		bar_start = start;
991 	if (bar_end > end)
992 		bar_end = end;
993 	bar_length = bar_end - bar_start + 1;
994 
995 	res = rman_reserve_resource(&iov->rman, bar_start, bar_end,
996 	    bar_length, flags, child);
997 	if (res == NULL)
998 		return (NULL);
999 
1000 	rle = resource_list_add(&dinfo->resources, SYS_RES_MEMORY, *rid,
1001 	    bar_start, bar_end, 1);
1002 	if (rle == NULL) {
1003 		rman_release_resource(res);
1004 		return (NULL);
1005 	}
1006 
1007 	rman_set_rid(res, *rid);
1008 
1009 	if (flags & RF_ACTIVE) {
1010 		error = bus_activate_resource(child, SYS_RES_MEMORY, *rid, res);
1011 		if (error != 0) {
1012 			resource_list_delete(&dinfo->resources, SYS_RES_MEMORY,
1013 			    *rid);
1014 			rman_release_resource(res);
1015 			return (NULL);
1016 		}
1017 	}
1018 	rle->res = res;
1019 
1020 	return (res);
1021 }
1022 
1023 int
1024 pci_vf_release_mem_resource(device_t dev, device_t child, int rid,
1025     struct resource *r)
1026 {
1027 	struct pci_devinfo *dinfo;
1028 	struct resource_list_entry *rle;
1029 	int error;
1030 
1031 	dinfo = device_get_ivars(child);
1032 
1033 	if (rman_get_flags(r) & RF_ACTIVE) {
1034 		error = bus_deactivate_resource(child, SYS_RES_MEMORY, rid, r);
1035 		if (error != 0)
1036 			return (error);
1037 	}
1038 
1039 	rle = resource_list_find(&dinfo->resources, SYS_RES_MEMORY, rid);
1040 	if (rle != NULL) {
1041 		rle->res = NULL;
1042 		resource_list_delete(&dinfo->resources, SYS_RES_MEMORY,
1043 		    rid);
1044 	}
1045 
1046 	return (rman_release_resource(r));
1047 }
1048 
1049