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