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