1/*-
2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
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 unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: stable/10/sys/dev/pci/pci_user.c 339932 2018-10-30 21:31:32Z jhb $");
29
30#include "opt_bus.h"	/* XXX trim includes */
31#include "opt_compat.h"
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/malloc.h>
36#include <sys/module.h>
37#include <sys/linker.h>
38#include <sys/fcntl.h>
39#include <sys/conf.h>
40#include <sys/kernel.h>
41#include <sys/proc.h>
42#include <sys/queue.h>
43#include <sys/types.h>
44
45#include <vm/vm.h>
46#include <vm/pmap.h>
47#include <vm/vm_extern.h>
48
49#include <sys/bus.h>
50#include <machine/bus.h>
51#include <sys/rman.h>
52#include <machine/resource.h>
53
54#include <sys/pciio.h>
55#include <dev/pci/pcireg.h>
56#include <dev/pci/pcivar.h>
57
58#include "pcib_if.h"
59#include "pci_if.h"
60
61/*
62 * This is the user interface to PCI configuration space.
63 */
64
65static d_open_t 	pci_open;
66static d_close_t	pci_close;
67static int	pci_conf_match(struct pci_match_conf *matches, int num_matches,
68			       struct pci_conf *match_buf);
69static d_ioctl_t	pci_ioctl;
70
71struct cdevsw pcicdev = {
72	.d_version =	D_VERSION,
73	.d_flags =	D_NEEDGIANT,
74	.d_open =	pci_open,
75	.d_close =	pci_close,
76	.d_ioctl =	pci_ioctl,
77	.d_name =	"pci",
78};
79
80static int
81pci_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
82{
83	int error;
84
85	if (oflags & FWRITE) {
86		error = securelevel_gt(td->td_ucred, 0);
87		if (error)
88			return (error);
89	}
90
91	return (0);
92}
93
94static int
95pci_close(struct cdev *dev, int flag, int devtype, struct thread *td)
96{
97	return 0;
98}
99
100/*
101 * Match a single pci_conf structure against an array of pci_match_conf
102 * structures.  The first argument, 'matches', is an array of num_matches
103 * pci_match_conf structures.  match_buf is a pointer to the pci_conf
104 * structure that will be compared to every entry in the matches array.
105 * This function returns 1 on failure, 0 on success.
106 */
107static int
108pci_conf_match(struct pci_match_conf *matches, int num_matches,
109	       struct pci_conf *match_buf)
110{
111	int i;
112
113	if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
114		return(1);
115
116	for (i = 0; i < num_matches; i++) {
117		/*
118		 * I'm not sure why someone would do this...but...
119		 */
120		if (matches[i].flags == PCI_GETCONF_NO_MATCH)
121			continue;
122
123		/*
124		 * Look at each of the match flags.  If it's set, do the
125		 * comparison.  If the comparison fails, we don't have a
126		 * match, go on to the next item if there is one.
127		 */
128		if (((matches[i].flags & PCI_GETCONF_MATCH_DOMAIN) != 0)
129		 && (match_buf->pc_sel.pc_domain !=
130		 matches[i].pc_sel.pc_domain))
131			continue;
132
133		if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0)
134		 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
135			continue;
136
137		if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0)
138		 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
139			continue;
140
141		if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0)
142		 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
143			continue;
144
145		if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0)
146		 && (match_buf->pc_vendor != matches[i].pc_vendor))
147			continue;
148
149		if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0)
150		 && (match_buf->pc_device != matches[i].pc_device))
151			continue;
152
153		if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0)
154		 && (match_buf->pc_class != matches[i].pc_class))
155			continue;
156
157		if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0)
158		 && (match_buf->pd_unit != matches[i].pd_unit))
159			continue;
160
161		if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0)
162		 && (strncmp(matches[i].pd_name, match_buf->pd_name,
163			     sizeof(match_buf->pd_name)) != 0))
164			continue;
165
166		return(0);
167	}
168
169	return(1);
170}
171
172#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
173    defined(COMPAT_FREEBSD6)
174#define PRE7_COMPAT
175
176typedef enum {
177	PCI_GETCONF_NO_MATCH_OLD	= 0x00,
178	PCI_GETCONF_MATCH_BUS_OLD	= 0x01,
179	PCI_GETCONF_MATCH_DEV_OLD	= 0x02,
180	PCI_GETCONF_MATCH_FUNC_OLD	= 0x04,
181	PCI_GETCONF_MATCH_NAME_OLD	= 0x08,
182	PCI_GETCONF_MATCH_UNIT_OLD	= 0x10,
183	PCI_GETCONF_MATCH_VENDOR_OLD	= 0x20,
184	PCI_GETCONF_MATCH_DEVICE_OLD	= 0x40,
185	PCI_GETCONF_MATCH_CLASS_OLD	= 0x80
186} pci_getconf_flags_old;
187
188struct pcisel_old {
189	u_int8_t	pc_bus;		/* bus number */
190	u_int8_t	pc_dev;		/* device on this bus */
191	u_int8_t	pc_func;	/* function on this device */
192};
193
194struct pci_conf_old {
195	struct pcisel_old pc_sel;	/* bus+slot+function */
196	u_int8_t	pc_hdr;		/* PCI header type */
197	u_int16_t	pc_subvendor;	/* card vendor ID */
198	u_int16_t	pc_subdevice;	/* card device ID, assigned by
199					   card vendor */
200	u_int16_t	pc_vendor;	/* chip vendor ID */
201	u_int16_t	pc_device;	/* chip device ID, assigned by
202					   chip vendor */
203	u_int8_t	pc_class;	/* chip PCI class */
204	u_int8_t	pc_subclass;	/* chip PCI subclass */
205	u_int8_t	pc_progif;	/* chip PCI programming interface */
206	u_int8_t	pc_revid;	/* chip revision ID */
207	char		pd_name[PCI_MAXNAMELEN + 1];  /* device name */
208	u_long		pd_unit;	/* device unit number */
209};
210
211struct pci_match_conf_old {
212	struct pcisel_old	pc_sel;		/* bus+slot+function */
213	char			pd_name[PCI_MAXNAMELEN + 1];  /* device name */
214	u_long			pd_unit;	/* Unit number */
215	u_int16_t		pc_vendor;	/* PCI Vendor ID */
216	u_int16_t		pc_device;	/* PCI Device ID */
217	u_int8_t		pc_class;	/* PCI class */
218	pci_getconf_flags_old	flags;		/* Matching expression */
219};
220
221struct pci_io_old {
222	struct pcisel_old pi_sel;	/* device to operate on */
223	int		pi_reg;		/* configuration register to examine */
224	int		pi_width;	/* width (in bytes) of read or write */
225	u_int32_t	pi_data;	/* data to write or result of read */
226};
227
228#ifdef COMPAT_FREEBSD32
229struct pci_conf_old32 {
230	struct pcisel_old pc_sel;	/* bus+slot+function */
231	uint8_t		pc_hdr;		/* PCI header type */
232	uint16_t	pc_subvendor;	/* card vendor ID */
233	uint16_t	pc_subdevice;	/* card device ID, assigned by
234					   card vendor */
235	uint16_t	pc_vendor;	/* chip vendor ID */
236	uint16_t	pc_device;	/* chip device ID, assigned by
237					   chip vendor */
238	uint8_t		pc_class;	/* chip PCI class */
239	uint8_t		pc_subclass;	/* chip PCI subclass */
240	uint8_t		pc_progif;	/* chip PCI programming interface */
241	uint8_t		pc_revid;	/* chip revision ID */
242	char		pd_name[PCI_MAXNAMELEN + 1]; /* device name */
243	uint32_t	pd_unit;	/* device unit number (u_long) */
244};
245
246struct pci_match_conf_old32 {
247	struct pcisel_old pc_sel;	/* bus+slot+function */
248	char		pd_name[PCI_MAXNAMELEN + 1]; /* device name */
249	uint32_t	pd_unit;	/* Unit number (u_long) */
250	uint16_t	pc_vendor;	/* PCI Vendor ID */
251	uint16_t	pc_device;	/* PCI Device ID */
252	uint8_t		pc_class;	/* PCI class */
253	pci_getconf_flags_old flags;	/* Matching expression */
254};
255
256struct pci_conf_io32 {
257	uint32_t	pat_buf_len;	/* pattern buffer length */
258	uint32_t	num_patterns;	/* number of patterns */
259	uint32_t	patterns;	/* pattern buffer
260					   (struct pci_match_conf_old32 *) */
261	uint32_t	match_buf_len;	/* match buffer length */
262	uint32_t	num_matches;	/* number of matches returned */
263	uint32_t	matches;	/* match buffer
264					   (struct pci_conf_old32 *) */
265	uint32_t	offset;		/* offset into device list */
266	uint32_t	generation;	/* device list generation */
267	pci_getconf_status status;	/* request status */
268};
269
270#define	PCIOCGETCONF_OLD32	_IOWR('p', 1, struct pci_conf_io32)
271#endif	/* COMPAT_FREEBSD32 */
272
273#define	PCIOCGETCONF_OLD	_IOWR('p', 1, struct pci_conf_io)
274#define	PCIOCREAD_OLD		_IOWR('p', 2, struct pci_io_old)
275#define	PCIOCWRITE_OLD		_IOWR('p', 3, struct pci_io_old)
276
277static int	pci_conf_match_old(struct pci_match_conf_old *matches,
278		    int num_matches, struct pci_conf *match_buf);
279
280static int
281pci_conf_match_old(struct pci_match_conf_old *matches, int num_matches,
282    struct pci_conf *match_buf)
283{
284	int i;
285
286	if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
287		return(1);
288
289	for (i = 0; i < num_matches; i++) {
290		if (match_buf->pc_sel.pc_domain != 0)
291			continue;
292
293		/*
294		 * I'm not sure why someone would do this...but...
295		 */
296		if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD)
297			continue;
298
299		/*
300		 * Look at each of the match flags.  If it's set, do the
301		 * comparison.  If the comparison fails, we don't have a
302		 * match, go on to the next item if there is one.
303		 */
304		if (((matches[i].flags & PCI_GETCONF_MATCH_BUS_OLD) != 0)
305		 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
306			continue;
307
308		if (((matches[i].flags & PCI_GETCONF_MATCH_DEV_OLD) != 0)
309		 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
310			continue;
311
312		if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC_OLD) != 0)
313		 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
314			continue;
315
316		if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0)
317		 && (match_buf->pc_vendor != matches[i].pc_vendor))
318			continue;
319
320		if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0)
321		 && (match_buf->pc_device != matches[i].pc_device))
322			continue;
323
324		if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0)
325		 && (match_buf->pc_class != matches[i].pc_class))
326			continue;
327
328		if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0)
329		 && (match_buf->pd_unit != matches[i].pd_unit))
330			continue;
331
332		if (((matches[i].flags & PCI_GETCONF_MATCH_NAME_OLD) != 0)
333		 && (strncmp(matches[i].pd_name, match_buf->pd_name,
334			     sizeof(match_buf->pd_name)) != 0))
335			continue;
336
337		return(0);
338	}
339
340	return(1);
341}
342
343#ifdef COMPAT_FREEBSD32
344static int
345pci_conf_match_old32(struct pci_match_conf_old32 *matches, int num_matches,
346    struct pci_conf *match_buf)
347{
348	int i;
349
350	if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
351		return(1);
352
353	for (i = 0; i < num_matches; i++) {
354		if (match_buf->pc_sel.pc_domain != 0)
355			continue;
356
357		/*
358		 * I'm not sure why someone would do this...but...
359		 */
360		if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD)
361			continue;
362
363		/*
364		 * Look at each of the match flags.  If it's set, do the
365		 * comparison.  If the comparison fails, we don't have a
366		 * match, go on to the next item if there is one.
367		 */
368		if (((matches[i].flags & PCI_GETCONF_MATCH_BUS_OLD) != 0) &&
369		    (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
370			continue;
371
372		if (((matches[i].flags & PCI_GETCONF_MATCH_DEV_OLD) != 0) &&
373		    (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
374			continue;
375
376		if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC_OLD) != 0) &&
377		    (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
378			continue;
379
380		if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0) &&
381		    (match_buf->pc_vendor != matches[i].pc_vendor))
382			continue;
383
384		if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0) &&
385		    (match_buf->pc_device != matches[i].pc_device))
386			continue;
387
388		if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0) &&
389		    (match_buf->pc_class != matches[i].pc_class))
390			continue;
391
392		if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0) &&
393		    ((u_int32_t)match_buf->pd_unit != matches[i].pd_unit))
394			continue;
395
396		if (((matches[i].flags & PCI_GETCONF_MATCH_NAME_OLD) != 0) &&
397		    (strncmp(matches[i].pd_name, match_buf->pd_name,
398		    sizeof(match_buf->pd_name)) != 0))
399			continue;
400
401		return (0);
402	}
403
404	return (1);
405}
406#endif	/* COMPAT_FREEBSD32 */
407#endif	/* PRE7_COMPAT */
408
409/*
410 * Like PVE_NEXT but takes an explicit length since 'pve' is a user
411 * pointer that cannot be dereferenced.
412 */
413#define	PVE_NEXT_LEN(pve, datalen)					\
414	((struct pci_vpd_element *)((char *)(pve) +			\
415	    sizeof(struct pci_vpd_element) + (datalen)))
416
417static int
418pci_list_vpd(device_t dev, struct pci_list_vpd_io *lvio)
419{
420	struct pci_vpd_element vpd_element, *vpd_user;
421	struct pcicfg_vpd *vpd;
422	size_t len;
423	int error, i;
424
425	vpd = pci_fetch_vpd_list(dev);
426	if (vpd->vpd_reg == 0 || vpd->vpd_ident == NULL)
427		return (ENXIO);
428
429	/*
430	 * Calculate the amount of space needed in the data buffer.  An
431	 * identifier element is always present followed by the read-only
432	 * and read-write keywords.
433	 */
434	len = sizeof(struct pci_vpd_element) + strlen(vpd->vpd_ident);
435	for (i = 0; i < vpd->vpd_rocnt; i++)
436		len += sizeof(struct pci_vpd_element) + vpd->vpd_ros[i].len;
437	for (i = 0; i < vpd->vpd_wcnt; i++)
438		len += sizeof(struct pci_vpd_element) + vpd->vpd_w[i].len;
439
440	if (lvio->plvi_len == 0) {
441		lvio->plvi_len = len;
442		return (0);
443	}
444	if (lvio->plvi_len < len) {
445		lvio->plvi_len = len;
446		return (ENOMEM);
447	}
448
449	/*
450	 * Copyout the identifier string followed by each keyword and
451	 * value.
452	 */
453	vpd_user = lvio->plvi_data;
454	vpd_element.pve_keyword[0] = '\0';
455	vpd_element.pve_keyword[1] = '\0';
456	vpd_element.pve_flags = PVE_FLAG_IDENT;
457	vpd_element.pve_datalen = strlen(vpd->vpd_ident);
458	error = copyout(&vpd_element, vpd_user, sizeof(vpd_element));
459	if (error)
460		return (error);
461	error = copyout(vpd->vpd_ident, vpd_user->pve_data,
462	    strlen(vpd->vpd_ident));
463	if (error)
464		return (error);
465	vpd_user = PVE_NEXT_LEN(vpd_user, vpd_element.pve_datalen);
466	vpd_element.pve_flags = 0;
467	for (i = 0; i < vpd->vpd_rocnt; i++) {
468		vpd_element.pve_keyword[0] = vpd->vpd_ros[i].keyword[0];
469		vpd_element.pve_keyword[1] = vpd->vpd_ros[i].keyword[1];
470		vpd_element.pve_datalen = vpd->vpd_ros[i].len;
471		error = copyout(&vpd_element, vpd_user, sizeof(vpd_element));
472		if (error)
473			return (error);
474		error = copyout(vpd->vpd_ros[i].value, vpd_user->pve_data,
475		    vpd->vpd_ros[i].len);
476		if (error)
477			return (error);
478		vpd_user = PVE_NEXT_LEN(vpd_user, vpd_element.pve_datalen);
479	}
480	vpd_element.pve_flags = PVE_FLAG_RW;
481	for (i = 0; i < vpd->vpd_wcnt; i++) {
482		vpd_element.pve_keyword[0] = vpd->vpd_w[i].keyword[0];
483		vpd_element.pve_keyword[1] = vpd->vpd_w[i].keyword[1];
484		vpd_element.pve_datalen = vpd->vpd_w[i].len;
485		error = copyout(&vpd_element, vpd_user, sizeof(vpd_element));
486		if (error)
487			return (error);
488		error = copyout(vpd->vpd_w[i].value, vpd_user->pve_data,
489		    vpd->vpd_w[i].len);
490		if (error)
491			return (error);
492		vpd_user = PVE_NEXT_LEN(vpd_user, vpd_element.pve_datalen);
493	}
494	KASSERT((char *)vpd_user - (char *)lvio->plvi_data == len,
495	    ("length mismatch"));
496	lvio->plvi_len = len;
497	return (0);
498}
499
500static int
501pci_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
502{
503	device_t pcidev, brdev;
504	void *confdata;
505	const char *name;
506	struct devlist *devlist_head;
507	struct pci_conf_io *cio = NULL;
508	struct pci_devinfo *dinfo;
509	struct pci_io *io;
510	struct pci_bar_io *bio;
511	struct pci_list_vpd_io *lvio;
512	struct pci_match_conf *pattern_buf;
513	struct pci_map *pm;
514	size_t confsz, iolen, pbufsz;
515	int error, ionum, i, num_patterns;
516#ifdef PRE7_COMPAT
517#ifdef COMPAT_FREEBSD32
518	struct pci_conf_io32 *cio32 = NULL;
519	struct pci_conf_old32 conf_old32;
520	struct pci_match_conf_old32 *pattern_buf_old32 = NULL;
521#endif
522	struct pci_conf_old conf_old;
523	struct pci_io iodata;
524	struct pci_io_old *io_old;
525	struct pci_match_conf_old *pattern_buf_old = NULL;
526
527	io_old = NULL;
528#endif
529
530	if (!(flag & FWRITE)) {
531		switch (cmd) {
532#ifdef PRE7_COMPAT
533#ifdef COMPAT_FREEBSD32
534		case PCIOCGETCONF_OLD32:
535#endif
536		case PCIOCGETCONF_OLD:
537#endif
538		case PCIOCGETCONF:
539		case PCIOCGETBAR:
540		case PCIOCLISTVPD:
541			break;
542		default:
543			return (EPERM);
544		}
545	}
546
547	switch (cmd) {
548#ifdef PRE7_COMPAT
549#ifdef COMPAT_FREEBSD32
550	case PCIOCGETCONF_OLD32:
551               cio32 = (struct pci_conf_io32 *)data;
552               cio = malloc(sizeof(struct pci_conf_io), M_TEMP, M_WAITOK);
553               cio->pat_buf_len = cio32->pat_buf_len;
554               cio->num_patterns = cio32->num_patterns;
555               cio->patterns = (void *)(uintptr_t)cio32->patterns;
556               cio->match_buf_len = cio32->match_buf_len;
557               cio->num_matches = cio32->num_matches;
558               cio->matches = (void *)(uintptr_t)cio32->matches;
559               cio->offset = cio32->offset;
560               cio->generation = cio32->generation;
561               cio->status = cio32->status;
562               cio32->num_matches = 0;
563               break;
564#endif
565	case PCIOCGETCONF_OLD:
566#endif
567	case PCIOCGETCONF:
568		cio = (struct pci_conf_io *)data;
569	}
570
571	switch (cmd) {
572#ifdef PRE7_COMPAT
573#ifdef COMPAT_FREEBSD32
574	case PCIOCGETCONF_OLD32:
575#endif
576	case PCIOCGETCONF_OLD:
577#endif
578	case PCIOCGETCONF:
579
580		pattern_buf = NULL;
581		num_patterns = 0;
582		dinfo = NULL;
583
584		cio->num_matches = 0;
585
586		/*
587		 * If the user specified an offset into the device list,
588		 * but the list has changed since they last called this
589		 * ioctl, tell them that the list has changed.  They will
590		 * have to get the list from the beginning.
591		 */
592		if ((cio->offset != 0)
593		 && (cio->generation != pci_generation)){
594			cio->status = PCI_GETCONF_LIST_CHANGED;
595			error = 0;
596			goto getconfexit;
597		}
598
599		/*
600		 * Check to see whether the user has asked for an offset
601		 * past the end of our list.
602		 */
603		if (cio->offset >= pci_numdevs) {
604			cio->status = PCI_GETCONF_LAST_DEVICE;
605			error = 0;
606			goto getconfexit;
607		}
608
609		/* get the head of the device queue */
610		devlist_head = &pci_devq;
611
612		/*
613		 * Determine how much room we have for pci_conf structures.
614		 * Round the user's buffer size down to the nearest
615		 * multiple of sizeof(struct pci_conf) in case the user
616		 * didn't specify a multiple of that size.
617		 */
618#ifdef PRE7_COMPAT
619#ifdef COMPAT_FREEBSD32
620		if (cmd == PCIOCGETCONF_OLD32)
621			confsz = sizeof(struct pci_conf_old32);
622		else
623#endif
624		if (cmd == PCIOCGETCONF_OLD)
625			confsz = sizeof(struct pci_conf_old);
626		else
627#endif
628			confsz = sizeof(struct pci_conf);
629		iolen = min(cio->match_buf_len - (cio->match_buf_len % confsz),
630		    pci_numdevs * confsz);
631
632		/*
633		 * Since we know that iolen is a multiple of the size of
634		 * the pciconf union, it's okay to do this.
635		 */
636		ionum = iolen / confsz;
637
638		/*
639		 * If this test is true, the user wants the pci_conf
640		 * structures returned to match the supplied entries.
641		 */
642		if ((cio->num_patterns > 0) && (cio->num_patterns < pci_numdevs)
643		 && (cio->pat_buf_len > 0)) {
644			/*
645			 * pat_buf_len needs to be:
646			 * num_patterns * sizeof(struct pci_match_conf)
647			 * While it is certainly possible the user just
648			 * allocated a large buffer, but set the number of
649			 * matches correctly, it is far more likely that
650			 * their kernel doesn't match the userland utility
651			 * they're using.  It's also possible that the user
652			 * forgot to initialize some variables.  Yes, this
653			 * may be overly picky, but I hazard to guess that
654			 * it's far more likely to just catch folks that
655			 * updated their kernel but not their userland.
656			 */
657#ifdef PRE7_COMPAT
658#ifdef COMPAT_FREEBSD32
659			if (cmd == PCIOCGETCONF_OLD32)
660				pbufsz = sizeof(struct pci_match_conf_old32);
661			else
662#endif
663			if (cmd == PCIOCGETCONF_OLD)
664				pbufsz = sizeof(struct pci_match_conf_old);
665			else
666#endif
667				pbufsz = sizeof(struct pci_match_conf);
668			if (cio->num_patterns * pbufsz != cio->pat_buf_len) {
669				/* The user made a mistake, return an error. */
670				cio->status = PCI_GETCONF_ERROR;
671				error = EINVAL;
672				goto getconfexit;
673			}
674
675			/*
676			 * Allocate a buffer to hold the patterns.
677			 */
678#ifdef PRE7_COMPAT
679#ifdef COMPAT_FREEBSD32
680			if (cmd == PCIOCGETCONF_OLD32) {
681				pattern_buf_old32 = malloc(cio->pat_buf_len,
682				    M_TEMP, M_WAITOK);
683				error = copyin(cio->patterns,
684				    pattern_buf_old32, cio->pat_buf_len);
685			} else
686#endif /* COMPAT_FREEBSD32 */
687			if (cmd == PCIOCGETCONF_OLD) {
688				pattern_buf_old = malloc(cio->pat_buf_len,
689				    M_TEMP, M_WAITOK);
690				error = copyin(cio->patterns,
691				    pattern_buf_old, cio->pat_buf_len);
692			} else
693#endif /* PRE7_COMPAT */
694			{
695				pattern_buf = malloc(cio->pat_buf_len, M_TEMP,
696				    M_WAITOK);
697				error = copyin(cio->patterns, pattern_buf,
698				    cio->pat_buf_len);
699			}
700			if (error != 0) {
701				error = EINVAL;
702				goto getconfexit;
703			}
704			num_patterns = cio->num_patterns;
705		} else if ((cio->num_patterns > 0)
706			|| (cio->pat_buf_len > 0)) {
707			/*
708			 * The user made a mistake, spit out an error.
709			 */
710			cio->status = PCI_GETCONF_ERROR;
711			error = EINVAL;
712                       goto getconfexit;
713		}
714
715		/*
716		 * Go through the list of devices and copy out the devices
717		 * that match the user's criteria.
718		 */
719		for (cio->num_matches = 0, i = 0,
720				 dinfo = STAILQ_FIRST(devlist_head);
721		     dinfo != NULL;
722		     dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
723
724			if (i < cio->offset)
725				continue;
726
727			/* Populate pd_name and pd_unit */
728			name = NULL;
729			if (dinfo->cfg.dev)
730				name = device_get_name(dinfo->cfg.dev);
731			if (name) {
732				strncpy(dinfo->conf.pd_name, name,
733					sizeof(dinfo->conf.pd_name));
734				dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0;
735				dinfo->conf.pd_unit =
736					device_get_unit(dinfo->cfg.dev);
737			} else {
738				dinfo->conf.pd_name[0] = '\0';
739				dinfo->conf.pd_unit = 0;
740			}
741
742#ifdef PRE7_COMPAT
743			if (
744#ifdef COMPAT_FREEBSD32
745			    (cmd == PCIOCGETCONF_OLD32 &&
746			    (pattern_buf_old32 == NULL ||
747			    pci_conf_match_old32(pattern_buf_old32,
748			    num_patterns, &dinfo->conf) == 0)) ||
749#endif
750			    (cmd == PCIOCGETCONF_OLD &&
751			    (pattern_buf_old == NULL ||
752			    pci_conf_match_old(pattern_buf_old, num_patterns,
753			    &dinfo->conf) == 0)) ||
754			    (cmd == PCIOCGETCONF &&
755			    (pattern_buf == NULL ||
756			    pci_conf_match(pattern_buf, num_patterns,
757			    &dinfo->conf) == 0))) {
758#else
759			if (pattern_buf == NULL ||
760			    pci_conf_match(pattern_buf, num_patterns,
761			    &dinfo->conf) == 0) {
762#endif
763				/*
764				 * If we've filled up the user's buffer,
765				 * break out at this point.  Since we've
766				 * got a match here, we'll pick right back
767				 * up at the matching entry.  We can also
768				 * tell the user that there are more matches
769				 * left.
770				 */
771				if (cio->num_matches >= ionum)
772					break;
773
774#ifdef PRE7_COMPAT
775#ifdef COMPAT_FREEBSD32
776				if (cmd == PCIOCGETCONF_OLD32) {
777					conf_old32.pc_sel.pc_bus =
778					    dinfo->conf.pc_sel.pc_bus;
779					conf_old32.pc_sel.pc_dev =
780					    dinfo->conf.pc_sel.pc_dev;
781					conf_old32.pc_sel.pc_func =
782					    dinfo->conf.pc_sel.pc_func;
783					conf_old32.pc_hdr = dinfo->conf.pc_hdr;
784					conf_old32.pc_subvendor =
785					    dinfo->conf.pc_subvendor;
786					conf_old32.pc_subdevice =
787					    dinfo->conf.pc_subdevice;
788					conf_old32.pc_vendor =
789					    dinfo->conf.pc_vendor;
790					conf_old32.pc_device =
791					    dinfo->conf.pc_device;
792					conf_old32.pc_class =
793					    dinfo->conf.pc_class;
794					conf_old32.pc_subclass =
795					    dinfo->conf.pc_subclass;
796					conf_old32.pc_progif =
797					    dinfo->conf.pc_progif;
798					conf_old32.pc_revid =
799					    dinfo->conf.pc_revid;
800					strncpy(conf_old32.pd_name,
801					    dinfo->conf.pd_name,
802					    sizeof(conf_old32.pd_name));
803					conf_old32.pd_name[PCI_MAXNAMELEN] = 0;
804					conf_old32.pd_unit =
805					    (uint32_t)dinfo->conf.pd_unit;
806					confdata = &conf_old32;
807				} else
808#endif /* COMPAT_FREEBSD32 */
809				if (cmd == PCIOCGETCONF_OLD) {
810					conf_old.pc_sel.pc_bus =
811					    dinfo->conf.pc_sel.pc_bus;
812					conf_old.pc_sel.pc_dev =
813					    dinfo->conf.pc_sel.pc_dev;
814					conf_old.pc_sel.pc_func =
815					    dinfo->conf.pc_sel.pc_func;
816					conf_old.pc_hdr = dinfo->conf.pc_hdr;
817					conf_old.pc_subvendor =
818					    dinfo->conf.pc_subvendor;
819					conf_old.pc_subdevice =
820					    dinfo->conf.pc_subdevice;
821					conf_old.pc_vendor =
822					    dinfo->conf.pc_vendor;
823					conf_old.pc_device =
824					    dinfo->conf.pc_device;
825					conf_old.pc_class =
826					    dinfo->conf.pc_class;
827					conf_old.pc_subclass =
828					    dinfo->conf.pc_subclass;
829					conf_old.pc_progif =
830					    dinfo->conf.pc_progif;
831					conf_old.pc_revid =
832					    dinfo->conf.pc_revid;
833					strncpy(conf_old.pd_name,
834					    dinfo->conf.pd_name,
835					    sizeof(conf_old.pd_name));
836					conf_old.pd_name[PCI_MAXNAMELEN] = 0;
837					conf_old.pd_unit =
838					    dinfo->conf.pd_unit;
839					confdata = &conf_old;
840				} else
841#endif /* PRE7_COMPAT */
842					confdata = &dinfo->conf;
843				error = copyout(confdata,
844				    (caddr_t)cio->matches +
845				    confsz * cio->num_matches, confsz);
846				if (error)
847					break;
848				cio->num_matches++;
849			}
850		}
851
852		/*
853		 * Set the pointer into the list, so if the user is getting
854		 * n records at a time, where n < pci_numdevs,
855		 */
856		cio->offset = i;
857
858		/*
859		 * Set the generation, the user will need this if they make
860		 * another ioctl call with offset != 0.
861		 */
862		cio->generation = pci_generation;
863
864		/*
865		 * If this is the last device, inform the user so he won't
866		 * bother asking for more devices.  If dinfo isn't NULL, we
867		 * know that there are more matches in the list because of
868		 * the way the traversal is done.
869		 */
870		if (dinfo == NULL)
871			cio->status = PCI_GETCONF_LAST_DEVICE;
872		else
873			cio->status = PCI_GETCONF_MORE_DEVS;
874
875getconfexit:
876#ifdef PRE7_COMPAT
877#ifdef COMPAT_FREEBSD32
878		if (cmd == PCIOCGETCONF_OLD32) {
879			cio32->status = cio->status;
880			cio32->generation = cio->generation;
881			cio32->offset = cio->offset;
882			cio32->num_matches = cio->num_matches;
883			free(cio, M_TEMP);
884		}
885		if (pattern_buf_old32 != NULL)
886			free(pattern_buf_old32, M_TEMP);
887#endif
888		if (pattern_buf_old != NULL)
889			free(pattern_buf_old, M_TEMP);
890#endif
891		if (pattern_buf != NULL)
892			free(pattern_buf, M_TEMP);
893
894		break;
895
896#ifdef PRE7_COMPAT
897	case PCIOCREAD_OLD:
898	case PCIOCWRITE_OLD:
899		io_old = (struct pci_io_old *)data;
900		iodata.pi_sel.pc_domain = 0;
901		iodata.pi_sel.pc_bus = io_old->pi_sel.pc_bus;
902		iodata.pi_sel.pc_dev = io_old->pi_sel.pc_dev;
903		iodata.pi_sel.pc_func = io_old->pi_sel.pc_func;
904		iodata.pi_reg = io_old->pi_reg;
905		iodata.pi_width = io_old->pi_width;
906		iodata.pi_data = io_old->pi_data;
907		data = (caddr_t)&iodata;
908		/* FALLTHROUGH */
909#endif
910	case PCIOCREAD:
911	case PCIOCWRITE:
912		io = (struct pci_io *)data;
913		switch(io->pi_width) {
914		case 4:
915		case 2:
916		case 1:
917			/* Make sure register is not negative and aligned. */
918			if (io->pi_reg < 0 ||
919			    io->pi_reg & (io->pi_width - 1)) {
920				error = EINVAL;
921				break;
922			}
923			/*
924			 * Assume that the user-level bus number is
925			 * in fact the physical PCI bus number.
926			 * Look up the grandparent, i.e. the bridge device,
927			 * so that we can issue configuration space cycles.
928			 */
929			pcidev = pci_find_dbsf(io->pi_sel.pc_domain,
930			    io->pi_sel.pc_bus, io->pi_sel.pc_dev,
931			    io->pi_sel.pc_func);
932			if (pcidev) {
933				brdev = device_get_parent(
934				    device_get_parent(pcidev));
935
936#ifdef PRE7_COMPAT
937				if (cmd == PCIOCWRITE || cmd == PCIOCWRITE_OLD)
938#else
939				if (cmd == PCIOCWRITE)
940#endif
941					PCIB_WRITE_CONFIG(brdev,
942							  io->pi_sel.pc_bus,
943							  io->pi_sel.pc_dev,
944							  io->pi_sel.pc_func,
945							  io->pi_reg,
946							  io->pi_data,
947							  io->pi_width);
948#ifdef PRE7_COMPAT
949				else if (cmd == PCIOCREAD_OLD)
950					io_old->pi_data =
951						PCIB_READ_CONFIG(brdev,
952							  io->pi_sel.pc_bus,
953							  io->pi_sel.pc_dev,
954							  io->pi_sel.pc_func,
955							  io->pi_reg,
956							  io->pi_width);
957#endif
958				else
959					io->pi_data =
960						PCIB_READ_CONFIG(brdev,
961							  io->pi_sel.pc_bus,
962							  io->pi_sel.pc_dev,
963							  io->pi_sel.pc_func,
964							  io->pi_reg,
965							  io->pi_width);
966				error = 0;
967			} else {
968#ifdef COMPAT_FREEBSD4
969				if (cmd == PCIOCREAD_OLD) {
970					io_old->pi_data = -1;
971					error = 0;
972				} else
973#endif
974					error = ENODEV;
975			}
976			break;
977		default:
978			error = EINVAL;
979			break;
980		}
981		break;
982
983	case PCIOCGETBAR:
984		bio = (struct pci_bar_io *)data;
985
986		/*
987		 * Assume that the user-level bus number is
988		 * in fact the physical PCI bus number.
989		 */
990		pcidev = pci_find_dbsf(bio->pbi_sel.pc_domain,
991		    bio->pbi_sel.pc_bus, bio->pbi_sel.pc_dev,
992		    bio->pbi_sel.pc_func);
993		if (pcidev == NULL) {
994			error = ENODEV;
995			break;
996		}
997		pm = pci_find_bar(pcidev, bio->pbi_reg);
998		if (pm == NULL) {
999			error = EINVAL;
1000			break;
1001		}
1002		bio->pbi_base = pm->pm_value;
1003		bio->pbi_length = (pci_addr_t)1 << pm->pm_size;
1004		bio->pbi_enabled = pci_bar_enabled(pcidev, pm);
1005		error = 0;
1006		break;
1007	case PCIOCATTACHED:
1008		error = 0;
1009		io = (struct pci_io *)data;
1010		pcidev = pci_find_dbsf(io->pi_sel.pc_domain, io->pi_sel.pc_bus,
1011				       io->pi_sel.pc_dev, io->pi_sel.pc_func);
1012		if (pcidev != NULL)
1013			io->pi_data = device_is_attached(pcidev);
1014		else
1015			error = ENODEV;
1016		break;
1017	case PCIOCLISTVPD:
1018		lvio = (struct pci_list_vpd_io *)data;
1019
1020		/*
1021		 * Assume that the user-level bus number is
1022		 * in fact the physical PCI bus number.
1023		 */
1024		pcidev = pci_find_dbsf(lvio->plvi_sel.pc_domain,
1025		    lvio->plvi_sel.pc_bus, lvio->plvi_sel.pc_dev,
1026		    lvio->plvi_sel.pc_func);
1027		if (pcidev == NULL) {
1028			error = ENODEV;
1029			break;
1030		}
1031		error = pci_list_vpd(pcidev, lvio);
1032		break;
1033	default:
1034		error = ENOTTY;
1035		break;
1036	}
1037
1038	return (error);
1039}
1040