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 const struct bus_type pci_epf_bus_type;
21 static const struct device_type pci_epf_type;
24 * pci_epf_unbind() - Notify the function driver that the binding between the
25 * EPF device and EPC device has been lost
26 * @epf: the EPF device which has lost the binding with the EPC device
28 * Invoke to notify the function driver that the binding between the EPF device
29 * and EPC device has been lost.
31 void pci_epf_unbind(struct pci_epf *epf)
33 struct pci_epf *epf_vf;
36 dev_WARN(&epf->dev, "epf device not bound to driver\n");
40 mutex_lock(&epf->lock);
41 list_for_each_entry(epf_vf, &epf->pci_vepf, list) {
43 epf_vf->driver->ops->unbind(epf_vf);
46 epf->driver->ops->unbind(epf);
47 mutex_unlock(&epf->lock);
48 module_put(epf->driver->owner);
50 EXPORT_SYMBOL_GPL(pci_epf_unbind);
53 * pci_epf_bind() - Notify the function driver that the EPF device has been
54 * bound to a EPC device
55 * @epf: the EPF device which has been bound to the EPC device
57 * Invoke to notify the function driver that it has been bound to a EPC device
59 int pci_epf_bind(struct pci_epf *epf)
61 struct device *dev = &epf->dev;
62 struct pci_epf *epf_vf;
68 dev_WARN(dev, "epf device not bound to driver\n");
72 if (!try_module_get(epf->driver->owner))
75 mutex_lock(&epf->lock);
76 list_for_each_entry(epf_vf, &epf->pci_vepf, list) {
77 vfunc_no = epf_vf->vfunc_no;
80 dev_err(dev, "Invalid virtual function number\n");
86 func_no = epf->func_no;
87 if (!IS_ERR_OR_NULL(epc)) {
89 dev_err(dev, "No support for virt function\n");
94 if (vfunc_no > epc->max_vfs[func_no]) {
95 dev_err(dev, "PF%d: Exceeds max vfunc number\n",
103 func_no = epf->sec_epc_func_no;
104 if (!IS_ERR_OR_NULL(epc)) {
106 dev_err(dev, "No support for virt function\n");
111 if (vfunc_no > epc->max_vfs[func_no]) {
112 dev_err(dev, "PF%d: Exceeds max vfunc number\n",
119 epf_vf->func_no = epf->func_no;
120 epf_vf->sec_epc_func_no = epf->sec_epc_func_no;
121 epf_vf->epc = epf->epc;
122 epf_vf->sec_epc = epf->sec_epc;
123 ret = epf_vf->driver->ops->bind(epf_vf);
126 epf_vf->is_bound = true;
129 ret = epf->driver->ops->bind(epf);
132 epf->is_bound = true;
134 mutex_unlock(&epf->lock);
138 mutex_unlock(&epf->lock);
143 EXPORT_SYMBOL_GPL(pci_epf_bind);
146 * pci_epf_add_vepf() - associate virtual EP function to physical EP function
147 * @epf_pf: the physical EP function to which the virtual EP function should be
149 * @epf_vf: the virtual EP function to be added
151 * A physical endpoint function can be associated with multiple virtual
152 * endpoint functions. Invoke pci_epf_add_epf() to add a virtual PCI endpoint
153 * function to a physical PCI endpoint function.
155 int pci_epf_add_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf)
159 if (IS_ERR_OR_NULL(epf_pf) || IS_ERR_OR_NULL(epf_vf))
162 if (epf_pf->epc || epf_vf->epc || epf_vf->epf_pf)
165 if (epf_pf->sec_epc || epf_vf->sec_epc)
168 mutex_lock(&epf_pf->lock);
169 vfunc_no = find_first_zero_bit(&epf_pf->vfunction_num_map,
171 if (vfunc_no >= BITS_PER_LONG) {
172 mutex_unlock(&epf_pf->lock);
176 set_bit(vfunc_no, &epf_pf->vfunction_num_map);
177 epf_vf->vfunc_no = vfunc_no;
179 epf_vf->epf_pf = epf_pf;
180 epf_vf->is_vf = true;
182 list_add_tail(&epf_vf->list, &epf_pf->pci_vepf);
183 mutex_unlock(&epf_pf->lock);
187 EXPORT_SYMBOL_GPL(pci_epf_add_vepf);
190 * pci_epf_remove_vepf() - remove virtual EP function from physical EP function
191 * @epf_pf: the physical EP function from which the virtual EP function should
193 * @epf_vf: the virtual EP function to be removed
195 * Invoke to remove a virtual endpoint function from the physical endpoint
198 void pci_epf_remove_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf)
200 if (IS_ERR_OR_NULL(epf_pf) || IS_ERR_OR_NULL(epf_vf))
203 mutex_lock(&epf_pf->lock);
204 clear_bit(epf_vf->vfunc_no, &epf_pf->vfunction_num_map);
205 list_del(&epf_vf->list);
206 mutex_unlock(&epf_pf->lock);
208 EXPORT_SYMBOL_GPL(pci_epf_remove_vepf);
211 * pci_epf_free_space() - free the allocated PCI EPF register space
212 * @epf: the EPF device from whom to free the memory
213 * @addr: the virtual address of the PCI EPF register space
214 * @bar: the BAR number corresponding to the register space
215 * @type: Identifies if the allocated space is for primary EPC or secondary EPC
217 * Invoke to free the allocated PCI EPF register space.
219 void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar,
220 enum pci_epc_interface_type type)
223 struct pci_epf_bar *epf_bar;
229 if (type == PRIMARY_INTERFACE) {
234 epf_bar = epf->sec_epc_bar;
237 dev = epc->dev.parent;
238 dma_free_coherent(dev, epf_bar[bar].size, addr,
239 epf_bar[bar].phys_addr);
241 epf_bar[bar].phys_addr = 0;
242 epf_bar[bar].addr = NULL;
243 epf_bar[bar].size = 0;
244 epf_bar[bar].barno = 0;
245 epf_bar[bar].flags = 0;
247 EXPORT_SYMBOL_GPL(pci_epf_free_space);
250 * pci_epf_alloc_space() - allocate memory for the PCI EPF register space
251 * @epf: the EPF device to whom allocate the memory
252 * @size: the size of the memory that has to be allocated
253 * @bar: the BAR number corresponding to the allocated register space
254 * @epc_features: the features provided by the EPC specific to this EPF
255 * @type: Identifies if the allocation is for primary EPC or secondary EPC
257 * Invoke to allocate memory for the PCI EPF register space.
259 void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
260 const struct pci_epc_features *epc_features,
261 enum pci_epc_interface_type type)
263 u64 bar_fixed_size = epc_features->bar[bar].fixed_size;
264 size_t align = epc_features->align;
265 struct pci_epf_bar *epf_bar;
266 dma_addr_t phys_addr;
274 if (epc_features->bar[bar].type == BAR_FIXED && bar_fixed_size) {
275 if (size > bar_fixed_size) {
277 "requested BAR size is larger than fixed size\n");
280 size = bar_fixed_size;
284 size = ALIGN(size, align);
286 size = roundup_pow_of_two(size);
288 if (type == PRIMARY_INTERFACE) {
293 epf_bar = epf->sec_epc_bar;
296 dev = epc->dev.parent;
297 space = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
299 dev_err(dev, "failed to allocate mem space\n");
303 epf_bar[bar].phys_addr = phys_addr;
304 epf_bar[bar].addr = space;
305 epf_bar[bar].size = size;
306 epf_bar[bar].barno = bar;
307 epf_bar[bar].flags |= upper_32_bits(size) ?
308 PCI_BASE_ADDRESS_MEM_TYPE_64 :
309 PCI_BASE_ADDRESS_MEM_TYPE_32;
313 EXPORT_SYMBOL_GPL(pci_epf_alloc_space);
315 static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
317 struct config_group *group, *tmp;
319 if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
322 mutex_lock(&pci_epf_mutex);
323 list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
324 pci_ep_cfs_remove_epf_group(group);
325 list_del(&driver->epf_group);
326 mutex_unlock(&pci_epf_mutex);
330 * pci_epf_unregister_driver() - unregister the PCI EPF driver
331 * @driver: the PCI EPF driver that has to be unregistered
333 * Invoke to unregister the PCI EPF driver.
335 void pci_epf_unregister_driver(struct pci_epf_driver *driver)
337 pci_epf_remove_cfs(driver);
338 driver_unregister(&driver->driver);
340 EXPORT_SYMBOL_GPL(pci_epf_unregister_driver);
342 static int pci_epf_add_cfs(struct pci_epf_driver *driver)
344 struct config_group *group;
345 const struct pci_epf_device_id *id;
347 if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
350 INIT_LIST_HEAD(&driver->epf_group);
352 id = driver->id_table;
353 while (id->name[0]) {
354 group = pci_ep_cfs_add_epf_group(id->name);
356 pci_epf_remove_cfs(driver);
357 return PTR_ERR(group);
360 mutex_lock(&pci_epf_mutex);
361 list_add_tail(&group->group_entry, &driver->epf_group);
362 mutex_unlock(&pci_epf_mutex);
370 * __pci_epf_register_driver() - register a new PCI EPF driver
371 * @driver: structure representing PCI EPF driver
372 * @owner: the owner of the module that registers the PCI EPF driver
374 * Invoke to register a new PCI EPF driver.
376 int __pci_epf_register_driver(struct pci_epf_driver *driver,
377 struct module *owner)
384 if (!driver->ops->bind || !driver->ops->unbind)
387 driver->driver.bus = &pci_epf_bus_type;
388 driver->driver.owner = owner;
390 ret = driver_register(&driver->driver);
394 pci_epf_add_cfs(driver);
398 EXPORT_SYMBOL_GPL(__pci_epf_register_driver);
401 * pci_epf_destroy() - destroy the created PCI EPF device
402 * @epf: the PCI EPF device that has to be destroyed.
404 * Invoke to destroy the PCI EPF device created by invoking pci_epf_create().
406 void pci_epf_destroy(struct pci_epf *epf)
408 device_unregister(&epf->dev);
410 EXPORT_SYMBOL_GPL(pci_epf_destroy);
413 * pci_epf_create() - create a new PCI EPF device
414 * @name: the name of the PCI EPF device. This name will be used to bind the
415 * EPF device to a EPF driver
417 * Invoke to create a new PCI EPF device by providing the name of the function
420 struct pci_epf *pci_epf_create(const char *name)
427 epf = kzalloc(sizeof(*epf), GFP_KERNEL);
429 return ERR_PTR(-ENOMEM);
431 len = strchrnul(name, '.') - name;
432 epf->name = kstrndup(name, len, GFP_KERNEL);
435 return ERR_PTR(-ENOMEM);
438 /* VFs are numbered starting with 1. So set BIT(0) by default */
439 epf->vfunction_num_map = 1;
440 INIT_LIST_HEAD(&epf->pci_vepf);
443 device_initialize(dev);
444 dev->bus = &pci_epf_bus_type;
445 dev->type = &pci_epf_type;
446 mutex_init(&epf->lock);
448 ret = dev_set_name(dev, "%s", name);
454 ret = device_add(dev);
462 EXPORT_SYMBOL_GPL(pci_epf_create);
464 static void pci_epf_dev_release(struct device *dev)
466 struct pci_epf *epf = to_pci_epf(dev);
472 static const struct device_type pci_epf_type = {
473 .release = pci_epf_dev_release,
476 static const struct pci_epf_device_id *
477 pci_epf_match_id(const struct pci_epf_device_id *id, const struct pci_epf *epf)
479 while (id->name[0]) {
480 if (strcmp(epf->name, id->name) == 0)
488 static int pci_epf_device_match(struct device *dev, struct device_driver *drv)
490 struct pci_epf *epf = to_pci_epf(dev);
491 struct pci_epf_driver *driver = to_pci_epf_driver(drv);
493 if (driver->id_table)
494 return !!pci_epf_match_id(driver->id_table, epf);
496 return !strcmp(epf->name, drv->name);
499 static int pci_epf_device_probe(struct device *dev)
501 struct pci_epf *epf = to_pci_epf(dev);
502 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
507 epf->driver = driver;
509 return driver->probe(epf, pci_epf_match_id(driver->id_table, epf));
512 static void pci_epf_device_remove(struct device *dev)
514 struct pci_epf *epf = to_pci_epf(dev);
515 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
522 static const struct bus_type pci_epf_bus_type = {
524 .match = pci_epf_device_match,
525 .probe = pci_epf_device_probe,
526 .remove = pci_epf_device_remove,
529 static int __init pci_epf_init(void)
533 ret = bus_register(&pci_epf_bus_type);
535 pr_err("failed to register pci epf bus --> %d\n", ret);
541 module_init(pci_epf_init);
543 static void __exit pci_epf_exit(void)
545 bus_unregister(&pci_epf_bus_type);
547 module_exit(pci_epf_exit);
549 MODULE_DESCRIPTION("PCI EPF Library");
550 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");