]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/ofed/include/linux/pci.h
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / ofed / 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  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #ifndef _LINUX_PCI_H_
30 #define _LINUX_PCI_H_
31
32 #define CONFIG_PCI_MSI
33
34 #include <linux/types.h>
35
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/pciio.h>
39 #include <sys/rman.h>
40 #include <dev/pci/pcivar.h>
41 #include <dev/pci/pcireg.h>
42 #include <dev/pci/pci_private.h>
43
44 #include <machine/resource.h>
45
46 #include <linux/init.h>
47 #include <linux/list.h>
48 #include <linux/dmapool.h>
49 #include <linux/dma-mapping.h>
50 #include <linux/compiler.h>
51 #include <linux/errno.h>
52 #include <asm/atomic.h>
53 #include <linux/device.h>
54
55 struct pci_device_id {
56         uint32_t        vendor;
57         uint32_t        device;
58         uint32_t        subvendor;
59         uint32_t        subdevice;
60         uint32_t        class_mask;
61         uintptr_t       driver_data;
62 };
63
64 #define MODULE_DEVICE_TABLE(bus, table)
65 #define PCI_ANY_ID              (-1)
66 #define PCI_VENDOR_ID_MELLANOX                  0x15b3
67 #define PCI_VENDOR_ID_TOPSPIN                   0x1867
68 #define PCI_DEVICE_ID_MELLANOX_TAVOR            0x5a44
69 #define PCI_DEVICE_ID_MELLANOX_TAVOR_BRIDGE     0x5a46
70 #define PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT     0x6278
71 #define PCI_DEVICE_ID_MELLANOX_ARBEL            0x6282
72 #define PCI_DEVICE_ID_MELLANOX_SINAI_OLD        0x5e8c
73 #define PCI_DEVICE_ID_MELLANOX_SINAI            0x6274
74
75
76 #define PCI_VDEVICE(vendor, device)                                     \
77             PCI_VENDOR_ID_##vendor, (device), PCI_ANY_ID, PCI_ANY_ID, 0, 0
78 #define PCI_DEVICE(vendor, device)                                      \
79             (vendor), (device), PCI_ANY_ID, PCI_ANY_ID, 0, 0
80
81 #define to_pci_dev(n)   container_of(n, struct pci_dev, dev)
82
83 #define PCI_VENDOR_ID   PCIR_DEVVENDOR
84 #define PCI_COMMAND     PCIR_COMMAND
85 #define PCI_EXP_DEVCTL  PCIR_EXPRESS_DEVICE_CTL
86 #define PCI_EXP_LNKCTL  PCIR_EXPRESS_LINK_CTL
87
88 #define IORESOURCE_MEM  SYS_RES_MEMORY
89 #define IORESOURCE_IO   SYS_RES_IOPORT
90 #define IORESOURCE_IRQ  SYS_RES_IRQ
91
92 struct pci_dev;
93
94 struct pci_driver {
95         struct list_head                links;
96         char                            *name;
97         struct pci_device_id            *id_table;
98         int  (*probe)(struct pci_dev *dev, const struct pci_device_id *id);
99         void (*remove)(struct pci_dev *dev);
100         driver_t                        driver;
101         devclass_t                      bsdclass;
102 };
103
104 extern struct list_head pci_drivers;
105 extern struct list_head pci_devices;
106 extern spinlock_t pci_lock;
107
108 #define __devexit_p(x)  x
109
110 struct pci_dev {
111         struct device           dev;
112         struct list_head        links;
113         struct pci_driver       *pdrv;
114         uint64_t                dma_mask;
115         uint16_t                device;
116         uint16_t                vendor;
117         unsigned int            irq;
118 };
119
120 static inline struct resource_list_entry *
121 _pci_get_rle(struct pci_dev *pdev, int type, int rid)
122 {
123         struct pci_devinfo *dinfo;
124         struct resource_list *rl;
125
126         dinfo = device_get_ivars(pdev->dev.bsddev);
127         rl = &dinfo->resources;
128         return resource_list_find(rl, type, rid);
129 }
130
131 static inline struct resource_list_entry *
132 _pci_get_bar(struct pci_dev *pdev, int bar)
133 {
134         struct resource_list_entry *rle;
135
136         bar = PCIR_BAR(bar);
137         if ((rle = _pci_get_rle(pdev, SYS_RES_MEMORY, bar)) == NULL)
138                 rle = _pci_get_rle(pdev, SYS_RES_IOPORT, bar);
139         return (rle);
140 }
141
142 static inline struct device *
143 _pci_find_irq_dev(unsigned int irq)
144 {
145         struct pci_dev *pdev;
146
147         spin_lock(&pci_lock);
148         list_for_each_entry(pdev, &pci_devices, links) {
149                 if (irq == pdev->dev.irq)
150                         break;
151                 if (irq >= pdev->dev.msix && irq < pdev->dev.msix_max)
152                         break;
153         }
154         spin_unlock(&pci_lock);
155         if (pdev)
156                 return &pdev->dev;
157         return (NULL);
158 }
159
160 static inline unsigned long
161 pci_resource_start(struct pci_dev *pdev, int bar)
162 {
163         struct resource_list_entry *rle;
164
165         if ((rle = _pci_get_bar(pdev, bar)) == NULL)
166                 return (0);
167         return rle->start;
168 }
169
170 static inline unsigned long
171 pci_resource_len(struct pci_dev *pdev, int bar)
172 {
173         struct resource_list_entry *rle;
174
175         if ((rle = _pci_get_bar(pdev, bar)) == NULL)
176                 return (0);
177         return rle->count;
178 }
179
180 /*
181  * All drivers just seem to want to inspect the type not flags.
182  */
183 static inline int
184 pci_resource_flags(struct pci_dev *pdev, int bar)
185 {
186         struct resource_list_entry *rle;
187
188         if ((rle = _pci_get_bar(pdev, bar)) == NULL)
189                 return (0);
190         return rle->type;
191 }
192
193 static inline const char *
194 pci_name(struct pci_dev *d)
195 {
196
197         return device_get_desc(d->dev.bsddev);
198 }
199
200 static inline void *
201 pci_get_drvdata(struct pci_dev *pdev)
202 {
203
204         return dev_get_drvdata(&pdev->dev);
205 }
206
207 static inline void
208 pci_set_drvdata(struct pci_dev *pdev, void *data)
209 {
210
211         dev_set_drvdata(&pdev->dev, data);
212 }
213
214 static inline int
215 pci_enable_device(struct pci_dev *pdev)
216 {
217
218         pci_enable_io(pdev->dev.bsddev, SYS_RES_IOPORT);
219         pci_enable_io(pdev->dev.bsddev, SYS_RES_MEMORY);
220         return (0);
221 }
222
223 static inline void
224 pci_disable_device(struct pci_dev *pdev)
225 {
226 }
227
228 static inline int
229 pci_set_master(struct pci_dev *pdev)
230 {
231
232         pci_enable_busmaster(pdev->dev.bsddev);
233         return (0);
234 }
235
236 static inline int
237 pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
238 {
239         int rid;
240         int type;
241
242         type = pci_resource_flags(pdev, bar);
243         if (type == 0)
244                 return (-ENODEV);
245         rid = PCIR_BAR(bar);
246         if (bus_alloc_resource_any(pdev->dev.bsddev, type, &rid,
247             RF_ACTIVE) == NULL)
248                 return (-EINVAL);
249         return (0);
250 }
251
252 static inline void
253 pci_release_region(struct pci_dev *pdev, int bar)
254 {
255         struct resource_list_entry *rle;
256
257         if ((rle = _pci_get_bar(pdev, bar)) == NULL)
258                 return;
259         bus_release_resource(pdev->dev.bsddev, rle->type, rle->rid, rle->res);
260 }
261
262 static inline void
263 pci_release_regions(struct pci_dev *pdev)
264 {
265         int i;
266
267         for (i = 0; i <= PCIR_MAX_BAR_0; i++)
268                 pci_release_region(pdev, i);
269 }
270
271 static inline int
272 pci_request_regions(struct pci_dev *pdev, const char *res_name)
273 {
274         int error;
275         int i;
276
277         for (i = 0; i <= PCIR_MAX_BAR_0; i++) {
278                 error = pci_request_region(pdev, i, res_name);
279                 if (error && error != -ENODEV) {
280                         pci_release_regions(pdev);
281                         return (error);
282                 }
283         }
284         return (0);
285 }
286
287 static inline void
288 pci_disable_msix(struct pci_dev *pdev)
289 {
290
291         pci_release_msi(pdev->dev.bsddev);
292 }
293
294 #define PCI_CAP_ID_EXP  PCIY_EXPRESS
295 #define PCI_CAP_ID_PCIX PCIY_PCIX
296
297 static inline int
298 pci_find_capability(struct pci_dev *pdev, int capid)
299 {
300         int reg;
301
302         if (pci_find_cap(pdev->dev.bsddev, capid, &reg))
303                 return (0);
304         return (reg);
305 }
306
307 static inline int
308 pci_read_config_byte(struct pci_dev *pdev, int where, u8 *val)
309 {
310
311         *val = (u8)pci_read_config(pdev->dev.bsddev, where, 1);
312         return (0);
313 }
314
315 static inline int
316 pci_read_config_word(struct pci_dev *pdev, int where, u16 *val)
317 {
318
319         *val = (u16)pci_read_config(pdev->dev.bsddev, where, 2);
320         return (0);
321 }
322
323 static inline int
324 pci_read_config_dword(struct pci_dev *pdev, int where, u32 *val)
325 {
326
327         *val = (u32)pci_read_config(pdev->dev.bsddev, where, 4);
328         return (0);
329
330
331 static inline int
332 pci_write_config_byte(struct pci_dev *pdev, int where, u8 val)
333 {
334
335         pci_write_config(pdev->dev.bsddev, where, val, 1);
336         return (0);
337 }
338
339 static inline int
340 pci_write_config_word(struct pci_dev *pdev, int where, u16 val)
341 {
342
343         pci_write_config(pdev->dev.bsddev, where, val, 2);
344         return (0);
345 }
346
347 static inline int
348 pci_write_config_dword(struct pci_dev *pdev, int where, u32 val)
349
350
351         pci_write_config(pdev->dev.bsddev, where, val, 4);
352         return (0);
353 }
354
355 static struct pci_driver *
356 linux_pci_find(device_t dev, struct pci_device_id **idp)
357 {
358         struct pci_device_id *id;
359         struct pci_driver *pdrv;
360         uint16_t vendor;
361         uint16_t device;
362
363         vendor = pci_get_vendor(dev);
364         device = pci_get_device(dev);
365
366         spin_lock(&pci_lock);
367         list_for_each_entry(pdrv, &pci_drivers, links) {
368                 for (id = pdrv->id_table; id->vendor != 0; id++) {
369                         if (vendor == id->vendor && device == id->device) {
370                                 *idp = id;
371                                 spin_unlock(&pci_lock);
372                                 return (pdrv);
373                         }
374                 }
375         }
376         spin_unlock(&pci_lock);
377         return (NULL);
378 }
379
380 static inline int
381 linux_pci_probe(device_t dev)
382 {
383         struct pci_device_id *id;
384         struct pci_driver *pdrv;
385
386         if ((pdrv = linux_pci_find(dev, &id)) == NULL)
387                 return (ENXIO);
388         if (device_get_driver(dev) != &pdrv->driver)
389                 return (ENXIO);
390         device_set_desc(dev, pdrv->name);
391         return (0);
392 }
393
394 static inline int
395 linux_pci_attach(device_t dev)
396 {
397         struct resource_list_entry *rle;
398         struct pci_dev *pdev;
399         struct pci_driver *pdrv;
400         struct pci_device_id *id;
401         int error;
402
403         pdrv = linux_pci_find(dev, &id);
404         pdev = device_get_softc(dev);
405         pdev->dev.parent = &linux_rootdev;
406         pdev->dev.bsddev = dev;
407         INIT_LIST_HEAD(&pdev->dev.irqents);
408         pdev->device = id->device;
409         pdev->vendor = id->vendor;
410         pdev->dev.dma_mask = &pdev->dma_mask;
411         pdev->pdrv = pdrv;
412         kobject_init(&pdev->dev.kobj, &dev_ktype);
413         kobject_set_name(&pdev->dev.kobj, device_get_nameunit(dev));
414         kobject_add(&pdev->dev.kobj, &linux_rootdev.kobj,
415             kobject_name(&pdev->dev.kobj));
416         rle = _pci_get_rle(pdev, SYS_RES_IRQ, 0);
417         if (rle)
418                 pdev->dev.irq = rle->start;
419         else
420                 pdev->dev.irq = 0;
421         pdev->irq = pdev->dev.irq;
422         mtx_unlock(&Giant);
423         spin_lock(&pci_lock);
424         list_add(&pdev->links, &pci_devices);
425         spin_unlock(&pci_lock);
426         error = pdrv->probe(pdev, id);
427         mtx_lock(&Giant);
428         if (error) {
429                 spin_lock(&pci_lock);
430                 list_del(&pdev->links);
431                 spin_unlock(&pci_lock);
432                 put_device(&pdev->dev);
433                 return (-error);
434         }
435         return (0);
436 }
437
438 static inline int
439 linux_pci_detach(device_t dev)
440 {
441         struct pci_dev *pdev;
442
443         pdev = device_get_softc(dev);
444         mtx_unlock(&Giant);
445         pdev->pdrv->remove(pdev);
446         mtx_lock(&Giant);
447         spin_lock(&pci_lock);
448         list_del(&pdev->links);
449         spin_unlock(&pci_lock);
450         put_device(&pdev->dev);
451
452         return (0);
453 }
454
455 static device_method_t pci_methods[] = {
456         DEVMETHOD(device_probe, linux_pci_probe),
457         DEVMETHOD(device_attach, linux_pci_attach),
458         DEVMETHOD(device_detach, linux_pci_detach),
459         {0, 0}
460 };
461
462 static inline int
463 pci_register_driver(struct pci_driver *pdrv)
464 {
465         devclass_t bus;
466         int error;
467
468         spin_lock(&pci_lock);
469         list_add(&pdrv->links, &pci_drivers);
470         spin_unlock(&pci_lock);
471         bus = devclass_find("pci");
472         pdrv->driver.name = pdrv->name;
473         pdrv->driver.methods = pci_methods;
474         pdrv->driver.size = sizeof(struct pci_dev);
475         mtx_lock(&Giant);
476         error = devclass_add_driver(bus, &pdrv->driver, BUS_PASS_DEFAULT,
477             &pdrv->bsdclass);
478         mtx_unlock(&Giant);
479         if (error)
480                 return (-error);
481         return (0);
482 }
483
484 static inline void
485 pci_unregister_driver(struct pci_driver *pdrv)
486 {
487         devclass_t bus;
488
489         list_del(&pdrv->links);
490         bus = devclass_find("pci");
491         mtx_lock(&Giant);
492         devclass_delete_driver(bus, &pdrv->driver);
493         mtx_unlock(&Giant);
494 }
495
496 struct msix_entry {
497         int entry;
498         int vector;
499 };
500
501 /*
502  * Enable msix, positive errors indicate actual number of available
503  * vectors.  Negative errors are failures.
504  */
505 static inline int
506 pci_enable_msix(struct pci_dev *pdev, struct msix_entry *entries, int nreq)
507 {
508         struct resource_list_entry *rle;
509         int error;
510         int avail;
511         int i;
512
513         avail = pci_msix_count(pdev->dev.bsddev);
514         if (avail < nreq) {
515                 if (avail == 0)
516                         return -EINVAL;
517                 return avail;
518         }
519         avail = nreq;
520         if ((error = -pci_alloc_msix(pdev->dev.bsddev, &avail)) != 0)
521                 return error;
522         rle = _pci_get_rle(pdev, SYS_RES_IRQ, 1);
523         pdev->dev.msix = rle->start;
524         pdev->dev.msix_max = rle->start + avail;
525         for (i = 0; i < nreq; i++)
526                 entries[i].vector = pdev->dev.msix + i;
527         return (0);
528 }
529
530 /* XXX This should not be necessary. */
531 #define pcix_set_mmrbc(d, v)    0
532 #define pcix_get_max_mmrbc(d)   0
533 #define pcie_set_readrq(d, v)   0
534
535 #define PCI_DMA_BIDIRECTIONAL   0
536 #define PCI_DMA_TODEVICE        1
537 #define PCI_DMA_FROMDEVICE      2
538 #define PCI_DMA_NONE            3
539
540 #define pci_pool                dma_pool
541 #define pci_pool_destroy        dma_pool_destroy
542 #define pci_pool_alloc          dma_pool_alloc
543 #define pci_pool_free           dma_pool_free
544 #define pci_pool_create(_name, _pdev, _size, _align, _alloc)            \
545             dma_pool_create(_name, &(_pdev)->dev, _size, _align, _alloc)
546 #define pci_free_consistent(_hwdev, _size, _vaddr, _dma_handle)         \
547             dma_free_coherent((_hwdev) == NULL ? NULL : &(_hwdev)->dev, \
548                 _size, _vaddr, _dma_handle)
549 #define pci_map_sg(_hwdev, _sg, _nents, _dir)                           \
550             dma_map_sg((_hwdev) == NULL ? NULL : &(_hwdev->dev),        \
551                 _sg, _nents, (enum dma_data_direction)_dir)
552 #define pci_map_single(_hwdev, _ptr, _size, _dir)                       \
553             dma_map_single((_hwdev) == NULL ? NULL : &(_hwdev->dev),    \
554                 (_ptr), (_size), (enum dma_data_direction)_dir)
555 #define pci_unmap_single(_hwdev, _addr, _size, _dir)                    \
556             dma_unmap_single((_hwdev) == NULL ? NULL : &(_hwdev)->dev,  \
557                 _addr, _size, (enum dma_data_direction)_dir)
558 #define pci_unmap_sg(_hwdev, _sg, _nents, _dir)                         \
559             dma_unmap_sg((_hwdev) == NULL ? NULL : &(_hwdev)->dev,      \
560                 _sg, _nents, (enum dma_data_direction)_dir)
561 #define pci_map_page(_hwdev, _page, _offset, _size, _dir)               \
562             dma_map_page((_hwdev) == NULL ? NULL : &(_hwdev)->dev, _page,\
563                 _offset, _size, (enum dma_data_direction)_dir)
564 #define pci_unmap_page(_hwdev, _dma_address, _size, _dir)               \
565             dma_unmap_page((_hwdev) == NULL ? NULL : &(_hwdev)->dev,    \
566                 _dma_address, _size, (enum dma_data_direction)_dir)
567 #define pci_set_dma_mask(_pdev, mask)   dma_set_mask(&(_pdev)->dev, (mask))
568 #define pci_dma_mapping_error(_pdev, _dma_addr)                         \
569             dma_mapping_error(&(_pdev)->dev, _dma_addr)
570 #define pci_set_consistent_dma_mask(_pdev, _mask)                       \
571             dma_set_coherent_mask(&(_pdev)->dev, (_mask))
572 #define DECLARE_PCI_UNMAP_ADDR(x)       DEFINE_DMA_UNMAP_ADDR(x);
573 #define DECLARE_PCI_UNMAP_LEN(x)        DEFINE_DMA_UNMAP_LEN(x);
574 #define pci_unmap_addr          dma_unmap_addr
575 #define pci_unmap_addr_set      dma_unmap_addr_set
576 #define pci_unmap_len           dma_unmap_len
577 #define pci_unmap_len_set       dma_unmap_len_set
578
579
580 #endif  /* _LINUX_PCI_H_ */