2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
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.
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.
32 #define CONFIG_PCI_MSI
34 #include <linux/types.h>
36 #include <sys/param.h>
38 #include <sys/pciio.h>
40 #include <dev/pci/pcivar.h>
41 #include <dev/pci/pcireg.h>
42 #include <dev/pci/pci_private.h>
44 #include <machine/resource.h>
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>
55 struct pci_device_id {
61 uintptr_t driver_data;
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
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
81 #define to_pci_dev(n) container_of(n, struct pci_dev, dev)
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
88 #define IORESOURCE_MEM SYS_RES_MEMORY
89 #define IORESOURCE_IO SYS_RES_IOPORT
90 #define IORESOURCE_IRQ SYS_RES_IRQ
95 struct list_head links;
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);
104 extern struct list_head pci_drivers;
105 extern struct list_head pci_devices;
106 extern spinlock_t pci_lock;
108 #define __devexit_p(x) x
112 struct list_head links;
113 struct pci_driver *pdrv;
120 static inline struct resource_list_entry *
121 _pci_get_rle(struct pci_dev *pdev, int type, int rid)
123 struct pci_devinfo *dinfo;
124 struct resource_list *rl;
126 dinfo = device_get_ivars(pdev->dev.bsddev);
127 rl = &dinfo->resources;
128 return resource_list_find(rl, type, rid);
131 static inline struct resource_list_entry *
132 _pci_get_bar(struct pci_dev *pdev, int bar)
134 struct resource_list_entry *rle;
137 if ((rle = _pci_get_rle(pdev, SYS_RES_MEMORY, bar)) == NULL)
138 rle = _pci_get_rle(pdev, SYS_RES_IOPORT, bar);
142 static inline struct device *
143 _pci_find_irq_dev(unsigned int irq)
145 struct pci_dev *pdev;
147 spin_lock(&pci_lock);
148 list_for_each_entry(pdev, &pci_devices, links) {
149 if (irq == pdev->dev.irq)
151 if (irq >= pdev->dev.msix && irq < pdev->dev.msix_max)
154 spin_unlock(&pci_lock);
160 static inline unsigned long
161 pci_resource_start(struct pci_dev *pdev, int bar)
163 struct resource_list_entry *rle;
165 if ((rle = _pci_get_bar(pdev, bar)) == NULL)
170 static inline unsigned long
171 pci_resource_len(struct pci_dev *pdev, int bar)
173 struct resource_list_entry *rle;
175 if ((rle = _pci_get_bar(pdev, bar)) == NULL)
181 * All drivers just seem to want to inspect the type not flags.
184 pci_resource_flags(struct pci_dev *pdev, int bar)
186 struct resource_list_entry *rle;
188 if ((rle = _pci_get_bar(pdev, bar)) == NULL)
193 static inline const char *
194 pci_name(struct pci_dev *d)
197 return device_get_desc(d->dev.bsddev);
201 pci_get_drvdata(struct pci_dev *pdev)
204 return dev_get_drvdata(&pdev->dev);
208 pci_set_drvdata(struct pci_dev *pdev, void *data)
211 dev_set_drvdata(&pdev->dev, data);
215 pci_enable_device(struct pci_dev *pdev)
218 pci_enable_io(pdev->dev.bsddev, SYS_RES_IOPORT);
219 pci_enable_io(pdev->dev.bsddev, SYS_RES_MEMORY);
224 pci_disable_device(struct pci_dev *pdev)
229 pci_set_master(struct pci_dev *pdev)
232 pci_enable_busmaster(pdev->dev.bsddev);
237 pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
242 type = pci_resource_flags(pdev, bar);
246 if (bus_alloc_resource_any(pdev->dev.bsddev, type, &rid,
253 pci_release_region(struct pci_dev *pdev, int bar)
255 struct resource_list_entry *rle;
257 if ((rle = _pci_get_bar(pdev, bar)) == NULL)
259 bus_release_resource(pdev->dev.bsddev, rle->type, rle->rid, rle->res);
263 pci_release_regions(struct pci_dev *pdev)
267 for (i = 0; i <= PCIR_MAX_BAR_0; i++)
268 pci_release_region(pdev, i);
272 pci_request_regions(struct pci_dev *pdev, const char *res_name)
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);
288 pci_disable_msix(struct pci_dev *pdev)
291 pci_release_msi(pdev->dev.bsddev);
294 #define PCI_CAP_ID_EXP PCIY_EXPRESS
295 #define PCI_CAP_ID_PCIX PCIY_PCIX
298 pci_find_capability(struct pci_dev *pdev, int capid)
302 if (pci_find_cap(pdev->dev.bsddev, capid, ®))
308 pci_read_config_byte(struct pci_dev *pdev, int where, u8 *val)
311 *val = (u8)pci_read_config(pdev->dev.bsddev, where, 1);
316 pci_read_config_word(struct pci_dev *pdev, int where, u16 *val)
319 *val = (u16)pci_read_config(pdev->dev.bsddev, where, 2);
324 pci_read_config_dword(struct pci_dev *pdev, int where, u32 *val)
327 *val = (u32)pci_read_config(pdev->dev.bsddev, where, 4);
332 pci_write_config_byte(struct pci_dev *pdev, int where, u8 val)
335 pci_write_config(pdev->dev.bsddev, where, val, 1);
340 pci_write_config_word(struct pci_dev *pdev, int where, u16 val)
343 pci_write_config(pdev->dev.bsddev, where, val, 2);
348 pci_write_config_dword(struct pci_dev *pdev, int where, u32 val)
351 pci_write_config(pdev->dev.bsddev, where, val, 4);
355 static struct pci_driver *
356 linux_pci_find(device_t dev, struct pci_device_id **idp)
358 struct pci_device_id *id;
359 struct pci_driver *pdrv;
363 vendor = pci_get_vendor(dev);
364 device = pci_get_device(dev);
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) {
371 spin_unlock(&pci_lock);
376 spin_unlock(&pci_lock);
381 linux_pci_probe(device_t dev)
383 struct pci_device_id *id;
384 struct pci_driver *pdrv;
386 if ((pdrv = linux_pci_find(dev, &id)) == NULL)
388 if (device_get_driver(dev) != &pdrv->driver)
390 device_set_desc(dev, pdrv->name);
395 linux_pci_attach(device_t dev)
397 struct resource_list_entry *rle;
398 struct pci_dev *pdev;
399 struct pci_driver *pdrv;
400 struct pci_device_id *id;
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;
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);
418 pdev->dev.irq = rle->start;
421 pdev->irq = pdev->dev.irq;
423 spin_lock(&pci_lock);
424 list_add(&pdev->links, &pci_devices);
425 spin_unlock(&pci_lock);
426 error = pdrv->probe(pdev, id);
429 spin_lock(&pci_lock);
430 list_del(&pdev->links);
431 spin_unlock(&pci_lock);
432 put_device(&pdev->dev);
439 linux_pci_detach(device_t dev)
441 struct pci_dev *pdev;
443 pdev = device_get_softc(dev);
445 pdev->pdrv->remove(pdev);
447 spin_lock(&pci_lock);
448 list_del(&pdev->links);
449 spin_unlock(&pci_lock);
450 put_device(&pdev->dev);
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),
463 pci_register_driver(struct pci_driver *pdrv)
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);
476 error = devclass_add_driver(bus, &pdrv->driver, BUS_PASS_DEFAULT,
485 pci_unregister_driver(struct pci_driver *pdrv)
489 list_del(&pdrv->links);
490 bus = devclass_find("pci");
492 devclass_delete_driver(bus, &pdrv->driver);
502 * Enable msix, positive errors indicate actual number of available
503 * vectors. Negative errors are failures.
506 pci_enable_msix(struct pci_dev *pdev, struct msix_entry *entries, int nreq)
508 struct resource_list_entry *rle;
513 avail = pci_msix_count(pdev->dev.bsddev);
520 if ((error = -pci_alloc_msix(pdev->dev.bsddev, &avail)) != 0)
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;
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
535 #define PCI_DMA_BIDIRECTIONAL 0
536 #define PCI_DMA_TODEVICE 1
537 #define PCI_DMA_FROMDEVICE 2
538 #define PCI_DMA_NONE 3
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
580 #endif /* _LINUX_PCI_H_ */