pci.h revision 331801
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: stable/11/sys/compat/linuxkpi/common/include/linux/pci.h 331801 2018-03-30 18:53:12Z hselasky $
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/pciio.h>
41#include <sys/rman.h>
42#include <dev/pci/pcivar.h>
43#include <dev/pci/pcireg.h>
44#include <dev/pci/pci_private.h>
45
46#include <machine/resource.h>
47
48#include <linux/list.h>
49#include <linux/dmapool.h>
50#include <linux/dma-mapping.h>
51#include <linux/compiler.h>
52#include <linux/errno.h>
53#include <asm/atomic.h>
54#include <linux/device.h>
55
56struct pci_device_id {
57	uint32_t	vendor;
58	uint32_t	device;
59	uint32_t	subvendor;
60	uint32_t	subdevice;
61	uint32_t	class;
62	uint32_t	class_mask;
63	uintptr_t	driver_data;
64};
65
66#define	MODULE_DEVICE_TABLE(bus, table)
67
68#define	PCI_BASE_CLASS_DISPLAY		0x03
69#define	PCI_CLASS_DISPLAY_VGA		0x0300
70#define	PCI_CLASS_DISPLAY_OTHER		0x0380
71#define	PCI_BASE_CLASS_BRIDGE		0x06
72#define	PCI_CLASS_BRIDGE_ISA		0x0601
73
74#define	PCI_ANY_ID		(-1)
75#define	PCI_VENDOR_ID_APPLE		0x106b
76#define	PCI_VENDOR_ID_ASUSTEK		0x1043
77#define	PCI_VENDOR_ID_ATI		0x1002
78#define	PCI_VENDOR_ID_DELL		0x1028
79#define	PCI_VENDOR_ID_HP		0x103c
80#define	PCI_VENDOR_ID_IBM		0x1014
81#define	PCI_VENDOR_ID_INTEL		0x8086
82#define	PCI_VENDOR_ID_MELLANOX			0x15b3
83#define	PCI_VENDOR_ID_REDHAT_QUMRANET	0x1af4
84#define	PCI_VENDOR_ID_SERVERWORKS	0x1166
85#define	PCI_VENDOR_ID_SONY		0x104d
86#define	PCI_VENDOR_ID_TOPSPIN			0x1867
87#define	PCI_VENDOR_ID_VIA		0x1106
88#define	PCI_SUBVENDOR_ID_REDHAT_QUMRANET	0x1af4
89#define	PCI_DEVICE_ID_ATI_RADEON_QY	0x5159
90#define	PCI_DEVICE_ID_MELLANOX_TAVOR		0x5a44
91#define	PCI_DEVICE_ID_MELLANOX_TAVOR_BRIDGE	0x5a46
92#define	PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT	0x6278
93#define	PCI_DEVICE_ID_MELLANOX_ARBEL		0x6282
94#define	PCI_DEVICE_ID_MELLANOX_SINAI_OLD	0x5e8c
95#define	PCI_DEVICE_ID_MELLANOX_SINAI		0x6274
96#define	PCI_SUBDEVICE_ID_QEMU		0x1100
97
98#define PCI_DEVFN(slot, func)   ((((slot) & 0x1f) << 3) | ((func) & 0x07))
99#define PCI_SLOT(devfn)		(((devfn) >> 3) & 0x1f)
100#define PCI_FUNC(devfn)		((devfn) & 0x07)
101
102#define PCI_VDEVICE(_vendor, _device)					\
103	    .vendor = PCI_VENDOR_ID_##_vendor, .device = (_device),	\
104	    .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID
105#define	PCI_DEVICE(_vendor, _device)					\
106	    .vendor = (_vendor), .device = (_device),			\
107	    .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID
108
109#define	to_pci_dev(n)	container_of(n, struct pci_dev, dev)
110
111#define	PCI_VENDOR_ID		PCIR_DEVVENDOR
112#define	PCI_COMMAND		PCIR_COMMAND
113#define	PCI_EXP_DEVCTL		PCIER_DEVICE_CTL		/* Device Control */
114#define	PCI_EXP_LNKCTL		PCIER_LINK_CTL			/* Link Control */
115#define	PCI_EXP_FLAGS_TYPE	PCIEM_FLAGS_TYPE		/* Device/Port type */
116#define	PCI_EXP_DEVCAP		PCIER_DEVICE_CAP		/* Device capabilities */
117#define	PCI_EXP_DEVSTA		PCIER_DEVICE_STA		/* Device Status */
118#define	PCI_EXP_LNKCAP		PCIER_LINK_CAP			/* Link Capabilities */
119#define	PCI_EXP_LNKSTA		PCIER_LINK_STA			/* Link Status */
120#define	PCI_EXP_SLTCAP		PCIER_SLOT_CAP			/* Slot Capabilities */
121#define	PCI_EXP_SLTCTL		PCIER_SLOT_CTL			/* Slot Control */
122#define	PCI_EXP_SLTSTA		PCIER_SLOT_STA			/* Slot Status */
123#define	PCI_EXP_RTCTL		PCIER_ROOT_CTL			/* Root Control */
124#define	PCI_EXP_RTCAP		PCIER_ROOT_CAP			/* Root Capabilities */
125#define	PCI_EXP_RTSTA		PCIER_ROOT_STA			/* Root Status */
126#define	PCI_EXP_DEVCAP2		PCIER_DEVICE_CAP2		/* Device Capabilities 2 */
127#define	PCI_EXP_DEVCTL2		PCIER_DEVICE_CTL2		/* Device Control 2 */
128#define	PCI_EXP_LNKCAP2		PCIER_LINK_CAP2			/* Link Capabilities 2 */
129#define	PCI_EXP_LNKCTL2		PCIER_LINK_CTL2			/* Link Control 2 */
130#define	PCI_EXP_LNKSTA2		PCIER_LINK_STA2			/* Link Status 2 */
131#define	PCI_EXP_FLAGS		PCIER_FLAGS			/* Capabilities register */
132#define	PCI_EXP_FLAGS_VERS	PCIEM_FLAGS_VERSION		/* Capability version */
133#define	PCI_EXP_TYPE_ROOT_PORT	PCIEM_TYPE_ROOT_PORT		/* Root Port */
134#define	PCI_EXP_TYPE_ENDPOINT	PCIEM_TYPE_ENDPOINT		/* Express Endpoint */
135#define	PCI_EXP_TYPE_LEG_END	PCIEM_TYPE_LEGACY_ENDPOINT	/* Legacy Endpoint */
136#define	PCI_EXP_TYPE_DOWNSTREAM PCIEM_TYPE_DOWNSTREAM_PORT	/* Downstream Port */
137#define	PCI_EXP_FLAGS_SLOT	PCIEM_FLAGS_SLOT		/* Slot implemented */
138#define	PCI_EXP_TYPE_RC_EC	PCIEM_TYPE_ROOT_EC		/* Root Complex Event Collector */
139#define	PCI_EXP_LNKCAP_SLS_2_5GB 0x01	/* Supported Link Speed 2.5GT/s */
140#define	PCI_EXP_LNKCAP_SLS_5_0GB 0x02	/* Supported Link Speed 5.0GT/s */
141#define	PCI_EXP_LNKCAP_MLW	0x03f0	/* Maximum Link Width */
142#define	PCI_EXP_LNKCAP2_SLS_2_5GB 0x02	/* Supported Link Speed 2.5GT/s */
143#define	PCI_EXP_LNKCAP2_SLS_5_0GB 0x04	/* Supported Link Speed 5.0GT/s */
144#define	PCI_EXP_LNKCAP2_SLS_8_0GB 0x08	/* Supported Link Speed 8.0GT/s */
145
146#define PCI_EXP_LNKCTL_HAWD	PCIEM_LINK_CTL_HAWD
147#define PCI_EXP_LNKCAP_CLKPM	0x00040000
148#define PCI_EXP_DEVSTA_TRPND	0x0020
149
150#define	IORESOURCE_MEM	(1 << SYS_RES_MEMORY)
151#define	IORESOURCE_IO	(1 << SYS_RES_IOPORT)
152#define	IORESOURCE_IRQ	(1 << SYS_RES_IRQ)
153
154enum pci_bus_speed {
155	PCI_SPEED_UNKNOWN = -1,
156	PCIE_SPEED_2_5GT,
157	PCIE_SPEED_5_0GT,
158	PCIE_SPEED_8_0GT,
159};
160
161enum pcie_link_width {
162	PCIE_LNK_WIDTH_UNKNOWN = 0xFF,
163};
164
165typedef int pci_power_t;
166
167#define PCI_D0	PCI_POWERSTATE_D0
168#define PCI_D1	PCI_POWERSTATE_D1
169#define PCI_D2	PCI_POWERSTATE_D2
170#define PCI_D3hot	PCI_POWERSTATE_D3
171#define PCI_D3cold	4
172
173#define PCI_POWER_ERROR	PCI_POWERSTATE_UNKNOWN
174
175struct pci_dev;
176
177struct pci_driver {
178	struct list_head		links;
179	char				*name;
180	const struct pci_device_id		*id_table;
181	int  (*probe)(struct pci_dev *dev, const struct pci_device_id *id);
182	void (*remove)(struct pci_dev *dev);
183	int  (*suspend) (struct pci_dev *dev, pm_message_t state);	/* Device suspended */
184	int  (*resume) (struct pci_dev *dev);		/* Device woken up */
185	void (*shutdown) (struct pci_dev *dev);		/* Device shutdown */
186	driver_t			bsddriver;
187	devclass_t			bsdclass;
188	struct device_driver		driver;
189	const struct pci_error_handlers       *err_handler;
190	bool				isdrm;
191};
192
193struct pci_bus {
194	struct pci_dev	*self;
195	int		number;
196};
197
198extern struct list_head pci_drivers;
199extern struct list_head pci_devices;
200extern spinlock_t pci_lock;
201
202#define	__devexit_p(x)	x
203
204struct pci_dev {
205	struct device		dev;
206	struct list_head	links;
207	struct pci_driver	*pdrv;
208	struct pci_bus		*bus;
209	uint64_t		dma_mask;
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};
219
220static inline struct resource_list_entry *
221linux_pci_get_rle(struct pci_dev *pdev, int type, int rid)
222{
223	struct pci_devinfo *dinfo;
224	struct resource_list *rl;
225
226	dinfo = device_get_ivars(pdev->dev.bsddev);
227	rl = &dinfo->resources;
228	return resource_list_find(rl, type, rid);
229}
230
231static inline struct resource_list_entry *
232linux_pci_get_bar(struct pci_dev *pdev, int bar)
233{
234	struct resource_list_entry *rle;
235
236	bar = PCIR_BAR(bar);
237	if ((rle = linux_pci_get_rle(pdev, SYS_RES_MEMORY, bar)) == NULL)
238		rle = linux_pci_get_rle(pdev, SYS_RES_IOPORT, bar);
239	return (rle);
240}
241
242static inline struct device *
243linux_pci_find_irq_dev(unsigned int irq)
244{
245	struct pci_dev *pdev;
246	struct device *found;
247
248	found = NULL;
249	spin_lock(&pci_lock);
250	list_for_each_entry(pdev, &pci_devices, links) {
251		if (irq == pdev->dev.irq ||
252		    (irq >= pdev->dev.msix && irq < pdev->dev.msix_max)) {
253			found = &pdev->dev;
254			break;
255		}
256	}
257	spin_unlock(&pci_lock);
258	return (found);
259}
260
261static inline unsigned long
262pci_resource_start(struct pci_dev *pdev, int bar)
263{
264	struct resource_list_entry *rle;
265
266	if ((rle = linux_pci_get_bar(pdev, bar)) == NULL)
267		return (0);
268	return rle->start;
269}
270
271static inline unsigned long
272pci_resource_len(struct pci_dev *pdev, int bar)
273{
274	struct resource_list_entry *rle;
275
276	if ((rle = linux_pci_get_bar(pdev, bar)) == NULL)
277		return (0);
278	return rle->count;
279}
280
281static inline int
282pci_resource_type(struct pci_dev *pdev, int bar)
283{
284	struct pci_map *pm;
285
286	pm = pci_find_bar(pdev->dev.bsddev, PCIR_BAR(bar));
287	if (!pm)
288		return (-1);
289
290	if (PCI_BAR_IO(pm->pm_value))
291		return (SYS_RES_IOPORT);
292	else
293		return (SYS_RES_MEMORY);
294}
295
296/*
297 * All drivers just seem to want to inspect the type not flags.
298 */
299static inline int
300pci_resource_flags(struct pci_dev *pdev, int bar)
301{
302	int type;
303
304	type = pci_resource_type(pdev, bar);
305	if (type < 0)
306		return (0);
307	return (1 << type);
308}
309
310static inline const char *
311pci_name(struct pci_dev *d)
312{
313
314	return device_get_desc(d->dev.bsddev);
315}
316
317static inline void *
318pci_get_drvdata(struct pci_dev *pdev)
319{
320
321	return dev_get_drvdata(&pdev->dev);
322}
323
324static inline void
325pci_set_drvdata(struct pci_dev *pdev, void *data)
326{
327
328	dev_set_drvdata(&pdev->dev, data);
329}
330
331static inline int
332pci_enable_device(struct pci_dev *pdev)
333{
334
335	pci_enable_io(pdev->dev.bsddev, SYS_RES_IOPORT);
336	pci_enable_io(pdev->dev.bsddev, SYS_RES_MEMORY);
337	return (0);
338}
339
340static inline void
341pci_disable_device(struct pci_dev *pdev)
342{
343
344	pci_disable_io(pdev->dev.bsddev, SYS_RES_IOPORT);
345	pci_disable_io(pdev->dev.bsddev, SYS_RES_MEMORY);
346}
347
348static inline int
349pci_set_master(struct pci_dev *pdev)
350{
351
352	pci_enable_busmaster(pdev->dev.bsddev);
353	return (0);
354}
355
356static inline int
357pci_set_power_state(struct pci_dev *pdev, int state)
358{
359
360	pci_set_powerstate(pdev->dev.bsddev, state);
361	return (0);
362}
363
364static inline int
365pci_clear_master(struct pci_dev *pdev)
366{
367
368	pci_disable_busmaster(pdev->dev.bsddev);
369	return (0);
370}
371
372static inline int
373pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
374{
375	int rid;
376	int type;
377
378	type = pci_resource_type(pdev, bar);
379	if (type < 0)
380		return (-ENODEV);
381	rid = PCIR_BAR(bar);
382	if (bus_alloc_resource_any(pdev->dev.bsddev, type, &rid,
383	    RF_ACTIVE) == NULL)
384		return (-EINVAL);
385	return (0);
386}
387
388static inline void
389pci_release_region(struct pci_dev *pdev, int bar)
390{
391	struct resource_list_entry *rle;
392
393	if ((rle = linux_pci_get_bar(pdev, bar)) == NULL)
394		return;
395	bus_release_resource(pdev->dev.bsddev, rle->type, rle->rid, rle->res);
396}
397
398static inline void
399pci_release_regions(struct pci_dev *pdev)
400{
401	int i;
402
403	for (i = 0; i <= PCIR_MAX_BAR_0; i++)
404		pci_release_region(pdev, i);
405}
406
407static inline int
408pci_request_regions(struct pci_dev *pdev, const char *res_name)
409{
410	int error;
411	int i;
412
413	for (i = 0; i <= PCIR_MAX_BAR_0; i++) {
414		error = pci_request_region(pdev, i, res_name);
415		if (error && error != -ENODEV) {
416			pci_release_regions(pdev);
417			return (error);
418		}
419	}
420	return (0);
421}
422
423static inline void
424pci_disable_msix(struct pci_dev *pdev)
425{
426
427	pci_release_msi(pdev->dev.bsddev);
428
429	/*
430	 * The MSIX IRQ numbers associated with this PCI device are no
431	 * longer valid and might be re-assigned. Make sure
432	 * linux_pci_find_irq_dev() does no longer see them by
433	 * resetting their references to zero:
434	 */
435	pdev->dev.msix = 0;
436	pdev->dev.msix_max = 0;
437}
438
439static inline bus_addr_t
440pci_bus_address(struct pci_dev *pdev, int bar)
441{
442
443	return (pci_resource_start(pdev, bar));
444}
445
446#define	PCI_CAP_ID_EXP	PCIY_EXPRESS
447#define	PCI_CAP_ID_PCIX	PCIY_PCIX
448#define PCI_CAP_ID_AGP  PCIY_AGP
449#define PCI_CAP_ID_PM   PCIY_PMG
450
451#define PCI_EXP_DEVCTL		PCIER_DEVICE_CTL
452#define PCI_EXP_DEVCTL_PAYLOAD	PCIEM_CTL_MAX_PAYLOAD
453#define PCI_EXP_DEVCTL_READRQ	PCIEM_CTL_MAX_READ_REQUEST
454#define PCI_EXP_LNKCTL		PCIER_LINK_CTL
455#define PCI_EXP_LNKSTA		PCIER_LINK_STA
456
457static inline int
458pci_find_capability(struct pci_dev *pdev, int capid)
459{
460	int reg;
461
462	if (pci_find_cap(pdev->dev.bsddev, capid, &reg))
463		return (0);
464	return (reg);
465}
466
467static inline int pci_pcie_cap(struct pci_dev *dev)
468{
469	return pci_find_capability(dev, PCI_CAP_ID_EXP);
470}
471
472
473static inline int
474pci_read_config_byte(struct pci_dev *pdev, int where, u8 *val)
475{
476
477	*val = (u8)pci_read_config(pdev->dev.bsddev, where, 1);
478	return (0);
479}
480
481static inline int
482pci_read_config_word(struct pci_dev *pdev, int where, u16 *val)
483{
484
485	*val = (u16)pci_read_config(pdev->dev.bsddev, where, 2);
486	return (0);
487}
488
489static inline int
490pci_read_config_dword(struct pci_dev *pdev, int where, u32 *val)
491{
492
493	*val = (u32)pci_read_config(pdev->dev.bsddev, where, 4);
494	return (0);
495}
496
497static inline int
498pci_write_config_byte(struct pci_dev *pdev, int where, u8 val)
499{
500
501	pci_write_config(pdev->dev.bsddev, where, val, 1);
502	return (0);
503}
504
505static inline int
506pci_write_config_word(struct pci_dev *pdev, int where, u16 val)
507{
508
509	pci_write_config(pdev->dev.bsddev, where, val, 2);
510	return (0);
511}
512
513static inline int
514pci_write_config_dword(struct pci_dev *pdev, int where, u32 val)
515{
516
517	pci_write_config(pdev->dev.bsddev, where, val, 4);
518	return (0);
519}
520
521int	linux_pci_register_driver(struct pci_driver *pdrv);
522int	linux_pci_register_drm_driver(struct pci_driver *pdrv);
523void	linux_pci_unregister_driver(struct pci_driver *pdrv);
524
525#define	pci_register_driver(pdrv)	linux_pci_register_driver(pdrv)
526#define	pci_unregister_driver(pdrv)	linux_pci_unregister_driver(pdrv)
527
528struct msix_entry {
529	int entry;
530	int vector;
531};
532
533/*
534 * Enable msix, positive errors indicate actual number of available
535 * vectors.  Negative errors are failures.
536 *
537 * NB: define added to prevent this definition of pci_enable_msix from
538 * clashing with the native FreeBSD version.
539 */
540#define	pci_enable_msix		linux_pci_enable_msix
541static inline int
542pci_enable_msix(struct pci_dev *pdev, struct msix_entry *entries, int nreq)
543{
544	struct resource_list_entry *rle;
545	int error;
546	int avail;
547	int i;
548
549	avail = pci_msix_count(pdev->dev.bsddev);
550	if (avail < nreq) {
551		if (avail == 0)
552			return -EINVAL;
553		return avail;
554	}
555	avail = nreq;
556	if ((error = -pci_alloc_msix(pdev->dev.bsddev, &avail)) != 0)
557		return error;
558	/*
559	 * Handle case where "pci_alloc_msix()" may allocate less
560	 * interrupts than available and return with no error:
561	 */
562	if (avail < nreq) {
563		pci_release_msi(pdev->dev.bsddev);
564		return avail;
565	}
566	rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 1);
567	pdev->dev.msix = rle->start;
568	pdev->dev.msix_max = rle->start + avail;
569	for (i = 0; i < nreq; i++)
570		entries[i].vector = pdev->dev.msix + i;
571	return (0);
572}
573
574#define	pci_enable_msix_range	linux_pci_enable_msix_range
575static inline int
576pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries,
577    int minvec, int maxvec)
578{
579	int nvec = maxvec;
580	int rc;
581
582	if (maxvec < minvec)
583		return (-ERANGE);
584
585	do {
586		rc = pci_enable_msix(dev, entries, nvec);
587		if (rc < 0) {
588			return (rc);
589		} else if (rc > 0) {
590			if (rc < minvec)
591				return (-ENOSPC);
592			nvec = rc;
593		}
594	} while (rc);
595	return (nvec);
596}
597
598static inline int pci_channel_offline(struct pci_dev *pdev)
599{
600	return false;
601}
602
603static inline int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
604{
605	return -ENODEV;
606}
607static inline void pci_disable_sriov(struct pci_dev *dev)
608{
609}
610
611#define DEFINE_PCI_DEVICE_TABLE(_table) \
612	const struct pci_device_id _table[] __devinitdata
613
614
615/* XXX This should not be necessary. */
616#define	pcix_set_mmrbc(d, v)	0
617#define	pcix_get_max_mmrbc(d)	0
618#define	pcie_set_readrq(d, v)	0
619
620#define	PCI_DMA_BIDIRECTIONAL	0
621#define	PCI_DMA_TODEVICE	1
622#define	PCI_DMA_FROMDEVICE	2
623#define	PCI_DMA_NONE		3
624
625#define	pci_pool		dma_pool
626#define	pci_pool_destroy(...)	dma_pool_destroy(__VA_ARGS__)
627#define	pci_pool_alloc(...)	dma_pool_alloc(__VA_ARGS__)
628#define	pci_pool_free(...)	dma_pool_free(__VA_ARGS__)
629#define	pci_pool_create(_name, _pdev, _size, _align, _alloc)		\
630	    dma_pool_create(_name, &(_pdev)->dev, _size, _align, _alloc)
631#define	pci_free_consistent(_hwdev, _size, _vaddr, _dma_handle)		\
632	    dma_free_coherent((_hwdev) == NULL ? NULL : &(_hwdev)->dev,	\
633		_size, _vaddr, _dma_handle)
634#define	pci_map_sg(_hwdev, _sg, _nents, _dir)				\
635	    dma_map_sg((_hwdev) == NULL ? NULL : &(_hwdev->dev),	\
636		_sg, _nents, (enum dma_data_direction)_dir)
637#define	pci_map_single(_hwdev, _ptr, _size, _dir)			\
638	    dma_map_single((_hwdev) == NULL ? NULL : &(_hwdev->dev),	\
639		(_ptr), (_size), (enum dma_data_direction)_dir)
640#define	pci_unmap_single(_hwdev, _addr, _size, _dir)			\
641	    dma_unmap_single((_hwdev) == NULL ? NULL : &(_hwdev)->dev,	\
642		_addr, _size, (enum dma_data_direction)_dir)
643#define	pci_unmap_sg(_hwdev, _sg, _nents, _dir)				\
644	    dma_unmap_sg((_hwdev) == NULL ? NULL : &(_hwdev)->dev,	\
645		_sg, _nents, (enum dma_data_direction)_dir)
646#define	pci_map_page(_hwdev, _page, _offset, _size, _dir)		\
647	    dma_map_page((_hwdev) == NULL ? NULL : &(_hwdev)->dev, _page,\
648		_offset, _size, (enum dma_data_direction)_dir)
649#define	pci_unmap_page(_hwdev, _dma_address, _size, _dir)		\
650	    dma_unmap_page((_hwdev) == NULL ? NULL : &(_hwdev)->dev,	\
651		_dma_address, _size, (enum dma_data_direction)_dir)
652#define	pci_set_dma_mask(_pdev, mask)	dma_set_mask(&(_pdev)->dev, (mask))
653#define	pci_dma_mapping_error(_pdev, _dma_addr)				\
654	    dma_mapping_error(&(_pdev)->dev, _dma_addr)
655#define	pci_set_consistent_dma_mask(_pdev, _mask)			\
656	    dma_set_coherent_mask(&(_pdev)->dev, (_mask))
657#define	DECLARE_PCI_UNMAP_ADDR(x)	DEFINE_DMA_UNMAP_ADDR(x);
658#define	DECLARE_PCI_UNMAP_LEN(x)	DEFINE_DMA_UNMAP_LEN(x);
659#define	pci_unmap_addr		dma_unmap_addr
660#define	pci_unmap_addr_set	dma_unmap_addr_set
661#define	pci_unmap_len		dma_unmap_len
662#define	pci_unmap_len_set	dma_unmap_len_set
663
664typedef unsigned int __bitwise pci_channel_state_t;
665typedef unsigned int __bitwise pci_ers_result_t;
666
667enum pci_channel_state {
668	pci_channel_io_normal = 1,
669	pci_channel_io_frozen = 2,
670	pci_channel_io_perm_failure = 3,
671};
672
673enum pci_ers_result {
674	PCI_ERS_RESULT_NONE = 1,
675	PCI_ERS_RESULT_CAN_RECOVER = 2,
676	PCI_ERS_RESULT_NEED_RESET = 3,
677	PCI_ERS_RESULT_DISCONNECT = 4,
678	PCI_ERS_RESULT_RECOVERED = 5,
679};
680
681
682/* PCI bus error event callbacks */
683struct pci_error_handlers {
684	pci_ers_result_t (*error_detected)(struct pci_dev *dev,
685	    enum pci_channel_state error);
686	pci_ers_result_t (*mmio_enabled)(struct pci_dev *dev);
687	pci_ers_result_t (*link_reset)(struct pci_dev *dev);
688	pci_ers_result_t (*slot_reset)(struct pci_dev *dev);
689	void (*resume)(struct pci_dev *dev);
690};
691
692/* FreeBSD does not support SRIOV - yet */
693static inline struct pci_dev *pci_physfn(struct pci_dev *dev)
694{
695	return dev;
696}
697
698static inline bool pci_is_pcie(struct pci_dev *dev)
699{
700	return !!pci_pcie_cap(dev);
701}
702
703static inline u16 pcie_flags_reg(struct pci_dev *dev)
704{
705	int pos;
706	u16 reg16;
707
708	pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
709	if (!pos)
710		return 0;
711
712	pci_read_config_word(dev, pos + PCI_EXP_FLAGS, &reg16);
713
714	return reg16;
715}
716
717
718static inline int pci_pcie_type(struct pci_dev *dev)
719{
720	return (pcie_flags_reg(dev) & PCI_EXP_FLAGS_TYPE) >> 4;
721}
722
723static inline int pcie_cap_version(struct pci_dev *dev)
724{
725	return pcie_flags_reg(dev) & PCI_EXP_FLAGS_VERS;
726}
727
728static inline bool pcie_cap_has_lnkctl(struct pci_dev *dev)
729{
730	int type = pci_pcie_type(dev);
731
732	return pcie_cap_version(dev) > 1 ||
733	       type == PCI_EXP_TYPE_ROOT_PORT ||
734	       type == PCI_EXP_TYPE_ENDPOINT ||
735	       type == PCI_EXP_TYPE_LEG_END;
736}
737
738static inline bool pcie_cap_has_devctl(const struct pci_dev *dev)
739{
740		return true;
741}
742
743static inline bool pcie_cap_has_sltctl(struct pci_dev *dev)
744{
745	int type = pci_pcie_type(dev);
746
747	return pcie_cap_version(dev) > 1 || type == PCI_EXP_TYPE_ROOT_PORT ||
748	    (type == PCI_EXP_TYPE_DOWNSTREAM &&
749	    pcie_flags_reg(dev) & PCI_EXP_FLAGS_SLOT);
750}
751
752static inline bool pcie_cap_has_rtctl(struct pci_dev *dev)
753{
754	int type = pci_pcie_type(dev);
755
756	return pcie_cap_version(dev) > 1 || type == PCI_EXP_TYPE_ROOT_PORT ||
757	    type == PCI_EXP_TYPE_RC_EC;
758}
759
760static bool pcie_capability_reg_implemented(struct pci_dev *dev, int pos)
761{
762	if (!pci_is_pcie(dev))
763		return false;
764
765	switch (pos) {
766	case PCI_EXP_FLAGS_TYPE:
767		return true;
768	case PCI_EXP_DEVCAP:
769	case PCI_EXP_DEVCTL:
770	case PCI_EXP_DEVSTA:
771		return pcie_cap_has_devctl(dev);
772	case PCI_EXP_LNKCAP:
773	case PCI_EXP_LNKCTL:
774	case PCI_EXP_LNKSTA:
775		return pcie_cap_has_lnkctl(dev);
776	case PCI_EXP_SLTCAP:
777	case PCI_EXP_SLTCTL:
778	case PCI_EXP_SLTSTA:
779		return pcie_cap_has_sltctl(dev);
780	case PCI_EXP_RTCTL:
781	case PCI_EXP_RTCAP:
782	case PCI_EXP_RTSTA:
783		return pcie_cap_has_rtctl(dev);
784	case PCI_EXP_DEVCAP2:
785	case PCI_EXP_DEVCTL2:
786	case PCI_EXP_LNKCAP2:
787	case PCI_EXP_LNKCTL2:
788	case PCI_EXP_LNKSTA2:
789		return pcie_cap_version(dev) > 1;
790	default:
791		return false;
792	}
793}
794
795static inline int
796pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *dst)
797{
798	if (pos & 3)
799		return -EINVAL;
800
801	if (!pcie_capability_reg_implemented(dev, pos))
802		return -EINVAL;
803
804	return pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, dst);
805}
806
807static inline int
808pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *dst)
809{
810	if (pos & 3)
811		return -EINVAL;
812
813	if (!pcie_capability_reg_implemented(dev, pos))
814		return -EINVAL;
815
816	return pci_read_config_word(dev, pci_pcie_cap(dev) + pos, dst);
817}
818
819static inline int
820pcie_capability_write_word(struct pci_dev *dev, int pos, u16 val)
821{
822	if (pos & 1)
823		return -EINVAL;
824
825	if (!pcie_capability_reg_implemented(dev, pos))
826		return 0;
827
828	return pci_write_config_word(dev, pci_pcie_cap(dev) + pos, val);
829}
830
831static inline int pcie_get_minimum_link(struct pci_dev *dev,
832    enum pci_bus_speed *speed, enum pcie_link_width *width)
833{
834	*speed = PCI_SPEED_UNKNOWN;
835	*width = PCIE_LNK_WIDTH_UNKNOWN;
836	return (0);
837}
838
839static inline int
840pci_num_vf(struct pci_dev *dev)
841{
842	return (0);
843}
844
845#endif	/* _LINUX_PCI_H_ */
846