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