Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * PCI Endpoint *Function* (EPF) library
  4 *
  5 * Copyright (C) 2017 Texas Instruments
  6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
  7 */
  8
  9#include <linux/device.h>
 10#include <linux/dma-mapping.h>
 11#include <linux/slab.h>
 12#include <linux/module.h>
 13
 14#include <linux/pci-epc.h>
 15#include <linux/pci-epf.h>
 16#include <linux/pci-ep-cfs.h>
 17
 18static DEFINE_MUTEX(pci_epf_mutex);
 19
 20static struct bus_type pci_epf_bus_type;
 21static const struct device_type pci_epf_type;
 22
 23/**
 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
 27 *
 28 * Invoke to notify the function driver that the binding between the EPF device
 29 * and EPC device has been lost.
 30 */
 31void pci_epf_unbind(struct pci_epf *epf)
 32{
 33	struct pci_epf *epf_vf;
 34
 35	if (!epf->driver) {
 36		dev_WARN(&epf->dev, "epf device not bound to driver\n");
 37		return;
 38	}
 39
 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);
 44	}
 45	if (epf->is_bound)
 46		epf->driver->ops->unbind(epf);
 47	mutex_unlock(&epf->lock);
 48	module_put(epf->driver->owner);
 49}
 50EXPORT_SYMBOL_GPL(pci_epf_unbind);
 51
 52/**
 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
 56 *
 57 * Invoke to notify the function driver that it has been bound to a EPC device
 58 */
 59int pci_epf_bind(struct pci_epf *epf)
 60{
 61	struct device *dev = &epf->dev;
 62	struct pci_epf *epf_vf;
 63	u8 func_no, vfunc_no;
 64	struct pci_epc *epc;
 65	int ret;
 66
 67	if (!epf->driver) {
 68		dev_WARN(dev, "epf device not bound to driver\n");
 69		return -EINVAL;
 70	}
 71
 72	if (!try_module_get(epf->driver->owner))
 73		return -EAGAIN;
 74
 75	mutex_lock(&epf->lock);
 76	list_for_each_entry(epf_vf, &epf->pci_vepf, list) {
 77		vfunc_no = epf_vf->vfunc_no;
 78
 79		if (vfunc_no < 1) {
 80			dev_err(dev, "Invalid virtual function number\n");
 81			ret = -EINVAL;
 82			goto ret;
 83		}
 84
 85		epc = epf->epc;
 86		func_no = epf->func_no;
 87		if (!IS_ERR_OR_NULL(epc)) {
 88			if (!epc->max_vfs) {
 89				dev_err(dev, "No support for virt function\n");
 90				ret = -EINVAL;
 91				goto ret;
 92			}
 93
 94			if (vfunc_no > epc->max_vfs[func_no]) {
 95				dev_err(dev, "PF%d: Exceeds max vfunc number\n",
 96					func_no);
 97				ret = -EINVAL;
 98				goto ret;
 99			}
100		}
101
102		epc = epf->sec_epc;
103		func_no = epf->sec_epc_func_no;
104		if (!IS_ERR_OR_NULL(epc)) {
105			if (!epc->max_vfs) {
106				dev_err(dev, "No support for virt function\n");
107				ret = -EINVAL;
108				goto ret;
109			}
110
111			if (vfunc_no > epc->max_vfs[func_no]) {
112				dev_err(dev, "PF%d: Exceeds max vfunc number\n",
113					func_no);
114				ret = -EINVAL;
115				goto ret;
116			}
117		}
118
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);
124		if (ret)
125			goto ret;
126		epf_vf->is_bound = true;
127	}
128
129	ret = epf->driver->ops->bind(epf);
130	if (ret)
131		goto ret;
132	epf->is_bound = true;
133
134	mutex_unlock(&epf->lock);
135	return 0;
136
137ret:
138	mutex_unlock(&epf->lock);
139	pci_epf_unbind(epf);
140
141	return ret;
142}
143EXPORT_SYMBOL_GPL(pci_epf_bind);
144
145/**
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
148 *   associated
149 * @epf_vf: the virtual EP function to be added
150 *
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.
154 */
155int pci_epf_add_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf)
156{
157	u32 vfunc_no;
158
159	if (IS_ERR_OR_NULL(epf_pf) || IS_ERR_OR_NULL(epf_vf))
160		return -EINVAL;
161
162	if (epf_pf->epc || epf_vf->epc || epf_vf->epf_pf)
163		return -EBUSY;
164
165	if (epf_pf->sec_epc || epf_vf->sec_epc)
166		return -EBUSY;
167
168	mutex_lock(&epf_pf->lock);
169	vfunc_no = find_first_zero_bit(&epf_pf->vfunction_num_map,
170				       BITS_PER_LONG);
171	if (vfunc_no >= BITS_PER_LONG) {
172		mutex_unlock(&epf_pf->lock);
173		return -EINVAL;
174	}
175
176	set_bit(vfunc_no, &epf_pf->vfunction_num_map);
177	epf_vf->vfunc_no = vfunc_no;
178
179	epf_vf->epf_pf = epf_pf;
180	epf_vf->is_vf = true;
181
182	list_add_tail(&epf_vf->list, &epf_pf->pci_vepf);
183	mutex_unlock(&epf_pf->lock);
184
185	return 0;
186}
187EXPORT_SYMBOL_GPL(pci_epf_add_vepf);
188
189/**
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
192 *   be removed
193 * @epf_vf: the virtual EP function to be removed
194 *
195 * Invoke to remove a virtual endpoint function from the physical endpoint
196 * function.
197 */
198void pci_epf_remove_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf)
199{
200	if (IS_ERR_OR_NULL(epf_pf) || IS_ERR_OR_NULL(epf_vf))
201		return;
202
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);
207}
208EXPORT_SYMBOL_GPL(pci_epf_remove_vepf);
209
210/**
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
216 *
217 * Invoke to free the allocated PCI EPF register space.
218 */
219void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar,
220			enum pci_epc_interface_type type)
221{
222	struct device *dev;
223	struct pci_epf_bar *epf_bar;
224	struct pci_epc *epc;
225
226	if (!addr)
227		return;
228
229	if (type == PRIMARY_INTERFACE) {
230		epc = epf->epc;
231		epf_bar = epf->bar;
232	} else {
233		epc = epf->sec_epc;
234		epf_bar = epf->sec_epc_bar;
235	}
236
237	dev = epc->dev.parent;
238	dma_free_coherent(dev, epf_bar[bar].size, addr,
239			  epf_bar[bar].phys_addr);
240
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;
246}
247EXPORT_SYMBOL_GPL(pci_epf_free_space);
248
249/**
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 * @align: alignment size for the allocation region
255 * @type: Identifies if the allocation is for primary EPC or secondary EPC
256 *
257 * Invoke to allocate memory for the PCI EPF register space.
258 */
259void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
260			  size_t align, enum pci_epc_interface_type type)
261{
262	struct pci_epf_bar *epf_bar;
263	dma_addr_t phys_addr;
264	struct pci_epc *epc;
265	struct device *dev;
266	void *space;
 
 
267
268	if (size < 128)
269		size = 128;
270
271	if (align)
272		size = ALIGN(size, align);
273	else
274		size = roundup_pow_of_two(size);
275
276	if (type == PRIMARY_INTERFACE) {
277		epc = epf->epc;
278		epf_bar = epf->bar;
279	} else {
280		epc = epf->sec_epc;
281		epf_bar = epf->sec_epc_bar;
282	}
283
284	dev = epc->dev.parent;
285	space = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
286	if (!space) {
287		dev_err(dev, "failed to allocate mem space\n");
288		return NULL;
289	}
290
291	epf_bar[bar].phys_addr = phys_addr;
292	epf_bar[bar].addr = space;
293	epf_bar[bar].size = size;
294	epf_bar[bar].barno = bar;
295	epf_bar[bar].flags |= upper_32_bits(size) ?
296				PCI_BASE_ADDRESS_MEM_TYPE_64 :
297				PCI_BASE_ADDRESS_MEM_TYPE_32;
298
299	return space;
300}
301EXPORT_SYMBOL_GPL(pci_epf_alloc_space);
302
303static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
304{
305	struct config_group *group, *tmp;
306
307	if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
308		return;
309
310	mutex_lock(&pci_epf_mutex);
311	list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
312		pci_ep_cfs_remove_epf_group(group);
313	list_del(&driver->epf_group);
314	mutex_unlock(&pci_epf_mutex);
315}
316
317/**
318 * pci_epf_unregister_driver() - unregister the PCI EPF driver
319 * @driver: the PCI EPF driver that has to be unregistered
320 *
321 * Invoke to unregister the PCI EPF driver.
322 */
323void pci_epf_unregister_driver(struct pci_epf_driver *driver)
324{
325	pci_epf_remove_cfs(driver);
326	driver_unregister(&driver->driver);
327}
328EXPORT_SYMBOL_GPL(pci_epf_unregister_driver);
329
330static int pci_epf_add_cfs(struct pci_epf_driver *driver)
331{
332	struct config_group *group;
333	const struct pci_epf_device_id *id;
334
335	if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
336		return 0;
337
338	INIT_LIST_HEAD(&driver->epf_group);
339
340	id = driver->id_table;
341	while (id->name[0]) {
342		group = pci_ep_cfs_add_epf_group(id->name);
343		if (IS_ERR(group)) {
344			pci_epf_remove_cfs(driver);
345			return PTR_ERR(group);
346		}
347
348		mutex_lock(&pci_epf_mutex);
349		list_add_tail(&group->group_entry, &driver->epf_group);
350		mutex_unlock(&pci_epf_mutex);
351		id++;
352	}
353
354	return 0;
355}
356
357/**
358 * __pci_epf_register_driver() - register a new PCI EPF driver
359 * @driver: structure representing PCI EPF driver
360 * @owner: the owner of the module that registers the PCI EPF driver
361 *
362 * Invoke to register a new PCI EPF driver.
363 */
364int __pci_epf_register_driver(struct pci_epf_driver *driver,
365			      struct module *owner)
366{
367	int ret;
368
369	if (!driver->ops)
370		return -EINVAL;
371
372	if (!driver->ops->bind || !driver->ops->unbind)
373		return -EINVAL;
374
375	driver->driver.bus = &pci_epf_bus_type;
376	driver->driver.owner = owner;
377
378	ret = driver_register(&driver->driver);
379	if (ret)
380		return ret;
381
382	pci_epf_add_cfs(driver);
383
384	return 0;
385}
386EXPORT_SYMBOL_GPL(__pci_epf_register_driver);
387
388/**
389 * pci_epf_destroy() - destroy the created PCI EPF device
390 * @epf: the PCI EPF device that has to be destroyed.
391 *
392 * Invoke to destroy the PCI EPF device created by invoking pci_epf_create().
393 */
394void pci_epf_destroy(struct pci_epf *epf)
395{
396	device_unregister(&epf->dev);
397}
398EXPORT_SYMBOL_GPL(pci_epf_destroy);
399
400/**
401 * pci_epf_create() - create a new PCI EPF device
402 * @name: the name of the PCI EPF device. This name will be used to bind the
403 *	  EPF device to a EPF driver
404 *
405 * Invoke to create a new PCI EPF device by providing the name of the function
406 * device.
407 */
408struct pci_epf *pci_epf_create(const char *name)
409{
410	int ret;
411	struct pci_epf *epf;
412	struct device *dev;
413	int len;
414
415	epf = kzalloc(sizeof(*epf), GFP_KERNEL);
416	if (!epf)
417		return ERR_PTR(-ENOMEM);
418
419	len = strchrnul(name, '.') - name;
420	epf->name = kstrndup(name, len, GFP_KERNEL);
421	if (!epf->name) {
422		kfree(epf);
423		return ERR_PTR(-ENOMEM);
424	}
425
426	/* VFs are numbered starting with 1. So set BIT(0) by default */
427	epf->vfunction_num_map = 1;
428	INIT_LIST_HEAD(&epf->pci_vepf);
429
430	dev = &epf->dev;
431	device_initialize(dev);
432	dev->bus = &pci_epf_bus_type;
433	dev->type = &pci_epf_type;
434	mutex_init(&epf->lock);
435
436	ret = dev_set_name(dev, "%s", name);
437	if (ret) {
438		put_device(dev);
439		return ERR_PTR(ret);
440	}
441
442	ret = device_add(dev);
443	if (ret) {
444		put_device(dev);
445		return ERR_PTR(ret);
446	}
447
448	return epf;
449}
450EXPORT_SYMBOL_GPL(pci_epf_create);
451
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
452static void pci_epf_dev_release(struct device *dev)
453{
454	struct pci_epf *epf = to_pci_epf(dev);
455
456	kfree(epf->name);
457	kfree(epf);
458}
459
460static const struct device_type pci_epf_type = {
461	.release	= pci_epf_dev_release,
462};
463
464static const struct pci_epf_device_id *
465pci_epf_match_id(const struct pci_epf_device_id *id, const struct pci_epf *epf)
466{
467	while (id->name[0]) {
468		if (strcmp(epf->name, id->name) == 0)
469			return id;
470		id++;
471	}
472
473	return NULL;
474}
475
476static int pci_epf_device_match(struct device *dev, struct device_driver *drv)
477{
478	struct pci_epf *epf = to_pci_epf(dev);
479	struct pci_epf_driver *driver = to_pci_epf_driver(drv);
480
481	if (driver->id_table)
482		return !!pci_epf_match_id(driver->id_table, epf);
483
484	return !strcmp(epf->name, drv->name);
485}
486
487static int pci_epf_device_probe(struct device *dev)
488{
489	struct pci_epf *epf = to_pci_epf(dev);
490	struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
491
492	if (!driver->probe)
493		return -ENODEV;
494
495	epf->driver = driver;
496
497	return driver->probe(epf, pci_epf_match_id(driver->id_table, epf));
498}
499
500static void pci_epf_device_remove(struct device *dev)
501{
 
502	struct pci_epf *epf = to_pci_epf(dev);
503	struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
504
505	if (driver->remove)
506		driver->remove(epf);
507	epf->driver = NULL;
 
 
508}
509
510static struct bus_type pci_epf_bus_type = {
511	.name		= "pci-epf",
512	.match		= pci_epf_device_match,
513	.probe		= pci_epf_device_probe,
514	.remove		= pci_epf_device_remove,
515};
516
517static int __init pci_epf_init(void)
518{
519	int ret;
520
521	ret = bus_register(&pci_epf_bus_type);
522	if (ret) {
523		pr_err("failed to register pci epf bus --> %d\n", ret);
524		return ret;
525	}
526
527	return 0;
528}
529module_init(pci_epf_init);
530
531static void __exit pci_epf_exit(void)
532{
533	bus_unregister(&pci_epf_bus_type);
534}
535module_exit(pci_epf_exit);
536
537MODULE_DESCRIPTION("PCI EPF Library");
538MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * PCI Endpoint *Function* (EPF) library
  4 *
  5 * Copyright (C) 2017 Texas Instruments
  6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
  7 */
  8
  9#include <linux/device.h>
 10#include <linux/dma-mapping.h>
 11#include <linux/slab.h>
 12#include <linux/module.h>
 13
 14#include <linux/pci-epc.h>
 15#include <linux/pci-epf.h>
 16#include <linux/pci-ep-cfs.h>
 17
 18static DEFINE_MUTEX(pci_epf_mutex);
 19
 20static struct bus_type pci_epf_bus_type;
 21static const struct device_type pci_epf_type;
 22
 23/**
 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
 27 *
 28 * Invoke to notify the function driver that the binding between the EPF device
 29 * and EPC device has been lost.
 30 */
 31void pci_epf_unbind(struct pci_epf *epf)
 32{
 
 
 33	if (!epf->driver) {
 34		dev_WARN(&epf->dev, "epf device not bound to driver\n");
 35		return;
 36	}
 37
 38	mutex_lock(&epf->lock);
 39	epf->driver->ops->unbind(epf);
 
 
 
 
 
 40	mutex_unlock(&epf->lock);
 41	module_put(epf->driver->owner);
 42}
 43EXPORT_SYMBOL_GPL(pci_epf_unbind);
 44
 45/**
 46 * pci_epf_bind() - Notify the function driver that the EPF device has been
 47 *		    bound to a EPC device
 48 * @epf: the EPF device which has been bound to the EPC device
 49 *
 50 * Invoke to notify the function driver that it has been bound to a EPC device
 51 */
 52int pci_epf_bind(struct pci_epf *epf)
 53{
 
 
 
 
 54	int ret;
 55
 56	if (!epf->driver) {
 57		dev_WARN(&epf->dev, "epf device not bound to driver\n");
 58		return -EINVAL;
 59	}
 60
 61	if (!try_module_get(epf->driver->owner))
 62		return -EAGAIN;
 63
 64	mutex_lock(&epf->lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 65	ret = epf->driver->ops->bind(epf);
 
 
 
 
 
 
 
 
 66	mutex_unlock(&epf->lock);
 
 67
 68	return ret;
 69}
 70EXPORT_SYMBOL_GPL(pci_epf_bind);
 71
 72/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 73 * pci_epf_free_space() - free the allocated PCI EPF register space
 74 * @epf: the EPF device from whom to free the memory
 75 * @addr: the virtual address of the PCI EPF register space
 76 * @bar: the BAR number corresponding to the register space
 
 77 *
 78 * Invoke to free the allocated PCI EPF register space.
 79 */
 80void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar)
 
 81{
 82	struct device *dev = epf->epc->dev.parent;
 
 
 83
 84	if (!addr)
 85		return;
 86
 87	dma_free_coherent(dev, epf->bar[bar].size, addr,
 88			  epf->bar[bar].phys_addr);
 
 
 
 
 
 89
 90	epf->bar[bar].phys_addr = 0;
 91	epf->bar[bar].addr = NULL;
 92	epf->bar[bar].size = 0;
 93	epf->bar[bar].barno = 0;
 94	epf->bar[bar].flags = 0;
 
 
 
 
 95}
 96EXPORT_SYMBOL_GPL(pci_epf_free_space);
 97
 98/**
 99 * pci_epf_alloc_space() - allocate memory for the PCI EPF register space
100 * @epf: the EPF device to whom allocate the memory
101 * @size: the size of the memory that has to be allocated
102 * @bar: the BAR number corresponding to the allocated register space
103 * @align: alignment size for the allocation region
 
104 *
105 * Invoke to allocate memory for the PCI EPF register space.
106 */
107void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
108			  size_t align)
109{
 
 
 
 
110	void *space;
111	struct device *dev = epf->epc->dev.parent;
112	dma_addr_t phys_addr;
113
114	if (size < 128)
115		size = 128;
116
117	if (align)
118		size = ALIGN(size, align);
119	else
120		size = roundup_pow_of_two(size);
121
 
 
 
 
 
 
 
 
 
122	space = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
123	if (!space) {
124		dev_err(dev, "failed to allocate mem space\n");
125		return NULL;
126	}
127
128	epf->bar[bar].phys_addr = phys_addr;
129	epf->bar[bar].addr = space;
130	epf->bar[bar].size = size;
131	epf->bar[bar].barno = bar;
132	epf->bar[bar].flags |= upper_32_bits(size) ?
133				PCI_BASE_ADDRESS_MEM_TYPE_64 :
134				PCI_BASE_ADDRESS_MEM_TYPE_32;
135
136	return space;
137}
138EXPORT_SYMBOL_GPL(pci_epf_alloc_space);
139
140static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
141{
142	struct config_group *group, *tmp;
143
144	if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
145		return;
146
147	mutex_lock(&pci_epf_mutex);
148	list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
149		pci_ep_cfs_remove_epf_group(group);
150	list_del(&driver->epf_group);
151	mutex_unlock(&pci_epf_mutex);
152}
153
154/**
155 * pci_epf_unregister_driver() - unregister the PCI EPF driver
156 * @driver: the PCI EPF driver that has to be unregistered
157 *
158 * Invoke to unregister the PCI EPF driver.
159 */
160void pci_epf_unregister_driver(struct pci_epf_driver *driver)
161{
162	pci_epf_remove_cfs(driver);
163	driver_unregister(&driver->driver);
164}
165EXPORT_SYMBOL_GPL(pci_epf_unregister_driver);
166
167static int pci_epf_add_cfs(struct pci_epf_driver *driver)
168{
169	struct config_group *group;
170	const struct pci_epf_device_id *id;
171
172	if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
173		return 0;
174
175	INIT_LIST_HEAD(&driver->epf_group);
176
177	id = driver->id_table;
178	while (id->name[0]) {
179		group = pci_ep_cfs_add_epf_group(id->name);
180		if (IS_ERR(group)) {
181			pci_epf_remove_cfs(driver);
182			return PTR_ERR(group);
183		}
184
185		mutex_lock(&pci_epf_mutex);
186		list_add_tail(&group->group_entry, &driver->epf_group);
187		mutex_unlock(&pci_epf_mutex);
188		id++;
189	}
190
191	return 0;
192}
193
194/**
195 * __pci_epf_register_driver() - register a new PCI EPF driver
196 * @driver: structure representing PCI EPF driver
197 * @owner: the owner of the module that registers the PCI EPF driver
198 *
199 * Invoke to register a new PCI EPF driver.
200 */
201int __pci_epf_register_driver(struct pci_epf_driver *driver,
202			      struct module *owner)
203{
204	int ret;
205
206	if (!driver->ops)
207		return -EINVAL;
208
209	if (!driver->ops->bind || !driver->ops->unbind)
210		return -EINVAL;
211
212	driver->driver.bus = &pci_epf_bus_type;
213	driver->driver.owner = owner;
214
215	ret = driver_register(&driver->driver);
216	if (ret)
217		return ret;
218
219	pci_epf_add_cfs(driver);
220
221	return 0;
222}
223EXPORT_SYMBOL_GPL(__pci_epf_register_driver);
224
225/**
226 * pci_epf_destroy() - destroy the created PCI EPF device
227 * @epf: the PCI EPF device that has to be destroyed.
228 *
229 * Invoke to destroy the PCI EPF device created by invoking pci_epf_create().
230 */
231void pci_epf_destroy(struct pci_epf *epf)
232{
233	device_unregister(&epf->dev);
234}
235EXPORT_SYMBOL_GPL(pci_epf_destroy);
236
237/**
238 * pci_epf_create() - create a new PCI EPF device
239 * @name: the name of the PCI EPF device. This name will be used to bind the
240 *	  the EPF device to a EPF driver
241 *
242 * Invoke to create a new PCI EPF device by providing the name of the function
243 * device.
244 */
245struct pci_epf *pci_epf_create(const char *name)
246{
247	int ret;
248	struct pci_epf *epf;
249	struct device *dev;
250	int len;
251
252	epf = kzalloc(sizeof(*epf), GFP_KERNEL);
253	if (!epf)
254		return ERR_PTR(-ENOMEM);
255
256	len = strchrnul(name, '.') - name;
257	epf->name = kstrndup(name, len, GFP_KERNEL);
258	if (!epf->name) {
259		kfree(epf);
260		return ERR_PTR(-ENOMEM);
261	}
262
 
 
 
 
263	dev = &epf->dev;
264	device_initialize(dev);
265	dev->bus = &pci_epf_bus_type;
266	dev->type = &pci_epf_type;
267	mutex_init(&epf->lock);
268
269	ret = dev_set_name(dev, "%s", name);
270	if (ret) {
271		put_device(dev);
272		return ERR_PTR(ret);
273	}
274
275	ret = device_add(dev);
276	if (ret) {
277		put_device(dev);
278		return ERR_PTR(ret);
279	}
280
281	return epf;
282}
283EXPORT_SYMBOL_GPL(pci_epf_create);
284
285const struct pci_epf_device_id *
286pci_epf_match_device(const struct pci_epf_device_id *id, struct pci_epf *epf)
287{
288	if (!id || !epf)
289		return NULL;
290
291	while (*id->name) {
292		if (strcmp(epf->name, id->name) == 0)
293			return id;
294		id++;
295	}
296
297	return NULL;
298}
299EXPORT_SYMBOL_GPL(pci_epf_match_device);
300
301static void pci_epf_dev_release(struct device *dev)
302{
303	struct pci_epf *epf = to_pci_epf(dev);
304
305	kfree(epf->name);
306	kfree(epf);
307}
308
309static const struct device_type pci_epf_type = {
310	.release	= pci_epf_dev_release,
311};
312
313static int
314pci_epf_match_id(const struct pci_epf_device_id *id, const struct pci_epf *epf)
315{
316	while (id->name[0]) {
317		if (strcmp(epf->name, id->name) == 0)
318			return true;
319		id++;
320	}
321
322	return false;
323}
324
325static int pci_epf_device_match(struct device *dev, struct device_driver *drv)
326{
327	struct pci_epf *epf = to_pci_epf(dev);
328	struct pci_epf_driver *driver = to_pci_epf_driver(drv);
329
330	if (driver->id_table)
331		return pci_epf_match_id(driver->id_table, epf);
332
333	return !strcmp(epf->name, drv->name);
334}
335
336static int pci_epf_device_probe(struct device *dev)
337{
338	struct pci_epf *epf = to_pci_epf(dev);
339	struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
340
341	if (!driver->probe)
342		return -ENODEV;
343
344	epf->driver = driver;
345
346	return driver->probe(epf);
347}
348
349static int pci_epf_device_remove(struct device *dev)
350{
351	int ret = 0;
352	struct pci_epf *epf = to_pci_epf(dev);
353	struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
354
355	if (driver->remove)
356		ret = driver->remove(epf);
357	epf->driver = NULL;
358
359	return ret;
360}
361
362static struct bus_type pci_epf_bus_type = {
363	.name		= "pci-epf",
364	.match		= pci_epf_device_match,
365	.probe		= pci_epf_device_probe,
366	.remove		= pci_epf_device_remove,
367};
368
369static int __init pci_epf_init(void)
370{
371	int ret;
372
373	ret = bus_register(&pci_epf_bus_type);
374	if (ret) {
375		pr_err("failed to register pci epf bus --> %d\n", ret);
376		return ret;
377	}
378
379	return 0;
380}
381module_init(pci_epf_init);
382
383static void __exit pci_epf_exit(void)
384{
385	bus_unregister(&pci_epf_bus_type);
386}
387module_exit(pci_epf_exit);
388
389MODULE_DESCRIPTION("PCI EPF Library");
390MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
391MODULE_LICENSE("GPL v2");