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 .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
83 #define to_pci_dev(n) container_of(n, struct pci_dev, dev)
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
90 #define IORESOURCE_MEM SYS_RES_MEMORY
91 #define IORESOURCE_IO SYS_RES_IOPORT
92 #define IORESOURCE_IRQ SYS_RES_IRQ
97 struct list_head links;
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);
106 extern struct list_head pci_drivers;
107 extern struct list_head pci_devices;
108 extern spinlock_t pci_lock;
110 #define __devexit_p(x) x
114 struct list_head links;
115 struct pci_driver *pdrv;
122 static inline struct resource_list_entry *
123 _pci_get_rle(struct pci_dev *pdev, int type, int rid)
125 struct pci_devinfo *dinfo;
126 struct resource_list *rl;
128 dinfo = device_get_ivars(pdev->dev.bsddev);
129 rl = &dinfo->resources;
130 return resource_list_find(rl, type, rid);
133 static inline struct resource_list_entry *
134 _pci_get_bar(struct pci_dev *pdev, int bar)
136 struct resource_list_entry *rle;
139 if ((rle = _pci_get_rle(pdev, SYS_RES_MEMORY, bar)) == NULL)
140 rle = _pci_get_rle(pdev, SYS_RES_IOPORT, bar);
144 static inline struct device *
145 _pci_find_irq_dev(unsigned int irq)
147 struct pci_dev *pdev;
149 spin_lock(&pci_lock);
150 list_for_each_entry(pdev, &pci_devices, links) {
151 if (irq == pdev->dev.irq)
153 if (irq >= pdev->dev.msix && irq < pdev->dev.msix_max)
156 spin_unlock(&pci_lock);
162 static inline unsigned long
163 pci_resource_start(struct pci_dev *pdev, int bar)
165 struct resource_list_entry *rle;
167 if ((rle = _pci_get_bar(pdev, bar)) == NULL)
172 static inline unsigned long
173 pci_resource_len(struct pci_dev *pdev, int bar)
175 struct resource_list_entry *rle;
177 if ((rle = _pci_get_bar(pdev, bar)) == NULL)
183 * All drivers just seem to want to inspect the type not flags.
186 pci_resource_flags(struct pci_dev *pdev, int bar)
188 struct resource_list_entry *rle;
190 if ((rle = _pci_get_bar(pdev, bar)) == NULL)
195 static inline const char *
196 pci_name(struct pci_dev *d)
199 return device_get_desc(d->dev.bsddev);
203 pci_get_drvdata(struct pci_dev *pdev)
206 return dev_get_drvdata(&pdev->dev);
210 pci_set_drvdata(struct pci_dev *pdev, void *data)
213 dev_set_drvdata(&pdev->dev, data);
217 pci_enable_device(struct pci_dev *pdev)
220 pci_enable_io(pdev->dev.bsddev, SYS_RES_IOPORT);
221 pci_enable_io(pdev->dev.bsddev, SYS_RES_MEMORY);
226 pci_disable_device(struct pci_dev *pdev)
231 pci_set_master(struct pci_dev *pdev)
234 pci_enable_busmaster(pdev->dev.bsddev);
239 pci_request_region(struct pci_dev *pdev, int bar, const char *res_name)
244 type = pci_resource_flags(pdev, bar);
248 if (bus_alloc_resource_any(pdev->dev.bsddev, type, &rid,
255 pci_release_region(struct pci_dev *pdev, int bar)
257 struct resource_list_entry *rle;
259 if ((rle = _pci_get_bar(pdev, bar)) == NULL)
261 bus_release_resource(pdev->dev.bsddev, rle->type, rle->rid, rle->res);
265 pci_release_regions(struct pci_dev *pdev)
269 for (i = 0; i <= PCIR_MAX_BAR_0; i++)
270 pci_release_region(pdev, i);
274 pci_request_regions(struct pci_dev *pdev, const char *res_name)
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);
290 pci_disable_msix(struct pci_dev *pdev)
293 pci_release_msi(pdev->dev.bsddev);
296 #define PCI_CAP_ID_EXP PCIY_EXPRESS
297 #define PCI_CAP_ID_PCIX PCIY_PCIX
300 pci_find_capability(struct pci_dev *pdev, int capid)
304 if (pci_find_cap(pdev->dev.bsddev, capid, ®))
310 pci_read_config_byte(struct pci_dev *pdev, int where, u8 *val)
313 *val = (u8)pci_read_config(pdev->dev.bsddev, where, 1);
318 pci_read_config_word(struct pci_dev *pdev, int where, u16 *val)
321 *val = (u16)pci_read_config(pdev->dev.bsddev, where, 2);
326 pci_read_config_dword(struct pci_dev *pdev, int where, u32 *val)
329 *val = (u32)pci_read_config(pdev->dev.bsddev, where, 4);
334 pci_write_config_byte(struct pci_dev *pdev, int where, u8 val)
337 pci_write_config(pdev->dev.bsddev, where, val, 1);
342 pci_write_config_word(struct pci_dev *pdev, int where, u16 val)
345 pci_write_config(pdev->dev.bsddev, where, val, 2);
350 pci_write_config_dword(struct pci_dev *pdev, int where, u32 val)
353 pci_write_config(pdev->dev.bsddev, where, val, 4);
357 static struct pci_driver *
358 linux_pci_find(device_t dev, struct pci_device_id **idp)
360 struct pci_device_id *id;
361 struct pci_driver *pdrv;
365 vendor = pci_get_vendor(dev);
366 device = pci_get_device(dev);
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) {
373 spin_unlock(&pci_lock);
378 spin_unlock(&pci_lock);
383 linux_pci_probe(device_t dev)
385 struct pci_device_id *id;
386 struct pci_driver *pdrv;
388 if ((pdrv = linux_pci_find(dev, &id)) == NULL)
390 if (device_get_driver(dev) != &pdrv->driver)
392 device_set_desc(dev, pdrv->name);
397 linux_pci_attach(device_t dev)
399 struct resource_list_entry *rle;
400 struct pci_dev *pdev;
401 struct pci_driver *pdrv;
402 struct pci_device_id *id;
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;
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);
420 pdev->dev.irq = rle->start;
423 pdev->irq = pdev->dev.irq;
425 spin_lock(&pci_lock);
426 list_add(&pdev->links, &pci_devices);
427 spin_unlock(&pci_lock);
428 error = pdrv->probe(pdev, id);
431 spin_lock(&pci_lock);
432 list_del(&pdev->links);
433 spin_unlock(&pci_lock);
434 put_device(&pdev->dev);
441 linux_pci_detach(device_t dev)
443 struct pci_dev *pdev;
445 pdev = device_get_softc(dev);
447 pdev->pdrv->remove(pdev);
449 spin_lock(&pci_lock);
450 list_del(&pdev->links);
451 spin_unlock(&pci_lock);
452 put_device(&pdev->dev);
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),
465 pci_register_driver(struct pci_driver *pdrv)
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);
478 error = devclass_add_driver(bus, &pdrv->driver, BUS_PASS_DEFAULT,
487 pci_unregister_driver(struct pci_driver *pdrv)
491 list_del(&pdrv->links);
492 bus = devclass_find("pci");
494 devclass_delete_driver(bus, &pdrv->driver);
504 * Enable msix, positive errors indicate actual number of available
505 * vectors. Negative errors are failures.
508 pci_enable_msix(struct pci_dev *pdev, struct msix_entry *entries, int nreq)
510 struct resource_list_entry *rle;
515 avail = pci_msix_count(pdev->dev.bsddev);
522 if ((error = -pci_alloc_msix(pdev->dev.bsddev, &avail)) != 0)
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;
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
537 #define PCI_DMA_BIDIRECTIONAL 0
538 #define PCI_DMA_TODEVICE 1
539 #define PCI_DMA_FROMDEVICE 2
540 #define PCI_DMA_NONE 3
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
582 #endif /* _LINUX_PCI_H_ */