xref: /freebsd-13.1/sys/dev/pci/pci.c (revision f0163795)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1997, Stefan Esser <[email protected]>
5  * Copyright (c) 2000, Michael Smith <[email protected]>
6  * Copyright (c) 2000, BSDi
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice unmodified, this list of conditions, and the following
14  *    disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include "opt_acpi.h"
35 #include "opt_iommu.h"
36 #include "opt_bus.h"
37 
38 #include <sys/param.h>
39 #include <sys/conf.h>
40 #include <sys/endian.h>
41 #include <sys/eventhandler.h>
42 #include <sys/fcntl.h>
43 #include <sys/kernel.h>
44 #include <sys/limits.h>
45 #include <sys/linker.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/queue.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
51 #include <sys/taskqueue.h>
52 #include <sys/tree.h>
53 
54 #include <vm/vm.h>
55 #include <vm/pmap.h>
56 #include <vm/vm_extern.h>
57 
58 #include <sys/bus.h>
59 #include <machine/bus.h>
60 #include <sys/rman.h>
61 #include <machine/resource.h>
62 #include <machine/stdarg.h>
63 
64 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__)
65 #include <machine/intr_machdep.h>
66 #endif
67 
68 #include <sys/pciio.h>
69 #include <dev/pci/pcireg.h>
70 #include <dev/pci/pcivar.h>
71 #include <dev/pci/pci_private.h>
72 
73 #ifdef PCI_IOV
74 #include <sys/nv.h>
75 #include <dev/pci/pci_iov_private.h>
76 #endif
77 
78 #include <dev/usb/controller/xhcireg.h>
79 #include <dev/usb/controller/ehcireg.h>
80 #include <dev/usb/controller/ohcireg.h>
81 #include <dev/usb/controller/uhcireg.h>
82 
83 #include <dev/iommu/iommu.h>
84 
85 #include "pcib_if.h"
86 #include "pci_if.h"
87 
88 #define	PCIR_IS_BIOS(cfg, reg)						\
89 	(((cfg)->hdrtype == PCIM_HDRTYPE_NORMAL && reg == PCIR_BIOS) ||	\
90 	 ((cfg)->hdrtype == PCIM_HDRTYPE_BRIDGE && reg == PCIR_BIOS_1))
91 
92 static int		pci_has_quirk(uint32_t devid, int quirk);
93 static pci_addr_t	pci_mapbase(uint64_t mapreg);
94 static const char	*pci_maptype(uint64_t mapreg);
95 static int		pci_maprange(uint64_t mapreg);
96 static pci_addr_t	pci_rombase(uint64_t mapreg);
97 static int		pci_romsize(uint64_t testval);
98 static void		pci_fixancient(pcicfgregs *cfg);
99 static int		pci_printf(pcicfgregs *cfg, const char *fmt, ...);
100 
101 static int		pci_porten(device_t dev);
102 static int		pci_memen(device_t dev);
103 static void		pci_assign_interrupt(device_t bus, device_t dev,
104 			    int force_route);
105 static int		pci_add_map(device_t bus, device_t dev, int reg,
106 			    struct resource_list *rl, int force, int prefetch);
107 static int		pci_probe(device_t dev);
108 static void		pci_load_vendor_data(void);
109 static int		pci_describe_parse_line(char **ptr, int *vendor,
110 			    int *device, char **desc);
111 static char		*pci_describe_device(device_t dev);
112 static int		pci_modevent(module_t mod, int what, void *arg);
113 static void		pci_hdrtypedata(device_t pcib, int b, int s, int f,
114 			    pcicfgregs *cfg);
115 static void		pci_read_cap(device_t pcib, pcicfgregs *cfg);
116 static int		pci_read_vpd_reg(device_t pcib, pcicfgregs *cfg,
117 			    int reg, uint32_t *data);
118 #if 0
119 static int		pci_write_vpd_reg(device_t pcib, pcicfgregs *cfg,
120 			    int reg, uint32_t data);
121 #endif
122 static void		pci_read_vpd(device_t pcib, pcicfgregs *cfg);
123 static void		pci_mask_msix(device_t dev, u_int index);
124 static void		pci_unmask_msix(device_t dev, u_int index);
125 static int		pci_msi_blacklisted(void);
126 static int		pci_msix_blacklisted(void);
127 static void		pci_resume_msi(device_t dev);
128 static void		pci_resume_msix(device_t dev);
129 static int		pci_remap_intr_method(device_t bus, device_t dev,
130 			    u_int irq);
131 static void		pci_hint_device_unit(device_t acdev, device_t child,
132 			    const char *name, int *unitp);
133 static int		pci_reset_post(device_t dev, device_t child);
134 static int		pci_reset_prepare(device_t dev, device_t child);
135 static int		pci_reset_child(device_t dev, device_t child,
136 			    int flags);
137 
138 static int		pci_get_id_method(device_t dev, device_t child,
139 			    enum pci_id_type type, uintptr_t *rid);
140 
141 static struct pci_devinfo * pci_fill_devinfo(device_t pcib, device_t bus, int d,
142     int b, int s, int f, uint16_t vid, uint16_t did);
143 
144 static device_method_t pci_methods[] = {
145 	/* Device interface */
146 	DEVMETHOD(device_probe,		pci_probe),
147 	DEVMETHOD(device_attach,	pci_attach),
148 	DEVMETHOD(device_detach,	pci_detach),
149 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
150 	DEVMETHOD(device_suspend,	bus_generic_suspend),
151 	DEVMETHOD(device_resume,	pci_resume),
152 
153 	/* Bus interface */
154 	DEVMETHOD(bus_print_child,	pci_print_child),
155 	DEVMETHOD(bus_probe_nomatch,	pci_probe_nomatch),
156 	DEVMETHOD(bus_read_ivar,	pci_read_ivar),
157 	DEVMETHOD(bus_write_ivar,	pci_write_ivar),
158 	DEVMETHOD(bus_driver_added,	pci_driver_added),
159 	DEVMETHOD(bus_setup_intr,	pci_setup_intr),
160 	DEVMETHOD(bus_teardown_intr,	pci_teardown_intr),
161 	DEVMETHOD(bus_reset_prepare,	pci_reset_prepare),
162 	DEVMETHOD(bus_reset_post,	pci_reset_post),
163 	DEVMETHOD(bus_reset_child,	pci_reset_child),
164 
165 	DEVMETHOD(bus_get_dma_tag,	pci_get_dma_tag),
166 	DEVMETHOD(bus_get_resource_list,pci_get_resource_list),
167 	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
168 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
169 	DEVMETHOD(bus_delete_resource,	pci_delete_resource),
170 	DEVMETHOD(bus_alloc_resource,	pci_alloc_resource),
171 	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
172 	DEVMETHOD(bus_release_resource,	pci_release_resource),
173 	DEVMETHOD(bus_activate_resource, pci_activate_resource),
174 	DEVMETHOD(bus_deactivate_resource, pci_deactivate_resource),
175 	DEVMETHOD(bus_child_deleted,	pci_child_deleted),
176 	DEVMETHOD(bus_child_detached,	pci_child_detached),
177 	DEVMETHOD(bus_child_pnpinfo_str, pci_child_pnpinfo_str_method),
178 	DEVMETHOD(bus_child_location_str, pci_child_location_str_method),
179 	DEVMETHOD(bus_hint_device_unit,	pci_hint_device_unit),
180 	DEVMETHOD(bus_remap_intr,	pci_remap_intr_method),
181 	DEVMETHOD(bus_suspend_child,	pci_suspend_child),
182 	DEVMETHOD(bus_resume_child,	pci_resume_child),
183 	DEVMETHOD(bus_rescan,		pci_rescan_method),
184 
185 	/* PCI interface */
186 	DEVMETHOD(pci_read_config,	pci_read_config_method),
187 	DEVMETHOD(pci_write_config,	pci_write_config_method),
188 	DEVMETHOD(pci_enable_busmaster,	pci_enable_busmaster_method),
189 	DEVMETHOD(pci_disable_busmaster, pci_disable_busmaster_method),
190 	DEVMETHOD(pci_enable_io,	pci_enable_io_method),
191 	DEVMETHOD(pci_disable_io,	pci_disable_io_method),
192 	DEVMETHOD(pci_get_vpd_ident,	pci_get_vpd_ident_method),
193 	DEVMETHOD(pci_get_vpd_readonly,	pci_get_vpd_readonly_method),
194 	DEVMETHOD(pci_get_powerstate,	pci_get_powerstate_method),
195 	DEVMETHOD(pci_set_powerstate,	pci_set_powerstate_method),
196 	DEVMETHOD(pci_assign_interrupt,	pci_assign_interrupt_method),
197 	DEVMETHOD(pci_find_cap,		pci_find_cap_method),
198 	DEVMETHOD(pci_find_next_cap,	pci_find_next_cap_method),
199 	DEVMETHOD(pci_find_extcap,	pci_find_extcap_method),
200 	DEVMETHOD(pci_find_next_extcap,	pci_find_next_extcap_method),
201 	DEVMETHOD(pci_find_htcap,	pci_find_htcap_method),
202 	DEVMETHOD(pci_find_next_htcap,	pci_find_next_htcap_method),
203 	DEVMETHOD(pci_alloc_msi,	pci_alloc_msi_method),
204 	DEVMETHOD(pci_alloc_msix,	pci_alloc_msix_method),
205 	DEVMETHOD(pci_enable_msi,	pci_enable_msi_method),
206 	DEVMETHOD(pci_enable_msix,	pci_enable_msix_method),
207 	DEVMETHOD(pci_disable_msi,	pci_disable_msi_method),
208 	DEVMETHOD(pci_remap_msix,	pci_remap_msix_method),
209 	DEVMETHOD(pci_release_msi,	pci_release_msi_method),
210 	DEVMETHOD(pci_msi_count,	pci_msi_count_method),
211 	DEVMETHOD(pci_msix_count,	pci_msix_count_method),
212 	DEVMETHOD(pci_msix_pba_bar,	pci_msix_pba_bar_method),
213 	DEVMETHOD(pci_msix_table_bar,	pci_msix_table_bar_method),
214 	DEVMETHOD(pci_get_id,		pci_get_id_method),
215 	DEVMETHOD(pci_alloc_devinfo,	pci_alloc_devinfo_method),
216 	DEVMETHOD(pci_child_added,	pci_child_added_method),
217 #ifdef PCI_IOV
218 	DEVMETHOD(pci_iov_attach,	pci_iov_attach_method),
219 	DEVMETHOD(pci_iov_detach,	pci_iov_detach_method),
220 	DEVMETHOD(pci_create_iov_child,	pci_create_iov_child_method),
221 #endif
222 
223 	DEVMETHOD_END
224 };
225 
226 DEFINE_CLASS_0(pci, pci_driver, pci_methods, sizeof(struct pci_softc));
227 
228 static devclass_t pci_devclass;
229 EARLY_DRIVER_MODULE(pci, pcib, pci_driver, pci_devclass, pci_modevent, NULL,
230     BUS_PASS_BUS);
231 MODULE_VERSION(pci, 1);
232 
233 static char	*pci_vendordata;
234 static size_t	pci_vendordata_size;
235 
236 struct pci_quirk {
237 	uint32_t devid;	/* Vendor/device of the card */
238 	int	type;
239 #define	PCI_QUIRK_MAP_REG	1 /* PCI map register in weird place */
240 #define	PCI_QUIRK_DISABLE_MSI	2 /* Neither MSI nor MSI-X work */
241 #define	PCI_QUIRK_ENABLE_MSI_VM	3 /* Older chipset in VM where MSI works */
242 #define	PCI_QUIRK_UNMAP_REG	4 /* Ignore PCI map register */
243 #define	PCI_QUIRK_DISABLE_MSIX	5 /* MSI-X doesn't work */
244 #define	PCI_QUIRK_MSI_INTX_BUG	6 /* PCIM_CMD_INTxDIS disables MSI */
245 #define	PCI_QUIRK_REALLOC_BAR	7 /* Can't allocate memory at the default address */
246 	int	arg1;
247 	int	arg2;
248 };
249 
250 static const struct pci_quirk pci_quirks[] = {
251 	/* The Intel 82371AB and 82443MX have a map register at offset 0x90. */
252 	{ 0x71138086, PCI_QUIRK_MAP_REG,	0x90,	 0 },
253 	{ 0x719b8086, PCI_QUIRK_MAP_REG,	0x90,	 0 },
254 	/* As does the Serverworks OSB4 (the SMBus mapping register) */
255 	{ 0x02001166, PCI_QUIRK_MAP_REG,	0x90,	 0 },
256 
257 	/*
258 	 * MSI doesn't work with the ServerWorks CNB20-HE Host Bridge
259 	 * or the CMIC-SL (AKA ServerWorks GC_LE).
260 	 */
261 	{ 0x00141166, PCI_QUIRK_DISABLE_MSI,	0,	0 },
262 	{ 0x00171166, PCI_QUIRK_DISABLE_MSI,	0,	0 },
263 
264 	/*
265 	 * MSI doesn't work on earlier Intel chipsets including
266 	 * E7500, E7501, E7505, 845, 865, 875/E7210, and 855.
267 	 */
268 	{ 0x25408086, PCI_QUIRK_DISABLE_MSI,	0,	0 },
269 	{ 0x254c8086, PCI_QUIRK_DISABLE_MSI,	0,	0 },
270 	{ 0x25508086, PCI_QUIRK_DISABLE_MSI,	0,	0 },
271 	{ 0x25608086, PCI_QUIRK_DISABLE_MSI,	0,	0 },
272 	{ 0x25708086, PCI_QUIRK_DISABLE_MSI,	0,	0 },
273 	{ 0x25788086, PCI_QUIRK_DISABLE_MSI,	0,	0 },
274 	{ 0x35808086, PCI_QUIRK_DISABLE_MSI,	0,	0 },
275 
276 	/*
277 	 * MSI doesn't work with devices behind the AMD 8131 HT-PCIX
278 	 * bridge.
279 	 */
280 	{ 0x74501022, PCI_QUIRK_DISABLE_MSI,	0,	0 },
281 
282 	/*
283 	 * Some virtualization environments emulate an older chipset
284 	 * but support MSI just fine.  QEMU uses the Intel 82440.
285 	 */
286 	{ 0x12378086, PCI_QUIRK_ENABLE_MSI_VM,	0,	0 },
287 
288 	/*
289 	 * HPET MMIO base address may appear in Bar1 for AMD SB600 SMBus
290 	 * controller depending on SoftPciRst register (PM_IO 0x55 [7]).
291 	 * It prevents us from attaching hpet(4) when the bit is unset.
292 	 * Note this quirk only affects SB600 revision A13 and earlier.
293 	 * For SB600 A21 and later, firmware must set the bit to hide it.
294 	 * For SB700 and later, it is unused and hardcoded to zero.
295 	 */
296 	{ 0x43851002, PCI_QUIRK_UNMAP_REG,	0x14,	0 },
297 
298 	/*
299 	 * Atheros AR8161/AR8162/E2200/E2400/E2500 Ethernet controllers have
300 	 * a bug that MSI interrupt does not assert if PCIM_CMD_INTxDIS bit
301 	 * of the command register is set.
302 	 */
303 	{ 0x10911969, PCI_QUIRK_MSI_INTX_BUG,	0,	0 },
304 	{ 0xE0911969, PCI_QUIRK_MSI_INTX_BUG,	0,	0 },
305 	{ 0xE0A11969, PCI_QUIRK_MSI_INTX_BUG,	0,	0 },
306 	{ 0xE0B11969, PCI_QUIRK_MSI_INTX_BUG,	0,	0 },
307 	{ 0x10901969, PCI_QUIRK_MSI_INTX_BUG,	0,	0 },
308 
309 	/*
310 	 * Broadcom BCM5714(S)/BCM5715(S)/BCM5780(S) Ethernet MACs don't
311 	 * issue MSI interrupts with PCIM_CMD_INTxDIS set either.
312 	 */
313 	{ 0x166814e4, PCI_QUIRK_MSI_INTX_BUG,	0,	0 }, /* BCM5714 */
314 	{ 0x166914e4, PCI_QUIRK_MSI_INTX_BUG,	0,	0 }, /* BCM5714S */
315 	{ 0x166a14e4, PCI_QUIRK_MSI_INTX_BUG,	0,	0 }, /* BCM5780 */
316 	{ 0x166b14e4, PCI_QUIRK_MSI_INTX_BUG,	0,	0 }, /* BCM5780S */
317 	{ 0x167814e4, PCI_QUIRK_MSI_INTX_BUG,	0,	0 }, /* BCM5715 */
318 	{ 0x167914e4, PCI_QUIRK_MSI_INTX_BUG,	0,	0 }, /* BCM5715S */
319 
320 	/*
321 	 * HPE Gen 10 VGA has a memory range that can't be allocated in the
322 	 * expected place.
323 	 */
324 	{ 0x98741002, PCI_QUIRK_REALLOC_BAR,	0, 	0 },
325 	{ 0 }
326 };
327 
328 /* map register information */
329 #define	PCI_MAPMEM	0x01	/* memory map */
330 #define	PCI_MAPMEMP	0x02	/* prefetchable memory map */
331 #define	PCI_MAPPORT	0x04	/* port map */
332 
333 struct devlist pci_devq;
334 uint32_t pci_generation;
335 uint32_t pci_numdevs = 0;
336 static int pcie_chipset, pcix_chipset;
337 
338 /* sysctl vars */
339 SYSCTL_NODE(_hw, OID_AUTO, pci, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
340     "PCI bus tuning parameters");
341 
342 static int pci_enable_io_modes = 1;
343 SYSCTL_INT(_hw_pci, OID_AUTO, enable_io_modes, CTLFLAG_RWTUN,
344     &pci_enable_io_modes, 1,
345     "Enable I/O and memory bits in the config register.  Some BIOSes do not"
346     " enable these bits correctly.  We'd like to do this all the time, but"
347     " there are some peripherals that this causes problems with.");
348 
349 static int pci_do_realloc_bars = 1;
350 SYSCTL_INT(_hw_pci, OID_AUTO, realloc_bars, CTLFLAG_RWTUN,
351     &pci_do_realloc_bars, 0,
352     "Attempt to allocate a new range for any BARs whose original "
353     "firmware-assigned ranges fail to allocate during the initial device scan.");
354 
355 static int pci_do_power_nodriver = 0;
356 SYSCTL_INT(_hw_pci, OID_AUTO, do_power_nodriver, CTLFLAG_RWTUN,
357     &pci_do_power_nodriver, 0,
358     "Place a function into D3 state when no driver attaches to it.  0 means"
359     " disable.  1 means conservatively place devices into D3 state.  2 means"
360     " aggressively place devices into D3 state.  3 means put absolutely"
361     " everything in D3 state.");
362 
363 int pci_do_power_resume = 1;
364 SYSCTL_INT(_hw_pci, OID_AUTO, do_power_resume, CTLFLAG_RWTUN,
365     &pci_do_power_resume, 1,
366   "Transition from D3 -> D0 on resume.");
367 
368 int pci_do_power_suspend = 1;
369 SYSCTL_INT(_hw_pci, OID_AUTO, do_power_suspend, CTLFLAG_RWTUN,
370     &pci_do_power_suspend, 1,
371   "Transition from D0 -> D3 on suspend.");
372 
373 static int pci_do_msi = 1;
374 SYSCTL_INT(_hw_pci, OID_AUTO, enable_msi, CTLFLAG_RWTUN, &pci_do_msi, 1,
375     "Enable support for MSI interrupts");
376 
377 static int pci_do_msix = 1;
378 SYSCTL_INT(_hw_pci, OID_AUTO, enable_msix, CTLFLAG_RWTUN, &pci_do_msix, 1,
379     "Enable support for MSI-X interrupts");
380 
381 static int pci_msix_rewrite_table = 0;
382 SYSCTL_INT(_hw_pci, OID_AUTO, msix_rewrite_table, CTLFLAG_RWTUN,
383     &pci_msix_rewrite_table, 0,
384     "Rewrite entire MSI-X table when updating MSI-X entries");
385 
386 static int pci_honor_msi_blacklist = 1;
387 SYSCTL_INT(_hw_pci, OID_AUTO, honor_msi_blacklist, CTLFLAG_RDTUN,
388     &pci_honor_msi_blacklist, 1, "Honor chipset blacklist for MSI/MSI-X");
389 
390 #if defined(__i386__) || defined(__amd64__)
391 static int pci_usb_takeover = 1;
392 #else
393 static int pci_usb_takeover = 0;
394 #endif
395 SYSCTL_INT(_hw_pci, OID_AUTO, usb_early_takeover, CTLFLAG_RDTUN,
396     &pci_usb_takeover, 1,
397     "Enable early takeover of USB controllers. Disable this if you depend on"
398     " BIOS emulation of USB devices, that is you use USB devices (like"
399     " keyboard or mouse) but do not load USB drivers");
400 
401 static int pci_clear_bars;
402 SYSCTL_INT(_hw_pci, OID_AUTO, clear_bars, CTLFLAG_RDTUN, &pci_clear_bars, 0,
403     "Ignore firmware-assigned resources for BARs.");
404 
405 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
406 static int pci_clear_buses;
407 SYSCTL_INT(_hw_pci, OID_AUTO, clear_buses, CTLFLAG_RDTUN, &pci_clear_buses, 0,
408     "Ignore firmware-assigned bus numbers.");
409 #endif
410 
411 static int pci_enable_ari = 1;
412 SYSCTL_INT(_hw_pci, OID_AUTO, enable_ari, CTLFLAG_RDTUN, &pci_enable_ari,
413     0, "Enable support for PCIe Alternative RID Interpretation");
414 
415 int pci_enable_aspm = 1;
416 SYSCTL_INT(_hw_pci, OID_AUTO, enable_aspm, CTLFLAG_RDTUN, &pci_enable_aspm,
417     0, "Enable support for PCIe Active State Power Management");
418 
419 static int pci_clear_aer_on_attach = 0;
420 SYSCTL_INT(_hw_pci, OID_AUTO, clear_aer_on_attach, CTLFLAG_RWTUN,
421     &pci_clear_aer_on_attach, 0,
422     "Clear port and device AER state on driver attach");
423 
424 static int
pci_has_quirk(uint32_t devid,int quirk)425 pci_has_quirk(uint32_t devid, int quirk)
426 {
427 	const struct pci_quirk *q;
428 
429 	for (q = &pci_quirks[0]; q->devid; q++) {
430 		if (q->devid == devid && q->type == quirk)
431 			return (1);
432 	}
433 	return (0);
434 }
435 
436 /* Find a device_t by bus/slot/function in domain 0 */
437 
438 device_t
pci_find_bsf(uint8_t bus,uint8_t slot,uint8_t func)439 pci_find_bsf(uint8_t bus, uint8_t slot, uint8_t func)
440 {
441 
442 	return (pci_find_dbsf(0, bus, slot, func));
443 }
444 
445 /* Find a device_t by domain/bus/slot/function */
446 
447 device_t
pci_find_dbsf(uint32_t domain,uint8_t bus,uint8_t slot,uint8_t func)448 pci_find_dbsf(uint32_t domain, uint8_t bus, uint8_t slot, uint8_t func)
449 {
450 	struct pci_devinfo *dinfo = NULL;
451 
452 	STAILQ_FOREACH(dinfo, &pci_devq, pci_links) {
453 		if ((dinfo->cfg.domain == domain) &&
454 		    (dinfo->cfg.bus == bus) &&
455 		    (dinfo->cfg.slot == slot) &&
456 		    (dinfo->cfg.func == func)) {
457 			break;
458 		}
459 	}
460 
461 	return (dinfo != NULL ? dinfo->cfg.dev : NULL);
462 }
463 
464 /* Find a device_t by vendor/device ID */
465 
466 device_t
pci_find_device(uint16_t vendor,uint16_t device)467 pci_find_device(uint16_t vendor, uint16_t device)
468 {
469 	struct pci_devinfo *dinfo;
470 
471 	STAILQ_FOREACH(dinfo, &pci_devq, pci_links) {
472 		if ((dinfo->cfg.vendor == vendor) &&
473 		    (dinfo->cfg.device == device)) {
474 			return (dinfo->cfg.dev);
475 		}
476 	}
477 
478 	return (NULL);
479 }
480 
481 device_t
pci_find_class(uint8_t class,uint8_t subclass)482 pci_find_class(uint8_t class, uint8_t subclass)
483 {
484 	struct pci_devinfo *dinfo;
485 
486 	STAILQ_FOREACH(dinfo, &pci_devq, pci_links) {
487 		if (dinfo->cfg.baseclass == class &&
488 		    dinfo->cfg.subclass == subclass) {
489 			return (dinfo->cfg.dev);
490 		}
491 	}
492 
493 	return (NULL);
494 }
495 
496 device_t
pci_find_class_from(uint8_t class,uint8_t subclass,device_t from)497 pci_find_class_from(uint8_t class, uint8_t subclass, device_t from)
498 {
499 	struct pci_devinfo *dinfo;
500 	bool found = false;
501 
502 	STAILQ_FOREACH(dinfo, &pci_devq, pci_links) {
503 		if (from != NULL && found == false) {
504 			if (from != dinfo->cfg.dev)
505 				continue;
506 			found = true;
507 			continue;
508 		}
509 		if (dinfo->cfg.baseclass == class &&
510 		    dinfo->cfg.subclass == subclass) {
511 			return (dinfo->cfg.dev);
512 		}
513 	}
514 
515 	return (NULL);
516 }
517 
518 static int
pci_printf(pcicfgregs * cfg,const char * fmt,...)519 pci_printf(pcicfgregs *cfg, const char *fmt, ...)
520 {
521 	va_list ap;
522 	int retval;
523 
524 	retval = printf("pci%d:%d:%d:%d: ", cfg->domain, cfg->bus, cfg->slot,
525 	    cfg->func);
526 	va_start(ap, fmt);
527 	retval += vprintf(fmt, ap);
528 	va_end(ap);
529 	return (retval);
530 }
531 
532 /* return base address of memory or port map */
533 
534 static pci_addr_t
pci_mapbase(uint64_t mapreg)535 pci_mapbase(uint64_t mapreg)
536 {
537 
538 	if (PCI_BAR_MEM(mapreg))
539 		return (mapreg & PCIM_BAR_MEM_BASE);
540 	else
541 		return (mapreg & PCIM_BAR_IO_BASE);
542 }
543 
544 /* return map type of memory or port map */
545 
546 static const char *
pci_maptype(uint64_t mapreg)547 pci_maptype(uint64_t mapreg)
548 {
549 
550 	if (PCI_BAR_IO(mapreg))
551 		return ("I/O Port");
552 	if (mapreg & PCIM_BAR_MEM_PREFETCH)
553 		return ("Prefetchable Memory");
554 	return ("Memory");
555 }
556 
557 /* return log2 of map size decoded for memory or port map */
558 
559 int
pci_mapsize(uint64_t testval)560 pci_mapsize(uint64_t testval)
561 {
562 	int ln2size;
563 
564 	testval = pci_mapbase(testval);
565 	ln2size = 0;
566 	if (testval != 0) {
567 		while ((testval & 1) == 0)
568 		{
569 			ln2size++;
570 			testval >>= 1;
571 		}
572 	}
573 	return (ln2size);
574 }
575 
576 /* return base address of device ROM */
577 
578 static pci_addr_t
pci_rombase(uint64_t mapreg)579 pci_rombase(uint64_t mapreg)
580 {
581 
582 	return (mapreg & PCIM_BIOS_ADDR_MASK);
583 }
584 
585 /* return log2 of map size decided for device ROM */
586 
587 static int
pci_romsize(uint64_t testval)588 pci_romsize(uint64_t testval)
589 {
590 	int ln2size;
591 
592 	testval = pci_rombase(testval);
593 	ln2size = 0;
594 	if (testval != 0) {
595 		while ((testval & 1) == 0)
596 		{
597 			ln2size++;
598 			testval >>= 1;
599 		}
600 	}
601 	return (ln2size);
602 }
603 
604 /* return log2 of address range supported by map register */
605 
606 static int
pci_maprange(uint64_t mapreg)607 pci_maprange(uint64_t mapreg)
608 {
609 	int ln2range = 0;
610 
611 	if (PCI_BAR_IO(mapreg))
612 		ln2range = 32;
613 	else
614 		switch (mapreg & PCIM_BAR_MEM_TYPE) {
615 		case PCIM_BAR_MEM_32:
616 			ln2range = 32;
617 			break;
618 		case PCIM_BAR_MEM_1MB:
619 			ln2range = 20;
620 			break;
621 		case PCIM_BAR_MEM_64:
622 			ln2range = 64;
623 			break;
624 		}
625 	return (ln2range);
626 }
627 
628 /* adjust some values from PCI 1.0 devices to match 2.0 standards ... */
629 
630 static void
pci_fixancient(pcicfgregs * cfg)631 pci_fixancient(pcicfgregs *cfg)
632 {
633 	if ((cfg->hdrtype & PCIM_HDRTYPE) != PCIM_HDRTYPE_NORMAL)
634 		return;
635 
636 	/* PCI to PCI bridges use header type 1 */
637 	if (cfg->baseclass == PCIC_BRIDGE && cfg->subclass == PCIS_BRIDGE_PCI)
638 		cfg->hdrtype = PCIM_HDRTYPE_BRIDGE;
639 }
640 
641 /* extract header type specific config data */
642 
643 static void
pci_hdrtypedata(device_t pcib,int b,int s,int f,pcicfgregs * cfg)644 pci_hdrtypedata(device_t pcib, int b, int s, int f, pcicfgregs *cfg)
645 {
646 #define	REG(n, w)	PCIB_READ_CONFIG(pcib, b, s, f, n, w)
647 	switch (cfg->hdrtype & PCIM_HDRTYPE) {
648 	case PCIM_HDRTYPE_NORMAL:
649 		cfg->subvendor      = REG(PCIR_SUBVEND_0, 2);
650 		cfg->subdevice      = REG(PCIR_SUBDEV_0, 2);
651 		cfg->mingnt         = REG(PCIR_MINGNT, 1);
652 		cfg->maxlat         = REG(PCIR_MAXLAT, 1);
653 		cfg->nummaps	    = PCI_MAXMAPS_0;
654 		break;
655 	case PCIM_HDRTYPE_BRIDGE:
656 		cfg->bridge.br_seclat = REG(PCIR_SECLAT_1, 1);
657 		cfg->bridge.br_subbus = REG(PCIR_SUBBUS_1, 1);
658 		cfg->bridge.br_secbus = REG(PCIR_SECBUS_1, 1);
659 		cfg->bridge.br_pribus = REG(PCIR_PRIBUS_1, 1);
660 		cfg->bridge.br_control = REG(PCIR_BRIDGECTL_1, 2);
661 		cfg->nummaps	    = PCI_MAXMAPS_1;
662 		break;
663 	case PCIM_HDRTYPE_CARDBUS:
664 		cfg->bridge.br_seclat = REG(PCIR_SECLAT_2, 1);
665 		cfg->bridge.br_subbus = REG(PCIR_SUBBUS_2, 1);
666 		cfg->bridge.br_secbus = REG(PCIR_SECBUS_2, 1);
667 		cfg->bridge.br_pribus = REG(PCIR_PRIBUS_2, 1);
668 		cfg->bridge.br_control = REG(PCIR_BRIDGECTL_2, 2);
669 		cfg->subvendor      = REG(PCIR_SUBVEND_2, 2);
670 		cfg->subdevice      = REG(PCIR_SUBDEV_2, 2);
671 		cfg->nummaps	    = PCI_MAXMAPS_2;
672 		break;
673 	}
674 #undef REG
675 }
676 
677 /* read configuration header into pcicfgregs structure */
678 struct pci_devinfo *
pci_read_device(device_t pcib,device_t bus,int d,int b,int s,int f)679 pci_read_device(device_t pcib, device_t bus, int d, int b, int s, int f)
680 {
681 #define	REG(n, w)	PCIB_READ_CONFIG(pcib, b, s, f, n, w)
682 	uint16_t vid, did;
683 
684 	vid = REG(PCIR_VENDOR, 2);
685 	if (vid == PCIV_INVALID)
686 		return (NULL);
687 
688 	did = REG(PCIR_DEVICE, 2);
689 
690 	return (pci_fill_devinfo(pcib, bus, d, b, s, f, vid, did));
691 }
692 
693 struct pci_devinfo *
pci_alloc_devinfo_method(device_t dev)694 pci_alloc_devinfo_method(device_t dev)
695 {
696 
697 	return (malloc(sizeof(struct pci_devinfo), M_DEVBUF,
698 	    M_WAITOK | M_ZERO));
699 }
700 
701 static struct pci_devinfo *
pci_fill_devinfo(device_t pcib,device_t bus,int d,int b,int s,int f,uint16_t vid,uint16_t did)702 pci_fill_devinfo(device_t pcib, device_t bus, int d, int b, int s, int f,
703     uint16_t vid, uint16_t did)
704 {
705 	struct pci_devinfo *devlist_entry;
706 	pcicfgregs *cfg;
707 
708 	devlist_entry = PCI_ALLOC_DEVINFO(bus);
709 
710 	cfg = &devlist_entry->cfg;
711 
712 	cfg->domain		= d;
713 	cfg->bus		= b;
714 	cfg->slot		= s;
715 	cfg->func		= f;
716 	cfg->vendor		= vid;
717 	cfg->device		= did;
718 	cfg->cmdreg		= REG(PCIR_COMMAND, 2);
719 	cfg->statreg		= REG(PCIR_STATUS, 2);
720 	cfg->baseclass		= REG(PCIR_CLASS, 1);
721 	cfg->subclass		= REG(PCIR_SUBCLASS, 1);
722 	cfg->progif		= REG(PCIR_PROGIF, 1);
723 	cfg->revid		= REG(PCIR_REVID, 1);
724 	cfg->hdrtype		= REG(PCIR_HDRTYPE, 1);
725 	cfg->cachelnsz		= REG(PCIR_CACHELNSZ, 1);
726 	cfg->lattimer		= REG(PCIR_LATTIMER, 1);
727 	cfg->intpin		= REG(PCIR_INTPIN, 1);
728 	cfg->intline		= REG(PCIR_INTLINE, 1);
729 
730 	cfg->mfdev		= (cfg->hdrtype & PCIM_MFDEV) != 0;
731 	cfg->hdrtype		&= ~PCIM_MFDEV;
732 	STAILQ_INIT(&cfg->maps);
733 
734 	cfg->iov		= NULL;
735 
736 	pci_fixancient(cfg);
737 	pci_hdrtypedata(pcib, b, s, f, cfg);
738 
739 	if (REG(PCIR_STATUS, 2) & PCIM_STATUS_CAPPRESENT)
740 		pci_read_cap(pcib, cfg);
741 
742 	STAILQ_INSERT_TAIL(&pci_devq, devlist_entry, pci_links);
743 
744 	devlist_entry->conf.pc_sel.pc_domain = cfg->domain;
745 	devlist_entry->conf.pc_sel.pc_bus = cfg->bus;
746 	devlist_entry->conf.pc_sel.pc_dev = cfg->slot;
747 	devlist_entry->conf.pc_sel.pc_func = cfg->func;
748 	devlist_entry->conf.pc_hdr = cfg->hdrtype;
749 
750 	devlist_entry->conf.pc_subvendor = cfg->subvendor;
751 	devlist_entry->conf.pc_subdevice = cfg->subdevice;
752 	devlist_entry->conf.pc_vendor = cfg->vendor;
753 	devlist_entry->conf.pc_device = cfg->device;
754 
755 	devlist_entry->conf.pc_class = cfg->baseclass;
756 	devlist_entry->conf.pc_subclass = cfg->subclass;
757 	devlist_entry->conf.pc_progif = cfg->progif;
758 	devlist_entry->conf.pc_revid = cfg->revid;
759 
760 	pci_numdevs++;
761 	pci_generation++;
762 
763 	return (devlist_entry);
764 }
765 #undef REG
766 
767 static void
pci_ea_fill_info(device_t pcib,pcicfgregs * cfg)768 pci_ea_fill_info(device_t pcib, pcicfgregs *cfg)
769 {
770 #define	REG(n, w)	PCIB_READ_CONFIG(pcib, cfg->bus, cfg->slot, cfg->func, \
771     cfg->ea.ea_location + (n), w)
772 	int num_ent;
773 	int ptr;
774 	int a, b;
775 	uint32_t val;
776 	int ent_size;
777 	uint32_t dw[4];
778 	uint64_t base, max_offset;
779 	struct pci_ea_entry *eae;
780 
781 	if (cfg->ea.ea_location == 0)
782 		return;
783 
784 	STAILQ_INIT(&cfg->ea.ea_entries);
785 
786 	/* Determine the number of entries */
787 	num_ent = REG(PCIR_EA_NUM_ENT, 2);
788 	num_ent &= PCIM_EA_NUM_ENT_MASK;
789 
790 	/* Find the first entry to care of */
791 	ptr = PCIR_EA_FIRST_ENT;
792 
793 	/* Skip DWORD 2 for type 1 functions */
794 	if ((cfg->hdrtype & PCIM_HDRTYPE) == PCIM_HDRTYPE_BRIDGE)
795 		ptr += 4;
796 
797 	for (a = 0; a < num_ent; a++) {
798 		eae = malloc(sizeof(*eae), M_DEVBUF, M_WAITOK | M_ZERO);
799 		eae->eae_cfg_offset = cfg->ea.ea_location + ptr;
800 
801 		/* Read a number of dwords in the entry */
802 		val = REG(ptr, 4);
803 		ptr += 4;
804 		ent_size = (val & PCIM_EA_ES);
805 
806 		for (b = 0; b < ent_size; b++) {
807 			dw[b] = REG(ptr, 4);
808 			ptr += 4;
809 		}
810 
811 		eae->eae_flags = val;
812 		eae->eae_bei = (PCIM_EA_BEI & val) >> PCIM_EA_BEI_OFFSET;
813 
814 		base = dw[0] & PCIM_EA_FIELD_MASK;
815 		max_offset = dw[1] | ~PCIM_EA_FIELD_MASK;
816 		b = 2;
817 		if (((dw[0] & PCIM_EA_IS_64) != 0) && (b < ent_size)) {
818 			base |= (uint64_t)dw[b] << 32UL;
819 			b++;
820 		}
821 		if (((dw[1] & PCIM_EA_IS_64) != 0)
822 		    && (b < ent_size)) {
823 			max_offset |= (uint64_t)dw[b] << 32UL;
824 			b++;
825 		}
826 
827 		eae->eae_base = base;
828 		eae->eae_max_offset = max_offset;
829 
830 		STAILQ_INSERT_TAIL(&cfg->ea.ea_entries, eae, eae_link);
831 
832 		if (bootverbose) {
833 			printf("PCI(EA) dev %04x:%04x, bei %d, flags #%x, base #%jx, max_offset #%jx\n",
834 			    cfg->vendor, cfg->device, eae->eae_bei, eae->eae_flags,
835 			    (uintmax_t)eae->eae_base, (uintmax_t)eae->eae_max_offset);
836 		}
837 	}
838 }
839 #undef REG
840 
841 static void
pci_read_cap(device_t pcib,pcicfgregs * cfg)842 pci_read_cap(device_t pcib, pcicfgregs *cfg)
843 {
844 #define	REG(n, w)	PCIB_READ_CONFIG(pcib, cfg->bus, cfg->slot, cfg->func, n, w)
845 #define	WREG(n, v, w)	PCIB_WRITE_CONFIG(pcib, cfg->bus, cfg->slot, cfg->func, n, v, w)
846 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__)
847 	uint64_t addr;
848 #endif
849 	uint32_t val;
850 	int	ptr, nextptr, ptrptr;
851 
852 	switch (cfg->hdrtype & PCIM_HDRTYPE) {
853 	case PCIM_HDRTYPE_NORMAL:
854 	case PCIM_HDRTYPE_BRIDGE:
855 		ptrptr = PCIR_CAP_PTR;
856 		break;
857 	case PCIM_HDRTYPE_CARDBUS:
858 		ptrptr = PCIR_CAP_PTR_2;	/* cardbus capabilities ptr */
859 		break;
860 	default:
861 		return;		/* no extended capabilities support */
862 	}
863 	nextptr = REG(ptrptr, 1);	/* sanity check? */
864 
865 	/*
866 	 * Read capability entries.
867 	 */
868 	while (nextptr != 0) {
869 		/* Sanity check */
870 		if (nextptr > 255) {
871 			printf("illegal PCI extended capability offset %d\n",
872 			    nextptr);
873 			return;
874 		}
875 		/* Find the next entry */
876 		ptr = nextptr;
877 		nextptr = REG(ptr + PCICAP_NEXTPTR, 1);
878 
879 		/* Process this entry */
880 		switch (REG(ptr + PCICAP_ID, 1)) {
881 		case PCIY_PMG:		/* PCI power management */
882 			if (cfg->pp.pp_cap == 0) {
883 				cfg->pp.pp_cap = REG(ptr + PCIR_POWER_CAP, 2);
884 				cfg->pp.pp_status = ptr + PCIR_POWER_STATUS;
885 				cfg->pp.pp_bse = ptr + PCIR_POWER_BSE;
886 				if ((nextptr - ptr) > PCIR_POWER_DATA)
887 					cfg->pp.pp_data = ptr + PCIR_POWER_DATA;
888 			}
889 			break;
890 		case PCIY_HT:		/* HyperTransport */
891 			/* Determine HT-specific capability type. */
892 			val = REG(ptr + PCIR_HT_COMMAND, 2);
893 
894 			if ((val & 0xe000) == PCIM_HTCAP_SLAVE)
895 				cfg->ht.ht_slave = ptr;
896 
897 #if defined(__i386__) || defined(__amd64__) || defined(__powerpc__)
898 			switch (val & PCIM_HTCMD_CAP_MASK) {
899 			case PCIM_HTCAP_MSI_MAPPING:
900 				if (!(val & PCIM_HTCMD_MSI_FIXED)) {
901 					/* Sanity check the mapping window. */
902 					addr = REG(ptr + PCIR_HTMSI_ADDRESS_HI,
903 					    4);
904 					addr <<= 32;
905 					addr |= REG(ptr + PCIR_HTMSI_ADDRESS_LO,
906 					    4);
907 					if (addr != MSI_INTEL_ADDR_BASE)
908 						device_printf(pcib,
909 	    "HT device at pci%d:%d:%d:%d has non-default MSI window 0x%llx\n",
910 						    cfg->domain, cfg->bus,
911 						    cfg->slot, cfg->func,
912 						    (long long)addr);
913 				} else
914 					addr = MSI_INTEL_ADDR_BASE;
915 
916 				cfg->ht.ht_msimap = ptr;
917 				cfg->ht.ht_msictrl = val;
918 				cfg->ht.ht_msiaddr = addr;
919 				break;
920 			}
921 #endif
922 			break;
923 		case PCIY_MSI:		/* PCI MSI */
924 			cfg->msi.msi_location = ptr;
925 			cfg->msi.msi_ctrl = REG(ptr + PCIR_MSI_CTRL, 2);
926 			cfg->msi.msi_msgnum = 1 << ((cfg->msi.msi_ctrl &
927 						     PCIM_MSICTRL_MMC_MASK)>>1);
928 			break;
929 		case PCIY_MSIX:		/* PCI MSI-X */
930 			cfg->msix.msix_location = ptr;
931 			cfg->msix.msix_ctrl = REG(ptr + PCIR_MSIX_CTRL, 2);
932 			cfg->msix.msix_msgnum = (cfg->msix.msix_ctrl &
933 			    PCIM_MSIXCTRL_TABLE_SIZE) + 1;
934 			val = REG(ptr + PCIR_MSIX_TABLE, 4);
935 			cfg->msix.msix_table_bar = PCIR_BAR(val &
936 			    PCIM_MSIX_BIR_MASK);
937 			cfg->msix.msix_table_offset = val & ~PCIM_MSIX_BIR_MASK;
938 			val = REG(ptr + PCIR_MSIX_PBA, 4);
939 			cfg->msix.msix_pba_bar = PCIR_BAR(val &
940 			    PCIM_MSIX_BIR_MASK);
941 			cfg->msix.msix_pba_offset = val & ~PCIM_MSIX_BIR_MASK;
942 			break;
943 		case PCIY_VPD:		/* PCI Vital Product Data */
944 			cfg->vpd.vpd_reg = ptr;
945 			break;
946 		case PCIY_SUBVENDOR:
947 			/* Should always be true. */
948 			if ((cfg->hdrtype & PCIM_HDRTYPE) ==
949 			    PCIM_HDRTYPE_BRIDGE) {
950 				val = REG(ptr + PCIR_SUBVENDCAP_ID, 4);
951 				cfg->subvendor = val & 0xffff;
952 				cfg->subdevice = val >> 16;
953 			}
954 			break;
955 		case PCIY_PCIX:		/* PCI-X */
956 			/*
957 			 * Assume we have a PCI-X chipset if we have
958 			 * at least one PCI-PCI bridge with a PCI-X
959 			 * capability.  Note that some systems with
960 			 * PCI-express or HT chipsets might match on
961 			 * this check as well.
962 			 */
963 			if ((cfg->hdrtype & PCIM_HDRTYPE) ==
964 			    PCIM_HDRTYPE_BRIDGE)
965 				pcix_chipset = 1;
966 			cfg->pcix.pcix_location = ptr;
967 			break;
968 		case PCIY_EXPRESS:	/* PCI-express */
969 			/*
970 			 * Assume we have a PCI-express chipset if we have
971 			 * at least one PCI-express device.
972 			 */
973 			pcie_chipset = 1;
974 			cfg->pcie.pcie_location = ptr;
975 			val = REG(ptr + PCIER_FLAGS, 2);
976 			cfg->pcie.pcie_type = val & PCIEM_FLAGS_TYPE;
977 			break;
978 		case PCIY_EA:		/* Enhanced Allocation */
979 			cfg->ea.ea_location = ptr;
980 			pci_ea_fill_info(pcib, cfg);
981 			break;
982 		default:
983 			break;
984 		}
985 	}
986 
987 #if defined(__powerpc__)
988 	/*
989 	 * Enable the MSI mapping window for all HyperTransport
990 	 * slaves.  PCI-PCI bridges have their windows enabled via
991 	 * PCIB_MAP_MSI().
992 	 */
993 	if (cfg->ht.ht_slave != 0 && cfg->ht.ht_msimap != 0 &&
994 	    !(cfg->ht.ht_msictrl & PCIM_HTCMD_MSI_ENABLE)) {
995 		device_printf(pcib,
996 	    "Enabling MSI window for HyperTransport slave at pci%d:%d:%d:%d\n",
997 		    cfg->domain, cfg->bus, cfg->slot, cfg->func);
998 		 cfg->ht.ht_msictrl |= PCIM_HTCMD_MSI_ENABLE;
999 		 WREG(cfg->ht.ht_msimap + PCIR_HT_COMMAND, cfg->ht.ht_msictrl,
1000 		     2);
1001 	}
1002 #endif
1003 /* REG and WREG use carry through to next functions */
1004 }
1005 
1006 /*
1007  * PCI Vital Product Data
1008  */
1009 
1010 #define	PCI_VPD_TIMEOUT		1000000
1011 
1012 static int
pci_read_vpd_reg(device_t pcib,pcicfgregs * cfg,int reg,uint32_t * data)1013 pci_read_vpd_reg(device_t pcib, pcicfgregs *cfg, int reg, uint32_t *data)
1014 {
1015 	int count = PCI_VPD_TIMEOUT;
1016 
1017 	KASSERT((reg & 3) == 0, ("VPD register must by 4 byte aligned"));
1018 
1019 	WREG(cfg->vpd.vpd_reg + PCIR_VPD_ADDR, reg, 2);
1020 
1021 	while ((REG(cfg->vpd.vpd_reg + PCIR_VPD_ADDR, 2) & 0x8000) != 0x8000) {
1022 		if (--count < 0)
1023 			return (ENXIO);
1024 		DELAY(1);	/* limit looping */
1025 	}
1026 	*data = (REG(cfg->vpd.vpd_reg + PCIR_VPD_DATA, 4));
1027 
1028 	return (0);
1029 }
1030 
1031 #if 0
1032 static int
1033 pci_write_vpd_reg(device_t pcib, pcicfgregs *cfg, int reg, uint32_t data)
1034 {
1035 	int count = PCI_VPD_TIMEOUT;
1036 
1037 	KASSERT((reg & 3) == 0, ("VPD register must by 4 byte aligned"));
1038 
1039 	WREG(cfg->vpd.vpd_reg + PCIR_VPD_DATA, data, 4);
1040 	WREG(cfg->vpd.vpd_reg + PCIR_VPD_ADDR, reg | 0x8000, 2);
1041 	while ((REG(cfg->vpd.vpd_reg + PCIR_VPD_ADDR, 2) & 0x8000) == 0x8000) {
1042 		if (--count < 0)
1043 			return (ENXIO);
1044 		DELAY(1);	/* limit looping */
1045 	}
1046 
1047 	return (0);
1048 }
1049 #endif
1050 
1051 #undef PCI_VPD_TIMEOUT
1052 
1053 struct vpd_readstate {
1054 	device_t	pcib;
1055 	pcicfgregs	*cfg;
1056 	uint32_t	val;
1057 	int		bytesinval;
1058 	int		off;
1059 	uint8_t		cksum;
1060 };
1061 
1062 static int
vpd_nextbyte(struct vpd_readstate * vrs,uint8_t * data)1063 vpd_nextbyte(struct vpd_readstate *vrs, uint8_t *data)
1064 {
1065 	uint32_t reg;
1066 	uint8_t byte;
1067 
1068 	if (vrs->bytesinval == 0) {
1069 		if (pci_read_vpd_reg(vrs->pcib, vrs->cfg, vrs->off, &reg))
1070 			return (ENXIO);
1071 		vrs->val = le32toh(reg);
1072 		vrs->off += 4;
1073 		byte = vrs->val & 0xff;
1074 		vrs->bytesinval = 3;
1075 	} else {
1076 		vrs->val = vrs->val >> 8;
1077 		byte = vrs->val & 0xff;
1078 		vrs->bytesinval--;
1079 	}
1080 
1081 	vrs->cksum += byte;
1082 	*data = byte;
1083 	return (0);
1084 }
1085 
1086 static void
pci_read_vpd(device_t pcib,pcicfgregs * cfg)1087 pci_read_vpd(device_t pcib, pcicfgregs *cfg)
1088 {
1089 	struct vpd_readstate vrs;
1090 	int state;
1091 	int name;
1092 	int remain;
1093 	int i;
1094 	int alloc, off;		/* alloc/off for RO/W arrays */
1095 	int cksumvalid;
1096 	int dflen;
1097 	int firstrecord;
1098 	uint8_t byte;
1099 	uint8_t byte2;
1100 
1101 	/* init vpd reader */
1102 	vrs.bytesinval = 0;
1103 	vrs.off = 0;
1104 	vrs.pcib = pcib;
1105 	vrs.cfg = cfg;
1106 	vrs.cksum = 0;
1107 
1108 	state = 0;
1109 	name = remain = i = 0;	/* shut up stupid gcc */
1110 	alloc = off = 0;	/* shut up stupid gcc */
1111 	dflen = 0;		/* shut up stupid gcc */
1112 	cksumvalid = -1;
1113 	firstrecord = 1;
1114 	while (state >= 0) {
1115 		if (vpd_nextbyte(&vrs, &byte)) {
1116 			pci_printf(cfg, "VPD read timed out\n");
1117 			state = -2;
1118 			break;
1119 		}
1120 #if 0
1121 		pci_printf(cfg, "vpd: val: %#x, off: %d, bytesinval: %d, byte: "
1122 		    "%#hhx, state: %d, remain: %d, name: %#x, i: %d\n", vrs.val,
1123 		    vrs.off, vrs.bytesinval, byte, state, remain, name, i);
1124 #endif
1125 		switch (state) {
1126 		case 0:		/* item name */
1127 			if (byte & 0x80) {
1128 				if (vpd_nextbyte(&vrs, &byte2)) {
1129 					state = -2;
1130 					break;
1131 				}
1132 				remain = byte2;
1133 				if (vpd_nextbyte(&vrs, &byte2)) {
1134 					state = -2;
1135 					break;
1136 				}
1137 				remain |= byte2 << 8;
1138 				name = byte & 0x7f;
1139 			} else {
1140 				remain = byte & 0x7;
1141 				name = (byte >> 3) & 0xf;
1142 			}
1143 			if (firstrecord) {
1144 				if (name != 0x2) {
1145 					pci_printf(cfg, "VPD data does not " \
1146 					    "start with ident (%#x)\n", name);
1147 					state = -2;
1148 					break;
1149 				}
1150 				firstrecord = 0;
1151 			}
1152 			if (vrs.off + remain - vrs.bytesinval > 0x8000) {
1153 				pci_printf(cfg,
1154 				    "VPD data overflow, remain %#x\n", remain);
1155 				state = -1;
1156 				break;
1157 			}
1158 			switch (name) {
1159 			case 0x2:	/* String */
1160 				if (cfg->vpd.vpd_ident != NULL) {
1161 					pci_printf(cfg,
1162 					    "duplicate VPD ident record\n");
1163 					state = -2;
1164 					break;
1165 				}
1166 				if (remain > 255) {
1167 					pci_printf(cfg,
1168 					    "VPD ident length %d exceeds 255\n",
1169 					    remain);
1170 					state = -2;
1171 					break;
1172 				}
1173 				cfg->vpd.vpd_ident = malloc(remain + 1,
1174 				    M_DEVBUF, M_WAITOK);
1175 				i = 0;
1176 				state = 1;
1177 				break;
1178 			case 0xf:	/* End */
1179 				state = -1;
1180 				break;
1181 			case 0x10:	/* VPD-R */
1182 				alloc = 8;
1183 				off = 0;
1184 				cfg->vpd.vpd_ros = malloc(alloc *
1185 				    sizeof(*cfg->vpd.vpd_ros), M_DEVBUF,
1186 				    M_WAITOK | M_ZERO);
1187 				state = 2;
1188 				break;
1189 			case 0x11:	/* VPD-W */
1190 				alloc = 8;
1191 				off = 0;
1192 				cfg->vpd.vpd_w = malloc(alloc *
1193 				    sizeof(*cfg->vpd.vpd_w), M_DEVBUF,
1194 				    M_WAITOK | M_ZERO);
1195 				state = 5;
1196 				break;
1197 			default:	/* Invalid data, abort */
1198 				pci_printf(cfg, "invalid VPD name: %#x\n", name);
1199 				state = -2;
1200 				break;
1201 			}
1202 			break;
1203 
1204 		case 1:	/* Identifier String */
1205 			cfg->vpd.vpd_ident[i++] = byte;
1206 			remain--;
1207 			if (remain == 0)  {
1208 				cfg->vpd.vpd_ident[i] = '\0';
1209 				state = 0;
1210 			}
1211 			break;
1212 
1213 		case 2:	/* VPD-R Keyword Header */
1214 			if (off == alloc) {
1215 				cfg->vpd.vpd_ros = reallocf(cfg->vpd.vpd_ros,
1216 				    (alloc *= 2) * sizeof(*cfg->vpd.vpd_ros),
1217 				    M_DEVBUF, M_WAITOK | M_ZERO);
1218 			}
1219 			cfg->vpd.vpd_ros[off].keyword[0] = byte;
1220 			if (vpd_nextbyte(&vrs, &byte2)) {
1221 				state = -2;
1222 				break;
1223 			}
1224 			cfg->vpd.vpd_ros[off].keyword[1] = byte2;
1225 			if (vpd_nextbyte(&vrs, &byte2)) {
1226 				state = -2;
1227 				break;
1228 			}
1229 			cfg->vpd.vpd_ros[off].len = dflen = byte2;
1230 			if (dflen == 0 &&
1231 			    strncmp(cfg->vpd.vpd_ros[off].keyword, "RV",
1232 			    2) == 0) {
1233 				/*
1234 				 * if this happens, we can't trust the rest
1235 				 * of the VPD.
1236 				 */
1237 				pci_printf(cfg, "invalid VPD RV record");
1238 				cksumvalid = 0;
1239 				state = -1;
1240 				break;
1241 			} else if (dflen == 0) {
1242 				cfg->vpd.vpd_ros[off].value = malloc(1 *
1243 				    sizeof(*cfg->vpd.vpd_ros[off].value),
1244 				    M_DEVBUF, M_WAITOK);
1245 				cfg->vpd.vpd_ros[off].value[0] = '\x00';
1246 			} else
1247 				cfg->vpd.vpd_ros[off].value = malloc(
1248 				    (dflen + 1) *
1249 				    sizeof(*cfg->vpd.vpd_ros[off].value),
1250 				    M_DEVBUF, M_WAITOK);
1251 			remain -= 3;
1252 			i = 0;
1253 			/* keep in sync w/ state 3's transistions */
1254 			if (dflen == 0 && remain == 0)
1255 				state = 0;
1256 			else if (dflen == 0)
1257 				state = 2;
1258 			else
1259 				state = 3;
1260 			break;
1261 
1262 		case 3:	/* VPD-R Keyword Value */
1263 			cfg->vpd.vpd_ros[off].value[i++] = byte;
1264 			if (strncmp(cfg->vpd.vpd_ros[off].keyword,
1265 			    "RV", 2) == 0 && cksumvalid == -1) {
1266 				if (vrs.cksum == 0)
1267 					cksumvalid = 1;
1268 				else {
1269 					if (bootverbose)
1270 						pci_printf(cfg,
1271 					    "bad VPD cksum, remain %hhu\n",
1272 						    vrs.cksum);
1273 					cksumvalid = 0;
1274 					state = -1;
1275 					break;
1276 				}
1277 			}
1278 			dflen--;
1279 			remain--;
1280 			/* keep in sync w/ state 2's transistions */
1281 			if (dflen == 0)
1282 				cfg->vpd.vpd_ros[off++].value[i++] = '\0';
1283 			if (dflen == 0 && remain == 0) {
1284 				cfg->vpd.vpd_rocnt = off;
1285 				cfg->vpd.vpd_ros = reallocf(cfg->vpd.vpd_ros,
1286 				    off * sizeof(*cfg->vpd.vpd_ros),
1287 				    M_DEVBUF, M_WAITOK | M_ZERO);
1288 				state = 0;
1289 			} else if (dflen == 0)
1290 				state = 2;
1291 			break;
1292 
1293 		case 4:
1294 			remain--;
1295 			if (remain == 0)
1296 				state = 0;
1297 			break;
1298 
1299 		case 5:	/* VPD-W Keyword Header */
1300 			if (off == alloc) {
1301 				cfg->vpd.vpd_w = reallocf(cfg->vpd.vpd_w,
1302 				    (alloc *= 2) * sizeof(*cfg->vpd.vpd_w),
1303 				    M_DEVBUF, M_WAITOK | M_ZERO);
1304 			}
1305 			cfg->vpd.vpd_w[off].keyword[0] = byte;
1306 			if (vpd_nextbyte(&vrs, &byte2)) {
1307 				state = -2;
1308 				break;
1309 			}
1310 			cfg->vpd.vpd_w[off].keyword[1] = byte2;
1311 			if (vpd_nextbyte(&vrs, &byte2)) {
1312 				state = -2;
1313 				break;
1314 			}
1315 			cfg->vpd.vpd_w[off].len = dflen = byte2;
1316 			cfg->vpd.vpd_w[off].start = vrs.off - vrs.bytesinval;
1317 			cfg->vpd.vpd_w[off].value = malloc((dflen + 1) *
1318 			    sizeof(*cfg->vpd.vpd_w[off].value),
1319 			    M_DEVBUF, M_WAITOK);
1320 			remain -= 3;
1321 			i = 0;
1322 			/* keep in sync w/ state 6's transistions */
1323 			if (dflen == 0 && remain == 0)
1324 				state = 0;
1325 			else if (dflen == 0)
1326 				state = 5;
1327 			else
1328 				state = 6;
1329 			break;
1330 
1331 		case 6:	/* VPD-W Keyword Value */
1332 			cfg->vpd.vpd_w[off].value[i++] = byte;
1333 			dflen--;
1334 			remain--;
1335 			/* keep in sync w/ state 5's transistions */
1336 			if (dflen == 0)
1337 				cfg->vpd.vpd_w[off++].value[i++] = '\0';
1338 			if (dflen == 0 && remain == 0) {
1339 				cfg->vpd.vpd_wcnt = off;
1340 				cfg->vpd.vpd_w = reallocf(cfg->vpd.vpd_w,
1341 				    off * sizeof(*cfg->vpd.vpd_w),
1342 				    M_DEVBUF, M_WAITOK | M_ZERO);
1343 				state = 0;
1344 			} else if (dflen == 0)
1345 				state = 5;
1346 			break;
1347 
1348 		default:
1349 			pci_printf(cfg, "invalid state: %d\n", state);
1350 			state = -1;
1351 			break;
1352 		}
1353 
1354 		if (cfg->vpd.vpd_ident == NULL || cfg->vpd.vpd_ident[0] == '\0') {
1355 			pci_printf(cfg, "no valid vpd ident found\n");
1356 			state = -2;
1357 		}
1358 	}
1359 
1360 	if (cksumvalid <= 0 || state < -1) {
1361 		/* read-only data bad, clean up */
1362 		if (cfg->vpd.vpd_ros != NULL) {
1363 			for (off = 0; cfg->vpd.vpd_ros[off].value; off++)
1364 				free(cfg->vpd.vpd_ros[off].value, M_DEVBUF);
1365 			free(cfg->vpd.vpd_ros, M_DEVBUF);
1366 			cfg->vpd.vpd_ros = NULL;
1367 		}
1368 	}
1369 	if (state < -1) {
1370 		/* I/O error, clean up */
1371 		pci_printf(cfg, "failed to read VPD data.\n");
1372 		if (cfg->vpd.vpd_ident != NULL) {
1373 			free(cfg->vpd.vpd_ident, M_DEVBUF);
1374 			cfg->vpd.vpd_ident = NULL;
1375 		}
1376 		if (cfg->vpd.vpd_w != NULL) {
1377 			for (off = 0; cfg->vpd.vpd_w[off].value; off++)
1378 				free(cfg->vpd.vpd_w[off].value, M_DEVBUF);
1379 			free(cfg->vpd.vpd_w, M_DEVBUF);
1380 			cfg->vpd.vpd_w = NULL;
1381 		}
1382 	}
1383 	cfg->vpd.vpd_cached = 1;
1384 #undef REG
1385 #undef WREG
1386 }
1387 
1388 int
pci_get_vpd_ident_method(device_t dev,device_t child,const char ** identptr)1389 pci_get_vpd_ident_method(device_t dev, device_t child, const char **identptr)
1390 {
1391 	struct pci_devinfo *dinfo = device_get_ivars(child);
1392 	pcicfgregs *cfg = &dinfo->cfg;
1393 
1394 	if (!cfg->vpd.vpd_cached && cfg->vpd.vpd_reg != 0)
1395 		pci_read_vpd(device_get_parent(dev), cfg);
1396 
1397 	*identptr = cfg->vpd.vpd_ident;
1398 
1399 	if (*identptr == NULL)
1400 		return (ENXIO);
1401 
1402 	return (0);
1403 }
1404 
1405 int
pci_get_vpd_readonly_method(device_t dev,device_t child,const char * kw,const char ** vptr)1406 pci_get_vpd_readonly_method(device_t dev, device_t child, const char *kw,
1407 	const char **vptr)
1408 {
1409 	struct pci_devinfo *dinfo = device_get_ivars(child);
1410 	pcicfgregs *cfg = &dinfo->cfg;
1411 	int i;
1412 
1413 	if (!cfg->vpd.vpd_cached && cfg->vpd.vpd_reg != 0)
1414 		pci_read_vpd(device_get_parent(dev), cfg);
1415 
1416 	for (i = 0; i < cfg->vpd.vpd_rocnt; i++)
1417 		if (memcmp(kw, cfg->vpd.vpd_ros[i].keyword,
1418 		    sizeof(cfg->vpd.vpd_ros[i].keyword)) == 0) {
1419 			*vptr = cfg->vpd.vpd_ros[i].value;
1420 			return (0);
1421 		}
1422 
1423 	*vptr = NULL;
1424 	return (ENXIO);
1425 }
1426 
1427 struct pcicfg_vpd *
pci_fetch_vpd_list(device_t dev)1428 pci_fetch_vpd_list(device_t dev)
1429 {
1430 	struct pci_devinfo *dinfo = device_get_ivars(dev);
1431 	pcicfgregs *cfg = &dinfo->cfg;
1432 
1433 	if (!cfg->vpd.vpd_cached && cfg->vpd.vpd_reg != 0)
1434 		pci_read_vpd(device_get_parent(device_get_parent(dev)), cfg);
1435 	return (&cfg->vpd);
1436 }
1437 
1438 /*
1439  * Find the requested HyperTransport capability and return the offset
1440  * in configuration space via the pointer provided.  The function
1441  * returns 0 on success and an error code otherwise.
1442  */
1443 int
pci_find_htcap_method(device_t dev,device_t child,int capability,int * capreg)1444 pci_find_htcap_method(device_t dev, device_t child, int capability, int *capreg)
1445 {
1446 	int ptr, error;
1447 	uint16_t val;
1448 
1449 	error = pci_find_cap(child, PCIY_HT, &ptr);
1450 	if (error)
1451 		return (error);
1452 
1453 	/*
1454 	 * Traverse the capabilities list checking each HT capability
1455 	 * to see if it matches the requested HT capability.
1456 	 */
1457 	for (;;) {
1458 		val = pci_read_config(child, ptr + PCIR_HT_COMMAND, 2);
1459 		if (capability == PCIM_HTCAP_SLAVE ||
1460 		    capability == PCIM_HTCAP_HOST)
1461 			val &= 0xe000;
1462 		else
1463 			val &= PCIM_HTCMD_CAP_MASK;
1464 		if (val == capability) {
1465 			if (capreg != NULL)
1466 				*capreg = ptr;
1467 			return (0);
1468 		}
1469 
1470 		/* Skip to the next HT capability. */
1471 		if (pci_find_next_cap(child, PCIY_HT, ptr, &ptr) != 0)
1472 			break;
1473 	}
1474 
1475 	return (ENOENT);
1476 }
1477 
1478 /*
1479  * Find the next requested HyperTransport capability after start and return
1480  * the offset in configuration space via the pointer provided.  The function
1481  * returns 0 on success and an error code otherwise.
1482  */
1483 int
pci_find_next_htcap_method(device_t dev,device_t child,int capability,int start,int * capreg)1484 pci_find_next_htcap_method(device_t dev, device_t child, int capability,
1485     int start, int *capreg)
1486 {
1487 	int ptr;
1488 	uint16_t val;
1489 
1490 	KASSERT(pci_read_config(child, start + PCICAP_ID, 1) == PCIY_HT,
1491 	    ("start capability is not HyperTransport capability"));
1492 	ptr = start;
1493 
1494 	/*
1495 	 * Traverse the capabilities list checking each HT capability
1496 	 * to see if it matches the requested HT capability.
1497 	 */
1498 	for (;;) {
1499 		/* Skip to the next HT capability. */
1500 		if (pci_find_next_cap(child, PCIY_HT, ptr, &ptr) != 0)
1501 			break;
1502 
1503 		val = pci_read_config(child, ptr + PCIR_HT_COMMAND, 2);
1504 		if (capability == PCIM_HTCAP_SLAVE ||
1505 		    capability == PCIM_HTCAP_HOST)
1506 			val &= 0xe000;
1507 		else
1508 			val &= PCIM_HTCMD_CAP_MASK;
1509 		if (val == capability) {
1510 			if (capreg != NULL)
1511 				*capreg = ptr;
1512 			return (0);
1513 		}
1514 	}
1515 
1516 	return (ENOENT);
1517 }
1518 
1519 /*
1520  * Find the requested capability and return the offset in
1521  * configuration space via the pointer provided.  The function returns
1522  * 0 on success and an error code otherwise.
1523  */
1524 int
pci_find_cap_method(device_t dev,device_t child,int capability,int * capreg)1525 pci_find_cap_method(device_t dev, device_t child, int capability,
1526     int *capreg)
1527 {
1528 	struct pci_devinfo *dinfo = device_get_ivars(child);
1529 	pcicfgregs *cfg = &dinfo->cfg;
1530 	uint32_t status;
1531 	uint8_t ptr;
1532 
1533 	/*
1534 	 * Check the CAP_LIST bit of the PCI status register first.
1535 	 */
1536 	status = pci_read_config(child, PCIR_STATUS, 2);
1537 	if (!(status & PCIM_STATUS_CAPPRESENT))
1538 		return (ENXIO);
1539 
1540 	/*
1541 	 * Determine the start pointer of the capabilities list.
1542 	 */
1543 	switch (cfg->hdrtype & PCIM_HDRTYPE) {
1544 	case PCIM_HDRTYPE_NORMAL:
1545 	case PCIM_HDRTYPE_BRIDGE:
1546 		ptr = PCIR_CAP_PTR;
1547 		break;
1548 	case PCIM_HDRTYPE_CARDBUS:
1549 		ptr = PCIR_CAP_PTR_2;
1550 		break;
1551 	default:
1552 		/* XXX: panic? */
1553 		return (ENXIO);		/* no extended capabilities support */
1554 	}
1555 	ptr = pci_read_config(child, ptr, 1);
1556 
1557 	/*
1558 	 * Traverse the capabilities list.
1559 	 */
1560 	while (ptr != 0) {
1561 		if (pci_read_config(child, ptr + PCICAP_ID, 1) == capability) {
1562 			if (capreg != NULL)
1563 				*capreg = ptr;
1564 			return (0);
1565 		}
1566 		ptr = pci_read_config(child, ptr + PCICAP_NEXTPTR, 1);
1567 	}
1568 
1569 	return (ENOENT);
1570 }
1571 
1572 /*
1573  * Find the next requested capability after start and return the offset in
1574  * configuration space via the pointer provided.  The function returns
1575  * 0 on success and an error code otherwise.
1576  */
1577 int
pci_find_next_cap_method(device_t dev,device_t child,int capability,int start,int * capreg)1578 pci_find_next_cap_method(device_t dev, device_t child, int capability,
1579     int start, int *capreg)
1580 {
1581 	uint8_t ptr;
1582 
1583 	KASSERT(pci_read_config(child, start + PCICAP_ID, 1) == capability,
1584 	    ("start capability is not expected capability"));
1585 
1586 	ptr = pci_read_config(child, start + PCICAP_NEXTPTR, 1);
1587 	while (ptr != 0) {
1588 		if (pci_read_config(child, ptr + PCICAP_ID, 1) == capability) {
1589 			if (capreg != NULL)
1590 				*capreg = ptr;
1591 			return (0);
1592 		}
1593 		ptr = pci_read_config(child, ptr + PCICAP_NEXTPTR, 1);
1594 	}
1595 
1596 	return (ENOENT);
1597 }
1598 
1599 /*
1600  * Find the requested extended capability and return the offset in
1601  * configuration space via the pointer provided.  The function returns
1602  * 0 on success and an error code otherwise.
1603  */
1604 int
pci_find_extcap_method(device_t dev,device_t child,int capability,int * capreg)1605 pci_find_extcap_method(device_t dev, device_t child, int capability,
1606     int *capreg)
1607 {
1608 	struct pci_devinfo *dinfo = device_get_ivars(child);
1609 	pcicfgregs *cfg = &dinfo->cfg;
1610 	uint32_t ecap;
1611 	uint16_t ptr;
1612 
1613 	/* Only supported for PCI-express devices. */
1614 	if (cfg->pcie.pcie_location == 0)
1615 		return (ENXIO);
1616 
1617 	ptr = PCIR_EXTCAP;
1618 	ecap = pci_read_config(child, ptr, 4);
1619 	if (ecap == 0xffffffff || ecap == 0)
1620 		return (ENOENT);
1621 	for (;;) {
1622 		if (PCI_EXTCAP_ID(ecap) == capability) {
1623 			if (capreg != NULL)
1624 				*capreg = ptr;
1625 			return (0);
1626 		}
1627 		ptr = PCI_EXTCAP_NEXTPTR(ecap);
1628 		if (ptr == 0)
1629 			break;
1630 		ecap = pci_read_config(child, ptr, 4);
1631 	}
1632 
1633 	return (ENOENT);
1634 }
1635 
1636 /*
1637  * Find the next requested extended capability after start and return the
1638  * offset in configuration space via the pointer provided.  The function
1639  * returns 0 on success and an error code otherwise.
1640  */
1641 int
pci_find_next_extcap_method(device_t dev,device_t child,int capability,int start,int * capreg)1642 pci_find_next_extcap_method(device_t dev, device_t child, int capability,
1643     int start, int *capreg)
1644 {
1645 	struct pci_devinfo *dinfo = device_get_ivars(child);
1646 	pcicfgregs *cfg = &dinfo->cfg;
1647 	uint32_t ecap;
1648 	uint16_t ptr;
1649 
1650 	/* Only supported for PCI-express devices. */
1651 	if (cfg->pcie.pcie_location == 0)
1652 		return (ENXIO);
1653 
1654 	ecap = pci_read_config(child, start, 4);
1655 	KASSERT(PCI_EXTCAP_ID(ecap) == capability,
1656 	    ("start extended capability is not expected capability"));
1657 	ptr = PCI_EXTCAP_NEXTPTR(ecap);
1658 	while (ptr != 0) {
1659 		ecap = pci_read_config(child, ptr, 4);
1660 		if (PCI_EXTCAP_ID(ecap) == capability) {
1661 			if (capreg != NULL)
1662 				*capreg = ptr;
1663 			return (0);
1664 		}
1665 		ptr = PCI_EXTCAP_NEXTPTR(ecap);
1666 	}
1667 
1668 	return (ENOENT);
1669 }
1670 
1671 /*
1672  * Support for MSI-X message interrupts.
1673  */
1674 static void
pci_write_msix_entry(device_t dev,u_int index,uint64_t address,uint32_t data)1675 pci_write_msix_entry(device_t dev, u_int index, uint64_t address, uint32_t data)
1676 {
1677 	struct pci_devinfo *dinfo = device_get_ivars(dev);
1678 	struct pcicfg_msix *msix = &dinfo->cfg.msix;
1679 	uint32_t offset;
1680 
1681 	KASSERT(msix->msix_table_len > index, ("bogus index"));
1682 	offset = msix->msix_table_offset + index * 16;
1683 	bus_write_4(msix->msix_table_res, offset, address & 0xffffffff);
1684 	bus_write_4(msix->msix_table_res, offset + 4, address >> 32);
1685 	bus_write_4(msix->msix_table_res, offset + 8, data);
1686 }
1687 
1688 void
pci_enable_msix_method(device_t dev,device_t child,u_int index,uint64_t address,uint32_t data)1689 pci_enable_msix_method(device_t dev, device_t child, u_int index,
1690     uint64_t address, uint32_t data)
1691 {
1692 
1693 	if (pci_msix_rewrite_table) {
1694 		struct pci_devinfo *dinfo = device_get_ivars(child);
1695 		struct pcicfg_msix *msix = &dinfo->cfg.msix;
1696 
1697 		/*
1698 		 * Some VM hosts require MSIX to be disabled in the
1699 		 * control register before updating the MSIX table
1700 		 * entries are allowed. It is not enough to only
1701 		 * disable MSIX while updating a single entry. MSIX
1702 		 * must be disabled while updating all entries in the
1703 		 * table.
1704 		 */
1705 		pci_write_config(child,
1706 		    msix->msix_location + PCIR_MSIX_CTRL,
1707 		    msix->msix_ctrl & ~PCIM_MSIXCTRL_MSIX_ENABLE, 2);
1708 		pci_resume_msix(child);
1709 	} else
1710 		pci_write_msix_entry(child, index, address, data);
1711 
1712 	/* Enable MSI -> HT mapping. */
1713 	pci_ht_map_msi(child, address);
1714 }
1715 
1716 void
pci_mask_msix(device_t dev,u_int index)1717 pci_mask_msix(device_t dev, u_int index)
1718 {
1719 	struct pci_devinfo *dinfo = device_get_ivars(dev);
1720 	struct pcicfg_msix *msix = &dinfo->cfg.msix;
1721 	uint32_t offset, val;
1722 
1723 	KASSERT(msix->msix_msgnum > index, ("bogus index"));
1724 	offset = msix->msix_table_offset + index * 16 + 12;
1725 	val = bus_read_4(msix->msix_table_res, offset);
1726 	val |= PCIM_MSIX_VCTRL_MASK;
1727 
1728 	/*
1729 	 * Some devices (e.g. Samsung PM961) do not support reads of this
1730 	 * register, so always write the new value.
1731 	 */
1732 	bus_write_4(msix->msix_table_res, offset, val);
1733 }
1734 
1735 void
pci_unmask_msix(device_t dev,u_int index)1736 pci_unmask_msix(device_t dev, u_int index)
1737 {
1738 	struct pci_devinfo *dinfo = device_get_ivars(dev);
1739 	struct pcicfg_msix *msix = &dinfo->cfg.msix;
1740 	uint32_t offset, val;
1741 
1742 	KASSERT(msix->msix_table_len > index, ("bogus index"));
1743 	offset = msix->msix_table_offset + index * 16 + 12;
1744 	val = bus_read_4(msix->msix_table_res, offset);
1745 	val &= ~PCIM_MSIX_VCTRL_MASK;
1746 
1747 	/*
1748 	 * Some devices (e.g. Samsung PM961) do not support reads of this
1749 	 * register, so always write the new value.
1750 	 */
1751 	bus_write_4(msix->msix_table_res, offset, val);
1752 }
1753 
1754 int
pci_pending_msix(device_t dev,u_int index)1755 pci_pending_msix(device_t dev, u_int index)
1756 {
1757 	struct pci_devinfo *dinfo = device_get_ivars(dev);
1758 	struct pcicfg_msix *msix = &dinfo->cfg.msix;
1759 	uint32_t offset, bit;
1760 
1761 	KASSERT(msix->msix_table_len > index, ("bogus index"));
1762 	offset = msix->msix_pba_offset + (index / 32) * 4;
1763 	bit = 1 << index % 32;
1764 	return (bus_read_4(msix->msix_pba_res, offset) & bit);
1765 }
1766 
1767 /*
1768  * Restore MSI-X registers and table during resume.  If MSI-X is
1769  * enabled then walk the virtual table to restore the actual MSI-X
1770  * table.
1771  */
1772 static void
pci_resume_msix(device_t dev)1773 pci_resume_msix(device_t dev)
1774 {
1775 	struct pci_devinfo *dinfo = device_get_ivars(dev);
1776 	struct pcicfg_msix *msix = &dinfo->cfg.msix;
1777 	struct msix_table_entry *mte;
1778 	struct msix_vector *mv;
1779 	int i;
1780 
1781 	if (msix->msix_alloc > 0) {
1782 		/* First, mask all vectors. */
1783 		for (i = 0; i < msix->msix_msgnum; i++)
1784 			pci_mask_msix(dev, i);
1785 
1786 		/* Second, program any messages with at least one handler. */
1787 		for (i = 0; i < msix->msix_table_len; i++) {
1788 			mte = &msix->msix_table[i];
1789 			if (mte->mte_vector == 0 || mte->mte_handlers == 0)
1790 				continue;
1791 			mv = &msix->msix_vectors[mte->mte_vector - 1];
1792 			pci_write_msix_entry(dev, i, mv->mv_address,
1793 			    mv->mv_data);
1794 			pci_unmask_msix(dev, i);
1795 		}
1796 	}
1797 	pci_write_config(dev, msix->msix_location + PCIR_MSIX_CTRL,
1798 	    msix->msix_ctrl, 2);
1799 }
1800 
1801 /*
1802  * Attempt to allocate *count MSI-X messages.  The actual number allocated is
1803  * returned in *count.  After this function returns, each message will be
1804  * available to the driver as SYS_RES_IRQ resources starting at rid 1.
1805  */
1806 int
pci_alloc_msix_method(device_t dev,device_t child,int * count)1807 pci_alloc_msix_method(device_t dev, device_t child, int *count)
1808 {
1809 	struct pci_devinfo *dinfo = device_get_ivars(child);
1810 	pcicfgregs *cfg = &dinfo->cfg;
1811 	struct resource_list_entry *rle;
1812 	int actual, error, i, irq, max;
1813 
1814 	/* Don't let count == 0 get us into trouble. */
1815 	if (*count == 0)
1816 		return (EINVAL);
1817 
1818 	/* If rid 0 is allocated, then fail. */
1819 	rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, 0);
1820 	if (rle != NULL && rle->res != NULL)
1821 		return (ENXIO);
1822 
1823 	/* Already have allocated messages? */
1824 	if (cfg->msi.msi_alloc != 0 || cfg->msix.msix_alloc != 0)
1825 		return (ENXIO);
1826 
1827 	/* If MSI-X is blacklisted for this system, fail. */
1828 	if (pci_msix_blacklisted())
1829 		return (ENXIO);
1830 
1831 	/* MSI-X capability present? */
1832 	if (cfg->msix.msix_location == 0 || !pci_do_msix)
1833 		return (ENODEV);
1834 
1835 	/* Make sure the appropriate BARs are mapped. */
1836 	rle = resource_list_find(&dinfo->resources, SYS_RES_MEMORY,
1837 	    cfg->msix.msix_table_bar);
1838 	if (rle == NULL || rle->res == NULL ||
1839 	    !(rman_get_flags(rle->res) & RF_ACTIVE))
1840 		return (ENXIO);
1841 	cfg->msix.msix_table_res = rle->res;
1842 	if (cfg->msix.msix_pba_bar != cfg->msix.msix_table_bar) {
1843 		rle = resource_list_find(&dinfo->resources, SYS_RES_MEMORY,
1844 		    cfg->msix.msix_pba_bar);
1845 		if (rle == NULL || rle->res == NULL ||
1846 		    !(rman_get_flags(rle->res) & RF_ACTIVE))
1847 			return (ENXIO);
1848 	}
1849 	cfg->msix.msix_pba_res = rle->res;
1850 
1851 	if (bootverbose)
1852 		device_printf(child,
1853 		    "attempting to allocate %d MSI-X vectors (%d supported)\n",
1854 		    *count, cfg->msix.msix_msgnum);
1855 	max = min(*count, cfg->msix.msix_msgnum);
1856 	for (i = 0; i < max; i++) {
1857 		/* Allocate a message. */
1858 		error = PCIB_ALLOC_MSIX(device_get_parent(dev), child, &irq);
1859 		if (error) {
1860 			if (i == 0)
1861 				return (error);
1862 			break;
1863 		}
1864 		resource_list_add(&dinfo->resources, SYS_RES_IRQ, i + 1, irq,
1865 		    irq, 1);
1866 	}
1867 	actual = i;
1868 
1869 	if (bootverbose) {
1870 		rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, 1);
1871 		if (actual == 1)
1872 			device_printf(child, "using IRQ %ju for MSI-X\n",
1873 			    rle->start);
1874 		else {
1875 			int run;
1876 
1877 			/*
1878 			 * Be fancy and try to print contiguous runs of
1879 			 * IRQ values as ranges.  'irq' is the previous IRQ.
1880 			 * 'run' is true if we are in a range.
1881 			 */
1882 			device_printf(child, "using IRQs %ju", rle->start);
1883 			irq = rle->start;
1884 			run = 0;
1885 			for (i = 1; i < actual; i++) {
1886 				rle = resource_list_find(&dinfo->resources,
1887 				    SYS_RES_IRQ, i + 1);
1888 
1889 				/* Still in a run? */
1890 				if (rle->start == irq + 1) {
1891 					run = 1;
1892 					irq++;
1893 					continue;
1894 				}
1895 
1896 				/* Finish previous range. */
1897 				if (run) {
1898 					printf("-%d", irq);
1899 					run = 0;
1900 				}
1901 
1902 				/* Start new range. */
1903 				printf(",%ju", rle->start);
1904 				irq = rle->start;
1905 			}
1906 
1907 			/* Unfinished range? */
1908 			if (run)
1909 				printf("-%d", irq);
1910 			printf(" for MSI-X\n");
1911 		}
1912 	}
1913 
1914 	/* Mask all vectors. */
1915 	for (i = 0; i < cfg->msix.msix_msgnum; i++)
1916 		pci_mask_msix(child, i);
1917 
1918 	/* Allocate and initialize vector data and virtual table. */
1919 	cfg->msix.msix_vectors = malloc(sizeof(struct msix_vector) * actual,
1920 	    M_DEVBUF, M_WAITOK | M_ZERO);
1921 	cfg->msix.msix_table = malloc(sizeof(struct msix_table_entry) * actual,
1922 	    M_DEVBUF, M_WAITOK | M_ZERO);
1923 	for (i = 0; i < actual; i++) {
1924 		rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, i + 1);
1925 		cfg->msix.msix_vectors[i].mv_irq = rle->start;
1926 		cfg->msix.msix_table[i].mte_vector = i + 1;
1927 	}
1928 
1929 	/* Update control register to enable MSI-X. */
1930 	cfg->msix.msix_ctrl |= PCIM_MSIXCTRL_MSIX_ENABLE;
1931 	pci_write_config(child, cfg->msix.msix_location + PCIR_MSIX_CTRL,
1932 	    cfg->msix.msix_ctrl, 2);
1933 
1934 	/* Update counts of alloc'd messages. */
1935 	cfg->msix.msix_alloc = actual;
1936 	cfg->msix.msix_table_len = actual;
1937 	*count = actual;
1938 	return (0);
1939 }
1940 
1941 /*
1942  * By default, pci_alloc_msix() will assign the allocated IRQ
1943  * resources consecutively to the first N messages in the MSI-X table.
1944  * However, device drivers may want to use different layouts if they
1945  * either receive fewer messages than they asked for, or they wish to
1946  * populate the MSI-X table sparsely.  This method allows the driver
1947  * to specify what layout it wants.  It must be called after a
1948  * successful pci_alloc_msix() but before any of the associated
1949  * SYS_RES_IRQ resources are allocated via bus_alloc_resource().
1950  *
1951  * The 'vectors' array contains 'count' message vectors.  The array
1952  * maps directly to the MSI-X table in that index 0 in the array
1953  * specifies the vector for the first message in the MSI-X table, etc.
1954  * The vector value in each array index can either be 0 to indicate
1955  * that no vector should be assigned to a message slot, or it can be a
1956  * number from 1 to N (where N is the count returned from a
1957  * succcessful call to pci_alloc_msix()) to indicate which message
1958  * vector (IRQ) to be used for the corresponding message.
1959  *
1960  * On successful return, each message with a non-zero vector will have
1961  * an associated SYS_RES_IRQ whose rid is equal to the array index +
1962  * 1.  Additionally, if any of the IRQs allocated via the previous
1963  * call to pci_alloc_msix() are not used in the mapping, those IRQs
1964  * will be freed back to the system automatically.
1965  *
1966  * For example, suppose a driver has a MSI-X table with 6 messages and
1967  * asks for 6 messages, but pci_alloc_msix() only returns a count of
1968  * 3.  Call the three vectors allocated by pci_alloc_msix() A, B, and
1969  * C.  After the call to pci_alloc_msix(), the device will be setup to
1970  * have an MSI-X table of ABC--- (where - means no vector assigned).
1971  * If the driver then passes a vector array of { 1, 0, 1, 2, 0, 2 },
1972  * then the MSI-X table will look like A-AB-B, and the 'C' vector will
1973  * be freed back to the system.  This device will also have valid
1974  * SYS_RES_IRQ rids of 1, 3, 4, and 6.
1975  *
1976  * In any case, the SYS_RES_IRQ rid X will always map to the message
1977  * at MSI-X table index X - 1 and will only be valid if a vector is
1978  * assigned to that table entry.
1979  */
1980 int
pci_remap_msix_method(device_t dev,device_t child,int count,const u_int * vectors)1981 pci_remap_msix_method(device_t dev, device_t child, int count,
1982     const u_int *vectors)
1983 {
1984 	struct pci_devinfo *dinfo = device_get_ivars(child);
1985 	struct pcicfg_msix *msix = &dinfo->cfg.msix;
1986 	struct resource_list_entry *rle;
1987 	int i, irq, j, *used;
1988 
1989 	/*
1990 	 * Have to have at least one message in the table but the
1991 	 * table can't be bigger than the actual MSI-X table in the
1992 	 * device.
1993 	 */
1994 	if (count == 0 || count > msix->msix_msgnum)
1995 		return (EINVAL);
1996 
1997 	/* Sanity check the vectors. */
1998 	for (i = 0; i < count; i++)
1999 		if (vectors[i] > msix->msix_alloc)
2000 			return (EINVAL);
2001 
2002 	/*
2003 	 * Make sure there aren't any holes in the vectors to be used.
2004 	 * It's a big pain to support it, and it doesn't really make
2005 	 * sense anyway.  Also, at least one vector must be used.
2006 	 */
2007 	used = malloc(sizeof(int) * msix->msix_alloc, M_DEVBUF, M_WAITOK |
2008 	    M_ZERO);
2009 	for (i = 0; i < count; i++)
2010 		if (vectors[i] != 0)
2011 			used[vectors[i] - 1] = 1;
2012 	for (i = 0; i < msix->msix_alloc - 1; i++)
2013 		if (used[i] == 0 && used[i + 1] == 1) {
2014 			free(used, M_DEVBUF);
2015 			return (EINVAL);
2016 		}
2017 	if (used[0] != 1) {
2018 		free(used, M_DEVBUF);
2019 		return (EINVAL);
2020 	}
2021 
2022 	/* Make sure none of the resources are allocated. */
2023 	for (i = 0; i < msix->msix_table_len; i++) {
2024 		if (msix->msix_table[i].mte_vector == 0)
2025 			continue;
2026 		if (msix->msix_table[i].mte_handlers > 0) {
2027 			free(used, M_DEVBUF);
2028 			return (EBUSY);
2029 		}
2030 		rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, i + 1);
2031 		KASSERT(rle != NULL, ("missing resource"));
2032 		if (rle->res != NULL) {
2033 			free(used, M_DEVBUF);
2034 			return (EBUSY);
2035 		}
2036 	}
2037 
2038 	/* Free the existing resource list entries. */
2039 	for (i = 0; i < msix->msix_table_len; i++) {
2040 		if (msix->msix_table[i].mte_vector == 0)
2041 			continue;
2042 		resource_list_delete(&dinfo->resources, SYS_RES_IRQ, i + 1);
2043 	}
2044 
2045 	/*
2046 	 * Build the new virtual table keeping track of which vectors are
2047 	 * used.
2048 	 */
2049 	free(msix->msix_table, M_DEVBUF);
2050 	msix->msix_table = malloc(sizeof(struct msix_table_entry) * count,
2051 	    M_DEVBUF, M_WAITOK | M_ZERO);
2052 	for (i = 0; i < count; i++)
2053 		msix->msix_table[i].mte_vector = vectors[i];
2054 	msix->msix_table_len = count;
2055 
2056 	/* Free any unused IRQs and resize the vectors array if necessary. */
2057 	j = msix->msix_alloc - 1;
2058 	if (used[j] == 0) {
2059 		struct msix_vector *vec;
2060 
2061 		while (used[j] == 0) {
2062 			PCIB_RELEASE_MSIX(device_get_parent(dev), child,
2063 			    msix->msix_vectors[j].mv_irq);
2064 			j--;
2065 		}
2066 		vec = malloc(sizeof(struct msix_vector) * (j + 1), M_DEVBUF,
2067 		    M_WAITOK);
2068 		bcopy(msix->msix_vectors, vec, sizeof(struct msix_vector) *
2069 		    (j + 1));
2070 		free(msix->msix_vectors, M_DEVBUF);
2071 		msix->msix_vectors = vec;
2072 		msix->msix_alloc = j + 1;
2073 	}
2074 	free(used, M_DEVBUF);
2075 
2076 	/* Map the IRQs onto the rids. */
2077 	for (i = 0; i < count; i++) {
2078 		if (vectors[i] == 0)
2079 			continue;
2080 		irq = msix->msix_vectors[vectors[i] - 1].mv_irq;
2081 		resource_list_add(&dinfo->resources, SYS_RES_IRQ, i + 1, irq,
2082 		    irq, 1);
2083 	}
2084 
2085 	if (bootverbose) {
2086 		device_printf(child, "Remapped MSI-X IRQs as: ");
2087 		for (i = 0; i < count; i++) {
2088 			if (i != 0)
2089 				printf(", ");
2090 			if (vectors[i] == 0)
2091 				printf("---");
2092 			else
2093 				printf("%d",
2094 				    msix->msix_vectors[vectors[i] - 1].mv_irq);
2095 		}
2096 		printf("\n");
2097 	}
2098 
2099 	return (0);
2100 }
2101 
2102 static int
pci_release_msix(device_t dev,device_t child)2103 pci_release_msix(device_t dev, device_t child)
2104 {
2105 	struct pci_devinfo *dinfo = device_get_ivars(child);
2106 	struct pcicfg_msix *msix = &dinfo->cfg.msix;
2107 	struct resource_list_entry *rle;
2108 	int i;
2109 
2110 	/* Do we have any messages to release? */
2111 	if (msix->msix_alloc == 0)
2112 		return (ENODEV);
2113 
2114 	/* Make sure none of the resources are allocated. */
2115 	for (i = 0; i < msix->msix_table_len; i++) {
2116 		if (msix->msix_table[i].mte_vector == 0)
2117 			continue;
2118 		if (msix->msix_table[i].mte_handlers > 0)
2119 			return (EBUSY);
2120 		rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, i + 1);
2121 		KASSERT(rle != NULL, ("missing resource"));
2122 		if (rle->res != NULL)
2123 			return (EBUSY);
2124 	}
2125 
2126 	/* Update control register to disable MSI-X. */
2127 	msix->msix_ctrl &= ~PCIM_MSIXCTRL_MSIX_ENABLE;
2128 	pci_write_config(child, msix->msix_location + PCIR_MSIX_CTRL,
2129 	    msix->msix_ctrl, 2);
2130 
2131 	/* Free the resource list entries. */
2132 	for (i = 0; i < msix->msix_table_len; i++) {
2133 		if (msix->msix_table[i].mte_vector == 0)
2134 			continue;
2135 		resource_list_delete(&dinfo->resources, SYS_RES_IRQ, i + 1);
2136 	}
2137 	free(msix->msix_table, M_DEVBUF);
2138 	msix->msix_table_len = 0;
2139 
2140 	/* Release the IRQs. */
2141 	for (i = 0; i < msix->msix_alloc; i++)
2142 		PCIB_RELEASE_MSIX(device_get_parent(dev), child,
2143 		    msix->msix_vectors[i].mv_irq);
2144 	free(msix->msix_vectors, M_DEVBUF);
2145 	msix->msix_alloc = 0;
2146 	return (0);
2147 }
2148 
2149 /*
2150  * Return the max supported MSI-X messages this device supports.
2151  * Basically, assuming the MD code can alloc messages, this function
2152  * should return the maximum value that pci_alloc_msix() can return.
2153  * Thus, it is subject to the tunables, etc.
2154  */
2155 int
pci_msix_count_method(device_t dev,device_t child)2156 pci_msix_count_method(device_t dev, device_t child)
2157 {
2158 	struct pci_devinfo *dinfo = device_get_ivars(child);
2159 	struct pcicfg_msix *msix = &dinfo->cfg.msix;
2160 
2161 	if (pci_do_msix && msix->msix_location != 0)
2162 		return (msix->msix_msgnum);
2163 	return (0);
2164 }
2165 
2166 int
pci_msix_pba_bar_method(device_t dev,device_t child)2167 pci_msix_pba_bar_method(device_t dev, device_t child)
2168 {
2169 	struct pci_devinfo *dinfo = device_get_ivars(child);
2170 	struct pcicfg_msix *msix = &dinfo->cfg.msix;
2171 
2172 	if (pci_do_msix && msix->msix_location != 0)
2173 		return (msix->msix_pba_bar);
2174 	return (-1);
2175 }
2176 
2177 int
pci_msix_table_bar_method(device_t dev,device_t child)2178 pci_msix_table_bar_method(device_t dev, device_t child)
2179 {
2180 	struct pci_devinfo *dinfo = device_get_ivars(child);
2181 	struct pcicfg_msix *msix = &dinfo->cfg.msix;
2182 
2183 	if (pci_do_msix && msix->msix_location != 0)
2184 		return (msix->msix_table_bar);
2185 	return (-1);
2186 }
2187 
2188 /*
2189  * HyperTransport MSI mapping control
2190  */
2191 void
pci_ht_map_msi(device_t dev,uint64_t addr)2192 pci_ht_map_msi(device_t dev, uint64_t addr)
2193 {
2194 	struct pci_devinfo *dinfo = device_get_ivars(dev);
2195 	struct pcicfg_ht *ht = &dinfo->cfg.ht;
2196 
2197 	if (!ht->ht_msimap)
2198 		return;
2199 
2200 	if (addr && !(ht->ht_msictrl & PCIM_HTCMD_MSI_ENABLE) &&
2201 	    ht->ht_msiaddr >> 20 == addr >> 20) {
2202 		/* Enable MSI -> HT mapping. */
2203 		ht->ht_msictrl |= PCIM_HTCMD_MSI_ENABLE;
2204 		pci_write_config(dev, ht->ht_msimap + PCIR_HT_COMMAND,
2205 		    ht->ht_msictrl, 2);
2206 	}
2207 
2208 	if (!addr && ht->ht_msictrl & PCIM_HTCMD_MSI_ENABLE) {
2209 		/* Disable MSI -> HT mapping. */
2210 		ht->ht_msictrl &= ~PCIM_HTCMD_MSI_ENABLE;
2211 		pci_write_config(dev, ht->ht_msimap + PCIR_HT_COMMAND,
2212 		    ht->ht_msictrl, 2);
2213 	}
2214 }
2215 
2216 int
pci_get_relaxed_ordering_enabled(device_t dev)2217 pci_get_relaxed_ordering_enabled(device_t dev)
2218 {
2219 	struct pci_devinfo *dinfo = device_get_ivars(dev);
2220 	int cap;
2221 	uint16_t val;
2222 
2223 	cap = dinfo->cfg.pcie.pcie_location;
2224 	if (cap == 0)
2225 		return (0);
2226 	val = pci_read_config(dev, cap + PCIER_DEVICE_CTL, 2);
2227 	val &= PCIEM_CTL_RELAXED_ORD_ENABLE;
2228 	return (val != 0);
2229 }
2230 
2231 int
pci_get_max_payload(device_t dev)2232 pci_get_max_payload(device_t dev)
2233 {
2234 	struct pci_devinfo *dinfo = device_get_ivars(dev);
2235 	int cap;
2236 	uint16_t val;
2237 
2238 	cap = dinfo->cfg.pcie.pcie_location;
2239 	if (cap == 0)
2240 		return (0);
2241 	val = pci_read_config(dev, cap + PCIER_DEVICE_CTL, 2);
2242 	val &= PCIEM_CTL_MAX_PAYLOAD;
2243 	val >>= 5;
2244 	return (1 << (val + 7));
2245 }
2246 
2247 int
pci_get_max_read_req(device_t dev)2248 pci_get_max_read_req(device_t dev)
2249 {
2250 	struct pci_devinfo *dinfo = device_get_ivars(dev);
2251 	int cap;
2252 	uint16_t val;
2253 
2254 	cap = dinfo->cfg.pcie.pcie_location;
2255 	if (cap == 0)
2256 		return (0);
2257 	val = pci_read_config(dev, cap + PCIER_DEVICE_CTL, 2);
2258 	val &= PCIEM_CTL_MAX_READ_REQUEST;
2259 	val >>= 12;
2260 	return (1 << (val + 7));
2261 }
2262 
2263 int
pci_set_max_read_req(device_t dev,int size)2264 pci_set_max_read_req(device_t dev, int size)
2265 {
2266 	struct pci_devinfo *dinfo = device_get_ivars(dev);
2267 	int cap;
2268 	uint16_t val;
2269 
2270 	cap = dinfo->cfg.pcie.pcie_location;
2271 	if (cap == 0)
2272 		return (0);
2273 	if (size < 128)
2274 		size = 128;
2275 	if (size > 4096)
2276 		size = 4096;
2277 	size = (1 << (fls(size) - 1));
2278 	val = pci_read_config(dev, cap + PCIER_DEVICE_CTL, 2);
2279 	val &= ~PCIEM_CTL_MAX_READ_REQUEST;
2280 	val |= (fls(size) - 8) << 12;
2281 	pci_write_config(dev, cap + PCIER_DEVICE_CTL, val, 2);
2282 	return (size);
2283 }
2284 
2285 uint32_t
pcie_read_config(device_t dev,int reg,int width)2286 pcie_read_config(device_t dev, int reg, int width)
2287 {
2288 	struct pci_devinfo *dinfo = device_get_ivars(dev);
2289 	int cap;
2290 
2291 	cap = dinfo->cfg.pcie.pcie_location;
2292 	if (cap == 0) {
2293 		if (width == 2)
2294 			return (0xffff);
2295 		return (0xffffffff);
2296 	}
2297 
2298 	return (pci_read_config(dev, cap + reg, width));
2299 }
2300 
2301 void
pcie_write_config(device_t dev,int reg,uint32_t value,int width)2302 pcie_write_config(device_t dev, int reg, uint32_t value, int width)
2303 {
2304 	struct pci_devinfo *dinfo = device_get_ivars(dev);
2305 	int cap;
2306 
2307 	cap = dinfo->cfg.pcie.pcie_location;
2308 	if (cap == 0)
2309 		return;
2310 	pci_write_config(dev, cap + reg, value, width);
2311 }
2312 
2313 /*
2314  * Adjusts a PCI-e capability register by clearing the bits in mask
2315  * and setting the bits in (value & mask).  Bits not set in mask are
2316  * not adjusted.
2317  *
2318  * Returns the old value on success or all ones on failure.
2319  */
2320 uint32_t
pcie_adjust_config(device_t dev,int reg,uint32_t mask,uint32_t value,int width)2321 pcie_adjust_config(device_t dev, int reg, uint32_t mask, uint32_t value,
2322     int width)
2323 {
2324 	struct pci_devinfo *dinfo = device_get_ivars(dev);
2325 	uint32_t old, new;
2326 	int cap;
2327 
2328 	cap = dinfo->cfg.pcie.pcie_location;
2329 	if (cap == 0) {
2330 		if (width == 2)
2331 			return (0xffff);
2332 		return (0xffffffff);
2333 	}
2334 
2335 	old = pci_read_config(dev, cap + reg, width);
2336 	new = old & ~mask;
2337 	new |= (value & mask);
2338 	pci_write_config(dev, cap + reg, new, width);
2339 	return (old);
2340 }
2341 
2342 /*
2343  * Support for MSI message signalled interrupts.
2344  */
2345 void
pci_enable_msi_method(device_t dev,device_t child,uint64_t address,uint16_t data)2346 pci_enable_msi_method(device_t dev, device_t child, uint64_t address,
2347     uint16_t data)
2348 {
2349 	struct pci_devinfo *dinfo = device_get_ivars(child);
2350 	struct pcicfg_msi *msi = &dinfo->cfg.msi;
2351 
2352 	/* Write data and address values. */
2353 	pci_write_config(child, msi->msi_location + PCIR_MSI_ADDR,
2354 	    address & 0xffffffff, 4);
2355 	if (msi->msi_ctrl & PCIM_MSICTRL_64BIT) {
2356 		pci_write_config(child, msi->msi_location + PCIR_MSI_ADDR_HIGH,
2357 		    address >> 32, 4);
2358 		pci_write_config(child, msi->msi_location + PCIR_MSI_DATA_64BIT,
2359 		    data, 2);
2360 	} else
2361 		pci_write_config(child, msi->msi_location + PCIR_MSI_DATA, data,
2362 		    2);
2363 
2364 	/* Enable MSI in the control register. */
2365 	msi->msi_ctrl |= PCIM_MSICTRL_MSI_ENABLE;
2366 	pci_write_config(child, msi->msi_location + PCIR_MSI_CTRL,
2367 	    msi->msi_ctrl, 2);
2368 
2369 	/* Enable MSI -> HT mapping. */
2370 	pci_ht_map_msi(child, address);
2371 }
2372 
2373 void
pci_disable_msi_method(device_t dev,device_t child)2374 pci_disable_msi_method(device_t dev, device_t child)
2375 {
2376 	struct pci_devinfo *dinfo = device_get_ivars(child);
2377 	struct pcicfg_msi *msi = &dinfo->cfg.msi;
2378 
2379 	/* Disable MSI -> HT mapping. */
2380 	pci_ht_map_msi(child, 0);
2381 
2382 	/* Disable MSI in the control register. */
2383 	msi->msi_ctrl &= ~PCIM_MSICTRL_MSI_ENABLE;
2384 	pci_write_config(child, msi->msi_location + PCIR_MSI_CTRL,
2385 	    msi->msi_ctrl, 2);
2386 }
2387 
2388 /*
2389  * Restore MSI registers during resume.  If MSI is enabled then
2390  * restore the data and address registers in addition to the control
2391  * register.
2392  */
2393 static void
pci_resume_msi(device_t dev)2394 pci_resume_msi(device_t dev)
2395 {
2396 	struct pci_devinfo *dinfo = device_get_ivars(dev);
2397 	struct pcicfg_msi *msi = &dinfo->cfg.msi;
2398 	uint64_t address;
2399 	uint16_t data;
2400 
2401 	if (msi->msi_ctrl & PCIM_MSICTRL_MSI_ENABLE) {
2402 		address = msi->msi_addr;
2403 		data = msi->msi_data;
2404 		pci_write_config(dev, msi->msi_location + PCIR_MSI_ADDR,
2405 		    address & 0xffffffff, 4);
2406 		if (msi->msi_ctrl & PCIM_MSICTRL_64BIT) {
2407 			pci_write_config(dev, msi->msi_location +
2408 			    PCIR_MSI_ADDR_HIGH, address >> 32, 4);
2409 			pci_write_config(dev, msi->msi_location +
2410 			    PCIR_MSI_DATA_64BIT, data, 2);
2411 		} else
2412 			pci_write_config(dev, msi->msi_location + PCIR_MSI_DATA,
2413 			    data, 2);
2414 	}
2415 	pci_write_config(dev, msi->msi_location + PCIR_MSI_CTRL, msi->msi_ctrl,
2416 	    2);
2417 }
2418 
2419 static int
pci_remap_intr_method(device_t bus,device_t dev,u_int irq)2420 pci_remap_intr_method(device_t bus, device_t dev, u_int irq)
2421 {
2422 	struct pci_devinfo *dinfo = device_get_ivars(dev);
2423 	pcicfgregs *cfg = &dinfo->cfg;
2424 	struct resource_list_entry *rle;
2425 	struct msix_table_entry *mte;
2426 	struct msix_vector *mv;
2427 	uint64_t addr;
2428 	uint32_t data;
2429 	int error, i, j;
2430 
2431 	/*
2432 	 * Handle MSI first.  We try to find this IRQ among our list
2433 	 * of MSI IRQs.  If we find it, we request updated address and
2434 	 * data registers and apply the results.
2435 	 */
2436 	if (cfg->msi.msi_alloc > 0) {
2437 		/* If we don't have any active handlers, nothing to do. */
2438 		if (cfg->msi.msi_handlers == 0)
2439 			return (0);
2440 		for (i = 0; i < cfg->msi.msi_alloc; i++) {
2441 			rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ,
2442 			    i + 1);
2443 			if (rle->start == irq) {
2444 				error = PCIB_MAP_MSI(device_get_parent(bus),
2445 				    dev, irq, &addr, &data);
2446 				if (error)
2447 					return (error);
2448 				pci_disable_msi(dev);
2449 				dinfo->cfg.msi.msi_addr = addr;
2450 				dinfo->cfg.msi.msi_data = data;
2451 				pci_enable_msi(dev, addr, data);
2452 				return (0);
2453 			}
2454 		}
2455 		return (ENOENT);
2456 	}
2457 
2458 	/*
2459 	 * For MSI-X, we check to see if we have this IRQ.  If we do,
2460 	 * we request the updated mapping info.  If that works, we go
2461 	 * through all the slots that use this IRQ and update them.
2462 	 */
2463 	if (cfg->msix.msix_alloc > 0) {
2464 		for (i = 0; i < cfg->msix.msix_alloc; i++) {
2465 			mv = &cfg->msix.msix_vectors[i];
2466 			if (mv->mv_irq == irq) {
2467 				error = PCIB_MAP_MSI(device_get_parent(bus),
2468 				    dev, irq, &addr, &data);
2469 				if (error)
2470 					return (error);
2471 				mv->mv_address = addr;
2472 				mv->mv_data = data;
2473 				for (j = 0; j < cfg->msix.msix_table_len; j++) {
2474 					mte = &cfg->msix.msix_table[j];
2475 					if (mte->mte_vector != i + 1)
2476 						continue;
2477 					if (mte->mte_handlers == 0)
2478 						continue;
2479 					pci_mask_msix(dev, j);
2480 					pci_enable_msix(dev, j, addr, data);
2481 					pci_unmask_msix(dev, j);
2482 				}
2483 			}
2484 		}
2485 		return (ENOENT);
2486 	}
2487 
2488 	return (ENOENT);
2489 }
2490 
2491 /*
2492  * Returns true if the specified device is blacklisted because MSI
2493  * doesn't work.
2494  */
2495 int
pci_msi_device_blacklisted(device_t dev)2496 pci_msi_device_blacklisted(device_t dev)
2497 {
2498 
2499 	if (!pci_honor_msi_blacklist)
2500 		return (0);
2501 
2502 	return (pci_has_quirk(pci_get_devid(dev), PCI_QUIRK_DISABLE_MSI));
2503 }
2504 
2505 /*
2506  * Determine if MSI is blacklisted globally on this system.  Currently,
2507  * we just check for blacklisted chipsets as represented by the
2508  * host-PCI bridge at device 0:0:0.  In the future, it may become
2509  * necessary to check other system attributes, such as the kenv values
2510  * that give the motherboard manufacturer and model number.
2511  */
2512 static int
pci_msi_blacklisted(void)2513 pci_msi_blacklisted(void)
2514 {
2515 	device_t dev;
2516 
2517 	if (!pci_honor_msi_blacklist)
2518 		return (0);
2519 
2520 	/* Blacklist all non-PCI-express and non-PCI-X chipsets. */
2521 	if (!(pcie_chipset || pcix_chipset)) {
2522 		if (vm_guest != VM_GUEST_NO) {
2523 			/*
2524 			 * Whitelist older chipsets in virtual
2525 			 * machines known to support MSI.
2526 			 */
2527 			dev = pci_find_bsf(0, 0, 0);
2528 			if (dev != NULL)
2529 				return (!pci_has_quirk(pci_get_devid(dev),
2530 					PCI_QUIRK_ENABLE_MSI_VM));
2531 		}
2532 		return (1);
2533 	}
2534 
2535 	dev = pci_find_bsf(0, 0, 0);
2536 	if (dev != NULL)
2537 		return (pci_msi_device_blacklisted(dev));
2538 	return (0);
2539 }
2540 
2541 /*
2542  * Returns true if the specified device is blacklisted because MSI-X
2543  * doesn't work.  Note that this assumes that if MSI doesn't work,
2544  * MSI-X doesn't either.
2545  */
2546 int
pci_msix_device_blacklisted(device_t dev)2547 pci_msix_device_blacklisted(device_t dev)
2548 {
2549 
2550 	if (!pci_honor_msi_blacklist)
2551 		return (0);
2552 
2553 	if (pci_has_quirk(pci_get_devid(dev), PCI_QUIRK_DISABLE_MSIX))
2554 		return (1);
2555 
2556 	return (pci_msi_device_blacklisted(dev));
2557 }
2558 
2559 /*
2560  * Determine if MSI-X is blacklisted globally on this system.  If MSI
2561  * is blacklisted, assume that MSI-X is as well.  Check for additional
2562  * chipsets where MSI works but MSI-X does not.
2563  */
2564 static int
pci_msix_blacklisted(void)2565 pci_msix_blacklisted(void)
2566 {
2567 	device_t dev;
2568 
2569 	if (!pci_honor_msi_blacklist)
2570 		return (0);
2571 
2572 	dev = pci_find_bsf(0, 0, 0);
2573 	if (dev != NULL && pci_has_quirk(pci_get_devid(dev),
2574 	    PCI_QUIRK_DISABLE_MSIX))
2575 		return (1);
2576 
2577 	return (pci_msi_blacklisted());
2578 }
2579 
2580 /*
2581  * Attempt to allocate *count MSI messages.  The actual number allocated is
2582  * returned in *count.  After this function returns, each message will be
2583  * available to the driver as SYS_RES_IRQ resources starting at a rid 1.
2584  */
2585 int
pci_alloc_msi_method(device_t dev,device_t child,int * count)2586 pci_alloc_msi_method(device_t dev, device_t child, int *count)
2587 {
2588 	struct pci_devinfo *dinfo = device_get_ivars(child);
2589 	pcicfgregs *cfg = &dinfo->cfg;
2590 	struct resource_list_entry *rle;
2591 	int actual, error, i, irqs[32];
2592 	uint16_t ctrl;
2593 
2594 	/* Don't let count == 0 get us into trouble. */
2595 	if (*count == 0)
2596 		return (EINVAL);
2597 
2598 	/* If rid 0 is allocated, then fail. */
2599 	rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, 0);
2600 	if (rle != NULL && rle->res != NULL)
2601 		return (ENXIO);
2602 
2603 	/* Already have allocated messages? */
2604 	if (cfg->msi.msi_alloc != 0 || cfg->msix.msix_alloc != 0)
2605 		return (ENXIO);
2606 
2607 	/* If MSI is blacklisted for this system, fail. */
2608 	if (pci_msi_blacklisted())
2609 		return (ENXIO);
2610 
2611 	/* MSI capability present? */
2612 	if (cfg->msi.msi_location == 0 || !pci_do_msi)
2613 		return (ENODEV);
2614 
2615 	if (bootverbose)
2616 		device_printf(child,
2617 		    "attempting to allocate %d MSI vectors (%d supported)\n",
2618 		    *count, cfg->msi.msi_msgnum);
2619 
2620 	/* Don't ask for more than the device supports. */
2621 	actual = min(*count, cfg->msi.msi_msgnum);
2622 
2623 	/* Don't ask for more than 32 messages. */
2624 	actual = min(actual, 32);
2625 
2626 	/* MSI requires power of 2 number of messages. */
2627 	if (!powerof2(actual))
2628 		return (EINVAL);
2629 
2630 	for (;;) {
2631 		/* Try to allocate N messages. */
2632 		error = PCIB_ALLOC_MSI(device_get_parent(dev), child, actual,
2633 		    actual, irqs);
2634 		if (error == 0)
2635 			break;
2636 		if (actual == 1)
2637 			return (error);
2638 
2639 		/* Try N / 2. */
2640 		actual >>= 1;
2641 	}
2642 
2643 	/*
2644 	 * We now have N actual messages mapped onto SYS_RES_IRQ
2645 	 * resources in the irqs[] array, so add new resources
2646 	 * starting at rid 1.
2647 	 */
2648 	for (i = 0; i < actual; i++)
2649 		resource_list_add(&dinfo->resources, SYS_RES_IRQ, i + 1,
2650 		    irqs[i], irqs[i], 1);
2651 
2652 	if (bootverbose) {
2653 		if (actual == 1)
2654 			device_printf(child, "using IRQ %d for MSI\n", irqs[0]);
2655 		else {
2656 			int run;
2657 
2658 			/*
2659 			 * Be fancy and try to print contiguous runs
2660 			 * of IRQ values as ranges.  'run' is true if
2661 			 * we are in a range.
2662 			 */
2663 			device_printf(child, "using IRQs %d", irqs[0]);
2664 			run = 0;
2665 			for (i = 1; i < actual; i++) {
2666 				/* Still in a run? */
2667 				if (irqs[i] == irqs[i - 1] + 1) {
2668 					run = 1;
2669 					continue;
2670 				}
2671 
2672 				/* Finish previous range. */
2673 				if (run) {
2674 					printf("-%d", irqs[i - 1]);
2675 					run = 0;
2676 				}
2677 
2678 				/* Start new range. */
2679 				printf(",%d", irqs[i]);
2680 			}
2681 
2682 			/* Unfinished range? */
2683 			if (run)
2684 				printf("-%d", irqs[actual - 1]);
2685 			printf(" for MSI\n");
2686 		}
2687 	}
2688 
2689 	/* Update control register with actual count. */
2690 	ctrl = cfg->msi.msi_ctrl;
2691 	ctrl &= ~PCIM_MSICTRL_MME_MASK;
2692 	ctrl |= (ffs(actual) - 1) << 4;
2693 	cfg->msi.msi_ctrl = ctrl;
2694 	pci_write_config(child, cfg->msi.msi_location + PCIR_MSI_CTRL, ctrl, 2);
2695 
2696 	/* Update counts of alloc'd messages. */
2697 	cfg->msi.msi_alloc = actual;
2698 	cfg->msi.msi_handlers = 0;
2699 	*count = actual;
2700 	return (0);
2701 }
2702 
2703 /* Release the MSI messages associated with this device. */
2704 int
pci_release_msi_method(device_t dev,device_t child)2705 pci_release_msi_method(device_t dev, device_t child)
2706 {
2707 	struct pci_devinfo *dinfo = device_get_ivars(child);
2708 	struct pcicfg_msi *msi = &dinfo->cfg.msi;
2709 	struct resource_list_entry *rle;
2710 	int error, i, irqs[32];
2711 
2712 	/* Try MSI-X first. */
2713 	error = pci_release_msix(dev, child);
2714 	if (error != ENODEV)
2715 		return (error);
2716 
2717 	/* Do we have any messages to release? */
2718 	if (msi->msi_alloc == 0)
2719 		return (ENODEV);
2720 	KASSERT(msi->msi_alloc <= 32, ("more than 32 alloc'd messages"));
2721 
2722 	/* Make sure none of the resources are allocated. */
2723 	if (msi->msi_handlers > 0)
2724 		return (EBUSY);
2725 	for (i = 0; i < msi->msi_alloc; i++) {
2726 		rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, i + 1);
2727 		KASSERT(rle != NULL, ("missing MSI resource"));
2728 		if (rle->res != NULL)
2729 			return (EBUSY);
2730 		irqs[i] = rle->start;
2731 	}
2732 
2733 	/* Update control register with 0 count. */
2734 	KASSERT(!(msi->msi_ctrl & PCIM_MSICTRL_MSI_ENABLE),
2735 	    ("%s: MSI still enabled", __func__));
2736 	msi->msi_ctrl &= ~PCIM_MSICTRL_MME_MASK;
2737 	pci_write_config(child, msi->msi_location + PCIR_MSI_CTRL,
2738 	    msi->msi_ctrl, 2);
2739 
2740 	/* Release the messages. */
2741 	PCIB_RELEASE_MSI(device_get_parent(dev), child, msi->msi_alloc, irqs);
2742 	for (i = 0; i < msi->msi_alloc; i++)
2743 		resource_list_delete(&dinfo->resources, SYS_RES_IRQ, i + 1);
2744 
2745 	/* Update alloc count. */
2746 	msi->msi_alloc = 0;
2747 	msi->msi_addr = 0;
2748 	msi->msi_data = 0;
2749 	return (0);
2750 }
2751 
2752 /*
2753  * Return the max supported MSI messages this device supports.
2754  * Basically, assuming the MD code can alloc messages, this function
2755  * should return the maximum value that pci_alloc_msi() can return.
2756  * Thus, it is subject to the tunables, etc.
2757  */
2758 int
pci_msi_count_method(device_t dev,device_t child)2759 pci_msi_count_method(device_t dev, device_t child)
2760 {
2761 	struct pci_devinfo *dinfo = device_get_ivars(child);
2762 	struct pcicfg_msi *msi = &dinfo->cfg.msi;
2763 
2764 	if (pci_do_msi && msi->msi_location != 0)
2765 		return (msi->msi_msgnum);
2766 	return (0);
2767 }
2768 
2769 /* free pcicfgregs structure and all depending data structures */
2770 
2771 int
pci_freecfg(struct pci_devinfo * dinfo)2772 pci_freecfg(struct pci_devinfo *dinfo)
2773 {
2774 	struct devlist *devlist_head;
2775 	struct pci_map *pm, *next;
2776 	int i;
2777 
2778 	devlist_head = &pci_devq;
2779 
2780 	if (dinfo->cfg.vpd.vpd_reg) {
2781 		free(dinfo->cfg.vpd.vpd_ident, M_DEVBUF);
2782 		for (i = 0; i < dinfo->cfg.vpd.vpd_rocnt; i++)
2783 			free(dinfo->cfg.vpd.vpd_ros[i].value, M_DEVBUF);
2784 		free(dinfo->cfg.vpd.vpd_ros, M_DEVBUF);
2785 		for (i = 0; i < dinfo->cfg.vpd.vpd_wcnt; i++)
2786 			free(dinfo->cfg.vpd.vpd_w[i].value, M_DEVBUF);
2787 		free(dinfo->cfg.vpd.vpd_w, M_DEVBUF);
2788 	}
2789 	STAILQ_FOREACH_SAFE(pm, &dinfo->cfg.maps, pm_link, next) {
2790 		free(pm, M_DEVBUF);
2791 	}
2792 	STAILQ_REMOVE(devlist_head, dinfo, pci_devinfo, pci_links);
2793 	free(dinfo, M_DEVBUF);
2794 
2795 	/* increment the generation count */
2796 	pci_generation++;
2797 
2798 	/* we're losing one device */
2799 	pci_numdevs--;
2800 	return (0);
2801 }
2802 
2803 /*
2804  * PCI power manangement
2805  */
2806 int
pci_set_powerstate_method(device_t dev,device_t child,int state)2807 pci_set_powerstate_method(device_t dev, device_t child, int state)
2808 {
2809 	struct pci_devinfo *dinfo = device_get_ivars(child);
2810 	pcicfgregs *cfg = &dinfo->cfg;
2811 	uint16_t status;
2812 	int oldstate, highest, delay;
2813 
2814 	if (cfg->pp.pp_cap == 0)
2815 		return (EOPNOTSUPP);
2816 
2817 	/*
2818 	 * Optimize a no state change request away.  While it would be OK to
2819 	 * write to the hardware in theory, some devices have shown odd
2820 	 * behavior when going from D3 -> D3.
2821 	 */
2822 	oldstate = pci_get_powerstate(child);
2823 	if (oldstate == state)
2824 		return (0);
2825 
2826 	/*
2827 	 * The PCI power management specification states that after a state
2828 	 * transition between PCI power states, system software must
2829 	 * guarantee a minimal delay before the function accesses the device.
2830 	 * Compute the worst case delay that we need to guarantee before we
2831 	 * access the device.  Many devices will be responsive much more
2832 	 * quickly than this delay, but there are some that don't respond
2833 	 * instantly to state changes.  Transitions to/from D3 state require
2834 	 * 10ms, while D2 requires 200us, and D0/1 require none.  The delay
2835 	 * is done below with DELAY rather than a sleeper function because
2836 	 * this function can be called from contexts where we cannot sleep.
2837 	 */
2838 	highest = (oldstate > state) ? oldstate : state;
2839 	if (highest == PCI_POWERSTATE_D3)
2840 	    delay = 10000;
2841 	else if (highest == PCI_POWERSTATE_D2)
2842 	    delay = 200;
2843 	else
2844 	    delay = 0;
2845 	status = PCI_READ_CONFIG(dev, child, cfg->pp.pp_status, 2)
2846 	    & ~PCIM_PSTAT_DMASK;
2847 	switch (state) {
2848 	case PCI_POWERSTATE_D0:
2849 		status |= PCIM_PSTAT_D0;
2850 		break;
2851 	case PCI_POWERSTATE_D1:
2852 		if ((cfg->pp.pp_cap & PCIM_PCAP_D1SUPP) == 0)
2853 			return (EOPNOTSUPP);
2854 		status |= PCIM_PSTAT_D1;
2855 		break;
2856 	case PCI_POWERSTATE_D2:
2857 		if ((cfg->pp.pp_cap & PCIM_PCAP_D2SUPP) == 0)
2858 			return (EOPNOTSUPP);
2859 		status |= PCIM_PSTAT_D2;
2860 		break;
2861 	case PCI_POWERSTATE_D3:
2862 		status |= PCIM_PSTAT_D3;
2863 		break;
2864 	default:
2865 		return (EINVAL);
2866 	}
2867 
2868 	if (bootverbose)
2869 		pci_printf(cfg, "Transition from D%d to D%d\n", oldstate,
2870 		    state);
2871 
2872 	PCI_WRITE_CONFIG(dev, child, cfg->pp.pp_status, status, 2);
2873 	if (delay)
2874 		DELAY(delay);
2875 	return (0);
2876 }
2877 
2878 int
pci_get_powerstate_method(device_t dev,device_t child)2879 pci_get_powerstate_method(device_t dev, device_t child)
2880 {
2881 	struct pci_devinfo *dinfo = device_get_ivars(child);
2882 	pcicfgregs *cfg = &dinfo->cfg;
2883 	uint16_t status;
2884 	int result;
2885 
2886 	if (cfg->pp.pp_cap != 0) {
2887 		status = PCI_READ_CONFIG(dev, child, cfg->pp.pp_status, 2);
2888 		switch (status & PCIM_PSTAT_DMASK) {
2889 		case PCIM_PSTAT_D0:
2890 			result = PCI_POWERSTATE_D0;
2891 			break;
2892 		case PCIM_PSTAT_D1:
2893 			result = PCI_POWERSTATE_D1;
2894 			break;
2895 		case PCIM_PSTAT_D2:
2896 			result = PCI_POWERSTATE_D2;
2897 			break;
2898 		case PCIM_PSTAT_D3:
2899 			result = PCI_POWERSTATE_D3;
2900 			break;
2901 		default:
2902 			result = PCI_POWERSTATE_UNKNOWN;
2903 			break;
2904 		}
2905 	} else {
2906 		/* No support, device is always at D0 */
2907 		result = PCI_POWERSTATE_D0;
2908 	}
2909 	return (result);
2910 }
2911 
2912 /*
2913  * Some convenience functions for PCI device drivers.
2914  */
2915 
2916 static __inline void
pci_set_command_bit(device_t dev,device_t child,uint16_t bit)2917 pci_set_command_bit(device_t dev, device_t child, uint16_t bit)
2918 {
2919 	uint16_t	command;
2920 
2921 	command = PCI_READ_CONFIG(dev, child, PCIR_COMMAND, 2);
2922 	command |= bit;
2923 	PCI_WRITE_CONFIG(dev, child, PCIR_COMMAND, command, 2);
2924 }
2925 
2926 static __inline void
pci_clear_command_bit(device_t dev,device_t child,uint16_t bit)2927 pci_clear_command_bit(device_t dev, device_t child, uint16_t bit)
2928 {
2929 	uint16_t	command;
2930 
2931 	command = PCI_READ_CONFIG(dev, child, PCIR_COMMAND, 2);
2932 	command &= ~bit;
2933 	PCI_WRITE_CONFIG(dev, child, PCIR_COMMAND, command, 2);
2934 }
2935 
2936 int
pci_enable_busmaster_method(device_t dev,device_t child)2937 pci_enable_busmaster_method(device_t dev, device_t child)
2938 {
2939 	pci_set_command_bit(dev, child, PCIM_CMD_BUSMASTEREN);
2940 	return (0);
2941 }
2942 
2943 int
pci_disable_busmaster_method(device_t dev,device_t child)2944 pci_disable_busmaster_method(device_t dev, device_t child)
2945 {
2946 	pci_clear_command_bit(dev, child, PCIM_CMD_BUSMASTEREN);
2947 	return (0);
2948 }
2949 
2950 int
pci_enable_io_method(device_t dev,device_t child,int space)2951 pci_enable_io_method(device_t dev, device_t child, int space)
2952 {
2953 	uint16_t bit;
2954 
2955 	switch(space) {
2956 	case SYS_RES_IOPORT:
2957 		bit = PCIM_CMD_PORTEN;
2958 		break;
2959 	case SYS_RES_MEMORY:
2960 		bit = PCIM_CMD_MEMEN;
2961 		break;
2962 	default:
2963 		return (EINVAL);
2964 	}
2965 	pci_set_command_bit(dev, child, bit);
2966 	return (0);
2967 }
2968 
2969 int
pci_disable_io_method(device_t dev,device_t child,int space)2970 pci_disable_io_method(device_t dev, device_t child, int space)
2971 {
2972 	uint16_t bit;
2973 
2974 	switch(space) {
2975 	case SYS_RES_IOPORT:
2976 		bit = PCIM_CMD_PORTEN;
2977 		break;
2978 	case SYS_RES_MEMORY:
2979 		bit = PCIM_CMD_MEMEN;
2980 		break;
2981 	default:
2982 		return (EINVAL);
2983 	}
2984 	pci_clear_command_bit(dev, child, bit);
2985 	return (0);
2986 }
2987 
2988 /*
2989  * New style pci driver.  Parent device is either a pci-host-bridge or a
2990  * pci-pci-bridge.  Both kinds are represented by instances of pcib.
2991  */
2992 
2993 void
pci_print_verbose(struct pci_devinfo * dinfo)2994 pci_print_verbose(struct pci_devinfo *dinfo)
2995 {
2996 
2997 	if (bootverbose) {
2998 		pcicfgregs *cfg = &dinfo->cfg;
2999 
3000 		printf("found->\tvendor=0x%04x, dev=0x%04x, revid=0x%02x\n",
3001 		    cfg->vendor, cfg->device, cfg->revid);
3002 		printf("\tdomain=%d, bus=%d, slot=%d, func=%d\n",
3003 		    cfg->domain, cfg->bus, cfg->slot, cfg->func);
3004 		printf("\tclass=%02x-%02x-%02x, hdrtype=0x%02x, mfdev=%d\n",
3005 		    cfg->baseclass, cfg->subclass, cfg->progif, cfg->hdrtype,
3006 		    cfg->mfdev);
3007 		printf("\tcmdreg=0x%04x, statreg=0x%04x, cachelnsz=%d (dwords)\n",
3008 		    cfg->cmdreg, cfg->statreg, cfg->cachelnsz);
3009 		printf("\tlattimer=0x%02x (%d ns), mingnt=0x%02x (%d ns), maxlat=0x%02x (%d ns)\n",
3010 		    cfg->lattimer, cfg->lattimer * 30, cfg->mingnt,
3011 		    cfg->mingnt * 250, cfg->maxlat, cfg->maxlat * 250);
3012 		if (cfg->intpin > 0)
3013 			printf("\tintpin=%c, irq=%d\n",
3014 			    cfg->intpin +'a' -1, cfg->intline);
3015 		if (cfg->pp.pp_cap) {
3016 			uint16_t status;
3017 
3018 			status = pci_read_config(cfg->dev, cfg->pp.pp_status, 2);
3019 			printf("\tpowerspec %d  supports D0%s%s D3  current D%d\n",
3020 			    cfg->pp.pp_cap & PCIM_PCAP_SPEC,
3021 			    cfg->pp.pp_cap & PCIM_PCAP_D1SUPP ? " D1" : "",
3022 			    cfg->pp.pp_cap & PCIM_PCAP_D2SUPP ? " D2" : "",
3023 			    status & PCIM_PSTAT_DMASK);
3024 		}
3025 		if (cfg->msi.msi_location) {
3026 			int ctrl;
3027 
3028 			ctrl = cfg->msi.msi_ctrl;
3029 			printf("\tMSI supports %d message%s%s%s\n",
3030 			    cfg->msi.msi_msgnum,
3031 			    (cfg->msi.msi_msgnum == 1) ? "" : "s",
3032 			    (ctrl & PCIM_MSICTRL_64BIT) ? ", 64 bit" : "",
3033 			    (ctrl & PCIM_MSICTRL_VECTOR) ? ", vector masks":"");
3034 		}
3035 		if (cfg->msix.msix_location) {
3036 			printf("\tMSI-X supports %d message%s ",
3037 			    cfg->msix.msix_msgnum,
3038 			    (cfg->msix.msix_msgnum == 1) ? "" : "s");
3039 			if (cfg->msix.msix_table_bar == cfg->msix.msix_pba_bar)
3040 				printf("in map 0x%x\n",
3041 				    cfg->msix.msix_table_bar);
3042 			else
3043 				printf("in maps 0x%x and 0x%x\n",
3044 				    cfg->msix.msix_table_bar,
3045 				    cfg->msix.msix_pba_bar);
3046 		}
3047 	}
3048 }
3049 
3050 static int
pci_porten(device_t dev)3051 pci_porten(device_t dev)
3052 {
3053 	return (pci_read_config(dev, PCIR_COMMAND, 2) & PCIM_CMD_PORTEN) != 0;
3054 }
3055 
3056 static int
pci_memen(device_t dev)3057 pci_memen(device_t dev)
3058 {
3059 	return (pci_read_config(dev, PCIR_COMMAND, 2) & PCIM_CMD_MEMEN) != 0;
3060 }
3061 
3062 void
pci_read_bar(device_t dev,int reg,pci_addr_t * mapp,pci_addr_t * testvalp,int * bar64)3063 pci_read_bar(device_t dev, int reg, pci_addr_t *mapp, pci_addr_t *testvalp,
3064     int *bar64)
3065 {
3066 	struct pci_devinfo *dinfo;
3067 	pci_addr_t map, testval;
3068 	int ln2range;
3069 	uint16_t cmd;
3070 
3071 	/*
3072 	 * The device ROM BAR is special.  It is always a 32-bit
3073 	 * memory BAR.  Bit 0 is special and should not be set when
3074 	 * sizing the BAR.
3075 	 */
3076 	dinfo = device_get_ivars(dev);
3077 	if (PCIR_IS_BIOS(&dinfo->cfg, reg)) {
3078 		map = pci_read_config(dev, reg, 4);
3079 		pci_write_config(dev, reg, 0xfffffffe, 4);
3080 		testval = pci_read_config(dev, reg, 4);
3081 		pci_write_config(dev, reg, map, 4);
3082 		*mapp = map;
3083 		*testvalp = testval;
3084 		if (bar64 != NULL)
3085 			*bar64 = 0;
3086 		return;
3087 	}
3088 
3089 	map = pci_read_config(dev, reg, 4);
3090 	ln2range = pci_maprange(map);
3091 	if (ln2range == 64)
3092 		map |= (pci_addr_t)pci_read_config(dev, reg + 4, 4) << 32;
3093 
3094 	/*
3095 	 * Disable decoding via the command register before
3096 	 * determining the BAR's length since we will be placing it in
3097 	 * a weird state.
3098 	 */
3099 	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
3100 	pci_write_config(dev, PCIR_COMMAND,
3101 	    cmd & ~(PCI_BAR_MEM(map) ? PCIM_CMD_MEMEN : PCIM_CMD_PORTEN), 2);
3102 
3103 	/*
3104 	 * Determine the BAR's length by writing all 1's.  The bottom
3105 	 * log_2(size) bits of the BAR will stick as 0 when we read
3106 	 * the value back.
3107 	 *
3108 	 * NB: according to the PCI Local Bus Specification, rev. 3.0:
3109 	 * "Software writes 0FFFFFFFFh to both registers, reads them back,
3110 	 * and combines the result into a 64-bit value." (section 6.2.5.1)
3111 	 *
3112 	 * Writes to both registers must be performed before attempting to
3113 	 * read back the size value.
3114 	 */
3115 	testval = 0;
3116 	pci_write_config(dev, reg, 0xffffffff, 4);
3117 	if (ln2range == 64) {
3118 		pci_write_config(dev, reg + 4, 0xffffffff, 4);
3119 		testval |= (pci_addr_t)pci_read_config(dev, reg + 4, 4) << 32;
3120 	}
3121 	testval |= pci_read_config(dev, reg, 4);
3122 
3123 	/*
3124 	 * Restore the original value of the BAR.  We may have reprogrammed
3125 	 * the BAR of the low-level console device and when booting verbose,
3126 	 * we need the console device addressable.
3127 	 */
3128 	pci_write_config(dev, reg, map, 4);
3129 	if (ln2range == 64)
3130 		pci_write_config(dev, reg + 4, map >> 32, 4);
3131 	pci_write_config(dev, PCIR_COMMAND, cmd, 2);
3132 
3133 	*mapp = map;
3134 	*testvalp = testval;
3135 	if (bar64 != NULL)
3136 		*bar64 = (ln2range == 64);
3137 }
3138 
3139 static void
pci_write_bar(device_t dev,struct pci_map * pm,pci_addr_t base)3140 pci_write_bar(device_t dev, struct pci_map *pm, pci_addr_t base)
3141 {
3142 	struct pci_devinfo *dinfo;
3143 	int ln2range;
3144 
3145 	/* The device ROM BAR is always a 32-bit memory BAR. */
3146 	dinfo = device_get_ivars(dev);
3147 	if (PCIR_IS_BIOS(&dinfo->cfg, pm->pm_reg))
3148 		ln2range = 32;
3149 	else
3150 		ln2range = pci_maprange(pm->pm_value);
3151 	pci_write_config(dev, pm->pm_reg, base, 4);
3152 	if (ln2range == 64)
3153 		pci_write_config(dev, pm->pm_reg + 4, base >> 32, 4);
3154 	pm->pm_value = pci_read_config(dev, pm->pm_reg, 4);
3155 	if (ln2range == 64)
3156 		pm->pm_value |= (pci_addr_t)pci_read_config(dev,
3157 		    pm->pm_reg + 4, 4) << 32;
3158 }
3159 
3160 struct pci_map *
pci_find_bar(device_t dev,int reg)3161 pci_find_bar(device_t dev, int reg)
3162 {
3163 	struct pci_devinfo *dinfo;
3164 	struct pci_map *pm;
3165 
3166 	dinfo = device_get_ivars(dev);
3167 	STAILQ_FOREACH(pm, &dinfo->cfg.maps, pm_link) {
3168 		if (pm->pm_reg == reg)
3169 			return (pm);
3170 	}
3171 	return (NULL);
3172 }
3173 
3174 int
pci_bar_enabled(device_t dev,struct pci_map * pm)3175 pci_bar_enabled(device_t dev, struct pci_map *pm)
3176 {
3177 	struct pci_devinfo *dinfo;
3178 	uint16_t cmd;
3179 
3180 	dinfo = device_get_ivars(dev);
3181 	if (PCIR_IS_BIOS(&dinfo->cfg, pm->pm_reg) &&
3182 	    !(pm->pm_value & PCIM_BIOS_ENABLE))
3183 		return (0);
3184 #ifdef PCI_IOV
3185 	if ((dinfo->cfg.flags & PCICFG_VF) != 0) {
3186 		struct pcicfg_iov *iov;
3187 
3188 		iov = dinfo->cfg.iov;
3189 		cmd = pci_read_config(iov->iov_pf,
3190 		    iov->iov_pos + PCIR_SRIOV_CTL, 2);
3191 		return ((cmd & PCIM_SRIOV_VF_MSE) != 0);
3192 	}
3193 #endif
3194 	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
3195 	if (PCIR_IS_BIOS(&dinfo->cfg, pm->pm_reg) || PCI_BAR_MEM(pm->pm_value))
3196 		return ((cmd & PCIM_CMD_MEMEN) != 0);
3197 	else
3198 		return ((cmd & PCIM_CMD_PORTEN) != 0);
3199 }
3200 
3201 struct pci_map *
pci_add_bar(device_t dev,int reg,pci_addr_t value,pci_addr_t size)3202 pci_add_bar(device_t dev, int reg, pci_addr_t value, pci_addr_t size)
3203 {
3204 	struct pci_devinfo *dinfo;
3205 	struct pci_map *pm, *prev;
3206 
3207 	dinfo = device_get_ivars(dev);
3208 	pm = malloc(sizeof(*pm), M_DEVBUF, M_WAITOK | M_ZERO);
3209 	pm->pm_reg = reg;
3210 	pm->pm_value = value;
3211 	pm->pm_size = size;
3212 	STAILQ_FOREACH(prev, &dinfo->cfg.maps, pm_link) {
3213 		KASSERT(prev->pm_reg != pm->pm_reg, ("duplicate map %02x",
3214 		    reg));
3215 		if (STAILQ_NEXT(prev, pm_link) == NULL ||
3216 		    STAILQ_NEXT(prev, pm_link)->pm_reg > pm->pm_reg)
3217 			break;
3218 	}
3219 	if (prev != NULL)
3220 		STAILQ_INSERT_AFTER(&dinfo->cfg.maps, prev, pm, pm_link);
3221 	else
3222 		STAILQ_INSERT_TAIL(&dinfo->cfg.maps, pm, pm_link);
3223 	return (pm);
3224 }
3225 
3226 static void
pci_restore_bars(device_t dev)3227 pci_restore_bars(device_t dev)
3228 {
3229 	struct pci_devinfo *dinfo;
3230 	struct pci_map *pm;
3231 	int ln2range;
3232 
3233 	dinfo = device_get_ivars(dev);
3234 	STAILQ_FOREACH(pm, &dinfo->cfg.maps, pm_link) {
3235 		if (PCIR_IS_BIOS(&dinfo->cfg, pm->pm_reg))
3236 			ln2range = 32;
3237 		else
3238 			ln2range = pci_maprange(pm->pm_value);
3239 		pci_write_config(dev, pm->pm_reg, pm->pm_value, 4);
3240 		if (ln2range == 64)
3241 			pci_write_config(dev, pm->pm_reg + 4,
3242 			    pm->pm_value >> 32, 4);
3243 	}
3244 }
3245 
3246 /*
3247  * Add a resource based on a pci map register. Return 1 if the map
3248  * register is a 32bit map register or 2 if it is a 64bit register.
3249  */
3250 static int
pci_add_map(device_t bus,device_t dev,int reg,struct resource_list * rl,int force,int prefetch)3251 pci_add_map(device_t bus, device_t dev, int reg, struct resource_list *rl,
3252     int force, int prefetch)
3253 {
3254 	struct pci_map *pm;
3255 	pci_addr_t base, map, testval;
3256 	pci_addr_t start, end, count;
3257 	int barlen, basezero, flags, maprange, mapsize, type;
3258 	uint16_t cmd;
3259 	struct resource *res;
3260 
3261 	/*
3262 	 * The BAR may already exist if the device is a CardBus card
3263 	 * whose CIS is stored in this BAR.
3264 	 */
3265 	pm = pci_find_bar(dev, reg);
3266 	if (pm != NULL) {
3267 		maprange = pci_maprange(pm->pm_value);
3268 		barlen = maprange == 64 ? 2 : 1;
3269 		return (barlen);
3270 	}
3271 
3272 	pci_read_bar(dev, reg, &map, &testval, NULL);
3273 	if (PCI_BAR_MEM(map)) {
3274 		type = SYS_RES_MEMORY;
3275 		if (map & PCIM_BAR_MEM_PREFETCH)
3276 			prefetch = 1;
3277 	} else
3278 		type = SYS_RES_IOPORT;
3279 	mapsize = pci_mapsize(testval);
3280 	base = pci_mapbase(map);
3281 #ifdef __PCI_BAR_ZERO_VALID
3282 	basezero = 0;
3283 #else
3284 	basezero = base == 0;
3285 #endif
3286 	maprange = pci_maprange(map);
3287 	barlen = maprange == 64 ? 2 : 1;
3288 
3289 	/*
3290 	 * For I/O registers, if bottom bit is set, and the next bit up
3291 	 * isn't clear, we know we have a BAR that doesn't conform to the
3292 	 * spec, so ignore it.  Also, sanity check the size of the data
3293 	 * areas to the type of memory involved.  Memory must be at least
3294 	 * 16 bytes in size, while I/O ranges must be at least 4.
3295 	 */
3296 	if (PCI_BAR_IO(testval) && (testval & PCIM_BAR_IO_RESERVED) != 0)
3297 		return (barlen);
3298 	if ((type == SYS_RES_MEMORY && mapsize < 4) ||
3299 	    (type == SYS_RES_IOPORT && mapsize < 2))
3300 		return (barlen);
3301 
3302 	/* Save a record of this BAR. */
3303 	pm = pci_add_bar(dev, reg, map, mapsize);
3304 	if (bootverbose) {
3305 		printf("\tmap[%02x]: type %s, range %2d, base %#jx, size %2d",
3306 		    reg, pci_maptype(map), maprange, (uintmax_t)base, mapsize);
3307 		if (type == SYS_RES_IOPORT && !pci_porten(dev))
3308 			printf(", port disabled\n");
3309 		else if (type == SYS_RES_MEMORY && !pci_memen(dev))
3310 			printf(", memory disabled\n");
3311 		else
3312 			printf(", enabled\n");
3313 	}
3314 
3315 	/*
3316 	 * If base is 0, then we have problems if this architecture does
3317 	 * not allow that.  It is best to ignore such entries for the
3318 	 * moment.  These will be allocated later if the driver specifically
3319 	 * requests them.  However, some removable buses look better when
3320 	 * all resources are allocated, so allow '0' to be overriden.
3321 	 *
3322 	 * Similarly treat maps whose values is the same as the test value
3323 	 * read back.  These maps have had all f's written to them by the
3324 	 * BIOS in an attempt to disable the resources.
3325 	 */
3326 	if (!force && (basezero || map == testval))
3327 		return (barlen);
3328 	if ((u_long)base != base) {
3329 		device_printf(bus,
3330 		    "pci%d:%d:%d:%d bar %#x too many address bits",
3331 		    pci_get_domain(dev), pci_get_bus(dev), pci_get_slot(dev),
3332 		    pci_get_function(dev), reg);
3333 		return (barlen);
3334 	}
3335 
3336 	/*
3337 	 * This code theoretically does the right thing, but has
3338 	 * undesirable side effects in some cases where peripherals
3339 	 * respond oddly to having these bits enabled.  Let the user
3340 	 * be able to turn them off (since pci_enable_io_modes is 1 by
3341 	 * default).
3342 	 */
3343 	if (pci_enable_io_modes) {
3344 		/* Turn on resources that have been left off by a lazy BIOS */
3345 		if (type == SYS_RES_IOPORT && !pci_porten(dev)) {
3346 			cmd = pci_read_config(dev, PCIR_COMMAND, 2);
3347 			cmd |= PCIM_CMD_PORTEN;
3348 			pci_write_config(dev, PCIR_COMMAND, cmd, 2);
3349 		}
3350 		if (type == SYS_RES_MEMORY && !pci_memen(dev)) {
3351 			cmd = pci_read_config(dev, PCIR_COMMAND, 2);
3352 			cmd |= PCIM_CMD_MEMEN;
3353 			pci_write_config(dev, PCIR_COMMAND, cmd, 2);
3354 		}
3355 	} else {
3356 		if (type == SYS_RES_IOPORT && !pci_porten(dev))
3357 			return (barlen);
3358 		if (type == SYS_RES_MEMORY && !pci_memen(dev))
3359 			return (barlen);
3360 	}
3361 
3362 	count = (pci_addr_t)1 << mapsize;
3363 	flags = RF_ALIGNMENT_LOG2(mapsize);
3364 	if (prefetch)
3365 		flags |= RF_PREFETCHABLE;
3366 	if (basezero || base == pci_mapbase(testval) || pci_clear_bars) {
3367 		start = 0;	/* Let the parent decide. */
3368 		end = ~0;
3369 	} else {
3370 		start = base;
3371 		end = base + count - 1;
3372 	}
3373 	resource_list_add(rl, type, reg, start, end, count);
3374 
3375 	/*
3376 	 * Try to allocate the resource for this BAR from our parent
3377 	 * so that this resource range is already reserved.  The
3378 	 * driver for this device will later inherit this resource in
3379 	 * pci_alloc_resource().
3380 	 */
3381 	res = resource_list_reserve(rl, bus, dev, type, &reg, start, end, count,
3382 	    flags);
3383 	if ((pci_do_realloc_bars
3384 		|| pci_has_quirk(pci_get_devid(dev), PCI_QUIRK_REALLOC_BAR))
3385 	    && res == NULL && (start != 0 || end != ~0)) {
3386 		/*
3387 		 * If the allocation fails, try to allocate a resource for
3388 		 * this BAR using any available range.  The firmware felt
3389 		 * it was important enough to assign a resource, so don't
3390 		 * disable decoding if we can help it.
3391 		 */
3392 		resource_list_delete(rl, type, reg);
3393 		resource_list_add(rl, type, reg, 0, ~0, count);
3394 		res = resource_list_reserve(rl, bus, dev, type, &reg, 0, ~0,
3395 		    count, flags);
3396 	}
3397 	if (res == NULL) {
3398 		/*
3399 		 * If the allocation fails, delete the resource list entry
3400 		 * and disable decoding for this device.
3401 		 *
3402 		 * If the driver requests this resource in the future,
3403 		 * pci_reserve_map() will try to allocate a fresh
3404 		 * resource range.
3405 		 */
3406 		resource_list_delete(rl, type, reg);
3407 		pci_disable_io(dev, type);
3408 		if (bootverbose)
3409 			device_printf(bus,
3410 			    "pci%d:%d:%d:%d bar %#x failed to allocate\n",
3411 			    pci_get_domain(dev), pci_get_bus(dev),
3412 			    pci_get_slot(dev), pci_get_function(dev), reg);
3413 	} else {
3414 		start = rman_get_start(res);
3415 		pci_write_bar(dev, pm, start);
3416 	}
3417 	return (barlen);
3418 }
3419 
3420 /*
3421  * For ATA devices we need to decide early what addressing mode to use.
3422  * Legacy demands that the primary and secondary ATA ports sits on the
3423  * same addresses that old ISA hardware did. This dictates that we use
3424  * those addresses and ignore the BAR's if we cannot set PCI native
3425  * addressing mode.
3426  */
3427 static void
pci_ata_maps(device_t bus,device_t dev,struct resource_list * rl,int force,uint32_t prefetchmask)3428 pci_ata_maps(device_t bus, device_t dev, struct resource_list *rl, int force,
3429     uint32_t prefetchmask)
3430 {
3431 	int rid, type, progif;
3432 #if 0
3433 	/* if this device supports PCI native addressing use it */
3434 	progif = pci_read_config(dev, PCIR_PROGIF, 1);
3435 	if ((progif & 0x8a) == 0x8a) {
3436 		if (pci_mapbase(pci_read_config(dev, PCIR_BAR(0), 4)) &&
3437 		    pci_mapbase(pci_read_config(dev, PCIR_BAR(2), 4))) {
3438 			printf("Trying ATA native PCI addressing mode\n");
3439 			pci_write_config(dev, PCIR_PROGIF, progif | 0x05, 1);
3440 		}
3441 	}
3442 #endif
3443 	progif = pci_read_config(dev, PCIR_PROGIF, 1);
3444 	type = SYS_RES_IOPORT;
3445 	if (progif & PCIP_STORAGE_IDE_MODEPRIM) {
3446 		pci_add_map(bus, dev, PCIR_BAR(0), rl, force,
3447 		    prefetchmask & (1 << 0));
3448 		pci_add_map(bus, dev, PCIR_BAR(1), rl, force,
3449 		    prefetchmask & (1 << 1));
3450 	} else {
3451 		rid = PCIR_BAR(0);
3452 		resource_list_add(rl, type, rid, 0x1f0, 0x1f7, 8);
3453 		(void)resource_list_reserve(rl, bus, dev, type, &rid, 0x1f0,
3454 		    0x1f7, 8, 0);
3455 		rid = PCIR_BAR(1);
3456 		resource_list_add(rl, type, rid, 0x3f6, 0x3f6, 1);
3457 		(void)resource_list_reserve(rl, bus, dev, type, &rid, 0x3f6,
3458 		    0x3f6, 1, 0);
3459 	}
3460 	if (progif & PCIP_STORAGE_IDE_MODESEC) {
3461 		pci_add_map(bus, dev, PCIR_BAR(2), rl, force,
3462 		    prefetchmask & (1 << 2));
3463 		pci_add_map(bus, dev, PCIR_BAR(3), rl, force,
3464 		    prefetchmask & (1 << 3));
3465 	} else {
3466 		rid = PCIR_BAR(2);
3467 		resource_list_add(rl, type, rid, 0x170, 0x177, 8);
3468 		(void)resource_list_reserve(rl, bus, dev, type, &rid, 0x170,
3469 		    0x177, 8, 0);
3470 		rid = PCIR_BAR(3);
3471 		resource_list_add(rl, type, rid, 0x376, 0x376, 1);
3472 		(void)resource_list_reserve(rl, bus, dev, type, &rid, 0x376,
3473 		    0x376, 1, 0);
3474 	}
3475 	pci_add_map(bus, dev, PCIR_BAR(4), rl, force,
3476 	    prefetchmask & (1 << 4));
3477 	pci_add_map(bus, dev, PCIR_BAR(5), rl, force,
3478 	    prefetchmask & (1 << 5));
3479 }
3480 
3481 static void
pci_assign_interrupt(device_t bus,device_t dev,int force_route)3482 pci_assign_interrupt(device_t bus, device_t dev, int force_route)
3483 {
3484 	struct pci_devinfo *dinfo = device_get_ivars(dev);
3485 	pcicfgregs *cfg = &dinfo->cfg;
3486 	char tunable_name[64];
3487 	int irq;
3488 
3489 	/* Has to have an intpin to have an interrupt. */
3490 	if (cfg->intpin == 0)
3491 		return;
3492 
3493 	/* Let the user override the IRQ with a tunable. */
3494 	irq = PCI_INVALID_IRQ;
3495 	snprintf(tunable_name, sizeof(tunable_name),
3496 	    "hw.pci%d.%d.%d.INT%c.irq",
3497 	    cfg->domain, cfg->bus, cfg->slot, cfg->intpin + 'A' - 1);
3498 	if (TUNABLE_INT_FETCH(tunable_name, &irq) && (irq >= 255 || irq <= 0))
3499 		irq = PCI_INVALID_IRQ;
3500 
3501 	/*
3502 	 * If we didn't get an IRQ via the tunable, then we either use the
3503 	 * IRQ value in the intline register or we ask the bus to route an
3504 	 * interrupt for us.  If force_route is true, then we only use the
3505 	 * value in the intline register if the bus was unable to assign an
3506 	 * IRQ.
3507 	 */
3508 	if (!PCI_INTERRUPT_VALID(irq)) {
3509 		if (!PCI_INTERRUPT_VALID(cfg->intline) || force_route)
3510 			irq = PCI_ASSIGN_INTERRUPT(bus, dev);
3511 		if (!PCI_INTERRUPT_VALID(irq))
3512 			irq = cfg->intline;
3513 	}
3514 
3515 	/* If after all that we don't have an IRQ, just bail. */
3516 	if (!PCI_INTERRUPT_VALID(irq))
3517 		return;
3518 
3519 	/* Update the config register if it changed. */
3520 	if (irq != cfg->intline) {
3521 		cfg->intline = irq;
3522 		pci_write_config(dev, PCIR_INTLINE, irq, 1);
3523 	}
3524 
3525 	/* Add this IRQ as rid 0 interrupt resource. */
3526 	resource_list_add(&dinfo->resources, SYS_RES_IRQ, 0, irq, irq, 1);
3527 }
3528 
3529 /* Perform early OHCI takeover from SMM. */
3530 static void
ohci_early_takeover(device_t self)3531 ohci_early_takeover(device_t self)
3532 {
3533 	struct resource *res;
3534 	uint32_t ctl;
3535 	int rid;
3536 	int i;
3537 
3538 	rid = PCIR_BAR(0);
3539 	res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
3540 	if (res == NULL)
3541 		return;
3542 
3543 	ctl = bus_read_4(res, OHCI_CONTROL);
3544 	if (ctl & OHCI_IR) {
3545 		if (bootverbose)
3546 			printf("ohci early: "
3547 			    "SMM active, request owner change\n");
3548 		bus_write_4(res, OHCI_COMMAND_STATUS, OHCI_OCR);
3549 		for (i = 0; (i < 100) && (ctl & OHCI_IR); i++) {
3550 			DELAY(1000);
3551 			ctl = bus_read_4(res, OHCI_CONTROL);
3552 		}
3553 		if (ctl & OHCI_IR) {
3554 			if (bootverbose)
3555 				printf("ohci early: "
3556 				    "SMM does not respond, resetting\n");
3557 			bus_write_4(res, OHCI_CONTROL, OHCI_HCFS_RESET);
3558 		}
3559 		/* Disable interrupts */
3560 		bus_write_4(res, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
3561 	}
3562 
3563 	bus_release_resource(self, SYS_RES_MEMORY, rid, res);
3564 }
3565 
3566 /* Perform early UHCI takeover from SMM. */
3567 static void
uhci_early_takeover(device_t self)3568 uhci_early_takeover(device_t self)
3569 {
3570 	struct resource *res;
3571 	int rid;
3572 
3573 	/*
3574 	 * Set the PIRQD enable bit and switch off all the others. We don't
3575 	 * want legacy support to interfere with us XXX Does this also mean
3576 	 * that the BIOS won't touch the keyboard anymore if it is connected
3577 	 * to the ports of the root hub?
3578 	 */
3579 	pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2);
3580 
3581 	/* Disable interrupts */
3582 	rid = PCI_UHCI_BASE_REG;
3583 	res = bus_alloc_resource_any(self, SYS_RES_IOPORT, &rid, RF_ACTIVE);
3584 	if (res != NULL) {
3585 		bus_write_2(res, UHCI_INTR, 0);
3586 		bus_release_resource(self, SYS_RES_IOPORT, rid, res);
3587 	}
3588 }
3589 
3590 /* Perform early EHCI takeover from SMM. */
3591 static void
ehci_early_takeover(device_t self)3592 ehci_early_takeover(device_t self)
3593 {
3594 	struct resource *res;
3595 	uint32_t cparams;
3596 	uint32_t eec;
3597 	uint8_t eecp;
3598 	uint8_t bios_sem;
3599 	uint8_t offs;
3600 	int rid;
3601 	int i;
3602 
3603 	rid = PCIR_BAR(0);
3604 	res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
3605 	if (res == NULL)
3606 		return;
3607 
3608 	cparams = bus_read_4(res, EHCI_HCCPARAMS);
3609 
3610 	/* Synchronise with the BIOS if it owns the controller. */
3611 	for (eecp = EHCI_HCC_EECP(cparams); eecp != 0;
3612 	    eecp = EHCI_EECP_NEXT(eec)) {
3613 		eec = pci_read_config(self, eecp, 4);
3614 		if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) {
3615 			continue;
3616 		}
3617 		bios_sem = pci_read_config(self, eecp +
3618 		    EHCI_LEGSUP_BIOS_SEM, 1);
3619 		if (bios_sem == 0) {
3620 			continue;
3621 		}
3622 		if (bootverbose)
3623 			printf("ehci early: "
3624 			    "SMM active, request owner change\n");
3625 
3626 		pci_write_config(self, eecp + EHCI_LEGSUP_OS_SEM, 1, 1);
3627 
3628 		for (i = 0; (i < 100) && (bios_sem != 0); i++) {
3629 			DELAY(1000);
3630 			bios_sem = pci_read_config(self, eecp +
3631 			    EHCI_LEGSUP_BIOS_SEM, 1);
3632 		}
3633 
3634 		if (bios_sem != 0) {
3635 			if (bootverbose)
3636 				printf("ehci early: "
3637 				    "SMM does not respond\n");
3638 		}
3639 		/* Disable interrupts */
3640 		offs = EHCI_CAPLENGTH(bus_read_4(res, EHCI_CAPLEN_HCIVERSION));
3641 		bus_write_4(res, offs + EHCI_USBINTR, 0);
3642 	}
3643 	bus_release_resource(self, SYS_RES_MEMORY, rid, res);
3644 }
3645 
3646 /* Perform early XHCI takeover from SMM. */
3647 static void
xhci_early_takeover(device_t self)3648 xhci_early_takeover(device_t self)
3649 {
3650 	struct resource *res;
3651 	uint32_t cparams;
3652 	uint32_t eec;
3653 	uint8_t eecp;
3654 	uint8_t bios_sem;
3655 	uint8_t offs;
3656 	int rid;
3657 	int i;
3658 
3659 	rid = PCIR_BAR(0);
3660 	res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
3661 	if (res == NULL)
3662 		return;
3663 
3664 	cparams = bus_read_4(res, XHCI_HCSPARAMS0);
3665 
3666 	eec = -1;
3667 
3668 	/* Synchronise with the BIOS if it owns the controller. */
3669 	for (eecp = XHCI_HCS0_XECP(cparams) << 2; eecp != 0 && XHCI_XECP_NEXT(eec);
3670 	    eecp += XHCI_XECP_NEXT(eec) << 2) {
3671 		eec = bus_read_4(res, eecp);
3672 
3673 		if (XHCI_XECP_ID(eec) != XHCI_ID_USB_LEGACY)
3674 			continue;
3675 
3676 		bios_sem = bus_read_1(res, eecp + XHCI_XECP_BIOS_SEM);
3677 		if (bios_sem == 0)
3678 			continue;
3679 
3680 		if (bootverbose)
3681 			printf("xhci early: "
3682 			    "SMM active, request owner change\n");
3683 
3684 		bus_write_1(res, eecp + XHCI_XECP_OS_SEM, 1);
3685 
3686 		/* wait a maximum of 5 second */
3687 
3688 		for (i = 0; (i < 5000) && (bios_sem != 0); i++) {
3689 			DELAY(1000);
3690 			bios_sem = bus_read_1(res, eecp +
3691 			    XHCI_XECP_BIOS_SEM);
3692 		}
3693 
3694 		if (bios_sem != 0) {
3695 			if (bootverbose)
3696 				printf("xhci early: "
3697 				    "SMM does not respond\n");
3698 		}
3699 
3700 		/* Disable interrupts */
3701 		offs = bus_read_1(res, XHCI_CAPLENGTH);
3702 		bus_write_4(res, offs + XHCI_USBCMD, 0);
3703 		bus_read_4(res, offs + XHCI_USBSTS);
3704 	}
3705 	bus_release_resource(self, SYS_RES_MEMORY, rid, res);
3706 }
3707 
3708 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
3709 static void
pci_reserve_secbus(device_t bus,device_t dev,pcicfgregs * cfg,struct resource_list * rl)3710 pci_reserve_secbus(device_t bus, device_t dev, pcicfgregs *cfg,
3711     struct resource_list *rl)
3712 {
3713 	struct resource *res;
3714 	char *cp;
3715 	rman_res_t start, end, count;
3716 	int rid, sec_bus, sec_reg, sub_bus, sub_reg, sup_bus;
3717 
3718 	switch (cfg->hdrtype & PCIM_HDRTYPE) {
3719 	case PCIM_HDRTYPE_BRIDGE:
3720 		sec_reg = PCIR_SECBUS_1;
3721 		sub_reg = PCIR_SUBBUS_1;
3722 		break;
3723 	case PCIM_HDRTYPE_CARDBUS:
3724 		sec_reg = PCIR_SECBUS_2;
3725 		sub_reg = PCIR_SUBBUS_2;
3726 		break;
3727 	default:
3728 		return;
3729 	}
3730 
3731 	/*
3732 	 * If the existing bus range is valid, attempt to reserve it
3733 	 * from our parent.  If this fails for any reason, clear the
3734 	 * secbus and subbus registers.
3735 	 *
3736 	 * XXX: Should we reset sub_bus to sec_bus if it is < sec_bus?
3737 	 * This would at least preserve the existing sec_bus if it is
3738 	 * valid.
3739 	 */
3740 	sec_bus = PCI_READ_CONFIG(bus, dev, sec_reg, 1);
3741 	sub_bus = PCI_READ_CONFIG(bus, dev, sub_reg, 1);
3742 
3743 	/* Quirk handling. */
3744 	switch (pci_get_devid(dev)) {
3745 	case 0x12258086:		/* Intel 82454KX/GX (Orion) */
3746 		sup_bus = pci_read_config(dev, 0x41, 1);
3747 		if (sup_bus != 0xff) {
3748 			sec_bus = sup_bus + 1;
3749 			sub_bus = sup_bus + 1;
3750 			PCI_WRITE_CONFIG(bus, dev, sec_reg, sec_bus, 1);
3751 			PCI_WRITE_CONFIG(bus, dev, sub_reg, sub_bus, 1);
3752 		}
3753 		break;
3754 
3755 	case 0x00dd10de:
3756 		/* Compaq R3000 BIOS sets wrong subordinate bus number. */
3757 		if ((cp = kern_getenv("smbios.planar.maker")) == NULL)
3758 			break;
3759 		if (strncmp(cp, "Compal", 6) != 0) {
3760 			freeenv(cp);
3761 			break;
3762 		}
3763 		freeenv(cp);
3764 		if ((cp = kern_getenv("smbios.planar.product")) == NULL)
3765 			break;
3766 		if (strncmp(cp, "08A0", 4) != 0) {
3767 			freeenv(cp);
3768 			break;
3769 		}
3770 		freeenv(cp);
3771 		if (sub_bus < 0xa) {
3772 			sub_bus = 0xa;
3773 			PCI_WRITE_CONFIG(bus, dev, sub_reg, sub_bus, 1);
3774 		}
3775 		break;
3776 	}
3777 
3778 	if (bootverbose)
3779 		printf("\tsecbus=%d, subbus=%d\n", sec_bus, sub_bus);
3780 	if (sec_bus > 0 && sub_bus >= sec_bus) {
3781 		start = sec_bus;
3782 		end = sub_bus;
3783 		count = end - start + 1;
3784 
3785 		resource_list_add(rl, PCI_RES_BUS, 0, 0, ~0, count);
3786 
3787 		/*
3788 		 * If requested, clear secondary bus registers in
3789 		 * bridge devices to force a complete renumbering
3790 		 * rather than reserving the existing range.  However,
3791 		 * preserve the existing size.
3792 		 */
3793 		if (pci_clear_buses)
3794 			goto clear;
3795 
3796 		rid = 0;
3797 		res = resource_list_reserve(rl, bus, dev, PCI_RES_BUS, &rid,
3798 		    start, end, count, 0);
3799 		if (res != NULL)
3800 			return;
3801 
3802 		if (bootverbose)
3803 			device_printf(bus,
3804 			    "pci%d:%d:%d:%d secbus failed to allocate\n",
3805 			    pci_get_domain(dev), pci_get_bus(dev),
3806 			    pci_get_slot(dev), pci_get_function(dev));
3807 	}
3808 
3809 clear:
3810 	PCI_WRITE_CONFIG(bus, dev, sec_reg, 0, 1);
3811 	PCI_WRITE_CONFIG(bus, dev, sub_reg, 0, 1);
3812 }
3813 
3814 static struct resource *
pci_alloc_secbus(device_t dev,device_t child,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)3815 pci_alloc_secbus(device_t dev, device_t child, int *rid, rman_res_t start,
3816     rman_res_t end, rman_res_t count, u_int flags)
3817 {
3818 	struct pci_devinfo *dinfo;
3819 	pcicfgregs *cfg;
3820 	struct resource_list *rl;
3821 	struct resource *res;
3822 	int sec_reg, sub_reg;
3823 
3824 	dinfo = device_get_ivars(child);
3825 	cfg = &dinfo->cfg;
3826 	rl = &dinfo->resources;
3827 	switch (cfg->hdrtype & PCIM_HDRTYPE) {
3828 	case PCIM_HDRTYPE_BRIDGE:
3829 		sec_reg = PCIR_SECBUS_1;
3830 		sub_reg = PCIR_SUBBUS_1;
3831 		break;
3832 	case PCIM_HDRTYPE_CARDBUS:
3833 		sec_reg = PCIR_SECBUS_2;
3834 		sub_reg = PCIR_SUBBUS_2;
3835 		break;
3836 	default:
3837 		return (NULL);
3838 	}
3839 
3840 	if (*rid != 0)
3841 		return (NULL);
3842 
3843 	if (resource_list_find(rl, PCI_RES_BUS, *rid) == NULL)
3844 		resource_list_add(rl, PCI_RES_BUS, *rid, start, end, count);
3845 	if (!resource_list_reserved(rl, PCI_RES_BUS, *rid)) {
3846 		res = resource_list_reserve(rl, dev, child, PCI_RES_BUS, rid,
3847 		    start, end, count, flags & ~RF_ACTIVE);
3848 		if (res == NULL) {
3849 			resource_list_delete(rl, PCI_RES_BUS, *rid);
3850 			device_printf(child, "allocating %ju bus%s failed\n",
3851 			    count, count == 1 ? "" : "es");
3852 			return (NULL);
3853 		}
3854 		if (bootverbose)
3855 			device_printf(child,
3856 			    "Lazy allocation of %ju bus%s at %ju\n", count,
3857 			    count == 1 ? "" : "es", rman_get_start(res));
3858 		PCI_WRITE_CONFIG(dev, child, sec_reg, rman_get_start(res), 1);
3859 		PCI_WRITE_CONFIG(dev, child, sub_reg, rman_get_end(res), 1);
3860 	}
3861 	return (resource_list_alloc(rl, dev, child, PCI_RES_BUS, rid, start,
3862 	    end, count, flags));
3863 }
3864 #endif
3865 
3866 static int
pci_ea_bei_to_rid(device_t dev,int bei)3867 pci_ea_bei_to_rid(device_t dev, int bei)
3868 {
3869 #ifdef PCI_IOV
3870 	struct pci_devinfo *dinfo;
3871 	int iov_pos;
3872 	struct pcicfg_iov *iov;
3873 
3874 	dinfo = device_get_ivars(dev);
3875 	iov = dinfo->cfg.iov;
3876 	if (iov != NULL)
3877 		iov_pos = iov->iov_pos;
3878 	else
3879 		iov_pos = 0;
3880 #endif
3881 
3882 	/* Check if matches BAR */
3883 	if ((bei >= PCIM_EA_BEI_BAR_0) &&
3884 	    (bei <= PCIM_EA_BEI_BAR_5))
3885 		return (PCIR_BAR(bei));
3886 
3887 	/* Check ROM */
3888 	if (bei == PCIM_EA_BEI_ROM)
3889 		return (PCIR_BIOS);
3890 
3891 #ifdef PCI_IOV
3892 	/* Check if matches VF_BAR */
3893 	if ((iov != NULL) && (bei >= PCIM_EA_BEI_VF_BAR_0) &&
3894 	    (bei <= PCIM_EA_BEI_VF_BAR_5))
3895 		return (PCIR_SRIOV_BAR(bei - PCIM_EA_BEI_VF_BAR_0) +
3896 		    iov_pos);
3897 #endif
3898 
3899 	return (-1);
3900 }
3901 
3902 int
pci_ea_is_enabled(device_t dev,int rid)3903 pci_ea_is_enabled(device_t dev, int rid)
3904 {
3905 	struct pci_ea_entry *ea;
3906 	struct pci_devinfo *dinfo;
3907 
3908 	dinfo = device_get_ivars(dev);
3909 
3910 	STAILQ_FOREACH(ea, &dinfo->cfg.ea.ea_entries, eae_link) {
3911 		if (pci_ea_bei_to_rid(dev, ea->eae_bei) == rid)
3912 			return ((ea->eae_flags & PCIM_EA_ENABLE) > 0);
3913 	}
3914 
3915 	return (0);
3916 }
3917 
3918 void
pci_add_resources_ea(device_t bus,device_t dev,int alloc_iov)3919 pci_add_resources_ea(device_t bus, device_t dev, int alloc_iov)
3920 {
3921 	struct pci_ea_entry *ea;
3922 	struct pci_devinfo *dinfo;
3923 	pci_addr_t start, end, count;
3924 	struct resource_list *rl;
3925 	int type, flags, rid;
3926 	struct resource *res;
3927 	uint32_t tmp;
3928 #ifdef PCI_IOV
3929 	struct pcicfg_iov *iov;
3930 #endif
3931 
3932 	dinfo = device_get_ivars(dev);
3933 	rl = &dinfo->resources;
3934 	flags = 0;
3935 
3936 #ifdef PCI_IOV
3937 	iov = dinfo->cfg.iov;
3938 #endif
3939 
3940 	if (dinfo->cfg.ea.ea_location == 0)
3941 		return;
3942 
3943 	STAILQ_FOREACH(ea, &dinfo->cfg.ea.ea_entries, eae_link) {
3944 		/*
3945 		 * TODO: Ignore EA-BAR if is not enabled.
3946 		 *   Currently the EA implementation supports
3947 		 *   only situation, where EA structure contains
3948 		 *   predefined entries. In case they are not enabled
3949 		 *   leave them unallocated and proceed with
3950 		 *   a legacy-BAR mechanism.
3951 		 */
3952 		if ((ea->eae_flags & PCIM_EA_ENABLE) == 0)
3953 			continue;
3954 
3955 		switch ((ea->eae_flags & PCIM_EA_PP) >> PCIM_EA_PP_OFFSET) {
3956 		case PCIM_EA_P_MEM_PREFETCH:
3957 		case PCIM_EA_P_VF_MEM_PREFETCH:
3958 			flags = RF_PREFETCHABLE;
3959 			/* FALLTHROUGH */
3960 		case PCIM_EA_P_VF_MEM:
3961 		case PCIM_EA_P_MEM:
3962 			type = SYS_RES_MEMORY;
3963 			break;
3964 		case PCIM_EA_P_IO:
3965 			type = SYS_RES_IOPORT;
3966 			break;
3967 		default:
3968 			continue;
3969 		}
3970 
3971 		if (alloc_iov != 0) {
3972 #ifdef PCI_IOV
3973 			/* Allocating IOV, confirm BEI matches */
3974 			if ((ea->eae_bei < PCIM_EA_BEI_VF_BAR_0) ||
3975 			    (ea->eae_bei > PCIM_EA_BEI_VF_BAR_5))
3976 				continue;
3977 #else
3978 			continue;
3979 #endif
3980 		} else {
3981 			/* Allocating BAR, confirm BEI matches */
3982 			if (((ea->eae_bei < PCIM_EA_BEI_BAR_0) ||
3983 			    (ea->eae_bei > PCIM_EA_BEI_BAR_5)) &&
3984 			    (ea->eae_bei != PCIM_EA_BEI_ROM))
3985 				continue;
3986 		}
3987 
3988 		rid = pci_ea_bei_to_rid(dev, ea->eae_bei);
3989 		if (rid < 0)
3990 			continue;
3991 
3992 		/* Skip resources already allocated by EA */
3993 		if ((resource_list_find(rl, SYS_RES_MEMORY, rid) != NULL) ||
3994 		    (resource_list_find(rl, SYS_RES_IOPORT, rid) != NULL))
3995 			continue;
3996 
3997 		start = ea->eae_base;
3998 		count = ea->eae_max_offset + 1;
3999 #ifdef PCI_IOV
4000 		if (iov != NULL)
4001 			count = count * iov->iov_num_vfs;
4002 #endif
4003 		end = start + count - 1;
4004 		if (count == 0)
4005 			continue;
4006 
4007 		resource_list_add(rl, type, rid, start, end, count);
4008 		res = resource_list_reserve(rl, bus, dev, type, &rid, start, end, count,
4009 		    flags);
4010 		if (res == NULL) {
4011 			resource_list_delete(rl, type, rid);
4012 
4013 			/*
4014 			 * Failed to allocate using EA, disable entry.
4015 			 * Another attempt to allocation will be performed
4016 			 * further, but this time using legacy BAR registers
4017 			 */
4018 			tmp = pci_read_config(dev, ea->eae_cfg_offset, 4);
4019 			tmp &= ~PCIM_EA_ENABLE;
4020 			pci_write_config(dev, ea->eae_cfg_offset, tmp, 4);
4021 
4022 			/*
4023 			 * Disabling entry might fail in case it is hardwired.
4024 			 * Read flags again to match current status.
4025 			 */
4026 			ea->eae_flags = pci_read_config(dev, ea->eae_cfg_offset, 4);
4027 
4028 			continue;
4029 		}
4030 
4031 		/* As per specification, fill BAR with zeros */
4032 		pci_write_config(dev, rid, 0, 4);
4033 	}
4034 }
4035 
4036 void
pci_add_resources(device_t bus,device_t dev,int force,uint32_t prefetchmask)4037 pci_add_resources(device_t bus, device_t dev, int force, uint32_t prefetchmask)
4038 {
4039 	struct pci_devinfo *dinfo;
4040 	pcicfgregs *cfg;
4041 	struct resource_list *rl;
4042 	const struct pci_quirk *q;
4043 	uint32_t devid;
4044 	int i;
4045 
4046 	dinfo = device_get_ivars(dev);
4047 	cfg = &dinfo->cfg;
4048 	rl = &dinfo->resources;
4049 	devid = (cfg->device << 16) | cfg->vendor;
4050 
4051 	/* Allocate resources using Enhanced Allocation */
4052 	pci_add_resources_ea(bus, dev, 0);
4053 
4054 	/* ATA devices needs special map treatment */
4055 	if ((pci_get_class(dev) == PCIC_STORAGE) &&
4056 	    (pci_get_subclass(dev) == PCIS_STORAGE_IDE) &&
4057 	    ((pci_get_progif(dev) & PCIP_STORAGE_IDE_MASTERDEV) ||
4058 	     (!pci_read_config(dev, PCIR_BAR(0), 4) &&
4059 	      !pci_read_config(dev, PCIR_BAR(2), 4))) )
4060 		pci_ata_maps(bus, dev, rl, force, prefetchmask);
4061 	else
4062 		for (i = 0; i < cfg->nummaps;) {
4063 			/* Skip resources already managed by EA */
4064 			if ((resource_list_find(rl, SYS_RES_MEMORY, PCIR_BAR(i)) != NULL) ||
4065 			    (resource_list_find(rl, SYS_RES_IOPORT, PCIR_BAR(i)) != NULL) ||
4066 			    pci_ea_is_enabled(dev, PCIR_BAR(i))) {
4067 				i++;
4068 				continue;
4069 			}
4070 
4071 			/*
4072 			 * Skip quirked resources.
4073 			 */
4074 			for (q = &pci_quirks[0]; q->devid != 0; q++)
4075 				if (q->devid == devid &&
4076 				    q->type == PCI_QUIRK_UNMAP_REG &&
4077 				    q->arg1 == PCIR_BAR(i))
4078 					break;
4079 			if (q->devid != 0) {
4080 				i++;
4081 				continue;
4082 			}
4083 			i += pci_add_map(bus, dev, PCIR_BAR(i), rl, force,
4084 			    prefetchmask & (1 << i));
4085 		}
4086 
4087 	/*
4088 	 * Add additional, quirked resources.
4089 	 */
4090 	for (q = &pci_quirks[0]; q->devid != 0; q++)
4091 		if (q->devid == devid && q->type == PCI_QUIRK_MAP_REG)
4092 			pci_add_map(bus, dev, q->arg1, rl, force, 0);
4093 
4094 	if (cfg->intpin > 0 && PCI_INTERRUPT_VALID(cfg->intline)) {
4095 #ifdef __PCI_REROUTE_INTERRUPT
4096 		/*
4097 		 * Try to re-route interrupts. Sometimes the BIOS or
4098 		 * firmware may leave bogus values in these registers.
4099 		 * If the re-route fails, then just stick with what we
4100 		 * have.
4101 		 */
4102 		pci_assign_interrupt(bus, dev, 1);
4103 #else
4104 		pci_assign_interrupt(bus, dev, 0);
4105 #endif
4106 	}
4107 
4108 	if (pci_usb_takeover && pci_get_class(dev) == PCIC_SERIALBUS &&
4109 	    pci_get_subclass(dev) == PCIS_SERIALBUS_USB) {
4110 		if (pci_get_progif(dev) == PCIP_SERIALBUS_USB_XHCI)
4111 			xhci_early_takeover(dev);
4112 		else if (pci_get_progif(dev) == PCIP_SERIALBUS_USB_EHCI)
4113 			ehci_early_takeover(dev);
4114 		else if (pci_get_progif(dev) == PCIP_SERIALBUS_USB_OHCI)
4115 			ohci_early_takeover(dev);
4116 		else if (pci_get_progif(dev) == PCIP_SERIALBUS_USB_UHCI)
4117 			uhci_early_takeover(dev);
4118 	}
4119 
4120 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
4121 	/*
4122 	 * Reserve resources for secondary bus ranges behind bridge
4123 	 * devices.
4124 	 */
4125 	pci_reserve_secbus(bus, dev, cfg, rl);
4126 #endif
4127 }
4128 
4129 static struct pci_devinfo *
pci_identify_function(device_t pcib,device_t dev,int domain,int busno,int slot,int func)4130 pci_identify_function(device_t pcib, device_t dev, int domain, int busno,
4131     int slot, int func)
4132 {
4133 	struct pci_devinfo *dinfo;
4134 
4135 	dinfo = pci_read_device(pcib, dev, domain, busno, slot, func);
4136 	if (dinfo != NULL)
4137 		pci_add_child(dev, dinfo);
4138 
4139 	return (dinfo);
4140 }
4141 
4142 void
pci_add_children(device_t dev,int domain,int busno)4143 pci_add_children(device_t dev, int domain, int busno)
4144 {
4145 #define	REG(n, w)	PCIB_READ_CONFIG(pcib, busno, s, f, n, w)
4146 	device_t pcib = device_get_parent(dev);
4147 	struct pci_devinfo *dinfo;
4148 	int maxslots;
4149 	int s, f, pcifunchigh;
4150 	uint8_t hdrtype;
4151 	int first_func;
4152 
4153 	/*
4154 	 * Try to detect a device at slot 0, function 0.  If it exists, try to
4155 	 * enable ARI.  We must enable ARI before detecting the rest of the
4156 	 * functions on this bus as ARI changes the set of slots and functions
4157 	 * that are legal on this bus.
4158 	 */
4159 	dinfo = pci_identify_function(pcib, dev, domain, busno, 0, 0);
4160 	if (dinfo != NULL && pci_enable_ari)
4161 		PCIB_TRY_ENABLE_ARI(pcib, dinfo->cfg.dev);
4162 
4163 	/*
4164 	 * Start looking for new devices on slot 0 at function 1 because we
4165 	 * just identified the device at slot 0, function 0.
4166 	 */
4167 	first_func = 1;
4168 
4169 	maxslots = PCIB_MAXSLOTS(pcib);
4170 	for (s = 0; s <= maxslots; s++, first_func = 0) {
4171 		pcifunchigh = 0;
4172 		f = 0;
4173 		DELAY(1);
4174 
4175 		/* If function 0 is not present, skip to the next slot. */
4176 		if (REG(PCIR_VENDOR, 2) == PCIV_INVALID)
4177 			continue;
4178 		hdrtype = REG(PCIR_HDRTYPE, 1);
4179 		if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
4180 			continue;
4181 		if (hdrtype & PCIM_MFDEV)
4182 			pcifunchigh = PCIB_MAXFUNCS(pcib);
4183 		for (f = first_func; f <= pcifunchigh; f++)
4184 			pci_identify_function(pcib, dev, domain, busno, s, f);
4185 	}
4186 #undef REG
4187 }
4188 
4189 int
pci_rescan_method(device_t dev)4190 pci_rescan_method(device_t dev)
4191 {
4192 #define	REG(n, w)	PCIB_READ_CONFIG(pcib, busno, s, f, n, w)
4193 	device_t pcib = device_get_parent(dev);
4194 	device_t child, *devlist, *unchanged;
4195 	int devcount, error, i, j, maxslots, oldcount;
4196 	int busno, domain, s, f, pcifunchigh;
4197 	uint8_t hdrtype;
4198 
4199 	/* No need to check for ARI on a rescan. */
4200 	error = device_get_children(dev, &devlist, &devcount);
4201 	if (error)
4202 		return (error);
4203 	if (devcount != 0) {
4204 		unchanged = malloc(devcount * sizeof(device_t), M_TEMP,
4205 		    M_NOWAIT | M_ZERO);
4206 		if (unchanged == NULL) {
4207 			free(devlist, M_TEMP);
4208 			return (ENOMEM);
4209 		}
4210 	} else
4211 		unchanged = NULL;
4212 
4213 	domain = pcib_get_domain(dev);
4214 	busno = pcib_get_bus(dev);
4215 	maxslots = PCIB_MAXSLOTS(pcib);
4216 	for (s = 0; s <= maxslots; s++) {
4217 		/* If function 0 is not present, skip to the next slot. */
4218 		f = 0;
4219 		if (REG(PCIR_VENDOR, 2) == PCIV_INVALID)
4220 			continue;
4221 		pcifunchigh = 0;
4222 		hdrtype = REG(PCIR_HDRTYPE, 1);
4223 		if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
4224 			continue;
4225 		if (hdrtype & PCIM_MFDEV)
4226 			pcifunchigh = PCIB_MAXFUNCS(pcib);
4227 		for (f = 0; f <= pcifunchigh; f++) {
4228 			if (REG(PCIR_VENDOR, 2) == PCIV_INVALID)
4229 				continue;
4230 
4231 			/*
4232 			 * Found a valid function.  Check if a
4233 			 * device_t for this device already exists.
4234 			 */
4235 			for (i = 0; i < devcount; i++) {
4236 				child = devlist[i];
4237 				if (child == NULL)
4238 					continue;
4239 				if (pci_get_slot(child) == s &&
4240 				    pci_get_function(child) == f) {
4241 					unchanged[i] = child;
4242 					goto next_func;
4243 				}
4244 			}
4245 
4246 			pci_identify_function(pcib, dev, domain, busno, s, f);
4247 		next_func:;
4248 		}
4249 	}
4250 
4251 	/* Remove devices that are no longer present. */
4252 	for (i = 0; i < devcount; i++) {
4253 		if (unchanged[i] != NULL)
4254 			continue;
4255 		device_delete_child(dev, devlist[i]);
4256 	}
4257 
4258 	free(devlist, M_TEMP);
4259 	oldcount = devcount;
4260 
4261 	/* Try to attach the devices just added. */
4262 	error = device_get_children(dev, &devlist, &devcount);
4263 	if (error) {
4264 		free(unchanged, M_TEMP);
4265 		return (error);
4266 	}
4267 
4268 	for (i = 0; i < devcount; i++) {
4269 		for (j = 0; j < oldcount; j++) {
4270 			if (devlist[i] == unchanged[j])
4271 				goto next_device;
4272 		}
4273 
4274 		device_probe_and_attach(devlist[i]);
4275 	next_device:;
4276 	}
4277 
4278 	free(unchanged, M_TEMP);
4279 	free(devlist, M_TEMP);
4280 	return (0);
4281 #undef REG
4282 }
4283 
4284 #ifdef PCI_IOV
4285 device_t
pci_add_iov_child(device_t bus,device_t pf,uint16_t rid,uint16_t vid,uint16_t did)4286 pci_add_iov_child(device_t bus, device_t pf, uint16_t rid, uint16_t vid,
4287     uint16_t did)
4288 {
4289 	struct pci_devinfo *vf_dinfo;
4290 	device_t pcib;
4291 	int busno, slot, func;
4292 
4293 	pcib = device_get_parent(bus);
4294 
4295 	PCIB_DECODE_RID(pcib, rid, &busno, &slot, &func);
4296 
4297 	vf_dinfo = pci_fill_devinfo(pcib, bus, pci_get_domain(pcib), busno,
4298 	    slot, func, vid, did);
4299 
4300 	vf_dinfo->cfg.flags |= PCICFG_VF;
4301 	pci_add_child(bus, vf_dinfo);
4302 
4303 	return (vf_dinfo->cfg.dev);
4304 }
4305 
4306 device_t
pci_create_iov_child_method(device_t bus,device_t pf,uint16_t rid,uint16_t vid,uint16_t did)4307 pci_create_iov_child_method(device_t bus, device_t pf, uint16_t rid,
4308     uint16_t vid, uint16_t did)
4309 {
4310 
4311 	return (pci_add_iov_child(bus, pf, rid, vid, did));
4312 }
4313 #endif
4314 
4315 /*
4316  * For PCIe device set Max_Payload_Size to match PCIe root's.
4317  */
4318 static void
pcie_setup_mps(device_t dev)4319 pcie_setup_mps(device_t dev)
4320 {
4321 	struct pci_devinfo *dinfo = device_get_ivars(dev);
4322 	device_t root;
4323 	uint16_t rmps, mmps, mps;
4324 
4325 	if (dinfo->cfg.pcie.pcie_location == 0)
4326 		return;
4327 	root = pci_find_pcie_root_port(dev);
4328 	if (root == NULL)
4329 		return;
4330 	/* Check whether the MPS is already configured. */
4331 	rmps = pcie_read_config(root, PCIER_DEVICE_CTL, 2) &
4332 	    PCIEM_CTL_MAX_PAYLOAD;
4333 	mps = pcie_read_config(dev, PCIER_DEVICE_CTL, 2) &
4334 	    PCIEM_CTL_MAX_PAYLOAD;
4335 	if (mps == rmps)
4336 		return;
4337 	/* Check whether the device is capable of the root's MPS. */
4338 	mmps = (pcie_read_config(dev, PCIER_DEVICE_CAP, 2) &
4339 	    PCIEM_CAP_MAX_PAYLOAD) << 5;
4340 	if (rmps > mmps) {
4341 		/*
4342 		 * The device is unable to handle root's MPS.  Limit root.
4343 		 * XXX: We should traverse through all the tree, applying
4344 		 * it to all the devices.
4345 		 */
4346 		pcie_adjust_config(root, PCIER_DEVICE_CTL,
4347 		    PCIEM_CTL_MAX_PAYLOAD, mmps, 2);
4348 	} else {
4349 		pcie_adjust_config(dev, PCIER_DEVICE_CTL,
4350 		    PCIEM_CTL_MAX_PAYLOAD, rmps, 2);
4351 	}
4352 }
4353 
4354 static void
pci_add_child_clear_aer(device_t dev,struct pci_devinfo * dinfo)4355 pci_add_child_clear_aer(device_t dev, struct pci_devinfo *dinfo)
4356 {
4357 	int aer;
4358 	uint32_t r;
4359 	uint16_t r2;
4360 
4361 	if (dinfo->cfg.pcie.pcie_location != 0 &&
4362 	    dinfo->cfg.pcie.pcie_type == PCIEM_TYPE_ROOT_PORT) {
4363 		r2 = pci_read_config(dev, dinfo->cfg.pcie.pcie_location +
4364 		    PCIER_ROOT_CTL, 2);
4365 		r2 &= ~(PCIEM_ROOT_CTL_SERR_CORR |
4366 		    PCIEM_ROOT_CTL_SERR_NONFATAL | PCIEM_ROOT_CTL_SERR_FATAL);
4367 		pci_write_config(dev, dinfo->cfg.pcie.pcie_location +
4368 		    PCIER_ROOT_CTL, r2, 2);
4369 	}
4370 	if (pci_find_extcap(dev, PCIZ_AER, &aer) == 0) {
4371 		r = pci_read_config(dev, aer + PCIR_AER_UC_STATUS, 4);
4372 		pci_write_config(dev, aer + PCIR_AER_UC_STATUS, r, 4);
4373 		if (r != 0 && bootverbose) {
4374 			pci_printf(&dinfo->cfg,
4375 			    "clearing AER UC 0x%08x -> 0x%08x\n",
4376 			    r, pci_read_config(dev, aer + PCIR_AER_UC_STATUS,
4377 			    4));
4378 		}
4379 
4380 		r = pci_read_config(dev, aer + PCIR_AER_UC_MASK, 4);
4381 		r &= ~(PCIM_AER_UC_TRAINING_ERROR |
4382 		    PCIM_AER_UC_DL_PROTOCOL_ERROR |
4383 		    PCIM_AER_UC_SURPRISE_LINK_DOWN |
4384 		    PCIM_AER_UC_POISONED_TLP |
4385 		    PCIM_AER_UC_FC_PROTOCOL_ERROR |
4386 		    PCIM_AER_UC_COMPLETION_TIMEOUT |
4387 		    PCIM_AER_UC_COMPLETER_ABORT |
4388 		    PCIM_AER_UC_UNEXPECTED_COMPLETION |
4389 		    PCIM_AER_UC_RECEIVER_OVERFLOW |
4390 		    PCIM_AER_UC_MALFORMED_TLP |
4391 		    PCIM_AER_UC_ECRC_ERROR |
4392 		    PCIM_AER_UC_UNSUPPORTED_REQUEST |
4393 		    PCIM_AER_UC_ACS_VIOLATION |
4394 		    PCIM_AER_UC_INTERNAL_ERROR |
4395 		    PCIM_AER_UC_MC_BLOCKED_TLP |
4396 		    PCIM_AER_UC_ATOMIC_EGRESS_BLK |
4397 		    PCIM_AER_UC_TLP_PREFIX_BLOCKED);
4398 		pci_write_config(dev, aer + PCIR_AER_UC_MASK, r, 4);
4399 
4400 		r = pci_read_config(dev, aer + PCIR_AER_COR_STATUS, 4);
4401 		pci_write_config(dev, aer + PCIR_AER_COR_STATUS, r, 4);
4402 		if (r != 0 && bootverbose) {
4403 			pci_printf(&dinfo->cfg,
4404 			    "clearing AER COR 0x%08x -> 0x%08x\n",
4405 			    r, pci_read_config(dev, aer + PCIR_AER_COR_STATUS,
4406 			    4));
4407 		}
4408 
4409 		r = pci_read_config(dev, aer + PCIR_AER_COR_MASK, 4);
4410 		r &= ~(PCIM_AER_COR_RECEIVER_ERROR |
4411 		    PCIM_AER_COR_BAD_TLP |
4412 		    PCIM_AER_COR_BAD_DLLP |
4413 		    PCIM_AER_COR_REPLAY_ROLLOVER |
4414 		    PCIM_AER_COR_REPLAY_TIMEOUT |
4415 		    PCIM_AER_COR_ADVISORY_NF_ERROR |
4416 		    PCIM_AER_COR_INTERNAL_ERROR |
4417 		    PCIM_AER_COR_HEADER_LOG_OVFLOW);
4418 		pci_write_config(dev, aer + PCIR_AER_COR_MASK, r, 4);
4419 
4420 		r = pci_read_config(dev, dinfo->cfg.pcie.pcie_location +
4421 		    PCIER_DEVICE_CTL, 2);
4422 		r |=  PCIEM_CTL_COR_ENABLE | PCIEM_CTL_NFER_ENABLE |
4423 		    PCIEM_CTL_FER_ENABLE | PCIEM_CTL_URR_ENABLE;
4424 		pci_write_config(dev, dinfo->cfg.pcie.pcie_location +
4425 		    PCIER_DEVICE_CTL, r, 2);
4426 	}
4427 }
4428 
4429 void
pci_add_child(device_t bus,struct pci_devinfo * dinfo)4430 pci_add_child(device_t bus, struct pci_devinfo *dinfo)
4431 {
4432 	device_t dev;
4433 
4434 	dinfo->cfg.dev = dev = device_add_child(bus, NULL, -1);
4435 	device_set_ivars(dev, dinfo);
4436 	resource_list_init(&dinfo->resources);
4437 	pci_cfg_save(dev, dinfo, 0);
4438 	pci_cfg_restore(dev, dinfo);
4439 	pci_print_verbose(dinfo);
4440 	pci_add_resources(bus, dev, 0, 0);
4441 	pcie_setup_mps(dev);
4442 	pci_child_added(dinfo->cfg.dev);
4443 
4444 	if (pci_clear_aer_on_attach)
4445 		pci_add_child_clear_aer(dev, dinfo);
4446 
4447 	EVENTHANDLER_INVOKE(pci_add_device, dinfo->cfg.dev);
4448 }
4449 
4450 void
pci_child_added_method(device_t dev,device_t child)4451 pci_child_added_method(device_t dev, device_t child)
4452 {
4453 
4454 }
4455 
4456 static int
pci_probe(device_t dev)4457 pci_probe(device_t dev)
4458 {
4459 
4460 	device_set_desc(dev, "PCI bus");
4461 
4462 	/* Allow other subclasses to override this driver. */
4463 	return (BUS_PROBE_GENERIC);
4464 }
4465 
4466 int
pci_attach_common(device_t dev)4467 pci_attach_common(device_t dev)
4468 {
4469 	struct pci_softc *sc;
4470 	int busno, domain;
4471 #ifdef PCI_RES_BUS
4472 	int rid;
4473 #endif
4474 
4475 	sc = device_get_softc(dev);
4476 	domain = pcib_get_domain(dev);
4477 	busno = pcib_get_bus(dev);
4478 #ifdef PCI_RES_BUS
4479 	rid = 0;
4480 	sc->sc_bus = bus_alloc_resource(dev, PCI_RES_BUS, &rid, busno, busno,
4481 	    1, 0);
4482 	if (sc->sc_bus == NULL) {
4483 		device_printf(dev, "failed to allocate bus number\n");
4484 		return (ENXIO);
4485 	}
4486 #endif
4487 	if (bootverbose)
4488 		device_printf(dev, "domain=%d, physical bus=%d\n",
4489 		    domain, busno);
4490 	sc->sc_dma_tag = bus_get_dma_tag(dev);
4491 	return (0);
4492 }
4493 
4494 int
pci_attach(device_t dev)4495 pci_attach(device_t dev)
4496 {
4497 	int busno, domain, error;
4498 
4499 	error = pci_attach_common(dev);
4500 	if (error)
4501 		return (error);
4502 
4503 	/*
4504 	 * Since there can be multiple independently numbered PCI
4505 	 * buses on systems with multiple PCI domains, we can't use
4506 	 * the unit number to decide which bus we are probing. We ask
4507 	 * the parent pcib what our domain and bus numbers are.
4508 	 */
4509 	domain = pcib_get_domain(dev);
4510 	busno = pcib_get_bus(dev);
4511 	pci_add_children(dev, domain, busno);
4512 	return (bus_generic_attach(dev));
4513 }
4514 
4515 int
pci_detach(device_t dev)4516 pci_detach(device_t dev)
4517 {
4518 #ifdef PCI_RES_BUS
4519 	struct pci_softc *sc;
4520 #endif
4521 	int error;
4522 
4523 	error = bus_generic_detach(dev);
4524 	if (error)
4525 		return (error);
4526 #ifdef PCI_RES_BUS
4527 	sc = device_get_softc(dev);
4528 	error = bus_release_resource(dev, PCI_RES_BUS, 0, sc->sc_bus);
4529 	if (error)
4530 		return (error);
4531 #endif
4532 	return (device_delete_children(dev));
4533 }
4534 
4535 static void
pci_hint_device_unit(device_t dev,device_t child,const char * name,int * unitp)4536 pci_hint_device_unit(device_t dev, device_t child, const char *name, int *unitp)
4537 {
4538 	int line, unit;
4539 	const char *at;
4540 	char me1[24], me2[32];
4541 	uint8_t b, s, f;
4542 	uint32_t d;
4543 
4544 	d = pci_get_domain(child);
4545 	b = pci_get_bus(child);
4546 	s = pci_get_slot(child);
4547 	f = pci_get_function(child);
4548 	snprintf(me1, sizeof(me1), "pci%u:%u:%u", b, s, f);
4549 	snprintf(me2, sizeof(me2), "pci%u:%u:%u:%u", d, b, s, f);
4550 	line = 0;
4551 	while (resource_find_dev(&line, name, &unit, "at", NULL) == 0) {
4552 		resource_string_value(name, unit, "at", &at);
4553 		if (strcmp(at, me1) != 0 && strcmp(at, me2) != 0)
4554 			continue; /* No match, try next candidate */
4555 		*unitp = unit;
4556 		return;
4557 	}
4558 }
4559 
4560 static void
pci_set_power_child(device_t dev,device_t child,int state)4561 pci_set_power_child(device_t dev, device_t child, int state)
4562 {
4563 	device_t pcib;
4564 	int dstate;
4565 
4566 	/*
4567 	 * Set the device to the given state.  If the firmware suggests
4568 	 * a different power state, use it instead.  If power management
4569 	 * is not present, the firmware is responsible for managing
4570 	 * device power.  Skip children who aren't attached since they
4571 	 * are handled separately.
4572 	 */
4573 	pcib = device_get_parent(dev);
4574 	dstate = state;
4575 	if (device_is_attached(child) &&
4576 	    PCIB_POWER_FOR_SLEEP(pcib, child, &dstate) == 0)
4577 		pci_set_powerstate(child, dstate);
4578 }
4579 
4580 int
pci_suspend_child(device_t dev,device_t child)4581 pci_suspend_child(device_t dev, device_t child)
4582 {
4583 	struct pci_devinfo *dinfo;
4584 	struct resource_list_entry *rle;
4585 	int error;
4586 
4587 	dinfo = device_get_ivars(child);
4588 
4589 	/*
4590 	 * Save the PCI configuration space for the child and set the
4591 	 * device in the appropriate power state for this sleep state.
4592 	 */
4593 	pci_cfg_save(child, dinfo, 0);
4594 
4595 	/* Suspend devices before potentially powering them down. */
4596 	error = bus_generic_suspend_child(dev, child);
4597 
4598 	if (error)
4599 		return (error);
4600 
4601 	if (pci_do_power_suspend) {
4602 		/*
4603 		 * Make sure this device's interrupt handler is not invoked
4604 		 * in the case the device uses a shared interrupt that can
4605 		 * be raised by some other device.
4606 		 * This is applicable only to regular (legacy) PCI interrupts
4607 		 * as MSI/MSI-X interrupts are never shared.
4608 		 */
4609 		rle = resource_list_find(&dinfo->resources,
4610 		    SYS_RES_IRQ, 0);
4611 		if (rle != NULL && rle->res != NULL)
4612 			(void)bus_suspend_intr(child, rle->res);
4613 		pci_set_power_child(dev, child, PCI_POWERSTATE_D3);
4614 	}
4615 
4616 	return (0);
4617 }
4618 
4619 int
pci_resume_child(device_t dev,device_t child)4620 pci_resume_child(device_t dev, device_t child)
4621 {
4622 	struct pci_devinfo *dinfo;
4623 	struct resource_list_entry *rle;
4624 
4625 	if (pci_do_power_resume)
4626 		pci_set_power_child(dev, child, PCI_POWERSTATE_D0);
4627 
4628 	dinfo = device_get_ivars(child);
4629 	pci_cfg_restore(child, dinfo);
4630 	if (!device_is_attached(child))
4631 		pci_cfg_save(child, dinfo, 1);
4632 
4633 	bus_generic_resume_child(dev, child);
4634 
4635 	/*
4636 	 * Allow interrupts only after fully resuming the driver and hardware.
4637 	 */
4638 	if (pci_do_power_suspend) {
4639 		/* See pci_suspend_child for details. */
4640 		rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, 0);
4641 		if (rle != NULL && rle->res != NULL)
4642 			(void)bus_resume_intr(child, rle->res);
4643 	}
4644 
4645 	return (0);
4646 }
4647 
4648 int
pci_resume(device_t dev)4649 pci_resume(device_t dev)
4650 {
4651 	device_t child, *devlist;
4652 	int error, i, numdevs;
4653 
4654 	if ((error = device_get_children(dev, &devlist, &numdevs)) != 0)
4655 		return (error);
4656 
4657 	/*
4658 	 * Resume critical devices first, then everything else later.
4659 	 */
4660 	for (i = 0; i < numdevs; i++) {
4661 		child = devlist[i];
4662 		switch (pci_get_class(child)) {
4663 		case PCIC_DISPLAY:
4664 		case PCIC_MEMORY:
4665 		case PCIC_BRIDGE:
4666 		case PCIC_BASEPERIPH:
4667 			BUS_RESUME_CHILD(dev, child);
4668 			break;
4669 		}
4670 	}
4671 	for (i = 0; i < numdevs; i++) {
4672 		child = devlist[i];
4673 		switch (pci_get_class(child)) {
4674 		case PCIC_DISPLAY:
4675 		case PCIC_MEMORY:
4676 		case PCIC_BRIDGE:
4677 		case PCIC_BASEPERIPH:
4678 			break;
4679 		default:
4680 			BUS_RESUME_CHILD(dev, child);
4681 		}
4682 	}
4683 	free(devlist, M_TEMP);
4684 	return (0);
4685 }
4686 
4687 static void
pci_load_vendor_data(void)4688 pci_load_vendor_data(void)
4689 {
4690 	caddr_t data;
4691 	void *ptr;
4692 	size_t sz;
4693 
4694 	data = preload_search_by_type("pci_vendor_data");
4695 	if (data != NULL) {
4696 		ptr = preload_fetch_addr(data);
4697 		sz = preload_fetch_size(data);
4698 		if (ptr != NULL && sz != 0) {
4699 			pci_vendordata = ptr;
4700 			pci_vendordata_size = sz;
4701 			/* terminate the database */
4702 			pci_vendordata[pci_vendordata_size] = '\n';
4703 		}
4704 	}
4705 }
4706 
4707 void
pci_driver_added(device_t dev,driver_t * driver)4708 pci_driver_added(device_t dev, driver_t *driver)
4709 {
4710 	int numdevs;
4711 	device_t *devlist;
4712 	device_t child;
4713 	struct pci_devinfo *dinfo;
4714 	int i;
4715 
4716 	if (bootverbose)
4717 		device_printf(dev, "driver added\n");
4718 	DEVICE_IDENTIFY(driver, dev);
4719 	if (device_get_children(dev, &devlist, &numdevs) != 0)
4720 		return;
4721 	for (i = 0; i < numdevs; i++) {
4722 		child = devlist[i];
4723 		if (device_get_state(child) != DS_NOTPRESENT)
4724 			continue;
4725 		dinfo = device_get_ivars(child);
4726 		pci_print_verbose(dinfo);
4727 		if (bootverbose)
4728 			pci_printf(&dinfo->cfg, "reprobing on driver added\n");
4729 		pci_cfg_restore(child, dinfo);
4730 		if (device_probe_and_attach(child) != 0)
4731 			pci_child_detached(dev, child);
4732 	}
4733 	free(devlist, M_TEMP);
4734 }
4735 
4736 int
pci_setup_intr(device_t dev,device_t child,struct resource * irq,int flags,driver_filter_t * filter,driver_intr_t * intr,void * arg,void ** cookiep)4737 pci_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
4738     driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
4739 {
4740 	struct pci_devinfo *dinfo;
4741 	struct msix_table_entry *mte;
4742 	struct msix_vector *mv;
4743 	uint64_t addr;
4744 	uint32_t data;
4745 	void *cookie;
4746 	int error, rid;
4747 
4748 	error = bus_generic_setup_intr(dev, child, irq, flags, filter, intr,
4749 	    arg, &cookie);
4750 	if (error)
4751 		return (error);
4752 
4753 	/* If this is not a direct child, just bail out. */
4754 	if (device_get_parent(child) != dev) {
4755 		*cookiep = cookie;
4756 		return(0);
4757 	}
4758 
4759 	rid = rman_get_rid(irq);
4760 	if (rid == 0) {
4761 		/* Make sure that INTx is enabled */
4762 		pci_clear_command_bit(dev, child, PCIM_CMD_INTxDIS);
4763 	} else {
4764 		/*
4765 		 * Check to see if the interrupt is MSI or MSI-X.
4766 		 * Ask our parent to map the MSI and give
4767 		 * us the address and data register values.
4768 		 * If we fail for some reason, teardown the
4769 		 * interrupt handler.
4770 		 */
4771 		dinfo = device_get_ivars(child);
4772 		if (dinfo->cfg.msi.msi_alloc > 0) {
4773 			if (dinfo->cfg.msi.msi_addr == 0) {
4774 				KASSERT(dinfo->cfg.msi.msi_handlers == 0,
4775 			    ("MSI has handlers, but vectors not mapped"));
4776 				error = PCIB_MAP_MSI(device_get_parent(dev),
4777 				    child, rman_get_start(irq), &addr, &data);
4778 				if (error)
4779 					goto bad;
4780 				dinfo->cfg.msi.msi_addr = addr;
4781 				dinfo->cfg.msi.msi_data = data;
4782 			}
4783 			if (dinfo->cfg.msi.msi_handlers == 0)
4784 				pci_enable_msi(child, dinfo->cfg.msi.msi_addr,
4785 				    dinfo->cfg.msi.msi_data);
4786 			dinfo->cfg.msi.msi_handlers++;
4787 		} else {
4788 			KASSERT(dinfo->cfg.msix.msix_alloc > 0,
4789 			    ("No MSI or MSI-X interrupts allocated"));
4790 			KASSERT(rid <= dinfo->cfg.msix.msix_table_len,
4791 			    ("MSI-X index too high"));
4792 			mte = &dinfo->cfg.msix.msix_table[rid - 1];
4793 			KASSERT(mte->mte_vector != 0, ("no message vector"));
4794 			mv = &dinfo->cfg.msix.msix_vectors[mte->mte_vector - 1];
4795 			KASSERT(mv->mv_irq == rman_get_start(irq),
4796 			    ("IRQ mismatch"));
4797 			if (mv->mv_address == 0) {
4798 				KASSERT(mte->mte_handlers == 0,
4799 		    ("MSI-X table entry has handlers, but vector not mapped"));
4800 				error = PCIB_MAP_MSI(device_get_parent(dev),
4801 				    child, rman_get_start(irq), &addr, &data);
4802 				if (error)
4803 					goto bad;
4804 				mv->mv_address = addr;
4805 				mv->mv_data = data;
4806 			}
4807 
4808 			/*
4809 			 * The MSIX table entry must be made valid by
4810 			 * incrementing the mte_handlers before
4811 			 * calling pci_enable_msix() and
4812 			 * pci_resume_msix(). Else the MSIX rewrite
4813 			 * table quirk will not work as expected.
4814 			 */
4815 			mte->mte_handlers++;
4816 			if (mte->mte_handlers == 1) {
4817 				pci_enable_msix(child, rid - 1, mv->mv_address,
4818 				    mv->mv_data);
4819 				pci_unmask_msix(child, rid - 1);
4820 			}
4821 		}
4822 
4823 		/*
4824 		 * Make sure that INTx is disabled if we are using MSI/MSI-X,
4825 		 * unless the device is affected by PCI_QUIRK_MSI_INTX_BUG,
4826 		 * in which case we "enable" INTx so MSI/MSI-X actually works.
4827 		 */
4828 		if (!pci_has_quirk(pci_get_devid(child),
4829 		    PCI_QUIRK_MSI_INTX_BUG))
4830 			pci_set_command_bit(dev, child, PCIM_CMD_INTxDIS);
4831 		else
4832 			pci_clear_command_bit(dev, child, PCIM_CMD_INTxDIS);
4833 	bad:
4834 		if (error) {
4835 			(void)bus_generic_teardown_intr(dev, child, irq,
4836 			    cookie);
4837 			return (error);
4838 		}
4839 	}
4840 	*cookiep = cookie;
4841 	return (0);
4842 }
4843 
4844 int
pci_teardown_intr(device_t dev,device_t child,struct resource * irq,void * cookie)4845 pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
4846     void *cookie)
4847 {
4848 	struct msix_table_entry *mte;
4849 	struct resource_list_entry *rle;
4850 	struct pci_devinfo *dinfo;
4851 	int error, rid;
4852 
4853 	if (irq == NULL || !(rman_get_flags(irq) & RF_ACTIVE))
4854 		return (EINVAL);
4855 
4856 	/* If this isn't a direct child, just bail out */
4857 	if (device_get_parent(child) != dev)
4858 		return(bus_generic_teardown_intr(dev, child, irq, cookie));
4859 
4860 	rid = rman_get_rid(irq);
4861 	if (rid == 0) {
4862 		/* Mask INTx */
4863 		pci_set_command_bit(dev, child, PCIM_CMD_INTxDIS);
4864 	} else {
4865 		/*
4866 		 * Check to see if the interrupt is MSI or MSI-X.  If so,
4867 		 * decrement the appropriate handlers count and mask the
4868 		 * MSI-X message, or disable MSI messages if the count
4869 		 * drops to 0.
4870 		 */
4871 		dinfo = device_get_ivars(child);
4872 		rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, rid);
4873 		if (rle->res != irq)
4874 			return (EINVAL);
4875 		if (dinfo->cfg.msi.msi_alloc > 0) {
4876 			KASSERT(rid <= dinfo->cfg.msi.msi_alloc,
4877 			    ("MSI-X index too high"));
4878 			if (dinfo->cfg.msi.msi_handlers == 0)
4879 				return (EINVAL);
4880 			dinfo->cfg.msi.msi_handlers--;
4881 			if (dinfo->cfg.msi.msi_handlers == 0)
4882 				pci_disable_msi(child);
4883 		} else {
4884 			KASSERT(dinfo->cfg.msix.msix_alloc > 0,
4885 			    ("No MSI or MSI-X interrupts allocated"));
4886 			KASSERT(rid <= dinfo->cfg.msix.msix_table_len,
4887 			    ("MSI-X index too high"));
4888 			mte = &dinfo->cfg.msix.msix_table[rid - 1];
4889 			if (mte->mte_handlers == 0)
4890 				return (EINVAL);
4891 			mte->mte_handlers--;
4892 			if (mte->mte_handlers == 0)
4893 				pci_mask_msix(child, rid - 1);
4894 		}
4895 	}
4896 	error = bus_generic_teardown_intr(dev, child, irq, cookie);
4897 	if (rid > 0)
4898 		KASSERT(error == 0,
4899 		    ("%s: generic teardown failed for MSI/MSI-X", __func__));
4900 	return (error);
4901 }
4902 
4903 int
pci_print_child(device_t dev,device_t child)4904 pci_print_child(device_t dev, device_t child)
4905 {
4906 	struct pci_devinfo *dinfo;
4907 	struct resource_list *rl;
4908 	int retval = 0;
4909 
4910 	dinfo = device_get_ivars(child);
4911 	rl = &dinfo->resources;
4912 
4913 	retval += bus_print_child_header(dev, child);
4914 
4915 	retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#jx");
4916 	retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx");
4917 	retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
4918 	if (device_get_flags(dev))
4919 		retval += printf(" flags %#x", device_get_flags(dev));
4920 
4921 	retval += printf(" at device %d.%d", pci_get_slot(child),
4922 	    pci_get_function(child));
4923 
4924 	retval += bus_print_child_domain(dev, child);
4925 	retval += bus_print_child_footer(dev, child);
4926 
4927 	return (retval);
4928 }
4929 
4930 static const struct
4931 {
4932 	int		class;
4933 	int		subclass;
4934 	int		report; /* 0 = bootverbose, 1 = always */
4935 	const char	*desc;
4936 } pci_nomatch_tab[] = {
4937 	{PCIC_OLD,		-1,			1, "old"},
4938 	{PCIC_OLD,		PCIS_OLD_NONVGA,	1, "non-VGA display device"},
4939 	{PCIC_OLD,		PCIS_OLD_VGA,		1, "VGA-compatible display device"},
4940 	{PCIC_STORAGE,		-1,			1, "mass storage"},
4941 	{PCIC_STORAGE,		PCIS_STORAGE_SCSI,	1, "SCSI"},
4942 	{PCIC_STORAGE,		PCIS_STORAGE_IDE,	1, "ATA"},
4943 	{PCIC_STORAGE,		PCIS_STORAGE_FLOPPY,	1, "floppy disk"},
4944 	{PCIC_STORAGE,		PCIS_STORAGE_IPI,	1, "IPI"},
4945 	{PCIC_STORAGE,		PCIS_STORAGE_RAID,	1, "RAID"},
4946 	{PCIC_STORAGE,		PCIS_STORAGE_ATA_ADMA,	1, "ATA (ADMA)"},
4947 	{PCIC_STORAGE,		PCIS_STORAGE_SATA,	1, "SATA"},
4948 	{PCIC_STORAGE,		PCIS_STORAGE_SAS,	1, "SAS"},
4949 	{PCIC_STORAGE,		PCIS_STORAGE_NVM,	1, "NVM"},
4950 	{PCIC_NETWORK,		-1,			1, "network"},
4951 	{PCIC_NETWORK,		PCIS_NETWORK_ETHERNET,	1, "ethernet"},
4952 	{PCIC_NETWORK,		PCIS_NETWORK_TOKENRING,	1, "token ring"},
4953 	{PCIC_NETWORK,		PCIS_NETWORK_FDDI,	1, "fddi"},
4954 	{PCIC_NETWORK,		PCIS_NETWORK_ATM,	1, "ATM"},
4955 	{PCIC_NETWORK,		PCIS_NETWORK_ISDN,	1, "ISDN"},
4956 	{PCIC_DISPLAY,		-1,			1, "display"},
4957 	{PCIC_DISPLAY,		PCIS_DISPLAY_VGA,	1, "VGA"},
4958 	{PCIC_DISPLAY,		PCIS_DISPLAY_XGA,	1, "XGA"},
4959 	{PCIC_DISPLAY,		PCIS_DISPLAY_3D,	1, "3D"},
4960 	{PCIC_MULTIMEDIA,	-1,			1, "multimedia"},
4961 	{PCIC_MULTIMEDIA,	PCIS_MULTIMEDIA_VIDEO,	1, "video"},
4962 	{PCIC_MULTIMEDIA,	PCIS_MULTIMEDIA_AUDIO,	1, "audio"},
4963 	{PCIC_MULTIMEDIA,	PCIS_MULTIMEDIA_TELE,	1, "telephony"},
4964 	{PCIC_MULTIMEDIA,	PCIS_MULTIMEDIA_HDA,	1, "HDA"},
4965 	{PCIC_MEMORY,		-1,			1, "memory"},
4966 	{PCIC_MEMORY,		PCIS_MEMORY_RAM,	1, "RAM"},
4967 	{PCIC_MEMORY,		PCIS_MEMORY_FLASH,	1, "flash"},
4968 	{PCIC_BRIDGE,		-1,			1, "bridge"},
4969 	{PCIC_BRIDGE,		PCIS_BRIDGE_HOST,	1, "HOST-PCI"},
4970 	{PCIC_BRIDGE,		PCIS_BRIDGE_ISA,	1, "PCI-ISA"},
4971 	{PCIC_BRIDGE,		PCIS_BRIDGE_EISA,	1, "PCI-EISA"},
4972 	{PCIC_BRIDGE,		PCIS_BRIDGE_MCA,	1, "PCI-MCA"},
4973 	{PCIC_BRIDGE,		PCIS_BRIDGE_PCI,	1, "PCI-PCI"},
4974 	{PCIC_BRIDGE,		PCIS_BRIDGE_PCMCIA,	1, "PCI-PCMCIA"},
4975 	{PCIC_BRIDGE,		PCIS_BRIDGE_NUBUS,	1, "PCI-NuBus"},
4976 	{PCIC_BRIDGE,		PCIS_BRIDGE_CARDBUS,	1, "PCI-CardBus"},
4977 	{PCIC_BRIDGE,		PCIS_BRIDGE_RACEWAY,	1, "PCI-RACEway"},
4978 	{PCIC_SIMPLECOMM,	-1,			1, "simple comms"},
4979 	{PCIC_SIMPLECOMM,	PCIS_SIMPLECOMM_UART,	1, "UART"},	/* could detect 16550 */
4980 	{PCIC_SIMPLECOMM,	PCIS_SIMPLECOMM_PAR,	1, "parallel port"},
4981 	{PCIC_SIMPLECOMM,	PCIS_SIMPLECOMM_MULSER,	1, "multiport serial"},
4982 	{PCIC_SIMPLECOMM,	PCIS_SIMPLECOMM_MODEM,	1, "generic modem"},
4983 	{PCIC_BASEPERIPH,	-1,			0, "base peripheral"},
4984 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_PIC,	1, "interrupt controller"},
4985 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_DMA,	1, "DMA controller"},
4986 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_TIMER,	1, "timer"},
4987 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_RTC,	1, "realtime clock"},
4988 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_PCIHOT,	1, "PCI hot-plug controller"},
4989 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_SDHC,	1, "SD host controller"},
4990 	{PCIC_BASEPERIPH,	PCIS_BASEPERIPH_IOMMU,	1, "IOMMU"},
4991 	{PCIC_INPUTDEV,		-1,			1, "input device"},
4992 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_KEYBOARD,	1, "keyboard"},
4993 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_DIGITIZER,1, "digitizer"},
4994 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_MOUSE,	1, "mouse"},
4995 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_SCANNER,	1, "scanner"},
4996 	{PCIC_INPUTDEV,		PCIS_INPUTDEV_GAMEPORT,	1, "gameport"},
4997 	{PCIC_DOCKING,		-1,			1, "docking station"},
4998 	{PCIC_PROCESSOR,	-1,			1, "processor"},
4999 	{PCIC_SERIALBUS,	-1,			1, "serial bus"},
5000 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_FW,	1, "FireWire"},
5001 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_ACCESS,	1, "AccessBus"},
5002 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_SSA,	1, "SSA"},
5003 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_USB,	1, "USB"},
5004 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_FC,	1, "Fibre Channel"},
5005 	{PCIC_SERIALBUS,	PCIS_SERIALBUS_SMBUS,	0, "SMBus"},
5006 	{PCIC_WIRELESS,		-1,			1, "wireless controller"},
5007 	{PCIC_WIRELESS,		PCIS_WIRELESS_IRDA,	1, "iRDA"},
5008 	{PCIC_WIRELESS,		PCIS_WIRELESS_IR,	1, "IR"},
5009 	{PCIC_WIRELESS,		PCIS_WIRELESS_RF,	1, "RF"},
5010 	{PCIC_INTELLIIO,	-1,			1, "intelligent I/O controller"},
5011 	{PCIC_INTELLIIO,	PCIS_INTELLIIO_I2O,	1, "I2O"},
5012 	{PCIC_SATCOM,		-1,			1, "satellite communication"},
5013 	{PCIC_SATCOM,		PCIS_SATCOM_TV,		1, "sat TV"},
5014 	{PCIC_SATCOM,		PCIS_SATCOM_AUDIO,	1, "sat audio"},
5015 	{PCIC_SATCOM,		PCIS_SATCOM_VOICE,	1, "sat voice"},
5016 	{PCIC_SATCOM,		PCIS_SATCOM_DATA,	1, "sat data"},
5017 	{PCIC_CRYPTO,		-1,			1, "encrypt/decrypt"},
5018 	{PCIC_CRYPTO,		PCIS_CRYPTO_NETCOMP,	1, "network/computer crypto"},
5019 	{PCIC_CRYPTO,		PCIS_CRYPTO_ENTERTAIN,	1, "entertainment crypto"},
5020 	{PCIC_DASP,		-1,			0, "dasp"},
5021 	{PCIC_DASP,		PCIS_DASP_DPIO,		1, "DPIO module"},
5022 	{PCIC_DASP,		PCIS_DASP_PERFCNTRS,	1, "performance counters"},
5023 	{PCIC_DASP,		PCIS_DASP_COMM_SYNC,	1, "communication synchronizer"},
5024 	{PCIC_DASP,		PCIS_DASP_MGMT_CARD,	1, "signal processing management"},
5025 	{0, 0, 0,		NULL}
5026 };
5027 
5028 void
pci_probe_nomatch(device_t dev,device_t child)5029 pci_probe_nomatch(device_t dev, device_t child)
5030 {
5031 	int i, report;
5032 	const char *cp, *scp;
5033 	char *device;
5034 
5035 	/*
5036 	 * Look for a listing for this device in a loaded device database.
5037 	 */
5038 	report = 1;
5039 	if ((device = pci_describe_device(child)) != NULL) {
5040 		device_printf(dev, "<%s>", device);
5041 		free(device, M_DEVBUF);
5042 	} else {
5043 		/*
5044 		 * Scan the class/subclass descriptions for a general
5045 		 * description.
5046 		 */
5047 		cp = "unknown";
5048 		scp = NULL;
5049 		for (i = 0; pci_nomatch_tab[i].desc != NULL; i++) {
5050 			if (pci_nomatch_tab[i].class == pci_get_class(child)) {
5051 				if (pci_nomatch_tab[i].subclass == -1) {
5052 					cp = pci_nomatch_tab[i].desc;
5053 					report = pci_nomatch_tab[i].report;
5054 				} else if (pci_nomatch_tab[i].subclass ==
5055 				    pci_get_subclass(child)) {
5056 					scp = pci_nomatch_tab[i].desc;
5057 					report = pci_nomatch_tab[i].report;
5058 				}
5059 			}
5060 		}
5061 		if (report || bootverbose) {
5062 			device_printf(dev, "<%s%s%s>",
5063 			    cp ? cp : "",
5064 			    ((cp != NULL) && (scp != NULL)) ? ", " : "",
5065 			    scp ? scp : "");
5066 		}
5067 	}
5068 	if (report || bootverbose) {
5069 		printf(" at device %d.%d (no driver attached)\n",
5070 		    pci_get_slot(child), pci_get_function(child));
5071 	}
5072 	pci_cfg_save(child, device_get_ivars(child), 1);
5073 }
5074 
5075 void
pci_child_detached(device_t dev,device_t child)5076 pci_child_detached(device_t dev, device_t child)
5077 {
5078 	struct pci_devinfo *dinfo;
5079 	struct resource_list *rl;
5080 
5081 	dinfo = device_get_ivars(child);
5082 	rl = &dinfo->resources;
5083 
5084 	/*
5085 	 * Have to deallocate IRQs before releasing any MSI messages and
5086 	 * have to release MSI messages before deallocating any memory
5087 	 * BARs.
5088 	 */
5089 	if (resource_list_release_active(rl, dev, child, SYS_RES_IRQ) != 0)
5090 		pci_printf(&dinfo->cfg, "Device leaked IRQ resources\n");
5091 	if (dinfo->cfg.msi.msi_alloc != 0 || dinfo->cfg.msix.msix_alloc != 0) {
5092 		if (dinfo->cfg.msi.msi_alloc != 0)
5093 			pci_printf(&dinfo->cfg, "Device leaked %d MSI "
5094 			    "vectors\n", dinfo->cfg.msi.msi_alloc);
5095 		else
5096 			pci_printf(&dinfo->cfg, "Device leaked %d MSI-X "
5097 			    "vectors\n", dinfo->cfg.msix.msix_alloc);
5098 		(void)pci_release_msi(child);
5099 	}
5100 	if (resource_list_release_active(rl, dev, child, SYS_RES_MEMORY) != 0)
5101 		pci_printf(&dinfo->cfg, "Device leaked memory resources\n");
5102 	if (resource_list_release_active(rl, dev, child, SYS_RES_IOPORT) != 0)
5103 		pci_printf(&dinfo->cfg, "Device leaked I/O resources\n");
5104 #ifdef PCI_RES_BUS
5105 	if (resource_list_release_active(rl, dev, child, PCI_RES_BUS) != 0)
5106 		pci_printf(&dinfo->cfg, "Device leaked PCI bus numbers\n");
5107 #endif
5108 
5109 	pci_cfg_save(child, dinfo, 1);
5110 }
5111 
5112 /*
5113  * Parse the PCI device database, if loaded, and return a pointer to a
5114  * description of the device.
5115  *
5116  * The database is flat text formatted as follows:
5117  *
5118  * Any line not in a valid format is ignored.
5119  * Lines are terminated with newline '\n' characters.
5120  *
5121  * A VENDOR line consists of the 4 digit (hex) vendor code, a TAB, then
5122  * the vendor name.
5123  *
5124  * A DEVICE line is entered immediately below the corresponding VENDOR ID.
5125  * - devices cannot be listed without a corresponding VENDOR line.
5126  * A DEVICE line consists of a TAB, the 4 digit (hex) device code,
5127  * another TAB, then the device name.
5128  */
5129 
5130 /*
5131  * Assuming (ptr) points to the beginning of a line in the database,
5132  * return the vendor or device and description of the next entry.
5133  * The value of (vendor) or (device) inappropriate for the entry type
5134  * is set to -1.  Returns nonzero at the end of the database.
5135  *
5136  * Note that this is slightly unrobust in the face of corrupt data;
5137  * we attempt to safeguard against this by spamming the end of the
5138  * database with a newline when we initialise.
5139  */
5140 static int
pci_describe_parse_line(char ** ptr,int * vendor,int * device,char ** desc)5141 pci_describe_parse_line(char **ptr, int *vendor, int *device, char **desc)
5142 {
5143 	char	*cp = *ptr;
5144 	int	left;
5145 
5146 	*device = -1;
5147 	*vendor = -1;
5148 	**desc = '\0';
5149 	for (;;) {
5150 		left = pci_vendordata_size - (cp - pci_vendordata);
5151 		if (left <= 0) {
5152 			*ptr = cp;
5153 			return(1);
5154 		}
5155 
5156 		/* vendor entry? */
5157 		if (*cp != '\t' &&
5158 		    sscanf(cp, "%x\t%80[^\n]", vendor, *desc) == 2)
5159 			break;
5160 		/* device entry? */
5161 		if (*cp == '\t' &&
5162 		    sscanf(cp, "%x\t%80[^\n]", device, *desc) == 2)
5163 			break;
5164 
5165 		/* skip to next line */
5166 		while (*cp != '\n' && left > 0) {
5167 			cp++;
5168 			left--;
5169 		}
5170 		if (*cp == '\n') {
5171 			cp++;
5172 			left--;
5173 		}
5174 	}
5175 	/* skip to next line */
5176 	while (*cp != '\n' && left > 0) {
5177 		cp++;
5178 		left--;
5179 	}
5180 	if (*cp == '\n' && left > 0)
5181 		cp++;
5182 	*ptr = cp;
5183 	return(0);
5184 }
5185 
5186 static char *
pci_describe_device(device_t dev)5187 pci_describe_device(device_t dev)
5188 {
5189 	int	vendor, device;
5190 	char	*desc, *vp, *dp, *line;
5191 
5192 	desc = vp = dp = NULL;
5193 
5194 	/*
5195 	 * If we have no vendor data, we can't do anything.
5196 	 */
5197 	if (pci_vendordata == NULL)
5198 		goto out;
5199 
5200 	/*
5201 	 * Scan the vendor data looking for this device
5202 	 */
5203 	line = pci_vendordata;
5204 	if ((vp = malloc(80, M_DEVBUF, M_NOWAIT)) == NULL)
5205 		goto out;
5206 	for (;;) {
5207 		if (pci_describe_parse_line(&line, &vendor, &device, &vp))
5208 			goto out;
5209 		if (vendor == pci_get_vendor(dev))
5210 			break;
5211 	}
5212 	if ((dp = malloc(80, M_DEVBUF, M_NOWAIT)) == NULL)
5213 		goto out;
5214 	for (;;) {
5215 		if (pci_describe_parse_line(&line, &vendor, &device, &dp)) {
5216 			*dp = 0;
5217 			break;
5218 		}
5219 		if (vendor != -1) {
5220 			*dp = 0;
5221 			break;
5222 		}
5223 		if (device == pci_get_device(dev))
5224 			break;
5225 	}
5226 	if (dp[0] == '\0')
5227 		snprintf(dp, 80, "0x%x", pci_get_device(dev));
5228 	if ((desc = malloc(strlen(vp) + strlen(dp) + 3, M_DEVBUF, M_NOWAIT)) !=
5229 	    NULL)
5230 		sprintf(desc, "%s, %s", vp, dp);
5231 out:
5232 	if (vp != NULL)
5233 		free(vp, M_DEVBUF);
5234 	if (dp != NULL)
5235 		free(dp, M_DEVBUF);
5236 	return(desc);
5237 }
5238 
5239 int
pci_read_ivar(device_t dev,device_t child,int which,uintptr_t * result)5240 pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
5241 {
5242 	struct pci_devinfo *dinfo;
5243 	pcicfgregs *cfg;
5244 
5245 	dinfo = device_get_ivars(child);
5246 	cfg = &dinfo->cfg;
5247 
5248 	switch (which) {
5249 	case PCI_IVAR_ETHADDR:
5250 		/*
5251 		 * The generic accessor doesn't deal with failure, so
5252 		 * we set the return value, then return an error.
5253 		 */
5254 		*((uint8_t **) result) = NULL;
5255 		return (EINVAL);
5256 	case PCI_IVAR_SUBVENDOR:
5257 		*result = cfg->subvendor;
5258 		break;
5259 	case PCI_IVAR_SUBDEVICE:
5260 		*result = cfg->subdevice;
5261 		break;
5262 	case PCI_IVAR_VENDOR:
5263 		*result = cfg->vendor;
5264 		break;
5265 	case PCI_IVAR_DEVICE:
5266 		*result = cfg->device;
5267 		break;
5268 	case PCI_IVAR_DEVID:
5269 		*result = (cfg->device << 16) | cfg->vendor;
5270 		break;
5271 	case PCI_IVAR_CLASS:
5272 		*result = cfg->baseclass;
5273 		break;
5274 	case PCI_IVAR_SUBCLASS:
5275 		*result = cfg->subclass;
5276 		break;
5277 	case PCI_IVAR_PROGIF:
5278 		*result = cfg->progif;
5279 		break;
5280 	case PCI_IVAR_REVID:
5281 		*result = cfg->revid;
5282 		break;
5283 	case PCI_IVAR_INTPIN:
5284 		*result = cfg->intpin;
5285 		break;
5286 	case PCI_IVAR_IRQ:
5287 		*result = cfg->intline;
5288 		break;
5289 	case PCI_IVAR_DOMAIN:
5290 		*result = cfg->domain;
5291 		break;
5292 	case PCI_IVAR_BUS:
5293 		*result = cfg->bus;
5294 		break;
5295 	case PCI_IVAR_SLOT:
5296 		*result = cfg->slot;
5297 		break;
5298 	case PCI_IVAR_FUNCTION:
5299 		*result = cfg->func;
5300 		break;
5301 	case PCI_IVAR_CMDREG:
5302 		*result = cfg->cmdreg;
5303 		break;
5304 	case PCI_IVAR_CACHELNSZ:
5305 		*result = cfg->cachelnsz;
5306 		break;
5307 	case PCI_IVAR_MINGNT:
5308 		if (cfg->hdrtype != PCIM_HDRTYPE_NORMAL) {
5309 			*result = -1;
5310 			return (EINVAL);
5311 		}
5312 		*result = cfg->mingnt;
5313 		break;
5314 	case PCI_IVAR_MAXLAT:
5315 		if (cfg->hdrtype != PCIM_HDRTYPE_NORMAL) {
5316 			*result = -1;
5317 			return (EINVAL);
5318 		}
5319 		*result = cfg->maxlat;
5320 		break;
5321 	case PCI_IVAR_LATTIMER:
5322 		*result = cfg->lattimer;
5323 		break;
5324 	default:
5325 		return (ENOENT);
5326 	}
5327 	return (0);
5328 }
5329 
5330 int
pci_write_ivar(device_t dev,device_t child,int which,uintptr_t value)5331 pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
5332 {
5333 	struct pci_devinfo *dinfo;
5334 
5335 	dinfo = device_get_ivars(child);
5336 
5337 	switch (which) {
5338 	case PCI_IVAR_INTPIN:
5339 		dinfo->cfg.intpin = value;
5340 		return (0);
5341 	case PCI_IVAR_ETHADDR:
5342 	case PCI_IVAR_SUBVENDOR:
5343 	case PCI_IVAR_SUBDEVICE:
5344 	case PCI_IVAR_VENDOR:
5345 	case PCI_IVAR_DEVICE:
5346 	case PCI_IVAR_DEVID:
5347 	case PCI_IVAR_CLASS:
5348 	case PCI_IVAR_SUBCLASS:
5349 	case PCI_IVAR_PROGIF:
5350 	case PCI_IVAR_REVID:
5351 	case PCI_IVAR_IRQ:
5352 	case PCI_IVAR_DOMAIN:
5353 	case PCI_IVAR_BUS:
5354 	case PCI_IVAR_SLOT:
5355 	case PCI_IVAR_FUNCTION:
5356 		return (EINVAL);	/* disallow for now */
5357 
5358 	default:
5359 		return (ENOENT);
5360 	}
5361 }
5362 
5363 #include "opt_ddb.h"
5364 #ifdef DDB
5365 #include <ddb/ddb.h>
5366 #include <sys/cons.h>
5367 
5368 /*
5369  * List resources based on pci map registers, used for within ddb
5370  */
5371 
DB_SHOW_COMMAND(pciregs,db_pci_dump)5372 DB_SHOW_COMMAND(pciregs, db_pci_dump)
5373 {
5374 	struct pci_devinfo *dinfo;
5375 	struct devlist *devlist_head;
5376 	struct pci_conf *p;
5377 	const char *name;
5378 	int i, error, none_count;
5379 
5380 	none_count = 0;
5381 	/* get the head of the device queue */
5382 	devlist_head = &pci_devq;
5383 
5384 	/*
5385 	 * Go through the list of devices and print out devices
5386 	 */
5387 	for (error = 0, i = 0,
5388 	     dinfo = STAILQ_FIRST(devlist_head);
5389 	     (dinfo != NULL) && (error == 0) && (i < pci_numdevs) && !db_pager_quit;
5390 	     dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
5391 		/* Populate pd_name and pd_unit */
5392 		name = NULL;
5393 		if (dinfo->cfg.dev)
5394 			name = device_get_name(dinfo->cfg.dev);
5395 
5396 		p = &dinfo->conf;
5397 		db_printf("%s%d@pci%d:%d:%d:%d:\tclass=0x%06x card=0x%08x "
5398 			"chip=0x%08x rev=0x%02x hdr=0x%02x\n",
5399 			(name && *name) ? name : "none",
5400 			(name && *name) ? (int)device_get_unit(dinfo->cfg.dev) :
5401 			none_count++,
5402 			p->pc_sel.pc_domain, p->pc_sel.pc_bus, p->pc_sel.pc_dev,
5403 			p->pc_sel.pc_func, (p->pc_class << 16) |
5404 			(p->pc_subclass << 8) | p->pc_progif,
5405 			(p->pc_subdevice << 16) | p->pc_subvendor,
5406 			(p->pc_device << 16) | p->pc_vendor,
5407 			p->pc_revid, p->pc_hdr);
5408 	}
5409 }
5410 #endif /* DDB */
5411 
5412 struct resource *
pci_reserve_map(device_t dev,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int num,u_int flags)5413 pci_reserve_map(device_t dev, device_t child, int type, int *rid,
5414     rman_res_t start, rman_res_t end, rman_res_t count, u_int num,
5415     u_int flags)
5416 {
5417 	struct pci_devinfo *dinfo = device_get_ivars(child);
5418 	struct resource_list *rl = &dinfo->resources;
5419 	struct resource *res;
5420 	struct pci_map *pm;
5421 	uint16_t cmd;
5422 	pci_addr_t map, testval;
5423 	int mapsize;
5424 
5425 	res = NULL;
5426 
5427 	/* If rid is managed by EA, ignore it */
5428 	if (pci_ea_is_enabled(child, *rid))
5429 		goto out;
5430 
5431 	pm = pci_find_bar(child, *rid);
5432 	if (pm != NULL) {
5433 		/* This is a BAR that we failed to allocate earlier. */
5434 		mapsize = pm->pm_size;
5435 		map = pm->pm_value;
5436 	} else {
5437 		/*
5438 		 * Weed out the bogons, and figure out how large the
5439 		 * BAR/map is.  BARs that read back 0 here are bogus
5440 		 * and unimplemented.  Note: atapci in legacy mode are
5441 		 * special and handled elsewhere in the code.  If you
5442 		 * have a atapci device in legacy mode and it fails
5443 		 * here, that other code is broken.
5444 		 */
5445 		pci_read_bar(child, *rid, &map, &testval, NULL);
5446 
5447 		/*
5448 		 * Determine the size of the BAR and ignore BARs with a size
5449 		 * of 0.  Device ROM BARs use a different mask value.
5450 		 */
5451 		if (PCIR_IS_BIOS(&dinfo->cfg, *rid))
5452 			mapsize = pci_romsize(testval);
5453 		else
5454 			mapsize = pci_mapsize(testval);
5455 		if (mapsize == 0)
5456 			goto out;
5457 		pm = pci_add_bar(child, *rid, map, mapsize);
5458 	}
5459 
5460 	if (PCI_BAR_MEM(map) || PCIR_IS_BIOS(&dinfo->cfg, *rid)) {
5461 		if (type != SYS_RES_MEMORY) {
5462 			if (bootverbose)
5463 				device_printf(dev,
5464 				    "child %s requested type %d for rid %#x,"
5465 				    " but the BAR says it is an memio\n",
5466 				    device_get_nameunit(child), type, *rid);
5467 			goto out;
5468 		}
5469 	} else {
5470 		if (type != SYS_RES_IOPORT) {
5471 			if (bootverbose)
5472 				device_printf(dev,
5473 				    "child %s requested type %d for rid %#x,"
5474 				    " but the BAR says it is an ioport\n",
5475 				    device_get_nameunit(child), type, *rid);
5476 			goto out;
5477 		}
5478 	}
5479 
5480 	/*
5481 	 * For real BARs, we need to override the size that
5482 	 * the driver requests, because that's what the BAR
5483 	 * actually uses and we would otherwise have a
5484 	 * situation where we might allocate the excess to
5485 	 * another driver, which won't work.
5486 	 */
5487 	count = ((pci_addr_t)1 << mapsize) * num;
5488 	if (RF_ALIGNMENT(flags) < mapsize)
5489 		flags = (flags & ~RF_ALIGNMENT_MASK) | RF_ALIGNMENT_LOG2(mapsize);
5490 	if (PCI_BAR_MEM(map) && (map & PCIM_BAR_MEM_PREFETCH))
5491 		flags |= RF_PREFETCHABLE;
5492 
5493 	/*
5494 	 * Allocate enough resource, and then write back the
5495 	 * appropriate BAR for that resource.
5496 	 */
5497 	resource_list_add(rl, type, *rid, start, end, count);
5498 	res = resource_list_reserve(rl, dev, child, type, rid, start, end,
5499 	    count, flags & ~RF_ACTIVE);
5500 	if (res == NULL) {
5501 		resource_list_delete(rl, type, *rid);
5502 		device_printf(child,
5503 		    "%#jx bytes of rid %#x res %d failed (%#jx, %#jx).\n",
5504 		    count, *rid, type, start, end);
5505 		goto out;
5506 	}
5507 	if (bootverbose)
5508 		device_printf(child,
5509 		    "Lazy allocation of %#jx bytes rid %#x type %d at %#jx\n",
5510 		    count, *rid, type, rman_get_start(res));
5511 
5512 	/* Disable decoding via the CMD register before updating the BAR */
5513 	cmd = pci_read_config(child, PCIR_COMMAND, 2);
5514 	pci_write_config(child, PCIR_COMMAND,
5515 	    cmd & ~(PCI_BAR_MEM(map) ? PCIM_CMD_MEMEN : PCIM_CMD_PORTEN), 2);
5516 
5517 	map = rman_get_start(res);
5518 	pci_write_bar(child, pm, map);
5519 
5520 	/* Restore the original value of the CMD register */
5521 	pci_write_config(child, PCIR_COMMAND, cmd, 2);
5522 out:
5523 	return (res);
5524 }
5525 
5526 struct resource *
pci_alloc_multi_resource(device_t dev,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_long num,u_int flags)5527 pci_alloc_multi_resource(device_t dev, device_t child, int type, int *rid,
5528     rman_res_t start, rman_res_t end, rman_res_t count, u_long num,
5529     u_int flags)
5530 {
5531 	struct pci_devinfo *dinfo;
5532 	struct resource_list *rl;
5533 	struct resource_list_entry *rle;
5534 	struct resource *res;
5535 	pcicfgregs *cfg;
5536 
5537 	/*
5538 	 * Perform lazy resource allocation
5539 	 */
5540 	dinfo = device_get_ivars(child);
5541 	rl = &dinfo->resources;
5542 	cfg = &dinfo->cfg;
5543 	switch (type) {
5544 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
5545 	case PCI_RES_BUS:
5546 		return (pci_alloc_secbus(dev, child, rid, start, end, count,
5547 		    flags));
5548 #endif
5549 	case SYS_RES_IRQ:
5550 		/*
5551 		 * Can't alloc legacy interrupt once MSI messages have
5552 		 * been allocated.
5553 		 */
5554 		if (*rid == 0 && (cfg->msi.msi_alloc > 0 ||
5555 		    cfg->msix.msix_alloc > 0))
5556 			return (NULL);
5557 
5558 		/*
5559 		 * If the child device doesn't have an interrupt
5560 		 * routed and is deserving of an interrupt, try to
5561 		 * assign it one.
5562 		 */
5563 		if (*rid == 0 && !PCI_INTERRUPT_VALID(cfg->intline) &&
5564 		    (cfg->intpin != 0))
5565 			pci_assign_interrupt(dev, child, 0);
5566 		break;
5567 	case SYS_RES_IOPORT:
5568 	case SYS_RES_MEMORY:
5569 #ifdef NEW_PCIB
5570 		/*
5571 		 * PCI-PCI bridge I/O window resources are not BARs.
5572 		 * For those allocations just pass the request up the
5573 		 * tree.
5574 		 */
5575 		if (cfg->hdrtype == PCIM_HDRTYPE_BRIDGE) {
5576 			switch (*rid) {
5577 			case PCIR_IOBASEL_1:
5578 			case PCIR_MEMBASE_1:
5579 			case PCIR_PMBASEL_1:
5580 				/*
5581 				 * XXX: Should we bother creating a resource
5582 				 * list entry?
5583 				 */
5584 				return (bus_generic_alloc_resource(dev, child,
5585 				    type, rid, start, end, count, flags));
5586 			}
5587 		}
5588 #endif
5589 		/* Reserve resources for this BAR if needed. */
5590 		rle = resource_list_find(rl, type, *rid);
5591 		if (rle == NULL) {
5592 			res = pci_reserve_map(dev, child, type, rid, start, end,
5593 			    count, num, flags);
5594 			if (res == NULL)
5595 				return (NULL);
5596 		}
5597 	}
5598 	return (resource_list_alloc(rl, dev, child, type, rid,
5599 	    start, end, count, flags));
5600 }
5601 
5602 struct resource *
pci_alloc_resource(device_t dev,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)5603 pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
5604     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
5605 {
5606 #ifdef PCI_IOV
5607 	struct pci_devinfo *dinfo;
5608 #endif
5609 
5610 	if (device_get_parent(child) != dev)
5611 		return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
5612 		    type, rid, start, end, count, flags));
5613 
5614 #ifdef PCI_IOV
5615 	dinfo = device_get_ivars(child);
5616 	if (dinfo->cfg.flags & PCICFG_VF) {
5617 		switch (type) {
5618 		/* VFs can't have I/O BARs. */
5619 		case SYS_RES_IOPORT:
5620 			return (NULL);
5621 		case SYS_RES_MEMORY:
5622 			return (pci_vf_alloc_mem_resource(dev, child, rid,
5623 			    start, end, count, flags));
5624 		}
5625 
5626 		/* Fall through for other types of resource allocations. */
5627 	}
5628 #endif
5629 
5630 	return (pci_alloc_multi_resource(dev, child, type, rid, start, end,
5631 	    count, 1, flags));
5632 }
5633 
5634 int
pci_release_resource(device_t dev,device_t child,int type,int rid,struct resource * r)5635 pci_release_resource(device_t dev, device_t child, int type, int rid,
5636     struct resource *r)
5637 {
5638 	struct pci_devinfo *dinfo;
5639 	struct resource_list *rl;
5640 	pcicfgregs *cfg;
5641 
5642 	if (device_get_parent(child) != dev)
5643 		return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
5644 		    type, rid, r));
5645 
5646 	dinfo = device_get_ivars(child);
5647 	cfg = &dinfo->cfg;
5648 
5649 #ifdef PCI_IOV
5650 	if (dinfo->cfg.flags & PCICFG_VF) {
5651 		switch (type) {
5652 		/* VFs can't have I/O BARs. */
5653 		case SYS_RES_IOPORT:
5654 			return (EDOOFUS);
5655 		case SYS_RES_MEMORY:
5656 			return (pci_vf_release_mem_resource(dev, child, rid,
5657 			    r));
5658 		}
5659 
5660 		/* Fall through for other types of resource allocations. */
5661 	}
5662 #endif
5663 
5664 #ifdef NEW_PCIB
5665 	/*
5666 	 * PCI-PCI bridge I/O window resources are not BARs.  For
5667 	 * those allocations just pass the request up the tree.
5668 	 */
5669 	if (cfg->hdrtype == PCIM_HDRTYPE_BRIDGE &&
5670 	    (type == SYS_RES_IOPORT || type == SYS_RES_MEMORY)) {
5671 		switch (rid) {
5672 		case PCIR_IOBASEL_1:
5673 		case PCIR_MEMBASE_1:
5674 		case PCIR_PMBASEL_1:
5675 			return (bus_generic_release_resource(dev, child, type,
5676 			    rid, r));
5677 		}
5678 	}
5679 #endif
5680 
5681 	rl = &dinfo->resources;
5682 	return (resource_list_release(rl, dev, child, type, rid, r));
5683 }
5684 
5685 int
pci_activate_resource(device_t dev,device_t child,int type,int rid,struct resource * r)5686 pci_activate_resource(device_t dev, device_t child, int type, int rid,
5687     struct resource *r)
5688 {
5689 	struct pci_devinfo *dinfo;
5690 	int error;
5691 
5692 	error = bus_generic_activate_resource(dev, child, type, rid, r);
5693 	if (error)
5694 		return (error);
5695 
5696 	/* Enable decoding in the command register when activating BARs. */
5697 	if (device_get_parent(child) == dev) {
5698 		/* Device ROMs need their decoding explicitly enabled. */
5699 		dinfo = device_get_ivars(child);
5700 		if (type == SYS_RES_MEMORY && PCIR_IS_BIOS(&dinfo->cfg, rid))
5701 			pci_write_bar(child, pci_find_bar(child, rid),
5702 			    rman_get_start(r) | PCIM_BIOS_ENABLE);
5703 		switch (type) {
5704 		case SYS_RES_IOPORT:
5705 		case SYS_RES_MEMORY:
5706 			error = PCI_ENABLE_IO(dev, child, type);
5707 			break;
5708 		}
5709 	}
5710 	return (error);
5711 }
5712 
5713 int
pci_deactivate_resource(device_t dev,device_t child,int type,int rid,struct resource * r)5714 pci_deactivate_resource(device_t dev, device_t child, int type,
5715     int rid, struct resource *r)
5716 {
5717 	struct pci_devinfo *dinfo;
5718 	int error;
5719 
5720 	error = bus_generic_deactivate_resource(dev, child, type, rid, r);
5721 	if (error)
5722 		return (error);
5723 
5724 	/* Disable decoding for device ROMs. */
5725 	if (device_get_parent(child) == dev) {
5726 		dinfo = device_get_ivars(child);
5727 		if (type == SYS_RES_MEMORY && PCIR_IS_BIOS(&dinfo->cfg, rid))
5728 			pci_write_bar(child, pci_find_bar(child, rid),
5729 			    rman_get_start(r));
5730 	}
5731 	return (0);
5732 }
5733 
5734 void
pci_child_deleted(device_t dev,device_t child)5735 pci_child_deleted(device_t dev, device_t child)
5736 {
5737 	struct resource_list_entry *rle;
5738 	struct resource_list *rl;
5739 	struct pci_devinfo *dinfo;
5740 
5741 	dinfo = device_get_ivars(child);
5742 	rl = &dinfo->resources;
5743 
5744 	EVENTHANDLER_INVOKE(pci_delete_device, child);
5745 
5746 	/* Turn off access to resources we're about to free */
5747 	if (bus_child_present(child) != 0) {
5748 		pci_write_config(child, PCIR_COMMAND, pci_read_config(child,
5749 		    PCIR_COMMAND, 2) & ~(PCIM_CMD_MEMEN | PCIM_CMD_PORTEN), 2);
5750 
5751 		pci_disable_busmaster(child);
5752 	}
5753 
5754 	/* Free all allocated resources */
5755 	STAILQ_FOREACH(rle, rl, link) {
5756 		if (rle->res) {
5757 			if (rman_get_flags(rle->res) & RF_ACTIVE ||
5758 			    resource_list_busy(rl, rle->type, rle->rid)) {
5759 				pci_printf(&dinfo->cfg,
5760 				    "Resource still owned, oops. "
5761 				    "(type=%d, rid=%d, addr=%lx)\n",
5762 				    rle->type, rle->rid,
5763 				    rman_get_start(rle->res));
5764 				bus_release_resource(child, rle->type, rle->rid,
5765 				    rle->res);
5766 			}
5767 			resource_list_unreserve(rl, dev, child, rle->type,
5768 			    rle->rid);
5769 		}
5770 	}
5771 	resource_list_free(rl);
5772 
5773 	pci_freecfg(dinfo);
5774 }
5775 
5776 void
pci_delete_resource(device_t dev,device_t child,int type,int rid)5777 pci_delete_resource(device_t dev, device_t child, int type, int rid)
5778 {
5779 	struct pci_devinfo *dinfo;
5780 	struct resource_list *rl;
5781 	struct resource_list_entry *rle;
5782 
5783 	if (device_get_parent(child) != dev)
5784 		return;
5785 
5786 	dinfo = device_get_ivars(child);
5787 	rl = &dinfo->resources;
5788 	rle = resource_list_find(rl, type, rid);
5789 	if (rle == NULL)
5790 		return;
5791 
5792 	if (rle->res) {
5793 		if (rman_get_flags(rle->res) & RF_ACTIVE ||
5794 		    resource_list_busy(rl, type, rid)) {
5795 			device_printf(dev, "delete_resource: "
5796 			    "Resource still owned by child, oops. "
5797 			    "(type=%d, rid=%d, addr=%jx)\n",
5798 			    type, rid, rman_get_start(rle->res));
5799 			return;
5800 		}
5801 		resource_list_unreserve(rl, dev, child, type, rid);
5802 	}
5803 	resource_list_delete(rl, type, rid);
5804 }
5805 
5806 struct resource_list *
pci_get_resource_list(device_t dev,device_t child)5807 pci_get_resource_list (device_t dev, device_t child)
5808 {
5809 	struct pci_devinfo *dinfo = device_get_ivars(child);
5810 
5811 	return (&dinfo->resources);
5812 }
5813 
5814 #ifdef IOMMU
5815 bus_dma_tag_t
pci_get_dma_tag(device_t bus,device_t dev)5816 pci_get_dma_tag(device_t bus, device_t dev)
5817 {
5818 	bus_dma_tag_t tag;
5819 	struct pci_softc *sc;
5820 
5821 	if (device_get_parent(dev) == bus) {
5822 		/* try iommu and return if it works */
5823 		tag = iommu_get_dma_tag(bus, dev);
5824 	} else
5825 		tag = NULL;
5826 	if (tag == NULL) {
5827 		sc = device_get_softc(bus);
5828 		tag = sc->sc_dma_tag;
5829 	}
5830 	return (tag);
5831 }
5832 #else
5833 bus_dma_tag_t
pci_get_dma_tag(device_t bus,device_t dev)5834 pci_get_dma_tag(device_t bus, device_t dev)
5835 {
5836 	struct pci_softc *sc = device_get_softc(bus);
5837 
5838 	return (sc->sc_dma_tag);
5839 }
5840 #endif
5841 
5842 uint32_t
pci_read_config_method(device_t dev,device_t child,int reg,int width)5843 pci_read_config_method(device_t dev, device_t child, int reg, int width)
5844 {
5845 	struct pci_devinfo *dinfo = device_get_ivars(child);
5846 	pcicfgregs *cfg = &dinfo->cfg;
5847 
5848 #ifdef PCI_IOV
5849 	/*
5850 	 * SR-IOV VFs don't implement the VID or DID registers, so we have to
5851 	 * emulate them here.
5852 	 */
5853 	if (cfg->flags & PCICFG_VF) {
5854 		if (reg == PCIR_VENDOR) {
5855 			switch (width) {
5856 			case 4:
5857 				return (cfg->device << 16 | cfg->vendor);
5858 			case 2:
5859 				return (cfg->vendor);
5860 			case 1:
5861 				return (cfg->vendor & 0xff);
5862 			default:
5863 				return (0xffffffff);
5864 			}
5865 		} else if (reg == PCIR_DEVICE) {
5866 			switch (width) {
5867 			/* Note that an unaligned 4-byte read is an error. */
5868 			case 2:
5869 				return (cfg->device);
5870 			case 1:
5871 				return (cfg->device & 0xff);
5872 			default:
5873 				return (0xffffffff);
5874 			}
5875 		}
5876 	}
5877 #endif
5878 
5879 	return (PCIB_READ_CONFIG(device_get_parent(dev),
5880 	    cfg->bus, cfg->slot, cfg->func, reg, width));
5881 }
5882 
5883 void
pci_write_config_method(device_t dev,device_t child,int reg,uint32_t val,int width)5884 pci_write_config_method(device_t dev, device_t child, int reg,
5885     uint32_t val, int width)
5886 {
5887 	struct pci_devinfo *dinfo = device_get_ivars(child);
5888 	pcicfgregs *cfg = &dinfo->cfg;
5889 
5890 	PCIB_WRITE_CONFIG(device_get_parent(dev),
5891 	    cfg->bus, cfg->slot, cfg->func, reg, val, width);
5892 }
5893 
5894 int
pci_child_location_str_method(device_t dev,device_t child,char * buf,size_t buflen)5895 pci_child_location_str_method(device_t dev, device_t child, char *buf,
5896     size_t buflen)
5897 {
5898 
5899 	snprintf(buf, buflen, "slot=%d function=%d dbsf=pci%d:%d:%d:%d",
5900 	    pci_get_slot(child), pci_get_function(child), pci_get_domain(child),
5901 	    pci_get_bus(child), pci_get_slot(child), pci_get_function(child));
5902 	return (0);
5903 }
5904 
5905 int
pci_child_pnpinfo_str_method(device_t dev,device_t child,char * buf,size_t buflen)5906 pci_child_pnpinfo_str_method(device_t dev, device_t child, char *buf,
5907     size_t buflen)
5908 {
5909 	struct pci_devinfo *dinfo;
5910 	pcicfgregs *cfg;
5911 
5912 	dinfo = device_get_ivars(child);
5913 	cfg = &dinfo->cfg;
5914 	snprintf(buf, buflen, "vendor=0x%04x device=0x%04x subvendor=0x%04x "
5915 	    "subdevice=0x%04x class=0x%02x%02x%02x", cfg->vendor, cfg->device,
5916 	    cfg->subvendor, cfg->subdevice, cfg->baseclass, cfg->subclass,
5917 	    cfg->progif);
5918 	return (0);
5919 }
5920 
5921 int
pci_assign_interrupt_method(device_t dev,device_t child)5922 pci_assign_interrupt_method(device_t dev, device_t child)
5923 {
5924 	struct pci_devinfo *dinfo = device_get_ivars(child);
5925 	pcicfgregs *cfg = &dinfo->cfg;
5926 
5927 	return (PCIB_ROUTE_INTERRUPT(device_get_parent(dev), child,
5928 	    cfg->intpin));
5929 }
5930 
5931 static void
pci_lookup(void * arg,const char * name,device_t * dev)5932 pci_lookup(void *arg, const char *name, device_t *dev)
5933 {
5934 	long val;
5935 	char *end;
5936 	int domain, bus, slot, func;
5937 
5938 	if (*dev != NULL)
5939 		return;
5940 
5941 	/*
5942 	 * Accept pciconf-style selectors of either pciD:B:S:F or
5943 	 * pciB:S:F.  In the latter case, the domain is assumed to
5944 	 * be zero.
5945 	 */
5946 	if (strncmp(name, "pci", 3) != 0)
5947 		return;
5948 	val = strtol(name + 3, &end, 10);
5949 	if (val < 0 || val > INT_MAX || *end != ':')
5950 		return;
5951 	domain = val;
5952 	val = strtol(end + 1, &end, 10);
5953 	if (val < 0 || val > INT_MAX || *end != ':')
5954 		return;
5955 	bus = val;
5956 	val = strtol(end + 1, &end, 10);
5957 	if (val < 0 || val > INT_MAX)
5958 		return;
5959 	slot = val;
5960 	if (*end == ':') {
5961 		val = strtol(end + 1, &end, 10);
5962 		if (val < 0 || val > INT_MAX || *end != '\0')
5963 			return;
5964 		func = val;
5965 	} else if (*end == '\0') {
5966 		func = slot;
5967 		slot = bus;
5968 		bus = domain;
5969 		domain = 0;
5970 	} else
5971 		return;
5972 
5973 	if (domain > PCI_DOMAINMAX || bus > PCI_BUSMAX || slot > PCI_SLOTMAX ||
5974 	    func > PCIE_ARI_FUNCMAX || (slot != 0 && func > PCI_FUNCMAX))
5975 		return;
5976 
5977 	*dev = pci_find_dbsf(domain, bus, slot, func);
5978 }
5979 
5980 static int
pci_modevent(module_t mod,int what,void * arg)5981 pci_modevent(module_t mod, int what, void *arg)
5982 {
5983 	static struct cdev *pci_cdev;
5984 	static eventhandler_tag tag;
5985 
5986 	switch (what) {
5987 	case MOD_LOAD:
5988 		STAILQ_INIT(&pci_devq);
5989 		pci_generation = 0;
5990 		pci_cdev = make_dev(&pcicdev, 0, UID_ROOT, GID_WHEEL, 0644,
5991 		    "pci");
5992 		pci_load_vendor_data();
5993 		tag = EVENTHANDLER_REGISTER(dev_lookup, pci_lookup, NULL,
5994 		    1000);
5995 		break;
5996 
5997 	case MOD_UNLOAD:
5998 		if (tag != NULL)
5999 			EVENTHANDLER_DEREGISTER(dev_lookup, tag);
6000 		destroy_dev(pci_cdev);
6001 		break;
6002 	}
6003 
6004 	return (0);
6005 }
6006 
6007 static void
pci_cfg_restore_pcie(device_t dev,struct pci_devinfo * dinfo)6008 pci_cfg_restore_pcie(device_t dev, struct pci_devinfo *dinfo)
6009 {
6010 #define	WREG(n, v)	pci_write_config(dev, pos + (n), (v), 2)
6011 	struct pcicfg_pcie *cfg;
6012 	int version, pos;
6013 
6014 	cfg = &dinfo->cfg.pcie;
6015 	pos = cfg->pcie_location;
6016 
6017 	version = cfg->pcie_flags & PCIEM_FLAGS_VERSION;
6018 
6019 	WREG(PCIER_DEVICE_CTL, cfg->pcie_device_ctl);
6020 
6021 	if (version > 1 || cfg->pcie_type == PCIEM_TYPE_ROOT_PORT ||
6022 	    cfg->pcie_type == PCIEM_TYPE_ENDPOINT ||
6023 	    cfg->pcie_type == PCIEM_TYPE_LEGACY_ENDPOINT)
6024 		WREG(PCIER_LINK_CTL, cfg->pcie_link_ctl);
6025 
6026 	if (version > 1 || (cfg->pcie_type == PCIEM_TYPE_ROOT_PORT ||
6027 	    (cfg->pcie_type == PCIEM_TYPE_DOWNSTREAM_PORT &&
6028 	     (cfg->pcie_flags & PCIEM_FLAGS_SLOT))))
6029 		WREG(PCIER_SLOT_CTL, cfg->pcie_slot_ctl);
6030 
6031 	if (version > 1 || cfg->pcie_type == PCIEM_TYPE_ROOT_PORT ||
6032 	    cfg->pcie_type == PCIEM_TYPE_ROOT_EC)
6033 		WREG(PCIER_ROOT_CTL, cfg->pcie_root_ctl);
6034 
6035 	if (version > 1) {
6036 		WREG(PCIER_DEVICE_CTL2, cfg->pcie_device_ctl2);
6037 		WREG(PCIER_LINK_CTL2, cfg->pcie_link_ctl2);
6038 		WREG(PCIER_SLOT_CTL2, cfg->pcie_slot_ctl2);
6039 	}
6040 #undef WREG
6041 }
6042 
6043 static void
pci_cfg_restore_pcix(device_t dev,struct pci_devinfo * dinfo)6044 pci_cfg_restore_pcix(device_t dev, struct pci_devinfo *dinfo)
6045 {
6046 	pci_write_config(dev, dinfo->cfg.pcix.pcix_location + PCIXR_COMMAND,
6047 	    dinfo->cfg.pcix.pcix_command,  2);
6048 }
6049 
6050 void
pci_cfg_restore(device_t dev,struct pci_devinfo * dinfo)6051 pci_cfg_restore(device_t dev, struct pci_devinfo *dinfo)
6052 {
6053 
6054 	/*
6055 	 * Restore the device to full power mode.  We must do this
6056 	 * before we restore the registers because moving from D3 to
6057 	 * D0 will cause the chip's BARs and some other registers to
6058 	 * be reset to some unknown power on reset values.  Cut down
6059 	 * the noise on boot by doing nothing if we are already in
6060 	 * state D0.
6061 	 */
6062 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0)
6063 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
6064 	pci_write_config(dev, PCIR_INTLINE, dinfo->cfg.intline, 1);
6065 	pci_write_config(dev, PCIR_INTPIN, dinfo->cfg.intpin, 1);
6066 	pci_write_config(dev, PCIR_CACHELNSZ, dinfo->cfg.cachelnsz, 1);
6067 	pci_write_config(dev, PCIR_LATTIMER, dinfo->cfg.lattimer, 1);
6068 	pci_write_config(dev, PCIR_PROGIF, dinfo->cfg.progif, 1);
6069 	pci_write_config(dev, PCIR_REVID, dinfo->cfg.revid, 1);
6070 	switch (dinfo->cfg.hdrtype & PCIM_HDRTYPE) {
6071 	case PCIM_HDRTYPE_NORMAL:
6072 		pci_write_config(dev, PCIR_MINGNT, dinfo->cfg.mingnt, 1);
6073 		pci_write_config(dev, PCIR_MAXLAT, dinfo->cfg.maxlat, 1);
6074 		break;
6075 	case PCIM_HDRTYPE_BRIDGE:
6076 		pci_write_config(dev, PCIR_SECLAT_1,
6077 		    dinfo->cfg.bridge.br_seclat, 1);
6078 		pci_write_config(dev, PCIR_SUBBUS_1,
6079 		    dinfo->cfg.bridge.br_subbus, 1);
6080 		pci_write_config(dev, PCIR_SECBUS_1,
6081 		    dinfo->cfg.bridge.br_secbus, 1);
6082 		pci_write_config(dev, PCIR_PRIBUS_1,
6083 		    dinfo->cfg.bridge.br_pribus, 1);
6084 		pci_write_config(dev, PCIR_BRIDGECTL_1,
6085 		    dinfo->cfg.bridge.br_control, 2);
6086 		break;
6087 	case PCIM_HDRTYPE_CARDBUS:
6088 		pci_write_config(dev, PCIR_SECLAT_2,
6089 		    dinfo->cfg.bridge.br_seclat, 1);
6090 		pci_write_config(dev, PCIR_SUBBUS_2,
6091 		    dinfo->cfg.bridge.br_subbus, 1);
6092 		pci_write_config(dev, PCIR_SECBUS_2,
6093 		    dinfo->cfg.bridge.br_secbus, 1);
6094 		pci_write_config(dev, PCIR_PRIBUS_2,
6095 		    dinfo->cfg.bridge.br_pribus, 1);
6096 		pci_write_config(dev, PCIR_BRIDGECTL_2,
6097 		    dinfo->cfg.bridge.br_control, 2);
6098 		break;
6099 	}
6100 	pci_restore_bars(dev);
6101 
6102 	if ((dinfo->cfg.hdrtype & PCIM_HDRTYPE) != PCIM_HDRTYPE_BRIDGE)
6103 		pci_write_config(dev, PCIR_COMMAND, dinfo->cfg.cmdreg, 2);
6104 
6105 	/*
6106 	 * Restore extended capabilities for PCI-Express and PCI-X
6107 	 */
6108 	if (dinfo->cfg.pcie.pcie_location != 0)
6109 		pci_cfg_restore_pcie(dev, dinfo);
6110 	if (dinfo->cfg.pcix.pcix_location != 0)
6111 		pci_cfg_restore_pcix(dev, dinfo);
6112 
6113 	/* Restore MSI and MSI-X configurations if they are present. */
6114 	if (dinfo->cfg.msi.msi_location != 0)
6115 		pci_resume_msi(dev);
6116 	if (dinfo->cfg.msix.msix_location != 0)
6117 		pci_resume_msix(dev);
6118 
6119 #ifdef PCI_IOV
6120 	if (dinfo->cfg.iov != NULL)
6121 		pci_iov_cfg_restore(dev, dinfo);
6122 #endif
6123 }
6124 
6125 static void
pci_cfg_save_pcie(device_t dev,struct pci_devinfo * dinfo)6126 pci_cfg_save_pcie(device_t dev, struct pci_devinfo *dinfo)
6127 {
6128 #define	RREG(n)	pci_read_config(dev, pos + (n), 2)
6129 	struct pcicfg_pcie *cfg;
6130 	int version, pos;
6131 
6132 	cfg = &dinfo->cfg.pcie;
6133 	pos = cfg->pcie_location;
6134 
6135 	cfg->pcie_flags = RREG(PCIER_FLAGS);
6136 
6137 	version = cfg->pcie_flags & PCIEM_FLAGS_VERSION;
6138 
6139 	cfg->pcie_device_ctl = RREG(PCIER_DEVICE_CTL);
6140 
6141 	if (version > 1 || cfg->pcie_type == PCIEM_TYPE_ROOT_PORT ||
6142 	    cfg->pcie_type == PCIEM_TYPE_ENDPOINT ||
6143 	    cfg->pcie_type == PCIEM_TYPE_LEGACY_ENDPOINT)
6144 		cfg->pcie_link_ctl = RREG(PCIER_LINK_CTL);
6145 
6146 	if (version > 1 || (cfg->pcie_type == PCIEM_TYPE_ROOT_PORT ||
6147 	    (cfg->pcie_type == PCIEM_TYPE_DOWNSTREAM_PORT &&
6148 	     (cfg->pcie_flags & PCIEM_FLAGS_SLOT))))
6149 		cfg->pcie_slot_ctl = RREG(PCIER_SLOT_CTL);
6150 
6151 	if (version > 1 || cfg->pcie_type == PCIEM_TYPE_ROOT_PORT ||
6152 	    cfg->pcie_type == PCIEM_TYPE_ROOT_EC)
6153 		cfg->pcie_root_ctl = RREG(PCIER_ROOT_CTL);
6154 
6155 	if (version > 1) {
6156 		cfg->pcie_device_ctl2 = RREG(PCIER_DEVICE_CTL2);
6157 		cfg->pcie_link_ctl2 = RREG(PCIER_LINK_CTL2);
6158 		cfg->pcie_slot_ctl2 = RREG(PCIER_SLOT_CTL2);
6159 	}
6160 #undef RREG
6161 }
6162 
6163 static void
pci_cfg_save_pcix(device_t dev,struct pci_devinfo * dinfo)6164 pci_cfg_save_pcix(device_t dev, struct pci_devinfo *dinfo)
6165 {
6166 	dinfo->cfg.pcix.pcix_command = pci_read_config(dev,
6167 	    dinfo->cfg.pcix.pcix_location + PCIXR_COMMAND, 2);
6168 }
6169 
6170 void
pci_cfg_save(device_t dev,struct pci_devinfo * dinfo,int setstate)6171 pci_cfg_save(device_t dev, struct pci_devinfo *dinfo, int setstate)
6172 {
6173 	uint32_t cls;
6174 	int ps;
6175 
6176 	/*
6177 	 * Some drivers apparently write to these registers w/o updating our
6178 	 * cached copy.  No harm happens if we update the copy, so do so here
6179 	 * so we can restore them.  The COMMAND register is modified by the
6180 	 * bus w/o updating the cache.  This should represent the normally
6181 	 * writable portion of the 'defined' part of type 0/1/2 headers.
6182 	 */
6183 	dinfo->cfg.vendor = pci_read_config(dev, PCIR_VENDOR, 2);
6184 	dinfo->cfg.device = pci_read_config(dev, PCIR_DEVICE, 2);
6185 	dinfo->cfg.cmdreg = pci_read_config(dev, PCIR_COMMAND, 2);
6186 	dinfo->cfg.intline = pci_read_config(dev, PCIR_INTLINE, 1);
6187 	dinfo->cfg.intpin = pci_read_config(dev, PCIR_INTPIN, 1);
6188 	dinfo->cfg.cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
6189 	dinfo->cfg.lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
6190 	dinfo->cfg.baseclass = pci_read_config(dev, PCIR_CLASS, 1);
6191 	dinfo->cfg.subclass = pci_read_config(dev, PCIR_SUBCLASS, 1);
6192 	dinfo->cfg.progif = pci_read_config(dev, PCIR_PROGIF, 1);
6193 	dinfo->cfg.revid = pci_read_config(dev, PCIR_REVID, 1);
6194 	switch (dinfo->cfg.hdrtype & PCIM_HDRTYPE) {
6195 	case PCIM_HDRTYPE_NORMAL:
6196 		dinfo->cfg.subvendor = pci_read_config(dev, PCIR_SUBVEND_0, 2);
6197 		dinfo->cfg.subdevice = pci_read_config(dev, PCIR_SUBDEV_0, 2);
6198 		dinfo->cfg.mingnt = pci_read_config(dev, PCIR_MINGNT, 1);
6199 		dinfo->cfg.maxlat = pci_read_config(dev, PCIR_MAXLAT, 1);
6200 		break;
6201 	case PCIM_HDRTYPE_BRIDGE:
6202 		dinfo->cfg.bridge.br_seclat = pci_read_config(dev,
6203 		    PCIR_SECLAT_1, 1);
6204 		dinfo->cfg.bridge.br_subbus = pci_read_config(dev,
6205 		    PCIR_SUBBUS_1, 1);
6206 		dinfo->cfg.bridge.br_secbus = pci_read_config(dev,
6207 		    PCIR_SECBUS_1, 1);
6208 		dinfo->cfg.bridge.br_pribus = pci_read_config(dev,
6209 		    PCIR_PRIBUS_1, 1);
6210 		dinfo->cfg.bridge.br_control = pci_read_config(dev,
6211 		    PCIR_BRIDGECTL_1, 2);
6212 		break;
6213 	case PCIM_HDRTYPE_CARDBUS:
6214 		dinfo->cfg.bridge.br_seclat = pci_read_config(dev,
6215 		    PCIR_SECLAT_2, 1);
6216 		dinfo->cfg.bridge.br_subbus = pci_read_config(dev,
6217 		    PCIR_SUBBUS_2, 1);
6218 		dinfo->cfg.bridge.br_secbus = pci_read_config(dev,
6219 		    PCIR_SECBUS_2, 1);
6220 		dinfo->cfg.bridge.br_pribus = pci_read_config(dev,
6221 		    PCIR_PRIBUS_2, 1);
6222 		dinfo->cfg.bridge.br_control = pci_read_config(dev,
6223 		    PCIR_BRIDGECTL_2, 2);
6224 		dinfo->cfg.subvendor = pci_read_config(dev, PCIR_SUBVEND_2, 2);
6225 		dinfo->cfg.subdevice = pci_read_config(dev, PCIR_SUBDEV_2, 2);
6226 		break;
6227 	}
6228 
6229 	if (dinfo->cfg.pcie.pcie_location != 0)
6230 		pci_cfg_save_pcie(dev, dinfo);
6231 
6232 	if (dinfo->cfg.pcix.pcix_location != 0)
6233 		pci_cfg_save_pcix(dev, dinfo);
6234 
6235 #ifdef PCI_IOV
6236 	if (dinfo->cfg.iov != NULL)
6237 		pci_iov_cfg_save(dev, dinfo);
6238 #endif
6239 
6240 	/*
6241 	 * don't set the state for display devices, base peripherals and
6242 	 * memory devices since bad things happen when they are powered down.
6243 	 * We should (a) have drivers that can easily detach and (b) use
6244 	 * generic drivers for these devices so that some device actually
6245 	 * attaches.  We need to make sure that when we implement (a) we don't
6246 	 * power the device down on a reattach.
6247 	 */
6248 	cls = pci_get_class(dev);
6249 	if (!setstate)
6250 		return;
6251 	switch (pci_do_power_nodriver)
6252 	{
6253 		case 0:		/* NO powerdown at all */
6254 			return;
6255 		case 1:		/* Conservative about what to power down */
6256 			if (cls == PCIC_STORAGE)
6257 				return;
6258 			/*FALLTHROUGH*/
6259 		case 2:		/* Aggressive about what to power down */
6260 			if (cls == PCIC_DISPLAY || cls == PCIC_MEMORY ||
6261 			    cls == PCIC_BASEPERIPH)
6262 				return;
6263 			/*FALLTHROUGH*/
6264 		case 3:		/* Power down everything */
6265 			break;
6266 	}
6267 	/*
6268 	 * PCI spec says we can only go into D3 state from D0 state.
6269 	 * Transition from D[12] into D0 before going to D3 state.
6270 	 */
6271 	ps = pci_get_powerstate(dev);
6272 	if (ps != PCI_POWERSTATE_D0 && ps != PCI_POWERSTATE_D3)
6273 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
6274 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D3)
6275 		pci_set_powerstate(dev, PCI_POWERSTATE_D3);
6276 }
6277 
6278 /* Wrapper APIs suitable for device driver use. */
6279 void
pci_save_state(device_t dev)6280 pci_save_state(device_t dev)
6281 {
6282 	struct pci_devinfo *dinfo;
6283 
6284 	dinfo = device_get_ivars(dev);
6285 	pci_cfg_save(dev, dinfo, 0);
6286 }
6287 
6288 void
pci_restore_state(device_t dev)6289 pci_restore_state(device_t dev)
6290 {
6291 	struct pci_devinfo *dinfo;
6292 
6293 	dinfo = device_get_ivars(dev);
6294 	pci_cfg_restore(dev, dinfo);
6295 }
6296 
6297 static int
pci_get_id_method(device_t dev,device_t child,enum pci_id_type type,uintptr_t * id)6298 pci_get_id_method(device_t dev, device_t child, enum pci_id_type type,
6299     uintptr_t *id)
6300 {
6301 
6302 	return (PCIB_GET_ID(device_get_parent(dev), child, type, id));
6303 }
6304 
6305 /* Find the upstream port of a given PCI device in a root complex. */
6306 device_t
pci_find_pcie_root_port(device_t dev)6307 pci_find_pcie_root_port(device_t dev)
6308 {
6309 	struct pci_devinfo *dinfo;
6310 	devclass_t pci_class;
6311 	device_t pcib, bus;
6312 
6313 	pci_class = devclass_find("pci");
6314 	KASSERT(device_get_devclass(device_get_parent(dev)) == pci_class,
6315 	    ("%s: non-pci device %s", __func__, device_get_nameunit(dev)));
6316 
6317 	/*
6318 	 * Walk the bridge hierarchy until we find a PCI-e root
6319 	 * port or a non-PCI device.
6320 	 */
6321 	for (;;) {
6322 		bus = device_get_parent(dev);
6323 		KASSERT(bus != NULL, ("%s: null parent of %s", __func__,
6324 		    device_get_nameunit(dev)));
6325 
6326 		pcib = device_get_parent(bus);
6327 		KASSERT(pcib != NULL, ("%s: null bridge of %s", __func__,
6328 		    device_get_nameunit(bus)));
6329 
6330 		/*
6331 		 * pcib's parent must be a PCI bus for this to be a
6332 		 * PCI-PCI bridge.
6333 		 */
6334 		if (device_get_devclass(device_get_parent(pcib)) != pci_class)
6335 			return (NULL);
6336 
6337 		dinfo = device_get_ivars(pcib);
6338 		if (dinfo->cfg.pcie.pcie_location != 0 &&
6339 		    dinfo->cfg.pcie.pcie_type == PCIEM_TYPE_ROOT_PORT)
6340 			return (pcib);
6341 
6342 		dev = pcib;
6343 	}
6344 }
6345 
6346 /*
6347  * Wait for pending transactions to complete on a PCI-express function.
6348  *
6349  * The maximum delay is specified in milliseconds in max_delay.  Note
6350  * that this function may sleep.
6351  *
6352  * Returns true if the function is idle and false if the timeout is
6353  * exceeded.  If dev is not a PCI-express function, this returns true.
6354  */
6355 bool
pcie_wait_for_pending_transactions(device_t dev,u_int max_delay)6356 pcie_wait_for_pending_transactions(device_t dev, u_int max_delay)
6357 {
6358 	struct pci_devinfo *dinfo = device_get_ivars(dev);
6359 	uint16_t sta;
6360 	int cap;
6361 
6362 	cap = dinfo->cfg.pcie.pcie_location;
6363 	if (cap == 0)
6364 		return (true);
6365 
6366 	sta = pci_read_config(dev, cap + PCIER_DEVICE_STA, 2);
6367 	while (sta & PCIEM_STA_TRANSACTION_PND) {
6368 		if (max_delay == 0)
6369 			return (false);
6370 
6371 		/* Poll once every 100 milliseconds up to the timeout. */
6372 		if (max_delay > 100) {
6373 			pause_sbt("pcietp", 100 * SBT_1MS, 0, C_HARDCLOCK);
6374 			max_delay -= 100;
6375 		} else {
6376 			pause_sbt("pcietp", max_delay * SBT_1MS, 0,
6377 			    C_HARDCLOCK);
6378 			max_delay = 0;
6379 		}
6380 		sta = pci_read_config(dev, cap + PCIER_DEVICE_STA, 2);
6381 	}
6382 
6383 	return (true);
6384 }
6385 
6386 /*
6387  * Determine the maximum Completion Timeout in microseconds.
6388  *
6389  * For non-PCI-express functions this returns 0.
6390  */
6391 int
pcie_get_max_completion_timeout(device_t dev)6392 pcie_get_max_completion_timeout(device_t dev)
6393 {
6394 	struct pci_devinfo *dinfo = device_get_ivars(dev);
6395 	int cap;
6396 
6397 	cap = dinfo->cfg.pcie.pcie_location;
6398 	if (cap == 0)
6399 		return (0);
6400 
6401 	/*
6402 	 * Functions using the 1.x spec use the default timeout range of
6403 	 * 50 microseconds to 50 milliseconds.  Functions that do not
6404 	 * support programmable timeouts also use this range.
6405 	 */
6406 	if ((dinfo->cfg.pcie.pcie_flags & PCIEM_FLAGS_VERSION) < 2 ||
6407 	    (pci_read_config(dev, cap + PCIER_DEVICE_CAP2, 4) &
6408 	    PCIEM_CAP2_COMP_TIMO_RANGES) == 0)
6409 		return (50 * 1000);
6410 
6411 	switch (pci_read_config(dev, cap + PCIER_DEVICE_CTL2, 2) &
6412 	    PCIEM_CTL2_COMP_TIMO_VAL) {
6413 	case PCIEM_CTL2_COMP_TIMO_100US:
6414 		return (100);
6415 	case PCIEM_CTL2_COMP_TIMO_10MS:
6416 		return (10 * 1000);
6417 	case PCIEM_CTL2_COMP_TIMO_55MS:
6418 		return (55 * 1000);
6419 	case PCIEM_CTL2_COMP_TIMO_210MS:
6420 		return (210 * 1000);
6421 	case PCIEM_CTL2_COMP_TIMO_900MS:
6422 		return (900 * 1000);
6423 	case PCIEM_CTL2_COMP_TIMO_3500MS:
6424 		return (3500 * 1000);
6425 	case PCIEM_CTL2_COMP_TIMO_13S:
6426 		return (13 * 1000 * 1000);
6427 	case PCIEM_CTL2_COMP_TIMO_64S:
6428 		return (64 * 1000 * 1000);
6429 	default:
6430 		return (50 * 1000);
6431 	}
6432 }
6433 
6434 void
pcie_apei_error(device_t dev,int sev,uint8_t * aerp)6435 pcie_apei_error(device_t dev, int sev, uint8_t *aerp)
6436 {
6437 	struct pci_devinfo *dinfo = device_get_ivars(dev);
6438 	const char *s;
6439 	int aer;
6440 	uint32_t r, r1;
6441 	uint16_t rs;
6442 
6443 	if (sev == PCIEM_STA_CORRECTABLE_ERROR)
6444 		s = "Correctable";
6445 	else if (sev == PCIEM_STA_NON_FATAL_ERROR)
6446 		s = "Uncorrectable (Non-Fatal)";
6447 	else
6448 		s = "Uncorrectable (Fatal)";
6449 	device_printf(dev, "%s PCIe error reported by APEI\n", s);
6450 	if (aerp) {
6451 		if (sev == PCIEM_STA_CORRECTABLE_ERROR) {
6452 			r = le32dec(aerp + PCIR_AER_COR_STATUS);
6453 			r1 = le32dec(aerp + PCIR_AER_COR_MASK);
6454 		} else {
6455 			r = le32dec(aerp + PCIR_AER_UC_STATUS);
6456 			r1 = le32dec(aerp + PCIR_AER_UC_MASK);
6457 		}
6458 		device_printf(dev, "status 0x%08x mask 0x%08x", r, r1);
6459 		if (sev != PCIEM_STA_CORRECTABLE_ERROR) {
6460 			r = le32dec(aerp + PCIR_AER_UC_SEVERITY);
6461 			rs = le16dec(aerp + PCIR_AER_CAP_CONTROL);
6462 			printf(" severity 0x%08x first %d\n",
6463 			    r, rs & 0x1f);
6464 		} else
6465 			printf("\n");
6466 	}
6467 
6468 	/* As kind of recovery just report and clear the error statuses. */
6469 	if (pci_find_extcap(dev, PCIZ_AER, &aer) == 0) {
6470 		r = pci_read_config(dev, aer + PCIR_AER_UC_STATUS, 4);
6471 		if (r != 0) {
6472 			pci_write_config(dev, aer + PCIR_AER_UC_STATUS, r, 4);
6473 			device_printf(dev, "Clearing UC AER errors 0x%08x\n", r);
6474 		}
6475 
6476 		r = pci_read_config(dev, aer + PCIR_AER_COR_STATUS, 4);
6477 		if (r != 0) {
6478 			pci_write_config(dev, aer + PCIR_AER_COR_STATUS, r, 4);
6479 			device_printf(dev, "Clearing COR AER errors 0x%08x\n", r);
6480 		}
6481 	}
6482 	if (dinfo->cfg.pcie.pcie_location != 0) {
6483 		rs = pci_read_config(dev, dinfo->cfg.pcie.pcie_location +
6484 		    PCIER_DEVICE_STA, 2);
6485 		if ((rs & (PCIEM_STA_CORRECTABLE_ERROR |
6486 		    PCIEM_STA_NON_FATAL_ERROR | PCIEM_STA_FATAL_ERROR |
6487 		    PCIEM_STA_UNSUPPORTED_REQ)) != 0) {
6488 			pci_write_config(dev, dinfo->cfg.pcie.pcie_location +
6489 			    PCIER_DEVICE_STA, rs, 2);
6490 			device_printf(dev, "Clearing PCIe errors 0x%04x\n", rs);
6491 		}
6492 	}
6493 }
6494 
6495 /*
6496  * Perform a Function Level Reset (FLR) on a device.
6497  *
6498  * This function first waits for any pending transactions to complete
6499  * within the timeout specified by max_delay.  If transactions are
6500  * still pending, the function will return false without attempting a
6501  * reset.
6502  *
6503  * If dev is not a PCI-express function or does not support FLR, this
6504  * function returns false.
6505  *
6506  * Note that no registers are saved or restored.  The caller is
6507  * responsible for saving and restoring any registers including
6508  * PCI-standard registers via pci_save_state() and
6509  * pci_restore_state().
6510  */
6511 bool
pcie_flr(device_t dev,u_int max_delay,bool force)6512 pcie_flr(device_t dev, u_int max_delay, bool force)
6513 {
6514 	struct pci_devinfo *dinfo = device_get_ivars(dev);
6515 	uint16_t cmd, ctl;
6516 	int compl_delay;
6517 	int cap;
6518 
6519 	cap = dinfo->cfg.pcie.pcie_location;
6520 	if (cap == 0)
6521 		return (false);
6522 
6523 	if (!(pci_read_config(dev, cap + PCIER_DEVICE_CAP, 4) & PCIEM_CAP_FLR))
6524 		return (false);
6525 
6526 	/*
6527 	 * Disable busmastering to prevent generation of new
6528 	 * transactions while waiting for the device to go idle.  If
6529 	 * the idle timeout fails, the command register is restored
6530 	 * which will re-enable busmastering.
6531 	 */
6532 	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
6533 	pci_write_config(dev, PCIR_COMMAND, cmd & ~(PCIM_CMD_BUSMASTEREN), 2);
6534 	if (!pcie_wait_for_pending_transactions(dev, max_delay)) {
6535 		if (!force) {
6536 			pci_write_config(dev, PCIR_COMMAND, cmd, 2);
6537 			return (false);
6538 		}
6539 		pci_printf(&dinfo->cfg,
6540 		    "Resetting with transactions pending after %d ms\n",
6541 		    max_delay);
6542 
6543 		/*
6544 		 * Extend the post-FLR delay to cover the maximum
6545 		 * Completion Timeout delay of anything in flight
6546 		 * during the FLR delay.  Enforce a minimum delay of
6547 		 * at least 10ms.
6548 		 */
6549 		compl_delay = pcie_get_max_completion_timeout(dev) / 1000;
6550 		if (compl_delay < 10)
6551 			compl_delay = 10;
6552 	} else
6553 		compl_delay = 0;
6554 
6555 	/* Initiate the reset. */
6556 	ctl = pci_read_config(dev, cap + PCIER_DEVICE_CTL, 2);
6557 	pci_write_config(dev, cap + PCIER_DEVICE_CTL, ctl |
6558 	    PCIEM_CTL_INITIATE_FLR, 2);
6559 
6560 	/* Wait for 100ms. */
6561 	pause_sbt("pcieflr", (100 + compl_delay) * SBT_1MS, 0, C_HARDCLOCK);
6562 
6563 	if (pci_read_config(dev, cap + PCIER_DEVICE_STA, 2) &
6564 	    PCIEM_STA_TRANSACTION_PND)
6565 		pci_printf(&dinfo->cfg, "Transactions pending after FLR!\n");
6566 	return (true);
6567 }
6568 
6569 /*
6570  * Attempt a power-management reset by cycling the device in/out of D3
6571  * state.  PCI spec says we can only go into D3 state from D0 state.
6572  * Transition from D[12] into D0 before going to D3 state.
6573  */
6574 int
pci_power_reset(device_t dev)6575 pci_power_reset(device_t dev)
6576 {
6577 	int ps;
6578 
6579 	ps = pci_get_powerstate(dev);
6580 	if (ps != PCI_POWERSTATE_D0 && ps != PCI_POWERSTATE_D3)
6581 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
6582 	pci_set_powerstate(dev, PCI_POWERSTATE_D3);
6583 	pci_set_powerstate(dev, ps);
6584 	return (0);
6585 }
6586 
6587 /*
6588  * Try link drop and retrain of the downstream port of upstream
6589  * switch, for PCIe.  According to the PCIe 3.0 spec 6.6.1, this must
6590  * cause Conventional Hot reset of the device in the slot.
6591  * Alternative, for PCIe, could be the secondary bus reset initiatied
6592  * on the upstream switch PCIR_BRIDGECTL_1, bit 6.
6593  */
6594 int
pcie_link_reset(device_t port,int pcie_location)6595 pcie_link_reset(device_t port, int pcie_location)
6596 {
6597 	uint16_t v;
6598 
6599 	v = pci_read_config(port, pcie_location + PCIER_LINK_CTL, 2);
6600 	v |= PCIEM_LINK_CTL_LINK_DIS;
6601 	pci_write_config(port, pcie_location + PCIER_LINK_CTL, v, 2);
6602 	pause_sbt("pcier1", mstosbt(20), 0, 0);
6603 	v &= ~PCIEM_LINK_CTL_LINK_DIS;
6604 	v |= PCIEM_LINK_CTL_RETRAIN_LINK;
6605 	pci_write_config(port, pcie_location + PCIER_LINK_CTL, v, 2);
6606 	pause_sbt("pcier2", mstosbt(100), 0, 0); /* 100 ms */
6607 	v = pci_read_config(port, pcie_location + PCIER_LINK_STA, 2);
6608 	return ((v & PCIEM_LINK_STA_TRAINING) != 0 ? ETIMEDOUT : 0);
6609 }
6610 
6611 static int
pci_reset_post(device_t dev,device_t child)6612 pci_reset_post(device_t dev, device_t child)
6613 {
6614 
6615 	if (dev == device_get_parent(child))
6616 		pci_restore_state(child);
6617 	return (0);
6618 }
6619 
6620 static int
pci_reset_prepare(device_t dev,device_t child)6621 pci_reset_prepare(device_t dev, device_t child)
6622 {
6623 
6624 	if (dev == device_get_parent(child))
6625 		pci_save_state(child);
6626 	return (0);
6627 }
6628 
6629 static int
pci_reset_child(device_t dev,device_t child,int flags)6630 pci_reset_child(device_t dev, device_t child, int flags)
6631 {
6632 	int error;
6633 
6634 	if (dev == NULL || device_get_parent(child) != dev)
6635 		return (0);
6636 	if ((flags & DEVF_RESET_DETACH) != 0) {
6637 		error = device_get_state(child) == DS_ATTACHED ?
6638 		    device_detach(child) : 0;
6639 	} else {
6640 		error = BUS_SUSPEND_CHILD(dev, child);
6641 	}
6642 	if (error == 0) {
6643 		if (!pcie_flr(child, 1000, false)) {
6644 			error = BUS_RESET_PREPARE(dev, child);
6645 			if (error == 0)
6646 				pci_power_reset(child);
6647 			BUS_RESET_POST(dev, child);
6648 		}
6649 		if ((flags & DEVF_RESET_DETACH) != 0)
6650 			device_probe_and_attach(child);
6651 		else
6652 			BUS_RESUME_CHILD(dev, child);
6653 	}
6654 	return (error);
6655 }
6656 
6657 const struct pci_device_table *
pci_match_device(device_t child,const struct pci_device_table * id,size_t nelt)6658 pci_match_device(device_t child, const struct pci_device_table *id, size_t nelt)
6659 {
6660 	bool match;
6661 	uint16_t vendor, device, subvendor, subdevice, class, subclass, revid;
6662 
6663 	vendor = pci_get_vendor(child);
6664 	device = pci_get_device(child);
6665 	subvendor = pci_get_subvendor(child);
6666 	subdevice = pci_get_subdevice(child);
6667 	class = pci_get_class(child);
6668 	subclass = pci_get_subclass(child);
6669 	revid = pci_get_revid(child);
6670 	while (nelt-- > 0) {
6671 		match = true;
6672 		if (id->match_flag_vendor)
6673 			match &= vendor == id->vendor;
6674 		if (id->match_flag_device)
6675 			match &= device == id->device;
6676 		if (id->match_flag_subvendor)
6677 			match &= subvendor == id->subvendor;
6678 		if (id->match_flag_subdevice)
6679 			match &= subdevice == id->subdevice;
6680 		if (id->match_flag_class)
6681 			match &= class == id->class_id;
6682 		if (id->match_flag_subclass)
6683 			match &= subclass == id->subclass;
6684 		if (id->match_flag_revid)
6685 			match &= revid == id->revid;
6686 		if (match)
6687 			return (id);
6688 		id++;
6689 	}
6690 	return (NULL);
6691 }
6692 
6693 static void
pci_print_faulted_dev_name(const struct pci_devinfo * dinfo)6694 pci_print_faulted_dev_name(const struct pci_devinfo *dinfo)
6695 {
6696 	const char *dev_name;
6697 	device_t dev;
6698 
6699 	dev = dinfo->cfg.dev;
6700 	printf("pci%d:%d:%d:%d", dinfo->cfg.domain, dinfo->cfg.bus,
6701 	    dinfo->cfg.slot, dinfo->cfg.func);
6702 	dev_name = device_get_name(dev);
6703 	if (dev_name != NULL)
6704 		printf(" (%s%d)", dev_name, device_get_unit(dev));
6705 }
6706 
6707 void
pci_print_faulted_dev(void)6708 pci_print_faulted_dev(void)
6709 {
6710 	struct pci_devinfo *dinfo;
6711 	device_t dev;
6712 	int aer, i;
6713 	uint32_t r1, r2;
6714 	uint16_t status;
6715 
6716 	STAILQ_FOREACH(dinfo, &pci_devq, pci_links) {
6717 		dev = dinfo->cfg.dev;
6718 		status = pci_read_config(dev, PCIR_STATUS, 2);
6719 		status &= PCIM_STATUS_MDPERR | PCIM_STATUS_STABORT |
6720 		    PCIM_STATUS_RTABORT | PCIM_STATUS_RMABORT |
6721 		    PCIM_STATUS_SERR | PCIM_STATUS_PERR;
6722 		if (status != 0) {
6723 			pci_print_faulted_dev_name(dinfo);
6724 			printf(" error 0x%04x\n", status);
6725 		}
6726 		if (dinfo->cfg.pcie.pcie_location != 0) {
6727 			status = pci_read_config(dev,
6728 			    dinfo->cfg.pcie.pcie_location +
6729 			    PCIER_DEVICE_STA, 2);
6730 			if ((status & (PCIEM_STA_CORRECTABLE_ERROR |
6731 			    PCIEM_STA_NON_FATAL_ERROR | PCIEM_STA_FATAL_ERROR |
6732 			    PCIEM_STA_UNSUPPORTED_REQ)) != 0) {
6733 				pci_print_faulted_dev_name(dinfo);
6734 				printf(" PCIe DEVCTL 0x%04x DEVSTA 0x%04x\n",
6735 				    pci_read_config(dev,
6736 				    dinfo->cfg.pcie.pcie_location +
6737 				    PCIER_DEVICE_CTL, 2),
6738 				    status);
6739 			}
6740 		}
6741 		if (pci_find_extcap(dev, PCIZ_AER, &aer) == 0) {
6742 			r1 = pci_read_config(dev, aer + PCIR_AER_UC_STATUS, 4);
6743 			r2 = pci_read_config(dev, aer + PCIR_AER_COR_STATUS, 4);
6744 			if (r1 != 0 || r2 != 0) {
6745 				pci_print_faulted_dev_name(dinfo);
6746 				printf(" AER UC 0x%08x Mask 0x%08x Svr 0x%08x\n"
6747 				    "  COR 0x%08x Mask 0x%08x Ctl 0x%08x\n",
6748 				    r1, pci_read_config(dev, aer +
6749 				    PCIR_AER_UC_MASK, 4),
6750 				    pci_read_config(dev, aer +
6751 				    PCIR_AER_UC_SEVERITY, 4),
6752 				    r2, pci_read_config(dev, aer +
6753 				    PCIR_AER_COR_MASK, 4),
6754 				    pci_read_config(dev, aer +
6755 				    PCIR_AER_CAP_CONTROL, 4));
6756 				for (i = 0; i < 4; i++) {
6757 					r1 = pci_read_config(dev, aer +
6758 					    PCIR_AER_HEADER_LOG + i * 4, 4);
6759 					printf("    HL%d: 0x%08x\n", i, r1);
6760 				}
6761 			}
6762 		}
6763 	}
6764 }
6765 
6766 #ifdef DDB
DB_SHOW_COMMAND(pcierr,pci_print_faulted_dev_db)6767 DB_SHOW_COMMAND(pcierr, pci_print_faulted_dev_db)
6768 {
6769 
6770 	pci_print_faulted_dev();
6771 }
6772 
6773 static void
db_clear_pcie_errors(const struct pci_devinfo * dinfo)6774 db_clear_pcie_errors(const struct pci_devinfo *dinfo)
6775 {
6776 	device_t dev;
6777 	int aer;
6778 	uint32_t r;
6779 
6780 	dev = dinfo->cfg.dev;
6781 	r = pci_read_config(dev, dinfo->cfg.pcie.pcie_location +
6782 	    PCIER_DEVICE_STA, 2);
6783 	pci_write_config(dev, dinfo->cfg.pcie.pcie_location +
6784 	    PCIER_DEVICE_STA, r, 2);
6785 
6786 	if (pci_find_extcap(dev, PCIZ_AER, &aer) != 0)
6787 		return;
6788 	r = pci_read_config(dev, aer + PCIR_AER_UC_STATUS, 4);
6789 	if (r != 0)
6790 		pci_write_config(dev, aer + PCIR_AER_UC_STATUS, r, 4);
6791 	r = pci_read_config(dev, aer + PCIR_AER_COR_STATUS, 4);
6792 	if (r != 0)
6793 		pci_write_config(dev, aer + PCIR_AER_COR_STATUS, r, 4);
6794 }
6795 
DB_COMMAND(pci_clearerr,db_pci_clearerr)6796 DB_COMMAND(pci_clearerr, db_pci_clearerr)
6797 {
6798 	struct pci_devinfo *dinfo;
6799 	device_t dev;
6800 	uint16_t status, status1;
6801 
6802 	STAILQ_FOREACH(dinfo, &pci_devq, pci_links) {
6803 		dev = dinfo->cfg.dev;
6804 		status1 = status = pci_read_config(dev, PCIR_STATUS, 2);
6805 		status1 &= PCIM_STATUS_MDPERR | PCIM_STATUS_STABORT |
6806 		    PCIM_STATUS_RTABORT | PCIM_STATUS_RMABORT |
6807 		    PCIM_STATUS_SERR | PCIM_STATUS_PERR;
6808 		if (status1 != 0) {
6809 			status &= ~status1;
6810 			pci_write_config(dev, PCIR_STATUS, status, 2);
6811 		}
6812 		if (dinfo->cfg.pcie.pcie_location != 0)
6813 			db_clear_pcie_errors(dinfo);
6814 	}
6815 }
6816 #endif
6817