Merge tag 'pci-v6.10-changes' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-2.6-block.git] / drivers / pci / endpoint / pci-epf-core.c
CommitLineData
8cfab3cf 1// SPDX-License-Identifier: GPL-2.0
9b41d19a 2/*
5e8cb403
KVA
3 * PCI Endpoint *Function* (EPF) library
4 *
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
5e8cb403
KVA
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>
3a401a2c 16#include <linux/pci-ep-cfs.h>
5e8cb403 17
ef1433f7
KVA
18static DEFINE_MUTEX(pci_epf_mutex);
19
b91da730 20static const struct bus_type pci_epf_bus_type;
36b85189 21static const struct device_type pci_epf_type;
5e8cb403 22
5e8cb403
KVA
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{
1cf362e9
KVA
33 struct pci_epf *epf_vf;
34
5e8cb403
KVA
35 if (!epf->driver) {
36 dev_WARN(&epf->dev, "epf device not bound to driver\n");
37 return;
38 }
39
07301c98 40 mutex_lock(&epf->lock);
1cf362e9
KVA
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);
07301c98 47 mutex_unlock(&epf->lock);
5e8cb403
KVA
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{
53fd3cbe 61 struct device *dev = &epf->dev;
1cf362e9 62 struct pci_epf *epf_vf;
53fd3cbe
KVA
63 u8 func_no, vfunc_no;
64 struct pci_epc *epc;
07301c98
KVA
65 int ret;
66
5e8cb403 67 if (!epf->driver) {
53fd3cbe 68 dev_WARN(dev, "epf device not bound to driver\n");
5e8cb403
KVA
69 return -EINVAL;
70 }
71
72 if (!try_module_get(epf->driver->owner))
73 return -EAGAIN;
74
07301c98 75 mutex_lock(&epf->lock);
1cf362e9 76 list_for_each_entry(epf_vf, &epf->pci_vepf, list) {
53fd3cbe
KVA
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
1cf362e9 119 epf_vf->func_no = epf->func_no;
53fd3cbe 120 epf_vf->sec_epc_func_no = epf->sec_epc_func_no;
1cf362e9
KVA
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
07301c98 129 ret = epf->driver->ops->bind(epf);
1cf362e9
KVA
130 if (ret)
131 goto ret;
132 epf->is_bound = true;
133
134 mutex_unlock(&epf->lock);
135 return 0;
136
137ret:
07301c98 138 mutex_unlock(&epf->lock);
1cf362e9 139 pci_epf_unbind(epf);
07301c98
KVA
140
141 return ret;
5e8cb403
KVA
142}
143EXPORT_SYMBOL_GPL(pci_epf_bind);
144
1cf362e9
KVA
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 *
b2105b9f 195 * Invoke to remove a virtual endpoint function from the physical endpoint
1cf362e9
KVA
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
5e8cb403
KVA
210/**
211 * pci_epf_free_space() - free the allocated PCI EPF register space
9b41d19a 212 * @epf: the EPF device from whom to free the memory
5e8cb403
KVA
213 * @addr: the virtual address of the PCI EPF register space
214 * @bar: the BAR number corresponding to the register space
63840ff5 215 * @type: Identifies if the allocated space is for primary EPC or secondary EPC
5e8cb403
KVA
216 *
217 * Invoke to free the allocated PCI EPF register space.
218 */
63840ff5
KVA
219void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar,
220 enum pci_epc_interface_type type)
5e8cb403 221{
80c253bd 222 struct device *dev;
63840ff5
KVA
223 struct pci_epf_bar *epf_bar;
224 struct pci_epc *epc;
5e8cb403
KVA
225
226 if (!addr)
227 return;
228
63840ff5
KVA
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);
5e8cb403 240
63840ff5
KVA
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;
5e8cb403
KVA
246}
247EXPORT_SYMBOL_GPL(pci_epf_free_space);
248
249/**
250 * pci_epf_alloc_space() - allocate memory for the PCI EPF register space
9b41d19a 251 * @epf: the EPF device to whom allocate the memory
5e8cb403
KVA
252 * @size: the size of the memory that has to be allocated
253 * @bar: the BAR number corresponding to the allocated register space
e891becd 254 * @epc_features: the features provided by the EPC specific to this EPF
63840ff5 255 * @type: Identifies if the allocation is for primary EPC or secondary EPC
5e8cb403
KVA
256 *
257 * Invoke to allocate memory for the PCI EPF register space.
29a025b6
NC
258 * Flag PCI_BASE_ADDRESS_MEM_TYPE_64 will automatically get set if the BAR
259 * can only be a 64-bit BAR, or if the requested size is larger than 2 GB.
5e8cb403 260 */
2a9a8016 261void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
e891becd
NC
262 const struct pci_epc_features *epc_features,
263 enum pci_epc_interface_type type)
5e8cb403 264{
e01c9797 265 u64 bar_fixed_size = epc_features->bar[bar].fixed_size;
e891becd 266 size_t align = epc_features->align;
63840ff5 267 struct pci_epf_bar *epf_bar;
5e8cb403 268 dma_addr_t phys_addr;
63840ff5
KVA
269 struct pci_epc *epc;
270 struct device *dev;
271 void *space;
5e8cb403
KVA
272
273 if (size < 128)
274 size = 128;
2a9a8016 275
e01c9797
NC
276 if (epc_features->bar[bar].type == BAR_FIXED && bar_fixed_size) {
277 if (size > bar_fixed_size) {
278 dev_err(&epf->dev,
279 "requested BAR size is larger than fixed size\n");
280 return NULL;
281 }
84b51a6b 282 size = bar_fixed_size;
e01c9797 283 }
84b51a6b 284
2a9a8016
KVA
285 if (align)
286 size = ALIGN(size, align);
287 else
288 size = roundup_pow_of_two(size);
5e8cb403 289
63840ff5
KVA
290 if (type == PRIMARY_INTERFACE) {
291 epc = epf->epc;
292 epf_bar = epf->bar;
293 } else {
294 epc = epf->sec_epc;
295 epf_bar = epf->sec_epc_bar;
296 }
297
298 dev = epc->dev.parent;
5e8cb403
KVA
299 space = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
300 if (!space) {
301 dev_err(dev, "failed to allocate mem space\n");
302 return NULL;
303 }
304
63840ff5
KVA
305 epf_bar[bar].phys_addr = phys_addr;
306 epf_bar[bar].addr = space;
307 epf_bar[bar].size = size;
308 epf_bar[bar].barno = bar;
29a025b6
NC
309 if (upper_32_bits(size) || epc_features->bar[bar].only_64bit)
310 epf_bar[bar].flags |= PCI_BASE_ADDRESS_MEM_TYPE_64;
311 else
312 epf_bar[bar].flags |= PCI_BASE_ADDRESS_MEM_TYPE_32;
5e8cb403
KVA
313
314 return space;
315}
316EXPORT_SYMBOL_GPL(pci_epf_alloc_space);
317
a83a2173 318static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
5e8cb403 319{
fb0de5b8 320 struct config_group *group, *tmp;
ef1433f7 321
a83a2173
KVA
322 if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
323 return;
324
ef1433f7 325 mutex_lock(&pci_epf_mutex);
fb0de5b8 326 list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
ef1433f7
KVA
327 pci_ep_cfs_remove_epf_group(group);
328 list_del(&driver->epf_group);
329 mutex_unlock(&pci_epf_mutex);
a83a2173
KVA
330}
331
332/**
333 * pci_epf_unregister_driver() - unregister the PCI EPF driver
334 * @driver: the PCI EPF driver that has to be unregistered
335 *
336 * Invoke to unregister the PCI EPF driver.
337 */
338void pci_epf_unregister_driver(struct pci_epf_driver *driver)
339{
340 pci_epf_remove_cfs(driver);
5e8cb403
KVA
341 driver_unregister(&driver->driver);
342}
343EXPORT_SYMBOL_GPL(pci_epf_unregister_driver);
344
a83a2173
KVA
345static int pci_epf_add_cfs(struct pci_epf_driver *driver)
346{
347 struct config_group *group;
348 const struct pci_epf_device_id *id;
349
350 if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
351 return 0;
352
353 INIT_LIST_HEAD(&driver->epf_group);
354
355 id = driver->id_table;
356 while (id->name[0]) {
357 group = pci_ep_cfs_add_epf_group(id->name);
358 if (IS_ERR(group)) {
359 pci_epf_remove_cfs(driver);
360 return PTR_ERR(group);
361 }
362
363 mutex_lock(&pci_epf_mutex);
364 list_add_tail(&group->group_entry, &driver->epf_group);
365 mutex_unlock(&pci_epf_mutex);
366 id++;
367 }
368
369 return 0;
370}
371
5e8cb403
KVA
372/**
373 * __pci_epf_register_driver() - register a new PCI EPF driver
374 * @driver: structure representing PCI EPF driver
375 * @owner: the owner of the module that registers the PCI EPF driver
376 *
377 * Invoke to register a new PCI EPF driver.
378 */
379int __pci_epf_register_driver(struct pci_epf_driver *driver,
380 struct module *owner)
381{
382 int ret;
383
384 if (!driver->ops)
385 return -EINVAL;
386
5779dd0a 387 if (!driver->ops->bind || !driver->ops->unbind)
5e8cb403
KVA
388 return -EINVAL;
389
390 driver->driver.bus = &pci_epf_bus_type;
391 driver->driver.owner = owner;
392
393 ret = driver_register(&driver->driver);
394 if (ret)
395 return ret;
396
a83a2173 397 pci_epf_add_cfs(driver);
3a401a2c 398
5e8cb403
KVA
399 return 0;
400}
401EXPORT_SYMBOL_GPL(__pci_epf_register_driver);
402
403/**
404 * pci_epf_destroy() - destroy the created PCI EPF device
405 * @epf: the PCI EPF device that has to be destroyed.
406 *
407 * Invoke to destroy the PCI EPF device created by invoking pci_epf_create().
408 */
409void pci_epf_destroy(struct pci_epf *epf)
410{
411 device_unregister(&epf->dev);
412}
413EXPORT_SYMBOL_GPL(pci_epf_destroy);
414
415/**
416 * pci_epf_create() - create a new PCI EPF device
417 * @name: the name of the PCI EPF device. This name will be used to bind the
b2105b9f 418 * EPF device to a EPF driver
5e8cb403
KVA
419 *
420 * Invoke to create a new PCI EPF device by providing the name of the function
421 * device.
422 */
423struct pci_epf *pci_epf_create(const char *name)
424{
425 int ret;
426 struct pci_epf *epf;
427 struct device *dev;
36cc14ac 428 int len;
5e8cb403
KVA
429
430 epf = kzalloc(sizeof(*epf), GFP_KERNEL);
50ee1061
REF
431 if (!epf)
432 return ERR_PTR(-ENOMEM);
5e8cb403 433
36cc14ac
REF
434 len = strchrnul(name, '.') - name;
435 epf->name = kstrndup(name, len, GFP_KERNEL);
5e8cb403 436 if (!epf->name) {
50ee1061
REF
437 kfree(epf);
438 return ERR_PTR(-ENOMEM);
5e8cb403
KVA
439 }
440
1cf362e9
KVA
441 /* VFs are numbered starting with 1. So set BIT(0) by default */
442 epf->vfunction_num_map = 1;
443 INIT_LIST_HEAD(&epf->pci_vepf);
444
5e8cb403
KVA
445 dev = &epf->dev;
446 device_initialize(dev);
447 dev->bus = &pci_epf_bus_type;
448 dev->type = &pci_epf_type;
07301c98 449 mutex_init(&epf->lock);
5e8cb403
KVA
450
451 ret = dev_set_name(dev, "%s", name);
50ee1061
REF
452 if (ret) {
453 put_device(dev);
454 return ERR_PTR(ret);
455 }
5e8cb403
KVA
456
457 ret = device_add(dev);
50ee1061
REF
458 if (ret) {
459 put_device(dev);
460 return ERR_PTR(ret);
461 }
5e8cb403 462
5e8cb403 463 return epf;
5e8cb403
KVA
464}
465EXPORT_SYMBOL_GPL(pci_epf_create);
466
467static void pci_epf_dev_release(struct device *dev)
468{
469 struct pci_epf *epf = to_pci_epf(dev);
470
471 kfree(epf->name);
472 kfree(epf);
473}
474
36b85189 475static const struct device_type pci_epf_type = {
5e8cb403
KVA
476 .release = pci_epf_dev_release,
477};
478
081c715d 479static const struct pci_epf_device_id *
5e8cb403
KVA
480pci_epf_match_id(const struct pci_epf_device_id *id, const struct pci_epf *epf)
481{
482 while (id->name[0]) {
483 if (strcmp(epf->name, id->name) == 0)
081c715d 484 return id;
5e8cb403
KVA
485 id++;
486 }
487
081c715d 488 return NULL;
5e8cb403
KVA
489}
490
491static int pci_epf_device_match(struct device *dev, struct device_driver *drv)
492{
493 struct pci_epf *epf = to_pci_epf(dev);
494 struct pci_epf_driver *driver = to_pci_epf_driver(drv);
495
496 if (driver->id_table)
081c715d 497 return !!pci_epf_match_id(driver->id_table, epf);
5e8cb403
KVA
498
499 return !strcmp(epf->name, drv->name);
500}
501
502static int pci_epf_device_probe(struct device *dev)
503{
504 struct pci_epf *epf = to_pci_epf(dev);
505 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
506
507 if (!driver->probe)
508 return -ENODEV;
509
510 epf->driver = driver;
511
081c715d 512 return driver->probe(epf, pci_epf_match_id(driver->id_table, epf));
5e8cb403
KVA
513}
514
fc7a6209 515static void pci_epf_device_remove(struct device *dev)
5e8cb403 516{
5e8cb403
KVA
517 struct pci_epf *epf = to_pci_epf(dev);
518 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
519
28daeff6 520 if (driver->remove)
dde0a318 521 driver->remove(epf);
5e8cb403 522 epf->driver = NULL;
5e8cb403
KVA
523}
524
b91da730 525static const struct bus_type pci_epf_bus_type = {
5e8cb403
KVA
526 .name = "pci-epf",
527 .match = pci_epf_device_match,
528 .probe = pci_epf_device_probe,
529 .remove = pci_epf_device_remove,
530};
531
532static int __init pci_epf_init(void)
533{
534 int ret;
535
536 ret = bus_register(&pci_epf_bus_type);
537 if (ret) {
538 pr_err("failed to register pci epf bus --> %d\n", ret);
539 return ret;
540 }
541
542 return 0;
543}
544module_init(pci_epf_init);
545
546static void __exit pci_epf_exit(void)
547{
548 bus_unregister(&pci_epf_bus_type);
549}
550module_exit(pci_epf_exit);
551
552MODULE_DESCRIPTION("PCI EPF Library");
553MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");