]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/ofed/include/linux/pci.h
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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             .vendor = PCI_VENDOR_ID_##_vendor, .device = (_device),     \
78             .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID
79 #define PCI_DEVICE(_vendor, _device)                                    \
80             .vendor = (_vendor), .device = (_device),                   \
81             .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID
82
83 #define to_pci_dev(n)   container_of(n, struct pci_dev, dev)
84
85 #define PCI_VENDOR_ID   PCIR_DEVVENDOR
86 #define PCI_COMMAND     PCIR_COMMAND
87 #define PCI_EXP_DEVCTL  PCIER_DEVICE_CTL
88 #define PCI_EXP_LNKCTL  PCIER_LINK_CTL
89
90 #define IORESOURCE_MEM  SYS_RES_MEMORY
91 #define IORESOURCE_IO   SYS_RES_IOPORT
92 #define IORESOURCE_IRQ  SYS_RES_IRQ
93
94 struct pci_dev;
95
96 struct pci_driver {
97         struct list_head                links;
98         char                            *name;
99         struct pci_device_id            *id_table;
100         int  (*probe)(struct pci_dev *dev, const struct pci_device_id *id);
101         void (*remove)(struct pci_dev *dev);
102         driver_t                        driver;
103         devclass_t                      bsdclass;
104 };
105
106 extern struct list_head pci_drivers;
107 extern struct list_head pci_devices;
108 extern spinlock_t pci_lock;
109
110 #define __devexit_p(x)  x
111
112 struct pci_dev {
113         struct device           dev;
114         struct list_head        links;
115         struct pci_driver       *pdrv;
116         uint64_t                dma_mask;
117         uint16_t                device;
118         uint16_t                vendor;
119         unsigned int            irq;
120 };
121
122 static inline struct resource_list_entry *
123 _pci_get_rle(struct pci_dev *pdev, int type, int rid)
124 {
125         struct pci_devinfo *dinfo;
126         struct resource_list *rl;
127
128         dinfo = device_get_ivars(pdev->dev.bsddev);
129         rl = &dinfo->resources;
130         return resource_list_find(rl, type, rid);
131 }
132
133 static inline struct resource_list_entry *
134 _pci_get_bar(struct pci_dev *pdev, int bar)
135 {
136         struct resource_list_entry *rle;
137
138         bar = PCIR_BAR(bar);
139         if ((rle = _pci_get_rle(pdev, SYS_RES_MEMORY, bar)) == NULL)
140                 rle = _pci_get_rle(pdev, SYS_RES_IOPORT, bar);
141         return (rle);
142 }
143
144 static inline struct device *
145 _pci_find_irq_dev(unsigned int irq)
146 {
147         struct pci_dev *pdev;
148
149         spin_lock(&pci_lock);
150         list_for_each_entry(pdev, &pci_devices, links) {
151                 if (irq == pdev->dev.irq)
152                         break;
153                 if (irq >= pdev->dev.msix && irq < pdev->dev.msix_max)
154                         break;
155         }
156         spin_unlock(&pci_lock);
157         if (pdev)
158                 return &pdev->dev;
159         return (NULL);
160 }
161
162 static inline unsigned long
163 pci_resource_start(struct pci_dev *pdev, int bar)
164 {
165         struct resource_list_entry *rle;
166
167         if ((rle = _pci_get_bar(pdev, bar)) == NULL)
168                 return (0);
169         return rle->start;
170 }
171
172 static inline unsigned long
173 pci_resource_len(struct pci_dev *pdev, int bar)
174 {
175         struct resource_list_entry *rle;
176
177         if ((rle = _pci_get_bar(pdev, bar)) == NULL)
178                 return (0);
179         return rle->count;
180 }
181
182 /*
183  * All drivers just seem to want to inspect the type not flags.
184  */
185 static inline int
186 pci_resource_flags(struct pci_dev *pdev, int bar)
187 {
188         struct resource_list_entry *rle;
189
190         if ((rle = _pci_get_bar(pdev, bar)) == NULL)
191                 return (0);
192         return rle->type;
193 }
194
195 static inline const char *
196 pci_name(struct pci_dev *d)
197 {
198
199         return device_get_desc(d->dev.bsddev);
200 }
201
202 static inline void *
203 pci_get_drvdata(struct pci_dev *pdev)
204 {
205
206         return dev_get_drvdata(&pdev->dev);
207 }
208
209 static inline void
210 pci_set_drvdata(struct pci_dev *pdev, void *data)
211 {
212
213         dev_set_drvdata(&pdev->dev, data);
214 }
215
216 static inline int
217 pci_enable_device(struct pci_dev *pdev)
218 {
219
220         pci_enable_io(pdev->dev.bsddev, SYS_RES_IOPORT);
221         pci_enable_io(pdev->dev.bsddev, SYS_RES_MEMORY);
222         return (0);
223 }
224
225 static inline void
226 pci_disable_device(struct pci_dev *pdev)
227 {
228 }
229
230 static inline int
231 pci_set_master(struct pci_dev *pdev)
232 {
233
234         pci_enable_busmaster(pdev->dev.bsddev);
235         return (0);
236 }
237
238 static inline int
239 pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
240 {
241         int rid;
242         int type;
243
244         type = pci_resource_flags(pdev, bar);
245         if (type == 0)
246                 return (-ENODEV);
247         rid = PCIR_BAR(bar);
248         if (bus_alloc_resource_any(pdev->dev.bsddev, type, &rid,
249             RF_ACTIVE) == NULL)
250                 return (-EINVAL);
251         return (0);
252 }
253
254 static inline void
255 pci_release_region(struct pci_dev *pdev, int bar)
256 {
257         struct resource_list_entry *rle;
258
259         if ((rle = _pci_get_bar(pdev, bar)) == NULL)
260                 return;
261         bus_release_resource(pdev->dev.bsddev, rle->type, rle->rid, rle->res);
262 }
263
264 static inline void
265 pci_release_regions(struct pci_dev *pdev)
266 {
267         int i;
268
269         for (i = 0; i <= PCIR_MAX_BAR_0; i++)
270                 pci_release_region(pdev, i);
271 }
272
273 static inline int
274 pci_request_regions(struct pci_dev *pdev, const char *res_name)
275 {
276         int error;
277         int i;
278
279         for (i = 0; i <= PCIR_MAX_BAR_0; i++) {
280                 error = pci_request_region(pdev, i, res_name);
281                 if (error && error != -ENODEV) {
282                         pci_release_regions(pdev);
283                         return (error);
284                 }
285         }
286         return (0);
287 }
288
289 static inline void
290 pci_disable_msix(struct pci_dev *pdev)
291 {
292
293         pci_release_msi(pdev->dev.bsddev);
294 }
295
296 #define PCI_CAP_ID_EXP  PCIY_EXPRESS
297 #define PCI_CAP_ID_PCIX PCIY_PCIX
298
299 static inline int
300 pci_find_capability(struct pci_dev *pdev, int capid)
301 {
302         int reg;
303
304         if (pci_find_cap(pdev->dev.bsddev, capid, &reg))
305                 return (0);
306         return (reg);
307 }
308
309 static inline int
310 pci_read_config_byte(struct pci_dev *pdev, int where, u8 *val)
311 {
312
313         *val = (u8)pci_read_config(pdev->dev.bsddev, where, 1);
314         return (0);
315 }
316
317 static inline int
318 pci_read_config_word(struct pci_dev *pdev, int where, u16 *val)
319 {
320
321         *val = (u16)pci_read_config(pdev->dev.bsddev, where, 2);
322         return (0);
323 }
324
325 static inline int
326 pci_read_config_dword(struct pci_dev *pdev, int where, u32 *val)
327 {
328
329         *val = (u32)pci_read_config(pdev->dev.bsddev, where, 4);
330         return (0);
331
332
333 static inline int
334 pci_write_config_byte(struct pci_dev *pdev, int where, u8 val)
335 {
336
337         pci_write_config(pdev->dev.bsddev, where, val, 1);
338         return (0);
339 }
340
341 static inline int
342 pci_write_config_word(struct pci_dev *pdev, int where, u16 val)
343 {
344
345         pci_write_config(pdev->dev.bsddev, where, val, 2);
346         return (0);
347 }
348
349 static inline int
350 pci_write_config_dword(struct pci_dev *pdev, int where, u32 val)
351
352
353         pci_write_config(pdev->dev.bsddev, where, val, 4);
354         return (0);
355 }
356
357 static struct pci_driver *
358 linux_pci_find(device_t dev, struct pci_device_id **idp)
359 {
360         struct pci_device_id *id;
361         struct pci_driver *pdrv;
362         uint16_t vendor;
363         uint16_t device;
364
365         vendor = pci_get_vendor(dev);
366         device = pci_get_device(dev);
367
368         spin_lock(&pci_lock);
369         list_for_each_entry(pdrv, &pci_drivers, links) {
370                 for (id = pdrv->id_table; id->vendor != 0; id++) {
371                         if (vendor == id->vendor && device == id->device) {
372                                 *idp = id;
373                                 spin_unlock(&pci_lock);
374                                 return (pdrv);
375                         }
376                 }
377         }
378         spin_unlock(&pci_lock);
379         return (NULL);
380 }
381
382 static inline int
383 linux_pci_probe(device_t dev)
384 {
385         struct pci_device_id *id;
386         struct pci_driver *pdrv;
387
388         if ((pdrv = linux_pci_find(dev, &id)) == NULL)
389                 return (ENXIO);
390         if (device_get_driver(dev) != &pdrv->driver)
391                 return (ENXIO);
392         device_set_desc(dev, pdrv->name);
393         return (0);
394 }
395
396 static inline int
397 linux_pci_attach(device_t dev)
398 {
399         struct resource_list_entry *rle;
400         struct pci_dev *pdev;
401         struct pci_driver *pdrv;
402         struct pci_device_id *id;
403         int error;
404
405         pdrv = linux_pci_find(dev, &id);
406         pdev = device_get_softc(dev);
407         pdev->dev.parent = &linux_rootdev;
408         pdev->dev.bsddev = dev;
409         INIT_LIST_HEAD(&pdev->dev.irqents);
410         pdev->device = id->device;
411         pdev->vendor = id->vendor;
412         pdev->dev.dma_mask = &pdev->dma_mask;
413         pdev->pdrv = pdrv;
414         kobject_init(&pdev->dev.kobj, &dev_ktype);
415         kobject_set_name(&pdev->dev.kobj, device_get_nameunit(dev));
416         kobject_add(&pdev->dev.kobj, &linux_rootdev.kobj,
417             kobject_name(&pdev->dev.kobj));
418         rle = _pci_get_rle(pdev, SYS_RES_IRQ, 0);
419         if (rle)
420                 pdev->dev.irq = rle->start;
421         else
422                 pdev->dev.irq = 0;
423         pdev->irq = pdev->dev.irq;
424         mtx_unlock(&Giant);
425         spin_lock(&pci_lock);
426         list_add(&pdev->links, &pci_devices);
427         spin_unlock(&pci_lock);
428         error = pdrv->probe(pdev, id);
429         mtx_lock(&Giant);
430         if (error) {
431                 spin_lock(&pci_lock);
432                 list_del(&pdev->links);
433                 spin_unlock(&pci_lock);
434                 put_device(&pdev->dev);
435                 return (-error);
436         }
437         return (0);
438 }
439
440 static inline int
441 linux_pci_detach(device_t dev)
442 {
443         struct pci_dev *pdev;
444
445         pdev = device_get_softc(dev);
446         mtx_unlock(&Giant);
447         pdev->pdrv->remove(pdev);
448         mtx_lock(&Giant);
449         spin_lock(&pci_lock);
450         list_del(&pdev->links);
451         spin_unlock(&pci_lock);
452         put_device(&pdev->dev);
453
454         return (0);
455 }
456
457 static device_method_t pci_methods[] = {
458         DEVMETHOD(device_probe, linux_pci_probe),
459         DEVMETHOD(device_attach, linux_pci_attach),
460         DEVMETHOD(device_detach, linux_pci_detach),
461         {0, 0}
462 };
463
464 static inline int
465 pci_register_driver(struct pci_driver *pdrv)
466 {
467         devclass_t bus;
468         int error;
469
470         spin_lock(&pci_lock);
471         list_add(&pdrv->links, &pci_drivers);
472         spin_unlock(&pci_lock);
473         bus = devclass_find("pci");
474         pdrv->driver.name = pdrv->name;
475         pdrv->driver.methods = pci_methods;
476         pdrv->driver.size = sizeof(struct pci_dev);
477         mtx_lock(&Giant);
478         error = devclass_add_driver(bus, &pdrv->driver, BUS_PASS_DEFAULT,
479             &pdrv->bsdclass);
480         mtx_unlock(&Giant);
481         if (error)
482                 return (-error);
483         return (0);
484 }
485
486 static inline void
487 pci_unregister_driver(struct pci_driver *pdrv)
488 {
489         devclass_t bus;
490
491         list_del(&pdrv->links);
492         bus = devclass_find("pci");
493         mtx_lock(&Giant);
494         devclass_delete_driver(bus, &pdrv->driver);
495         mtx_unlock(&Giant);
496 }
497
498 struct msix_entry {
499         int entry;
500         int vector;
501 };
502
503 /*
504  * Enable msix, positive errors indicate actual number of available
505  * vectors.  Negative errors are failures.
506  */
507 static inline int
508 pci_enable_msix(struct pci_dev *pdev, struct msix_entry *entries, int nreq)
509 {
510         struct resource_list_entry *rle;
511         int error;
512         int avail;
513         int i;
514
515         avail = pci_msix_count(pdev->dev.bsddev);
516         if (avail < nreq) {
517                 if (avail == 0)
518                         return -EINVAL;
519                 return avail;
520         }
521         avail = nreq;
522         if ((error = -pci_alloc_msix(pdev->dev.bsddev, &avail)) != 0)
523                 return error;
524         rle = _pci_get_rle(pdev, SYS_RES_IRQ, 1);
525         pdev->dev.msix = rle->start;
526         pdev->dev.msix_max = rle->start + avail;
527         for (i = 0; i < nreq; i++)
528                 entries[i].vector = pdev->dev.msix + i;
529         return (0);
530 }
531
532 /* XXX This should not be necessary. */
533 #define pcix_set_mmrbc(d, v)    0
534 #define pcix_get_max_mmrbc(d)   0
535 #define pcie_set_readrq(d, v)   0
536
537 #define PCI_DMA_BIDIRECTIONAL   0
538 #define PCI_DMA_TODEVICE        1
539 #define PCI_DMA_FROMDEVICE      2
540 #define PCI_DMA_NONE            3
541
542 #define pci_pool                dma_pool
543 #define pci_pool_destroy        dma_pool_destroy
544 #define pci_pool_alloc          dma_pool_alloc
545 #define pci_pool_free           dma_pool_free
546 #define pci_pool_create(_name, _pdev, _size, _align, _alloc)            \
547             dma_pool_create(_name, &(_pdev)->dev, _size, _align, _alloc)
548 #define pci_free_consistent(_hwdev, _size, _vaddr, _dma_handle)         \
549             dma_free_coherent((_hwdev) == NULL ? NULL : &(_hwdev)->dev, \
550                 _size, _vaddr, _dma_handle)
551 #define pci_map_sg(_hwdev, _sg, _nents, _dir)                           \
552             dma_map_sg((_hwdev) == NULL ? NULL : &(_hwdev->dev),        \
553                 _sg, _nents, (enum dma_data_direction)_dir)
554 #define pci_map_single(_hwdev, _ptr, _size, _dir)                       \
555             dma_map_single((_hwdev) == NULL ? NULL : &(_hwdev->dev),    \
556                 (_ptr), (_size), (enum dma_data_direction)_dir)
557 #define pci_unmap_single(_hwdev, _addr, _size, _dir)                    \
558             dma_unmap_single((_hwdev) == NULL ? NULL : &(_hwdev)->dev,  \
559                 _addr, _size, (enum dma_data_direction)_dir)
560 #define pci_unmap_sg(_hwdev, _sg, _nents, _dir)                         \
561             dma_unmap_sg((_hwdev) == NULL ? NULL : &(_hwdev)->dev,      \
562                 _sg, _nents, (enum dma_data_direction)_dir)
563 #define pci_map_page(_hwdev, _page, _offset, _size, _dir)               \
564             dma_map_page((_hwdev) == NULL ? NULL : &(_hwdev)->dev, _page,\
565                 _offset, _size, (enum dma_data_direction)_dir)
566 #define pci_unmap_page(_hwdev, _dma_address, _size, _dir)               \
567             dma_unmap_page((_hwdev) == NULL ? NULL : &(_hwdev)->dev,    \
568                 _dma_address, _size, (enum dma_data_direction)_dir)
569 #define pci_set_dma_mask(_pdev, mask)   dma_set_mask(&(_pdev)->dev, (mask))
570 #define pci_dma_mapping_error(_pdev, _dma_addr)                         \
571             dma_mapping_error(&(_pdev)->dev, _dma_addr)
572 #define pci_set_consistent_dma_mask(_pdev, _mask)                       \
573             dma_set_coherent_mask(&(_pdev)->dev, (_mask))
574 #define DECLARE_PCI_UNMAP_ADDR(x)       DEFINE_DMA_UNMAP_ADDR(x);
575 #define DECLARE_PCI_UNMAP_LEN(x)        DEFINE_DMA_UNMAP_LEN(x);
576 #define pci_unmap_addr          dma_unmap_addr
577 #define pci_unmap_addr_set      dma_unmap_addr_set
578 #define pci_unmap_len           dma_unmap_len
579 #define pci_unmap_len_set       dma_unmap_len_set
580
581
582 #endif  /* _LINUX_PCI_H_ */