1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
9 * Architecture Overview
10 * =====================
11 * CDX is a Hardware Architecture designed for AMD FPGA devices. It
12 * consists of sophisticated mechanism for interaction between FPGA,
13 * Firmware and the APUs (Application CPUs).
15 * Firmware resides on RPU (Realtime CPUs) which interacts with
16 * the FPGA program manager and the APUs. The RPU provides memory-mapped
17 * interface (RPU if) which is used to communicate with APUs.
19 * The diagram below shows an overview of the CDX architecture:
21 * +--------------------------------------+
22 * | Application CPUs (APU) |
24 * | CDX device drivers|
30 * +-----------------------------|--------+
31 * | (discover, config,
34 * +------------------------| RPU if |----+
37 * | Realtime CPUs (RPU) |
39 * +--------------------------------------+
41 * +---------------------|----------------+
43 * | +-----------------------+ |
45 * | +-------+ +-------+ +-------+ |
46 * | | dev 1 | | dev 2 | | dev 3 | |
47 * | +-------+ +-------+ +-------+ |
48 * +--------------------------------------+
50 * The RPU firmware extracts the device information from the loaded FPGA
51 * image and implements a mechanism that allows the APU drivers to
52 * enumerate such devices (device personality and resource details) via
53 * a dedicated communication channel. RPU mediates operations such as
54 * discover, reset and rescan of the FPGA devices for the APU. This is
55 * done using memory mapped interface provided by the RPU to APU.
58 #include <linux/init.h>
59 #include <linux/kernel.h>
61 #include <linux/of_device.h>
62 #include <linux/of_platform.h>
63 #include <linux/platform_device.h>
64 #include <linux/slab.h>
66 #include <linux/idr.h>
67 #include <linux/cdx/cdx_bus.h>
68 #include <linux/iommu.h>
69 #include <linux/dma-map-ops.h>
70 #include <linux/debugfs.h>
73 /* Default DMA mask for devices on a CDX bus */
74 #define CDX_DEFAULT_DMA_MASK (~0ULL)
75 #define MAX_CDX_CONTROLLERS 16
77 /* IDA for CDX controllers registered with the CDX bus */
78 static DEFINE_IDA(cdx_controller_ida);
79 /* Lock to protect controller ops */
80 static DEFINE_MUTEX(cdx_controller_lock);
81 /* Debugfs dir for cdx bus */
82 static struct dentry *cdx_debugfs_dir;
84 static char *compat_node_name = "xlnx,versal-net-cdx";
86 static void cdx_destroy_res_attr(struct cdx_device *cdx_dev, int num);
89 * cdx_dev_reset - Reset a CDX device
92 * Return: -errno on failure, 0 on success.
94 int cdx_dev_reset(struct device *dev)
96 struct cdx_device *cdx_dev = to_cdx_device(dev);
97 struct cdx_controller *cdx = cdx_dev->cdx;
98 struct cdx_device_config dev_config = {0};
99 struct cdx_driver *cdx_drv;
102 cdx_drv = to_cdx_driver(dev->driver);
103 /* Notify driver that device is being reset */
104 if (cdx_drv && cdx_drv->reset_prepare)
105 cdx_drv->reset_prepare(cdx_dev);
107 dev_config.type = CDX_DEV_RESET_CONF;
108 ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num,
109 cdx_dev->dev_num, &dev_config);
111 dev_err(dev, "cdx device reset failed\n");
113 /* Notify driver that device reset is complete */
114 if (cdx_drv && cdx_drv->reset_done)
115 cdx_drv->reset_done(cdx_dev);
119 EXPORT_SYMBOL_GPL(cdx_dev_reset);
122 * reset_cdx_device - Reset a CDX device
124 * @data: This is always passed as NULL, and is not used in this API,
125 * but is required here as the device_for_each_child() API expects
126 * the passed function to have this as an argument.
128 * Return: -errno on failure, 0 on success.
130 static int reset_cdx_device(struct device *dev, void *data)
132 return cdx_dev_reset(dev);
136 * cdx_unregister_device - Unregister a CDX device
138 * @data: This is always passed as NULL, and is not used in this API,
139 * but is required here as the bus_for_each_dev() API expects
140 * the passed function (cdx_unregister_device) to have this
143 * Return: 0 on success.
145 static int cdx_unregister_device(struct device *dev,
148 struct cdx_device *cdx_dev = to_cdx_device(dev);
149 struct cdx_controller *cdx = cdx_dev->cdx;
151 if (cdx_dev->is_bus) {
152 device_for_each_child(dev, NULL, cdx_unregister_device);
153 if (cdx_dev->enabled && cdx->ops->bus_disable)
154 cdx->ops->bus_disable(cdx, cdx_dev->bus_num);
156 cdx_destroy_res_attr(cdx_dev, MAX_CDX_DEV_RESOURCES);
157 debugfs_remove_recursive(cdx_dev->debugfs_dir);
158 kfree(cdx_dev->driver_override);
159 cdx_dev->driver_override = NULL;
163 * Do not free cdx_dev here as it would be freed in
164 * cdx_device_release() called from within put_device().
166 device_del(&cdx_dev->dev);
167 put_device(&cdx_dev->dev);
172 static void cdx_unregister_devices(struct bus_type *bus)
174 /* Reset all the devices attached to cdx bus */
175 bus_for_each_dev(bus, NULL, NULL, cdx_unregister_device);
179 * cdx_match_one_device - Tell if a CDX device structure has a matching
180 * CDX device id structure
181 * @id: single CDX device id structure to match
182 * @dev: the CDX device structure to match against
184 * Return: matching cdx_device_id structure or NULL if there is no match.
186 static inline const struct cdx_device_id *
187 cdx_match_one_device(const struct cdx_device_id *id,
188 const struct cdx_device *dev)
190 /* Use vendor ID and device ID for matching */
191 if ((id->vendor == CDX_ANY_ID || id->vendor == dev->vendor) &&
192 (id->device == CDX_ANY_ID || id->device == dev->device) &&
193 (id->subvendor == CDX_ANY_ID || id->subvendor == dev->subsystem_vendor) &&
194 (id->subdevice == CDX_ANY_ID || id->subdevice == dev->subsystem_device) &&
195 !((id->class ^ dev->class) & id->class_mask))
201 * cdx_match_id - See if a CDX device matches a given cdx_id table
202 * @ids: array of CDX device ID structures to search in
203 * @dev: the CDX device structure to match against.
205 * Used by a driver to check whether a CDX device is in its list of
206 * supported devices. Returns the matching cdx_device_id structure or
207 * NULL if there is no match.
209 * Return: matching cdx_device_id structure or NULL if there is no match.
211 static inline const struct cdx_device_id *
212 cdx_match_id(const struct cdx_device_id *ids, struct cdx_device *dev)
215 while (ids->vendor || ids->device) {
216 if (cdx_match_one_device(ids, dev))
224 int cdx_set_master(struct cdx_device *cdx_dev)
226 struct cdx_controller *cdx = cdx_dev->cdx;
227 struct cdx_device_config dev_config;
228 int ret = -EOPNOTSUPP;
230 dev_config.type = CDX_DEV_BUS_MASTER_CONF;
231 dev_config.bus_master_enable = true;
232 if (cdx->ops->dev_configure)
233 ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num,
234 cdx_dev->dev_num, &dev_config);
238 EXPORT_SYMBOL_GPL(cdx_set_master);
240 int cdx_clear_master(struct cdx_device *cdx_dev)
242 struct cdx_controller *cdx = cdx_dev->cdx;
243 struct cdx_device_config dev_config;
244 int ret = -EOPNOTSUPP;
246 dev_config.type = CDX_DEV_BUS_MASTER_CONF;
247 dev_config.bus_master_enable = false;
248 if (cdx->ops->dev_configure)
249 ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num,
250 cdx_dev->dev_num, &dev_config);
254 EXPORT_SYMBOL_GPL(cdx_clear_master);
257 * cdx_bus_match - device to driver matching callback
258 * @dev: the cdx device to match against
259 * @drv: the device driver to search for matching cdx device
262 * Return: true on success, false otherwise.
264 static int cdx_bus_match(struct device *dev, struct device_driver *drv)
266 struct cdx_device *cdx_dev = to_cdx_device(dev);
267 struct cdx_driver *cdx_drv = to_cdx_driver(drv);
268 const struct cdx_device_id *found_id = NULL;
269 const struct cdx_device_id *ids;
274 ids = cdx_drv->match_id_table;
276 /* When driver_override is set, only bind to the matching driver */
277 if (cdx_dev->driver_override && strcmp(cdx_dev->driver_override, drv->name))
280 found_id = cdx_match_id(ids, cdx_dev);
286 * In case override_only was set, enforce driver_override
289 if (!found_id->override_only)
291 if (cdx_dev->driver_override)
295 found_id = cdx_match_id(ids, cdx_dev);
301 static int cdx_probe(struct device *dev)
303 struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
304 struct cdx_device *cdx_dev = to_cdx_device(dev);
307 error = cdx_drv->probe(cdx_dev);
309 dev_err_probe(dev, error, "%s failed\n", __func__);
316 static void cdx_remove(struct device *dev)
318 struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
319 struct cdx_device *cdx_dev = to_cdx_device(dev);
321 if (cdx_drv && cdx_drv->remove)
322 cdx_drv->remove(cdx_dev);
325 static void cdx_shutdown(struct device *dev)
327 struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
328 struct cdx_device *cdx_dev = to_cdx_device(dev);
330 if (cdx_drv && cdx_drv->shutdown)
331 cdx_drv->shutdown(cdx_dev);
334 static int cdx_dma_configure(struct device *dev)
336 struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
337 struct cdx_device *cdx_dev = to_cdx_device(dev);
338 struct cdx_controller *cdx = cdx_dev->cdx;
339 u32 input_id = cdx_dev->req_id;
342 ret = of_dma_configure_id(dev, cdx->dev->of_node, 0, &input_id);
343 if (ret && ret != -EPROBE_DEFER) {
344 dev_err(dev, "of_dma_configure_id() failed\n");
348 if (!ret && !cdx_drv->driver_managed_dma) {
349 ret = iommu_device_use_default_domain(dev);
351 arch_teardown_dma_ops(dev);
357 static void cdx_dma_cleanup(struct device *dev)
359 struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
361 if (!cdx_drv->driver_managed_dma)
362 iommu_device_unuse_default_domain(dev);
365 /* show configuration fields */
366 #define cdx_config_attr(field, format_string) \
368 field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
370 struct cdx_device *cdx_dev = to_cdx_device(dev); \
371 return sysfs_emit(buf, format_string, cdx_dev->field); \
373 static DEVICE_ATTR_RO(field)
375 cdx_config_attr(vendor, "0x%04x\n");
376 cdx_config_attr(device, "0x%04x\n");
377 cdx_config_attr(subsystem_vendor, "0x%04x\n");
378 cdx_config_attr(subsystem_device, "0x%04x\n");
379 cdx_config_attr(revision, "0x%02x\n");
380 cdx_config_attr(class, "0x%06x\n");
382 static ssize_t remove_store(struct device *dev,
383 struct device_attribute *attr,
384 const char *buf, size_t count)
388 if (kstrtobool(buf, &val) < 0)
394 if (device_remove_file_self(dev, attr)) {
397 ret = cdx_unregister_device(dev, NULL);
404 static DEVICE_ATTR_WO(remove);
406 static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
407 const char *buf, size_t count)
409 struct cdx_device *cdx_dev = to_cdx_device(dev);
413 if (kstrtobool(buf, &val) < 0)
420 /* Reset all the devices attached to cdx bus */
421 ret = device_for_each_child(dev, NULL, reset_cdx_device);
423 ret = cdx_dev_reset(dev);
425 return ret < 0 ? ret : count;
427 static DEVICE_ATTR_WO(reset);
429 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
432 struct cdx_device *cdx_dev = to_cdx_device(dev);
434 return sprintf(buf, "cdx:v%04Xd%04Xsv%04Xsd%04Xc%06X\n", cdx_dev->vendor,
435 cdx_dev->device, cdx_dev->subsystem_vendor, cdx_dev->subsystem_device,
438 static DEVICE_ATTR_RO(modalias);
440 static ssize_t driver_override_store(struct device *dev,
441 struct device_attribute *attr,
442 const char *buf, size_t count)
444 struct cdx_device *cdx_dev = to_cdx_device(dev);
447 if (WARN_ON(dev->bus != &cdx_bus_type))
450 ret = driver_set_override(dev, &cdx_dev->driver_override, buf, count);
457 static ssize_t driver_override_show(struct device *dev,
458 struct device_attribute *attr, char *buf)
460 struct cdx_device *cdx_dev = to_cdx_device(dev);
462 return sysfs_emit(buf, "%s\n", cdx_dev->driver_override);
464 static DEVICE_ATTR_RW(driver_override);
466 static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
467 const char *buf, size_t count)
469 struct cdx_device *cdx_dev = to_cdx_device(dev);
470 struct cdx_controller *cdx = cdx_dev->cdx;
474 if (kstrtobool(buf, &enable) < 0)
477 if (enable == cdx_dev->enabled)
480 if (enable && cdx->ops->bus_enable)
481 ret = cdx->ops->bus_enable(cdx, cdx_dev->bus_num);
482 else if (!enable && cdx->ops->bus_disable)
483 ret = cdx->ops->bus_disable(cdx, cdx_dev->bus_num);
488 cdx_dev->enabled = enable;
490 return ret < 0 ? ret : count;
493 static ssize_t enable_show(struct device *dev, struct device_attribute *attr, char *buf)
495 struct cdx_device *cdx_dev = to_cdx_device(dev);
497 return sysfs_emit(buf, "%u\n", cdx_dev->enabled);
499 static DEVICE_ATTR_RW(enable);
501 static umode_t cdx_dev_attrs_are_visible(struct kobject *kobj, struct attribute *a, int n)
503 struct device *dev = kobj_to_dev(kobj);
504 struct cdx_device *cdx_dev;
506 cdx_dev = to_cdx_device(dev);
507 if (!cdx_dev->is_bus)
513 static umode_t cdx_bus_attrs_are_visible(struct kobject *kobj, struct attribute *a, int n)
515 struct device *dev = kobj_to_dev(kobj);
516 struct cdx_device *cdx_dev;
518 cdx_dev = to_cdx_device(dev);
525 static struct attribute *cdx_dev_attrs[] = {
526 &dev_attr_remove.attr,
527 &dev_attr_reset.attr,
528 &dev_attr_vendor.attr,
529 &dev_attr_device.attr,
530 &dev_attr_subsystem_vendor.attr,
531 &dev_attr_subsystem_device.attr,
532 &dev_attr_class.attr,
533 &dev_attr_revision.attr,
534 &dev_attr_modalias.attr,
535 &dev_attr_driver_override.attr,
539 static const struct attribute_group cdx_dev_group = {
540 .attrs = cdx_dev_attrs,
541 .is_visible = cdx_dev_attrs_are_visible,
544 static struct attribute *cdx_bus_dev_attrs[] = {
545 &dev_attr_enable.attr,
546 &dev_attr_reset.attr,
550 static const struct attribute_group cdx_bus_dev_group = {
551 .attrs = cdx_bus_dev_attrs,
552 .is_visible = cdx_bus_attrs_are_visible,
555 static const struct attribute_group *cdx_dev_groups[] = {
561 static int cdx_debug_resource_show(struct seq_file *s, void *data)
563 struct cdx_device *cdx_dev = s->private;
566 for (i = 0; i < MAX_CDX_DEV_RESOURCES; i++) {
567 struct resource *res = &cdx_dev->res[i];
569 seq_printf(s, "%pr\n", res);
574 DEFINE_SHOW_ATTRIBUTE(cdx_debug_resource);
576 static void cdx_device_debugfs_init(struct cdx_device *cdx_dev)
578 cdx_dev->debugfs_dir = debugfs_create_dir(dev_name(&cdx_dev->dev), cdx_debugfs_dir);
579 if (IS_ERR(cdx_dev->debugfs_dir))
582 debugfs_create_file("resource", 0444, cdx_dev->debugfs_dir, cdx_dev,
583 &cdx_debug_resource_fops);
586 static ssize_t rescan_store(const struct bus_type *bus,
587 const char *buf, size_t count)
589 struct cdx_controller *cdx;
590 struct platform_device *pd;
591 struct device_node *np;
594 if (kstrtobool(buf, &val) < 0)
600 mutex_lock(&cdx_controller_lock);
602 /* Unregister all the devices on the bus */
603 cdx_unregister_devices(&cdx_bus_type);
605 /* Rescan all the devices */
606 for_each_compatible_node(np, NULL, compat_node_name) {
607 pd = of_find_device_by_node(np);
614 cdx = platform_get_drvdata(pd);
615 if (cdx && cdx->controller_registered && cdx->ops->scan)
618 put_device(&pd->dev);
622 mutex_unlock(&cdx_controller_lock);
626 static BUS_ATTR_WO(rescan);
628 static struct attribute *cdx_bus_attrs[] = {
629 &bus_attr_rescan.attr,
632 ATTRIBUTE_GROUPS(cdx_bus);
634 struct bus_type cdx_bus_type = {
636 .match = cdx_bus_match,
638 .remove = cdx_remove,
639 .shutdown = cdx_shutdown,
640 .dma_configure = cdx_dma_configure,
641 .dma_cleanup = cdx_dma_cleanup,
642 .bus_groups = cdx_bus_groups,
643 .dev_groups = cdx_dev_groups,
645 EXPORT_SYMBOL_GPL(cdx_bus_type);
647 int __cdx_driver_register(struct cdx_driver *cdx_driver,
648 struct module *owner)
652 cdx_driver->driver.owner = owner;
653 cdx_driver->driver.bus = &cdx_bus_type;
655 error = driver_register(&cdx_driver->driver);
657 pr_err("driver_register() failed for %s: %d\n",
658 cdx_driver->driver.name, error);
664 EXPORT_SYMBOL_GPL(__cdx_driver_register);
666 void cdx_driver_unregister(struct cdx_driver *cdx_driver)
668 driver_unregister(&cdx_driver->driver);
670 EXPORT_SYMBOL_GPL(cdx_driver_unregister);
672 static void cdx_device_release(struct device *dev)
674 struct cdx_device *cdx_dev = to_cdx_device(dev);
679 static const struct vm_operations_struct cdx_phys_vm_ops = {
680 #ifdef CONFIG_HAVE_IOREMAP_PROT
681 .access = generic_access_phys,
686 * cdx_mmap_resource - map a CDX resource into user memory space
687 * @fp: File pointer. Not used in this function, but required where
688 * this API is registered as a callback.
689 * @kobj: kobject for mapping
690 * @attr: struct bin_attribute for the file being mapped
691 * @vma: struct vm_area_struct passed into the mmap
693 * Use the regular CDX mapping routines to map a CDX resource into userspace.
695 * Return: true on success, false otherwise.
697 static int cdx_mmap_resource(struct file *fp, struct kobject *kobj,
698 struct bin_attribute *attr,
699 struct vm_area_struct *vma)
701 struct cdx_device *cdx_dev = to_cdx_device(kobj_to_dev(kobj));
702 int num = (unsigned long)attr->private;
703 struct resource *res;
706 res = &cdx_dev->res[num];
707 if (iomem_is_exclusive(res->start))
710 /* Make sure the caller is mapping a valid resource for this device */
711 size = ((cdx_resource_len(cdx_dev, num) - 1) >> PAGE_SHIFT) + 1;
712 if (vma->vm_pgoff + vma_pages(vma) > size)
716 * Map memory region and vm->vm_pgoff is expected to be an
717 * offset within that region.
719 vma->vm_page_prot = pgprot_device(vma->vm_page_prot);
720 vma->vm_pgoff += (cdx_resource_start(cdx_dev, num) >> PAGE_SHIFT);
721 vma->vm_ops = &cdx_phys_vm_ops;
722 return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
723 vma->vm_end - vma->vm_start,
727 static void cdx_destroy_res_attr(struct cdx_device *cdx_dev, int num)
731 /* removing the bin attributes */
732 for (i = 0; i < num; i++) {
733 struct bin_attribute *res_attr;
735 res_attr = cdx_dev->res_attr[i];
737 sysfs_remove_bin_file(&cdx_dev->dev.kobj, res_attr);
743 #define CDX_RES_ATTR_NAME_LEN 10
744 static int cdx_create_res_attr(struct cdx_device *cdx_dev, int num)
746 struct bin_attribute *res_attr;
750 res_attr = kzalloc(sizeof(*res_attr) + CDX_RES_ATTR_NAME_LEN, GFP_ATOMIC);
754 res_attr_name = (char *)(res_attr + 1);
756 sysfs_bin_attr_init(res_attr);
758 cdx_dev->res_attr[num] = res_attr;
759 sprintf(res_attr_name, "resource%d", num);
761 res_attr->mmap = cdx_mmap_resource;
762 res_attr->attr.name = res_attr_name;
763 res_attr->attr.mode = 0600;
764 res_attr->size = cdx_resource_len(cdx_dev, num);
765 res_attr->private = (void *)(unsigned long)num;
766 ret = sysfs_create_bin_file(&cdx_dev->dev.kobj, res_attr);
773 int cdx_device_add(struct cdx_dev_params *dev_params)
775 struct cdx_controller *cdx = dev_params->cdx;
776 struct cdx_device *cdx_dev;
779 cdx_dev = kzalloc(sizeof(*cdx_dev), GFP_KERNEL);
783 /* Populate resource */
784 memcpy(cdx_dev->res, dev_params->res, sizeof(struct resource) *
785 dev_params->res_count);
786 cdx_dev->res_count = dev_params->res_count;
788 /* Populate CDX dev params */
789 cdx_dev->req_id = dev_params->req_id;
790 cdx_dev->vendor = dev_params->vendor;
791 cdx_dev->device = dev_params->device;
792 cdx_dev->subsystem_vendor = dev_params->subsys_vendor;
793 cdx_dev->subsystem_device = dev_params->subsys_device;
794 cdx_dev->class = dev_params->class;
795 cdx_dev->revision = dev_params->revision;
796 cdx_dev->bus_num = dev_params->bus_num;
797 cdx_dev->dev_num = dev_params->dev_num;
798 cdx_dev->cdx = dev_params->cdx;
799 cdx_dev->dma_mask = CDX_DEFAULT_DMA_MASK;
801 /* Initialize generic device */
802 device_initialize(&cdx_dev->dev);
803 cdx_dev->dev.parent = dev_params->parent;
804 cdx_dev->dev.bus = &cdx_bus_type;
805 cdx_dev->dev.dma_mask = &cdx_dev->dma_mask;
806 cdx_dev->dev.release = cdx_device_release;
809 dev_set_name(&cdx_dev->dev, "cdx-%02x:%02x",
810 ((cdx->id << CDX_CONTROLLER_ID_SHIFT) | (cdx_dev->bus_num & CDX_BUS_NUM_MASK)),
813 ret = device_add(&cdx_dev->dev);
815 dev_err(&cdx_dev->dev,
816 "cdx device add failed: %d", ret);
820 /* Create resource<N> attributes */
821 for (i = 0; i < MAX_CDX_DEV_RESOURCES; i++) {
822 if (cdx_resource_flags(cdx_dev, i) & IORESOURCE_MEM) {
823 /* skip empty resources */
824 if (!cdx_resource_len(cdx_dev, i))
827 ret = cdx_create_res_attr(cdx_dev, i);
829 dev_err(&cdx_dev->dev,
830 "cdx device resource<%d> file creation failed: %d", i, ret);
831 goto resource_create_fail;
836 cdx_device_debugfs_init(cdx_dev);
839 resource_create_fail:
840 cdx_destroy_res_attr(cdx_dev, i);
841 device_del(&cdx_dev->dev);
844 * Do not free cdx_dev here as it would be freed in
845 * cdx_device_release() called from put_device().
847 put_device(&cdx_dev->dev);
851 EXPORT_SYMBOL_NS_GPL(cdx_device_add, CDX_BUS_CONTROLLER);
853 struct device *cdx_bus_add(struct cdx_controller *cdx, u8 bus_num)
855 struct cdx_device *cdx_dev;
858 cdx_dev = kzalloc(sizeof(*cdx_dev), GFP_KERNEL);
862 device_initialize(&cdx_dev->dev);
865 cdx_dev->dev.parent = cdx->dev;
866 cdx_dev->dev.bus = &cdx_bus_type;
867 cdx_dev->dev.release = cdx_device_release;
868 cdx_dev->is_bus = true;
869 cdx_dev->bus_num = bus_num;
871 dev_set_name(&cdx_dev->dev, "cdx-%02x",
872 ((cdx->id << CDX_CONTROLLER_ID_SHIFT) | (bus_num & CDX_BUS_NUM_MASK)));
874 ret = device_add(&cdx_dev->dev);
876 dev_err(&cdx_dev->dev, "cdx bus device add failed: %d\n", ret);
877 goto device_add_fail;
880 if (cdx->ops->bus_enable) {
881 ret = cdx->ops->bus_enable(cdx, bus_num);
882 if (ret && ret != -EALREADY) {
883 dev_err(cdx->dev, "cdx bus enable failed: %d\n", ret);
884 goto bus_enable_fail;
888 cdx_dev->enabled = true;
889 return &cdx_dev->dev;
892 device_del(&cdx_dev->dev);
894 put_device(&cdx_dev->dev);
898 EXPORT_SYMBOL_NS_GPL(cdx_bus_add, CDX_BUS_CONTROLLER);
900 int cdx_register_controller(struct cdx_controller *cdx)
904 ret = ida_alloc_range(&cdx_controller_ida, 0, MAX_CDX_CONTROLLERS - 1, GFP_KERNEL);
907 "No free index available. Maximum controllers already registered\n");
908 cdx->id = (u8)MAX_CDX_CONTROLLERS;
912 mutex_lock(&cdx_controller_lock);
915 /* Scan all the devices */
918 cdx->controller_registered = true;
919 mutex_unlock(&cdx_controller_lock);
923 EXPORT_SYMBOL_NS_GPL(cdx_register_controller, CDX_BUS_CONTROLLER);
925 void cdx_unregister_controller(struct cdx_controller *cdx)
927 if (cdx->id >= MAX_CDX_CONTROLLERS)
930 mutex_lock(&cdx_controller_lock);
932 cdx->controller_registered = false;
933 device_for_each_child(cdx->dev, NULL, cdx_unregister_device);
934 ida_free(&cdx_controller_ida, cdx->id);
936 mutex_unlock(&cdx_controller_lock);
938 EXPORT_SYMBOL_NS_GPL(cdx_unregister_controller, CDX_BUS_CONTROLLER);
940 static int __init cdx_bus_init(void)
944 ret = bus_register(&cdx_bus_type);
946 cdx_debugfs_dir = debugfs_create_dir(cdx_bus_type.name, NULL);
950 postcore_initcall(cdx_bus_init);