Lines Matching +full:pci +full:- +full:ep
1 // SPDX-License-Identifier: GPL-2.0
3 * PCI Endpoint *Function* (EPF) library
10 #include <linux/dma-mapping.h>
14 #include <linux/pci-epc.h>
15 #include <linux/pci-epf.h>
16 #include <linux/pci-ep-cfs.h>
24 * pci_epf_unbind() - Notify the function driver that the binding between the
35 if (!epf->driver) { in pci_epf_unbind()
36 dev_WARN(&epf->dev, "epf device not bound to driver\n"); in pci_epf_unbind()
40 mutex_lock(&epf->lock); in pci_epf_unbind()
41 list_for_each_entry(epf_vf, &epf->pci_vepf, list) { in pci_epf_unbind()
42 if (epf_vf->is_bound) in pci_epf_unbind()
43 epf_vf->driver->ops->unbind(epf_vf); in pci_epf_unbind()
45 if (epf->is_bound) in pci_epf_unbind()
46 epf->driver->ops->unbind(epf); in pci_epf_unbind()
47 mutex_unlock(&epf->lock); in pci_epf_unbind()
48 module_put(epf->driver->owner); in pci_epf_unbind()
53 * pci_epf_bind() - Notify the function driver that the EPF device has been
61 struct device *dev = &epf->dev; in pci_epf_bind()
67 if (!epf->driver) { in pci_epf_bind()
69 return -EINVAL; in pci_epf_bind()
72 if (!try_module_get(epf->driver->owner)) in pci_epf_bind()
73 return -EAGAIN; in pci_epf_bind()
75 mutex_lock(&epf->lock); in pci_epf_bind()
76 list_for_each_entry(epf_vf, &epf->pci_vepf, list) { in pci_epf_bind()
77 vfunc_no = epf_vf->vfunc_no; in pci_epf_bind()
81 ret = -EINVAL; in pci_epf_bind()
85 epc = epf->epc; in pci_epf_bind()
86 func_no = epf->func_no; in pci_epf_bind()
88 if (!epc->max_vfs) { in pci_epf_bind()
90 ret = -EINVAL; in pci_epf_bind()
94 if (vfunc_no > epc->max_vfs[func_no]) { in pci_epf_bind()
97 ret = -EINVAL; in pci_epf_bind()
102 epc = epf->sec_epc; in pci_epf_bind()
103 func_no = epf->sec_epc_func_no; in pci_epf_bind()
105 if (!epc->max_vfs) { in pci_epf_bind()
107 ret = -EINVAL; in pci_epf_bind()
111 if (vfunc_no > epc->max_vfs[func_no]) { in pci_epf_bind()
114 ret = -EINVAL; in pci_epf_bind()
119 epf_vf->func_no = epf->func_no; in pci_epf_bind()
120 epf_vf->sec_epc_func_no = epf->sec_epc_func_no; in pci_epf_bind()
121 epf_vf->epc = epf->epc; in pci_epf_bind()
122 epf_vf->sec_epc = epf->sec_epc; in pci_epf_bind()
123 ret = epf_vf->driver->ops->bind(epf_vf); in pci_epf_bind()
126 epf_vf->is_bound = true; in pci_epf_bind()
129 ret = epf->driver->ops->bind(epf); in pci_epf_bind()
132 epf->is_bound = true; in pci_epf_bind()
134 mutex_unlock(&epf->lock); in pci_epf_bind()
138 mutex_unlock(&epf->lock); in 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
152 * endpoint functions. Invoke pci_epf_add_epf() to add a virtual PCI endpoint
153 * function to a physical PCI endpoint function.
160 return -EINVAL; in pci_epf_add_vepf()
162 if (epf_pf->epc || epf_vf->epc || epf_vf->epf_pf) in pci_epf_add_vepf()
163 return -EBUSY; in pci_epf_add_vepf()
165 if (epf_pf->sec_epc || epf_vf->sec_epc) in pci_epf_add_vepf()
166 return -EBUSY; in pci_epf_add_vepf()
168 mutex_lock(&epf_pf->lock); in pci_epf_add_vepf()
169 vfunc_no = find_first_zero_bit(&epf_pf->vfunction_num_map, in pci_epf_add_vepf()
172 mutex_unlock(&epf_pf->lock); in pci_epf_add_vepf()
173 return -EINVAL; in pci_epf_add_vepf()
176 set_bit(vfunc_no, &epf_pf->vfunction_num_map); in pci_epf_add_vepf()
177 epf_vf->vfunc_no = vfunc_no; in pci_epf_add_vepf()
179 epf_vf->epf_pf = epf_pf; in pci_epf_add_vepf()
180 epf_vf->is_vf = true; in pci_epf_add_vepf()
182 list_add_tail(&epf_vf->list, &epf_pf->pci_vepf); in pci_epf_add_vepf()
183 mutex_unlock(&epf_pf->lock); in 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
203 mutex_lock(&epf_pf->lock); in pci_epf_remove_vepf()
204 clear_bit(epf_vf->vfunc_no, &epf_pf->vfunction_num_map); in pci_epf_remove_vepf()
205 epf_vf->epf_pf = NULL; in pci_epf_remove_vepf()
206 list_del(&epf_vf->list); in pci_epf_remove_vepf()
207 mutex_unlock(&epf_pf->lock); in pci_epf_remove_vepf()
212 * pci_epf_free_space() - free the allocated PCI EPF register space
214 * @addr: the virtual address of the PCI EPF register space
218 * Invoke to free the allocated PCI EPF register space.
231 epc = epf->epc; in pci_epf_free_space()
232 epf_bar = epf->bar; in pci_epf_free_space()
234 epc = epf->sec_epc; in pci_epf_free_space()
235 epf_bar = epf->sec_epc_bar; in pci_epf_free_space()
238 dev = epc->dev.parent; in pci_epf_free_space()
251 * pci_epf_alloc_space() - allocate memory for the PCI EPF register space
258 * Invoke to allocate memory for the PCI EPF register space.
260 * can only be a 64-bit BAR, or if the requested size is larger than 2 GB.
266 u64 bar_fixed_size = epc_features->bar[bar].fixed_size; in pci_epf_alloc_space()
267 size_t align = epc_features->align; in pci_epf_alloc_space()
277 if (epc_features->bar[bar].type == BAR_FIXED && bar_fixed_size) { in pci_epf_alloc_space()
279 dev_err(&epf->dev, in pci_epf_alloc_space()
292 epc = epf->epc; in pci_epf_alloc_space()
293 epf_bar = epf->bar; in pci_epf_alloc_space()
295 epc = epf->sec_epc; in pci_epf_alloc_space()
296 epf_bar = epf->sec_epc_bar; in pci_epf_alloc_space()
299 dev = epc->dev.parent; in pci_epf_alloc_space()
310 if (upper_32_bits(size) || epc_features->bar[bar].only_64bit) in pci_epf_alloc_space()
327 list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry) in pci_epf_remove_cfs()
329 list_del(&driver->epf_group); in pci_epf_remove_cfs()
334 * pci_epf_unregister_driver() - unregister the PCI EPF driver
335 * @driver: the PCI EPF driver that has to be unregistered
337 * Invoke to unregister the PCI EPF driver.
342 driver_unregister(&driver->driver); in pci_epf_unregister_driver()
354 INIT_LIST_HEAD(&driver->epf_group); in pci_epf_add_cfs()
356 id = driver->id_table; in pci_epf_add_cfs()
357 while (id->name[0]) { in pci_epf_add_cfs()
358 group = pci_ep_cfs_add_epf_group(id->name); in pci_epf_add_cfs()
365 list_add_tail(&group->group_entry, &driver->epf_group); in pci_epf_add_cfs()
374 * __pci_epf_register_driver() - register a new PCI EPF driver
375 * @driver: structure representing PCI EPF driver
376 * @owner: the owner of the module that registers the PCI EPF driver
378 * Invoke to register a new PCI EPF driver.
385 if (!driver->ops) in __pci_epf_register_driver()
386 return -EINVAL; in __pci_epf_register_driver()
388 if (!driver->ops->bind || !driver->ops->unbind) in __pci_epf_register_driver()
389 return -EINVAL; in __pci_epf_register_driver()
391 driver->driver.bus = &pci_epf_bus_type; in __pci_epf_register_driver()
392 driver->driver.owner = owner; in __pci_epf_register_driver()
394 ret = driver_register(&driver->driver); in __pci_epf_register_driver()
405 * pci_epf_destroy() - destroy the created PCI EPF device
406 * @epf: the PCI EPF device that has to be destroyed.
408 * Invoke to destroy the PCI EPF device created by invoking pci_epf_create().
412 device_unregister(&epf->dev); in pci_epf_destroy()
417 * pci_epf_create() - create a new PCI EPF device
418 * @name: the name of the PCI EPF device. This name will be used to bind the
421 * Invoke to create a new PCI EPF device by providing the name of the function
433 return ERR_PTR(-ENOMEM); in pci_epf_create()
435 len = strchrnul(name, '.') - name; in pci_epf_create()
436 epf->name = kstrndup(name, len, GFP_KERNEL); in pci_epf_create()
437 if (!epf->name) { in pci_epf_create()
439 return ERR_PTR(-ENOMEM); in pci_epf_create()
443 epf->vfunction_num_map = 1; in pci_epf_create()
444 INIT_LIST_HEAD(&epf->pci_vepf); in pci_epf_create()
446 dev = &epf->dev; in pci_epf_create()
448 dev->bus = &pci_epf_bus_type; in pci_epf_create()
449 dev->type = &pci_epf_type; in pci_epf_create()
450 mutex_init(&epf->lock); in pci_epf_create()
472 kfree(epf->name); in pci_epf_dev_release()
483 while (id->name[0]) { in pci_epf_match_id()
484 if (strcmp(epf->name, id->name) == 0) in pci_epf_match_id()
497 if (driver->id_table) in pci_epf_device_match()
498 return !!pci_epf_match_id(driver->id_table, epf); in pci_epf_device_match()
500 return !strcmp(epf->name, drv->name); in pci_epf_device_match()
506 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver); in pci_epf_device_probe()
508 if (!driver->probe) in pci_epf_device_probe()
509 return -ENODEV; in pci_epf_device_probe()
511 epf->driver = driver; in pci_epf_device_probe()
513 return driver->probe(epf, pci_epf_match_id(driver->id_table, epf)); in pci_epf_device_probe()
519 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver); in pci_epf_device_remove()
521 if (driver->remove) in pci_epf_device_remove()
522 driver->remove(epf); in pci_epf_device_remove()
523 epf->driver = NULL; in pci_epf_device_remove()
527 .name = "pci-epf",
539 pr_err("failed to register pci epf bus --> %d\n", ret); in pci_epf_init()
553 MODULE_DESCRIPTION("PCI EPF Library");