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