pci: Remove unused MSI remap method
[dragonfly.git] / sys / bus / pci / pci.c
1 /*-
2  * Copyright (c) 1997, Stefan Esser <se@kfreebsd.org>
3  * Copyright (c) 2000, Michael Smith <msmith@kfreebsd.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  * $FreeBSD: src/sys/dev/pci/pci.c,v 1.355.2.9.2.1 2009/04/15 03:14:26 kensmith Exp $
29  */
30
31 #include "opt_bus.h"
32 #include "opt_acpi.h"
33 #include "opt_compat_oldpci.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/linker.h>
40 #include <sys/fcntl.h>
41 #include <sys/conf.h>
42 #include <sys/kernel.h>
43 #include <sys/queue.h>
44 #include <sys/sysctl.h>
45 #include <sys/endian.h>
46 #include <sys/machintr.h>
47
48 #include <machine/msi_machdep.h>
49
50 #include <vm/vm.h>
51 #include <vm/pmap.h>
52 #include <vm/vm_extern.h>
53
54 #include <sys/bus.h>
55 #include <sys/rman.h>
56 #include <sys/device.h>
57
58 #include <sys/pciio.h>
59 #include <bus/pci/pcireg.h>
60 #include <bus/pci/pcivar.h>
61 #include <bus/pci/pci_private.h>
62
63 #include "pcib_if.h"
64 #include "pci_if.h"
65
66 #ifdef __HAVE_ACPI
67 #include <contrib/dev/acpica/acpi.h>
68 #include "acpi_if.h"
69 #else
70 #define ACPI_PWR_FOR_SLEEP(x, y, z)
71 #endif
72
73 extern struct dev_ops pcic_ops; /* XXX */
74
75 typedef void    (*pci_read_cap_t)(device_t, int, int, pcicfgregs *);
76
77 static uint32_t         pci_mapbase(unsigned mapreg);
78 static const char       *pci_maptype(unsigned mapreg);
79 static int              pci_mapsize(unsigned testval);
80 static int              pci_maprange(unsigned mapreg);
81 static void             pci_fixancient(pcicfgregs *cfg);
82
83 static int              pci_porten(device_t pcib, int b, int s, int f);
84 static int              pci_memen(device_t pcib, int b, int s, int f);
85 static void             pci_assign_interrupt(device_t bus, device_t dev,
86                             int force_route);
87 static int              pci_add_map(device_t pcib, device_t bus, device_t dev,
88                             int b, int s, int f, int reg,
89                             struct resource_list *rl, int force, int prefetch);
90 static int              pci_probe(device_t dev);
91 static int              pci_attach(device_t dev);
92 static void             pci_child_detached(device_t, device_t);
93 static void             pci_load_vendor_data(void);
94 static int              pci_describe_parse_line(char **ptr, int *vendor,
95                             int *device, char **desc);
96 static char             *pci_describe_device(device_t dev);
97 static int              pci_modevent(module_t mod, int what, void *arg);
98 static void             pci_hdrtypedata(device_t pcib, int b, int s, int f,
99                             pcicfgregs *cfg);
100 static void             pci_read_capabilities(device_t pcib, pcicfgregs *cfg);
101 static int              pci_read_vpd_reg(device_t pcib, pcicfgregs *cfg,
102                             int reg, uint32_t *data);
103 #if 0
104 static int              pci_write_vpd_reg(device_t pcib, pcicfgregs *cfg,
105                             int reg, uint32_t data);
106 #endif
107 static void             pci_read_vpd(device_t pcib, pcicfgregs *cfg);
108 static void             pci_disable_msi(device_t dev);
109 static void             pci_enable_msi(device_t dev, uint64_t address,
110                             uint16_t data);
111 static void             pci_enable_msix(device_t dev, u_int index,
112                             uint64_t address, uint32_t data);
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 void             pci_resume_msi(device_t dev);
117 static void             pci_resume_msix(device_t dev);
118 static int              pcie_slotimpl(const pcicfgregs *);
119 static void             pci_print_verbose_expr(const pcicfgregs *);
120
121 static void             pci_read_cap_pmgt(device_t, int, int, pcicfgregs *);
122 static void             pci_read_cap_ht(device_t, int, int, pcicfgregs *);
123 static void             pci_read_cap_msi(device_t, int, int, pcicfgregs *);
124 static void             pci_read_cap_msix(device_t, int, int, pcicfgregs *);
125 static void             pci_read_cap_vpd(device_t, int, int, pcicfgregs *);
126 static void             pci_read_cap_subvendor(device_t, int, int,
127                             pcicfgregs *);
128 static void             pci_read_cap_pcix(device_t, int, int, pcicfgregs *);
129 static void             pci_read_cap_express(device_t, int, int, pcicfgregs *);
130
131 static device_method_t pci_methods[] = {
132         /* Device interface */
133         DEVMETHOD(device_probe,         pci_probe),
134         DEVMETHOD(device_attach,        pci_attach),
135         DEVMETHOD(device_detach,        bus_generic_detach),
136         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
137         DEVMETHOD(device_suspend,       pci_suspend),
138         DEVMETHOD(device_resume,        pci_resume),
139
140         /* Bus interface */
141         DEVMETHOD(bus_print_child,      pci_print_child),
142         DEVMETHOD(bus_probe_nomatch,    pci_probe_nomatch),
143         DEVMETHOD(bus_read_ivar,        pci_read_ivar),
144         DEVMETHOD(bus_write_ivar,       pci_write_ivar),
145         DEVMETHOD(bus_driver_added,     pci_driver_added),
146         DEVMETHOD(bus_child_detached,   pci_child_detached),
147         DEVMETHOD(bus_setup_intr,       pci_setup_intr),
148         DEVMETHOD(bus_teardown_intr,    pci_teardown_intr),
149
150         DEVMETHOD(bus_get_resource_list,pci_get_resource_list),
151         DEVMETHOD(bus_set_resource,     bus_generic_rl_set_resource),
152         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
153         DEVMETHOD(bus_delete_resource,  pci_delete_resource),
154         DEVMETHOD(bus_alloc_resource,   pci_alloc_resource),
155         DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
156         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
157         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
158         DEVMETHOD(bus_child_pnpinfo_str, pci_child_pnpinfo_str_method),
159         DEVMETHOD(bus_child_location_str, pci_child_location_str_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_extcap,      pci_find_extcap_method),
174         DEVMETHOD(pci_alloc_msi,        pci_alloc_msi_method),
175         DEVMETHOD(pci_alloc_msix,       pci_alloc_msix_method),
176         DEVMETHOD(pci_release_msi,      pci_release_msi_method),
177         DEVMETHOD(pci_msi_count,        pci_msi_count_method),
178         DEVMETHOD(pci_msix_count,       pci_msix_count_method),
179
180         { 0, 0 }
181 };
182
183 DEFINE_CLASS_0(pci, pci_driver, pci_methods, 0);
184
185 static devclass_t pci_devclass;
186 DRIVER_MODULE(pci, pcib, pci_driver, pci_devclass, pci_modevent, NULL);
187 MODULE_VERSION(pci, 1);
188
189 static char     *pci_vendordata;
190 static size_t   pci_vendordata_size;
191
192
193 static const struct pci_read_cap {
194         int             cap;
195         pci_read_cap_t  read_cap;
196 } pci_read_caps[] = {
197         { PCIY_PMG,             pci_read_cap_pmgt },
198         { PCIY_HT,              pci_read_cap_ht },
199         { PCIY_MSI,             pci_read_cap_msi },
200         { PCIY_MSIX,            pci_read_cap_msix },
201         { PCIY_VPD,             pci_read_cap_vpd },
202         { PCIY_SUBVENDOR,       pci_read_cap_subvendor },
203         { PCIY_PCIX,            pci_read_cap_pcix },
204         { PCIY_EXPRESS,         pci_read_cap_express },
205         { 0, NULL } /* required last entry */
206 };
207
208 struct pci_quirk {
209         uint32_t devid; /* Vendor/device of the card */
210         int     type;
211 #define PCI_QUIRK_MAP_REG       1 /* PCI map register in weird place */
212 #define PCI_QUIRK_DISABLE_MSI   2 /* MSI/MSI-X doesn't work */
213         int     arg1;
214         int     arg2;
215 };
216
217 struct pci_quirk pci_quirks[] = {
218         /* The Intel 82371AB and 82443MX has a map register at offset 0x90. */
219         { 0x71138086, PCI_QUIRK_MAP_REG,        0x90,    0 },
220         { 0x719b8086, PCI_QUIRK_MAP_REG,        0x90,    0 },
221         /* As does the Serverworks OSB4 (the SMBus mapping register) */
222         { 0x02001166, PCI_QUIRK_MAP_REG,        0x90,    0 },
223
224         /*
225          * MSI doesn't work with the ServerWorks CNB20-HE Host Bridge
226          * or the CMIC-SL (AKA ServerWorks GC_LE).
227          */
228         { 0x00141166, PCI_QUIRK_DISABLE_MSI,    0,      0 },
229         { 0x00171166, PCI_QUIRK_DISABLE_MSI,    0,      0 },
230
231         /*
232          * MSI doesn't work on earlier Intel chipsets including
233          * E7500, E7501, E7505, 845, 865, 875/E7210, and 855.
234          */
235         { 0x25408086, PCI_QUIRK_DISABLE_MSI,    0,      0 },
236         { 0x254c8086, PCI_QUIRK_DISABLE_MSI,    0,      0 },
237         { 0x25508086, PCI_QUIRK_DISABLE_MSI,    0,      0 },
238         { 0x25608086, PCI_QUIRK_DISABLE_MSI,    0,      0 },
239         { 0x25708086, PCI_QUIRK_DISABLE_MSI,    0,      0 },
240         { 0x25788086, PCI_QUIRK_DISABLE_MSI,    0,      0 },
241         { 0x35808086, PCI_QUIRK_DISABLE_MSI,    0,      0 },
242
243         /*
244          * MSI doesn't work with devices behind the AMD 8131 HT-PCIX
245          * bridge.
246          */
247         { 0x74501022, PCI_QUIRK_DISABLE_MSI,    0,      0 },
248
249         { 0 }
250 };
251
252 /* map register information */
253 #define PCI_MAPMEM      0x01    /* memory map */
254 #define PCI_MAPMEMP     0x02    /* prefetchable memory map */
255 #define PCI_MAPPORT     0x04    /* port map */
256
257 struct devlist pci_devq;
258 uint32_t pci_generation;
259 uint32_t pci_numdevs = 0;
260 static int pcie_chipset, pcix_chipset;
261
262 /* sysctl vars */
263 SYSCTL_NODE(_hw, OID_AUTO, pci, CTLFLAG_RD, 0, "PCI bus tuning parameters");
264
265 static int pci_enable_io_modes = 1;
266 TUNABLE_INT("hw.pci.enable_io_modes", &pci_enable_io_modes);
267 SYSCTL_INT(_hw_pci, OID_AUTO, enable_io_modes, CTLFLAG_RW,
268     &pci_enable_io_modes, 1,
269     "Enable I/O and memory bits in the config register.  Some BIOSes do not\n\
270 enable these bits correctly.  We'd like to do this all the time, but there\n\
271 are some peripherals that this causes problems with.");
272
273 static int pci_do_power_nodriver = 0;
274 TUNABLE_INT("hw.pci.do_power_nodriver", &pci_do_power_nodriver);
275 SYSCTL_INT(_hw_pci, OID_AUTO, do_power_nodriver, CTLFLAG_RW,
276     &pci_do_power_nodriver, 0,
277   "Place a function into D3 state when no driver attaches to it.  0 means\n\
278 disable.  1 means conservatively place devices into D3 state.  2 means\n\
279 aggressively place devices into D3 state.  3 means put absolutely everything\n\
280 in D3 state.");
281
282 static int pci_do_power_resume = 1;
283 TUNABLE_INT("hw.pci.do_power_resume", &pci_do_power_resume);
284 SYSCTL_INT(_hw_pci, OID_AUTO, do_power_resume, CTLFLAG_RW,
285     &pci_do_power_resume, 1,
286   "Transition from D3 -> D0 on resume.");
287
288 static int pci_do_msi = 1;
289 TUNABLE_INT("hw.pci.enable_msi", &pci_do_msi);
290 SYSCTL_INT(_hw_pci, OID_AUTO, enable_msi, CTLFLAG_RW, &pci_do_msi, 1,
291     "Enable support for MSI interrupts");
292
293 static int pci_do_msix = 0;
294 #if 0
295 TUNABLE_INT("hw.pci.enable_msix", &pci_do_msix);
296 SYSCTL_INT(_hw_pci, OID_AUTO, enable_msix, CTLFLAG_RW, &pci_do_msix, 1,
297     "Enable support for MSI-X interrupts");
298 #endif
299
300 static int pci_honor_msi_blacklist = 1;
301 TUNABLE_INT("hw.pci.honor_msi_blacklist", &pci_honor_msi_blacklist);
302 SYSCTL_INT(_hw_pci, OID_AUTO, honor_msi_blacklist, CTLFLAG_RD,
303     &pci_honor_msi_blacklist, 1, "Honor chipset blacklist for MSI");
304
305 static int pci_msi_cpuid;
306
307 /* Find a device_t by bus/slot/function in domain 0 */
308
309 device_t
310 pci_find_bsf(uint8_t bus, uint8_t slot, uint8_t func)
311 {
312
313         return (pci_find_dbsf(0, bus, slot, func));
314 }
315
316 /* Find a device_t by domain/bus/slot/function */
317
318 device_t
319 pci_find_dbsf(uint32_t domain, uint8_t bus, uint8_t slot, uint8_t func)
320 {
321         struct pci_devinfo *dinfo;
322
323         STAILQ_FOREACH(dinfo, &pci_devq, pci_links) {
324                 if ((dinfo->cfg.domain == domain) &&
325                     (dinfo->cfg.bus == bus) &&
326                     (dinfo->cfg.slot == slot) &&
327                     (dinfo->cfg.func == func)) {
328                         return (dinfo->cfg.dev);
329                 }
330         }
331
332         return (NULL);
333 }
334
335 /* Find a device_t by vendor/device ID */
336
337 device_t
338 pci_find_device(uint16_t vendor, uint16_t device)
339 {
340         struct pci_devinfo *dinfo;
341
342         STAILQ_FOREACH(dinfo, &pci_devq, pci_links) {
343                 if ((dinfo->cfg.vendor == vendor) &&
344                     (dinfo->cfg.device == device)) {
345                         return (dinfo->cfg.dev);
346                 }
347         }
348
349         return (NULL);
350 }
351
352 /* return base address of memory or port map */
353
354 static uint32_t
355 pci_mapbase(uint32_t mapreg)
356 {
357
358         if (PCI_BAR_MEM(mapreg))
359                 return (mapreg & PCIM_BAR_MEM_BASE);
360         else
361                 return (mapreg & PCIM_BAR_IO_BASE);
362 }
363
364 /* return map type of memory or port map */
365
366 static const char *
367 pci_maptype(unsigned mapreg)
368 {
369
370         if (PCI_BAR_IO(mapreg))
371                 return ("I/O Port");
372         if (mapreg & PCIM_BAR_MEM_PREFETCH)
373                 return ("Prefetchable Memory");
374         return ("Memory");
375 }
376
377 /* return log2 of map size decoded for memory or port map */
378
379 static int
380 pci_mapsize(uint32_t testval)
381 {
382         int ln2size;
383
384         testval = pci_mapbase(testval);
385         ln2size = 0;
386         if (testval != 0) {
387                 while ((testval & 1) == 0)
388                 {
389                         ln2size++;
390                         testval >>= 1;
391                 }
392         }
393         return (ln2size);
394 }
395
396 /* return log2 of address range supported by map register */
397
398 static int
399 pci_maprange(unsigned mapreg)
400 {
401         int ln2range = 0;
402
403         if (PCI_BAR_IO(mapreg))
404                 ln2range = 32;
405         else
406                 switch (mapreg & PCIM_BAR_MEM_TYPE) {
407                 case PCIM_BAR_MEM_32:
408                         ln2range = 32;
409                         break;
410                 case PCIM_BAR_MEM_1MB:
411                         ln2range = 20;
412                         break;
413                 case PCIM_BAR_MEM_64:
414                         ln2range = 64;
415                         break;
416                 }
417         return (ln2range);
418 }
419
420 /* adjust some values from PCI 1.0 devices to match 2.0 standards ... */
421
422 static void
423 pci_fixancient(pcicfgregs *cfg)
424 {
425         if (cfg->hdrtype != 0)
426                 return;
427
428         /* PCI to PCI bridges use header type 1 */
429         if (cfg->baseclass == PCIC_BRIDGE && cfg->subclass == PCIS_BRIDGE_PCI)
430                 cfg->hdrtype = 1;
431 }
432
433 /* extract header type specific config data */
434
435 static void
436 pci_hdrtypedata(device_t pcib, int b, int s, int f, pcicfgregs *cfg)
437 {
438 #define REG(n, w)       PCIB_READ_CONFIG(pcib, b, s, f, n, w)
439         switch (cfg->hdrtype) {
440         case 0:
441                 cfg->subvendor      = REG(PCIR_SUBVEND_0, 2);
442                 cfg->subdevice      = REG(PCIR_SUBDEV_0, 2);
443                 cfg->nummaps        = PCI_MAXMAPS_0;
444                 break;
445         case 1:
446                 cfg->nummaps        = PCI_MAXMAPS_1;
447 #ifdef COMPAT_OLDPCI
448                 cfg->secondarybus   = REG(PCIR_SECBUS_1, 1);
449 #endif
450                 break;
451         case 2:
452                 cfg->subvendor      = REG(PCIR_SUBVEND_2, 2);
453                 cfg->subdevice      = REG(PCIR_SUBDEV_2, 2);
454                 cfg->nummaps        = PCI_MAXMAPS_2;
455 #ifdef COMPAT_OLDPCI
456                 cfg->secondarybus   = REG(PCIR_SECBUS_2, 1);
457 #endif
458                 break;
459         }
460 #undef REG
461 }
462
463 /* read configuration header into pcicfgregs structure */
464 struct pci_devinfo *
465 pci_read_device(device_t pcib, int d, int b, int s, int f, size_t size)
466 {
467 #define REG(n, w)       PCIB_READ_CONFIG(pcib, b, s, f, n, w)
468         pcicfgregs *cfg = NULL;
469         struct pci_devinfo *devlist_entry;
470         struct devlist *devlist_head;
471
472         devlist_head = &pci_devq;
473
474         devlist_entry = NULL;
475
476         if (REG(PCIR_DEVVENDOR, 4) != -1) {
477                 devlist_entry = kmalloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
478
479                 cfg = &devlist_entry->cfg;
480
481                 cfg->domain             = d;
482                 cfg->bus                = b;
483                 cfg->slot               = s;
484                 cfg->func               = f;
485                 cfg->vendor             = REG(PCIR_VENDOR, 2);
486                 cfg->device             = REG(PCIR_DEVICE, 2);
487                 cfg->cmdreg             = REG(PCIR_COMMAND, 2);
488                 cfg->statreg            = REG(PCIR_STATUS, 2);
489                 cfg->baseclass          = REG(PCIR_CLASS, 1);
490                 cfg->subclass           = REG(PCIR_SUBCLASS, 1);
491                 cfg->progif             = REG(PCIR_PROGIF, 1);
492                 cfg->revid              = REG(PCIR_REVID, 1);
493                 cfg->hdrtype            = REG(PCIR_HDRTYPE, 1);
494                 cfg->cachelnsz          = REG(PCIR_CACHELNSZ, 1);
495                 cfg->lattimer           = REG(PCIR_LATTIMER, 1);
496                 cfg->intpin             = REG(PCIR_INTPIN, 1);
497                 cfg->intline            = REG(PCIR_INTLINE, 1);
498
499                 cfg->mingnt             = REG(PCIR_MINGNT, 1);
500                 cfg->maxlat             = REG(PCIR_MAXLAT, 1);
501
502                 cfg->mfdev              = (cfg->hdrtype & PCIM_MFDEV) != 0;
503                 cfg->hdrtype            &= ~PCIM_MFDEV;
504
505                 pci_fixancient(cfg);
506                 pci_hdrtypedata(pcib, b, s, f, cfg);
507
508                 pci_read_capabilities(pcib, cfg);
509
510                 STAILQ_INSERT_TAIL(devlist_head, devlist_entry, pci_links);
511
512                 devlist_entry->conf.pc_sel.pc_domain = cfg->domain;
513                 devlist_entry->conf.pc_sel.pc_bus = cfg->bus;
514                 devlist_entry->conf.pc_sel.pc_dev = cfg->slot;
515                 devlist_entry->conf.pc_sel.pc_func = cfg->func;
516                 devlist_entry->conf.pc_hdr = cfg->hdrtype;
517
518                 devlist_entry->conf.pc_subvendor = cfg->subvendor;
519                 devlist_entry->conf.pc_subdevice = cfg->subdevice;
520                 devlist_entry->conf.pc_vendor = cfg->vendor;
521                 devlist_entry->conf.pc_device = cfg->device;
522
523                 devlist_entry->conf.pc_class = cfg->baseclass;
524                 devlist_entry->conf.pc_subclass = cfg->subclass;
525                 devlist_entry->conf.pc_progif = cfg->progif;
526                 devlist_entry->conf.pc_revid = cfg->revid;
527
528                 pci_numdevs++;
529                 pci_generation++;
530         }
531         return (devlist_entry);
532 #undef REG
533 }
534
535 static int
536 pci_fixup_nextptr(int *nextptr0)
537 {
538         int nextptr = *nextptr0;
539
540         /* "Next pointer" is only one byte */
541         KASSERT(nextptr <= 0xff, ("Illegal next pointer %d\n", nextptr));
542
543         if (nextptr & 0x3) {
544                 /*
545                  * PCI local bus spec 3.0:
546                  *
547                  * "... The bottom two bits of all pointers are reserved
548                  *  and must be implemented as 00b although software must
549                  *  mask them to allow for future uses of these bits ..."
550                  */
551                 if (bootverbose) {
552                         kprintf("Illegal PCI extended capability "
553                                 "offset, fixup 0x%02x -> 0x%02x\n",
554                                 nextptr, nextptr & ~0x3);
555                 }
556                 nextptr &= ~0x3;
557         }
558         *nextptr0 = nextptr;
559
560         if (nextptr < 0x40) {
561                 if (nextptr != 0) {
562                         kprintf("Illegal PCI extended capability "
563                                 "offset 0x%02x", nextptr);
564                 }
565                 return 0;
566         }
567         return 1;
568 }
569
570 static void
571 pci_read_cap_pmgt(device_t pcib, int ptr, int nextptr, pcicfgregs *cfg)
572 {
573 #define REG(n, w)       \
574         PCIB_READ_CONFIG(pcib, cfg->bus, cfg->slot, cfg->func, n, w)
575
576         struct pcicfg_pp *pp = &cfg->pp;
577
578         if (pp->pp_cap)
579                 return;
580
581         pp->pp_cap = REG(ptr + PCIR_POWER_CAP, 2);
582         pp->pp_status = ptr + PCIR_POWER_STATUS;
583         pp->pp_pmcsr = ptr + PCIR_POWER_PMCSR;
584
585         if ((nextptr - ptr) > PCIR_POWER_DATA) {
586                 /*
587                  * XXX
588                  * We should write to data_select and read back from
589                  * data_scale to determine whether data register is
590                  * implemented.
591                  */
592 #ifdef foo
593                 pp->pp_data = ptr + PCIR_POWER_DATA;
594 #else
595                 pp->pp_data = 0;
596 #endif
597         }
598
599 #undef REG
600 }
601
602 static void
603 pci_read_cap_ht(device_t pcib, int ptr, int nextptr, pcicfgregs *cfg)
604 {
605 #if defined(__i386__) || defined(__x86_64__)
606
607 #define REG(n, w)       \
608         PCIB_READ_CONFIG(pcib, cfg->bus, cfg->slot, cfg->func, n, w)
609
610         struct pcicfg_ht *ht = &cfg->ht;
611         uint64_t addr;
612         uint32_t val;
613
614         /* Determine HT-specific capability type. */
615         val = REG(ptr + PCIR_HT_COMMAND, 2);
616
617         if ((val & 0xe000) == PCIM_HTCAP_SLAVE)
618                 cfg->ht.ht_slave = ptr;
619
620         if ((val & PCIM_HTCMD_CAP_MASK) != PCIM_HTCAP_MSI_MAPPING)
621                 return;
622
623         if (!(val & PCIM_HTCMD_MSI_FIXED)) {
624                 /* Sanity check the mapping window. */
625                 addr = REG(ptr + PCIR_HTMSI_ADDRESS_HI, 4);
626                 addr <<= 32;
627                 addr |= REG(ptr + PCIR_HTMSI_ADDRESS_LO, 4);
628                 if (addr != MSI_X86_ADDR_BASE) {
629                         device_printf(pcib, "HT Bridge at pci%d:%d:%d:%d "
630                                 "has non-default MSI window 0x%llx\n",
631                                 cfg->domain, cfg->bus, cfg->slot, cfg->func,
632                                 (long long)addr);
633                 }
634         } else {
635                 addr = MSI_X86_ADDR_BASE;
636         }
637
638         ht->ht_msimap = ptr;
639         ht->ht_msictrl = val;
640         ht->ht_msiaddr = addr;
641
642 #undef REG
643
644 #endif  /* __i386__ || __x86_64__ */
645 }
646
647 static void
648 pci_read_cap_msi(device_t pcib, int ptr, int nextptr, pcicfgregs *cfg)
649 {
650 #define REG(n, w)       \
651         PCIB_READ_CONFIG(pcib, cfg->bus, cfg->slot, cfg->func, n, w)
652
653         struct pcicfg_msi *msi = &cfg->msi;
654
655         msi->msi_location = ptr;
656         msi->msi_ctrl = REG(ptr + PCIR_MSI_CTRL, 2);
657         msi->msi_msgnum = 1 << ((msi->msi_ctrl & PCIM_MSICTRL_MMC_MASK) >> 1);
658
659 #undef REG
660 }
661
662 static void
663 pci_read_cap_msix(device_t pcib, int ptr, int nextptr, pcicfgregs *cfg)
664 {
665 #define REG(n, w)       \
666         PCIB_READ_CONFIG(pcib, cfg->bus, cfg->slot, cfg->func, n, w)
667
668         struct pcicfg_msix *msix = &cfg->msix;
669         uint32_t val;
670
671         msix->msix_location = ptr;
672         msix->msix_ctrl = REG(ptr + PCIR_MSIX_CTRL, 2);
673         msix->msix_msgnum = (msix->msix_ctrl & PCIM_MSIXCTRL_TABLE_SIZE) + 1;
674
675         val = REG(ptr + PCIR_MSIX_TABLE, 4);
676         msix->msix_table_bar = PCIR_BAR(val & PCIM_MSIX_BIR_MASK);
677         msix->msix_table_offset = val & ~PCIM_MSIX_BIR_MASK;
678
679         val = REG(ptr + PCIR_MSIX_PBA, 4);
680         msix->msix_pba_bar = PCIR_BAR(val & PCIM_MSIX_BIR_MASK);
681         msix->msix_pba_offset = val & ~PCIM_MSIX_BIR_MASK;
682
683 #undef REG
684 }
685
686 static void
687 pci_read_cap_vpd(device_t pcib, int ptr, int nextptr, pcicfgregs *cfg)
688 {
689         cfg->vpd.vpd_reg = ptr;
690 }
691
692 static void
693 pci_read_cap_subvendor(device_t pcib, int ptr, int nextptr, pcicfgregs *cfg)
694 {
695 #define REG(n, w)       \
696         PCIB_READ_CONFIG(pcib, cfg->bus, cfg->slot, cfg->func, n, w)
697
698         /* Should always be true. */
699         if ((cfg->hdrtype & PCIM_HDRTYPE) == 1) {
700                 uint32_t val;
701
702                 val = REG(ptr + PCIR_SUBVENDCAP_ID, 4);
703                 cfg->subvendor = val & 0xffff;
704                 cfg->subdevice = val >> 16;
705         }
706
707 #undef REG
708 }
709
710 static void
711 pci_read_cap_pcix(device_t pcib, int ptr, int nextptr, pcicfgregs *cfg)
712 {
713         /*
714          * Assume we have a PCI-X chipset if we have
715          * at least one PCI-PCI bridge with a PCI-X
716          * capability.  Note that some systems with
717          * PCI-express or HT chipsets might match on
718          * this check as well.
719          */
720         if ((cfg->hdrtype & PCIM_HDRTYPE) == 1)
721                 pcix_chipset = 1;
722
723         cfg->pcix.pcix_ptr = ptr;
724 }
725
726 static int
727 pcie_slotimpl(const pcicfgregs *cfg)
728 {
729         const struct pcicfg_expr *expr = &cfg->expr;
730         uint16_t port_type;
731
732         /*
733          * Only version 1 can be parsed currently 
734          */
735         if ((expr->expr_cap & PCIEM_CAP_VER_MASK) != PCIEM_CAP_VER_1)
736                 return 0;
737
738         /*
739          * - Slot implemented bit is meaningful iff current port is
740          *   root port or down stream port.
741          * - Testing for root port or down stream port is meanningful
742          *   iff PCI configure has type 1 header.
743          */
744
745         if (cfg->hdrtype != 1)
746                 return 0;
747
748         port_type = expr->expr_cap & PCIEM_CAP_PORT_TYPE;
749         if (port_type != PCIE_ROOT_PORT && port_type != PCIE_DOWN_STREAM_PORT)
750                 return 0;
751
752         if (!(expr->expr_cap & PCIEM_CAP_SLOT_IMPL))
753                 return 0;
754
755         return 1;
756 }
757
758 static void
759 pci_read_cap_express(device_t pcib, int ptr, int nextptr, pcicfgregs *cfg)
760 {
761 #define REG(n, w)       \
762         PCIB_READ_CONFIG(pcib, cfg->bus, cfg->slot, cfg->func, n, w)
763
764         struct pcicfg_expr *expr = &cfg->expr;
765
766         /*
767          * Assume we have a PCI-express chipset if we have
768          * at least one PCI-express device.
769          */
770         pcie_chipset = 1;
771
772         expr->expr_ptr = ptr;
773         expr->expr_cap = REG(ptr + PCIER_CAPABILITY, 2);
774
775         /*
776          * Only version 1 can be parsed currently 
777          */
778         if ((expr->expr_cap & PCIEM_CAP_VER_MASK) != PCIEM_CAP_VER_1)
779                 return;
780
781         /*
782          * Read slot capabilities.  Slot capabilities exists iff
783          * current port's slot is implemented
784          */
785         if (pcie_slotimpl(cfg))
786                 expr->expr_slotcap = REG(ptr + PCIER_SLOTCAP, 4);
787
788 #undef REG
789 }
790
791 static void
792 pci_read_capabilities(device_t pcib, pcicfgregs *cfg)
793 {
794 #define REG(n, w)       PCIB_READ_CONFIG(pcib, cfg->bus, cfg->slot, cfg->func, n, w)
795 #define WREG(n, v, w)   PCIB_WRITE_CONFIG(pcib, cfg->bus, cfg->slot, cfg->func, n, v, w)
796
797         uint32_t val;
798         int nextptr, ptrptr;
799
800         if ((REG(PCIR_STATUS, 2) & PCIM_STATUS_CAPPRESENT) == 0) {
801                 /* No capabilities */
802                 return;
803         }
804
805         switch (cfg->hdrtype & PCIM_HDRTYPE) {
806         case 0:
807         case 1:
808                 ptrptr = PCIR_CAP_PTR;
809                 break;
810         case 2:
811                 ptrptr = PCIR_CAP_PTR_2;        /* cardbus capabilities ptr */
812                 break;
813         default:
814                 return;                         /* no capabilities support */
815         }
816         nextptr = REG(ptrptr, 1);       /* sanity check? */
817
818         /*
819          * Read capability entries.
820          */
821         while (pci_fixup_nextptr(&nextptr)) {
822                 const struct pci_read_cap *rc;
823                 int ptr = nextptr;
824
825                 /* Find the next entry */
826                 nextptr = REG(ptr + PCICAP_NEXTPTR, 1);
827
828                 /* Process this entry */
829                 val = REG(ptr + PCICAP_ID, 1);
830                 for (rc = pci_read_caps; rc->read_cap != NULL; ++rc) {
831                         if (rc->cap == val) {
832                                 rc->read_cap(pcib, ptr, nextptr, cfg);
833                                 break;
834                         }
835                 }
836         }
837
838 #if defined(__i386__) || defined(__x86_64__)
839         /*
840          * Enable the MSI mapping window for all HyperTransport
841          * slaves.  PCI-PCI bridges have their windows enabled via
842          * PCIB_MAP_MSI().
843          */
844         if (cfg->ht.ht_slave != 0 && cfg->ht.ht_msimap != 0 &&
845             !(cfg->ht.ht_msictrl & PCIM_HTCMD_MSI_ENABLE)) {
846                 device_printf(pcib,
847             "Enabling MSI window for HyperTransport slave at pci%d:%d:%d:%d\n",
848                     cfg->domain, cfg->bus, cfg->slot, cfg->func);
849                  cfg->ht.ht_msictrl |= PCIM_HTCMD_MSI_ENABLE;
850                  WREG(cfg->ht.ht_msimap + PCIR_HT_COMMAND, cfg->ht.ht_msictrl,
851                      2);
852         }
853 #endif
854
855 /* REG and WREG use carry through to next functions */
856 }
857
858 /*
859  * PCI Vital Product Data
860  */
861
862 #define PCI_VPD_TIMEOUT         1000000
863
864 static int
865 pci_read_vpd_reg(device_t pcib, pcicfgregs *cfg, int reg, uint32_t *data)
866 {
867         int count = PCI_VPD_TIMEOUT;
868
869         KASSERT((reg & 3) == 0, ("VPD register must by 4 byte aligned"));
870
871         WREG(cfg->vpd.vpd_reg + PCIR_VPD_ADDR, reg, 2);
872
873         while ((REG(cfg->vpd.vpd_reg + PCIR_VPD_ADDR, 2) & 0x8000) != 0x8000) {
874                 if (--count < 0)
875                         return (ENXIO);
876                 DELAY(1);       /* limit looping */
877         }
878         *data = (REG(cfg->vpd.vpd_reg + PCIR_VPD_DATA, 4));
879
880         return (0);
881 }
882
883 #if 0
884 static int
885 pci_write_vpd_reg(device_t pcib, pcicfgregs *cfg, int reg, uint32_t data)
886 {
887         int count = PCI_VPD_TIMEOUT;
888
889         KASSERT((reg & 3) == 0, ("VPD register must by 4 byte aligned"));
890
891         WREG(cfg->vpd.vpd_reg + PCIR_VPD_DATA, data, 4);
892         WREG(cfg->vpd.vpd_reg + PCIR_VPD_ADDR, reg | 0x8000, 2);
893         while ((REG(cfg->vpd.vpd_reg + PCIR_VPD_ADDR, 2) & 0x8000) == 0x8000) {
894                 if (--count < 0)
895                         return (ENXIO);
896                 DELAY(1);       /* limit looping */
897         }
898
899         return (0);
900 }
901 #endif
902
903 #undef PCI_VPD_TIMEOUT
904
905 struct vpd_readstate {
906         device_t        pcib;
907         pcicfgregs      *cfg;
908         uint32_t        val;
909         int             bytesinval;
910         int             off;
911         uint8_t         cksum;
912 };
913
914 static int
915 vpd_nextbyte(struct vpd_readstate *vrs, uint8_t *data)
916 {
917         uint32_t reg;
918         uint8_t byte;
919
920         if (vrs->bytesinval == 0) {
921                 if (pci_read_vpd_reg(vrs->pcib, vrs->cfg, vrs->off, &reg))
922                         return (ENXIO);
923                 vrs->val = le32toh(reg);
924                 vrs->off += 4;
925                 byte = vrs->val & 0xff;
926                 vrs->bytesinval = 3;
927         } else {
928                 vrs->val = vrs->val >> 8;
929                 byte = vrs->val & 0xff;
930                 vrs->bytesinval--;
931         }
932
933         vrs->cksum += byte;
934         *data = byte;
935         return (0);
936 }
937
938 int
939 pcie_slot_implemented(device_t dev)
940 {
941         struct pci_devinfo *dinfo = device_get_ivars(dev);
942
943         return pcie_slotimpl(&dinfo->cfg);
944 }
945
946 void
947 pcie_set_max_readrq(device_t dev, uint16_t rqsize)
948 {
949         uint8_t expr_ptr;
950         uint16_t val;
951
952         rqsize &= PCIEM_DEVCTL_MAX_READRQ_MASK;
953         if (rqsize > PCIEM_DEVCTL_MAX_READRQ_4096) {
954                 panic("%s: invalid max read request size 0x%02x\n",
955                       device_get_nameunit(dev), rqsize);
956         }
957
958         expr_ptr = pci_get_pciecap_ptr(dev);
959         if (!expr_ptr)
960                 panic("%s: not PCIe device\n", device_get_nameunit(dev));
961
962         val = pci_read_config(dev, expr_ptr + PCIER_DEVCTRL, 2);
963         if ((val & PCIEM_DEVCTL_MAX_READRQ_MASK) != rqsize) {
964                 if (bootverbose)
965                         device_printf(dev, "adjust device control 0x%04x", val);
966
967                 val &= ~PCIEM_DEVCTL_MAX_READRQ_MASK;
968                 val |= rqsize;
969                 pci_write_config(dev, expr_ptr + PCIER_DEVCTRL, val, 2);
970
971                 if (bootverbose)
972                         kprintf(" -> 0x%04x\n", val);
973         }
974 }
975
976 uint16_t
977 pcie_get_max_readrq(device_t dev)
978 {
979         uint8_t expr_ptr;
980         uint16_t val;
981
982         expr_ptr = pci_get_pciecap_ptr(dev);
983         if (!expr_ptr)
984                 panic("%s: not PCIe device\n", device_get_nameunit(dev));
985
986         val = pci_read_config(dev, expr_ptr + PCIER_DEVCTRL, 2);
987         return (val & PCIEM_DEVCTL_MAX_READRQ_MASK);
988 }
989
990 static void
991 pci_read_vpd(device_t pcib, pcicfgregs *cfg)
992 {
993         struct vpd_readstate vrs;
994         int state;
995         int name;
996         int remain;
997         int i;
998         int alloc, off;         /* alloc/off for RO/W arrays */
999         int cksumvalid;
1000         int dflen;
1001         uint8_t byte;
1002         uint8_t byte2;
1003
1004         /* init vpd reader */
1005         vrs.bytesinval = 0;
1006         vrs.off = 0;
1007         vrs.pcib = pcib;
1008         vrs.cfg = cfg;
1009         vrs.cksum = 0;
1010
1011         state = 0;
1012         name = remain = i = 0;  /* shut up stupid gcc */
1013         alloc = off = 0;        /* shut up stupid gcc */
1014         dflen = 0;              /* shut up stupid gcc */
1015         cksumvalid = -1;
1016         while (state >= 0) {
1017                 if (vpd_nextbyte(&vrs, &byte)) {
1018                         state = -2;
1019                         break;
1020                 }
1021 #if 0
1022                 kprintf("vpd: val: %#x, off: %d, bytesinval: %d, byte: %#hhx, " \
1023                     "state: %d, remain: %d, name: %#x, i: %d\n", vrs.val,
1024                     vrs.off, vrs.bytesinval, byte, state, remain, name, i);
1025 #endif
1026                 switch (state) {
1027                 case 0:         /* item name */
1028                         if (byte & 0x80) {
1029                                 if (vpd_nextbyte(&vrs, &byte2)) {
1030                                         state = -2;
1031                                         break;
1032                                 }
1033                                 remain = byte2;
1034                                 if (vpd_nextbyte(&vrs, &byte2)) {
1035                                         state = -2;
1036                                         break;
1037                                 }
1038                                 remain |= byte2 << 8;
1039                                 if (remain > (0x7f*4 - vrs.off)) {
1040                                         state = -1;
1041                                         kprintf(
1042                             "pci%d:%d:%d:%d: invalid VPD data, remain %#x\n",
1043                                             cfg->domain, cfg->bus, cfg->slot,
1044                                             cfg->func, remain);
1045                                 }
1046                                 name = byte & 0x7f;
1047                         } else {
1048                                 remain = byte & 0x7;
1049                                 name = (byte >> 3) & 0xf;
1050                         }
1051                         switch (name) {
1052                         case 0x2:       /* String */
1053                                 cfg->vpd.vpd_ident = kmalloc(remain + 1,
1054                                     M_DEVBUF, M_WAITOK);
1055                                 i = 0;
1056                                 state = 1;
1057                                 break;
1058                         case 0xf:       /* End */
1059                                 state = -1;
1060                                 break;
1061                         case 0x10:      /* VPD-R */
1062                                 alloc = 8;
1063                                 off = 0;
1064                                 cfg->vpd.vpd_ros = kmalloc(alloc *
1065                                     sizeof(*cfg->vpd.vpd_ros), M_DEVBUF,
1066                                     M_WAITOK | M_ZERO);
1067                                 state = 2;
1068                                 break;
1069                         case 0x11:      /* VPD-W */
1070                                 alloc = 8;
1071                                 off = 0;
1072                                 cfg->vpd.vpd_w = kmalloc(alloc *
1073                                     sizeof(*cfg->vpd.vpd_w), M_DEVBUF,
1074                                     M_WAITOK | M_ZERO);
1075                                 state = 5;
1076                                 break;
1077                         default:        /* Invalid data, abort */
1078                                 state = -1;
1079                                 break;
1080                         }
1081                         break;
1082
1083                 case 1: /* Identifier String */
1084                         cfg->vpd.vpd_ident[i++] = byte;
1085                         remain--;
1086                         if (remain == 0)  {
1087                                 cfg->vpd.vpd_ident[i] = '\0';
1088                                 state = 0;
1089                         }
1090                         break;
1091
1092                 case 2: /* VPD-R Keyword Header */
1093                         if (off == alloc) {
1094                                 cfg->vpd.vpd_ros = krealloc(cfg->vpd.vpd_ros,
1095                                     (alloc *= 2) * sizeof(*cfg->vpd.vpd_ros),
1096                                     M_DEVBUF, M_WAITOK | M_ZERO);
1097                         }
1098                         cfg->vpd.vpd_ros[off].keyword[0] = byte;
1099                         if (vpd_nextbyte(&vrs, &byte2)) {
1100                                 state = -2;
1101                                 break;
1102                         }
1103                         cfg->vpd.vpd_ros[off].keyword[1] = byte2;
1104                         if (vpd_nextbyte(&vrs, &byte2)) {
1105                                 state = -2;
1106                                 break;
1107                         }
1108                         dflen = byte2;
1109                         if (dflen == 0 &&
1110                             strncmp(cfg->vpd.vpd_ros[off].keyword, "RV",
1111                             2) == 0) {
1112                                 /*
1113                                  * if this happens, we can't trust the rest
1114                                  * of the VPD.
1115                                  */
1116                                 kprintf(
1117                                     "pci%d:%d:%d:%d: bad keyword length: %d\n",
1118                                     cfg->domain, cfg->bus, cfg->slot,
1119                                     cfg->func, dflen);
1120                                 cksumvalid = 0;
1121                                 state = -1;
1122                                 break;
1123                         } else if (dflen == 0) {
1124                                 cfg->vpd.vpd_ros[off].value = kmalloc(1 *
1125                                     sizeof(*cfg->vpd.vpd_ros[off].value),
1126                                     M_DEVBUF, M_WAITOK);
1127                                 cfg->vpd.vpd_ros[off].value[0] = '\x00';
1128                         } else
1129                                 cfg->vpd.vpd_ros[off].value = kmalloc(
1130                                     (dflen + 1) *
1131                                     sizeof(*cfg->vpd.vpd_ros[off].value),
1132                                     M_DEVBUF, M_WAITOK);
1133                         remain -= 3;
1134                         i = 0;
1135                         /* keep in sync w/ state 3's transistions */
1136                         if (dflen == 0 && remain == 0)
1137                                 state = 0;
1138                         else if (dflen == 0)
1139                                 state = 2;
1140                         else
1141                                 state = 3;
1142                         break;
1143
1144                 case 3: /* VPD-R Keyword Value */
1145                         cfg->vpd.vpd_ros[off].value[i++] = byte;
1146                         if (strncmp(cfg->vpd.vpd_ros[off].keyword,
1147                             "RV", 2) == 0 && cksumvalid == -1) {
1148                                 if (vrs.cksum == 0)
1149                                         cksumvalid = 1;
1150                                 else {
1151                                         if (bootverbose)
1152                                                 kprintf(
1153                                 "pci%d:%d:%d:%d: bad VPD cksum, remain %hhu\n",
1154                                                     cfg->domain, cfg->bus,
1155                                                     cfg->slot, cfg->func,
1156                                                     vrs.cksum);
1157                                         cksumvalid = 0;
1158                                         state = -1;
1159                                         break;
1160                                 }
1161                         }
1162                         dflen--;
1163                         remain--;
1164                         /* keep in sync w/ state 2's transistions */
1165                         if (dflen == 0)
1166                                 cfg->vpd.vpd_ros[off++].value[i++] = '\0';
1167                         if (dflen == 0 && remain == 0) {
1168                                 cfg->vpd.vpd_rocnt = off;
1169                                 cfg->vpd.vpd_ros = krealloc(cfg->vpd.vpd_ros,
1170                                     off * sizeof(*cfg->vpd.vpd_ros),
1171                                     M_DEVBUF, M_WAITOK | M_ZERO);
1172                                 state = 0;
1173                         } else if (dflen == 0)
1174                                 state = 2;
1175                         break;
1176
1177                 case 4:
1178                         remain--;
1179                         if (remain == 0)
1180                                 state = 0;
1181                         break;
1182
1183                 case 5: /* VPD-W Keyword Header */
1184                         if (off == alloc) {
1185                                 cfg->vpd.vpd_w = krealloc(cfg->vpd.vpd_w,
1186                                     (alloc *= 2) * sizeof(*cfg->vpd.vpd_w),
1187                                     M_DEVBUF, M_WAITOK | M_ZERO);
1188                         }
1189                         cfg->vpd.vpd_w[off].keyword[0] = byte;
1190                         if (vpd_nextbyte(&vrs, &byte2)) {
1191                                 state = -2;
1192                                 break;
1193                         }
1194                         cfg->vpd.vpd_w[off].keyword[1] = byte2;
1195                         if (vpd_nextbyte(&vrs, &byte2)) {
1196                                 state = -2;
1197                                 break;
1198                         }
1199                         cfg->vpd.vpd_w[off].len = dflen = byte2;
1200                         cfg->vpd.vpd_w[off].start = vrs.off - vrs.bytesinval;
1201                         cfg->vpd.vpd_w[off].value = kmalloc((dflen + 1) *
1202                             sizeof(*cfg->vpd.vpd_w[off].value),
1203                             M_DEVBUF, M_WAITOK);
1204                         remain -= 3;
1205                         i = 0;
1206                         /* keep in sync w/ state 6's transistions */
1207                         if (dflen == 0 && remain == 0)
1208                                 state = 0;
1209                         else if (dflen == 0)
1210                                 state = 5;
1211                         else
1212                                 state = 6;
1213                         break;
1214
1215                 case 6: /* VPD-W Keyword Value */
1216                         cfg->vpd.vpd_w[off].value[i++] = byte;
1217                         dflen--;
1218                         remain--;
1219                         /* keep in sync w/ state 5's transistions */
1220                         if (dflen == 0)
1221                                 cfg->vpd.vpd_w[off++].value[i++] = '\0';
1222                         if (dflen == 0 && remain == 0) {
1223                                 cfg->vpd.vpd_wcnt = off;
1224                                 cfg->vpd.vpd_w = krealloc(cfg->vpd.vpd_w,
1225                                     off * sizeof(*cfg->vpd.vpd_w),
1226                                     M_DEVBUF, M_WAITOK | M_ZERO);
1227                                 state = 0;
1228                         } else if (dflen == 0)
1229                                 state = 5;
1230                         break;
1231
1232                 default:
1233                         kprintf("pci%d:%d:%d:%d: invalid state: %d\n",
1234                             cfg->domain, cfg->bus, cfg->slot, cfg->func,
1235                             state);
1236                         state = -1;
1237                         break;
1238                 }
1239         }
1240
1241         if (cksumvalid == 0 || state < -1) {
1242                 /* read-only data bad, clean up */
1243                 if (cfg->vpd.vpd_ros != NULL) {
1244                         for (off = 0; cfg->vpd.vpd_ros[off].value; off++)
1245                                 kfree(cfg->vpd.vpd_ros[off].value, M_DEVBUF);
1246                         kfree(cfg->vpd.vpd_ros, M_DEVBUF);
1247                         cfg->vpd.vpd_ros = NULL;
1248                 }
1249         }
1250         if (state < -1) {
1251                 /* I/O error, clean up */
1252                 kprintf("pci%d:%d:%d:%d: failed to read VPD data.\n",
1253                     cfg->domain, cfg->bus, cfg->slot, cfg->func);
1254                 if (cfg->vpd.vpd_ident != NULL) {
1255                         kfree(cfg->vpd.vpd_ident, M_DEVBUF);
1256                         cfg->vpd.vpd_ident = NULL;
1257                 }
1258                 if (cfg->vpd.vpd_w != NULL) {
1259                         for (off = 0; cfg->vpd.vpd_w[off].value; off++)
1260                                 kfree(cfg->vpd.vpd_w[off].value, M_DEVBUF);
1261                         kfree(cfg->vpd.vpd_w, M_DEVBUF);
1262                         cfg->vpd.vpd_w = NULL;
1263                 }
1264         }
1265         cfg->vpd.vpd_cached = 1;
1266 #undef REG
1267 #undef WREG
1268 }
1269
1270 int
1271 pci_get_vpd_ident_method(device_t dev, device_t child, const char **identptr)
1272 {
1273         struct pci_devinfo *dinfo = device_get_ivars(child);
1274         pcicfgregs *cfg = &dinfo->cfg;
1275
1276         if (!cfg->vpd.vpd_cached && cfg->vpd.vpd_reg != 0)
1277                 pci_read_vpd(device_get_parent(dev), cfg);
1278
1279         *identptr = cfg->vpd.vpd_ident;
1280
1281         if (*identptr == NULL)
1282                 return (ENXIO);
1283
1284         return (0);
1285 }
1286
1287 int
1288 pci_get_vpd_readonly_method(device_t dev, device_t child, const char *kw,
1289         const char **vptr)
1290 {
1291         struct pci_devinfo *dinfo = device_get_ivars(child);
1292         pcicfgregs *cfg = &dinfo->cfg;
1293         int i;
1294
1295         if (!cfg->vpd.vpd_cached && cfg->vpd.vpd_reg != 0)
1296                 pci_read_vpd(device_get_parent(dev), cfg);
1297
1298         for (i = 0; i < cfg->vpd.vpd_rocnt; i++)
1299                 if (memcmp(kw, cfg->vpd.vpd_ros[i].keyword,
1300                     sizeof(cfg->vpd.vpd_ros[i].keyword)) == 0) {
1301                         *vptr = cfg->vpd.vpd_ros[i].value;
1302                 }
1303
1304         if (i != cfg->vpd.vpd_rocnt)
1305                 return (0);
1306
1307         *vptr = NULL;
1308         return (ENXIO);
1309 }
1310
1311 /*
1312  * Return the offset in configuration space of the requested extended
1313  * capability entry or 0 if the specified capability was not found.
1314  */
1315 int
1316 pci_find_extcap_method(device_t dev, device_t child, int capability,
1317     int *capreg)
1318 {
1319         struct pci_devinfo *dinfo = device_get_ivars(child);
1320         pcicfgregs *cfg = &dinfo->cfg;
1321         u_int32_t status;
1322         u_int8_t ptr;
1323
1324         /*
1325          * Check the CAP_LIST bit of the PCI status register first.
1326          */
1327         status = pci_read_config(child, PCIR_STATUS, 2);
1328         if (!(status & PCIM_STATUS_CAPPRESENT))
1329                 return (ENXIO);
1330
1331         /*
1332          * Determine the start pointer of the capabilities list.
1333          */
1334         switch (cfg->hdrtype & PCIM_HDRTYPE) {
1335         case 0:
1336         case 1:
1337                 ptr = PCIR_CAP_PTR;
1338                 break;
1339         case 2:
1340                 ptr = PCIR_CAP_PTR_2;
1341                 break;
1342         default:
1343                 /* XXX: panic? */
1344                 return (ENXIO);         /* no extended capabilities support */
1345         }
1346         ptr = pci_read_config(child, ptr, 1);
1347
1348         /*
1349          * Traverse the capabilities list.
1350          */
1351         while (ptr != 0) {
1352                 if (pci_read_config(child, ptr + PCICAP_ID, 1) == capability) {
1353                         if (capreg != NULL)
1354                                 *capreg = ptr;
1355                         return (0);
1356                 }
1357                 ptr = pci_read_config(child, ptr + PCICAP_NEXTPTR, 1);
1358         }
1359
1360         return (ENOENT);
1361 }
1362
1363 /*
1364  * Support for MSI-X message interrupts.
1365  */
1366 void
1367 pci_enable_msix(device_t dev, u_int index, uint64_t address, uint32_t data)
1368 {
1369         struct pci_devinfo *dinfo = device_get_ivars(dev);
1370         struct pcicfg_msix *msix = &dinfo->cfg.msix;
1371         uint32_t offset;
1372
1373         KASSERT(msix->msix_table_len > index, ("bogus index"));
1374         offset = msix->msix_table_offset + index * 16;
1375         bus_write_4(msix->msix_table_res, offset, address & 0xffffffff);
1376         bus_write_4(msix->msix_table_res, offset + 4, address >> 32);
1377         bus_write_4(msix->msix_table_res, offset + 8, data);
1378
1379         /* Enable MSI -> HT mapping. */
1380         pci_ht_map_msi(dev, address);
1381 }
1382
1383 void
1384 pci_mask_msix(device_t dev, u_int index)
1385 {
1386         struct pci_devinfo *dinfo = device_get_ivars(dev);
1387         struct pcicfg_msix *msix = &dinfo->cfg.msix;
1388         uint32_t offset, val;
1389
1390         KASSERT(msix->msix_msgnum > index, ("bogus index"));
1391         offset = msix->msix_table_offset + index * 16 + 12;
1392         val = bus_read_4(msix->msix_table_res, offset);
1393         if (!(val & PCIM_MSIX_VCTRL_MASK)) {
1394                 val |= PCIM_MSIX_VCTRL_MASK;
1395                 bus_write_4(msix->msix_table_res, offset, val);
1396         }
1397 }
1398
1399 void
1400 pci_unmask_msix(device_t dev, u_int index)
1401 {
1402         struct pci_devinfo *dinfo = device_get_ivars(dev);
1403         struct pcicfg_msix *msix = &dinfo->cfg.msix;
1404         uint32_t offset, val;
1405
1406         KASSERT(msix->msix_table_len > index, ("bogus index"));
1407         offset = msix->msix_table_offset + index * 16 + 12;
1408         val = bus_read_4(msix->msix_table_res, offset);
1409         if (val & PCIM_MSIX_VCTRL_MASK) {
1410                 val &= ~PCIM_MSIX_VCTRL_MASK;
1411                 bus_write_4(msix->msix_table_res, offset, val);
1412         }
1413 }
1414
1415 int
1416 pci_pending_msix(device_t dev, u_int index)
1417 {
1418         struct pci_devinfo *dinfo = device_get_ivars(dev);
1419         struct pcicfg_msix *msix = &dinfo->cfg.msix;
1420         uint32_t offset, bit;
1421
1422         KASSERT(msix->msix_table_len > index, ("bogus index"));
1423         offset = msix->msix_pba_offset + (index / 32) * 4;
1424         bit = 1 << index % 32;
1425         return (bus_read_4(msix->msix_pba_res, offset) & bit);
1426 }
1427
1428 /*
1429  * Restore MSI-X registers and table during resume.  If MSI-X is
1430  * enabled then walk the virtual table to restore the actual MSI-X
1431  * table.
1432  */
1433 static void
1434 pci_resume_msix(device_t dev)
1435 {
1436         struct pci_devinfo *dinfo = device_get_ivars(dev);
1437         struct pcicfg_msix *msix = &dinfo->cfg.msix;
1438         struct msix_table_entry *mte;
1439         struct msix_vector *mv;
1440         int i;
1441
1442         if (msix->msix_alloc > 0) {
1443                 /* First, mask all vectors. */
1444                 for (i = 0; i < msix->msix_msgnum; i++)
1445                         pci_mask_msix(dev, i);
1446
1447                 /* Second, program any messages with at least one handler. */
1448                 for (i = 0; i < msix->msix_table_len; i++) {
1449                         mte = &msix->msix_table[i];
1450                         if (mte->mte_vector == 0 || mte->mte_handlers == 0)
1451                                 continue;
1452                         mv = &msix->msix_vectors[mte->mte_vector - 1];
1453                         pci_enable_msix(dev, i, mv->mv_address, mv->mv_data);
1454                         pci_unmask_msix(dev, i);
1455                 }
1456         }
1457         pci_write_config(dev, msix->msix_location + PCIR_MSIX_CTRL,
1458             msix->msix_ctrl, 2);
1459 }
1460
1461 /*
1462  * Attempt to allocate *count MSI-X messages.  The actual number allocated is
1463  * returned in *count.  After this function returns, each message will be
1464  * available to the driver as SYS_RES_IRQ resources starting at rid 1.
1465  */
1466 int
1467 pci_alloc_msix_method(device_t dev, device_t child, int *count)
1468 {
1469         struct pci_devinfo *dinfo = device_get_ivars(child);
1470         pcicfgregs *cfg = &dinfo->cfg;
1471         struct resource_list_entry *rle;
1472         int actual, error, i, irq, max;
1473
1474         /* Don't let count == 0 get us into trouble. */
1475         if (*count == 0)
1476                 return (EINVAL);
1477
1478         /* If rid 0 is allocated, then fail. */
1479         rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, 0);
1480         if (rle != NULL && rle->res != NULL)
1481                 return (ENXIO);
1482
1483         /* Already have allocated messages? */
1484         if (cfg->msi.msi_alloc != 0 || cfg->msix.msix_alloc != 0)
1485                 return (ENXIO);
1486
1487         /* If MSI is blacklisted for this system, fail. */
1488         if (pci_msi_blacklisted())
1489                 return (ENXIO);
1490
1491         /* MSI-X capability present? */
1492         if (cfg->msix.msix_location == 0 || !pci_do_msix)
1493                 return (ENODEV);
1494
1495         /* Make sure the appropriate BARs are mapped. */
1496         rle = resource_list_find(&dinfo->resources, SYS_RES_MEMORY,
1497             cfg->msix.msix_table_bar);
1498         if (rle == NULL || rle->res == NULL ||
1499             !(rman_get_flags(rle->res) & RF_ACTIVE))
1500                 return (ENXIO);
1501         cfg->msix.msix_table_res = rle->res;
1502         if (cfg->msix.msix_pba_bar != cfg->msix.msix_table_bar) {
1503                 rle = resource_list_find(&dinfo->resources, SYS_RES_MEMORY,
1504                     cfg->msix.msix_pba_bar);
1505                 if (rle == NULL || rle->res == NULL ||
1506                     !(rman_get_flags(rle->res) & RF_ACTIVE))
1507                         return (ENXIO);
1508         }
1509         cfg->msix.msix_pba_res = rle->res;
1510
1511         if (bootverbose)
1512                 device_printf(child,
1513                     "attempting to allocate %d MSI-X vectors (%d supported)\n",
1514                     *count, cfg->msix.msix_msgnum);
1515         max = min(*count, cfg->msix.msix_msgnum);
1516         for (i = 0; i < max; i++) {
1517                 /* Allocate a message. */
1518                 error = PCIB_ALLOC_MSIX(device_get_parent(dev), child, &irq);
1519                 if (error)
1520                         break;
1521                 resource_list_add(&dinfo->resources, SYS_RES_IRQ, i + 1, irq,
1522                     irq, 1, -1);
1523         }
1524         actual = i;
1525
1526         if (actual == 0) {
1527                 if (bootverbose) {
1528                         device_printf(child,
1529                             "could not allocate any MSI-X vectors\n");
1530                 }
1531                 return  (ENXIO);
1532         }
1533
1534         if (bootverbose) {
1535                 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, 1);
1536                 if (actual == 1)
1537                         device_printf(child, "using IRQ %lu for MSI-X\n",
1538                             rle->start);
1539                 else {
1540                         int run;
1541
1542                         /*
1543                          * Be fancy and try to print contiguous runs of
1544                          * IRQ values as ranges.  'irq' is the previous IRQ.
1545                          * 'run' is true if we are in a range.
1546                          */
1547                         device_printf(child, "using IRQs %lu", rle->start);
1548                         irq = rle->start;
1549                         run = 0;
1550                         for (i = 1; i < actual; i++) {
1551                                 rle = resource_list_find(&dinfo->resources,
1552                                     SYS_RES_IRQ, i + 1);
1553
1554                                 /* Still in a run? */
1555                                 if (rle->start == irq + 1) {
1556                                         run = 1;
1557                                         irq++;
1558                                         continue;
1559                                 }
1560
1561                                 /* Finish previous range. */
1562                                 if (run) {
1563                                         kprintf("-%d", irq);
1564                                         run = 0;
1565                                 }
1566
1567                                 /* Start new range. */
1568                                 kprintf(",%lu", rle->start);
1569                                 irq = rle->start;
1570                         }
1571
1572                         /* Unfinished range? */
1573                         if (run)
1574                                 kprintf("-%d", irq);
1575                         kprintf(" for MSI-X\n");
1576                 }
1577         }
1578
1579         /* Mask all vectors. */
1580         for (i = 0; i < cfg->msix.msix_msgnum; i++)
1581                 pci_mask_msix(child, i);
1582
1583         /* Allocate and initialize vector data and virtual table. */
1584         cfg->msix.msix_vectors = kmalloc(sizeof(struct msix_vector) * actual,
1585             M_DEVBUF, M_WAITOK | M_ZERO);
1586         cfg->msix.msix_table = kmalloc(sizeof(struct msix_table_entry) * actual,
1587             M_DEVBUF, M_WAITOK | M_ZERO);
1588         for (i = 0; i < actual; i++) {
1589                 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, i + 1);
1590                 cfg->msix.msix_vectors[i].mv_irq = rle->start;
1591                 cfg->msix.msix_table[i].mte_vector = i + 1;
1592         }
1593
1594         /* Update control register to enable MSI-X. */
1595         cfg->msix.msix_ctrl |= PCIM_MSIXCTRL_MSIX_ENABLE;
1596         pci_write_config(child, cfg->msix.msix_location + PCIR_MSIX_CTRL,
1597             cfg->msix.msix_ctrl, 2);
1598
1599         /* Update counts of alloc'd messages. */
1600         cfg->msix.msix_alloc = actual;
1601         cfg->msix.msix_table_len = actual;
1602         *count = actual;
1603         return (0);
1604 }
1605
1606 static int
1607 pci_release_msix(device_t dev, device_t child)
1608 {
1609         struct pci_devinfo *dinfo = device_get_ivars(child);
1610         struct pcicfg_msix *msix = &dinfo->cfg.msix;
1611         struct resource_list_entry *rle;
1612         int i;
1613
1614         /* Do we have any messages to release? */
1615         if (msix->msix_alloc == 0)
1616                 return (ENODEV);
1617
1618         /* Make sure none of the resources are allocated. */
1619         for (i = 0; i < msix->msix_table_len; i++) {
1620                 if (msix->msix_table[i].mte_vector == 0)
1621                         continue;
1622                 if (msix->msix_table[i].mte_handlers > 0)
1623                         return (EBUSY);
1624                 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, i + 1);
1625                 KASSERT(rle != NULL, ("missing resource"));
1626                 if (rle->res != NULL)
1627                         return (EBUSY);
1628         }
1629
1630         /* Update control register to disable MSI-X. */
1631         msix->msix_ctrl &= ~PCIM_MSIXCTRL_MSIX_ENABLE;
1632         pci_write_config(child, msix->msix_location + PCIR_MSIX_CTRL,
1633             msix->msix_ctrl, 2);
1634
1635         /* Free the resource list entries. */
1636         for (i = 0; i < msix->msix_table_len; i++) {
1637                 if (msix->msix_table[i].mte_vector == 0)
1638                         continue;
1639                 resource_list_delete(&dinfo->resources, SYS_RES_IRQ, i + 1);
1640         }
1641         kfree(msix->msix_table, M_DEVBUF);
1642         msix->msix_table_len = 0;
1643
1644         /* Release the IRQs. */
1645         for (i = 0; i < msix->msix_alloc; i++)
1646                 PCIB_RELEASE_MSIX(device_get_parent(dev), child,
1647                     msix->msix_vectors[i].mv_irq);
1648         kfree(msix->msix_vectors, M_DEVBUF);
1649         msix->msix_alloc = 0;
1650         return (0);
1651 }
1652
1653 /*
1654  * Return the max supported MSI-X messages this device supports.
1655  * Basically, assuming the MD code can alloc messages, this function
1656  * should return the maximum value that pci_alloc_msix() can return.
1657  * Thus, it is subject to the tunables, etc.
1658  */
1659 int
1660 pci_msix_count_method(device_t dev, device_t child)
1661 {
1662         struct pci_devinfo *dinfo = device_get_ivars(child);
1663         struct pcicfg_msix *msix = &dinfo->cfg.msix;
1664
1665         if (pci_do_msix && msix->msix_location != 0)
1666                 return (msix->msix_msgnum);
1667         return (0);
1668 }
1669
1670 /*
1671  * HyperTransport MSI mapping control
1672  */
1673 void
1674 pci_ht_map_msi(device_t dev, uint64_t addr)
1675 {
1676         struct pci_devinfo *dinfo = device_get_ivars(dev);
1677         struct pcicfg_ht *ht = &dinfo->cfg.ht;
1678
1679         if (!ht->ht_msimap)
1680                 return;
1681
1682         if (addr && !(ht->ht_msictrl & PCIM_HTCMD_MSI_ENABLE) &&
1683             ht->ht_msiaddr >> 20 == addr >> 20) {
1684                 /* Enable MSI -> HT mapping. */
1685                 ht->ht_msictrl |= PCIM_HTCMD_MSI_ENABLE;
1686                 pci_write_config(dev, ht->ht_msimap + PCIR_HT_COMMAND,
1687                     ht->ht_msictrl, 2);
1688         }
1689
1690         if (!addr && ht->ht_msictrl & PCIM_HTCMD_MSI_ENABLE) {
1691                 /* Disable MSI -> HT mapping. */
1692                 ht->ht_msictrl &= ~PCIM_HTCMD_MSI_ENABLE;
1693                 pci_write_config(dev, ht->ht_msimap + PCIR_HT_COMMAND,
1694                     ht->ht_msictrl, 2);
1695         }
1696 }
1697
1698 /*
1699  * Support for MSI message signalled interrupts.
1700  */
1701 void
1702 pci_enable_msi(device_t dev, uint64_t address, uint16_t data)
1703 {
1704         struct pci_devinfo *dinfo = device_get_ivars(dev);
1705         struct pcicfg_msi *msi = &dinfo->cfg.msi;
1706
1707         /* Write data and address values. */
1708         pci_write_config(dev, msi->msi_location + PCIR_MSI_ADDR,
1709             address & 0xffffffff, 4);
1710         if (msi->msi_ctrl & PCIM_MSICTRL_64BIT) {
1711                 pci_write_config(dev, msi->msi_location + PCIR_MSI_ADDR_HIGH,
1712                     address >> 32, 4);
1713                 pci_write_config(dev, msi->msi_location + PCIR_MSI_DATA_64BIT,
1714                     data, 2);
1715         } else
1716                 pci_write_config(dev, msi->msi_location + PCIR_MSI_DATA, data,
1717                     2);
1718
1719         /* Enable MSI in the control register. */
1720         msi->msi_ctrl |= PCIM_MSICTRL_MSI_ENABLE;
1721         pci_write_config(dev, msi->msi_location + PCIR_MSI_CTRL, msi->msi_ctrl,
1722             2);
1723
1724         /* Enable MSI -> HT mapping. */
1725         pci_ht_map_msi(dev, address);
1726 }
1727
1728 void
1729 pci_disable_msi(device_t dev)
1730 {
1731         struct pci_devinfo *dinfo = device_get_ivars(dev);
1732         struct pcicfg_msi *msi = &dinfo->cfg.msi;
1733
1734         /* Disable MSI -> HT mapping. */
1735         pci_ht_map_msi(dev, 0);
1736
1737         /* Disable MSI in the control register. */
1738         msi->msi_ctrl &= ~PCIM_MSICTRL_MSI_ENABLE;
1739         pci_write_config(dev, msi->msi_location + PCIR_MSI_CTRL, msi->msi_ctrl,
1740             2);
1741 }
1742
1743 /*
1744  * Restore MSI registers during resume.  If MSI is enabled then
1745  * restore the data and address registers in addition to the control
1746  * register.
1747  */
1748 static void
1749 pci_resume_msi(device_t dev)
1750 {
1751         struct pci_devinfo *dinfo = device_get_ivars(dev);
1752         struct pcicfg_msi *msi = &dinfo->cfg.msi;
1753         uint64_t address;
1754         uint16_t data;
1755
1756         if (msi->msi_ctrl & PCIM_MSICTRL_MSI_ENABLE) {
1757                 address = msi->msi_addr;
1758                 data = msi->msi_data;
1759                 pci_write_config(dev, msi->msi_location + PCIR_MSI_ADDR,
1760                     address & 0xffffffff, 4);
1761                 if (msi->msi_ctrl & PCIM_MSICTRL_64BIT) {
1762                         pci_write_config(dev, msi->msi_location +
1763                             PCIR_MSI_ADDR_HIGH, address >> 32, 4);
1764                         pci_write_config(dev, msi->msi_location +
1765                             PCIR_MSI_DATA_64BIT, data, 2);
1766                 } else
1767                         pci_write_config(dev, msi->msi_location + PCIR_MSI_DATA,
1768                             data, 2);
1769         }
1770         pci_write_config(dev, msi->msi_location + PCIR_MSI_CTRL, msi->msi_ctrl,
1771             2);
1772 }
1773
1774 /*
1775  * Returns true if the specified device is blacklisted because MSI
1776  * doesn't work.
1777  */
1778 int
1779 pci_msi_device_blacklisted(device_t dev)
1780 {
1781         struct pci_quirk *q;
1782
1783         if (!pci_honor_msi_blacklist)
1784                 return (0);
1785
1786         for (q = &pci_quirks[0]; q->devid; q++) {
1787                 if (q->devid == pci_get_devid(dev) &&
1788                     q->type == PCI_QUIRK_DISABLE_MSI)
1789                         return (1);
1790         }
1791         return (0);
1792 }
1793
1794 /*
1795  * Determine if MSI is blacklisted globally on this sytem.  Currently,
1796  * we just check for blacklisted chipsets as represented by the
1797  * host-PCI bridge at device 0:0:0.  In the future, it may become
1798  * necessary to check other system attributes, such as the kenv values
1799  * that give the motherboard manufacturer and model number.
1800  */
1801 static int
1802 pci_msi_blacklisted(void)
1803 {
1804         device_t dev;
1805
1806         if (!pci_honor_msi_blacklist)
1807                 return (0);
1808
1809         /* Blacklist all non-PCI-express and non-PCI-X chipsets. */
1810         if (!(pcie_chipset || pcix_chipset))
1811                 return (1);
1812
1813         dev = pci_find_bsf(0, 0, 0);
1814         if (dev != NULL)
1815                 return (pci_msi_device_blacklisted(dev));
1816         return (0);
1817 }
1818
1819 /*
1820  * Attempt to allocate count MSI messages on start_cpuid.
1821  *
1822  * If start_cpuid < 0, then the MSI messages' target CPU will be
1823  * selected automaticly.
1824  *
1825  * If the caller explicitly specified the MSI messages' target CPU,
1826  * i.e. start_cpuid >= 0, then we will try to allocate the count MSI
1827  * messages on the specified CPU, if the allocation fails due to MD
1828  * does not have enough vectors (EMSGSIZE), then we will try next
1829  * available CPU, until the allocation fails on all CPUs.
1830  *
1831  * EMSGSIZE will be returned, if all available CPUs does not have
1832  * enough vectors for the requested amount of MSI messages.  Caller
1833  * should either reduce the amount of MSI messages to be requested,
1834  * or simply giving up using MSI.
1835  *
1836  * The available SYS_RES_IRQ resources' rids, which are >= 1, are
1837  * returned in 'rid' array, if the allocation succeeds.
1838  */
1839 int
1840 pci_alloc_msi_method(device_t dev, device_t child, int *rid, int count,
1841     int start_cpuid)
1842 {
1843         struct pci_devinfo *dinfo = device_get_ivars(child);
1844         pcicfgregs *cfg = &dinfo->cfg;
1845         struct resource_list_entry *rle;
1846         int error, i, irqs[32], cpuid = 0;
1847         uint16_t ctrl;
1848
1849         KASSERT(count != 0 && count <= 32 && powerof2(count),
1850             ("invalid MSI count %d\n", count));
1851         KASSERT(start_cpuid < ncpus, ("invalid cpuid %d\n", start_cpuid));
1852
1853         /* If rid 0 is allocated, then fail. */
1854         rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, 0);
1855         if (rle != NULL && rle->res != NULL)
1856                 return (ENXIO);
1857
1858         /* Already have allocated messages? */
1859         if (cfg->msi.msi_alloc != 0 || cfg->msix.msix_alloc != 0)
1860                 return (ENXIO);
1861
1862         /* If MSI is blacklisted for this system, fail. */
1863         if (pci_msi_blacklisted())
1864                 return (ENXIO);
1865
1866         /* MSI capability present? */
1867         if (cfg->msi.msi_location == 0 || !pci_do_msi)
1868                 return (ENODEV);
1869
1870         KASSERT(count <= cfg->msi.msi_msgnum, ("large MSI count %d, max %d\n",
1871             count, cfg->msi.msi_msgnum));
1872
1873         if (bootverbose) {
1874                 device_printf(child,
1875                     "attempting to allocate %d MSI vectors (%d supported)\n",
1876                     count, cfg->msi.msi_msgnum);
1877         }
1878
1879         if (start_cpuid < 0)
1880                 start_cpuid = atomic_fetchadd_int(&pci_msi_cpuid, 1) % ncpus;
1881
1882         error = EINVAL;
1883         for (i = 0; i < ncpus; ++i) {
1884                 cpuid = (start_cpuid + i) % ncpus;
1885
1886                 error = PCIB_ALLOC_MSI(device_get_parent(dev), child, count,
1887                     cfg->msi.msi_msgnum, irqs, cpuid);
1888                 if (error == 0)
1889                         break;
1890                 else if (error != EMSGSIZE)
1891                         return error;
1892         }
1893         if (error)
1894                 return error;
1895
1896         /*
1897          * We now have N messages mapped onto SYS_RES_IRQ resources in
1898          * the irqs[] array, so add new resources starting at rid 1.
1899          */
1900         for (i = 0; i < count; i++) {
1901                 rid[i] = i + 1;
1902                 resource_list_add(&dinfo->resources, SYS_RES_IRQ, i + 1,
1903                     irqs[i], irqs[i], 1, cpuid);
1904         }
1905
1906         if (bootverbose) {
1907                 if (count == 1) {
1908                         device_printf(child, "using IRQ %d on cpu%d for MSI\n",
1909                             irqs[0], cpuid);
1910                 } else {
1911                         int run;
1912
1913                         /*
1914                          * Be fancy and try to print contiguous runs
1915                          * of IRQ values as ranges.  'run' is true if
1916                          * we are in a range.
1917                          */
1918                         device_printf(child, "using IRQs %d", irqs[0]);
1919                         run = 0;
1920                         for (i = 1; i < count; i++) {
1921
1922                                 /* Still in a run? */
1923                                 if (irqs[i] == irqs[i - 1] + 1) {
1924                                         run = 1;
1925                                         continue;
1926                                 }
1927
1928                                 /* Finish previous range. */
1929                                 if (run) {
1930                                         kprintf("-%d", irqs[i - 1]);
1931                                         run = 0;
1932                                 }
1933
1934                                 /* Start new range. */
1935                                 kprintf(",%d", irqs[i]);
1936                         }
1937
1938                         /* Unfinished range? */
1939                         if (run)
1940                                 kprintf("-%d", irqs[count - 1]);
1941                         kprintf(" for MSI on cpu%d\n", cpuid);
1942                 }
1943         }
1944
1945         /* Update control register with count. */
1946         ctrl = cfg->msi.msi_ctrl;
1947         ctrl &= ~PCIM_MSICTRL_MME_MASK;
1948         ctrl |= (ffs(count) - 1) << 4;
1949         cfg->msi.msi_ctrl = ctrl;
1950         pci_write_config(child, cfg->msi.msi_location + PCIR_MSI_CTRL, ctrl, 2);
1951
1952         /* Update counts of alloc'd messages. */
1953         cfg->msi.msi_alloc = count;
1954         cfg->msi.msi_handlers = 0;
1955         return (0);
1956 }
1957
1958 /* Release the MSI messages associated with this device. */
1959 int
1960 pci_release_msi_method(device_t dev, device_t child)
1961 {
1962         struct pci_devinfo *dinfo = device_get_ivars(child);
1963         struct pcicfg_msi *msi = &dinfo->cfg.msi;
1964         struct resource_list_entry *rle;
1965         int error, i, irqs[32], cpuid = -1;
1966
1967         /* Try MSI-X first. */
1968         error = pci_release_msix(dev, child);
1969         if (error != ENODEV)
1970                 return (error);
1971
1972         /* Do we have any messages to release? */
1973         if (msi->msi_alloc == 0)
1974                 return (ENODEV);
1975         KASSERT(msi->msi_alloc <= 32, ("more than 32 alloc'd messages"));
1976
1977         /* Make sure none of the resources are allocated. */
1978         if (msi->msi_handlers > 0)
1979                 return (EBUSY);
1980         for (i = 0; i < msi->msi_alloc; i++) {
1981                 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, i + 1);
1982                 KASSERT(rle != NULL, ("missing MSI resource"));
1983                 if (rle->res != NULL)
1984                         return (EBUSY);
1985                 if (i == 0) {
1986                         cpuid = rle->cpuid;
1987                         KASSERT(cpuid >= 0 && cpuid < ncpus,
1988                             ("invalid MSI target cpuid %d\n", cpuid));
1989                 } else {
1990                         KASSERT(rle->cpuid == cpuid,
1991                             ("MSI targets different cpus, "
1992                              "was cpu%d, now cpu%d", cpuid, rle->cpuid));
1993                 }
1994                 irqs[i] = rle->start;
1995         }
1996
1997         /* Update control register with 0 count. */
1998         KASSERT(!(msi->msi_ctrl & PCIM_MSICTRL_MSI_ENABLE),
1999             ("%s: MSI still enabled", __func__));
2000         msi->msi_ctrl &= ~PCIM_MSICTRL_MME_MASK;
2001         pci_write_config(child, msi->msi_location + PCIR_MSI_CTRL,
2002             msi->msi_ctrl, 2);
2003
2004         /* Release the messages. */
2005         PCIB_RELEASE_MSI(device_get_parent(dev), child, msi->msi_alloc, irqs,
2006             cpuid);
2007         for (i = 0; i < msi->msi_alloc; i++)
2008                 resource_list_delete(&dinfo->resources, SYS_RES_IRQ, i + 1);
2009
2010         /* Update alloc count. */
2011         msi->msi_alloc = 0;
2012         msi->msi_addr = 0;
2013         msi->msi_data = 0;
2014         return (0);
2015 }
2016
2017 /*
2018  * Return the max supported MSI messages this device supports.
2019  * Basically, assuming the MD code can alloc messages, this function
2020  * should return the maximum value that pci_alloc_msi() can return.
2021  * Thus, it is subject to the tunables, etc.
2022  */
2023 int
2024 pci_msi_count_method(device_t dev, device_t child)
2025 {
2026         struct pci_devinfo *dinfo = device_get_ivars(child);
2027         struct pcicfg_msi *msi = &dinfo->cfg.msi;
2028
2029         if (pci_do_msi && msi->msi_location != 0)
2030                 return (msi->msi_msgnum);
2031         return (0);
2032 }
2033
2034 /* kfree pcicfgregs structure and all depending data structures */
2035
2036 int
2037 pci_freecfg(struct pci_devinfo *dinfo)
2038 {
2039         struct devlist *devlist_head;
2040         int i;
2041
2042         devlist_head = &pci_devq;
2043
2044         if (dinfo->cfg.vpd.vpd_reg) {
2045                 kfree(dinfo->cfg.vpd.vpd_ident, M_DEVBUF);
2046                 for (i = 0; i < dinfo->cfg.vpd.vpd_rocnt; i++)
2047                         kfree(dinfo->cfg.vpd.vpd_ros[i].value, M_DEVBUF);
2048                 kfree(dinfo->cfg.vpd.vpd_ros, M_DEVBUF);
2049                 for (i = 0; i < dinfo->cfg.vpd.vpd_wcnt; i++)
2050                         kfree(dinfo->cfg.vpd.vpd_w[i].value, M_DEVBUF);
2051                 kfree(dinfo->cfg.vpd.vpd_w, M_DEVBUF);
2052         }
2053         STAILQ_REMOVE(devlist_head, dinfo, pci_devinfo, pci_links);
2054         kfree(dinfo, M_DEVBUF);
2055
2056         /* increment the generation count */
2057         pci_generation++;
2058
2059         /* we're losing one device */
2060         pci_numdevs--;
2061         return (0);
2062 }
2063
2064 /*
2065  * PCI power manangement
2066  */
2067 int
2068 pci_set_powerstate_method(device_t dev, device_t child, int state)
2069 {
2070         struct pci_devinfo *dinfo = device_get_ivars(child);
2071         pcicfgregs *cfg = &dinfo->cfg;
2072         uint16_t status;
2073         int result, oldstate, highest, delay;
2074
2075         if (cfg->pp.pp_cap == 0)
2076                 return (EOPNOTSUPP);
2077
2078         /*
2079          * Optimize a no state change request away.  While it would be OK to
2080          * write to the hardware in theory, some devices have shown odd
2081          * behavior when going from D3 -> D3.
2082          */
2083         oldstate = pci_get_powerstate(child);
2084         if (oldstate == state)
2085                 return (0);
2086
2087         /*
2088          * The PCI power management specification states that after a state
2089          * transition between PCI power states, system software must
2090          * guarantee a minimal delay before the function accesses the device.
2091          * Compute the worst case delay that we need to guarantee before we
2092          * access the device.  Many devices will be responsive much more
2093          * quickly than this delay, but there are some that don't respond
2094          * instantly to state changes.  Transitions to/from D3 state require
2095          * 10ms, while D2 requires 200us, and D0/1 require none.  The delay
2096          * is done below with DELAY rather than a sleeper function because
2097          * this function can be called from contexts where we cannot sleep.
2098          */
2099         highest = (oldstate > state) ? oldstate : state;
2100         if (highest == PCI_POWERSTATE_D3)
2101             delay = 10000;
2102         else if (highest == PCI_POWERSTATE_D2)
2103             delay = 200;
2104         else
2105             delay = 0;
2106         status = PCI_READ_CONFIG(dev, child, cfg->pp.pp_status, 2)
2107             & ~PCIM_PSTAT_DMASK;
2108         result = 0;
2109         switch (state) {
2110         case PCI_POWERSTATE_D0:
2111                 status |= PCIM_PSTAT_D0;
2112                 break;
2113         case PCI_POWERSTATE_D1:
2114                 if ((cfg->pp.pp_cap & PCIM_PCAP_D1SUPP) == 0)
2115                         return (EOPNOTSUPP);
2116                 status |= PCIM_PSTAT_D1;
2117                 break;
2118         case PCI_POWERSTATE_D2:
2119                 if ((cfg->pp.pp_cap & PCIM_PCAP_D2SUPP) == 0)
2120                         return (EOPNOTSUPP);
2121                 status |= PCIM_PSTAT_D2;
2122                 break;
2123         case PCI_POWERSTATE_D3:
2124                 status |= PCIM_PSTAT_D3;
2125                 break;
2126         default:
2127                 return (EINVAL);
2128         }
2129
2130         if (bootverbose)
2131                 kprintf(
2132                     "pci%d:%d:%d:%d: Transition from D%d to D%d\n",
2133                     dinfo->cfg.domain, dinfo->cfg.bus, dinfo->cfg.slot,
2134                     dinfo->cfg.func, oldstate, state);
2135
2136         PCI_WRITE_CONFIG(dev, child, cfg->pp.pp_status, status, 2);
2137         if (delay)
2138                 DELAY(delay);
2139         return (0);
2140 }
2141
2142 int
2143 pci_get_powerstate_method(device_t dev, device_t child)
2144 {
2145         struct pci_devinfo *dinfo = device_get_ivars(child);
2146         pcicfgregs *cfg = &dinfo->cfg;
2147         uint16_t status;
2148         int result;
2149
2150         if (cfg->pp.pp_cap != 0) {
2151                 status = PCI_READ_CONFIG(dev, child, cfg->pp.pp_status, 2);
2152                 switch (status & PCIM_PSTAT_DMASK) {
2153                 case PCIM_PSTAT_D0:
2154                         result = PCI_POWERSTATE_D0;
2155                         break;
2156                 case PCIM_PSTAT_D1:
2157                         result = PCI_POWERSTATE_D1;
2158                         break;
2159                 case PCIM_PSTAT_D2:
2160                         result = PCI_POWERSTATE_D2;
2161                         break;
2162                 case PCIM_PSTAT_D3:
2163                         result = PCI_POWERSTATE_D3;
2164                         break;
2165                 default:
2166                         result = PCI_POWERSTATE_UNKNOWN;
2167                         break;
2168                 }
2169         } else {
2170                 /* No support, device is always at D0 */
2171                 result = PCI_POWERSTATE_D0;
2172         }
2173         return (result);
2174 }
2175
2176 /*
2177  * Some convenience functions for PCI device drivers.
2178  */
2179
2180 static __inline void
2181 pci_set_command_bit(device_t dev, device_t child, uint16_t bit)
2182 {
2183         uint16_t        command;
2184
2185         command = PCI_READ_CONFIG(dev, child, PCIR_COMMAND, 2);
2186         command |= bit;
2187         PCI_WRITE_CONFIG(dev, child, PCIR_COMMAND, command, 2);
2188 }
2189
2190 static __inline void
2191 pci_clear_command_bit(device_t dev, device_t child, uint16_t bit)
2192 {
2193         uint16_t        command;
2194
2195         command = PCI_READ_CONFIG(dev, child, PCIR_COMMAND, 2);
2196         command &= ~bit;
2197         PCI_WRITE_CONFIG(dev, child, PCIR_COMMAND, command, 2);
2198 }
2199
2200 int
2201 pci_enable_busmaster_method(device_t dev, device_t child)
2202 {
2203         pci_set_command_bit(dev, child, PCIM_CMD_BUSMASTEREN);
2204         return (0);
2205 }
2206
2207 int
2208 pci_disable_busmaster_method(device_t dev, device_t child)
2209 {
2210         pci_clear_command_bit(dev, child, PCIM_CMD_BUSMASTEREN);
2211         return (0);
2212 }
2213
2214 int
2215 pci_enable_io_method(device_t dev, device_t child, int space)
2216 {
2217         uint16_t command;
2218         uint16_t bit;
2219         char *error;
2220
2221         bit = 0;
2222         error = NULL;
2223
2224         switch(space) {
2225         case SYS_RES_IOPORT:
2226                 bit = PCIM_CMD_PORTEN;
2227                 error = "port";
2228                 break;
2229         case SYS_RES_MEMORY:
2230                 bit = PCIM_CMD_MEMEN;
2231                 error = "memory";
2232                 break;
2233         default:
2234                 return (EINVAL);
2235         }
2236         pci_set_command_bit(dev, child, bit);
2237         /* Some devices seem to need a brief stall here, what do to? */
2238         command = PCI_READ_CONFIG(dev, child, PCIR_COMMAND, 2);
2239         if (command & bit)
2240                 return (0);
2241         device_printf(child, "failed to enable %s mapping!\n", error);
2242         return (ENXIO);
2243 }
2244
2245 int
2246 pci_disable_io_method(device_t dev, device_t child, int space)
2247 {
2248         uint16_t command;
2249         uint16_t bit;
2250         char *error;
2251
2252         bit = 0;
2253         error = NULL;
2254
2255         switch(space) {
2256         case SYS_RES_IOPORT:
2257                 bit = PCIM_CMD_PORTEN;
2258                 error = "port";
2259                 break;
2260         case SYS_RES_MEMORY:
2261                 bit = PCIM_CMD_MEMEN;
2262                 error = "memory";
2263                 break;
2264         default:
2265                 return (EINVAL);
2266         }
2267         pci_clear_command_bit(dev, child, bit);
2268         command = PCI_READ_CONFIG(dev, child, PCIR_COMMAND, 2);
2269         if (command & bit) {
2270                 device_printf(child, "failed to disable %s mapping!\n", error);
2271                 return (ENXIO);
2272         }
2273         return (0);
2274 }
2275
2276 /*
2277  * New style pci driver.  Parent device is either a pci-host-bridge or a
2278  * pci-pci-bridge.  Both kinds are represented by instances of pcib.
2279  */
2280
2281 void
2282 pci_print_verbose(struct pci_devinfo *dinfo)
2283 {
2284
2285         if (bootverbose) {
2286                 pcicfgregs *cfg = &dinfo->cfg;
2287
2288                 kprintf("found->\tvendor=0x%04x, dev=0x%04x, revid=0x%02x\n",
2289                     cfg->vendor, cfg->device, cfg->revid);
2290                 kprintf("\tdomain=%d, bus=%d, slot=%d, func=%d\n",
2291                     cfg->domain, cfg->bus, cfg->slot, cfg->func);
2292                 kprintf("\tclass=%02x-%02x-%02x, hdrtype=0x%02x, mfdev=%d\n",
2293                     cfg->baseclass, cfg->subclass, cfg->progif, cfg->hdrtype,
2294                     cfg->mfdev);
2295                 kprintf("\tcmdreg=0x%04x, statreg=0x%04x, cachelnsz=%d (dwords)\n",
2296                     cfg->cmdreg, cfg->statreg, cfg->cachelnsz);
2297                 kprintf("\tlattimer=0x%02x (%d ns), mingnt=0x%02x (%d ns), maxlat=0x%02x (%d ns)\n",
2298                     cfg->lattimer, cfg->lattimer * 30, cfg->mingnt,
2299                     cfg->mingnt * 250, cfg->maxlat, cfg->maxlat * 250);
2300                 if (cfg->intpin > 0)
2301                         kprintf("\tintpin=%c, irq=%d\n",
2302                             cfg->intpin +'a' -1, cfg->intline);
2303                 if (cfg->pp.pp_cap) {
2304                         uint16_t status;
2305
2306                         status = pci_read_config(cfg->dev, cfg->pp.pp_status, 2);
2307                         kprintf("\tpowerspec %d  supports D0%s%s D3  current D%d\n",
2308                             cfg->pp.pp_cap & PCIM_PCAP_SPEC,
2309                             cfg->pp.pp_cap & PCIM_PCAP_D1SUPP ? " D1" : "",
2310                             cfg->pp.pp_cap & PCIM_PCAP_D2SUPP ? " D2" : "",
2311                             status & PCIM_PSTAT_DMASK);
2312                 }
2313                 if (cfg->msi.msi_location) {
2314                         int ctrl;
2315
2316                         ctrl = cfg->msi.msi_ctrl;
2317                         kprintf("\tMSI supports %d message%s%s%s\n",
2318                             cfg->msi.msi_msgnum,
2319                             (cfg->msi.msi_msgnum == 1) ? "" : "s",
2320                             (ctrl & PCIM_MSICTRL_64BIT) ? ", 64 bit" : "",
2321                             (ctrl & PCIM_MSICTRL_VECTOR) ? ", vector masks":"");
2322                 }
2323                 if (cfg->msix.msix_location) {
2324                         kprintf("\tMSI-X supports %d message%s ",
2325                             cfg->msix.msix_msgnum,
2326                             (cfg->msix.msix_msgnum == 1) ? "" : "s");
2327                         if (cfg->msix.msix_table_bar == cfg->msix.msix_pba_bar)
2328                                 kprintf("in map 0x%x\n",
2329                                     cfg->msix.msix_table_bar);
2330                         else
2331                                 kprintf("in maps 0x%x and 0x%x\n",
2332                                     cfg->msix.msix_table_bar,
2333                                     cfg->msix.msix_pba_bar);
2334                 }
2335                 pci_print_verbose_expr(cfg);
2336         }
2337 }
2338
2339 static void
2340 pci_print_verbose_expr(const pcicfgregs *cfg)
2341 {
2342         const struct pcicfg_expr *expr = &cfg->expr;
2343         const char *port_name;
2344         uint16_t port_type;
2345
2346         if (!bootverbose)
2347                 return;
2348
2349         if (expr->expr_ptr == 0) /* No PCI Express capability */
2350                 return;
2351
2352         kprintf("\tPCI Express ver.%d cap=0x%04x",
2353                 expr->expr_cap & PCIEM_CAP_VER_MASK, expr->expr_cap);
2354         if ((expr->expr_cap & PCIEM_CAP_VER_MASK) != PCIEM_CAP_VER_1)
2355                 goto back;
2356
2357         port_type = expr->expr_cap & PCIEM_CAP_PORT_TYPE;
2358
2359         switch (port_type) {
2360         case PCIE_END_POINT:
2361                 port_name = "DEVICE";
2362                 break;
2363         case PCIE_LEG_END_POINT:
2364                 port_name = "LEGDEV";
2365                 break;
2366         case PCIE_ROOT_PORT:
2367                 port_name = "ROOT";
2368                 break;
2369         case PCIE_UP_STREAM_PORT:
2370                 port_name = "UPSTREAM";
2371                 break;
2372         case PCIE_DOWN_STREAM_PORT:
2373                 port_name = "DOWNSTRM";
2374                 break;
2375         case PCIE_PCIE2PCI_BRIDGE:
2376                 port_name = "PCIE2PCI";
2377                 break;
2378         case PCIE_PCI2PCIE_BRIDGE:
2379                 port_name = "PCI2PCIE";
2380                 break;
2381         default:
2382                 port_name = NULL;
2383                 break;
2384         }
2385         if ((port_type == PCIE_ROOT_PORT ||
2386              port_type == PCIE_DOWN_STREAM_PORT) &&
2387             !(expr->expr_cap & PCIEM_CAP_SLOT_IMPL))
2388                 port_name = NULL;
2389         if (port_name != NULL)
2390                 kprintf("[%s]", port_name);
2391
2392         if (pcie_slotimpl(cfg)) {
2393                 kprintf(", slotcap=0x%08x", expr->expr_slotcap);
2394                 if (expr->expr_slotcap & PCIEM_SLTCAP_HP_CAP)
2395                         kprintf("[HOTPLUG]");
2396         }
2397 back:
2398         kprintf("\n");
2399 }
2400
2401 static int
2402 pci_porten(device_t pcib, int b, int s, int f)
2403 {
2404         return (PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2)
2405                 & PCIM_CMD_PORTEN) != 0;
2406 }
2407
2408 static int
2409 pci_memen(device_t pcib, int b, int s, int f)
2410 {
2411         return (PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2)
2412                 & PCIM_CMD_MEMEN) != 0;
2413 }
2414
2415 /*
2416  * Add a resource based on a pci map register. Return 1 if the map
2417  * register is a 32bit map register or 2 if it is a 64bit register.
2418  */
2419 static int
2420 pci_add_map(device_t pcib, device_t bus, device_t dev,
2421     int b, int s, int f, int reg, struct resource_list *rl, int force,
2422     int prefetch)
2423 {
2424         uint32_t map;
2425         pci_addr_t base;
2426         pci_addr_t start, end, count;
2427         uint8_t ln2size;
2428         uint8_t ln2range;
2429         uint32_t testval;
2430         uint16_t cmd;
2431         int type;
2432         int barlen;
2433         struct resource *res;
2434
2435         map = PCIB_READ_CONFIG(pcib, b, s, f, reg, 4);
2436         PCIB_WRITE_CONFIG(pcib, b, s, f, reg, 0xffffffff, 4);
2437         testval = PCIB_READ_CONFIG(pcib, b, s, f, reg, 4);
2438         PCIB_WRITE_CONFIG(pcib, b, s, f, reg, map, 4);
2439
2440         if (PCI_BAR_MEM(map)) {
2441                 type = SYS_RES_MEMORY;
2442                 if (map & PCIM_BAR_MEM_PREFETCH)
2443                         prefetch = 1;
2444         } else
2445                 type = SYS_RES_IOPORT;
2446         ln2size = pci_mapsize(testval);
2447         ln2range = pci_maprange(testval);
2448         base = pci_mapbase(map);
2449         barlen = ln2range == 64 ? 2 : 1;
2450
2451         /*
2452          * For I/O registers, if bottom bit is set, and the next bit up
2453          * isn't clear, we know we have a BAR that doesn't conform to the
2454          * spec, so ignore it.  Also, sanity check the size of the data
2455          * areas to the type of memory involved.  Memory must be at least
2456          * 16 bytes in size, while I/O ranges must be at least 4.
2457          */
2458         if (PCI_BAR_IO(testval) && (testval & PCIM_BAR_IO_RESERVED) != 0)
2459                 return (barlen);
2460         if ((type == SYS_RES_MEMORY && ln2size < 4) ||
2461             (type == SYS_RES_IOPORT && ln2size < 2))
2462                 return (barlen);
2463
2464         if (ln2range == 64)
2465                 /* Read the other half of a 64bit map register */
2466                 base |= (uint64_t) PCIB_READ_CONFIG(pcib, b, s, f, reg + 4, 4) << 32;
2467         if (bootverbose) {
2468                 kprintf("\tmap[%02x]: type %s, range %2d, base %#jx, size %2d",
2469                     reg, pci_maptype(map), ln2range, (uintmax_t)base, ln2size);
2470                 if (type == SYS_RES_IOPORT && !pci_porten(pcib, b, s, f))
2471                         kprintf(", port disabled\n");
2472                 else if (type == SYS_RES_MEMORY && !pci_memen(pcib, b, s, f))
2473                         kprintf(", memory disabled\n");
2474                 else
2475                         kprintf(", enabled\n");
2476         }
2477
2478         /*
2479          * If base is 0, then we have problems.  It is best to ignore
2480          * such entries for the moment.  These will be allocated later if
2481          * the driver specifically requests them.  However, some
2482          * removable busses look better when all resources are allocated,
2483          * so allow '0' to be overriden.
2484          *
2485          * Similarly treat maps whose values is the same as the test value
2486          * read back.  These maps have had all f's written to them by the
2487          * BIOS in an attempt to disable the resources.
2488          */
2489         if (!force && (base == 0 || map == testval))
2490                 return (barlen);
2491         if ((u_long)base != base) {
2492                 device_printf(bus,
2493                     "pci%d:%d:%d:%d bar %#x too many address bits",
2494                     pci_get_domain(dev), b, s, f, reg);
2495                 return (barlen);
2496         }
2497
2498         /*
2499          * This code theoretically does the right thing, but has
2500          * undesirable side effects in some cases where peripherals
2501          * respond oddly to having these bits enabled.  Let the user
2502          * be able to turn them off (since pci_enable_io_modes is 1 by
2503          * default).
2504          */
2505         if (pci_enable_io_modes) {
2506                 /* Turn on resources that have been left off by a lazy BIOS */
2507                 if (type == SYS_RES_IOPORT && !pci_porten(pcib, b, s, f)) {
2508                         cmd = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2);
2509                         cmd |= PCIM_CMD_PORTEN;
2510                         PCIB_WRITE_CONFIG(pcib, b, s, f, PCIR_COMMAND, cmd, 2);
2511                 }
2512                 if (type == SYS_RES_MEMORY && !pci_memen(pcib, b, s, f)) {
2513                         cmd = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_COMMAND, 2);
2514                         cmd |= PCIM_CMD_MEMEN;
2515                         PCIB_WRITE_CONFIG(pcib, b, s, f, PCIR_COMMAND, cmd, 2);
2516                 }
2517         } else {
2518                 if (type == SYS_RES_IOPORT && !pci_porten(pcib, b, s, f))
2519                         return (barlen);
2520                 if (type == SYS_RES_MEMORY && !pci_memen(pcib, b, s, f))
2521                         return (barlen);
2522         }
2523
2524         count = 1 << ln2size;
2525         if (base == 0 || base == pci_mapbase(testval)) {
2526                 start = 0;      /* Let the parent decide. */
2527                 end = ~0ULL;
2528         } else {
2529                 start = base;
2530                 end = base + (1 << ln2size) - 1;
2531         }
2532         resource_list_add(rl, type, reg, start, end, count, -1);
2533
2534         /*
2535          * Try to allocate the resource for this BAR from our parent
2536          * so that this resource range is already reserved.  The
2537          * driver for this device will later inherit this resource in
2538          * pci_alloc_resource().
2539          */
2540         res = resource_list_alloc(rl, bus, dev, type, &reg, start, end, count,
2541             prefetch ? RF_PREFETCHABLE : 0, -1);
2542         if (res == NULL) {
2543                 /*
2544                  * If the allocation fails, delete the resource list
2545                  * entry to force pci_alloc_resource() to allocate
2546                  * resources from the parent.
2547                  */
2548                 resource_list_delete(rl, type, reg);
2549 #ifdef PCI_BAR_CLEAR
2550                 /* Clear the BAR */
2551                 start = 0;
2552 #else   /* !PCI_BAR_CLEAR */
2553                 /*
2554                  * Don't clear BAR here.  Some BIOS lists HPET as a
2555                  * PCI function, clearing the BAR causes HPET timer
2556                  * stop ticking.
2557                  */
2558                 if (bootverbose) {
2559                         kprintf("pci:%d:%d:%d: resource reservation failed "
2560                                 "%#jx - %#jx\n", b, s, f,
2561                                 (intmax_t)start, (intmax_t)end);
2562                 }
2563                 return (barlen);
2564 #endif  /* PCI_BAR_CLEAR */
2565         } else {
2566                 start = rman_get_start(res);
2567         }
2568         pci_write_config(dev, reg, start, 4);
2569         if (ln2range == 64)
2570                 pci_write_config(dev, reg + 4, start >> 32, 4);
2571         return (barlen);
2572 }
2573
2574 /*
2575  * For ATA devices we need to decide early what addressing mode to use.
2576  * Legacy demands that the primary and secondary ATA ports sits on the
2577  * same addresses that old ISA hardware did. This dictates that we use
2578  * those addresses and ignore the BAR's if we cannot set PCI native
2579  * addressing mode.
2580  */
2581 static void
2582 pci_ata_maps(device_t pcib, device_t bus, device_t dev, int b,
2583     int s, int f, struct resource_list *rl, int force, uint32_t prefetchmask)
2584 {
2585         int rid, type, progif;
2586 #if 0
2587         /* if this device supports PCI native addressing use it */
2588         progif = pci_read_config(dev, PCIR_PROGIF, 1);
2589         if ((progif & 0x8a) == 0x8a) {
2590                 if (pci_mapbase(pci_read_config(dev, PCIR_BAR(0), 4)) &&
2591                     pci_mapbase(pci_read_config(dev, PCIR_BAR(2), 4))) {
2592                         kprintf("Trying ATA native PCI addressing mode\n");
2593                         pci_write_config(dev, PCIR_PROGIF, progif | 0x05, 1);
2594                 }
2595         }
2596 #endif
2597         progif = pci_read_config(dev, PCIR_PROGIF, 1);
2598         type = SYS_RES_IOPORT;
2599         if (progif & PCIP_STORAGE_IDE_MODEPRIM) {
2600                 pci_add_map(pcib, bus, dev, b, s, f, PCIR_BAR(0), rl, force,
2601                     prefetchmask & (1 << 0));
2602                 pci_add_map(pcib, bus, dev, b, s, f, PCIR_BAR(1), rl, force,
2603                     prefetchmask & (1 << 1));
2604         } else {
2605                 rid = PCIR_BAR(0);
2606                 resource_list_add(rl, type, rid, 0x1f0, 0x1f7, 8, -1);
2607                 resource_list_alloc(rl, bus, dev, type, &rid, 0x1f0, 0x1f7, 8,
2608                     0, -1);
2609                 rid = PCIR_BAR(1);
2610                 resource_list_add(rl, type, rid, 0x3f6, 0x3f6, 1, -1);
2611                 resource_list_alloc(rl, bus, dev, type, &rid, 0x3f6, 0x3f6, 1,
2612                     0, -1);
2613         }
2614         if (progif & PCIP_STORAGE_IDE_MODESEC) {
2615                 pci_add_map(pcib, bus, dev, b, s, f, PCIR_BAR(2), rl, force,
2616                     prefetchmask & (1 << 2));
2617                 pci_add_map(pcib, bus, dev, b, s, f, PCIR_BAR(3), rl, force,
2618                     prefetchmask & (1 << 3));
2619         } else {
2620                 rid = PCIR_BAR(2);
2621                 resource_list_add(rl, type, rid, 0x170, 0x177, 8, -1);
2622                 resource_list_alloc(rl, bus, dev, type, &rid, 0x170, 0x177, 8,
2623                     0, -1);
2624                 rid = PCIR_BAR(3);
2625                 resource_list_add(rl, type, rid, 0x376, 0x376, 1, -1);
2626                 resource_list_alloc(rl, bus, dev, type, &rid, 0x376, 0x376, 1,
2627                     0, -1);
2628         }
2629         pci_add_map(pcib, bus, dev, b, s, f, PCIR_BAR(4), rl, force,
2630             prefetchmask & (1 << 4));
2631         pci_add_map(pcib, bus, dev, b, s, f, PCIR_BAR(5), rl, force,
2632             prefetchmask & (1 << 5));
2633 }
2634
2635 static void
2636 pci_assign_interrupt(device_t bus, device_t dev, int force_route)
2637 {
2638         struct pci_devinfo *dinfo = device_get_ivars(dev);
2639         pcicfgregs *cfg = &dinfo->cfg;
2640         char tunable_name[64];
2641         int irq;
2642
2643         /* Has to have an intpin to have an interrupt. */
2644         if (cfg->intpin == 0)
2645                 return;
2646
2647         /* Let the user override the IRQ with a tunable. */
2648         irq = PCI_INVALID_IRQ;
2649         ksnprintf(tunable_name, sizeof(tunable_name),
2650             "hw.pci%d.%d.%d.INT%c.irq",
2651             cfg->domain, cfg->bus, cfg->slot, cfg->intpin + 'A' - 1);
2652         if (TUNABLE_INT_FETCH(tunable_name, &irq) && (irq >= 255 || irq <= 0))
2653                 irq = PCI_INVALID_IRQ;
2654
2655         /*
2656          * If we didn't get an IRQ via the tunable, then we either use the
2657          * IRQ value in the intline register or we ask the bus to route an
2658          * interrupt for us.  If force_route is true, then we only use the
2659          * value in the intline register if the bus was unable to assign an
2660          * IRQ.
2661          */
2662         if (!PCI_INTERRUPT_VALID(irq)) {
2663                 if (!PCI_INTERRUPT_VALID(cfg->intline) || force_route)
2664                         irq = PCI_ASSIGN_INTERRUPT(bus, dev);
2665                 if (!PCI_INTERRUPT_VALID(irq))
2666                         irq = cfg->intline;
2667         }
2668
2669         /* If after all that we don't have an IRQ, just bail. */
2670         if (!PCI_INTERRUPT_VALID(irq))
2671                 return;
2672
2673         /* Update the config register if it changed. */
2674         if (irq != cfg->intline) {
2675                 cfg->intline = irq;
2676                 pci_write_config(dev, PCIR_INTLINE, irq, 1);
2677         }
2678
2679         /* Add this IRQ as rid 0 interrupt resource. */
2680         resource_list_add(&dinfo->resources, SYS_RES_IRQ, 0, irq, irq, 1,
2681             machintr_legacy_intr_cpuid(irq));
2682 }
2683
2684 void
2685 pci_add_resources(device_t pcib, device_t bus, device_t dev, int force, uint32_t prefetchmask)
2686 {
2687         struct pci_devinfo *dinfo = device_get_ivars(dev);
2688         pcicfgregs *cfg = &dinfo->cfg;
2689         struct resource_list *rl = &dinfo->resources;
2690         struct pci_quirk *q;
2691         int b, i, f, s;
2692
2693         b = cfg->bus;
2694         s = cfg->slot;
2695         f = cfg->func;
2696
2697         /* ATA devices needs special map treatment */
2698         if ((pci_get_class(dev) == PCIC_STORAGE) &&
2699             (pci_get_subclass(dev) == PCIS_STORAGE_IDE) &&
2700             ((pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) ||
2701              (!pci_read_config(dev, PCIR_BAR(0), 4) &&
2702               !pci_read_config(dev, PCIR_BAR(2), 4))) )
2703                 pci_ata_maps(pcib, bus, dev, b, s, f, rl, force, prefetchmask);
2704         else
2705                 for (i = 0; i < cfg->nummaps;)
2706                         i += pci_add_map(pcib, bus, dev, b, s, f, PCIR_BAR(i),
2707                             rl, force, prefetchmask & (1 << i));
2708
2709         /*
2710          * Add additional, quirked resources.
2711          */
2712         for (q = &pci_quirks[0]; q->devid; q++) {
2713                 if (q->devid == ((cfg->device << 16) | cfg->vendor)
2714                     && q->type == PCI_QUIRK_MAP_REG)
2715                         pci_add_map(pcib, bus, dev, b, s, f, q->arg1, rl,
2716                           force, 0);
2717         }
2718
2719         if (cfg->intpin > 0 && PCI_INTERRUPT_VALID(cfg->intline)) {
2720                 /*
2721                  * Try to re-route interrupts. Sometimes the BIOS or
2722                  * firmware may leave bogus values in these registers.
2723                  * If the re-route fails, then just stick with what we
2724                  * have.
2725                  */
2726                 pci_assign_interrupt(bus, dev, 1);
2727         }
2728 }
2729
2730 void
2731 pci_add_children(device_t dev, int domain, int busno, size_t dinfo_size)
2732 {
2733 #define REG(n, w)       PCIB_READ_CONFIG(pcib, busno, s, f, n, w)
2734         device_t pcib = device_get_parent(dev);
2735         struct pci_devinfo *dinfo;
2736         int maxslots;
2737         int s, f, pcifunchigh;
2738         uint8_t hdrtype;
2739
2740         KASSERT(dinfo_size >= sizeof(struct pci_devinfo),
2741             ("dinfo_size too small"));
2742         maxslots = PCIB_MAXSLOTS(pcib);
2743         for (s = 0; s <= maxslots; s++) {
2744                 pcifunchigh = 0;
2745                 f = 0;
2746                 DELAY(1);
2747                 hdrtype = REG(PCIR_HDRTYPE, 1);
2748                 if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
2749                         continue;
2750                 if (hdrtype & PCIM_MFDEV)
2751                         pcifunchigh = PCI_FUNCMAX;
2752                 for (f = 0; f <= pcifunchigh; f++) {
2753                         dinfo = pci_read_device(pcib, domain, busno, s, f,
2754                             dinfo_size);
2755                         if (dinfo != NULL) {
2756                                 pci_add_child(dev, dinfo);
2757                         }
2758                 }
2759         }
2760 #undef REG
2761 }
2762
2763 void
2764 pci_add_child(device_t bus, struct pci_devinfo *dinfo)
2765 {
2766         device_t pcib;
2767
2768         pcib = device_get_parent(bus);
2769         dinfo->cfg.dev = device_add_child(bus, NULL, -1);
2770         device_set_ivars(dinfo->cfg.dev, dinfo);
2771         resource_list_init(&dinfo->resources);
2772         pci_cfg_save(dinfo->cfg.dev, dinfo, 0);
2773         pci_cfg_restore(dinfo->cfg.dev, dinfo);
2774         pci_print_verbose(dinfo);
2775         pci_add_resources(pcib, bus, dinfo->cfg.dev, 0, 0);
2776 }
2777
2778 static int
2779 pci_probe(device_t dev)
2780 {
2781         device_set_desc(dev, "PCI bus");
2782
2783         /* Allow other subclasses to override this driver. */
2784         return (-1000);
2785 }
2786
2787 static int
2788 pci_attach(device_t dev)
2789 {
2790         int busno, domain;
2791
2792         /*
2793          * Since there can be multiple independantly numbered PCI
2794          * busses on systems with multiple PCI domains, we can't use
2795          * the unit number to decide which bus we are probing. We ask
2796          * the parent pcib what our domain and bus numbers are.
2797          */
2798         domain = pcib_get_domain(dev);
2799         busno = pcib_get_bus(dev);
2800         if (bootverbose)
2801                 device_printf(dev, "domain=%d, physical bus=%d\n",
2802                     domain, busno);
2803
2804         pci_add_children(dev, domain, busno, sizeof(struct pci_devinfo));
2805
2806         return (bus_generic_attach(dev));
2807 }
2808
2809 int
2810 pci_suspend(device_t dev)
2811 {
2812         int dstate, error, i, numdevs;
2813         device_t acpi_dev, child, *devlist;
2814         struct pci_devinfo *dinfo;
2815
2816         /*
2817          * Save the PCI configuration space for each child and set the
2818          * device in the appropriate power state for this sleep state.
2819          */
2820         acpi_dev = NULL;
2821         if (pci_do_power_resume)
2822                 acpi_dev = devclass_get_device(devclass_find("acpi"), 0);
2823         device_get_children(dev, &devlist, &numdevs);
2824         for (i = 0; i < numdevs; i++) {
2825                 child = devlist[i];
2826                 dinfo = (struct pci_devinfo *) device_get_ivars(child);
2827                 pci_cfg_save(child, dinfo, 0);
2828         }
2829
2830         /* Suspend devices before potentially powering them down. */
2831         error = bus_generic_suspend(dev);
2832         if (error) {
2833                 kfree(devlist, M_TEMP);
2834                 return (error);
2835         }
2836
2837         /*
2838          * Always set the device to D3.  If ACPI suggests a different
2839          * power state, use it instead.  If ACPI is not present, the
2840          * firmware is responsible for managing device power.  Skip
2841          * children who aren't attached since they are powered down
2842          * separately.  Only manage type 0 devices for now.
2843          */
2844         for (i = 0; acpi_dev && i < numdevs; i++) {
2845                 child = devlist[i];
2846                 dinfo = (struct pci_devinfo *) device_get_ivars(child);
2847                 if (device_is_attached(child) && dinfo->cfg.hdrtype == 0) {
2848                         dstate = PCI_POWERSTATE_D3;
2849                         ACPI_PWR_FOR_SLEEP(acpi_dev, child, &dstate);
2850                         pci_set_powerstate(child, dstate);
2851                 }
2852         }
2853         kfree(devlist, M_TEMP);
2854         return (0);
2855 }
2856
2857 int
2858 pci_resume(device_t dev)
2859 {
2860         int i, numdevs;
2861         device_t acpi_dev, child, *devlist;
2862         struct pci_devinfo *dinfo;
2863
2864         /*
2865          * Set each child to D0 and restore its PCI configuration space.
2866          */
2867         acpi_dev = NULL;
2868         if (pci_do_power_resume)
2869                 acpi_dev = devclass_get_device(devclass_find("acpi"), 0);
2870         device_get_children(dev, &devlist, &numdevs);
2871         for (i = 0; i < numdevs; i++) {
2872                 /*
2873                  * Notify ACPI we're going to D0 but ignore the result.  If
2874                  * ACPI is not present, the firmware is responsible for
2875                  * managing device power.  Only manage type 0 devices for now.
2876                  */
2877                 child = devlist[i];
2878                 dinfo = (struct pci_devinfo *) device_get_ivars(child);
2879                 if (acpi_dev && device_is_attached(child) &&
2880                     dinfo->cfg.hdrtype == 0) {
2881                         ACPI_PWR_FOR_SLEEP(acpi_dev, child, NULL);
2882                         pci_set_powerstate(child, PCI_POWERSTATE_D0);
2883                 }
2884
2885                 /* Now the device is powered up, restore its config space. */
2886                 pci_cfg_restore(child, dinfo);
2887         }
2888         kfree(devlist, M_TEMP);
2889         return (bus_generic_resume(dev));
2890 }
2891
2892 static void
2893 pci_load_vendor_data(void)
2894 {
2895         caddr_t vendordata, info;
2896
2897         if ((vendordata = preload_search_by_type("pci_vendor_data")) != NULL) {
2898                 info = preload_search_info(vendordata, MODINFO_ADDR);
2899                 pci_vendordata = *(char **)info;
2900                 info = preload_search_info(vendordata, MODINFO_SIZE);
2901                 pci_vendordata_size = *(size_t *)info;
2902                 /* terminate the database */
2903                 pci_vendordata[pci_vendordata_size] = '\n';
2904         }
2905 }
2906
2907 void
2908 pci_driver_added(device_t dev, driver_t *driver)
2909 {
2910         int numdevs;
2911         device_t *devlist;
2912         device_t child;
2913         struct pci_devinfo *dinfo;
2914         int i;
2915
2916         if (bootverbose)
2917                 device_printf(dev, "driver added\n");
2918         DEVICE_IDENTIFY(driver, dev);
2919         device_get_children(dev, &devlist, &numdevs);
2920         for (i = 0; i < numdevs; i++) {
2921                 child = devlist[i];
2922                 if (device_get_state(child) != DS_NOTPRESENT)
2923                         continue;
2924                 dinfo = device_get_ivars(child);
2925                 pci_print_verbose(dinfo);
2926                 if (bootverbose)
2927                         kprintf("pci%d:%d:%d:%d: reprobing on driver added\n",
2928                             dinfo->cfg.domain, dinfo->cfg.bus, dinfo->cfg.slot,
2929                             dinfo->cfg.func);
2930                 pci_cfg_restore(child, dinfo);
2931                 if (device_probe_and_attach(child) != 0)
2932                         pci_cfg_save(child, dinfo, 1);
2933         }
2934         kfree(devlist, M_TEMP);
2935 }
2936
2937 static void
2938 pci_child_detached(device_t parent __unused, device_t child)
2939 {
2940         /* Turn child's power off */
2941         pci_cfg_save(child, device_get_ivars(child), 1);
2942 }
2943
2944 int
2945 pci_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
2946     driver_intr_t *intr, void *arg, void **cookiep, lwkt_serialize_t serializer)
2947 {
2948         struct pci_devinfo *dinfo;
2949         struct msix_table_entry *mte;
2950         struct msix_vector *mv;
2951         uint64_t addr;
2952         uint32_t data;
2953         int rid, error;
2954         void *cookie;
2955
2956         error = bus_generic_setup_intr(dev, child, irq, flags, intr,
2957             arg, &cookie, serializer);
2958         if (error)
2959                 return (error);
2960
2961         /* If this is not a direct child, just bail out. */
2962         if (device_get_parent(child) != dev) {
2963                 *cookiep = cookie;
2964                 return(0);
2965         }
2966
2967         rid = rman_get_rid(irq);
2968         if (rid == 0) {
2969                 /* Make sure that INTx is enabled */
2970                 pci_clear_command_bit(dev, child, PCIM_CMD_INTxDIS);
2971         } else {
2972                 /*
2973                  * Check to see if the interrupt is MSI or MSI-X.
2974                  * Ask our parent to map the MSI and give
2975                  * us the address and data register values.
2976                  * If we fail for some reason, teardown the
2977                  * interrupt handler.
2978                  */
2979                 dinfo = device_get_ivars(child);
2980                 if (dinfo->cfg.msi.msi_alloc > 0) {
2981                         if (dinfo->cfg.msi.msi_addr == 0) {
2982                                 KASSERT(dinfo->cfg.msi.msi_handlers == 0,
2983                             ("MSI has handlers, but vectors not mapped"));
2984                                 error = PCIB_MAP_MSI(device_get_parent(dev),
2985                                     child, rman_get_start(irq), &addr, &data,
2986                                     rman_get_cpuid(irq));
2987                                 if (error)
2988                                         goto bad;
2989                                 dinfo->cfg.msi.msi_addr = addr;
2990                                 dinfo->cfg.msi.msi_data = data;
2991                                 pci_enable_msi(child, addr, data);
2992                         }
2993                         dinfo->cfg.msi.msi_handlers++;
2994                 } else {
2995                         KASSERT(dinfo->cfg.msix.msix_alloc > 0,
2996                             ("No MSI or MSI-X interrupts allocated"));
2997                         KASSERT(rid <= dinfo->cfg.msix.msix_table_len,
2998                             ("MSI-X index too high"));
2999                         mte = &dinfo->cfg.msix.msix_table[rid - 1];
3000                         KASSERT(mte->mte_vector != 0, ("no message vector"));
3001                         mv = &dinfo->cfg.msix.msix_vectors[mte->mte_vector - 1];
3002                         KASSERT(mv->mv_irq == rman_get_start(irq),
3003                             ("IRQ mismatch"));
3004                         if (mv->mv_address == 0) {
3005                                 KASSERT(mte->mte_handlers == 0,
3006                     ("MSI-X table entry has handlers, but vector not mapped"));
3007                                 error = PCIB_MAP_MSI(device_get_parent(dev),
3008                                     child, rman_get_start(irq), &addr, &data,
3009                                     rman_get_cpuid(irq));
3010                                 if (error)
3011                                         goto bad;
3012                                 mv->mv_address = addr;
3013                                 mv->mv_data = data;
3014                         }
3015                         if (mte->mte_handlers == 0) {
3016                                 pci_enable_msix(child, rid - 1, mv->mv_address,
3017                                     mv->mv_data);
3018                                 pci_unmask_msix(child, rid - 1);
3019                         }
3020                         mte->mte_handlers++;
3021                 }
3022
3023                 /* Make sure that INTx is disabled if we are using MSI/MSIX */
3024                 pci_set_command_bit(dev, child, PCIM_CMD_INTxDIS);
3025         bad:
3026                 if (error) {
3027                         (void)bus_generic_teardown_intr(dev, child, irq,
3028                             cookie);
3029                         return (error);
3030                 }
3031         }
3032         *cookiep = cookie;
3033         return (0);
3034 }
3035
3036 int
3037 pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
3038     void *cookie)
3039 {
3040         struct msix_table_entry *mte;
3041         struct resource_list_entry *rle;
3042         struct pci_devinfo *dinfo;
3043         int rid, error;
3044
3045         if (irq == NULL || !(rman_get_flags(irq) & RF_ACTIVE))
3046                 return (EINVAL);
3047
3048         /* If this isn't a direct child, just bail out */
3049         if (device_get_parent(child) != dev)
3050                 return(bus_generic_teardown_intr(dev, child, irq, cookie));
3051
3052         rid = rman_get_rid(irq);
3053         if (rid == 0) {
3054                 /* Mask INTx */
3055                 pci_set_command_bit(dev, child, PCIM_CMD_INTxDIS);
3056         } else {
3057                 /*
3058                  * Check to see if the interrupt is MSI or MSI-X.  If so,
3059                  * decrement the appropriate handlers count and mask the
3060                  * MSI-X message, or disable MSI messages if the count
3061                  * drops to 0.
3062                  */
3063                 dinfo = device_get_ivars(child);
3064                 rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, rid);
3065                 if (rle->res != irq)
3066                         return (EINVAL);
3067                 if (dinfo->cfg.msi.msi_alloc > 0) {
3068                         KASSERT(rid <= dinfo->cfg.msi.msi_alloc,
3069                             ("MSI-X index too high"));
3070                         if (dinfo->cfg.msi.msi_handlers == 0)
3071                                 return (EINVAL);
3072                         dinfo->cfg.msi.msi_handlers--;
3073                         if (dinfo->cfg.msi.msi_handlers == 0)
3074                                 pci_disable_msi(child);
3075                 } else {
3076                         KASSERT(dinfo->cfg.msix.msix_alloc > 0,
3077                             ("No MSI or MSI-X interrupts allocated"));
3078                         KASSERT(rid <= dinfo->cfg.msix.msix_table_len,
3079                             ("MSI-X index too high"));
3080                         mte = &dinfo->cfg.msix.msix_table[rid - 1];
3081                         if (mte->mte_handlers == 0)
3082                                 return (EINVAL);
3083                         mte->mte_handlers--;
3084                         if (mte->mte_handlers == 0)
3085                                 pci_mask_msix(child, rid - 1);
3086                 }
3087         }
3088         error = bus_generic_teardown_intr(dev, child, irq, cookie);
3089         if (rid > 0)
3090                 KASSERT(error == 0,
3091                     ("%s: generic teardown failed for MSI/MSI-X", __func__));
3092         return (error);
3093 }
3094
3095 int
3096 pci_print_child(device_t dev, device_t child)
3097 {
3098         struct pci_devinfo *dinfo;
3099         struct resource_list *rl;
3100         int retval = 0;
3101
3102         dinfo = device_get_ivars(child);
3103         rl = &dinfo->resources;
3104
3105         retval += bus_print_child_header(dev, child);
3106
3107         retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
3108         retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
3109         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
3110         if (device_get_flags(dev))
3111                 retval += kprintf(" flags %#x", device_get_flags(dev));
3112
3113         retval += kprintf(" at device %d.%d", pci_get_slot(child),
3114             pci_get_function(child));
3115
3116         retval += bus_print_child_footer(dev, child);
3117
3118         return (retval);
3119 }
3120
3121 static struct
3122 {
3123         int     class;
3124         int     subclass;
3125         char    *desc;
3126 } pci_nomatch_tab[] = {
3127         {PCIC_OLD,              -1,                     "old"},
3128         {PCIC_OLD,              PCIS_OLD_NONVGA,        "non-VGA display device"},
3129         {PCIC_OLD,              PCIS_OLD_VGA,           "VGA-compatible display device"},
3130         {PCIC_STORAGE,          -1,                     "mass storage"},
3131         {PCIC_STORAGE,          PCIS_STORAGE_SCSI,      "SCSI"},
3132         {PCIC_STORAGE,          PCIS_STORAGE_IDE,       "ATA"},
3133         {PCIC_STORAGE,          PCIS_STORAGE_FLOPPY,    "floppy disk"},
3134         {PCIC_STORAGE,          PCIS_STORAGE_IPI,       "IPI"},
3135         {PCIC_STORAGE,          PCIS_STORAGE_RAID,      "RAID"},
3136         {PCIC_STORAGE,          PCIS_STORAGE_ATA_ADMA,  "ATA (ADMA)"},
3137         {PCIC_STORAGE,          PCIS_STORAGE_SATA,      "SATA"},
3138         {PCIC_STORAGE,          PCIS_STORAGE_SAS,       "SAS"},
3139         {PCIC_NETWORK,          -1,                     "network"},
3140         {PCIC_NETWORK,          PCIS_NETWORK_ETHERNET,  "ethernet"},
3141         {PCIC_NETWORK,          PCIS_NETWORK_TOKENRING, "token ring"},
3142         {PCIC_NETWORK,          PCIS_NETWORK_FDDI,      "fddi"},
3143         {PCIC_NETWORK,          PCIS_NETWORK_ATM,       "ATM"},
3144         {PCIC_NETWORK,          PCIS_NETWORK_ISDN,      "ISDN"},
3145         {PCIC_DISPLAY,          -1,                     "display"},
3146         {PCIC_DISPLAY,          PCIS_DISPLAY_VGA,       "VGA"},
3147         {PCIC_DISPLAY,          PCIS_DISPLAY_XGA,       "XGA"},
3148         {PCIC_DISPLAY,          PCIS_DISPLAY_3D,        "3D"},
3149         {PCIC_MULTIMEDIA,       -1,                     "multimedia"},
3150         {PCIC_MULTIMEDIA,       PCIS_MULTIMEDIA_VIDEO,  "video"},
3151         {PCIC_MULTIMEDIA,       PCIS_MULTIMEDIA_AUDIO,  "audio"},
3152         {PCIC_MULTIMEDIA,       PCIS_MULTIMEDIA_TELE,   "telephony"},
3153         {PCIC_MULTIMEDIA,       PCIS_MULTIMEDIA_HDA,    "HDA"},
3154         {PCIC_MEMORY,           -1,                     "memory"},
3155         {PCIC_MEMORY,           PCIS_MEMORY_RAM,        "RAM"},
3156         {PCIC_MEMORY,           PCIS_MEMORY_FLASH,      "flash"},
3157         {PCIC_BRIDGE,           -1,                     "bridge"},
3158         {PCIC_BRIDGE,           PCIS_BRIDGE_HOST,       "HOST-PCI"},
3159         {PCIC_BRIDGE,           PCIS_BRIDGE_ISA,        "PCI-ISA"},
3160         {PCIC_BRIDGE,           PCIS_BRIDGE_EISA,       "PCI-EISA"},
3161         {PCIC_BRIDGE,           PCIS_BRIDGE_MCA,        "PCI-MCA"},
3162         {PCIC_BRIDGE,           PCIS_BRIDGE_PCI,        "PCI-PCI"},
3163         {PCIC_BRIDGE,           PCIS_BRIDGE_PCMCIA,     "PCI-PCMCIA"},
3164         {PCIC_BRIDGE,           PCIS_BRIDGE_NUBUS,      "PCI-NuBus"},
3165         {PCIC_BRIDGE,           PCIS_BRIDGE_CARDBUS,    "PCI-CardBus"},
3166         {PCIC_BRIDGE,           PCIS_BRIDGE_RACEWAY,    "PCI-RACEway"},
3167         {PCIC_SIMPLECOMM,       -1,                     "simple comms"},
3168         {PCIC_SIMPLECOMM,       PCIS_SIMPLECOMM_UART,   "UART"},        /* could detect 16550 */
3169         {PCIC_SIMPLECOMM,       PCIS_SIMPLECOMM_PAR,    "parallel port"},
3170         {PCIC_SIMPLECOMM,       PCIS_SIMPLECOMM_MULSER, "multiport serial"},
3171         {PCIC_SIMPLECOMM,       PCIS_SIMPLECOMM_MODEM,  "generic modem"},
3172         {PCIC_BASEPERIPH,       -1,                     "base peripheral"},
3173         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_PIC,    "interrupt controller"},
3174         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_DMA,    "DMA controller"},
3175         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_TIMER,  "timer"},
3176         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_RTC,    "realtime clock"},
3177         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_PCIHOT, "PCI hot-plug controller"},
3178         {PCIC_BASEPERIPH,       PCIS_BASEPERIPH_SDHC,   "SD host controller"},
3179         {PCIC_INPUTDEV,         -1,                     "input device"},
3180         {PCIC_INPUTDEV,         PCIS_INPUTDEV_KEYBOARD, "keyboard"},
3181         {PCIC_INPUTDEV,         PCIS_INPUTDEV_DIGITIZER,"digitizer"},
3182         {PCIC_INPUTDEV,         PCIS_INPUTDEV_MOUSE,    "mouse"},
3183         {PCIC_INPUTDEV,         PCIS_INPUTDEV_SCANNER,  "scanner"},
3184         {PCIC_INPUTDEV,         PCIS_INPUTDEV_GAMEPORT, "gameport"},
3185         {PCIC_DOCKING,          -1,                     "docking station"},
3186         {PCIC_PROCESSOR,        -1,                     "processor"},
3187         {PCIC_SERIALBUS,        -1,                     "serial bus"},
3188         {PCIC_SERIALBUS,        PCIS_SERIALBUS_FW,      "FireWire"},
3189         {PCIC_SERIALBUS,        PCIS_SERIALBUS_ACCESS,  "AccessBus"},
3190         {PCIC_SERIALBUS,        PCIS_SERIALBUS_SSA,     "SSA"},
3191         {PCIC_SERIALBUS,        PCIS_SERIALBUS_USB,     "USB"},
3192         {PCIC_SERIALBUS,        PCIS_SERIALBUS_FC,      "Fibre Channel"},
3193         {PCIC_SERIALBUS,        PCIS_SERIALBUS_SMBUS,   "SMBus"},
3194         {PCIC_WIRELESS,         -1,                     "wireless controller"},
3195         {PCIC_WIRELESS,         PCIS_WIRELESS_IRDA,     "iRDA"},
3196         {PCIC_WIRELESS,         PCIS_WIRELESS_IR,       "IR"},
3197         {PCIC_WIRELESS,         PCIS_WIRELESS_RF,       "RF"},
3198         {PCIC_INTELLIIO,        -1,                     "intelligent I/O controller"},
3199         {PCIC_INTELLIIO,        PCIS_INTELLIIO_I2O,     "I2O"},
3200         {PCIC_SATCOM,           -1,                     "satellite communication"},
3201         {PCIC_SATCOM,           PCIS_SATCOM_TV,         "sat TV"},
3202         {PCIC_SATCOM,           PCIS_SATCOM_AUDIO,      "sat audio"},
3203         {PCIC_SATCOM,           PCIS_SATCOM_VOICE,      "sat voice"},
3204         {PCIC_SATCOM,           PCIS_SATCOM_DATA,       "sat data"},
3205         {PCIC_CRYPTO,           -1,                     "encrypt/decrypt"},
3206         {PCIC_CRYPTO,           PCIS_CRYPTO_NETCOMP,    "network/computer crypto"},
3207         {PCIC_CRYPTO,           PCIS_CRYPTO_ENTERTAIN,  "entertainment crypto"},
3208         {PCIC_DASP,             -1,                     "dasp"},
3209         {PCIC_DASP,             PCIS_DASP_DPIO,         "DPIO module"},
3210         {0, 0,          NULL}
3211 };
3212
3213 void
3214 pci_probe_nomatch(device_t dev, device_t child)
3215 {
3216         int     i;
3217         char    *cp, *scp, *device;
3218
3219         /*
3220          * Look for a listing for this device in a loaded device database.
3221          */
3222         if ((device = pci_describe_device(child)) != NULL) {
3223                 device_printf(dev, "<%s>", device);
3224                 kfree(device, M_DEVBUF);
3225         } else {
3226                 /*
3227                  * Scan the class/subclass descriptions for a general
3228                  * description.
3229                  */
3230                 cp = "unknown";
3231                 scp = NULL;
3232                 for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
3233                         if (pci_nomatch_tab[i].class == pci_get_class(child)) {
3234                                 if (pci_nomatch_tab[i].subclass == -1) {
3235                                         cp = pci_nomatch_tab[i].desc;
3236                                 } else if (pci_nomatch_tab[i].subclass ==
3237                                     pci_get_subclass(child)) {
3238                                         scp = pci_nomatch_tab[i].desc;
3239                                 }
3240                         }
3241                 }
3242                 device_printf(dev, "<%s%s%s>",
3243                     cp ? cp : "",
3244                     ((cp != NULL) && (scp != NULL)) ? ", " : "",
3245                     scp ? scp : "");
3246         }
3247         kprintf(" (vendor 0x%04x, dev 0x%04x) at device %d.%d",
3248                 pci_get_vendor(child), pci_get_device(child),
3249                 pci_get_slot(child), pci_get_function(child));
3250         if (pci_get_intpin(child) > 0) {
3251                 int irq;
3252
3253                 irq = pci_get_irq(child);
3254                 if (PCI_INTERRUPT_VALID(irq))
3255                         kprintf(" irq %d", irq);
3256         }
3257         kprintf("\n");
3258
3259         pci_cfg_save(child, (struct pci_devinfo *)device_get_ivars(child), 1);
3260 }
3261
3262 /*
3263  * Parse the PCI device database, if loaded, and return a pointer to a
3264  * description of the device.
3265  *
3266  * The database is flat text formatted as follows:
3267  *
3268  * Any line not in a valid format is ignored.
3269  * Lines are terminated with newline '\n' characters.
3270  *
3271  * A VENDOR line consists of the 4 digit (hex) vendor code, a TAB, then
3272  * the vendor name.
3273  *
3274  * A DEVICE line is entered immediately below the corresponding VENDOR ID.
3275  * - devices cannot be listed without a corresponding VENDOR line.
3276  * A DEVICE line consists of a TAB, the 4 digit (hex) device code,
3277  * another TAB, then the device name.
3278  */
3279
3280 /*
3281  * Assuming (ptr) points to the beginning of a line in the database,
3282  * return the vendor or device and description of the next entry.
3283  * The value of (vendor) or (device) inappropriate for the entry type
3284  * is set to -1.  Returns nonzero at the end of the database.
3285  *
3286  * Note that this is slightly unrobust in the face of corrupt data;
3287  * we attempt to safeguard against this by spamming the end of the
3288  * database with a newline when we initialise.
3289  */
3290 static int
3291 pci_describe_parse_line(char **ptr, int *vendor, int *device, char **desc)
3292 {
3293         char    *cp = *ptr;
3294         int     left;
3295
3296         *device = -1;
3297         *vendor = -1;
3298         **desc = '\0';
3299         for (;;) {
3300                 left = pci_vendordata_size - (cp - pci_vendordata);
3301                 if (left <= 0) {
3302                         *ptr = cp;
3303                         return(1);
3304                 }
3305
3306                 /* vendor entry? */
3307                 if (*cp != '\t' &&
3308                     ksscanf(cp, "%x\t%80[^\n]", vendor, *desc) == 2)
3309                         break;
3310                 /* device entry? */
3311                 if (*cp == '\t' &&
3312                     ksscanf(cp, "%x\t%80[^\n]", device, *desc) == 2)
3313                         break;
3314
3315                 /* skip to next line */
3316                 while (*cp != '\n' && left > 0) {
3317                         cp++;
3318                         left--;
3319                 }
3320                 if (*cp == '\n') {
3321                         cp++;
3322                         left--;
3323                 }
3324         }
3325         /* skip to next line */
3326         while (*cp != '\n' && left > 0) {
3327                 cp++;
3328                 left--;
3329         }
3330         if (*cp == '\n' && left > 0)
3331                 cp++;
3332         *ptr = cp;
3333         return(0);
3334 }
3335
3336 static char *
3337 pci_describe_device(device_t dev)
3338 {
3339         int     vendor, device;
3340         char    *desc, *vp, *dp, *line;
3341
3342         desc = vp = dp = NULL;
3343
3344         /*
3345          * If we have no vendor data, we can't do anything.
3346          */
3347         if (pci_vendordata == NULL)
3348                 goto out;
3349
3350         /*
3351          * Scan the vendor data looking for this device
3352          */
3353         line = pci_vendordata;
3354         if ((vp = kmalloc(80, M_DEVBUF, M_NOWAIT)) == NULL)
3355                 goto out;
3356         for (;;) {
3357                 if (pci_describe_parse_line(&line, &vendor, &device, &vp))
3358                         goto out;
3359                 if (vendor == pci_get_vendor(dev))
3360                         break;
3361         }
3362         if ((dp = kmalloc(80, M_DEVBUF, M_NOWAIT)) == NULL)
3363                 goto out;
3364         for (;;) {
3365                 if (pci_describe_parse_line(&line, &vendor, &device, &dp)) {
3366                         *dp = 0;
3367                         break;
3368                 }
3369                 if (vendor != -1) {
3370                         *dp = 0;
3371                         break;
3372                 }
3373                 if (device == pci_get_device(dev))
3374                         break;
3375         }
3376         if (dp[0] == '\0')
3377                 ksnprintf(dp, 80, "0x%x", pci_get_device(dev));
3378         if ((desc = kmalloc(strlen(vp) + strlen(dp) + 3, M_DEVBUF, M_NOWAIT)) !=
3379             NULL)
3380                 ksprintf(desc, "%s, %s", vp, dp);
3381  out:
3382         if (vp != NULL)
3383                 kfree(vp, M_DEVBUF);
3384         if (dp != NULL)
3385                 kfree(dp, M_DEVBUF);
3386         return(desc);
3387 }
3388
3389 int
3390 pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
3391 {
3392         struct pci_devinfo *dinfo;
3393         pcicfgregs *cfg;
3394
3395         dinfo = device_get_ivars(child);
3396         cfg = &dinfo->cfg;
3397
3398         switch (which) {
3399         case PCI_IVAR_ETHADDR:
3400                 /*
3401                  * The generic accessor doesn't deal with failure, so
3402                  * we set the return value, then return an error.
3403                  */
3404                 *((uint8_t **) result) = NULL;
3405                 return (EINVAL);
3406         case PCI_IVAR_SUBVENDOR:
3407                 *result = cfg->subvendor;
3408                 break;
3409         case PCI_IVAR_SUBDEVICE:
3410                 *result = cfg->subdevice;
3411                 break;
3412         case PCI_IVAR_VENDOR:
3413                 *result = cfg->vendor;
3414                 break;
3415         case PCI_IVAR_DEVICE:
3416                 *result = cfg->device;
3417                 break;
3418         case PCI_IVAR_DEVID:
3419                 *result = (cfg->device << 16) | cfg->vendor;
3420                 break;
3421         case PCI_IVAR_CLASS:
3422                 *result = cfg->baseclass;
3423                 break;
3424         case PCI_IVAR_SUBCLASS:
3425                 *result = cfg->subclass;
3426                 break;
3427         case PCI_IVAR_PROGIF:
3428                 *result = cfg->progif;
3429                 break;
3430         case PCI_IVAR_REVID:
3431                 *result = cfg->revid;
3432                 break;
3433         case PCI_IVAR_INTPIN:
3434                 *result = cfg->intpin;
3435                 break;
3436         case PCI_IVAR_IRQ:
3437                 *result = cfg->intline;
3438                 break;
3439         case PCI_IVAR_DOMAIN:
3440                 *result = cfg->domain;
3441                 break;
3442         case PCI_IVAR_BUS:
3443                 *result = cfg->bus;
3444                 break;
3445         case PCI_IVAR_SLOT:
3446                 *result = cfg->slot;
3447                 break;
3448         case PCI_IVAR_FUNCTION:
3449                 *result = cfg->func;
3450                 break;
3451         case PCI_IVAR_CMDREG:
3452                 *result = cfg->cmdreg;
3453                 break;
3454         case PCI_IVAR_CACHELNSZ:
3455                 *result = cfg->cachelnsz;
3456                 break;
3457         case PCI_IVAR_MINGNT:
3458                 *result = cfg->mingnt;
3459                 break;
3460         case PCI_IVAR_MAXLAT:
3461                 *result = cfg->maxlat;
3462                 break;
3463         case PCI_IVAR_LATTIMER:
3464                 *result = cfg->lattimer;
3465                 break;
3466         case PCI_IVAR_PCIXCAP_PTR:
3467                 *result = cfg->pcix.pcix_ptr;
3468                 break;
3469         case PCI_IVAR_PCIECAP_PTR:
3470                 *result = cfg->expr.expr_ptr;
3471                 break;
3472         case PCI_IVAR_VPDCAP_PTR:
3473                 *result = cfg->vpd.vpd_reg;
3474                 break;
3475         default:
3476                 return (ENOENT);
3477         }
3478         return (0);
3479 }
3480
3481 int
3482 pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
3483 {
3484         struct pci_devinfo *dinfo;
3485
3486         dinfo = device_get_ivars(child);
3487
3488         switch (which) {
3489         case PCI_IVAR_INTPIN:
3490                 dinfo->cfg.intpin = value;
3491                 return (0);
3492         case PCI_IVAR_ETHADDR:
3493         case PCI_IVAR_SUBVENDOR:
3494         case PCI_IVAR_SUBDEVICE:
3495         case PCI_IVAR_VENDOR:
3496         case PCI_IVAR_DEVICE:
3497         case PCI_IVAR_DEVID:
3498         case PCI_IVAR_CLASS:
3499         case PCI_IVAR_SUBCLASS:
3500         case PCI_IVAR_PROGIF:
3501         case PCI_IVAR_REVID:
3502         case PCI_IVAR_IRQ:
3503         case PCI_IVAR_DOMAIN:
3504         case PCI_IVAR_BUS:
3505         case PCI_IVAR_SLOT:
3506         case PCI_IVAR_FUNCTION:
3507                 return (EINVAL);        /* disallow for now */
3508
3509         default:
3510                 return (ENOENT);
3511         }
3512 }
3513 #ifdef notyet
3514 #include "opt_ddb.h"
3515 #ifdef DDB
3516 #include <ddb/ddb.h>
3517 #include <sys/cons.h>
3518
3519 /*
3520  * List resources based on pci map registers, used for within ddb
3521  */
3522
3523 DB_SHOW_COMMAND(pciregs, db_pci_dump)
3524 {
3525         struct pci_devinfo *dinfo;
3526         struct devlist *devlist_head;
3527         struct pci_conf *p;
3528         const char *name;
3529         int i, error, none_count;
3530
3531         none_count = 0;
3532         /* get the head of the device queue */
3533         devlist_head = &pci_devq;
3534
3535         /*
3536          * Go through the list of devices and print out devices
3537          */
3538         for (error = 0, i = 0,
3539              dinfo = STAILQ_FIRST(devlist_head);
3540              (dinfo != NULL) && (error == 0) && (i < pci_numdevs) && !db_pager_quit;
3541              dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
3542
3543                 /* Populate pd_name and pd_unit */
3544                 name = NULL;
3545                 if (dinfo->cfg.dev)
3546                         name = device_get_name(dinfo->cfg.dev);
3547
3548                 p = &dinfo->conf;
3549                 db_kprintf("%s%d@pci%d:%d:%d:%d:\tclass=0x%06x card=0x%08x "
3550                         "chip=0x%08x rev=0x%02x hdr=0x%02x\n",
3551                         (name && *name) ? name : "none",
3552                         (name && *name) ? (int)device_get_unit(dinfo->cfg.dev) :
3553                         none_count++,
3554                         p->pc_sel.pc_domain, p->pc_sel.pc_bus, p->pc_sel.pc_dev,
3555                         p->pc_sel.pc_func, (p->pc_class << 16) |
3556                         (p->pc_subclass << 8) | p->pc_progif,
3557                         (p->pc_subdevice << 16) | p->pc_subvendor,
3558                         (p->pc_device << 16) | p->pc_vendor,
3559                         p->pc_revid, p->pc_hdr);
3560         }
3561 }
3562 #endif /* DDB */
3563 #endif
3564
3565 static struct resource *
3566 pci_alloc_map(device_t dev, device_t child, int type, int *rid,
3567     u_long start, u_long end, u_long count, u_int flags)
3568 {
3569         struct pci_devinfo *dinfo = device_get_ivars(child);
3570         struct resource_list *rl = &dinfo->resources;
3571         struct resource_list_entry *rle;
3572         struct resource *res;
3573         pci_addr_t map, testval;
3574         int mapsize;
3575
3576         /*
3577          * Weed out the bogons, and figure out how large the BAR/map
3578          * is.  Bars that read back 0 here are bogus and unimplemented.
3579          * Note: atapci in legacy mode are special and handled elsewhere
3580          * in the code.  If you have a atapci device in legacy mode and
3581          * it fails here, that other code is broken.
3582          */
3583         res = NULL;
3584         map = pci_read_config(child, *rid, 4);
3585         pci_write_config(child, *rid, 0xffffffff, 4);
3586         testval = pci_read_config(child, *rid, 4);
3587         if (pci_maprange(testval) == 64)
3588                 map |= (pci_addr_t)pci_read_config(child, *rid + 4, 4) << 32;
3589         if (pci_mapbase(testval) == 0)
3590                 goto out;
3591
3592         /*
3593          * Restore the original value of the BAR.  We may have reprogrammed
3594          * the BAR of the low-level console device and when booting verbose,
3595          * we need the console device addressable.
3596          */
3597         pci_write_config(child, *rid, map, 4);
3598
3599         if (PCI_BAR_MEM(testval)) {
3600                 if (type != SYS_RES_MEMORY) {
3601                         if (bootverbose)
3602                                 device_printf(dev,
3603                                     "child %s requested type %d for rid %#x,"
3604                                     " but the BAR says it is an memio\n",
3605                                     device_get_nameunit(child), type, *rid);
3606                         goto out;
3607                 }
3608         } else {
3609                 if (type != SYS_RES_IOPORT) {
3610                         if (bootverbose)
3611                                 device_printf(dev,
3612                                     "child %s requested type %d for rid %#x,"
3613                                     " but the BAR says it is an ioport\n",
3614                                     device_get_nameunit(child), type, *rid);
3615                         goto out;
3616                 }
3617         }
3618         /*
3619          * For real BARs, we need to override the size that
3620          * the driver requests, because that's what the BAR
3621          * actually uses and we would otherwise have a
3622          * situation where we might allocate the excess to
3623          * another driver, which won't work.
3624          */
3625         mapsize = pci_mapsize(testval);
3626         count = 1UL << mapsize;
3627         if (RF_ALIGNMENT(flags) < mapsize)
3628                 flags = (flags & ~RF_ALIGNMENT_MASK) | RF_ALIGNMENT_LOG2(mapsize);
3629         if (PCI_BAR_MEM(testval) && (testval & PCIM_BAR_MEM_PREFETCH))
3630                 flags |= RF_PREFETCHABLE;
3631
3632         /*
3633          * Allocate enough resource, and then write back the
3634          * appropriate bar for that resource.
3635          */
3636         res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child, type, rid,
3637             start, end, count, flags, -1);
3638         if (res == NULL) {
3639                 device_printf(child,
3640                     "%#lx bytes of rid %#x res %d failed (%#lx, %#lx).\n",
3641                     count, *rid, type, start, end);
3642                 goto out;
3643         }
3644         resource_list_add(rl, type, *rid, start, end, count, -1);
3645         rle = resource_list_find(rl, type, *rid);
3646         if (rle == NULL)
3647                 panic("pci_alloc_map: unexpectedly can't find resource.");
3648         rle->res = res;
3649         rle->start = rman_get_start(res);
3650         rle->end = rman_get_end(res);
3651         rle->count = count;
3652         if (bootverbose)
3653                 device_printf(child,
3654                     "Lazy allocation of %#lx bytes rid %#x type %d at %#lx\n",
3655                     count, *rid, type, rman_get_start(res));
3656         map = rman_get_start(res);
3657 out:;
3658         pci_write_config(child, *rid, map, 4);
3659         if (pci_maprange(testval) == 64)
3660                 pci_write_config(child, *rid + 4, map >> 32, 4);
3661         return (res);
3662 }
3663
3664
3665 struct resource *
3666 pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
3667     u_long start, u_long end, u_long count, u_int flags, int cpuid)
3668 {
3669         struct pci_devinfo *dinfo = device_get_ivars(child);
3670         struct resource_list *rl = &dinfo->resources;
3671         struct resource_list_entry *rle;
3672         pcicfgregs *cfg = &dinfo->cfg;
3673
3674         /*
3675          * Perform lazy resource allocation
3676          */
3677         if (device_get_parent(child) == dev) {
3678                 switch (type) {
3679                 case SYS_RES_IRQ:
3680                         /*
3681                          * Can't alloc legacy interrupt once MSI messages
3682                          * have been allocated.
3683                          */
3684                         if (*rid == 0 && (cfg->msi.msi_alloc > 0 ||
3685                             cfg->msix.msix_alloc > 0))
3686                                 return (NULL);
3687                         /*
3688                          * If the child device doesn't have an
3689                          * interrupt routed and is deserving of an
3690                          * interrupt, try to assign it one.
3691                          */
3692                         if (*rid == 0 && !PCI_INTERRUPT_VALID(cfg->intline) &&
3693                             (cfg->intpin != 0))
3694                                 pci_assign_interrupt(dev, child, 0);
3695                         break;
3696                 case SYS_RES_IOPORT:
3697                 case SYS_RES_MEMORY:
3698                         if (*rid < PCIR_BAR(cfg->nummaps)) {
3699                                 /*
3700                                  * Enable the I/O mode.  We should
3701                                  * also be assigning resources too
3702                                  * when none are present.  The
3703                                  * resource_list_alloc kind of sorta does
3704                                  * this...
3705                                  */
3706                                 if (PCI_ENABLE_IO(dev, child, type))
3707                                         return (NULL);
3708                         }
3709                         rle = resource_list_find(rl, type, *rid);
3710                         if (rle == NULL)
3711                                 return (pci_alloc_map(dev, child, type, rid,
3712                                     start, end, count, flags));
3713                         break;
3714                 }
3715                 /*
3716                  * If we've already allocated the resource, then
3717                  * return it now.  But first we may need to activate
3718                  * it, since we don't allocate the resource as active
3719                  * above.  Normally this would be done down in the
3720                  * nexus, but since we short-circuit that path we have
3721                  * to do its job here.  Not sure if we should kfree the
3722                  * resource if it fails to activate.
3723                  */
3724                 rle = resource_list_find(rl, type, *rid);
3725                 if (rle != NULL && rle->res != NULL) {
3726                         if (bootverbose)
3727                                 device_printf(child,
3728                             "Reserved %#lx bytes for rid %#x type %d at %#lx\n",
3729                                     rman_get_size(rle->res), *rid, type,
3730                                     rman_get_start(rle->res));
3731                         if ((flags & RF_ACTIVE) &&
3732                             bus_generic_activate_resource(dev, child, type,
3733                             *rid, rle->res) != 0)
3734                                 return (NULL);
3735                         return (rle->res);
3736                 }
3737         }
3738         return (resource_list_alloc(rl, dev, child, type, rid,
3739             start, end, count, flags, cpuid));
3740 }
3741
3742 void
3743 pci_delete_resource(device_t dev, device_t child, int type, int rid)
3744 {
3745         struct pci_devinfo *dinfo;
3746         struct resource_list *rl;
3747         struct resource_list_entry *rle;
3748
3749         if (device_get_parent(child) != dev)
3750                 return;
3751
3752         dinfo = device_get_ivars(child);
3753         rl = &dinfo->resources;
3754         rle = resource_list_find(rl, type, rid);
3755         if (rle) {
3756                 if (rle->res) {
3757                         if (rman_get_device(rle->res) != dev ||
3758                             rman_get_flags(rle->res) & RF_ACTIVE) {
3759                                 device_printf(dev, "delete_resource: "
3760                                     "Resource still owned by child, oops. "
3761                                     "(type=%d, rid=%d, addr=%lx)\n",
3762                                     rle->type, rle->rid,
3763                                     rman_get_start(rle->res));
3764                                 return;
3765                         }
3766                         bus_release_resource(dev, type, rid, rle->res);
3767                 }
3768                 resource_list_delete(rl, type, rid);
3769         }
3770         /*
3771          * Why do we turn off the PCI configuration BAR when we delete a
3772          * resource? -- imp
3773          */
3774         pci_write_config(child, rid, 0, 4);
3775         BUS_DELETE_RESOURCE(device_get_parent(dev), child, type, rid);
3776 }
3777
3778 struct resource_list *
3779 pci_get_resource_list (device_t dev, device_t child)
3780 {
3781         struct pci_devinfo *dinfo = device_get_ivars(child);
3782
3783         if (dinfo == NULL)
3784                 return (NULL);
3785
3786         return (&dinfo->resources);
3787 }
3788
3789 uint32_t
3790 pci_read_config_method(device_t dev, device_t child, int reg, int width)
3791 {
3792         struct pci_devinfo *dinfo = device_get_ivars(child);
3793         pcicfgregs *cfg = &dinfo->cfg;
3794
3795         return (PCIB_READ_CONFIG(device_get_parent(dev),
3796             cfg->bus, cfg->slot, cfg->func, reg, width));
3797 }
3798
3799 void
3800 pci_write_config_method(device_t dev, device_t child, int reg,
3801     uint32_t val, int width)
3802 {
3803         struct pci_devinfo *dinfo = device_get_ivars(child);
3804         pcicfgregs *cfg = &dinfo->cfg;
3805
3806         PCIB_WRITE_CONFIG(device_get_parent(dev),
3807             cfg->bus, cfg->slot, cfg->func, reg, val, width);
3808 }
3809
3810 int
3811 pci_child_location_str_method(device_t dev, device_t child, char *buf,
3812     size_t buflen)
3813 {
3814
3815         ksnprintf(buf, buflen, "slot=%d function=%d", pci_get_slot(child),
3816             pci_get_function(child));
3817         return (0);
3818 }
3819
3820 int
3821 pci_child_pnpinfo_str_method(device_t dev, device_t child, char *buf,
3822     size_t buflen)
3823 {
3824         struct pci_devinfo *dinfo;
3825         pcicfgregs *cfg;
3826
3827         dinfo = device_get_ivars(child);
3828         cfg = &dinfo->cfg;
3829         ksnprintf(buf, buflen, "vendor=0x%04x device=0x%04x subvendor=0x%04x "
3830             "subdevice=0x%04x class=0x%02x%02x%02x", cfg->vendor, cfg->device,
3831             cfg->subvendor, cfg->subdevice, cfg->baseclass, cfg->subclass,
3832             cfg->progif);
3833         return (0);
3834 }
3835
3836 int
3837 pci_assign_interrupt_method(device_t dev, device_t child)
3838 {
3839         struct pci_devinfo *dinfo = device_get_ivars(child);
3840         pcicfgregs *cfg = &dinfo->cfg;
3841
3842         return (PCIB_ROUTE_INTERRUPT(device_get_parent(dev), child,
3843             cfg->intpin));
3844 }
3845
3846 static int
3847 pci_modevent(module_t mod, int what, void *arg)
3848 {
3849         static struct cdev *pci_cdev;
3850
3851         switch (what) {
3852         case MOD_LOAD:
3853                 STAILQ_INIT(&pci_devq);
3854                 pci_generation = 0;
3855                 pci_cdev = make_dev(&pcic_ops, 0, UID_ROOT, GID_WHEEL, 0644,
3856                                     "pci");
3857                 pci_load_vendor_data();
3858                 break;
3859
3860         case MOD_UNLOAD:
3861                 destroy_dev(pci_cdev);
3862                 break;
3863         }
3864
3865         return (0);
3866 }
3867
3868 void
3869 pci_cfg_restore(device_t dev, struct pci_devinfo *dinfo)
3870 {
3871         int i;
3872
3873         /*
3874          * Only do header type 0 devices.  Type 1 devices are bridges,
3875          * which we know need special treatment.  Type 2 devices are
3876          * cardbus bridges which also require special treatment.
3877          * Other types are unknown, and we err on the side of safety
3878          * by ignoring them.
3879          */
3880         if (dinfo->cfg.hdrtype != 0)
3881                 return;
3882
3883         /*
3884          * Restore the device to full power mode.  We must do this
3885          * before we restore the registers because moving from D3 to
3886          * D0 will cause the chip's BARs and some other registers to
3887          * be reset to some unknown power on reset values.  Cut down
3888          * the noise on boot by doing nothing if we are already in
3889          * state D0.
3890          */
3891         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
3892                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
3893         }
3894         for (i = 0; i < dinfo->cfg.nummaps; i++)
3895                 pci_write_config(dev, PCIR_BAR(i), dinfo->cfg.bar[i], 4);
3896         pci_write_config(dev, PCIR_BIOS, dinfo->cfg.bios, 4);
3897         pci_write_config(dev, PCIR_COMMAND, dinfo->cfg.cmdreg, 2);
3898         pci_write_config(dev, PCIR_INTLINE, dinfo->cfg.intline, 1);
3899         pci_write_config(dev, PCIR_INTPIN, dinfo->cfg.intpin, 1);
3900         pci_write_config(dev, PCIR_MINGNT, dinfo->cfg.mingnt, 1);
3901         pci_write_config(dev, PCIR_MAXLAT, dinfo->cfg.maxlat, 1);
3902         pci_write_config(dev, PCIR_CACHELNSZ, dinfo->cfg.cachelnsz, 1);
3903         pci_write_config(dev, PCIR_LATTIMER, dinfo->cfg.lattimer, 1);
3904         pci_write_config(dev, PCIR_PROGIF, dinfo->cfg.progif, 1);
3905         pci_write_config(dev, PCIR_REVID, dinfo->cfg.revid, 1);
3906
3907         /* Restore MSI and MSI-X configurations if they are present. */
3908         if (dinfo->cfg.msi.msi_location != 0)
3909                 pci_resume_msi(dev);
3910         if (dinfo->cfg.msix.msix_location != 0)
3911                 pci_resume_msix(dev);
3912 }
3913
3914 void
3915 pci_cfg_save(device_t dev, struct pci_devinfo *dinfo, int setstate)
3916 {
3917         int i;
3918         uint32_t cls;
3919         int ps;
3920
3921         /*
3922          * Only do header type 0 devices.  Type 1 devices are bridges, which
3923          * we know need special treatment.  Type 2 devices are cardbus bridges
3924          * which also require special treatment.  Other types are unknown, and
3925          * we err on the side of safety by ignoring them.  Powering down
3926          * bridges should not be undertaken lightly.
3927          */
3928         if (dinfo->cfg.hdrtype != 0)
3929                 return;
3930         for (i = 0; i < dinfo->cfg.nummaps; i++)
3931                 dinfo->cfg.bar[i] = pci_read_config(dev, PCIR_BAR(i), 4);
3932         dinfo->cfg.bios = pci_read_config(dev, PCIR_BIOS, 4);
3933
3934         /*
3935          * Some drivers apparently write to these registers w/o updating our
3936          * cached copy.  No harm happens if we update the copy, so do so here
3937          * so we can restore them.  The COMMAND register is modified by the
3938          * bus w/o updating the cache.  This should represent the normally
3939          * writable portion of the 'defined' part of type 0 headers.  In
3940          * theory we also need to save/restore the PCI capability structures
3941          * we know about, but apart from power we don't know any that are
3942          * writable.
3943          */
3944         dinfo->cfg.subvendor = pci_read_config(dev, PCIR_SUBVEND_0, 2);
3945         dinfo->cfg.subdevice = pci_read_config(dev, PCIR_SUBDEV_0, 2);
3946         dinfo->cfg.vendor = pci_read_config(dev, PCIR_VENDOR, 2);
3947         dinfo->cfg.device = pci_read_config(dev, PCIR_DEVICE, 2);
3948         dinfo->cfg.cmdreg = pci_read_config(dev, PCIR_COMMAND, 2);
3949         dinfo->cfg.intline = pci_read_config(dev, PCIR_INTLINE, 1);
3950         dinfo->cfg.intpin = pci_read_config(dev, PCIR_INTPIN, 1);
3951         dinfo->cfg.mingnt = pci_read_config(dev, PCIR_MINGNT, 1);
3952         dinfo->cfg.maxlat = pci_read_config(dev, PCIR_MAXLAT, 1);
3953         dinfo->cfg.cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
3954         dinfo->cfg.lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
3955         dinfo->cfg.baseclass = pci_read_config(dev, PCIR_CLASS, 1);
3956         dinfo->cfg.subclass = pci_read_config(dev, PCIR_SUBCLASS, 1);
3957         dinfo->cfg.progif = pci_read_config(dev, PCIR_PROGIF, 1);
3958         dinfo->cfg.revid = pci_read_config(dev, PCIR_REVID, 1);
3959
3960         /*
3961          * don't set the state for display devices, base peripherals and
3962          * memory devices since bad things happen when they are powered down.
3963          * We should (a) have drivers that can easily detach and (b) use
3964          * generic drivers for these devices so that some device actually
3965          * attaches.  We need to make sure that when we implement (a) we don't
3966          * power the device down on a reattach.
3967          */
3968         cls = pci_get_class(dev);
3969         if (!setstate)
3970                 return;
3971         switch (pci_do_power_nodriver)
3972         {
3973                 case 0:         /* NO powerdown at all */
3974                         return;
3975                 case 1:         /* Conservative about what to power down */
3976                         if (cls == PCIC_STORAGE)
3977                                 return;
3978                         /*FALLTHROUGH*/
3979                 case 2:         /* Agressive about what to power down */
3980                         if (cls == PCIC_DISPLAY || cls == PCIC_MEMORY ||
3981                             cls == PCIC_BASEPERIPH)
3982                                 return;
3983                         /*FALLTHROUGH*/
3984                 case 3:         /* Power down everything */
3985                         break;
3986         }
3987         /*
3988          * PCI spec says we can only go into D3 state from D0 state.
3989          * Transition from D[12] into D0 before going to D3 state.
3990          */
3991         ps = pci_get_powerstate(dev);
3992         if (ps != PCI_POWERSTATE_D0 && ps != PCI_POWERSTATE_D3)
3993                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
3994         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D3)
3995                 pci_set_powerstate(dev, PCI_POWERSTATE_D3);
3996 }
3997
3998 #ifdef COMPAT_OLDPCI
3999
4000 /*
4001  * Locate the parent of a PCI device by scanning the PCI devlist
4002  * and return the entry for the parent.
4003  * For devices on PCI Bus 0 (the host bus), this is the PCI Host.
4004  * For devices on secondary PCI busses, this is that bus' PCI-PCI Bridge.
4005  */
4006 pcicfgregs *
4007 pci_devlist_get_parent(pcicfgregs *cfg)
4008 {
4009         struct devlist *devlist_head;
4010         struct pci_devinfo *dinfo;
4011         pcicfgregs *bridge_cfg;
4012         int i;
4013
4014         dinfo = STAILQ_FIRST(devlist_head = &pci_devq);
4015
4016         /* If the device is on PCI bus 0, look for the host */
4017         if (cfg->bus == 0) {
4018                 for (i = 0; (dinfo != NULL) && (i < pci_numdevs);
4019                 dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
4020                         bridge_cfg = &dinfo->cfg;
4021                         if (bridge_cfg->baseclass == PCIC_BRIDGE
4022                                 && bridge_cfg->subclass == PCIS_BRIDGE_HOST
4023                                 && bridge_cfg->bus == cfg->bus) {
4024                                 return bridge_cfg;
4025                         }
4026                 }
4027         }
4028
4029         /* If the device is not on PCI bus 0, look for the PCI-PCI bridge */
4030         if (cfg->bus > 0) {
4031                 for (i = 0; (dinfo != NULL) && (i < pci_numdevs);
4032                 dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
4033                         bridge_cfg = &dinfo->cfg;
4034                         if (bridge_cfg->baseclass == PCIC_BRIDGE
4035                                 && bridge_cfg->subclass == PCIS_BRIDGE_PCI
4036                                 && bridge_cfg->secondarybus == cfg->bus) {
4037                                 return bridge_cfg;
4038                         }
4039                 }
4040         }
4041
4042         return NULL; 
4043 }
4044
4045 #endif  /* COMPAT_OLDPCI */
4046
4047 int
4048 pci_alloc_1intr(device_t dev, int msi_enable, int *rid0, u_int *flags0)
4049 {
4050         int rid, type;
4051         u_int flags;
4052         char env[64];
4053
4054         rid = 0;
4055         type = PCI_INTR_TYPE_LEGACY;
4056         flags = RF_SHAREABLE | RF_ACTIVE;
4057
4058         ksnprintf(env, sizeof(env), "hw.%s.msi.enable",
4059             device_get_nameunit(dev));
4060         kgetenv_int(env, &msi_enable);
4061
4062         if (msi_enable) {
4063                 int cpu = -1;
4064
4065                 ksnprintf(env, sizeof(env), "hw.%s.msi.cpu",
4066                     device_get_nameunit(dev));
4067                 kgetenv_int(env, &cpu);
4068                 if (cpu >= ncpus)
4069                         cpu = ncpus - 1;
4070
4071                 if (pci_alloc_msi(dev, &rid, 1, cpu) == 0) {
4072                         flags &= ~RF_SHAREABLE;
4073                         type = PCI_INTR_TYPE_MSI;
4074                 }
4075         }
4076
4077         *rid0 = rid;
4078         *flags0 = flags;
4079
4080         return type;
4081 }