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