blob: 523a8cab3bfba16613a03d13916aef3b223ef2cb [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +05302/**
3 * PCI Endpoint *Function* (EPF) library
4 *
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +05307 */
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>
Kishon Vijay Abraham I3a401a22017-03-27 15:15:01 +053016#include <linux/pci-ep-cfs.h>
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +053017
Kishon Vijay Abraham Ief1433f2018-04-02 18:59:35 +053018static DEFINE_MUTEX(pci_epf_mutex);
19
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +053020static struct bus_type pci_epf_bus_type;
Bhumika Goyal36b85182017-08-19 13:52:19 +053021static const struct device_type pci_epf_type;
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +053022
23/**
24 * pci_epf_linkup() - Notify the function driver that EPC device has
25 * established a connection with the Root Complex.
26 * @epf: the EPF device bound to the EPC device which has established
27 * the connection with the host
28 *
29 * Invoke to notify the function driver that EPC device has established
30 * a connection with the Root Complex.
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 * pci_epf_unbind() - Notify the function driver that the binding between the
45 * EPF device and EPC device has been lost
46 * @epf: the EPF device which has lost the binding with the EPC device
47 *
48 * Invoke to notify the function driver that the binding between the EPF device
49 * and EPC device has been lost.
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 * pci_epf_bind() - Notify the function driver that the EPF device has been
65 * bound to a EPC device
66 * @epf: the EPF device which has been bound to the EPC device
67 *
68 * Invoke to notify the function driver that it has been bound to a EPC device
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 * pci_epf_free_space() - free the allocated PCI EPF register space
86 * @addr: the virtual address of the PCI EPF register space
87 * @bar: the BAR number corresponding to the register space
88 *
89 * Invoke to free the allocated PCI EPF register space.
90 */
91void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar)
92{
Kishon Vijay Abraham Ib3301042018-01-11 14:00:57 +053093 struct device *dev = epf->epc->dev.parent;
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +053094
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;
Niklas Casselbc4a4892018-03-28 13:50:07 +0200103 epf->bar[bar].barno = 0;
104 epf->bar[bar].flags = 0;
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530105}
106EXPORT_SYMBOL_GPL(pci_epf_free_space);
107
108/**
109 * pci_epf_alloc_space() - allocate memory for the PCI EPF register space
110 * @size: the size of the memory that has to be allocated
111 * @bar: the BAR number corresponding to the allocated register space
112 *
113 * Invoke to allocate memory for the PCI EPF register space.
114 */
115void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar)
116{
117 void *space;
Kishon Vijay Abraham Ib3301042018-01-11 14:00:57 +0530118 struct device *dev = epf->epc->dev.parent;
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530119 dma_addr_t phys_addr;
120
121 if (size < 128)
122 size = 128;
123 size = roundup_pow_of_two(size);
124
125 space = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
126 if (!space) {
127 dev_err(dev, "failed to allocate mem space\n");
128 return NULL;
129 }
130
131 epf->bar[bar].phys_addr = phys_addr;
132 epf->bar[bar].size = size;
Niklas Casselbc4a4892018-03-28 13:50:07 +0200133 epf->bar[bar].barno = bar;
134 epf->bar[bar].flags = PCI_BASE_ADDRESS_SPACE_MEMORY;
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530135
136 return space;
137}
138EXPORT_SYMBOL_GPL(pci_epf_alloc_space);
139
140/**
141 * pci_epf_unregister_driver() - unregister the PCI EPF driver
142 * @driver: the PCI EPF driver that has to be unregistered
143 *
144 * Invoke to unregister the PCI EPF driver.
145 */
146void pci_epf_unregister_driver(struct pci_epf_driver *driver)
147{
Kishon Vijay Abraham Ief1433f2018-04-02 18:59:35 +0530148 struct config_group *group;
149
150 mutex_lock(&pci_epf_mutex);
151 list_for_each_entry(group, &driver->epf_group, group_entry)
152 pci_ep_cfs_remove_epf_group(group);
153 list_del(&driver->epf_group);
154 mutex_unlock(&pci_epf_mutex);
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530155 driver_unregister(&driver->driver);
156}
157EXPORT_SYMBOL_GPL(pci_epf_unregister_driver);
158
159/**
160 * __pci_epf_register_driver() - register a new PCI EPF driver
161 * @driver: structure representing PCI EPF driver
162 * @owner: the owner of the module that registers the PCI EPF driver
163 *
164 * Invoke to register a new PCI EPF driver.
165 */
166int __pci_epf_register_driver(struct pci_epf_driver *driver,
167 struct module *owner)
168{
169 int ret;
Kishon Vijay Abraham Ief1433f2018-04-02 18:59:35 +0530170 struct config_group *group;
171 const struct pci_epf_device_id *id;
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530172
173 if (!driver->ops)
174 return -EINVAL;
175
176 if (!driver->ops->bind || !driver->ops->unbind || !driver->ops->linkup)
177 return -EINVAL;
178
179 driver->driver.bus = &pci_epf_bus_type;
180 driver->driver.owner = owner;
181
182 ret = driver_register(&driver->driver);
183 if (ret)
184 return ret;
185
Kishon Vijay Abraham Ief1433f2018-04-02 18:59:35 +0530186 INIT_LIST_HEAD(&driver->epf_group);
187
188 id = driver->id_table;
189 while (id->name[0]) {
190 group = pci_ep_cfs_add_epf_group(id->name);
191 mutex_lock(&pci_epf_mutex);
192 list_add_tail(&group->group_entry, &driver->epf_group);
193 mutex_unlock(&pci_epf_mutex);
194 id++;
195 }
Kishon Vijay Abraham I3a401a22017-03-27 15:15:01 +0530196
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530197 return 0;
198}
199EXPORT_SYMBOL_GPL(__pci_epf_register_driver);
200
201/**
202 * pci_epf_destroy() - destroy the created PCI EPF device
203 * @epf: the PCI EPF device that has to be destroyed.
204 *
205 * Invoke to destroy the PCI EPF device created by invoking pci_epf_create().
206 */
207void pci_epf_destroy(struct pci_epf *epf)
208{
209 device_unregister(&epf->dev);
210}
211EXPORT_SYMBOL_GPL(pci_epf_destroy);
212
213/**
214 * pci_epf_create() - create a new PCI EPF device
215 * @name: the name of the PCI EPF device. This name will be used to bind the
216 * the EPF device to a EPF driver
217 *
218 * Invoke to create a new PCI EPF device by providing the name of the function
219 * device.
220 */
221struct pci_epf *pci_epf_create(const char *name)
222{
223 int ret;
224 struct pci_epf *epf;
225 struct device *dev;
Rolf Evers-Fischer36cc14a2018-02-28 18:32:18 +0100226 int len;
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530227
228 epf = kzalloc(sizeof(*epf), GFP_KERNEL);
Rolf Evers-Fischer50ee1062018-02-28 18:32:20 +0100229 if (!epf)
230 return ERR_PTR(-ENOMEM);
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530231
Rolf Evers-Fischer36cc14a2018-02-28 18:32:18 +0100232 len = strchrnul(name, '.') - name;
233 epf->name = kstrndup(name, len, GFP_KERNEL);
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530234 if (!epf->name) {
Rolf Evers-Fischer50ee1062018-02-28 18:32:20 +0100235 kfree(epf);
236 return ERR_PTR(-ENOMEM);
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530237 }
238
239 dev = &epf->dev;
240 device_initialize(dev);
241 dev->bus = &pci_epf_bus_type;
242 dev->type = &pci_epf_type;
243
244 ret = dev_set_name(dev, "%s", name);
Rolf Evers-Fischer50ee1062018-02-28 18:32:20 +0100245 if (ret) {
246 put_device(dev);
247 return ERR_PTR(ret);
248 }
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530249
250 ret = device_add(dev);
Rolf Evers-Fischer50ee1062018-02-28 18:32:20 +0100251 if (ret) {
252 put_device(dev);
253 return ERR_PTR(ret);
254 }
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530255
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530256 return epf;
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530257}
258EXPORT_SYMBOL_GPL(pci_epf_create);
259
Kishon Vijay Abraham If01f9692017-08-18 20:27:54 +0530260const struct pci_epf_device_id *
261pci_epf_match_device(const struct pci_epf_device_id *id, struct pci_epf *epf)
262{
263 if (!id || !epf)
264 return NULL;
265
266 while (*id->name) {
267 if (strcmp(epf->name, id->name) == 0)
268 return id;
269 id++;
270 }
271
272 return NULL;
273}
274EXPORT_SYMBOL_GPL(pci_epf_match_device);
275
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530276static void pci_epf_dev_release(struct device *dev)
277{
278 struct pci_epf *epf = to_pci_epf(dev);
279
280 kfree(epf->name);
281 kfree(epf);
282}
283
Bhumika Goyal36b85182017-08-19 13:52:19 +0530284static const struct device_type pci_epf_type = {
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530285 .release = pci_epf_dev_release,
286};
287
288static int
289pci_epf_match_id(const struct pci_epf_device_id *id, const struct pci_epf *epf)
290{
291 while (id->name[0]) {
292 if (strcmp(epf->name, id->name) == 0)
293 return true;
294 id++;
295 }
296
297 return false;
298}
299
300static int pci_epf_device_match(struct device *dev, struct device_driver *drv)
301{
302 struct pci_epf *epf = to_pci_epf(dev);
303 struct pci_epf_driver *driver = to_pci_epf_driver(drv);
304
305 if (driver->id_table)
306 return pci_epf_match_id(driver->id_table, epf);
307
308 return !strcmp(epf->name, drv->name);
309}
310
311static int pci_epf_device_probe(struct device *dev)
312{
313 struct pci_epf *epf = to_pci_epf(dev);
314 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
315
316 if (!driver->probe)
317 return -ENODEV;
318
319 epf->driver = driver;
320
321 return driver->probe(epf);
322}
323
324static int pci_epf_device_remove(struct device *dev)
325{
Kishon Vijay Abraham I28daeff2017-08-18 20:27:55 +0530326 int ret = 0;
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530327 struct pci_epf *epf = to_pci_epf(dev);
328 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
329
Kishon Vijay Abraham I28daeff2017-08-18 20:27:55 +0530330 if (driver->remove)
331 ret = driver->remove(epf);
Kishon Vijay Abraham I5e8cb402017-04-10 19:25:10 +0530332 epf->driver = NULL;
333
334 return ret;
335}
336
337static struct bus_type pci_epf_bus_type = {
338 .name = "pci-epf",
339 .match = pci_epf_device_match,
340 .probe = pci_epf_device_probe,
341 .remove = pci_epf_device_remove,
342};
343
344static int __init pci_epf_init(void)
345{
346 int ret;
347
348 ret = bus_register(&pci_epf_bus_type);
349 if (ret) {
350 pr_err("failed to register pci epf bus --> %d\n", ret);
351 return ret;
352 }
353
354 return 0;
355}
356module_init(pci_epf_init);
357
358static void __exit pci_epf_exit(void)
359{
360 bus_unregister(&pci_epf_bus_type);
361}
362module_exit(pci_epf_exit);
363
364MODULE_DESCRIPTION("PCI EPF Library");
365MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
366MODULE_LICENSE("GPL v2");