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) {
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) {
42 if (epf_vf->is_bound)
43 epf_vf->driver->ops->unbind(epf_vf);
45 if (epf->is_bound)
46 epf->driver->ops->unbind(epf);
47 mutex_unlock(&epf->lock);
48 module_put(epf->driver->owner);
53 * pci_epf_bind() - Notify the function driver that the EPF device has been
61 struct device *dev = &epf->dev;
67 if (!epf->driver) {
69 return -EINVAL;
72 if (!try_module_get(epf->driver->owner))
73 return -EAGAIN;
75 mutex_lock(&epf->lock);
76 list_for_each_entry(epf_vf, &epf->pci_vepf, list) {
77 vfunc_no = epf_vf->vfunc_no;
81 ret = -EINVAL;
85 epc = epf->epc;
86 func_no = epf->func_no;
88 if (!epc->max_vfs) {
90 ret = -EINVAL;
94 if (vfunc_no > epc->max_vfs[func_no]) {
97 ret = -EINVAL;
102 epc = epf->sec_epc;
103 func_no = epf->sec_epc_func_no;
105 if (!epc->max_vfs) {
107 ret = -EINVAL;
111 if (vfunc_no > epc->max_vfs[func_no]) {
114 ret = -EINVAL;
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);
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;
162 if (epf_pf->epc || epf_vf->epc || epf_vf->epf_pf)
163 return -EBUSY;
165 if (epf_pf->sec_epc || epf_vf->sec_epc)
166 return -EBUSY;
168 mutex_lock(&epf_pf->lock);
169 vfunc_no = find_first_zero_bit(&epf_pf->vfunction_num_map,
172 mutex_unlock(&epf_pf->lock);
173 return -EINVAL;
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);
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);
204 clear_bit(epf_vf->vfunc_no, &epf_pf->vfunction_num_map);
205 epf_vf->epf_pf = NULL;
206 list_del(&epf_vf->list);
207 mutex_unlock(&epf_pf->lock);
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;
232 epf_bar = epf->bar;
234 epc = epf->sec_epc;
235 epf_bar = epf->sec_epc_bar;
238 dev = epc->dev.parent;
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;
267 size_t align = epc_features->align;
277 if (epc_features->bar[bar].type == BAR_FIXED && bar_fixed_size) {
279 dev_err(&epf->dev,
292 epc = epf->epc;
293 epf_bar = epf->bar;
295 epc = epf->sec_epc;
296 epf_bar = epf->sec_epc_bar;
299 dev = epc->dev.parent;
310 if (upper_32_bits(size) || epc_features->bar[bar].only_64bit)
327 list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
329 list_del(&driver->epf_group);
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);
354 INIT_LIST_HEAD(&driver->epf_group);
356 id = driver->id_table;
357 while (id->name[0]) {
358 group = pci_ep_cfs_add_epf_group(id->name);
365 list_add_tail(&group->group_entry, &driver->epf_group);
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)
386 return -EINVAL;
388 if (!driver->ops->bind || !driver->ops->unbind)
389 return -EINVAL;
391 driver->driver.bus = &pci_epf_bus_type;
392 driver->driver.owner = owner;
394 ret = driver_register(&driver->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);
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);
435 len = strchrnul(name, '.') - name;
436 epf->name = kstrndup(name, len, GFP_KERNEL);
437 if (!epf->name) {
439 return ERR_PTR(-ENOMEM);
443 epf->vfunction_num_map = 1;
444 INIT_LIST_HEAD(&epf->pci_vepf);
446 dev = &epf->dev;
448 dev->bus = &pci_epf_bus_type;
449 dev->type = &pci_epf_type;
450 mutex_init(&epf->lock);
472 kfree(epf->name);
483 while (id->name[0]) {
484 if (strcmp(epf->name, id->name) == 0)
497 if (driver->id_table)
498 return !!pci_epf_match_id(driver->id_table, epf);
500 return !strcmp(epf->name, drv->name);
506 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
508 if (!driver->probe)
509 return -ENODEV;
511 epf->driver = driver;
513 return driver->probe(epf, pci_epf_match_id(driver->id_table, epf));
519 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
521 if (driver->remove)
522 driver->remove(epf);
523 epf->driver = NULL;
527 .name = "pci-epf",
539 pr_err("failed to register pci epf bus --> %d\n", ret);
553 MODULE_DESCRIPTION("PCI EPF Library");