|
| 1 | +/* |
| 2 | + * Intel MIC Platform Software Stack (MPSS) |
| 3 | + * |
| 4 | + * Copyright(c) 2014 Intel Corporation. |
| 5 | + * |
| 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. |
| 9 | + * |
| 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. |
| 14 | + * |
| 15 | + * Intel Symmetric Communications Interface Bus driver. |
| 16 | + */ |
| 17 | +#include <linux/slab.h> |
| 18 | +#include <linux/module.h> |
| 19 | +#include <linux/idr.h> |
| 20 | +#include <linux/dma-mapping.h> |
| 21 | + |
| 22 | +#include "scif_bus.h" |
| 23 | + |
| 24 | +static ssize_t device_show(struct device *d, |
| 25 | + struct device_attribute *attr, char *buf) |
| 26 | +{ |
| 27 | + struct scif_hw_dev *dev = dev_to_scif(d); |
| 28 | + |
| 29 | + return sprintf(buf, "0x%04x\n", dev->id.device); |
| 30 | +} |
| 31 | + |
| 32 | +static DEVICE_ATTR_RO(device); |
| 33 | + |
| 34 | +static ssize_t vendor_show(struct device *d, |
| 35 | + struct device_attribute *attr, char *buf) |
| 36 | +{ |
| 37 | + struct scif_hw_dev *dev = dev_to_scif(d); |
| 38 | + |
| 39 | + return sprintf(buf, "0x%04x\n", dev->id.vendor); |
| 40 | +} |
| 41 | + |
| 42 | +static DEVICE_ATTR_RO(vendor); |
| 43 | + |
| 44 | +static ssize_t modalias_show(struct device *d, |
| 45 | + struct device_attribute *attr, char *buf) |
| 46 | +{ |
| 47 | + struct scif_hw_dev *dev = dev_to_scif(d); |
| 48 | + |
| 49 | + return sprintf(buf, "scif:d%08Xv%08X\n", |
| 50 | + dev->id.device, dev->id.vendor); |
| 51 | +} |
| 52 | + |
| 53 | +static DEVICE_ATTR_RO(modalias); |
| 54 | + |
| 55 | +static struct attribute *scif_dev_attrs[] = { |
| 56 | + &dev_attr_device.attr, |
| 57 | + &dev_attr_vendor.attr, |
| 58 | + &dev_attr_modalias.attr, |
| 59 | + NULL, |
| 60 | +}; |
| 61 | +ATTRIBUTE_GROUPS(scif_dev); |
| 62 | + |
| 63 | +static inline int scif_id_match(const struct scif_hw_dev *dev, |
| 64 | + const struct scif_hw_dev_id *id) |
| 65 | +{ |
| 66 | + if (id->device != dev->id.device && id->device != SCIF_DEV_ANY_ID) |
| 67 | + return 0; |
| 68 | + |
| 69 | + return id->vendor == SCIF_DEV_ANY_ID || id->vendor == dev->id.vendor; |
| 70 | +} |
| 71 | + |
| 72 | +/* |
| 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(). |
| 75 | + */ |
| 76 | +static int scif_dev_match(struct device *dv, struct device_driver *dr) |
| 77 | +{ |
| 78 | + unsigned int i; |
| 79 | + struct scif_hw_dev *dev = dev_to_scif(dv); |
| 80 | + const struct scif_hw_dev_id *ids; |
| 81 | + |
| 82 | + ids = drv_to_scif(dr)->id_table; |
| 83 | + for (i = 0; ids[i].device; i++) |
| 84 | + if (scif_id_match(dev, &ids[i])) |
| 85 | + return 1; |
| 86 | + return 0; |
| 87 | +} |
| 88 | + |
| 89 | +static int scif_uevent(struct device *dv, struct kobj_uevent_env *env) |
| 90 | +{ |
| 91 | + struct scif_hw_dev *dev = dev_to_scif(dv); |
| 92 | + |
| 93 | + return add_uevent_var(env, "MODALIAS=scif:d%08Xv%08X", |
| 94 | + dev->id.device, dev->id.vendor); |
| 95 | +} |
| 96 | + |
| 97 | +static int scif_dev_probe(struct device *d) |
| 98 | +{ |
| 99 | + struct scif_hw_dev *dev = dev_to_scif(d); |
| 100 | + struct scif_driver *drv = drv_to_scif(dev->dev.driver); |
| 101 | + |
| 102 | + return drv->probe(dev); |
| 103 | +} |
| 104 | + |
| 105 | +static int scif_dev_remove(struct device *d) |
| 106 | +{ |
| 107 | + struct scif_hw_dev *dev = dev_to_scif(d); |
| 108 | + struct scif_driver *drv = drv_to_scif(dev->dev.driver); |
| 109 | + |
| 110 | + drv->remove(dev); |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | +static struct bus_type scif_bus = { |
| 115 | + .name = "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, |
| 121 | +}; |
| 122 | + |
| 123 | +int scif_register_driver(struct scif_driver *driver) |
| 124 | +{ |
| 125 | + driver->driver.bus = &scif_bus; |
| 126 | + return driver_register(&driver->driver); |
| 127 | +} |
| 128 | +EXPORT_SYMBOL_GPL(scif_register_driver); |
| 129 | + |
| 130 | +void scif_unregister_driver(struct scif_driver *driver) |
| 131 | +{ |
| 132 | + driver_unregister(&driver->driver); |
| 133 | +} |
| 134 | +EXPORT_SYMBOL_GPL(scif_unregister_driver); |
| 135 | + |
| 136 | +static void scif_release_dev(struct device *d) |
| 137 | +{ |
| 138 | + struct scif_hw_dev *sdev = dev_to_scif(d); |
| 139 | + |
| 140 | + kfree(sdev); |
| 141 | +} |
| 142 | + |
| 143 | +struct scif_hw_dev * |
| 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) |
| 148 | +{ |
| 149 | + int ret; |
| 150 | + struct scif_hw_dev *sdev; |
| 151 | + |
| 152 | + sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); |
| 153 | + if (!sdev) |
| 154 | + return ERR_PTR(-ENOMEM); |
| 155 | + |
| 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; |
| 162 | + sdev->dnode = dnode; |
| 163 | + sdev->snode = snode; |
| 164 | + dev_set_drvdata(&sdev->dev, sdev); |
| 165 | + sdev->dev.bus = &scif_bus; |
| 166 | + sdev->mmio = mmio; |
| 167 | + sdev->aper = aper; |
| 168 | + sdev->dp = dp; |
| 169 | + sdev->rdp = rdp; |
| 170 | + sdev->dev.dma_mask = &sdev->dev.coherent_dma_mask; |
| 171 | + dma_set_mask(&sdev->dev, DMA_BIT_MASK(64)); |
| 172 | + sdev->dma_ch = chan; |
| 173 | + sdev->num_dma_ch = num_chan; |
| 174 | + dev_set_name(&sdev->dev, "scif-dev%u", sdev->dnode); |
| 175 | + /* |
| 176 | + * device_register() causes the bus infrastructure to look for a |
| 177 | + * matching driver. |
| 178 | + */ |
| 179 | + ret = device_register(&sdev->dev); |
| 180 | + if (ret) |
| 181 | + goto free_sdev; |
| 182 | + return sdev; |
| 183 | +free_sdev: |
| 184 | + kfree(sdev); |
| 185 | + return ERR_PTR(ret); |
| 186 | +} |
| 187 | +EXPORT_SYMBOL_GPL(scif_register_device); |
| 188 | + |
| 189 | +void scif_unregister_device(struct scif_hw_dev *sdev) |
| 190 | +{ |
| 191 | + device_unregister(&sdev->dev); |
| 192 | +} |
| 193 | +EXPORT_SYMBOL_GPL(scif_unregister_device); |
| 194 | + |
| 195 | +static int __init scif_init(void) |
| 196 | +{ |
| 197 | + return bus_register(&scif_bus); |
| 198 | +} |
| 199 | + |
| 200 | +static void __exit scif_exit(void) |
| 201 | +{ |
| 202 | + bus_unregister(&scif_bus); |
| 203 | +} |
| 204 | + |
| 205 | +core_initcall(scif_init); |
| 206 | +module_exit(scif_exit); |
| 207 | + |
| 208 | +MODULE_AUTHOR("Intel Corporation"); |
| 209 | +MODULE_DESCRIPTION("Intel(R) SCIF Bus driver"); |
| 210 | +MODULE_LICENSE("GPL v2"); |
0 commit comments