| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Thunderbolt Cactus Ridge driver - switch/port utility functions |
| 4 | * |
| 5 | * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> |
| 6 | */ |
| 7 | |
| 8 | #include <linux/delay.h> |
| 9 | #include <linux/idr.h> |
| 10 | #include <linux/nvmem-provider.h> |
| 11 | #include <linux/pm_runtime.h> |
| 12 | #include <linux/sched/signal.h> |
| 13 | #include <linux/sizes.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/vmalloc.h> |
| 16 | |
| 17 | #include "tb.h" |
| 18 | |
| 19 | /* Switch NVM support */ |
| 20 | |
| 21 | #define NVM_DEVID 0x05 |
| 22 | #define NVM_VERSION 0x08 |
| 23 | #define NVM_CSS 0x10 |
| 24 | #define NVM_FLASH_SIZE 0x45 |
| 25 | |
| 26 | #define NVM_MIN_SIZE SZ_32K |
| 27 | #define NVM_MAX_SIZE SZ_512K |
| 28 | |
| 29 | static DEFINE_IDA(nvm_ida); |
| 30 | |
| 31 | struct nvm_auth_status { |
| 32 | struct list_head list; |
| 33 | uuid_t uuid; |
| 34 | u32 status; |
| 35 | }; |
| 36 | |
| 37 | /* |
| 38 | * Hold NVM authentication failure status per switch This information |
| 39 | * needs to stay around even when the switch gets power cycled so we |
| 40 | * keep it separately. |
| 41 | */ |
| 42 | static LIST_HEAD(nvm_auth_status_cache); |
| 43 | static DEFINE_MUTEX(nvm_auth_status_lock); |
| 44 | |
| 45 | static struct nvm_auth_status *__nvm_get_auth_status(const struct tb_switch *sw) |
| 46 | { |
| 47 | struct nvm_auth_status *st; |
| 48 | |
| 49 | list_for_each_entry(st, &nvm_auth_status_cache, list) { |
| 50 | if (uuid_equal(&st->uuid, sw->uuid)) |
| 51 | return st; |
| 52 | } |
| 53 | |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | static void nvm_get_auth_status(const struct tb_switch *sw, u32 *status) |
| 58 | { |
| 59 | struct nvm_auth_status *st; |
| 60 | |
| 61 | mutex_lock(&nvm_auth_status_lock); |
| 62 | st = __nvm_get_auth_status(sw); |
| 63 | mutex_unlock(&nvm_auth_status_lock); |
| 64 | |
| 65 | *status = st ? st->status : 0; |
| 66 | } |
| 67 | |
| 68 | static void nvm_set_auth_status(const struct tb_switch *sw, u32 status) |
| 69 | { |
| 70 | struct nvm_auth_status *st; |
| 71 | |
| 72 | if (WARN_ON(!sw->uuid)) |
| 73 | return; |
| 74 | |
| 75 | mutex_lock(&nvm_auth_status_lock); |
| 76 | st = __nvm_get_auth_status(sw); |
| 77 | |
| 78 | if (!st) { |
| 79 | st = kzalloc(sizeof(*st), GFP_KERNEL); |
| 80 | if (!st) |
| 81 | goto unlock; |
| 82 | |
| 83 | memcpy(&st->uuid, sw->uuid, sizeof(st->uuid)); |
| 84 | INIT_LIST_HEAD(&st->list); |
| 85 | list_add_tail(&st->list, &nvm_auth_status_cache); |
| 86 | } |
| 87 | |
| 88 | st->status = status; |
| 89 | unlock: |
| 90 | mutex_unlock(&nvm_auth_status_lock); |
| 91 | } |
| 92 | |
| 93 | static void nvm_clear_auth_status(const struct tb_switch *sw) |
| 94 | { |
| 95 | struct nvm_auth_status *st; |
| 96 | |
| 97 | mutex_lock(&nvm_auth_status_lock); |
| 98 | st = __nvm_get_auth_status(sw); |
| 99 | if (st) { |
| 100 | list_del(&st->list); |
| 101 | kfree(st); |
| 102 | } |
| 103 | mutex_unlock(&nvm_auth_status_lock); |
| 104 | } |
| 105 | |
| 106 | static int nvm_validate_and_write(struct tb_switch *sw) |
| 107 | { |
| 108 | unsigned int image_size, hdr_size; |
| 109 | const u8 *buf = sw->nvm->buf; |
| 110 | u16 ds_size; |
| 111 | int ret; |
| 112 | |
| 113 | if (!buf) |
| 114 | return -EINVAL; |
| 115 | |
| 116 | image_size = sw->nvm->buf_data_size; |
| 117 | if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE) |
| 118 | return -EINVAL; |
| 119 | |
| 120 | /* |
| 121 | * FARB pointer must point inside the image and must at least |
| 122 | * contain parts of the digital section we will be reading here. |
| 123 | */ |
| 124 | hdr_size = (*(u32 *)buf) & 0xffffff; |
| 125 | if (hdr_size + NVM_DEVID + 2 >= image_size) |
| 126 | return -EINVAL; |
| 127 | |
| 128 | /* Digital section start should be aligned to 4k page */ |
| 129 | if (!IS_ALIGNED(hdr_size, SZ_4K)) |
| 130 | return -EINVAL; |
| 131 | |
| 132 | /* |
| 133 | * Read digital section size and check that it also fits inside |
| 134 | * the image. |
| 135 | */ |
| 136 | ds_size = *(u16 *)(buf + hdr_size); |
| 137 | if (ds_size >= image_size) |
| 138 | return -EINVAL; |
| 139 | |
| 140 | if (!sw->safe_mode) { |
| 141 | u16 device_id; |
| 142 | |
| 143 | /* |
| 144 | * Make sure the device ID in the image matches the one |
| 145 | * we read from the switch config space. |
| 146 | */ |
| 147 | device_id = *(u16 *)(buf + hdr_size + NVM_DEVID); |
| 148 | if (device_id != sw->config.device_id) |
| 149 | return -EINVAL; |
| 150 | |
| 151 | if (sw->generation < 3) { |
| 152 | /* Write CSS headers first */ |
| 153 | ret = dma_port_flash_write(sw->dma_port, |
| 154 | DMA_PORT_CSS_ADDRESS, buf + NVM_CSS, |
| 155 | DMA_PORT_CSS_MAX_SIZE); |
| 156 | if (ret) |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | /* Skip headers in the image */ |
| 161 | buf += hdr_size; |
| 162 | image_size -= hdr_size; |
| 163 | } |
| 164 | |
| 165 | return dma_port_flash_write(sw->dma_port, 0, buf, image_size); |
| 166 | } |
| 167 | |
| 168 | static int nvm_authenticate_host(struct tb_switch *sw) |
| 169 | { |
| 170 | int ret = 0; |
| 171 | |
| 172 | /* |
| 173 | * Root switch NVM upgrade requires that we disconnect the |
| 174 | * existing paths first (in case it is not in safe mode |
| 175 | * already). |
| 176 | */ |
| 177 | if (!sw->safe_mode) { |
| 178 | u32 status; |
| 179 | |
| 180 | ret = tb_domain_disconnect_all_paths(sw->tb); |
| 181 | if (ret) |
| 182 | return ret; |
| 183 | /* |
| 184 | * The host controller goes away pretty soon after this if |
| 185 | * everything goes well so getting timeout is expected. |
| 186 | */ |
| 187 | ret = dma_port_flash_update_auth(sw->dma_port); |
| 188 | if (!ret || ret == -ETIMEDOUT) |
| 189 | return 0; |
| 190 | |
| 191 | /* |
| 192 | * Any error from update auth operation requires power |
| 193 | * cycling of the host router. |
| 194 | */ |
| 195 | tb_sw_warn(sw, "failed to authenticate NVM, power cycling\n"); |
| 196 | if (dma_port_flash_update_auth_status(sw->dma_port, &status) > 0) |
| 197 | nvm_set_auth_status(sw, status); |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * From safe mode we can get out by just power cycling the |
| 202 | * switch. |
| 203 | */ |
| 204 | dma_port_power_cycle(sw->dma_port); |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | static int nvm_authenticate_device(struct tb_switch *sw) |
| 209 | { |
| 210 | int ret, retries = 10; |
| 211 | |
| 212 | ret = dma_port_flash_update_auth(sw->dma_port); |
| 213 | switch (ret) { |
| 214 | case 0: |
| 215 | case -ETIMEDOUT: |
| 216 | case -EACCES: |
| 217 | case -EINVAL: |
| 218 | /* Power cycle is required */ |
| 219 | break; |
| 220 | default: |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Poll here for the authentication status. It takes some time |
| 226 | * for the device to respond (we get timeout for a while). Once |
| 227 | * we get response the device needs to be power cycled in order |
| 228 | * to the new NVM to be taken into use. |
| 229 | */ |
| 230 | do { |
| 231 | u32 status; |
| 232 | |
| 233 | ret = dma_port_flash_update_auth_status(sw->dma_port, &status); |
| 234 | if (ret < 0 && ret != -ETIMEDOUT) |
| 235 | return ret; |
| 236 | if (ret > 0) { |
| 237 | if (status) { |
| 238 | tb_sw_warn(sw, "failed to authenticate NVM\n"); |
| 239 | nvm_set_auth_status(sw, status); |
| 240 | } |
| 241 | |
| 242 | tb_sw_info(sw, "power cycling the switch now\n"); |
| 243 | dma_port_power_cycle(sw->dma_port); |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | msleep(500); |
| 248 | } while (--retries); |
| 249 | |
| 250 | return -ETIMEDOUT; |
| 251 | } |
| 252 | |
| 253 | static int tb_switch_nvm_read(void *priv, unsigned int offset, void *val, |
| 254 | size_t bytes) |
| 255 | { |
| 256 | struct tb_switch *sw = priv; |
| 257 | int ret; |
| 258 | |
| 259 | pm_runtime_get_sync(&sw->dev); |
| 260 | ret = dma_port_flash_read(sw->dma_port, offset, val, bytes); |
| 261 | pm_runtime_mark_last_busy(&sw->dev); |
| 262 | pm_runtime_put_autosuspend(&sw->dev); |
| 263 | |
| 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | static int tb_switch_nvm_write(void *priv, unsigned int offset, void *val, |
| 268 | size_t bytes) |
| 269 | { |
| 270 | struct tb_switch *sw = priv; |
| 271 | int ret = 0; |
| 272 | |
| 273 | if (!mutex_trylock(&sw->tb->lock)) |
| 274 | return restart_syscall(); |
| 275 | |
| 276 | /* |
| 277 | * Since writing the NVM image might require some special steps, |
| 278 | * for example when CSS headers are written, we cache the image |
| 279 | * locally here and handle the special cases when the user asks |
| 280 | * us to authenticate the image. |
| 281 | */ |
| 282 | if (!sw->nvm->buf) { |
| 283 | sw->nvm->buf = vmalloc(NVM_MAX_SIZE); |
| 284 | if (!sw->nvm->buf) { |
| 285 | ret = -ENOMEM; |
| 286 | goto unlock; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | sw->nvm->buf_data_size = offset + bytes; |
| 291 | memcpy(sw->nvm->buf + offset, val, bytes); |
| 292 | |
| 293 | unlock: |
| 294 | mutex_unlock(&sw->tb->lock); |
| 295 | |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | static struct nvmem_device *register_nvmem(struct tb_switch *sw, int id, |
| 300 | size_t size, bool active) |
| 301 | { |
| 302 | struct nvmem_config config; |
| 303 | |
| 304 | memset(&config, 0, sizeof(config)); |
| 305 | |
| 306 | if (active) { |
| 307 | config.name = "nvm_active"; |
| 308 | config.reg_read = tb_switch_nvm_read; |
| 309 | config.read_only = true; |
| 310 | } else { |
| 311 | config.name = "nvm_non_active"; |
| 312 | config.reg_write = tb_switch_nvm_write; |
| 313 | config.root_only = true; |
| 314 | } |
| 315 | |
| 316 | config.id = id; |
| 317 | config.stride = 4; |
| 318 | config.word_size = 4; |
| 319 | config.size = size; |
| 320 | config.dev = &sw->dev; |
| 321 | config.owner = THIS_MODULE; |
| 322 | config.priv = sw; |
| 323 | |
| 324 | return nvmem_register(&config); |
| 325 | } |
| 326 | |
| 327 | static int tb_switch_nvm_add(struct tb_switch *sw) |
| 328 | { |
| 329 | struct nvmem_device *nvm_dev; |
| 330 | struct tb_switch_nvm *nvm; |
| 331 | u32 val; |
| 332 | int ret; |
| 333 | |
| 334 | if (!sw->dma_port) |
| 335 | return 0; |
| 336 | |
| 337 | nvm = kzalloc(sizeof(*nvm), GFP_KERNEL); |
| 338 | if (!nvm) |
| 339 | return -ENOMEM; |
| 340 | |
| 341 | nvm->id = ida_simple_get(&nvm_ida, 0, 0, GFP_KERNEL); |
| 342 | |
| 343 | /* |
| 344 | * If the switch is in safe-mode the only accessible portion of |
| 345 | * the NVM is the non-active one where userspace is expected to |
| 346 | * write new functional NVM. |
| 347 | */ |
| 348 | if (!sw->safe_mode) { |
| 349 | u32 nvm_size, hdr_size; |
| 350 | |
| 351 | ret = dma_port_flash_read(sw->dma_port, NVM_FLASH_SIZE, &val, |
| 352 | sizeof(val)); |
| 353 | if (ret) |
| 354 | goto err_ida; |
| 355 | |
| 356 | hdr_size = sw->generation < 3 ? SZ_8K : SZ_16K; |
| 357 | nvm_size = (SZ_1M << (val & 7)) / 8; |
| 358 | nvm_size = (nvm_size - hdr_size) / 2; |
| 359 | |
| 360 | ret = dma_port_flash_read(sw->dma_port, NVM_VERSION, &val, |
| 361 | sizeof(val)); |
| 362 | if (ret) |
| 363 | goto err_ida; |
| 364 | |
| 365 | nvm->major = val >> 16; |
| 366 | nvm->minor = val >> 8; |
| 367 | |
| 368 | nvm_dev = register_nvmem(sw, nvm->id, nvm_size, true); |
| 369 | if (IS_ERR(nvm_dev)) { |
| 370 | ret = PTR_ERR(nvm_dev); |
| 371 | goto err_ida; |
| 372 | } |
| 373 | nvm->active = nvm_dev; |
| 374 | } |
| 375 | |
| 376 | nvm_dev = register_nvmem(sw, nvm->id, NVM_MAX_SIZE, false); |
| 377 | if (IS_ERR(nvm_dev)) { |
| 378 | ret = PTR_ERR(nvm_dev); |
| 379 | goto err_nvm_active; |
| 380 | } |
| 381 | nvm->non_active = nvm_dev; |
| 382 | |
| 383 | sw->nvm = nvm; |
| 384 | return 0; |
| 385 | |
| 386 | err_nvm_active: |
| 387 | if (nvm->active) |
| 388 | nvmem_unregister(nvm->active); |
| 389 | err_ida: |
| 390 | ida_simple_remove(&nvm_ida, nvm->id); |
| 391 | kfree(nvm); |
| 392 | |
| 393 | return ret; |
| 394 | } |
| 395 | |
| 396 | static void tb_switch_nvm_remove(struct tb_switch *sw) |
| 397 | { |
| 398 | struct tb_switch_nvm *nvm; |
| 399 | |
| 400 | nvm = sw->nvm; |
| 401 | sw->nvm = NULL; |
| 402 | |
| 403 | if (!nvm) |
| 404 | return; |
| 405 | |
| 406 | /* Remove authentication status in case the switch is unplugged */ |
| 407 | if (!nvm->authenticating) |
| 408 | nvm_clear_auth_status(sw); |
| 409 | |
| 410 | nvmem_unregister(nvm->non_active); |
| 411 | if (nvm->active) |
| 412 | nvmem_unregister(nvm->active); |
| 413 | ida_simple_remove(&nvm_ida, nvm->id); |
| 414 | vfree(nvm->buf); |
| 415 | kfree(nvm); |
| 416 | } |
| 417 | |
| 418 | /* port utility functions */ |
| 419 | |
| 420 | static const char *tb_port_type(struct tb_regs_port_header *port) |
| 421 | { |
| 422 | switch (port->type >> 16) { |
| 423 | case 0: |
| 424 | switch ((u8) port->type) { |
| 425 | case 0: |
| 426 | return "Inactive"; |
| 427 | case 1: |
| 428 | return "Port"; |
| 429 | case 2: |
| 430 | return "NHI"; |
| 431 | default: |
| 432 | return "unknown"; |
| 433 | } |
| 434 | case 0x2: |
| 435 | return "Ethernet"; |
| 436 | case 0x8: |
| 437 | return "SATA"; |
| 438 | case 0xe: |
| 439 | return "DP/HDMI"; |
| 440 | case 0x10: |
| 441 | return "PCIe"; |
| 442 | case 0x20: |
| 443 | return "USB"; |
| 444 | default: |
| 445 | return "unknown"; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port) |
| 450 | { |
| 451 | tb_info(tb, |
| 452 | " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n", |
| 453 | port->port_number, port->vendor_id, port->device_id, |
| 454 | port->revision, port->thunderbolt_version, tb_port_type(port), |
| 455 | port->type); |
| 456 | tb_info(tb, " Max hop id (in/out): %d/%d\n", |
| 457 | port->max_in_hop_id, port->max_out_hop_id); |
| 458 | tb_info(tb, " Max counters: %d\n", port->max_counters); |
| 459 | tb_info(tb, " NFC Credits: %#x\n", port->nfc_credits); |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * tb_port_state() - get connectedness state of a port |
| 464 | * |
| 465 | * The port must have a TB_CAP_PHY (i.e. it should be a real port). |
| 466 | * |
| 467 | * Return: Returns an enum tb_port_state on success or an error code on failure. |
| 468 | */ |
| 469 | static int tb_port_state(struct tb_port *port) |
| 470 | { |
| 471 | struct tb_cap_phy phy; |
| 472 | int res; |
| 473 | if (port->cap_phy == 0) { |
| 474 | tb_port_WARN(port, "does not have a PHY\n"); |
| 475 | return -EINVAL; |
| 476 | } |
| 477 | res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2); |
| 478 | if (res) |
| 479 | return res; |
| 480 | return phy.state; |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * tb_wait_for_port() - wait for a port to become ready |
| 485 | * |
| 486 | * Wait up to 1 second for a port to reach state TB_PORT_UP. If |
| 487 | * wait_if_unplugged is set then we also wait if the port is in state |
| 488 | * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after |
| 489 | * switch resume). Otherwise we only wait if a device is registered but the link |
| 490 | * has not yet been established. |
| 491 | * |
| 492 | * Return: Returns an error code on failure. Returns 0 if the port is not |
| 493 | * connected or failed to reach state TB_PORT_UP within one second. Returns 1 |
| 494 | * if the port is connected and in state TB_PORT_UP. |
| 495 | */ |
| 496 | int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged) |
| 497 | { |
| 498 | int retries = 10; |
| 499 | int state; |
| 500 | if (!port->cap_phy) { |
| 501 | tb_port_WARN(port, "does not have PHY\n"); |
| 502 | return -EINVAL; |
| 503 | } |
| 504 | if (tb_is_upstream_port(port)) { |
| 505 | tb_port_WARN(port, "is the upstream port\n"); |
| 506 | return -EINVAL; |
| 507 | } |
| 508 | |
| 509 | while (retries--) { |
| 510 | state = tb_port_state(port); |
| 511 | if (state < 0) |
| 512 | return state; |
| 513 | if (state == TB_PORT_DISABLED) { |
| 514 | tb_port_info(port, "is disabled (state: 0)\n"); |
| 515 | return 0; |
| 516 | } |
| 517 | if (state == TB_PORT_UNPLUGGED) { |
| 518 | if (wait_if_unplugged) { |
| 519 | /* used during resume */ |
| 520 | tb_port_info(port, |
| 521 | "is unplugged (state: 7), retrying...\n"); |
| 522 | msleep(100); |
| 523 | continue; |
| 524 | } |
| 525 | tb_port_info(port, "is unplugged (state: 7)\n"); |
| 526 | return 0; |
| 527 | } |
| 528 | if (state == TB_PORT_UP) { |
| 529 | tb_port_info(port, |
| 530 | "is connected, link is up (state: 2)\n"); |
| 531 | return 1; |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * After plug-in the state is TB_PORT_CONNECTING. Give it some |
| 536 | * time. |
| 537 | */ |
| 538 | tb_port_info(port, |
| 539 | "is connected, link is not up (state: %d), retrying...\n", |
| 540 | state); |
| 541 | msleep(100); |
| 542 | } |
| 543 | tb_port_warn(port, |
| 544 | "failed to reach state TB_PORT_UP. Ignoring port...\n"); |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port |
| 550 | * |
| 551 | * Change the number of NFC credits allocated to @port by @credits. To remove |
| 552 | * NFC credits pass a negative amount of credits. |
| 553 | * |
| 554 | * Return: Returns 0 on success or an error code on failure. |
| 555 | */ |
| 556 | int tb_port_add_nfc_credits(struct tb_port *port, int credits) |
| 557 | { |
| 558 | if (credits == 0) |
| 559 | return 0; |
| 560 | tb_port_info(port, |
| 561 | "adding %#x NFC credits (%#x -> %#x)", |
| 562 | credits, |
| 563 | port->config.nfc_credits, |
| 564 | port->config.nfc_credits + credits); |
| 565 | port->config.nfc_credits += credits; |
| 566 | return tb_port_write(port, &port->config.nfc_credits, |
| 567 | TB_CFG_PORT, 4, 1); |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER |
| 572 | * |
| 573 | * Return: Returns 0 on success or an error code on failure. |
| 574 | */ |
| 575 | int tb_port_clear_counter(struct tb_port *port, int counter) |
| 576 | { |
| 577 | u32 zero[3] = { 0, 0, 0 }; |
| 578 | tb_port_info(port, "clearing counter %d\n", counter); |
| 579 | return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3); |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * tb_init_port() - initialize a port |
| 584 | * |
| 585 | * This is a helper method for tb_switch_alloc. Does not check or initialize |
| 586 | * any downstream switches. |
| 587 | * |
| 588 | * Return: Returns 0 on success or an error code on failure. |
| 589 | */ |
| 590 | static int tb_init_port(struct tb_port *port) |
| 591 | { |
| 592 | int res; |
| 593 | int cap; |
| 594 | |
| 595 | res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8); |
| 596 | if (res) |
| 597 | return res; |
| 598 | |
| 599 | /* Port 0 is the switch itself and has no PHY. */ |
| 600 | if (port->config.type == TB_TYPE_PORT && port->port != 0) { |
| 601 | cap = tb_port_find_cap(port, TB_PORT_CAP_PHY); |
| 602 | |
| 603 | if (cap > 0) |
| 604 | port->cap_phy = cap; |
| 605 | else |
| 606 | tb_port_WARN(port, "non switch port without a PHY\n"); |
| 607 | } |
| 608 | |
| 609 | tb_dump_port(port->sw->tb, &port->config); |
| 610 | |
| 611 | /* TODO: Read dual link port, DP port and more from EEPROM. */ |
| 612 | return 0; |
| 613 | |
| 614 | } |
| 615 | |
| 616 | /* switch utility functions */ |
| 617 | |
| 618 | static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw) |
| 619 | { |
| 620 | tb_info(tb, |
| 621 | " Switch: %x:%x (Revision: %d, TB Version: %d)\n", |
| 622 | sw->vendor_id, sw->device_id, sw->revision, |
| 623 | sw->thunderbolt_version); |
| 624 | tb_info(tb, " Max Port Number: %d\n", sw->max_port_number); |
| 625 | tb_info(tb, " Config:\n"); |
| 626 | tb_info(tb, |
| 627 | " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n", |
| 628 | sw->upstream_port_number, sw->depth, |
| 629 | (((u64) sw->route_hi) << 32) | sw->route_lo, |
| 630 | sw->enabled, sw->plug_events_delay); |
| 631 | tb_info(tb, |
| 632 | " unknown1: %#x unknown4: %#x\n", |
| 633 | sw->__unknown1, sw->__unknown4); |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET |
| 638 | * |
| 639 | * Return: Returns 0 on success or an error code on failure. |
| 640 | */ |
| 641 | int tb_switch_reset(struct tb *tb, u64 route) |
| 642 | { |
| 643 | struct tb_cfg_result res; |
| 644 | struct tb_regs_switch_header header = { |
| 645 | header.route_hi = route >> 32, |
| 646 | header.route_lo = route, |
| 647 | header.enabled = true, |
| 648 | }; |
| 649 | tb_info(tb, "resetting switch at %llx\n", route); |
| 650 | res.err = tb_cfg_write(tb->ctl, ((u32 *) &header) + 2, route, |
| 651 | 0, 2, 2, 2); |
| 652 | if (res.err) |
| 653 | return res.err; |
| 654 | res = tb_cfg_reset(tb->ctl, route, TB_CFG_DEFAULT_TIMEOUT); |
| 655 | if (res.err > 0) |
| 656 | return -EIO; |
| 657 | return res.err; |
| 658 | } |
| 659 | |
| 660 | struct tb_switch *get_switch_at_route(struct tb_switch *sw, u64 route) |
| 661 | { |
| 662 | u8 next_port = route; /* |
| 663 | * Routes use a stride of 8 bits, |
| 664 | * eventhough a port index has 6 bits at most. |
| 665 | * */ |
| 666 | if (route == 0) |
| 667 | return sw; |
| 668 | if (next_port > sw->config.max_port_number) |
| 669 | return NULL; |
| 670 | if (tb_is_upstream_port(&sw->ports[next_port])) |
| 671 | return NULL; |
| 672 | if (!sw->ports[next_port].remote) |
| 673 | return NULL; |
| 674 | return get_switch_at_route(sw->ports[next_port].remote->sw, |
| 675 | route >> TB_ROUTE_SHIFT); |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * tb_plug_events_active() - enable/disable plug events on a switch |
| 680 | * |
| 681 | * Also configures a sane plug_events_delay of 255ms. |
| 682 | * |
| 683 | * Return: Returns 0 on success or an error code on failure. |
| 684 | */ |
| 685 | static int tb_plug_events_active(struct tb_switch *sw, bool active) |
| 686 | { |
| 687 | u32 data; |
| 688 | int res; |
| 689 | |
| 690 | if (!sw->config.enabled) |
| 691 | return 0; |
| 692 | |
| 693 | sw->config.plug_events_delay = 0xff; |
| 694 | res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1); |
| 695 | if (res) |
| 696 | return res; |
| 697 | |
| 698 | res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1); |
| 699 | if (res) |
| 700 | return res; |
| 701 | |
| 702 | if (active) { |
| 703 | data = data & 0xFFFFFF83; |
| 704 | switch (sw->config.device_id) { |
| 705 | case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE: |
| 706 | case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE: |
| 707 | case PCI_DEVICE_ID_INTEL_PORT_RIDGE: |
| 708 | break; |
| 709 | default: |
| 710 | data |= 4; |
| 711 | } |
| 712 | } else { |
| 713 | data = data | 0x7c; |
| 714 | } |
| 715 | return tb_sw_write(sw, &data, TB_CFG_SWITCH, |
| 716 | sw->cap_plug_events + 1, 1); |
| 717 | } |
| 718 | |
| 719 | static ssize_t authorized_show(struct device *dev, |
| 720 | struct device_attribute *attr, |
| 721 | char *buf) |
| 722 | { |
| 723 | struct tb_switch *sw = tb_to_switch(dev); |
| 724 | |
| 725 | return sprintf(buf, "%u\n", sw->authorized); |
| 726 | } |
| 727 | |
| 728 | static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val) |
| 729 | { |
| 730 | int ret = -EINVAL; |
| 731 | |
| 732 | if (!mutex_trylock(&sw->tb->lock)) |
| 733 | return restart_syscall(); |
| 734 | |
| 735 | if (sw->authorized) |
| 736 | goto unlock; |
| 737 | |
| 738 | /* |
| 739 | * Make sure there is no PCIe rescan ongoing when a new PCIe |
| 740 | * tunnel is created. Otherwise the PCIe rescan code might find |
| 741 | * the new tunnel too early. |
| 742 | */ |
| 743 | pci_lock_rescan_remove(); |
| 744 | pm_runtime_get_sync(&sw->dev); |
| 745 | |
| 746 | switch (val) { |
| 747 | /* Approve switch */ |
| 748 | case 1: |
| 749 | if (sw->key) |
| 750 | ret = tb_domain_approve_switch_key(sw->tb, sw); |
| 751 | else |
| 752 | ret = tb_domain_approve_switch(sw->tb, sw); |
| 753 | break; |
| 754 | |
| 755 | /* Challenge switch */ |
| 756 | case 2: |
| 757 | if (sw->key) |
| 758 | ret = tb_domain_challenge_switch_key(sw->tb, sw); |
| 759 | break; |
| 760 | |
| 761 | default: |
| 762 | break; |
| 763 | } |
| 764 | |
| 765 | pm_runtime_mark_last_busy(&sw->dev); |
| 766 | pm_runtime_put_autosuspend(&sw->dev); |
| 767 | pci_unlock_rescan_remove(); |
| 768 | |
| 769 | if (!ret) { |
| 770 | sw->authorized = val; |
| 771 | /* Notify status change to the userspace */ |
| 772 | kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE); |
| 773 | } |
| 774 | |
| 775 | unlock: |
| 776 | mutex_unlock(&sw->tb->lock); |
| 777 | return ret; |
| 778 | } |
| 779 | |
| 780 | static ssize_t authorized_store(struct device *dev, |
| 781 | struct device_attribute *attr, |
| 782 | const char *buf, size_t count) |
| 783 | { |
| 784 | struct tb_switch *sw = tb_to_switch(dev); |
| 785 | unsigned int val; |
| 786 | ssize_t ret; |
| 787 | |
| 788 | ret = kstrtouint(buf, 0, &val); |
| 789 | if (ret) |
| 790 | return ret; |
| 791 | if (val > 2) |
| 792 | return -EINVAL; |
| 793 | |
| 794 | ret = tb_switch_set_authorized(sw, val); |
| 795 | |
| 796 | return ret ? ret : count; |
| 797 | } |
| 798 | static DEVICE_ATTR_RW(authorized); |
| 799 | |
| 800 | static ssize_t boot_show(struct device *dev, struct device_attribute *attr, |
| 801 | char *buf) |
| 802 | { |
| 803 | struct tb_switch *sw = tb_to_switch(dev); |
| 804 | |
| 805 | return sprintf(buf, "%u\n", sw->boot); |
| 806 | } |
| 807 | static DEVICE_ATTR_RO(boot); |
| 808 | |
| 809 | static ssize_t device_show(struct device *dev, struct device_attribute *attr, |
| 810 | char *buf) |
| 811 | { |
| 812 | struct tb_switch *sw = tb_to_switch(dev); |
| 813 | |
| 814 | return sprintf(buf, "%#x\n", sw->device); |
| 815 | } |
| 816 | static DEVICE_ATTR_RO(device); |
| 817 | |
| 818 | static ssize_t |
| 819 | device_name_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 820 | { |
| 821 | struct tb_switch *sw = tb_to_switch(dev); |
| 822 | |
| 823 | return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : ""); |
| 824 | } |
| 825 | static DEVICE_ATTR_RO(device_name); |
| 826 | |
| 827 | static ssize_t key_show(struct device *dev, struct device_attribute *attr, |
| 828 | char *buf) |
| 829 | { |
| 830 | struct tb_switch *sw = tb_to_switch(dev); |
| 831 | ssize_t ret; |
| 832 | |
| 833 | if (!mutex_trylock(&sw->tb->lock)) |
| 834 | return restart_syscall(); |
| 835 | |
| 836 | if (sw->key) |
| 837 | ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key); |
| 838 | else |
| 839 | ret = sprintf(buf, "\n"); |
| 840 | |
| 841 | mutex_unlock(&sw->tb->lock); |
| 842 | return ret; |
| 843 | } |
| 844 | |
| 845 | static ssize_t key_store(struct device *dev, struct device_attribute *attr, |
| 846 | const char *buf, size_t count) |
| 847 | { |
| 848 | struct tb_switch *sw = tb_to_switch(dev); |
| 849 | u8 key[TB_SWITCH_KEY_SIZE]; |
| 850 | ssize_t ret = count; |
| 851 | bool clear = false; |
| 852 | |
| 853 | if (!strcmp(buf, "\n")) |
| 854 | clear = true; |
| 855 | else if (hex2bin(key, buf, sizeof(key))) |
| 856 | return -EINVAL; |
| 857 | |
| 858 | if (!mutex_trylock(&sw->tb->lock)) |
| 859 | return restart_syscall(); |
| 860 | |
| 861 | if (sw->authorized) { |
| 862 | ret = -EBUSY; |
| 863 | } else { |
| 864 | kfree(sw->key); |
| 865 | if (clear) { |
| 866 | sw->key = NULL; |
| 867 | } else { |
| 868 | sw->key = kmemdup(key, sizeof(key), GFP_KERNEL); |
| 869 | if (!sw->key) |
| 870 | ret = -ENOMEM; |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | mutex_unlock(&sw->tb->lock); |
| 875 | return ret; |
| 876 | } |
| 877 | static DEVICE_ATTR(key, 0600, key_show, key_store); |
| 878 | |
| 879 | static void nvm_authenticate_start(struct tb_switch *sw) |
| 880 | { |
| 881 | struct pci_dev *root_port; |
| 882 | |
| 883 | /* |
| 884 | * During host router NVM upgrade we should not allow root port to |
| 885 | * go into D3cold because some root ports cannot trigger PME |
| 886 | * itself. To be on the safe side keep the root port in D0 during |
| 887 | * the whole upgrade process. |
| 888 | */ |
| 889 | root_port = pci_find_pcie_root_port(sw->tb->nhi->pdev); |
| 890 | if (root_port) |
| 891 | pm_runtime_get_noresume(&root_port->dev); |
| 892 | } |
| 893 | |
| 894 | static void nvm_authenticate_complete(struct tb_switch *sw) |
| 895 | { |
| 896 | struct pci_dev *root_port; |
| 897 | |
| 898 | root_port = pci_find_pcie_root_port(sw->tb->nhi->pdev); |
| 899 | if (root_port) |
| 900 | pm_runtime_put(&root_port->dev); |
| 901 | } |
| 902 | |
| 903 | static ssize_t nvm_authenticate_show(struct device *dev, |
| 904 | struct device_attribute *attr, char *buf) |
| 905 | { |
| 906 | struct tb_switch *sw = tb_to_switch(dev); |
| 907 | u32 status; |
| 908 | |
| 909 | nvm_get_auth_status(sw, &status); |
| 910 | return sprintf(buf, "%#x\n", status); |
| 911 | } |
| 912 | |
| 913 | static ssize_t nvm_authenticate_store(struct device *dev, |
| 914 | struct device_attribute *attr, const char *buf, size_t count) |
| 915 | { |
| 916 | struct tb_switch *sw = tb_to_switch(dev); |
| 917 | bool val; |
| 918 | int ret; |
| 919 | |
| 920 | if (!mutex_trylock(&sw->tb->lock)) |
| 921 | return restart_syscall(); |
| 922 | |
| 923 | /* If NVMem devices are not yet added */ |
| 924 | if (!sw->nvm) { |
| 925 | ret = -EAGAIN; |
| 926 | goto exit_unlock; |
| 927 | } |
| 928 | |
| 929 | ret = kstrtobool(buf, &val); |
| 930 | if (ret) |
| 931 | goto exit_unlock; |
| 932 | |
| 933 | /* Always clear the authentication status */ |
| 934 | nvm_clear_auth_status(sw); |
| 935 | |
| 936 | if (val) { |
| 937 | if (!sw->nvm->buf) { |
| 938 | ret = -EINVAL; |
| 939 | goto exit_unlock; |
| 940 | } |
| 941 | |
| 942 | pm_runtime_get_sync(&sw->dev); |
| 943 | ret = nvm_validate_and_write(sw); |
| 944 | if (ret) { |
| 945 | pm_runtime_mark_last_busy(&sw->dev); |
| 946 | pm_runtime_put_autosuspend(&sw->dev); |
| 947 | goto exit_unlock; |
| 948 | } |
| 949 | |
| 950 | sw->nvm->authenticating = true; |
| 951 | |
| 952 | if (!tb_route(sw)) { |
| 953 | /* |
| 954 | * Keep root port from suspending as long as the |
| 955 | * NVM upgrade process is running. |
| 956 | */ |
| 957 | nvm_authenticate_start(sw); |
| 958 | ret = nvm_authenticate_host(sw); |
| 959 | } else { |
| 960 | ret = nvm_authenticate_device(sw); |
| 961 | } |
| 962 | pm_runtime_mark_last_busy(&sw->dev); |
| 963 | pm_runtime_put_autosuspend(&sw->dev); |
| 964 | } |
| 965 | |
| 966 | exit_unlock: |
| 967 | mutex_unlock(&sw->tb->lock); |
| 968 | |
| 969 | if (ret) |
| 970 | return ret; |
| 971 | return count; |
| 972 | } |
| 973 | static DEVICE_ATTR_RW(nvm_authenticate); |
| 974 | |
| 975 | static ssize_t nvm_version_show(struct device *dev, |
| 976 | struct device_attribute *attr, char *buf) |
| 977 | { |
| 978 | struct tb_switch *sw = tb_to_switch(dev); |
| 979 | int ret; |
| 980 | |
| 981 | if (!mutex_trylock(&sw->tb->lock)) |
| 982 | return restart_syscall(); |
| 983 | |
| 984 | if (sw->safe_mode) |
| 985 | ret = -ENODATA; |
| 986 | else if (!sw->nvm) |
| 987 | ret = -EAGAIN; |
| 988 | else |
| 989 | ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor); |
| 990 | |
| 991 | mutex_unlock(&sw->tb->lock); |
| 992 | |
| 993 | return ret; |
| 994 | } |
| 995 | static DEVICE_ATTR_RO(nvm_version); |
| 996 | |
| 997 | static ssize_t vendor_show(struct device *dev, struct device_attribute *attr, |
| 998 | char *buf) |
| 999 | { |
| 1000 | struct tb_switch *sw = tb_to_switch(dev); |
| 1001 | |
| 1002 | return sprintf(buf, "%#x\n", sw->vendor); |
| 1003 | } |
| 1004 | static DEVICE_ATTR_RO(vendor); |
| 1005 | |
| 1006 | static ssize_t |
| 1007 | vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 1008 | { |
| 1009 | struct tb_switch *sw = tb_to_switch(dev); |
| 1010 | |
| 1011 | return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : ""); |
| 1012 | } |
| 1013 | static DEVICE_ATTR_RO(vendor_name); |
| 1014 | |
| 1015 | static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr, |
| 1016 | char *buf) |
| 1017 | { |
| 1018 | struct tb_switch *sw = tb_to_switch(dev); |
| 1019 | |
| 1020 | return sprintf(buf, "%pUb\n", sw->uuid); |
| 1021 | } |
| 1022 | static DEVICE_ATTR_RO(unique_id); |
| 1023 | |
| 1024 | static struct attribute *switch_attrs[] = { |
| 1025 | &dev_attr_authorized.attr, |
| 1026 | &dev_attr_boot.attr, |
| 1027 | &dev_attr_device.attr, |
| 1028 | &dev_attr_device_name.attr, |
| 1029 | &dev_attr_key.attr, |
| 1030 | &dev_attr_nvm_authenticate.attr, |
| 1031 | &dev_attr_nvm_version.attr, |
| 1032 | &dev_attr_vendor.attr, |
| 1033 | &dev_attr_vendor_name.attr, |
| 1034 | &dev_attr_unique_id.attr, |
| 1035 | NULL, |
| 1036 | }; |
| 1037 | |
| 1038 | static umode_t switch_attr_is_visible(struct kobject *kobj, |
| 1039 | struct attribute *attr, int n) |
| 1040 | { |
| 1041 | struct device *dev = container_of(kobj, struct device, kobj); |
| 1042 | struct tb_switch *sw = tb_to_switch(dev); |
| 1043 | |
| 1044 | if (attr == &dev_attr_key.attr) { |
| 1045 | if (tb_route(sw) && |
| 1046 | sw->tb->security_level == TB_SECURITY_SECURE && |
| 1047 | sw->security_level == TB_SECURITY_SECURE) |
| 1048 | return attr->mode; |
| 1049 | return 0; |
| 1050 | } else if (attr == &dev_attr_nvm_authenticate.attr || |
| 1051 | attr == &dev_attr_nvm_version.attr) { |
| 1052 | if (sw->dma_port) |
| 1053 | return attr->mode; |
| 1054 | return 0; |
| 1055 | } else if (attr == &dev_attr_boot.attr) { |
| 1056 | if (tb_route(sw)) |
| 1057 | return attr->mode; |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
| 1061 | return sw->safe_mode ? 0 : attr->mode; |
| 1062 | } |
| 1063 | |
| 1064 | static struct attribute_group switch_group = { |
| 1065 | .is_visible = switch_attr_is_visible, |
| 1066 | .attrs = switch_attrs, |
| 1067 | }; |
| 1068 | |
| 1069 | static const struct attribute_group *switch_groups[] = { |
| 1070 | &switch_group, |
| 1071 | NULL, |
| 1072 | }; |
| 1073 | |
| 1074 | static void tb_switch_release(struct device *dev) |
| 1075 | { |
| 1076 | struct tb_switch *sw = tb_to_switch(dev); |
| 1077 | |
| 1078 | dma_port_free(sw->dma_port); |
| 1079 | |
| 1080 | kfree(sw->uuid); |
| 1081 | kfree(sw->device_name); |
| 1082 | kfree(sw->vendor_name); |
| 1083 | kfree(sw->ports); |
| 1084 | kfree(sw->drom); |
| 1085 | kfree(sw->key); |
| 1086 | kfree(sw); |
| 1087 | } |
| 1088 | |
| 1089 | /* |
| 1090 | * Currently only need to provide the callbacks. Everything else is handled |
| 1091 | * in the connection manager. |
| 1092 | */ |
| 1093 | static int __maybe_unused tb_switch_runtime_suspend(struct device *dev) |
| 1094 | { |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
| 1098 | static int __maybe_unused tb_switch_runtime_resume(struct device *dev) |
| 1099 | { |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
| 1103 | static const struct dev_pm_ops tb_switch_pm_ops = { |
| 1104 | SET_RUNTIME_PM_OPS(tb_switch_runtime_suspend, tb_switch_runtime_resume, |
| 1105 | NULL) |
| 1106 | }; |
| 1107 | |
| 1108 | struct device_type tb_switch_type = { |
| 1109 | .name = "thunderbolt_device", |
| 1110 | .release = tb_switch_release, |
| 1111 | .pm = &tb_switch_pm_ops, |
| 1112 | }; |
| 1113 | |
| 1114 | static int tb_switch_get_generation(struct tb_switch *sw) |
| 1115 | { |
| 1116 | switch (sw->config.device_id) { |
| 1117 | case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE: |
| 1118 | case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE: |
| 1119 | case PCI_DEVICE_ID_INTEL_LIGHT_PEAK: |
| 1120 | case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C: |
| 1121 | case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C: |
| 1122 | case PCI_DEVICE_ID_INTEL_PORT_RIDGE: |
| 1123 | case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE: |
| 1124 | case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE: |
| 1125 | return 1; |
| 1126 | |
| 1127 | case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE: |
| 1128 | case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE: |
| 1129 | case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE: |
| 1130 | return 2; |
| 1131 | |
| 1132 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE: |
| 1133 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE: |
| 1134 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE: |
| 1135 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE: |
| 1136 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE: |
| 1137 | case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE: |
| 1138 | case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE: |
| 1139 | case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE: |
| 1140 | return 3; |
| 1141 | |
| 1142 | default: |
| 1143 | /* |
| 1144 | * For unknown switches assume generation to be 1 to be |
| 1145 | * on the safe side. |
| 1146 | */ |
| 1147 | tb_sw_warn(sw, "unsupported switch device id %#x\n", |
| 1148 | sw->config.device_id); |
| 1149 | return 1; |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | /** |
| 1154 | * tb_switch_alloc() - allocate a switch |
| 1155 | * @tb: Pointer to the owning domain |
| 1156 | * @parent: Parent device for this switch |
| 1157 | * @route: Route string for this switch |
| 1158 | * |
| 1159 | * Allocates and initializes a switch. Will not upload configuration to |
| 1160 | * the switch. For that you need to call tb_switch_configure() |
| 1161 | * separately. The returned switch should be released by calling |
| 1162 | * tb_switch_put(). |
| 1163 | * |
| 1164 | * Return: Pointer to the allocated switch or %NULL in case of failure |
| 1165 | */ |
| 1166 | struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent, |
| 1167 | u64 route) |
| 1168 | { |
| 1169 | int i; |
| 1170 | int cap; |
| 1171 | struct tb_switch *sw; |
| 1172 | int upstream_port = tb_cfg_get_upstream_port(tb->ctl, route); |
| 1173 | if (upstream_port < 0) |
| 1174 | return NULL; |
| 1175 | |
| 1176 | sw = kzalloc(sizeof(*sw), GFP_KERNEL); |
| 1177 | if (!sw) |
| 1178 | return NULL; |
| 1179 | |
| 1180 | sw->tb = tb; |
| 1181 | if (tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5)) |
| 1182 | goto err_free_sw_ports; |
| 1183 | |
| 1184 | tb_info(tb, "current switch config:\n"); |
| 1185 | tb_dump_switch(tb, &sw->config); |
| 1186 | |
| 1187 | /* configure switch */ |
| 1188 | sw->config.upstream_port_number = upstream_port; |
| 1189 | sw->config.depth = tb_route_length(route); |
| 1190 | sw->config.route_lo = route; |
| 1191 | sw->config.route_hi = route >> 32; |
| 1192 | sw->config.enabled = 0; |
| 1193 | |
| 1194 | /* initialize ports */ |
| 1195 | sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports), |
| 1196 | GFP_KERNEL); |
| 1197 | if (!sw->ports) |
| 1198 | goto err_free_sw_ports; |
| 1199 | |
| 1200 | for (i = 0; i <= sw->config.max_port_number; i++) { |
| 1201 | /* minimum setup for tb_find_cap and tb_drom_read to work */ |
| 1202 | sw->ports[i].sw = sw; |
| 1203 | sw->ports[i].port = i; |
| 1204 | } |
| 1205 | |
| 1206 | sw->generation = tb_switch_get_generation(sw); |
| 1207 | |
| 1208 | cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS); |
| 1209 | if (cap < 0) { |
| 1210 | tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n"); |
| 1211 | goto err_free_sw_ports; |
| 1212 | } |
| 1213 | sw->cap_plug_events = cap; |
| 1214 | |
| 1215 | /* Root switch is always authorized */ |
| 1216 | if (!route) |
| 1217 | sw->authorized = true; |
| 1218 | |
| 1219 | device_initialize(&sw->dev); |
| 1220 | sw->dev.parent = parent; |
| 1221 | sw->dev.bus = &tb_bus_type; |
| 1222 | sw->dev.type = &tb_switch_type; |
| 1223 | sw->dev.groups = switch_groups; |
| 1224 | dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); |
| 1225 | |
| 1226 | return sw; |
| 1227 | |
| 1228 | err_free_sw_ports: |
| 1229 | kfree(sw->ports); |
| 1230 | kfree(sw); |
| 1231 | |
| 1232 | return NULL; |
| 1233 | } |
| 1234 | |
| 1235 | /** |
| 1236 | * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode |
| 1237 | * @tb: Pointer to the owning domain |
| 1238 | * @parent: Parent device for this switch |
| 1239 | * @route: Route string for this switch |
| 1240 | * |
| 1241 | * This creates a switch in safe mode. This means the switch pretty much |
| 1242 | * lacks all capabilities except DMA configuration port before it is |
| 1243 | * flashed with a valid NVM firmware. |
| 1244 | * |
| 1245 | * The returned switch must be released by calling tb_switch_put(). |
| 1246 | * |
| 1247 | * Return: Pointer to the allocated switch or %NULL in case of failure |
| 1248 | */ |
| 1249 | struct tb_switch * |
| 1250 | tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route) |
| 1251 | { |
| 1252 | struct tb_switch *sw; |
| 1253 | |
| 1254 | sw = kzalloc(sizeof(*sw), GFP_KERNEL); |
| 1255 | if (!sw) |
| 1256 | return NULL; |
| 1257 | |
| 1258 | sw->tb = tb; |
| 1259 | sw->config.depth = tb_route_length(route); |
| 1260 | sw->config.route_hi = upper_32_bits(route); |
| 1261 | sw->config.route_lo = lower_32_bits(route); |
| 1262 | sw->safe_mode = true; |
| 1263 | |
| 1264 | device_initialize(&sw->dev); |
| 1265 | sw->dev.parent = parent; |
| 1266 | sw->dev.bus = &tb_bus_type; |
| 1267 | sw->dev.type = &tb_switch_type; |
| 1268 | sw->dev.groups = switch_groups; |
| 1269 | dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); |
| 1270 | |
| 1271 | return sw; |
| 1272 | } |
| 1273 | |
| 1274 | /** |
| 1275 | * tb_switch_configure() - Uploads configuration to the switch |
| 1276 | * @sw: Switch to configure |
| 1277 | * |
| 1278 | * Call this function before the switch is added to the system. It will |
| 1279 | * upload configuration to the switch and makes it available for the |
| 1280 | * connection manager to use. |
| 1281 | * |
| 1282 | * Return: %0 in case of success and negative errno in case of failure |
| 1283 | */ |
| 1284 | int tb_switch_configure(struct tb_switch *sw) |
| 1285 | { |
| 1286 | struct tb *tb = sw->tb; |
| 1287 | u64 route; |
| 1288 | int ret; |
| 1289 | |
| 1290 | route = tb_route(sw); |
| 1291 | tb_info(tb, |
| 1292 | "initializing Switch at %#llx (depth: %d, up port: %d)\n", |
| 1293 | route, tb_route_length(route), sw->config.upstream_port_number); |
| 1294 | |
| 1295 | if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL) |
| 1296 | tb_sw_warn(sw, "unknown switch vendor id %#x\n", |
| 1297 | sw->config.vendor_id); |
| 1298 | |
| 1299 | sw->config.enabled = 1; |
| 1300 | |
| 1301 | /* upload configuration */ |
| 1302 | ret = tb_sw_write(sw, 1 + (u32 *)&sw->config, TB_CFG_SWITCH, 1, 3); |
| 1303 | if (ret) |
| 1304 | return ret; |
| 1305 | |
| 1306 | return tb_plug_events_active(sw, true); |
| 1307 | } |
| 1308 | |
| 1309 | static int tb_switch_set_uuid(struct tb_switch *sw) |
| 1310 | { |
| 1311 | u32 uuid[4]; |
| 1312 | int cap, ret; |
| 1313 | |
| 1314 | ret = 0; |
| 1315 | if (sw->uuid) |
| 1316 | return ret; |
| 1317 | |
| 1318 | /* |
| 1319 | * The newer controllers include fused UUID as part of link |
| 1320 | * controller specific registers |
| 1321 | */ |
| 1322 | cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER); |
| 1323 | if (cap > 0) { |
| 1324 | ret = tb_sw_read(sw, uuid, TB_CFG_SWITCH, cap + 3, 4); |
| 1325 | if (ret) |
| 1326 | return ret; |
| 1327 | } else { |
| 1328 | /* |
| 1329 | * ICM generates UUID based on UID and fills the upper |
| 1330 | * two words with ones. This is not strictly following |
| 1331 | * UUID format but we want to be compatible with it so |
| 1332 | * we do the same here. |
| 1333 | */ |
| 1334 | uuid[0] = sw->uid & 0xffffffff; |
| 1335 | uuid[1] = (sw->uid >> 32) & 0xffffffff; |
| 1336 | uuid[2] = 0xffffffff; |
| 1337 | uuid[3] = 0xffffffff; |
| 1338 | } |
| 1339 | |
| 1340 | sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL); |
| 1341 | if (!sw->uuid) |
| 1342 | ret = -ENOMEM; |
| 1343 | return ret; |
| 1344 | } |
| 1345 | |
| 1346 | static int tb_switch_add_dma_port(struct tb_switch *sw) |
| 1347 | { |
| 1348 | u32 status; |
| 1349 | int ret; |
| 1350 | |
| 1351 | switch (sw->generation) { |
| 1352 | case 2: |
| 1353 | /* Only root switch can be upgraded */ |
| 1354 | if (tb_route(sw)) |
| 1355 | return 0; |
| 1356 | |
| 1357 | /* fallthrough */ |
| 1358 | case 3: |
| 1359 | ret = tb_switch_set_uuid(sw); |
| 1360 | if (ret) |
| 1361 | return ret; |
| 1362 | break; |
| 1363 | |
| 1364 | default: |
| 1365 | /* |
| 1366 | * DMA port is the only thing available when the switch |
| 1367 | * is in safe mode. |
| 1368 | */ |
| 1369 | if (!sw->safe_mode) |
| 1370 | return 0; |
| 1371 | break; |
| 1372 | } |
| 1373 | |
| 1374 | if (sw->no_nvm_upgrade) |
| 1375 | return 0; |
| 1376 | |
| 1377 | sw->dma_port = dma_port_alloc(sw); |
| 1378 | if (!sw->dma_port) |
| 1379 | return 0; |
| 1380 | |
| 1381 | /* |
| 1382 | * If there is status already set then authentication failed |
| 1383 | * when the dma_port_flash_update_auth() returned. Power cycling |
| 1384 | * is not needed (it was done already) so only thing we do here |
| 1385 | * is to unblock runtime PM of the root port. |
| 1386 | */ |
| 1387 | nvm_get_auth_status(sw, &status); |
| 1388 | if (status) { |
| 1389 | if (!tb_route(sw)) |
| 1390 | nvm_authenticate_complete(sw); |
| 1391 | return 0; |
| 1392 | } |
| 1393 | |
| 1394 | /* |
| 1395 | * Check status of the previous flash authentication. If there |
| 1396 | * is one we need to power cycle the switch in any case to make |
| 1397 | * it functional again. |
| 1398 | */ |
| 1399 | ret = dma_port_flash_update_auth_status(sw->dma_port, &status); |
| 1400 | if (ret <= 0) |
| 1401 | return ret; |
| 1402 | |
| 1403 | /* Now we can allow root port to suspend again */ |
| 1404 | if (!tb_route(sw)) |
| 1405 | nvm_authenticate_complete(sw); |
| 1406 | |
| 1407 | if (status) { |
| 1408 | tb_sw_info(sw, "switch flash authentication failed\n"); |
| 1409 | nvm_set_auth_status(sw, status); |
| 1410 | } |
| 1411 | |
| 1412 | tb_sw_info(sw, "power cycling the switch now\n"); |
| 1413 | dma_port_power_cycle(sw->dma_port); |
| 1414 | |
| 1415 | /* |
| 1416 | * We return error here which causes the switch adding failure. |
| 1417 | * It should appear back after power cycle is complete. |
| 1418 | */ |
| 1419 | return -ESHUTDOWN; |
| 1420 | } |
| 1421 | |
| 1422 | /** |
| 1423 | * tb_switch_add() - Add a switch to the domain |
| 1424 | * @sw: Switch to add |
| 1425 | * |
| 1426 | * This is the last step in adding switch to the domain. It will read |
| 1427 | * identification information from DROM and initializes ports so that |
| 1428 | * they can be used to connect other switches. The switch will be |
| 1429 | * exposed to the userspace when this function successfully returns. To |
| 1430 | * remove and release the switch, call tb_switch_remove(). |
| 1431 | * |
| 1432 | * Return: %0 in case of success and negative errno in case of failure |
| 1433 | */ |
| 1434 | int tb_switch_add(struct tb_switch *sw) |
| 1435 | { |
| 1436 | int i, ret; |
| 1437 | |
| 1438 | /* |
| 1439 | * Initialize DMA control port now before we read DROM. Recent |
| 1440 | * host controllers have more complete DROM on NVM that includes |
| 1441 | * vendor and model identification strings which we then expose |
| 1442 | * to the userspace. NVM can be accessed through DMA |
| 1443 | * configuration based mailbox. |
| 1444 | */ |
| 1445 | ret = tb_switch_add_dma_port(sw); |
| 1446 | if (ret) |
| 1447 | return ret; |
| 1448 | |
| 1449 | if (!sw->safe_mode) { |
| 1450 | /* read drom */ |
| 1451 | ret = tb_drom_read(sw); |
| 1452 | if (ret) { |
| 1453 | tb_sw_warn(sw, "tb_eeprom_read_rom failed\n"); |
| 1454 | return ret; |
| 1455 | } |
| 1456 | tb_sw_info(sw, "uid: %#llx\n", sw->uid); |
| 1457 | |
| 1458 | ret = tb_switch_set_uuid(sw); |
| 1459 | if (ret) |
| 1460 | return ret; |
| 1461 | |
| 1462 | for (i = 0; i <= sw->config.max_port_number; i++) { |
| 1463 | if (sw->ports[i].disabled) { |
| 1464 | tb_port_info(&sw->ports[i], "disabled by eeprom\n"); |
| 1465 | continue; |
| 1466 | } |
| 1467 | ret = tb_init_port(&sw->ports[i]); |
| 1468 | if (ret) |
| 1469 | return ret; |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | ret = device_add(&sw->dev); |
| 1474 | if (ret) |
| 1475 | return ret; |
| 1476 | |
| 1477 | ret = tb_switch_nvm_add(sw); |
| 1478 | if (ret) { |
| 1479 | device_del(&sw->dev); |
| 1480 | return ret; |
| 1481 | } |
| 1482 | |
| 1483 | pm_runtime_set_active(&sw->dev); |
| 1484 | if (sw->rpm) { |
| 1485 | pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY); |
| 1486 | pm_runtime_use_autosuspend(&sw->dev); |
| 1487 | pm_runtime_mark_last_busy(&sw->dev); |
| 1488 | pm_runtime_enable(&sw->dev); |
| 1489 | pm_request_autosuspend(&sw->dev); |
| 1490 | } |
| 1491 | |
| 1492 | return 0; |
| 1493 | } |
| 1494 | |
| 1495 | /** |
| 1496 | * tb_switch_remove() - Remove and release a switch |
| 1497 | * @sw: Switch to remove |
| 1498 | * |
| 1499 | * This will remove the switch from the domain and release it after last |
| 1500 | * reference count drops to zero. If there are switches connected below |
| 1501 | * this switch, they will be removed as well. |
| 1502 | */ |
| 1503 | void tb_switch_remove(struct tb_switch *sw) |
| 1504 | { |
| 1505 | int i; |
| 1506 | |
| 1507 | if (sw->rpm) { |
| 1508 | pm_runtime_get_sync(&sw->dev); |
| 1509 | pm_runtime_disable(&sw->dev); |
| 1510 | } |
| 1511 | |
| 1512 | /* port 0 is the switch itself and never has a remote */ |
| 1513 | for (i = 1; i <= sw->config.max_port_number; i++) { |
| 1514 | if (tb_is_upstream_port(&sw->ports[i])) |
| 1515 | continue; |
| 1516 | if (sw->ports[i].remote) |
| 1517 | tb_switch_remove(sw->ports[i].remote->sw); |
| 1518 | sw->ports[i].remote = NULL; |
| 1519 | if (sw->ports[i].xdomain) |
| 1520 | tb_xdomain_remove(sw->ports[i].xdomain); |
| 1521 | sw->ports[i].xdomain = NULL; |
| 1522 | } |
| 1523 | |
| 1524 | if (!sw->is_unplugged) |
| 1525 | tb_plug_events_active(sw, false); |
| 1526 | |
| 1527 | tb_switch_nvm_remove(sw); |
| 1528 | device_unregister(&sw->dev); |
| 1529 | } |
| 1530 | |
| 1531 | /** |
| 1532 | * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches |
| 1533 | */ |
| 1534 | void tb_sw_set_unplugged(struct tb_switch *sw) |
| 1535 | { |
| 1536 | int i; |
| 1537 | if (sw == sw->tb->root_switch) { |
| 1538 | tb_sw_WARN(sw, "cannot unplug root switch\n"); |
| 1539 | return; |
| 1540 | } |
| 1541 | if (sw->is_unplugged) { |
| 1542 | tb_sw_WARN(sw, "is_unplugged already set\n"); |
| 1543 | return; |
| 1544 | } |
| 1545 | sw->is_unplugged = true; |
| 1546 | for (i = 0; i <= sw->config.max_port_number; i++) { |
| 1547 | if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote) |
| 1548 | tb_sw_set_unplugged(sw->ports[i].remote->sw); |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | int tb_switch_resume(struct tb_switch *sw) |
| 1553 | { |
| 1554 | int i, err; |
| 1555 | tb_sw_info(sw, "resuming switch\n"); |
| 1556 | |
| 1557 | /* |
| 1558 | * Check for UID of the connected switches except for root |
| 1559 | * switch which we assume cannot be removed. |
| 1560 | */ |
| 1561 | if (tb_route(sw)) { |
| 1562 | u64 uid; |
| 1563 | |
| 1564 | err = tb_drom_read_uid_only(sw, &uid); |
| 1565 | if (err) { |
| 1566 | tb_sw_warn(sw, "uid read failed\n"); |
| 1567 | return err; |
| 1568 | } |
| 1569 | if (sw->uid != uid) { |
| 1570 | tb_sw_info(sw, |
| 1571 | "changed while suspended (uid %#llx -> %#llx)\n", |
| 1572 | sw->uid, uid); |
| 1573 | return -ENODEV; |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | /* upload configuration */ |
| 1578 | err = tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3); |
| 1579 | if (err) |
| 1580 | return err; |
| 1581 | |
| 1582 | err = tb_plug_events_active(sw, true); |
| 1583 | if (err) |
| 1584 | return err; |
| 1585 | |
| 1586 | /* check for surviving downstream switches */ |
| 1587 | for (i = 1; i <= sw->config.max_port_number; i++) { |
| 1588 | struct tb_port *port = &sw->ports[i]; |
| 1589 | if (tb_is_upstream_port(port)) |
| 1590 | continue; |
| 1591 | if (!port->remote) |
| 1592 | continue; |
| 1593 | if (tb_wait_for_port(port, true) <= 0 |
| 1594 | || tb_switch_resume(port->remote->sw)) { |
| 1595 | tb_port_warn(port, |
| 1596 | "lost during suspend, disconnecting\n"); |
| 1597 | tb_sw_set_unplugged(port->remote->sw); |
| 1598 | } |
| 1599 | } |
| 1600 | return 0; |
| 1601 | } |
| 1602 | |
| 1603 | void tb_switch_suspend(struct tb_switch *sw) |
| 1604 | { |
| 1605 | int i, err; |
| 1606 | err = tb_plug_events_active(sw, false); |
| 1607 | if (err) |
| 1608 | return; |
| 1609 | |
| 1610 | for (i = 1; i <= sw->config.max_port_number; i++) { |
| 1611 | if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote) |
| 1612 | tb_switch_suspend(sw->ports[i].remote->sw); |
| 1613 | } |
| 1614 | /* |
| 1615 | * TODO: invoke tb_cfg_prepare_to_sleep here? does not seem to have any |
| 1616 | * effect? |
| 1617 | */ |
| 1618 | } |
| 1619 | |
| 1620 | struct tb_sw_lookup { |
| 1621 | struct tb *tb; |
| 1622 | u8 link; |
| 1623 | u8 depth; |
| 1624 | const uuid_t *uuid; |
| 1625 | u64 route; |
| 1626 | }; |
| 1627 | |
| 1628 | static int tb_switch_match(struct device *dev, void *data) |
| 1629 | { |
| 1630 | struct tb_switch *sw = tb_to_switch(dev); |
| 1631 | struct tb_sw_lookup *lookup = data; |
| 1632 | |
| 1633 | if (!sw) |
| 1634 | return 0; |
| 1635 | if (sw->tb != lookup->tb) |
| 1636 | return 0; |
| 1637 | |
| 1638 | if (lookup->uuid) |
| 1639 | return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid)); |
| 1640 | |
| 1641 | if (lookup->route) { |
| 1642 | return sw->config.route_lo == lower_32_bits(lookup->route) && |
| 1643 | sw->config.route_hi == upper_32_bits(lookup->route); |
| 1644 | } |
| 1645 | |
| 1646 | /* Root switch is matched only by depth */ |
| 1647 | if (!lookup->depth) |
| 1648 | return !sw->depth; |
| 1649 | |
| 1650 | return sw->link == lookup->link && sw->depth == lookup->depth; |
| 1651 | } |
| 1652 | |
| 1653 | /** |
| 1654 | * tb_switch_find_by_link_depth() - Find switch by link and depth |
| 1655 | * @tb: Domain the switch belongs |
| 1656 | * @link: Link number the switch is connected |
| 1657 | * @depth: Depth of the switch in link |
| 1658 | * |
| 1659 | * Returned switch has reference count increased so the caller needs to |
| 1660 | * call tb_switch_put() when done with the switch. |
| 1661 | */ |
| 1662 | struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth) |
| 1663 | { |
| 1664 | struct tb_sw_lookup lookup; |
| 1665 | struct device *dev; |
| 1666 | |
| 1667 | memset(&lookup, 0, sizeof(lookup)); |
| 1668 | lookup.tb = tb; |
| 1669 | lookup.link = link; |
| 1670 | lookup.depth = depth; |
| 1671 | |
| 1672 | dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match); |
| 1673 | if (dev) |
| 1674 | return tb_to_switch(dev); |
| 1675 | |
| 1676 | return NULL; |
| 1677 | } |
| 1678 | |
| 1679 | /** |
| 1680 | * tb_switch_find_by_uuid() - Find switch by UUID |
| 1681 | * @tb: Domain the switch belongs |
| 1682 | * @uuid: UUID to look for |
| 1683 | * |
| 1684 | * Returned switch has reference count increased so the caller needs to |
| 1685 | * call tb_switch_put() when done with the switch. |
| 1686 | */ |
| 1687 | struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid) |
| 1688 | { |
| 1689 | struct tb_sw_lookup lookup; |
| 1690 | struct device *dev; |
| 1691 | |
| 1692 | memset(&lookup, 0, sizeof(lookup)); |
| 1693 | lookup.tb = tb; |
| 1694 | lookup.uuid = uuid; |
| 1695 | |
| 1696 | dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match); |
| 1697 | if (dev) |
| 1698 | return tb_to_switch(dev); |
| 1699 | |
| 1700 | return NULL; |
| 1701 | } |
| 1702 | |
| 1703 | /** |
| 1704 | * tb_switch_find_by_route() - Find switch by route string |
| 1705 | * @tb: Domain the switch belongs |
| 1706 | * @route: Route string to look for |
| 1707 | * |
| 1708 | * Returned switch has reference count increased so the caller needs to |
| 1709 | * call tb_switch_put() when done with the switch. |
| 1710 | */ |
| 1711 | struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route) |
| 1712 | { |
| 1713 | struct tb_sw_lookup lookup; |
| 1714 | struct device *dev; |
| 1715 | |
| 1716 | if (!route) |
| 1717 | return tb_switch_get(tb->root_switch); |
| 1718 | |
| 1719 | memset(&lookup, 0, sizeof(lookup)); |
| 1720 | lookup.tb = tb; |
| 1721 | lookup.route = route; |
| 1722 | |
| 1723 | dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match); |
| 1724 | if (dev) |
| 1725 | return tb_to_switch(dev); |
| 1726 | |
| 1727 | return NULL; |
| 1728 | } |
| 1729 | |
| 1730 | void tb_switch_exit(void) |
| 1731 | { |
| 1732 | ida_destroy(&nvm_ida); |
| 1733 | } |