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