2 * Intel MIC Platform Software Stack (MPSS)
4 * Copyright(c) 2014 Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * Intel Symmetric Communications Interface Bus driver.
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/idr.h>
20 #include <linux/dma-mapping.h>
24 static ssize_t device_show(struct device *d,
25 struct device_attribute *attr, char *buf)
27 struct scif_hw_dev *dev = dev_to_scif(d);
29 return sprintf(buf, "0x%04x\n", dev->id.device);
32 static DEVICE_ATTR_RO(device);
34 static ssize_t vendor_show(struct device *d,
35 struct device_attribute *attr, char *buf)
37 struct scif_hw_dev *dev = dev_to_scif(d);
39 return sprintf(buf, "0x%04x\n", dev->id.vendor);
42 static DEVICE_ATTR_RO(vendor);
44 static ssize_t modalias_show(struct device *d,
45 struct device_attribute *attr, char *buf)
47 struct scif_hw_dev *dev = dev_to_scif(d);
49 return sprintf(buf, "scif:d%08Xv%08X\n",
50 dev->id.device, dev->id.vendor);
53 static DEVICE_ATTR_RO(modalias);
55 static struct attribute *scif_dev_attrs[] = {
56 &dev_attr_device.attr,
57 &dev_attr_vendor.attr,
58 &dev_attr_modalias.attr,
61 ATTRIBUTE_GROUPS(scif_dev);
63 static inline int scif_id_match(const struct scif_hw_dev *dev,
64 const struct scif_hw_dev_id *id)
66 if (id->device != dev->id.device && id->device != SCIF_DEV_ANY_ID)
69 return id->vendor == SCIF_DEV_ANY_ID || id->vendor == dev->id.vendor;
73 * This looks through all the IDs a driver claims to support. If any of them
74 * match, we return 1 and the kernel will call scif_dev_probe().
76 static int scif_dev_match(struct device *dv, struct device_driver *dr)
79 struct scif_hw_dev *dev = dev_to_scif(dv);
80 const struct scif_hw_dev_id *ids;
82 ids = drv_to_scif(dr)->id_table;
83 for (i = 0; ids[i].device; i++)
84 if (scif_id_match(dev, &ids[i]))
89 static int scif_uevent(struct device *dv, struct kobj_uevent_env *env)
91 struct scif_hw_dev *dev = dev_to_scif(dv);
93 return add_uevent_var(env, "MODALIAS=scif:d%08Xv%08X",
94 dev->id.device, dev->id.vendor);
97 static int scif_dev_probe(struct device *d)
99 struct scif_hw_dev *dev = dev_to_scif(d);
100 struct scif_driver *drv = drv_to_scif(dev->dev.driver);
102 return drv->probe(dev);
105 static int scif_dev_remove(struct device *d)
107 struct scif_hw_dev *dev = dev_to_scif(d);
108 struct scif_driver *drv = drv_to_scif(dev->dev.driver);
114 static struct bus_type scif_bus = {
116 .match = scif_dev_match,
117 .dev_groups = scif_dev_groups,
118 .uevent = scif_uevent,
119 .probe = scif_dev_probe,
120 .remove = scif_dev_remove,
123 int scif_register_driver(struct scif_driver *driver)
125 driver->driver.bus = &scif_bus;
126 return driver_register(&driver->driver);
128 EXPORT_SYMBOL_GPL(scif_register_driver);
130 void scif_unregister_driver(struct scif_driver *driver)
132 driver_unregister(&driver->driver);
134 EXPORT_SYMBOL_GPL(scif_unregister_driver);
136 static void scif_release_dev(struct device *d)
138 struct scif_hw_dev *sdev = dev_to_scif(d);
144 scif_register_device(struct device *pdev, int id, struct dma_map_ops *dma_ops,
145 struct scif_hw_ops *hw_ops, u8 dnode, u8 snode,
146 struct mic_mw *mmio, struct mic_mw *aper, void *dp,
147 void __iomem *rdp, struct dma_chan **chan, int num_chan)
150 struct scif_hw_dev *sdev;
152 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
154 return ERR_PTR(-ENOMEM);
156 sdev->dev.parent = pdev;
157 sdev->id.device = id;
158 sdev->id.vendor = SCIF_DEV_ANY_ID;
159 sdev->dev.archdata.dma_ops = dma_ops;
160 sdev->dev.release = scif_release_dev;
161 sdev->hw_ops = hw_ops;
164 dev_set_drvdata(&sdev->dev, sdev);
165 sdev->dev.bus = &scif_bus;
170 sdev->dev.dma_mask = &sdev->dev.coherent_dma_mask;
171 dma_set_mask(&sdev->dev, DMA_BIT_MASK(64));
173 sdev->num_dma_ch = num_chan;
174 dev_set_name(&sdev->dev, "scif-dev%u", sdev->dnode);
176 * device_register() causes the bus infrastructure to look for a
179 ret = device_register(&sdev->dev);
187 EXPORT_SYMBOL_GPL(scif_register_device);
189 void scif_unregister_device(struct scif_hw_dev *sdev)
191 device_unregister(&sdev->dev);
193 EXPORT_SYMBOL_GPL(scif_unregister_device);
195 static int __init scif_init(void)
197 return bus_register(&scif_bus);
200 static void __exit scif_exit(void)
202 bus_unregister(&scif_bus);
205 core_initcall(scif_init);
206 module_exit(scif_exit);
208 MODULE_AUTHOR("Intel Corporation");
209 MODULE_DESCRIPTION("Intel(R) SCIF Bus driver");
210 MODULE_LICENSE("GPL v2");