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); |
Mika Westerberg | 4f7c2e0 | 2019-05-28 18:56:20 +0300 | [diff] [blame] | 242 | |
| 243 | if (!mutex_trylock(&sw->tb->lock)) { |
| 244 | ret = restart_syscall(); |
| 245 | goto out; |
| 246 | } |
| 247 | |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 248 | ret = dma_port_flash_read(sw->dma_port, offset, val, bytes); |
Mika Westerberg | 4f7c2e0 | 2019-05-28 18:56:20 +0300 | [diff] [blame] | 249 | mutex_unlock(&sw->tb->lock); |
| 250 | |
| 251 | out: |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 252 | pm_runtime_mark_last_busy(&sw->dev); |
| 253 | pm_runtime_put_autosuspend(&sw->dev); |
| 254 | |
| 255 | return ret; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static int tb_switch_nvm_write(void *priv, unsigned int offset, void *val, |
| 259 | size_t bytes) |
| 260 | { |
| 261 | struct tb_switch *sw = priv; |
| 262 | int ret = 0; |
| 263 | |
Mika Westerberg | 09f11b6 | 2019-03-19 16:48:41 +0200 | [diff] [blame] | 264 | if (!mutex_trylock(&sw->tb->lock)) |
| 265 | return restart_syscall(); |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 266 | |
| 267 | /* |
| 268 | * Since writing the NVM image might require some special steps, |
| 269 | * for example when CSS headers are written, we cache the image |
| 270 | * locally here and handle the special cases when the user asks |
| 271 | * us to authenticate the image. |
| 272 | */ |
| 273 | if (!sw->nvm->buf) { |
| 274 | sw->nvm->buf = vmalloc(NVM_MAX_SIZE); |
| 275 | if (!sw->nvm->buf) { |
| 276 | ret = -ENOMEM; |
| 277 | goto unlock; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | sw->nvm->buf_data_size = offset + bytes; |
| 282 | memcpy(sw->nvm->buf + offset, val, bytes); |
| 283 | |
| 284 | unlock: |
Mika Westerberg | 09f11b6 | 2019-03-19 16:48:41 +0200 | [diff] [blame] | 285 | mutex_unlock(&sw->tb->lock); |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 286 | |
| 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | static struct nvmem_device *register_nvmem(struct tb_switch *sw, int id, |
| 291 | size_t size, bool active) |
| 292 | { |
| 293 | struct nvmem_config config; |
| 294 | |
| 295 | memset(&config, 0, sizeof(config)); |
| 296 | |
| 297 | if (active) { |
| 298 | config.name = "nvm_active"; |
| 299 | config.reg_read = tb_switch_nvm_read; |
Mika Westerberg | 800161b | 2017-06-29 14:19:50 +0300 | [diff] [blame] | 300 | config.read_only = true; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 301 | } else { |
| 302 | config.name = "nvm_non_active"; |
| 303 | config.reg_write = tb_switch_nvm_write; |
Mika Westerberg | 800161b | 2017-06-29 14:19:50 +0300 | [diff] [blame] | 304 | config.root_only = true; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | config.id = id; |
| 308 | config.stride = 4; |
| 309 | config.word_size = 4; |
| 310 | config.size = size; |
| 311 | config.dev = &sw->dev; |
| 312 | config.owner = THIS_MODULE; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 313 | config.priv = sw; |
| 314 | |
| 315 | return nvmem_register(&config); |
| 316 | } |
| 317 | |
| 318 | static int tb_switch_nvm_add(struct tb_switch *sw) |
| 319 | { |
| 320 | struct nvmem_device *nvm_dev; |
| 321 | struct tb_switch_nvm *nvm; |
| 322 | u32 val; |
| 323 | int ret; |
| 324 | |
| 325 | if (!sw->dma_port) |
| 326 | return 0; |
| 327 | |
| 328 | nvm = kzalloc(sizeof(*nvm), GFP_KERNEL); |
| 329 | if (!nvm) |
| 330 | return -ENOMEM; |
| 331 | |
| 332 | nvm->id = ida_simple_get(&nvm_ida, 0, 0, GFP_KERNEL); |
| 333 | |
| 334 | /* |
| 335 | * If the switch is in safe-mode the only accessible portion of |
| 336 | * the NVM is the non-active one where userspace is expected to |
| 337 | * write new functional NVM. |
| 338 | */ |
| 339 | if (!sw->safe_mode) { |
| 340 | u32 nvm_size, hdr_size; |
| 341 | |
| 342 | ret = dma_port_flash_read(sw->dma_port, NVM_FLASH_SIZE, &val, |
| 343 | sizeof(val)); |
| 344 | if (ret) |
| 345 | goto err_ida; |
| 346 | |
| 347 | hdr_size = sw->generation < 3 ? SZ_8K : SZ_16K; |
| 348 | nvm_size = (SZ_1M << (val & 7)) / 8; |
| 349 | nvm_size = (nvm_size - hdr_size) / 2; |
| 350 | |
| 351 | ret = dma_port_flash_read(sw->dma_port, NVM_VERSION, &val, |
| 352 | sizeof(val)); |
| 353 | if (ret) |
| 354 | goto err_ida; |
| 355 | |
| 356 | nvm->major = val >> 16; |
| 357 | nvm->minor = val >> 8; |
| 358 | |
| 359 | nvm_dev = register_nvmem(sw, nvm->id, nvm_size, true); |
| 360 | if (IS_ERR(nvm_dev)) { |
| 361 | ret = PTR_ERR(nvm_dev); |
| 362 | goto err_ida; |
| 363 | } |
| 364 | nvm->active = nvm_dev; |
| 365 | } |
| 366 | |
| 367 | nvm_dev = register_nvmem(sw, nvm->id, NVM_MAX_SIZE, false); |
| 368 | if (IS_ERR(nvm_dev)) { |
| 369 | ret = PTR_ERR(nvm_dev); |
| 370 | goto err_nvm_active; |
| 371 | } |
| 372 | nvm->non_active = nvm_dev; |
| 373 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 374 | sw->nvm = nvm; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 375 | return 0; |
| 376 | |
| 377 | err_nvm_active: |
| 378 | if (nvm->active) |
| 379 | nvmem_unregister(nvm->active); |
| 380 | err_ida: |
| 381 | ida_simple_remove(&nvm_ida, nvm->id); |
| 382 | kfree(nvm); |
| 383 | |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | static void tb_switch_nvm_remove(struct tb_switch *sw) |
| 388 | { |
| 389 | struct tb_switch_nvm *nvm; |
| 390 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 391 | nvm = sw->nvm; |
| 392 | sw->nvm = NULL; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 393 | |
| 394 | if (!nvm) |
| 395 | return; |
| 396 | |
| 397 | /* Remove authentication status in case the switch is unplugged */ |
| 398 | if (!nvm->authenticating) |
| 399 | nvm_clear_auth_status(sw); |
| 400 | |
| 401 | nvmem_unregister(nvm->non_active); |
| 402 | if (nvm->active) |
| 403 | nvmem_unregister(nvm->active); |
| 404 | ida_simple_remove(&nvm_ida, nvm->id); |
| 405 | vfree(nvm->buf); |
| 406 | kfree(nvm); |
| 407 | } |
| 408 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 409 | /* port utility functions */ |
| 410 | |
| 411 | static const char *tb_port_type(struct tb_regs_port_header *port) |
| 412 | { |
| 413 | switch (port->type >> 16) { |
| 414 | case 0: |
| 415 | switch ((u8) port->type) { |
| 416 | case 0: |
| 417 | return "Inactive"; |
| 418 | case 1: |
| 419 | return "Port"; |
| 420 | case 2: |
| 421 | return "NHI"; |
| 422 | default: |
| 423 | return "unknown"; |
| 424 | } |
| 425 | case 0x2: |
| 426 | return "Ethernet"; |
| 427 | case 0x8: |
| 428 | return "SATA"; |
| 429 | case 0xe: |
| 430 | return "DP/HDMI"; |
| 431 | case 0x10: |
| 432 | return "PCIe"; |
| 433 | case 0x20: |
| 434 | return "USB"; |
| 435 | default: |
| 436 | return "unknown"; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port) |
| 441 | { |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 442 | tb_dbg(tb, |
| 443 | " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n", |
| 444 | port->port_number, port->vendor_id, port->device_id, |
| 445 | port->revision, port->thunderbolt_version, tb_port_type(port), |
| 446 | port->type); |
| 447 | tb_dbg(tb, " Max hop id (in/out): %d/%d\n", |
| 448 | port->max_in_hop_id, port->max_out_hop_id); |
| 449 | tb_dbg(tb, " Max counters: %d\n", port->max_counters); |
| 450 | tb_dbg(tb, " NFC Credits: %#x\n", port->nfc_credits); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /** |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 454 | * tb_port_state() - get connectedness state of a port |
| 455 | * |
| 456 | * The port must have a TB_CAP_PHY (i.e. it should be a real port). |
| 457 | * |
| 458 | * Return: Returns an enum tb_port_state on success or an error code on failure. |
| 459 | */ |
| 460 | static int tb_port_state(struct tb_port *port) |
| 461 | { |
| 462 | struct tb_cap_phy phy; |
| 463 | int res; |
| 464 | if (port->cap_phy == 0) { |
| 465 | tb_port_WARN(port, "does not have a PHY\n"); |
| 466 | return -EINVAL; |
| 467 | } |
| 468 | res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2); |
| 469 | if (res) |
| 470 | return res; |
| 471 | return phy.state; |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * tb_wait_for_port() - wait for a port to become ready |
| 476 | * |
| 477 | * Wait up to 1 second for a port to reach state TB_PORT_UP. If |
| 478 | * wait_if_unplugged is set then we also wait if the port is in state |
| 479 | * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after |
| 480 | * switch resume). Otherwise we only wait if a device is registered but the link |
| 481 | * has not yet been established. |
| 482 | * |
| 483 | * Return: Returns an error code on failure. Returns 0 if the port is not |
| 484 | * connected or failed to reach state TB_PORT_UP within one second. Returns 1 |
| 485 | * if the port is connected and in state TB_PORT_UP. |
| 486 | */ |
| 487 | int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged) |
| 488 | { |
| 489 | int retries = 10; |
| 490 | int state; |
| 491 | if (!port->cap_phy) { |
| 492 | tb_port_WARN(port, "does not have PHY\n"); |
| 493 | return -EINVAL; |
| 494 | } |
| 495 | if (tb_is_upstream_port(port)) { |
| 496 | tb_port_WARN(port, "is the upstream port\n"); |
| 497 | return -EINVAL; |
| 498 | } |
| 499 | |
| 500 | while (retries--) { |
| 501 | state = tb_port_state(port); |
| 502 | if (state < 0) |
| 503 | return state; |
| 504 | if (state == TB_PORT_DISABLED) { |
Mika Westerberg | 62efe69 | 2018-09-17 16:32:13 +0300 | [diff] [blame] | 505 | tb_port_dbg(port, "is disabled (state: 0)\n"); |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 506 | return 0; |
| 507 | } |
| 508 | if (state == TB_PORT_UNPLUGGED) { |
| 509 | if (wait_if_unplugged) { |
| 510 | /* used during resume */ |
Mika Westerberg | 62efe69 | 2018-09-17 16:32:13 +0300 | [diff] [blame] | 511 | tb_port_dbg(port, |
| 512 | "is unplugged (state: 7), retrying...\n"); |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 513 | msleep(100); |
| 514 | continue; |
| 515 | } |
Mika Westerberg | 62efe69 | 2018-09-17 16:32:13 +0300 | [diff] [blame] | 516 | tb_port_dbg(port, "is unplugged (state: 7)\n"); |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 517 | return 0; |
| 518 | } |
| 519 | if (state == TB_PORT_UP) { |
Mika Westerberg | 62efe69 | 2018-09-17 16:32:13 +0300 | [diff] [blame] | 520 | tb_port_dbg(port, "is connected, link is up (state: 2)\n"); |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 521 | return 1; |
| 522 | } |
| 523 | |
| 524 | /* |
| 525 | * After plug-in the state is TB_PORT_CONNECTING. Give it some |
| 526 | * time. |
| 527 | */ |
Mika Westerberg | 62efe69 | 2018-09-17 16:32:13 +0300 | [diff] [blame] | 528 | tb_port_dbg(port, |
| 529 | "is connected, link is not up (state: %d), retrying...\n", |
| 530 | state); |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 531 | msleep(100); |
| 532 | } |
| 533 | tb_port_warn(port, |
| 534 | "failed to reach state TB_PORT_UP. Ignoring port...\n"); |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | /** |
Andreas Noever | 520b670 | 2014-06-03 22:04:07 +0200 | [diff] [blame] | 539 | * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port |
| 540 | * |
| 541 | * Change the number of NFC credits allocated to @port by @credits. To remove |
| 542 | * NFC credits pass a negative amount of credits. |
| 543 | * |
| 544 | * Return: Returns 0 on success or an error code on failure. |
| 545 | */ |
| 546 | int tb_port_add_nfc_credits(struct tb_port *port, int credits) |
| 547 | { |
Mika Westerberg | c5ee6fe | 2018-10-11 11:38:22 +0300 | [diff] [blame] | 548 | u32 nfc_credits; |
| 549 | |
| 550 | if (credits == 0 || port->sw->is_unplugged) |
Andreas Noever | 520b670 | 2014-06-03 22:04:07 +0200 | [diff] [blame] | 551 | return 0; |
Mika Westerberg | c5ee6fe | 2018-10-11 11:38:22 +0300 | [diff] [blame] | 552 | |
| 553 | nfc_credits = port->config.nfc_credits & TB_PORT_NFC_CREDITS_MASK; |
| 554 | nfc_credits += credits; |
| 555 | |
| 556 | tb_port_dbg(port, "adding %d NFC credits to %lu", |
| 557 | credits, port->config.nfc_credits & TB_PORT_NFC_CREDITS_MASK); |
| 558 | |
| 559 | port->config.nfc_credits &= ~TB_PORT_NFC_CREDITS_MASK; |
| 560 | port->config.nfc_credits |= nfc_credits; |
| 561 | |
Andreas Noever | 520b670 | 2014-06-03 22:04:07 +0200 | [diff] [blame] | 562 | return tb_port_write(port, &port->config.nfc_credits, |
| 563 | TB_CFG_PORT, 4, 1); |
| 564 | } |
| 565 | |
| 566 | /** |
Mika Westerberg | 44242d6 | 2018-09-28 16:35:32 +0300 | [diff] [blame] | 567 | * tb_port_set_initial_credits() - Set initial port link credits allocated |
| 568 | * @port: Port to set the initial credits |
| 569 | * @credits: Number of credits to to allocate |
| 570 | * |
| 571 | * Set initial credits value to be used for ingress shared buffering. |
| 572 | */ |
| 573 | int tb_port_set_initial_credits(struct tb_port *port, u32 credits) |
| 574 | { |
| 575 | u32 data; |
| 576 | int ret; |
| 577 | |
| 578 | ret = tb_port_read(port, &data, TB_CFG_PORT, 5, 1); |
| 579 | if (ret) |
| 580 | return ret; |
| 581 | |
| 582 | data &= ~TB_PORT_LCA_MASK; |
| 583 | data |= (credits << TB_PORT_LCA_SHIFT) & TB_PORT_LCA_MASK; |
| 584 | |
| 585 | return tb_port_write(port, &data, TB_CFG_PORT, 5, 1); |
| 586 | } |
| 587 | |
| 588 | /** |
Andreas Noever | 520b670 | 2014-06-03 22:04:07 +0200 | [diff] [blame] | 589 | * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER |
| 590 | * |
| 591 | * Return: Returns 0 on success or an error code on failure. |
| 592 | */ |
| 593 | int tb_port_clear_counter(struct tb_port *port, int counter) |
| 594 | { |
| 595 | u32 zero[3] = { 0, 0, 0 }; |
Mika Westerberg | 62efe69 | 2018-09-17 16:32:13 +0300 | [diff] [blame] | 596 | tb_port_dbg(port, "clearing counter %d\n", counter); |
Andreas Noever | 520b670 | 2014-06-03 22:04:07 +0200 | [diff] [blame] | 597 | return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3); |
| 598 | } |
| 599 | |
| 600 | /** |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 601 | * tb_init_port() - initialize a port |
| 602 | * |
| 603 | * This is a helper method for tb_switch_alloc. Does not check or initialize |
| 604 | * any downstream switches. |
| 605 | * |
| 606 | * Return: Returns 0 on success or an error code on failure. |
| 607 | */ |
Andreas Noever | 343fcb8 | 2014-06-12 23:11:47 +0200 | [diff] [blame] | 608 | static int tb_init_port(struct tb_port *port) |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 609 | { |
| 610 | int res; |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 611 | int cap; |
Andreas Noever | 343fcb8 | 2014-06-12 23:11:47 +0200 | [diff] [blame] | 612 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 613 | res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8); |
Mika Westerberg | d94dcbb | 2018-07-04 08:50:01 +0300 | [diff] [blame] | 614 | if (res) { |
| 615 | if (res == -ENODEV) { |
| 616 | tb_dbg(port->sw->tb, " Port %d: not implemented\n", |
| 617 | port->port); |
| 618 | return 0; |
| 619 | } |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 620 | return res; |
Mika Westerberg | d94dcbb | 2018-07-04 08:50:01 +0300 | [diff] [blame] | 621 | } |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 622 | |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 623 | /* Port 0 is the switch itself and has no PHY. */ |
Andreas Noever | 343fcb8 | 2014-06-12 23:11:47 +0200 | [diff] [blame] | 624 | if (port->config.type == TB_TYPE_PORT && port->port != 0) { |
Mika Westerberg | da2da04 | 2017-06-06 15:24:58 +0300 | [diff] [blame] | 625 | cap = tb_port_find_cap(port, TB_PORT_CAP_PHY); |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 626 | |
| 627 | if (cap > 0) |
| 628 | port->cap_phy = cap; |
| 629 | else |
| 630 | tb_port_WARN(port, "non switch port without a PHY\n"); |
Mika Westerberg | 56183c8 | 2017-02-19 10:39:34 +0200 | [diff] [blame] | 631 | } else if (port->port != 0) { |
| 632 | cap = tb_port_find_cap(port, TB_PORT_CAP_ADAP); |
| 633 | if (cap > 0) |
| 634 | port->cap_adap = cap; |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 635 | } |
| 636 | |
Andreas Noever | 343fcb8 | 2014-06-12 23:11:47 +0200 | [diff] [blame] | 637 | tb_dump_port(port->sw->tb, &port->config); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 638 | |
Mika Westerberg | 0b2863a | 2017-02-19 16:57:27 +0200 | [diff] [blame] | 639 | /* Control port does not need HopID allocation */ |
| 640 | if (port->port) { |
| 641 | ida_init(&port->in_hopids); |
| 642 | ida_init(&port->out_hopids); |
| 643 | } |
| 644 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 645 | return 0; |
| 646 | |
| 647 | } |
| 648 | |
Mika Westerberg | 0b2863a | 2017-02-19 16:57:27 +0200 | [diff] [blame] | 649 | static int tb_port_alloc_hopid(struct tb_port *port, bool in, int min_hopid, |
| 650 | int max_hopid) |
| 651 | { |
| 652 | int port_max_hopid; |
| 653 | struct ida *ida; |
| 654 | |
| 655 | if (in) { |
| 656 | port_max_hopid = port->config.max_in_hop_id; |
| 657 | ida = &port->in_hopids; |
| 658 | } else { |
| 659 | port_max_hopid = port->config.max_out_hop_id; |
| 660 | ida = &port->out_hopids; |
| 661 | } |
| 662 | |
| 663 | /* HopIDs 0-7 are reserved */ |
| 664 | if (min_hopid < TB_PATH_MIN_HOPID) |
| 665 | min_hopid = TB_PATH_MIN_HOPID; |
| 666 | |
| 667 | if (max_hopid < 0 || max_hopid > port_max_hopid) |
| 668 | max_hopid = port_max_hopid; |
| 669 | |
| 670 | return ida_simple_get(ida, min_hopid, max_hopid + 1, GFP_KERNEL); |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * tb_port_alloc_in_hopid() - Allocate input HopID from port |
| 675 | * @port: Port to allocate HopID for |
| 676 | * @min_hopid: Minimum acceptable input HopID |
| 677 | * @max_hopid: Maximum acceptable input HopID |
| 678 | * |
| 679 | * Return: HopID between @min_hopid and @max_hopid or negative errno in |
| 680 | * case of error. |
| 681 | */ |
| 682 | int tb_port_alloc_in_hopid(struct tb_port *port, int min_hopid, int max_hopid) |
| 683 | { |
| 684 | return tb_port_alloc_hopid(port, true, min_hopid, max_hopid); |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * tb_port_alloc_out_hopid() - Allocate output HopID from port |
| 689 | * @port: Port to allocate HopID for |
| 690 | * @min_hopid: Minimum acceptable output HopID |
| 691 | * @max_hopid: Maximum acceptable output HopID |
| 692 | * |
| 693 | * Return: HopID between @min_hopid and @max_hopid or negative errno in |
| 694 | * case of error. |
| 695 | */ |
| 696 | int tb_port_alloc_out_hopid(struct tb_port *port, int min_hopid, int max_hopid) |
| 697 | { |
| 698 | return tb_port_alloc_hopid(port, false, min_hopid, max_hopid); |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * tb_port_release_in_hopid() - Release allocated input HopID from port |
| 703 | * @port: Port whose HopID to release |
| 704 | * @hopid: HopID to release |
| 705 | */ |
| 706 | void tb_port_release_in_hopid(struct tb_port *port, int hopid) |
| 707 | { |
| 708 | ida_simple_remove(&port->in_hopids, hopid); |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * tb_port_release_out_hopid() - Release allocated output HopID from port |
| 713 | * @port: Port whose HopID to release |
| 714 | * @hopid: HopID to release |
| 715 | */ |
| 716 | void tb_port_release_out_hopid(struct tb_port *port, int hopid) |
| 717 | { |
| 718 | ida_simple_remove(&port->out_hopids, hopid); |
| 719 | } |
| 720 | |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 721 | /** |
Mika Westerberg | fb19fac | 2017-02-19 21:51:30 +0200 | [diff] [blame] | 722 | * tb_next_port_on_path() - Return next port for given port on a path |
| 723 | * @start: Start port of the walk |
| 724 | * @end: End port of the walk |
| 725 | * @prev: Previous port (%NULL if this is the first) |
| 726 | * |
| 727 | * This function can be used to walk from one port to another if they |
| 728 | * are connected through zero or more switches. If the @prev is dual |
| 729 | * link port, the function follows that link and returns another end on |
| 730 | * that same link. |
| 731 | * |
| 732 | * If the @end port has been reached, return %NULL. |
| 733 | * |
| 734 | * Domain tb->lock must be held when this function is called. |
| 735 | */ |
| 736 | struct tb_port *tb_next_port_on_path(struct tb_port *start, struct tb_port *end, |
| 737 | struct tb_port *prev) |
| 738 | { |
| 739 | struct tb_port *next; |
| 740 | |
| 741 | if (!prev) |
| 742 | return start; |
| 743 | |
| 744 | if (prev->sw == end->sw) { |
| 745 | if (prev == end) |
| 746 | return NULL; |
| 747 | return end; |
| 748 | } |
| 749 | |
| 750 | if (start->sw->config.depth < end->sw->config.depth) { |
| 751 | if (prev->remote && |
| 752 | prev->remote->sw->config.depth > prev->sw->config.depth) |
| 753 | next = prev->remote; |
| 754 | else |
| 755 | next = tb_port_at(tb_route(end->sw), prev->sw); |
| 756 | } else { |
| 757 | if (tb_is_upstream_port(prev)) { |
| 758 | next = prev->remote; |
| 759 | } else { |
| 760 | next = tb_upstream_port(prev->sw); |
| 761 | /* |
| 762 | * Keep the same link if prev and next are both |
| 763 | * dual link ports. |
| 764 | */ |
| 765 | if (next->dual_link_port && |
| 766 | next->link_nr != prev->link_nr) { |
| 767 | next = next->dual_link_port; |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | return next; |
| 773 | } |
| 774 | |
| 775 | /** |
Mika Westerberg | e78db6f | 2017-10-12 16:45:50 +0300 | [diff] [blame] | 776 | * tb_port_is_enabled() - Is the adapter port enabled |
| 777 | * @port: Port to check |
| 778 | */ |
| 779 | bool tb_port_is_enabled(struct tb_port *port) |
| 780 | { |
| 781 | switch (port->config.type) { |
| 782 | case TB_TYPE_PCIE_UP: |
| 783 | case TB_TYPE_PCIE_DOWN: |
| 784 | return tb_pci_port_is_enabled(port); |
| 785 | |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 786 | case TB_TYPE_DP_HDMI_IN: |
| 787 | case TB_TYPE_DP_HDMI_OUT: |
| 788 | return tb_dp_port_is_enabled(port); |
| 789 | |
Mika Westerberg | e78db6f | 2017-10-12 16:45:50 +0300 | [diff] [blame] | 790 | default: |
| 791 | return false; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | /** |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 796 | * tb_pci_port_is_enabled() - Is the PCIe adapter port enabled |
| 797 | * @port: PCIe port to check |
| 798 | */ |
| 799 | bool tb_pci_port_is_enabled(struct tb_port *port) |
| 800 | { |
| 801 | u32 data; |
| 802 | |
| 803 | if (tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap, 1)) |
| 804 | return false; |
| 805 | |
| 806 | return !!(data & TB_PCI_EN); |
| 807 | } |
| 808 | |
| 809 | /** |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 810 | * tb_pci_port_enable() - Enable PCIe adapter port |
| 811 | * @port: PCIe port to enable |
| 812 | * @enable: Enable/disable the PCIe adapter |
| 813 | */ |
| 814 | int tb_pci_port_enable(struct tb_port *port, bool enable) |
| 815 | { |
| 816 | u32 word = enable ? TB_PCI_EN : 0x0; |
| 817 | if (!port->cap_adap) |
| 818 | return -ENXIO; |
| 819 | return tb_port_write(port, &word, TB_CFG_PORT, port->cap_adap, 1); |
| 820 | } |
| 821 | |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 822 | /** |
| 823 | * tb_dp_port_hpd_is_active() - Is HPD already active |
| 824 | * @port: DP out port to check |
| 825 | * |
| 826 | * Checks if the DP OUT adapter port has HDP bit already set. |
| 827 | */ |
| 828 | int tb_dp_port_hpd_is_active(struct tb_port *port) |
| 829 | { |
| 830 | u32 data; |
| 831 | int ret; |
| 832 | |
| 833 | ret = tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap + 2, 1); |
| 834 | if (ret) |
| 835 | return ret; |
| 836 | |
| 837 | return !!(data & TB_DP_HDP); |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * tb_dp_port_hpd_clear() - Clear HPD from DP IN port |
| 842 | * @port: Port to clear HPD |
| 843 | * |
| 844 | * If the DP IN port has HDP set, this function can be used to clear it. |
| 845 | */ |
| 846 | int tb_dp_port_hpd_clear(struct tb_port *port) |
| 847 | { |
| 848 | u32 data; |
| 849 | int ret; |
| 850 | |
| 851 | ret = tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap + 3, 1); |
| 852 | if (ret) |
| 853 | return ret; |
| 854 | |
| 855 | data |= TB_DP_HPDC; |
| 856 | return tb_port_write(port, &data, TB_CFG_PORT, port->cap_adap + 3, 1); |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * tb_dp_port_set_hops() - Set video/aux Hop IDs for DP port |
| 861 | * @port: DP IN/OUT port to set hops |
| 862 | * @video: Video Hop ID |
| 863 | * @aux_tx: AUX TX Hop ID |
| 864 | * @aux_rx: AUX RX Hop ID |
| 865 | * |
| 866 | * Programs specified Hop IDs for DP IN/OUT port. |
| 867 | */ |
| 868 | int tb_dp_port_set_hops(struct tb_port *port, unsigned int video, |
| 869 | unsigned int aux_tx, unsigned int aux_rx) |
| 870 | { |
| 871 | u32 data[2]; |
| 872 | int ret; |
| 873 | |
| 874 | ret = tb_port_read(port, data, TB_CFG_PORT, port->cap_adap, |
| 875 | ARRAY_SIZE(data)); |
| 876 | if (ret) |
| 877 | return ret; |
| 878 | |
| 879 | data[0] &= ~TB_DP_VIDEO_HOPID_MASK; |
| 880 | data[1] &= ~(TB_DP_AUX_RX_HOPID_MASK | TB_DP_AUX_TX_HOPID_MASK); |
| 881 | |
| 882 | data[0] |= (video << TB_DP_VIDEO_HOPID_SHIFT) & TB_DP_VIDEO_HOPID_MASK; |
| 883 | data[1] |= aux_tx & TB_DP_AUX_TX_HOPID_MASK; |
| 884 | data[1] |= (aux_rx << TB_DP_AUX_RX_HOPID_SHIFT) & TB_DP_AUX_RX_HOPID_MASK; |
| 885 | |
| 886 | return tb_port_write(port, data, TB_CFG_PORT, port->cap_adap, |
| 887 | ARRAY_SIZE(data)); |
| 888 | } |
| 889 | |
| 890 | /** |
| 891 | * tb_dp_port_is_enabled() - Is DP adapter port enabled |
| 892 | * @port: DP adapter port to check |
| 893 | */ |
| 894 | bool tb_dp_port_is_enabled(struct tb_port *port) |
| 895 | { |
| 896 | u32 data; |
| 897 | |
| 898 | if (tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap, 1)) |
| 899 | return false; |
| 900 | |
| 901 | return !!(data & (TB_DP_VIDEO_EN | TB_DP_AUX_EN)); |
| 902 | } |
| 903 | |
| 904 | /** |
| 905 | * tb_dp_port_enable() - Enables/disables DP paths of a port |
| 906 | * @port: DP IN/OUT port |
| 907 | * @enable: Enable/disable DP path |
| 908 | * |
| 909 | * Once Hop IDs are programmed DP paths can be enabled or disabled by |
| 910 | * calling this function. |
| 911 | */ |
| 912 | int tb_dp_port_enable(struct tb_port *port, bool enable) |
| 913 | { |
| 914 | u32 data; |
| 915 | int ret; |
| 916 | |
| 917 | ret = tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap, 1); |
| 918 | if (ret) |
| 919 | return ret; |
| 920 | |
| 921 | if (enable) |
| 922 | data |= TB_DP_VIDEO_EN | TB_DP_AUX_EN; |
| 923 | else |
| 924 | data &= ~(TB_DP_VIDEO_EN | TB_DP_AUX_EN); |
| 925 | |
| 926 | return tb_port_write(port, &data, TB_CFG_PORT, port->cap_adap, 1); |
| 927 | } |
| 928 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 929 | /* switch utility functions */ |
| 930 | |
| 931 | static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw) |
| 932 | { |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 933 | tb_dbg(tb, " Switch: %x:%x (Revision: %d, TB Version: %d)\n", |
| 934 | sw->vendor_id, sw->device_id, sw->revision, |
| 935 | sw->thunderbolt_version); |
| 936 | tb_dbg(tb, " Max Port Number: %d\n", sw->max_port_number); |
| 937 | tb_dbg(tb, " Config:\n"); |
| 938 | tb_dbg(tb, |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 939 | " 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] | 940 | sw->upstream_port_number, sw->depth, |
| 941 | (((u64) sw->route_hi) << 32) | sw->route_lo, |
| 942 | sw->enabled, sw->plug_events_delay); |
| 943 | tb_dbg(tb, " unknown1: %#x unknown4: %#x\n", |
| 944 | sw->__unknown1, sw->__unknown4); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 945 | } |
| 946 | |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 947 | /** |
| 948 | * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET |
| 949 | * |
| 950 | * Return: Returns 0 on success or an error code on failure. |
| 951 | */ |
| 952 | int tb_switch_reset(struct tb *tb, u64 route) |
| 953 | { |
| 954 | struct tb_cfg_result res; |
| 955 | struct tb_regs_switch_header header = { |
| 956 | header.route_hi = route >> 32, |
| 957 | header.route_lo = route, |
| 958 | header.enabled = true, |
| 959 | }; |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 960 | tb_dbg(tb, "resetting switch at %llx\n", route); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 961 | res.err = tb_cfg_write(tb->ctl, ((u32 *) &header) + 2, route, |
| 962 | 0, 2, 2, 2); |
| 963 | if (res.err) |
| 964 | return res.err; |
| 965 | res = tb_cfg_reset(tb->ctl, route, TB_CFG_DEFAULT_TIMEOUT); |
| 966 | if (res.err > 0) |
| 967 | return -EIO; |
| 968 | return res.err; |
| 969 | } |
| 970 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 971 | /** |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame] | 972 | * tb_plug_events_active() - enable/disable plug events on a switch |
| 973 | * |
| 974 | * Also configures a sane plug_events_delay of 255ms. |
| 975 | * |
| 976 | * Return: Returns 0 on success or an error code on failure. |
| 977 | */ |
| 978 | static int tb_plug_events_active(struct tb_switch *sw, bool active) |
| 979 | { |
| 980 | u32 data; |
| 981 | int res; |
| 982 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 983 | if (!sw->config.enabled) |
| 984 | return 0; |
| 985 | |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame] | 986 | sw->config.plug_events_delay = 0xff; |
| 987 | res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1); |
| 988 | if (res) |
| 989 | return res; |
| 990 | |
| 991 | res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1); |
| 992 | if (res) |
| 993 | return res; |
| 994 | |
| 995 | if (active) { |
| 996 | data = data & 0xFFFFFF83; |
| 997 | switch (sw->config.device_id) { |
Lukas Wunner | 1d11140 | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 998 | case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE: |
| 999 | case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE: |
| 1000 | case PCI_DEVICE_ID_INTEL_PORT_RIDGE: |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame] | 1001 | break; |
| 1002 | default: |
| 1003 | data |= 4; |
| 1004 | } |
| 1005 | } else { |
| 1006 | data = data | 0x7c; |
| 1007 | } |
| 1008 | return tb_sw_write(sw, &data, TB_CFG_SWITCH, |
| 1009 | sw->cap_plug_events + 1, 1); |
| 1010 | } |
| 1011 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1012 | static ssize_t authorized_show(struct device *dev, |
| 1013 | struct device_attribute *attr, |
| 1014 | char *buf) |
| 1015 | { |
| 1016 | struct tb_switch *sw = tb_to_switch(dev); |
| 1017 | |
| 1018 | return sprintf(buf, "%u\n", sw->authorized); |
| 1019 | } |
| 1020 | |
| 1021 | static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val) |
| 1022 | { |
| 1023 | int ret = -EINVAL; |
| 1024 | |
Mika Westerberg | 09f11b6 | 2019-03-19 16:48:41 +0200 | [diff] [blame] | 1025 | if (!mutex_trylock(&sw->tb->lock)) |
| 1026 | return restart_syscall(); |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1027 | |
| 1028 | if (sw->authorized) |
| 1029 | goto unlock; |
| 1030 | |
Mika Westerberg | a03e828 | 2018-01-18 20:27:47 +0300 | [diff] [blame] | 1031 | /* |
| 1032 | * Make sure there is no PCIe rescan ongoing when a new PCIe |
| 1033 | * tunnel is created. Otherwise the PCIe rescan code might find |
| 1034 | * the new tunnel too early. |
| 1035 | */ |
| 1036 | pci_lock_rescan_remove(); |
| 1037 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1038 | switch (val) { |
| 1039 | /* Approve switch */ |
| 1040 | case 1: |
| 1041 | if (sw->key) |
| 1042 | ret = tb_domain_approve_switch_key(sw->tb, sw); |
| 1043 | else |
| 1044 | ret = tb_domain_approve_switch(sw->tb, sw); |
| 1045 | break; |
| 1046 | |
| 1047 | /* Challenge switch */ |
| 1048 | case 2: |
| 1049 | if (sw->key) |
| 1050 | ret = tb_domain_challenge_switch_key(sw->tb, sw); |
| 1051 | break; |
| 1052 | |
| 1053 | default: |
| 1054 | break; |
| 1055 | } |
| 1056 | |
Mika Westerberg | a03e828 | 2018-01-18 20:27:47 +0300 | [diff] [blame] | 1057 | pci_unlock_rescan_remove(); |
| 1058 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1059 | if (!ret) { |
| 1060 | sw->authorized = val; |
| 1061 | /* Notify status change to the userspace */ |
| 1062 | kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE); |
| 1063 | } |
| 1064 | |
| 1065 | unlock: |
Mika Westerberg | 09f11b6 | 2019-03-19 16:48:41 +0200 | [diff] [blame] | 1066 | mutex_unlock(&sw->tb->lock); |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1067 | return ret; |
| 1068 | } |
| 1069 | |
| 1070 | static ssize_t authorized_store(struct device *dev, |
| 1071 | struct device_attribute *attr, |
| 1072 | const char *buf, size_t count) |
| 1073 | { |
| 1074 | struct tb_switch *sw = tb_to_switch(dev); |
| 1075 | unsigned int val; |
| 1076 | ssize_t ret; |
| 1077 | |
| 1078 | ret = kstrtouint(buf, 0, &val); |
| 1079 | if (ret) |
| 1080 | return ret; |
| 1081 | if (val > 2) |
| 1082 | return -EINVAL; |
| 1083 | |
Mika Westerberg | 4f7c2e0 | 2019-05-28 18:56:20 +0300 | [diff] [blame] | 1084 | pm_runtime_get_sync(&sw->dev); |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1085 | ret = tb_switch_set_authorized(sw, val); |
Mika Westerberg | 4f7c2e0 | 2019-05-28 18:56:20 +0300 | [diff] [blame] | 1086 | pm_runtime_mark_last_busy(&sw->dev); |
| 1087 | pm_runtime_put_autosuspend(&sw->dev); |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1088 | |
| 1089 | return ret ? ret : count; |
| 1090 | } |
| 1091 | static DEVICE_ATTR_RW(authorized); |
| 1092 | |
Yehezkel Bernat | 14862ee | 2018-01-22 12:50:09 +0200 | [diff] [blame] | 1093 | static ssize_t boot_show(struct device *dev, struct device_attribute *attr, |
| 1094 | char *buf) |
| 1095 | { |
| 1096 | struct tb_switch *sw = tb_to_switch(dev); |
| 1097 | |
| 1098 | return sprintf(buf, "%u\n", sw->boot); |
| 1099 | } |
| 1100 | static DEVICE_ATTR_RO(boot); |
| 1101 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1102 | static ssize_t device_show(struct device *dev, struct device_attribute *attr, |
| 1103 | char *buf) |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1104 | { |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1105 | struct tb_switch *sw = tb_to_switch(dev); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1106 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1107 | return sprintf(buf, "%#x\n", sw->device); |
| 1108 | } |
| 1109 | static DEVICE_ATTR_RO(device); |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame] | 1110 | |
Mika Westerberg | 72ee339 | 2017-06-06 15:25:05 +0300 | [diff] [blame] | 1111 | static ssize_t |
| 1112 | device_name_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 1113 | { |
| 1114 | struct tb_switch *sw = tb_to_switch(dev); |
| 1115 | |
| 1116 | return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : ""); |
| 1117 | } |
| 1118 | static DEVICE_ATTR_RO(device_name); |
| 1119 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1120 | static ssize_t key_show(struct device *dev, struct device_attribute *attr, |
| 1121 | char *buf) |
| 1122 | { |
| 1123 | struct tb_switch *sw = tb_to_switch(dev); |
| 1124 | ssize_t ret; |
| 1125 | |
Mika Westerberg | 09f11b6 | 2019-03-19 16:48:41 +0200 | [diff] [blame] | 1126 | if (!mutex_trylock(&sw->tb->lock)) |
| 1127 | return restart_syscall(); |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1128 | |
| 1129 | if (sw->key) |
| 1130 | ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key); |
| 1131 | else |
| 1132 | ret = sprintf(buf, "\n"); |
| 1133 | |
Mika Westerberg | 09f11b6 | 2019-03-19 16:48:41 +0200 | [diff] [blame] | 1134 | mutex_unlock(&sw->tb->lock); |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1135 | return ret; |
| 1136 | } |
| 1137 | |
| 1138 | static ssize_t key_store(struct device *dev, struct device_attribute *attr, |
| 1139 | const char *buf, size_t count) |
| 1140 | { |
| 1141 | struct tb_switch *sw = tb_to_switch(dev); |
| 1142 | u8 key[TB_SWITCH_KEY_SIZE]; |
| 1143 | ssize_t ret = count; |
Bernat, Yehezkel | e545f0d | 2017-08-15 08:19:20 +0300 | [diff] [blame] | 1144 | bool clear = false; |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1145 | |
Bernat, Yehezkel | e545f0d | 2017-08-15 08:19:20 +0300 | [diff] [blame] | 1146 | if (!strcmp(buf, "\n")) |
| 1147 | clear = true; |
| 1148 | else if (hex2bin(key, buf, sizeof(key))) |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1149 | return -EINVAL; |
| 1150 | |
Mika Westerberg | 09f11b6 | 2019-03-19 16:48:41 +0200 | [diff] [blame] | 1151 | if (!mutex_trylock(&sw->tb->lock)) |
| 1152 | return restart_syscall(); |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1153 | |
| 1154 | if (sw->authorized) { |
| 1155 | ret = -EBUSY; |
| 1156 | } else { |
| 1157 | kfree(sw->key); |
Bernat, Yehezkel | e545f0d | 2017-08-15 08:19:20 +0300 | [diff] [blame] | 1158 | if (clear) { |
| 1159 | sw->key = NULL; |
| 1160 | } else { |
| 1161 | sw->key = kmemdup(key, sizeof(key), GFP_KERNEL); |
| 1162 | if (!sw->key) |
| 1163 | ret = -ENOMEM; |
| 1164 | } |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1165 | } |
| 1166 | |
Mika Westerberg | 09f11b6 | 2019-03-19 16:48:41 +0200 | [diff] [blame] | 1167 | mutex_unlock(&sw->tb->lock); |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1168 | return ret; |
| 1169 | } |
Bernat, Yehezkel | 0956e41 | 2017-08-15 08:19:12 +0300 | [diff] [blame] | 1170 | static DEVICE_ATTR(key, 0600, key_show, key_store); |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1171 | |
Mika Westerberg | 1830b6e | 2018-11-26 12:47:46 +0300 | [diff] [blame] | 1172 | static void nvm_authenticate_start(struct tb_switch *sw) |
| 1173 | { |
| 1174 | struct pci_dev *root_port; |
| 1175 | |
| 1176 | /* |
| 1177 | * During host router NVM upgrade we should not allow root port to |
| 1178 | * go into D3cold because some root ports cannot trigger PME |
| 1179 | * itself. To be on the safe side keep the root port in D0 during |
| 1180 | * the whole upgrade process. |
| 1181 | */ |
| 1182 | root_port = pci_find_pcie_root_port(sw->tb->nhi->pdev); |
| 1183 | if (root_port) |
| 1184 | pm_runtime_get_noresume(&root_port->dev); |
| 1185 | } |
| 1186 | |
| 1187 | static void nvm_authenticate_complete(struct tb_switch *sw) |
| 1188 | { |
| 1189 | struct pci_dev *root_port; |
| 1190 | |
| 1191 | root_port = pci_find_pcie_root_port(sw->tb->nhi->pdev); |
| 1192 | if (root_port) |
| 1193 | pm_runtime_put(&root_port->dev); |
| 1194 | } |
| 1195 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1196 | static ssize_t nvm_authenticate_show(struct device *dev, |
| 1197 | struct device_attribute *attr, char *buf) |
| 1198 | { |
| 1199 | struct tb_switch *sw = tb_to_switch(dev); |
| 1200 | u32 status; |
| 1201 | |
| 1202 | nvm_get_auth_status(sw, &status); |
| 1203 | return sprintf(buf, "%#x\n", status); |
| 1204 | } |
| 1205 | |
| 1206 | static ssize_t nvm_authenticate_store(struct device *dev, |
| 1207 | struct device_attribute *attr, const char *buf, size_t count) |
| 1208 | { |
| 1209 | struct tb_switch *sw = tb_to_switch(dev); |
| 1210 | bool val; |
| 1211 | int ret; |
| 1212 | |
Mika Westerberg | 4f7c2e0 | 2019-05-28 18:56:20 +0300 | [diff] [blame] | 1213 | pm_runtime_get_sync(&sw->dev); |
| 1214 | |
| 1215 | if (!mutex_trylock(&sw->tb->lock)) { |
| 1216 | ret = restart_syscall(); |
| 1217 | goto exit_rpm; |
| 1218 | } |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1219 | |
| 1220 | /* If NVMem devices are not yet added */ |
| 1221 | if (!sw->nvm) { |
| 1222 | ret = -EAGAIN; |
| 1223 | goto exit_unlock; |
| 1224 | } |
| 1225 | |
| 1226 | ret = kstrtobool(buf, &val); |
| 1227 | if (ret) |
| 1228 | goto exit_unlock; |
| 1229 | |
| 1230 | /* Always clear the authentication status */ |
| 1231 | nvm_clear_auth_status(sw); |
| 1232 | |
| 1233 | if (val) { |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 1234 | if (!sw->nvm->buf) { |
| 1235 | ret = -EINVAL; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1236 | goto exit_unlock; |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 1237 | } |
| 1238 | |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 1239 | ret = nvm_validate_and_write(sw); |
Mika Westerberg | 4f7c2e0 | 2019-05-28 18:56:20 +0300 | [diff] [blame] | 1240 | if (ret) |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 1241 | goto exit_unlock; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1242 | |
| 1243 | sw->nvm->authenticating = true; |
| 1244 | |
Mika Westerberg | 1830b6e | 2018-11-26 12:47:46 +0300 | [diff] [blame] | 1245 | if (!tb_route(sw)) { |
| 1246 | /* |
| 1247 | * Keep root port from suspending as long as the |
| 1248 | * NVM upgrade process is running. |
| 1249 | */ |
| 1250 | nvm_authenticate_start(sw); |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1251 | ret = nvm_authenticate_host(sw); |
Mika Westerberg | 1830b6e | 2018-11-26 12:47:46 +0300 | [diff] [blame] | 1252 | if (ret) |
| 1253 | nvm_authenticate_complete(sw); |
| 1254 | } else { |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1255 | ret = nvm_authenticate_device(sw); |
Mika Westerberg | 1830b6e | 2018-11-26 12:47:46 +0300 | [diff] [blame] | 1256 | } |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | exit_unlock: |
Mika Westerberg | 09f11b6 | 2019-03-19 16:48:41 +0200 | [diff] [blame] | 1260 | mutex_unlock(&sw->tb->lock); |
Mika Westerberg | 4f7c2e0 | 2019-05-28 18:56:20 +0300 | [diff] [blame] | 1261 | exit_rpm: |
| 1262 | pm_runtime_mark_last_busy(&sw->dev); |
| 1263 | pm_runtime_put_autosuspend(&sw->dev); |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1264 | |
| 1265 | if (ret) |
| 1266 | return ret; |
| 1267 | return count; |
| 1268 | } |
| 1269 | static DEVICE_ATTR_RW(nvm_authenticate); |
| 1270 | |
| 1271 | static ssize_t nvm_version_show(struct device *dev, |
| 1272 | struct device_attribute *attr, char *buf) |
| 1273 | { |
| 1274 | struct tb_switch *sw = tb_to_switch(dev); |
| 1275 | int ret; |
| 1276 | |
Mika Westerberg | 09f11b6 | 2019-03-19 16:48:41 +0200 | [diff] [blame] | 1277 | if (!mutex_trylock(&sw->tb->lock)) |
| 1278 | return restart_syscall(); |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1279 | |
| 1280 | if (sw->safe_mode) |
| 1281 | ret = -ENODATA; |
| 1282 | else if (!sw->nvm) |
| 1283 | ret = -EAGAIN; |
| 1284 | else |
| 1285 | ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor); |
| 1286 | |
Mika Westerberg | 09f11b6 | 2019-03-19 16:48:41 +0200 | [diff] [blame] | 1287 | mutex_unlock(&sw->tb->lock); |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1288 | |
| 1289 | return ret; |
| 1290 | } |
| 1291 | static DEVICE_ATTR_RO(nvm_version); |
| 1292 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1293 | static ssize_t vendor_show(struct device *dev, struct device_attribute *attr, |
| 1294 | char *buf) |
| 1295 | { |
| 1296 | struct tb_switch *sw = tb_to_switch(dev); |
| 1297 | |
| 1298 | return sprintf(buf, "%#x\n", sw->vendor); |
| 1299 | } |
| 1300 | static DEVICE_ATTR_RO(vendor); |
| 1301 | |
Mika Westerberg | 72ee339 | 2017-06-06 15:25:05 +0300 | [diff] [blame] | 1302 | static ssize_t |
| 1303 | vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 1304 | { |
| 1305 | struct tb_switch *sw = tb_to_switch(dev); |
| 1306 | |
| 1307 | return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : ""); |
| 1308 | } |
| 1309 | static DEVICE_ATTR_RO(vendor_name); |
| 1310 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1311 | static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr, |
| 1312 | char *buf) |
| 1313 | { |
| 1314 | struct tb_switch *sw = tb_to_switch(dev); |
| 1315 | |
| 1316 | return sprintf(buf, "%pUb\n", sw->uuid); |
| 1317 | } |
| 1318 | static DEVICE_ATTR_RO(unique_id); |
| 1319 | |
| 1320 | static struct attribute *switch_attrs[] = { |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1321 | &dev_attr_authorized.attr, |
Yehezkel Bernat | 14862ee | 2018-01-22 12:50:09 +0200 | [diff] [blame] | 1322 | &dev_attr_boot.attr, |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1323 | &dev_attr_device.attr, |
Mika Westerberg | 72ee339 | 2017-06-06 15:25:05 +0300 | [diff] [blame] | 1324 | &dev_attr_device_name.attr, |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1325 | &dev_attr_key.attr, |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1326 | &dev_attr_nvm_authenticate.attr, |
| 1327 | &dev_attr_nvm_version.attr, |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1328 | &dev_attr_vendor.attr, |
Mika Westerberg | 72ee339 | 2017-06-06 15:25:05 +0300 | [diff] [blame] | 1329 | &dev_attr_vendor_name.attr, |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1330 | &dev_attr_unique_id.attr, |
| 1331 | NULL, |
| 1332 | }; |
| 1333 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1334 | static umode_t switch_attr_is_visible(struct kobject *kobj, |
| 1335 | struct attribute *attr, int n) |
| 1336 | { |
| 1337 | struct device *dev = container_of(kobj, struct device, kobj); |
| 1338 | struct tb_switch *sw = tb_to_switch(dev); |
| 1339 | |
Mika Westerberg | 58f414f | 2018-09-11 15:34:23 +0300 | [diff] [blame^] | 1340 | if (attr == &dev_attr_device.attr) { |
| 1341 | if (!sw->device) |
| 1342 | return 0; |
| 1343 | } else if (attr == &dev_attr_device_name.attr) { |
| 1344 | if (!sw->device_name) |
| 1345 | return 0; |
| 1346 | } else if (attr == &dev_attr_vendor.attr) { |
| 1347 | if (!sw->vendor) |
| 1348 | return 0; |
| 1349 | } else if (attr == &dev_attr_vendor_name.attr) { |
| 1350 | if (!sw->vendor_name) |
| 1351 | return 0; |
| 1352 | } else if (attr == &dev_attr_key.attr) { |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1353 | if (tb_route(sw) && |
| 1354 | sw->tb->security_level == TB_SECURITY_SECURE && |
| 1355 | sw->security_level == TB_SECURITY_SECURE) |
| 1356 | return attr->mode; |
| 1357 | return 0; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1358 | } else if (attr == &dev_attr_nvm_authenticate.attr || |
| 1359 | attr == &dev_attr_nvm_version.attr) { |
| 1360 | if (sw->dma_port) |
| 1361 | return attr->mode; |
| 1362 | return 0; |
Yehezkel Bernat | 14862ee | 2018-01-22 12:50:09 +0200 | [diff] [blame] | 1363 | } else if (attr == &dev_attr_boot.attr) { |
| 1364 | if (tb_route(sw)) |
| 1365 | return attr->mode; |
| 1366 | return 0; |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1367 | } |
| 1368 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1369 | return sw->safe_mode ? 0 : attr->mode; |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1370 | } |
| 1371 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1372 | static struct attribute_group switch_group = { |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1373 | .is_visible = switch_attr_is_visible, |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1374 | .attrs = switch_attrs, |
| 1375 | }; |
| 1376 | |
| 1377 | static const struct attribute_group *switch_groups[] = { |
| 1378 | &switch_group, |
| 1379 | NULL, |
| 1380 | }; |
| 1381 | |
| 1382 | static void tb_switch_release(struct device *dev) |
| 1383 | { |
| 1384 | struct tb_switch *sw = tb_to_switch(dev); |
Mika Westerberg | 0b2863a | 2017-02-19 16:57:27 +0200 | [diff] [blame] | 1385 | int i; |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1386 | |
Mika Westerberg | 3e13676 | 2017-06-06 15:25:14 +0300 | [diff] [blame] | 1387 | dma_port_free(sw->dma_port); |
| 1388 | |
Mika Westerberg | 0b2863a | 2017-02-19 16:57:27 +0200 | [diff] [blame] | 1389 | for (i = 1; i <= sw->config.max_port_number; i++) { |
| 1390 | if (!sw->ports[i].disabled) { |
| 1391 | ida_destroy(&sw->ports[i].in_hopids); |
| 1392 | ida_destroy(&sw->ports[i].out_hopids); |
| 1393 | } |
| 1394 | } |
| 1395 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1396 | kfree(sw->uuid); |
Mika Westerberg | 72ee339 | 2017-06-06 15:25:05 +0300 | [diff] [blame] | 1397 | kfree(sw->device_name); |
| 1398 | kfree(sw->vendor_name); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1399 | kfree(sw->ports); |
Andreas Noever | 343fcb8 | 2014-06-12 23:11:47 +0200 | [diff] [blame] | 1400 | kfree(sw->drom); |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1401 | kfree(sw->key); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1402 | kfree(sw); |
| 1403 | } |
| 1404 | |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 1405 | /* |
| 1406 | * Currently only need to provide the callbacks. Everything else is handled |
| 1407 | * in the connection manager. |
| 1408 | */ |
| 1409 | static int __maybe_unused tb_switch_runtime_suspend(struct device *dev) |
| 1410 | { |
Mika Westerberg | 4f7c2e0 | 2019-05-28 18:56:20 +0300 | [diff] [blame] | 1411 | struct tb_switch *sw = tb_to_switch(dev); |
| 1412 | const struct tb_cm_ops *cm_ops = sw->tb->cm_ops; |
| 1413 | |
| 1414 | if (cm_ops->runtime_suspend_switch) |
| 1415 | return cm_ops->runtime_suspend_switch(sw); |
| 1416 | |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 1417 | return 0; |
| 1418 | } |
| 1419 | |
| 1420 | static int __maybe_unused tb_switch_runtime_resume(struct device *dev) |
| 1421 | { |
Mika Westerberg | 4f7c2e0 | 2019-05-28 18:56:20 +0300 | [diff] [blame] | 1422 | struct tb_switch *sw = tb_to_switch(dev); |
| 1423 | const struct tb_cm_ops *cm_ops = sw->tb->cm_ops; |
| 1424 | |
| 1425 | if (cm_ops->runtime_resume_switch) |
| 1426 | return cm_ops->runtime_resume_switch(sw); |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 1427 | return 0; |
| 1428 | } |
| 1429 | |
| 1430 | static const struct dev_pm_ops tb_switch_pm_ops = { |
| 1431 | SET_RUNTIME_PM_OPS(tb_switch_runtime_suspend, tb_switch_runtime_resume, |
| 1432 | NULL) |
| 1433 | }; |
| 1434 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1435 | struct device_type tb_switch_type = { |
| 1436 | .name = "thunderbolt_device", |
| 1437 | .release = tb_switch_release, |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 1438 | .pm = &tb_switch_pm_ops, |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1439 | }; |
| 1440 | |
Mika Westerberg | 2c3c419 | 2017-06-06 15:25:13 +0300 | [diff] [blame] | 1441 | static int tb_switch_get_generation(struct tb_switch *sw) |
| 1442 | { |
| 1443 | switch (sw->config.device_id) { |
| 1444 | case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE: |
| 1445 | case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE: |
| 1446 | case PCI_DEVICE_ID_INTEL_LIGHT_PEAK: |
| 1447 | case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C: |
| 1448 | case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C: |
| 1449 | case PCI_DEVICE_ID_INTEL_PORT_RIDGE: |
| 1450 | case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE: |
| 1451 | case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE: |
| 1452 | return 1; |
| 1453 | |
| 1454 | case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE: |
| 1455 | case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE: |
| 1456 | case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE: |
| 1457 | return 2; |
| 1458 | |
| 1459 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE: |
| 1460 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE: |
| 1461 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE: |
| 1462 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE: |
| 1463 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE: |
Radion Mirchevsky | 4bac471 | 2017-10-04 16:43:43 +0300 | [diff] [blame] | 1464 | case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE: |
| 1465 | case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE: |
| 1466 | case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE: |
Mika Westerberg | 2c3c419 | 2017-06-06 15:25:13 +0300 | [diff] [blame] | 1467 | return 3; |
| 1468 | |
| 1469 | default: |
| 1470 | /* |
| 1471 | * For unknown switches assume generation to be 1 to be |
| 1472 | * on the safe side. |
| 1473 | */ |
| 1474 | tb_sw_warn(sw, "unsupported switch device id %#x\n", |
| 1475 | sw->config.device_id); |
| 1476 | return 1; |
| 1477 | } |
| 1478 | } |
| 1479 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1480 | /** |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1481 | * tb_switch_alloc() - allocate a switch |
| 1482 | * @tb: Pointer to the owning domain |
| 1483 | * @parent: Parent device for this switch |
| 1484 | * @route: Route string for this switch |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1485 | * |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1486 | * Allocates and initializes a switch. Will not upload configuration to |
| 1487 | * the switch. For that you need to call tb_switch_configure() |
| 1488 | * separately. The returned switch should be released by calling |
| 1489 | * tb_switch_put(). |
| 1490 | * |
Mika Westerberg | 444ac38 | 2018-12-30 12:17:52 +0200 | [diff] [blame] | 1491 | * Return: Pointer to the allocated switch or ERR_PTR() in case of |
| 1492 | * failure. |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1493 | */ |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1494 | struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent, |
| 1495 | u64 route) |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1496 | { |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1497 | struct tb_switch *sw; |
Mika Westerberg | f0342e7 | 2018-12-30 12:14:46 +0200 | [diff] [blame] | 1498 | int upstream_port; |
Mika Westerberg | 444ac38 | 2018-12-30 12:17:52 +0200 | [diff] [blame] | 1499 | int i, ret, depth; |
Mika Westerberg | f0342e7 | 2018-12-30 12:14:46 +0200 | [diff] [blame] | 1500 | |
| 1501 | /* Make sure we do not exceed maximum topology limit */ |
| 1502 | depth = tb_route_length(route); |
| 1503 | if (depth > TB_SWITCH_MAX_DEPTH) |
Mika Westerberg | 444ac38 | 2018-12-30 12:17:52 +0200 | [diff] [blame] | 1504 | return ERR_PTR(-EADDRNOTAVAIL); |
Mika Westerberg | f0342e7 | 2018-12-30 12:14:46 +0200 | [diff] [blame] | 1505 | |
| 1506 | upstream_port = tb_cfg_get_upstream_port(tb->ctl, route); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1507 | if (upstream_port < 0) |
Mika Westerberg | 444ac38 | 2018-12-30 12:17:52 +0200 | [diff] [blame] | 1508 | return ERR_PTR(upstream_port); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1509 | |
| 1510 | sw = kzalloc(sizeof(*sw), GFP_KERNEL); |
| 1511 | if (!sw) |
Mika Westerberg | 444ac38 | 2018-12-30 12:17:52 +0200 | [diff] [blame] | 1512 | return ERR_PTR(-ENOMEM); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1513 | |
| 1514 | sw->tb = tb; |
Mika Westerberg | 444ac38 | 2018-12-30 12:17:52 +0200 | [diff] [blame] | 1515 | ret = tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5); |
| 1516 | if (ret) |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1517 | goto err_free_sw_ports; |
| 1518 | |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 1519 | tb_dbg(tb, "current switch config:\n"); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1520 | tb_dump_switch(tb, &sw->config); |
| 1521 | |
| 1522 | /* configure switch */ |
| 1523 | sw->config.upstream_port_number = upstream_port; |
Mika Westerberg | f0342e7 | 2018-12-30 12:14:46 +0200 | [diff] [blame] | 1524 | sw->config.depth = depth; |
| 1525 | sw->config.route_hi = upper_32_bits(route); |
| 1526 | sw->config.route_lo = lower_32_bits(route); |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1527 | sw->config.enabled = 0; |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1528 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1529 | /* initialize ports */ |
| 1530 | sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports), |
| 1531 | GFP_KERNEL); |
Mika Westerberg | 444ac38 | 2018-12-30 12:17:52 +0200 | [diff] [blame] | 1532 | if (!sw->ports) { |
| 1533 | ret = -ENOMEM; |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1534 | goto err_free_sw_ports; |
Mika Westerberg | 444ac38 | 2018-12-30 12:17:52 +0200 | [diff] [blame] | 1535 | } |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1536 | |
| 1537 | for (i = 0; i <= sw->config.max_port_number; i++) { |
| 1538 | /* minimum setup for tb_find_cap and tb_drom_read to work */ |
| 1539 | sw->ports[i].sw = sw; |
| 1540 | sw->ports[i].port = i; |
| 1541 | } |
| 1542 | |
Mika Westerberg | 2c3c419 | 2017-06-06 15:25:13 +0300 | [diff] [blame] | 1543 | sw->generation = tb_switch_get_generation(sw); |
| 1544 | |
Mika Westerberg | 444ac38 | 2018-12-30 12:17:52 +0200 | [diff] [blame] | 1545 | ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS); |
| 1546 | if (ret < 0) { |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1547 | tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n"); |
| 1548 | goto err_free_sw_ports; |
| 1549 | } |
Mika Westerberg | 444ac38 | 2018-12-30 12:17:52 +0200 | [diff] [blame] | 1550 | sw->cap_plug_events = ret; |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1551 | |
Mika Westerberg | 444ac38 | 2018-12-30 12:17:52 +0200 | [diff] [blame] | 1552 | ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER); |
| 1553 | if (ret > 0) |
| 1554 | sw->cap_lc = ret; |
Mika Westerberg | a9be558 | 2019-01-09 16:42:12 +0200 | [diff] [blame] | 1555 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1556 | /* Root switch is always authorized */ |
| 1557 | if (!route) |
| 1558 | sw->authorized = true; |
| 1559 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1560 | device_initialize(&sw->dev); |
| 1561 | sw->dev.parent = parent; |
| 1562 | sw->dev.bus = &tb_bus_type; |
| 1563 | sw->dev.type = &tb_switch_type; |
| 1564 | sw->dev.groups = switch_groups; |
| 1565 | dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); |
| 1566 | |
| 1567 | return sw; |
| 1568 | |
| 1569 | err_free_sw_ports: |
| 1570 | kfree(sw->ports); |
| 1571 | kfree(sw); |
| 1572 | |
Mika Westerberg | 444ac38 | 2018-12-30 12:17:52 +0200 | [diff] [blame] | 1573 | return ERR_PTR(ret); |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | /** |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1577 | * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode |
| 1578 | * @tb: Pointer to the owning domain |
| 1579 | * @parent: Parent device for this switch |
| 1580 | * @route: Route string for this switch |
| 1581 | * |
| 1582 | * This creates a switch in safe mode. This means the switch pretty much |
| 1583 | * lacks all capabilities except DMA configuration port before it is |
| 1584 | * flashed with a valid NVM firmware. |
| 1585 | * |
| 1586 | * The returned switch must be released by calling tb_switch_put(). |
| 1587 | * |
Mika Westerberg | 444ac38 | 2018-12-30 12:17:52 +0200 | [diff] [blame] | 1588 | * Return: Pointer to the allocated switch or ERR_PTR() in case of failure |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1589 | */ |
| 1590 | struct tb_switch * |
| 1591 | tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route) |
| 1592 | { |
| 1593 | struct tb_switch *sw; |
| 1594 | |
| 1595 | sw = kzalloc(sizeof(*sw), GFP_KERNEL); |
| 1596 | if (!sw) |
Mika Westerberg | 444ac38 | 2018-12-30 12:17:52 +0200 | [diff] [blame] | 1597 | return ERR_PTR(-ENOMEM); |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1598 | |
| 1599 | sw->tb = tb; |
| 1600 | sw->config.depth = tb_route_length(route); |
| 1601 | sw->config.route_hi = upper_32_bits(route); |
| 1602 | sw->config.route_lo = lower_32_bits(route); |
| 1603 | sw->safe_mode = true; |
| 1604 | |
| 1605 | device_initialize(&sw->dev); |
| 1606 | sw->dev.parent = parent; |
| 1607 | sw->dev.bus = &tb_bus_type; |
| 1608 | sw->dev.type = &tb_switch_type; |
| 1609 | sw->dev.groups = switch_groups; |
| 1610 | dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); |
| 1611 | |
| 1612 | return sw; |
| 1613 | } |
| 1614 | |
| 1615 | /** |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1616 | * tb_switch_configure() - Uploads configuration to the switch |
| 1617 | * @sw: Switch to configure |
| 1618 | * |
| 1619 | * Call this function before the switch is added to the system. It will |
| 1620 | * upload configuration to the switch and makes it available for the |
| 1621 | * connection manager to use. |
| 1622 | * |
| 1623 | * Return: %0 in case of success and negative errno in case of failure |
| 1624 | */ |
| 1625 | int tb_switch_configure(struct tb_switch *sw) |
| 1626 | { |
| 1627 | struct tb *tb = sw->tb; |
| 1628 | u64 route; |
| 1629 | int ret; |
| 1630 | |
| 1631 | route = tb_route(sw); |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 1632 | tb_dbg(tb, "initializing Switch at %#llx (depth: %d, up port: %d)\n", |
| 1633 | route, tb_route_length(route), sw->config.upstream_port_number); |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1634 | |
| 1635 | if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL) |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1636 | tb_sw_warn(sw, "unknown switch vendor id %#x\n", |
| 1637 | sw->config.vendor_id); |
| 1638 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1639 | sw->config.enabled = 1; |
| 1640 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1641 | /* upload configuration */ |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1642 | ret = tb_sw_write(sw, 1 + (u32 *)&sw->config, TB_CFG_SWITCH, 1, 3); |
| 1643 | if (ret) |
| 1644 | return ret; |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1645 | |
Mika Westerberg | e879a70 | 2018-10-11 12:33:08 +0300 | [diff] [blame] | 1646 | ret = tb_lc_configure_link(sw); |
| 1647 | if (ret) |
| 1648 | return ret; |
| 1649 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1650 | return tb_plug_events_active(sw, true); |
| 1651 | } |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1652 | |
Aditya Pakki | 2cc1275 | 2019-03-20 10:57:54 -0500 | [diff] [blame] | 1653 | static int tb_switch_set_uuid(struct tb_switch *sw) |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1654 | { |
| 1655 | u32 uuid[4]; |
Mika Westerberg | a9be558 | 2019-01-09 16:42:12 +0200 | [diff] [blame] | 1656 | int ret; |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1657 | |
| 1658 | if (sw->uuid) |
Mika Westerberg | a9be558 | 2019-01-09 16:42:12 +0200 | [diff] [blame] | 1659 | return 0; |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1660 | |
| 1661 | /* |
| 1662 | * The newer controllers include fused UUID as part of link |
| 1663 | * controller specific registers |
| 1664 | */ |
Mika Westerberg | a9be558 | 2019-01-09 16:42:12 +0200 | [diff] [blame] | 1665 | ret = tb_lc_read_uuid(sw, uuid); |
| 1666 | if (ret) { |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1667 | /* |
| 1668 | * ICM generates UUID based on UID and fills the upper |
| 1669 | * two words with ones. This is not strictly following |
| 1670 | * UUID format but we want to be compatible with it so |
| 1671 | * we do the same here. |
| 1672 | */ |
| 1673 | uuid[0] = sw->uid & 0xffffffff; |
| 1674 | uuid[1] = (sw->uid >> 32) & 0xffffffff; |
| 1675 | uuid[2] = 0xffffffff; |
| 1676 | uuid[3] = 0xffffffff; |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1677 | } |
| 1678 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1679 | sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL); |
Aditya Pakki | 2cc1275 | 2019-03-20 10:57:54 -0500 | [diff] [blame] | 1680 | if (!sw->uuid) |
Mika Westerberg | a9be558 | 2019-01-09 16:42:12 +0200 | [diff] [blame] | 1681 | return -ENOMEM; |
| 1682 | return 0; |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1683 | } |
| 1684 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1685 | static int tb_switch_add_dma_port(struct tb_switch *sw) |
Mika Westerberg | 3e13676 | 2017-06-06 15:25:14 +0300 | [diff] [blame] | 1686 | { |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1687 | u32 status; |
| 1688 | int ret; |
| 1689 | |
Mika Westerberg | 3e13676 | 2017-06-06 15:25:14 +0300 | [diff] [blame] | 1690 | switch (sw->generation) { |
| 1691 | case 3: |
| 1692 | break; |
| 1693 | |
| 1694 | case 2: |
| 1695 | /* Only root switch can be upgraded */ |
| 1696 | if (tb_route(sw)) |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1697 | return 0; |
Mika Westerberg | 3e13676 | 2017-06-06 15:25:14 +0300 | [diff] [blame] | 1698 | break; |
| 1699 | |
| 1700 | default: |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1701 | /* |
| 1702 | * DMA port is the only thing available when the switch |
| 1703 | * is in safe mode. |
| 1704 | */ |
| 1705 | if (!sw->safe_mode) |
| 1706 | return 0; |
| 1707 | break; |
Mika Westerberg | 3e13676 | 2017-06-06 15:25:14 +0300 | [diff] [blame] | 1708 | } |
| 1709 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1710 | if (sw->no_nvm_upgrade) |
| 1711 | return 0; |
| 1712 | |
Mika Westerberg | 3e13676 | 2017-06-06 15:25:14 +0300 | [diff] [blame] | 1713 | sw->dma_port = dma_port_alloc(sw); |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1714 | if (!sw->dma_port) |
| 1715 | return 0; |
| 1716 | |
| 1717 | /* |
| 1718 | * Check status of the previous flash authentication. If there |
| 1719 | * is one we need to power cycle the switch in any case to make |
| 1720 | * it functional again. |
| 1721 | */ |
| 1722 | ret = dma_port_flash_update_auth_status(sw->dma_port, &status); |
| 1723 | if (ret <= 0) |
| 1724 | return ret; |
| 1725 | |
Mika Westerberg | 1830b6e | 2018-11-26 12:47:46 +0300 | [diff] [blame] | 1726 | /* Now we can allow root port to suspend again */ |
| 1727 | if (!tb_route(sw)) |
| 1728 | nvm_authenticate_complete(sw); |
| 1729 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1730 | if (status) { |
| 1731 | tb_sw_info(sw, "switch flash authentication failed\n"); |
Aditya Pakki | 2cc1275 | 2019-03-20 10:57:54 -0500 | [diff] [blame] | 1732 | ret = tb_switch_set_uuid(sw); |
| 1733 | if (ret) |
| 1734 | return ret; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1735 | nvm_set_auth_status(sw, status); |
| 1736 | } |
| 1737 | |
| 1738 | tb_sw_info(sw, "power cycling the switch now\n"); |
| 1739 | dma_port_power_cycle(sw->dma_port); |
| 1740 | |
| 1741 | /* |
| 1742 | * We return error here which causes the switch adding failure. |
| 1743 | * It should appear back after power cycle is complete. |
| 1744 | */ |
| 1745 | return -ESHUTDOWN; |
Mika Westerberg | 3e13676 | 2017-06-06 15:25:14 +0300 | [diff] [blame] | 1746 | } |
| 1747 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1748 | /** |
| 1749 | * tb_switch_add() - Add a switch to the domain |
| 1750 | * @sw: Switch to add |
| 1751 | * |
| 1752 | * This is the last step in adding switch to the domain. It will read |
| 1753 | * identification information from DROM and initializes ports so that |
| 1754 | * they can be used to connect other switches. The switch will be |
| 1755 | * exposed to the userspace when this function successfully returns. To |
| 1756 | * remove and release the switch, call tb_switch_remove(). |
| 1757 | * |
| 1758 | * Return: %0 in case of success and negative errno in case of failure |
| 1759 | */ |
| 1760 | int tb_switch_add(struct tb_switch *sw) |
| 1761 | { |
| 1762 | int i, ret; |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame] | 1763 | |
Mika Westerberg | 3e13676 | 2017-06-06 15:25:14 +0300 | [diff] [blame] | 1764 | /* |
| 1765 | * Initialize DMA control port now before we read DROM. Recent |
| 1766 | * host controllers have more complete DROM on NVM that includes |
| 1767 | * vendor and model identification strings which we then expose |
| 1768 | * to the userspace. NVM can be accessed through DMA |
| 1769 | * configuration based mailbox. |
| 1770 | */ |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1771 | ret = tb_switch_add_dma_port(sw); |
| 1772 | if (ret) |
Mika Westerberg | f53e767 | 2017-06-06 15:25:02 +0300 | [diff] [blame] | 1773 | return ret; |
Andreas Noever | 343fcb8 | 2014-06-12 23:11:47 +0200 | [diff] [blame] | 1774 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1775 | if (!sw->safe_mode) { |
| 1776 | /* read drom */ |
| 1777 | ret = tb_drom_read(sw); |
| 1778 | if (ret) { |
| 1779 | tb_sw_warn(sw, "tb_eeprom_read_rom failed\n"); |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1780 | return ret; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1781 | } |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 1782 | tb_sw_dbg(sw, "uid: %#llx\n", sw->uid); |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1783 | |
Aditya Pakki | 2cc1275 | 2019-03-20 10:57:54 -0500 | [diff] [blame] | 1784 | ret = tb_switch_set_uuid(sw); |
| 1785 | if (ret) |
| 1786 | return ret; |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1787 | |
| 1788 | for (i = 0; i <= sw->config.max_port_number; i++) { |
| 1789 | if (sw->ports[i].disabled) { |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 1790 | tb_port_dbg(&sw->ports[i], "disabled by eeprom\n"); |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1791 | continue; |
| 1792 | } |
| 1793 | ret = tb_init_port(&sw->ports[i]); |
| 1794 | if (ret) |
| 1795 | return ret; |
| 1796 | } |
Andreas Noever | 343fcb8 | 2014-06-12 23:11:47 +0200 | [diff] [blame] | 1797 | } |
| 1798 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1799 | ret = device_add(&sw->dev); |
| 1800 | if (ret) |
| 1801 | return ret; |
| 1802 | |
Mika Westerberg | a83bc4a | 2018-10-01 12:31:20 +0300 | [diff] [blame] | 1803 | if (tb_route(sw)) { |
| 1804 | dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n", |
| 1805 | sw->vendor, sw->device); |
| 1806 | if (sw->vendor_name && sw->device_name) |
| 1807 | dev_info(&sw->dev, "%s %s\n", sw->vendor_name, |
| 1808 | sw->device_name); |
| 1809 | } |
| 1810 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1811 | ret = tb_switch_nvm_add(sw); |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 1812 | if (ret) { |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1813 | device_del(&sw->dev); |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 1814 | return ret; |
| 1815 | } |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1816 | |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 1817 | pm_runtime_set_active(&sw->dev); |
| 1818 | if (sw->rpm) { |
| 1819 | pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY); |
| 1820 | pm_runtime_use_autosuspend(&sw->dev); |
| 1821 | pm_runtime_mark_last_busy(&sw->dev); |
| 1822 | pm_runtime_enable(&sw->dev); |
| 1823 | pm_request_autosuspend(&sw->dev); |
| 1824 | } |
| 1825 | |
| 1826 | return 0; |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1827 | } |
Andreas Noever | c90553b | 2014-06-03 22:04:11 +0200 | [diff] [blame] | 1828 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1829 | /** |
| 1830 | * tb_switch_remove() - Remove and release a switch |
| 1831 | * @sw: Switch to remove |
| 1832 | * |
| 1833 | * This will remove the switch from the domain and release it after last |
| 1834 | * reference count drops to zero. If there are switches connected below |
| 1835 | * this switch, they will be removed as well. |
| 1836 | */ |
| 1837 | void tb_switch_remove(struct tb_switch *sw) |
| 1838 | { |
| 1839 | int i; |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame] | 1840 | |
Mika Westerberg | 2d8ff0b | 2018-07-25 11:48:39 +0300 | [diff] [blame] | 1841 | if (sw->rpm) { |
| 1842 | pm_runtime_get_sync(&sw->dev); |
| 1843 | pm_runtime_disable(&sw->dev); |
| 1844 | } |
| 1845 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1846 | /* port 0 is the switch itself and never has a remote */ |
| 1847 | for (i = 1; i <= sw->config.max_port_number; i++) { |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 1848 | if (tb_port_has_remote(&sw->ports[i])) { |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1849 | tb_switch_remove(sw->ports[i].remote->sw); |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 1850 | sw->ports[i].remote = NULL; |
| 1851 | } else if (sw->ports[i].xdomain) { |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 1852 | tb_xdomain_remove(sw->ports[i].xdomain); |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 1853 | sw->ports[i].xdomain = NULL; |
| 1854 | } |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | if (!sw->is_unplugged) |
| 1858 | tb_plug_events_active(sw, false); |
Mika Westerberg | e879a70 | 2018-10-11 12:33:08 +0300 | [diff] [blame] | 1859 | tb_lc_unconfigure_link(sw); |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1860 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1861 | tb_switch_nvm_remove(sw); |
Mika Westerberg | a83bc4a | 2018-10-01 12:31:20 +0300 | [diff] [blame] | 1862 | |
| 1863 | if (tb_route(sw)) |
| 1864 | dev_info(&sw->dev, "device disconnected\n"); |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1865 | device_unregister(&sw->dev); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1866 | } |
| 1867 | |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 1868 | /** |
Lukas Wunner | aae20bb | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 1869 | * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 1870 | */ |
Lukas Wunner | aae20bb | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 1871 | void tb_sw_set_unplugged(struct tb_switch *sw) |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 1872 | { |
| 1873 | int i; |
| 1874 | if (sw == sw->tb->root_switch) { |
| 1875 | tb_sw_WARN(sw, "cannot unplug root switch\n"); |
| 1876 | return; |
| 1877 | } |
| 1878 | if (sw->is_unplugged) { |
| 1879 | tb_sw_WARN(sw, "is_unplugged already set\n"); |
| 1880 | return; |
| 1881 | } |
| 1882 | sw->is_unplugged = true; |
| 1883 | for (i = 0; i <= sw->config.max_port_number; i++) { |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 1884 | if (tb_port_has_remote(&sw->ports[i])) |
Lukas Wunner | aae20bb | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 1885 | tb_sw_set_unplugged(sw->ports[i].remote->sw); |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1886 | else if (sw->ports[i].xdomain) |
| 1887 | sw->ports[i].xdomain->is_unplugged = true; |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 1888 | } |
| 1889 | } |
| 1890 | |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1891 | int tb_switch_resume(struct tb_switch *sw) |
| 1892 | { |
| 1893 | int i, err; |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 1894 | tb_sw_dbg(sw, "resuming switch\n"); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1895 | |
Mika Westerberg | 08a5e4c | 2017-06-06 15:24:54 +0300 | [diff] [blame] | 1896 | /* |
| 1897 | * Check for UID of the connected switches except for root |
| 1898 | * switch which we assume cannot be removed. |
| 1899 | */ |
| 1900 | if (tb_route(sw)) { |
| 1901 | u64 uid; |
| 1902 | |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1903 | /* |
| 1904 | * Check first that we can still read the switch config |
| 1905 | * space. It may be that there is now another domain |
| 1906 | * connected. |
| 1907 | */ |
| 1908 | err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw)); |
| 1909 | if (err < 0) { |
| 1910 | tb_sw_info(sw, "switch not present anymore\n"); |
| 1911 | return err; |
| 1912 | } |
| 1913 | |
Mika Westerberg | 08a5e4c | 2017-06-06 15:24:54 +0300 | [diff] [blame] | 1914 | err = tb_drom_read_uid_only(sw, &uid); |
| 1915 | if (err) { |
| 1916 | tb_sw_warn(sw, "uid read failed\n"); |
| 1917 | return err; |
| 1918 | } |
| 1919 | if (sw->uid != uid) { |
| 1920 | tb_sw_info(sw, |
| 1921 | "changed while suspended (uid %#llx -> %#llx)\n", |
| 1922 | sw->uid, uid); |
| 1923 | return -ENODEV; |
| 1924 | } |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | /* upload configuration */ |
| 1928 | err = tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3); |
| 1929 | if (err) |
| 1930 | return err; |
| 1931 | |
Mika Westerberg | e879a70 | 2018-10-11 12:33:08 +0300 | [diff] [blame] | 1932 | err = tb_lc_configure_link(sw); |
| 1933 | if (err) |
| 1934 | return err; |
| 1935 | |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1936 | err = tb_plug_events_active(sw, true); |
| 1937 | if (err) |
| 1938 | return err; |
| 1939 | |
| 1940 | /* check for surviving downstream switches */ |
| 1941 | for (i = 1; i <= sw->config.max_port_number; i++) { |
| 1942 | struct tb_port *port = &sw->ports[i]; |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 1943 | |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1944 | if (!tb_port_has_remote(port) && !port->xdomain) |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1945 | continue; |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 1946 | |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1947 | if (tb_wait_for_port(port, true) <= 0) { |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1948 | tb_port_warn(port, |
| 1949 | "lost during suspend, disconnecting\n"); |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1950 | if (tb_port_has_remote(port)) |
| 1951 | tb_sw_set_unplugged(port->remote->sw); |
| 1952 | else if (port->xdomain) |
| 1953 | port->xdomain->is_unplugged = true; |
| 1954 | } else if (tb_port_has_remote(port)) { |
| 1955 | if (tb_switch_resume(port->remote->sw)) { |
| 1956 | tb_port_warn(port, |
| 1957 | "lost during suspend, disconnecting\n"); |
| 1958 | tb_sw_set_unplugged(port->remote->sw); |
| 1959 | } |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1960 | } |
| 1961 | } |
| 1962 | return 0; |
| 1963 | } |
| 1964 | |
| 1965 | void tb_switch_suspend(struct tb_switch *sw) |
| 1966 | { |
| 1967 | int i, err; |
| 1968 | err = tb_plug_events_active(sw, false); |
| 1969 | if (err) |
| 1970 | return; |
| 1971 | |
| 1972 | for (i = 1; i <= sw->config.max_port_number; i++) { |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 1973 | if (tb_port_has_remote(&sw->ports[i])) |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1974 | tb_switch_suspend(sw->ports[i].remote->sw); |
| 1975 | } |
Mika Westerberg | 5480dfc | 2019-01-09 17:25:43 +0200 | [diff] [blame] | 1976 | |
| 1977 | tb_lc_set_sleep(sw); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1978 | } |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1979 | |
| 1980 | struct tb_sw_lookup { |
| 1981 | struct tb *tb; |
| 1982 | u8 link; |
| 1983 | u8 depth; |
Christoph Hellwig | 7c39ffe | 2017-07-18 15:30:05 +0200 | [diff] [blame] | 1984 | const uuid_t *uuid; |
Radion Mirchevsky | 8e9267b | 2017-10-04 15:24:14 +0300 | [diff] [blame] | 1985 | u64 route; |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1986 | }; |
| 1987 | |
Suzuki K Poulose | 418e3ea | 2019-06-14 18:53:59 +0100 | [diff] [blame] | 1988 | static int tb_switch_match(struct device *dev, const void *data) |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1989 | { |
| 1990 | struct tb_switch *sw = tb_to_switch(dev); |
Suzuki K Poulose | 418e3ea | 2019-06-14 18:53:59 +0100 | [diff] [blame] | 1991 | const struct tb_sw_lookup *lookup = data; |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 1992 | |
| 1993 | if (!sw) |
| 1994 | return 0; |
| 1995 | if (sw->tb != lookup->tb) |
| 1996 | return 0; |
| 1997 | |
| 1998 | if (lookup->uuid) |
| 1999 | return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid)); |
| 2000 | |
Radion Mirchevsky | 8e9267b | 2017-10-04 15:24:14 +0300 | [diff] [blame] | 2001 | if (lookup->route) { |
| 2002 | return sw->config.route_lo == lower_32_bits(lookup->route) && |
| 2003 | sw->config.route_hi == upper_32_bits(lookup->route); |
| 2004 | } |
| 2005 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 2006 | /* Root switch is matched only by depth */ |
| 2007 | if (!lookup->depth) |
| 2008 | return !sw->depth; |
| 2009 | |
| 2010 | return sw->link == lookup->link && sw->depth == lookup->depth; |
| 2011 | } |
| 2012 | |
| 2013 | /** |
| 2014 | * tb_switch_find_by_link_depth() - Find switch by link and depth |
| 2015 | * @tb: Domain the switch belongs |
| 2016 | * @link: Link number the switch is connected |
| 2017 | * @depth: Depth of the switch in link |
| 2018 | * |
| 2019 | * Returned switch has reference count increased so the caller needs to |
| 2020 | * call tb_switch_put() when done with the switch. |
| 2021 | */ |
| 2022 | struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth) |
| 2023 | { |
| 2024 | struct tb_sw_lookup lookup; |
| 2025 | struct device *dev; |
| 2026 | |
| 2027 | memset(&lookup, 0, sizeof(lookup)); |
| 2028 | lookup.tb = tb; |
| 2029 | lookup.link = link; |
| 2030 | lookup.depth = depth; |
| 2031 | |
| 2032 | dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match); |
| 2033 | if (dev) |
| 2034 | return tb_to_switch(dev); |
| 2035 | |
| 2036 | return NULL; |
| 2037 | } |
| 2038 | |
| 2039 | /** |
Radion Mirchevsky | 432019d | 2017-10-04 15:07:40 +0300 | [diff] [blame] | 2040 | * tb_switch_find_by_uuid() - Find switch by UUID |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 2041 | * @tb: Domain the switch belongs |
| 2042 | * @uuid: UUID to look for |
| 2043 | * |
| 2044 | * Returned switch has reference count increased so the caller needs to |
| 2045 | * call tb_switch_put() when done with the switch. |
| 2046 | */ |
Christoph Hellwig | 7c39ffe | 2017-07-18 15:30:05 +0200 | [diff] [blame] | 2047 | 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] | 2048 | { |
| 2049 | struct tb_sw_lookup lookup; |
| 2050 | struct device *dev; |
| 2051 | |
| 2052 | memset(&lookup, 0, sizeof(lookup)); |
| 2053 | lookup.tb = tb; |
| 2054 | lookup.uuid = uuid; |
| 2055 | |
| 2056 | dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match); |
| 2057 | if (dev) |
| 2058 | return tb_to_switch(dev); |
| 2059 | |
| 2060 | return NULL; |
| 2061 | } |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 2062 | |
Radion Mirchevsky | 8e9267b | 2017-10-04 15:24:14 +0300 | [diff] [blame] | 2063 | /** |
| 2064 | * tb_switch_find_by_route() - Find switch by route string |
| 2065 | * @tb: Domain the switch belongs |
| 2066 | * @route: Route string to look for |
| 2067 | * |
| 2068 | * Returned switch has reference count increased so the caller needs to |
| 2069 | * call tb_switch_put() when done with the switch. |
| 2070 | */ |
| 2071 | struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route) |
| 2072 | { |
| 2073 | struct tb_sw_lookup lookup; |
| 2074 | struct device *dev; |
| 2075 | |
| 2076 | if (!route) |
| 2077 | return tb_switch_get(tb->root_switch); |
| 2078 | |
| 2079 | memset(&lookup, 0, sizeof(lookup)); |
| 2080 | lookup.tb = tb; |
| 2081 | lookup.route = route; |
| 2082 | |
| 2083 | dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match); |
| 2084 | if (dev) |
| 2085 | return tb_to_switch(dev); |
| 2086 | |
| 2087 | return NULL; |
| 2088 | } |
| 2089 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 2090 | void tb_switch_exit(void) |
| 2091 | { |
| 2092 | ida_destroy(&nvm_ida); |
| 2093 | } |