Merge tag 'ieee802154-for-davem-2019-11-13' of git://git.kernel.org/pub/scm/linux...
[linux-2.6-microblaze.git] / drivers / misc / mic / bus / vop_bus.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Intel MIC Platform Software Stack (MPSS)
4  *
5  * Copyright(c) 2016 Intel Corporation.
6  *
7  * Intel Virtio Over PCIe (VOP) Bus driver.
8  */
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <linux/idr.h>
12 #include <linux/dma-mapping.h>
13
14 #include "vop_bus.h"
15
16 static ssize_t device_show(struct device *d,
17                            struct device_attribute *attr, char *buf)
18 {
19         struct vop_device *dev = dev_to_vop(d);
20
21         return sprintf(buf, "0x%04x\n", dev->id.device);
22 }
23 static DEVICE_ATTR_RO(device);
24
25 static ssize_t vendor_show(struct device *d,
26                            struct device_attribute *attr, char *buf)
27 {
28         struct vop_device *dev = dev_to_vop(d);
29
30         return sprintf(buf, "0x%04x\n", dev->id.vendor);
31 }
32 static DEVICE_ATTR_RO(vendor);
33
34 static ssize_t modalias_show(struct device *d,
35                              struct device_attribute *attr, char *buf)
36 {
37         struct vop_device *dev = dev_to_vop(d);
38
39         return sprintf(buf, "vop:d%08Xv%08X\n",
40                        dev->id.device, dev->id.vendor);
41 }
42 static DEVICE_ATTR_RO(modalias);
43
44 static struct attribute *vop_dev_attrs[] = {
45         &dev_attr_device.attr,
46         &dev_attr_vendor.attr,
47         &dev_attr_modalias.attr,
48         NULL,
49 };
50 ATTRIBUTE_GROUPS(vop_dev);
51
52 static inline int vop_id_match(const struct vop_device *dev,
53                                const struct vop_device_id *id)
54 {
55         if (id->device != dev->id.device && id->device != VOP_DEV_ANY_ID)
56                 return 0;
57
58         return id->vendor == VOP_DEV_ANY_ID || id->vendor == dev->id.vendor;
59 }
60
61 /*
62  * This looks through all the IDs a driver claims to support.  If any of them
63  * match, we return 1 and the kernel will call vop_dev_probe().
64  */
65 static int vop_dev_match(struct device *dv, struct device_driver *dr)
66 {
67         unsigned int i;
68         struct vop_device *dev = dev_to_vop(dv);
69         const struct vop_device_id *ids;
70
71         ids = drv_to_vop(dr)->id_table;
72         for (i = 0; ids[i].device; i++)
73                 if (vop_id_match(dev, &ids[i]))
74                         return 1;
75         return 0;
76 }
77
78 static int vop_uevent(struct device *dv, struct kobj_uevent_env *env)
79 {
80         struct vop_device *dev = dev_to_vop(dv);
81
82         return add_uevent_var(env, "MODALIAS=vop:d%08Xv%08X",
83                               dev->id.device, dev->id.vendor);
84 }
85
86 static int vop_dev_probe(struct device *d)
87 {
88         struct vop_device *dev = dev_to_vop(d);
89         struct vop_driver *drv = drv_to_vop(dev->dev.driver);
90
91         return drv->probe(dev);
92 }
93
94 static int vop_dev_remove(struct device *d)
95 {
96         struct vop_device *dev = dev_to_vop(d);
97         struct vop_driver *drv = drv_to_vop(dev->dev.driver);
98
99         drv->remove(dev);
100         return 0;
101 }
102
103 static struct bus_type vop_bus = {
104         .name  = "vop_bus",
105         .match = vop_dev_match,
106         .dev_groups = vop_dev_groups,
107         .uevent = vop_uevent,
108         .probe = vop_dev_probe,
109         .remove = vop_dev_remove,
110 };
111
112 int vop_register_driver(struct vop_driver *driver)
113 {
114         driver->driver.bus = &vop_bus;
115         return driver_register(&driver->driver);
116 }
117 EXPORT_SYMBOL_GPL(vop_register_driver);
118
119 void vop_unregister_driver(struct vop_driver *driver)
120 {
121         driver_unregister(&driver->driver);
122 }
123 EXPORT_SYMBOL_GPL(vop_unregister_driver);
124
125 static void vop_release_dev(struct device *d)
126 {
127         struct vop_device *dev = dev_to_vop(d);
128
129         kfree(dev);
130 }
131
132 struct vop_device *
133 vop_register_device(struct device *pdev, int id,
134                     const struct dma_map_ops *dma_ops,
135                     struct vop_hw_ops *hw_ops, u8 dnode, struct mic_mw *aper,
136                     struct dma_chan *chan)
137 {
138         int ret;
139         struct vop_device *vdev;
140
141         vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
142         if (!vdev)
143                 return ERR_PTR(-ENOMEM);
144
145         vdev->dev.parent = pdev;
146         vdev->id.device = id;
147         vdev->id.vendor = VOP_DEV_ANY_ID;
148         vdev->dev.dma_ops = dma_ops;
149         vdev->dev.dma_mask = &vdev->dev.coherent_dma_mask;
150         dma_set_mask(&vdev->dev, DMA_BIT_MASK(64));
151         vdev->dev.release = vop_release_dev;
152         vdev->hw_ops = hw_ops;
153         vdev->dev.bus = &vop_bus;
154         vdev->dnode = dnode;
155         vdev->aper = aper;
156         vdev->dma_ch = chan;
157         vdev->index = dnode - 1;
158         dev_set_name(&vdev->dev, "vop-dev%u", vdev->index);
159         /*
160          * device_register() causes the bus infrastructure to look for a
161          * matching driver.
162          */
163         ret = device_register(&vdev->dev);
164         if (ret)
165                 goto free_vdev;
166         return vdev;
167 free_vdev:
168         put_device(&vdev->dev);
169         return ERR_PTR(ret);
170 }
171 EXPORT_SYMBOL_GPL(vop_register_device);
172
173 void vop_unregister_device(struct vop_device *dev)
174 {
175         device_unregister(&dev->dev);
176 }
177 EXPORT_SYMBOL_GPL(vop_unregister_device);
178
179 static int __init vop_init(void)
180 {
181         return bus_register(&vop_bus);
182 }
183
184 static void __exit vop_exit(void)
185 {
186         bus_unregister(&vop_bus);
187 }
188
189 core_initcall(vop_init);
190 module_exit(vop_exit);
191
192 MODULE_AUTHOR("Intel Corporation");
193 MODULE_DESCRIPTION("Intel(R) VOP Bus driver");
194 MODULE_LICENSE("GPL v2");