Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 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> |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 9 | #include <linux/idr.h> |
| 10 | #include <linux/nvmem-provider.h> |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 11 | #include <linux/pm_runtime.h> |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 12 | #include <linux/sizes.h> |
Sachin Kamat | 10fefe5 | 2014-06-20 14:32:30 +0530 | [diff] [blame] | 13 | #include <linux/slab.h> |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 14 | #include <linux/vmalloc.h> |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 15 | |
| 16 | #include "tb.h" |
| 17 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 18 | /* Switch authorization from userspace is serialized by this lock */ |
| 19 | static DEFINE_MUTEX(switch_lock); |
| 20 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 21 | /* Switch NVM support */ |
| 22 | |
| 23 | #define NVM_DEVID 0x05 |
| 24 | #define NVM_VERSION 0x08 |
| 25 | #define NVM_CSS 0x10 |
| 26 | #define NVM_FLASH_SIZE 0x45 |
| 27 | |
| 28 | #define NVM_MIN_SIZE SZ_32K |
| 29 | #define NVM_MAX_SIZE SZ_512K |
| 30 | |
| 31 | static DEFINE_IDA(nvm_ida); |
| 32 | |
| 33 | struct nvm_auth_status { |
| 34 | struct list_head list; |
Christoph Hellwig | 7c39ffe | 2017-07-18 15:30:05 +0200 | [diff] [blame] | 35 | uuid_t uuid; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 36 | u32 status; |
| 37 | }; |
| 38 | |
| 39 | /* |
| 40 | * Hold NVM authentication failure status per switch This information |
| 41 | * needs to stay around even when the switch gets power cycled so we |
| 42 | * keep it separately. |
| 43 | */ |
| 44 | static LIST_HEAD(nvm_auth_status_cache); |
| 45 | static DEFINE_MUTEX(nvm_auth_status_lock); |
| 46 | |
| 47 | static struct nvm_auth_status *__nvm_get_auth_status(const struct tb_switch *sw) |
| 48 | { |
| 49 | struct nvm_auth_status *st; |
| 50 | |
| 51 | list_for_each_entry(st, &nvm_auth_status_cache, list) { |
Christoph Hellwig | 7c39ffe | 2017-07-18 15:30:05 +0200 | [diff] [blame] | 52 | if (uuid_equal(&st->uuid, sw->uuid)) |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 53 | return st; |
| 54 | } |
| 55 | |
| 56 | return NULL; |
| 57 | } |
| 58 | |
| 59 | static void nvm_get_auth_status(const struct tb_switch *sw, u32 *status) |
| 60 | { |
| 61 | struct nvm_auth_status *st; |
| 62 | |
| 63 | mutex_lock(&nvm_auth_status_lock); |
| 64 | st = __nvm_get_auth_status(sw); |
| 65 | mutex_unlock(&nvm_auth_status_lock); |
| 66 | |
| 67 | *status = st ? st->status : 0; |
| 68 | } |
| 69 | |
| 70 | static void nvm_set_auth_status(const struct tb_switch *sw, u32 status) |
| 71 | { |
| 72 | struct nvm_auth_status *st; |
| 73 | |
| 74 | if (WARN_ON(!sw->uuid)) |
| 75 | return; |
| 76 | |
| 77 | mutex_lock(&nvm_auth_status_lock); |
| 78 | st = __nvm_get_auth_status(sw); |
| 79 | |
| 80 | if (!st) { |
| 81 | st = kzalloc(sizeof(*st), GFP_KERNEL); |
| 82 | if (!st) |
| 83 | goto unlock; |
| 84 | |
| 85 | memcpy(&st->uuid, sw->uuid, sizeof(st->uuid)); |
| 86 | INIT_LIST_HEAD(&st->list); |
| 87 | list_add_tail(&st->list, &nvm_auth_status_cache); |
| 88 | } |
| 89 | |
| 90 | st->status = status; |
| 91 | unlock: |
| 92 | mutex_unlock(&nvm_auth_status_lock); |
| 93 | } |
| 94 | |
| 95 | static void nvm_clear_auth_status(const struct tb_switch *sw) |
| 96 | { |
| 97 | struct nvm_auth_status *st; |
| 98 | |
| 99 | mutex_lock(&nvm_auth_status_lock); |
| 100 | st = __nvm_get_auth_status(sw); |
| 101 | if (st) { |
| 102 | list_del(&st->list); |
| 103 | kfree(st); |
| 104 | } |
| 105 | mutex_unlock(&nvm_auth_status_lock); |
| 106 | } |
| 107 | |
| 108 | static int nvm_validate_and_write(struct tb_switch *sw) |
| 109 | { |
| 110 | unsigned int image_size, hdr_size; |
| 111 | const u8 *buf = sw->nvm->buf; |
| 112 | u16 ds_size; |
| 113 | int ret; |
| 114 | |
| 115 | if (!buf) |
| 116 | return -EINVAL; |
| 117 | |
| 118 | image_size = sw->nvm->buf_data_size; |
| 119 | if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE) |
| 120 | return -EINVAL; |
| 121 | |
| 122 | /* |
| 123 | * FARB pointer must point inside the image and must at least |
| 124 | * contain parts of the digital section we will be reading here. |
| 125 | */ |
| 126 | hdr_size = (*(u32 *)buf) & 0xffffff; |
| 127 | if (hdr_size + NVM_DEVID + 2 >= image_size) |
| 128 | return -EINVAL; |
| 129 | |
| 130 | /* Digital section start should be aligned to 4k page */ |
| 131 | if (!IS_ALIGNED(hdr_size, SZ_4K)) |
| 132 | return -EINVAL; |
| 133 | |
| 134 | /* |
| 135 | * Read digital section size and check that it also fits inside |
| 136 | * the image. |
| 137 | */ |
| 138 | ds_size = *(u16 *)(buf + hdr_size); |
| 139 | if (ds_size >= image_size) |
| 140 | return -EINVAL; |
| 141 | |
| 142 | if (!sw->safe_mode) { |
| 143 | u16 device_id; |
| 144 | |
| 145 | /* |
| 146 | * Make sure the device ID in the image matches the one |
| 147 | * we read from the switch config space. |
| 148 | */ |
| 149 | device_id = *(u16 *)(buf + hdr_size + NVM_DEVID); |
| 150 | if (device_id != sw->config.device_id) |
| 151 | return -EINVAL; |
| 152 | |
| 153 | if (sw->generation < 3) { |
| 154 | /* Write CSS headers first */ |
| 155 | ret = dma_port_flash_write(sw->dma_port, |
| 156 | DMA_PORT_CSS_ADDRESS, buf + NVM_CSS, |
| 157 | DMA_PORT_CSS_MAX_SIZE); |
| 158 | if (ret) |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | /* Skip headers in the image */ |
| 163 | buf += hdr_size; |
| 164 | image_size -= hdr_size; |
| 165 | } |
| 166 | |
| 167 | return dma_port_flash_write(sw->dma_port, 0, buf, image_size); |
| 168 | } |
| 169 | |
| 170 | static int nvm_authenticate_host(struct tb_switch *sw) |
| 171 | { |
| 172 | int ret; |
| 173 | |
| 174 | /* |
| 175 | * Root switch NVM upgrade requires that we disconnect the |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 176 | * existing paths first (in case it is not in safe mode |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 177 | * already). |
| 178 | */ |
| 179 | if (!sw->safe_mode) { |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 180 | ret = tb_domain_disconnect_all_paths(sw->tb); |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 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 | return ret == -ETIMEDOUT ? 0 : ret; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * From safe mode we can get out by just power cycling the |
| 193 | * switch. |
| 194 | */ |
| 195 | dma_port_power_cycle(sw->dma_port); |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static int nvm_authenticate_device(struct tb_switch *sw) |
| 200 | { |
| 201 | int ret, retries = 10; |
| 202 | |
| 203 | ret = dma_port_flash_update_auth(sw->dma_port); |
| 204 | if (ret && ret != -ETIMEDOUT) |
| 205 | return ret; |
| 206 | |
| 207 | /* |
| 208 | * Poll here for the authentication status. It takes some time |
| 209 | * for the device to respond (we get timeout for a while). Once |
| 210 | * we get response the device needs to be power cycled in order |
| 211 | * to the new NVM to be taken into use. |
| 212 | */ |
| 213 | do { |
| 214 | u32 status; |
| 215 | |
| 216 | ret = dma_port_flash_update_auth_status(sw->dma_port, &status); |
| 217 | if (ret < 0 && ret != -ETIMEDOUT) |
| 218 | return ret; |
| 219 | if (ret > 0) { |
| 220 | if (status) { |
| 221 | tb_sw_warn(sw, "failed to authenticate NVM\n"); |
| 222 | nvm_set_auth_status(sw, status); |
| 223 | } |
| 224 | |
| 225 | tb_sw_info(sw, "power cycling the switch now\n"); |
| 226 | dma_port_power_cycle(sw->dma_port); |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | msleep(500); |
| 231 | } while (--retries); |
| 232 | |
| 233 | return -ETIMEDOUT; |
| 234 | } |
| 235 | |
| 236 | static int tb_switch_nvm_read(void *priv, unsigned int offset, void *val, |
| 237 | size_t bytes) |
| 238 | { |
| 239 | struct tb_switch *sw = priv; |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 240 | int ret; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 241 | |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 242 | pm_runtime_get_sync(&sw->dev); |
| 243 | ret = dma_port_flash_read(sw->dma_port, offset, val, bytes); |
| 244 | pm_runtime_mark_last_busy(&sw->dev); |
| 245 | pm_runtime_put_autosuspend(&sw->dev); |
| 246 | |
| 247 | return ret; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | static int tb_switch_nvm_write(void *priv, unsigned int offset, void *val, |
| 251 | size_t bytes) |
| 252 | { |
| 253 | struct tb_switch *sw = priv; |
| 254 | int ret = 0; |
| 255 | |
| 256 | if (mutex_lock_interruptible(&switch_lock)) |
| 257 | return -ERESTARTSYS; |
| 258 | |
| 259 | /* |
| 260 | * Since writing the NVM image might require some special steps, |
| 261 | * for example when CSS headers are written, we cache the image |
| 262 | * locally here and handle the special cases when the user asks |
| 263 | * us to authenticate the image. |
| 264 | */ |
| 265 | if (!sw->nvm->buf) { |
| 266 | sw->nvm->buf = vmalloc(NVM_MAX_SIZE); |
| 267 | if (!sw->nvm->buf) { |
| 268 | ret = -ENOMEM; |
| 269 | goto unlock; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | sw->nvm->buf_data_size = offset + bytes; |
| 274 | memcpy(sw->nvm->buf + offset, val, bytes); |
| 275 | |
| 276 | unlock: |
| 277 | mutex_unlock(&switch_lock); |
| 278 | |
| 279 | return ret; |
| 280 | } |
| 281 | |
| 282 | static struct nvmem_device *register_nvmem(struct tb_switch *sw, int id, |
| 283 | size_t size, bool active) |
| 284 | { |
| 285 | struct nvmem_config config; |
| 286 | |
| 287 | memset(&config, 0, sizeof(config)); |
| 288 | |
| 289 | if (active) { |
| 290 | config.name = "nvm_active"; |
| 291 | config.reg_read = tb_switch_nvm_read; |
Mika Westerberg | 800161b | 2017-06-29 14:19:50 +0300 | [diff] [blame] | 292 | config.read_only = true; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 293 | } else { |
| 294 | config.name = "nvm_non_active"; |
| 295 | config.reg_write = tb_switch_nvm_write; |
Mika Westerberg | 800161b | 2017-06-29 14:19:50 +0300 | [diff] [blame] | 296 | config.root_only = true; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 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; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 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 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 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 | { |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 439 | tb_dbg(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_dbg(tb, " Max hop id (in/out): %d/%d\n", |
| 445 | port->max_in_hop_id, port->max_out_hop_id); |
| 446 | tb_dbg(tb, " Max counters: %d\n", port->max_counters); |
| 447 | tb_dbg(tb, " NFC Credits: %#x\n", port->nfc_credits); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /** |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 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 | /** |
Andreas Noever | 520b670 | 2014-06-03 22:04:07 +0200 | [diff] [blame] | 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 | /** |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 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 | */ |
Andreas Noever | 343fcb8 | 2014-06-12 23:11:47 +0200 | [diff] [blame] | 578 | static int tb_init_port(struct tb_port *port) |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 579 | { |
| 580 | int res; |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 581 | int cap; |
Andreas Noever | 343fcb8 | 2014-06-12 23:11:47 +0200 | [diff] [blame] | 582 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 583 | res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8); |
| 584 | if (res) |
| 585 | return res; |
| 586 | |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 587 | /* Port 0 is the switch itself and has no PHY. */ |
Andreas Noever | 343fcb8 | 2014-06-12 23:11:47 +0200 | [diff] [blame] | 588 | if (port->config.type == TB_TYPE_PORT && port->port != 0) { |
Mika Westerberg | da2da04 | 2017-06-06 15:24:58 +0300 | [diff] [blame] | 589 | cap = tb_port_find_cap(port, TB_PORT_CAP_PHY); |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 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 | |
Andreas Noever | 343fcb8 | 2014-06-12 23:11:47 +0200 | [diff] [blame] | 597 | tb_dump_port(port->sw->tb, &port->config); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 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 | { |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 608 | tb_dbg(tb, " Switch: %x:%x (Revision: %d, TB Version: %d)\n", |
| 609 | sw->vendor_id, sw->device_id, sw->revision, |
| 610 | sw->thunderbolt_version); |
| 611 | tb_dbg(tb, " Max Port Number: %d\n", sw->max_port_number); |
| 612 | tb_dbg(tb, " Config:\n"); |
| 613 | tb_dbg(tb, |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 614 | " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n", |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 615 | sw->upstream_port_number, sw->depth, |
| 616 | (((u64) sw->route_hi) << 32) | sw->route_lo, |
| 617 | sw->enabled, sw->plug_events_delay); |
| 618 | tb_dbg(tb, " unknown1: %#x unknown4: %#x\n", |
| 619 | sw->__unknown1, sw->__unknown4); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 620 | } |
| 621 | |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 622 | /** |
| 623 | * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET |
| 624 | * |
| 625 | * Return: Returns 0 on success or an error code on failure. |
| 626 | */ |
| 627 | int tb_switch_reset(struct tb *tb, u64 route) |
| 628 | { |
| 629 | struct tb_cfg_result res; |
| 630 | struct tb_regs_switch_header header = { |
| 631 | header.route_hi = route >> 32, |
| 632 | header.route_lo = route, |
| 633 | header.enabled = true, |
| 634 | }; |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 635 | tb_dbg(tb, "resetting switch at %llx\n", route); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 636 | res.err = tb_cfg_write(tb->ctl, ((u32 *) &header) + 2, route, |
| 637 | 0, 2, 2, 2); |
| 638 | if (res.err) |
| 639 | return res.err; |
| 640 | res = tb_cfg_reset(tb->ctl, route, TB_CFG_DEFAULT_TIMEOUT); |
| 641 | if (res.err > 0) |
| 642 | return -EIO; |
| 643 | return res.err; |
| 644 | } |
| 645 | |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 646 | struct tb_switch *get_switch_at_route(struct tb_switch *sw, u64 route) |
| 647 | { |
| 648 | u8 next_port = route; /* |
| 649 | * Routes use a stride of 8 bits, |
| 650 | * eventhough a port index has 6 bits at most. |
| 651 | * */ |
| 652 | if (route == 0) |
| 653 | return sw; |
| 654 | if (next_port > sw->config.max_port_number) |
Sachin Kamat | c9c2dee | 2014-06-20 14:32:31 +0530 | [diff] [blame] | 655 | return NULL; |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 656 | if (tb_is_upstream_port(&sw->ports[next_port])) |
Sachin Kamat | c9c2dee | 2014-06-20 14:32:31 +0530 | [diff] [blame] | 657 | return NULL; |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 658 | if (!sw->ports[next_port].remote) |
Sachin Kamat | c9c2dee | 2014-06-20 14:32:31 +0530 | [diff] [blame] | 659 | return NULL; |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 660 | return get_switch_at_route(sw->ports[next_port].remote->sw, |
| 661 | route >> TB_ROUTE_SHIFT); |
| 662 | } |
| 663 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 664 | /** |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame] | 665 | * tb_plug_events_active() - enable/disable plug events on a switch |
| 666 | * |
| 667 | * Also configures a sane plug_events_delay of 255ms. |
| 668 | * |
| 669 | * Return: Returns 0 on success or an error code on failure. |
| 670 | */ |
| 671 | static int tb_plug_events_active(struct tb_switch *sw, bool active) |
| 672 | { |
| 673 | u32 data; |
| 674 | int res; |
| 675 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 676 | if (!sw->config.enabled) |
| 677 | return 0; |
| 678 | |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame] | 679 | sw->config.plug_events_delay = 0xff; |
| 680 | res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1); |
| 681 | if (res) |
| 682 | return res; |
| 683 | |
| 684 | res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1); |
| 685 | if (res) |
| 686 | return res; |
| 687 | |
| 688 | if (active) { |
| 689 | data = data & 0xFFFFFF83; |
| 690 | switch (sw->config.device_id) { |
Lukas Wunner | 1d11140 | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 691 | case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE: |
| 692 | case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE: |
| 693 | case PCI_DEVICE_ID_INTEL_PORT_RIDGE: |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame] | 694 | break; |
| 695 | default: |
| 696 | data |= 4; |
| 697 | } |
| 698 | } else { |
| 699 | data = data | 0x7c; |
| 700 | } |
| 701 | return tb_sw_write(sw, &data, TB_CFG_SWITCH, |
| 702 | sw->cap_plug_events + 1, 1); |
| 703 | } |
| 704 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 705 | static ssize_t authorized_show(struct device *dev, |
| 706 | struct device_attribute *attr, |
| 707 | char *buf) |
| 708 | { |
| 709 | struct tb_switch *sw = tb_to_switch(dev); |
| 710 | |
| 711 | return sprintf(buf, "%u\n", sw->authorized); |
| 712 | } |
| 713 | |
| 714 | static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val) |
| 715 | { |
| 716 | int ret = -EINVAL; |
| 717 | |
| 718 | if (mutex_lock_interruptible(&switch_lock)) |
| 719 | return -ERESTARTSYS; |
| 720 | |
| 721 | if (sw->authorized) |
| 722 | goto unlock; |
| 723 | |
Mika Westerberg | a03e828 | 2018-01-18 20:27:47 +0300 | [diff] [blame] | 724 | /* |
| 725 | * Make sure there is no PCIe rescan ongoing when a new PCIe |
| 726 | * tunnel is created. Otherwise the PCIe rescan code might find |
| 727 | * the new tunnel too early. |
| 728 | */ |
| 729 | pci_lock_rescan_remove(); |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 730 | pm_runtime_get_sync(&sw->dev); |
Mika Westerberg | a03e828 | 2018-01-18 20:27:47 +0300 | [diff] [blame] | 731 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 732 | switch (val) { |
| 733 | /* Approve switch */ |
| 734 | case 1: |
| 735 | if (sw->key) |
| 736 | ret = tb_domain_approve_switch_key(sw->tb, sw); |
| 737 | else |
| 738 | ret = tb_domain_approve_switch(sw->tb, sw); |
| 739 | break; |
| 740 | |
| 741 | /* Challenge switch */ |
| 742 | case 2: |
| 743 | if (sw->key) |
| 744 | ret = tb_domain_challenge_switch_key(sw->tb, sw); |
| 745 | break; |
| 746 | |
| 747 | default: |
| 748 | break; |
| 749 | } |
| 750 | |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 751 | pm_runtime_mark_last_busy(&sw->dev); |
| 752 | pm_runtime_put_autosuspend(&sw->dev); |
Mika Westerberg | a03e828 | 2018-01-18 20:27:47 +0300 | [diff] [blame] | 753 | pci_unlock_rescan_remove(); |
| 754 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 755 | if (!ret) { |
| 756 | sw->authorized = val; |
| 757 | /* Notify status change to the userspace */ |
| 758 | kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE); |
| 759 | } |
| 760 | |
| 761 | unlock: |
| 762 | mutex_unlock(&switch_lock); |
| 763 | return ret; |
| 764 | } |
| 765 | |
| 766 | static ssize_t authorized_store(struct device *dev, |
| 767 | struct device_attribute *attr, |
| 768 | const char *buf, size_t count) |
| 769 | { |
| 770 | struct tb_switch *sw = tb_to_switch(dev); |
| 771 | unsigned int val; |
| 772 | ssize_t ret; |
| 773 | |
| 774 | ret = kstrtouint(buf, 0, &val); |
| 775 | if (ret) |
| 776 | return ret; |
| 777 | if (val > 2) |
| 778 | return -EINVAL; |
| 779 | |
| 780 | ret = tb_switch_set_authorized(sw, val); |
| 781 | |
| 782 | return ret ? ret : count; |
| 783 | } |
| 784 | static DEVICE_ATTR_RW(authorized); |
| 785 | |
Yehezkel Bernat | 14862ee | 2018-01-22 12:50:09 +0200 | [diff] [blame] | 786 | static ssize_t boot_show(struct device *dev, struct device_attribute *attr, |
| 787 | char *buf) |
| 788 | { |
| 789 | struct tb_switch *sw = tb_to_switch(dev); |
| 790 | |
| 791 | return sprintf(buf, "%u\n", sw->boot); |
| 792 | } |
| 793 | static DEVICE_ATTR_RO(boot); |
| 794 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 795 | static ssize_t device_show(struct device *dev, struct device_attribute *attr, |
| 796 | char *buf) |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 797 | { |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 798 | struct tb_switch *sw = tb_to_switch(dev); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 799 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 800 | return sprintf(buf, "%#x\n", sw->device); |
| 801 | } |
| 802 | static DEVICE_ATTR_RO(device); |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame] | 803 | |
Mika Westerberg | 72ee339 | 2017-06-06 15:25:05 +0300 | [diff] [blame] | 804 | static ssize_t |
| 805 | device_name_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 806 | { |
| 807 | struct tb_switch *sw = tb_to_switch(dev); |
| 808 | |
| 809 | return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : ""); |
| 810 | } |
| 811 | static DEVICE_ATTR_RO(device_name); |
| 812 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 813 | static ssize_t key_show(struct device *dev, struct device_attribute *attr, |
| 814 | char *buf) |
| 815 | { |
| 816 | struct tb_switch *sw = tb_to_switch(dev); |
| 817 | ssize_t ret; |
| 818 | |
| 819 | if (mutex_lock_interruptible(&switch_lock)) |
| 820 | return -ERESTARTSYS; |
| 821 | |
| 822 | if (sw->key) |
| 823 | ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key); |
| 824 | else |
| 825 | ret = sprintf(buf, "\n"); |
| 826 | |
| 827 | mutex_unlock(&switch_lock); |
| 828 | return ret; |
| 829 | } |
| 830 | |
| 831 | static ssize_t key_store(struct device *dev, struct device_attribute *attr, |
| 832 | const char *buf, size_t count) |
| 833 | { |
| 834 | struct tb_switch *sw = tb_to_switch(dev); |
| 835 | u8 key[TB_SWITCH_KEY_SIZE]; |
| 836 | ssize_t ret = count; |
Bernat, Yehezkel | e545f0d | 2017-08-15 08:19:20 +0300 | [diff] [blame] | 837 | bool clear = false; |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 838 | |
Bernat, Yehezkel | e545f0d | 2017-08-15 08:19:20 +0300 | [diff] [blame] | 839 | if (!strcmp(buf, "\n")) |
| 840 | clear = true; |
| 841 | else if (hex2bin(key, buf, sizeof(key))) |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 842 | return -EINVAL; |
| 843 | |
| 844 | if (mutex_lock_interruptible(&switch_lock)) |
| 845 | return -ERESTARTSYS; |
| 846 | |
| 847 | if (sw->authorized) { |
| 848 | ret = -EBUSY; |
| 849 | } else { |
| 850 | kfree(sw->key); |
Bernat, Yehezkel | e545f0d | 2017-08-15 08:19:20 +0300 | [diff] [blame] | 851 | if (clear) { |
| 852 | sw->key = NULL; |
| 853 | } else { |
| 854 | sw->key = kmemdup(key, sizeof(key), GFP_KERNEL); |
| 855 | if (!sw->key) |
| 856 | ret = -ENOMEM; |
| 857 | } |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | mutex_unlock(&switch_lock); |
| 861 | return ret; |
| 862 | } |
Bernat, Yehezkel | 0956e41 | 2017-08-15 08:19:12 +0300 | [diff] [blame] | 863 | static DEVICE_ATTR(key, 0600, key_show, key_store); |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 864 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 865 | static ssize_t nvm_authenticate_show(struct device *dev, |
| 866 | struct device_attribute *attr, char *buf) |
| 867 | { |
| 868 | struct tb_switch *sw = tb_to_switch(dev); |
| 869 | u32 status; |
| 870 | |
| 871 | nvm_get_auth_status(sw, &status); |
| 872 | return sprintf(buf, "%#x\n", status); |
| 873 | } |
| 874 | |
| 875 | static ssize_t nvm_authenticate_store(struct device *dev, |
| 876 | struct device_attribute *attr, const char *buf, size_t count) |
| 877 | { |
| 878 | struct tb_switch *sw = tb_to_switch(dev); |
| 879 | bool val; |
| 880 | int ret; |
| 881 | |
| 882 | if (mutex_lock_interruptible(&switch_lock)) |
| 883 | return -ERESTARTSYS; |
| 884 | |
| 885 | /* If NVMem devices are not yet added */ |
| 886 | if (!sw->nvm) { |
| 887 | ret = -EAGAIN; |
| 888 | goto exit_unlock; |
| 889 | } |
| 890 | |
| 891 | ret = kstrtobool(buf, &val); |
| 892 | if (ret) |
| 893 | goto exit_unlock; |
| 894 | |
| 895 | /* Always clear the authentication status */ |
| 896 | nvm_clear_auth_status(sw); |
| 897 | |
| 898 | if (val) { |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 899 | if (!sw->nvm->buf) { |
| 900 | ret = -EINVAL; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 901 | goto exit_unlock; |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | pm_runtime_get_sync(&sw->dev); |
| 905 | ret = nvm_validate_and_write(sw); |
| 906 | if (ret) { |
| 907 | pm_runtime_mark_last_busy(&sw->dev); |
| 908 | pm_runtime_put_autosuspend(&sw->dev); |
| 909 | goto exit_unlock; |
| 910 | } |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 911 | |
| 912 | sw->nvm->authenticating = true; |
| 913 | |
| 914 | if (!tb_route(sw)) |
| 915 | ret = nvm_authenticate_host(sw); |
| 916 | else |
| 917 | ret = nvm_authenticate_device(sw); |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 918 | pm_runtime_mark_last_busy(&sw->dev); |
| 919 | pm_runtime_put_autosuspend(&sw->dev); |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | exit_unlock: |
| 923 | mutex_unlock(&switch_lock); |
| 924 | |
| 925 | if (ret) |
| 926 | return ret; |
| 927 | return count; |
| 928 | } |
| 929 | static DEVICE_ATTR_RW(nvm_authenticate); |
| 930 | |
| 931 | static ssize_t nvm_version_show(struct device *dev, |
| 932 | struct device_attribute *attr, char *buf) |
| 933 | { |
| 934 | struct tb_switch *sw = tb_to_switch(dev); |
| 935 | int ret; |
| 936 | |
| 937 | if (mutex_lock_interruptible(&switch_lock)) |
| 938 | return -ERESTARTSYS; |
| 939 | |
| 940 | if (sw->safe_mode) |
| 941 | ret = -ENODATA; |
| 942 | else if (!sw->nvm) |
| 943 | ret = -EAGAIN; |
| 944 | else |
| 945 | ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor); |
| 946 | |
| 947 | mutex_unlock(&switch_lock); |
| 948 | |
| 949 | return ret; |
| 950 | } |
| 951 | static DEVICE_ATTR_RO(nvm_version); |
| 952 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 953 | static ssize_t vendor_show(struct device *dev, struct device_attribute *attr, |
| 954 | char *buf) |
| 955 | { |
| 956 | struct tb_switch *sw = tb_to_switch(dev); |
| 957 | |
| 958 | return sprintf(buf, "%#x\n", sw->vendor); |
| 959 | } |
| 960 | static DEVICE_ATTR_RO(vendor); |
| 961 | |
Mika Westerberg | 72ee339 | 2017-06-06 15:25:05 +0300 | [diff] [blame] | 962 | static ssize_t |
| 963 | vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 964 | { |
| 965 | struct tb_switch *sw = tb_to_switch(dev); |
| 966 | |
| 967 | return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : ""); |
| 968 | } |
| 969 | static DEVICE_ATTR_RO(vendor_name); |
| 970 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 971 | static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr, |
| 972 | char *buf) |
| 973 | { |
| 974 | struct tb_switch *sw = tb_to_switch(dev); |
| 975 | |
| 976 | return sprintf(buf, "%pUb\n", sw->uuid); |
| 977 | } |
| 978 | static DEVICE_ATTR_RO(unique_id); |
| 979 | |
| 980 | static struct attribute *switch_attrs[] = { |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 981 | &dev_attr_authorized.attr, |
Yehezkel Bernat | 14862ee | 2018-01-22 12:50:09 +0200 | [diff] [blame] | 982 | &dev_attr_boot.attr, |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 983 | &dev_attr_device.attr, |
Mika Westerberg | 72ee339 | 2017-06-06 15:25:05 +0300 | [diff] [blame] | 984 | &dev_attr_device_name.attr, |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 985 | &dev_attr_key.attr, |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 986 | &dev_attr_nvm_authenticate.attr, |
| 987 | &dev_attr_nvm_version.attr, |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 988 | &dev_attr_vendor.attr, |
Mika Westerberg | 72ee339 | 2017-06-06 15:25:05 +0300 | [diff] [blame] | 989 | &dev_attr_vendor_name.attr, |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 990 | &dev_attr_unique_id.attr, |
| 991 | NULL, |
| 992 | }; |
| 993 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 994 | static umode_t switch_attr_is_visible(struct kobject *kobj, |
| 995 | struct attribute *attr, int n) |
| 996 | { |
| 997 | struct device *dev = container_of(kobj, struct device, kobj); |
| 998 | struct tb_switch *sw = tb_to_switch(dev); |
| 999 | |
| 1000 | if (attr == &dev_attr_key.attr) { |
| 1001 | if (tb_route(sw) && |
| 1002 | sw->tb->security_level == TB_SECURITY_SECURE && |
| 1003 | sw->security_level == TB_SECURITY_SECURE) |
| 1004 | return attr->mode; |
| 1005 | return 0; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1006 | } else if (attr == &dev_attr_nvm_authenticate.attr || |
| 1007 | attr == &dev_attr_nvm_version.attr) { |
| 1008 | if (sw->dma_port) |
| 1009 | return attr->mode; |
| 1010 | return 0; |
Yehezkel Bernat | 14862ee | 2018-01-22 12:50:09 +0200 | [diff] [blame] | 1011 | } else if (attr == &dev_attr_boot.attr) { |
| 1012 | if (tb_route(sw)) |
| 1013 | return attr->mode; |
| 1014 | return 0; |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1015 | } |
| 1016 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1017 | return sw->safe_mode ? 0 : attr->mode; |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1018 | } |
| 1019 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1020 | static struct attribute_group switch_group = { |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1021 | .is_visible = switch_attr_is_visible, |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1022 | .attrs = switch_attrs, |
| 1023 | }; |
| 1024 | |
| 1025 | static const struct attribute_group *switch_groups[] = { |
| 1026 | &switch_group, |
| 1027 | NULL, |
| 1028 | }; |
| 1029 | |
| 1030 | static void tb_switch_release(struct device *dev) |
| 1031 | { |
| 1032 | struct tb_switch *sw = tb_to_switch(dev); |
| 1033 | |
Mika Westerberg | 3e13676 | 2017-06-06 15:25:14 +0300 | [diff] [blame] | 1034 | dma_port_free(sw->dma_port); |
| 1035 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1036 | kfree(sw->uuid); |
Mika Westerberg | 72ee339 | 2017-06-06 15:25:05 +0300 | [diff] [blame] | 1037 | kfree(sw->device_name); |
| 1038 | kfree(sw->vendor_name); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1039 | kfree(sw->ports); |
Andreas Noever | 343fcb8 | 2014-06-12 23:11:47 +0200 | [diff] [blame] | 1040 | kfree(sw->drom); |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1041 | kfree(sw->key); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1042 | kfree(sw); |
| 1043 | } |
| 1044 | |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 1045 | /* |
| 1046 | * Currently only need to provide the callbacks. Everything else is handled |
| 1047 | * in the connection manager. |
| 1048 | */ |
| 1049 | static int __maybe_unused tb_switch_runtime_suspend(struct device *dev) |
| 1050 | { |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
| 1054 | static int __maybe_unused tb_switch_runtime_resume(struct device *dev) |
| 1055 | { |
| 1056 | return 0; |
| 1057 | } |
| 1058 | |
| 1059 | static const struct dev_pm_ops tb_switch_pm_ops = { |
| 1060 | SET_RUNTIME_PM_OPS(tb_switch_runtime_suspend, tb_switch_runtime_resume, |
| 1061 | NULL) |
| 1062 | }; |
| 1063 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1064 | struct device_type tb_switch_type = { |
| 1065 | .name = "thunderbolt_device", |
| 1066 | .release = tb_switch_release, |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 1067 | .pm = &tb_switch_pm_ops, |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1068 | }; |
| 1069 | |
Mika Westerberg | 2c3c419 | 2017-06-06 15:25:13 +0300 | [diff] [blame] | 1070 | static int tb_switch_get_generation(struct tb_switch *sw) |
| 1071 | { |
| 1072 | switch (sw->config.device_id) { |
| 1073 | case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE: |
| 1074 | case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE: |
| 1075 | case PCI_DEVICE_ID_INTEL_LIGHT_PEAK: |
| 1076 | case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C: |
| 1077 | case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C: |
| 1078 | case PCI_DEVICE_ID_INTEL_PORT_RIDGE: |
| 1079 | case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE: |
| 1080 | case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE: |
| 1081 | return 1; |
| 1082 | |
| 1083 | case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE: |
| 1084 | case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE: |
| 1085 | case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE: |
| 1086 | return 2; |
| 1087 | |
| 1088 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE: |
| 1089 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE: |
| 1090 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE: |
| 1091 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE: |
| 1092 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE: |
Radion Mirchevsky | 4bac471 | 2017-10-04 16:43:43 +0300 | [diff] [blame] | 1093 | case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE: |
| 1094 | case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE: |
| 1095 | case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE: |
Mika Westerberg | 2c3c419 | 2017-06-06 15:25:13 +0300 | [diff] [blame] | 1096 | return 3; |
| 1097 | |
| 1098 | default: |
| 1099 | /* |
| 1100 | * For unknown switches assume generation to be 1 to be |
| 1101 | * on the safe side. |
| 1102 | */ |
| 1103 | tb_sw_warn(sw, "unsupported switch device id %#x\n", |
| 1104 | sw->config.device_id); |
| 1105 | return 1; |
| 1106 | } |
| 1107 | } |
| 1108 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1109 | /** |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1110 | * tb_switch_alloc() - allocate a switch |
| 1111 | * @tb: Pointer to the owning domain |
| 1112 | * @parent: Parent device for this switch |
| 1113 | * @route: Route string for this switch |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1114 | * |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1115 | * Allocates and initializes a switch. Will not upload configuration to |
| 1116 | * the switch. For that you need to call tb_switch_configure() |
| 1117 | * separately. The returned switch should be released by calling |
| 1118 | * tb_switch_put(). |
| 1119 | * |
| 1120 | * Return: Pointer to the allocated switch or %NULL in case of failure |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1121 | */ |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1122 | struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent, |
| 1123 | u64 route) |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1124 | { |
| 1125 | int i; |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame] | 1126 | int cap; |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1127 | struct tb_switch *sw; |
| 1128 | int upstream_port = tb_cfg_get_upstream_port(tb->ctl, route); |
| 1129 | if (upstream_port < 0) |
| 1130 | return NULL; |
| 1131 | |
| 1132 | sw = kzalloc(sizeof(*sw), GFP_KERNEL); |
| 1133 | if (!sw) |
| 1134 | return NULL; |
| 1135 | |
| 1136 | sw->tb = tb; |
Lukas Wunner | aae20bb | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 1137 | if (tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5)) |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1138 | goto err_free_sw_ports; |
| 1139 | |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 1140 | tb_dbg(tb, "current switch config:\n"); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1141 | tb_dump_switch(tb, &sw->config); |
| 1142 | |
| 1143 | /* configure switch */ |
| 1144 | sw->config.upstream_port_number = upstream_port; |
| 1145 | sw->config.depth = tb_route_length(route); |
| 1146 | sw->config.route_lo = route; |
| 1147 | sw->config.route_hi = route >> 32; |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1148 | sw->config.enabled = 0; |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1149 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1150 | /* initialize ports */ |
| 1151 | sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports), |
| 1152 | GFP_KERNEL); |
| 1153 | if (!sw->ports) |
| 1154 | goto err_free_sw_ports; |
| 1155 | |
| 1156 | for (i = 0; i <= sw->config.max_port_number; i++) { |
| 1157 | /* minimum setup for tb_find_cap and tb_drom_read to work */ |
| 1158 | sw->ports[i].sw = sw; |
| 1159 | sw->ports[i].port = i; |
| 1160 | } |
| 1161 | |
Mika Westerberg | 2c3c419 | 2017-06-06 15:25:13 +0300 | [diff] [blame] | 1162 | sw->generation = tb_switch_get_generation(sw); |
| 1163 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1164 | cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS); |
| 1165 | if (cap < 0) { |
| 1166 | tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n"); |
| 1167 | goto err_free_sw_ports; |
| 1168 | } |
| 1169 | sw->cap_plug_events = cap; |
| 1170 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1171 | /* Root switch is always authorized */ |
| 1172 | if (!route) |
| 1173 | sw->authorized = true; |
| 1174 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1175 | device_initialize(&sw->dev); |
| 1176 | sw->dev.parent = parent; |
| 1177 | sw->dev.bus = &tb_bus_type; |
| 1178 | sw->dev.type = &tb_switch_type; |
| 1179 | sw->dev.groups = switch_groups; |
| 1180 | dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); |
| 1181 | |
| 1182 | return sw; |
| 1183 | |
| 1184 | err_free_sw_ports: |
| 1185 | kfree(sw->ports); |
| 1186 | kfree(sw); |
| 1187 | |
| 1188 | return NULL; |
| 1189 | } |
| 1190 | |
| 1191 | /** |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1192 | * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode |
| 1193 | * @tb: Pointer to the owning domain |
| 1194 | * @parent: Parent device for this switch |
| 1195 | * @route: Route string for this switch |
| 1196 | * |
| 1197 | * This creates a switch in safe mode. This means the switch pretty much |
| 1198 | * lacks all capabilities except DMA configuration port before it is |
| 1199 | * flashed with a valid NVM firmware. |
| 1200 | * |
| 1201 | * The returned switch must be released by calling tb_switch_put(). |
| 1202 | * |
| 1203 | * Return: Pointer to the allocated switch or %NULL in case of failure |
| 1204 | */ |
| 1205 | struct tb_switch * |
| 1206 | tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route) |
| 1207 | { |
| 1208 | struct tb_switch *sw; |
| 1209 | |
| 1210 | sw = kzalloc(sizeof(*sw), GFP_KERNEL); |
| 1211 | if (!sw) |
| 1212 | return NULL; |
| 1213 | |
| 1214 | sw->tb = tb; |
| 1215 | sw->config.depth = tb_route_length(route); |
| 1216 | sw->config.route_hi = upper_32_bits(route); |
| 1217 | sw->config.route_lo = lower_32_bits(route); |
| 1218 | sw->safe_mode = true; |
| 1219 | |
| 1220 | device_initialize(&sw->dev); |
| 1221 | sw->dev.parent = parent; |
| 1222 | sw->dev.bus = &tb_bus_type; |
| 1223 | sw->dev.type = &tb_switch_type; |
| 1224 | sw->dev.groups = switch_groups; |
| 1225 | dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); |
| 1226 | |
| 1227 | return sw; |
| 1228 | } |
| 1229 | |
| 1230 | /** |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1231 | * tb_switch_configure() - Uploads configuration to the switch |
| 1232 | * @sw: Switch to configure |
| 1233 | * |
| 1234 | * Call this function before the switch is added to the system. It will |
| 1235 | * upload configuration to the switch and makes it available for the |
| 1236 | * connection manager to use. |
| 1237 | * |
| 1238 | * Return: %0 in case of success and negative errno in case of failure |
| 1239 | */ |
| 1240 | int tb_switch_configure(struct tb_switch *sw) |
| 1241 | { |
| 1242 | struct tb *tb = sw->tb; |
| 1243 | u64 route; |
| 1244 | int ret; |
| 1245 | |
| 1246 | route = tb_route(sw); |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 1247 | tb_dbg(tb, "initializing Switch at %#llx (depth: %d, up port: %d)\n", |
| 1248 | route, tb_route_length(route), sw->config.upstream_port_number); |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1249 | |
| 1250 | if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL) |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1251 | tb_sw_warn(sw, "unknown switch vendor id %#x\n", |
| 1252 | sw->config.vendor_id); |
| 1253 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1254 | sw->config.enabled = 1; |
| 1255 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1256 | /* upload configuration */ |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1257 | ret = tb_sw_write(sw, 1 + (u32 *)&sw->config, TB_CFG_SWITCH, 1, 3); |
| 1258 | if (ret) |
| 1259 | return ret; |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1260 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1261 | return tb_plug_events_active(sw, true); |
| 1262 | } |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1263 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1264 | static void tb_switch_set_uuid(struct tb_switch *sw) |
| 1265 | { |
| 1266 | u32 uuid[4]; |
| 1267 | int cap; |
| 1268 | |
| 1269 | if (sw->uuid) |
| 1270 | return; |
| 1271 | |
| 1272 | /* |
| 1273 | * The newer controllers include fused UUID as part of link |
| 1274 | * controller specific registers |
| 1275 | */ |
| 1276 | cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER); |
| 1277 | if (cap > 0) { |
| 1278 | tb_sw_read(sw, uuid, TB_CFG_SWITCH, cap + 3, 4); |
| 1279 | } else { |
| 1280 | /* |
| 1281 | * ICM generates UUID based on UID and fills the upper |
| 1282 | * two words with ones. This is not strictly following |
| 1283 | * UUID format but we want to be compatible with it so |
| 1284 | * we do the same here. |
| 1285 | */ |
| 1286 | uuid[0] = sw->uid & 0xffffffff; |
| 1287 | uuid[1] = (sw->uid >> 32) & 0xffffffff; |
| 1288 | uuid[2] = 0xffffffff; |
| 1289 | uuid[3] = 0xffffffff; |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1290 | } |
| 1291 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1292 | sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL); |
| 1293 | } |
| 1294 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1295 | static int tb_switch_add_dma_port(struct tb_switch *sw) |
Mika Westerberg | 3e13676 | 2017-06-06 15:25:14 +0300 | [diff] [blame] | 1296 | { |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1297 | u32 status; |
| 1298 | int ret; |
| 1299 | |
Mika Westerberg | 3e13676 | 2017-06-06 15:25:14 +0300 | [diff] [blame] | 1300 | switch (sw->generation) { |
| 1301 | case 3: |
| 1302 | break; |
| 1303 | |
| 1304 | case 2: |
| 1305 | /* Only root switch can be upgraded */ |
| 1306 | if (tb_route(sw)) |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1307 | return 0; |
Mika Westerberg | 3e13676 | 2017-06-06 15:25:14 +0300 | [diff] [blame] | 1308 | break; |
| 1309 | |
| 1310 | default: |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1311 | /* |
| 1312 | * DMA port is the only thing available when the switch |
| 1313 | * is in safe mode. |
| 1314 | */ |
| 1315 | if (!sw->safe_mode) |
| 1316 | return 0; |
| 1317 | break; |
Mika Westerberg | 3e13676 | 2017-06-06 15:25:14 +0300 | [diff] [blame] | 1318 | } |
| 1319 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1320 | if (sw->no_nvm_upgrade) |
| 1321 | return 0; |
| 1322 | |
Mika Westerberg | 3e13676 | 2017-06-06 15:25:14 +0300 | [diff] [blame] | 1323 | sw->dma_port = dma_port_alloc(sw); |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1324 | if (!sw->dma_port) |
| 1325 | return 0; |
| 1326 | |
| 1327 | /* |
| 1328 | * Check status of the previous flash authentication. If there |
| 1329 | * is one we need to power cycle the switch in any case to make |
| 1330 | * it functional again. |
| 1331 | */ |
| 1332 | ret = dma_port_flash_update_auth_status(sw->dma_port, &status); |
| 1333 | if (ret <= 0) |
| 1334 | return ret; |
| 1335 | |
| 1336 | if (status) { |
| 1337 | tb_sw_info(sw, "switch flash authentication failed\n"); |
| 1338 | tb_switch_set_uuid(sw); |
| 1339 | nvm_set_auth_status(sw, status); |
| 1340 | } |
| 1341 | |
| 1342 | tb_sw_info(sw, "power cycling the switch now\n"); |
| 1343 | dma_port_power_cycle(sw->dma_port); |
| 1344 | |
| 1345 | /* |
| 1346 | * We return error here which causes the switch adding failure. |
| 1347 | * It should appear back after power cycle is complete. |
| 1348 | */ |
| 1349 | return -ESHUTDOWN; |
Mika Westerberg | 3e13676 | 2017-06-06 15:25:14 +0300 | [diff] [blame] | 1350 | } |
| 1351 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1352 | /** |
| 1353 | * tb_switch_add() - Add a switch to the domain |
| 1354 | * @sw: Switch to add |
| 1355 | * |
| 1356 | * This is the last step in adding switch to the domain. It will read |
| 1357 | * identification information from DROM and initializes ports so that |
| 1358 | * they can be used to connect other switches. The switch will be |
| 1359 | * exposed to the userspace when this function successfully returns. To |
| 1360 | * remove and release the switch, call tb_switch_remove(). |
| 1361 | * |
| 1362 | * Return: %0 in case of success and negative errno in case of failure |
| 1363 | */ |
| 1364 | int tb_switch_add(struct tb_switch *sw) |
| 1365 | { |
| 1366 | int i, ret; |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame] | 1367 | |
Mika Westerberg | 3e13676 | 2017-06-06 15:25:14 +0300 | [diff] [blame] | 1368 | /* |
| 1369 | * Initialize DMA control port now before we read DROM. Recent |
| 1370 | * host controllers have more complete DROM on NVM that includes |
| 1371 | * vendor and model identification strings which we then expose |
| 1372 | * to the userspace. NVM can be accessed through DMA |
| 1373 | * configuration based mailbox. |
| 1374 | */ |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1375 | ret = tb_switch_add_dma_port(sw); |
| 1376 | if (ret) |
Mika Westerberg | f53e767 | 2017-06-06 15:25:02 +0300 | [diff] [blame] | 1377 | return ret; |
Andreas Noever | 343fcb8 | 2014-06-12 23:11:47 +0200 | [diff] [blame] | 1378 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1379 | if (!sw->safe_mode) { |
| 1380 | /* read drom */ |
| 1381 | ret = tb_drom_read(sw); |
| 1382 | if (ret) { |
| 1383 | tb_sw_warn(sw, "tb_eeprom_read_rom failed\n"); |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1384 | return ret; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1385 | } |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 1386 | tb_sw_dbg(sw, "uid: %#llx\n", sw->uid); |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1387 | |
| 1388 | tb_switch_set_uuid(sw); |
| 1389 | |
| 1390 | for (i = 0; i <= sw->config.max_port_number; i++) { |
| 1391 | if (sw->ports[i].disabled) { |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 1392 | tb_port_dbg(&sw->ports[i], "disabled by eeprom\n"); |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1393 | continue; |
| 1394 | } |
| 1395 | ret = tb_init_port(&sw->ports[i]); |
| 1396 | if (ret) |
| 1397 | return ret; |
| 1398 | } |
Andreas Noever | 343fcb8 | 2014-06-12 23:11:47 +0200 | [diff] [blame] | 1399 | } |
| 1400 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1401 | ret = device_add(&sw->dev); |
| 1402 | if (ret) |
| 1403 | return ret; |
| 1404 | |
Mika Westerberg | a83bc4a | 2018-10-01 12:31:20 +0300 | [diff] [blame] | 1405 | if (tb_route(sw)) { |
| 1406 | dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n", |
| 1407 | sw->vendor, sw->device); |
| 1408 | if (sw->vendor_name && sw->device_name) |
| 1409 | dev_info(&sw->dev, "%s %s\n", sw->vendor_name, |
| 1410 | sw->device_name); |
| 1411 | } |
| 1412 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1413 | ret = tb_switch_nvm_add(sw); |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 1414 | if (ret) { |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1415 | device_del(&sw->dev); |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 1416 | return ret; |
| 1417 | } |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1418 | |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 1419 | pm_runtime_set_active(&sw->dev); |
| 1420 | if (sw->rpm) { |
| 1421 | pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY); |
| 1422 | pm_runtime_use_autosuspend(&sw->dev); |
| 1423 | pm_runtime_mark_last_busy(&sw->dev); |
| 1424 | pm_runtime_enable(&sw->dev); |
| 1425 | pm_request_autosuspend(&sw->dev); |
| 1426 | } |
| 1427 | |
| 1428 | return 0; |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1429 | } |
Andreas Noever | c90553b | 2014-06-03 22:04:11 +0200 | [diff] [blame] | 1430 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1431 | /** |
| 1432 | * tb_switch_remove() - Remove and release a switch |
| 1433 | * @sw: Switch to remove |
| 1434 | * |
| 1435 | * This will remove the switch from the domain and release it after last |
| 1436 | * reference count drops to zero. If there are switches connected below |
| 1437 | * this switch, they will be removed as well. |
| 1438 | */ |
| 1439 | void tb_switch_remove(struct tb_switch *sw) |
| 1440 | { |
| 1441 | int i; |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame] | 1442 | |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 1443 | if (sw->rpm) { |
| 1444 | pm_runtime_get_sync(&sw->dev); |
| 1445 | pm_runtime_disable(&sw->dev); |
| 1446 | } |
| 1447 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1448 | /* port 0 is the switch itself and never has a remote */ |
| 1449 | for (i = 1; i <= sw->config.max_port_number; i++) { |
| 1450 | if (tb_is_upstream_port(&sw->ports[i])) |
| 1451 | continue; |
| 1452 | if (sw->ports[i].remote) |
| 1453 | tb_switch_remove(sw->ports[i].remote->sw); |
| 1454 | sw->ports[i].remote = NULL; |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 1455 | if (sw->ports[i].xdomain) |
| 1456 | tb_xdomain_remove(sw->ports[i].xdomain); |
| 1457 | sw->ports[i].xdomain = NULL; |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | if (!sw->is_unplugged) |
| 1461 | tb_plug_events_active(sw, false); |
| 1462 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1463 | tb_switch_nvm_remove(sw); |
Mika Westerberg | a83bc4a | 2018-10-01 12:31:20 +0300 | [diff] [blame] | 1464 | |
| 1465 | if (tb_route(sw)) |
| 1466 | dev_info(&sw->dev, "device disconnected\n"); |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1467 | device_unregister(&sw->dev); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1468 | } |
| 1469 | |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 1470 | /** |
Lukas Wunner | aae20bb | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 1471 | * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 1472 | */ |
Lukas Wunner | aae20bb | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 1473 | void tb_sw_set_unplugged(struct tb_switch *sw) |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 1474 | { |
| 1475 | int i; |
| 1476 | if (sw == sw->tb->root_switch) { |
| 1477 | tb_sw_WARN(sw, "cannot unplug root switch\n"); |
| 1478 | return; |
| 1479 | } |
| 1480 | if (sw->is_unplugged) { |
| 1481 | tb_sw_WARN(sw, "is_unplugged already set\n"); |
| 1482 | return; |
| 1483 | } |
| 1484 | sw->is_unplugged = true; |
| 1485 | for (i = 0; i <= sw->config.max_port_number; i++) { |
| 1486 | if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote) |
Lukas Wunner | aae20bb | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 1487 | tb_sw_set_unplugged(sw->ports[i].remote->sw); |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 1488 | } |
| 1489 | } |
| 1490 | |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1491 | int tb_switch_resume(struct tb_switch *sw) |
| 1492 | { |
| 1493 | int i, err; |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 1494 | tb_sw_dbg(sw, "resuming switch\n"); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1495 | |
Mika Westerberg | 08a5e4c | 2017-06-06 15:24:54 +0300 | [diff] [blame] | 1496 | /* |
| 1497 | * Check for UID of the connected switches except for root |
| 1498 | * switch which we assume cannot be removed. |
| 1499 | */ |
| 1500 | if (tb_route(sw)) { |
| 1501 | u64 uid; |
| 1502 | |
| 1503 | err = tb_drom_read_uid_only(sw, &uid); |
| 1504 | if (err) { |
| 1505 | tb_sw_warn(sw, "uid read failed\n"); |
| 1506 | return err; |
| 1507 | } |
| 1508 | if (sw->uid != uid) { |
| 1509 | tb_sw_info(sw, |
| 1510 | "changed while suspended (uid %#llx -> %#llx)\n", |
| 1511 | sw->uid, uid); |
| 1512 | return -ENODEV; |
| 1513 | } |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | /* upload configuration */ |
| 1517 | err = tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3); |
| 1518 | if (err) |
| 1519 | return err; |
| 1520 | |
| 1521 | err = tb_plug_events_active(sw, true); |
| 1522 | if (err) |
| 1523 | return err; |
| 1524 | |
| 1525 | /* check for surviving downstream switches */ |
| 1526 | for (i = 1; i <= sw->config.max_port_number; i++) { |
| 1527 | struct tb_port *port = &sw->ports[i]; |
| 1528 | if (tb_is_upstream_port(port)) |
| 1529 | continue; |
| 1530 | if (!port->remote) |
| 1531 | continue; |
| 1532 | if (tb_wait_for_port(port, true) <= 0 |
| 1533 | || tb_switch_resume(port->remote->sw)) { |
| 1534 | tb_port_warn(port, |
| 1535 | "lost during suspend, disconnecting\n"); |
Lukas Wunner | aae20bb | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 1536 | tb_sw_set_unplugged(port->remote->sw); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1537 | } |
| 1538 | } |
| 1539 | return 0; |
| 1540 | } |
| 1541 | |
| 1542 | void tb_switch_suspend(struct tb_switch *sw) |
| 1543 | { |
| 1544 | int i, err; |
| 1545 | err = tb_plug_events_active(sw, false); |
| 1546 | if (err) |
| 1547 | return; |
| 1548 | |
| 1549 | for (i = 1; i <= sw->config.max_port_number; i++) { |
| 1550 | if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote) |
| 1551 | tb_switch_suspend(sw->ports[i].remote->sw); |
| 1552 | } |
| 1553 | /* |
| 1554 | * TODO: invoke tb_cfg_prepare_to_sleep here? does not seem to have any |
| 1555 | * effect? |
| 1556 | */ |
| 1557 | } |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1558 | |
| 1559 | struct tb_sw_lookup { |
| 1560 | struct tb *tb; |
| 1561 | u8 link; |
| 1562 | u8 depth; |
Christoph Hellwig | 7c39ffe | 2017-07-18 15:30:05 +0200 | [diff] [blame] | 1563 | const uuid_t *uuid; |
Radion Mirchevsky | 8e9267b | 2017-10-04 15:24:14 +0300 | [diff] [blame] | 1564 | u64 route; |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1565 | }; |
| 1566 | |
| 1567 | static int tb_switch_match(struct device *dev, void *data) |
| 1568 | { |
| 1569 | struct tb_switch *sw = tb_to_switch(dev); |
| 1570 | struct tb_sw_lookup *lookup = data; |
| 1571 | |
| 1572 | if (!sw) |
| 1573 | return 0; |
| 1574 | if (sw->tb != lookup->tb) |
| 1575 | return 0; |
| 1576 | |
| 1577 | if (lookup->uuid) |
| 1578 | return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid)); |
| 1579 | |
Radion Mirchevsky | 8e9267b | 2017-10-04 15:24:14 +0300 | [diff] [blame] | 1580 | if (lookup->route) { |
| 1581 | return sw->config.route_lo == lower_32_bits(lookup->route) && |
| 1582 | sw->config.route_hi == upper_32_bits(lookup->route); |
| 1583 | } |
| 1584 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1585 | /* Root switch is matched only by depth */ |
| 1586 | if (!lookup->depth) |
| 1587 | return !sw->depth; |
| 1588 | |
| 1589 | return sw->link == lookup->link && sw->depth == lookup->depth; |
| 1590 | } |
| 1591 | |
| 1592 | /** |
| 1593 | * tb_switch_find_by_link_depth() - Find switch by link and depth |
| 1594 | * @tb: Domain the switch belongs |
| 1595 | * @link: Link number the switch is connected |
| 1596 | * @depth: Depth of the switch in link |
| 1597 | * |
| 1598 | * Returned switch has reference count increased so the caller needs to |
| 1599 | * call tb_switch_put() when done with the switch. |
| 1600 | */ |
| 1601 | struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth) |
| 1602 | { |
| 1603 | struct tb_sw_lookup lookup; |
| 1604 | struct device *dev; |
| 1605 | |
| 1606 | memset(&lookup, 0, sizeof(lookup)); |
| 1607 | lookup.tb = tb; |
| 1608 | lookup.link = link; |
| 1609 | lookup.depth = depth; |
| 1610 | |
| 1611 | dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match); |
| 1612 | if (dev) |
| 1613 | return tb_to_switch(dev); |
| 1614 | |
| 1615 | return NULL; |
| 1616 | } |
| 1617 | |
| 1618 | /** |
Radion Mirchevsky | 432019d | 2017-10-04 15:07:40 +0300 | [diff] [blame] | 1619 | * tb_switch_find_by_uuid() - Find switch by UUID |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1620 | * @tb: Domain the switch belongs |
| 1621 | * @uuid: UUID to look for |
| 1622 | * |
| 1623 | * Returned switch has reference count increased so the caller needs to |
| 1624 | * call tb_switch_put() when done with the switch. |
| 1625 | */ |
Christoph Hellwig | 7c39ffe | 2017-07-18 15:30:05 +0200 | [diff] [blame] | 1626 | struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid) |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1627 | { |
| 1628 | struct tb_sw_lookup lookup; |
| 1629 | struct device *dev; |
| 1630 | |
| 1631 | memset(&lookup, 0, sizeof(lookup)); |
| 1632 | lookup.tb = tb; |
| 1633 | lookup.uuid = uuid; |
| 1634 | |
| 1635 | dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match); |
| 1636 | if (dev) |
| 1637 | return tb_to_switch(dev); |
| 1638 | |
| 1639 | return NULL; |
| 1640 | } |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1641 | |
Radion Mirchevsky | 8e9267b | 2017-10-04 15:24:14 +0300 | [diff] [blame] | 1642 | /** |
| 1643 | * tb_switch_find_by_route() - Find switch by route string |
| 1644 | * @tb: Domain the switch belongs |
| 1645 | * @route: Route string to look for |
| 1646 | * |
| 1647 | * Returned switch has reference count increased so the caller needs to |
| 1648 | * call tb_switch_put() when done with the switch. |
| 1649 | */ |
| 1650 | struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route) |
| 1651 | { |
| 1652 | struct tb_sw_lookup lookup; |
| 1653 | struct device *dev; |
| 1654 | |
| 1655 | if (!route) |
| 1656 | return tb_switch_get(tb->root_switch); |
| 1657 | |
| 1658 | memset(&lookup, 0, sizeof(lookup)); |
| 1659 | lookup.tb = tb; |
| 1660 | lookup.route = route; |
| 1661 | |
| 1662 | dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match); |
| 1663 | if (dev) |
| 1664 | return tb_to_switch(dev); |
| 1665 | |
| 1666 | return NULL; |
| 1667 | } |
| 1668 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1669 | void tb_switch_exit(void) |
| 1670 | { |
| 1671 | ida_destroy(&nvm_ida); |
| 1672 | } |