1 // SPDX-License-Identifier: GPL-2.0
3 * PCI Endpoint *Function* (EPF) library
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
9 #include <linux/device.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
14 #include <linux/pci-epc.h>
15 #include <linux/pci-epf.h>
16 #include <linux/pci-ep-cfs.h>
18 static DEFINE_MUTEX(pci_epf_mutex);
20 static struct bus_type pci_epf_bus_type;
21 static const struct device_type pci_epf_type;
24 * pci_epf_type_add_cfs() - Help function drivers to expose function specific
25 * attributes in configfs
26 * @epf: the EPF device that has to be configured using configfs
27 * @group: the parent configfs group (corresponding to entries in
30 * Invoke to expose function specific attributes in configfs. If the function
31 * driver does not have anything to expose (attributes configured by user),
34 struct config_group *pci_epf_type_add_cfs(struct pci_epf *epf,
35 struct config_group *group)
37 struct config_group *epf_type_group;
40 dev_err(&epf->dev, "epf device not bound to driver\n");
44 if (!epf->driver->ops->add_cfs)
47 mutex_lock(&epf->lock);
48 epf_type_group = epf->driver->ops->add_cfs(epf, group);
49 mutex_unlock(&epf->lock);
51 return epf_type_group;
53 EXPORT_SYMBOL_GPL(pci_epf_type_add_cfs);
56 * pci_epf_unbind() - Notify the function driver that the binding between the
57 * EPF device and EPC device has been lost
58 * @epf: the EPF device which has lost the binding with the EPC device
60 * Invoke to notify the function driver that the binding between the EPF device
61 * and EPC device has been lost.
63 void pci_epf_unbind(struct pci_epf *epf)
65 struct pci_epf *epf_vf;
68 dev_WARN(&epf->dev, "epf device not bound to driver\n");
72 mutex_lock(&epf->lock);
73 list_for_each_entry(epf_vf, &epf->pci_vepf, list) {
75 epf_vf->driver->ops->unbind(epf_vf);
78 epf->driver->ops->unbind(epf);
79 mutex_unlock(&epf->lock);
80 module_put(epf->driver->owner);
82 EXPORT_SYMBOL_GPL(pci_epf_unbind);
85 * pci_epf_bind() - Notify the function driver that the EPF device has been
86 * bound to a EPC device
87 * @epf: the EPF device which has been bound to the EPC device
89 * Invoke to notify the function driver that it has been bound to a EPC device
91 int pci_epf_bind(struct pci_epf *epf)
93 struct device *dev = &epf->dev;
94 struct pci_epf *epf_vf;
100 dev_WARN(dev, "epf device not bound to driver\n");
104 if (!try_module_get(epf->driver->owner))
107 mutex_lock(&epf->lock);
108 list_for_each_entry(epf_vf, &epf->pci_vepf, list) {
109 vfunc_no = epf_vf->vfunc_no;
112 dev_err(dev, "Invalid virtual function number\n");
118 func_no = epf->func_no;
119 if (!IS_ERR_OR_NULL(epc)) {
121 dev_err(dev, "No support for virt function\n");
126 if (vfunc_no > epc->max_vfs[func_no]) {
127 dev_err(dev, "PF%d: Exceeds max vfunc number\n",
135 func_no = epf->sec_epc_func_no;
136 if (!IS_ERR_OR_NULL(epc)) {
138 dev_err(dev, "No support for virt function\n");
143 if (vfunc_no > epc->max_vfs[func_no]) {
144 dev_err(dev, "PF%d: Exceeds max vfunc number\n",
151 epf_vf->func_no = epf->func_no;
152 epf_vf->sec_epc_func_no = epf->sec_epc_func_no;
153 epf_vf->epc = epf->epc;
154 epf_vf->sec_epc = epf->sec_epc;
155 ret = epf_vf->driver->ops->bind(epf_vf);
158 epf_vf->is_bound = true;
161 ret = epf->driver->ops->bind(epf);
164 epf->is_bound = true;
166 mutex_unlock(&epf->lock);
170 mutex_unlock(&epf->lock);
175 EXPORT_SYMBOL_GPL(pci_epf_bind);
178 * pci_epf_add_vepf() - associate virtual EP function to physical EP function
179 * @epf_pf: the physical EP function to which the virtual EP function should be
181 * @epf_vf: the virtual EP function to be added
183 * A physical endpoint function can be associated with multiple virtual
184 * endpoint functions. Invoke pci_epf_add_epf() to add a virtual PCI endpoint
185 * function to a physical PCI endpoint function.
187 int pci_epf_add_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf)
191 if (IS_ERR_OR_NULL(epf_pf) || IS_ERR_OR_NULL(epf_vf))
194 if (epf_pf->epc || epf_vf->epc || epf_vf->epf_pf)
197 if (epf_pf->sec_epc || epf_vf->sec_epc)
200 mutex_lock(&epf_pf->lock);
201 vfunc_no = find_first_zero_bit(&epf_pf->vfunction_num_map,
203 if (vfunc_no >= BITS_PER_LONG) {
204 mutex_unlock(&epf_pf->lock);
208 set_bit(vfunc_no, &epf_pf->vfunction_num_map);
209 epf_vf->vfunc_no = vfunc_no;
211 epf_vf->epf_pf = epf_pf;
212 epf_vf->is_vf = true;
214 list_add_tail(&epf_vf->list, &epf_pf->pci_vepf);
215 mutex_unlock(&epf_pf->lock);
219 EXPORT_SYMBOL_GPL(pci_epf_add_vepf);
222 * pci_epf_remove_vepf() - remove virtual EP function from physical EP function
223 * @epf_pf: the physical EP function from which the virtual EP function should
225 * @epf_vf: the virtual EP function to be removed
227 * Invoke to remove a virtual endpoint function from the physcial endpoint
230 void pci_epf_remove_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf)
232 if (IS_ERR_OR_NULL(epf_pf) || IS_ERR_OR_NULL(epf_vf))
235 mutex_lock(&epf_pf->lock);
236 clear_bit(epf_vf->vfunc_no, &epf_pf->vfunction_num_map);
237 list_del(&epf_vf->list);
238 mutex_unlock(&epf_pf->lock);
240 EXPORT_SYMBOL_GPL(pci_epf_remove_vepf);
243 * pci_epf_free_space() - free the allocated PCI EPF register space
244 * @epf: the EPF device from whom to free the memory
245 * @addr: the virtual address of the PCI EPF register space
246 * @bar: the BAR number corresponding to the register space
247 * @type: Identifies if the allocated space is for primary EPC or secondary EPC
249 * Invoke to free the allocated PCI EPF register space.
251 void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar,
252 enum pci_epc_interface_type type)
255 struct pci_epf_bar *epf_bar;
261 if (type == PRIMARY_INTERFACE) {
266 epf_bar = epf->sec_epc_bar;
269 dev = epc->dev.parent;
270 dma_free_coherent(dev, epf_bar[bar].size, addr,
271 epf_bar[bar].phys_addr);
273 epf_bar[bar].phys_addr = 0;
274 epf_bar[bar].addr = NULL;
275 epf_bar[bar].size = 0;
276 epf_bar[bar].barno = 0;
277 epf_bar[bar].flags = 0;
279 EXPORT_SYMBOL_GPL(pci_epf_free_space);
282 * pci_epf_alloc_space() - allocate memory for the PCI EPF register space
283 * @epf: the EPF device to whom allocate the memory
284 * @size: the size of the memory that has to be allocated
285 * @bar: the BAR number corresponding to the allocated register space
286 * @align: alignment size for the allocation region
287 * @type: Identifies if the allocation is for primary EPC or secondary EPC
289 * Invoke to allocate memory for the PCI EPF register space.
291 void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
292 size_t align, enum pci_epc_interface_type type)
294 struct pci_epf_bar *epf_bar;
295 dma_addr_t phys_addr;
304 size = ALIGN(size, align);
306 size = roundup_pow_of_two(size);
308 if (type == PRIMARY_INTERFACE) {
313 epf_bar = epf->sec_epc_bar;
316 dev = epc->dev.parent;
317 space = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
319 dev_err(dev, "failed to allocate mem space\n");
323 epf_bar[bar].phys_addr = phys_addr;
324 epf_bar[bar].addr = space;
325 epf_bar[bar].size = size;
326 epf_bar[bar].barno = bar;
327 epf_bar[bar].flags |= upper_32_bits(size) ?
328 PCI_BASE_ADDRESS_MEM_TYPE_64 :
329 PCI_BASE_ADDRESS_MEM_TYPE_32;
333 EXPORT_SYMBOL_GPL(pci_epf_alloc_space);
335 static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
337 struct config_group *group, *tmp;
339 if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
342 mutex_lock(&pci_epf_mutex);
343 list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
344 pci_ep_cfs_remove_epf_group(group);
345 list_del(&driver->epf_group);
346 mutex_unlock(&pci_epf_mutex);
350 * pci_epf_unregister_driver() - unregister the PCI EPF driver
351 * @driver: the PCI EPF driver that has to be unregistered
353 * Invoke to unregister the PCI EPF driver.
355 void pci_epf_unregister_driver(struct pci_epf_driver *driver)
357 pci_epf_remove_cfs(driver);
358 driver_unregister(&driver->driver);
360 EXPORT_SYMBOL_GPL(pci_epf_unregister_driver);
362 static int pci_epf_add_cfs(struct pci_epf_driver *driver)
364 struct config_group *group;
365 const struct pci_epf_device_id *id;
367 if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
370 INIT_LIST_HEAD(&driver->epf_group);
372 id = driver->id_table;
373 while (id->name[0]) {
374 group = pci_ep_cfs_add_epf_group(id->name);
376 pci_epf_remove_cfs(driver);
377 return PTR_ERR(group);
380 mutex_lock(&pci_epf_mutex);
381 list_add_tail(&group->group_entry, &driver->epf_group);
382 mutex_unlock(&pci_epf_mutex);
390 * __pci_epf_register_driver() - register a new PCI EPF driver
391 * @driver: structure representing PCI EPF driver
392 * @owner: the owner of the module that registers the PCI EPF driver
394 * Invoke to register a new PCI EPF driver.
396 int __pci_epf_register_driver(struct pci_epf_driver *driver,
397 struct module *owner)
404 if (!driver->ops->bind || !driver->ops->unbind)
407 driver->driver.bus = &pci_epf_bus_type;
408 driver->driver.owner = owner;
410 ret = driver_register(&driver->driver);
414 pci_epf_add_cfs(driver);
418 EXPORT_SYMBOL_GPL(__pci_epf_register_driver);
421 * pci_epf_destroy() - destroy the created PCI EPF device
422 * @epf: the PCI EPF device that has to be destroyed.
424 * Invoke to destroy the PCI EPF device created by invoking pci_epf_create().
426 void pci_epf_destroy(struct pci_epf *epf)
428 device_unregister(&epf->dev);
430 EXPORT_SYMBOL_GPL(pci_epf_destroy);
433 * pci_epf_create() - create a new PCI EPF device
434 * @name: the name of the PCI EPF device. This name will be used to bind the
435 * the EPF device to a EPF driver
437 * Invoke to create a new PCI EPF device by providing the name of the function
440 struct pci_epf *pci_epf_create(const char *name)
447 epf = kzalloc(sizeof(*epf), GFP_KERNEL);
449 return ERR_PTR(-ENOMEM);
451 len = strchrnul(name, '.') - name;
452 epf->name = kstrndup(name, len, GFP_KERNEL);
455 return ERR_PTR(-ENOMEM);
458 /* VFs are numbered starting with 1. So set BIT(0) by default */
459 epf->vfunction_num_map = 1;
460 INIT_LIST_HEAD(&epf->pci_vepf);
463 device_initialize(dev);
464 dev->bus = &pci_epf_bus_type;
465 dev->type = &pci_epf_type;
466 mutex_init(&epf->lock);
468 ret = dev_set_name(dev, "%s", name);
474 ret = device_add(dev);
482 EXPORT_SYMBOL_GPL(pci_epf_create);
484 static void pci_epf_dev_release(struct device *dev)
486 struct pci_epf *epf = to_pci_epf(dev);
492 static const struct device_type pci_epf_type = {
493 .release = pci_epf_dev_release,
497 pci_epf_match_id(const struct pci_epf_device_id *id, const struct pci_epf *epf)
499 while (id->name[0]) {
500 if (strcmp(epf->name, id->name) == 0)
508 static int pci_epf_device_match(struct device *dev, struct device_driver *drv)
510 struct pci_epf *epf = to_pci_epf(dev);
511 struct pci_epf_driver *driver = to_pci_epf_driver(drv);
513 if (driver->id_table)
514 return pci_epf_match_id(driver->id_table, epf);
516 return !strcmp(epf->name, drv->name);
519 static int pci_epf_device_probe(struct device *dev)
521 struct pci_epf *epf = to_pci_epf(dev);
522 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
527 epf->driver = driver;
529 return driver->probe(epf);
532 static void pci_epf_device_remove(struct device *dev)
534 struct pci_epf *epf = to_pci_epf(dev);
535 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
542 static struct bus_type pci_epf_bus_type = {
544 .match = pci_epf_device_match,
545 .probe = pci_epf_device_probe,
546 .remove = pci_epf_device_remove,
549 static int __init pci_epf_init(void)
553 ret = bus_register(&pci_epf_bus_type);
555 pr_err("failed to register pci epf bus --> %d\n", ret);
561 module_init(pci_epf_init);
563 static void __exit pci_epf_exit(void)
565 bus_unregister(&pci_epf_bus_type);
567 module_exit(pci_epf_exit);
569 MODULE_DESCRIPTION("PCI EPF Library");
570 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
571 MODULE_LICENSE("GPL v2");