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 
18 static DEFINE_MUTEX(pci_epf_mutex);
19 
20 static struct bus_type pci_epf_bus_type;
21 static const struct device_type pci_epf_type;
22 
23 /**
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
28  *         pci_epf_device_id)
29  *
30  * Invoke to expose function specific attributes in configfs. If the function
31  * driver does not have anything to expose (attributes configured by user),
32  * return NULL.
33  */
34 struct config_group *pci_epf_type_add_cfs(struct pci_epf *epf,
35 					  struct config_group *group)
36 {
37 	struct config_group *epf_type_group;
38 
39 	if (!epf->driver) {
40 		dev_err(&epf->dev, "epf device not bound to driver\n");
41 		return NULL;
42 	}
43 
44 	if (!epf->driver->ops->add_cfs)
45 		return NULL;
46 
47 	mutex_lock(&epf->lock);
48 	epf_type_group = epf->driver->ops->add_cfs(epf, group);
49 	mutex_unlock(&epf->lock);
50 
51 	return epf_type_group;
52 }
53 EXPORT_SYMBOL_GPL(pci_epf_type_add_cfs);
54 
55 /**
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
59  *
60  * Invoke to notify the function driver that the binding between the EPF device
61  * and EPC device has been lost.
62  */
63 void pci_epf_unbind(struct pci_epf *epf)
64 {
65 	struct pci_epf *epf_vf;
66 
67 	if (!epf->driver) {
68 		dev_WARN(&epf->dev, "epf device not bound to driver\n");
69 		return;
70 	}
71 
72 	mutex_lock(&epf->lock);
73 	list_for_each_entry(epf_vf, &epf->pci_vepf, list) {
74 		if (epf_vf->is_bound)
75 			epf_vf->driver->ops->unbind(epf_vf);
76 	}
77 	if (epf->is_bound)
78 		epf->driver->ops->unbind(epf);
79 	mutex_unlock(&epf->lock);
80 	module_put(epf->driver->owner);
81 }
82 EXPORT_SYMBOL_GPL(pci_epf_unbind);
83 
84 /**
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
88  *
89  * Invoke to notify the function driver that it has been bound to a EPC device
90  */
91 int pci_epf_bind(struct pci_epf *epf)
92 {
93 	struct device *dev = &epf->dev;
94 	struct pci_epf *epf_vf;
95 	u8 func_no, vfunc_no;
96 	struct pci_epc *epc;
97 	int ret;
98 
99 	if (!epf->driver) {
100 		dev_WARN(dev, "epf device not bound to driver\n");
101 		return -EINVAL;
102 	}
103 
104 	if (!try_module_get(epf->driver->owner))
105 		return -EAGAIN;
106 
107 	mutex_lock(&epf->lock);
108 	list_for_each_entry(epf_vf, &epf->pci_vepf, list) {
109 		vfunc_no = epf_vf->vfunc_no;
110 
111 		if (vfunc_no < 1) {
112 			dev_err(dev, "Invalid virtual function number\n");
113 			ret = -EINVAL;
114 			goto ret;
115 		}
116 
117 		epc = epf->epc;
118 		func_no = epf->func_no;
119 		if (!IS_ERR_OR_NULL(epc)) {
120 			if (!epc->max_vfs) {
121 				dev_err(dev, "No support for virt function\n");
122 				ret = -EINVAL;
123 				goto ret;
124 			}
125 
126 			if (vfunc_no > epc->max_vfs[func_no]) {
127 				dev_err(dev, "PF%d: Exceeds max vfunc number\n",
128 					func_no);
129 				ret = -EINVAL;
130 				goto ret;
131 			}
132 		}
133 
134 		epc = epf->sec_epc;
135 		func_no = epf->sec_epc_func_no;
136 		if (!IS_ERR_OR_NULL(epc)) {
137 			if (!epc->max_vfs) {
138 				dev_err(dev, "No support for virt function\n");
139 				ret = -EINVAL;
140 				goto ret;
141 			}
142 
143 			if (vfunc_no > epc->max_vfs[func_no]) {
144 				dev_err(dev, "PF%d: Exceeds max vfunc number\n",
145 					func_no);
146 				ret = -EINVAL;
147 				goto ret;
148 			}
149 		}
150 
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);
156 		if (ret)
157 			goto ret;
158 		epf_vf->is_bound = true;
159 	}
160 
161 	ret = epf->driver->ops->bind(epf);
162 	if (ret)
163 		goto ret;
164 	epf->is_bound = true;
165 
166 	mutex_unlock(&epf->lock);
167 	return 0;
168 
169 ret:
170 	mutex_unlock(&epf->lock);
171 	pci_epf_unbind(epf);
172 
173 	return ret;
174 }
175 EXPORT_SYMBOL_GPL(pci_epf_bind);
176 
177 /**
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
180  *   associated
181  * @epf_vf: the virtual EP function to be added
182  *
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.
186  */
187 int pci_epf_add_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf)
188 {
189 	u32 vfunc_no;
190 
191 	if (IS_ERR_OR_NULL(epf_pf) || IS_ERR_OR_NULL(epf_vf))
192 		return -EINVAL;
193 
194 	if (epf_pf->epc || epf_vf->epc || epf_vf->epf_pf)
195 		return -EBUSY;
196 
197 	if (epf_pf->sec_epc || epf_vf->sec_epc)
198 		return -EBUSY;
199 
200 	mutex_lock(&epf_pf->lock);
201 	vfunc_no = find_first_zero_bit(&epf_pf->vfunction_num_map,
202 				       BITS_PER_LONG);
203 	if (vfunc_no >= BITS_PER_LONG) {
204 		mutex_unlock(&epf_pf->lock);
205 		return -EINVAL;
206 	}
207 
208 	set_bit(vfunc_no, &epf_pf->vfunction_num_map);
209 	epf_vf->vfunc_no = vfunc_no;
210 
211 	epf_vf->epf_pf = epf_pf;
212 	epf_vf->is_vf = true;
213 
214 	list_add_tail(&epf_vf->list, &epf_pf->pci_vepf);
215 	mutex_unlock(&epf_pf->lock);
216 
217 	return 0;
218 }
219 EXPORT_SYMBOL_GPL(pci_epf_add_vepf);
220 
221 /**
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
224  *   be removed
225  * @epf_vf: the virtual EP function to be removed
226  *
227  * Invoke to remove a virtual endpoint function from the physical endpoint
228  * function.
229  */
230 void pci_epf_remove_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf)
231 {
232 	if (IS_ERR_OR_NULL(epf_pf) || IS_ERR_OR_NULL(epf_vf))
233 		return;
234 
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);
239 }
240 EXPORT_SYMBOL_GPL(pci_epf_remove_vepf);
241 
242 /**
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
248  *
249  * Invoke to free the allocated PCI EPF register space.
250  */
251 void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar,
252 			enum pci_epc_interface_type type)
253 {
254 	struct device *dev;
255 	struct pci_epf_bar *epf_bar;
256 	struct pci_epc *epc;
257 
258 	if (!addr)
259 		return;
260 
261 	if (type == PRIMARY_INTERFACE) {
262 		epc = epf->epc;
263 		epf_bar = epf->bar;
264 	} else {
265 		epc = epf->sec_epc;
266 		epf_bar = epf->sec_epc_bar;
267 	}
268 
269 	dev = epc->dev.parent;
270 	dma_free_coherent(dev, epf_bar[bar].size, addr,
271 			  epf_bar[bar].phys_addr);
272 
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;
278 }
279 EXPORT_SYMBOL_GPL(pci_epf_free_space);
280 
281 /**
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
288  *
289  * Invoke to allocate memory for the PCI EPF register space.
290  */
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)
293 {
294 	struct pci_epf_bar *epf_bar;
295 	dma_addr_t phys_addr;
296 	struct pci_epc *epc;
297 	struct device *dev;
298 	void *space;
299 
300 	if (size < 128)
301 		size = 128;
302 
303 	if (align)
304 		size = ALIGN(size, align);
305 	else
306 		size = roundup_pow_of_two(size);
307 
308 	if (type == PRIMARY_INTERFACE) {
309 		epc = epf->epc;
310 		epf_bar = epf->bar;
311 	} else {
312 		epc = epf->sec_epc;
313 		epf_bar = epf->sec_epc_bar;
314 	}
315 
316 	dev = epc->dev.parent;
317 	space = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
318 	if (!space) {
319 		dev_err(dev, "failed to allocate mem space\n");
320 		return NULL;
321 	}
322 
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;
330 
331 	return space;
332 }
333 EXPORT_SYMBOL_GPL(pci_epf_alloc_space);
334 
335 static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
336 {
337 	struct config_group *group, *tmp;
338 
339 	if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
340 		return;
341 
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);
347 }
348 
349 /**
350  * pci_epf_unregister_driver() - unregister the PCI EPF driver
351  * @driver: the PCI EPF driver that has to be unregistered
352  *
353  * Invoke to unregister the PCI EPF driver.
354  */
355 void pci_epf_unregister_driver(struct pci_epf_driver *driver)
356 {
357 	pci_epf_remove_cfs(driver);
358 	driver_unregister(&driver->driver);
359 }
360 EXPORT_SYMBOL_GPL(pci_epf_unregister_driver);
361 
362 static int pci_epf_add_cfs(struct pci_epf_driver *driver)
363 {
364 	struct config_group *group;
365 	const struct pci_epf_device_id *id;
366 
367 	if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
368 		return 0;
369 
370 	INIT_LIST_HEAD(&driver->epf_group);
371 
372 	id = driver->id_table;
373 	while (id->name[0]) {
374 		group = pci_ep_cfs_add_epf_group(id->name);
375 		if (IS_ERR(group)) {
376 			pci_epf_remove_cfs(driver);
377 			return PTR_ERR(group);
378 		}
379 
380 		mutex_lock(&pci_epf_mutex);
381 		list_add_tail(&group->group_entry, &driver->epf_group);
382 		mutex_unlock(&pci_epf_mutex);
383 		id++;
384 	}
385 
386 	return 0;
387 }
388 
389 /**
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
393  *
394  * Invoke to register a new PCI EPF driver.
395  */
396 int __pci_epf_register_driver(struct pci_epf_driver *driver,
397 			      struct module *owner)
398 {
399 	int ret;
400 
401 	if (!driver->ops)
402 		return -EINVAL;
403 
404 	if (!driver->ops->bind || !driver->ops->unbind)
405 		return -EINVAL;
406 
407 	driver->driver.bus = &pci_epf_bus_type;
408 	driver->driver.owner = owner;
409 
410 	ret = driver_register(&driver->driver);
411 	if (ret)
412 		return ret;
413 
414 	pci_epf_add_cfs(driver);
415 
416 	return 0;
417 }
418 EXPORT_SYMBOL_GPL(__pci_epf_register_driver);
419 
420 /**
421  * pci_epf_destroy() - destroy the created PCI EPF device
422  * @epf: the PCI EPF device that has to be destroyed.
423  *
424  * Invoke to destroy the PCI EPF device created by invoking pci_epf_create().
425  */
426 void pci_epf_destroy(struct pci_epf *epf)
427 {
428 	device_unregister(&epf->dev);
429 }
430 EXPORT_SYMBOL_GPL(pci_epf_destroy);
431 
432 /**
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  *	  EPF device to a EPF driver
436  *
437  * Invoke to create a new PCI EPF device by providing the name of the function
438  * device.
439  */
440 struct pci_epf *pci_epf_create(const char *name)
441 {
442 	int ret;
443 	struct pci_epf *epf;
444 	struct device *dev;
445 	int len;
446 
447 	epf = kzalloc(sizeof(*epf), GFP_KERNEL);
448 	if (!epf)
449 		return ERR_PTR(-ENOMEM);
450 
451 	len = strchrnul(name, '.') - name;
452 	epf->name = kstrndup(name, len, GFP_KERNEL);
453 	if (!epf->name) {
454 		kfree(epf);
455 		return ERR_PTR(-ENOMEM);
456 	}
457 
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);
461 
462 	dev = &epf->dev;
463 	device_initialize(dev);
464 	dev->bus = &pci_epf_bus_type;
465 	dev->type = &pci_epf_type;
466 	mutex_init(&epf->lock);
467 
468 	ret = dev_set_name(dev, "%s", name);
469 	if (ret) {
470 		put_device(dev);
471 		return ERR_PTR(ret);
472 	}
473 
474 	ret = device_add(dev);
475 	if (ret) {
476 		put_device(dev);
477 		return ERR_PTR(ret);
478 	}
479 
480 	return epf;
481 }
482 EXPORT_SYMBOL_GPL(pci_epf_create);
483 
484 static void pci_epf_dev_release(struct device *dev)
485 {
486 	struct pci_epf *epf = to_pci_epf(dev);
487 
488 	kfree(epf->name);
489 	kfree(epf);
490 }
491 
492 static const struct device_type pci_epf_type = {
493 	.release	= pci_epf_dev_release,
494 };
495 
496 static int
497 pci_epf_match_id(const struct pci_epf_device_id *id, const struct pci_epf *epf)
498 {
499 	while (id->name[0]) {
500 		if (strcmp(epf->name, id->name) == 0)
501 			return true;
502 		id++;
503 	}
504 
505 	return false;
506 }
507 
508 static int pci_epf_device_match(struct device *dev, struct device_driver *drv)
509 {
510 	struct pci_epf *epf = to_pci_epf(dev);
511 	struct pci_epf_driver *driver = to_pci_epf_driver(drv);
512 
513 	if (driver->id_table)
514 		return pci_epf_match_id(driver->id_table, epf);
515 
516 	return !strcmp(epf->name, drv->name);
517 }
518 
519 static int pci_epf_device_probe(struct device *dev)
520 {
521 	struct pci_epf *epf = to_pci_epf(dev);
522 	struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
523 
524 	if (!driver->probe)
525 		return -ENODEV;
526 
527 	epf->driver = driver;
528 
529 	return driver->probe(epf);
530 }
531 
532 static void pci_epf_device_remove(struct device *dev)
533 {
534 	struct pci_epf *epf = to_pci_epf(dev);
535 	struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
536 
537 	if (driver->remove)
538 		driver->remove(epf);
539 	epf->driver = NULL;
540 }
541 
542 static struct bus_type pci_epf_bus_type = {
543 	.name		= "pci-epf",
544 	.match		= pci_epf_device_match,
545 	.probe		= pci_epf_device_probe,
546 	.remove		= pci_epf_device_remove,
547 };
548 
549 static int __init pci_epf_init(void)
550 {
551 	int ret;
552 
553 	ret = bus_register(&pci_epf_bus_type);
554 	if (ret) {
555 		pr_err("failed to register pci epf bus --> %d\n", ret);
556 		return ret;
557 	}
558 
559 	return 0;
560 }
561 module_init(pci_epf_init);
562 
563 static void __exit pci_epf_exit(void)
564 {
565 	bus_unregister(&pci_epf_bus_type);
566 }
567 module_exit(pci_epf_exit);
568 
569 MODULE_DESCRIPTION("PCI EPF Library");
570 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
571 MODULE_LICENSE("GPL v2");
572