1 // SPDX-License-Identifier: GPL-2.0
3 * configfs to configure the PCI endpoint
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
9 #include <linux/module.h>
10 #include <linux/idr.h>
11 #include <linux/slab.h>
13 #include <linux/pci-epc.h>
14 #include <linux/pci-epf.h>
15 #include <linux/pci-ep-cfs.h>
17 static DEFINE_IDR(functions_idr);
18 static DEFINE_MUTEX(functions_mutex);
19 static struct config_group *functions_group;
20 static struct config_group *controllers_group;
22 struct pci_epf_group {
23 struct config_group group;
24 struct config_group primary_epc_group;
25 struct config_group secondary_epc_group;
26 struct config_group *type_group;
27 struct delayed_work cfs_work;
32 struct pci_epc_group {
33 struct config_group group;
38 static inline struct pci_epf_group *to_pci_epf_group(struct config_item *item)
40 return container_of(to_config_group(item), struct pci_epf_group, group);
43 static inline struct pci_epc_group *to_pci_epc_group(struct config_item *item)
45 return container_of(to_config_group(item), struct pci_epc_group, group);
48 static int pci_secondary_epc_epf_link(struct config_item *epf_item,
49 struct config_item *epc_item)
52 struct pci_epf_group *epf_group = to_pci_epf_group(epf_item->ci_parent);
53 struct pci_epc_group *epc_group = to_pci_epc_group(epc_item);
54 struct pci_epc *epc = epc_group->epc;
55 struct pci_epf *epf = epf_group->epf;
57 ret = pci_epc_add_epf(epc, epf, SECONDARY_INTERFACE);
61 ret = pci_epf_bind(epf);
63 pci_epc_remove_epf(epc, epf, SECONDARY_INTERFACE);
70 static void pci_secondary_epc_epf_unlink(struct config_item *epc_item,
71 struct config_item *epf_item)
73 struct pci_epf_group *epf_group = to_pci_epf_group(epf_item->ci_parent);
74 struct pci_epc_group *epc_group = to_pci_epc_group(epc_item);
78 WARN_ON_ONCE(epc_group->start);
83 pci_epc_remove_epf(epc, epf, SECONDARY_INTERFACE);
86 static struct configfs_item_operations pci_secondary_epc_item_ops = {
87 .allow_link = pci_secondary_epc_epf_link,
88 .drop_link = pci_secondary_epc_epf_unlink,
91 static const struct config_item_type pci_secondary_epc_type = {
92 .ct_item_ops = &pci_secondary_epc_item_ops,
93 .ct_owner = THIS_MODULE,
96 static struct config_group
97 *pci_ep_cfs_add_secondary_group(struct pci_epf_group *epf_group)
99 struct config_group *secondary_epc_group;
101 secondary_epc_group = &epf_group->secondary_epc_group;
102 config_group_init_type_name(secondary_epc_group, "secondary",
103 &pci_secondary_epc_type);
104 configfs_register_group(&epf_group->group, secondary_epc_group);
106 return secondary_epc_group;
109 static int pci_primary_epc_epf_link(struct config_item *epf_item,
110 struct config_item *epc_item)
113 struct pci_epf_group *epf_group = to_pci_epf_group(epf_item->ci_parent);
114 struct pci_epc_group *epc_group = to_pci_epc_group(epc_item);
115 struct pci_epc *epc = epc_group->epc;
116 struct pci_epf *epf = epf_group->epf;
118 ret = pci_epc_add_epf(epc, epf, PRIMARY_INTERFACE);
122 ret = pci_epf_bind(epf);
124 pci_epc_remove_epf(epc, epf, PRIMARY_INTERFACE);
131 static void pci_primary_epc_epf_unlink(struct config_item *epc_item,
132 struct config_item *epf_item)
134 struct pci_epf_group *epf_group = to_pci_epf_group(epf_item->ci_parent);
135 struct pci_epc_group *epc_group = to_pci_epc_group(epc_item);
139 WARN_ON_ONCE(epc_group->start);
141 epc = epc_group->epc;
142 epf = epf_group->epf;
144 pci_epc_remove_epf(epc, epf, PRIMARY_INTERFACE);
147 static struct configfs_item_operations pci_primary_epc_item_ops = {
148 .allow_link = pci_primary_epc_epf_link,
149 .drop_link = pci_primary_epc_epf_unlink,
152 static const struct config_item_type pci_primary_epc_type = {
153 .ct_item_ops = &pci_primary_epc_item_ops,
154 .ct_owner = THIS_MODULE,
157 static struct config_group
158 *pci_ep_cfs_add_primary_group(struct pci_epf_group *epf_group)
160 struct config_group *primary_epc_group = &epf_group->primary_epc_group;
162 config_group_init_type_name(primary_epc_group, "primary",
163 &pci_primary_epc_type);
164 configfs_register_group(&epf_group->group, primary_epc_group);
166 return primary_epc_group;
169 static ssize_t pci_epc_start_store(struct config_item *item, const char *page,
175 struct pci_epc_group *epc_group = to_pci_epc_group(item);
177 epc = epc_group->epc;
179 if (kstrtobool(page, &start) < 0)
182 if (start == epc_group->start)
187 epc_group->start = 0;
191 ret = pci_epc_start(epc);
193 dev_err(&epc->dev, "failed to start endpoint controller\n");
197 epc_group->start = start;
202 static ssize_t pci_epc_start_show(struct config_item *item, char *page)
204 return sysfs_emit(page, "%d\n", to_pci_epc_group(item)->start);
207 CONFIGFS_ATTR(pci_epc_, start);
209 static struct configfs_attribute *pci_epc_attrs[] = {
214 static int pci_epc_epf_link(struct config_item *epc_item,
215 struct config_item *epf_item)
218 struct pci_epf_group *epf_group = to_pci_epf_group(epf_item);
219 struct pci_epc_group *epc_group = to_pci_epc_group(epc_item);
220 struct pci_epc *epc = epc_group->epc;
221 struct pci_epf *epf = epf_group->epf;
223 ret = pci_epc_add_epf(epc, epf, PRIMARY_INTERFACE);
227 ret = pci_epf_bind(epf);
229 pci_epc_remove_epf(epc, epf, PRIMARY_INTERFACE);
236 static void pci_epc_epf_unlink(struct config_item *epc_item,
237 struct config_item *epf_item)
241 struct pci_epf_group *epf_group = to_pci_epf_group(epf_item);
242 struct pci_epc_group *epc_group = to_pci_epc_group(epc_item);
244 WARN_ON_ONCE(epc_group->start);
246 epc = epc_group->epc;
247 epf = epf_group->epf;
249 pci_epc_remove_epf(epc, epf, PRIMARY_INTERFACE);
252 static struct configfs_item_operations pci_epc_item_ops = {
253 .allow_link = pci_epc_epf_link,
254 .drop_link = pci_epc_epf_unlink,
257 static const struct config_item_type pci_epc_type = {
258 .ct_item_ops = &pci_epc_item_ops,
259 .ct_attrs = pci_epc_attrs,
260 .ct_owner = THIS_MODULE,
263 struct config_group *pci_ep_cfs_add_epc_group(const char *name)
267 struct config_group *group;
268 struct pci_epc_group *epc_group;
270 epc_group = kzalloc(sizeof(*epc_group), GFP_KERNEL);
276 group = &epc_group->group;
278 config_group_init_type_name(group, name, &pci_epc_type);
279 ret = configfs_register_group(controllers_group, group);
281 pr_err("failed to register configfs group for %s\n", name);
282 goto err_register_group;
285 epc = pci_epc_get(name);
291 epc_group->epc = epc;
296 configfs_unregister_group(group);
304 EXPORT_SYMBOL(pci_ep_cfs_add_epc_group);
306 void pci_ep_cfs_remove_epc_group(struct config_group *group)
308 struct pci_epc_group *epc_group;
313 epc_group = container_of(group, struct pci_epc_group, group);
314 pci_epc_put(epc_group->epc);
315 configfs_unregister_group(&epc_group->group);
318 EXPORT_SYMBOL(pci_ep_cfs_remove_epc_group);
320 #define PCI_EPF_HEADER_R(_name) \
321 static ssize_t pci_epf_##_name##_show(struct config_item *item, char *page) \
323 struct pci_epf *epf = to_pci_epf_group(item)->epf; \
324 if (WARN_ON_ONCE(!epf->header)) \
326 return sysfs_emit(page, "0x%04x\n", epf->header->_name); \
329 #define PCI_EPF_HEADER_W_u32(_name) \
330 static ssize_t pci_epf_##_name##_store(struct config_item *item, \
331 const char *page, size_t len) \
334 struct pci_epf *epf = to_pci_epf_group(item)->epf; \
335 if (WARN_ON_ONCE(!epf->header)) \
337 if (kstrtou32(page, 0, &val) < 0) \
339 epf->header->_name = val; \
343 #define PCI_EPF_HEADER_W_u16(_name) \
344 static ssize_t pci_epf_##_name##_store(struct config_item *item, \
345 const char *page, size_t len) \
348 struct pci_epf *epf = to_pci_epf_group(item)->epf; \
349 if (WARN_ON_ONCE(!epf->header)) \
351 if (kstrtou16(page, 0, &val) < 0) \
353 epf->header->_name = val; \
357 #define PCI_EPF_HEADER_W_u8(_name) \
358 static ssize_t pci_epf_##_name##_store(struct config_item *item, \
359 const char *page, size_t len) \
362 struct pci_epf *epf = to_pci_epf_group(item)->epf; \
363 if (WARN_ON_ONCE(!epf->header)) \
365 if (kstrtou8(page, 0, &val) < 0) \
367 epf->header->_name = val; \
371 static ssize_t pci_epf_msi_interrupts_store(struct config_item *item,
372 const char *page, size_t len)
376 if (kstrtou8(page, 0, &val) < 0)
379 to_pci_epf_group(item)->epf->msi_interrupts = val;
384 static ssize_t pci_epf_msi_interrupts_show(struct config_item *item,
387 return sysfs_emit(page, "%d\n",
388 to_pci_epf_group(item)->epf->msi_interrupts);
391 static ssize_t pci_epf_msix_interrupts_store(struct config_item *item,
392 const char *page, size_t len)
396 if (kstrtou16(page, 0, &val) < 0)
399 to_pci_epf_group(item)->epf->msix_interrupts = val;
404 static ssize_t pci_epf_msix_interrupts_show(struct config_item *item,
407 return sysfs_emit(page, "%d\n",
408 to_pci_epf_group(item)->epf->msix_interrupts);
411 PCI_EPF_HEADER_R(vendorid)
412 PCI_EPF_HEADER_W_u16(vendorid)
414 PCI_EPF_HEADER_R(deviceid)
415 PCI_EPF_HEADER_W_u16(deviceid)
417 PCI_EPF_HEADER_R(revid)
418 PCI_EPF_HEADER_W_u8(revid)
420 PCI_EPF_HEADER_R(progif_code)
421 PCI_EPF_HEADER_W_u8(progif_code)
423 PCI_EPF_HEADER_R(subclass_code)
424 PCI_EPF_HEADER_W_u8(subclass_code)
426 PCI_EPF_HEADER_R(baseclass_code)
427 PCI_EPF_HEADER_W_u8(baseclass_code)
429 PCI_EPF_HEADER_R(cache_line_size)
430 PCI_EPF_HEADER_W_u8(cache_line_size)
432 PCI_EPF_HEADER_R(subsys_vendor_id)
433 PCI_EPF_HEADER_W_u16(subsys_vendor_id)
435 PCI_EPF_HEADER_R(subsys_id)
436 PCI_EPF_HEADER_W_u16(subsys_id)
438 PCI_EPF_HEADER_R(interrupt_pin)
439 PCI_EPF_HEADER_W_u8(interrupt_pin)
441 CONFIGFS_ATTR(pci_epf_, vendorid);
442 CONFIGFS_ATTR(pci_epf_, deviceid);
443 CONFIGFS_ATTR(pci_epf_, revid);
444 CONFIGFS_ATTR(pci_epf_, progif_code);
445 CONFIGFS_ATTR(pci_epf_, subclass_code);
446 CONFIGFS_ATTR(pci_epf_, baseclass_code);
447 CONFIGFS_ATTR(pci_epf_, cache_line_size);
448 CONFIGFS_ATTR(pci_epf_, subsys_vendor_id);
449 CONFIGFS_ATTR(pci_epf_, subsys_id);
450 CONFIGFS_ATTR(pci_epf_, interrupt_pin);
451 CONFIGFS_ATTR(pci_epf_, msi_interrupts);
452 CONFIGFS_ATTR(pci_epf_, msix_interrupts);
454 static struct configfs_attribute *pci_epf_attrs[] = {
455 &pci_epf_attr_vendorid,
456 &pci_epf_attr_deviceid,
458 &pci_epf_attr_progif_code,
459 &pci_epf_attr_subclass_code,
460 &pci_epf_attr_baseclass_code,
461 &pci_epf_attr_cache_line_size,
462 &pci_epf_attr_subsys_vendor_id,
463 &pci_epf_attr_subsys_id,
464 &pci_epf_attr_interrupt_pin,
465 &pci_epf_attr_msi_interrupts,
466 &pci_epf_attr_msix_interrupts,
470 static int pci_epf_vepf_link(struct config_item *epf_pf_item,
471 struct config_item *epf_vf_item)
473 struct pci_epf_group *epf_vf_group = to_pci_epf_group(epf_vf_item);
474 struct pci_epf_group *epf_pf_group = to_pci_epf_group(epf_pf_item);
475 struct pci_epf *epf_pf = epf_pf_group->epf;
476 struct pci_epf *epf_vf = epf_vf_group->epf;
478 return pci_epf_add_vepf(epf_pf, epf_vf);
481 static void pci_epf_vepf_unlink(struct config_item *epf_pf_item,
482 struct config_item *epf_vf_item)
484 struct pci_epf_group *epf_vf_group = to_pci_epf_group(epf_vf_item);
485 struct pci_epf_group *epf_pf_group = to_pci_epf_group(epf_pf_item);
486 struct pci_epf *epf_pf = epf_pf_group->epf;
487 struct pci_epf *epf_vf = epf_vf_group->epf;
489 pci_epf_remove_vepf(epf_pf, epf_vf);
492 static void pci_epf_release(struct config_item *item)
494 struct pci_epf_group *epf_group = to_pci_epf_group(item);
496 mutex_lock(&functions_mutex);
497 idr_remove(&functions_idr, epf_group->index);
498 mutex_unlock(&functions_mutex);
499 pci_epf_destroy(epf_group->epf);
503 static struct configfs_item_operations pci_epf_ops = {
504 .allow_link = pci_epf_vepf_link,
505 .drop_link = pci_epf_vepf_unlink,
506 .release = pci_epf_release,
509 static const struct config_item_type pci_epf_type = {
510 .ct_item_ops = &pci_epf_ops,
511 .ct_attrs = pci_epf_attrs,
512 .ct_owner = THIS_MODULE,
516 * pci_epf_type_add_cfs() - Help function drivers to expose function specific
517 * attributes in configfs
518 * @epf: the EPF device that has to be configured using configfs
519 * @group: the parent configfs group (corresponding to entries in
522 * Invoke to expose function specific attributes in configfs.
524 * Return: A pointer to a config_group structure or NULL if the function driver
525 * does not have anything to expose (attributes configured by user) or if
526 * the function driver does not implement the add_cfs() method.
528 * Returns an error pointer if this function is called for an unbound EPF device
529 * or if the EPF driver add_cfs() method fails.
531 static struct config_group *pci_epf_type_add_cfs(struct pci_epf *epf,
532 struct config_group *group)
534 struct config_group *epf_type_group;
537 dev_err(&epf->dev, "epf device not bound to driver\n");
538 return ERR_PTR(-ENODEV);
541 if (!epf->driver->ops->add_cfs)
544 mutex_lock(&epf->lock);
545 epf_type_group = epf->driver->ops->add_cfs(epf, group);
546 mutex_unlock(&epf->lock);
548 return epf_type_group;
551 static void pci_ep_cfs_add_type_group(struct pci_epf_group *epf_group)
553 struct config_group *group;
555 group = pci_epf_type_add_cfs(epf_group->epf, &epf_group->group);
560 dev_err(&epf_group->epf->dev,
561 "failed to create epf type specific attributes\n");
565 configfs_register_group(&epf_group->group, group);
568 static void pci_epf_cfs_work(struct work_struct *work)
570 struct pci_epf_group *epf_group;
571 struct config_group *group;
573 epf_group = container_of(work, struct pci_epf_group, cfs_work.work);
574 group = pci_ep_cfs_add_primary_group(epf_group);
576 pr_err("failed to create 'primary' EPC interface\n");
580 group = pci_ep_cfs_add_secondary_group(epf_group);
582 pr_err("failed to create 'secondary' EPC interface\n");
586 pci_ep_cfs_add_type_group(epf_group);
589 static struct config_group *pci_epf_make(struct config_group *group,
592 struct pci_epf_group *epf_group;
597 epf_group = kzalloc(sizeof(*epf_group), GFP_KERNEL);
599 return ERR_PTR(-ENOMEM);
601 mutex_lock(&functions_mutex);
602 index = idr_alloc(&functions_idr, epf_group, 0, 0, GFP_KERNEL);
603 mutex_unlock(&functions_mutex);
609 epf_group->index = index;
611 config_group_init_type_name(&epf_group->group, name, &pci_epf_type);
613 epf_name = kasprintf(GFP_KERNEL, "%s.%d",
614 group->cg_item.ci_name, epf_group->index);
620 epf = pci_epf_create(epf_name);
622 pr_err("failed to create endpoint function device\n");
627 epf->group = &epf_group->group;
628 epf_group->epf = epf;
632 INIT_DELAYED_WORK(&epf_group->cfs_work, pci_epf_cfs_work);
633 queue_delayed_work(system_wq, &epf_group->cfs_work,
634 msecs_to_jiffies(1));
636 return &epf_group->group;
642 mutex_lock(&functions_mutex);
643 idr_remove(&functions_idr, epf_group->index);
644 mutex_unlock(&functions_mutex);
652 static void pci_epf_drop(struct config_group *group, struct config_item *item)
654 config_item_put(item);
657 static struct configfs_group_operations pci_epf_group_ops = {
658 .make_group = &pci_epf_make,
659 .drop_item = &pci_epf_drop,
662 static const struct config_item_type pci_epf_group_type = {
663 .ct_group_ops = &pci_epf_group_ops,
664 .ct_owner = THIS_MODULE,
667 struct config_group *pci_ep_cfs_add_epf_group(const char *name)
669 struct config_group *group;
671 group = configfs_register_default_group(functions_group, name,
672 &pci_epf_group_type);
674 pr_err("failed to register configfs group for %s function\n",
679 EXPORT_SYMBOL(pci_ep_cfs_add_epf_group);
681 void pci_ep_cfs_remove_epf_group(struct config_group *group)
683 if (IS_ERR_OR_NULL(group))
686 configfs_unregister_default_group(group);
688 EXPORT_SYMBOL(pci_ep_cfs_remove_epf_group);
690 static const struct config_item_type pci_functions_type = {
691 .ct_owner = THIS_MODULE,
694 static const struct config_item_type pci_controllers_type = {
695 .ct_owner = THIS_MODULE,
698 static const struct config_item_type pci_ep_type = {
699 .ct_owner = THIS_MODULE,
702 static struct configfs_subsystem pci_ep_cfs_subsys = {
705 .ci_namebuf = "pci_ep",
706 .ci_type = &pci_ep_type,
709 .su_mutex = __MUTEX_INITIALIZER(pci_ep_cfs_subsys.su_mutex),
712 static int __init pci_ep_cfs_init(void)
715 struct config_group *root = &pci_ep_cfs_subsys.su_group;
717 config_group_init(root);
719 ret = configfs_register_subsystem(&pci_ep_cfs_subsys);
721 pr_err("Error %d while registering subsystem %s\n",
722 ret, root->cg_item.ci_namebuf);
726 functions_group = configfs_register_default_group(root, "functions",
727 &pci_functions_type);
728 if (IS_ERR(functions_group)) {
729 ret = PTR_ERR(functions_group);
730 pr_err("Error %d while registering functions group\n",
732 goto err_functions_group;
736 configfs_register_default_group(root, "controllers",
737 &pci_controllers_type);
738 if (IS_ERR(controllers_group)) {
739 ret = PTR_ERR(controllers_group);
740 pr_err("Error %d while registering controllers group\n",
742 goto err_controllers_group;
747 err_controllers_group:
748 configfs_unregister_default_group(functions_group);
751 configfs_unregister_subsystem(&pci_ep_cfs_subsys);
756 module_init(pci_ep_cfs_init);
758 static void __exit pci_ep_cfs_exit(void)
760 configfs_unregister_default_group(controllers_group);
761 configfs_unregister_default_group(functions_group);
762 configfs_unregister_subsystem(&pci_ep_cfs_subsys);
764 module_exit(pci_ep_cfs_exit);
766 MODULE_DESCRIPTION("PCI EP CONFIGFS");
767 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");