pci.c revision 65347
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 * $FreeBSD: head/sys/dev/pci/pci.c 65347 2000-09-01 23:09:02Z msmith $
27 *
28 */
29
30#include "opt_bus.h"
31
32#include "opt_simos.h"
33#include "opt_compat_oldpci.h"
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/malloc.h>
38#include <sys/module.h>
39#include <sys/fcntl.h>
40#include <sys/conf.h>
41#include <sys/kernel.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#include <machine/md_var.h>		/* For the Alpha */
54
55#include <sys/pciio.h>
56#include <pci/pcireg.h>
57#include <pci/pcivar.h>
58
59#include "pcib_if.h"
60
61#ifdef __alpha__
62#include <machine/rpb.h>
63#endif
64
65#ifdef APIC_IO
66#include <machine/smp.h>
67#endif /* APIC_IO */
68
69static devclass_t	pci_devclass;
70
71struct pci_quirk {
72	u_int32_t devid;	/* Vendor/device of the card */
73	int	type;
74#define PCI_QUIRK_MAP_REG	1 /* PCI map register in wierd place */
75	int	arg1;
76	int	arg2;
77};
78
79struct pci_quirk pci_quirks[] = {
80	/*
81	 * The Intel 82371AB has a map register at offset 0x90.
82	 */
83	{ 0x71138086, PCI_QUIRK_MAP_REG,	0x90,	 0 },
84
85	{ 0 }
86};
87
88/* map register information */
89#define PCI_MAPMEM	0x01	/* memory map */
90#define PCI_MAPMEMP	0x02	/* prefetchable memory map */
91#define PCI_MAPPORT	0x04	/* port map */
92
93static STAILQ_HEAD(devlist, pci_devinfo) pci_devq;
94u_int32_t pci_numdevs = 0;
95static u_int32_t pci_generation = 0;
96
97/* return base address of memory or port map */
98
99static u_int32_t
100pci_mapbase(unsigned mapreg)
101{
102	int mask = 0x03;
103	if ((mapreg & 0x01) == 0)
104		mask = 0x0f;
105	return (mapreg & ~mask);
106}
107
108/* return map type of memory or port map */
109
110static int
111pci_maptype(unsigned mapreg)
112{
113	static u_int8_t maptype[0x10] = {
114		PCI_MAPMEM,		PCI_MAPPORT,
115		PCI_MAPMEM,		0,
116		PCI_MAPMEM,		PCI_MAPPORT,
117		0,			0,
118		PCI_MAPMEM|PCI_MAPMEMP,	PCI_MAPPORT,
119		PCI_MAPMEM|PCI_MAPMEMP, 0,
120		PCI_MAPMEM|PCI_MAPMEMP,	PCI_MAPPORT,
121		0,			0,
122	};
123
124	return maptype[mapreg & 0x0f];
125}
126
127/* return log2 of map size decoded for memory or port map */
128
129static int
130pci_mapsize(unsigned testval)
131{
132	int ln2size;
133
134	testval = pci_mapbase(testval);
135	ln2size = 0;
136	if (testval != 0) {
137		while ((testval & 1) == 0)
138		{
139			ln2size++;
140			testval >>= 1;
141		}
142	}
143	return (ln2size);
144}
145
146/* return log2 of address range supported by map register */
147
148static int
149pci_maprange(unsigned mapreg)
150{
151	int ln2range = 0;
152	switch (mapreg & 0x07) {
153	case 0x00:
154	case 0x01:
155	case 0x05:
156		ln2range = 32;
157		break;
158	case 0x02:
159		ln2range = 20;
160		break;
161	case 0x04:
162		ln2range = 64;
163		break;
164	}
165	return (ln2range);
166}
167
168/* adjust some values from PCI 1.0 devices to match 2.0 standards ... */
169
170static void
171pci_fixancient(pcicfgregs *cfg)
172{
173	if (cfg->hdrtype != 0)
174		return;
175
176	/* PCI to PCI bridges use header type 1 */
177	if (cfg->baseclass == PCIC_BRIDGE && cfg->subclass == PCIS_BRIDGE_PCI)
178		cfg->hdrtype = 1;
179}
180
181/* read config data specific to header type 1 device (PCI to PCI bridge) */
182
183static void *
184pci_readppb(device_t pcib, int b, int s, int f)
185{
186	pcih1cfgregs *p;
187
188	p = malloc(sizeof (pcih1cfgregs), M_DEVBUF, M_WAITOK);
189	if (p == NULL)
190		return (NULL);
191
192	bzero(p, sizeof *p);
193
194	p->secstat = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_SECSTAT_1, 2);
195	p->bridgectl = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_BRIDGECTL_1, 2);
196
197	p->seclat = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_SECLAT_1, 1);
198
199	p->iobase = PCI_PPBIOBASE (PCIB_READ_CONFIG(pcib, b, s, f,
200						    PCIR_IOBASEH_1, 2),
201				   PCIB_READ_CONFIG(pcib, b, s, f,
202						    PCIR_IOBASEL_1, 1));
203	p->iolimit = PCI_PPBIOLIMIT (PCIB_READ_CONFIG(pcib, b, s, f,
204						      PCIR_IOLIMITH_1, 2),
205				     PCIB_READ_CONFIG(pcib, b, s, f,
206						      PCIR_IOLIMITL_1, 1));
207
208	p->membase = PCI_PPBMEMBASE (0,
209				     PCIB_READ_CONFIG(pcib, b, s, f,
210						      PCIR_MEMBASE_1, 2));
211	p->memlimit = PCI_PPBMEMLIMIT (0,
212				       PCIB_READ_CONFIG(pcib, b, s, f,
213							PCIR_MEMLIMIT_1, 2));
214
215	p->pmembase = PCI_PPBMEMBASE (
216		(pci_addr_t)PCIB_READ_CONFIG(pcib, b, s, f, PCIR_PMBASEH_1, 4),
217		PCIB_READ_CONFIG(pcib, b, s, f, PCIR_PMBASEL_1, 2));
218
219	p->pmemlimit = PCI_PPBMEMLIMIT (
220		(pci_addr_t)PCIB_READ_CONFIG(pcib, b, s, f,
221					     PCIR_PMLIMITH_1, 4),
222		PCIB_READ_CONFIG(pcib, b, s, f, PCIR_PMLIMITL_1, 2));
223
224	return (p);
225}
226
227/* read config data specific to header type 2 device (PCI to CardBus bridge) */
228
229static void *
230pci_readpcb(device_t pcib, int b, int s, int f)
231{
232	pcih2cfgregs *p;
233
234	p = malloc(sizeof (pcih2cfgregs), M_DEVBUF, M_WAITOK);
235	if (p == NULL)
236		return (NULL);
237
238	bzero(p, sizeof *p);
239
240	p->secstat = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_SECSTAT_2, 2);
241	p->bridgectl = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_BRIDGECTL_2, 2);
242
243	p->seclat = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_SECLAT_2, 1);
244
245	p->membase0 = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_MEMBASE0_2, 4);
246	p->memlimit0 = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_MEMLIMIT0_2, 4);
247	p->membase1 = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_MEMBASE1_2, 4);
248	p->memlimit1 = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_MEMLIMIT1_2, 4);
249
250	p->iobase0 = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_IOBASE0_2, 4);
251	p->iolimit0 = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_IOLIMIT0_2, 4);
252	p->iobase1 = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_IOBASE1_2, 4);
253	p->iolimit1 = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_IOLIMIT1_2, 4);
254
255	p->pccardif = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_PCCARDIF_2, 4);
256	return p;
257}
258
259/* extract header type specific config data */
260
261static void
262pci_hdrtypedata(device_t pcib, int b, int s, int f, pcicfgregs *cfg)
263{
264#define REG(n, w)	PCIB_READ_CONFIG(pcib, b, s, f, n, w)
265	switch (cfg->hdrtype) {
266	case 0:
267		cfg->subvendor      = REG(PCIR_SUBVEND_0, 2);
268		cfg->subdevice      = REG(PCIR_SUBDEV_0, 2);
269		cfg->nummaps	    = PCI_MAXMAPS_0;
270		break;
271	case 1:
272		cfg->subvendor      = REG(PCIR_SUBVEND_1, 2);
273		cfg->subdevice      = REG(PCIR_SUBDEV_1, 2);
274		cfg->secondarybus   = REG(PCIR_SECBUS_1, 1);
275		cfg->subordinatebus = REG(PCIR_SUBBUS_1, 1);
276		cfg->nummaps	    = PCI_MAXMAPS_1;
277		cfg->hdrspec        = pci_readppb(pcib, b, s, f);
278		break;
279	case 2:
280		cfg->subvendor      = REG(PCIR_SUBVEND_2, 2);
281		cfg->subdevice      = REG(PCIR_SUBDEV_2, 2);
282		cfg->secondarybus   = REG(PCIR_SECBUS_2, 1);
283		cfg->subordinatebus = REG(PCIR_SUBBUS_2, 1);
284		cfg->nummaps	    = PCI_MAXMAPS_2;
285		cfg->hdrspec        = pci_readpcb(pcib, b, s, f);
286		break;
287	}
288#undef REG
289}
290
291/* read configuration header into pcicfgrect structure */
292
293static struct pci_devinfo *
294pci_read_device(device_t pcib, int b, int s, int f)
295{
296#define REG(n, w)	PCIB_READ_CONFIG(pcib, b, s, f, n, w)
297	pcicfgregs *cfg = NULL;
298	struct pci_devinfo *devlist_entry;
299	struct devlist *devlist_head;
300
301	devlist_head = &pci_devq;
302
303	devlist_entry = NULL;
304
305	if (PCIB_READ_CONFIG(pcib, b, s, f, PCIR_DEVVENDOR, 4) != -1) {
306		devlist_entry = malloc(sizeof(struct pci_devinfo),
307				       M_DEVBUF, M_WAITOK);
308		if (devlist_entry == NULL)
309			return (NULL);
310		bzero(devlist_entry, sizeof *devlist_entry);
311
312		cfg = &devlist_entry->cfg;
313
314		cfg->bus		= b;
315		cfg->slot		= s;
316		cfg->func		= f;
317		cfg->vendor		= REG(PCIR_VENDOR, 2);
318		cfg->device		= REG(PCIR_DEVICE, 2);
319		cfg->cmdreg		= REG(PCIR_COMMAND, 2);
320		cfg->statreg		= REG(PCIR_STATUS, 2);
321		cfg->baseclass		= REG(PCIR_CLASS, 1);
322		cfg->subclass		= REG(PCIR_SUBCLASS, 1);
323		cfg->progif		= REG(PCIR_PROGIF, 1);
324		cfg->revid		= REG(PCIR_REVID, 1);
325		cfg->hdrtype		= REG(PCIR_HEADERTYPE, 1);
326		cfg->cachelnsz		= REG(PCIR_CACHELNSZ, 1);
327		cfg->lattimer		= REG(PCIR_LATTIMER, 1);
328		cfg->intpin		= REG(PCIR_INTPIN, 1);
329		cfg->intline		= REG(PCIR_INTLINE, 1);
330#ifdef __alpha__
331		alpha_platform_assign_pciintr(cfg);
332#endif
333
334#ifdef APIC_IO
335		if (cfg->intpin != 0) {
336			int airq;
337
338			airq = pci_apic_irq(cfg->bus, cfg->slot, cfg->intpin);
339			if (airq >= 0) {
340				/* PCI specific entry found in MP table */
341				if (airq != cfg->intline) {
342					undirect_pci_irq(cfg->intline);
343					cfg->intline = airq;
344				}
345			} else {
346				/*
347				 * PCI interrupts might be redirected to the
348				 * ISA bus according to some MP tables. Use the
349				 * same methods as used by the ISA devices
350				 * devices to find the proper IOAPIC int pin.
351				 */
352				airq = isa_apic_irq(cfg->intline);
353				if ((airq >= 0) && (airq != cfg->intline)) {
354					/* XXX: undirect_pci_irq() ? */
355					undirect_isa_irq(cfg->intline);
356					cfg->intline = airq;
357				}
358			}
359		}
360#endif /* APIC_IO */
361
362		cfg->mingnt		= REG(PCIR_MINGNT, 1);
363		cfg->maxlat		= REG(PCIR_MAXLAT, 1);
364
365		cfg->mfdev		= (cfg->hdrtype & PCIM_MFDEV) != 0;
366		cfg->hdrtype		&= ~PCIM_MFDEV;
367
368		pci_fixancient(cfg);
369		pci_hdrtypedata(pcib, b, s, f, cfg);
370
371		STAILQ_INSERT_TAIL(devlist_head, devlist_entry, pci_links);
372
373		devlist_entry->conf.pc_sel.pc_bus = cfg->bus;
374		devlist_entry->conf.pc_sel.pc_dev = cfg->slot;
375		devlist_entry->conf.pc_sel.pc_func = cfg->func;
376		devlist_entry->conf.pc_hdr = cfg->hdrtype;
377
378		devlist_entry->conf.pc_subvendor = cfg->subvendor;
379		devlist_entry->conf.pc_subdevice = cfg->subdevice;
380		devlist_entry->conf.pc_vendor = cfg->vendor;
381		devlist_entry->conf.pc_device = cfg->device;
382
383		devlist_entry->conf.pc_class = cfg->baseclass;
384		devlist_entry->conf.pc_subclass = cfg->subclass;
385		devlist_entry->conf.pc_progif = cfg->progif;
386		devlist_entry->conf.pc_revid = cfg->revid;
387
388		pci_numdevs++;
389		pci_generation++;
390	}
391	return (devlist_entry);
392#undef REG
393}
394
395#if 0
396/* free pcicfgregs structure and all depending data structures */
397
398static int
399pci_freecfg(struct pci_devinfo *dinfo)
400{
401	struct devlist *devlist_head;
402
403	devlist_head = &pci_devq;
404
405	if (dinfo->cfg.hdrspec != NULL)
406		free(dinfo->cfg.hdrspec, M_DEVBUF);
407	if (dinfo->cfg.map != NULL)
408		free(dinfo->cfg.map, M_DEVBUF);
409	/* XXX this hasn't been tested */
410	STAILQ_REMOVE(devlist_head, dinfo, pci_devinfo, pci_links);
411	free(dinfo, M_DEVBUF);
412
413	/* increment the generation count */
414	pci_generation++;
415
416	/* we're losing one device */
417	pci_numdevs--;
418	return (0);
419}
420#endif
421
422
423/*
424 * This is the user interface to PCI configuration space.
425 */
426
427static int
428pci_open(dev_t dev, int oflags, int devtype, struct proc *p)
429{
430	if ((oflags & FWRITE) && securelevel > 0) {
431		return EPERM;
432	}
433	return 0;
434}
435
436static int
437pci_close(dev_t dev, int flag, int devtype, struct proc *p)
438{
439	return 0;
440}
441
442/*
443 * Match a single pci_conf structure against an array of pci_match_conf
444 * structures.  The first argument, 'matches', is an array of num_matches
445 * pci_match_conf structures.  match_buf is a pointer to the pci_conf
446 * structure that will be compared to every entry in the matches array.
447 * This function returns 1 on failure, 0 on success.
448 */
449static int
450pci_conf_match(struct pci_match_conf *matches, int num_matches,
451	       struct pci_conf *match_buf)
452{
453	int i;
454
455	if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
456		return(1);
457
458	for (i = 0; i < num_matches; i++) {
459		/*
460		 * I'm not sure why someone would do this...but...
461		 */
462		if (matches[i].flags == PCI_GETCONF_NO_MATCH)
463			continue;
464
465		/*
466		 * Look at each of the match flags.  If it's set, do the
467		 * comparison.  If the comparison fails, we don't have a
468		 * match, go on to the next item if there is one.
469		 */
470		if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0)
471		 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
472			continue;
473
474		if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0)
475		 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
476			continue;
477
478		if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0)
479		 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
480			continue;
481
482		if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0)
483		 && (match_buf->pc_vendor != matches[i].pc_vendor))
484			continue;
485
486		if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0)
487		 && (match_buf->pc_device != matches[i].pc_device))
488			continue;
489
490		if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0)
491		 && (match_buf->pc_class != matches[i].pc_class))
492			continue;
493
494		if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0)
495		 && (match_buf->pd_unit != matches[i].pd_unit))
496			continue;
497
498		if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0)
499		 && (strncmp(matches[i].pd_name, match_buf->pd_name,
500			     sizeof(match_buf->pd_name)) != 0))
501			continue;
502
503		return(0);
504	}
505
506	return(1);
507}
508
509/*
510 * Locate the parent of a PCI device by scanning the PCI devlist
511 * and return the entry for the parent.
512 * For devices on PCI Bus 0 (the host bus), this is the PCI Host.
513 * For devices on secondary PCI busses, this is that bus' PCI-PCI Bridge.
514 */
515
516pcicfgregs *
517pci_devlist_get_parent(pcicfgregs *cfg)
518{
519	struct devlist *devlist_head;
520	struct pci_devinfo *dinfo;
521	pcicfgregs *bridge_cfg;
522	int i;
523
524	dinfo = STAILQ_FIRST(devlist_head = &pci_devq);
525
526	/* If the device is on PCI bus 0, look for the host */
527	if (cfg->bus == 0) {
528		for (i = 0; (dinfo != NULL) && (i < pci_numdevs);
529		dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
530			bridge_cfg = &dinfo->cfg;
531			if (bridge_cfg->baseclass == PCIC_BRIDGE
532				&& bridge_cfg->subclass == PCIS_BRIDGE_HOST
533		    		&& bridge_cfg->bus == cfg->bus) {
534				return bridge_cfg;
535			}
536		}
537	}
538
539	/* If the device is not on PCI bus 0, look for the PCI-PCI bridge */
540	if (cfg->bus > 0) {
541		for (i = 0; (dinfo != NULL) && (i < pci_numdevs);
542		dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
543			bridge_cfg = &dinfo->cfg;
544			if (bridge_cfg->baseclass == PCIC_BRIDGE
545				&& bridge_cfg->subclass == PCIS_BRIDGE_PCI
546				&& bridge_cfg->secondarybus == cfg->bus) {
547				return bridge_cfg;
548			}
549		}
550	}
551
552	return NULL;
553}
554
555static int
556pci_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
557{
558	device_t pci, pcib;
559	struct pci_io *io;
560	const char *name;
561	int error;
562
563	if (!(flag & FWRITE))
564		return EPERM;
565
566
567	switch(cmd) {
568	case PCIOCGETCONF:
569		{
570		struct pci_devinfo *dinfo;
571		struct pci_conf_io *cio;
572		struct devlist *devlist_head;
573		struct pci_match_conf *pattern_buf;
574		int num_patterns;
575		size_t iolen;
576		int ionum, i;
577
578		cio = (struct pci_conf_io *)data;
579
580		num_patterns = 0;
581		dinfo = NULL;
582
583		/*
584		 * Hopefully the user won't pass in a null pointer, but it
585		 * can't hurt to check.
586		 */
587		if (cio == NULL) {
588			error = EINVAL;
589			break;
590		}
591
592		/*
593		 * If the user specified an offset into the device list,
594		 * but the list has changed since they last called this
595		 * ioctl, tell them that the list has changed.  They will
596		 * have to get the list from the beginning.
597		 */
598		if ((cio->offset != 0)
599		 && (cio->generation != pci_generation)){
600			cio->num_matches = 0;
601			cio->status = PCI_GETCONF_LIST_CHANGED;
602			error = 0;
603			break;
604		}
605
606		/*
607		 * Check to see whether the user has asked for an offset
608		 * past the end of our list.
609		 */
610		if (cio->offset >= pci_numdevs) {
611			cio->num_matches = 0;
612			cio->status = PCI_GETCONF_LAST_DEVICE;
613			error = 0;
614			break;
615		}
616
617		/* get the head of the device queue */
618		devlist_head = &pci_devq;
619
620		/*
621		 * Determine how much room we have for pci_conf structures.
622		 * Round the user's buffer size down to the nearest
623		 * multiple of sizeof(struct pci_conf) in case the user
624		 * didn't specify a multiple of that size.
625		 */
626		iolen = min(cio->match_buf_len -
627			    (cio->match_buf_len % sizeof(struct pci_conf)),
628			    pci_numdevs * sizeof(struct pci_conf));
629
630		/*
631		 * Since we know that iolen is a multiple of the size of
632		 * the pciconf union, it's okay to do this.
633		 */
634		ionum = iolen / sizeof(struct pci_conf);
635
636		/*
637		 * If this test is true, the user wants the pci_conf
638		 * structures returned to match the supplied entries.
639		 */
640		if ((cio->num_patterns > 0)
641		 && (cio->pat_buf_len > 0)) {
642			/*
643			 * pat_buf_len needs to be:
644			 * num_patterns * sizeof(struct pci_match_conf)
645			 * While it is certainly possible the user just
646			 * allocated a large buffer, but set the number of
647			 * matches correctly, it is far more likely that
648			 * their kernel doesn't match the userland utility
649			 * they're using.  It's also possible that the user
650			 * forgot to initialize some variables.  Yes, this
651			 * may be overly picky, but I hazard to guess that
652			 * it's far more likely to just catch folks that
653			 * updated their kernel but not their userland.
654			 */
655			if ((cio->num_patterns *
656			    sizeof(struct pci_match_conf)) != cio->pat_buf_len){
657				/* The user made a mistake, return an error*/
658				cio->status = PCI_GETCONF_ERROR;
659				printf("pci_ioctl: pat_buf_len %d != "
660				       "num_patterns (%d) * sizeof(struct "
661				       "pci_match_conf) (%d)\npci_ioctl: "
662				       "pat_buf_len should be = %d\n",
663				       cio->pat_buf_len, cio->num_patterns,
664				       (int)sizeof(struct pci_match_conf),
665				       (int)sizeof(struct pci_match_conf) *
666				       cio->num_patterns);
667				printf("pci_ioctl: do your headers match your "
668				       "kernel?\n");
669				cio->num_matches = 0;
670				error = EINVAL;
671				break;
672			}
673
674			/*
675			 * Check the user's buffer to make sure it's readable.
676			 */
677			if (!useracc((caddr_t)cio->patterns,
678				    cio->pat_buf_len, VM_PROT_READ)) {
679				printf("pci_ioctl: pattern buffer %p, "
680				       "length %u isn't user accessible for"
681				       " READ\n", cio->patterns,
682				       cio->pat_buf_len);
683				error = EACCES;
684				break;
685			}
686			/*
687			 * Allocate a buffer to hold the patterns.
688			 */
689			pattern_buf = malloc(cio->pat_buf_len, M_TEMP,
690					     M_WAITOK);
691			error = copyin(cio->patterns, pattern_buf,
692				       cio->pat_buf_len);
693			if (error != 0)
694				break;
695			num_patterns = cio->num_patterns;
696
697		} else if ((cio->num_patterns > 0)
698			|| (cio->pat_buf_len > 0)) {
699			/*
700			 * The user made a mistake, spit out an error.
701			 */
702			cio->status = PCI_GETCONF_ERROR;
703			cio->num_matches = 0;
704			printf("pci_ioctl: invalid GETCONF arguments\n");
705			error = EINVAL;
706			break;
707		} else
708			pattern_buf = NULL;
709
710		/*
711		 * Make sure we can write to the match buffer.
712		 */
713		if (!useracc((caddr_t)cio->matches,
714			     cio->match_buf_len, VM_PROT_WRITE)) {
715			printf("pci_ioctl: match buffer %p, length %u "
716			       "isn't user accessible for WRITE\n",
717			       cio->matches, cio->match_buf_len);
718			error = EACCES;
719			break;
720		}
721
722		/*
723		 * Go through the list of devices and copy out the devices
724		 * that match the user's criteria.
725		 */
726		for (cio->num_matches = 0, error = 0, i = 0,
727		     dinfo = STAILQ_FIRST(devlist_head);
728		     (dinfo != NULL) && (cio->num_matches < ionum)
729		     && (error == 0) && (i < pci_numdevs);
730		     dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
731
732			if (i < cio->offset)
733				continue;
734
735			/* Populate pd_name and pd_unit */
736			name = NULL;
737			if (dinfo->cfg.dev && dinfo->conf.pd_name[0] == '\0')
738				name = device_get_name(dinfo->cfg.dev);
739			if (name) {
740				strncpy(dinfo->conf.pd_name, name,
741					sizeof(dinfo->conf.pd_name));
742				dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0;
743				dinfo->conf.pd_unit =
744					device_get_unit(dinfo->cfg.dev);
745			}
746
747			if ((pattern_buf == NULL) ||
748			    (pci_conf_match(pattern_buf, num_patterns,
749					    &dinfo->conf) == 0)) {
750
751				/*
752				 * If we've filled up the user's buffer,
753				 * break out at this point.  Since we've
754				 * got a match here, we'll pick right back
755				 * up at the matching entry.  We can also
756				 * tell the user that there are more matches
757				 * left.
758				 */
759				if (cio->num_matches >= ionum)
760					break;
761
762				error = copyout(&dinfo->conf,
763					        &cio->matches[cio->num_matches],
764						sizeof(struct pci_conf));
765				cio->num_matches++;
766			}
767		}
768
769		/*
770		 * Set the pointer into the list, so if the user is getting
771		 * n records at a time, where n < pci_numdevs,
772		 */
773		cio->offset = i;
774
775		/*
776		 * Set the generation, the user will need this if they make
777		 * another ioctl call with offset != 0.
778		 */
779		cio->generation = pci_generation;
780
781		/*
782		 * If this is the last device, inform the user so he won't
783		 * bother asking for more devices.  If dinfo isn't NULL, we
784		 * know that there are more matches in the list because of
785		 * the way the traversal is done.
786		 */
787		if (dinfo == NULL)
788			cio->status = PCI_GETCONF_LAST_DEVICE;
789		else
790			cio->status = PCI_GETCONF_MORE_DEVS;
791
792		if (pattern_buf != NULL)
793			free(pattern_buf, M_TEMP);
794
795		break;
796		}
797	case PCIOCREAD:
798		io = (struct pci_io *)data;
799		switch(io->pi_width) {
800		case 4:
801		case 2:
802		case 1:
803			/*
804			 * Assume that the user-level bus number is
805			 * actually the pciN instance number. We map
806			 * from that to the real pcib+bus combination.
807			 */
808			pci = devclass_get_device(pci_devclass,
809						  io->pi_sel.pc_bus);
810			if (pci) {
811				int b = pcib_get_bus(pci);
812				pcib = device_get_parent(pci);
813				io->pi_data =
814					PCIB_READ_CONFIG(pcib,
815							 b,
816							 io->pi_sel.pc_dev,
817							 io->pi_sel.pc_func,
818							 io->pi_reg,
819							 io->pi_width);
820				error = 0;
821			} else {
822				error = ENODEV;
823			}
824			break;
825		default:
826			error = ENODEV;
827			break;
828		}
829		break;
830
831	case PCIOCWRITE:
832		io = (struct pci_io *)data;
833		switch(io->pi_width) {
834		case 4:
835		case 2:
836		case 1:
837			/*
838			 * Assume that the user-level bus number is
839			 * actually the pciN instance number. We map
840			 * from that to the real pcib+bus combination.
841			 */
842			pci = devclass_get_device(pci_devclass,
843						  io->pi_sel.pc_bus);
844			if (pci) {
845				int b = pcib_get_bus(pci);
846				pcib = device_get_parent(pci);
847				PCIB_WRITE_CONFIG(pcib,
848						  b,
849						  io->pi_sel.pc_dev,
850						  io->pi_sel.pc_func,
851						  io->pi_reg,
852						  io->pi_data,
853						  io->pi_width);
854				error = 0;
855			} else {
856				error = ENODEV;
857			}
858			break;
859		default:
860			error = ENODEV;
861			break;
862		}
863		break;
864
865	default:
866		error = ENOTTY;
867		break;
868	}
869
870	return (error);
871}
872
873#define	PCI_CDEV	78
874
875static struct cdevsw pcicdev = {
876	/* open */	pci_open,
877	/* close */	pci_close,
878	/* read */	noread,
879	/* write */	nowrite,
880	/* ioctl */	pci_ioctl,
881	/* poll */	nopoll,
882	/* mmap */	nommap,
883	/* strategy */	nostrategy,
884	/* name */	"pci",
885	/* maj */	PCI_CDEV,
886	/* dump */	nodump,
887	/* psize */	nopsize,
888	/* flags */	0,
889	/* bmaj */	-1
890};
891
892#include "pci_if.h"
893
894/*
895 * New style pci driver.  Parent device is either a pci-host-bridge or a
896 * pci-pci-bridge.  Both kinds are represented by instances of pcib.
897 */
898
899static void
900pci_print_verbose(struct pci_devinfo *dinfo)
901{
902	if (bootverbose) {
903		pcicfgregs *cfg = &dinfo->cfg;
904
905		printf("found->\tvendor=0x%04x, dev=0x%04x, revid=0x%02x\n",
906		       cfg->vendor, cfg->device, cfg->revid);
907		printf("\tclass=%02x-%02x-%02x, hdrtype=0x%02x, mfdev=%d\n",
908		       cfg->baseclass, cfg->subclass, cfg->progif,
909		       cfg->hdrtype, cfg->mfdev);
910		printf("\tsubordinatebus=%x \tsecondarybus=%x\n",
911		       cfg->subordinatebus, cfg->secondarybus);
912#ifdef PCI_DEBUG
913		printf("\tcmdreg=0x%04x, statreg=0x%04x, cachelnsz=%d (dwords)\n",
914		       cfg->cmdreg, cfg->statreg, cfg->cachelnsz);
915		printf("\tlattimer=0x%02x (%d ns), mingnt=0x%02x (%d ns), maxlat=0x%02x (%d ns)\n",
916		       cfg->lattimer, cfg->lattimer * 30,
917		       cfg->mingnt, cfg->mingnt * 250, cfg->maxlat, cfg->maxlat * 250);
918#endif /* PCI_DEBUG */
919		if (cfg->intpin > 0)
920			printf("\tintpin=%c, irq=%d\n", cfg->intpin +'a' -1, cfg->intline);
921	}
922}
923
924static int
925pci_porten(device_t pcib, int b, int s, int f)
926{
927	return (PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2)
928		& PCIM_CMD_PORTEN) != 0;
929}
930
931static int
932pci_memen(device_t pcib, int b, int s, int f)
933{
934	return (PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2)
935		& PCIM_CMD_MEMEN) != 0;
936}
937
938/*
939 * Add a resource based on a pci map register. Return 1 if the map
940 * register is a 32bit map register or 2 if it is a 64bit register.
941 */
942static int
943pci_add_map(device_t pcib, int b, int s, int f, int reg,
944	    struct resource_list *rl)
945{
946	u_int32_t map;
947	u_int64_t base;
948	u_int8_t ln2size;
949	u_int8_t ln2range;
950	u_int32_t testval;
951	u_int16_t cmd;
952	int type;
953
954	map = PCIB_READ_CONFIG(pcib, b, s, f, reg, 4);
955
956	if (map == 0 || map == 0xffffffff)
957		return 1; /* skip invalid entry */
958
959	PCIB_WRITE_CONFIG(pcib, b, s, f, reg, 0xffffffff, 4);
960	testval = PCIB_READ_CONFIG(pcib, b, s, f, reg, 4);
961	PCIB_WRITE_CONFIG(pcib, b, s, f, reg, map, 4);
962
963	base = pci_mapbase(map);
964	if (pci_maptype(map) & PCI_MAPMEM)
965		type = SYS_RES_MEMORY;
966	else
967		type = SYS_RES_IOPORT;
968	ln2size = pci_mapsize(testval);
969	ln2range = pci_maprange(testval);
970	if (ln2range == 64) {
971		/* Read the other half of a 64bit map register */
972		base |= (u_int64_t) PCIB_READ_CONFIG(pcib, b, s, f, reg + 4, 4) << 32;
973	}
974
975	if (bootverbose) {
976		printf("\tmap[%02x]: type %x, range %2d, base %08x, size %2d",
977		       reg, pci_maptype(map), ln2range,
978		       (unsigned int) base, ln2size);
979		if (type == SYS_RES_IOPORT && !pci_porten(pcib, b, s, f))
980			printf(", port disabled\n");
981		else if (type == SYS_RES_MEMORY && !pci_memen(pcib, b, s, f))
982			printf(", memory disabled\n");
983		else
984			printf(", enabled\n");
985	}
986
987	/* Turn on resources that have been left off by a lazy BIOS */
988	if (type == SYS_RES_IOPORT && !pci_porten(pcib, b, s, f)) {
989		cmd = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2);
990		cmd |= PCIM_CMD_PORTEN;
991		PCIB_WRITE_CONFIG(pcib, b, s, f, PCIR_COMMAND, cmd, 2);
992	}
993	if (type == SYS_RES_MEMORY && !pci_memen(pcib, b, s, f)) {
994		cmd = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2);
995		cmd |= PCIM_CMD_MEMEN;
996		PCIB_WRITE_CONFIG(pcib, b, s, f, PCIR_COMMAND, cmd, 2);
997	}
998
999	resource_list_add(rl, type, reg,
1000			  base, base + (1 << ln2size) - 1,
1001			  (1 << ln2size));
1002
1003	return (ln2range == 64) ? 2 : 1;
1004}
1005
1006static void
1007pci_add_resources(device_t pcib, int b, int s, int f, device_t dev)
1008{
1009	struct pci_devinfo *dinfo = device_get_ivars(dev);
1010	pcicfgregs *cfg = &dinfo->cfg;
1011	struct resource_list *rl = &dinfo->resources;
1012	struct pci_quirk *q;
1013	int i;
1014
1015	for (i = 0; i < cfg->nummaps;) {
1016		i += pci_add_map(pcib, b, s, f, PCIR_MAPS + i*4, rl);
1017	}
1018
1019	for (q = &pci_quirks[0]; q->devid; q++) {
1020		if (q->devid == ((cfg->device << 16) | cfg->vendor)
1021		    && q->type == PCI_QUIRK_MAP_REG)
1022			pci_add_map(pcib, b, s, f, q->arg1, rl);
1023	}
1024
1025	if (cfg->intpin > 0 && cfg->intline != 255)
1026		resource_list_add(rl, SYS_RES_IRQ, 0,
1027				  cfg->intline, cfg->intline, 1);
1028}
1029
1030static void
1031pci_add_children(device_t dev, int busno)
1032{
1033	device_t pcib = device_get_parent(dev);
1034	int maxslots;
1035	int s, f;
1036
1037	maxslots = PCIB_MAXSLOTS(pcib);
1038
1039	for (s = 0; s <= maxslots; s++) {
1040		int pcifunchigh = 0;
1041		for (f = 0; f <= pcifunchigh; f++) {
1042			struct pci_devinfo *dinfo =
1043				pci_read_device(pcib, busno, s, f);
1044			if (dinfo != NULL) {
1045				if (dinfo->cfg.mfdev)
1046					pcifunchigh = 7;
1047
1048				pci_print_verbose(dinfo);
1049				dinfo->cfg.dev = device_add_child(dev, NULL, -1);
1050				device_set_ivars(dinfo->cfg.dev, dinfo);
1051				pci_add_resources(pcib, busno, s, f,
1052						  dinfo->cfg.dev);
1053			}
1054		}
1055	}
1056}
1057
1058static int
1059pci_probe(device_t dev)
1060{
1061	static int once;
1062
1063	device_set_desc(dev, "PCI bus");
1064
1065	if (bootverbose)
1066		device_printf(dev, "physical bus=%d\n", pcib_get_bus(dev));
1067
1068	/*
1069	 * Since there can be multiple independantly numbered PCI
1070	 * busses on some large alpha systems, we can't use the unit
1071	 * number to decide what bus we are probing. We ask the parent
1072	 * pcib what our bus number is.
1073	 */
1074	pci_add_children(dev, pcib_get_bus(dev));
1075
1076	if (!once) {
1077		make_dev(&pcicdev, 0, UID_ROOT, GID_WHEEL, 0644, "pci");
1078		once++;
1079	}
1080
1081	return 0;
1082}
1083
1084static int
1085pci_print_resources(struct resource_list *rl, const char *name, int type,
1086		    const char *format)
1087{
1088	struct resource_list_entry *rle;
1089	int printed, retval;
1090
1091	printed = 0;
1092	retval = 0;
1093	/* Yes, this is kinda cheating */
1094	SLIST_FOREACH(rle, rl, link) {
1095		if (rle->type == type) {
1096			if (printed == 0)
1097				retval += printf(" %s ", name);
1098			else if (printed > 0)
1099				retval += printf(",");
1100			printed++;
1101			retval += printf(format, rle->start);
1102			if (rle->count > 1) {
1103				retval += printf("-");
1104				retval += printf(format, rle->start +
1105						 rle->count - 1);
1106			}
1107		}
1108	}
1109	return retval;
1110}
1111
1112static int
1113pci_print_child(device_t dev, device_t child)
1114{
1115	struct pci_devinfo *dinfo;
1116	struct resource_list *rl;
1117	pcicfgregs *cfg;
1118	int retval = 0;
1119
1120	dinfo = device_get_ivars(child);
1121	cfg = &dinfo->cfg;
1122	rl = &dinfo->resources;
1123
1124	retval += bus_print_child_header(dev, child);
1125
1126	retval += pci_print_resources(rl, "port", SYS_RES_IOPORT, "%#lx");
1127	retval += pci_print_resources(rl, "mem", SYS_RES_MEMORY, "%#lx");
1128	retval += pci_print_resources(rl, "irq", SYS_RES_IRQ, "%ld");
1129	if (device_get_flags(dev))
1130		retval += printf(" flags %#x", device_get_flags(dev));
1131
1132	retval += printf(" at device %d.%d", pci_get_slot(child),
1133			 pci_get_function(child));
1134
1135	retval += bus_print_child_footer(dev, child);
1136
1137	return (retval);
1138}
1139
1140static void
1141pci_probe_nomatch(device_t dev, device_t child)
1142{
1143	struct pci_devinfo *dinfo;
1144	pcicfgregs *cfg;
1145	const char *desc;
1146	int unknown;
1147
1148	unknown = 0;
1149	dinfo = device_get_ivars(child);
1150	cfg = &dinfo->cfg;
1151	desc = pci_ata_match(child);
1152	if (!desc) desc = pci_usb_match(child);
1153	if (!desc) desc = pci_vga_match(child);
1154	if (!desc) desc = pci_chip_match(child);
1155	if (!desc) {
1156		desc = "unknown card";
1157		unknown++;
1158	}
1159	device_printf(dev, "<%s>", desc);
1160	if (bootverbose || unknown) {
1161		printf(" (vendor=0x%04x, dev=0x%04x)",
1162			cfg->vendor,
1163			cfg->device);
1164	}
1165	printf(" at %d.%d",
1166		pci_get_slot(child),
1167		pci_get_function(child));
1168	if (cfg->intpin > 0 && cfg->intline != 255) {
1169		printf(" irq %d", cfg->intline);
1170	}
1171	printf("\n");
1172
1173	return;
1174}
1175
1176static int
1177pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1178{
1179	struct pci_devinfo *dinfo;
1180	pcicfgregs *cfg;
1181
1182	dinfo = device_get_ivars(child);
1183	cfg = &dinfo->cfg;
1184
1185	switch (which) {
1186	case PCI_IVAR_SUBVENDOR:
1187		*result = cfg->subvendor;
1188		break;
1189	case PCI_IVAR_SUBDEVICE:
1190		*result = cfg->subdevice;
1191		break;
1192	case PCI_IVAR_VENDOR:
1193		*result = cfg->vendor;
1194		break;
1195	case PCI_IVAR_DEVICE:
1196		*result = cfg->device;
1197		break;
1198	case PCI_IVAR_DEVID:
1199		*result = (cfg->device << 16) | cfg->vendor;
1200		break;
1201	case PCI_IVAR_CLASS:
1202		*result = cfg->baseclass;
1203		break;
1204	case PCI_IVAR_SUBCLASS:
1205		*result = cfg->subclass;
1206		break;
1207	case PCI_IVAR_PROGIF:
1208		*result = cfg->progif;
1209		break;
1210	case PCI_IVAR_REVID:
1211		*result = cfg->revid;
1212		break;
1213	case PCI_IVAR_INTPIN:
1214		*result = cfg->intpin;
1215		break;
1216	case PCI_IVAR_IRQ:
1217		*result = cfg->intline;
1218		break;
1219	case PCI_IVAR_BUS:
1220		*result = cfg->bus;
1221		break;
1222	case PCI_IVAR_SLOT:
1223		*result = cfg->slot;
1224		break;
1225	case PCI_IVAR_FUNCTION:
1226		*result = cfg->func;
1227		break;
1228	case PCI_IVAR_SECONDARYBUS:
1229		*result = cfg->secondarybus;
1230		break;
1231	case PCI_IVAR_SUBORDINATEBUS:
1232		*result = cfg->subordinatebus;
1233		break;
1234	default:
1235		return ENOENT;
1236	}
1237	return 0;
1238}
1239
1240static int
1241pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1242{
1243	struct pci_devinfo *dinfo;
1244	pcicfgregs *cfg;
1245
1246	dinfo = device_get_ivars(child);
1247	cfg = &dinfo->cfg;
1248
1249	switch (which) {
1250	case PCI_IVAR_SUBVENDOR:
1251	case PCI_IVAR_SUBDEVICE:
1252	case PCI_IVAR_VENDOR:
1253	case PCI_IVAR_DEVICE:
1254	case PCI_IVAR_DEVID:
1255	case PCI_IVAR_CLASS:
1256	case PCI_IVAR_SUBCLASS:
1257	case PCI_IVAR_PROGIF:
1258	case PCI_IVAR_REVID:
1259	case PCI_IVAR_INTPIN:
1260	case PCI_IVAR_IRQ:
1261	case PCI_IVAR_BUS:
1262	case PCI_IVAR_SLOT:
1263	case PCI_IVAR_FUNCTION:
1264		return EINVAL;	/* disallow for now */
1265
1266	case PCI_IVAR_SECONDARYBUS:
1267		cfg->secondarybus = value;
1268		break;
1269	case PCI_IVAR_SUBORDINATEBUS:
1270		cfg->subordinatebus = value;
1271		break;
1272	default:
1273		return ENOENT;
1274	}
1275	return 0;
1276}
1277
1278static struct resource *
1279pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
1280		   u_long start, u_long end, u_long count, u_int flags)
1281{
1282	struct pci_devinfo *dinfo = device_get_ivars(child);
1283	struct resource_list *rl = &dinfo->resources;
1284
1285	return resource_list_alloc(rl, dev, child, type, rid,
1286				   start, end, count, flags);
1287}
1288
1289static int
1290pci_release_resource(device_t dev, device_t child, int type, int rid,
1291		     struct resource *r)
1292{
1293	struct pci_devinfo *dinfo = device_get_ivars(child);
1294	struct resource_list *rl = &dinfo->resources;
1295
1296	return resource_list_release(rl, dev, child, type, rid, r);
1297}
1298
1299static int
1300pci_set_resource(device_t dev, device_t child, int type, int rid,
1301		 u_long start, u_long count)
1302{
1303	struct pci_devinfo *dinfo = device_get_ivars(child);
1304	struct resource_list *rl = &dinfo->resources;
1305
1306	resource_list_add(rl, type, rid, start, start + count - 1, count);
1307	return 0;
1308}
1309
1310static int
1311pci_get_resource(device_t dev, device_t child, int type, int rid,
1312		 u_long *startp, u_long *countp)
1313{
1314	struct pci_devinfo *dinfo = device_get_ivars(child);
1315	struct resource_list *rl = &dinfo->resources;
1316	struct resource_list_entry *rle;
1317
1318	rle = resource_list_find(rl, type, rid);
1319	if (!rle)
1320		return ENOENT;
1321
1322	if (startp)
1323		*startp = rle->start;
1324	if (countp)
1325		*countp = rle->count;
1326
1327	return 0;
1328}
1329
1330static void
1331pci_delete_resource(device_t dev, device_t child, int type, int rid)
1332{
1333	printf("pci_delete_resource: PCI resources can not be deleted\n");
1334}
1335
1336static u_int32_t
1337pci_read_config_method(device_t dev, device_t child, int reg, int width)
1338{
1339	struct pci_devinfo *dinfo = device_get_ivars(child);
1340	pcicfgregs *cfg = &dinfo->cfg;
1341
1342	return PCIB_READ_CONFIG(device_get_parent(dev),
1343				cfg->bus, cfg->slot, cfg->func,
1344				reg, width);
1345}
1346
1347static void
1348pci_write_config_method(device_t dev, device_t child, int reg,
1349			u_int32_t val, int width)
1350{
1351	struct pci_devinfo *dinfo = device_get_ivars(child);
1352	pcicfgregs *cfg = &dinfo->cfg;
1353
1354	PCIB_WRITE_CONFIG(device_get_parent(dev),
1355			  cfg->bus, cfg->slot, cfg->func,
1356			  reg, val, width);
1357}
1358
1359static int
1360pci_modevent(module_t mod, int what, void *arg)
1361{
1362	switch (what) {
1363	case MOD_LOAD:
1364		STAILQ_INIT(&pci_devq);
1365		break;
1366
1367	case MOD_UNLOAD:
1368		break;
1369	}
1370
1371	return 0;
1372}
1373
1374static device_method_t pci_methods[] = {
1375	/* Device interface */
1376	DEVMETHOD(device_probe,		pci_probe),
1377	DEVMETHOD(device_attach,	bus_generic_attach),
1378	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
1379	DEVMETHOD(device_suspend,	bus_generic_suspend),
1380	DEVMETHOD(device_resume,	bus_generic_resume),
1381
1382	/* Bus interface */
1383	DEVMETHOD(bus_print_child,	pci_print_child),
1384	DEVMETHOD(bus_probe_nomatch,	pci_probe_nomatch),
1385	DEVMETHOD(bus_read_ivar,	pci_read_ivar),
1386	DEVMETHOD(bus_write_ivar,	pci_write_ivar),
1387	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
1388	DEVMETHOD(bus_alloc_resource,	pci_alloc_resource),
1389	DEVMETHOD(bus_release_resource,	pci_release_resource),
1390	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
1391	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
1392	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
1393	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
1394	DEVMETHOD(bus_set_resource,	pci_set_resource),
1395	DEVMETHOD(bus_get_resource,	pci_get_resource),
1396	DEVMETHOD(bus_delete_resource,	pci_delete_resource),
1397
1398	/* PCI interface */
1399	DEVMETHOD(pci_read_config,	pci_read_config_method),
1400	DEVMETHOD(pci_write_config,	pci_write_config_method),
1401
1402	{ 0, 0 }
1403};
1404
1405static driver_t pci_driver = {
1406	"pci",
1407	pci_methods,
1408	1,			/* no softc */
1409};
1410DRIVER_MODULE(pci, pcib, pci_driver, pci_devclass, pci_modevent, 0);
1411