signal: Ensure generic siginfos the kernel sends have all bits initialized
[cris-mirror.git] / drivers / pci / endpoint / pci-epf-core.c
blobae1611a62808fac22085847c7be1f32c7c56ebdf
1 /**
2 * PCI Endpoint *Function* (EPF) library
4 * Copyright (C) 2017 Texas Instruments
5 * Author: Kishon Vijay Abraham I <kishon@ti.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 of
9 * the License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/device.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
25 #include <linux/pci-epc.h>
26 #include <linux/pci-epf.h>
27 #include <linux/pci-ep-cfs.h>
29 static struct bus_type pci_epf_bus_type;
30 static const struct device_type pci_epf_type;
32 /**
33 * pci_epf_linkup() - Notify the function driver that EPC device has
34 * established a connection with the Root Complex.
35 * @epf: the EPF device bound to the EPC device which has established
36 * the connection with the host
38 * Invoke to notify the function driver that EPC device has established
39 * a connection with the Root Complex.
41 void pci_epf_linkup(struct pci_epf *epf)
43 if (!epf->driver) {
44 dev_WARN(&epf->dev, "epf device not bound to driver\n");
45 return;
48 epf->driver->ops->linkup(epf);
50 EXPORT_SYMBOL_GPL(pci_epf_linkup);
52 /**
53 * pci_epf_unbind() - Notify the function driver that the binding between the
54 * EPF device and EPC device has been lost
55 * @epf: the EPF device which has lost the binding with the EPC device
57 * Invoke to notify the function driver that the binding between the EPF device
58 * and EPC device has been lost.
60 void pci_epf_unbind(struct pci_epf *epf)
62 if (!epf->driver) {
63 dev_WARN(&epf->dev, "epf device not bound to driver\n");
64 return;
67 epf->driver->ops->unbind(epf);
68 module_put(epf->driver->owner);
70 EXPORT_SYMBOL_GPL(pci_epf_unbind);
72 /**
73 * pci_epf_bind() - Notify the function driver that the EPF device has been
74 * bound to a EPC device
75 * @epf: the EPF device which has been bound to the EPC device
77 * Invoke to notify the function driver that it has been bound to a EPC device
79 int pci_epf_bind(struct pci_epf *epf)
81 if (!epf->driver) {
82 dev_WARN(&epf->dev, "epf device not bound to driver\n");
83 return -EINVAL;
86 if (!try_module_get(epf->driver->owner))
87 return -EAGAIN;
89 return epf->driver->ops->bind(epf);
91 EXPORT_SYMBOL_GPL(pci_epf_bind);
93 /**
94 * pci_epf_free_space() - free the allocated PCI EPF register space
95 * @addr: the virtual address of the PCI EPF register space
96 * @bar: the BAR number corresponding to the register space
98 * Invoke to free the allocated PCI EPF register space.
100 void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar)
102 struct device *dev = &epf->dev;
104 if (!addr)
105 return;
107 dma_free_coherent(dev, epf->bar[bar].size, addr,
108 epf->bar[bar].phys_addr);
110 epf->bar[bar].phys_addr = 0;
111 epf->bar[bar].size = 0;
113 EXPORT_SYMBOL_GPL(pci_epf_free_space);
116 * pci_epf_alloc_space() - allocate memory for the PCI EPF register space
117 * @size: the size of the memory that has to be allocated
118 * @bar: the BAR number corresponding to the allocated register space
120 * Invoke to allocate memory for the PCI EPF register space.
122 void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar)
124 void *space;
125 struct device *dev = &epf->dev;
126 dma_addr_t phys_addr;
128 if (size < 128)
129 size = 128;
130 size = roundup_pow_of_two(size);
132 space = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
133 if (!space) {
134 dev_err(dev, "failed to allocate mem space\n");
135 return NULL;
138 epf->bar[bar].phys_addr = phys_addr;
139 epf->bar[bar].size = size;
141 return space;
143 EXPORT_SYMBOL_GPL(pci_epf_alloc_space);
146 * pci_epf_unregister_driver() - unregister the PCI EPF driver
147 * @driver: the PCI EPF driver that has to be unregistered
149 * Invoke to unregister the PCI EPF driver.
151 void pci_epf_unregister_driver(struct pci_epf_driver *driver)
153 pci_ep_cfs_remove_epf_group(driver->group);
154 driver_unregister(&driver->driver);
156 EXPORT_SYMBOL_GPL(pci_epf_unregister_driver);
159 * __pci_epf_register_driver() - register a new PCI EPF driver
160 * @driver: structure representing PCI EPF driver
161 * @owner: the owner of the module that registers the PCI EPF driver
163 * Invoke to register a new PCI EPF driver.
165 int __pci_epf_register_driver(struct pci_epf_driver *driver,
166 struct module *owner)
168 int ret;
170 if (!driver->ops)
171 return -EINVAL;
173 if (!driver->ops->bind || !driver->ops->unbind || !driver->ops->linkup)
174 return -EINVAL;
176 driver->driver.bus = &pci_epf_bus_type;
177 driver->driver.owner = owner;
179 ret = driver_register(&driver->driver);
180 if (ret)
181 return ret;
183 driver->group = pci_ep_cfs_add_epf_group(driver->driver.name);
185 return 0;
187 EXPORT_SYMBOL_GPL(__pci_epf_register_driver);
190 * pci_epf_destroy() - destroy the created PCI EPF device
191 * @epf: the PCI EPF device that has to be destroyed.
193 * Invoke to destroy the PCI EPF device created by invoking pci_epf_create().
195 void pci_epf_destroy(struct pci_epf *epf)
197 device_unregister(&epf->dev);
199 EXPORT_SYMBOL_GPL(pci_epf_destroy);
202 * pci_epf_create() - create a new PCI EPF device
203 * @name: the name of the PCI EPF device. This name will be used to bind the
204 * the EPF device to a EPF driver
206 * Invoke to create a new PCI EPF device by providing the name of the function
207 * device.
209 struct pci_epf *pci_epf_create(const char *name)
211 int ret;
212 struct pci_epf *epf;
213 struct device *dev;
214 char *func_name;
215 char *buf;
217 epf = kzalloc(sizeof(*epf), GFP_KERNEL);
218 if (!epf) {
219 ret = -ENOMEM;
220 goto err_ret;
223 buf = kstrdup(name, GFP_KERNEL);
224 if (!buf) {
225 ret = -ENOMEM;
226 goto free_epf;
229 func_name = buf;
230 buf = strchrnul(buf, '.');
231 *buf = '\0';
233 epf->name = kstrdup(func_name, GFP_KERNEL);
234 if (!epf->name) {
235 ret = -ENOMEM;
236 goto free_func_name;
239 dev = &epf->dev;
240 device_initialize(dev);
241 dev->bus = &pci_epf_bus_type;
242 dev->type = &pci_epf_type;
244 ret = dev_set_name(dev, "%s", name);
245 if (ret)
246 goto put_dev;
248 ret = device_add(dev);
249 if (ret)
250 goto put_dev;
252 kfree(func_name);
253 return epf;
255 put_dev:
256 put_device(dev);
257 kfree(epf->name);
259 free_func_name:
260 kfree(func_name);
262 free_epf:
263 kfree(epf);
265 err_ret:
266 return ERR_PTR(ret);
268 EXPORT_SYMBOL_GPL(pci_epf_create);
270 const struct pci_epf_device_id *
271 pci_epf_match_device(const struct pci_epf_device_id *id, struct pci_epf *epf)
273 if (!id || !epf)
274 return NULL;
276 while (*id->name) {
277 if (strcmp(epf->name, id->name) == 0)
278 return id;
279 id++;
282 return NULL;
284 EXPORT_SYMBOL_GPL(pci_epf_match_device);
286 static void pci_epf_dev_release(struct device *dev)
288 struct pci_epf *epf = to_pci_epf(dev);
290 kfree(epf->name);
291 kfree(epf);
294 static const struct device_type pci_epf_type = {
295 .release = pci_epf_dev_release,
298 static int
299 pci_epf_match_id(const struct pci_epf_device_id *id, const struct pci_epf *epf)
301 while (id->name[0]) {
302 if (strcmp(epf->name, id->name) == 0)
303 return true;
304 id++;
307 return false;
310 static int pci_epf_device_match(struct device *dev, struct device_driver *drv)
312 struct pci_epf *epf = to_pci_epf(dev);
313 struct pci_epf_driver *driver = to_pci_epf_driver(drv);
315 if (driver->id_table)
316 return pci_epf_match_id(driver->id_table, epf);
318 return !strcmp(epf->name, drv->name);
321 static int pci_epf_device_probe(struct device *dev)
323 struct pci_epf *epf = to_pci_epf(dev);
324 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
326 if (!driver->probe)
327 return -ENODEV;
329 epf->driver = driver;
331 return driver->probe(epf);
334 static int pci_epf_device_remove(struct device *dev)
336 int ret = 0;
337 struct pci_epf *epf = to_pci_epf(dev);
338 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
340 if (driver->remove)
341 ret = driver->remove(epf);
342 epf->driver = NULL;
344 return ret;
347 static struct bus_type pci_epf_bus_type = {
348 .name = "pci-epf",
349 .match = pci_epf_device_match,
350 .probe = pci_epf_device_probe,
351 .remove = pci_epf_device_remove,
354 static int __init pci_epf_init(void)
356 int ret;
358 ret = bus_register(&pci_epf_bus_type);
359 if (ret) {
360 pr_err("failed to register pci epf bus --> %d\n", ret);
361 return ret;
364 return 0;
366 module_init(pci_epf_init);
368 static void __exit pci_epf_exit(void)
370 bus_unregister(&pci_epf_bus_type);
372 module_exit(pci_epf_exit);
374 MODULE_DESCRIPTION("PCI EPF Library");
375 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
376 MODULE_LICENSE("GPL v2");