Merge tag 'nfsd-6.7-2' of git://git.kernel.org/pub/scm/linux/kernel/git/cel/linux
[sfrench/cifs-2.6.git] / drivers / cdx / cdx.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * CDX bus driver.
4  *
5  * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
6  */
7
8 /*
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).
14  *
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.
18  *
19  * The diagram below shows an overview of the CDX architecture:
20  *
21  *          +--------------------------------------+
22  *          |    Application CPUs (APU)            |
23  *          |                                      |
24  *          |                    CDX device drivers|
25  *          |     Linux OS                |        |
26  *          |                        CDX bus       |
27  *          |                             |        |
28  *          |                     CDX controller   |
29  *          |                             |        |
30  *          +-----------------------------|--------+
31  *                                        | (discover, config,
32  *                                        |  reset, rescan)
33  *                                        |
34  *          +------------------------| RPU if |----+
35  *          |                             |        |
36  *          |                             V        |
37  *          |          Realtime CPUs (RPU)         |
38  *          |                                      |
39  *          +--------------------------------------+
40  *                                |
41  *          +---------------------|----------------+
42  *          |  FPGA               |                |
43  *          |      +-----------------------+       |
44  *          |      |           |           |       |
45  *          | +-------+    +-------+   +-------+   |
46  *          | | dev 1 |    | dev 2 |   | dev 3 |   |
47  *          | +-------+    +-------+   +-------+   |
48  *          +--------------------------------------+
49  *
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.
56  */
57
58 #include <linux/init.h>
59 #include <linux/kernel.h>
60 #include <linux/of_device.h>
61 #include <linux/slab.h>
62 #include <linux/mm.h>
63 #include <linux/idr.h>
64 #include <linux/cdx/cdx_bus.h>
65 #include <linux/iommu.h>
66 #include <linux/dma-map-ops.h>
67 #include "cdx.h"
68
69 /* Default DMA mask for devices on a CDX bus */
70 #define CDX_DEFAULT_DMA_MASK    (~0ULL)
71 #define MAX_CDX_CONTROLLERS 16
72
73 /* IDA for CDX controllers registered with the CDX bus */
74 static DEFINE_IDA(cdx_controller_ida);
75 /* Lock to protect controller ops */
76 static DEFINE_MUTEX(cdx_controller_lock);
77
78 static char *compat_node_name = "xlnx,versal-net-cdx";
79
80 /**
81  * cdx_dev_reset - Reset a CDX device
82  * @dev: CDX device
83  *
84  * Return: -errno on failure, 0 on success.
85  */
86 int cdx_dev_reset(struct device *dev)
87 {
88         struct cdx_device *cdx_dev = to_cdx_device(dev);
89         struct cdx_controller *cdx = cdx_dev->cdx;
90         struct cdx_device_config dev_config = {0};
91         struct cdx_driver *cdx_drv;
92         int ret;
93
94         cdx_drv = to_cdx_driver(dev->driver);
95         /* Notify driver that device is being reset */
96         if (cdx_drv && cdx_drv->reset_prepare)
97                 cdx_drv->reset_prepare(cdx_dev);
98
99         dev_config.type = CDX_DEV_RESET_CONF;
100         ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num,
101                                       cdx_dev->dev_num, &dev_config);
102         if (ret)
103                 dev_err(dev, "cdx device reset failed\n");
104
105         /* Notify driver that device reset is complete */
106         if (cdx_drv && cdx_drv->reset_done)
107                 cdx_drv->reset_done(cdx_dev);
108
109         return ret;
110 }
111 EXPORT_SYMBOL_GPL(cdx_dev_reset);
112
113 /**
114  * reset_cdx_device - Reset a CDX device
115  * @dev: CDX device
116  * @data: This is always passed as NULL, and is not used in this API,
117  *    but is required here as the device_for_each_child() API expects
118  *    the passed function to have this as an argument.
119  *
120  * Return: -errno on failure, 0 on success.
121  */
122 static int reset_cdx_device(struct device *dev, void *data)
123 {
124         return cdx_dev_reset(dev);
125 }
126
127 /**
128  * cdx_unregister_device - Unregister a CDX device
129  * @dev: CDX device
130  * @data: This is always passed as NULL, and is not used in this API,
131  *        but is required here as the bus_for_each_dev() API expects
132  *        the passed function (cdx_unregister_device) to have this
133  *        as an argument.
134  *
135  * Return: 0 on success.
136  */
137 static int cdx_unregister_device(struct device *dev,
138                                  void *data)
139 {
140         struct cdx_device *cdx_dev = to_cdx_device(dev);
141         struct cdx_controller *cdx = cdx_dev->cdx;
142
143         if (cdx_dev->is_bus) {
144                 device_for_each_child(dev, NULL, cdx_unregister_device);
145                 if (cdx_dev->enabled && cdx->ops->bus_disable)
146                         cdx->ops->bus_disable(cdx, cdx_dev->bus_num);
147         } else {
148                 kfree(cdx_dev->driver_override);
149                 cdx_dev->driver_override = NULL;
150         }
151
152         /*
153          * Do not free cdx_dev here as it would be freed in
154          * cdx_device_release() called from within put_device().
155          */
156         device_del(&cdx_dev->dev);
157         put_device(&cdx_dev->dev);
158
159         return 0;
160 }
161
162 static void cdx_unregister_devices(struct bus_type *bus)
163 {
164         /* Reset all the devices attached to cdx bus */
165         bus_for_each_dev(bus, NULL, NULL, cdx_unregister_device);
166 }
167
168 /**
169  * cdx_match_one_device - Tell if a CDX device structure has a matching
170  *                        CDX device id structure
171  * @id: single CDX device id structure to match
172  * @dev: the CDX device structure to match against
173  *
174  * Return: matching cdx_device_id structure or NULL if there is no match.
175  */
176 static inline const struct cdx_device_id *
177 cdx_match_one_device(const struct cdx_device_id *id,
178                      const struct cdx_device *dev)
179 {
180         /* Use vendor ID and device ID for matching */
181         if ((id->vendor == CDX_ANY_ID || id->vendor == dev->vendor) &&
182             (id->device == CDX_ANY_ID || id->device == dev->device) &&
183             (id->subvendor == CDX_ANY_ID || id->subvendor == dev->subsystem_vendor) &&
184             (id->subdevice == CDX_ANY_ID || id->subdevice == dev->subsystem_device) &&
185             !((id->class ^ dev->class) & id->class_mask))
186                 return id;
187         return NULL;
188 }
189
190 /**
191  * cdx_match_id - See if a CDX device matches a given cdx_id table
192  * @ids: array of CDX device ID structures to search in
193  * @dev: the CDX device structure to match against.
194  *
195  * Used by a driver to check whether a CDX device is in its list of
196  * supported devices. Returns the matching cdx_device_id structure or
197  * NULL if there is no match.
198  *
199  * Return: matching cdx_device_id structure or NULL if there is no match.
200  */
201 static inline const struct cdx_device_id *
202 cdx_match_id(const struct cdx_device_id *ids, struct cdx_device *dev)
203 {
204         if (ids) {
205                 while (ids->vendor || ids->device) {
206                         if (cdx_match_one_device(ids, dev))
207                                 return ids;
208                         ids++;
209                 }
210         }
211         return NULL;
212 }
213
214 int cdx_set_master(struct cdx_device *cdx_dev)
215 {
216         struct cdx_controller *cdx = cdx_dev->cdx;
217         struct cdx_device_config dev_config;
218         int ret = -EOPNOTSUPP;
219
220         dev_config.type = CDX_DEV_BUS_MASTER_CONF;
221         dev_config.bus_master_enable = true;
222         if (cdx->ops->dev_configure)
223                 ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num,
224                                               cdx_dev->dev_num, &dev_config);
225
226         return ret;
227 }
228 EXPORT_SYMBOL_GPL(cdx_set_master);
229
230 int cdx_clear_master(struct cdx_device *cdx_dev)
231 {
232         struct cdx_controller *cdx = cdx_dev->cdx;
233         struct cdx_device_config dev_config;
234         int ret = -EOPNOTSUPP;
235
236         dev_config.type = CDX_DEV_BUS_MASTER_CONF;
237         dev_config.bus_master_enable = false;
238         if (cdx->ops->dev_configure)
239                 ret = cdx->ops->dev_configure(cdx, cdx_dev->bus_num,
240                                               cdx_dev->dev_num, &dev_config);
241
242         return ret;
243 }
244 EXPORT_SYMBOL_GPL(cdx_clear_master);
245
246 /**
247  * cdx_bus_match - device to driver matching callback
248  * @dev: the cdx device to match against
249  * @drv: the device driver to search for matching cdx device
250  * structures
251  *
252  * Return: true on success, false otherwise.
253  */
254 static int cdx_bus_match(struct device *dev, struct device_driver *drv)
255 {
256         struct cdx_device *cdx_dev = to_cdx_device(dev);
257         struct cdx_driver *cdx_drv = to_cdx_driver(drv);
258         const struct cdx_device_id *found_id = NULL;
259         const struct cdx_device_id *ids;
260
261         if (cdx_dev->is_bus)
262                 return false;
263
264         ids = cdx_drv->match_id_table;
265
266         /* When driver_override is set, only bind to the matching driver */
267         if (cdx_dev->driver_override && strcmp(cdx_dev->driver_override, drv->name))
268                 return false;
269
270         found_id = cdx_match_id(ids, cdx_dev);
271         if (!found_id)
272                 return false;
273
274         do {
275                 /*
276                  * In case override_only was set, enforce driver_override
277                  * matching.
278                  */
279                 if (!found_id->override_only)
280                         return true;
281                 if (cdx_dev->driver_override)
282                         return true;
283
284                 ids = found_id + 1;
285                 found_id = cdx_match_id(ids, cdx_dev);
286         } while (found_id);
287
288         return false;
289 }
290
291 static int cdx_probe(struct device *dev)
292 {
293         struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
294         struct cdx_device *cdx_dev = to_cdx_device(dev);
295         int error;
296
297         error = cdx_drv->probe(cdx_dev);
298         if (error) {
299                 dev_err_probe(dev, error, "%s failed\n", __func__);
300                 return error;
301         }
302
303         return 0;
304 }
305
306 static void cdx_remove(struct device *dev)
307 {
308         struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
309         struct cdx_device *cdx_dev = to_cdx_device(dev);
310
311         if (cdx_drv && cdx_drv->remove)
312                 cdx_drv->remove(cdx_dev);
313 }
314
315 static void cdx_shutdown(struct device *dev)
316 {
317         struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
318         struct cdx_device *cdx_dev = to_cdx_device(dev);
319
320         if (cdx_drv && cdx_drv->shutdown)
321                 cdx_drv->shutdown(cdx_dev);
322 }
323
324 static int cdx_dma_configure(struct device *dev)
325 {
326         struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
327         struct cdx_device *cdx_dev = to_cdx_device(dev);
328         struct cdx_controller *cdx = cdx_dev->cdx;
329         u32 input_id = cdx_dev->req_id;
330         int ret;
331
332         ret = of_dma_configure_id(dev, cdx->dev->of_node, 0, &input_id);
333         if (ret && ret != -EPROBE_DEFER) {
334                 dev_err(dev, "of_dma_configure_id() failed\n");
335                 return ret;
336         }
337
338         if (!ret && !cdx_drv->driver_managed_dma) {
339                 ret = iommu_device_use_default_domain(dev);
340                 if (ret)
341                         arch_teardown_dma_ops(dev);
342         }
343
344         return 0;
345 }
346
347 static void cdx_dma_cleanup(struct device *dev)
348 {
349         struct cdx_driver *cdx_drv = to_cdx_driver(dev->driver);
350
351         if (!cdx_drv->driver_managed_dma)
352                 iommu_device_unuse_default_domain(dev);
353 }
354
355 /* show configuration fields */
356 #define cdx_config_attr(field, format_string)   \
357 static ssize_t  \
358 field##_show(struct device *dev, struct device_attribute *attr, char *buf)      \
359 {       \
360         struct cdx_device *cdx_dev = to_cdx_device(dev);        \
361         return sysfs_emit(buf, format_string, cdx_dev->field);  \
362 }       \
363 static DEVICE_ATTR_RO(field)
364
365 cdx_config_attr(vendor, "0x%04x\n");
366 cdx_config_attr(device, "0x%04x\n");
367 cdx_config_attr(subsystem_vendor, "0x%04x\n");
368 cdx_config_attr(subsystem_device, "0x%04x\n");
369 cdx_config_attr(revision, "0x%02x\n");
370 cdx_config_attr(class, "0x%06x\n");
371
372 static ssize_t remove_store(struct device *dev,
373                             struct device_attribute *attr,
374                             const char *buf, size_t count)
375 {
376         bool val;
377
378         if (kstrtobool(buf, &val) < 0)
379                 return -EINVAL;
380
381         if (!val)
382                 return -EINVAL;
383
384         if (device_remove_file_self(dev, attr)) {
385                 int ret;
386
387                 ret = cdx_unregister_device(dev, NULL);
388                 if (ret)
389                         return ret;
390         }
391
392         return count;
393 }
394 static DEVICE_ATTR_WO(remove);
395
396 static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
397                            const char *buf, size_t count)
398 {
399         struct cdx_device *cdx_dev = to_cdx_device(dev);
400         bool val;
401         int ret;
402
403         if (kstrtobool(buf, &val) < 0)
404                 return -EINVAL;
405
406         if (!val)
407                 return -EINVAL;
408
409         if (cdx_dev->is_bus)
410                 /* Reset all the devices attached to cdx bus */
411                 ret = device_for_each_child(dev, NULL, reset_cdx_device);
412         else
413                 ret = cdx_dev_reset(dev);
414
415         return ret < 0 ? ret : count;
416 }
417 static DEVICE_ATTR_WO(reset);
418
419 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
420                              char *buf)
421 {
422         struct cdx_device *cdx_dev = to_cdx_device(dev);
423
424         return sprintf(buf, "cdx:v%04Xd%04Xsv%04Xsd%04Xc%06X\n", cdx_dev->vendor,
425                         cdx_dev->device, cdx_dev->subsystem_vendor, cdx_dev->subsystem_device,
426                         cdx_dev->class);
427 }
428 static DEVICE_ATTR_RO(modalias);
429
430 static ssize_t driver_override_store(struct device *dev,
431                                      struct device_attribute *attr,
432                                      const char *buf, size_t count)
433 {
434         struct cdx_device *cdx_dev = to_cdx_device(dev);
435         int ret;
436
437         if (WARN_ON(dev->bus != &cdx_bus_type))
438                 return -EINVAL;
439
440         ret = driver_set_override(dev, &cdx_dev->driver_override, buf, count);
441         if (ret)
442                 return ret;
443
444         return count;
445 }
446
447 static ssize_t driver_override_show(struct device *dev,
448                                     struct device_attribute *attr, char *buf)
449 {
450         struct cdx_device *cdx_dev = to_cdx_device(dev);
451
452         return sysfs_emit(buf, "%s\n", cdx_dev->driver_override);
453 }
454 static DEVICE_ATTR_RW(driver_override);
455
456 static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
457                             const char *buf, size_t count)
458 {
459         struct cdx_device *cdx_dev = to_cdx_device(dev);
460         struct cdx_controller *cdx = cdx_dev->cdx;
461         bool enable;
462         int ret;
463
464         if (kstrtobool(buf, &enable) < 0)
465                 return -EINVAL;
466
467         if (enable == cdx_dev->enabled)
468                 return count;
469
470         if (enable && cdx->ops->bus_enable)
471                 ret = cdx->ops->bus_enable(cdx, cdx_dev->bus_num);
472         else if (!enable && cdx->ops->bus_disable)
473                 ret = cdx->ops->bus_disable(cdx, cdx_dev->bus_num);
474         else
475                 ret = -EOPNOTSUPP;
476
477         if (!ret)
478                 cdx_dev->enabled = enable;
479
480         return ret < 0 ? ret : count;
481 }
482
483 static ssize_t enable_show(struct device *dev, struct device_attribute *attr, char *buf)
484 {
485         struct cdx_device *cdx_dev = to_cdx_device(dev);
486
487         return sysfs_emit(buf, "%u\n", cdx_dev->enabled);
488 }
489 static DEVICE_ATTR_RW(enable);
490
491 static umode_t cdx_dev_attrs_are_visible(struct kobject *kobj, struct attribute *a, int n)
492 {
493         struct device *dev = kobj_to_dev(kobj);
494         struct cdx_device *cdx_dev;
495
496         cdx_dev = to_cdx_device(dev);
497         if (!cdx_dev->is_bus)
498                 return a->mode;
499
500         return 0;
501 }
502
503 static umode_t cdx_bus_attrs_are_visible(struct kobject *kobj, struct attribute *a, int n)
504 {
505         struct device *dev = kobj_to_dev(kobj);
506         struct cdx_device *cdx_dev;
507
508         cdx_dev = to_cdx_device(dev);
509         if (cdx_dev->is_bus)
510                 return a->mode;
511
512         return 0;
513 }
514
515 static struct attribute *cdx_dev_attrs[] = {
516         &dev_attr_remove.attr,
517         &dev_attr_reset.attr,
518         &dev_attr_vendor.attr,
519         &dev_attr_device.attr,
520         &dev_attr_subsystem_vendor.attr,
521         &dev_attr_subsystem_device.attr,
522         &dev_attr_class.attr,
523         &dev_attr_revision.attr,
524         &dev_attr_modalias.attr,
525         &dev_attr_driver_override.attr,
526         NULL,
527 };
528
529 static const struct attribute_group cdx_dev_group = {
530         .attrs = cdx_dev_attrs,
531         .is_visible = cdx_dev_attrs_are_visible,
532 };
533
534 static struct attribute *cdx_bus_dev_attrs[] = {
535         &dev_attr_enable.attr,
536         &dev_attr_reset.attr,
537         NULL,
538 };
539
540 static const struct attribute_group cdx_bus_dev_group = {
541         .attrs = cdx_bus_dev_attrs,
542         .is_visible = cdx_bus_attrs_are_visible,
543 };
544
545 static const struct attribute_group *cdx_dev_groups[] = {
546         &cdx_dev_group,
547         &cdx_bus_dev_group,
548         NULL,
549 };
550
551 static ssize_t rescan_store(const struct bus_type *bus,
552                             const char *buf, size_t count)
553 {
554         struct cdx_controller *cdx;
555         struct platform_device *pd;
556         struct device_node *np;
557         bool val;
558
559         if (kstrtobool(buf, &val) < 0)
560                 return -EINVAL;
561
562         if (!val)
563                 return -EINVAL;
564
565         mutex_lock(&cdx_controller_lock);
566
567         /* Unregister all the devices on the bus */
568         cdx_unregister_devices(&cdx_bus_type);
569
570         /* Rescan all the devices */
571         for_each_compatible_node(np, NULL, compat_node_name) {
572                 if (!np)
573                         return -EINVAL;
574
575                 pd = of_find_device_by_node(np);
576                 if (!pd)
577                         return -EINVAL;
578
579                 cdx = platform_get_drvdata(pd);
580                 if (cdx && cdx->controller_registered && cdx->ops->scan)
581                         cdx->ops->scan(cdx);
582
583                 put_device(&pd->dev);
584         }
585
586         mutex_unlock(&cdx_controller_lock);
587
588         return count;
589 }
590 static BUS_ATTR_WO(rescan);
591
592 static struct attribute *cdx_bus_attrs[] = {
593         &bus_attr_rescan.attr,
594         NULL,
595 };
596 ATTRIBUTE_GROUPS(cdx_bus);
597
598 struct bus_type cdx_bus_type = {
599         .name           = "cdx",
600         .match          = cdx_bus_match,
601         .probe          = cdx_probe,
602         .remove         = cdx_remove,
603         .shutdown       = cdx_shutdown,
604         .dma_configure  = cdx_dma_configure,
605         .dma_cleanup    = cdx_dma_cleanup,
606         .bus_groups     = cdx_bus_groups,
607         .dev_groups     = cdx_dev_groups,
608 };
609 EXPORT_SYMBOL_GPL(cdx_bus_type);
610
611 int __cdx_driver_register(struct cdx_driver *cdx_driver,
612                           struct module *owner)
613 {
614         int error;
615
616         cdx_driver->driver.owner = owner;
617         cdx_driver->driver.bus = &cdx_bus_type;
618
619         error = driver_register(&cdx_driver->driver);
620         if (error) {
621                 pr_err("driver_register() failed for %s: %d\n",
622                        cdx_driver->driver.name, error);
623                 return error;
624         }
625
626         return 0;
627 }
628 EXPORT_SYMBOL_GPL(__cdx_driver_register);
629
630 void cdx_driver_unregister(struct cdx_driver *cdx_driver)
631 {
632         driver_unregister(&cdx_driver->driver);
633 }
634 EXPORT_SYMBOL_GPL(cdx_driver_unregister);
635
636 static void cdx_device_release(struct device *dev)
637 {
638         struct cdx_device *cdx_dev = to_cdx_device(dev);
639
640         kfree(cdx_dev);
641 }
642
643 int cdx_device_add(struct cdx_dev_params *dev_params)
644 {
645         struct cdx_controller *cdx = dev_params->cdx;
646         struct cdx_device *cdx_dev;
647         int ret;
648
649         cdx_dev = kzalloc(sizeof(*cdx_dev), GFP_KERNEL);
650         if (!cdx_dev)
651                 return -ENOMEM;
652
653         /* Populate resource */
654         memcpy(cdx_dev->res, dev_params->res, sizeof(struct resource) *
655                 dev_params->res_count);
656         cdx_dev->res_count = dev_params->res_count;
657
658         /* Populate CDX dev params */
659         cdx_dev->req_id = dev_params->req_id;
660         cdx_dev->vendor = dev_params->vendor;
661         cdx_dev->device = dev_params->device;
662         cdx_dev->subsystem_vendor = dev_params->subsys_vendor;
663         cdx_dev->subsystem_device = dev_params->subsys_device;
664         cdx_dev->class = dev_params->class;
665         cdx_dev->revision = dev_params->revision;
666         cdx_dev->bus_num = dev_params->bus_num;
667         cdx_dev->dev_num = dev_params->dev_num;
668         cdx_dev->cdx = dev_params->cdx;
669         cdx_dev->dma_mask = CDX_DEFAULT_DMA_MASK;
670
671         /* Initialize generic device */
672         device_initialize(&cdx_dev->dev);
673         cdx_dev->dev.parent = dev_params->parent;
674         cdx_dev->dev.bus = &cdx_bus_type;
675         cdx_dev->dev.dma_mask = &cdx_dev->dma_mask;
676         cdx_dev->dev.release = cdx_device_release;
677
678         /* Set Name */
679         dev_set_name(&cdx_dev->dev, "cdx-%02x:%02x",
680                      ((cdx->id << CDX_CONTROLLER_ID_SHIFT) | (cdx_dev->bus_num & CDX_BUS_NUM_MASK)),
681                      cdx_dev->dev_num);
682
683         ret = device_add(&cdx_dev->dev);
684         if (ret) {
685                 dev_err(&cdx_dev->dev,
686                         "cdx device add failed: %d", ret);
687                 goto fail;
688         }
689
690         return 0;
691 fail:
692         /*
693          * Do not free cdx_dev here as it would be freed in
694          * cdx_device_release() called from put_device().
695          */
696         put_device(&cdx_dev->dev);
697
698         return ret;
699 }
700 EXPORT_SYMBOL_NS_GPL(cdx_device_add, CDX_BUS_CONTROLLER);
701
702 struct device *cdx_bus_add(struct cdx_controller *cdx, u8 bus_num)
703 {
704         struct cdx_device *cdx_dev;
705         int ret;
706
707         cdx_dev = kzalloc(sizeof(*cdx_dev), GFP_KERNEL);
708         if (!cdx_dev)
709                 return NULL;
710
711         device_initialize(&cdx_dev->dev);
712         cdx_dev->cdx = cdx;
713
714         cdx_dev->dev.parent = cdx->dev;
715         cdx_dev->dev.bus = &cdx_bus_type;
716         cdx_dev->dev.release = cdx_device_release;
717         cdx_dev->is_bus = true;
718         cdx_dev->bus_num = bus_num;
719
720         dev_set_name(&cdx_dev->dev, "cdx-%02x",
721                      ((cdx->id << CDX_CONTROLLER_ID_SHIFT) | (bus_num & CDX_BUS_NUM_MASK)));
722
723         ret = device_add(&cdx_dev->dev);
724         if (ret) {
725                 dev_err(&cdx_dev->dev, "cdx bus device add failed: %d\n", ret);
726                 goto device_add_fail;
727         }
728
729         if (cdx->ops->bus_enable) {
730                 ret = cdx->ops->bus_enable(cdx, bus_num);
731                 if (ret && ret != -EALREADY) {
732                         dev_err(cdx->dev, "cdx bus enable failed: %d\n", ret);
733                         goto bus_enable_fail;
734                 }
735         }
736
737         cdx_dev->enabled = true;
738         return &cdx_dev->dev;
739
740 bus_enable_fail:
741         device_del(&cdx_dev->dev);
742 device_add_fail:
743         put_device(&cdx_dev->dev);
744
745         return NULL;
746 }
747 EXPORT_SYMBOL_NS_GPL(cdx_bus_add, CDX_BUS_CONTROLLER);
748
749 int cdx_register_controller(struct cdx_controller *cdx)
750 {
751         int ret;
752
753         ret = ida_alloc_range(&cdx_controller_ida, 0,  MAX_CDX_CONTROLLERS - 1, GFP_KERNEL);
754         if (ret < 0) {
755                 dev_err(cdx->dev,
756                         "No free index available. Maximum controllers already registered\n");
757                 cdx->id = (u8)MAX_CDX_CONTROLLERS;
758                 return ret;
759         }
760
761         mutex_lock(&cdx_controller_lock);
762         cdx->id = ret;
763
764         /* Scan all the devices */
765         if (cdx->ops->scan)
766                 cdx->ops->scan(cdx);
767         cdx->controller_registered = true;
768         mutex_unlock(&cdx_controller_lock);
769
770         return 0;
771 }
772 EXPORT_SYMBOL_NS_GPL(cdx_register_controller, CDX_BUS_CONTROLLER);
773
774 void cdx_unregister_controller(struct cdx_controller *cdx)
775 {
776         if (cdx->id >= MAX_CDX_CONTROLLERS)
777                 return;
778
779         mutex_lock(&cdx_controller_lock);
780
781         cdx->controller_registered = false;
782         device_for_each_child(cdx->dev, NULL, cdx_unregister_device);
783         ida_free(&cdx_controller_ida, cdx->id);
784
785         mutex_unlock(&cdx_controller_lock);
786 }
787 EXPORT_SYMBOL_NS_GPL(cdx_unregister_controller, CDX_BUS_CONTROLLER);
788
789 static int __init cdx_bus_init(void)
790 {
791         return bus_register(&cdx_bus_type);
792 }
793 postcore_initcall(cdx_bus_init);