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