1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Thunderbolt bus support 4 * 5 * Copyright (C) 2017, Intel Corporation 6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com> 7 */ 8 9 #include <linux/device.h> 10 #include <linux/idr.h> 11 #include <linux/module.h> 12 #include <linux/pm_runtime.h> 13 #include <linux/slab.h> 14 #include <linux/random.h> 15 #include <crypto/sha2.h> 16 #include <crypto/utils.h> 17 18 #include "tb.h" 19 20 static DEFINE_IDA(tb_domain_ida); 21 22 static bool match_service_id(const struct tb_service_id *id, 23 const struct tb_service *svc) 24 { 25 if (id->match_flags & TBSVC_MATCH_PROTOCOL_KEY) { 26 if (strcmp(id->protocol_key, svc->key)) 27 return false; 28 } 29 30 if (id->match_flags & TBSVC_MATCH_PROTOCOL_ID) { 31 if (id->protocol_id != svc->prtcid) 32 return false; 33 } 34 35 if (id->match_flags & TBSVC_MATCH_PROTOCOL_VERSION) { 36 if (id->protocol_version != svc->prtcvers) 37 return false; 38 } 39 40 if (id->match_flags & TBSVC_MATCH_PROTOCOL_REVISION) { 41 if (id->protocol_revision != svc->prtcrevs) 42 return false; 43 } 44 45 return true; 46 } 47 48 static const struct tb_service_id *__tb_service_match(struct device *dev, 49 const struct device_driver *drv) 50 { 51 const struct tb_service_driver *driver; 52 const struct tb_service_id *ids; 53 struct tb_service *svc; 54 55 svc = tb_to_service(dev); 56 if (!svc) 57 return NULL; 58 59 driver = container_of_const(drv, struct tb_service_driver, driver); 60 if (!driver->id_table) 61 return NULL; 62 63 for (ids = driver->id_table; ids->match_flags != 0; ids++) { 64 if (match_service_id(ids, svc)) 65 return ids; 66 } 67 68 return NULL; 69 } 70 71 static int tb_service_match(struct device *dev, const struct device_driver *drv) 72 { 73 return !!__tb_service_match(dev, drv); 74 } 75 76 static int tb_service_probe(struct device *dev) 77 { 78 struct tb_service *svc = tb_to_service(dev); 79 struct tb_service_driver *driver; 80 const struct tb_service_id *id; 81 82 driver = container_of(dev->driver, struct tb_service_driver, driver); 83 id = __tb_service_match(dev, &driver->driver); 84 85 return driver->probe(svc, id); 86 } 87 88 static void tb_service_remove(struct device *dev) 89 { 90 struct tb_service *svc = tb_to_service(dev); 91 struct tb_service_driver *driver; 92 93 driver = container_of(dev->driver, struct tb_service_driver, driver); 94 if (driver->remove) 95 driver->remove(svc); 96 } 97 98 static void tb_service_shutdown(struct device *dev) 99 { 100 struct tb_service_driver *driver; 101 struct tb_service *svc; 102 103 svc = tb_to_service(dev); 104 if (!svc || !dev->driver) 105 return; 106 107 driver = container_of(dev->driver, struct tb_service_driver, driver); 108 if (driver->shutdown) 109 driver->shutdown(svc); 110 } 111 112 static const char * const tb_security_names[] = { 113 [TB_SECURITY_NONE] = "none", 114 [TB_SECURITY_USER] = "user", 115 [TB_SECURITY_SECURE] = "secure", 116 [TB_SECURITY_DPONLY] = "dponly", 117 [TB_SECURITY_USBONLY] = "usbonly", 118 [TB_SECURITY_NOPCIE] = "nopcie", 119 }; 120 121 static ssize_t boot_acl_show(struct device *dev, struct device_attribute *attr, 122 char *buf) 123 { 124 struct tb *tb = container_of(dev, struct tb, dev); 125 uuid_t *uuids; 126 ssize_t ret; 127 int i; 128 129 uuids = kzalloc_objs(uuid_t, tb->nboot_acl); 130 if (!uuids) 131 return -ENOMEM; 132 133 pm_runtime_get_sync(&tb->dev); 134 135 if (mutex_lock_interruptible(&tb->lock)) { 136 ret = -ERESTARTSYS; 137 goto out; 138 } 139 ret = tb->cm_ops->get_boot_acl(tb, uuids, tb->nboot_acl); 140 if (ret) { 141 mutex_unlock(&tb->lock); 142 goto out; 143 } 144 mutex_unlock(&tb->lock); 145 146 for (ret = 0, i = 0; i < tb->nboot_acl; i++) { 147 if (!uuid_is_null(&uuids[i])) 148 ret += sysfs_emit_at(buf, ret, "%pUb", &uuids[i]); 149 150 ret += sysfs_emit_at(buf, ret, "%s", i < tb->nboot_acl - 1 ? "," : "\n"); 151 } 152 153 out: 154 pm_runtime_mark_last_busy(&tb->dev); 155 pm_runtime_put_autosuspend(&tb->dev); 156 kfree(uuids); 157 158 return ret; 159 } 160 161 static ssize_t boot_acl_store(struct device *dev, struct device_attribute *attr, 162 const char *buf, size_t count) 163 { 164 struct tb *tb = container_of(dev, struct tb, dev); 165 char *str, *s, *uuid_str; 166 ssize_t ret = 0; 167 uuid_t *acl; 168 int i = 0; 169 170 /* 171 * Make sure the value is not bigger than tb->nboot_acl * UUID 172 * length + commas and optional "\n". Also the smallest allowable 173 * string is tb->nboot_acl * ",". 174 */ 175 if (count > (UUID_STRING_LEN + 1) * tb->nboot_acl + 1) 176 return -EINVAL; 177 if (count < tb->nboot_acl - 1) 178 return -EINVAL; 179 180 str = kstrdup(buf, GFP_KERNEL); 181 if (!str) 182 return -ENOMEM; 183 184 acl = kzalloc_objs(uuid_t, tb->nboot_acl); 185 if (!acl) { 186 ret = -ENOMEM; 187 goto err_free_str; 188 } 189 190 uuid_str = strim(str); 191 while ((s = strsep(&uuid_str, ",")) != NULL && i < tb->nboot_acl) { 192 size_t len = strlen(s); 193 194 if (len) { 195 if (len != UUID_STRING_LEN) { 196 ret = -EINVAL; 197 goto err_free_acl; 198 } 199 ret = uuid_parse(s, &acl[i]); 200 if (ret) 201 goto err_free_acl; 202 } 203 204 i++; 205 } 206 207 if (s || i < tb->nboot_acl) { 208 ret = -EINVAL; 209 goto err_free_acl; 210 } 211 212 pm_runtime_get_sync(&tb->dev); 213 214 if (mutex_lock_interruptible(&tb->lock)) { 215 ret = -ERESTARTSYS; 216 goto err_rpm_put; 217 } 218 ret = tb->cm_ops->set_boot_acl(tb, acl, tb->nboot_acl); 219 if (!ret) { 220 /* Notify userspace about the change */ 221 tb_domain_event(tb, NULL); 222 } 223 mutex_unlock(&tb->lock); 224 225 err_rpm_put: 226 pm_runtime_mark_last_busy(&tb->dev); 227 pm_runtime_put_autosuspend(&tb->dev); 228 err_free_acl: 229 kfree(acl); 230 err_free_str: 231 kfree(str); 232 233 return ret ?: count; 234 } 235 static DEVICE_ATTR_RW(boot_acl); 236 237 static ssize_t deauthorization_show(struct device *dev, 238 struct device_attribute *attr, 239 char *buf) 240 { 241 const struct tb *tb = container_of(dev, struct tb, dev); 242 bool deauthorization = false; 243 244 /* Only meaningful if authorization is supported */ 245 if (tb->security_level == TB_SECURITY_USER || 246 tb->security_level == TB_SECURITY_SECURE) 247 deauthorization = !!tb->cm_ops->disapprove_switch; 248 249 return sysfs_emit(buf, "%d\n", deauthorization); 250 } 251 static DEVICE_ATTR_RO(deauthorization); 252 253 static ssize_t iommu_dma_protection_show(struct device *dev, 254 struct device_attribute *attr, 255 char *buf) 256 { 257 struct tb *tb = container_of(dev, struct tb, dev); 258 259 return sysfs_emit(buf, "%d\n", tb->nhi->iommu_dma_protection); 260 } 261 static DEVICE_ATTR_RO(iommu_dma_protection); 262 263 static ssize_t security_show(struct device *dev, struct device_attribute *attr, 264 char *buf) 265 { 266 struct tb *tb = container_of(dev, struct tb, dev); 267 const char *name = "unknown"; 268 269 if (tb->security_level < ARRAY_SIZE(tb_security_names)) 270 name = tb_security_names[tb->security_level]; 271 272 return sysfs_emit(buf, "%s\n", name); 273 } 274 static DEVICE_ATTR_RO(security); 275 276 static struct attribute *domain_attrs[] = { 277 &dev_attr_boot_acl.attr, 278 &dev_attr_deauthorization.attr, 279 &dev_attr_iommu_dma_protection.attr, 280 &dev_attr_security.attr, 281 NULL, 282 }; 283 284 static umode_t domain_attr_is_visible(struct kobject *kobj, 285 struct attribute *attr, int n) 286 { 287 struct device *dev = kobj_to_dev(kobj); 288 struct tb *tb = container_of(dev, struct tb, dev); 289 290 if (attr == &dev_attr_boot_acl.attr) { 291 if (tb->nboot_acl && 292 tb->cm_ops->get_boot_acl && 293 tb->cm_ops->set_boot_acl) 294 return attr->mode; 295 return 0; 296 } 297 298 return attr->mode; 299 } 300 301 static const struct attribute_group domain_attr_group = { 302 .is_visible = domain_attr_is_visible, 303 .attrs = domain_attrs, 304 }; 305 306 static const struct attribute_group *domain_attr_groups[] = { 307 &domain_attr_group, 308 NULL, 309 }; 310 311 const struct bus_type tb_bus_type = { 312 .name = "thunderbolt", 313 .match = tb_service_match, 314 .probe = tb_service_probe, 315 .remove = tb_service_remove, 316 .shutdown = tb_service_shutdown, 317 }; 318 319 static void tb_domain_release(struct device *dev) 320 { 321 struct tb *tb = container_of(dev, struct tb, dev); 322 struct tb_nhi *nhi = tb->nhi; 323 324 tb_ctl_free(tb->ctl); 325 destroy_workqueue(tb->wq); 326 ida_free(&tb_domain_ida, tb->index); 327 mutex_destroy(&tb->lock); 328 kfree(tb); 329 330 complete(&nhi->domain_released); 331 } 332 333 const struct device_type tb_domain_type = { 334 .name = "thunderbolt_domain", 335 .release = tb_domain_release, 336 }; 337 338 static bool tb_domain_event_cb(void *data, enum tb_cfg_pkg_type type, 339 const void *buf, size_t size) 340 { 341 struct tb *tb = data; 342 343 if (!tb->cm_ops->handle_event) { 344 tb_warn(tb, "domain does not have event handler\n"); 345 return true; 346 } 347 348 switch (type) { 349 case TB_CFG_PKG_XDOMAIN_REQ: 350 case TB_CFG_PKG_XDOMAIN_RESP: 351 if (tb_is_xdomain_enabled()) 352 return tb_xdomain_handle_request(tb, type, buf, size); 353 break; 354 355 default: 356 tb->cm_ops->handle_event(tb, type, buf, size); 357 } 358 359 return true; 360 } 361 362 /** 363 * tb_domain_alloc() - Allocate a domain 364 * @nhi: Pointer to the host controller 365 * @timeout_msec: Control channel timeout for non-raw messages 366 * @privsize: Size of the connection manager private data 367 * 368 * Allocates and initializes a new Thunderbolt domain. Connection 369 * managers are expected to call this and then fill in @cm_ops 370 * accordingly. 371 * 372 * Call tb_domain_put() to release the domain before it has been added 373 * to the system. 374 * 375 * Return: Pointer to &struct tb or %NULL in case of error. 376 */ 377 struct tb *tb_domain_alloc(struct tb_nhi *nhi, int timeout_msec, size_t privsize) 378 { 379 struct tb *tb; 380 381 /* 382 * Make sure the structure sizes map with what the hardware 383 * expects because bit-fields are being used. 384 */ 385 BUILD_BUG_ON(sizeof(struct tb_regs_switch_header) != 5 * 4); 386 BUILD_BUG_ON(sizeof(struct tb_regs_port_header) != 8 * 4); 387 BUILD_BUG_ON(sizeof(struct tb_regs_hop) != 2 * 4); 388 389 tb = kzalloc(sizeof(*tb) + privsize, GFP_KERNEL); 390 if (!tb) 391 return NULL; 392 393 tb->nhi = nhi; 394 mutex_init(&tb->lock); 395 396 tb->index = ida_alloc(&tb_domain_ida, GFP_KERNEL); 397 if (tb->index < 0) 398 goto err_free; 399 400 tb->wq = alloc_ordered_workqueue("thunderbolt%d", 0, tb->index); 401 if (!tb->wq) 402 goto err_remove_ida; 403 404 tb->ctl = tb_ctl_alloc(nhi, tb->index, timeout_msec, tb_domain_event_cb, tb); 405 if (!tb->ctl) 406 goto err_destroy_wq; 407 408 tb->dev.parent = nhi->dev; 409 tb->dev.bus = &tb_bus_type; 410 tb->dev.type = &tb_domain_type; 411 tb->dev.groups = domain_attr_groups; 412 dev_set_name(&tb->dev, "domain%d", tb->index); 413 device_initialize(&tb->dev); 414 415 return tb; 416 417 err_destroy_wq: 418 destroy_workqueue(tb->wq); 419 err_remove_ida: 420 ida_free(&tb_domain_ida, tb->index); 421 err_free: 422 kfree(tb); 423 424 return NULL; 425 } 426 427 /** 428 * tb_domain_add() - Add domain to the system 429 * @tb: Domain to add 430 * @reset: Issue reset to the host router 431 * 432 * Starts the domain and adds it to the system. Hotplugging devices will 433 * work after this has been returned successfully. In order to remove 434 * and release the domain after this function has been called, call 435 * tb_domain_remove(). 436 * 437 * Return: %0 on success, negative errno otherwise. 438 */ 439 int tb_domain_add(struct tb *tb, bool reset) 440 { 441 int ret; 442 443 if (WARN_ON(!tb->cm_ops)) 444 return -EINVAL; 445 446 mutex_lock(&tb->lock); 447 /* 448 * tb_schedule_hotplug_handler may be called as soon as the config 449 * channel is started. Thats why we have to hold the lock here. 450 */ 451 tb_ctl_start(tb->ctl); 452 453 if (tb->cm_ops->driver_ready) { 454 ret = tb->cm_ops->driver_ready(tb); 455 if (ret) 456 goto err_ctl_stop; 457 } 458 459 tb_dbg(tb, "security level set to %s\n", 460 tb_security_names[tb->security_level]); 461 462 ret = device_add(&tb->dev); 463 if (ret) 464 goto err_ctl_stop; 465 466 /* Start the domain */ 467 if (tb->cm_ops->start) { 468 ret = tb->cm_ops->start(tb, reset); 469 if (ret) 470 goto err_domain_del; 471 } 472 473 /* This starts event processing */ 474 mutex_unlock(&tb->lock); 475 476 device_init_wakeup(&tb->dev, true); 477 478 pm_runtime_no_callbacks(&tb->dev); 479 pm_runtime_set_active(&tb->dev); 480 pm_runtime_enable(&tb->dev); 481 pm_runtime_set_autosuspend_delay(&tb->dev, TB_AUTOSUSPEND_DELAY); 482 pm_runtime_mark_last_busy(&tb->dev); 483 pm_runtime_use_autosuspend(&tb->dev); 484 485 return 0; 486 487 err_domain_del: 488 device_del(&tb->dev); 489 err_ctl_stop: 490 tb_ctl_stop(tb->ctl); 491 mutex_unlock(&tb->lock); 492 493 return ret; 494 } 495 496 /** 497 * tb_domain_remove() - Removes and releases a domain 498 * @tb: Domain to remove 499 * 500 * Stops the domain, removes it from the system and releases all 501 * resources once the last reference has been released. 502 */ 503 void tb_domain_remove(struct tb *tb) 504 { 505 mutex_lock(&tb->lock); 506 if (tb->cm_ops->stop) 507 tb->cm_ops->stop(tb); 508 /* Stop the domain control traffic */ 509 tb_ctl_stop(tb->ctl); 510 mutex_unlock(&tb->lock); 511 512 flush_workqueue(tb->wq); 513 514 if (tb->cm_ops->deinit) 515 tb->cm_ops->deinit(tb); 516 517 device_unregister(&tb->dev); 518 } 519 520 /** 521 * tb_domain_suspend_noirq() - Suspend a domain 522 * @tb: Domain to suspend 523 * 524 * Suspends all devices in the domain and stops the control channel. 525 * 526 * Return: %0 on success, negative errno otherwise. 527 */ 528 int tb_domain_suspend_noirq(struct tb *tb) 529 { 530 int ret = 0; 531 532 /* 533 * The control channel interrupt is left enabled during suspend 534 * and taking the lock here prevents any events happening before 535 * we actually have stopped the domain and the control channel. 536 */ 537 mutex_lock(&tb->lock); 538 if (tb->cm_ops->suspend_noirq) 539 ret = tb->cm_ops->suspend_noirq(tb); 540 if (!ret) 541 tb_ctl_stop(tb->ctl); 542 mutex_unlock(&tb->lock); 543 544 return ret; 545 } 546 547 /** 548 * tb_domain_resume_noirq() - Resume a domain 549 * @tb: Domain to resume 550 * 551 * Re-starts the control channel, and resumes all devices connected to 552 * the domain. 553 * 554 * Return: %0 on success, negative errno otherwise. 555 */ 556 int tb_domain_resume_noirq(struct tb *tb) 557 { 558 int ret = 0; 559 560 mutex_lock(&tb->lock); 561 tb_ctl_start(tb->ctl); 562 if (tb->cm_ops->resume_noirq) 563 ret = tb->cm_ops->resume_noirq(tb); 564 mutex_unlock(&tb->lock); 565 566 return ret; 567 } 568 569 int tb_domain_suspend(struct tb *tb) 570 { 571 return tb->cm_ops->suspend ? tb->cm_ops->suspend(tb) : 0; 572 } 573 574 int tb_domain_freeze_noirq(struct tb *tb) 575 { 576 int ret = 0; 577 578 mutex_lock(&tb->lock); 579 if (tb->cm_ops->freeze_noirq) 580 ret = tb->cm_ops->freeze_noirq(tb); 581 if (!ret) 582 tb_ctl_stop(tb->ctl); 583 mutex_unlock(&tb->lock); 584 585 return ret; 586 } 587 588 int tb_domain_thaw_noirq(struct tb *tb) 589 { 590 int ret = 0; 591 592 mutex_lock(&tb->lock); 593 tb_ctl_start(tb->ctl); 594 if (tb->cm_ops->thaw_noirq) 595 ret = tb->cm_ops->thaw_noirq(tb); 596 mutex_unlock(&tb->lock); 597 598 return ret; 599 } 600 601 void tb_domain_complete(struct tb *tb) 602 { 603 if (tb->cm_ops->complete) 604 tb->cm_ops->complete(tb); 605 } 606 607 int tb_domain_runtime_suspend(struct tb *tb) 608 { 609 if (tb->cm_ops->runtime_suspend) { 610 int ret = tb->cm_ops->runtime_suspend(tb); 611 if (ret) 612 return ret; 613 } 614 tb_ctl_stop(tb->ctl); 615 return 0; 616 } 617 618 int tb_domain_runtime_resume(struct tb *tb) 619 { 620 tb_ctl_start(tb->ctl); 621 if (tb->cm_ops->runtime_resume) { 622 int ret = tb->cm_ops->runtime_resume(tb); 623 if (ret) 624 return ret; 625 } 626 return 0; 627 } 628 629 /** 630 * tb_domain_disapprove_switch() - Disapprove switch 631 * @tb: Domain the switch belongs to 632 * @sw: Switch to disapprove 633 * 634 * This will disconnect PCIe tunnel from parent to this @sw. 635 * 636 * Return: %0 on success and negative errno in case of failure. 637 */ 638 int tb_domain_disapprove_switch(struct tb *tb, struct tb_switch *sw) 639 { 640 if (!tb->cm_ops->disapprove_switch) 641 return -EPERM; 642 643 return tb->cm_ops->disapprove_switch(tb, sw); 644 } 645 646 /** 647 * tb_domain_approve_switch() - Approve switch 648 * @tb: Domain the switch belongs to 649 * @sw: Switch to approve 650 * 651 * This will approve switch by connection manager specific means. In 652 * case of success the connection manager will create PCIe tunnel from 653 * parent to @sw. 654 * 655 * Return: %0 on success, negative errno otherwise. 656 */ 657 int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw) 658 { 659 struct tb_switch *parent_sw; 660 661 if (!tb->cm_ops->approve_switch) 662 return -EPERM; 663 664 /* The parent switch must be authorized before this one */ 665 parent_sw = tb_to_switch(sw->dev.parent); 666 if (!parent_sw || !parent_sw->authorized) 667 return -EINVAL; 668 669 return tb->cm_ops->approve_switch(tb, sw); 670 } 671 672 /** 673 * tb_domain_approve_switch_key() - Approve switch and add key 674 * @tb: Domain the switch belongs to 675 * @sw: Switch to approve 676 * 677 * For switches that support secure connect, this function first adds 678 * key to the switch NVM using connection manager specific means. If 679 * adding the key is successful, the switch is approved and connected. 680 * 681 * Return: %0 on success and negative errno in case of failure. 682 */ 683 int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw) 684 { 685 struct tb_switch *parent_sw; 686 int ret; 687 688 if (!tb->cm_ops->approve_switch || !tb->cm_ops->add_switch_key) 689 return -EPERM; 690 691 /* The parent switch must be authorized before this one */ 692 parent_sw = tb_to_switch(sw->dev.parent); 693 if (!parent_sw || !parent_sw->authorized) 694 return -EINVAL; 695 696 ret = tb->cm_ops->add_switch_key(tb, sw); 697 if (ret) 698 return ret; 699 700 return tb->cm_ops->approve_switch(tb, sw); 701 } 702 703 /** 704 * tb_domain_challenge_switch_key() - Challenge and approve switch 705 * @tb: Domain the switch belongs to 706 * @sw: Switch to approve 707 * 708 * For switches that support secure connect, this function generates 709 * random challenge and sends it to the switch. The switch responds to 710 * this and if the response matches our random challenge, the switch is 711 * approved and connected. 712 * 713 * Return: %0 on success and negative errno in case of failure. 714 */ 715 int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw) 716 { 717 u8 challenge[TB_SWITCH_KEY_SIZE]; 718 u8 response[TB_SWITCH_KEY_SIZE]; 719 u8 hmac[TB_SWITCH_KEY_SIZE]; 720 struct tb_switch *parent_sw; 721 int ret; 722 723 if (!tb->cm_ops->approve_switch || !tb->cm_ops->challenge_switch_key) 724 return -EPERM; 725 726 /* The parent switch must be authorized before this one */ 727 parent_sw = tb_to_switch(sw->dev.parent); 728 if (!parent_sw || !parent_sw->authorized) 729 return -EINVAL; 730 731 get_random_bytes(challenge, sizeof(challenge)); 732 ret = tb->cm_ops->challenge_switch_key(tb, sw, challenge, response); 733 if (ret) 734 return ret; 735 736 static_assert(sizeof(hmac) == SHA256_DIGEST_SIZE); 737 hmac_sha256_usingrawkey(sw->key, TB_SWITCH_KEY_SIZE, 738 challenge, sizeof(challenge), hmac); 739 740 /* The returned HMAC must match the one we calculated */ 741 if (crypto_memneq(response, hmac, sizeof(hmac))) 742 return -EKEYREJECTED; 743 744 return tb->cm_ops->approve_switch(tb, sw); 745 } 746 747 /** 748 * tb_domain_disconnect_pcie_paths() - Disconnect all PCIe paths 749 * @tb: Domain whose PCIe paths to disconnect 750 * 751 * This needs to be called in preparation for NVM upgrade of the host 752 * controller. Makes sure all PCIe paths are disconnected. 753 * 754 * Return: %0 on success and negative errno in case of error. 755 */ 756 int tb_domain_disconnect_pcie_paths(struct tb *tb) 757 { 758 if (!tb->cm_ops->disconnect_pcie_paths) 759 return -EPERM; 760 761 return tb->cm_ops->disconnect_pcie_paths(tb); 762 } 763 764 /** 765 * tb_domain_approve_xdomain_paths() - Enable DMA paths for XDomain 766 * @tb: Domain enabling the DMA paths 767 * @xd: XDomain DMA paths are created to 768 * @transmit_path: HopID we are using to send out packets 769 * @transmit_ring: DMA ring used to send out packets 770 * @receive_path: HopID the other end is using to send packets to us 771 * @receive_ring: DMA ring used to receive packets from @receive_path 772 * 773 * Calls connection manager specific method to enable DMA paths to the 774 * XDomain in question. 775 * 776 * Return: 777 * * %0 - On success. 778 * * %-ENOTSUPP - If the connection manager implementation does not support 779 * XDomains. 780 * * Negative errno - An error occurred. 781 */ 782 int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd, 783 int transmit_path, int transmit_ring, 784 int receive_path, int receive_ring) 785 { 786 if (!tb->cm_ops->approve_xdomain_paths) 787 return -ENOTSUPP; 788 789 return tb->cm_ops->approve_xdomain_paths(tb, xd, transmit_path, 790 transmit_ring, receive_path, receive_ring); 791 } 792 793 /** 794 * tb_domain_disconnect_xdomain_paths() - Disable DMA paths for XDomain 795 * @tb: Domain disabling the DMA paths 796 * @xd: XDomain whose DMA paths are disconnected 797 * @transmit_path: HopID we are using to send out packets 798 * @transmit_ring: DMA ring used to send out packets 799 * @receive_path: HopID the other end is using to send packets to us 800 * @receive_ring: DMA ring used to receive packets from @receive_path 801 * 802 * Calls connection manager specific method to disconnect DMA paths to 803 * the XDomain in question. 804 * 805 * Return: 806 * * %0 - On success. 807 * * %-ENOTSUPP - If the connection manager implementation does not support 808 * XDomains. 809 * * Negative errno - An error occurred. 810 */ 811 int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd, 812 int transmit_path, int transmit_ring, 813 int receive_path, int receive_ring) 814 { 815 if (!tb->cm_ops->disconnect_xdomain_paths) 816 return -ENOTSUPP; 817 818 return tb->cm_ops->disconnect_xdomain_paths(tb, xd, transmit_path, 819 transmit_ring, receive_path, receive_ring); 820 } 821 822 static int disconnect_xdomain(struct device *dev, void *data) 823 { 824 struct tb_xdomain *xd; 825 struct tb *tb = data; 826 int ret = 0; 827 828 xd = tb_to_xdomain(dev); 829 if (xd && xd->tb == tb) 830 ret = tb_xdomain_disable_all_paths(xd); 831 832 return ret; 833 } 834 835 /** 836 * tb_domain_disconnect_all_paths() - Disconnect all paths for the domain 837 * @tb: Domain whose paths are disconnected 838 * 839 * This function can be used to disconnect all paths (PCIe, XDomain) for 840 * example in preparation for host NVM firmware upgrade. After this is 841 * called the paths cannot be established without resetting the switch. 842 * 843 * Return: %0 in case of success and negative errno otherwise. 844 */ 845 int tb_domain_disconnect_all_paths(struct tb *tb) 846 { 847 int ret; 848 849 ret = tb_domain_disconnect_pcie_paths(tb); 850 if (ret) 851 return ret; 852 853 return bus_for_each_dev(&tb_bus_type, NULL, tb, disconnect_xdomain); 854 } 855 856 struct unregister_context { 857 const struct tb *tb; 858 int n; 859 }; 860 861 static int unregister_unplugged_xdomain(struct device *dev, void *data) 862 { 863 struct unregister_context *ctx = data; 864 struct tb_xdomain *xd; 865 866 xd = tb_to_xdomain(dev); 867 if (xd && xd->tb == ctx->tb && xd->is_unplugged) { 868 tb_xdomain_unregister(xd); 869 ctx->n++; 870 } 871 return 0; 872 } 873 874 int tb_domain_unregister_unplugged_xdomains(struct tb *tb) 875 { 876 struct unregister_context ctx; 877 878 ctx.tb = tb_domain_get(tb); 879 ctx.n = 0; 880 bus_for_each_dev(&tb_bus_type, NULL, &ctx, unregister_unplugged_xdomain); 881 tb_domain_put(tb); 882 883 return ctx.n; 884 } 885 886 int tb_domain_init(void) 887 { 888 int ret; 889 890 tb_configfs_init(); 891 tb_debugfs_init(); 892 tb_acpi_init(); 893 894 ret = tb_xdomain_init(); 895 if (ret) 896 goto err_acpi; 897 ret = bus_register(&tb_bus_type); 898 if (ret) 899 goto err_xdomain; 900 901 return 0; 902 903 err_xdomain: 904 tb_xdomain_exit(); 905 err_acpi: 906 tb_acpi_exit(); 907 tb_debugfs_exit(); 908 909 return ret; 910 } 911 912 void tb_domain_exit(void) 913 { 914 bus_unregister(&tb_bus_type); 915 ida_destroy(&tb_domain_ida); 916 tb_nvm_exit(); 917 tb_xdomain_exit(); 918 tb_acpi_exit(); 919 tb_debugfs_exit(); 920 tb_configfs_exit(); 921 } 922