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