1
2
3
4
5
6
7
8
9#include <linux/device.h>
10#include <linux/dma-mapping.h>
11#include <linux/slab.h>
12#include <linux/module.h>
13
14#include <linux/pci-epc.h>
15#include <linux/pci-epf.h>
16#include <linux/pci-ep-cfs.h>
17
18static DEFINE_MUTEX(pci_epf_mutex);
19
20static struct bus_type pci_epf_bus_type;
21static const struct device_type pci_epf_type;
22
23
24
25
26
27
28
29
30
31
32void pci_epf_linkup(struct pci_epf *epf)
33{
34 if (!epf->driver) {
35 dev_WARN(&epf->dev, "epf device not bound to driver\n");
36 return;
37 }
38
39 epf->driver->ops->linkup(epf);
40}
41EXPORT_SYMBOL_GPL(pci_epf_linkup);
42
43
44
45
46
47
48
49
50
51void pci_epf_unbind(struct pci_epf *epf)
52{
53 if (!epf->driver) {
54 dev_WARN(&epf->dev, "epf device not bound to driver\n");
55 return;
56 }
57
58 epf->driver->ops->unbind(epf);
59 module_put(epf->driver->owner);
60}
61EXPORT_SYMBOL_GPL(pci_epf_unbind);
62
63
64
65
66
67
68
69
70int pci_epf_bind(struct pci_epf *epf)
71{
72 if (!epf->driver) {
73 dev_WARN(&epf->dev, "epf device not bound to driver\n");
74 return -EINVAL;
75 }
76
77 if (!try_module_get(epf->driver->owner))
78 return -EAGAIN;
79
80 return epf->driver->ops->bind(epf);
81}
82EXPORT_SYMBOL_GPL(pci_epf_bind);
83
84
85
86
87
88
89
90
91void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar)
92{
93 struct device *dev = epf->epc->dev.parent;
94
95 if (!addr)
96 return;
97
98 dma_free_coherent(dev, epf->bar[bar].size, addr,
99 epf->bar[bar].phys_addr);
100
101 epf->bar[bar].phys_addr = 0;
102 epf->bar[bar].size = 0;
103 epf->bar[bar].barno = 0;
104 epf->bar[bar].flags = 0;
105}
106EXPORT_SYMBOL_GPL(pci_epf_free_space);
107
108
109
110
111
112
113
114
115
116void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
117 size_t align)
118{
119 void *space;
120 struct device *dev = epf->epc->dev.parent;
121 dma_addr_t phys_addr;
122
123 if (size < 128)
124 size = 128;
125
126 if (align)
127 size = ALIGN(size, align);
128 else
129 size = roundup_pow_of_two(size);
130
131 space = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
132 if (!space) {
133 dev_err(dev, "failed to allocate mem space\n");
134 return NULL;
135 }
136
137 epf->bar[bar].phys_addr = phys_addr;
138 epf->bar[bar].size = size;
139 epf->bar[bar].barno = bar;
140 epf->bar[bar].flags |= upper_32_bits(size) ?
141 PCI_BASE_ADDRESS_MEM_TYPE_64 :
142 PCI_BASE_ADDRESS_MEM_TYPE_32;
143
144 return space;
145}
146EXPORT_SYMBOL_GPL(pci_epf_alloc_space);
147
148static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
149{
150 struct config_group *group, *tmp;
151
152 if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
153 return;
154
155 mutex_lock(&pci_epf_mutex);
156 list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
157 pci_ep_cfs_remove_epf_group(group);
158 list_del(&driver->epf_group);
159 mutex_unlock(&pci_epf_mutex);
160}
161
162
163
164
165
166
167
168void pci_epf_unregister_driver(struct pci_epf_driver *driver)
169{
170 pci_epf_remove_cfs(driver);
171 driver_unregister(&driver->driver);
172}
173EXPORT_SYMBOL_GPL(pci_epf_unregister_driver);
174
175static int pci_epf_add_cfs(struct pci_epf_driver *driver)
176{
177 struct config_group *group;
178 const struct pci_epf_device_id *id;
179
180 if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
181 return 0;
182
183 INIT_LIST_HEAD(&driver->epf_group);
184
185 id = driver->id_table;
186 while (id->name[0]) {
187 group = pci_ep_cfs_add_epf_group(id->name);
188 if (IS_ERR(group)) {
189 pci_epf_remove_cfs(driver);
190 return PTR_ERR(group);
191 }
192
193 mutex_lock(&pci_epf_mutex);
194 list_add_tail(&group->group_entry, &driver->epf_group);
195 mutex_unlock(&pci_epf_mutex);
196 id++;
197 }
198
199 return 0;
200}
201
202
203
204
205
206
207
208
209int __pci_epf_register_driver(struct pci_epf_driver *driver,
210 struct module *owner)
211{
212 int ret;
213
214 if (!driver->ops)
215 return -EINVAL;
216
217 if (!driver->ops->bind || !driver->ops->unbind || !driver->ops->linkup)
218 return -EINVAL;
219
220 driver->driver.bus = &pci_epf_bus_type;
221 driver->driver.owner = owner;
222
223 ret = driver_register(&driver->driver);
224 if (ret)
225 return ret;
226
227 pci_epf_add_cfs(driver);
228
229 return 0;
230}
231EXPORT_SYMBOL_GPL(__pci_epf_register_driver);
232
233
234
235
236
237
238
239void pci_epf_destroy(struct pci_epf *epf)
240{
241 device_unregister(&epf->dev);
242}
243EXPORT_SYMBOL_GPL(pci_epf_destroy);
244
245
246
247
248
249
250
251
252
253struct pci_epf *pci_epf_create(const char *name)
254{
255 int ret;
256 struct pci_epf *epf;
257 struct device *dev;
258 int len;
259
260 epf = kzalloc(sizeof(*epf), GFP_KERNEL);
261 if (!epf)
262 return ERR_PTR(-ENOMEM);
263
264 len = strchrnul(name, '.') - name;
265 epf->name = kstrndup(name, len, GFP_KERNEL);
266 if (!epf->name) {
267 kfree(epf);
268 return ERR_PTR(-ENOMEM);
269 }
270
271 dev = &epf->dev;
272 device_initialize(dev);
273 dev->bus = &pci_epf_bus_type;
274 dev->type = &pci_epf_type;
275
276 ret = dev_set_name(dev, "%s", name);
277 if (ret) {
278 put_device(dev);
279 return ERR_PTR(ret);
280 }
281
282 ret = device_add(dev);
283 if (ret) {
284 put_device(dev);
285 return ERR_PTR(ret);
286 }
287
288 return epf;
289}
290EXPORT_SYMBOL_GPL(pci_epf_create);
291
292const struct pci_epf_device_id *
293pci_epf_match_device(const struct pci_epf_device_id *id, struct pci_epf *epf)
294{
295 if (!id || !epf)
296 return NULL;
297
298 while (*id->name) {
299 if (strcmp(epf->name, id->name) == 0)
300 return id;
301 id++;
302 }
303
304 return NULL;
305}
306EXPORT_SYMBOL_GPL(pci_epf_match_device);
307
308static void pci_epf_dev_release(struct device *dev)
309{
310 struct pci_epf *epf = to_pci_epf(dev);
311
312 kfree(epf->name);
313 kfree(epf);
314}
315
316static const struct device_type pci_epf_type = {
317 .release = pci_epf_dev_release,
318};
319
320static int
321pci_epf_match_id(const struct pci_epf_device_id *id, const struct pci_epf *epf)
322{
323 while (id->name[0]) {
324 if (strcmp(epf->name, id->name) == 0)
325 return true;
326 id++;
327 }
328
329 return false;
330}
331
332static int pci_epf_device_match(struct device *dev, struct device_driver *drv)
333{
334 struct pci_epf *epf = to_pci_epf(dev);
335 struct pci_epf_driver *driver = to_pci_epf_driver(drv);
336
337 if (driver->id_table)
338 return pci_epf_match_id(driver->id_table, epf);
339
340 return !strcmp(epf->name, drv->name);
341}
342
343static int pci_epf_device_probe(struct device *dev)
344{
345 struct pci_epf *epf = to_pci_epf(dev);
346 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
347
348 if (!driver->probe)
349 return -ENODEV;
350
351 epf->driver = driver;
352
353 return driver->probe(epf);
354}
355
356static int pci_epf_device_remove(struct device *dev)
357{
358 int ret = 0;
359 struct pci_epf *epf = to_pci_epf(dev);
360 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
361
362 if (driver->remove)
363 ret = driver->remove(epf);
364 epf->driver = NULL;
365
366 return ret;
367}
368
369static struct bus_type pci_epf_bus_type = {
370 .name = "pci-epf",
371 .match = pci_epf_device_match,
372 .probe = pci_epf_device_probe,
373 .remove = pci_epf_device_remove,
374};
375
376static int __init pci_epf_init(void)
377{
378 int ret;
379
380 ret = bus_register(&pci_epf_bus_type);
381 if (ret) {
382 pr_err("failed to register pci epf bus --> %d\n", ret);
383 return ret;
384 }
385
386 return 0;
387}
388module_init(pci_epf_init);
389
390static void __exit pci_epf_exit(void)
391{
392 bus_unregister(&pci_epf_bus_type);
393}
394module_exit(pci_epf_exit);
395
396MODULE_DESCRIPTION("PCI EPF Library");
397MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
398MODULE_LICENSE("GPL v2");
399