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