xref: /freebsd/sys/compat/linuxkpi/common/include/linux/pci.h (revision 33595d4ae01c10c436a83e7f5afd2c07a7fbd2c5)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  * Copyright (c) 2020-2025 The FreeBSD Foundation
8  *
9  * Portions of this software were developed by Björn Zeeb
10  * under sponsorship from the FreeBSD Foundation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice unmodified, this list of conditions, and the following
17  *    disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #ifndef	_LINUXKPI_LINUX_PCI_H_
34 #define	_LINUXKPI_LINUX_PCI_H_
35 
36 #define	CONFIG_PCI_MSI
37 
38 #include <linux/types.h>
39 #include <linux/device/driver.h>
40 
41 #include <sys/param.h>
42 #include <sys/bus.h>
43 #include <sys/module.h>
44 #include <sys/nv.h>
45 #include <sys/pciio.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pci_private.h>
49 
50 #include <machine/resource.h>
51 
52 #include <linux/list.h>
53 #include <linux/dmapool.h>
54 #include <linux/dma-mapping.h>
55 #include <linux/compiler.h>
56 #include <linux/errno.h>
57 #include <asm/atomic.h>
58 #include <asm/memtype.h>
59 #include <linux/device.h>
60 #include <linux/pci_ids.h>
61 #include <linux/pm.h>
62 
63 /*
64  * <linux/ioport.h> should be included here, like Linux, but we can't have that
65  * because Linux `struct resource` definition would conflict with FreeBSD
66  * native definition.
67  *
68  * At least the amdgpu DRM driver (amdgpu_isp.c at the time of this writing)
69  * relies on this indirect include to get the definition of Linux `struct
70  * resource`. As a workaround, we include <linux/ioport.h> from
71  * <linux/mfd/core.h>.
72  */
73 
74 #include <linux/kernel.h>	/* pr_debug */
75 
76 struct pci_device_id {
77 	uint32_t	vendor;
78 	uint32_t	device;
79 	uint32_t	subvendor;
80 	uint32_t	subdevice;
81 	uint32_t	class;
82 	uint32_t	class_mask;
83 	uintptr_t	driver_data;
84 };
85 
86 #define	MODULE_DEVICE_TABLE_BUS_pci(_bus, _table)			\
87 MODULE_PNP_INFO("U32:vendor;U32:device;V32:subvendor;V32:subdevice",	\
88     _bus, lkpi_ ## _table, _table, nitems(_table) - 1)
89 
90 #define	PCI_ANY_ID			-1U
91 
92 #define PCI_DEVFN(slot, func)   ((((slot) & 0x1f) << 3) | ((func) & 0x07))
93 #define PCI_SLOT(devfn)		(((devfn) >> 3) & 0x1f)
94 #define PCI_FUNC(devfn)		((devfn) & 0x07)
95 #define	PCI_BUS_NUM(devfn)	(((devfn) >> 8) & 0xff)
96 #define	PCI_DEVID(bus, devfn)	((((uint16_t)(bus)) << 8) | (devfn))
97 
98 #define PCI_VDEVICE(_vendor, _device)					\
99 	    .vendor = PCI_VENDOR_ID_##_vendor, .device = (_device),	\
100 	    .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID
101 #define	PCI_DEVICE(_vendor, _device)					\
102 	    .vendor = (_vendor), .device = (_device),			\
103 	    .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID
104 
105 #define	to_pci_dev(n)	container_of(n, struct pci_dev, dev)
106 
107 #define	PCI_STD_NUM_BARS	6
108 #define	PCI_BASE_ADDRESS_0	PCIR_BARS
109 #define	PCI_BASE_ADDRESS_MEM_TYPE_64	PCIM_BAR_MEM_64
110 #define	PCI_VENDOR_ID		PCIR_VENDOR
111 #define	PCI_DEVICE_ID		PCIR_DEVICE
112 #define	PCI_COMMAND		PCIR_COMMAND
113 #define	PCI_COMMAND_INTX_DISABLE	PCIM_CMD_INTxDIS
114 #define	PCI_COMMAND_MEMORY	PCIM_CMD_MEMEN
115 #define	PCI_PRIMARY_BUS		PCIR_PRIBUS_1
116 #define	PCI_SECONDARY_BUS	PCIR_SECBUS_1
117 #define	PCI_SUBORDINATE_BUS	PCIR_SUBBUS_1
118 #define	PCI_SEC_LATENCY_TIMER	PCIR_SECLAT_1
119 #define	PCI_EXP_DEVCTL		PCIER_DEVICE_CTL		/* Device Control */
120 #define	PCI_EXP_LNKCTL		PCIER_LINK_CTL			/* Link Control */
121 #define	PCI_EXP_LNKCTL_ASPM_L0S	PCIEM_LINK_CTL_ASPMC_L0S
122 #define	PCI_EXP_LNKCTL_ASPM_L1	PCIEM_LINK_CTL_ASPMC_L1
123 #define PCI_EXP_LNKCTL_ASPMC	PCIEM_LINK_CTL_ASPMC
124 #define	PCI_EXP_LNKCTL_CLKREQ_EN PCIEM_LINK_CTL_ECPM		/* Enable clock PM */
125 #define PCI_EXP_LNKCTL_HAWD	PCIEM_LINK_CTL_HAWD
126 #define	PCI_EXP_FLAGS_TYPE	PCIEM_FLAGS_TYPE		/* Device/Port type */
127 #define	PCI_EXP_DEVCAP		PCIER_DEVICE_CAP		/* Device capabilities */
128 #define	PCI_EXP_DEVSTA		PCIER_DEVICE_STA		/* Device Status */
129 #define	PCI_EXP_LNKCAP		PCIER_LINK_CAP			/* Link Capabilities */
130 #define	PCI_EXP_LNKSTA		PCIER_LINK_STA			/* Link Status */
131 #define	PCI_EXP_SLTCAP		PCIER_SLOT_CAP			/* Slot Capabilities */
132 #define	PCI_EXP_SLTCTL		PCIER_SLOT_CTL			/* Slot Control */
133 #define	PCI_EXP_SLTSTA		PCIER_SLOT_STA			/* Slot Status */
134 #define	PCI_EXP_RTCTL		PCIER_ROOT_CTL			/* Root Control */
135 #define	PCI_EXP_RTCAP		PCIER_ROOT_CAP			/* Root Capabilities */
136 #define	PCI_EXP_RTSTA		PCIER_ROOT_STA			/* Root Status */
137 #define	PCI_EXP_DEVCAP2		PCIER_DEVICE_CAP2		/* Device Capabilities 2 */
138 #define	PCI_EXP_DEVCTL2		PCIER_DEVICE_CTL2		/* Device Control 2 */
139 #define	PCI_EXP_DEVCTL2_LTR_EN	PCIEM_CTL2_LTR_ENABLE
140 #define	PCI_EXP_DEVCTL2_COMP_TMOUT_DIS	PCIEM_CTL2_COMP_TIMO_DISABLE
141 #define	PCI_EXP_LNKCAP2		PCIER_LINK_CAP2			/* Link Capabilities 2 */
142 #define	PCI_EXP_LNKCTL2		PCIER_LINK_CTL2			/* Link Control 2 */
143 #define	PCI_EXP_LNKSTA2		PCIER_LINK_STA2			/* Link Status 2 */
144 #define	PCI_EXP_FLAGS		PCIER_FLAGS			/* Capabilities register */
145 #define	PCI_EXP_FLAGS_VERS	PCIEM_FLAGS_VERSION		/* Capability version */
146 #define	PCI_EXP_TYPE_ROOT_PORT	PCIEM_TYPE_ROOT_PORT		/* Root Port */
147 #define	PCI_EXP_TYPE_ENDPOINT	PCIEM_TYPE_ENDPOINT		/* Express Endpoint */
148 #define	PCI_EXP_TYPE_LEG_END	PCIEM_TYPE_LEGACY_ENDPOINT	/* Legacy Endpoint */
149 #define	PCI_EXP_TYPE_DOWNSTREAM PCIEM_TYPE_DOWNSTREAM_PORT	/* Downstream Port */
150 #define	PCI_EXP_FLAGS_SLOT	PCIEM_FLAGS_SLOT		/* Slot implemented */
151 #define	PCI_EXP_TYPE_RC_EC	PCIEM_TYPE_ROOT_EC		/* Root Complex Event Collector */
152 #define	PCI_EXP_LNKSTA_CLS	PCIEM_LINK_STA_SPEED
153 #define	PCI_EXP_LNKSTA_CLS_8_0GB	0x0003	/* Current Link Speed 8.0GT/s */
154 #define	PCI_EXP_LNKCAP_SLS_2_5GB 0x01	/* Supported Link Speed 2.5GT/s */
155 #define	PCI_EXP_LNKCAP_SLS_5_0GB 0x02	/* Supported Link Speed 5.0GT/s */
156 #define	PCI_EXP_LNKCAP_SLS_8_0GB 0x03	/* Supported Link Speed 8.0GT/s */
157 #define	PCI_EXP_LNKCAP_SLS_16_0GB 0x04	/* Supported Link Speed 16.0GT/s */
158 #define	PCI_EXP_LNKCAP_SLS_32_0GB 0x05	/* Supported Link Speed 32.0GT/s */
159 #define	PCI_EXP_LNKCAP_SLS_64_0GB 0x06	/* Supported Link Speed 64.0GT/s */
160 #define	PCI_EXP_LNKCAP_MLW	0x03f0	/* Maximum Link Width */
161 #define	PCI_EXP_LNKCAP2_SLS_2_5GB 0x02	/* Supported Link Speed 2.5GT/s */
162 #define	PCI_EXP_LNKCAP2_SLS_5_0GB 0x04	/* Supported Link Speed 5.0GT/s */
163 #define	PCI_EXP_LNKCAP2_SLS_8_0GB 0x08	/* Supported Link Speed 8.0GT/s */
164 #define	PCI_EXP_LNKCAP2_SLS_16_0GB 0x10	/* Supported Link Speed 16.0GT/s */
165 #define	PCI_EXP_LNKCAP2_SLS_32_0GB 0x20	/* Supported Link Speed 32.0GT/s */
166 #define	PCI_EXP_LNKCAP2_SLS_64_0GB 0x40	/* Supported Link Speed 64.0GT/s */
167 #define	PCI_EXP_LNKCTL2_TLS		0x000f
168 #define	PCI_EXP_LNKCTL2_TLS_2_5GT	0x0001	/* Supported Speed 2.5GT/s */
169 #define	PCI_EXP_LNKCTL2_TLS_5_0GT	0x0002	/* Supported Speed 5GT/s */
170 #define	PCI_EXP_LNKCTL2_TLS_8_0GT	0x0003	/* Supported Speed 8GT/s */
171 #define	PCI_EXP_LNKCTL2_TLS_16_0GT	0x0004	/* Supported Speed 16GT/s */
172 #define	PCI_EXP_LNKCTL2_TLS_32_0GT	0x0005	/* Supported Speed 32GT/s */
173 #define	PCI_EXP_LNKCTL2_TLS_64_0GT	0x0006	/* Supported Speed 64GT/s */
174 #define	PCI_EXP_LNKCTL2_ENTER_COMP	0x0010	/* Enter Compliance */
175 #define	PCI_EXP_LNKCTL2_TX_MARGIN	0x0380	/* Transmit Margin */
176 
177 #define	PCI_MSI_ADDRESS_LO	PCIR_MSI_ADDR
178 #define	PCI_MSI_ADDRESS_HI	PCIR_MSI_ADDR_HIGH
179 #define	PCI_MSI_FLAGS		PCIR_MSI_CTRL
180 #define	PCI_MSI_FLAGS_ENABLE	PCIM_MSICTRL_MSI_ENABLE
181 #define	PCI_MSIX_FLAGS		PCIR_MSIX_CTRL
182 #define	PCI_MSIX_FLAGS_ENABLE	PCIM_MSIXCTRL_MSIX_ENABLE
183 
184 #define PCI_EXP_LNKCAP_CLKPM	0x00040000
185 #define PCI_EXP_DEVSTA_TRPND	0x0020
186 
187 #define	IORESOURCE_MEM	(1 << SYS_RES_MEMORY)
188 #define	IORESOURCE_IO	(1 << SYS_RES_IOPORT)
189 #define	IORESOURCE_IRQ	(1 << SYS_RES_IRQ)
190 
191 enum pci_bus_speed {
192 	PCI_SPEED_UNKNOWN = -1,
193 	PCIE_SPEED_2_5GT,
194 	PCIE_SPEED_5_0GT,
195 	PCIE_SPEED_8_0GT,
196 	PCIE_SPEED_16_0GT,
197 	PCIE_SPEED_32_0GT,
198 	PCIE_SPEED_64_0GT,
199 };
200 
201 enum pcie_link_width {
202 	PCIE_LNK_WIDTH_RESRV	= 0x00,
203 	PCIE_LNK_X1		= 0x01,
204 	PCIE_LNK_X2		= 0x02,
205 	PCIE_LNK_X4		= 0x04,
206 	PCIE_LNK_X8		= 0x08,
207 	PCIE_LNK_X12		= 0x0c,
208 	PCIE_LNK_X16		= 0x10,
209 	PCIE_LNK_X32		= 0x20,
210 	PCIE_LNK_WIDTH_UNKNOWN	= 0xff,
211 };
212 
213 #define	PCIE_LINK_STATE_L0S		0x00000001
214 #define	PCIE_LINK_STATE_L1		0x00000002
215 #define	PCIE_LINK_STATE_CLKPM		0x00000004
216 
217 typedef int pci_power_t;
218 
219 #define PCI_D0		PCI_POWERSTATE_D0
220 #define PCI_D1		PCI_POWERSTATE_D1
221 #define PCI_D2		PCI_POWERSTATE_D2
222 #define PCI_D3hot	PCI_POWERSTATE_D3_HOT
223 #define PCI_D3cold	PCI_POWERSTATE_D3_COLD
224 
225 #define PCI_POWER_ERROR	PCI_POWERSTATE_UNKNOWN
226 
227 extern const char *pci_power_names[6];
228 
229 #define	PCI_ERR_UNCOR_STATUS		PCIR_AER_UC_STATUS
230 #define	PCI_ERR_COR_STATUS		PCIR_AER_COR_STATUS
231 #define	PCI_ERR_ROOT_COMMAND		PCIR_AER_ROOTERR_CMD
232 #define	PCI_ERR_ROOT_ERR_SRC		PCIR_AER_COR_SOURCE_ID
233 
234 #define	PCI_EXT_CAP_ID_ERR		PCIZ_AER
235 #define	PCI_EXT_CAP_ID_L1SS		PCIZ_L1PM
236 
237 #define	PCI_L1SS_CTL1			0x8
238 #define	PCI_L1SS_CTL1_L1SS_MASK		0xf
239 
240 #define	PCI_IRQ_INTX			0x01
241 #define	PCI_IRQ_MSI			0x02
242 #define	PCI_IRQ_MSIX			0x04
243 #define	PCI_IRQ_ALL_TYPES		(PCI_IRQ_MSIX|PCI_IRQ_MSI|PCI_IRQ_INTX)
244 
245 #if defined(LINUXKPI_VERSION) && (LINUXKPI_VERSION <= 61000)
246 #define	PCI_IRQ_LEGACY			PCI_IRQ_INTX
247 #endif
248 
249 /*
250  * Linux PCI code uses `PCI_SET_ERROR_RESPONSE()` to indicate to the caller of
251  * a `pci_read_*()` function that the read failed. An example of failure is
252  * whether the device was disconnected. It is a bit weird because Linux
253  * `pci_read_*()` can return an error value, as the read value is stored in a
254  * integer passed by pointer.
255  *
256  * We don't set PCI_ERROR_RESPONSE anywhere as of this commit, but the DRM
257  * drivers started to use `PCI_POSSIBLE_ERROR()`.
258  */
259 #define	PCI_ERROR_RESPONSE		(~0ULL)
260 #define	PCI_SET_ERROR_RESPONSE(val)	(*(val) = ((typeof(*(val))) PCI_ERROR_RESPONSE))
261 #define	PCI_POSSIBLE_ERROR(val)		((val) == ((typeof(val)) PCI_ERROR_RESPONSE))
262 
263 struct pci_dev;
264 
265 struct pci_driver {
266 	struct list_head		node;
267 	char				*name;
268 	const struct pci_device_id		*id_table;
269 	int  (*probe)(struct pci_dev *dev, const struct pci_device_id *id);
270 	void (*remove)(struct pci_dev *dev);
271 	int  (*suspend) (struct pci_dev *dev, pm_message_t state);	/* Device suspended */
272 	int  (*resume) (struct pci_dev *dev);		/* Device woken up */
273 	void (*shutdown) (struct pci_dev *dev);		/* Device shutdown */
274 	driver_t			bsddriver;
275 	devclass_t			bsdclass;
276 	struct device_driver		driver;
277 	const struct pci_error_handlers       *err_handler;
278 	bool				isdrm;
279 	int				bsd_probe_return;
280 	int  (*bsd_iov_init)(device_t dev, uint16_t num_vfs,
281 	    const nvlist_t *pf_config);
282 	void  (*bsd_iov_uninit)(device_t dev);
283 	int  (*bsd_iov_add_vf)(device_t dev, uint16_t vfnum,
284 	    const nvlist_t *vf_config);
285 };
286 
287 struct pci_bus {
288 	struct pci_dev	*self;
289 	/* struct pci_bus	*parent */
290 	int		domain;
291 	int		number;
292 };
293 
294 extern struct list_head pci_drivers;
295 extern struct list_head pci_devices;
296 extern spinlock_t pci_lock;
297 
298 #define	__devexit_p(x)	x
299 
300 #define	module_pci_driver(_drv)						\
301     module_driver(_drv, linux_pci_register_driver, linux_pci_unregister_driver)
302 
303 struct msi_msg {
304 	uint32_t			data;
305 };
306 
307 struct pci_msi_desc {
308 	struct {
309 		bool			is_64;
310 	} msi_attrib;
311 };
312 
313 struct msi_desc {
314 	struct msi_msg			msg;
315 	struct pci_msi_desc		pci;
316 };
317 
318 struct msix_entry {
319 	int entry;
320 	int vector;
321 };
322 
323 /*
324  * If we find drivers accessing this from multiple KPIs we may have to
325  * refcount objects of this structure.
326  */
327 struct resource;
328 struct pci_mmio_region {
329 	TAILQ_ENTRY(pci_mmio_region)	next;
330 	struct resource			*res;
331 	int				rid;
332 	int				type;
333 };
334 
335 struct pci_dev {
336 	struct device		dev;
337 	struct list_head	links;
338 	struct pci_driver	*pdrv;
339 	struct pci_bus		*bus;
340 	struct pci_dev		*root;
341 	pci_power_t		current_state;
342 	uint16_t		device;
343 	uint16_t		vendor;
344 	uint16_t		subsystem_vendor;
345 	uint16_t		subsystem_device;
346 	unsigned int		irq;
347 	unsigned int		devfn;
348 	uint32_t		class;
349 	uint8_t			revision;
350 	uint8_t			msi_cap;
351 	uint8_t			msix_cap;
352 	bool			managed;	/* devres "pcim_*(). */
353 	bool			want_iomap_res;
354 	bool			msi_enabled;
355 	bool			msix_enabled;
356 	phys_addr_t		rom;
357 	size_t			romlen;
358 	struct msi_desc		**msi_desc;
359 	char			*path_name;
360 	spinlock_t		pcie_cap_lock;
361 
362 	TAILQ_HEAD(, pci_mmio_region)	mmio;
363 };
364 
365 int pci_alloc_irq_vectors(struct pci_dev *pdev, int minv, int maxv,
366     unsigned int flags);
367 bool pci_device_is_present(struct pci_dev *pdev);
368 
369 int linuxkpi_pcim_enable_device(struct pci_dev *pdev);
370 void __iomem **linuxkpi_pcim_iomap_table(struct pci_dev *pdev);
371 void *linuxkpi_pci_iomap_range(struct pci_dev *, int,
372     unsigned long, unsigned long);
373 void *linuxkpi_pci_iomap(struct pci_dev *, int, unsigned long);
374 void *linuxkpi_pcim_iomap(struct pci_dev *, int, unsigned long);
375 void linuxkpi_pci_iounmap(struct pci_dev *pdev, void *res);
376 int linuxkpi_pcim_iomap_regions(struct pci_dev *pdev, uint32_t mask,
377     const char *name);
378 int linuxkpi_pci_request_region(struct pci_dev *, int, const char *);
379 int linuxkpi_pci_request_regions(struct pci_dev *pdev, const char *res_name);
380 int linuxkpi_pcim_request_all_regions(struct pci_dev *, const char *);
381 void linuxkpi_pci_release_region(struct pci_dev *pdev, int bar);
382 void linuxkpi_pci_release_regions(struct pci_dev *pdev);
383 int linuxkpi_pci_enable_msix(struct pci_dev *pdev, struct msix_entry *entries,
384     int nreq);
385 
386 /* Internal helper function(s). */
387 struct pci_dev *lkpinew_pci_dev(device_t);
388 void lkpi_pci_devres_release(struct device *, void *);
389 struct pci_dev *lkpi_pci_get_device(uint32_t, uint32_t, struct pci_dev *);
390 struct msi_desc *lkpi_pci_msi_desc_alloc(int);
391 struct device *lkpi_pci_find_irq_dev(unsigned int irq);
392 int _lkpi_pci_enable_msi_range(struct pci_dev *pdev, int minvec, int maxvec);
393 
394 #define	pci_err(pdev, fmt, ...)						\
395     dev_err(&(pdev)->dev, fmt, ##__VA_ARGS__)
396 #define	pci_info(pdev, fmt, ...)					\
397     dev_info(&(pdev)->dev, fmt, ##__VA_ARGS__)
398 
399 static inline bool
400 dev_is_pci(struct device *dev)
401 {
402 	return (is_pci_device(dev->bsddev));
403 }
404 
405 static inline uint16_t
406 pci_dev_id(struct pci_dev *pdev)
407 {
408 	return (PCI_DEVID(pdev->bus->number, pdev->devfn));
409 }
410 
411 static inline int
412 pci_resource_type(struct pci_dev *pdev, int bar)
413 {
414 	struct pci_map *pm;
415 
416 	pm = pci_find_bar(pdev->dev.bsddev, PCIR_BAR(bar));
417 	if (!pm)
418 		return (-1);
419 
420 	if (PCI_BAR_IO(pm->pm_value))
421 		return (SYS_RES_IOPORT);
422 	else
423 		return (SYS_RES_MEMORY);
424 }
425 
426 /*
427  * All drivers just seem to want to inspect the type not flags.
428  */
429 static inline int
430 pci_resource_flags(struct pci_dev *pdev, int bar)
431 {
432 	int type;
433 
434 	type = pci_resource_type(pdev, bar);
435 	if (type < 0)
436 		return (0);
437 	return (1 << type);
438 }
439 
440 static inline const char *
441 pci_name(struct pci_dev *d)
442 {
443 	return d->path_name;
444 }
445 
446 static inline void *
447 pci_get_drvdata(struct pci_dev *pdev)
448 {
449 
450 	return dev_get_drvdata(&pdev->dev);
451 }
452 
453 static inline void
454 pci_set_drvdata(struct pci_dev *pdev, void *data)
455 {
456 
457 	dev_set_drvdata(&pdev->dev, data);
458 }
459 
460 static inline struct pci_dev *
461 pci_dev_get(struct pci_dev *pdev)
462 {
463 
464 	if (pdev != NULL)
465 		get_device(&pdev->dev);
466 	return (pdev);
467 }
468 
469 static __inline void
470 pci_dev_put(struct pci_dev *pdev)
471 {
472 
473 	if (pdev != NULL)
474 		put_device(&pdev->dev);
475 }
476 
477 static inline int
478 pci_enable_device(struct pci_dev *pdev)
479 {
480 
481 	pci_enable_io(pdev->dev.bsddev, SYS_RES_IOPORT);
482 	pci_enable_io(pdev->dev.bsddev, SYS_RES_MEMORY);
483 	return (0);
484 }
485 
486 static inline void
487 pci_disable_device(struct pci_dev *pdev)
488 {
489 
490 	pci_disable_busmaster(pdev->dev.bsddev);
491 }
492 
493 static inline int
494 pci_set_master(struct pci_dev *pdev)
495 {
496 
497 	pci_enable_busmaster(pdev->dev.bsddev);
498 	return (0);
499 }
500 
501 static inline int
502 pci_set_power_state(struct pci_dev *pdev, int state)
503 {
504 
505 	pci_set_powerstate(pdev->dev.bsddev, state);
506 	return (0);
507 }
508 
509 static inline int
510 pci_clear_master(struct pci_dev *pdev)
511 {
512 
513 	pci_disable_busmaster(pdev->dev.bsddev);
514 	return (0);
515 }
516 
517 static inline bool
518 pci_is_root_bus(struct pci_bus *pbus)
519 {
520 
521 	return (pbus->self == NULL);
522 }
523 
524 static inline struct pci_dev *
525 pci_upstream_bridge(struct pci_dev *pdev)
526 {
527 
528 	if (pci_is_root_bus(pdev->bus))
529 		return (NULL);
530 
531 	/*
532 	 * If we do not have a (proper) "upstream bridge" set, e.g., we point
533 	 * to ourselves, try to handle this case on the fly like we do
534 	 * for pcie_find_root_port().
535 	 */
536 	if (pdev == pdev->bus->self) {
537 		device_t bridge;
538 
539 		/*
540 		 * In the case of DRM drivers, the passed device is a child of
541 		 * `vgapci`. We want to start the lookup from `vgapci`, so the
542 		 * parent of the passed `drmn`.
543 		 *
544 		 * We can use the `isdrm` flag to determine this.
545 		 */
546 		bridge = pdev->dev.bsddev;
547 		if (pdev->pdrv != NULL && pdev->pdrv->isdrm)
548 			bridge = device_get_parent(bridge);
549 		if (bridge == NULL)
550 			goto done;
551 
552 		bridge = device_get_parent(bridge);
553 		if (bridge == NULL)
554 			goto done;
555 		bridge = device_get_parent(bridge);
556 		if (bridge == NULL)
557 			goto done;
558 		if (!is_pci_device(bridge))
559 			goto done;
560 
561 		/*
562 		 * "bridge" is a PCI-to-PCI bridge.  Create a Linux pci_dev
563 		 * for it so it can be returned.
564 		 */
565 		pdev->bus->self = lkpinew_pci_dev(bridge);
566 	}
567 done:
568 	return (pdev->bus->self);
569 }
570 
571 #define	pci_request_region(pdev, bar, res_name)				\
572     linuxkpi_pci_request_region(pdev, bar, res_name)
573 #define	pci_release_region(pdev, bar)					\
574     linuxkpi_pci_release_region(pdev, bar)
575 #define	pci_request_regions(pdev, res_name)				\
576     linuxkpi_pci_request_regions(pdev, res_name)
577 #define	pci_release_regions(pdev)					\
578     linuxkpi_pci_release_regions(pdev)
579 #define	pcim_request_all_regions(pdev, name)				\
580     linuxkpi_pcim_request_all_regions(pdev, name)
581 
582 static inline void
583 lkpi_pci_disable_msix(struct pci_dev *pdev)
584 {
585 
586 	pci_release_msi(pdev->dev.bsddev);
587 
588 	/*
589 	 * The MSIX IRQ numbers associated with this PCI device are no
590 	 * longer valid and might be re-assigned. Make sure
591 	 * lkpi_pci_find_irq_dev() does no longer see them by
592 	 * resetting their references to zero:
593 	 */
594 	pdev->dev.irq_start = 0;
595 	pdev->dev.irq_end = 0;
596 	pdev->msix_enabled = false;
597 }
598 /* Only for consistency. No conflict on that one. */
599 #define	pci_disable_msix(pdev)		lkpi_pci_disable_msix(pdev)
600 
601 static inline void
602 lkpi_pci_disable_msi(struct pci_dev *pdev)
603 {
604 
605 	pci_release_msi(pdev->dev.bsddev);
606 
607 	pdev->dev.irq_start = 0;
608 	pdev->dev.irq_end = 0;
609 	pdev->irq = pdev->dev.irq;
610 	pdev->msi_enabled = false;
611 }
612 #define	pci_disable_msi(pdev)		lkpi_pci_disable_msi(pdev)
613 #define	pci_free_irq_vectors(pdev)	lkpi_pci_disable_msi(pdev)
614 
615 unsigned long	pci_resource_start(struct pci_dev *pdev, int bar);
616 unsigned long	pci_resource_len(struct pci_dev *pdev, int bar);
617 
618 static inline bus_addr_t
619 pci_bus_address(struct pci_dev *pdev, int bar)
620 {
621 
622 	return (pci_resource_start(pdev, bar));
623 }
624 
625 #define	PCI_CAP_ID_EXP	PCIY_EXPRESS
626 #define	PCI_CAP_ID_PCIX	PCIY_PCIX
627 #define PCI_CAP_ID_AGP  PCIY_AGP
628 #define PCI_CAP_ID_PM   PCIY_PMG
629 
630 #define PCI_EXP_DEVCTL		PCIER_DEVICE_CTL
631 #define PCI_EXP_DEVCTL_PAYLOAD	PCIEM_CTL_MAX_PAYLOAD
632 #define PCI_EXP_DEVCTL_READRQ	PCIEM_CTL_MAX_READ_REQUEST
633 #define PCI_EXP_LNKCTL		PCIER_LINK_CTL
634 #define PCI_EXP_LNKSTA		PCIER_LINK_STA
635 
636 static inline int
637 pci_find_capability(struct pci_dev *pdev, int capid)
638 {
639 	int reg;
640 
641 	if (pci_find_cap(pdev->dev.bsddev, capid, &reg))
642 		return (0);
643 	return (reg);
644 }
645 
646 static inline int pci_pcie_cap(struct pci_dev *dev)
647 {
648 	return pci_find_capability(dev, PCI_CAP_ID_EXP);
649 }
650 
651 static inline int
652 pci_find_ext_capability(struct pci_dev *pdev, int capid)
653 {
654 	int reg;
655 
656 	if (pci_find_extcap(pdev->dev.bsddev, capid, &reg))
657 		return (0);
658 	return (reg);
659 }
660 
661 static __inline bool
662 pci_pme_capable(struct pci_dev *pdev, pci_power_t state)
663 {
664 	return (pci_has_pme(pdev->dev.bsddev, state));
665 }
666 
667 static inline int
668 pci_disable_link_state(struct pci_dev *pdev, uint32_t flags)
669 {
670 
671 	if (!pci_enable_aspm)
672 		return (-EPERM);
673 
674 	return (-ENXIO);
675 }
676 
677 static inline int
678 pci_read_config_byte(const struct pci_dev *pdev, int where, u8 *val)
679 {
680 
681 	*val = (u8)pci_read_config(pdev->dev.bsddev, where, 1);
682 	return (0);
683 }
684 
685 static inline int
686 pci_read_config_word(const struct pci_dev *pdev, int where, u16 *val)
687 {
688 
689 	*val = (u16)pci_read_config(pdev->dev.bsddev, where, 2);
690 	return (0);
691 }
692 
693 static inline int
694 pci_read_config_dword(const struct pci_dev *pdev, int where, u32 *val)
695 {
696 
697 	*val = (u32)pci_read_config(pdev->dev.bsddev, where, 4);
698 	return (0);
699 }
700 
701 static inline int
702 pci_write_config_byte(const struct pci_dev *pdev, int where, u8 val)
703 {
704 
705 	pci_write_config(pdev->dev.bsddev, where, val, 1);
706 	return (0);
707 }
708 
709 static inline int
710 pci_write_config_word(const struct pci_dev *pdev, int where, u16 val)
711 {
712 
713 	pci_write_config(pdev->dev.bsddev, where, val, 2);
714 	return (0);
715 }
716 
717 static inline int
718 pci_write_config_dword(const struct pci_dev *pdev, int where, u32 val)
719 {
720 
721 	pci_write_config(pdev->dev.bsddev, where, val, 4);
722 	return (0);
723 }
724 
725 int	linux_pci_register_driver(struct pci_driver *pdrv);
726 int	linux_pci_register_drm_driver(struct pci_driver *pdrv);
727 void	linux_pci_unregister_driver(struct pci_driver *pdrv);
728 void	linux_pci_unregister_drm_driver(struct pci_driver *pdrv);
729 
730 #define	pci_register_driver(pdrv)					\
731     linux_pci_register_driver(pdrv)
732 #define	pci_unregister_driver(pdrv)					\
733     linux_pci_unregister_driver(pdrv)
734 
735 /*
736  * Enable msix, positive errors indicate actual number of available
737  * vectors.  Negative errors are failures.
738  *
739  * NB: define added to prevent this definition of pci_enable_msix from
740  * clashing with the native FreeBSD version.
741  */
742 #define	pci_enable_msix(...)						\
743     linuxkpi_pci_enable_msix(__VA_ARGS__)
744 
745 #define	pci_enable_msix_range(...)					\
746     linux_pci_enable_msix_range(__VA_ARGS__)
747 
748 static inline int
749 pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
750     int minvec, int maxvec)
751 {
752 	int nvec = maxvec;
753 	int rc;
754 
755 	if (maxvec < minvec)
756 		return (-ERANGE);
757 
758 	do {
759 		rc = pci_enable_msix(dev, entries, nvec);
760 		if (rc < 0) {
761 			return (rc);
762 		} else if (rc > 0) {
763 			if (rc < minvec)
764 				return (-ENOSPC);
765 			nvec = rc;
766 		}
767 	} while (rc);
768 	return (nvec);
769 }
770 
771 #define	pci_enable_msi(pdev)						\
772     linux_pci_enable_msi(pdev)
773 
774 static inline int
775 pci_enable_msi(struct pci_dev *pdev)
776 {
777 
778 	return (_lkpi_pci_enable_msi_range(pdev, 1, 1));
779 }
780 
781 static inline int
782 pci_channel_offline(struct pci_dev *pdev)
783 {
784 
785 	return (pci_read_config(pdev->dev.bsddev, PCIR_VENDOR, 2) == PCIV_INVALID);
786 }
787 
788 static inline int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
789 {
790 	return -ENODEV;
791 }
792 
793 static inline void pci_disable_sriov(struct pci_dev *dev)
794 {
795 }
796 
797 #define	pci_iomap_range(pdev, mmio_bar, mmio_off, mmio_size)		\
798     linuxkpi_pci_iomap_range(pdev, mmio_bar, mmio_off, mmio_size)
799 #define	pci_iomap(pdev, mmio_bar, mmio_size)				\
800     linuxkpi_pci_iomap(pdev, mmio_bar, mmio_size)
801 #define	pcim_iomap(pdev, bar, maxlen)					\
802     linuxkpi_pcim_iomap(pdev, bar, maxlen)
803 #define	pci_iounmap(pdev, res)						\
804     linuxkpi_pci_iounmap(pdev, res)
805 
806 static inline void
807 lkpi_pci_save_state(struct pci_dev *pdev)
808 {
809 
810 	pci_save_state(pdev->dev.bsddev);
811 }
812 
813 static inline void
814 lkpi_pci_restore_state(struct pci_dev *pdev)
815 {
816 
817 	pci_restore_state(pdev->dev.bsddev);
818 }
819 
820 #define pci_save_state(dev)	lkpi_pci_save_state(dev)
821 #define pci_restore_state(dev)	lkpi_pci_restore_state(dev)
822 
823 static inline int
824 linuxkpi_pci_enable_wake(struct pci_dev *pdev, pci_power_t state, bool ena)
825 {
826 	/*
827 	 * We do not currently support this in device.h either to
828 	 * check if the device is allowed to wake up in first place.
829 	 */
830 	pr_debug("%s: TODO\n", __func__);
831 	return (0);
832 }
833 #define	pci_enable_wake(dev, state, ena)				\
834     linuxkpi_pci_enable_wake(dev, state, ena)
835 
836 static inline int
837 pci_reset_function(struct pci_dev *pdev)
838 {
839 
840 	return (-ENOSYS);
841 }
842 
843 #define DEFINE_PCI_DEVICE_TABLE(_table) \
844 	const struct pci_device_id _table[] __devinitdata
845 
846 /* XXX This should not be necessary. */
847 #define	pcix_set_mmrbc(d, v)	0
848 #define	pcix_get_max_mmrbc(d)	0
849 #define	pcie_set_readrq(d, v)	pci_set_max_read_req((d)->dev.bsddev, (v))
850 
851 #define	PCI_DMA_BIDIRECTIONAL	0
852 #define	PCI_DMA_TODEVICE	1
853 #define	PCI_DMA_FROMDEVICE	2
854 #define	PCI_DMA_NONE		3
855 
856 #define	pci_pool		dma_pool
857 #define	pci_pool_destroy(...)	dma_pool_destroy(__VA_ARGS__)
858 #define	pci_pool_alloc(...)	dma_pool_alloc(__VA_ARGS__)
859 #define	pci_pool_free(...)	dma_pool_free(__VA_ARGS__)
860 #define	pci_pool_create(_name, _pdev, _size, _align, _alloc)		\
861 	    dma_pool_create(_name, &(_pdev)->dev, _size, _align, _alloc)
862 #define	pci_free_consistent(_hwdev, _size, _vaddr, _dma_handle)		\
863 	    dma_free_coherent((_hwdev) == NULL ? NULL : &(_hwdev)->dev,	\
864 		_size, _vaddr, _dma_handle)
865 #define	pci_map_sg(_hwdev, _sg, _nents, _dir)				\
866 	    dma_map_sg((_hwdev) == NULL ? NULL : &(_hwdev->dev),	\
867 		_sg, _nents, (enum dma_data_direction)_dir)
868 #define	pci_map_single(_hwdev, _ptr, _size, _dir)			\
869 	    dma_map_single((_hwdev) == NULL ? NULL : &(_hwdev->dev),	\
870 		(_ptr), (_size), (enum dma_data_direction)_dir)
871 #define	pci_unmap_single(_hwdev, _addr, _size, _dir)			\
872 	    dma_unmap_single((_hwdev) == NULL ? NULL : &(_hwdev)->dev,	\
873 		_addr, _size, (enum dma_data_direction)_dir)
874 #define	pci_unmap_sg(_hwdev, _sg, _nents, _dir)				\
875 	    dma_unmap_sg((_hwdev) == NULL ? NULL : &(_hwdev)->dev,	\
876 		_sg, _nents, (enum dma_data_direction)_dir)
877 #define	pci_map_page(_hwdev, _page, _offset, _size, _dir)		\
878 	    dma_map_page((_hwdev) == NULL ? NULL : &(_hwdev)->dev, _page,\
879 		_offset, _size, (enum dma_data_direction)_dir)
880 #define	pci_unmap_page(_hwdev, _dma_address, _size, _dir)		\
881 	    dma_unmap_page((_hwdev) == NULL ? NULL : &(_hwdev)->dev,	\
882 		_dma_address, _size, (enum dma_data_direction)_dir)
883 #define	pci_set_dma_mask(_pdev, mask)	dma_set_mask(&(_pdev)->dev, (mask))
884 #define	pci_dma_mapping_error(_pdev, _dma_addr)				\
885 	    dma_mapping_error(&(_pdev)->dev, _dma_addr)
886 #define	pci_set_consistent_dma_mask(_pdev, _mask)			\
887 	    dma_set_coherent_mask(&(_pdev)->dev, (_mask))
888 #define	DECLARE_PCI_UNMAP_ADDR(x)	DEFINE_DMA_UNMAP_ADDR(x);
889 #define	DECLARE_PCI_UNMAP_LEN(x)	DEFINE_DMA_UNMAP_LEN(x);
890 #define	pci_unmap_addr		dma_unmap_addr
891 #define	pci_unmap_addr_set	dma_unmap_addr_set
892 #define	pci_unmap_len		dma_unmap_len
893 #define	pci_unmap_len_set	dma_unmap_len_set
894 
895 void *linuxkpi_pci_map_rom(struct pci_dev *pdev, size_t *size);
896 void linuxkpi_pci_unmap_rom(struct pci_dev *pdev, void *rom);
897 #define	pci_map_rom(pdev, size)		linuxkpi_pci_map_rom(pdev, size)
898 #define	pci_unmap_rom(pdev, rom)	linuxkpi_pci_unmap_rom(pdev, rom)
899 
900 typedef unsigned int __bitwise pci_channel_state_t;
901 typedef unsigned int __bitwise pci_ers_result_t;
902 
903 enum pci_channel_state {
904 	pci_channel_io_normal = 1,
905 	pci_channel_io_frozen = 2,
906 	pci_channel_io_perm_failure = 3,
907 };
908 
909 enum pci_ers_result {
910 	PCI_ERS_RESULT_NONE = 1,
911 	PCI_ERS_RESULT_CAN_RECOVER = 2,
912 	PCI_ERS_RESULT_NEED_RESET = 3,
913 	PCI_ERS_RESULT_DISCONNECT = 4,
914 	PCI_ERS_RESULT_RECOVERED = 5,
915 };
916 
917 /* PCI bus error event callbacks */
918 struct pci_error_handlers {
919 	pci_ers_result_t (*error_detected)(struct pci_dev *dev,
920 	    enum pci_channel_state error);
921 	pci_ers_result_t (*mmio_enabled)(struct pci_dev *dev);
922 	pci_ers_result_t (*link_reset)(struct pci_dev *dev);
923 	pci_ers_result_t (*slot_reset)(struct pci_dev *dev);
924 	void (*resume)(struct pci_dev *dev);
925 };
926 
927 /* FreeBSD does not support SRIOV - yet */
928 static inline struct pci_dev *pci_physfn(struct pci_dev *dev)
929 {
930 	return dev;
931 }
932 
933 static inline bool pci_is_pcie(struct pci_dev *dev)
934 {
935 	return !!pci_pcie_cap(dev);
936 }
937 
938 static inline u16 pcie_flags_reg(struct pci_dev *dev)
939 {
940 	int pos;
941 	u16 reg16;
942 
943 	pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
944 	if (!pos)
945 		return 0;
946 
947 	pci_read_config_word(dev, pos + PCI_EXP_FLAGS, &reg16);
948 
949 	return reg16;
950 }
951 
952 static inline int pci_pcie_type(struct pci_dev *dev)
953 {
954 	return (pcie_flags_reg(dev) & PCI_EXP_FLAGS_TYPE) >> 4;
955 }
956 
957 static inline int pcie_cap_version(struct pci_dev *dev)
958 {
959 	return pcie_flags_reg(dev) & PCI_EXP_FLAGS_VERS;
960 }
961 
962 static inline bool pcie_cap_has_lnkctl(struct pci_dev *dev)
963 {
964 	int type = pci_pcie_type(dev);
965 
966 	return pcie_cap_version(dev) > 1 ||
967 	       type == PCI_EXP_TYPE_ROOT_PORT ||
968 	       type == PCI_EXP_TYPE_ENDPOINT ||
969 	       type == PCI_EXP_TYPE_LEG_END;
970 }
971 
972 static inline bool pcie_cap_has_devctl(const struct pci_dev *dev)
973 {
974 		return true;
975 }
976 
977 static inline bool pcie_cap_has_sltctl(struct pci_dev *dev)
978 {
979 	int type = pci_pcie_type(dev);
980 
981 	return pcie_cap_version(dev) > 1 || type == PCI_EXP_TYPE_ROOT_PORT ||
982 	    (type == PCI_EXP_TYPE_DOWNSTREAM &&
983 	    pcie_flags_reg(dev) & PCI_EXP_FLAGS_SLOT);
984 }
985 
986 static inline bool pcie_cap_has_rtctl(struct pci_dev *dev)
987 {
988 	int type = pci_pcie_type(dev);
989 
990 	return pcie_cap_version(dev) > 1 || type == PCI_EXP_TYPE_ROOT_PORT ||
991 	    type == PCI_EXP_TYPE_RC_EC;
992 }
993 
994 static bool pcie_capability_reg_implemented(struct pci_dev *dev, int pos)
995 {
996 	if (!pci_is_pcie(dev))
997 		return false;
998 
999 	switch (pos) {
1000 	case PCI_EXP_FLAGS_TYPE:
1001 		return true;
1002 	case PCI_EXP_DEVCAP:
1003 	case PCI_EXP_DEVCTL:
1004 	case PCI_EXP_DEVSTA:
1005 		return pcie_cap_has_devctl(dev);
1006 	case PCI_EXP_LNKCAP:
1007 	case PCI_EXP_LNKCTL:
1008 	case PCI_EXP_LNKSTA:
1009 		return pcie_cap_has_lnkctl(dev);
1010 	case PCI_EXP_SLTCAP:
1011 	case PCI_EXP_SLTCTL:
1012 	case PCI_EXP_SLTSTA:
1013 		return pcie_cap_has_sltctl(dev);
1014 	case PCI_EXP_RTCTL:
1015 	case PCI_EXP_RTCAP:
1016 	case PCI_EXP_RTSTA:
1017 		return pcie_cap_has_rtctl(dev);
1018 	case PCI_EXP_DEVCAP2:
1019 	case PCI_EXP_DEVCTL2:
1020 	case PCI_EXP_LNKCAP2:
1021 	case PCI_EXP_LNKCTL2:
1022 	case PCI_EXP_LNKSTA2:
1023 		return pcie_cap_version(dev) > 1;
1024 	default:
1025 		return false;
1026 	}
1027 }
1028 
1029 static inline int
1030 pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *dst)
1031 {
1032 	*dst = 0;
1033 	if (pos & 3)
1034 		return -EINVAL;
1035 
1036 	if (!pcie_capability_reg_implemented(dev, pos))
1037 		return -EINVAL;
1038 
1039 	return pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, dst);
1040 }
1041 
1042 static inline int
1043 pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *dst)
1044 {
1045 	*dst = 0;
1046 	if (pos & 3)
1047 		return -EINVAL;
1048 
1049 	if (!pcie_capability_reg_implemented(dev, pos))
1050 		return -EINVAL;
1051 
1052 	return pci_read_config_word(dev, pci_pcie_cap(dev) + pos, dst);
1053 }
1054 
1055 static inline int
1056 pcie_capability_write_word(struct pci_dev *dev, int pos, u16 val)
1057 {
1058 	if (pos & 1)
1059 		return -EINVAL;
1060 
1061 	if (!pcie_capability_reg_implemented(dev, pos))
1062 		return 0;
1063 
1064 	return pci_write_config_word(dev, pci_pcie_cap(dev) + pos, val);
1065 }
1066 
1067 static inline int
1068 pcie_capability_clear_and_set_word(struct pci_dev *dev, int pos,
1069     uint16_t clear, uint16_t set)
1070 {
1071 	int error;
1072 	uint16_t v;
1073 
1074 	if (pos == PCI_EXP_LNKCTL || pos == PCI_EXP_RTCTL)
1075 		spin_lock(&dev->pcie_cap_lock);
1076 
1077 	error = pcie_capability_read_word(dev, pos, &v);
1078 	if (error == 0) {
1079 		v &= ~clear;
1080 		v |= set;
1081 		error = pcie_capability_write_word(dev, pos, v);
1082 	}
1083 
1084 	if (pos == PCI_EXP_LNKCTL || pos == PCI_EXP_RTCTL)
1085 		spin_unlock(&dev->pcie_cap_lock);
1086 
1087 	return (error);
1088 }
1089 
1090 static inline int
1091 pcie_capability_set_word(struct pci_dev *dev, int pos, uint16_t val)
1092 {
1093 	return (pcie_capability_clear_and_set_word(dev, pos, 0, val));
1094 }
1095 
1096 static inline int
1097 pcie_capability_clear_word(struct pci_dev *dev, int pos, uint16_t val)
1098 {
1099 	return (pcie_capability_clear_and_set_word(dev, pos, val, 0));
1100 }
1101 
1102 static inline int pcie_get_minimum_link(struct pci_dev *dev,
1103     enum pci_bus_speed *speed, enum pcie_link_width *width)
1104 {
1105 	*speed = PCI_SPEED_UNKNOWN;
1106 	*width = PCIE_LNK_WIDTH_UNKNOWN;
1107 	return (0);
1108 }
1109 
1110 static inline int
1111 pci_num_vf(struct pci_dev *dev)
1112 {
1113 	return (0);
1114 }
1115 
1116 static inline enum pci_bus_speed
1117 pcie_get_speed_cap(struct pci_dev *dev)
1118 {
1119 	struct pci_dev *pbus;
1120 	device_t root;
1121 	uint32_t lnkcap, lnkcap2;
1122 	int error, pos;
1123 
1124 	/*
1125 	 * We should always be called on a PCI device.
1126 	 * The only current consumer I could find was amdgpu which either
1127 	 * calls us directly on a pdev(drmn?) or with the result of
1128 	 * pci_upstream_bridge().
1129 	 *
1130 	 * Treat "drmn" as special again as it is not a PCI device.
1131 	 */
1132 	if (dev->pdrv != NULL && dev->pdrv->isdrm) {
1133 		pbus = pci_upstream_bridge(dev);
1134 		if (pbus == NULL)
1135 			return (PCI_SPEED_UNKNOWN);
1136 	} else
1137 		pbus = dev;
1138 
1139 	/* "root" may be misleading as it may not be that. */
1140 	root = pbus->dev.bsddev;
1141 
1142 	if (pci_get_vendor(root) == PCI_VENDOR_ID_VIA ||
1143 	    pci_get_vendor(root) == PCI_VENDOR_ID_SERVERWORKS)
1144 		return (PCI_SPEED_UNKNOWN);
1145 
1146 	if ((error = pci_find_cap(root, PCIY_EXPRESS, &pos)) != 0)
1147 		return (PCI_SPEED_UNKNOWN);
1148 
1149 	lnkcap2 = pci_read_config(root, pos + PCIER_LINK_CAP2, 4);
1150 
1151 	if (lnkcap2) {	/* PCIe r3.0-compliant */
1152 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
1153 			return (PCIE_SPEED_2_5GT);
1154 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
1155 			return (PCIE_SPEED_5_0GT);
1156 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
1157 			return (PCIE_SPEED_8_0GT);
1158 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_16_0GB)
1159 			return (PCIE_SPEED_16_0GT);
1160 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_32_0GB)
1161 			return (PCIE_SPEED_32_0GT);
1162 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_64_0GB)
1163 			return (PCIE_SPEED_64_0GT);
1164 	} else {	/* pre-r3.0 */
1165 		lnkcap = pci_read_config(root, pos + PCIER_LINK_CAP, 4);
1166 		if (lnkcap & PCI_EXP_LNKCAP_SLS_2_5GB)
1167 			return (PCIE_SPEED_2_5GT);
1168 		if (lnkcap & PCI_EXP_LNKCAP_SLS_5_0GB)
1169 			return (PCIE_SPEED_5_0GT);
1170 		if (lnkcap & PCI_EXP_LNKCAP_SLS_8_0GB)
1171 			return (PCIE_SPEED_8_0GT);
1172 		if (lnkcap & PCI_EXP_LNKCAP_SLS_16_0GB)
1173 			return (PCIE_SPEED_16_0GT);
1174 		if (lnkcap & PCI_EXP_LNKCAP_SLS_32_0GB)
1175 			return (PCIE_SPEED_32_0GT);
1176 		if (lnkcap & PCI_EXP_LNKCAP_SLS_64_0GB)
1177 			return (PCIE_SPEED_64_0GT);
1178 	}
1179 	return (PCI_SPEED_UNKNOWN);
1180 }
1181 
1182 static inline enum pcie_link_width
1183 pcie_get_width_cap(struct pci_dev *dev)
1184 {
1185 	uint32_t lnkcap;
1186 
1187 	pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
1188 	if (lnkcap)
1189 		return ((lnkcap & PCI_EXP_LNKCAP_MLW) >> 4);
1190 
1191 	return (PCIE_LNK_WIDTH_UNKNOWN);
1192 }
1193 
1194 static inline int
1195 pcie_get_mps(struct pci_dev *dev)
1196 {
1197 	return (pci_get_max_payload(dev->dev.bsddev));
1198 }
1199 
1200 static inline uint32_t
1201 PCIE_SPEED2MBS_ENC(enum pci_bus_speed spd)
1202 {
1203 
1204 	switch(spd) {
1205 	case PCIE_SPEED_64_0GT:
1206 		return (64000 * 128 / 130);
1207 	case PCIE_SPEED_32_0GT:
1208 		return (32000 * 128 / 130);
1209 	case PCIE_SPEED_16_0GT:
1210 		return (16000 * 128 / 130);
1211 	case PCIE_SPEED_8_0GT:
1212 		return (8000 * 128 / 130);
1213 	case PCIE_SPEED_5_0GT:
1214 		return (5000 * 8 / 10);
1215 	case PCIE_SPEED_2_5GT:
1216 		return (2500 * 8 / 10);
1217 	default:
1218 		return (0);
1219 	}
1220 }
1221 
1222 static inline uint32_t
1223 pcie_bandwidth_available(struct pci_dev *pdev,
1224     struct pci_dev **limiting,
1225     enum pci_bus_speed *speed,
1226     enum pcie_link_width *width)
1227 {
1228 	enum pci_bus_speed nspeed = pcie_get_speed_cap(pdev);
1229 	enum pcie_link_width nwidth = pcie_get_width_cap(pdev);
1230 
1231 	if (speed)
1232 		*speed = nspeed;
1233 	if (width)
1234 		*width = nwidth;
1235 
1236 	return (nwidth * PCIE_SPEED2MBS_ENC(nspeed));
1237 }
1238 
1239 static inline bool
1240 pcie_aspm_enabled(struct pci_dev *pdev)
1241 {
1242 	return (false);
1243 }
1244 
1245 static inline struct pci_dev *
1246 pcie_find_root_port(struct pci_dev *pdev)
1247 {
1248 	device_t root;
1249 
1250 	if (pdev->root != NULL)
1251 		return (pdev->root);
1252 
1253 	root = pci_find_pcie_root_port(pdev->dev.bsddev);
1254 	if (root == NULL)
1255 		return (NULL);
1256 
1257 	pdev->root = lkpinew_pci_dev(root);
1258 	return (pdev->root);
1259 }
1260 
1261 /* This is needed when people rip out the device "HotPlug". */
1262 static inline void
1263 pci_lock_rescan_remove(void)
1264 {
1265 }
1266 
1267 static inline void
1268 pci_unlock_rescan_remove(void)
1269 {
1270 }
1271 
1272 static __inline void
1273 pci_stop_and_remove_bus_device(struct pci_dev *pdev)
1274 {
1275 }
1276 
1277 static inline int
1278 pci_rescan_bus(struct pci_bus *pbus)
1279 {
1280 	device_t *devlist, parent;
1281 	int devcount, error;
1282 
1283 	if (!device_is_attached(pbus->self->dev.bsddev))
1284 		return (0);
1285 	/* pci_rescan_method() will work on the pcib (parent). */
1286 	error = BUS_RESCAN(pbus->self->dev.bsddev);
1287 	if (error != 0)
1288 		return (0);
1289 
1290 	parent = device_get_parent(pbus->self->dev.bsddev);
1291 	error = device_get_children(parent, &devlist, &devcount);
1292 	if (error != 0)
1293 		return (0);
1294 	if (devcount != 0)
1295 		free(devlist, M_TEMP);
1296 
1297 	return (devcount);
1298 }
1299 
1300 /*
1301  * The following functions can be used to attach/detach the LinuxKPI's
1302  * PCI device runtime. The pci_driver and pci_device_id pointer is
1303  * allowed to be NULL. Other pointers must be all valid.
1304  * The pci_dev structure should be zero-initialized before passed
1305  * to the linux_pci_attach_device function.
1306  */
1307 extern int linux_pci_attach_device(device_t, struct pci_driver *,
1308     const struct pci_device_id *, struct pci_dev *);
1309 extern int linux_pci_detach_device(struct pci_dev *);
1310 
1311 static inline int
1312 pci_dev_present(const struct pci_device_id *cur)
1313 {
1314 	while (cur != NULL && (cur->vendor || cur->device)) {
1315 		if (pci_find_device(cur->vendor, cur->device) != NULL) {
1316 			return (1);
1317 		}
1318 		cur++;
1319 	}
1320 	return (0);
1321 }
1322 
1323 static inline bool
1324 pci_dev_is_disconnected(const struct pci_dev *pdev)
1325 {
1326 	pr_debug("TODO: %s\n", __func__);
1327 	return (false);
1328 }
1329 
1330 static inline const struct pci_device_id *
1331 pci_match_id(const struct pci_device_id *ids, struct pci_dev *pdev)
1332 {
1333 	if (ids == NULL)
1334 		return (NULL);
1335 
1336 	for (;
1337 	     ids->vendor != 0 || ids->subvendor != 0 || ids->class_mask != 0;
1338 	     ids++)
1339 		if ((ids->vendor == PCI_ANY_ID ||
1340 		     ids->vendor == pdev->vendor) &&
1341 		    (ids->device == PCI_ANY_ID ||
1342 		     ids->device == pdev->device) &&
1343 		    (ids->subvendor == PCI_ANY_ID ||
1344 		     ids->subvendor == pdev->subsystem_vendor) &&
1345 		    (ids->subdevice == PCI_ANY_ID ||
1346 		     ids->subdevice == pdev->subsystem_device) &&
1347 		    ((ids->class ^ pdev->class) & ids->class_mask) == 0)
1348 			return (ids);
1349 
1350 	return (NULL);
1351 }
1352 
1353 struct pci_dev *lkpi_pci_get_domain_bus_and_slot(int domain,
1354     unsigned int bus, unsigned int devfn);
1355 #define	pci_get_domain_bus_and_slot(domain, bus, devfn)	\
1356 	lkpi_pci_get_domain_bus_and_slot(domain, bus, devfn)
1357 
1358 struct pci_dev *lkpi_pci_get_slot(struct pci_bus *, unsigned int);
1359 #ifndef	WANT_NATIVE_PCI_GET_SLOT
1360 #define	pci_get_slot(_pbus, _devfn)				\
1361     lkpi_pci_get_slot(_pbus, _devfn)
1362 #endif
1363 
1364 static inline int
1365 pci_domain_nr(struct pci_bus *pbus)
1366 {
1367 
1368 	return (pbus->domain);
1369 }
1370 
1371 static inline int
1372 pci_bus_read_config(struct pci_bus *bus, unsigned int devfn,
1373                     int pos, uint32_t *val, int len)
1374 {
1375 
1376 	*val = pci_read_config(bus->self->dev.bsddev, pos, len);
1377 	return (0);
1378 }
1379 
1380 static inline int
1381 pci_bus_read_config_word(struct pci_bus *bus, unsigned int devfn, int pos, u16 *val)
1382 {
1383 	uint32_t tmp;
1384 	int ret;
1385 
1386 	ret = pci_bus_read_config(bus, devfn, pos, &tmp, 2);
1387 	*val = (u16)tmp;
1388 	return (ret);
1389 }
1390 
1391 static inline int
1392 pci_bus_read_config_byte(struct pci_bus *bus, unsigned int devfn, int pos, u8 *val)
1393 {
1394 	uint32_t tmp;
1395 	int ret;
1396 
1397 	ret = pci_bus_read_config(bus, devfn, pos, &tmp, 1);
1398 	*val = (u8)tmp;
1399 	return (ret);
1400 }
1401 
1402 static inline int
1403 pci_bus_write_config(struct pci_bus *bus, unsigned int devfn, int pos,
1404     uint32_t val, int size)
1405 {
1406 
1407 	pci_write_config(bus->self->dev.bsddev, pos, val, size);
1408 	return (0);
1409 }
1410 
1411 static inline int
1412 pci_bus_write_config_byte(struct pci_bus *bus, unsigned int devfn, int pos,
1413     uint8_t val)
1414 {
1415 	return (pci_bus_write_config(bus, devfn, pos, val, 1));
1416 }
1417 
1418 static inline int
1419 pci_bus_write_config_word(struct pci_bus *bus, unsigned int devfn, int pos,
1420     uint16_t val)
1421 {
1422 	return (pci_bus_write_config(bus, devfn, pos, val, 2));
1423 }
1424 
1425 struct pci_dev *lkpi_pci_get_class(unsigned int class, struct pci_dev *from);
1426 #define	pci_get_class(class, from)	lkpi_pci_get_class(class, from)
1427 struct pci_dev *lkpi_pci_get_base_class(unsigned int class,
1428     struct pci_dev *from);
1429 #define	pci_get_base_class(class, from)	lkpi_pci_get_base_class(class, from)
1430 
1431 /* -------------------------------------------------------------------------- */
1432 
1433 #define	pcim_enable_device(pdev)					\
1434     linuxkpi_pcim_enable_device(pdev)
1435 #define	pcim_iomap_table(pdev)						\
1436     linuxkpi_pcim_iomap_table(pdev)
1437 #define	pcim_iomap_regions(pdev, mask, name)				\
1438     linuxkpi_pcim_iomap_regions(pdev,  mask, name)
1439 
1440 static inline int
1441 pcim_iomap_regions_request_all(struct pci_dev *pdev, uint32_t mask, char *name)
1442 {
1443 	uint32_t requests, req_mask;
1444 	int bar, error;
1445 
1446 	/* Request all the BARs ("regions") we do not iomap. */
1447 	req_mask = ((1 << (PCIR_MAX_BAR_0 + 1)) - 1) & ~mask;
1448 	for (bar = requests = 0; requests != req_mask; bar++) {
1449 		if ((req_mask & (1 << bar)) == 0)
1450 			continue;
1451 		error = pci_request_region(pdev, bar, name);
1452 		if (error != 0 && error != -ENODEV)
1453 			goto err;
1454 		requests |= (1 << bar);
1455 	}
1456 
1457 	error = pcim_iomap_regions(pdev, mask, name);
1458 	if (error != 0)
1459 		goto err;
1460 
1461 	return (0);
1462 
1463 err:
1464 	for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) {
1465 		if ((requests & (1 << bar)) != 0)
1466 			pci_release_region(pdev, bar);
1467 	}
1468 
1469 	return (-EINVAL);
1470 }
1471 
1472 /*
1473  * We cannot simply re-define pci_get_device() as we would normally do
1474  * and then hide it in linux_pci.c as too many semi-native drivers still
1475  * include linux/pci.h and run into the conflict with native PCI. Linux drivers
1476  * using pci_get_device() need to be changed to call linuxkpi_pci_get_device().
1477  */
1478 static inline struct pci_dev *
1479 linuxkpi_pci_get_device(uint32_t vendor, uint32_t device, struct pci_dev *odev)
1480 {
1481 
1482 	return (lkpi_pci_get_device(vendor, device, odev));
1483 }
1484 
1485 #define	for_each_pci_dev(_pdev)						\
1486     while ((_pdev = linuxkpi_pci_get_device(PCI_ANY_ID, PCI_ANY_ID, _pdev)) != NULL)
1487 
1488 /* This is a FreeBSD extension so we can use bus_*(). */
1489 static inline void
1490 linuxkpi_pcim_want_to_use_bus_functions(struct pci_dev *pdev)
1491 {
1492 	pdev->want_iomap_res = true;
1493 }
1494 
1495 static inline bool
1496 pci_is_thunderbolt_attached(struct pci_dev *pdev)
1497 {
1498 
1499 	return (false);
1500 }
1501 
1502 static inline void *
1503 pci_platform_rom(struct pci_dev *pdev, size_t *size)
1504 {
1505 
1506 	return (NULL);
1507 }
1508 
1509 static inline void
1510 pci_ignore_hotplug(struct pci_dev *pdev)
1511 {
1512 }
1513 
1514 static inline const char *
1515 pci_power_name(pci_power_t state)
1516 {
1517 	int pstate = state + 1;
1518 
1519 	if (pstate >= 0 && pstate < nitems(pci_power_names))
1520 		return (pci_power_names[pstate]);
1521 	else
1522 		return (pci_power_names[0]);
1523 }
1524 
1525 static inline int
1526 pcie_get_readrq(struct pci_dev *dev)
1527 {
1528 	u16 ctl;
1529 
1530 	if (pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl))
1531 		return (-EINVAL);
1532 
1533 	return (128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12));
1534 }
1535 
1536 static inline bool
1537 pci_is_enabled(struct pci_dev *pdev)
1538 {
1539 
1540 	return ((pci_read_config(pdev->dev.bsddev, PCIR_COMMAND, 2) &
1541 	    PCIM_CMD_BUSMASTEREN) != 0);
1542 }
1543 
1544 static inline int
1545 pci_wait_for_pending_transaction(struct pci_dev *pdev)
1546 {
1547 
1548 	return (0);
1549 }
1550 
1551 static inline int
1552 pci_assign_resource(struct pci_dev *pdev, int bar)
1553 {
1554 
1555 	return (0);
1556 }
1557 
1558 static inline int
1559 pci_irq_vector(struct pci_dev *pdev, unsigned int vector)
1560 {
1561 
1562 	if (!pdev->msix_enabled && !pdev->msi_enabled) {
1563 		if (vector != 0)
1564 			return (-EINVAL);
1565 		return (pdev->irq);
1566 	}
1567 
1568 	if (pdev->msix_enabled || pdev->msi_enabled) {
1569 		if ((pdev->dev.irq_start + vector) >= pdev->dev.irq_end)
1570 			return (-EINVAL);
1571 		return (pdev->dev.irq_start + vector);
1572 	}
1573 
1574         return (-ENXIO);
1575 }
1576 
1577 static inline int
1578 pci_wake_from_d3(struct pci_dev *pdev, bool enable)
1579 {
1580 
1581 	pr_debug("%s: TODO\n", __func__);
1582 	return (0);
1583 }
1584 
1585 #endif	/* _LINUXKPI_LINUX_PCI_H_ */
1586