]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/pci/pci.c
MFC r270212,r270332:
[FreeBSD/stable/10.git] / sys / dev / pci / pci.c
1 /*-
2  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3  * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
4  * Copyright (c) 2000, BSDi
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_bus.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/linker.h>
39 #include <sys/fcntl.h>
40 #include <sys/conf.h>
41 #include <sys/kernel.h>
42 #include <sys/queue.h>
43 #include <sys/sysctl.h>
44 #include <sys/endian.h>
45
46 #include <vm/vm.h>
47 #include <vm/pmap.h>
48 #include <vm/vm_extern.h>
49
50 #include <sys/bus.h>
51 #include <machine/bus.h>
52 #include <sys/rman.h>
53 #include <machine/resource.h>
54 #include <machine/stdarg.h>
55
56 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__)
57 #include <machine/intr_machdep.h>
58 #endif
59
60 #include <sys/pciio.h>
61 #include <dev/pci/pcireg.h>
62 #include <dev/pci/pcivar.h>
63 #include <dev/pci/pci_private.h>
64
65 #include <dev/usb/controller/xhcireg.h>
66 #include <dev/usb/controller/ehcireg.h>
67 #include <dev/usb/controller/ohcireg.h>
68 #include <dev/usb/controller/uhcireg.h>
69
70 #include "pcib_if.h"
71 #include "pci_if.h"
72
73 #define PCIR_IS_BIOS(cfg, reg)                                          \
74         (((cfg)->hdrtype == PCIM_HDRTYPE_NORMAL && reg == PCIR_BIOS) || \
75          ((cfg)->hdrtype == PCIM_HDRTYPE_BRIDGE && reg == PCIR_BIOS_1))
76
77 static int              pci_has_quirk(uint32_t devid, int quirk);
78 static pci_addr_t       pci_mapbase(uint64_t mapreg);
79 static const char       *pci_maptype(uint64_t mapreg);
80 static int              pci_mapsize(uint64_t testval);
81 static int              pci_maprange(uint64_t mapreg);
82 static pci_addr_t       pci_rombase(uint64_t mapreg);
83 static int              pci_romsize(uint64_t testval);
84 static void             pci_fixancient(pcicfgregs *cfg);
85 static int              pci_printf(pcicfgregs *cfg, const char *fmt, ...);
86
87 static int              pci_porten(device_t dev);
88 static int              pci_memen(device_t dev);
89 static void             pci_assign_interrupt(device_t bus, device_t dev,
90                             int force_route);
91 static int              pci_add_map(device_t bus, device_t dev, int reg,
92                             struct resource_list *rl, int force, int prefetch);
93 static int              pci_probe(device_t dev);
94 static int              pci_attach(device_t dev);
95 #ifdef PCI_RES_BUS
96 static int              pci_detach(device_t dev);
97 #endif
98 static void             pci_load_vendor_data(void);
99 static int              pci_describe_parse_line(char **ptr, int *vendor,
100                             int *device, char **desc);
101 static char             *pci_describe_device(device_t dev);
102 static int              pci_modevent(module_t mod, int what, void *arg);
103 static void             pci_hdrtypedata(device_t pcib, int b, int s, int f,
104                             pcicfgregs *cfg);
105 static void             pci_read_cap(device_t pcib, pcicfgregs *cfg);
106 static int              pci_read_vpd_reg(device_t pcib, pcicfgregs *cfg,
107                             int reg, uint32_t *data);
108 #if 0
109 static int              pci_write_vpd_reg(device_t pcib, pcicfgregs *cfg,
110                             int reg, uint32_t data);
111 #endif
112 static void             pci_read_vpd(device_t pcib, pcicfgregs *cfg);
113 static void             pci_mask_msix(device_t dev, u_int index);
114 static void             pci_unmask_msix(device_t dev, u_int index);
115 static int              pci_msi_blacklisted(void);
116 static int              pci_msix_blacklisted(void);
117 static void             pci_resume_msi(device_t dev);
118 static void             pci_resume_msix(device_t dev);
119 static int              pci_remap_intr_method(device_t bus, device_t dev,
120                             u_int irq);
121
122 static uint16_t         pci_get_rid_method(device_t dev, device_t child);
123
124 static device_method_t pci_methods[] = {
125         /* Device interface */
126         DEVMETHOD(device_probe,         pci_probe),
127         DEVMETHOD(device_attach,        pci_attach),
128 #ifdef PCI_RES_BUS
129         DEVMETHOD(device_detach,        pci_detach),
130 #else
131         DEVMETHOD(device_detach,        bus_generic_detach),
132 #endif
133         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
134         DEVMETHOD(device_suspend,       pci_suspend),
135         DEVMETHOD(device_resume,        pci_resume),
136
137         /* Bus interface */
138         DEVMETHOD(bus_print_child,      pci_print_child),
139         DEVMETHOD(bus_probe_nomatch,    pci_probe_nomatch),
140         DEVMETHOD(bus_read_ivar,        pci_read_ivar),
141         DEVMETHOD(bus_write_ivar,       pci_write_ivar),
142         DEVMETHOD(bus_driver_added,     pci_driver_added),
143         DEVMETHOD(bus_setup_intr,       pci_setup_intr),
144         DEVMETHOD(bus_teardown_intr,    pci_teardown_intr),
145
146         DEVMETHOD(bus_get_dma_tag,      pci_get_dma_tag),
147         DEVMETHOD(bus_get_resource_list,pci_get_resource_list),
148         DEVMETHOD(bus_set_resource,     bus_generic_rl_set_resource),
149         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
150         DEVMETHOD(bus_delete_resource,  pci_delete_resource),
151         DEVMETHOD(bus_alloc_resource,   pci_alloc_resource),
152         DEVMETHOD(bus_adjust_resource,  bus_generic_adjust_resource),
153         DEVMETHOD(bus_release_resource, pci_release_resource),
154         DEVMETHOD(bus_activate_resource, pci_activate_resource),
155         DEVMETHOD(bus_deactivate_resource, pci_deactivate_resource),
156         DEVMETHOD(bus_child_detached,   pci_child_detached),
157         DEVMETHOD(bus_child_pnpinfo_str, pci_child_pnpinfo_str_method),
158         DEVMETHOD(bus_child_location_str, pci_child_location_str_method),
159         DEVMETHOD(bus_remap_intr,       pci_remap_intr_method),
160
161         /* PCI interface */
162         DEVMETHOD(pci_read_config,      pci_read_config_method),
163         DEVMETHOD(pci_write_config,     pci_write_config_method),
164         DEVMETHOD(pci_enable_busmaster, pci_enable_busmaster_method),
165         DEVMETHOD(pci_disable_busmaster, pci_disable_busmaster_method),
166         DEVMETHOD(pci_enable_io,        pci_enable_io_method),
167         DEVMETHOD(pci_disable_io,       pci_disable_io_method),
168         DEVMETHOD(pci_get_vpd_ident,    pci_get_vpd_ident_method),
169         DEVMETHOD(pci_get_vpd_readonly, pci_get_vpd_readonly_method),
170         DEVMETHOD(pci_get_powerstate,   pci_get_powerstate_method),
171         DEVMETHOD(pci_set_powerstate,   pci_set_powerstate_method),
172         DEVMETHOD(pci_assign_interrupt, pci_assign_interrupt_method),
173         DEVMETHOD(pci_find_cap,         pci_find_cap_method),
174         DEVMETHOD(pci_find_extcap,      pci_find_extcap_method),
175         DEVMETHOD(pci_find_htcap,       pci_find_htcap_method),
176         DEVMETHOD(pci_alloc_msi,        pci_alloc_msi_method),
177         DEVMETHOD(pci_alloc_msix,       pci_alloc_msix_method),
178         DEVMETHOD(pci_enable_msi,       pci_enable_msi_method),
179         DEVMETHOD(pci_enable_msix,      pci_enable_msix_method),
180         DEVMETHOD(pci_disable_msi,      pci_disable_msi_method),
181         DEVMETHOD(pci_remap_msix,       pci_remap_msix_method),
182         DEVMETHOD(pci_release_msi,      pci_release_msi_method),
183         DEVMETHOD(pci_msi_count,        pci_msi_count_method),
184         DEVMETHOD(pci_msix_count,       pci_msix_count_method),
185         DEVMETHOD(pci_get_rid,          pci_get_rid_method),
186         DEVMETHOD(pci_child_added,      pci_child_added_method),
187
188         DEVMETHOD_END
189 };
190
191 DEFINE_CLASS_0(pci, pci_driver, pci_methods, sizeof(struct pci_softc));
192
193 static devclass_t pci_devclass;
194 DRIVER_MODULE(pci, pcib, pci_driver, pci_devclass, pci_modevent, NULL);
195 MODULE_VERSION(pci, 1);
196
197 static char     *pci_vendordata;
198 static size_t   pci_vendordata_size;
199
200 struct pci_quirk {
201         uint32_t devid; /* Vendor/device of the card */
202         int     type;
203 #define PCI_QUIRK_MAP_REG       1 /* PCI map register in weird place */
204 #define PCI_QUIRK_DISABLE_MSI   2 /* Neither MSI nor MSI-X work */
205 #define PCI_QUIRK_ENABLE_MSI_VM 3 /* Older chipset in VM where MSI works */
206 #define PCI_QUIRK_UNMAP_REG     4 /* Ignore PCI map register */
207 #define PCI_QUIRK_DISABLE_MSIX  5 /* MSI-X doesn't work */
208 #define PCI_QUIRK_MSI_INTX_BUG  6 /* PCIM_CMD_INTxDIS disables MSI */
209         int     arg1;
210         int     arg2;
211 };
212
213 static const struct pci_quirk pci_quirks[] = {
214         /* The Intel 82371AB and 82443MX have a map register at offset 0x90. */
215         { 0x71138086, PCI_QUIRK_MAP_REG,        0x90,    0 },
216         { 0x719b8086, PCI_QUIRK_MAP_REG,        0x90,    0 },
217         /* As does the Serverworks OSB4 (the SMBus mapping register) */
218         { 0x02001166, PCI_QUIRK_MAP_REG,        0x90,    0 },
219
220         /*
221          * MSI doesn't work with the ServerWorks CNB20-HE Host Bridge
222          * or the CMIC-SL (AKA ServerWorks GC_LE).
223          */
224         { 0x00141166, PCI_QUIRK_DISABLE_MSI,    0,      0 },
225         { 0x00171166, PCI_QUIRK_DISABLE_MSI,    0,      0 },
226
227         /*
228          * MSI doesn't work on earlier Intel chipsets including
229          * E7500, E7501, E7505, 845, 865, 875/E7210, and 855.
230          */
231         { 0x25408086, PCI_QUIRK_DISABLE_MSI,    0,      0 },
232         { 0x254c8086, PCI_QUIRK_DISABLE_MSI,    0,      0 },
233         { 0x25508086, PCI_QUIRK_DISABLE_MSI,    0,      0 },
234         { 0x25608086, PCI_QUIRK_DISABLE_MSI,    0,      0 },
235         { 0x25708086, PCI_QUIRK_DISABLE_MSI,    0,      0 },
236         { 0x25788086, PCI_QUIRK_DISABLE_MSI,    0,      0 },
237         { 0x35808086, PCI_QUIRK_DISABLE_MSI,    0,      0 },
238
239         /*
240          * MSI doesn't work with devices behind the AMD 8131 HT-PCIX
241          * bridge.
242          */
243         { 0x74501022, PCI_QUIRK_DISABLE_MSI,    0,      0 },
244
245         /*
246          * MSI-X allocation doesn't work properly for devices passed through
247          * by VMware up to at least ESXi 5.1.
248          */
249         { 0x079015ad, PCI_QUIRK_DISABLE_MSIX,   0,      0 }, /* PCI/PCI-X */
250         { 0x07a015ad, PCI_QUIRK_DISABLE_MSIX,   0,      0 }, /* PCIe */
251
252         /*
253          * Some virtualization environments emulate an older chipset
254          * but support MSI just fine.  QEMU uses the Intel 82440.
255          */
256         { 0x12378086, PCI_QUIRK_ENABLE_MSI_VM,  0,      0 },
257
258         /*
259          * HPET MMIO base address may appear in Bar1 for AMD SB600 SMBus
260          * controller depending on SoftPciRst register (PM_IO 0x55 [7]).
261          * It prevents us from attaching hpet(4) when the bit is unset.
262          * Note this quirk only affects SB600 revision A13 and earlier.
263          * For SB600 A21 and later, firmware must set the bit to hide it.
264          * For SB700 and later, it is unused and hardcoded to zero.
265          */
266         { 0x43851002, PCI_QUIRK_UNMAP_REG,      0x14,   0 },
267
268         /*
269          * Atheros AR8161/AR8162/E2200 Ethernet controllers have a bug that
270          * MSI interrupt does not assert if PCIM_CMD_INTxDIS bit of the
271          * command register is set.
272          */
273         { 0x10911969, PCI_QUIRK_MSI_INTX_BUG,   0,      0 },
274         { 0xE0911969, PCI_QUIRK_MSI_INTX_BUG,   0,      0 },
275         { 0x10901969, PCI_QUIRK_MSI_INTX_BUG,   0,      0 },
276
277         /*
278          * Broadcom BCM5714(S)/BCM5715(S)/BCM5780(S) Ethernet MACs don't
279          * issue MSI interrupts with PCIM_CMD_INTxDIS set either.
280          */
281         { 0x166814e4, PCI_QUIRK_MSI_INTX_BUG,   0,      0 }, /* BCM5714 */
282         { 0x166914e4, PCI_QUIRK_MSI_INTX_BUG,   0,      0 }, /* BCM5714S */
283         { 0x166a14e4, PCI_QUIRK_MSI_INTX_BUG,   0,      0 }, /* BCM5780 */
284         { 0x166b14e4, PCI_QUIRK_MSI_INTX_BUG,   0,      0 }, /* BCM5780S */
285         { 0x167814e4, PCI_QUIRK_MSI_INTX_BUG,   0,      0 }, /* BCM5715 */
286         { 0x167914e4, PCI_QUIRK_MSI_INTX_BUG,   0,      0 }, /* BCM5715S */
287
288         { 0 }
289 };
290
291 /* map register information */
292 #define PCI_MAPMEM      0x01    /* memory map */
293 #define PCI_MAPMEMP     0x02    /* prefetchable memory map */
294 #define PCI_MAPPORT     0x04    /* port map */
295
296 struct devlist pci_devq;
297 uint32_t pci_generation;
298 uint32_t pci_numdevs = 0;
299 static int pcie_chipset, pcix_chipset;
300
301 /* sysctl vars */
302 SYSCTL_NODE(_hw, OID_AUTO, pci, CTLFLAG_RD, 0, "PCI bus tuning parameters");
303
304 static int pci_enable_io_modes = 1;
305 TUNABLE_INT("hw.pci.enable_io_modes", &pci_enable_io_modes);
306 SYSCTL_INT(_hw_pci, OID_AUTO, enable_io_modes, CTLFLAG_RW,
307     &pci_enable_io_modes, 1,
308     "Enable I/O and memory bits in the config register.  Some BIOSes do not\n\
309 enable these bits correctly.  We'd like to do this all the time, but there\n\
310 are some peripherals that this causes problems with.");
311
312 static int pci_do_realloc_bars = 0;
313 TUNABLE_INT("hw.pci.realloc_bars", &pci_do_realloc_bars);
314 SYSCTL_INT(_hw_pci, OID_AUTO, realloc_bars, CTLFLAG_RW,
315     &pci_do_realloc_bars, 0,
316     "Attempt to allocate a new range for any BARs whose original firmware-assigned ranges fail to allocate during the initial device scan.");
317
318 static int pci_do_power_nodriver = 0;
319 TUNABLE_INT("hw.pci.do_power_nodriver", &pci_do_power_nodriver);
320 SYSCTL_INT(_hw_pci, OID_AUTO, do_power_nodriver, CTLFLAG_RW,
321     &pci_do_power_nodriver, 0,
322   "Place a function into D3 state when no driver attaches to it.  0 means\n\
323 disable.  1 means conservatively place devices into D3 state.  2 means\n\
324 agressively place devices into D3 state.  3 means put absolutely everything\n\
325 in D3 state.");
326
327 int pci_do_power_resume = 1;
328 TUNABLE_INT("hw.pci.do_power_resume", &pci_do_power_resume);
329 SYSCTL_INT(_hw_pci, OID_AUTO, do_power_resume, CTLFLAG_RW,
330     &pci_do_power_resume, 1,
331   "Transition from D3 -> D0 on resume.");
332
333 int pci_do_power_suspend = 1;
334 TUNABLE_INT("hw.pci.do_power_suspend", &pci_do_power_suspend);
335 SYSCTL_INT(_hw_pci, OID_AUTO, do_power_suspend, CTLFLAG_RW,
336     &pci_do_power_suspend, 1,
337   "Transition from D0 -> D3 on suspend.");
338
339 static int pci_do_msi = 1;
340 TUNABLE_INT("hw.pci.enable_msi", &pci_do_msi);
341 SYSCTL_INT(_hw_pci, OID_AUTO, enable_msi, CTLFLAG_RW, &pci_do_msi, 1,
342     "Enable support for MSI interrupts");
343
344 static int pci_do_msix = 1;
345 TUNABLE_INT("hw.pci.enable_msix", &pci_do_msix);
346 SYSCTL_INT(_hw_pci, OID_AUTO, enable_msix, CTLFLAG_RW, &pci_do_msix, 1,
347     "Enable support for MSI-X interrupts");
348
349 static int pci_honor_msi_blacklist = 1;
350 TUNABLE_INT("hw.pci.honor_msi_blacklist", &pci_honor_msi_blacklist);
351 SYSCTL_INT(_hw_pci, OID_AUTO, honor_msi_blacklist, CTLFLAG_RD,
352     &pci_honor_msi_blacklist, 1, "Honor chipset blacklist for MSI/MSI-X");
353
354 #if defined(__i386__) || defined(__amd64__)
355 static int pci_usb_takeover = 1;
356 #else
357 static int pci_usb_takeover = 0;
358 #endif
359 TUNABLE_INT("hw.pci.usb_early_takeover", &pci_usb_takeover);
360 SYSCTL_INT(_hw_pci, OID_AUTO, usb_early_takeover, CTLFLAG_RDTUN,
361     &pci_usb_takeover, 1, "Enable early takeover of USB controllers.\n\
362 Disable this if you depend on BIOS emulation of USB devices, that is\n\
363 you use USB devices (like keyboard or mouse) but do not load USB drivers");
364
365 static int pci_clear_bars;
366 TUNABLE_INT("hw.pci.clear_bars", &pci_clear_bars);
367 SYSCTL_INT(_hw_pci, OID_AUTO, clear_bars, CTLFLAG_RDTUN, &pci_clear_bars, 0,
368     "Ignore firmware-assigned resources for BARs.");
369
370 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
371 static int pci_clear_buses;
372 TUNABLE_INT("hw.pci.clear_buses", &pci_clear_buses);
373 SYSCTL_INT(_hw_pci, OID_AUTO, clear_buses, CTLFLAG_RDTUN, &pci_clear_buses, 0,
374     "Ignore firmware-assigned bus numbers.");
375 #endif
376
377 static int pci_enable_ari = 1;
378 TUNABLE_INT("hw.pci.enable_ari", &pci_enable_ari);
379 SYSCTL_INT(_hw_pci, OID_AUTO, enable_ari, CTLFLAG_RDTUN, &pci_enable_ari,
380     0, "Enable support for PCIe Alternative RID Interpretation");
381
382 static int
383 pci_has_quirk(uint32_t devid, int quirk)
384 {
385         const struct pci_quirk *q;
386
387         for (q = &pci_quirks[0]; q->devid; q++) {
388                 if (q->devid == devid && q->type == quirk)
389                         return (1);
390         }
391         return (0);
392 }
393
394 /* Find a device_t by bus/slot/function in domain 0 */
395
396 device_t
397 pci_find_bsf(uint8_t bus, uint8_t slot, uint8_t func)
398 {
399
400         return (pci_find_dbsf(0, bus, slot, func));
401 }
402
403 /* Find a device_t by domain/bus/slot/function */
404
405 device_t
406 pci_find_dbsf(uint32_t domain, uint8_t bus, uint8_t slot, uint8_t func)
407 {
408         struct pci_devinfo *dinfo;
409
410         STAILQ_FOREACH(dinfo, &pci_devq, pci_links) {
411                 if ((dinfo->cfg.domain == domain) &&
412                     (dinfo->cfg.bus == bus) &&
413                     (dinfo->cfg.slot == slot) &&
414                     (dinfo->cfg.func == func)) {
415                         return (dinfo->cfg.dev);
416                 }
417         }
418
419         return (NULL);
420 }
421
422 /* Find a device_t by vendor/device ID */
423
424 device_t
425 pci_find_device(uint16_t vendor, uint16_t device)
426 {
427         struct pci_devinfo *dinfo;
428
429         STAILQ_FOREACH(dinfo, &pci_devq, pci_links) {
430                 if ((dinfo->cfg.vendor == vendor) &&
431                     (dinfo->cfg.device == device)) {
432                         return (dinfo->cfg.dev);
433                 }
434         }
435
436         return (NULL);
437 }
438
439 device_t
440 pci_find_class(uint8_t class, uint8_t subclass)
441 {
442         struct pci_devinfo *dinfo;
443
444         STAILQ_FOREACH(dinfo, &pci_devq, pci_links) {
445                 if (dinfo->cfg.baseclass == class &&
446                     dinfo->cfg.subclass == subclass) {
447                         return (dinfo->cfg.dev);
448                 }
449         }
450
451         return (NULL);
452 }
453
454 static int
455 pci_printf(pcicfgregs *cfg, const char *fmt, ...)
456 {
457         va_list ap;
458         int retval;
459
460         retval = printf("pci%d:%d:%d:%d: ", cfg->domain, cfg->bus, cfg->slot,
461             cfg->func);
462         va_start(ap, fmt);
463         retval += vprintf(fmt, ap);
464         va_end(ap);
465         return (retval);
466 }
467
468 /* return base address of memory or port map */
469
470 static pci_addr_t
471 pci_mapbase(uint64_t mapreg)
472 {
473
474         if (PCI_BAR_MEM(mapreg))
475                 return (mapreg & PCIM_BAR_MEM_BASE);
476         else
477                 return (mapreg & PCIM_BAR_IO_BASE);
478 }
479
480 /* return map type of memory or port map */
481
482 static const char *
483 pci_maptype(uint64_t mapreg)
484 {
485
486         if (PCI_BAR_IO(mapreg))
487                 return ("I/O Port");
488         if (mapreg & PCIM_BAR_MEM_PREFETCH)
489                 return ("Prefetchable Memory");
490         return ("Memory");
491 }
492
493 /* return log2 of map size decoded for memory or port map */
494
495 static int
496 pci_mapsize(uint64_t testval)
497 {
498         int ln2size;
499
500         testval = pci_mapbase(testval);
501         ln2size = 0;
502         if (testval != 0) {
503                 while ((testval & 1) == 0)
504                 {
505                         ln2size++;
506                         testval >>= 1;
507                 }
508         }
509         return (ln2size);
510 }
511
512 /* return base address of device ROM */
513
514 static pci_addr_t
515 pci_rombase(uint64_t mapreg)
516 {
517
518         return (mapreg & PCIM_BIOS_ADDR_MASK);
519 }
520
521 /* return log2 of map size decided for device ROM */
522
523 static int
524 pci_romsize(uint64_t testval)
525 {
526         int ln2size;
527
528         testval = pci_rombase(testval);
529         ln2size = 0;
530         if (testval != 0) {
531                 while ((testval & 1) == 0)
532                 {
533                         ln2size++;
534                         testval >>= 1;
535                 }
536         }
537         return (ln2size);
538 }
539         
540 /* return log2 of address range supported by map register */
541
542 static int
543 pci_maprange(uint64_t mapreg)
544 {
545         int ln2range = 0;
546
547         if (PCI_BAR_IO(mapreg))
548                 ln2range = 32;
549         else
550                 switch (mapreg & PCIM_BAR_MEM_TYPE) {
551                 case PCIM_BAR_MEM_32:
552                         ln2range = 32;
553                         break;
554                 case PCIM_BAR_MEM_1MB:
555                         ln2range = 20;
556                         break;
557                 case PCIM_BAR_MEM_64:
558                         ln2range = 64;
559                         break;
560                 }
561         return (ln2range);
562 }
563
564 /* adjust some values from PCI 1.0 devices to match 2.0 standards ... */
565
566 static void
567 pci_fixancient(pcicfgregs *cfg)
568 {
569         if ((cfg->hdrtype & PCIM_HDRTYPE) != PCIM_HDRTYPE_NORMAL)
570                 return;
571
572         /* PCI to PCI bridges use header type 1 */
573         if (cfg->baseclass == PCIC_BRIDGE && cfg->subclass == PCIS_BRIDGE_PCI)
574                 cfg->hdrtype = PCIM_HDRTYPE_BRIDGE;
575 }
576
577 /* extract header type specific config data */
578
579 static void
580 pci_hdrtypedata(device_t pcib, int b, int s, int f, pcicfgregs *cfg)
581 {
582 #define REG(n, w)       PCIB_READ_CONFIG(pcib, b, s, f, n, w)
583         switch (cfg->hdrtype & PCIM_HDRTYPE) {
584         case PCIM_HDRTYPE_NORMAL:
585                 cfg->subvendor      = REG(PCIR_SUBVEND_0, 2);
586                 cfg->subdevice      = REG(PCIR_SUBDEV_0, 2);
587                 cfg->mingnt         = REG(PCIR_MINGNT, 1);
588                 cfg->maxlat         = REG(PCIR_MAXLAT, 1);
589                 cfg->nummaps        = PCI_MAXMAPS_0;
590                 break;
591         case PCIM_HDRTYPE_BRIDGE:
592                 cfg->nummaps        = PCI_MAXMAPS_1;
593                 break;
594         case PCIM_HDRTYPE_CARDBUS:
595                 cfg->subvendor      = REG(PCIR_SUBVEND_2, 2);
596                 cfg->subdevice      = REG(PCIR_SUBDEV_2, 2);
597                 cfg->nummaps        = PCI_MAXMAPS_2;
598                 break;
599         }
600 #undef REG
601 }
602
603 /* read configuration header into pcicfgregs structure */
604 struct pci_devinfo *
605 pci_read_device(device_t pcib, int d, int b, int s, int f, size_t size)
606 {
607 #define REG(n, w)       PCIB_READ_CONFIG(pcib, b, s, f, n, w)
608         pcicfgregs *cfg = NULL;
609         struct pci_devinfo *devlist_entry;
610         struct devlist *devlist_head;
611
612         devlist_head = &pci_devq;
613
614         devlist_entry = NULL;
615
616         if (REG(PCIR_DEVVENDOR, 4) != 0xfffffffful) {
617                 devlist_entry = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
618                 if (devlist_entry == NULL)
619                         return (NULL);
620
621                 cfg = &devlist_entry->cfg;
622
623                 cfg->domain             = d;
624                 cfg->bus                = b;
625                 cfg->slot               = s;
626                 cfg->func               = f;
627                 cfg->vendor             = REG(PCIR_VENDOR, 2);
628                 cfg->device             = REG(PCIR_DEVICE, 2);
629                 cfg->cmdreg             = REG(PCIR_COMMAND, 2);
630                 cfg->statreg            = REG(PCIR_STATUS, 2);
631                 cfg->baseclass          = REG(PCIR_CLASS, 1);
632                 cfg->subclass           = REG(PCIR_SUBCLASS, 1);
633                 cfg->progif             = REG(PCIR_PROGIF, 1);
634                 cfg->revid              = REG(PCIR_REVID, 1);
635                 cfg->hdrtype            = REG(PCIR_HDRTYPE, 1);
636                 cfg->cachelnsz          = REG(PCIR_CACHELNSZ, 1);
637                 cfg->lattimer           = REG(PCIR_LATTIMER, 1);
638                 cfg->intpin             = REG(PCIR_INTPIN, 1);
639                 cfg->intline            = REG(PCIR_INTLINE, 1);
640
641                 cfg->mfdev              = (cfg->hdrtype & PCIM_MFDEV) != 0;
642                 cfg->hdrtype            &= ~PCIM_MFDEV;
643                 STAILQ_INIT(&cfg->maps);
644
645                 pci_fixancient(cfg);
646                 pci_hdrtypedata(pcib, b, s, f, cfg);
647
648                 if (REG(PCIR_STATUS, 2) & PCIM_STATUS_CAPPRESENT)
649                         pci_read_cap(pcib, cfg);
650
651                 STAILQ_INSERT_TAIL(devlist_head, devlist_entry, pci_links);
652
653                 devlist_entry->conf.pc_sel.pc_domain = cfg->domain;
654                 devlist_entry->conf.pc_sel.pc_bus = cfg->bus;
655                 devlist_entry->conf.pc_sel.pc_dev = cfg->slot;
656                 devlist_entry->conf.pc_sel.pc_func = cfg->func;
657                 devlist_entry->conf.pc_hdr = cfg->hdrtype;
658
659                 devlist_entry->conf.pc_subvendor = cfg->subvendor;
660                 devlist_entry->conf.pc_subdevice = cfg->subdevice;
661                 devlist_entry->conf.pc_vendor = cfg->vendor;
662                 devlist_entry->conf.pc_device = cfg->device;
663
664                 devlist_entry->conf.pc_class = cfg->baseclass;
665                 devlist_entry->conf.pc_subclass = cfg->subclass;
666                 devlist_entry->conf.pc_progif = cfg->progif;
667                 devlist_entry->conf.pc_revid = cfg->revid;
668
669                 pci_numdevs++;
670                 pci_generation++;
671         }
672         return (devlist_entry);
673 #undef REG
674 }
675
676 static void
677 pci_read_cap(device_t pcib, pcicfgregs *cfg)
678 {
679 #define REG(n, w)       PCIB_READ_CONFIG(pcib, cfg->bus, cfg->slot, cfg->func, n, w)
680 #define WREG(n, v, w)   PCIB_WRITE_CONFIG(pcib, cfg->bus, cfg->slot, cfg->func, n, v, w)
681 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__)
682         uint64_t addr;
683 #endif
684         uint32_t val;
685         int     ptr, nextptr, ptrptr;
686
687         switch (cfg->hdrtype & PCIM_HDRTYPE) {
688         case PCIM_HDRTYPE_NORMAL:
689         case PCIM_HDRTYPE_BRIDGE:
690                 ptrptr = PCIR_CAP_PTR;
691                 break;
692         case PCIM_HDRTYPE_CARDBUS:
693                 ptrptr = PCIR_CAP_PTR_2;        /* cardbus capabilities ptr */
694                 break;
695         default:
696                 return;         /* no extended capabilities support */
697         }
698         nextptr = REG(ptrptr, 1);       /* sanity check? */
699
700         /*
701          * Read capability entries.
702          */
703         while (nextptr != 0) {
704                 /* Sanity check */
705                 if (nextptr > 255) {
706                         printf("illegal PCI extended capability offset %d\n",
707                             nextptr);
708                         return;
709                 }
710                 /* Find the next entry */
711                 ptr = nextptr;
712                 nextptr = REG(ptr + PCICAP_NEXTPTR, 1);
713
714                 /* Process this entry */
715                 switch (REG(ptr + PCICAP_ID, 1)) {
716                 case PCIY_PMG:          /* PCI power management */
717                         if (cfg->pp.pp_cap == 0) {
718                                 cfg->pp.pp_cap = REG(ptr + PCIR_POWER_CAP, 2);
719                                 cfg->pp.pp_status = ptr + PCIR_POWER_STATUS;
720                                 cfg->pp.pp_bse = ptr + PCIR_POWER_BSE;
721                                 if ((nextptr - ptr) > PCIR_POWER_DATA)
722                                         cfg->pp.pp_data = ptr + PCIR_POWER_DATA;
723                         }
724                         break;
725                 case PCIY_HT:           /* HyperTransport */
726                         /* Determine HT-specific capability type. */
727                         val = REG(ptr + PCIR_HT_COMMAND, 2);
728
729                         if ((val & 0xe000) == PCIM_HTCAP_SLAVE)
730                                 cfg->ht.ht_slave = ptr;
731
732 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__)
733                         switch (val & PCIM_HTCMD_CAP_MASK) {
734                         case PCIM_HTCAP_MSI_MAPPING:
735                                 if (!(val & PCIM_HTCMD_MSI_FIXED)) {
736                                         /* Sanity check the mapping window. */
737                                         addr = REG(ptr + PCIR_HTMSI_ADDRESS_HI,
738                                             4);
739                                         addr <<= 32;
740                                         addr |= REG(ptr + PCIR_HTMSI_ADDRESS_LO,
741                                             4);
742                                         if (addr != MSI_INTEL_ADDR_BASE)
743                                                 device_printf(pcib,
744             "HT device at pci%d:%d:%d:%d has non-default MSI window 0x%llx\n",
745                                                     cfg->domain, cfg->bus,
746                                                     cfg->slot, cfg->func,
747                                                     (long long)addr);
748                                 } else
749                                         addr = MSI_INTEL_ADDR_BASE;
750
751                                 cfg->ht.ht_msimap = ptr;
752                                 cfg->ht.ht_msictrl = val;
753                                 cfg->ht.ht_msiaddr = addr;
754                                 break;
755                         }
756 #endif
757                         break;
758                 case PCIY_MSI:          /* PCI MSI */
759                         cfg->msi.msi_location = ptr;
760                         cfg->msi.msi_ctrl = REG(ptr + PCIR_MSI_CTRL, 2);
761                         cfg->msi.msi_msgnum = 1 << ((cfg->msi.msi_ctrl &
762                                                      PCIM_MSICTRL_MMC_MASK)>>1);
763                         break;
764                 case PCIY_MSIX:         /* PCI MSI-X */
765                         cfg->msix.msix_location = ptr;
766                         cfg->msix.msix_ctrl = REG(ptr + PCIR_MSIX_CTRL, 2);
767                         cfg->msix.msix_msgnum = (cfg->msix.msix_ctrl &
768                             PCIM_MSIXCTRL_TABLE_SIZE) + 1;
769                         val = REG(ptr + PCIR_MSIX_TABLE, 4);
770                         cfg->msix.msix_table_bar = PCIR_BAR(val &
771                             PCIM_MSIX_BIR_MASK);
772                         cfg->msix.msix_table_offset = val & ~PCIM_MSIX_BIR_MASK;
773                         val = REG(ptr + PCIR_MSIX_PBA, 4);
774                         cfg->msix.msix_pba_bar = PCIR_BAR(val &
775                             PCIM_MSIX_BIR_MASK);
776                         cfg->msix.msix_pba_offset = val & ~PCIM_MSIX_BIR_MASK;
777                         break;
778                 case PCIY_VPD:          /* PCI Vital Product Data */
779                         cfg->vpd.vpd_reg = ptr;
780                         break;
781                 case PCIY_SUBVENDOR:
782                         /* Should always be true. */
783                         if ((cfg->hdrtype & PCIM_HDRTYPE) ==
784                             PCIM_HDRTYPE_BRIDGE) {
785                                 val = REG(ptr + PCIR_SUBVENDCAP_ID, 4);
786                                 cfg->subvendor = val & 0xffff;
787                                 cfg->subdevice = val >> 16;
788                         }
789                         break;
790                 case PCIY_PCIX:         /* PCI-X */
791                         /*
792                          * Assume we have a PCI-X chipset if we have
793                          * at least one PCI-PCI bridge with a PCI-X
794                          * capability.  Note that some systems with
795                          * PCI-express or HT chipsets might match on
796                          * this check as well.
797                          */
798                         if ((cfg->hdrtype & PCIM_HDRTYPE) ==
799                             PCIM_HDRTYPE_BRIDGE)
800                                 pcix_chipset = 1;
801                         cfg->pcix.pcix_location = ptr;
802                         break;
803                 case PCIY_EXPRESS:      /* PCI-express */
804                         /*
805                          * Assume we have a PCI-express chipset if we have
806                          * at least one PCI-express device.
807                          */
808                         pcie_chipset = 1;
809                         cfg->pcie.pcie_location = ptr;
810                         val = REG(ptr + PCIER_FLAGS, 2);
811                         cfg->pcie.pcie_type = val & PCIEM_FLAGS_TYPE;
812                         break;
813                 default:
814                         break;
815                 }
816         }
817
818 #if defined(__powerpc__)
819         /*
820          * Enable the MSI mapping window for all HyperTransport
821          * slaves.  PCI-PCI bridges have their windows enabled via
822          * PCIB_MAP_MSI().
823          */
824         if (cfg->ht.ht_slave != 0 && cfg->ht.ht_msimap != 0 &&
825             !(cfg->ht.ht_msictrl & PCIM_HTCMD_MSI_ENABLE)) {
826                 device_printf(pcib,
827             "Enabling MSI window for HyperTransport slave at pci%d:%d:%d:%d\n",
828                     cfg->domain, cfg->bus, cfg->slot, cfg->func);
829                  cfg->ht.ht_msictrl |= PCIM_HTCMD_MSI_ENABLE;
830                  WREG(cfg->ht.ht_msimap + PCIR_HT_COMMAND, cfg->ht.ht_msictrl,
831                      2);
832         }
833 #endif
834 /* REG and WREG use carry through to next functions */
835 }
836
837 /*
838  * PCI Vital Product Data
839  */
840
841 #define PCI_VPD_TIMEOUT         1000000
842
843 static int
844 pci_read_vpd_reg(device_t pcib, pcicfgregs *cfg, int reg, uint32_t *data)
845 {
846         int count = PCI_VPD_TIMEOUT;
847
848         KASSERT((reg & 3) == 0, ("VPD register must by 4 byte aligned"));
849
850         WREG(cfg->vpd.vpd_reg + PCIR_VPD_ADDR, reg, 2);
851
852         while ((REG(cfg->vpd.vpd_reg + PCIR_VPD_ADDR, 2) & 0x8000) != 0x8000) {
853                 if (--count < 0)
854                         return (ENXIO);
855                 DELAY(1);       /* limit looping */
856         }
857         *data = (REG(cfg->vpd.vpd_reg + PCIR_VPD_DATA, 4));
858
859         return (0);
860 }
861
862 #if 0
863 static int
864 pci_write_vpd_reg(device_t pcib, pcicfgregs *cfg, int reg, uint32_t data)
865 {
866         int count = PCI_VPD_TIMEOUT;
867
868         KASSERT((reg & 3) == 0, ("VPD register must by 4 byte aligned"));
869
870         WREG(cfg->vpd.vpd_reg + PCIR_VPD_DATA, data, 4);
871         WREG(cfg->vpd.vpd_reg + PCIR_VPD_ADDR, reg | 0x8000, 2);
872         while ((REG(cfg->vpd.vpd_reg + PCIR_VPD_ADDR, 2) & 0x8000) == 0x8000) {
873                 if (--count < 0)
874                         return (ENXIO);
875                 DELAY(1);       /* limit looping */
876         }
877
878         return (0);
879 }
880 #endif
881
882 #undef PCI_VPD_TIMEOUT
883
884 struct vpd_readstate {
885         device_t        pcib;
886         pcicfgregs      *cfg;
887         uint32_t        val;
888         int             bytesinval;
889         int             off;
890         uint8_t         cksum;
891 };
892
893 static int
894 vpd_nextbyte(struct vpd_readstate *vrs, uint8_t *data)
895 {
896         uint32_t reg;
897         uint8_t byte;
898
899         if (vrs->bytesinval == 0) {
900                 if (pci_read_vpd_reg(vrs->pcib, vrs->cfg, vrs->off, &reg))
901                         return (ENXIO);
902                 vrs->val = le32toh(reg);
903                 vrs->off += 4;
904                 byte = vrs->val & 0xff;
905                 vrs->bytesinval = 3;
906         } else {
907                 vrs->val = vrs->val >> 8;
908                 byte = vrs->val & 0xff;
909                 vrs->bytesinval--;
910         }
911
912         vrs->cksum += byte;
913         *data = byte;
914         return (0);
915 }
916
917 static void
918 pci_read_vpd(device_t pcib, pcicfgregs *cfg)
919 {
920         struct vpd_readstate vrs;
921         int state;
922         int name;
923         int remain;
924         int i;
925         int alloc, off;         /* alloc/off for RO/W arrays */
926         int cksumvalid;
927         int dflen;
928         uint8_t byte;
929         uint8_t byte2;
930
931         /* init vpd reader */
932         vrs.bytesinval = 0;
933         vrs.off = 0;
934         vrs.pcib = pcib;
935         vrs.cfg = cfg;
936         vrs.cksum = 0;
937
938         state = 0;
939         name = remain = i = 0;  /* shut up stupid gcc */
940         alloc = off = 0;        /* shut up stupid gcc */
941         dflen = 0;              /* shut up stupid gcc */
942         cksumvalid = -1;
943         while (state >= 0) {
944                 if (vpd_nextbyte(&vrs, &byte)) {
945                         state = -2;
946                         break;
947                 }
948 #if 0
949                 printf("vpd: val: %#x, off: %d, bytesinval: %d, byte: %#hhx, " \
950                     "state: %d, remain: %d, name: %#x, i: %d\n", vrs.val,
951                     vrs.off, vrs.bytesinval, byte, state, remain, name, i);
952 #endif
953                 switch (state) {
954                 case 0:         /* item name */
955                         if (byte & 0x80) {
956                                 if (vpd_nextbyte(&vrs, &byte2)) {
957                                         state = -2;
958                                         break;
959                                 }
960                                 remain = byte2;
961                                 if (vpd_nextbyte(&vrs, &byte2)) {
962                                         state = -2;
963                                         break;
964                                 }
965                                 remain |= byte2 << 8;
966                                 if (remain > (0x7f*4 - vrs.off)) {
967                                         state = -1;
968                                         pci_printf(cfg,
969                                             "invalid VPD data, remain %#x\n",
970                                             remain);
971                                 }
972                                 name = byte & 0x7f;
973                         } else {
974                                 remain = byte & 0x7;
975                                 name = (byte >> 3) & 0xf;
976                         }
977                         switch (name) {
978                         case 0x2:       /* String */
979                                 cfg->vpd.vpd_ident = malloc(remain + 1,
980                                     M_DEVBUF, M_WAITOK);
981                                 i = 0;
982                                 state = 1;
983                                 break;
984                         case 0xf:       /* End */
985                                 state = -1;
986                                 break;
987                         case 0x10:      /* VPD-R */
988                                 alloc = 8;
989                                 off = 0;
990                                 cfg->vpd.vpd_ros = malloc(alloc *
991                                     sizeof(*cfg->vpd.vpd_ros), M_DEVBUF,
992                                     M_WAITOK | M_ZERO);
993                                 state = 2;
994                                 break;
995                         case 0x11:      /* VPD-W */
996                                 alloc = 8;
997                                 off = 0;
998                                 cfg->vpd.vpd_w = malloc(alloc *
999                                     sizeof(*cfg->vpd.vpd_w), M_DEVBUF,
1000                                     M_WAITOK | M_ZERO);
1001                                 state = 5;
1002                                 break;
1003                         default:        /* Invalid data, abort */
1004                                 state = -1;
1005                                 break;
1006                         }
1007                         break;
1008
1009                 case 1: /* Identifier String */
1010                         cfg->vpd.vpd_ident[i++] = byte;
1011                         remain--;
1012                         if (remain == 0)  {
1013                                 cfg->vpd.vpd_ident[i] = '\0';
1014                                 state = 0;
1015                         }
1016                         break;
1017
1018                 case 2: /* VPD-R Keyword Header */
1019                         if (off == alloc) {
1020                                 cfg->vpd.vpd_ros = reallocf(cfg->vpd.vpd_ros,
1021                                     (alloc *= 2) * sizeof(*cfg->vpd.vpd_ros),
1022                                     M_DEVBUF, M_WAITOK | M_ZERO);
1023                         }
1024                         cfg->vpd.vpd_ros[off].keyword[0] = byte;
1025                         if (vpd_nextbyte(&vrs, &byte2)) {
1026                                 state = -2;
1027                                 break;
1028                         }
1029                         cfg->vpd.vpd_ros[off].keyword[1] = byte2;
1030                         if (vpd_nextbyte(&vrs, &byte2)) {
1031                                 state = -2;
1032                                 break;
1033                         }
1034                         cfg->vpd.vpd_ros[off].len = dflen = byte2;
1035                         if (dflen == 0 &&
1036                             strncmp(cfg->vpd.vpd_ros[off].keyword, "RV",
1037                             2) == 0) {
1038                                 /*
1039                                  * if this happens, we can't trust the rest
1040                                  * of the VPD.
1041                                  */
1042                                 pci_printf(cfg, "bad keyword length: %d\n",
1043                                     dflen);
1044                                 cksumvalid = 0;
1045                                 state = -1;
1046                                 break;
1047                         } else if (dflen == 0) {
1048                                 cfg->vpd.vpd_ros[off].value = malloc(1 *
1049                                     sizeof(*cfg->vpd.vpd_ros[off].value),
1050                                     M_DEVBUF, M_WAITOK);
1051                                 cfg->vpd.vpd_ros[off].value[0] = '\x00';
1052                         } else
1053                                 cfg->vpd.vpd_ros[off].value = malloc(
1054                                     (dflen + 1) *
1055                                     sizeof(*cfg->vpd.vpd_ros[off].value),
1056                                     M_DEVBUF, M_WAITOK);
1057                         remain -= 3;
1058                         i = 0;
1059                         /* keep in sync w/ state 3's transistions */
1060                         if (dflen == 0 && remain == 0)
1061                                 state = 0;
1062                         else if (dflen == 0)
1063                                 state = 2;
1064                         else
1065                                 state = 3;
1066                         break;
1067
1068                 case 3: /* VPD-R Keyword Value */
1069                         cfg->vpd.vpd_ros[off].value[i++] = byte;
1070                         if (strncmp(cfg->vpd.vpd_ros[off].keyword,
1071                             "RV", 2) == 0 && cksumvalid == -1) {
1072                                 if (vrs.cksum == 0)
1073                                         cksumvalid = 1;
1074                                 else {
1075                                         if (bootverbose)
1076                                                 pci_printf(cfg,
1077                                             "bad VPD cksum, remain %hhu\n",
1078                                                     vrs.cksum);
1079                                         cksumvalid = 0;
1080                                         state = -1;
1081                                         break;
1082                                 }
1083                         }
1084                         dflen--;
1085                         remain--;
1086                         /* keep in sync w/ state 2's transistions */
1087                         if (dflen == 0)
1088                                 cfg->vpd.vpd_ros[off++].value[i++] = '\0';
1089                         if (dflen == 0 && remain == 0) {
1090                                 cfg->vpd.vpd_rocnt = off;
1091                                 cfg->vpd.vpd_ros = reallocf(cfg->vpd.vpd_ros,
1092                                     off * sizeof(*cfg->vpd.vpd_ros),
1093                                     M_DEVBUF, M_WAITOK | M_ZERO);
1094                                 state = 0;
1095                         } else if (dflen == 0)
1096                                 state = 2;
1097                         break;
1098
1099                 case 4:
1100                         remain--;
1101                         if (remain == 0)
1102                                 state = 0;
1103                         break;
1104
1105                 case 5: /* VPD-W Keyword Header */
1106                         if (off == alloc) {
1107                                 cfg->vpd.vpd_w = reallocf(cfg->vpd.vpd_w,
1108                                     (alloc *= 2) * sizeof(*cfg->vpd.vpd_w),
1109                                     M_DEVBUF, M_WAITOK | M_ZERO);
1110                         }
1111                         cfg->vpd.vpd_w[off].keyword[0] = byte;
1112                         if (vpd_nextbyte(&vrs, &byte2)) {
1113                                 state = -2;
1114                                 break;
1115                         }
1116                         cfg->vpd.vpd_w[off].keyword[1] = byte2;
1117                         if (vpd_nextbyte(&vrs, &byte2)) {
1118                                 state = -2;
1119                                 break;
1120                         }
1121                         cfg->vpd.vpd_w[off].len = dflen = byte2;
1122                         cfg->vpd.vpd_w[off].start = vrs.off - vrs.bytesinval;
1123                         cfg->vpd.vpd_w[off].value = malloc((dflen + 1) *
1124                             sizeof(*cfg->vpd.vpd_w[off].value),
1125                             M_DEVBUF, M_WAITOK);
1126                         remain -= 3;
1127                         i = 0;
1128                         /* keep in sync w/ state 6's transistions */
1129                         if (dflen == 0 && remain == 0)
1130                                 state = 0;
1131                         else if (dflen == 0)
1132                                 state = 5;
1133                         else
1134                                 state = 6;
1135                         break;
1136
1137                 case 6: /* VPD-W Keyword Value */
1138                         cfg->vpd.vpd_w[off].value[i++] = byte;
1139                         dflen--;
1140                         remain--;
1141                         /* keep in sync w/ state 5's transistions */
1142                         if (dflen == 0)
1143                                 cfg->vpd.vpd_w[off++].value[i++] = '\0';
1144                         if (dflen == 0 && remain == 0) {
1145                                 cfg->vpd.vpd_wcnt = off;
1146                                 cfg->vpd.vpd_w = reallocf(cfg->vpd.vpd_w,
1147                                     off * sizeof(*cfg->vpd.vpd_w),
1148                                     M_DEVBUF, M_WAITOK | M_ZERO);
1149                                 state = 0;
1150                         } else if (dflen == 0)
1151                                 state = 5;
1152                         break;
1153
1154                 default:
1155                         pci_printf(cfg, "invalid state: %d\n", state);
1156                         state = -1;
1157                         break;
1158                 }
1159         }
1160
1161         if (cksumvalid == 0 || state < -1) {
1162                 /* read-only data bad, clean up */
1163                 if (cfg->vpd.vpd_ros != NULL) {
1164                         for (off = 0; cfg->vpd.vpd_ros[off].value; off++)
1165                                 free(cfg->vpd.vpd_ros[off].value, M_DEVBUF);
1166                         free(cfg->vpd.vpd_ros, M_DEVBUF);
1167                         cfg->vpd.vpd_ros = NULL;
1168                 }
1169         }
1170         if (state < -1) {
1171                 /* I/O error, clean up */
1172                 pci_printf(cfg, "failed to read VPD data.\n");
1173                 if (cfg->vpd.vpd_ident != NULL) {
1174                         free(cfg->vpd.vpd_ident, M_DEVBUF);
1175                         cfg->vpd.vpd_ident = NULL;
1176                 }
1177                 if (cfg->vpd.vpd_w != NULL) {
1178                         for (off = 0; cfg->vpd.vpd_w[off].value; off++)
1179                                 free(cfg->vpd.vpd_w[off].value, M_DEVBUF);
1180                         free(cfg->vpd.vpd_w, M_DEVBUF);
1181                         cfg->vpd.vpd_w = NULL;
1182                 }
1183         }
1184         cfg->vpd.vpd_cached = 1;
1185 #undef REG
1186 #undef WREG
1187 }
1188
1189 int
1190 pci_get_vpd_ident_method(device_t dev, device_t child, const char **identptr)
1191 {
1192         struct pci_devinfo *dinfo = device_get_ivars(child);
1193         pcicfgregs *cfg = &dinfo->cfg;
1194
1195         if (!cfg->vpd.vpd_cached && cfg->vpd.vpd_reg != 0)
1196                 pci_read_vpd(device_get_parent(dev), cfg);
1197
1198         *identptr = cfg->vpd.vpd_ident;
1199
1200         if (*identptr == NULL)
1201                 return (ENXIO);
1202
1203         return (0);
1204 }
1205
1206 int
1207 pci_get_vpd_readonly_method(device_t dev, device_t child, const char *kw,
1208         const char **vptr)
1209 {
1210         struct pci_devinfo *dinfo = device_get_ivars(child);
1211         pcicfgregs *cfg = &dinfo->cfg;
1212         int i;
1213
1214         if (!cfg->vpd.vpd_cached && cfg->vpd.vpd_reg != 0)
1215                 pci_read_vpd(device_get_parent(dev), cfg);
1216
1217         for (i = 0; i < cfg->vpd.vpd_rocnt; i++)
1218                 if (memcmp(kw, cfg->vpd.vpd_ros[i].keyword,
1219                     sizeof(cfg->vpd.vpd_ros[i].keyword)) == 0) {
1220                         *vptr = cfg->vpd.vpd_ros[i].value;
1221                         return (0);
1222                 }
1223
1224         *vptr = NULL;
1225         return (ENXIO);
1226 }
1227
1228 struct pcicfg_vpd *
1229 pci_fetch_vpd_list(device_t dev)
1230 {
1231         struct pci_devinfo *dinfo = device_get_ivars(dev);
1232         pcicfgregs *cfg = &dinfo->cfg;
1233
1234         if (!cfg->vpd.vpd_cached && cfg->vpd.vpd_reg != 0)
1235                 pci_read_vpd(device_get_parent(device_get_parent(dev)), cfg);
1236         return (&cfg->vpd);
1237 }
1238
1239 /*
1240  * Find the requested HyperTransport capability and return the offset
1241  * in configuration space via the pointer provided.  The function
1242  * returns 0 on success and an error code otherwise.
1243  */
1244 int
1245 pci_find_htcap_method(device_t dev, device_t child, int capability, int *capreg)
1246 {
1247         int ptr, error;
1248         uint16_t val;
1249
1250         error = pci_find_cap(child, PCIY_HT, &ptr);
1251         if (error)
1252                 return (error);
1253
1254         /*
1255          * Traverse the capabilities list checking each HT capability
1256          * to see if it matches the requested HT capability.
1257          */
1258         while (ptr != 0) {
1259                 val = pci_read_config(child, ptr + PCIR_HT_COMMAND, 2);
1260                 if (capability == PCIM_HTCAP_SLAVE ||
1261                     capability == PCIM_HTCAP_HOST)
1262                         val &= 0xe000;
1263                 else
1264                         val &= PCIM_HTCMD_CAP_MASK;
1265                 if (val == capability) {
1266                         if (capreg != NULL)
1267                                 *capreg = ptr;
1268                         return (0);
1269                 }
1270
1271                 /* Skip to the next HT capability. */
1272                 while (ptr != 0) {
1273                         ptr = pci_read_config(child, ptr + PCICAP_NEXTPTR, 1);
1274                         if (pci_read_config(child, ptr + PCICAP_ID, 1) ==
1275                             PCIY_HT)
1276                                 break;
1277                 }
1278         }
1279         return (ENOENT);
1280 }
1281
1282 /*
1283  * Find the requested capability and return the offset in
1284  * configuration space via the pointer provided.  The function returns
1285  * 0 on success and an error code otherwise.
1286  */
1287 int
1288 pci_find_cap_method(device_t dev, device_t child, int capability,
1289     int *capreg)
1290 {
1291         struct pci_devinfo *dinfo = device_get_ivars(child);
1292         pcicfgregs *cfg = &dinfo->cfg;
1293         u_int32_t status;
1294         u_int8_t ptr;
1295
1296         /*
1297          * Check the CAP_LIST bit of the PCI status register first.
1298          */
1299         status = pci_read_config(child, PCIR_STATUS, 2);
1300         if (!(status & PCIM_STATUS_CAPPRESENT))
1301                 return (ENXIO);
1302
1303         /*
1304          * Determine the start pointer of the capabilities list.
1305          */
1306         switch (cfg->hdrtype & PCIM_HDRTYPE) {
1307         case PCIM_HDRTYPE_NORMAL:
1308         case PCIM_HDRTYPE_BRIDGE:
1309                 ptr = PCIR_CAP_PTR;
1310                 break;
1311         case PCIM_HDRTYPE_CARDBUS:
1312                 ptr = PCIR_CAP_PTR_2;
1313                 break;
1314         default:
1315                 /* XXX: panic? */
1316                 return (ENXIO);         /* no extended capabilities support */
1317         }
1318         ptr = pci_read_config(child, ptr, 1);
1319
1320         /*
1321          * Traverse the capabilities list.
1322          */
1323         while (ptr != 0) {
1324                 if (pci_read_config(child, ptr + PCICAP_ID, 1) == capability) {
1325                         if (capreg != NULL)
1326                                 *capreg = ptr;
1327                         return (0);
1328                 }
1329                 ptr = pci_read_config(child, ptr + PCICAP_NEXTPTR, 1);
1330         }
1331
1332         return (ENOENT);
1333 }
1334
1335 /*
1336  * Find the requested extended capability and return the offset in
1337  * configuration space via the pointer provided.  The function returns
1338  * 0 on success and an error code otherwise.
1339  */
1340 int
1341 pci_find_extcap_method(device_t dev, device_t child, int capability,
1342     int *capreg)
1343 {
1344         struct pci_devinfo *dinfo = device_get_ivars(child);
1345         pcicfgregs *cfg = &dinfo->cfg;
1346         uint32_t ecap;
1347         uint16_t ptr;
1348
1349         /* Only supported for PCI-express devices. */
1350         if (cfg->pcie.pcie_location == 0)
1351                 return (ENXIO);
1352
1353         ptr = PCIR_EXTCAP;
1354         ecap = pci_read_config(child, ptr, 4);
1355         if (ecap == 0xffffffff || ecap == 0)
1356                 return (ENOENT);
1357         for (;;) {
1358                 if (PCI_EXTCAP_ID(ecap) == capability) {
1359                         if (capreg != NULL)
1360                                 *capreg = ptr;
1361                         return (0);
1362                 }
1363                 ptr = PCI_EXTCAP_NEXTPTR(ecap);
1364                 if (ptr == 0)
1365                         break;
1366                 ecap = pci_read_config(child, ptr, 4);
1367         }
1368
1369         return (ENOENT);
1370 }
1371
1372 /*
1373  * Support for MSI-X message interrupts.
1374  */
1375 void
1376 pci_enable_msix_method(device_t dev, device_t child, u_int index,
1377     uint64_t address, uint32_t data)
1378 {
1379         struct pci_devinfo *dinfo = device_get_ivars(child);
1380         struct pcicfg_msix *msix = &dinfo->cfg.msix;
1381         uint32_t offset;
1382
1383         KASSERT(msix->msix_table_len > index, ("bogus index"));
1384         offset = msix->msix_table_offset + index * 16;
1385         bus_write_4(msix->msix_table_res, offset, address & 0xffffffff);
1386         bus_write_4(msix->msix_table_res, offset + 4, address >> 32);
1387         bus_write_4(msix->msix_table_res, offset + 8, data);
1388
1389         /* Enable MSI -> HT mapping. */
1390         pci_ht_map_msi(child, address);
1391 }
1392
1393 void
1394 pci_mask_msix(device_t dev, u_int index)
1395 {
1396         struct pci_devinfo *dinfo = device_get_ivars(dev);
1397         struct pcicfg_msix *msix = &dinfo->cfg.msix;
1398         uint32_t offset, val;
1399
1400         KASSERT(msix->msix_msgnum > index, ("bogus index"));
1401         offset = msix->msix_table_offset + index * 16 + 12;
1402         val = bus_read_4(msix->msix_table_res, offset);
1403         if (!(val & PCIM_MSIX_VCTRL_MASK)) {
1404                 val |= PCIM_MSIX_VCTRL_MASK;
1405                 bus_write_4(msix->msix_table_res, offset, val);
1406         }
1407 }
1408
1409 void
1410 pci_unmask_msix(device_t dev, u_int index)
1411 {
1412         struct pci_devinfo *dinfo = device_get_ivars(dev);
1413         struct pcicfg_msix *msix = &dinfo->cfg.msix;
1414         uint32_t offset, val;
1415
1416         KASSERT(msix->msix_table_len > index, ("bogus index"));
1417         offset = msix->msix_table_offset + index * 16 + 12;
1418         val = bus_read_4(msix->msix_table_res, offset);
1419         if (val & PCIM_MSIX_VCTRL_MASK) {
1420                 val &= ~PCIM_MSIX_VCTRL_MASK;
1421                 bus_write_4(msix->msix_table_res, offset, val);
1422         }
1423 }
1424
1425 int
1426 pci_pending_msix(device_t dev, u_int index)
1427 {
1428         struct pci_devinfo *dinfo = device_get_ivars(dev);
1429         struct pcicfg_msix *msix = &dinfo->cfg.msix;
1430         uint32_t offset, bit;
1431
1432         KASSERT(msix->msix_table_len > index, ("bogus index"));
1433         offset = msix->msix_pba_offset + (index / 32) * 4;
1434         bit = 1 << index % 32;
1435         return (bus_read_4(msix->msix_pba_res, offset) & bit);
1436 }
1437
1438 /*
1439  * Restore MSI-X registers and table during resume.  If MSI-X is
1440  * enabled then walk the virtual table to restore the actual MSI-X
1441  * table.
1442  */
1443 static void
1444 pci_resume_msix(device_t dev)
1445 {
1446         struct pci_devinfo *dinfo = device_get_ivars(dev);
1447         struct pcicfg_msix *msix = &dinfo->cfg.msix;
1448         struct msix_table_entry *mte;
1449         struct msix_vector *mv;
1450         int i;
1451
1452         if (msix->msix_alloc > 0) {
1453                 /* First, mask all vectors. */
1454                 for (i = 0; i < msix->msix_msgnum; i++)
1455                         pci_mask_msix(dev, i);
1456
1457                 /* Second, program any messages with at least one handler. */
1458                 for (i = 0; i < msix->msix_table_len; i++) {
1459                         mte = &msix->msix_table[i];
1460                         if (mte->mte_vector == 0 || mte->mte_handlers == 0)
1461                                 continue;
1462                         mv = &msix->msix_vectors[mte->mte_vector - 1];
1463                         pci_enable_msix(dev, i, mv->mv_address, mv->mv_data);
1464                         pci_unmask_msix(dev, i);
1465                 }
1466         }
1467         pci_write_config(dev, msix->msix_location + PCIR_MSIX_CTRL,
1468             msix->msix_ctrl, 2);
1469 }
1470
1471 /*
1472  * Attempt to allocate *count MSI-X messages.  The actual number allocated is
1473  * returned in *count.  After this function returns, each message will be
1474  * available to the driver as SYS_RES_IRQ resources starting at rid 1.
1475  */
1476 int
1477 pci_alloc_msix_method(device_t dev, device_t child, int *count)
1478 {
1479         struct pci_devinfo *dinfo = device_get_ivars(child);
1480         pcicfgregs *cfg = &dinfo->cfg;
1481         struct resource_list_entry *rle;
1482         int actual, error, i, irq, max;
1483
1484         /* Don't let count == 0 get us into trouble. */
1485         if (*count == 0)
1486                 return (EINVAL);
1487
1488         /* If rid 0 is allocated, then fail. */
1489         rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, 0);
1490         if (rle != NULL && rle->res != NULL)
1491                 return (ENXIO);
1492
1493         /* Already have allocated messages? */
1494         if (cfg->msi.msi_alloc != 0 || cfg->msix.msix_alloc != 0)
1495                 return (ENXIO);
1496
1497         /* If MSI-X is blacklisted for this system, fail. */
1498         if (pci_msix_blacklisted())
1499                 return (ENXIO);
1500
1501         /* MSI-X capability present? */
1502         if (cfg->msix.msix_location == 0 || !pci_do_msix)
1503                 return (ENODEV);
1504
1505         /* Make sure the appropriate BARs are mapped. */
1506         rle = resource_list_find(&dinfo->resources, SYS_RES_MEMORY,
1507             cfg->msix.msix_table_bar);
1508         if (rle == NULL || rle->res == NULL ||
1509             !(rman_get_flags(rle->res) & RF_ACTIVE))
1510                 return (ENXIO);
1511         cfg->msix.msix_table_res = rle->res;
1512         if (cfg->msix.msix_pba_bar != cfg->msix.msix_table_bar) {
1513                 rle = resource_list_find(&dinfo->resources, SYS_RES_MEMORY,
1514                     cfg->msix.msix_pba_bar);
1515                 if (rle == NULL || rle->res == NULL ||
1516                     !(rman_get_flags(rle->res) & RF_ACTIVE))
1517                         return (ENXIO);
1518         }
1519         cfg->msix.msix_pba_res = rle->res;
1520
1521         if (bootverbose)
1522                 device_printf(child,
1523                     "attempting to allocate %d MSI-X vectors (%d supported)\n",
1524                     *count, cfg->msix.msix_msgnum);
1525         max = min(*count, cfg->msix.msix_msgnum);
1526         for (i = 0; i < max; i++) {
1527                 /* Allocate a message. */
1528                 error = PCIB_ALLOC_MSIX(device_get_parent(dev), child, &irq);
1529                 if (error) {
1530                         if (i == 0)
1531                                 return (error);
1532                         break;
1533                 }
1534                 resource_list_add(&dinfo->resources, SYS_RES_IRQ, i + 1, irq,
1535                     irq, 1);
1536         }
1537         actual = i;
1538
1539         if (bootverbose) {
1540                 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, 1);
1541                 if (actual == 1)
1542                         device_printf(child, "using IRQ %lu for MSI-X\n",
1543                             rle->start);
1544                 else {
1545                         int run;
1546
1547                         /*
1548                          * Be fancy and try to print contiguous runs of
1549                          * IRQ values as ranges.  'irq' is the previous IRQ.
1550                          * 'run' is true if we are in a range.
1551                          */
1552                         device_printf(child, "using IRQs %lu", rle->start);
1553                         irq = rle->start;
1554                         run = 0;
1555                         for (i = 1; i < actual; i++) {
1556                                 rle = resource_list_find(&dinfo->resources,
1557                                     SYS_RES_IRQ, i + 1);
1558
1559                                 /* Still in a run? */
1560                                 if (rle->start == irq + 1) {
1561                                         run = 1;
1562                                         irq++;
1563                                         continue;
1564                                 }
1565
1566                                 /* Finish previous range. */
1567                                 if (run) {
1568                                         printf("-%d", irq);
1569                                         run = 0;
1570                                 }
1571
1572                                 /* Start new range. */
1573                                 printf(",%lu", rle->start);
1574                                 irq = rle->start;
1575                         }
1576
1577                         /* Unfinished range? */
1578                         if (run)
1579                                 printf("-%d", irq);
1580                         printf(" for MSI-X\n");
1581                 }
1582         }
1583
1584         /* Mask all vectors. */
1585         for (i = 0; i < cfg->msix.msix_msgnum; i++)
1586                 pci_mask_msix(child, i);
1587
1588         /* Allocate and initialize vector data and virtual table. */
1589         cfg->msix.msix_vectors = malloc(sizeof(struct msix_vector) * actual,
1590             M_DEVBUF, M_WAITOK | M_ZERO);
1591         cfg->msix.msix_table = malloc(sizeof(struct msix_table_entry) * actual,
1592             M_DEVBUF, M_WAITOK | M_ZERO);
1593         for (i = 0; i < actual; i++) {
1594                 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, i + 1);
1595                 cfg->msix.msix_vectors[i].mv_irq = rle->start;
1596                 cfg->msix.msix_table[i].mte_vector = i + 1;
1597         }
1598
1599         /* Update control register to enable MSI-X. */
1600         cfg->msix.msix_ctrl |= PCIM_MSIXCTRL_MSIX_ENABLE;
1601         pci_write_config(child, cfg->msix.msix_location + PCIR_MSIX_CTRL,
1602             cfg->msix.msix_ctrl, 2);
1603
1604         /* Update counts of alloc'd messages. */
1605         cfg->msix.msix_alloc = actual;
1606         cfg->msix.msix_table_len = actual;
1607         *count = actual;
1608         return (0);
1609 }
1610
1611 /*
1612  * By default, pci_alloc_msix() will assign the allocated IRQ
1613  * resources consecutively to the first N messages in the MSI-X table.
1614  * However, device drivers may want to use different layouts if they
1615  * either receive fewer messages than they asked for, or they wish to
1616  * populate the MSI-X table sparsely.  This method allows the driver
1617  * to specify what layout it wants.  It must be called after a
1618  * successful pci_alloc_msix() but before any of the associated
1619  * SYS_RES_IRQ resources are allocated via bus_alloc_resource().
1620  *
1621  * The 'vectors' array contains 'count' message vectors.  The array
1622  * maps directly to the MSI-X table in that index 0 in the array
1623  * specifies the vector for the first message in the MSI-X table, etc.
1624  * The vector value in each array index can either be 0 to indicate
1625  * that no vector should be assigned to a message slot, or it can be a
1626  * number from 1 to N (where N is the count returned from a
1627  * succcessful call to pci_alloc_msix()) to indicate which message
1628  * vector (IRQ) to be used for the corresponding message.
1629  *
1630  * On successful return, each message with a non-zero vector will have
1631  * an associated SYS_RES_IRQ whose rid is equal to the array index +
1632  * 1.  Additionally, if any of the IRQs allocated via the previous
1633  * call to pci_alloc_msix() are not used in the mapping, those IRQs
1634  * will be freed back to the system automatically.
1635  *
1636  * For example, suppose a driver has a MSI-X table with 6 messages and
1637  * asks for 6 messages, but pci_alloc_msix() only returns a count of
1638  * 3.  Call the three vectors allocated by pci_alloc_msix() A, B, and
1639  * C.  After the call to pci_alloc_msix(), the device will be setup to
1640  * have an MSI-X table of ABC--- (where - means no vector assigned).
1641  * If the driver then passes a vector array of { 1, 0, 1, 2, 0, 2 },
1642  * then the MSI-X table will look like A-AB-B, and the 'C' vector will
1643  * be freed back to the system.  This device will also have valid
1644  * SYS_RES_IRQ rids of 1, 3, 4, and 6.
1645  *
1646  * In any case, the SYS_RES_IRQ rid X will always map to the message
1647  * at MSI-X table index X - 1 and will only be valid if a vector is
1648  * assigned to that table entry.
1649  */
1650 int
1651 pci_remap_msix_method(device_t dev, device_t child, int count,
1652     const u_int *vectors)
1653 {
1654         struct pci_devinfo *dinfo = device_get_ivars(child);
1655         struct pcicfg_msix *msix = &dinfo->cfg.msix;
1656         struct resource_list_entry *rle;
1657         int i, irq, j, *used;
1658
1659         /*
1660          * Have to have at least one message in the table but the
1661          * table can't be bigger than the actual MSI-X table in the
1662          * device.
1663          */
1664         if (count == 0 || count > msix->msix_msgnum)
1665                 return (EINVAL);
1666
1667         /* Sanity check the vectors. */
1668         for (i = 0; i < count; i++)
1669                 if (vectors[i] > msix->msix_alloc)
1670                         return (EINVAL);
1671
1672         /*
1673          * Make sure there aren't any holes in the vectors to be used.
1674          * It's a big pain to support it, and it doesn't really make
1675          * sense anyway.  Also, at least one vector must be used.
1676          */
1677         used = malloc(sizeof(int) * msix->msix_alloc, M_DEVBUF, M_WAITOK |
1678             M_ZERO);
1679         for (i = 0; i < count; i++)
1680                 if (vectors[i] != 0)
1681                         used[vectors[i] - 1] = 1;
1682         for (i = 0; i < msix->msix_alloc - 1; i++)
1683                 if (used[i] == 0 && used[i + 1] == 1) {
1684                         free(used, M_DEVBUF);
1685                         return (EINVAL);
1686                 }
1687         if (used[0] != 1) {
1688                 free(used, M_DEVBUF);
1689                 return (EINVAL);
1690         }
1691         
1692         /* Make sure none of the resources are allocated. */
1693         for (i = 0; i < msix->msix_table_len; i++) {
1694                 if (msix->msix_table[i].mte_vector == 0)
1695                         continue;
1696                 if (msix->msix_table[i].mte_handlers > 0)
1697                         return (EBUSY);
1698                 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, i + 1);
1699                 KASSERT(rle != NULL, ("missing resource"));
1700                 if (rle->res != NULL)
1701                         return (EBUSY);
1702         }
1703
1704         /* Free the existing resource list entries. */
1705         for (i = 0; i < msix->msix_table_len; i++) {
1706                 if (msix->msix_table[i].mte_vector == 0)
1707                         continue;
1708                 resource_list_delete(&dinfo->resources, SYS_RES_IRQ, i + 1);
1709         }
1710
1711         /*
1712          * Build the new virtual table keeping track of which vectors are
1713          * used.
1714          */
1715         free(msix->msix_table, M_DEVBUF);
1716         msix->msix_table = malloc(sizeof(struct msix_table_entry) * count,
1717             M_DEVBUF, M_WAITOK | M_ZERO);
1718         for (i = 0; i < count; i++)
1719                 msix->msix_table[i].mte_vector = vectors[i];
1720         msix->msix_table_len = count;
1721
1722         /* Free any unused IRQs and resize the vectors array if necessary. */
1723         j = msix->msix_alloc - 1;
1724         if (used[j] == 0) {
1725                 struct msix_vector *vec;
1726
1727                 while (used[j] == 0) {
1728                         PCIB_RELEASE_MSIX(device_get_parent(dev), child,
1729                             msix->msix_vectors[j].mv_irq);
1730                         j--;
1731                 }
1732                 vec = malloc(sizeof(struct msix_vector) * (j + 1), M_DEVBUF,
1733                     M_WAITOK);
1734                 bcopy(msix->msix_vectors, vec, sizeof(struct msix_vector) *
1735                     (j + 1));
1736                 free(msix->msix_vectors, M_DEVBUF);
1737                 msix->msix_vectors = vec;
1738                 msix->msix_alloc = j + 1;
1739         }
1740         free(used, M_DEVBUF);
1741
1742         /* Map the IRQs onto the rids. */
1743         for (i = 0; i < count; i++) {
1744                 if (vectors[i] == 0)
1745                         continue;
1746                 irq = msix->msix_vectors[vectors[i]].mv_irq;
1747                 resource_list_add(&dinfo->resources, SYS_RES_IRQ, i + 1, irq,
1748                     irq, 1);
1749         }
1750
1751         if (bootverbose) {
1752                 device_printf(child, "Remapped MSI-X IRQs as: ");
1753                 for (i = 0; i < count; i++) {
1754                         if (i != 0)
1755                                 printf(", ");
1756                         if (vectors[i] == 0)
1757                                 printf("---");
1758                         else
1759                                 printf("%d",
1760                                     msix->msix_vectors[vectors[i]].mv_irq);
1761                 }
1762                 printf("\n");
1763         }
1764
1765         return (0);
1766 }
1767
1768 static int
1769 pci_release_msix(device_t dev, device_t child)
1770 {
1771         struct pci_devinfo *dinfo = device_get_ivars(child);
1772         struct pcicfg_msix *msix = &dinfo->cfg.msix;
1773         struct resource_list_entry *rle;
1774         int i;
1775
1776         /* Do we have any messages to release? */
1777         if (msix->msix_alloc == 0)
1778                 return (ENODEV);
1779
1780         /* Make sure none of the resources are allocated. */
1781         for (i = 0; i < msix->msix_table_len; i++) {
1782                 if (msix->msix_table[i].mte_vector == 0)
1783                         continue;
1784                 if (msix->msix_table[i].mte_handlers > 0)
1785                         return (EBUSY);
1786                 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, i + 1);
1787                 KASSERT(rle != NULL, ("missing resource"));
1788                 if (rle->res != NULL)
1789                         return (EBUSY);
1790         }
1791
1792         /* Update control register to disable MSI-X. */
1793         msix->msix_ctrl &= ~PCIM_MSIXCTRL_MSIX_ENABLE;
1794         pci_write_config(child, msix->msix_location + PCIR_MSIX_CTRL,
1795             msix->msix_ctrl, 2);
1796
1797         /* Free the resource list entries. */
1798         for (i = 0; i < msix->msix_table_len; i++) {
1799                 if (msix->msix_table[i].mte_vector == 0)
1800                         continue;
1801                 resource_list_delete(&dinfo->resources, SYS_RES_IRQ, i + 1);
1802         }
1803         free(msix->msix_table, M_DEVBUF);
1804         msix->msix_table_len = 0;
1805
1806         /* Release the IRQs. */
1807         for (i = 0; i < msix->msix_alloc; i++)
1808                 PCIB_RELEASE_MSIX(device_get_parent(dev), child,
1809                     msix->msix_vectors[i].mv_irq);
1810         free(msix->msix_vectors, M_DEVBUF);
1811         msix->msix_alloc = 0;
1812         return (0);
1813 }
1814
1815 /*
1816  * Return the max supported MSI-X messages this device supports.
1817  * Basically, assuming the MD code can alloc messages, this function
1818  * should return the maximum value that pci_alloc_msix() can return.
1819  * Thus, it is subject to the tunables, etc.
1820  */
1821 int
1822 pci_msix_count_method(device_t dev, device_t child)
1823 {
1824         struct pci_devinfo *dinfo = device_get_ivars(child);
1825         struct pcicfg_msix *msix = &dinfo->cfg.msix;
1826
1827         if (pci_do_msix && msix->msix_location != 0)
1828                 return (msix->msix_msgnum);
1829         return (0);
1830 }
1831
1832 /*
1833  * HyperTransport MSI mapping control
1834  */
1835 void
1836 pci_ht_map_msi(device_t dev, uint64_t addr)
1837 {
1838         struct pci_devinfo *dinfo = device_get_ivars(dev);
1839         struct pcicfg_ht *ht = &dinfo->cfg.ht;
1840
1841         if (!ht->ht_msimap)
1842                 return;
1843
1844         if (addr && !(ht->ht_msictrl & PCIM_HTCMD_MSI_ENABLE) &&
1845             ht->ht_msiaddr >> 20 == addr >> 20) {
1846                 /* Enable MSI -> HT mapping. */
1847                 ht->ht_msictrl |= PCIM_HTCMD_MSI_ENABLE;
1848                 pci_write_config(dev, ht->ht_msimap + PCIR_HT_COMMAND,
1849                     ht->ht_msictrl, 2);
1850         }
1851
1852         if (!addr && ht->ht_msictrl & PCIM_HTCMD_MSI_ENABLE) {
1853                 /* Disable MSI -> HT mapping. */
1854                 ht->ht_msictrl &= ~PCIM_HTCMD_MSI_ENABLE;
1855                 pci_write_config(dev, ht->ht_msimap + PCIR_HT_COMMAND,
1856                     ht->ht_msictrl, 2);
1857         }
1858 }
1859
1860 int
1861 pci_get_max_read_req(device_t dev)
1862 {
1863         struct pci_devinfo *dinfo = device_get_ivars(dev);
1864         int cap;
1865         uint16_t val;
1866
1867         cap = dinfo->cfg.pcie.pcie_location;
1868         if (cap == 0)
1869                 return (0);
1870         val = pci_read_config(dev, cap + PCIER_DEVICE_CTL, 2);
1871         val &= PCIEM_CTL_MAX_READ_REQUEST;
1872         val >>= 12;
1873         return (1 << (val + 7));
1874 }
1875
1876 int
1877 pci_set_max_read_req(device_t dev, int size)
1878 {
1879         struct pci_devinfo *dinfo = device_get_ivars(dev);
1880         int cap;
1881         uint16_t val;
1882
1883         cap = dinfo->cfg.pcie.pcie_location;
1884         if (cap == 0)
1885                 return (0);
1886         if (size < 128)
1887                 size = 128;
1888         if (size > 4096)
1889                 size = 4096;
1890         size = (1 << (fls(size) - 1));
1891         val = pci_read_config(dev, cap + PCIER_DEVICE_CTL, 2);
1892         val &= ~PCIEM_CTL_MAX_READ_REQUEST;
1893         val |= (fls(size) - 8) << 12;
1894         pci_write_config(dev, cap + PCIER_DEVICE_CTL, val, 2);
1895         return (size);
1896 }
1897
1898 uint32_t
1899 pcie_read_config(device_t dev, int reg, int width)
1900 {
1901         struct pci_devinfo *dinfo = device_get_ivars(dev);
1902         int cap;
1903
1904         cap = dinfo->cfg.pcie.pcie_location;
1905         if (cap == 0) {
1906                 if (width == 2)
1907                         return (0xffff);
1908                 return (0xffffffff);
1909         }
1910
1911         return (pci_read_config(dev, cap + reg, width));
1912 }
1913
1914 void
1915 pcie_write_config(device_t dev, int reg, uint32_t value, int width)
1916 {
1917         struct pci_devinfo *dinfo = device_get_ivars(dev);
1918         int cap;
1919
1920         cap = dinfo->cfg.pcie.pcie_location;
1921         if (cap == 0)
1922                 return;
1923         pci_write_config(dev, cap + reg, value, width);
1924 }
1925
1926 /*
1927  * Adjusts a PCI-e capability register by clearing the bits in mask
1928  * and setting the bits in (value & mask).  Bits not set in mask are
1929  * not adjusted.
1930  *
1931  * Returns the old value on success or all ones on failure.
1932  */
1933 uint32_t
1934 pcie_adjust_config(device_t dev, int reg, uint32_t mask, uint32_t value,
1935     int width)
1936 {
1937         struct pci_devinfo *dinfo = device_get_ivars(dev);
1938         uint32_t old, new;
1939         int cap;
1940
1941         cap = dinfo->cfg.pcie.pcie_location;
1942         if (cap == 0) {
1943                 if (width == 2)
1944                         return (0xffff);
1945                 return (0xffffffff);
1946         }
1947
1948         old = pci_read_config(dev, cap + reg, width);
1949         new = old & ~mask;
1950         new |= (value & mask);
1951         pci_write_config(dev, cap + reg, new, width);
1952         return (old);
1953 }
1954
1955 /*
1956  * Support for MSI message signalled interrupts.
1957  */
1958 void
1959 pci_enable_msi_method(device_t dev, device_t child, uint64_t address,
1960     uint16_t data)
1961 {
1962         struct pci_devinfo *dinfo = device_get_ivars(child);
1963         struct pcicfg_msi *msi = &dinfo->cfg.msi;
1964
1965         /* Write data and address values. */
1966         pci_write_config(child, msi->msi_location + PCIR_MSI_ADDR,
1967             address & 0xffffffff, 4);
1968         if (msi->msi_ctrl & PCIM_MSICTRL_64BIT) {
1969                 pci_write_config(child, msi->msi_location + PCIR_MSI_ADDR_HIGH,
1970                     address >> 32, 4);
1971                 pci_write_config(child, msi->msi_location + PCIR_MSI_DATA_64BIT,
1972                     data, 2);
1973         } else
1974                 pci_write_config(child, msi->msi_location + PCIR_MSI_DATA, data,
1975                     2);
1976
1977         /* Enable MSI in the control register. */
1978         msi->msi_ctrl |= PCIM_MSICTRL_MSI_ENABLE;
1979         pci_write_config(child, msi->msi_location + PCIR_MSI_CTRL,
1980             msi->msi_ctrl, 2);
1981
1982         /* Enable MSI -> HT mapping. */
1983         pci_ht_map_msi(child, address);
1984 }
1985
1986 void
1987 pci_disable_msi_method(device_t dev, device_t child)
1988 {
1989         struct pci_devinfo *dinfo = device_get_ivars(child);
1990         struct pcicfg_msi *msi = &dinfo->cfg.msi;
1991
1992         /* Disable MSI -> HT mapping. */
1993         pci_ht_map_msi(child, 0);
1994
1995         /* Disable MSI in the control register. */
1996         msi->msi_ctrl &= ~PCIM_MSICTRL_MSI_ENABLE;
1997         pci_write_config(child, msi->msi_location + PCIR_MSI_CTRL,
1998             msi->msi_ctrl, 2);
1999 }
2000
2001 /*
2002  * Restore MSI registers during resume.  If MSI is enabled then
2003  * restore the data and address registers in addition to the control
2004  * register.
2005  */
2006 static void
2007 pci_resume_msi(device_t dev)
2008 {
2009         struct pci_devinfo *dinfo = device_get_ivars(dev);
2010         struct pcicfg_msi *msi = &dinfo->cfg.msi;
2011         uint64_t address;
2012         uint16_t data;
2013
2014         if (msi->msi_ctrl & PCIM_MSICTRL_MSI_ENABLE) {
2015                 address = msi->msi_addr;
2016                 data = msi->msi_data;
2017                 pci_write_config(dev, msi->msi_location + PCIR_MSI_ADDR,
2018                     address & 0xffffffff, 4);
2019                 if (msi->msi_ctrl & PCIM_MSICTRL_64BIT) {
2020                         pci_write_config(dev, msi->msi_location +
2021                             PCIR_MSI_ADDR_HIGH, address >> 32, 4);
2022                         pci_write_config(dev, msi->msi_location +
2023                             PCIR_MSI_DATA_64BIT, data, 2);
2024                 } else
2025                         pci_write_config(dev, msi->msi_location + PCIR_MSI_DATA,
2026                             data, 2);
2027         }
2028         pci_write_config(dev, msi->msi_location + PCIR_MSI_CTRL, msi->msi_ctrl,
2029             2);
2030 }
2031
2032 static int
2033 pci_remap_intr_method(device_t bus, device_t dev, u_int irq)
2034 {
2035         struct pci_devinfo *dinfo = device_get_ivars(dev);
2036         pcicfgregs *cfg = &dinfo->cfg;
2037         struct resource_list_entry *rle;
2038         struct msix_table_entry *mte;
2039         struct msix_vector *mv;
2040         uint64_t addr;
2041         uint32_t data;  
2042         int error, i, j;
2043
2044         /*
2045          * Handle MSI first.  We try to find this IRQ among our list
2046          * of MSI IRQs.  If we find it, we request updated address and
2047          * data registers and apply the results.
2048          */
2049         if (cfg->msi.msi_alloc > 0) {
2050
2051                 /* If we don't have any active handlers, nothing to do. */
2052                 if (cfg->msi.msi_handlers == 0)
2053                         return (0);
2054                 for (i = 0; i < cfg->msi.msi_alloc; i++) {
2055                         rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ,
2056                             i + 1);
2057                         if (rle->start == irq) {
2058                                 error = PCIB_MAP_MSI(device_get_parent(bus),
2059                                     dev, irq, &addr, &data);
2060                                 if (error)
2061                                         return (error);
2062                                 pci_disable_msi(dev);
2063                                 dinfo->cfg.msi.msi_addr = addr;
2064                                 dinfo->cfg.msi.msi_data = data;
2065                                 pci_enable_msi(dev, addr, data);
2066                                 return (0);
2067                         }
2068                 }
2069                 return (ENOENT);
2070         }
2071
2072         /*
2073          * For MSI-X, we check to see if we have this IRQ.  If we do,
2074          * we request the updated mapping info.  If that works, we go
2075          * through all the slots that use this IRQ and update them.
2076          */
2077         if (cfg->msix.msix_alloc > 0) {
2078                 for (i = 0; i < cfg->msix.msix_alloc; i++) {
2079                         mv = &cfg->msix.msix_vectors[i];
2080                         if (mv->mv_irq == irq) {
2081                                 error = PCIB_MAP_MSI(device_get_parent(bus),
2082                                     dev, irq, &addr, &data);
2083                                 if (error)
2084                                         return (error);
2085                                 mv->mv_address = addr;
2086                                 mv->mv_data = data;
2087                                 for (j = 0; j < cfg->msix.msix_table_len; j++) {
2088                                         mte = &cfg->msix.msix_table[j];
2089                                         if (mte->mte_vector != i + 1)
2090                                                 continue;
2091                                         if (mte->mte_handlers == 0)
2092                                                 continue;
2093                                         pci_mask_msix(dev, j);
2094                                         pci_enable_msix(dev, j, addr, data);
2095                                         pci_unmask_msix(dev, j);
2096                                 }
2097                         }
2098                 }
2099                 return (ENOENT);
2100         }
2101
2102         return (ENOENT);
2103 }
2104
2105 /*
2106  * Returns true if the specified device is blacklisted because MSI
2107  * doesn't work.
2108  */
2109 int
2110 pci_msi_device_blacklisted(device_t dev)
2111 {
2112
2113         if (!pci_honor_msi_blacklist)
2114                 return (0);
2115
2116         return (pci_has_quirk(pci_get_devid(dev), PCI_QUIRK_DISABLE_MSI));
2117 }
2118
2119 /*
2120  * Determine if MSI is blacklisted globally on this system.  Currently,
2121  * we just check for blacklisted chipsets as represented by the
2122  * host-PCI bridge at device 0:0:0.  In the future, it may become
2123  * necessary to check other system attributes, such as the kenv values
2124  * that give the motherboard manufacturer and model number.
2125  */
2126 static int
2127 pci_msi_blacklisted(void)
2128 {
2129         device_t dev;
2130
2131         if (!pci_honor_msi_blacklist)
2132                 return (0);
2133
2134         /* Blacklist all non-PCI-express and non-PCI-X chipsets. */
2135         if (!(pcie_chipset || pcix_chipset)) {
2136                 if (vm_guest != VM_GUEST_NO) {
2137                         /*
2138                          * Whitelist older chipsets in virtual
2139                          * machines known to support MSI.
2140                          */
2141                         dev = pci_find_bsf(0, 0, 0);
2142                         if (dev != NULL)
2143                                 return (!pci_has_quirk(pci_get_devid(dev),
2144                                         PCI_QUIRK_ENABLE_MSI_VM));
2145                 }
2146                 return (1);
2147         }
2148
2149         dev = pci_find_bsf(0, 0, 0);
2150         if (dev != NULL)
2151                 return (pci_msi_device_blacklisted(dev));
2152         return (0);
2153 }
2154
2155 /*
2156  * Returns true if the specified device is blacklisted because MSI-X
2157  * doesn't work.  Note that this assumes that if MSI doesn't work,
2158  * MSI-X doesn't either.
2159  */
2160 int
2161 pci_msix_device_blacklisted(device_t dev)
2162 {
2163
2164         if (!pci_honor_msi_blacklist)
2165                 return (0);
2166
2167         if (pci_has_quirk(pci_get_devid(dev), PCI_QUIRK_DISABLE_MSIX))
2168                 return (1);
2169
2170         return (pci_msi_device_blacklisted(dev));
2171 }
2172
2173 /*
2174  * Determine if MSI-X is blacklisted globally on this system.  If MSI
2175  * is blacklisted, assume that MSI-X is as well.  Check for additional
2176  * chipsets where MSI works but MSI-X does not.
2177  */
2178 static int
2179 pci_msix_blacklisted(void)
2180 {
2181         device_t dev;
2182
2183         if (!pci_honor_msi_blacklist)
2184                 return (0);
2185
2186         dev = pci_find_bsf(0, 0, 0);
2187         if (dev != NULL && pci_has_quirk(pci_get_devid(dev),
2188             PCI_QUIRK_DISABLE_MSIX))
2189                 return (1);
2190
2191         return (pci_msi_blacklisted());
2192 }
2193
2194 /*
2195  * Attempt to allocate *count MSI messages.  The actual number allocated is
2196  * returned in *count.  After this function returns, each message will be
2197  * available to the driver as SYS_RES_IRQ resources starting at a rid 1.
2198  */
2199 int
2200 pci_alloc_msi_method(device_t dev, device_t child, int *count)
2201 {
2202         struct pci_devinfo *dinfo = device_get_ivars(child);
2203         pcicfgregs *cfg = &dinfo->cfg;
2204         struct resource_list_entry *rle;
2205         int actual, error, i, irqs[32];
2206         uint16_t ctrl;
2207
2208         /* Don't let count == 0 get us into trouble. */
2209         if (*count == 0)
2210                 return (EINVAL);
2211
2212         /* If rid 0 is allocated, then fail. */
2213         rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, 0);
2214         if (rle != NULL && rle->res != NULL)
2215                 return (ENXIO);
2216
2217         /* Already have allocated messages? */
2218         if (cfg->msi.msi_alloc != 0 || cfg->msix.msix_alloc != 0)
2219                 return (ENXIO);
2220
2221         /* If MSI is blacklisted for this system, fail. */
2222         if (pci_msi_blacklisted())
2223                 return (ENXIO);
2224
2225         /* MSI capability present? */
2226         if (cfg->msi.msi_location == 0 || !pci_do_msi)
2227                 return (ENODEV);
2228
2229         if (bootverbose)
2230                 device_printf(child,
2231                     "attempting to allocate %d MSI vectors (%d supported)\n",
2232                     *count, cfg->msi.msi_msgnum);
2233
2234         /* Don't ask for more than the device supports. */
2235         actual = min(*count, cfg->msi.msi_msgnum);
2236
2237         /* Don't ask for more than 32 messages. */
2238         actual = min(actual, 32);
2239
2240         /* MSI requires power of 2 number of messages. */
2241         if (!powerof2(actual))
2242                 return (EINVAL);
2243
2244         for (;;) {
2245                 /* Try to allocate N messages. */
2246                 error = PCIB_ALLOC_MSI(device_get_parent(dev), child, actual,
2247                     actual, irqs);
2248                 if (error == 0)
2249                         break;
2250                 if (actual == 1)
2251                         return (error);
2252
2253                 /* Try N / 2. */
2254                 actual >>= 1;
2255         }
2256
2257         /*
2258          * We now have N actual messages mapped onto SYS_RES_IRQ
2259          * resources in the irqs[] array, so add new resources
2260          * starting at rid 1.
2261          */
2262         for (i = 0; i < actual; i++)
2263                 resource_list_add(&dinfo->resources, SYS_RES_IRQ, i + 1,
2264                     irqs[i], irqs[i], 1);
2265
2266         if (bootverbose) {
2267                 if (actual == 1)
2268                         device_printf(child, "using IRQ %d for MSI\n", irqs[0]);
2269                 else {
2270                         int run;
2271
2272                         /*
2273                          * Be fancy and try to print contiguous runs
2274                          * of IRQ values as ranges.  'run' is true if
2275                          * we are in a range.
2276                          */
2277                         device_printf(child, "using IRQs %d", irqs[0]);
2278                         run = 0;
2279                         for (i = 1; i < actual; i++) {
2280
2281                                 /* Still in a run? */
2282                                 if (irqs[i] == irqs[i - 1] + 1) {
2283                                         run = 1;
2284                                         continue;
2285                                 }
2286
2287                                 /* Finish previous range. */
2288                                 if (run) {
2289                                         printf("-%d", irqs[i - 1]);
2290                                         run = 0;
2291                                 }
2292
2293                                 /* Start new range. */
2294                                 printf(",%d", irqs[i]);
2295                         }
2296
2297                         /* Unfinished range? */
2298                         if (run)
2299                                 printf("-%d", irqs[actual - 1]);
2300                         printf(" for MSI\n");
2301                 }
2302         }
2303
2304         /* Update control register with actual count. */
2305         ctrl = cfg->msi.msi_ctrl;
2306         ctrl &= ~PCIM_MSICTRL_MME_MASK;
2307         ctrl |= (ffs(actual) - 1) << 4;
2308         cfg->msi.msi_ctrl = ctrl;
2309         pci_write_config(child, cfg->msi.msi_location + PCIR_MSI_CTRL, ctrl, 2);
2310
2311         /* Update counts of alloc'd messages. */
2312         cfg->msi.msi_alloc = actual;
2313         cfg->msi.msi_handlers = 0;
2314         *count = actual;
2315         return (0);
2316 }
2317
2318 /* Release the MSI messages associated with this device. */
2319 int
2320 pci_release_msi_method(device_t dev, device_t child)
2321 {
2322         struct pci_devinfo *dinfo = device_get_ivars(child);
2323         struct pcicfg_msi *msi = &dinfo->cfg.msi;
2324         struct resource_list_entry *rle;
2325         int error, i, irqs[32];
2326
2327         /* Try MSI-X first. */
2328         error = pci_release_msix(dev, child);
2329         if (error != ENODEV)
2330                 return (error);
2331
2332         /* Do we have any messages to release? */
2333         if (msi->msi_alloc == 0)
2334                 return (ENODEV);
2335         KASSERT(msi->msi_alloc <= 32, ("more than 32 alloc'd messages"));
2336
2337         /* Make sure none of the resources are allocated. */
2338         if (msi->msi_handlers > 0)
2339                 return (EBUSY);
2340         for (i = 0; i < msi->msi_alloc; i++) {
2341                 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, i + 1);
2342                 KASSERT(rle != NULL, ("missing MSI resource"));
2343                 if (rle->res != NULL)
2344                         return (EBUSY);
2345                 irqs[i] = rle->start;
2346         }
2347
2348         /* Update control register with 0 count. */
2349         KASSERT(!(msi->msi_ctrl & PCIM_MSICTRL_MSI_ENABLE),
2350             ("%s: MSI still enabled", __func__));
2351         msi->msi_ctrl &= ~PCIM_MSICTRL_MME_MASK;
2352         pci_write_config(child, msi->msi_location + PCIR_MSI_CTRL,
2353             msi->msi_ctrl, 2);
2354
2355         /* Release the messages. */
2356         PCIB_RELEASE_MSI(device_get_parent(dev), child, msi->msi_alloc, irqs);
2357         for (i = 0; i < msi->msi_alloc; i++)
2358                 resource_list_delete(&dinfo->resources, SYS_RES_IRQ, i + 1);
2359
2360         /* Update alloc count. */
2361         msi->msi_alloc = 0;
2362         msi->msi_addr = 0;
2363         msi->msi_data = 0;
2364         return (0);
2365 }
2366
2367 /*
2368  * Return the max supported MSI messages this device supports.
2369  * Basically, assuming the MD code can alloc messages, this function
2370  * should return the maximum value that pci_alloc_msi() can return.
2371  * Thus, it is subject to the tunables, etc.
2372  */
2373 int
2374 pci_msi_count_method(device_t dev, device_t child)
2375 {
2376         struct pci_devinfo *dinfo = device_get_ivars(child);
2377         struct pcicfg_msi *msi = &dinfo->cfg.msi;
2378
2379         if (pci_do_msi && msi->msi_location != 0)
2380                 return (msi->msi_msgnum);
2381         return (0);
2382 }
2383
2384 /* free pcicfgregs structure and all depending data structures */
2385
2386 int
2387 pci_freecfg(struct pci_devinfo *dinfo)
2388 {
2389         struct devlist *devlist_head;
2390         struct pci_map *pm, *next;
2391         int i;
2392
2393         devlist_head = &pci_devq;
2394
2395         if (dinfo->cfg.vpd.vpd_reg) {
2396                 free(dinfo->cfg.vpd.vpd_ident, M_DEVBUF);
2397                 for (i = 0; i < dinfo->cfg.vpd.vpd_rocnt; i++)
2398                         free(dinfo->cfg.vpd.vpd_ros[i].value, M_DEVBUF);
2399                 free(dinfo->cfg.vpd.vpd_ros, M_DEVBUF);
2400                 for (i = 0; i < dinfo->cfg.vpd.vpd_wcnt; i++)
2401                         free(dinfo->cfg.vpd.vpd_w[i].value, M_DEVBUF);
2402                 free(dinfo->cfg.vpd.vpd_w, M_DEVBUF);
2403         }
2404         STAILQ_FOREACH_SAFE(pm, &dinfo->cfg.maps, pm_link, next) {
2405                 free(pm, M_DEVBUF);
2406         }
2407         STAILQ_REMOVE(devlist_head, dinfo, pci_devinfo, pci_links);
2408         free(dinfo, M_DEVBUF);
2409
2410         /* increment the generation count */
2411         pci_generation++;
2412
2413         /* we're losing one device */
2414         pci_numdevs--;
2415         return (0);
2416 }
2417
2418 /*
2419  * PCI power manangement
2420  */
2421 int
2422 pci_set_powerstate_method(device_t dev, device_t child, int state)
2423 {
2424         struct pci_devinfo *dinfo = device_get_ivars(child);
2425         pcicfgregs *cfg = &dinfo->cfg;
2426         uint16_t status;
2427         int oldstate, highest, delay;
2428
2429         if (cfg->pp.pp_cap == 0)
2430                 return (EOPNOTSUPP);
2431
2432         /*
2433          * Optimize a no state change request away.  While it would be OK to
2434          * write to the hardware in theory, some devices have shown odd
2435          * behavior when going from D3 -> D3.
2436          */
2437         oldstate = pci_get_powerstate(child);
2438         if (oldstate == state)
2439                 return (0);
2440
2441         /*
2442          * The PCI power management specification states that after a state
2443          * transition between PCI power states, system software must
2444          * guarantee a minimal delay before the function accesses the device.
2445          * Compute the worst case delay that we need to guarantee before we
2446          * access the device.  Many devices will be responsive much more
2447          * quickly than this delay, but there are some that don't respond
2448          * instantly to state changes.  Transitions to/from D3 state require
2449          * 10ms, while D2 requires 200us, and D0/1 require none.  The delay
2450          * is done below with DELAY rather than a sleeper function because
2451          * this function can be called from contexts where we cannot sleep.
2452          */
2453         highest = (oldstate > state) ? oldstate : state;
2454         if (highest == PCI_POWERSTATE_D3)
2455             delay = 10000;
2456         else if (highest == PCI_POWERSTATE_D2)
2457             delay = 200;
2458         else
2459             delay = 0;
2460         status = PCI_READ_CONFIG(dev, child, cfg->pp.pp_status, 2)
2461             & ~PCIM_PSTAT_DMASK;
2462         switch (state) {
2463         case PCI_POWERSTATE_D0:
2464                 status |= PCIM_PSTAT_D0;
2465                 break;
2466         case PCI_POWERSTATE_D1:
2467                 if ((cfg->pp.pp_cap & PCIM_PCAP_D1SUPP) == 0)
2468                         return (EOPNOTSUPP);
2469                 status |= PCIM_PSTAT_D1;
2470                 break;
2471         case PCI_POWERSTATE_D2:
2472                 if ((cfg->pp.pp_cap & PCIM_PCAP_D2SUPP) == 0)
2473                         return (EOPNOTSUPP);
2474                 status |= PCIM_PSTAT_D2;
2475                 break;
2476         case PCI_POWERSTATE_D3:
2477                 status |= PCIM_PSTAT_D3;
2478                 break;
2479         default:
2480                 return (EINVAL);
2481         }
2482
2483         if (bootverbose)
2484                 pci_printf(cfg, "Transition from D%d to D%d\n", oldstate,
2485                     state);
2486
2487         PCI_WRITE_CONFIG(dev, child, cfg->pp.pp_status, status, 2);
2488         if (delay)
2489                 DELAY(delay);
2490         return (0);
2491 }
2492
2493 int
2494 pci_get_powerstate_method(device_t dev, device_t child)
2495 {
2496         struct pci_devinfo *dinfo = device_get_ivars(child);
2497         pcicfgregs *cfg = &dinfo->cfg;
2498         uint16_t status;
2499         int result;
2500
2501         if (cfg->pp.pp_cap != 0) {
2502                 status = PCI_READ_CONFIG(dev, child, cfg->pp.pp_status, 2);
2503                 switch (status & PCIM_PSTAT_DMASK) {
2504                 case PCIM_PSTAT_D0:
2505                         result = PCI_POWERSTATE_D0;
2506                         break;
2507                 case PCIM_PSTAT_D1:
2508                         result = PCI_POWERSTATE_D1;
2509                         break;
2510                 case PCIM_PSTAT_D2:
2511                         result = PCI_POWERSTATE_D2;
2512                         break;
2513                 case PCIM_PSTAT_D3:
2514                         result = PCI_POWERSTATE_D3;
2515                         break;
2516                 default:
2517                         result = PCI_POWERSTATE_UNKNOWN;
2518                         break;
2519                 }
2520         } else {
2521                 /* No support, device is always at D0 */
2522                 result = PCI_POWERSTATE_D0;
2523         }
2524         return (result);
2525 }
2526
2527 /*
2528  * Some convenience functions for PCI device drivers.
2529  */
2530
2531 static __inline void
2532 pci_set_command_bit(device_t dev, device_t child, uint16_t bit)
2533 {
2534         uint16_t        command;
2535
2536         command = PCI_READ_CONFIG(dev, child, PCIR_COMMAND, 2);
2537         command |= bit;
2538         PCI_WRITE_CONFIG(dev, child, PCIR_COMMAND, command, 2);
2539 }
2540
2541 static __inline void
2542 pci_clear_command_bit(device_t dev, device_t child, uint16_t bit)
2543 {
2544         uint16_t        command;
2545
2546         command = PCI_READ_CONFIG(dev, child, PCIR_COMMAND, 2);
2547         command &= ~bit;
2548         PCI_WRITE_CONFIG(dev, child, PCIR_COMMAND, command, 2);
2549 }
2550
2551 int
2552 pci_enable_busmaster_method(device_t dev, device_t child)
2553 {
2554         pci_set_command_bit(dev, child, PCIM_CMD_BUSMASTEREN);
2555         return (0);
2556 }
2557
2558 int
2559 pci_disable_busmaster_method(device_t dev, device_t child)
2560 {
2561         pci_clear_command_bit(dev, child, PCIM_CMD_BUSMASTEREN);
2562         return (0);
2563 }
2564
2565 int
2566 pci_enable_io_method(device_t dev, device_t child, int space)
2567 {
2568         uint16_t bit;
2569
2570         switch(space) {
2571         case SYS_RES_IOPORT:
2572                 bit = PCIM_CMD_PORTEN;
2573                 break;
2574         case SYS_RES_MEMORY:
2575                 bit = PCIM_CMD_MEMEN;
2576                 break;
2577         default:
2578                 return (EINVAL);
2579         }
2580         pci_set_command_bit(dev, child, bit);
2581         return (0);
2582 }
2583
2584 int
2585 pci_disable_io_method(device_t dev, device_t child, int space)
2586 {
2587         uint16_t bit;
2588
2589         switch(space) {
2590         case SYS_RES_IOPORT:
2591                 bit = PCIM_CMD_PORTEN;
2592                 break;
2593         case SYS_RES_MEMORY:
2594                 bit = PCIM_CMD_MEMEN;
2595                 break;
2596         default:
2597                 return (EINVAL);
2598         }
2599         pci_clear_command_bit(dev, child, bit);
2600         return (0);
2601 }
2602
2603 /*
2604  * New style pci driver.  Parent device is either a pci-host-bridge or a
2605  * pci-pci-bridge.  Both kinds are represented by instances of pcib.
2606  */
2607
2608 void
2609 pci_print_verbose(struct pci_devinfo *dinfo)
2610 {
2611
2612         if (bootverbose) {
2613                 pcicfgregs *cfg = &dinfo->cfg;
2614
2615                 printf("found->\tvendor=0x%04x, dev=0x%04x, revid=0x%02x\n",
2616                     cfg->vendor, cfg->device, cfg->revid);
2617                 printf("\tdomain=%d, bus=%d, slot=%d, func=%d\n",
2618                     cfg->domain, cfg->bus, cfg->slot, cfg->func);
2619                 printf("\tclass=%02x-%02x-%02x, hdrtype=0x%02x, mfdev=%d\n",
2620                     cfg->baseclass, cfg->subclass, cfg->progif, cfg->hdrtype,
2621                     cfg->mfdev);
2622                 printf("\tcmdreg=0x%04x, statreg=0x%04x, cachelnsz=%d (dwords)\n",
2623                     cfg->cmdreg, cfg->statreg, cfg->cachelnsz);
2624                 printf("\tlattimer=0x%02x (%d ns), mingnt=0x%02x (%d ns), maxlat=0x%02x (%d ns)\n",
2625                     cfg->lattimer, cfg->lattimer * 30, cfg->mingnt,
2626                     cfg->mingnt * 250, cfg->maxlat, cfg->maxlat * 250);
2627                 if (cfg->intpin > 0)
2628                         printf("\tintpin=%c, irq=%d\n",
2629                             cfg->intpin +'a' -1, cfg->intline);
2630                 if (cfg->pp.pp_cap) {
2631                         uint16_t status;
2632
2633                         status = pci_read_config(cfg->dev, cfg->pp.pp_status, 2);
2634                         printf("\tpowerspec %d  supports D0%s%s D3  current D%d\n",
2635                             cfg->pp.pp_cap & PCIM_PCAP_SPEC,
2636                             cfg->pp.pp_cap & PCIM_PCAP_D1SUPP ? " D1" : "",
2637                             cfg->pp.pp_cap & PCIM_PCAP_D2SUPP ? " D2" : "",
2638                             status & PCIM_PSTAT_DMASK);
2639                 }
2640                 if (cfg->msi.msi_location) {
2641                         int ctrl;
2642
2643                         ctrl = cfg->msi.msi_ctrl;
2644                         printf("\tMSI supports %d message%s%s%s\n",
2645                             cfg->msi.msi_msgnum,
2646                             (cfg->msi.msi_msgnum == 1) ? "" : "s",
2647                             (ctrl & PCIM_MSICTRL_64BIT) ? ", 64 bit" : "",
2648                             (ctrl & PCIM_MSICTRL_VECTOR) ? ", vector masks":"");
2649                 }
2650                 if (cfg->msix.msix_location) {
2651                         printf("\tMSI-X supports %d message%s ",
2652                             cfg->msix.msix_msgnum,
2653                             (cfg->msix.msix_msgnum == 1) ? "" : "s");
2654                         if (cfg->msix.msix_table_bar == cfg->msix.msix_pba_bar)
2655                                 printf("in map 0x%x\n",
2656                                     cfg->msix.msix_table_bar);
2657                         else
2658                                 printf("in maps 0x%x and 0x%x\n",
2659                                     cfg->msix.msix_table_bar,
2660                                     cfg->msix.msix_pba_bar);
2661                 }
2662         }
2663 }
2664
2665 static int
2666 pci_porten(device_t dev)
2667 {
2668         return (pci_read_config(dev, PCIR_COMMAND, 2) & PCIM_CMD_PORTEN) != 0;
2669 }
2670
2671 static int
2672 pci_memen(device_t dev)
2673 {
2674         return (pci_read_config(dev, PCIR_COMMAND, 2) & PCIM_CMD_MEMEN) != 0;
2675 }
2676
2677 static void
2678 pci_read_bar(device_t dev, int reg, pci_addr_t *mapp, pci_addr_t *testvalp)
2679 {
2680         struct pci_devinfo *dinfo;
2681         pci_addr_t map, testval;
2682         int ln2range;
2683         uint16_t cmd;
2684
2685         /*
2686          * The device ROM BAR is special.  It is always a 32-bit
2687          * memory BAR.  Bit 0 is special and should not be set when
2688          * sizing the BAR.
2689          */
2690         dinfo = device_get_ivars(dev);
2691         if (PCIR_IS_BIOS(&dinfo->cfg, reg)) {
2692                 map = pci_read_config(dev, reg, 4);
2693                 pci_write_config(dev, reg, 0xfffffffe, 4);
2694                 testval = pci_read_config(dev, reg, 4);
2695                 pci_write_config(dev, reg, map, 4);
2696                 *mapp = map;
2697                 *testvalp = testval;
2698                 return;
2699         }
2700
2701         map = pci_read_config(dev, reg, 4);
2702         ln2range = pci_maprange(map);
2703         if (ln2range == 64)
2704                 map |= (pci_addr_t)pci_read_config(dev, reg + 4, 4) << 32;
2705
2706         /*
2707          * Disable decoding via the command register before
2708          * determining the BAR's length since we will be placing it in
2709          * a weird state.
2710          */
2711         cmd = pci_read_config(dev, PCIR_COMMAND, 2);
2712         pci_write_config(dev, PCIR_COMMAND,
2713             cmd & ~(PCI_BAR_MEM(map) ? PCIM_CMD_MEMEN : PCIM_CMD_PORTEN), 2);
2714
2715         /*
2716          * Determine the BAR's length by writing all 1's.  The bottom
2717          * log_2(size) bits of the BAR will stick as 0 when we read
2718          * the value back.
2719          */
2720         pci_write_config(dev, reg, 0xffffffff, 4);
2721         testval = pci_read_config(dev, reg, 4);
2722         if (ln2range == 64) {
2723                 pci_write_config(dev, reg + 4, 0xffffffff, 4);
2724                 testval |= (pci_addr_t)pci_read_config(dev, reg + 4, 4) << 32;
2725         }
2726
2727         /*
2728          * Restore the original value of the BAR.  We may have reprogrammed
2729          * the BAR of the low-level console device and when booting verbose,
2730          * we need the console device addressable.
2731          */
2732         pci_write_config(dev, reg, map, 4);
2733         if (ln2range == 64)
2734                 pci_write_config(dev, reg + 4, map >> 32, 4);
2735         pci_write_config(dev, PCIR_COMMAND, cmd, 2);
2736
2737         *mapp = map;
2738         *testvalp = testval;
2739 }
2740
2741 static void
2742 pci_write_bar(device_t dev, struct pci_map *pm, pci_addr_t base)
2743 {
2744         struct pci_devinfo *dinfo;
2745         int ln2range;
2746
2747         /* The device ROM BAR is always a 32-bit memory BAR. */
2748         dinfo = device_get_ivars(dev);
2749         if (PCIR_IS_BIOS(&dinfo->cfg, pm->pm_reg))
2750                 ln2range = 32;
2751         else
2752                 ln2range = pci_maprange(pm->pm_value);
2753         pci_write_config(dev, pm->pm_reg, base, 4);
2754         if (ln2range == 64)
2755                 pci_write_config(dev, pm->pm_reg + 4, base >> 32, 4);
2756         pm->pm_value = pci_read_config(dev, pm->pm_reg, 4);
2757         if (ln2range == 64)
2758                 pm->pm_value |= (pci_addr_t)pci_read_config(dev,
2759                     pm->pm_reg + 4, 4) << 32;
2760 }
2761
2762 struct pci_map *
2763 pci_find_bar(device_t dev, int reg)
2764 {
2765         struct pci_devinfo *dinfo;
2766         struct pci_map *pm;
2767
2768         dinfo = device_get_ivars(dev);
2769         STAILQ_FOREACH(pm, &dinfo->cfg.maps, pm_link) {
2770                 if (pm->pm_reg == reg)
2771                         return (pm);
2772         }
2773         return (NULL);
2774 }
2775
2776 int
2777 pci_bar_enabled(device_t dev, struct pci_map *pm)
2778 {
2779         struct pci_devinfo *dinfo;
2780         uint16_t cmd;
2781
2782         dinfo = device_get_ivars(dev);
2783         if (PCIR_IS_BIOS(&dinfo->cfg, pm->pm_reg) &&
2784             !(pm->pm_value & PCIM_BIOS_ENABLE))
2785                 return (0);
2786         cmd = pci_read_config(dev, PCIR_COMMAND, 2);
2787         if (PCIR_IS_BIOS(&dinfo->cfg, pm->pm_reg) || PCI_BAR_MEM(pm->pm_value))
2788                 return ((cmd & PCIM_CMD_MEMEN) != 0);
2789         else
2790                 return ((cmd & PCIM_CMD_PORTEN) != 0);
2791 }
2792
2793 static struct pci_map *
2794 pci_add_bar(device_t dev, int reg, pci_addr_t value, pci_addr_t size)
2795 {
2796         struct pci_devinfo *dinfo;
2797         struct pci_map *pm, *prev;
2798
2799         dinfo = device_get_ivars(dev);
2800         pm = malloc(sizeof(*pm), M_DEVBUF, M_WAITOK | M_ZERO);
2801         pm->pm_reg = reg;
2802         pm->pm_value = value;
2803         pm->pm_size = size;
2804         STAILQ_FOREACH(prev, &dinfo->cfg.maps, pm_link) {
2805                 KASSERT(prev->pm_reg != pm->pm_reg, ("duplicate map %02x",
2806                     reg));
2807                 if (STAILQ_NEXT(prev, pm_link) == NULL ||
2808                     STAILQ_NEXT(prev, pm_link)->pm_reg > pm->pm_reg)
2809                         break;
2810         }
2811         if (prev != NULL)
2812                 STAILQ_INSERT_AFTER(&dinfo->cfg.maps, prev, pm, pm_link);
2813         else
2814                 STAILQ_INSERT_TAIL(&dinfo->cfg.maps, pm, pm_link);
2815         return (pm);
2816 }
2817
2818 static void
2819 pci_restore_bars(device_t dev)
2820 {
2821         struct pci_devinfo *dinfo;
2822         struct pci_map *pm;
2823         int ln2range;
2824
2825         dinfo = device_get_ivars(dev);
2826         STAILQ_FOREACH(pm, &dinfo->cfg.maps, pm_link) {
2827                 if (PCIR_IS_BIOS(&dinfo->cfg, pm->pm_reg))
2828                         ln2range = 32;
2829                 else
2830                         ln2range = pci_maprange(pm->pm_value);
2831                 pci_write_config(dev, pm->pm_reg, pm->pm_value, 4);
2832                 if (ln2range == 64)
2833                         pci_write_config(dev, pm->pm_reg + 4,
2834                             pm->pm_value >> 32, 4);
2835         }
2836 }
2837
2838 /*
2839  * Add a resource based on a pci map register. Return 1 if the map
2840  * register is a 32bit map register or 2 if it is a 64bit register.
2841  */
2842 static int
2843 pci_add_map(device_t bus, device_t dev, int reg, struct resource_list *rl,
2844     int force, int prefetch)
2845 {
2846         struct pci_map *pm;
2847         pci_addr_t base, map, testval;
2848         pci_addr_t start, end, count;
2849         int barlen, basezero, flags, maprange, mapsize, type;
2850         uint16_t cmd;
2851         struct resource *res;
2852
2853         /*
2854          * The BAR may already exist if the device is a CardBus card
2855          * whose CIS is stored in this BAR.
2856          */
2857         pm = pci_find_bar(dev, reg);
2858         if (pm != NULL) {
2859                 maprange = pci_maprange(pm->pm_value);
2860                 barlen = maprange == 64 ? 2 : 1;
2861                 return (barlen);
2862         }
2863
2864         pci_read_bar(dev, reg, &map, &testval);
2865         if (PCI_BAR_MEM(map)) {
2866                 type = SYS_RES_MEMORY;
2867                 if (map & PCIM_BAR_MEM_PREFETCH)
2868                         prefetch = 1;
2869         } else
2870                 type = SYS_RES_IOPORT;
2871         mapsize = pci_mapsize(testval);
2872         base = pci_mapbase(map);
2873 #ifdef __PCI_BAR_ZERO_VALID
2874         basezero = 0;
2875 #else
2876         basezero = base == 0;
2877 #endif
2878         maprange = pci_maprange(map);
2879         barlen = maprange == 64 ? 2 : 1;
2880
2881         /*
2882          * For I/O registers, if bottom bit is set, and the next bit up
2883          * isn't clear, we know we have a BAR that doesn't conform to the
2884          * spec, so ignore it.  Also, sanity check the size of the data
2885          * areas to the type of memory involved.  Memory must be at least
2886          * 16 bytes in size, while I/O ranges must be at least 4.
2887          */
2888         if (PCI_BAR_IO(testval) && (testval & PCIM_BAR_IO_RESERVED) != 0)
2889                 return (barlen);
2890         if ((type == SYS_RES_MEMORY && mapsize < 4) ||
2891             (type == SYS_RES_IOPORT && mapsize < 2))
2892                 return (barlen);
2893
2894         /* Save a record of this BAR. */
2895         pm = pci_add_bar(dev, reg, map, mapsize);
2896         if (bootverbose) {
2897                 printf("\tmap[%02x]: type %s, range %2d, base %#jx, size %2d",
2898                     reg, pci_maptype(map), maprange, (uintmax_t)base, mapsize);
2899                 if (type == SYS_RES_IOPORT && !pci_porten(dev))
2900                         printf(", port disabled\n");
2901                 else if (type == SYS_RES_MEMORY && !pci_memen(dev))
2902                         printf(", memory disabled\n");
2903                 else
2904                         printf(", enabled\n");
2905         }
2906
2907         /*
2908          * If base is 0, then we have problems if this architecture does
2909          * not allow that.  It is best to ignore such entries for the
2910          * moment.  These will be allocated later if the driver specifically
2911          * requests them.  However, some removable busses look better when
2912          * all resources are allocated, so allow '0' to be overriden.
2913          *
2914          * Similarly treat maps whose values is the same as the test value
2915          * read back.  These maps have had all f's written to them by the
2916          * BIOS in an attempt to disable the resources.
2917          */
2918         if (!force && (basezero || map == testval))
2919                 return (barlen);
2920         if ((u_long)base != base) {
2921                 device_printf(bus,
2922                     "pci%d:%d:%d:%d bar %#x too many address bits",
2923                     pci_get_domain(dev), pci_get_bus(dev), pci_get_slot(dev),
2924                     pci_get_function(dev), reg);
2925                 return (barlen);
2926         }
2927
2928         /*
2929          * This code theoretically does the right thing, but has
2930          * undesirable side effects in some cases where peripherals
2931          * respond oddly to having these bits enabled.  Let the user
2932          * be able to turn them off (since pci_enable_io_modes is 1 by
2933          * default).
2934          */
2935         if (pci_enable_io_modes) {
2936                 /* Turn on resources that have been left off by a lazy BIOS */
2937                 if (type == SYS_RES_IOPORT && !pci_porten(dev)) {
2938                         cmd = pci_read_config(dev, PCIR_COMMAND, 2);
2939                         cmd |= PCIM_CMD_PORTEN;
2940                         pci_write_config(dev, PCIR_COMMAND, cmd, 2);
2941                 }
2942                 if (type == SYS_RES_MEMORY && !pci_memen(dev)) {
2943                         cmd = pci_read_config(dev, PCIR_COMMAND, 2);
2944                         cmd |= PCIM_CMD_MEMEN;
2945                         pci_write_config(dev, PCIR_COMMAND, cmd, 2);
2946                 }
2947         } else {
2948                 if (type == SYS_RES_IOPORT && !pci_porten(dev))
2949                         return (barlen);
2950                 if (type == SYS_RES_MEMORY && !pci_memen(dev))
2951                         return (barlen);
2952         }
2953
2954         count = (pci_addr_t)1 << mapsize;
2955         flags = RF_ALIGNMENT_LOG2(mapsize);
2956         if (prefetch)
2957                 flags |= RF_PREFETCHABLE;
2958         if (basezero || base == pci_mapbase(testval) || pci_clear_bars) {
2959                 start = 0;      /* Let the parent decide. */
2960                 end = ~0ul;
2961         } else {
2962                 start = base;
2963                 end = base + count - 1;
2964         }
2965         resource_list_add(rl, type, reg, start, end, count);
2966
2967         /*
2968          * Try to allocate the resource for this BAR from our parent
2969          * so that this resource range is already reserved.  The
2970          * driver for this device will later inherit this resource in
2971          * pci_alloc_resource().
2972          */
2973         res = resource_list_reserve(rl, bus, dev, type, &reg, start, end, count,
2974             flags);
2975         if (pci_do_realloc_bars && res == NULL && (start != 0 || end != ~0ul)) {
2976                 /*
2977                  * If the allocation fails, try to allocate a resource for
2978                  * this BAR using any available range.  The firmware felt
2979                  * it was important enough to assign a resource, so don't
2980                  * disable decoding if we can help it.
2981                  */
2982                 resource_list_delete(rl, type, reg);
2983                 resource_list_add(rl, type, reg, 0, ~0ul, count);
2984                 res = resource_list_reserve(rl, bus, dev, type, &reg, 0, ~0ul,
2985                     count, flags);
2986         }
2987         if (res == NULL) {
2988                 /*
2989                  * If the allocation fails, delete the resource list entry
2990                  * and disable decoding for this device.
2991                  *
2992                  * If the driver requests this resource in the future,
2993                  * pci_reserve_map() will try to allocate a fresh
2994                  * resource range.
2995                  */
2996                 resource_list_delete(rl, type, reg);
2997                 pci_disable_io(dev, type);
2998                 if (bootverbose)
2999                         device_printf(bus,
3000                             "pci%d:%d:%d:%d bar %#x failed to allocate\n",
3001                             pci_get_domain(dev), pci_get_bus(dev),
3002                             pci_get_slot(dev), pci_get_function(dev), reg);
3003         } else {
3004                 start = rman_get_start(res);
3005                 pci_write_bar(dev, pm, start);
3006         }
3007         return (barlen);
3008 }
3009
3010 /*
3011  * For ATA devices we need to decide early what addressing mode to use.
3012  * Legacy demands that the primary and secondary ATA ports sits on the
3013  * same addresses that old ISA hardware did. This dictates that we use
3014  * those addresses and ignore the BAR's if we cannot set PCI native
3015  * addressing mode.
3016  */
3017 static void
3018 pci_ata_maps(device_t bus, device_t dev, struct resource_list *rl, int force,
3019     uint32_t prefetchmask)
3020 {
3021         int rid, type, progif;
3022 #if 0
3023         /* if this device supports PCI native addressing use it */
3024         progif = pci_read_config(dev, PCIR_PROGIF, 1);
3025         if ((progif & 0x8a) == 0x8a) {
3026                 if (pci_mapbase(pci_read_config(dev, PCIR_BAR(0), 4)) &&
3027                     pci_mapbase(pci_read_config(dev, PCIR_BAR(2), 4))) {
3028                         printf("Trying ATA native PCI addressing mode\n");
3029                         pci_write_config(dev, PCIR_PROGIF, progif | 0x05, 1);
3030                 }
3031         }
3032 #endif
3033         progif = pci_read_config(dev, PCIR_PROGIF, 1);
3034         type = SYS_RES_IOPORT;
3035         if (progif & PCIP_STORAGE_IDE_MODEPRIM) {
3036                 pci_add_map(bus, dev, PCIR_BAR(0), rl, force,
3037                     prefetchmask & (1 << 0));
3038                 pci_add_map(bus, dev, PCIR_BAR(1), rl, force,
3039                     prefetchmask & (1 << 1));
3040         } else {
3041                 rid = PCIR_BAR(0);
3042                 resource_list_add(rl, type, rid, 0x1f0, 0x1f7, 8);
3043                 (void)resource_list_reserve(rl, bus, dev, type, &rid, 0x1f0,
3044                     0x1f7, 8, 0);
3045                 rid = PCIR_BAR(1);
3046                 resource_list_add(rl, type, rid, 0x3f6, 0x3f6, 1);
3047                 (void)resource_list_reserve(rl, bus, dev, type, &rid, 0x3f6,
3048                     0x3f6, 1, 0);
3049         }
3050         if (progif & PCIP_STORAGE_IDE_MODESEC) {
3051                 pci_add_map(bus, dev, PCIR_BAR(2), rl, force,
3052                     prefetchmask & (1 << 2));
3053                 pci_add_map(bus, dev, PCIR_BAR(3), rl, force,
3054                     prefetchmask & (1 << 3));
3055         } else {
3056                 rid = PCIR_BAR(2);
3057                 resource_list_add(rl, type, rid, 0x170, 0x177, 8);
3058                 (void)resource_list_reserve(rl, bus, dev, type, &rid, 0x170,
3059                     0x177, 8, 0);
3060                 rid = PCIR_BAR(3);
3061                 resource_list_add(rl, type, rid, 0x376, 0x376, 1);
3062                 (void)resource_list_reserve(rl, bus, dev, type, &rid, 0x376,
3063                     0x376, 1, 0);
3064         }
3065         pci_add_map(bus, dev, PCIR_BAR(4), rl, force,
3066             prefetchmask & (1 << 4));
3067         pci_add_map(bus, dev, PCIR_BAR(5), rl, force,
3068             prefetchmask & (1 << 5));
3069 }
3070
3071 static void
3072 pci_assign_interrupt(device_t bus, device_t dev, int force_route)
3073 {
3074         struct pci_devinfo *dinfo = device_get_ivars(dev);
3075         pcicfgregs *cfg = &dinfo->cfg;
3076         char tunable_name[64];
3077         int irq;
3078
3079         /* Has to have an intpin to have an interrupt. */
3080         if (cfg->intpin == 0)
3081                 return;
3082
3083         /* Let the user override the IRQ with a tunable. */
3084         irq = PCI_INVALID_IRQ;
3085         snprintf(tunable_name, sizeof(tunable_name),
3086             "hw.pci%d.%d.%d.INT%c.irq",
3087             cfg->domain, cfg->bus, cfg->slot, cfg->intpin + 'A' - 1);
3088         if (TUNABLE_INT_FETCH(tunable_name, &irq) && (irq >= 255 || irq <= 0))
3089                 irq = PCI_INVALID_IRQ;
3090
3091         /*
3092          * If we didn't get an IRQ via the tunable, then we either use the
3093          * IRQ value in the intline register or we ask the bus to route an
3094          * interrupt for us.  If force_route is true, then we only use the
3095          * value in the intline register if the bus was unable to assign an
3096          * IRQ.
3097          */
3098         if (!PCI_INTERRUPT_VALID(irq)) {
3099                 if (!PCI_INTERRUPT_VALID(cfg->intline) || force_route)
3100                         irq = PCI_ASSIGN_INTERRUPT(bus, dev);
3101                 if (!PCI_INTERRUPT_VALID(irq))
3102                         irq = cfg->intline;
3103         }
3104
3105         /* If after all that we don't have an IRQ, just bail. */
3106         if (!PCI_INTERRUPT_VALID(irq))
3107                 return;
3108
3109         /* Update the config register if it changed. */
3110         if (irq != cfg->intline) {
3111                 cfg->intline = irq;
3112                 pci_write_config(dev, PCIR_INTLINE, irq, 1);
3113         }
3114
3115         /* Add this IRQ as rid 0 interrupt resource. */
3116         resource_list_add(&dinfo->resources, SYS_RES_IRQ, 0, irq, irq, 1);
3117 }
3118
3119 /* Perform early OHCI takeover from SMM. */
3120 static void
3121 ohci_early_takeover(device_t self)
3122 {
3123         struct resource *res;
3124         uint32_t ctl;
3125         int rid;
3126         int i;
3127
3128         rid = PCIR_BAR(0);
3129         res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
3130         if (res == NULL)
3131                 return;
3132
3133         ctl = bus_read_4(res, OHCI_CONTROL);
3134         if (ctl & OHCI_IR) {
3135                 if (bootverbose)
3136                         printf("ohci early: "
3137                             "SMM active, request owner change\n");
3138                 bus_write_4(res, OHCI_COMMAND_STATUS, OHCI_OCR);
3139                 for (i = 0; (i < 100) && (ctl & OHCI_IR); i++) {
3140                         DELAY(1000);
3141                         ctl = bus_read_4(res, OHCI_CONTROL);
3142                 }
3143                 if (ctl & OHCI_IR) {
3144                         if (bootverbose)
3145                                 printf("ohci early: "
3146                                     "SMM does not respond, resetting\n");
3147                         bus_write_4(res, OHCI_CONTROL, OHCI_HCFS_RESET);
3148                 }
3149                 /* Disable interrupts */
3150                 bus_write_4(res, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
3151         }
3152
3153         bus_release_resource(self, SYS_RES_MEMORY, rid, res);
3154 }
3155
3156 /* Perform early UHCI takeover from SMM. */
3157 static void
3158 uhci_early_takeover(device_t self)
3159 {
3160         struct resource *res;
3161         int rid;
3162
3163         /*
3164          * Set the PIRQD enable bit and switch off all the others. We don't
3165          * want legacy support to interfere with us XXX Does this also mean
3166          * that the BIOS won't touch the keyboard anymore if it is connected
3167          * to the ports of the root hub?
3168          */
3169         pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2);
3170
3171         /* Disable interrupts */
3172         rid = PCI_UHCI_BASE_REG;
3173         res = bus_alloc_resource_any(self, SYS_RES_IOPORT, &rid, RF_ACTIVE);
3174         if (res != NULL) {
3175                 bus_write_2(res, UHCI_INTR, 0);
3176                 bus_release_resource(self, SYS_RES_IOPORT, rid, res);
3177         }
3178 }
3179
3180 /* Perform early EHCI takeover from SMM. */
3181 static void
3182 ehci_early_takeover(device_t self)
3183 {
3184         struct resource *res;
3185         uint32_t cparams;
3186         uint32_t eec;
3187         uint8_t eecp;
3188         uint8_t bios_sem;
3189         uint8_t offs;
3190         int rid;
3191         int i;
3192
3193         rid = PCIR_BAR(0);
3194         res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
3195         if (res == NULL)
3196                 return;
3197
3198         cparams = bus_read_4(res, EHCI_HCCPARAMS);
3199
3200         /* Synchronise with the BIOS if it owns the controller. */
3201         for (eecp = EHCI_HCC_EECP(cparams); eecp != 0;
3202             eecp = EHCI_EECP_NEXT(eec)) {
3203                 eec = pci_read_config(self, eecp, 4);
3204                 if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) {
3205                         continue;
3206                 }
3207                 bios_sem = pci_read_config(self, eecp +
3208                     EHCI_LEGSUP_BIOS_SEM, 1);
3209                 if (bios_sem == 0) {
3210                         continue;
3211                 }
3212                 if (bootverbose)
3213                         printf("ehci early: "
3214                             "SMM active, request owner change\n");
3215
3216                 pci_write_config(self, eecp + EHCI_LEGSUP_OS_SEM, 1, 1);
3217
3218                 for (i = 0; (i < 100) && (bios_sem != 0); i++) {
3219                         DELAY(1000);
3220                         bios_sem = pci_read_config(self, eecp +
3221                             EHCI_LEGSUP_BIOS_SEM, 1);
3222                 }
3223
3224                 if (bios_sem != 0) {
3225                         if (bootverbose)
3226                                 printf("ehci early: "
3227                                     "SMM does not respond\n");
3228                 }
3229                 /* Disable interrupts */
3230                 offs = EHCI_CAPLENGTH(bus_read_4(res, EHCI_CAPLEN_HCIVERSION));
3231                 bus_write_4(res, offs + EHCI_USBINTR, 0);
3232         }
3233         bus_release_resource(self, SYS_RES_MEMORY, rid, res);
3234 }
3235
3236 /* Perform early XHCI takeover from SMM. */
3237 static void
3238 xhci_early_takeover(device_t self)
3239 {
3240         struct resource *res;
3241         uint32_t cparams;
3242         uint32_t eec;
3243         uint8_t eecp;
3244         uint8_t bios_sem;
3245         uint8_t offs;
3246         int rid;
3247         int i;
3248
3249         rid = PCIR_BAR(0);
3250         res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
3251         if (res == NULL)
3252                 return;
3253
3254         cparams = bus_read_4(res, XHCI_HCSPARAMS0);
3255
3256         eec = -1;
3257
3258         /* Synchronise with the BIOS if it owns the controller. */
3259         for (eecp = XHCI_HCS0_XECP(cparams) << 2; eecp != 0 && XHCI_XECP_NEXT(eec);
3260             eecp += XHCI_XECP_NEXT(eec) << 2) {
3261                 eec = bus_read_4(res, eecp);
3262
3263                 if (XHCI_XECP_ID(eec) != XHCI_ID_USB_LEGACY)
3264                         continue;
3265
3266                 bios_sem = bus_read_1(res, eecp + XHCI_XECP_BIOS_SEM);
3267                 if (bios_sem == 0)
3268                         continue;
3269
3270                 if (bootverbose)
3271                         printf("xhci early: "
3272                             "SMM active, request owner change\n");
3273
3274                 bus_write_1(res, eecp + XHCI_XECP_OS_SEM, 1);
3275
3276                 /* wait a maximum of 5 second */
3277
3278                 for (i = 0; (i < 5000) && (bios_sem != 0); i++) {
3279                         DELAY(1000);
3280                         bios_sem = bus_read_1(res, eecp +
3281                             XHCI_XECP_BIOS_SEM);
3282                 }
3283
3284                 if (bios_sem != 0) {
3285                         if (bootverbose)
3286                                 printf("xhci early: "
3287                                     "SMM does not respond\n");
3288                 }
3289
3290                 /* Disable interrupts */
3291                 offs = bus_read_1(res, XHCI_CAPLENGTH);
3292                 bus_write_4(res, offs + XHCI_USBCMD, 0);
3293                 bus_read_4(res, offs + XHCI_USBSTS);
3294         }
3295         bus_release_resource(self, SYS_RES_MEMORY, rid, res);
3296 }
3297
3298 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
3299 static void
3300 pci_reserve_secbus(device_t bus, device_t dev, pcicfgregs *cfg,
3301     struct resource_list *rl)
3302 {
3303         struct resource *res;
3304         char *cp;
3305         u_long start, end, count;
3306         int rid, sec_bus, sec_reg, sub_bus, sub_reg, sup_bus;
3307
3308         switch (cfg->hdrtype & PCIM_HDRTYPE) {
3309         case PCIM_HDRTYPE_BRIDGE:
3310                 sec_reg = PCIR_SECBUS_1;
3311                 sub_reg = PCIR_SUBBUS_1;
3312                 break;
3313         case PCIM_HDRTYPE_CARDBUS:
3314                 sec_reg = PCIR_SECBUS_2;
3315                 sub_reg = PCIR_SUBBUS_2;
3316                 break;
3317         default:
3318                 return;
3319         }
3320
3321         /*
3322          * If the existing bus range is valid, attempt to reserve it
3323          * from our parent.  If this fails for any reason, clear the
3324          * secbus and subbus registers.
3325          *
3326          * XXX: Should we reset sub_bus to sec_bus if it is < sec_bus?
3327          * This would at least preserve the existing sec_bus if it is
3328          * valid.
3329          */
3330         sec_bus = PCI_READ_CONFIG(bus, dev, sec_reg, 1);
3331         sub_bus = PCI_READ_CONFIG(bus, dev, sub_reg, 1);
3332
3333         /* Quirk handling. */
3334         switch (pci_get_devid(dev)) {
3335         case 0x12258086:                /* Intel 82454KX/GX (Orion) */
3336                 sup_bus = pci_read_config(dev, 0x41, 1);
3337                 if (sup_bus != 0xff) {
3338                         sec_bus = sup_bus + 1;
3339                         sub_bus = sup_bus + 1;
3340                         PCI_WRITE_CONFIG(bus, dev, sec_reg, sec_bus, 1);
3341                         PCI_WRITE_CONFIG(bus, dev, sub_reg, sub_bus, 1);
3342                 }
3343                 break;
3344
3345         case 0x00dd10de:
3346                 /* Compaq R3000 BIOS sets wrong subordinate bus number. */
3347                 if ((cp = getenv("smbios.planar.maker")) == NULL)
3348                         break;
3349                 if (strncmp(cp, "Compal", 6) != 0) {
3350                         freeenv(cp);
3351                         break;
3352                 }
3353                 freeenv(cp);
3354                 if ((cp = getenv("smbios.planar.product")) == NULL)
3355                         break;
3356                 if (strncmp(cp, "08A0", 4) != 0) {
3357                         freeenv(cp);
3358                         break;
3359                 }
3360                 freeenv(cp);
3361                 if (sub_bus < 0xa) {
3362                         sub_bus = 0xa;
3363                         PCI_WRITE_CONFIG(bus, dev, sub_reg, sub_bus, 1);
3364                 }
3365                 break;
3366         }
3367
3368         if (bootverbose)
3369                 printf("\tsecbus=%d, subbus=%d\n", sec_bus, sub_bus);
3370         if (sec_bus > 0 && sub_bus >= sec_bus) {
3371                 start = sec_bus;
3372                 end = sub_bus;
3373                 count = end - start + 1;
3374
3375                 resource_list_add(rl, PCI_RES_BUS, 0, 0ul, ~0ul, count);
3376
3377                 /*
3378                  * If requested, clear secondary bus registers in
3379                  * bridge devices to force a complete renumbering
3380                  * rather than reserving the existing range.  However,
3381                  * preserve the existing size.
3382                  */
3383                 if (pci_clear_buses)
3384                         goto clear;
3385
3386                 rid = 0;
3387                 res = resource_list_reserve(rl, bus, dev, PCI_RES_BUS, &rid,
3388                     start, end, count, 0);
3389                 if (res != NULL)
3390                         return;
3391
3392                 if (bootverbose)
3393                         device_printf(bus,
3394                             "pci%d:%d:%d:%d secbus failed to allocate\n",
3395                             pci_get_domain(dev), pci_get_bus(dev),
3396                             pci_get_slot(dev), pci_get_function(dev));
3397         }
3398
3399 clear:
3400         PCI_WRITE_CONFIG(bus, dev, sec_reg, 0, 1);
3401         PCI_WRITE_CONFIG(bus, dev, sub_reg, 0, 1);
3402 }
3403
3404 static struct resource *
3405 pci_alloc_secbus(device_t dev, device_t child, int *rid, u_long start,
3406     u_long end, u_long count, u_int flags)
3407 {
3408         struct pci_devinfo *dinfo;
3409         pcicfgregs *cfg;
3410         struct resource_list *rl;
3411         struct resource *res;
3412         int sec_reg, sub_reg;
3413
3414         dinfo = device_get_ivars(child);
3415         cfg = &dinfo->cfg;
3416         rl = &dinfo->resources;
3417         switch (cfg->hdrtype & PCIM_HDRTYPE) {
3418         case PCIM_HDRTYPE_BRIDGE:
3419                 sec_reg = PCIR_SECBUS_1;
3420                 sub_reg = PCIR_SUBBUS_1;
3421                 break;
3422         case PCIM_HDRTYPE_CARDBUS:
3423                 sec_reg = PCIR_SECBUS_2;
3424                 sub_reg = PCIR_SUBBUS_2;
3425                 break;
3426         default:
3427                 return (NULL);
3428         }
3429
3430         if (*rid != 0)
3431                 return (NULL);
3432
3433         if (resource_list_find(rl, PCI_RES_BUS, *rid) == NULL)
3434                 resource_list_add(rl, PCI_RES_BUS, *rid, start, end, count);
3435         if (!resource_list_reserved(rl, PCI_RES_BUS, *rid)) {
3436                 res = resource_list_reserve(rl, dev, child, PCI_RES_BUS, rid,
3437                     start, end, count, flags & ~RF_ACTIVE);
3438                 if (res == NULL) {
3439                         resource_list_delete(rl, PCI_RES_BUS, *rid);
3440                         device_printf(child, "allocating %lu bus%s failed\n",
3441                             count, count == 1 ? "" : "es");
3442                         return (NULL);
3443                 }
3444                 if (bootverbose)
3445                         device_printf(child,
3446                             "Lazy allocation of %lu bus%s at %lu\n", count,
3447                             count == 1 ? "" : "es", rman_get_start(res));
3448                 PCI_WRITE_CONFIG(dev, child, sec_reg, rman_get_start(res), 1);
3449                 PCI_WRITE_CONFIG(dev, child, sub_reg, rman_get_end(res), 1);
3450         }
3451         return (resource_list_alloc(rl, dev, child, PCI_RES_BUS, rid, start,
3452             end, count, flags));
3453 }
3454 #endif
3455
3456 void
3457 pci_add_resources(device_t bus, device_t dev, int force, uint32_t prefetchmask)
3458 {
3459         struct pci_devinfo *dinfo;
3460         pcicfgregs *cfg;
3461         struct resource_list *rl;
3462         const struct pci_quirk *q;
3463         uint32_t devid;
3464         int i;
3465
3466         dinfo = device_get_ivars(dev);
3467         cfg = &dinfo->cfg;
3468         rl = &dinfo->resources;
3469         devid = (cfg->device << 16) | cfg->vendor;
3470
3471         /* ATA devices needs special map treatment */
3472         if ((pci_get_class(dev) == PCIC_STORAGE) &&
3473             (pci_get_subclass(dev) == PCIS_STORAGE_IDE) &&
3474             ((pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) ||
3475              (!pci_read_config(dev, PCIR_BAR(0), 4) &&
3476               !pci_read_config(dev, PCIR_BAR(2), 4))) )
3477                 pci_ata_maps(bus, dev, rl, force, prefetchmask);
3478         else
3479                 for (i = 0; i < cfg->nummaps;) {
3480                         /*
3481                          * Skip quirked resources.
3482                          */
3483                         for (q = &pci_quirks[0]; q->devid != 0; q++)
3484                                 if (q->devid == devid &&
3485                                     q->type == PCI_QUIRK_UNMAP_REG &&
3486                                     q->arg1 == PCIR_BAR(i))
3487                                         break;
3488                         if (q->devid != 0) {
3489                                 i++;
3490                                 continue;
3491                         }
3492                         i += pci_add_map(bus, dev, PCIR_BAR(i), rl, force,
3493                             prefetchmask & (1 << i));
3494                 }
3495
3496         /*
3497          * Add additional, quirked resources.
3498          */
3499         for (q = &pci_quirks[0]; q->devid != 0; q++)
3500                 if (q->devid == devid && q->type == PCI_QUIRK_MAP_REG)
3501                         pci_add_map(bus, dev, q->arg1, rl, force, 0);
3502
3503         if (cfg->intpin > 0 && PCI_INTERRUPT_VALID(cfg->intline)) {
3504 #ifdef __PCI_REROUTE_INTERRUPT
3505                 /*
3506                  * Try to re-route interrupts. Sometimes the BIOS or
3507                  * firmware may leave bogus values in these registers.
3508                  * If the re-route fails, then just stick with what we
3509                  * have.
3510                  */
3511                 pci_assign_interrupt(bus, dev, 1);
3512 #else
3513                 pci_assign_interrupt(bus, dev, 0);
3514 #endif
3515         }
3516
3517         if (pci_usb_takeover && pci_get_class(dev) == PCIC_SERIALBUS &&
3518             pci_get_subclass(dev) == PCIS_SERIALBUS_USB) {
3519                 if (pci_get_progif(dev) == PCIP_SERIALBUS_USB_XHCI)
3520                         xhci_early_takeover(dev);
3521                 else if (pci_get_progif(dev) == PCIP_SERIALBUS_USB_EHCI)
3522                         ehci_early_takeover(dev);
3523                 else if (pci_get_progif(dev) == PCIP_SERIALBUS_USB_OHCI)
3524                         ohci_early_takeover(dev);
3525                 else if (pci_get_progif(dev) == PCIP_SERIALBUS_USB_UHCI)
3526                         uhci_early_takeover(dev);
3527         }
3528
3529 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
3530         /*
3531          * Reserve resources for secondary bus ranges behind bridge
3532          * devices.
3533          */
3534         pci_reserve_secbus(bus, dev, cfg, rl);
3535 #endif
3536 }
3537
3538 static struct pci_devinfo *
3539 pci_identify_function(device_t pcib, device_t dev, int domain, int busno,
3540     int slot, int func, size_t dinfo_size)
3541 {
3542         struct pci_devinfo *dinfo;
3543
3544         dinfo = pci_read_device(pcib, domain, busno, slot, func, dinfo_size);
3545         if (dinfo != NULL)
3546                 pci_add_child(dev, dinfo);
3547
3548         return (dinfo);
3549 }
3550
3551 void
3552 pci_add_children(device_t dev, int domain, int busno, size_t dinfo_size)
3553 {
3554 #define REG(n, w)       PCIB_READ_CONFIG(pcib, busno, s, f, n, w)
3555         device_t pcib = device_get_parent(dev);
3556         struct pci_devinfo *dinfo;
3557         int maxslots;
3558         int s, f, pcifunchigh;
3559         uint8_t hdrtype;
3560         int first_func;
3561
3562         /*
3563          * Try to detect a device at slot 0, function 0.  If it exists, try to
3564          * enable ARI.  We must enable ARI before detecting the rest of the
3565          * functions on this bus as ARI changes the set of slots and functions
3566          * that are legal on this bus.
3567          */
3568         dinfo = pci_identify_function(pcib, dev, domain, busno, 0, 0,
3569             dinfo_size);
3570         if (dinfo != NULL && pci_enable_ari)
3571                 PCIB_TRY_ENABLE_ARI(pcib, dinfo->cfg.dev);
3572
3573         /*
3574          * Start looking for new devices on slot 0 at function 1 because we
3575          * just identified the device at slot 0, function 0.
3576          */
3577         first_func = 1;
3578
3579         KASSERT(dinfo_size >= sizeof(struct pci_devinfo),
3580             ("dinfo_size too small"));
3581         maxslots = PCIB_MAXSLOTS(pcib);
3582         for (s = 0; s <= maxslots; s++, first_func = 0) {
3583                 pcifunchigh = 0;
3584                 f = 0;
3585                 DELAY(1);
3586                 hdrtype = REG(PCIR_HDRTYPE, 1);
3587                 if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
3588                         continue;
3589                 if (hdrtype & PCIM_MFDEV)
3590                         pcifunchigh = PCIB_MAXFUNCS(pcib);
3591                 for (f = first_func; f <= pcifunchigh; f++)
3592                         pci_identify_function(pcib, dev, domain, busno, s, f,
3593                             dinfo_size);
3594         }
3595 #undef REG
3596 }
3597
3598 void
3599 pci_add_child(device_t bus, struct pci_devinfo *dinfo)
3600 {
3601         dinfo->cfg.dev = device_add_child(bus, NULL, -1);
3602         device_set_ivars(dinfo->cfg.dev, dinfo);
3603         resource_list_init(&dinfo->resources);
3604         pci_cfg_save(dinfo->cfg.dev, dinfo, 0);
3605         pci_cfg_restore(dinfo->cfg.dev, dinfo);
3606         pci_print_verbose(dinfo);
3607         pci_add_resources(bus, dinfo->cfg.dev, 0, 0);
3608         pci_child_added(dinfo->cfg.dev);
3609 }
3610
3611 void
3612 pci_child_added_method(device_t dev, device_t child)
3613 {
3614
3615 }
3616
3617 static int
3618 pci_probe(device_t dev)
3619 {
3620
3621         device_set_desc(dev, "PCI bus");
3622
3623         /* Allow other subclasses to override this driver. */
3624         return (BUS_PROBE_GENERIC);
3625 }
3626
3627 int
3628 pci_attach_common(device_t dev)
3629 {
3630         struct pci_softc *sc;
3631         int busno, domain;
3632 #ifdef PCI_DMA_BOUNDARY
3633         int error, tag_valid;
3634 #endif
3635 #ifdef PCI_RES_BUS
3636         int rid;
3637 #endif
3638
3639         sc = device_get_softc(dev);
3640         domain = pcib_get_domain(dev);
3641         busno = pcib_get_bus(dev);
3642 #ifdef PCI_RES_BUS
3643         rid = 0;
3644         sc->sc_bus = bus_alloc_resource(dev, PCI_RES_BUS, &rid, busno, busno,
3645             1, 0);
3646         if (sc->sc_bus == NULL) {
3647                 device_printf(dev, "failed to allocate bus number\n");
3648                 return (ENXIO);
3649         }
3650 #endif
3651         if (bootverbose)
3652                 device_printf(dev, "domain=%d, physical bus=%d\n",
3653                     domain, busno);
3654 #ifdef PCI_DMA_BOUNDARY
3655         tag_valid = 0;
3656         if (device_get_devclass(device_get_parent(device_get_parent(dev))) !=
3657             devclass_find("pci")) {
3658                 error = bus_dma_tag_create(bus_get_dma_tag(dev), 1,
3659                     PCI_DMA_BOUNDARY, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
3660                     NULL, NULL, BUS_SPACE_MAXSIZE, BUS_SPACE_UNRESTRICTED,
3661                     BUS_SPACE_MAXSIZE, 0, NULL, NULL, &sc->sc_dma_tag);
3662                 if (error)
3663                         device_printf(dev, "Failed to create DMA tag: %d\n",
3664                             error);
3665                 else
3666                         tag_valid = 1;
3667         }
3668         if (!tag_valid)
3669 #endif
3670                 sc->sc_dma_tag = bus_get_dma_tag(dev);
3671         return (0);
3672 }
3673
3674 static int
3675 pci_attach(device_t dev)
3676 {
3677         int busno, domain, error;
3678
3679         error = pci_attach_common(dev);
3680         if (error)
3681                 return (error);
3682
3683         /*
3684          * Since there can be multiple independantly numbered PCI
3685          * busses on systems with multiple PCI domains, we can't use
3686          * the unit number to decide which bus we are probing. We ask
3687          * the parent pcib what our domain and bus numbers are.
3688          */
3689         domain = pcib_get_domain(dev);
3690         busno = pcib_get_bus(dev);
3691         pci_add_children(dev, domain, busno, sizeof(struct pci_devinfo));
3692         return (bus_generic_attach(dev));
3693 }
3694
3695 #ifdef PCI_RES_BUS
3696 static int
3697 pci_detach(device_t dev)
3698 {
3699         struct pci_softc *sc;
3700         int error;
3701
3702         error = bus_generic_detach(dev);
3703         if (error)
3704                 return (error);
3705         sc = device_get_softc(dev);
3706         return (bus_release_resource(dev, PCI_RES_BUS, 0, sc->sc_bus));
3707 }
3708 #endif
3709
3710 static void
3711 pci_set_power_children(device_t dev, device_t *devlist, int numdevs,
3712     int state)
3713 {
3714         device_t child, pcib;
3715         int dstate, i;
3716
3717         /*
3718          * Set the device to the given state.  If the firmware suggests
3719          * a different power state, use it instead.  If power management
3720          * is not present, the firmware is responsible for managing
3721          * device power.  Skip children who aren't attached since they
3722          * are handled separately.
3723          */
3724         pcib = device_get_parent(dev);
3725         for (i = 0; i < numdevs; i++) {
3726                 child = devlist[i];
3727                 dstate = state;
3728                 if (device_is_attached(child) &&
3729                     PCIB_POWER_FOR_SLEEP(pcib, dev, &dstate) == 0)
3730                         pci_set_powerstate(child, dstate);
3731         }
3732 }
3733
3734 int
3735 pci_suspend(device_t dev)
3736 {
3737         device_t child, *devlist;
3738         struct pci_devinfo *dinfo;
3739         int error, i, numdevs;
3740
3741         /*
3742          * Save the PCI configuration space for each child and set the
3743          * device in the appropriate power state for this sleep state.
3744          */
3745         if ((error = device_get_children(dev, &devlist, &numdevs)) != 0)
3746                 return (error);
3747         for (i = 0; i < numdevs; i++) {
3748                 child = devlist[i];
3749                 dinfo = device_get_ivars(child);
3750                 pci_cfg_save(child, dinfo, 0);
3751         }
3752
3753         /* Suspend devices before potentially powering them down. */
3754         error = bus_generic_suspend(dev);
3755         if (error) {
3756                 free(devlist, M_TEMP);
3757                 return (error);
3758         }
3759         if (pci_do_power_suspend)
3760                 pci_set_power_children(dev, devlist, numdevs,
3761                     PCI_POWERSTATE_D3);
3762         free(devlist, M_TEMP);
3763         return (0);
3764 }
3765
3766 int
3767 pci_resume(device_t dev)
3768 {
3769         device_t child, *devlist;
3770         struct pci_devinfo *dinfo;
3771         int error, i, numdevs;
3772
3773         /*
3774          * Set each child to D0 and restore its PCI configuration space.
3775          */
3776         if ((error = device_get_children(dev, &devlist, &numdevs)) != 0)
3777                 return (error);
3778         if (pci_do_power_resume)
3779                 pci_set_power_children(dev, devlist, numdevs,
3780                     PCI_POWERSTATE_D0);
3781
3782         /* Now the device is powered up, restore its config space. */
3783         for (i = 0; i < numdevs; i++) {
3784                 child = devlist[i];
3785                 dinfo = device_get_ivars(child);
3786
3787                 pci_cfg_restore(child, dinfo);
3788                 if (!device_is_attached(child))
3789                         pci_cfg_save(child, dinfo, 1);
3790         }
3791
3792         /*
3793          * Resume critical devices first, then everything else later.
3794          */
3795         for (i = 0; i < numdevs; i++) {
3796                 child = devlist[i];
3797                 switch (pci_get_class(child)) {
3798                 case PCIC_DISPLAY:
3799                 case PCIC_MEMORY:
3800                 case PCIC_BRIDGE:
3801                 case PCIC_BASEPERIPH:
3802                         DEVICE_RESUME(child);
3803                         break;
3804                 }
3805         }
3806         for (i = 0; i < numdevs; i++) {
3807                 child = devlist[i];
3808                 switch (pci_get_class(child)) {
3809                 case PCIC_DISPLAY:
3810                 case PCIC_MEMORY:
3811                 case PCIC_BRIDGE:
3812                 case PCIC_BASEPERIPH:
3813                         break;
3814                 default:
3815                         DEVICE_RESUME(child);
3816                 }
3817         }
3818         free(devlist, M_TEMP);
3819         return (0);
3820 }
3821
3822 static void
3823 pci_load_vendor_data(void)
3824 {
3825         caddr_t data;
3826         void *ptr;
3827         size_t sz;
3828
3829         data = preload_search_by_type("pci_vendor_data");
3830         if (data != NULL) {
3831                 ptr = preload_fetch_addr(data);
3832                 sz = preload_fetch_size(data);
3833                 if (ptr != NULL && sz != 0) {
3834                         pci_vendordata = ptr;
3835                         pci_vendordata_size = sz;
3836                         /* terminate the database */
3837                         pci_vendordata[pci_vendordata_size] = '\n';
3838                 }
3839         }
3840 }
3841
3842 void
3843 pci_driver_added(device_t dev, driver_t *driver)
3844 {
3845         int numdevs;
3846         device_t *devlist;
3847         device_t child;
3848         struct pci_devinfo *dinfo;
3849         int i;
3850
3851         if (bootverbose)
3852                 device_printf(dev, "driver added\n");
3853         DEVICE_IDENTIFY(driver, dev);
3854         if (device_get_children(dev, &devlist, &numdevs) != 0)
3855                 return;
3856         for (i = 0; i < numdevs; i++) {
3857                 child = devlist[i];
3858                 if (device_get_state(child) != DS_NOTPRESENT)
3859                         continue;
3860                 dinfo = device_get_ivars(child);
3861                 pci_print_verbose(dinfo);
3862                 if (bootverbose)
3863                         pci_printf(&dinfo->cfg, "reprobing on driver added\n");
3864                 pci_cfg_restore(child, dinfo);
3865                 if (device_probe_and_attach(child) != 0)
3866                         pci_child_detached(dev, child);
3867         }
3868         free(devlist, M_TEMP);
3869 }
3870
3871 int
3872 pci_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
3873     driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
3874 {
3875         struct pci_devinfo *dinfo;
3876         struct msix_table_entry *mte;
3877         struct msix_vector *mv;
3878         uint64_t addr;
3879         uint32_t data;
3880         void *cookie;
3881         int error, rid;
3882
3883         error = bus_generic_setup_intr(dev, child, irq, flags, filter, intr,
3884             arg, &cookie);
3885         if (error)
3886                 return (error);
3887
3888         /* If this is not a direct child, just bail out. */
3889         if (device_get_parent(child) != dev) {
3890                 *cookiep = cookie;
3891                 return(0);
3892         }
3893
3894         rid = rman_get_rid(irq);
3895         if (rid == 0) {
3896                 /* Make sure that INTx is enabled */
3897                 pci_clear_command_bit(dev, child, PCIM_CMD_INTxDIS);
3898         } else {
3899                 /*
3900                  * Check to see if the interrupt is MSI or MSI-X.
3901                  * Ask our parent to map the MSI and give
3902                  * us the address and data register values.
3903                  * If we fail for some reason, teardown the
3904                  * interrupt handler.
3905                  */
3906                 dinfo = device_get_ivars(child);
3907                 if (dinfo->cfg.msi.msi_alloc > 0) {
3908                         if (dinfo->cfg.msi.msi_addr == 0) {
3909                                 KASSERT(dinfo->cfg.msi.msi_handlers == 0,
3910                             ("MSI has handlers, but vectors not mapped"));
3911                                 error = PCIB_MAP_MSI(device_get_parent(dev),
3912                                     child, rman_get_start(irq), &addr, &data);
3913                                 if (error)
3914                                         goto bad;
3915                                 dinfo->cfg.msi.msi_addr = addr;
3916                                 dinfo->cfg.msi.msi_data = data;
3917                         }
3918                         if (dinfo->cfg.msi.msi_handlers == 0)
3919                                 pci_enable_msi(child, dinfo->cfg.msi.msi_addr,
3920                                     dinfo->cfg.msi.msi_data);
3921                         dinfo->cfg.msi.msi_handlers++;
3922                 } else {
3923                         KASSERT(dinfo->cfg.msix.msix_alloc > 0,
3924                             ("No MSI or MSI-X interrupts allocated"));
3925                         KASSERT(rid <= dinfo->cfg.msix.msix_table_len,
3926                             ("MSI-X index too high"));
3927                         mte = &dinfo->cfg.msix.msix_table[rid - 1];
3928                         KASSERT(mte->mte_vector != 0, ("no message vector"));
3929                         mv = &dinfo->cfg.msix.msix_vectors[mte->mte_vector - 1];
3930                         KASSERT(mv->mv_irq == rman_get_start(irq),
3931                             ("IRQ mismatch"));
3932                         if (mv->mv_address == 0) {
3933                                 KASSERT(mte->mte_handlers == 0,
3934                     ("MSI-X table entry has handlers, but vector not mapped"));
3935                                 error = PCIB_MAP_MSI(device_get_parent(dev),
3936                                     child, rman_get_start(irq), &addr, &data);
3937                                 if (error)
3938                                         goto bad;
3939                                 mv->mv_address = addr;
3940                                 mv->mv_data = data;
3941                         }
3942                         if (mte->mte_handlers == 0) {
3943                                 pci_enable_msix(child, rid - 1, mv->mv_address,
3944                                     mv->mv_data);
3945                                 pci_unmask_msix(child, rid - 1);
3946                         }
3947                         mte->mte_handlers++;
3948                 }
3949
3950                 /*
3951                  * Make sure that INTx is disabled if we are using MSI/MSI-X,
3952                  * unless the device is affected by PCI_QUIRK_MSI_INTX_BUG,
3953                  * in which case we "enable" INTx so MSI/MSI-X actually works.
3954                  */
3955                 if (!pci_has_quirk(pci_get_devid(child),
3956                     PCI_QUIRK_MSI_INTX_BUG))
3957                         pci_set_command_bit(dev, child, PCIM_CMD_INTxDIS);
3958                 else
3959                         pci_clear_command_bit(dev, child, PCIM_CMD_INTxDIS);
3960         bad:
3961                 if (error) {
3962                         (void)bus_generic_teardown_intr(dev, child, irq,
3963                             cookie);
3964                         return (error);
3965                 }
3966         }
3967         *cookiep = cookie;
3968         return (0);
3969 }
3970
3971 int
3972 pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
3973     void *cookie)
3974 {
3975         struct msix_table_entry *mte;
3976         struct resource_list_entry *rle;
3977         struct pci_devinfo *dinfo;
3978         int error, rid;
3979
3980         if (irq == NULL || !(rman_get_flags(irq) & RF_ACTIVE))
3981                 return (EINVAL);
3982
3983         /* If this isn't a direct child, just bail out */
3984         if (device_get_parent(child) != dev)
3985                 return(bus_generic_teardown_intr(dev, child, irq, cookie));
3986
3987         rid = rman_get_rid(irq);
3988         if (rid == 0) {
3989                 /* Mask INTx */
3990                 pci_set_command_bit(dev, child, PCIM_CMD_INTxDIS);
3991         } else {
3992                 /*
3993                  * Check to see if the interrupt is MSI or MSI-X.  If so,
3994                  * decrement the appropriate handlers count and mask the
3995                  * MSI-X message, or disable MSI messages if the count
3996                  * drops to 0.
3997                  */
3998                 dinfo = device_get_ivars(child);
3999                 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, rid);
4000                 if (rle->res != irq)
4001                         return (EINVAL);
4002                 if (dinfo->cfg.msi.msi_alloc > 0) {
4003                         KASSERT(rid <= dinfo->cfg.msi.msi_alloc,
4004                             ("MSI-X index too high"));
4005                         if (dinfo->cfg.msi.msi_handlers == 0)
4006                                 return (EINVAL);
4007                         dinfo->cfg.msi.msi_handlers--;
4008                         if (dinfo->cfg.msi.msi_handlers == 0)
4009                                 pci_disable_msi(child);
4010                 } else {
4011                         KASSERT(dinfo->cfg.msix.msix_alloc > 0,
4012                             ("No MSI or MSI-X interrupts allocated"));
4013                         KASSERT(rid <= dinfo->cfg.msix.msix_table_len,
4014                             ("MSI-X index too high"));
4015                         mte = &dinfo->cfg.msix.msix_table[rid - 1];
4016                         if (mte->mte_handlers == 0)
4017                                 return (EINVAL);
4018                         mte->mte_handlers--;
4019                         if (mte->mte_handlers == 0)
4020                                 pci_mask_msix(child, rid - 1);
4021                 }
4022         }
4023         error = bus_generic_teardown_intr(dev, child, irq, cookie);
4024         if (rid > 0)
4025                 KASSERT(error == 0,
4026                     ("%s: generic teardown failed for MSI/MSI-X", __func__));
4027         return (error);
4028 }
4029
4030 int
4031 pci_print_child(device_t dev, device_t child)
4032 {
4033         struct pci_devinfo *dinfo;
4034         struct resource_list *rl;
4035         int retval = 0;
4036
4037         dinfo = device_get_ivars(child);
4038         rl = &dinfo->resources;
4039
4040         retval += bus_print_child_header(dev, child);
4041
4042         retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
4043         retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
4044         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
4045         if (device_get_flags(dev))
4046                 retval += printf(" flags %#x", device_get_flags(dev));
4047
4048         retval += printf(" at device %d.%d", pci_get_slot(child),
4049             pci_get_function(child));
4050
4051         retval += bus_print_child_domain(dev, child);
4052         retval += bus_print_child_footer(dev, child);
4053
4054         return (retval);
4055 }
4056
4057 static const struct
4058 {
4059         int             class;
4060         int             subclass;
4061         int             report; /* 0 = bootverbose, 1 = always */
4062         const char      *desc;
4063 } pci_nomatch_tab[] = {
4064         {PCIC_OLD,              -1,                     1, "old"},
4065         {PCIC_OLD,              PCIS_OLD_NONVGA,        1, "non-VGA display device"},
4066         {PCIC_OLD,              PCIS_OLD_VGA,           1, "VGA-compatible display device"},
4067         {PCIC_STORAGE,          -1,                     1, "mass storage"},
4068         {PCIC_STORAGE,          PCIS_STORAGE_SCSI,      1, "SCSI"},
4069         {PCIC_STORAGE,          PCIS_STORAGE_IDE,       1, "ATA"},
4070         {PCIC_STORAGE,          PCIS_STORAGE_FLOPPY,    1, "floppy disk"},
4071         {PCIC_STORAGE,          PCIS_STORAGE_IPI,       1, "IPI"},
4072         {PCIC_STORAGE,          PCIS_STORAGE_RAID,      1, "RAID"},
4073         {PCIC_STORAGE,          PCIS_STORAGE_ATA_ADMA,  1, "ATA (ADMA)"},
4074         {PCIC_STORAGE,          PCIS_STORAGE_SATA,      1, "SATA"},
4075         {PCIC_STORAGE,          PCIS_STORAGE_SAS,       1, "SAS"},
4076         {PCIC_STORAGE,          PCIS_STORAGE_NVM,       1, "NVM"},
4077         {PCIC_NETWORK,          -1,                     1, "network"},
4078         {PCIC_NETWORK,          PCIS_NETWORK_ETHERNET,  1, "ethernet"},
4079         {PCIC_NETWORK,          PCIS_NETWORK_TOKENRING, 1, "token ring"},
4080         {PCIC_NETWORK,          PCIS_NETWORK_FDDI,      1, "fddi"},
4081         {PCIC_NETWORK,          PCIS_NETWORK_ATM,       1, "ATM"},
4082         {PCIC_NETWORK,          PCIS_NETWORK_ISDN,      1, "ISDN"},
4083         {PCIC_DISPLAY,          -1,                     1, "display"},
4084         {PCIC_DISPLAY,          PCIS_DISPLAY_VGA,       1, "VGA"},
4085         {PCIC_DISPLAY,          PCIS_DISPLAY_XGA,       1, "XGA"},
4086         {PCIC_DISPLAY,          PCIS_DISPLAY_3D,        1, "3D"},
4087         {PCIC_MULTIMEDIA,       -1,                     1, "multimedia"},
4088         {PCIC_MULTIMEDIA,       PCIS_MULTIMEDIA_VIDEO,  1, "video"},
4089         {PCIC_MULTIMEDIA,       PCIS_MULTIMEDIA_AUDIO,  1, "audio"},
4090         {PCIC_MULTIMEDIA,       PCIS_MULTIMEDIA_TELE,   1, "telephony"},
4091         {PCIC_MULTIMEDIA,       PCIS_MULTIMEDIA_HDA,    1, "HDA"},
4092         {PCIC_MEMORY,           -1,                     1, "memory"},
4093         {PCIC_MEMORY,           PCIS_MEMORY_RAM,        1, "RAM"},
4094         {PCIC_MEMORY,           PCIS_MEMORY_FLASH,      1, "flash"},
4095         {PCIC_BRIDGE,           -1,                     1, "bridge"},
4096         {PCIC_BRIDGE,           PCIS_BRIDGE_HOST,       1, "HOST-PCI"},
4097         {PCIC_BRIDGE,           PCIS_BRIDGE_ISA,        1, "PCI-ISA"},
4098         {PCIC_BRIDGE,           PCIS_BRIDGE_EISA,       1, "PCI-EISA"},
4099         {PCIC_BRIDGE,           PCIS_BRIDGE_MCA,        1, "PCI-MCA"},
4100         {PCIC_BRIDGE,           PCIS_BRIDGE_PCI,        1, "PCI-PCI"},
4101         {PCIC_BRIDGE,           PCIS_BRIDGE_PCMCIA,     1, "PCI-PCMCIA"},
4102         {PCIC_BRIDGE,           PCIS_BRIDGE_NUBUS,      1, "PCI-NuBus"},
4103         {PCIC_BRIDGE,           PCIS_BRIDGE_CARDBUS,    1, "PCI-CardBus"},
4104         {PCIC_BRIDGE,           PCIS_BRIDGE_RACEWAY,    1, "PCI-RACEway"},
4105         {PCIC_SIMPLECOMM,       -1,                     1, "simple comms"},
4106         {PCIC_SIMPLECOMM,       PCIS_SIMPLECOMM_UART,   1, "UART"},     /* could detect 16550 */
4107         {PCIC_SIMPLECOMM,       PCIS_SIMPLECOMM_PAR,    1, "parallel port"},
4108         {PCIC_SIMPLECOMM,       PCIS_SIMPLECOMM_MULSER, 1, "multiport serial"},
4109         {PCIC_SIMPLECOMM,       PCIS_SIMPLECOMM_MODEM,  1, "generic modem"},
4110         {PCIC_BASEPERIPH,       -1,                     0, "base peripheral"},
4111         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_PIC,    1, "interrupt controller"},
4112         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_DMA,    1, "DMA controller"},
4113         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_TIMER,  1, "timer"},
4114         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_RTC,    1, "realtime clock"},
4115         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_PCIHOT, 1, "PCI hot-plug controller"},
4116         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_SDHC,   1, "SD host controller"},
4117         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_IOMMU,  1, "IOMMU"},
4118         {PCIC_INPUTDEV,         -1,                     1, "input device"},
4119         {PCIC_INPUTDEV,         PCIS_INPUTDEV_KEYBOARD, 1, "keyboard"},
4120         {PCIC_INPUTDEV,         PCIS_INPUTDEV_DIGITIZER,1, "digitizer"},
4121         {PCIC_INPUTDEV,         PCIS_INPUTDEV_MOUSE,    1, "mouse"},
4122         {PCIC_INPUTDEV,         PCIS_INPUTDEV_SCANNER,  1, "scanner"},
4123         {PCIC_INPUTDEV,         PCIS_INPUTDEV_GAMEPORT, 1, "gameport"},
4124         {PCIC_DOCKING,          -1,                     1, "docking station"},
4125         {PCIC_PROCESSOR,        -1,                     1, "processor"},
4126         {PCIC_SERIALBUS,        -1,                     1, "serial bus"},
4127         {PCIC_SERIALBUS,        PCIS_SERIALBUS_FW,      1, "FireWire"},
4128         {PCIC_SERIALBUS,        PCIS_SERIALBUS_ACCESS,  1, "AccessBus"},
4129         {PCIC_SERIALBUS,        PCIS_SERIALBUS_SSA,     1, "SSA"},
4130         {PCIC_SERIALBUS,        PCIS_SERIALBUS_USB,     1, "USB"},
4131         {PCIC_SERIALBUS,        PCIS_SERIALBUS_FC,      1, "Fibre Channel"},
4132         {PCIC_SERIALBUS,        PCIS_SERIALBUS_SMBUS,   0, "SMBus"},
4133         {PCIC_WIRELESS,         -1,                     1, "wireless controller"},
4134         {PCIC_WIRELESS,         PCIS_WIRELESS_IRDA,     1, "iRDA"},
4135         {PCIC_WIRELESS,         PCIS_WIRELESS_IR,       1, "IR"},
4136         {PCIC_WIRELESS,         PCIS_WIRELESS_RF,       1, "RF"},
4137         {PCIC_INTELLIIO,        -1,                     1, "intelligent I/O controller"},
4138         {PCIC_INTELLIIO,        PCIS_INTELLIIO_I2O,     1, "I2O"},
4139         {PCIC_SATCOM,           -1,                     1, "satellite communication"},
4140         {PCIC_SATCOM,           PCIS_SATCOM_TV,         1, "sat TV"},
4141         {PCIC_SATCOM,           PCIS_SATCOM_AUDIO,      1, "sat audio"},
4142         {PCIC_SATCOM,           PCIS_SATCOM_VOICE,      1, "sat voice"},
4143         {PCIC_SATCOM,           PCIS_SATCOM_DATA,       1, "sat data"},
4144         {PCIC_CRYPTO,           -1,                     1, "encrypt/decrypt"},
4145         {PCIC_CRYPTO,           PCIS_CRYPTO_NETCOMP,    1, "network/computer crypto"},
4146         {PCIC_CRYPTO,           PCIS_CRYPTO_ENTERTAIN,  1, "entertainment crypto"},
4147         {PCIC_DASP,             -1,                     0, "dasp"},
4148         {PCIC_DASP,             PCIS_DASP_DPIO,         1, "DPIO module"},
4149         {0, 0, 0,               NULL}
4150 };
4151
4152 void
4153 pci_probe_nomatch(device_t dev, device_t child)
4154 {
4155         int i, report;
4156         const char *cp, *scp;
4157         char *device;
4158
4159         /*
4160          * Look for a listing for this device in a loaded device database.
4161          */
4162         report = 1;
4163         if ((device = pci_describe_device(child)) != NULL) {
4164                 device_printf(dev, "<%s>", device);
4165                 free(device, M_DEVBUF);
4166         } else {
4167                 /*
4168                  * Scan the class/subclass descriptions for a general
4169                  * description.
4170                  */
4171                 cp = "unknown";
4172                 scp = NULL;
4173                 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
4174                         if (pci_nomatch_tab[i].class == pci_get_class(child)) {
4175                                 if (pci_nomatch_tab[i].subclass == -1) {
4176                                         cp = pci_nomatch_tab[i].desc;
4177                                         report = pci_nomatch_tab[i].report;
4178                                 } else if (pci_nomatch_tab[i].subclass ==
4179                                     pci_get_subclass(child)) {
4180                                         scp = pci_nomatch_tab[i].desc;
4181                                         report = pci_nomatch_tab[i].report;
4182                                 }
4183                         }
4184                 }
4185                 if (report || bootverbose) {
4186                         device_printf(dev, "<%s%s%s>",
4187                             cp ? cp : "",
4188                             ((cp != NULL) && (scp != NULL)) ? ", " : "",
4189                             scp ? scp : "");
4190                 }
4191         }
4192         if (report || bootverbose) {
4193                 printf(" at device %d.%d (no driver attached)\n",
4194                     pci_get_slot(child), pci_get_function(child));
4195         }
4196         pci_cfg_save(child, device_get_ivars(child), 1);
4197 }
4198
4199 void
4200 pci_child_detached(device_t dev, device_t child)
4201 {
4202         struct pci_devinfo *dinfo;
4203         struct resource_list *rl;
4204
4205         dinfo = device_get_ivars(child);
4206         rl = &dinfo->resources;
4207
4208         /*
4209          * Have to deallocate IRQs before releasing any MSI messages and
4210          * have to release MSI messages before deallocating any memory
4211          * BARs.
4212          */
4213         if (resource_list_release_active(rl, dev, child, SYS_RES_IRQ) != 0)
4214                 pci_printf(&dinfo->cfg, "Device leaked IRQ resources\n");
4215         if (dinfo->cfg.msi.msi_alloc != 0 || dinfo->cfg.msix.msix_alloc != 0) {
4216                 pci_printf(&dinfo->cfg, "Device leaked MSI vectors\n");
4217                 (void)pci_release_msi(child);
4218         }
4219         if (resource_list_release_active(rl, dev, child, SYS_RES_MEMORY) != 0)
4220                 pci_printf(&dinfo->cfg, "Device leaked memory resources\n");
4221         if (resource_list_release_active(rl, dev, child, SYS_RES_IOPORT) != 0)
4222                 pci_printf(&dinfo->cfg, "Device leaked I/O resources\n");
4223 #ifdef PCI_RES_BUS
4224         if (resource_list_release_active(rl, dev, child, PCI_RES_BUS) != 0)
4225                 pci_printf(&dinfo->cfg, "Device leaked PCI bus numbers\n");
4226 #endif
4227
4228         pci_cfg_save(child, dinfo, 1);
4229 }
4230
4231 /*
4232  * Parse the PCI device database, if loaded, and return a pointer to a
4233  * description of the device.
4234  *
4235  * The database is flat text formatted as follows:
4236  *
4237  * Any line not in a valid format is ignored.
4238  * Lines are terminated with newline '\n' characters.
4239  *
4240  * A VENDOR line consists of the 4 digit (hex) vendor code, a TAB, then
4241  * the vendor name.
4242  *
4243  * A DEVICE line is entered immediately below the corresponding VENDOR ID.
4244  * - devices cannot be listed without a corresponding VENDOR line.
4245  * A DEVICE line consists of a TAB, the 4 digit (hex) device code,
4246  * another TAB, then the device name.
4247  */
4248
4249 /*
4250  * Assuming (ptr) points to the beginning of a line in the database,
4251  * return the vendor or device and description of the next entry.
4252  * The value of (vendor) or (device) inappropriate for the entry type
4253  * is set to -1.  Returns nonzero at the end of the database.
4254  *
4255  * Note that this is slightly unrobust in the face of corrupt data;
4256  * we attempt to safeguard against this by spamming the end of the
4257  * database with a newline when we initialise.
4258  */
4259 static int
4260 pci_describe_parse_line(char **ptr, int *vendor, int *device, char **desc)
4261 {
4262         char    *cp = *ptr;
4263         int     left;
4264
4265         *device = -1;
4266         *vendor = -1;
4267         **desc = '\0';
4268         for (;;) {
4269                 left = pci_vendordata_size - (cp - pci_vendordata);
4270                 if (left <= 0) {
4271                         *ptr = cp;
4272                         return(1);
4273                 }
4274
4275                 /* vendor entry? */
4276                 if (*cp != '\t' &&
4277                     sscanf(cp, "%x\t%80[^\n]", vendor, *desc) == 2)
4278                         break;
4279                 /* device entry? */
4280                 if (*cp == '\t' &&
4281                     sscanf(cp, "%x\t%80[^\n]", device, *desc) == 2)
4282                         break;
4283
4284                 /* skip to next line */
4285                 while (*cp != '\n' && left > 0) {
4286                         cp++;
4287                         left--;
4288                 }
4289                 if (*cp == '\n') {
4290                         cp++;
4291                         left--;
4292                 }
4293         }
4294         /* skip to next line */
4295         while (*cp != '\n' && left > 0) {
4296                 cp++;
4297                 left--;
4298         }
4299         if (*cp == '\n' && left > 0)
4300                 cp++;
4301         *ptr = cp;
4302         return(0);
4303 }
4304
4305 static char *
4306 pci_describe_device(device_t dev)
4307 {
4308         int     vendor, device;
4309         char    *desc, *vp, *dp, *line;
4310
4311         desc = vp = dp = NULL;
4312
4313         /*
4314          * If we have no vendor data, we can't do anything.
4315          */
4316         if (pci_vendordata == NULL)
4317                 goto out;
4318
4319         /*
4320          * Scan the vendor data looking for this device
4321          */
4322         line = pci_vendordata;
4323         if ((vp = malloc(80, M_DEVBUF, M_NOWAIT)) == NULL)
4324                 goto out;
4325         for (;;) {
4326                 if (pci_describe_parse_line(&line, &vendor, &device, &vp))
4327                         goto out;
4328                 if (vendor == pci_get_vendor(dev))
4329                         break;
4330         }
4331         if ((dp = malloc(80, M_DEVBUF, M_NOWAIT)) == NULL)
4332                 goto out;
4333         for (;;) {
4334                 if (pci_describe_parse_line(&line, &vendor, &device, &dp)) {
4335                         *dp = 0;
4336                         break;
4337                 }
4338                 if (vendor != -1) {
4339                         *dp = 0;
4340                         break;
4341                 }
4342                 if (device == pci_get_device(dev))
4343                         break;
4344         }
4345         if (dp[0] == '\0')
4346                 snprintf(dp, 80, "0x%x", pci_get_device(dev));
4347         if ((desc = malloc(strlen(vp) + strlen(dp) + 3, M_DEVBUF, M_NOWAIT)) !=
4348             NULL)
4349                 sprintf(desc, "%s, %s", vp, dp);
4350 out:
4351         if (vp != NULL)
4352                 free(vp, M_DEVBUF);
4353         if (dp != NULL)
4354                 free(dp, M_DEVBUF);
4355         return(desc);
4356 }
4357
4358 int
4359 pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
4360 {
4361         struct pci_devinfo *dinfo;
4362         pcicfgregs *cfg;
4363
4364         dinfo = device_get_ivars(child);
4365         cfg = &dinfo->cfg;
4366
4367         switch (which) {
4368         case PCI_IVAR_ETHADDR:
4369                 /*
4370                  * The generic accessor doesn't deal with failure, so
4371                  * we set the return value, then return an error.
4372                  */
4373                 *((uint8_t **) result) = NULL;
4374                 return (EINVAL);
4375         case PCI_IVAR_SUBVENDOR:
4376                 *result = cfg->subvendor;
4377                 break;
4378         case PCI_IVAR_SUBDEVICE:
4379                 *result = cfg->subdevice;
4380                 break;
4381         case PCI_IVAR_VENDOR:
4382                 *result = cfg->vendor;
4383                 break;
4384         case PCI_IVAR_DEVICE:
4385                 *result = cfg->device;
4386                 break;
4387         case PCI_IVAR_DEVID:
4388                 *result = (cfg->device << 16) | cfg->vendor;
4389                 break;
4390         case PCI_IVAR_CLASS:
4391                 *result = cfg->baseclass;
4392                 break;
4393         case PCI_IVAR_SUBCLASS:
4394                 *result = cfg->subclass;
4395                 break;
4396         case PCI_IVAR_PROGIF:
4397                 *result = cfg->progif;
4398                 break;
4399         case PCI_IVAR_REVID:
4400                 *result = cfg->revid;
4401                 break;
4402         case PCI_IVAR_INTPIN:
4403                 *result = cfg->intpin;
4404                 break;
4405         case PCI_IVAR_IRQ:
4406                 *result = cfg->intline;
4407                 break;
4408         case PCI_IVAR_DOMAIN:
4409                 *result = cfg->domain;
4410                 break;
4411         case PCI_IVAR_BUS:
4412                 *result = cfg->bus;
4413                 break;
4414         case PCI_IVAR_SLOT:
4415                 *result = cfg->slot;
4416                 break;
4417         case PCI_IVAR_FUNCTION:
4418                 *result = cfg->func;
4419                 break;
4420         case PCI_IVAR_CMDREG:
4421                 *result = cfg->cmdreg;
4422                 break;
4423         case PCI_IVAR_CACHELNSZ:
4424                 *result = cfg->cachelnsz;
4425                 break;
4426         case PCI_IVAR_MINGNT:
4427                 if (cfg->hdrtype != PCIM_HDRTYPE_NORMAL) {
4428                         *result = -1;
4429                         return (EINVAL);
4430                 }
4431                 *result = cfg->mingnt;
4432                 break;
4433         case PCI_IVAR_MAXLAT:
4434                 if (cfg->hdrtype != PCIM_HDRTYPE_NORMAL) {
4435                         *result = -1;
4436                         return (EINVAL);
4437                 }
4438                 *result = cfg->maxlat;
4439                 break;
4440         case PCI_IVAR_LATTIMER:
4441                 *result = cfg->lattimer;
4442                 break;
4443         default:
4444                 return (ENOENT);
4445         }
4446         return (0);
4447 }
4448
4449 int
4450 pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
4451 {
4452         struct pci_devinfo *dinfo;
4453
4454         dinfo = device_get_ivars(child);
4455
4456         switch (which) {
4457         case PCI_IVAR_INTPIN:
4458                 dinfo->cfg.intpin = value;
4459                 return (0);
4460         case PCI_IVAR_ETHADDR:
4461         case PCI_IVAR_SUBVENDOR:
4462         case PCI_IVAR_SUBDEVICE:
4463         case PCI_IVAR_VENDOR:
4464         case PCI_IVAR_DEVICE:
4465         case PCI_IVAR_DEVID:
4466         case PCI_IVAR_CLASS:
4467         case PCI_IVAR_SUBCLASS:
4468         case PCI_IVAR_PROGIF:
4469         case PCI_IVAR_REVID:
4470         case PCI_IVAR_IRQ:
4471         case PCI_IVAR_DOMAIN:
4472         case PCI_IVAR_BUS:
4473         case PCI_IVAR_SLOT:
4474         case PCI_IVAR_FUNCTION:
4475                 return (EINVAL);        /* disallow for now */
4476
4477         default:
4478                 return (ENOENT);
4479         }
4480 }
4481
4482 #include "opt_ddb.h"
4483 #ifdef DDB
4484 #include <ddb/ddb.h>
4485 #include <sys/cons.h>
4486
4487 /*
4488  * List resources based on pci map registers, used for within ddb
4489  */
4490
4491 DB_SHOW_COMMAND(pciregs, db_pci_dump)
4492 {
4493         struct pci_devinfo *dinfo;
4494         struct devlist *devlist_head;
4495         struct pci_conf *p;
4496         const char *name;
4497         int i, error, none_count;
4498
4499         none_count = 0;
4500         /* get the head of the device queue */
4501         devlist_head = &pci_devq;
4502
4503         /*
4504          * Go through the list of devices and print out devices
4505          */
4506         for (error = 0, i = 0,
4507              dinfo = STAILQ_FIRST(devlist_head);
4508              (dinfo != NULL) && (error == 0) && (i < pci_numdevs) && !db_pager_quit;
4509              dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
4510
4511                 /* Populate pd_name and pd_unit */
4512                 name = NULL;
4513                 if (dinfo->cfg.dev)
4514                         name = device_get_name(dinfo->cfg.dev);
4515
4516                 p = &dinfo->conf;
4517                 db_printf("%s%d@pci%d:%d:%d:%d:\tclass=0x%06x card=0x%08x "
4518                         "chip=0x%08x rev=0x%02x hdr=0x%02x\n",
4519                         (name && *name) ? name : "none",
4520                         (name && *name) ? (int)device_get_unit(dinfo->cfg.dev) :
4521                         none_count++,
4522                         p->pc_sel.pc_domain, p->pc_sel.pc_bus, p->pc_sel.pc_dev,
4523                         p->pc_sel.pc_func, (p->pc_class << 16) |
4524                         (p->pc_subclass << 8) | p->pc_progif,
4525                         (p->pc_subdevice << 16) | p->pc_subvendor,
4526                         (p->pc_device << 16) | p->pc_vendor,
4527                         p->pc_revid, p->pc_hdr);
4528         }
4529 }
4530 #endif /* DDB */
4531
4532 static struct resource *
4533 pci_reserve_map(device_t dev, device_t child, int type, int *rid,
4534     u_long start, u_long end, u_long count, u_int flags)
4535 {
4536         struct pci_devinfo *dinfo = device_get_ivars(child);
4537         struct resource_list *rl = &dinfo->resources;
4538         struct resource *res;
4539         struct pci_map *pm;
4540         pci_addr_t map, testval;
4541         int mapsize;
4542
4543         res = NULL;
4544         pm = pci_find_bar(child, *rid);
4545         if (pm != NULL) {
4546                 /* This is a BAR that we failed to allocate earlier. */
4547                 mapsize = pm->pm_size;
4548                 map = pm->pm_value;
4549         } else {
4550                 /*
4551                  * Weed out the bogons, and figure out how large the
4552                  * BAR/map is.  BARs that read back 0 here are bogus
4553                  * and unimplemented.  Note: atapci in legacy mode are
4554                  * special and handled elsewhere in the code.  If you
4555                  * have a atapci device in legacy mode and it fails
4556                  * here, that other code is broken.
4557                  */
4558                 pci_read_bar(child, *rid, &map, &testval);
4559
4560                 /*
4561                  * Determine the size of the BAR and ignore BARs with a size
4562                  * of 0.  Device ROM BARs use a different mask value.
4563                  */
4564                 if (PCIR_IS_BIOS(&dinfo->cfg, *rid))
4565                         mapsize = pci_romsize(testval);
4566                 else
4567                         mapsize = pci_mapsize(testval);
4568                 if (mapsize == 0)
4569                         goto out;
4570                 pm = pci_add_bar(child, *rid, map, mapsize);
4571         }
4572
4573         if (PCI_BAR_MEM(map) || PCIR_IS_BIOS(&dinfo->cfg, *rid)) {
4574                 if (type != SYS_RES_MEMORY) {
4575                         if (bootverbose)
4576                                 device_printf(dev,
4577                                     "child %s requested type %d for rid %#x,"
4578                                     " but the BAR says it is an memio\n",
4579                                     device_get_nameunit(child), type, *rid);
4580                         goto out;
4581                 }
4582         } else {
4583                 if (type != SYS_RES_IOPORT) {
4584                         if (bootverbose)
4585                                 device_printf(dev,
4586                                     "child %s requested type %d for rid %#x,"
4587                                     " but the BAR says it is an ioport\n",
4588                                     device_get_nameunit(child), type, *rid);
4589                         goto out;
4590                 }
4591         }
4592
4593         /*
4594          * For real BARs, we need to override the size that
4595          * the driver requests, because that's what the BAR
4596          * actually uses and we would otherwise have a
4597          * situation where we might allocate the excess to
4598          * another driver, which won't work.
4599          */
4600         count = (pci_addr_t)1 << mapsize;
4601         if (RF_ALIGNMENT(flags) < mapsize)
4602                 flags = (flags & ~RF_ALIGNMENT_MASK) | RF_ALIGNMENT_LOG2(mapsize);
4603         if (PCI_BAR_MEM(map) && (map & PCIM_BAR_MEM_PREFETCH))
4604                 flags |= RF_PREFETCHABLE;
4605
4606         /*
4607          * Allocate enough resource, and then write back the
4608          * appropriate BAR for that resource.
4609          */
4610         resource_list_add(rl, type, *rid, start, end, count);
4611         res = resource_list_reserve(rl, dev, child, type, rid, start, end,
4612             count, flags & ~RF_ACTIVE);
4613         if (res == NULL) {
4614                 resource_list_delete(rl, type, *rid);
4615                 device_printf(child,
4616                     "%#lx bytes of rid %#x res %d failed (%#lx, %#lx).\n",
4617                     count, *rid, type, start, end);
4618                 goto out;
4619         }
4620         if (bootverbose)
4621                 device_printf(child,
4622                     "Lazy allocation of %#lx bytes rid %#x type %d at %#lx\n",
4623                     count, *rid, type, rman_get_start(res));
4624         map = rman_get_start(res);
4625         pci_write_bar(child, pm, map);
4626 out:
4627         return (res);
4628 }
4629
4630 struct resource *
4631 pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
4632                    u_long start, u_long end, u_long count, u_int flags)
4633 {
4634         struct pci_devinfo *dinfo;
4635         struct resource_list *rl;
4636         struct resource_list_entry *rle;
4637         struct resource *res;
4638         pcicfgregs *cfg;
4639
4640         if (device_get_parent(child) != dev)
4641                 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
4642                     type, rid, start, end, count, flags));
4643
4644         /*
4645          * Perform lazy resource allocation
4646          */
4647         dinfo = device_get_ivars(child);
4648         rl = &dinfo->resources;
4649         cfg = &dinfo->cfg;
4650         switch (type) {
4651 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
4652         case PCI_RES_BUS:
4653                 return (pci_alloc_secbus(dev, child, rid, start, end, count,
4654                     flags));
4655 #endif
4656         case SYS_RES_IRQ:
4657                 /*
4658                  * Can't alloc legacy interrupt once MSI messages have
4659                  * been allocated.
4660                  */
4661                 if (*rid == 0 && (cfg->msi.msi_alloc > 0 ||
4662                     cfg->msix.msix_alloc > 0))
4663                         return (NULL);
4664
4665                 /*
4666                  * If the child device doesn't have an interrupt
4667                  * routed and is deserving of an interrupt, try to
4668                  * assign it one.
4669                  */
4670                 if (*rid == 0 && !PCI_INTERRUPT_VALID(cfg->intline) &&
4671                     (cfg->intpin != 0))
4672                         pci_assign_interrupt(dev, child, 0);
4673                 break;
4674         case SYS_RES_IOPORT:
4675         case SYS_RES_MEMORY:
4676 #ifdef NEW_PCIB
4677                 /*
4678                  * PCI-PCI bridge I/O window resources are not BARs.
4679                  * For those allocations just pass the request up the
4680                  * tree.
4681                  */
4682                 if (cfg->hdrtype == PCIM_HDRTYPE_BRIDGE) {
4683                         switch (*rid) {
4684                         case PCIR_IOBASEL_1:
4685                         case PCIR_MEMBASE_1:
4686                         case PCIR_PMBASEL_1:
4687                                 /*
4688                                  * XXX: Should we bother creating a resource
4689                                  * list entry?
4690                                  */
4691                                 return (bus_generic_alloc_resource(dev, child,
4692                                     type, rid, start, end, count, flags));
4693                         }
4694                 }
4695 #endif
4696                 /* Reserve resources for this BAR if needed. */
4697                 rle = resource_list_find(rl, type, *rid);
4698                 if (rle == NULL) {
4699                         res = pci_reserve_map(dev, child, type, rid, start, end,
4700                             count, flags);
4701                         if (res == NULL)
4702                                 return (NULL);
4703                 }
4704         }
4705         return (resource_list_alloc(rl, dev, child, type, rid,
4706             start, end, count, flags));
4707 }
4708
4709 int
4710 pci_release_resource(device_t dev, device_t child, int type, int rid,
4711     struct resource *r)
4712 {
4713         struct pci_devinfo *dinfo;
4714         struct resource_list *rl;
4715         pcicfgregs *cfg;
4716
4717         if (device_get_parent(child) != dev)
4718                 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
4719                     type, rid, r));
4720
4721         dinfo = device_get_ivars(child);
4722         cfg = &dinfo->cfg;
4723 #ifdef NEW_PCIB
4724         /*
4725          * PCI-PCI bridge I/O window resources are not BARs.  For
4726          * those allocations just pass the request up the tree.
4727          */
4728         if (cfg->hdrtype == PCIM_HDRTYPE_BRIDGE &&
4729             (type == SYS_RES_IOPORT || type == SYS_RES_MEMORY)) {
4730                 switch (rid) {
4731                 case PCIR_IOBASEL_1:
4732                 case PCIR_MEMBASE_1:
4733                 case PCIR_PMBASEL_1:
4734                         return (bus_generic_release_resource(dev, child, type,
4735                             rid, r));
4736                 }
4737         }
4738 #endif
4739
4740         rl = &dinfo->resources;
4741         return (resource_list_release(rl, dev, child, type, rid, r));
4742 }
4743
4744 int
4745 pci_activate_resource(device_t dev, device_t child, int type, int rid,
4746     struct resource *r)
4747 {
4748         struct pci_devinfo *dinfo;
4749         int error;
4750
4751         error = bus_generic_activate_resource(dev, child, type, rid, r);
4752         if (error)
4753                 return (error);
4754
4755         /* Enable decoding in the command register when activating BARs. */
4756         if (device_get_parent(child) == dev) {
4757                 /* Device ROMs need their decoding explicitly enabled. */
4758                 dinfo = device_get_ivars(child);
4759                 if (type == SYS_RES_MEMORY && PCIR_IS_BIOS(&dinfo->cfg, rid))
4760                         pci_write_bar(child, pci_find_bar(child, rid),
4761                             rman_get_start(r) | PCIM_BIOS_ENABLE);
4762                 switch (type) {
4763                 case SYS_RES_IOPORT:
4764                 case SYS_RES_MEMORY:
4765                         error = PCI_ENABLE_IO(dev, child, type);
4766                         break;
4767                 }
4768         }
4769         return (error);
4770 }
4771
4772 int
4773 pci_deactivate_resource(device_t dev, device_t child, int type,
4774     int rid, struct resource *r)
4775 {
4776         struct pci_devinfo *dinfo;
4777         int error;
4778
4779         error = bus_generic_deactivate_resource(dev, child, type, rid, r);
4780         if (error)
4781                 return (error);
4782
4783         /* Disable decoding for device ROMs. */ 
4784         if (device_get_parent(child) == dev) {
4785                 dinfo = device_get_ivars(child);
4786                 if (type == SYS_RES_MEMORY && PCIR_IS_BIOS(&dinfo->cfg, rid))
4787                         pci_write_bar(child, pci_find_bar(child, rid),
4788                             rman_get_start(r));
4789         }
4790         return (0);
4791 }
4792
4793 void
4794 pci_delete_child(device_t dev, device_t child)
4795 {
4796         struct resource_list_entry *rle;
4797         struct resource_list *rl;
4798         struct pci_devinfo *dinfo;
4799
4800         dinfo = device_get_ivars(child);
4801         rl = &dinfo->resources;
4802
4803         if (device_is_attached(child))
4804                 device_detach(child);
4805
4806         /* Turn off access to resources we're about to free */
4807         pci_write_config(child, PCIR_COMMAND, pci_read_config(child,
4808             PCIR_COMMAND, 2) & ~(PCIM_CMD_MEMEN | PCIM_CMD_PORTEN), 2);
4809
4810         /* Free all allocated resources */
4811         STAILQ_FOREACH(rle, rl, link) {
4812                 if (rle->res) {
4813                         if (rman_get_flags(rle->res) & RF_ACTIVE ||
4814                             resource_list_busy(rl, rle->type, rle->rid)) {
4815                                 pci_printf(&dinfo->cfg,
4816                                     "Resource still owned, oops. "
4817                                     "(type=%d, rid=%d, addr=%lx)\n",
4818                                     rle->type, rle->rid,
4819                                     rman_get_start(rle->res));
4820                                 bus_release_resource(child, rle->type, rle->rid,
4821                                     rle->res);
4822                         }
4823                         resource_list_unreserve(rl, dev, child, rle->type,
4824                             rle->rid);
4825                 }
4826         }
4827         resource_list_free(rl);
4828
4829         device_delete_child(dev, child);
4830         pci_freecfg(dinfo);
4831 }
4832
4833 void
4834 pci_delete_resource(device_t dev, device_t child, int type, int rid)
4835 {
4836         struct pci_devinfo *dinfo;
4837         struct resource_list *rl;
4838         struct resource_list_entry *rle;
4839
4840         if (device_get_parent(child) != dev)
4841                 return;
4842
4843         dinfo = device_get_ivars(child);
4844         rl = &dinfo->resources;
4845         rle = resource_list_find(rl, type, rid);
4846         if (rle == NULL)
4847                 return;
4848
4849         if (rle->res) {
4850                 if (rman_get_flags(rle->res) & RF_ACTIVE ||
4851                     resource_list_busy(rl, type, rid)) {
4852                         device_printf(dev, "delete_resource: "
4853                             "Resource still owned by child, oops. "
4854                             "(type=%d, rid=%d, addr=%lx)\n",
4855                             type, rid, rman_get_start(rle->res));
4856                         return;
4857                 }
4858                 resource_list_unreserve(rl, dev, child, type, rid);
4859         }
4860         resource_list_delete(rl, type, rid);
4861 }
4862
4863 struct resource_list *
4864 pci_get_resource_list (device_t dev, device_t child)
4865 {
4866         struct pci_devinfo *dinfo = device_get_ivars(child);
4867
4868         return (&dinfo->resources);
4869 }
4870
4871 bus_dma_tag_t
4872 pci_get_dma_tag(device_t bus, device_t dev)
4873 {
4874         struct pci_softc *sc = device_get_softc(bus);
4875
4876         return (sc->sc_dma_tag);
4877 }
4878
4879 uint32_t
4880 pci_read_config_method(device_t dev, device_t child, int reg, int width)
4881 {
4882         struct pci_devinfo *dinfo = device_get_ivars(child);
4883         pcicfgregs *cfg = &dinfo->cfg;
4884
4885         return (PCIB_READ_CONFIG(device_get_parent(dev),
4886             cfg->bus, cfg->slot, cfg->func, reg, width));
4887 }
4888
4889 void
4890 pci_write_config_method(device_t dev, device_t child, int reg,
4891     uint32_t val, int width)
4892 {
4893         struct pci_devinfo *dinfo = device_get_ivars(child);
4894         pcicfgregs *cfg = &dinfo->cfg;
4895
4896         PCIB_WRITE_CONFIG(device_get_parent(dev),
4897             cfg->bus, cfg->slot, cfg->func, reg, val, width);
4898 }
4899
4900 int
4901 pci_child_location_str_method(device_t dev, device_t child, char *buf,
4902     size_t buflen)
4903 {
4904
4905         snprintf(buf, buflen, "slot=%d function=%d", pci_get_slot(child),
4906             pci_get_function(child));
4907         return (0);
4908 }
4909
4910 int
4911 pci_child_pnpinfo_str_method(device_t dev, device_t child, char *buf,
4912     size_t buflen)
4913 {
4914         struct pci_devinfo *dinfo;
4915         pcicfgregs *cfg;
4916
4917         dinfo = device_get_ivars(child);
4918         cfg = &dinfo->cfg;
4919         snprintf(buf, buflen, "vendor=0x%04x device=0x%04x subvendor=0x%04x "
4920             "subdevice=0x%04x class=0x%02x%02x%02x", cfg->vendor, cfg->device,
4921             cfg->subvendor, cfg->subdevice, cfg->baseclass, cfg->subclass,
4922             cfg->progif);
4923         return (0);
4924 }
4925
4926 int
4927 pci_assign_interrupt_method(device_t dev, device_t child)
4928 {
4929         struct pci_devinfo *dinfo = device_get_ivars(child);
4930         pcicfgregs *cfg = &dinfo->cfg;
4931
4932         return (PCIB_ROUTE_INTERRUPT(device_get_parent(dev), child,
4933             cfg->intpin));
4934 }
4935
4936 static int
4937 pci_modevent(module_t mod, int what, void *arg)
4938 {
4939         static struct cdev *pci_cdev;
4940
4941         switch (what) {
4942         case MOD_LOAD:
4943                 STAILQ_INIT(&pci_devq);
4944                 pci_generation = 0;
4945                 pci_cdev = make_dev(&pcicdev, 0, UID_ROOT, GID_WHEEL, 0644,
4946                     "pci");
4947                 pci_load_vendor_data();
4948                 break;
4949
4950         case MOD_UNLOAD:
4951                 destroy_dev(pci_cdev);
4952                 break;
4953         }
4954
4955         return (0);
4956 }
4957
4958 static void
4959 pci_cfg_restore_pcie(device_t dev, struct pci_devinfo *dinfo)
4960 {
4961 #define WREG(n, v)      pci_write_config(dev, pos + (n), (v), 2)
4962         struct pcicfg_pcie *cfg;
4963         int version, pos;
4964
4965         cfg = &dinfo->cfg.pcie;
4966         pos = cfg->pcie_location;
4967
4968         version = cfg->pcie_flags & PCIEM_FLAGS_VERSION;
4969
4970         WREG(PCIER_DEVICE_CTL, cfg->pcie_device_ctl);
4971
4972         if (version > 1 || cfg->pcie_type == PCIEM_TYPE_ROOT_PORT ||
4973             cfg->pcie_type == PCIEM_TYPE_ENDPOINT ||
4974             cfg->pcie_type == PCIEM_TYPE_LEGACY_ENDPOINT)
4975                 WREG(PCIER_LINK_CTL, cfg->pcie_link_ctl);
4976
4977         if (version > 1 || (cfg->pcie_type == PCIEM_TYPE_ROOT_PORT ||
4978             (cfg->pcie_type == PCIEM_TYPE_DOWNSTREAM_PORT &&
4979              (cfg->pcie_flags & PCIEM_FLAGS_SLOT))))
4980                 WREG(PCIER_SLOT_CTL, cfg->pcie_slot_ctl);
4981
4982         if (version > 1 || cfg->pcie_type == PCIEM_TYPE_ROOT_PORT ||
4983             cfg->pcie_type == PCIEM_TYPE_ROOT_EC)
4984                 WREG(PCIER_ROOT_CTL, cfg->pcie_root_ctl);
4985
4986         if (version > 1) {
4987                 WREG(PCIER_DEVICE_CTL2, cfg->pcie_device_ctl2);
4988                 WREG(PCIER_LINK_CTL2, cfg->pcie_link_ctl2);
4989                 WREG(PCIER_SLOT_CTL2, cfg->pcie_slot_ctl2);
4990         }
4991 #undef WREG
4992 }
4993
4994 static void
4995 pci_cfg_restore_pcix(device_t dev, struct pci_devinfo *dinfo)
4996 {
4997         pci_write_config(dev, dinfo->cfg.pcix.pcix_location + PCIXR_COMMAND,
4998             dinfo->cfg.pcix.pcix_command,  2);
4999 }
5000
5001 void
5002 pci_cfg_restore(device_t dev, struct pci_devinfo *dinfo)
5003 {
5004
5005         /*
5006          * Only do header type 0 devices.  Type 1 devices are bridges,
5007          * which we know need special treatment.  Type 2 devices are
5008          * cardbus bridges which also require special treatment.
5009          * Other types are unknown, and we err on the side of safety
5010          * by ignoring them.
5011          */
5012         if ((dinfo->cfg.hdrtype & PCIM_HDRTYPE) != PCIM_HDRTYPE_NORMAL)
5013                 return;
5014
5015         /*
5016          * Restore the device to full power mode.  We must do this
5017          * before we restore the registers because moving from D3 to
5018          * D0 will cause the chip's BARs and some other registers to
5019          * be reset to some unknown power on reset values.  Cut down
5020          * the noise on boot by doing nothing if we are already in
5021          * state D0.
5022          */
5023         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0)
5024                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
5025         pci_restore_bars(dev);
5026         pci_write_config(dev, PCIR_COMMAND, dinfo->cfg.cmdreg, 2);
5027         pci_write_config(dev, PCIR_INTLINE, dinfo->cfg.intline, 1);
5028         pci_write_config(dev, PCIR_INTPIN, dinfo->cfg.intpin, 1);
5029         pci_write_config(dev, PCIR_MINGNT, dinfo->cfg.mingnt, 1);
5030         pci_write_config(dev, PCIR_MAXLAT, dinfo->cfg.maxlat, 1);
5031         pci_write_config(dev, PCIR_CACHELNSZ, dinfo->cfg.cachelnsz, 1);
5032         pci_write_config(dev, PCIR_LATTIMER, dinfo->cfg.lattimer, 1);
5033         pci_write_config(dev, PCIR_PROGIF, dinfo->cfg.progif, 1);
5034         pci_write_config(dev, PCIR_REVID, dinfo->cfg.revid, 1);
5035
5036         /*
5037          * Restore extended capabilities for PCI-Express and PCI-X
5038          */
5039         if (dinfo->cfg.pcie.pcie_location != 0)
5040                 pci_cfg_restore_pcie(dev, dinfo);
5041         if (dinfo->cfg.pcix.pcix_location != 0)
5042                 pci_cfg_restore_pcix(dev, dinfo);
5043
5044         /* Restore MSI and MSI-X configurations if they are present. */
5045         if (dinfo->cfg.msi.msi_location != 0)
5046                 pci_resume_msi(dev);
5047         if (dinfo->cfg.msix.msix_location != 0)
5048                 pci_resume_msix(dev);
5049 }
5050
5051 static void
5052 pci_cfg_save_pcie(device_t dev, struct pci_devinfo *dinfo)
5053 {
5054 #define RREG(n) pci_read_config(dev, pos + (n), 2)
5055         struct pcicfg_pcie *cfg;
5056         int version, pos;
5057
5058         cfg = &dinfo->cfg.pcie;
5059         pos = cfg->pcie_location;
5060
5061         cfg->pcie_flags = RREG(PCIER_FLAGS);
5062
5063         version = cfg->pcie_flags & PCIEM_FLAGS_VERSION;
5064
5065         cfg->pcie_device_ctl = RREG(PCIER_DEVICE_CTL);
5066
5067         if (version > 1 || cfg->pcie_type == PCIEM_TYPE_ROOT_PORT ||
5068             cfg->pcie_type == PCIEM_TYPE_ENDPOINT ||
5069             cfg->pcie_type == PCIEM_TYPE_LEGACY_ENDPOINT)
5070                 cfg->pcie_link_ctl = RREG(PCIER_LINK_CTL);
5071
5072         if (version > 1 || (cfg->pcie_type == PCIEM_TYPE_ROOT_PORT ||
5073             (cfg->pcie_type == PCIEM_TYPE_DOWNSTREAM_PORT &&
5074              (cfg->pcie_flags & PCIEM_FLAGS_SLOT))))
5075                 cfg->pcie_slot_ctl = RREG(PCIER_SLOT_CTL);
5076
5077         if (version > 1 || cfg->pcie_type == PCIEM_TYPE_ROOT_PORT ||
5078             cfg->pcie_type == PCIEM_TYPE_ROOT_EC)
5079                 cfg->pcie_root_ctl = RREG(PCIER_ROOT_CTL);
5080
5081         if (version > 1) {
5082                 cfg->pcie_device_ctl2 = RREG(PCIER_DEVICE_CTL2);
5083                 cfg->pcie_link_ctl2 = RREG(PCIER_LINK_CTL2);
5084                 cfg->pcie_slot_ctl2 = RREG(PCIER_SLOT_CTL2);
5085         }
5086 #undef RREG
5087 }
5088
5089 static void
5090 pci_cfg_save_pcix(device_t dev, struct pci_devinfo *dinfo)
5091 {
5092         dinfo->cfg.pcix.pcix_command = pci_read_config(dev,
5093             dinfo->cfg.pcix.pcix_location + PCIXR_COMMAND, 2);
5094 }
5095
5096 void
5097 pci_cfg_save(device_t dev, struct pci_devinfo *dinfo, int setstate)
5098 {
5099         uint32_t cls;
5100         int ps;
5101
5102         /*
5103          * Only do header type 0 devices.  Type 1 devices are bridges, which
5104          * we know need special treatment.  Type 2 devices are cardbus bridges
5105          * which also require special treatment.  Other types are unknown, and
5106          * we err on the side of safety by ignoring them.  Powering down
5107          * bridges should not be undertaken lightly.
5108          */
5109         if ((dinfo->cfg.hdrtype & PCIM_HDRTYPE) != PCIM_HDRTYPE_NORMAL)
5110                 return;
5111
5112         /*
5113          * Some drivers apparently write to these registers w/o updating our
5114          * cached copy.  No harm happens if we update the copy, so do so here
5115          * so we can restore them.  The COMMAND register is modified by the
5116          * bus w/o updating the cache.  This should represent the normally
5117          * writable portion of the 'defined' part of type 0 headers.  In
5118          * theory we also need to save/restore the PCI capability structures
5119          * we know about, but apart from power we don't know any that are
5120          * writable.
5121          */
5122         dinfo->cfg.subvendor = pci_read_config(dev, PCIR_SUBVEND_0, 2);
5123         dinfo->cfg.subdevice = pci_read_config(dev, PCIR_SUBDEV_0, 2);
5124         dinfo->cfg.vendor = pci_read_config(dev, PCIR_VENDOR, 2);
5125         dinfo->cfg.device = pci_read_config(dev, PCIR_DEVICE, 2);
5126         dinfo->cfg.cmdreg = pci_read_config(dev, PCIR_COMMAND, 2);
5127         dinfo->cfg.intline = pci_read_config(dev, PCIR_INTLINE, 1);
5128         dinfo->cfg.intpin = pci_read_config(dev, PCIR_INTPIN, 1);
5129         dinfo->cfg.mingnt = pci_read_config(dev, PCIR_MINGNT, 1);
5130         dinfo->cfg.maxlat = pci_read_config(dev, PCIR_MAXLAT, 1);
5131         dinfo->cfg.cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
5132         dinfo->cfg.lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
5133         dinfo->cfg.baseclass = pci_read_config(dev, PCIR_CLASS, 1);
5134         dinfo->cfg.subclass = pci_read_config(dev, PCIR_SUBCLASS, 1);
5135         dinfo->cfg.progif = pci_read_config(dev, PCIR_PROGIF, 1);
5136         dinfo->cfg.revid = pci_read_config(dev, PCIR_REVID, 1);
5137
5138         if (dinfo->cfg.pcie.pcie_location != 0)
5139                 pci_cfg_save_pcie(dev, dinfo);
5140
5141         if (dinfo->cfg.pcix.pcix_location != 0)
5142                 pci_cfg_save_pcix(dev, dinfo);
5143
5144         /*
5145          * don't set the state for display devices, base peripherals and
5146          * memory devices since bad things happen when they are powered down.
5147          * We should (a) have drivers that can easily detach and (b) use
5148          * generic drivers for these devices so that some device actually
5149          * attaches.  We need to make sure that when we implement (a) we don't
5150          * power the device down on a reattach.
5151          */
5152         cls = pci_get_class(dev);
5153         if (!setstate)
5154                 return;
5155         switch (pci_do_power_nodriver)
5156         {
5157                 case 0:         /* NO powerdown at all */
5158                         return;
5159                 case 1:         /* Conservative about what to power down */
5160                         if (cls == PCIC_STORAGE)
5161                                 return;
5162                         /*FALLTHROUGH*/
5163                 case 2:         /* Agressive about what to power down */
5164                         if (cls == PCIC_DISPLAY || cls == PCIC_MEMORY ||
5165                             cls == PCIC_BASEPERIPH)
5166                                 return;
5167                         /*FALLTHROUGH*/
5168                 case 3:         /* Power down everything */
5169                         break;
5170         }
5171         /*
5172          * PCI spec says we can only go into D3 state from D0 state.
5173          * Transition from D[12] into D0 before going to D3 state.
5174          */
5175         ps = pci_get_powerstate(dev);
5176         if (ps != PCI_POWERSTATE_D0 && ps != PCI_POWERSTATE_D3)
5177                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
5178         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D3)
5179                 pci_set_powerstate(dev, PCI_POWERSTATE_D3);
5180 }
5181
5182 /* Wrapper APIs suitable for device driver use. */
5183 void
5184 pci_save_state(device_t dev)
5185 {
5186         struct pci_devinfo *dinfo;
5187
5188         dinfo = device_get_ivars(dev);
5189         pci_cfg_save(dev, dinfo, 0);
5190 }
5191
5192 void
5193 pci_restore_state(device_t dev)
5194 {
5195         struct pci_devinfo *dinfo;
5196
5197         dinfo = device_get_ivars(dev);
5198         pci_cfg_restore(dev, dinfo);
5199 }
5200
5201 static uint16_t
5202 pci_get_rid_method(device_t dev, device_t child)
5203 {
5204
5205         return (PCIB_GET_RID(device_get_parent(dev), child));
5206 }
5207
5208 /* Find the upstream port of a given PCI device in a root complex. */
5209 device_t
5210 pci_find_pcie_root_port(device_t dev)
5211 {
5212         struct pci_devinfo *dinfo;
5213         devclass_t pci_class;
5214         device_t pcib, bus;
5215
5216         pci_class = devclass_find("pci");
5217         KASSERT(device_get_devclass(device_get_parent(dev)) == pci_class,
5218             ("%s: non-pci device %s", __func__, device_get_nameunit(dev)));
5219
5220         /*
5221          * Walk the bridge hierarchy until we find a PCI-e root
5222          * port or a non-PCI device.
5223          */
5224         for (;;) {
5225                 bus = device_get_parent(dev);
5226                 KASSERT(bus != NULL, ("%s: null parent of %s", __func__,
5227                     device_get_nameunit(dev)));
5228
5229                 pcib = device_get_parent(bus);
5230                 KASSERT(pcib != NULL, ("%s: null bridge of %s", __func__,
5231                     device_get_nameunit(bus)));
5232
5233                 /*
5234                  * pcib's parent must be a PCI bus for this to be a
5235                  * PCI-PCI bridge.
5236                  */
5237                 if (device_get_devclass(device_get_parent(pcib)) != pci_class)
5238                         return (NULL);
5239
5240                 dinfo = device_get_ivars(pcib);
5241                 if (dinfo->cfg.pcie.pcie_location != 0 &&
5242                     dinfo->cfg.pcie.pcie_type == PCIEM_TYPE_ROOT_PORT)
5243                         return (pcib);
5244
5245                 dev = pcib;
5246         }
5247 }