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  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #ifndef	_LINUX_PCI_H_
32 #define	_LINUX_PCI_H_
33 
34 #define	CONFIG_PCI_MSI
35 
36 #include <linux/types.h>
37 
38 #include <sys/param.h>
39 #include <sys/bus.h>
40 #include <sys/nv.h>
41 #include <sys/pciio.h>
42 #include <sys/rman.h>
43 #include <sys/bus.h>
44 #include <dev/pci/pcivar.h>
45 #include <dev/pci/pcireg.h>
46 #include <dev/pci/pci_private.h>
47 
48 #include <machine/resource.h>
49 
50 #include <linux/list.h>
51 #include <linux/dmapool.h>
52 #include <linux/dma-mapping.h>
53 #include <linux/compiler.h>
54 #include <linux/errno.h>
55 #include <asm/atomic.h>
56 #include <linux/device.h>
57 #include <linux/pci_ids.h>
58 
59 struct pci_device_id {
60 	uint32_t	vendor;
61 	uint32_t	device;
62 	uint32_t	subvendor;
63 	uint32_t	subdevice;
64 	uint32_t	class;
65 	uint32_t	class_mask;
66 	uintptr_t	driver_data;
67 };
68 
69 #define	MODULE_DEVICE_TABLE(bus, table)
70 
71 #define	PCI_ANY_ID			-1U
72 
73 #define PCI_DEVFN(slot, func)   ((((slot) & 0x1f) << 3) | ((func) & 0x07))
74 #define PCI_SLOT(devfn)		(((devfn) >> 3) & 0x1f)
75 #define PCI_FUNC(devfn)		((devfn) & 0x07)
76 #define	PCI_BUS_NUM(devfn)	(((devfn) >> 8) & 0xff)
77 
78 #define PCI_VDEVICE(_vendor, _device)					\
79 	    .vendor = PCI_VENDOR_ID_##_vendor, .device = (_device),	\
80 	    .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID
81 #define	PCI_DEVICE(_vendor, _device)					\
82 	    .vendor = (_vendor), .device = (_device),			\
83 	    .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID
84 
85 #define	to_pci_dev(n)	container_of(n, struct pci_dev, dev)
86 
87 #define	PCI_VENDOR_ID		PCIR_DEVVENDOR
88 #define	PCI_COMMAND		PCIR_COMMAND
89 #define	PCI_EXP_DEVCTL		PCIER_DEVICE_CTL		/* Device Control */
90 #define	PCI_EXP_LNKCTL		PCIER_LINK_CTL			/* Link Control */
91 #define	PCI_EXP_FLAGS_TYPE	PCIEM_FLAGS_TYPE		/* Device/Port type */
92 #define	PCI_EXP_DEVCAP		PCIER_DEVICE_CAP		/* Device capabilities */
93 #define	PCI_EXP_DEVSTA		PCIER_DEVICE_STA		/* Device Status */
94 #define	PCI_EXP_LNKCAP		PCIER_LINK_CAP			/* Link Capabilities */
95 #define	PCI_EXP_LNKSTA		PCIER_LINK_STA			/* Link Status */
96 #define	PCI_EXP_SLTCAP		PCIER_SLOT_CAP			/* Slot Capabilities */
97 #define	PCI_EXP_SLTCTL		PCIER_SLOT_CTL			/* Slot Control */
98 #define	PCI_EXP_SLTSTA		PCIER_SLOT_STA			/* Slot Status */
99 #define	PCI_EXP_RTCTL		PCIER_ROOT_CTL			/* Root Control */
100 #define	PCI_EXP_RTCAP		PCIER_ROOT_CAP			/* Root Capabilities */
101 #define	PCI_EXP_RTSTA		PCIER_ROOT_STA			/* Root Status */
102 #define	PCI_EXP_DEVCAP2		PCIER_DEVICE_CAP2		/* Device Capabilities 2 */
103 #define	PCI_EXP_DEVCTL2		PCIER_DEVICE_CTL2		/* Device Control 2 */
104 #define	PCI_EXP_LNKCAP2		PCIER_LINK_CAP2			/* Link Capabilities 2 */
105 #define	PCI_EXP_LNKCTL2		PCIER_LINK_CTL2			/* Link Control 2 */
106 #define	PCI_EXP_LNKSTA2		PCIER_LINK_STA2			/* Link Status 2 */
107 #define	PCI_EXP_FLAGS		PCIER_FLAGS			/* Capabilities register */
108 #define	PCI_EXP_FLAGS_VERS	PCIEM_FLAGS_VERSION		/* Capability version */
109 #define	PCI_EXP_TYPE_ROOT_PORT	PCIEM_TYPE_ROOT_PORT		/* Root Port */
110 #define	PCI_EXP_TYPE_ENDPOINT	PCIEM_TYPE_ENDPOINT		/* Express Endpoint */
111 #define	PCI_EXP_TYPE_LEG_END	PCIEM_TYPE_LEGACY_ENDPOINT	/* Legacy Endpoint */
112 #define	PCI_EXP_TYPE_DOWNSTREAM PCIEM_TYPE_DOWNSTREAM_PORT	/* Downstream Port */
113 #define	PCI_EXP_FLAGS_SLOT	PCIEM_FLAGS_SLOT		/* Slot implemented */
114 #define	PCI_EXP_TYPE_RC_EC	PCIEM_TYPE_ROOT_EC		/* Root Complex Event Collector */
115 #define	PCI_EXP_LNKCAP_SLS_2_5GB 0x01	/* Supported Link Speed 2.5GT/s */
116 #define	PCI_EXP_LNKCAP_SLS_5_0GB 0x02	/* Supported Link Speed 5.0GT/s */
117 #define	PCI_EXP_LNKCAP_SLS_8_0GB 0x04	/* Supported Link Speed 8.0GT/s */
118 #define	PCI_EXP_LNKCAP_SLS_16_0GB 0x08	/* Supported Link Speed 16.0GT/s */
119 #define	PCI_EXP_LNKCAP_MLW	0x03f0	/* Maximum Link Width */
120 #define	PCI_EXP_LNKCAP2_SLS_2_5GB 0x02	/* Supported Link Speed 2.5GT/s */
121 #define	PCI_EXP_LNKCAP2_SLS_5_0GB 0x04	/* Supported Link Speed 5.0GT/s */
122 #define	PCI_EXP_LNKCAP2_SLS_8_0GB 0x08	/* Supported Link Speed 8.0GT/s */
123 #define	PCI_EXP_LNKCAP2_SLS_16_0GB 0x10	/* Supported Link Speed 16.0GT/s */
124 
125 #define PCI_EXP_LNKCTL_HAWD	PCIEM_LINK_CTL_HAWD
126 #define PCI_EXP_LNKCAP_CLKPM	0x00040000
127 #define PCI_EXP_DEVSTA_TRPND	0x0020
128 
129 #define	IORESOURCE_MEM	(1 << SYS_RES_MEMORY)
130 #define	IORESOURCE_IO	(1 << SYS_RES_IOPORT)
131 #define	IORESOURCE_IRQ	(1 << SYS_RES_IRQ)
132 
133 enum pci_bus_speed {
134 	PCI_SPEED_UNKNOWN = -1,
135 	PCIE_SPEED_2_5GT,
136 	PCIE_SPEED_5_0GT,
137 	PCIE_SPEED_8_0GT,
138 	PCIE_SPEED_16_0GT,
139 };
140 
141 enum pcie_link_width {
142 	PCIE_LNK_WIDTH_RESRV	= 0x00,
143 	PCIE_LNK_X1		= 0x01,
144 	PCIE_LNK_X2		= 0x02,
145 	PCIE_LNK_X4		= 0x04,
146 	PCIE_LNK_X8		= 0x08,
147 	PCIE_LNK_X12		= 0x0c,
148 	PCIE_LNK_X16		= 0x10,
149 	PCIE_LNK_X32		= 0x20,
150 	PCIE_LNK_WIDTH_UNKNOWN	= 0xff,
151 };
152 
153 typedef int pci_power_t;
154 
155 #define PCI_D0	PCI_POWERSTATE_D0
156 #define PCI_D1	PCI_POWERSTATE_D1
157 #define PCI_D2	PCI_POWERSTATE_D2
158 #define PCI_D3hot	PCI_POWERSTATE_D3
159 #define PCI_D3cold	4
160 
161 #define PCI_POWER_ERROR	PCI_POWERSTATE_UNKNOWN
162 
163 struct pci_dev;
164 
165 struct pci_driver {
166 	struct list_head		links;
167 	char				*name;
168 	const struct pci_device_id		*id_table;
169 	int  (*probe)(struct pci_dev *dev, const struct pci_device_id *id);
170 	void (*remove)(struct pci_dev *dev);
171 	int  (*suspend) (struct pci_dev *dev, pm_message_t state);	/* Device suspended */
172 	int  (*resume) (struct pci_dev *dev);		/* Device woken up */
173 	void (*shutdown) (struct pci_dev *dev);		/* Device shutdown */
174 	driver_t			bsddriver;
175 	devclass_t			bsdclass;
176 	struct device_driver		driver;
177 	const struct pci_error_handlers       *err_handler;
178 	bool				isdrm;
179 	int  (*bsd_iov_init)(device_t dev, uint16_t num_vfs,
180 	    const nvlist_t *pf_config);
181 	void  (*bsd_iov_uninit)(device_t dev);
182 	int  (*bsd_iov_add_vf)(device_t dev, uint16_t vfnum,
183 	    const nvlist_t *vf_config);
184 };
185 
186 struct pci_bus {
187 	struct pci_dev	*self;
188 	int		domain;
189 	int		number;
190 };
191 
192 extern struct list_head pci_drivers;
193 extern struct list_head pci_devices;
194 extern spinlock_t pci_lock;
195 
196 #define	__devexit_p(x)	x
197 
198 struct pci_mmio_region {
199 	TAILQ_ENTRY(pci_mmio_region)	next;
200 	struct resource			*res;
201 	int				rid;
202 	int				type;
203 };
204 
205 struct pci_dev {
206 	struct device		dev;
207 	struct list_head	links;
208 	struct pci_driver	*pdrv;
209 	struct pci_bus		*bus;
210 	uint16_t		device;
211 	uint16_t		vendor;
212 	uint16_t		subsystem_vendor;
213 	uint16_t		subsystem_device;
214 	unsigned int		irq;
215 	unsigned int		devfn;
216 	uint32_t		class;
217 	uint8_t			revision;
218 	bool			msi_enabled;
219 	phys_addr_t		rom;
220 	size_t			romlen;
221 
222 	TAILQ_HEAD(, pci_mmio_region)	mmio;
223 };
224 
225 static inline struct resource_list_entry *
226 linux_pci_get_rle(struct pci_dev *pdev, int type, int rid)
227 {
228 	struct pci_devinfo *dinfo;
229 	struct resource_list *rl;
230 
231 	dinfo = device_get_ivars(pdev->dev.bsddev);
232 	rl = &dinfo->resources;
233 	return resource_list_find(rl, type, rid);
234 }
235 
236 static inline struct resource_list_entry *
237 linux_pci_get_bar(struct pci_dev *pdev, int bar)
238 {
239 	struct resource_list_entry *rle;
240 
241 	bar = PCIR_BAR(bar);
242 	if ((rle = linux_pci_get_rle(pdev, SYS_RES_MEMORY, bar)) == NULL)
243 		rle = linux_pci_get_rle(pdev, SYS_RES_IOPORT, bar);
244 	return (rle);
245 }
246 
247 static inline struct device *
248 linux_pci_find_irq_dev(unsigned int irq)
249 {
250 	struct pci_dev *pdev;
251 	struct device *found;
252 
253 	found = NULL;
254 	spin_lock(&pci_lock);
255 	list_for_each_entry(pdev, &pci_devices, links) {
256 		if (irq == pdev->dev.irq ||
257 		    (irq >= pdev->dev.irq_start && irq < pdev->dev.irq_end)) {
258 			found = &pdev->dev;
259 			break;
260 		}
261 	}
262 	spin_unlock(&pci_lock);
263 	return (found);
264 }
265 
266 static inline int
267 pci_resource_type(struct pci_dev *pdev, int bar)
268 {
269 	struct pci_map *pm;
270 
271 	pm = pci_find_bar(pdev->dev.bsddev, PCIR_BAR(bar));
272 	if (!pm)
273 		return (-1);
274 
275 	if (PCI_BAR_IO(pm->pm_value))
276 		return (SYS_RES_IOPORT);
277 	else
278 		return (SYS_RES_MEMORY);
279 }
280 
281 /*
282  * All drivers just seem to want to inspect the type not flags.
283  */
284 static inline int
285 pci_resource_flags(struct pci_dev *pdev, int bar)
286 {
287 	int type;
288 
289 	type = pci_resource_type(pdev, bar);
290 	if (type < 0)
291 		return (0);
292 	return (1 << type);
293 }
294 
295 static inline const char *
296 pci_name(struct pci_dev *d)
297 {
298 
299 	return device_get_desc(d->dev.bsddev);
300 }
301 
302 static inline void *
303 pci_get_drvdata(struct pci_dev *pdev)
304 {
305 
306 	return dev_get_drvdata(&pdev->dev);
307 }
308 
309 static inline void
310 pci_set_drvdata(struct pci_dev *pdev, void *data)
311 {
312 
313 	dev_set_drvdata(&pdev->dev, data);
314 }
315 
316 static __inline void
317 pci_dev_put(struct pci_dev *pdev)
318 {
319 
320 	if (pdev != NULL)
321 		put_device(&pdev->dev);
322 }
323 
324 static inline int
325 pci_enable_device(struct pci_dev *pdev)
326 {
327 
328 	pci_enable_io(pdev->dev.bsddev, SYS_RES_IOPORT);
329 	pci_enable_io(pdev->dev.bsddev, SYS_RES_MEMORY);
330 	return (0);
331 }
332 
333 static inline void
334 pci_disable_device(struct pci_dev *pdev)
335 {
336 
337 	pci_disable_busmaster(pdev->dev.bsddev);
338 }
339 
340 static inline int
341 pci_set_master(struct pci_dev *pdev)
342 {
343 
344 	pci_enable_busmaster(pdev->dev.bsddev);
345 	return (0);
346 }
347 
348 static inline int
349 pci_set_power_state(struct pci_dev *pdev, int state)
350 {
351 
352 	pci_set_powerstate(pdev->dev.bsddev, state);
353 	return (0);
354 }
355 
356 static inline int
357 pci_clear_master(struct pci_dev *pdev)
358 {
359 
360 	pci_disable_busmaster(pdev->dev.bsddev);
361 	return (0);
362 }
363 
364 static inline int
365 pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
366 {
367 	int rid;
368 	int type;
369 
370 	type = pci_resource_type(pdev, bar);
371 	if (type < 0)
372 		return (-ENODEV);
373 	rid = PCIR_BAR(bar);
374 	if (bus_alloc_resource_any(pdev->dev.bsddev, type, &rid,
375 	    RF_ACTIVE) == NULL)
376 		return (-EINVAL);
377 	return (0);
378 }
379 
380 static inline void
381 pci_release_region(struct pci_dev *pdev, int bar)
382 {
383 	struct resource_list_entry *rle;
384 
385 	if ((rle = linux_pci_get_bar(pdev, bar)) == NULL)
386 		return;
387 	bus_release_resource(pdev->dev.bsddev, rle->type, rle->rid, rle->res);
388 }
389 
390 static inline void
391 pci_release_regions(struct pci_dev *pdev)
392 {
393 	int i;
394 
395 	for (i = 0; i <= PCIR_MAX_BAR_0; i++)
396 		pci_release_region(pdev, i);
397 }
398 
399 static inline int
400 pci_request_regions(struct pci_dev *pdev, const char *res_name)
401 {
402 	int error;
403 	int i;
404 
405 	for (i = 0; i <= PCIR_MAX_BAR_0; i++) {
406 		error = pci_request_region(pdev, i, res_name);
407 		if (error && error != -ENODEV) {
408 			pci_release_regions(pdev);
409 			return (error);
410 		}
411 	}
412 	return (0);
413 }
414 
415 static inline void
416 pci_disable_msix(struct pci_dev *pdev)
417 {
418 
419 	pci_release_msi(pdev->dev.bsddev);
420 
421 	/*
422 	 * The MSIX IRQ numbers associated with this PCI device are no
423 	 * longer valid and might be re-assigned. Make sure
424 	 * linux_pci_find_irq_dev() does no longer see them by
425 	 * resetting their references to zero:
426 	 */
427 	pdev->dev.irq_start = 0;
428 	pdev->dev.irq_end = 0;
429 }
430 
431 #define	pci_disable_msi(pdev) \
432   linux_pci_disable_msi(pdev)
433 
434 static inline void
435 linux_pci_disable_msi(struct pci_dev *pdev)
436 {
437 
438 	pci_release_msi(pdev->dev.bsddev);
439 
440 	pdev->dev.irq_start = 0;
441 	pdev->dev.irq_end = 0;
442 	pdev->irq = pdev->dev.irq;
443 	pdev->msi_enabled = false;
444 }
445 
446 #define	pci_free_irq_vectors(pdev) \
447 	linux_pci_disable_msi(pdev)
448 
449 unsigned long	pci_resource_start(struct pci_dev *pdev, int bar);
450 unsigned long	pci_resource_len(struct pci_dev *pdev, int bar);
451 
452 static inline bus_addr_t
453 pci_bus_address(struct pci_dev *pdev, int bar)
454 {
455 
456 	return (pci_resource_start(pdev, bar));
457 }
458 
459 #define	PCI_CAP_ID_EXP	PCIY_EXPRESS
460 #define	PCI_CAP_ID_PCIX	PCIY_PCIX
461 #define PCI_CAP_ID_AGP  PCIY_AGP
462 #define PCI_CAP_ID_PM   PCIY_PMG
463 
464 #define PCI_EXP_DEVCTL		PCIER_DEVICE_CTL
465 #define PCI_EXP_DEVCTL_PAYLOAD	PCIEM_CTL_MAX_PAYLOAD
466 #define PCI_EXP_DEVCTL_READRQ	PCIEM_CTL_MAX_READ_REQUEST
467 #define PCI_EXP_LNKCTL		PCIER_LINK_CTL
468 #define PCI_EXP_LNKSTA		PCIER_LINK_STA
469 
470 static inline int
471 pci_find_capability(struct pci_dev *pdev, int capid)
472 {
473 	int reg;
474 
475 	if (pci_find_cap(pdev->dev.bsddev, capid, &reg))
476 		return (0);
477 	return (reg);
478 }
479 
480 static inline int pci_pcie_cap(struct pci_dev *dev)
481 {
482 	return pci_find_capability(dev, PCI_CAP_ID_EXP);
483 }
484 
485 static inline int
486 pci_read_config_byte(struct pci_dev *pdev, int where, u8 *val)
487 {
488 
489 	*val = (u8)pci_read_config(pdev->dev.bsddev, where, 1);
490 	return (0);
491 }
492 
493 static inline int
494 pci_read_config_word(struct pci_dev *pdev, int where, u16 *val)
495 {
496 
497 	*val = (u16)pci_read_config(pdev->dev.bsddev, where, 2);
498 	return (0);
499 }
500 
501 static inline int
502 pci_read_config_dword(struct pci_dev *pdev, int where, u32 *val)
503 {
504 
505 	*val = (u32)pci_read_config(pdev->dev.bsddev, where, 4);
506 	return (0);
507 }
508 
509 static inline int
510 pci_write_config_byte(struct pci_dev *pdev, int where, u8 val)
511 {
512 
513 	pci_write_config(pdev->dev.bsddev, where, val, 1);
514 	return (0);
515 }
516 
517 static inline int
518 pci_write_config_word(struct pci_dev *pdev, int where, u16 val)
519 {
520 
521 	pci_write_config(pdev->dev.bsddev, where, val, 2);
522 	return (0);
523 }
524 
525 static inline int
526 pci_write_config_dword(struct pci_dev *pdev, int where, u32 val)
527 {
528 
529 	pci_write_config(pdev->dev.bsddev, where, val, 4);
530 	return (0);
531 }
532 
533 int	linux_pci_register_driver(struct pci_driver *pdrv);
534 int	linux_pci_register_drm_driver(struct pci_driver *pdrv);
535 void	linux_pci_unregister_driver(struct pci_driver *pdrv);
536 void	linux_pci_unregister_drm_driver(struct pci_driver *pdrv);
537 
538 #define	pci_register_driver(pdrv)	linux_pci_register_driver(pdrv)
539 #define	pci_unregister_driver(pdrv)	linux_pci_unregister_driver(pdrv)
540 
541 struct msix_entry {
542 	int entry;
543 	int vector;
544 };
545 
546 /*
547  * Enable msix, positive errors indicate actual number of available
548  * vectors.  Negative errors are failures.
549  *
550  * NB: define added to prevent this definition of pci_enable_msix from
551  * clashing with the native FreeBSD version.
552  */
553 #define	pci_enable_msix(...) \
554   linux_pci_enable_msix(__VA_ARGS__)
555 
556 static inline int
557 pci_enable_msix(struct pci_dev *pdev, struct msix_entry *entries, int nreq)
558 {
559 	struct resource_list_entry *rle;
560 	int error;
561 	int avail;
562 	int i;
563 
564 	avail = pci_msix_count(pdev->dev.bsddev);
565 	if (avail < nreq) {
566 		if (avail == 0)
567 			return -EINVAL;
568 		return avail;
569 	}
570 	avail = nreq;
571 	if ((error = -pci_alloc_msix(pdev->dev.bsddev, &avail)) != 0)
572 		return error;
573 	/*
574 	 * Handle case where "pci_alloc_msix()" may allocate less
575 	 * interrupts than available and return with no error:
576 	 */
577 	if (avail < nreq) {
578 		pci_release_msi(pdev->dev.bsddev);
579 		return avail;
580 	}
581 	rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 1);
582 	pdev->dev.irq_start = rle->start;
583 	pdev->dev.irq_end = rle->start + avail;
584 	for (i = 0; i < nreq; i++)
585 		entries[i].vector = pdev->dev.irq_start + i;
586 	return (0);
587 }
588 
589 #define	pci_enable_msix_range(...) \
590   linux_pci_enable_msix_range(__VA_ARGS__)
591 
592 static inline int
593 pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
594     int minvec, int maxvec)
595 {
596 	int nvec = maxvec;
597 	int rc;
598 
599 	if (maxvec < minvec)
600 		return (-ERANGE);
601 
602 	do {
603 		rc = pci_enable_msix(dev, entries, nvec);
604 		if (rc < 0) {
605 			return (rc);
606 		} else if (rc > 0) {
607 			if (rc < minvec)
608 				return (-ENOSPC);
609 			nvec = rc;
610 		}
611 	} while (rc);
612 	return (nvec);
613 }
614 
615 #define	pci_enable_msi(pdev) \
616   linux_pci_enable_msi(pdev)
617 
618 static inline int
619 pci_enable_msi(struct pci_dev *pdev)
620 {
621 	struct resource_list_entry *rle;
622 	int error;
623 	int avail;
624 
625 	avail = pci_msi_count(pdev->dev.bsddev);
626 	if (avail < 1)
627 		return -EINVAL;
628 
629 	avail = 1;	/* this function only enable one MSI IRQ */
630 	if ((error = -pci_alloc_msi(pdev->dev.bsddev, &avail)) != 0)
631 		return error;
632 
633 	rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 1);
634 	pdev->dev.irq_start = rle->start;
635 	pdev->dev.irq_end = rle->start + avail;
636 	pdev->irq = rle->start;
637 	pdev->msi_enabled = true;
638 	return (0);
639 }
640 
641 static inline int
642 pci_channel_offline(struct pci_dev *pdev)
643 {
644 
645 	return (pci_read_config(pdev->dev.bsddev, PCIR_VENDOR, 2) == PCIV_INVALID);
646 }
647 
648 static inline int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
649 {
650 	return -ENODEV;
651 }
652 static inline void pci_disable_sriov(struct pci_dev *dev)
653 {
654 }
655 
656 static inline void *
657 pci_iomap(struct pci_dev *dev, int mmio_bar, int mmio_size __unused)
658 {
659 	struct pci_mmio_region *mmio;
660 
661 	mmio = malloc(sizeof(*mmio), M_DEVBUF, M_WAITOK | M_ZERO);
662 	mmio->rid = PCIR_BAR(mmio_bar);
663 	mmio->type = pci_resource_type(dev, mmio_bar);
664 	mmio->res = bus_alloc_resource_any(dev->dev.bsddev, mmio->type,
665 	    &mmio->rid, RF_ACTIVE);
666 	if (mmio->res == NULL) {
667 		free(mmio, M_DEVBUF);
668 		return (NULL);
669 	}
670 	TAILQ_INSERT_TAIL(&dev->mmio, mmio, next);
671 
672 	return ((void *)rman_get_bushandle(mmio->res));
673 }
674 
675 static inline void
676 pci_iounmap(struct pci_dev *dev, void *res)
677 {
678 	struct pci_mmio_region *mmio, *p;
679 
680 	TAILQ_FOREACH_SAFE(mmio, &dev->mmio, next, p) {
681 		if (res != (void *)rman_get_bushandle(mmio->res))
682 			continue;
683 		bus_release_resource(dev->dev.bsddev,
684 		    mmio->type, mmio->rid, mmio->res);
685 		TAILQ_REMOVE(&dev->mmio, mmio, next);
686 		free(mmio, M_DEVBUF);
687 		return;
688 	}
689 }
690 
691 static inline void
692 lkpi_pci_save_state(struct pci_dev *pdev)
693 {
694 
695 	pci_save_state(pdev->dev.bsddev);
696 }
697 
698 static inline void
699 lkpi_pci_restore_state(struct pci_dev *pdev)
700 {
701 
702 	pci_restore_state(pdev->dev.bsddev);
703 }
704 
705 #define pci_save_state(dev)	lkpi_pci_save_state(dev)
706 #define pci_restore_state(dev)	lkpi_pci_restore_state(dev)
707 
708 #define DEFINE_PCI_DEVICE_TABLE(_table) \
709 	const struct pci_device_id _table[] __devinitdata
710 
711 /* XXX This should not be necessary. */
712 #define	pcix_set_mmrbc(d, v)	0
713 #define	pcix_get_max_mmrbc(d)	0
714 #define	pcie_set_readrq(d, v)	pci_set_max_read_req((d)->dev.bsddev, (v))
715 
716 #define	PCI_DMA_BIDIRECTIONAL	0
717 #define	PCI_DMA_TODEVICE	1
718 #define	PCI_DMA_FROMDEVICE	2
719 #define	PCI_DMA_NONE		3
720 
721 #define	pci_pool		dma_pool
722 #define	pci_pool_destroy(...)	dma_pool_destroy(__VA_ARGS__)
723 #define	pci_pool_alloc(...)	dma_pool_alloc(__VA_ARGS__)
724 #define	pci_pool_free(...)	dma_pool_free(__VA_ARGS__)
725 #define	pci_pool_create(_name, _pdev, _size, _align, _alloc)		\
726 	    dma_pool_create(_name, &(_pdev)->dev, _size, _align, _alloc)
727 #define	pci_free_consistent(_hwdev, _size, _vaddr, _dma_handle)		\
728 	    dma_free_coherent((_hwdev) == NULL ? NULL : &(_hwdev)->dev,	\
729 		_size, _vaddr, _dma_handle)
730 #define	pci_map_sg(_hwdev, _sg, _nents, _dir)				\
731 	    dma_map_sg((_hwdev) == NULL ? NULL : &(_hwdev->dev),	\
732 		_sg, _nents, (enum dma_data_direction)_dir)
733 #define	pci_map_single(_hwdev, _ptr, _size, _dir)			\
734 	    dma_map_single((_hwdev) == NULL ? NULL : &(_hwdev->dev),	\
735 		(_ptr), (_size), (enum dma_data_direction)_dir)
736 #define	pci_unmap_single(_hwdev, _addr, _size, _dir)			\
737 	    dma_unmap_single((_hwdev) == NULL ? NULL : &(_hwdev)->dev,	\
738 		_addr, _size, (enum dma_data_direction)_dir)
739 #define	pci_unmap_sg(_hwdev, _sg, _nents, _dir)				\
740 	    dma_unmap_sg((_hwdev) == NULL ? NULL : &(_hwdev)->dev,	\
741 		_sg, _nents, (enum dma_data_direction)_dir)
742 #define	pci_map_page(_hwdev, _page, _offset, _size, _dir)		\
743 	    dma_map_page((_hwdev) == NULL ? NULL : &(_hwdev)->dev, _page,\
744 		_offset, _size, (enum dma_data_direction)_dir)
745 #define	pci_unmap_page(_hwdev, _dma_address, _size, _dir)		\
746 	    dma_unmap_page((_hwdev) == NULL ? NULL : &(_hwdev)->dev,	\
747 		_dma_address, _size, (enum dma_data_direction)_dir)
748 #define	pci_set_dma_mask(_pdev, mask)	dma_set_mask(&(_pdev)->dev, (mask))
749 #define	pci_dma_mapping_error(_pdev, _dma_addr)				\
750 	    dma_mapping_error(&(_pdev)->dev, _dma_addr)
751 #define	pci_set_consistent_dma_mask(_pdev, _mask)			\
752 	    dma_set_coherent_mask(&(_pdev)->dev, (_mask))
753 #define	DECLARE_PCI_UNMAP_ADDR(x)	DEFINE_DMA_UNMAP_ADDR(x);
754 #define	DECLARE_PCI_UNMAP_LEN(x)	DEFINE_DMA_UNMAP_LEN(x);
755 #define	pci_unmap_addr		dma_unmap_addr
756 #define	pci_unmap_addr_set	dma_unmap_addr_set
757 #define	pci_unmap_len		dma_unmap_len
758 #define	pci_unmap_len_set	dma_unmap_len_set
759 
760 typedef unsigned int __bitwise pci_channel_state_t;
761 typedef unsigned int __bitwise pci_ers_result_t;
762 
763 enum pci_channel_state {
764 	pci_channel_io_normal = 1,
765 	pci_channel_io_frozen = 2,
766 	pci_channel_io_perm_failure = 3,
767 };
768 
769 enum pci_ers_result {
770 	PCI_ERS_RESULT_NONE = 1,
771 	PCI_ERS_RESULT_CAN_RECOVER = 2,
772 	PCI_ERS_RESULT_NEED_RESET = 3,
773 	PCI_ERS_RESULT_DISCONNECT = 4,
774 	PCI_ERS_RESULT_RECOVERED = 5,
775 };
776 
777 /* PCI bus error event callbacks */
778 struct pci_error_handlers {
779 	pci_ers_result_t (*error_detected)(struct pci_dev *dev,
780 	    enum pci_channel_state error);
781 	pci_ers_result_t (*mmio_enabled)(struct pci_dev *dev);
782 	pci_ers_result_t (*link_reset)(struct pci_dev *dev);
783 	pci_ers_result_t (*slot_reset)(struct pci_dev *dev);
784 	void (*resume)(struct pci_dev *dev);
785 };
786 
787 /* FreeBSD does not support SRIOV - yet */
788 static inline struct pci_dev *pci_physfn(struct pci_dev *dev)
789 {
790 	return dev;
791 }
792 
793 static inline bool pci_is_pcie(struct pci_dev *dev)
794 {
795 	return !!pci_pcie_cap(dev);
796 }
797 
798 static inline u16 pcie_flags_reg(struct pci_dev *dev)
799 {
800 	int pos;
801 	u16 reg16;
802 
803 	pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
804 	if (!pos)
805 		return 0;
806 
807 	pci_read_config_word(dev, pos + PCI_EXP_FLAGS, &reg16);
808 
809 	return reg16;
810 }
811 
812 static inline int pci_pcie_type(struct pci_dev *dev)
813 {
814 	return (pcie_flags_reg(dev) & PCI_EXP_FLAGS_TYPE) >> 4;
815 }
816 
817 static inline int pcie_cap_version(struct pci_dev *dev)
818 {
819 	return pcie_flags_reg(dev) & PCI_EXP_FLAGS_VERS;
820 }
821 
822 static inline bool pcie_cap_has_lnkctl(struct pci_dev *dev)
823 {
824 	int type = pci_pcie_type(dev);
825 
826 	return pcie_cap_version(dev) > 1 ||
827 	       type == PCI_EXP_TYPE_ROOT_PORT ||
828 	       type == PCI_EXP_TYPE_ENDPOINT ||
829 	       type == PCI_EXP_TYPE_LEG_END;
830 }
831 
832 static inline bool pcie_cap_has_devctl(const struct pci_dev *dev)
833 {
834 		return true;
835 }
836 
837 static inline bool pcie_cap_has_sltctl(struct pci_dev *dev)
838 {
839 	int type = pci_pcie_type(dev);
840 
841 	return pcie_cap_version(dev) > 1 || type == PCI_EXP_TYPE_ROOT_PORT ||
842 	    (type == PCI_EXP_TYPE_DOWNSTREAM &&
843 	    pcie_flags_reg(dev) & PCI_EXP_FLAGS_SLOT);
844 }
845 
846 static inline bool pcie_cap_has_rtctl(struct pci_dev *dev)
847 {
848 	int type = pci_pcie_type(dev);
849 
850 	return pcie_cap_version(dev) > 1 || type == PCI_EXP_TYPE_ROOT_PORT ||
851 	    type == PCI_EXP_TYPE_RC_EC;
852 }
853 
854 static bool pcie_capability_reg_implemented(struct pci_dev *dev, int pos)
855 {
856 	if (!pci_is_pcie(dev))
857 		return false;
858 
859 	switch (pos) {
860 	case PCI_EXP_FLAGS_TYPE:
861 		return true;
862 	case PCI_EXP_DEVCAP:
863 	case PCI_EXP_DEVCTL:
864 	case PCI_EXP_DEVSTA:
865 		return pcie_cap_has_devctl(dev);
866 	case PCI_EXP_LNKCAP:
867 	case PCI_EXP_LNKCTL:
868 	case PCI_EXP_LNKSTA:
869 		return pcie_cap_has_lnkctl(dev);
870 	case PCI_EXP_SLTCAP:
871 	case PCI_EXP_SLTCTL:
872 	case PCI_EXP_SLTSTA:
873 		return pcie_cap_has_sltctl(dev);
874 	case PCI_EXP_RTCTL:
875 	case PCI_EXP_RTCAP:
876 	case PCI_EXP_RTSTA:
877 		return pcie_cap_has_rtctl(dev);
878 	case PCI_EXP_DEVCAP2:
879 	case PCI_EXP_DEVCTL2:
880 	case PCI_EXP_LNKCAP2:
881 	case PCI_EXP_LNKCTL2:
882 	case PCI_EXP_LNKSTA2:
883 		return pcie_cap_version(dev) > 1;
884 	default:
885 		return false;
886 	}
887 }
888 
889 static inline int
890 pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *dst)
891 {
892 	if (pos & 3)
893 		return -EINVAL;
894 
895 	if (!pcie_capability_reg_implemented(dev, pos))
896 		return -EINVAL;
897 
898 	return pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, dst);
899 }
900 
901 static inline int
902 pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *dst)
903 {
904 	if (pos & 3)
905 		return -EINVAL;
906 
907 	if (!pcie_capability_reg_implemented(dev, pos))
908 		return -EINVAL;
909 
910 	return pci_read_config_word(dev, pci_pcie_cap(dev) + pos, dst);
911 }
912 
913 static inline int
914 pcie_capability_write_word(struct pci_dev *dev, int pos, u16 val)
915 {
916 	if (pos & 1)
917 		return -EINVAL;
918 
919 	if (!pcie_capability_reg_implemented(dev, pos))
920 		return 0;
921 
922 	return pci_write_config_word(dev, pci_pcie_cap(dev) + pos, val);
923 }
924 
925 static inline int pcie_get_minimum_link(struct pci_dev *dev,
926     enum pci_bus_speed *speed, enum pcie_link_width *width)
927 {
928 	*speed = PCI_SPEED_UNKNOWN;
929 	*width = PCIE_LNK_WIDTH_UNKNOWN;
930 	return (0);
931 }
932 
933 static inline int
934 pci_num_vf(struct pci_dev *dev)
935 {
936 	return (0);
937 }
938 
939 static inline enum pci_bus_speed
940 pcie_get_speed_cap(struct pci_dev *dev)
941 {
942 	device_t root;
943 	uint32_t lnkcap, lnkcap2;
944 	int error, pos;
945 
946 	root = device_get_parent(dev->dev.bsddev);
947 	if (root == NULL)
948 		return (PCI_SPEED_UNKNOWN);
949 	root = device_get_parent(root);
950 	if (root == NULL)
951 		return (PCI_SPEED_UNKNOWN);
952 	root = device_get_parent(root);
953 	if (root == NULL)
954 		return (PCI_SPEED_UNKNOWN);
955 
956 	if (pci_get_vendor(root) == PCI_VENDOR_ID_VIA ||
957 	    pci_get_vendor(root) == PCI_VENDOR_ID_SERVERWORKS)
958 		return (PCI_SPEED_UNKNOWN);
959 
960 	if ((error = pci_find_cap(root, PCIY_EXPRESS, &pos)) != 0)
961 		return (PCI_SPEED_UNKNOWN);
962 
963 	lnkcap2 = pci_read_config(root, pos + PCIER_LINK_CAP2, 4);
964 
965 	if (lnkcap2) {	/* PCIe r3.0-compliant */
966 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
967 			return (PCIE_SPEED_2_5GT);
968 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
969 			return (PCIE_SPEED_5_0GT);
970 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
971 			return (PCIE_SPEED_8_0GT);
972 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_16_0GB)
973 			return (PCIE_SPEED_16_0GT);
974 	} else {	/* pre-r3.0 */
975 		lnkcap = pci_read_config(root, pos + PCIER_LINK_CAP, 4);
976 		if (lnkcap & PCI_EXP_LNKCAP_SLS_2_5GB)
977 			return (PCIE_SPEED_2_5GT);
978 		if (lnkcap & PCI_EXP_LNKCAP_SLS_5_0GB)
979 			return (PCIE_SPEED_5_0GT);
980 		if (lnkcap & PCI_EXP_LNKCAP_SLS_8_0GB)
981 			return (PCIE_SPEED_8_0GT);
982 		if (lnkcap & PCI_EXP_LNKCAP_SLS_16_0GB)
983 			return (PCIE_SPEED_16_0GT);
984 	}
985 	return (PCI_SPEED_UNKNOWN);
986 }
987 
988 static inline enum pcie_link_width
989 pcie_get_width_cap(struct pci_dev *dev)
990 {
991 	uint32_t lnkcap;
992 
993 	pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &lnkcap);
994 	if (lnkcap)
995 		return ((lnkcap & PCI_EXP_LNKCAP_MLW) >> 4);
996 
997 	return (PCIE_LNK_WIDTH_UNKNOWN);
998 }
999 
1000 static inline int
1001 pcie_get_mps(struct pci_dev *dev)
1002 {
1003 	return (pci_get_max_payload(dev->dev.bsddev));
1004 }
1005 
1006 static inline uint32_t
1007 PCIE_SPEED2MBS_ENC(enum pci_bus_speed spd)
1008 {
1009 
1010 	switch(spd) {
1011 	case PCIE_SPEED_16_0GT:
1012 		return (16000 * 128 / 130);
1013 	case PCIE_SPEED_8_0GT:
1014 		return (8000 * 128 / 130);
1015 	case PCIE_SPEED_5_0GT:
1016 		return (5000 * 8 / 10);
1017 	case PCIE_SPEED_2_5GT:
1018 		return (2500 * 8 / 10);
1019 	default:
1020 		return (0);
1021 	}
1022 }
1023 
1024 static inline uint32_t
1025 pcie_bandwidth_available(struct pci_dev *pdev,
1026     struct pci_dev **limiting,
1027     enum pci_bus_speed *speed,
1028     enum pcie_link_width *width)
1029 {
1030 	enum pci_bus_speed nspeed = pcie_get_speed_cap(pdev);
1031 	enum pcie_link_width nwidth = pcie_get_width_cap(pdev);
1032 
1033 	if (speed)
1034 		*speed = nspeed;
1035 	if (width)
1036 		*width = nwidth;
1037 
1038 	return (nwidth * PCIE_SPEED2MBS_ENC(nspeed));
1039 }
1040 
1041 /*
1042  * The following functions can be used to attach/detach the LinuxKPI's
1043  * PCI device runtime. The pci_driver and pci_device_id pointer is
1044  * allowed to be NULL. Other pointers must be all valid.
1045  * The pci_dev structure should be zero-initialized before passed
1046  * to the linux_pci_attach_device function.
1047  */
1048 extern int linux_pci_attach_device(device_t, struct pci_driver *,
1049     const struct pci_device_id *, struct pci_dev *);
1050 extern int linux_pci_detach_device(struct pci_dev *);
1051 
1052 static inline int
1053 pci_dev_present(const struct pci_device_id *cur)
1054 {
1055 	while (cur != NULL && (cur->vendor || cur->device)) {
1056 		if (pci_find_device(cur->vendor, cur->device) != NULL) {
1057 			return (1);
1058 		}
1059 		cur++;
1060 	}
1061 	return (0);
1062 }
1063 
1064 static inline bool
1065 pci_is_root_bus(struct pci_bus *pbus)
1066 {
1067 
1068 	return (pbus->self == NULL);
1069 }
1070 
1071 struct pci_dev *lkpi_pci_get_domain_bus_and_slot(int domain,
1072     unsigned int bus, unsigned int devfn);
1073 #define	pci_get_domain_bus_and_slot(domain, bus, devfn)	\
1074 	lkpi_pci_get_domain_bus_and_slot(domain, bus, devfn)
1075 
1076 static inline int
1077 pci_domain_nr(struct pci_bus *pbus)
1078 {
1079 
1080 	return (pbus->domain);
1081 }
1082 
1083 static inline int
1084 pci_bus_read_config(struct pci_bus *bus, unsigned int devfn,
1085                     int pos, uint32_t *val, int len)
1086 {
1087 
1088 	*val = pci_read_config(bus->self->dev.bsddev, pos, len);
1089 	return (0);
1090 }
1091 
1092 static inline int
1093 pci_bus_read_config_word(struct pci_bus *bus, unsigned int devfn, int pos, u16 *val)
1094 {
1095 	uint32_t tmp;
1096 	int ret;
1097 
1098 	ret = pci_bus_read_config(bus, devfn, pos, &tmp, 2);
1099 	*val = (u16)tmp;
1100 	return (ret);
1101 }
1102 
1103 static inline int
1104 pci_bus_read_config_byte(struct pci_bus *bus, unsigned int devfn, int pos, u8 *val)
1105 {
1106 	uint32_t tmp;
1107 	int ret;
1108 
1109 	ret = pci_bus_read_config(bus, devfn, pos, &tmp, 1);
1110 	*val = (u8)tmp;
1111 	return (ret);
1112 }
1113 
1114 static inline int
1115 pci_bus_write_config(struct pci_bus *bus, unsigned int devfn, int pos,
1116     uint32_t val, int size)
1117 {
1118 
1119 	pci_write_config(bus->self->dev.bsddev, pos, val, size);
1120 	return (0);
1121 }
1122 
1123 static inline int
1124 pci_bus_write_config_byte(struct pci_bus *bus, unsigned int devfn, int pos,
1125     uint8_t val)
1126 {
1127 	return (pci_bus_write_config(bus, devfn, pos, val, 1));
1128 }
1129 
1130 static inline int
1131 pci_bus_write_config_word(struct pci_bus *bus, unsigned int devfn, int pos,
1132     uint16_t val)
1133 {
1134 	return (pci_bus_write_config(bus, devfn, pos, val, 2));
1135 }
1136 
1137 struct pci_dev *lkpi_pci_get_class(unsigned int class, struct pci_dev *from);
1138 #define	pci_get_class(class, from)	lkpi_pci_get_class(class, from)
1139 
1140 #endif	/* _LINUX_PCI_H_ */
1141