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 
device_show(struct device * d,struct device_attribute * attr,char * buf)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 static DEVICE_ATTR_RO(device);
32 
vendor_show(struct device * d,struct device_attribute * attr,char * buf)33 static ssize_t vendor_show(struct device *d,
34 			   struct device_attribute *attr, char *buf)
35 {
36 	struct scif_hw_dev *dev = dev_to_scif(d);
37 
38 	return sprintf(buf, "0x%04x\n", dev->id.vendor);
39 }
40 static DEVICE_ATTR_RO(vendor);
41 
modalias_show(struct device * d,struct device_attribute * attr,char * buf)42 static ssize_t modalias_show(struct device *d,
43 			     struct device_attribute *attr, char *buf)
44 {
45 	struct scif_hw_dev *dev = dev_to_scif(d);
46 
47 	return sprintf(buf, "scif:d%08Xv%08X\n",
48 		       dev->id.device, dev->id.vendor);
49 }
50 static DEVICE_ATTR_RO(modalias);
51 
52 static struct attribute *scif_dev_attrs[] = {
53 	&dev_attr_device.attr,
54 	&dev_attr_vendor.attr,
55 	&dev_attr_modalias.attr,
56 	NULL,
57 };
58 ATTRIBUTE_GROUPS(scif_dev);
59 
scif_id_match(const struct scif_hw_dev * dev,const struct scif_hw_dev_id * id)60 static inline int scif_id_match(const struct scif_hw_dev *dev,
61 				const struct scif_hw_dev_id *id)
62 {
63 	if (id->device != dev->id.device && id->device != SCIF_DEV_ANY_ID)
64 		return 0;
65 
66 	return id->vendor == SCIF_DEV_ANY_ID || id->vendor == dev->id.vendor;
67 }
68 
69 /*
70  * This looks through all the IDs a driver claims to support.  If any of them
71  * match, we return 1 and the kernel will call scif_dev_probe().
72  */
scif_dev_match(struct device * dv,struct device_driver * dr)73 static int scif_dev_match(struct device *dv, struct device_driver *dr)
74 {
75 	unsigned int i;
76 	struct scif_hw_dev *dev = dev_to_scif(dv);
77 	const struct scif_hw_dev_id *ids;
78 
79 	ids = drv_to_scif(dr)->id_table;
80 	for (i = 0; ids[i].device; i++)
81 		if (scif_id_match(dev, &ids[i]))
82 			return 1;
83 	return 0;
84 }
85 
scif_uevent(struct device * dv,struct kobj_uevent_env * env)86 static int scif_uevent(struct device *dv, struct kobj_uevent_env *env)
87 {
88 	struct scif_hw_dev *dev = dev_to_scif(dv);
89 
90 	return add_uevent_var(env, "MODALIAS=scif:d%08Xv%08X",
91 			      dev->id.device, dev->id.vendor);
92 }
93 
scif_dev_probe(struct device * d)94 static int scif_dev_probe(struct device *d)
95 {
96 	struct scif_hw_dev *dev = dev_to_scif(d);
97 	struct scif_driver *drv = drv_to_scif(dev->dev.driver);
98 
99 	return drv->probe(dev);
100 }
101 
scif_dev_remove(struct device * d)102 static int scif_dev_remove(struct device *d)
103 {
104 	struct scif_hw_dev *dev = dev_to_scif(d);
105 	struct scif_driver *drv = drv_to_scif(dev->dev.driver);
106 
107 	drv->remove(dev);
108 	return 0;
109 }
110 
111 static struct bus_type scif_bus = {
112 	.name  = "scif_bus",
113 	.match = scif_dev_match,
114 	.dev_groups = scif_dev_groups,
115 	.uevent = scif_uevent,
116 	.probe = scif_dev_probe,
117 	.remove = scif_dev_remove,
118 };
119 
scif_register_driver(struct scif_driver * driver)120 int scif_register_driver(struct scif_driver *driver)
121 {
122 	driver->driver.bus = &scif_bus;
123 	return driver_register(&driver->driver);
124 }
125 EXPORT_SYMBOL_GPL(scif_register_driver);
126 
scif_unregister_driver(struct scif_driver * driver)127 void scif_unregister_driver(struct scif_driver *driver)
128 {
129 	driver_unregister(&driver->driver);
130 }
131 EXPORT_SYMBOL_GPL(scif_unregister_driver);
132 
scif_release_dev(struct device * d)133 static void scif_release_dev(struct device *d)
134 {
135 	struct scif_hw_dev *sdev = dev_to_scif(d);
136 
137 	kfree(sdev);
138 }
139 
140 struct scif_hw_dev *
scif_register_device(struct device * pdev,int id,const struct dma_map_ops * dma_ops,struct scif_hw_ops * hw_ops,u8 dnode,u8 snode,struct mic_mw * mmio,struct mic_mw * aper,void * dp,void __iomem * rdp,struct dma_chan ** chan,int num_chan,bool card_rel_da)141 scif_register_device(struct device *pdev, int id, const struct dma_map_ops *dma_ops,
142 		     struct scif_hw_ops *hw_ops, u8 dnode, u8 snode,
143 		     struct mic_mw *mmio, struct mic_mw *aper, void *dp,
144 		     void __iomem *rdp, struct dma_chan **chan, int num_chan,
145 		     bool card_rel_da)
146 {
147 	int ret;
148 	struct scif_hw_dev *sdev;
149 
150 	sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
151 	if (!sdev)
152 		return ERR_PTR(-ENOMEM);
153 
154 	sdev->dev.parent = pdev;
155 	sdev->id.device = id;
156 	sdev->id.vendor = SCIF_DEV_ANY_ID;
157 	sdev->dev.dma_ops = dma_ops;
158 	sdev->dev.release = scif_release_dev;
159 	sdev->hw_ops = hw_ops;
160 	sdev->dnode = dnode;
161 	sdev->snode = snode;
162 	dev_set_drvdata(&sdev->dev, sdev);
163 	sdev->dev.bus = &scif_bus;
164 	sdev->mmio = mmio;
165 	sdev->aper = aper;
166 	sdev->dp = dp;
167 	sdev->rdp = rdp;
168 	sdev->dev.dma_mask = &sdev->dev.coherent_dma_mask;
169 	dma_set_mask(&sdev->dev, DMA_BIT_MASK(64));
170 	sdev->dma_ch = chan;
171 	sdev->num_dma_ch = num_chan;
172 	sdev->card_rel_da = card_rel_da;
173 	dev_set_name(&sdev->dev, "scif-dev%u", sdev->dnode);
174 	/*
175 	 * device_register() causes the bus infrastructure to look for a
176 	 * matching driver.
177 	 */
178 	ret = device_register(&sdev->dev);
179 	if (ret)
180 		goto free_sdev;
181 	return sdev;
182 free_sdev:
183 	put_device(&sdev->dev);
184 	return ERR_PTR(ret);
185 }
186 EXPORT_SYMBOL_GPL(scif_register_device);
187 
scif_unregister_device(struct scif_hw_dev * sdev)188 void scif_unregister_device(struct scif_hw_dev *sdev)
189 {
190 	device_unregister(&sdev->dev);
191 }
192 EXPORT_SYMBOL_GPL(scif_unregister_device);
193 
scif_init(void)194 static int __init scif_init(void)
195 {
196 	return bus_register(&scif_bus);
197 }
198 
scif_exit(void)199 static void __exit scif_exit(void)
200 {
201 	bus_unregister(&scif_bus);
202 }
203 
204 core_initcall(scif_init);
205 module_exit(scif_exit);
206 
207 MODULE_AUTHOR("Intel Corporation");
208 MODULE_DESCRIPTION("Intel(R) SCIF Bus driver");
209 MODULE_LICENSE("GPL v2");
210