Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
a25c8b2f | 2 | /* |
15c6784c | 3 | * Thunderbolt driver - switch/port utility functions |
a25c8b2f AN |
4 | * |
5 | * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> | |
15c6784c | 6 | * Copyright (C) 2018, Intel Corporation |
a25c8b2f AN |
7 | */ |
8 | ||
9 | #include <linux/delay.h> | |
e6b245cc MW |
10 | #include <linux/idr.h> |
11 | #include <linux/nvmem-provider.h> | |
2d8ff0b5 | 12 | #include <linux/pm_runtime.h> |
09f11b6c | 13 | #include <linux/sched/signal.h> |
e6b245cc | 14 | #include <linux/sizes.h> |
10fefe56 | 15 | #include <linux/slab.h> |
a25c8b2f AN |
16 | |
17 | #include "tb.h" | |
18 | ||
e6b245cc MW |
19 | /* Switch NVM support */ |
20 | ||
e6b245cc | 21 | #define NVM_CSS 0x10 |
e6b245cc MW |
22 | |
23 | struct nvm_auth_status { | |
24 | struct list_head list; | |
7c39ffe7 | 25 | uuid_t uuid; |
e6b245cc MW |
26 | u32 status; |
27 | }; | |
28 | ||
4b794f80 ML |
29 | enum nvm_write_ops { |
30 | WRITE_AND_AUTHENTICATE = 1, | |
31 | WRITE_ONLY = 2, | |
32 | }; | |
33 | ||
e6b245cc MW |
34 | /* |
35 | * Hold NVM authentication failure status per switch This information | |
36 | * needs to stay around even when the switch gets power cycled so we | |
37 | * keep it separately. | |
38 | */ | |
39 | static LIST_HEAD(nvm_auth_status_cache); | |
40 | static DEFINE_MUTEX(nvm_auth_status_lock); | |
41 | ||
42 | static struct nvm_auth_status *__nvm_get_auth_status(const struct tb_switch *sw) | |
43 | { | |
44 | struct nvm_auth_status *st; | |
45 | ||
46 | list_for_each_entry(st, &nvm_auth_status_cache, list) { | |
7c39ffe7 | 47 | if (uuid_equal(&st->uuid, sw->uuid)) |
e6b245cc MW |
48 | return st; |
49 | } | |
50 | ||
51 | return NULL; | |
52 | } | |
53 | ||
54 | static void nvm_get_auth_status(const struct tb_switch *sw, u32 *status) | |
55 | { | |
56 | struct nvm_auth_status *st; | |
57 | ||
58 | mutex_lock(&nvm_auth_status_lock); | |
59 | st = __nvm_get_auth_status(sw); | |
60 | mutex_unlock(&nvm_auth_status_lock); | |
61 | ||
62 | *status = st ? st->status : 0; | |
63 | } | |
64 | ||
65 | static void nvm_set_auth_status(const struct tb_switch *sw, u32 status) | |
66 | { | |
67 | struct nvm_auth_status *st; | |
68 | ||
69 | if (WARN_ON(!sw->uuid)) | |
70 | return; | |
71 | ||
72 | mutex_lock(&nvm_auth_status_lock); | |
73 | st = __nvm_get_auth_status(sw); | |
74 | ||
75 | if (!st) { | |
76 | st = kzalloc(sizeof(*st), GFP_KERNEL); | |
77 | if (!st) | |
78 | goto unlock; | |
79 | ||
80 | memcpy(&st->uuid, sw->uuid, sizeof(st->uuid)); | |
81 | INIT_LIST_HEAD(&st->list); | |
82 | list_add_tail(&st->list, &nvm_auth_status_cache); | |
83 | } | |
84 | ||
85 | st->status = status; | |
86 | unlock: | |
87 | mutex_unlock(&nvm_auth_status_lock); | |
88 | } | |
89 | ||
90 | static void nvm_clear_auth_status(const struct tb_switch *sw) | |
91 | { | |
92 | struct nvm_auth_status *st; | |
93 | ||
94 | mutex_lock(&nvm_auth_status_lock); | |
95 | st = __nvm_get_auth_status(sw); | |
96 | if (st) { | |
97 | list_del(&st->list); | |
98 | kfree(st); | |
99 | } | |
100 | mutex_unlock(&nvm_auth_status_lock); | |
101 | } | |
102 | ||
103 | static int nvm_validate_and_write(struct tb_switch *sw) | |
104 | { | |
105 | unsigned int image_size, hdr_size; | |
106 | const u8 *buf = sw->nvm->buf; | |
107 | u16 ds_size; | |
108 | int ret; | |
109 | ||
110 | if (!buf) | |
111 | return -EINVAL; | |
112 | ||
113 | image_size = sw->nvm->buf_data_size; | |
114 | if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE) | |
115 | return -EINVAL; | |
116 | ||
117 | /* | |
118 | * FARB pointer must point inside the image and must at least | |
119 | * contain parts of the digital section we will be reading here. | |
120 | */ | |
121 | hdr_size = (*(u32 *)buf) & 0xffffff; | |
122 | if (hdr_size + NVM_DEVID + 2 >= image_size) | |
123 | return -EINVAL; | |
124 | ||
125 | /* Digital section start should be aligned to 4k page */ | |
126 | if (!IS_ALIGNED(hdr_size, SZ_4K)) | |
127 | return -EINVAL; | |
128 | ||
129 | /* | |
130 | * Read digital section size and check that it also fits inside | |
131 | * the image. | |
132 | */ | |
133 | ds_size = *(u16 *)(buf + hdr_size); | |
134 | if (ds_size >= image_size) | |
135 | return -EINVAL; | |
136 | ||
137 | if (!sw->safe_mode) { | |
138 | u16 device_id; | |
139 | ||
140 | /* | |
141 | * Make sure the device ID in the image matches the one | |
142 | * we read from the switch config space. | |
143 | */ | |
144 | device_id = *(u16 *)(buf + hdr_size + NVM_DEVID); | |
145 | if (device_id != sw->config.device_id) | |
146 | return -EINVAL; | |
147 | ||
148 | if (sw->generation < 3) { | |
149 | /* Write CSS headers first */ | |
150 | ret = dma_port_flash_write(sw->dma_port, | |
151 | DMA_PORT_CSS_ADDRESS, buf + NVM_CSS, | |
152 | DMA_PORT_CSS_MAX_SIZE); | |
153 | if (ret) | |
154 | return ret; | |
155 | } | |
156 | ||
157 | /* Skip headers in the image */ | |
158 | buf += hdr_size; | |
159 | image_size -= hdr_size; | |
160 | } | |
161 | ||
b0407983 | 162 | if (tb_switch_is_usb4(sw)) |
4b794f80 ML |
163 | ret = usb4_switch_nvm_write(sw, 0, buf, image_size); |
164 | else | |
165 | ret = dma_port_flash_write(sw->dma_port, 0, buf, image_size); | |
166 | if (!ret) | |
167 | sw->nvm->flushed = true; | |
168 | return ret; | |
e6b245cc MW |
169 | } |
170 | ||
b0407983 | 171 | static int nvm_authenticate_host_dma_port(struct tb_switch *sw) |
e6b245cc | 172 | { |
7a7ebfa8 | 173 | int ret = 0; |
e6b245cc MW |
174 | |
175 | /* | |
176 | * Root switch NVM upgrade requires that we disconnect the | |
d1ff7024 | 177 | * existing paths first (in case it is not in safe mode |
e6b245cc MW |
178 | * already). |
179 | */ | |
180 | if (!sw->safe_mode) { | |
7a7ebfa8 MW |
181 | u32 status; |
182 | ||
d1ff7024 | 183 | ret = tb_domain_disconnect_all_paths(sw->tb); |
e6b245cc MW |
184 | if (ret) |
185 | return ret; | |
186 | /* | |
187 | * The host controller goes away pretty soon after this if | |
188 | * everything goes well so getting timeout is expected. | |
189 | */ | |
190 | ret = dma_port_flash_update_auth(sw->dma_port); | |
7a7ebfa8 MW |
191 | if (!ret || ret == -ETIMEDOUT) |
192 | return 0; | |
193 | ||
194 | /* | |
195 | * Any error from update auth operation requires power | |
196 | * cycling of the host router. | |
197 | */ | |
198 | tb_sw_warn(sw, "failed to authenticate NVM, power cycling\n"); | |
199 | if (dma_port_flash_update_auth_status(sw->dma_port, &status) > 0) | |
200 | nvm_set_auth_status(sw, status); | |
e6b245cc MW |
201 | } |
202 | ||
203 | /* | |
204 | * From safe mode we can get out by just power cycling the | |
205 | * switch. | |
206 | */ | |
207 | dma_port_power_cycle(sw->dma_port); | |
7a7ebfa8 | 208 | return ret; |
e6b245cc MW |
209 | } |
210 | ||
b0407983 | 211 | static int nvm_authenticate_device_dma_port(struct tb_switch *sw) |
e6b245cc MW |
212 | { |
213 | int ret, retries = 10; | |
214 | ||
215 | ret = dma_port_flash_update_auth(sw->dma_port); | |
7a7ebfa8 MW |
216 | switch (ret) { |
217 | case 0: | |
218 | case -ETIMEDOUT: | |
219 | case -EACCES: | |
220 | case -EINVAL: | |
221 | /* Power cycle is required */ | |
222 | break; | |
223 | default: | |
e6b245cc | 224 | return ret; |
7a7ebfa8 | 225 | } |
e6b245cc MW |
226 | |
227 | /* | |
228 | * Poll here for the authentication status. It takes some time | |
229 | * for the device to respond (we get timeout for a while). Once | |
230 | * we get response the device needs to be power cycled in order | |
231 | * to the new NVM to be taken into use. | |
232 | */ | |
233 | do { | |
234 | u32 status; | |
235 | ||
236 | ret = dma_port_flash_update_auth_status(sw->dma_port, &status); | |
237 | if (ret < 0 && ret != -ETIMEDOUT) | |
238 | return ret; | |
239 | if (ret > 0) { | |
240 | if (status) { | |
241 | tb_sw_warn(sw, "failed to authenticate NVM\n"); | |
242 | nvm_set_auth_status(sw, status); | |
243 | } | |
244 | ||
245 | tb_sw_info(sw, "power cycling the switch now\n"); | |
246 | dma_port_power_cycle(sw->dma_port); | |
247 | return 0; | |
248 | } | |
249 | ||
250 | msleep(500); | |
251 | } while (--retries); | |
252 | ||
253 | return -ETIMEDOUT; | |
254 | } | |
255 | ||
b0407983 MW |
256 | static void nvm_authenticate_start_dma_port(struct tb_switch *sw) |
257 | { | |
258 | struct pci_dev *root_port; | |
259 | ||
260 | /* | |
261 | * During host router NVM upgrade we should not allow root port to | |
262 | * go into D3cold because some root ports cannot trigger PME | |
263 | * itself. To be on the safe side keep the root port in D0 during | |
264 | * the whole upgrade process. | |
265 | */ | |
6ae72bfa | 266 | root_port = pcie_find_root_port(sw->tb->nhi->pdev); |
b0407983 MW |
267 | if (root_port) |
268 | pm_runtime_get_noresume(&root_port->dev); | |
269 | } | |
270 | ||
271 | static void nvm_authenticate_complete_dma_port(struct tb_switch *sw) | |
272 | { | |
273 | struct pci_dev *root_port; | |
274 | ||
6ae72bfa | 275 | root_port = pcie_find_root_port(sw->tb->nhi->pdev); |
b0407983 MW |
276 | if (root_port) |
277 | pm_runtime_put(&root_port->dev); | |
278 | } | |
279 | ||
280 | static inline bool nvm_readable(struct tb_switch *sw) | |
281 | { | |
282 | if (tb_switch_is_usb4(sw)) { | |
283 | /* | |
284 | * USB4 devices must support NVM operations but it is | |
285 | * optional for hosts. Therefore we query the NVM sector | |
286 | * size here and if it is supported assume NVM | |
287 | * operations are implemented. | |
288 | */ | |
289 | return usb4_switch_nvm_sector_size(sw) > 0; | |
290 | } | |
291 | ||
292 | /* Thunderbolt 2 and 3 devices support NVM through DMA port */ | |
293 | return !!sw->dma_port; | |
294 | } | |
295 | ||
296 | static inline bool nvm_upgradeable(struct tb_switch *sw) | |
297 | { | |
298 | if (sw->no_nvm_upgrade) | |
299 | return false; | |
300 | return nvm_readable(sw); | |
301 | } | |
302 | ||
303 | static inline int nvm_read(struct tb_switch *sw, unsigned int address, | |
304 | void *buf, size_t size) | |
305 | { | |
306 | if (tb_switch_is_usb4(sw)) | |
307 | return usb4_switch_nvm_read(sw, address, buf, size); | |
308 | return dma_port_flash_read(sw->dma_port, address, buf, size); | |
309 | } | |
310 | ||
311 | static int nvm_authenticate(struct tb_switch *sw) | |
312 | { | |
313 | int ret; | |
314 | ||
315 | if (tb_switch_is_usb4(sw)) | |
316 | return usb4_switch_nvm_authenticate(sw); | |
317 | ||
318 | if (!tb_route(sw)) { | |
319 | nvm_authenticate_start_dma_port(sw); | |
320 | ret = nvm_authenticate_host_dma_port(sw); | |
321 | } else { | |
322 | ret = nvm_authenticate_device_dma_port(sw); | |
323 | } | |
324 | ||
325 | return ret; | |
326 | } | |
327 | ||
e6b245cc MW |
328 | static int tb_switch_nvm_read(void *priv, unsigned int offset, void *val, |
329 | size_t bytes) | |
330 | { | |
719a5fe8 MW |
331 | struct tb_nvm *nvm = priv; |
332 | struct tb_switch *sw = tb_to_switch(nvm->dev); | |
2d8ff0b5 MW |
333 | int ret; |
334 | ||
335 | pm_runtime_get_sync(&sw->dev); | |
4f7c2e0d MW |
336 | |
337 | if (!mutex_trylock(&sw->tb->lock)) { | |
338 | ret = restart_syscall(); | |
339 | goto out; | |
340 | } | |
341 | ||
b0407983 | 342 | ret = nvm_read(sw, offset, val, bytes); |
4f7c2e0d MW |
343 | mutex_unlock(&sw->tb->lock); |
344 | ||
345 | out: | |
2d8ff0b5 MW |
346 | pm_runtime_mark_last_busy(&sw->dev); |
347 | pm_runtime_put_autosuspend(&sw->dev); | |
e6b245cc | 348 | |
2d8ff0b5 | 349 | return ret; |
e6b245cc MW |
350 | } |
351 | ||
352 | static int tb_switch_nvm_write(void *priv, unsigned int offset, void *val, | |
353 | size_t bytes) | |
354 | { | |
719a5fe8 MW |
355 | struct tb_nvm *nvm = priv; |
356 | struct tb_switch *sw = tb_to_switch(nvm->dev); | |
357 | int ret; | |
e6b245cc | 358 | |
09f11b6c MW |
359 | if (!mutex_trylock(&sw->tb->lock)) |
360 | return restart_syscall(); | |
e6b245cc MW |
361 | |
362 | /* | |
363 | * Since writing the NVM image might require some special steps, | |
364 | * for example when CSS headers are written, we cache the image | |
365 | * locally here and handle the special cases when the user asks | |
366 | * us to authenticate the image. | |
367 | */ | |
719a5fe8 | 368 | ret = tb_nvm_write_buf(nvm, offset, val, bytes); |
09f11b6c | 369 | mutex_unlock(&sw->tb->lock); |
e6b245cc MW |
370 | |
371 | return ret; | |
372 | } | |
373 | ||
e6b245cc MW |
374 | static int tb_switch_nvm_add(struct tb_switch *sw) |
375 | { | |
719a5fe8 | 376 | struct tb_nvm *nvm; |
e6b245cc MW |
377 | u32 val; |
378 | int ret; | |
379 | ||
b0407983 | 380 | if (!nvm_readable(sw)) |
e6b245cc MW |
381 | return 0; |
382 | ||
b0407983 MW |
383 | /* |
384 | * The NVM format of non-Intel hardware is not known so | |
385 | * currently restrict NVM upgrade for Intel hardware. We may | |
386 | * relax this in the future when we learn other NVM formats. | |
387 | */ | |
83d17036 MW |
388 | if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL && |
389 | sw->config.vendor_id != 0x8087) { | |
b0407983 MW |
390 | dev_info(&sw->dev, |
391 | "NVM format of vendor %#x is not known, disabling NVM upgrade\n", | |
392 | sw->config.vendor_id); | |
393 | return 0; | |
394 | } | |
395 | ||
719a5fe8 MW |
396 | nvm = tb_nvm_alloc(&sw->dev); |
397 | if (IS_ERR(nvm)) | |
398 | return PTR_ERR(nvm); | |
e6b245cc MW |
399 | |
400 | /* | |
401 | * If the switch is in safe-mode the only accessible portion of | |
402 | * the NVM is the non-active one where userspace is expected to | |
403 | * write new functional NVM. | |
404 | */ | |
405 | if (!sw->safe_mode) { | |
406 | u32 nvm_size, hdr_size; | |
407 | ||
b0407983 | 408 | ret = nvm_read(sw, NVM_FLASH_SIZE, &val, sizeof(val)); |
e6b245cc | 409 | if (ret) |
719a5fe8 | 410 | goto err_nvm; |
e6b245cc MW |
411 | |
412 | hdr_size = sw->generation < 3 ? SZ_8K : SZ_16K; | |
413 | nvm_size = (SZ_1M << (val & 7)) / 8; | |
414 | nvm_size = (nvm_size - hdr_size) / 2; | |
415 | ||
b0407983 | 416 | ret = nvm_read(sw, NVM_VERSION, &val, sizeof(val)); |
e6b245cc | 417 | if (ret) |
719a5fe8 | 418 | goto err_nvm; |
e6b245cc MW |
419 | |
420 | nvm->major = val >> 16; | |
421 | nvm->minor = val >> 8; | |
422 | ||
719a5fe8 MW |
423 | ret = tb_nvm_add_active(nvm, nvm_size, tb_switch_nvm_read); |
424 | if (ret) | |
425 | goto err_nvm; | |
e6b245cc MW |
426 | } |
427 | ||
3f415e5e | 428 | if (!sw->no_nvm_upgrade) { |
719a5fe8 MW |
429 | ret = tb_nvm_add_non_active(nvm, NVM_MAX_SIZE, |
430 | tb_switch_nvm_write); | |
431 | if (ret) | |
432 | goto err_nvm; | |
e6b245cc | 433 | } |
e6b245cc | 434 | |
e6b245cc | 435 | sw->nvm = nvm; |
e6b245cc MW |
436 | return 0; |
437 | ||
719a5fe8 MW |
438 | err_nvm: |
439 | tb_nvm_free(nvm); | |
e6b245cc MW |
440 | return ret; |
441 | } | |
442 | ||
443 | static void tb_switch_nvm_remove(struct tb_switch *sw) | |
444 | { | |
719a5fe8 | 445 | struct tb_nvm *nvm; |
e6b245cc | 446 | |
e6b245cc MW |
447 | nvm = sw->nvm; |
448 | sw->nvm = NULL; | |
e6b245cc MW |
449 | |
450 | if (!nvm) | |
451 | return; | |
452 | ||
453 | /* Remove authentication status in case the switch is unplugged */ | |
454 | if (!nvm->authenticating) | |
455 | nvm_clear_auth_status(sw); | |
456 | ||
719a5fe8 | 457 | tb_nvm_free(nvm); |
e6b245cc MW |
458 | } |
459 | ||
a25c8b2f AN |
460 | /* port utility functions */ |
461 | ||
462 | static const char *tb_port_type(struct tb_regs_port_header *port) | |
463 | { | |
464 | switch (port->type >> 16) { | |
465 | case 0: | |
466 | switch ((u8) port->type) { | |
467 | case 0: | |
468 | return "Inactive"; | |
469 | case 1: | |
470 | return "Port"; | |
471 | case 2: | |
472 | return "NHI"; | |
473 | default: | |
474 | return "unknown"; | |
475 | } | |
476 | case 0x2: | |
477 | return "Ethernet"; | |
478 | case 0x8: | |
479 | return "SATA"; | |
480 | case 0xe: | |
481 | return "DP/HDMI"; | |
482 | case 0x10: | |
483 | return "PCIe"; | |
484 | case 0x20: | |
485 | return "USB"; | |
486 | default: | |
487 | return "unknown"; | |
488 | } | |
489 | } | |
490 | ||
491 | static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port) | |
492 | { | |
daa5140f MW |
493 | tb_dbg(tb, |
494 | " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n", | |
495 | port->port_number, port->vendor_id, port->device_id, | |
496 | port->revision, port->thunderbolt_version, tb_port_type(port), | |
497 | port->type); | |
498 | tb_dbg(tb, " Max hop id (in/out): %d/%d\n", | |
499 | port->max_in_hop_id, port->max_out_hop_id); | |
500 | tb_dbg(tb, " Max counters: %d\n", port->max_counters); | |
501 | tb_dbg(tb, " NFC Credits: %#x\n", port->nfc_credits); | |
a25c8b2f AN |
502 | } |
503 | ||
9da672a4 AN |
504 | /** |
505 | * tb_port_state() - get connectedness state of a port | |
506 | * | |
507 | * The port must have a TB_CAP_PHY (i.e. it should be a real port). | |
508 | * | |
509 | * Return: Returns an enum tb_port_state on success or an error code on failure. | |
510 | */ | |
511 | static int tb_port_state(struct tb_port *port) | |
512 | { | |
513 | struct tb_cap_phy phy; | |
514 | int res; | |
515 | if (port->cap_phy == 0) { | |
516 | tb_port_WARN(port, "does not have a PHY\n"); | |
517 | return -EINVAL; | |
518 | } | |
519 | res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2); | |
520 | if (res) | |
521 | return res; | |
522 | return phy.state; | |
523 | } | |
524 | ||
525 | /** | |
526 | * tb_wait_for_port() - wait for a port to become ready | |
527 | * | |
528 | * Wait up to 1 second for a port to reach state TB_PORT_UP. If | |
529 | * wait_if_unplugged is set then we also wait if the port is in state | |
530 | * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after | |
531 | * switch resume). Otherwise we only wait if a device is registered but the link | |
532 | * has not yet been established. | |
533 | * | |
534 | * Return: Returns an error code on failure. Returns 0 if the port is not | |
535 | * connected or failed to reach state TB_PORT_UP within one second. Returns 1 | |
536 | * if the port is connected and in state TB_PORT_UP. | |
537 | */ | |
538 | int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged) | |
539 | { | |
540 | int retries = 10; | |
541 | int state; | |
542 | if (!port->cap_phy) { | |
543 | tb_port_WARN(port, "does not have PHY\n"); | |
544 | return -EINVAL; | |
545 | } | |
546 | if (tb_is_upstream_port(port)) { | |
547 | tb_port_WARN(port, "is the upstream port\n"); | |
548 | return -EINVAL; | |
549 | } | |
550 | ||
551 | while (retries--) { | |
552 | state = tb_port_state(port); | |
553 | if (state < 0) | |
554 | return state; | |
555 | if (state == TB_PORT_DISABLED) { | |
62efe699 | 556 | tb_port_dbg(port, "is disabled (state: 0)\n"); |
9da672a4 AN |
557 | return 0; |
558 | } | |
559 | if (state == TB_PORT_UNPLUGGED) { | |
560 | if (wait_if_unplugged) { | |
561 | /* used during resume */ | |
62efe699 MW |
562 | tb_port_dbg(port, |
563 | "is unplugged (state: 7), retrying...\n"); | |
9da672a4 AN |
564 | msleep(100); |
565 | continue; | |
566 | } | |
62efe699 | 567 | tb_port_dbg(port, "is unplugged (state: 7)\n"); |
9da672a4 AN |
568 | return 0; |
569 | } | |
570 | if (state == TB_PORT_UP) { | |
62efe699 | 571 | tb_port_dbg(port, "is connected, link is up (state: 2)\n"); |
9da672a4 AN |
572 | return 1; |
573 | } | |
574 | ||
575 | /* | |
576 | * After plug-in the state is TB_PORT_CONNECTING. Give it some | |
577 | * time. | |
578 | */ | |
62efe699 MW |
579 | tb_port_dbg(port, |
580 | "is connected, link is not up (state: %d), retrying...\n", | |
581 | state); | |
9da672a4 AN |
582 | msleep(100); |
583 | } | |
584 | tb_port_warn(port, | |
585 | "failed to reach state TB_PORT_UP. Ignoring port...\n"); | |
586 | return 0; | |
587 | } | |
588 | ||
520b6702 AN |
589 | /** |
590 | * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port | |
591 | * | |
592 | * Change the number of NFC credits allocated to @port by @credits. To remove | |
593 | * NFC credits pass a negative amount of credits. | |
594 | * | |
595 | * Return: Returns 0 on success or an error code on failure. | |
596 | */ | |
597 | int tb_port_add_nfc_credits(struct tb_port *port, int credits) | |
598 | { | |
c5ee6feb MW |
599 | u32 nfc_credits; |
600 | ||
601 | if (credits == 0 || port->sw->is_unplugged) | |
520b6702 | 602 | return 0; |
c5ee6feb | 603 | |
edfbd68b MW |
604 | /* |
605 | * USB4 restricts programming NFC buffers to lane adapters only | |
606 | * so skip other ports. | |
607 | */ | |
608 | if (tb_switch_is_usb4(port->sw) && !tb_port_is_null(port)) | |
609 | return 0; | |
610 | ||
8f57d478 | 611 | nfc_credits = port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK; |
c5ee6feb MW |
612 | nfc_credits += credits; |
613 | ||
8f57d478 MW |
614 | tb_port_dbg(port, "adding %d NFC credits to %lu", credits, |
615 | port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK); | |
c5ee6feb | 616 | |
8f57d478 | 617 | port->config.nfc_credits &= ~ADP_CS_4_NFC_BUFFERS_MASK; |
c5ee6feb MW |
618 | port->config.nfc_credits |= nfc_credits; |
619 | ||
520b6702 | 620 | return tb_port_write(port, &port->config.nfc_credits, |
8f57d478 | 621 | TB_CFG_PORT, ADP_CS_4, 1); |
520b6702 AN |
622 | } |
623 | ||
44242d6c MW |
624 | /** |
625 | * tb_port_set_initial_credits() - Set initial port link credits allocated | |
626 | * @port: Port to set the initial credits | |
627 | * @credits: Number of credits to to allocate | |
628 | * | |
629 | * Set initial credits value to be used for ingress shared buffering. | |
630 | */ | |
631 | int tb_port_set_initial_credits(struct tb_port *port, u32 credits) | |
632 | { | |
633 | u32 data; | |
634 | int ret; | |
635 | ||
8f57d478 | 636 | ret = tb_port_read(port, &data, TB_CFG_PORT, ADP_CS_5, 1); |
44242d6c MW |
637 | if (ret) |
638 | return ret; | |
639 | ||
8f57d478 MW |
640 | data &= ~ADP_CS_5_LCA_MASK; |
641 | data |= (credits << ADP_CS_5_LCA_SHIFT) & ADP_CS_5_LCA_MASK; | |
44242d6c | 642 | |
8f57d478 | 643 | return tb_port_write(port, &data, TB_CFG_PORT, ADP_CS_5, 1); |
44242d6c MW |
644 | } |
645 | ||
520b6702 AN |
646 | /** |
647 | * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER | |
648 | * | |
649 | * Return: Returns 0 on success or an error code on failure. | |
650 | */ | |
651 | int tb_port_clear_counter(struct tb_port *port, int counter) | |
652 | { | |
653 | u32 zero[3] = { 0, 0, 0 }; | |
62efe699 | 654 | tb_port_dbg(port, "clearing counter %d\n", counter); |
520b6702 AN |
655 | return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3); |
656 | } | |
657 | ||
b0407983 MW |
658 | /** |
659 | * tb_port_unlock() - Unlock downstream port | |
660 | * @port: Port to unlock | |
661 | * | |
662 | * Needed for USB4 but can be called for any CIO/USB4 ports. Makes the | |
663 | * downstream router accessible for CM. | |
664 | */ | |
665 | int tb_port_unlock(struct tb_port *port) | |
666 | { | |
667 | if (tb_switch_is_icm(port->sw)) | |
668 | return 0; | |
669 | if (!tb_port_is_null(port)) | |
670 | return -EINVAL; | |
671 | if (tb_switch_is_usb4(port->sw)) | |
672 | return usb4_port_unlock(port); | |
673 | return 0; | |
674 | } | |
675 | ||
341d4518 MW |
676 | static int __tb_port_enable(struct tb_port *port, bool enable) |
677 | { | |
678 | int ret; | |
679 | u32 phy; | |
680 | ||
681 | if (!tb_port_is_null(port)) | |
682 | return -EINVAL; | |
683 | ||
684 | ret = tb_port_read(port, &phy, TB_CFG_PORT, | |
685 | port->cap_phy + LANE_ADP_CS_1, 1); | |
686 | if (ret) | |
687 | return ret; | |
688 | ||
689 | if (enable) | |
690 | phy &= ~LANE_ADP_CS_1_LD; | |
691 | else | |
692 | phy |= LANE_ADP_CS_1_LD; | |
693 | ||
694 | return tb_port_write(port, &phy, TB_CFG_PORT, | |
695 | port->cap_phy + LANE_ADP_CS_1, 1); | |
696 | } | |
697 | ||
698 | /** | |
699 | * tb_port_enable() - Enable lane adapter | |
700 | * @port: Port to enable (can be %NULL) | |
701 | * | |
702 | * This is used for lane 0 and 1 adapters to enable it. | |
703 | */ | |
704 | int tb_port_enable(struct tb_port *port) | |
705 | { | |
706 | return __tb_port_enable(port, true); | |
707 | } | |
708 | ||
709 | /** | |
710 | * tb_port_disable() - Disable lane adapter | |
711 | * @port: Port to disable (can be %NULL) | |
712 | * | |
713 | * This is used for lane 0 and 1 adapters to disable it. | |
714 | */ | |
715 | int tb_port_disable(struct tb_port *port) | |
716 | { | |
717 | return __tb_port_enable(port, false); | |
718 | } | |
719 | ||
a25c8b2f AN |
720 | /** |
721 | * tb_init_port() - initialize a port | |
722 | * | |
723 | * This is a helper method for tb_switch_alloc. Does not check or initialize | |
724 | * any downstream switches. | |
725 | * | |
726 | * Return: Returns 0 on success or an error code on failure. | |
727 | */ | |
343fcb8c | 728 | static int tb_init_port(struct tb_port *port) |
a25c8b2f AN |
729 | { |
730 | int res; | |
9da672a4 | 731 | int cap; |
343fcb8c | 732 | |
a25c8b2f | 733 | res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8); |
d94dcbb1 MW |
734 | if (res) { |
735 | if (res == -ENODEV) { | |
736 | tb_dbg(port->sw->tb, " Port %d: not implemented\n", | |
737 | port->port); | |
8824d19b | 738 | port->disabled = true; |
d94dcbb1 MW |
739 | return 0; |
740 | } | |
a25c8b2f | 741 | return res; |
d94dcbb1 | 742 | } |
a25c8b2f | 743 | |
9da672a4 | 744 | /* Port 0 is the switch itself and has no PHY. */ |
343fcb8c | 745 | if (port->config.type == TB_TYPE_PORT && port->port != 0) { |
da2da04b | 746 | cap = tb_port_find_cap(port, TB_PORT_CAP_PHY); |
9da672a4 AN |
747 | |
748 | if (cap > 0) | |
749 | port->cap_phy = cap; | |
750 | else | |
751 | tb_port_WARN(port, "non switch port without a PHY\n"); | |
b0407983 MW |
752 | |
753 | cap = tb_port_find_cap(port, TB_PORT_CAP_USB4); | |
754 | if (cap > 0) | |
755 | port->cap_usb4 = cap; | |
56183c88 MW |
756 | } else if (port->port != 0) { |
757 | cap = tb_port_find_cap(port, TB_PORT_CAP_ADAP); | |
758 | if (cap > 0) | |
759 | port->cap_adap = cap; | |
9da672a4 AN |
760 | } |
761 | ||
343fcb8c | 762 | tb_dump_port(port->sw->tb, &port->config); |
a25c8b2f | 763 | |
0b2863ac MW |
764 | /* Control port does not need HopID allocation */ |
765 | if (port->port) { | |
766 | ida_init(&port->in_hopids); | |
767 | ida_init(&port->out_hopids); | |
768 | } | |
769 | ||
8afe909b | 770 | INIT_LIST_HEAD(&port->list); |
a25c8b2f AN |
771 | return 0; |
772 | ||
773 | } | |
774 | ||
0b2863ac MW |
775 | static int tb_port_alloc_hopid(struct tb_port *port, bool in, int min_hopid, |
776 | int max_hopid) | |
777 | { | |
778 | int port_max_hopid; | |
779 | struct ida *ida; | |
780 | ||
781 | if (in) { | |
782 | port_max_hopid = port->config.max_in_hop_id; | |
783 | ida = &port->in_hopids; | |
784 | } else { | |
785 | port_max_hopid = port->config.max_out_hop_id; | |
786 | ida = &port->out_hopids; | |
787 | } | |
788 | ||
12676423 MW |
789 | /* |
790 | * NHI can use HopIDs 1-max for other adapters HopIDs 0-7 are | |
791 | * reserved. | |
792 | */ | |
a3cfebdc | 793 | if (!tb_port_is_nhi(port) && min_hopid < TB_PATH_MIN_HOPID) |
0b2863ac MW |
794 | min_hopid = TB_PATH_MIN_HOPID; |
795 | ||
796 | if (max_hopid < 0 || max_hopid > port_max_hopid) | |
797 | max_hopid = port_max_hopid; | |
798 | ||
799 | return ida_simple_get(ida, min_hopid, max_hopid + 1, GFP_KERNEL); | |
800 | } | |
801 | ||
802 | /** | |
803 | * tb_port_alloc_in_hopid() - Allocate input HopID from port | |
804 | * @port: Port to allocate HopID for | |
805 | * @min_hopid: Minimum acceptable input HopID | |
806 | * @max_hopid: Maximum acceptable input HopID | |
807 | * | |
808 | * Return: HopID between @min_hopid and @max_hopid or negative errno in | |
809 | * case of error. | |
810 | */ | |
811 | int tb_port_alloc_in_hopid(struct tb_port *port, int min_hopid, int max_hopid) | |
812 | { | |
813 | return tb_port_alloc_hopid(port, true, min_hopid, max_hopid); | |
814 | } | |
815 | ||
816 | /** | |
817 | * tb_port_alloc_out_hopid() - Allocate output HopID from port | |
818 | * @port: Port to allocate HopID for | |
819 | * @min_hopid: Minimum acceptable output HopID | |
820 | * @max_hopid: Maximum acceptable output HopID | |
821 | * | |
822 | * Return: HopID between @min_hopid and @max_hopid or negative errno in | |
823 | * case of error. | |
824 | */ | |
825 | int tb_port_alloc_out_hopid(struct tb_port *port, int min_hopid, int max_hopid) | |
826 | { | |
827 | return tb_port_alloc_hopid(port, false, min_hopid, max_hopid); | |
828 | } | |
829 | ||
830 | /** | |
831 | * tb_port_release_in_hopid() - Release allocated input HopID from port | |
832 | * @port: Port whose HopID to release | |
833 | * @hopid: HopID to release | |
834 | */ | |
835 | void tb_port_release_in_hopid(struct tb_port *port, int hopid) | |
836 | { | |
837 | ida_simple_remove(&port->in_hopids, hopid); | |
838 | } | |
839 | ||
840 | /** | |
841 | * tb_port_release_out_hopid() - Release allocated output HopID from port | |
842 | * @port: Port whose HopID to release | |
843 | * @hopid: HopID to release | |
844 | */ | |
845 | void tb_port_release_out_hopid(struct tb_port *port, int hopid) | |
846 | { | |
847 | ida_simple_remove(&port->out_hopids, hopid); | |
848 | } | |
849 | ||
69eb79f7 MW |
850 | static inline bool tb_switch_is_reachable(const struct tb_switch *parent, |
851 | const struct tb_switch *sw) | |
852 | { | |
853 | u64 mask = (1ULL << parent->config.depth * 8) - 1; | |
854 | return (tb_route(parent) & mask) == (tb_route(sw) & mask); | |
855 | } | |
856 | ||
fb19fac1 MW |
857 | /** |
858 | * tb_next_port_on_path() - Return next port for given port on a path | |
859 | * @start: Start port of the walk | |
860 | * @end: End port of the walk | |
861 | * @prev: Previous port (%NULL if this is the first) | |
862 | * | |
863 | * This function can be used to walk from one port to another if they | |
864 | * are connected through zero or more switches. If the @prev is dual | |
865 | * link port, the function follows that link and returns another end on | |
866 | * that same link. | |
867 | * | |
868 | * If the @end port has been reached, return %NULL. | |
869 | * | |
870 | * Domain tb->lock must be held when this function is called. | |
871 | */ | |
872 | struct tb_port *tb_next_port_on_path(struct tb_port *start, struct tb_port *end, | |
873 | struct tb_port *prev) | |
874 | { | |
875 | struct tb_port *next; | |
876 | ||
877 | if (!prev) | |
878 | return start; | |
879 | ||
880 | if (prev->sw == end->sw) { | |
881 | if (prev == end) | |
882 | return NULL; | |
883 | return end; | |
884 | } | |
885 | ||
69eb79f7 MW |
886 | if (tb_switch_is_reachable(prev->sw, end->sw)) { |
887 | next = tb_port_at(tb_route(end->sw), prev->sw); | |
888 | /* Walk down the topology if next == prev */ | |
fb19fac1 | 889 | if (prev->remote && |
69eb79f7 | 890 | (next == prev || next->dual_link_port == prev)) |
fb19fac1 | 891 | next = prev->remote; |
fb19fac1 MW |
892 | } else { |
893 | if (tb_is_upstream_port(prev)) { | |
894 | next = prev->remote; | |
895 | } else { | |
896 | next = tb_upstream_port(prev->sw); | |
897 | /* | |
898 | * Keep the same link if prev and next are both | |
899 | * dual link ports. | |
900 | */ | |
901 | if (next->dual_link_port && | |
902 | next->link_nr != prev->link_nr) { | |
903 | next = next->dual_link_port; | |
904 | } | |
905 | } | |
906 | } | |
907 | ||
69eb79f7 | 908 | return next != prev ? next : NULL; |
fb19fac1 MW |
909 | } |
910 | ||
5b7b8c0a MW |
911 | /** |
912 | * tb_port_get_link_speed() - Get current link speed | |
913 | * @port: Port to check (USB4 or CIO) | |
914 | * | |
915 | * Returns link speed in Gb/s or negative errno in case of failure. | |
916 | */ | |
917 | int tb_port_get_link_speed(struct tb_port *port) | |
91c0c120 MW |
918 | { |
919 | u32 val, speed; | |
920 | int ret; | |
921 | ||
922 | if (!port->cap_phy) | |
923 | return -EINVAL; | |
924 | ||
925 | ret = tb_port_read(port, &val, TB_CFG_PORT, | |
926 | port->cap_phy + LANE_ADP_CS_1, 1); | |
927 | if (ret) | |
928 | return ret; | |
929 | ||
930 | speed = (val & LANE_ADP_CS_1_CURRENT_SPEED_MASK) >> | |
931 | LANE_ADP_CS_1_CURRENT_SPEED_SHIFT; | |
932 | return speed == LANE_ADP_CS_1_CURRENT_SPEED_GEN3 ? 20 : 10; | |
933 | } | |
934 | ||
4210d50f IH |
935 | /** |
936 | * tb_port_get_link_width() - Get current link width | |
937 | * @port: Port to check (USB4 or CIO) | |
938 | * | |
939 | * Returns link width. Return values can be 1 (Single-Lane), 2 (Dual-Lane) | |
940 | * or negative errno in case of failure. | |
941 | */ | |
942 | int tb_port_get_link_width(struct tb_port *port) | |
91c0c120 MW |
943 | { |
944 | u32 val; | |
945 | int ret; | |
946 | ||
947 | if (!port->cap_phy) | |
948 | return -EINVAL; | |
949 | ||
950 | ret = tb_port_read(port, &val, TB_CFG_PORT, | |
951 | port->cap_phy + LANE_ADP_CS_1, 1); | |
952 | if (ret) | |
953 | return ret; | |
954 | ||
955 | return (val & LANE_ADP_CS_1_CURRENT_WIDTH_MASK) >> | |
956 | LANE_ADP_CS_1_CURRENT_WIDTH_SHIFT; | |
957 | } | |
958 | ||
959 | static bool tb_port_is_width_supported(struct tb_port *port, int width) | |
960 | { | |
961 | u32 phy, widths; | |
962 | int ret; | |
963 | ||
964 | if (!port->cap_phy) | |
965 | return false; | |
966 | ||
967 | ret = tb_port_read(port, &phy, TB_CFG_PORT, | |
968 | port->cap_phy + LANE_ADP_CS_0, 1); | |
969 | if (ret) | |
e9d0e751 | 970 | return false; |
91c0c120 MW |
971 | |
972 | widths = (phy & LANE_ADP_CS_0_SUPPORTED_WIDTH_MASK) >> | |
973 | LANE_ADP_CS_0_SUPPORTED_WIDTH_SHIFT; | |
974 | ||
975 | return !!(widths & width); | |
976 | } | |
977 | ||
978 | static int tb_port_set_link_width(struct tb_port *port, unsigned int width) | |
979 | { | |
980 | u32 val; | |
981 | int ret; | |
982 | ||
983 | if (!port->cap_phy) | |
984 | return -EINVAL; | |
985 | ||
986 | ret = tb_port_read(port, &val, TB_CFG_PORT, | |
987 | port->cap_phy + LANE_ADP_CS_1, 1); | |
988 | if (ret) | |
989 | return ret; | |
990 | ||
991 | val &= ~LANE_ADP_CS_1_TARGET_WIDTH_MASK; | |
992 | switch (width) { | |
993 | case 1: | |
994 | val |= LANE_ADP_CS_1_TARGET_WIDTH_SINGLE << | |
995 | LANE_ADP_CS_1_TARGET_WIDTH_SHIFT; | |
996 | break; | |
997 | case 2: | |
998 | val |= LANE_ADP_CS_1_TARGET_WIDTH_DUAL << | |
999 | LANE_ADP_CS_1_TARGET_WIDTH_SHIFT; | |
1000 | break; | |
1001 | default: | |
1002 | return -EINVAL; | |
1003 | } | |
1004 | ||
1005 | val |= LANE_ADP_CS_1_LB; | |
1006 | ||
1007 | return tb_port_write(port, &val, TB_CFG_PORT, | |
1008 | port->cap_phy + LANE_ADP_CS_1, 1); | |
1009 | } | |
1010 | ||
1011 | static int tb_port_lane_bonding_enable(struct tb_port *port) | |
1012 | { | |
1013 | int ret; | |
1014 | ||
1015 | /* | |
1016 | * Enable lane bonding for both links if not already enabled by | |
1017 | * for example the boot firmware. | |
1018 | */ | |
1019 | ret = tb_port_get_link_width(port); | |
1020 | if (ret == 1) { | |
1021 | ret = tb_port_set_link_width(port, 2); | |
1022 | if (ret) | |
1023 | return ret; | |
1024 | } | |
1025 | ||
1026 | ret = tb_port_get_link_width(port->dual_link_port); | |
1027 | if (ret == 1) { | |
1028 | ret = tb_port_set_link_width(port->dual_link_port, 2); | |
1029 | if (ret) { | |
1030 | tb_port_set_link_width(port, 1); | |
1031 | return ret; | |
1032 | } | |
1033 | } | |
1034 | ||
1035 | port->bonded = true; | |
1036 | port->dual_link_port->bonded = true; | |
1037 | ||
1038 | return 0; | |
1039 | } | |
1040 | ||
1041 | static void tb_port_lane_bonding_disable(struct tb_port *port) | |
1042 | { | |
1043 | port->dual_link_port->bonded = false; | |
1044 | port->bonded = false; | |
1045 | ||
1046 | tb_port_set_link_width(port->dual_link_port, 1); | |
1047 | tb_port_set_link_width(port, 1); | |
1048 | } | |
1049 | ||
e78db6f0 MW |
1050 | /** |
1051 | * tb_port_is_enabled() - Is the adapter port enabled | |
1052 | * @port: Port to check | |
1053 | */ | |
1054 | bool tb_port_is_enabled(struct tb_port *port) | |
1055 | { | |
1056 | switch (port->config.type) { | |
1057 | case TB_TYPE_PCIE_UP: | |
1058 | case TB_TYPE_PCIE_DOWN: | |
1059 | return tb_pci_port_is_enabled(port); | |
1060 | ||
4f807e47 MW |
1061 | case TB_TYPE_DP_HDMI_IN: |
1062 | case TB_TYPE_DP_HDMI_OUT: | |
1063 | return tb_dp_port_is_enabled(port); | |
1064 | ||
e6f81858 RM |
1065 | case TB_TYPE_USB3_UP: |
1066 | case TB_TYPE_USB3_DOWN: | |
1067 | return tb_usb3_port_is_enabled(port); | |
1068 | ||
e78db6f0 MW |
1069 | default: |
1070 | return false; | |
1071 | } | |
1072 | } | |
1073 | ||
e6f81858 RM |
1074 | /** |
1075 | * tb_usb3_port_is_enabled() - Is the USB3 adapter port enabled | |
1076 | * @port: USB3 adapter port to check | |
1077 | */ | |
1078 | bool tb_usb3_port_is_enabled(struct tb_port *port) | |
1079 | { | |
1080 | u32 data; | |
1081 | ||
1082 | if (tb_port_read(port, &data, TB_CFG_PORT, | |
1083 | port->cap_adap + ADP_USB3_CS_0, 1)) | |
1084 | return false; | |
1085 | ||
1086 | return !!(data & ADP_USB3_CS_0_PE); | |
1087 | } | |
1088 | ||
1089 | /** | |
1090 | * tb_usb3_port_enable() - Enable USB3 adapter port | |
1091 | * @port: USB3 adapter port to enable | |
1092 | * @enable: Enable/disable the USB3 adapter | |
1093 | */ | |
1094 | int tb_usb3_port_enable(struct tb_port *port, bool enable) | |
1095 | { | |
1096 | u32 word = enable ? (ADP_USB3_CS_0_PE | ADP_USB3_CS_0_V) | |
1097 | : ADP_USB3_CS_0_V; | |
1098 | ||
1099 | if (!port->cap_adap) | |
1100 | return -ENXIO; | |
1101 | return tb_port_write(port, &word, TB_CFG_PORT, | |
1102 | port->cap_adap + ADP_USB3_CS_0, 1); | |
1103 | } | |
1104 | ||
0414bec5 MW |
1105 | /** |
1106 | * tb_pci_port_is_enabled() - Is the PCIe adapter port enabled | |
1107 | * @port: PCIe port to check | |
1108 | */ | |
1109 | bool tb_pci_port_is_enabled(struct tb_port *port) | |
1110 | { | |
1111 | u32 data; | |
1112 | ||
778bfca3 MW |
1113 | if (tb_port_read(port, &data, TB_CFG_PORT, |
1114 | port->cap_adap + ADP_PCIE_CS_0, 1)) | |
0414bec5 MW |
1115 | return false; |
1116 | ||
778bfca3 | 1117 | return !!(data & ADP_PCIE_CS_0_PE); |
0414bec5 MW |
1118 | } |
1119 | ||
93f36ade MW |
1120 | /** |
1121 | * tb_pci_port_enable() - Enable PCIe adapter port | |
1122 | * @port: PCIe port to enable | |
1123 | * @enable: Enable/disable the PCIe adapter | |
1124 | */ | |
1125 | int tb_pci_port_enable(struct tb_port *port, bool enable) | |
1126 | { | |
778bfca3 | 1127 | u32 word = enable ? ADP_PCIE_CS_0_PE : 0x0; |
93f36ade MW |
1128 | if (!port->cap_adap) |
1129 | return -ENXIO; | |
778bfca3 MW |
1130 | return tb_port_write(port, &word, TB_CFG_PORT, |
1131 | port->cap_adap + ADP_PCIE_CS_0, 1); | |
93f36ade MW |
1132 | } |
1133 | ||
4f807e47 MW |
1134 | /** |
1135 | * tb_dp_port_hpd_is_active() - Is HPD already active | |
1136 | * @port: DP out port to check | |
1137 | * | |
1138 | * Checks if the DP OUT adapter port has HDP bit already set. | |
1139 | */ | |
1140 | int tb_dp_port_hpd_is_active(struct tb_port *port) | |
1141 | { | |
1142 | u32 data; | |
1143 | int ret; | |
1144 | ||
98176380 MW |
1145 | ret = tb_port_read(port, &data, TB_CFG_PORT, |
1146 | port->cap_adap + ADP_DP_CS_2, 1); | |
4f807e47 MW |
1147 | if (ret) |
1148 | return ret; | |
1149 | ||
98176380 | 1150 | return !!(data & ADP_DP_CS_2_HDP); |
4f807e47 MW |
1151 | } |
1152 | ||
1153 | /** | |
1154 | * tb_dp_port_hpd_clear() - Clear HPD from DP IN port | |
1155 | * @port: Port to clear HPD | |
1156 | * | |
1157 | * If the DP IN port has HDP set, this function can be used to clear it. | |
1158 | */ | |
1159 | int tb_dp_port_hpd_clear(struct tb_port *port) | |
1160 | { | |
1161 | u32 data; | |
1162 | int ret; | |
1163 | ||
98176380 MW |
1164 | ret = tb_port_read(port, &data, TB_CFG_PORT, |
1165 | port->cap_adap + ADP_DP_CS_3, 1); | |
4f807e47 MW |
1166 | if (ret) |
1167 | return ret; | |
1168 | ||
98176380 MW |
1169 | data |= ADP_DP_CS_3_HDPC; |
1170 | return tb_port_write(port, &data, TB_CFG_PORT, | |
1171 | port->cap_adap + ADP_DP_CS_3, 1); | |
4f807e47 MW |
1172 | } |
1173 | ||
1174 | /** | |
1175 | * tb_dp_port_set_hops() - Set video/aux Hop IDs for DP port | |
1176 | * @port: DP IN/OUT port to set hops | |
1177 | * @video: Video Hop ID | |
1178 | * @aux_tx: AUX TX Hop ID | |
1179 | * @aux_rx: AUX RX Hop ID | |
1180 | * | |
1181 | * Programs specified Hop IDs for DP IN/OUT port. | |
1182 | */ | |
1183 | int tb_dp_port_set_hops(struct tb_port *port, unsigned int video, | |
1184 | unsigned int aux_tx, unsigned int aux_rx) | |
1185 | { | |
1186 | u32 data[2]; | |
1187 | int ret; | |
1188 | ||
98176380 MW |
1189 | ret = tb_port_read(port, data, TB_CFG_PORT, |
1190 | port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); | |
4f807e47 MW |
1191 | if (ret) |
1192 | return ret; | |
1193 | ||
98176380 MW |
1194 | data[0] &= ~ADP_DP_CS_0_VIDEO_HOPID_MASK; |
1195 | data[1] &= ~ADP_DP_CS_1_AUX_RX_HOPID_MASK; | |
1196 | data[1] &= ~ADP_DP_CS_1_AUX_RX_HOPID_MASK; | |
4f807e47 | 1197 | |
98176380 MW |
1198 | data[0] |= (video << ADP_DP_CS_0_VIDEO_HOPID_SHIFT) & |
1199 | ADP_DP_CS_0_VIDEO_HOPID_MASK; | |
1200 | data[1] |= aux_tx & ADP_DP_CS_1_AUX_TX_HOPID_MASK; | |
1201 | data[1] |= (aux_rx << ADP_DP_CS_1_AUX_RX_HOPID_SHIFT) & | |
1202 | ADP_DP_CS_1_AUX_RX_HOPID_MASK; | |
4f807e47 | 1203 | |
98176380 MW |
1204 | return tb_port_write(port, data, TB_CFG_PORT, |
1205 | port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); | |
4f807e47 MW |
1206 | } |
1207 | ||
1208 | /** | |
1209 | * tb_dp_port_is_enabled() - Is DP adapter port enabled | |
1210 | * @port: DP adapter port to check | |
1211 | */ | |
1212 | bool tb_dp_port_is_enabled(struct tb_port *port) | |
1213 | { | |
fd5c46b7 | 1214 | u32 data[2]; |
4f807e47 | 1215 | |
98176380 | 1216 | if (tb_port_read(port, data, TB_CFG_PORT, port->cap_adap + ADP_DP_CS_0, |
fd5c46b7 | 1217 | ARRAY_SIZE(data))) |
4f807e47 MW |
1218 | return false; |
1219 | ||
98176380 | 1220 | return !!(data[0] & (ADP_DP_CS_0_VE | ADP_DP_CS_0_AE)); |
4f807e47 MW |
1221 | } |
1222 | ||
1223 | /** | |
1224 | * tb_dp_port_enable() - Enables/disables DP paths of a port | |
1225 | * @port: DP IN/OUT port | |
1226 | * @enable: Enable/disable DP path | |
1227 | * | |
1228 | * Once Hop IDs are programmed DP paths can be enabled or disabled by | |
1229 | * calling this function. | |
1230 | */ | |
1231 | int tb_dp_port_enable(struct tb_port *port, bool enable) | |
1232 | { | |
fd5c46b7 | 1233 | u32 data[2]; |
4f807e47 MW |
1234 | int ret; |
1235 | ||
98176380 MW |
1236 | ret = tb_port_read(port, data, TB_CFG_PORT, |
1237 | port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); | |
4f807e47 MW |
1238 | if (ret) |
1239 | return ret; | |
1240 | ||
1241 | if (enable) | |
98176380 | 1242 | data[0] |= ADP_DP_CS_0_VE | ADP_DP_CS_0_AE; |
4f807e47 | 1243 | else |
98176380 | 1244 | data[0] &= ~(ADP_DP_CS_0_VE | ADP_DP_CS_0_AE); |
4f807e47 | 1245 | |
98176380 MW |
1246 | return tb_port_write(port, data, TB_CFG_PORT, |
1247 | port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); | |
4f807e47 MW |
1248 | } |
1249 | ||
a25c8b2f AN |
1250 | /* switch utility functions */ |
1251 | ||
b0407983 MW |
1252 | static const char *tb_switch_generation_name(const struct tb_switch *sw) |
1253 | { | |
1254 | switch (sw->generation) { | |
1255 | case 1: | |
1256 | return "Thunderbolt 1"; | |
1257 | case 2: | |
1258 | return "Thunderbolt 2"; | |
1259 | case 3: | |
1260 | return "Thunderbolt 3"; | |
1261 | case 4: | |
1262 | return "USB4"; | |
1263 | default: | |
1264 | return "Unknown"; | |
1265 | } | |
1266 | } | |
1267 | ||
1268 | static void tb_dump_switch(const struct tb *tb, const struct tb_switch *sw) | |
a25c8b2f | 1269 | { |
b0407983 MW |
1270 | const struct tb_regs_switch_header *regs = &sw->config; |
1271 | ||
1272 | tb_dbg(tb, " %s Switch: %x:%x (Revision: %d, TB Version: %d)\n", | |
1273 | tb_switch_generation_name(sw), regs->vendor_id, regs->device_id, | |
1274 | regs->revision, regs->thunderbolt_version); | |
1275 | tb_dbg(tb, " Max Port Number: %d\n", regs->max_port_number); | |
daa5140f MW |
1276 | tb_dbg(tb, " Config:\n"); |
1277 | tb_dbg(tb, | |
a25c8b2f | 1278 | " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n", |
b0407983 MW |
1279 | regs->upstream_port_number, regs->depth, |
1280 | (((u64) regs->route_hi) << 32) | regs->route_lo, | |
1281 | regs->enabled, regs->plug_events_delay); | |
daa5140f | 1282 | tb_dbg(tb, " unknown1: %#x unknown4: %#x\n", |
b0407983 | 1283 | regs->__unknown1, regs->__unknown4); |
a25c8b2f AN |
1284 | } |
1285 | ||
23dd5bb4 AN |
1286 | /** |
1287 | * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET | |
356b6c4e | 1288 | * @sw: Switch to reset |
23dd5bb4 AN |
1289 | * |
1290 | * Return: Returns 0 on success or an error code on failure. | |
1291 | */ | |
356b6c4e | 1292 | int tb_switch_reset(struct tb_switch *sw) |
23dd5bb4 AN |
1293 | { |
1294 | struct tb_cfg_result res; | |
356b6c4e MW |
1295 | |
1296 | if (sw->generation > 1) | |
1297 | return 0; | |
1298 | ||
1299 | tb_sw_dbg(sw, "resetting switch\n"); | |
1300 | ||
1301 | res.err = tb_sw_write(sw, ((u32 *) &sw->config) + 2, | |
1302 | TB_CFG_SWITCH, 2, 2); | |
23dd5bb4 AN |
1303 | if (res.err) |
1304 | return res.err; | |
356b6c4e | 1305 | res = tb_cfg_reset(sw->tb->ctl, tb_route(sw), TB_CFG_DEFAULT_TIMEOUT); |
23dd5bb4 AN |
1306 | if (res.err > 0) |
1307 | return -EIO; | |
1308 | return res.err; | |
1309 | } | |
1310 | ||
ca389f71 AN |
1311 | /** |
1312 | * tb_plug_events_active() - enable/disable plug events on a switch | |
1313 | * | |
1314 | * Also configures a sane plug_events_delay of 255ms. | |
1315 | * | |
1316 | * Return: Returns 0 on success or an error code on failure. | |
1317 | */ | |
1318 | static int tb_plug_events_active(struct tb_switch *sw, bool active) | |
1319 | { | |
1320 | u32 data; | |
1321 | int res; | |
1322 | ||
5cb6ed31 | 1323 | if (tb_switch_is_icm(sw) || tb_switch_is_usb4(sw)) |
bfe778ac MW |
1324 | return 0; |
1325 | ||
ca389f71 AN |
1326 | sw->config.plug_events_delay = 0xff; |
1327 | res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1); | |
1328 | if (res) | |
1329 | return res; | |
1330 | ||
1331 | res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1); | |
1332 | if (res) | |
1333 | return res; | |
1334 | ||
1335 | if (active) { | |
1336 | data = data & 0xFFFFFF83; | |
1337 | switch (sw->config.device_id) { | |
1d111406 LW |
1338 | case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE: |
1339 | case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE: | |
1340 | case PCI_DEVICE_ID_INTEL_PORT_RIDGE: | |
ca389f71 AN |
1341 | break; |
1342 | default: | |
1343 | data |= 4; | |
1344 | } | |
1345 | } else { | |
1346 | data = data | 0x7c; | |
1347 | } | |
1348 | return tb_sw_write(sw, &data, TB_CFG_SWITCH, | |
1349 | sw->cap_plug_events + 1, 1); | |
1350 | } | |
1351 | ||
f67cf491 MW |
1352 | static ssize_t authorized_show(struct device *dev, |
1353 | struct device_attribute *attr, | |
1354 | char *buf) | |
1355 | { | |
1356 | struct tb_switch *sw = tb_to_switch(dev); | |
1357 | ||
1358 | return sprintf(buf, "%u\n", sw->authorized); | |
1359 | } | |
1360 | ||
1361 | static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val) | |
1362 | { | |
1363 | int ret = -EINVAL; | |
1364 | ||
09f11b6c MW |
1365 | if (!mutex_trylock(&sw->tb->lock)) |
1366 | return restart_syscall(); | |
f67cf491 MW |
1367 | |
1368 | if (sw->authorized) | |
1369 | goto unlock; | |
1370 | ||
1371 | switch (val) { | |
1372 | /* Approve switch */ | |
1373 | case 1: | |
1374 | if (sw->key) | |
1375 | ret = tb_domain_approve_switch_key(sw->tb, sw); | |
1376 | else | |
1377 | ret = tb_domain_approve_switch(sw->tb, sw); | |
1378 | break; | |
1379 | ||
1380 | /* Challenge switch */ | |
1381 | case 2: | |
1382 | if (sw->key) | |
1383 | ret = tb_domain_challenge_switch_key(sw->tb, sw); | |
1384 | break; | |
1385 | ||
1386 | default: | |
1387 | break; | |
1388 | } | |
1389 | ||
1390 | if (!ret) { | |
1391 | sw->authorized = val; | |
1392 | /* Notify status change to the userspace */ | |
1393 | kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE); | |
1394 | } | |
1395 | ||
1396 | unlock: | |
09f11b6c | 1397 | mutex_unlock(&sw->tb->lock); |
f67cf491 MW |
1398 | return ret; |
1399 | } | |
1400 | ||
1401 | static ssize_t authorized_store(struct device *dev, | |
1402 | struct device_attribute *attr, | |
1403 | const char *buf, size_t count) | |
1404 | { | |
1405 | struct tb_switch *sw = tb_to_switch(dev); | |
1406 | unsigned int val; | |
1407 | ssize_t ret; | |
1408 | ||
1409 | ret = kstrtouint(buf, 0, &val); | |
1410 | if (ret) | |
1411 | return ret; | |
1412 | if (val > 2) | |
1413 | return -EINVAL; | |
1414 | ||
4f7c2e0d | 1415 | pm_runtime_get_sync(&sw->dev); |
f67cf491 | 1416 | ret = tb_switch_set_authorized(sw, val); |
4f7c2e0d MW |
1417 | pm_runtime_mark_last_busy(&sw->dev); |
1418 | pm_runtime_put_autosuspend(&sw->dev); | |
f67cf491 MW |
1419 | |
1420 | return ret ? ret : count; | |
1421 | } | |
1422 | static DEVICE_ATTR_RW(authorized); | |
1423 | ||
14862ee3 YB |
1424 | static ssize_t boot_show(struct device *dev, struct device_attribute *attr, |
1425 | char *buf) | |
1426 | { | |
1427 | struct tb_switch *sw = tb_to_switch(dev); | |
1428 | ||
1429 | return sprintf(buf, "%u\n", sw->boot); | |
1430 | } | |
1431 | static DEVICE_ATTR_RO(boot); | |
1432 | ||
bfe778ac MW |
1433 | static ssize_t device_show(struct device *dev, struct device_attribute *attr, |
1434 | char *buf) | |
1435 | { | |
1436 | struct tb_switch *sw = tb_to_switch(dev); | |
ca389f71 | 1437 | |
bfe778ac MW |
1438 | return sprintf(buf, "%#x\n", sw->device); |
1439 | } | |
1440 | static DEVICE_ATTR_RO(device); | |
1441 | ||
72ee3390 MW |
1442 | static ssize_t |
1443 | device_name_show(struct device *dev, struct device_attribute *attr, char *buf) | |
1444 | { | |
1445 | struct tb_switch *sw = tb_to_switch(dev); | |
1446 | ||
1447 | return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : ""); | |
1448 | } | |
1449 | static DEVICE_ATTR_RO(device_name); | |
1450 | ||
b406357c CK |
1451 | static ssize_t |
1452 | generation_show(struct device *dev, struct device_attribute *attr, char *buf) | |
1453 | { | |
1454 | struct tb_switch *sw = tb_to_switch(dev); | |
1455 | ||
1456 | return sprintf(buf, "%u\n", sw->generation); | |
1457 | } | |
1458 | static DEVICE_ATTR_RO(generation); | |
1459 | ||
f67cf491 MW |
1460 | static ssize_t key_show(struct device *dev, struct device_attribute *attr, |
1461 | char *buf) | |
1462 | { | |
1463 | struct tb_switch *sw = tb_to_switch(dev); | |
1464 | ssize_t ret; | |
1465 | ||
09f11b6c MW |
1466 | if (!mutex_trylock(&sw->tb->lock)) |
1467 | return restart_syscall(); | |
f67cf491 MW |
1468 | |
1469 | if (sw->key) | |
1470 | ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key); | |
1471 | else | |
1472 | ret = sprintf(buf, "\n"); | |
1473 | ||
09f11b6c | 1474 | mutex_unlock(&sw->tb->lock); |
f67cf491 MW |
1475 | return ret; |
1476 | } | |
1477 | ||
1478 | static ssize_t key_store(struct device *dev, struct device_attribute *attr, | |
1479 | const char *buf, size_t count) | |
1480 | { | |
1481 | struct tb_switch *sw = tb_to_switch(dev); | |
1482 | u8 key[TB_SWITCH_KEY_SIZE]; | |
1483 | ssize_t ret = count; | |
e545f0d8 | 1484 | bool clear = false; |
f67cf491 | 1485 | |
e545f0d8 BY |
1486 | if (!strcmp(buf, "\n")) |
1487 | clear = true; | |
1488 | else if (hex2bin(key, buf, sizeof(key))) | |
f67cf491 MW |
1489 | return -EINVAL; |
1490 | ||
09f11b6c MW |
1491 | if (!mutex_trylock(&sw->tb->lock)) |
1492 | return restart_syscall(); | |
f67cf491 MW |
1493 | |
1494 | if (sw->authorized) { | |
1495 | ret = -EBUSY; | |
1496 | } else { | |
1497 | kfree(sw->key); | |
e545f0d8 BY |
1498 | if (clear) { |
1499 | sw->key = NULL; | |
1500 | } else { | |
1501 | sw->key = kmemdup(key, sizeof(key), GFP_KERNEL); | |
1502 | if (!sw->key) | |
1503 | ret = -ENOMEM; | |
1504 | } | |
f67cf491 MW |
1505 | } |
1506 | ||
09f11b6c | 1507 | mutex_unlock(&sw->tb->lock); |
f67cf491 MW |
1508 | return ret; |
1509 | } | |
0956e411 | 1510 | static DEVICE_ATTR(key, 0600, key_show, key_store); |
f67cf491 | 1511 | |
91c0c120 MW |
1512 | static ssize_t speed_show(struct device *dev, struct device_attribute *attr, |
1513 | char *buf) | |
1514 | { | |
1515 | struct tb_switch *sw = tb_to_switch(dev); | |
1516 | ||
1517 | return sprintf(buf, "%u.0 Gb/s\n", sw->link_speed); | |
1518 | } | |
1519 | ||
1520 | /* | |
1521 | * Currently all lanes must run at the same speed but we expose here | |
1522 | * both directions to allow possible asymmetric links in the future. | |
1523 | */ | |
1524 | static DEVICE_ATTR(rx_speed, 0444, speed_show, NULL); | |
1525 | static DEVICE_ATTR(tx_speed, 0444, speed_show, NULL); | |
1526 | ||
1527 | static ssize_t lanes_show(struct device *dev, struct device_attribute *attr, | |
1528 | char *buf) | |
1529 | { | |
1530 | struct tb_switch *sw = tb_to_switch(dev); | |
1531 | ||
1532 | return sprintf(buf, "%u\n", sw->link_width); | |
1533 | } | |
1534 | ||
1535 | /* | |
1536 | * Currently link has same amount of lanes both directions (1 or 2) but | |
1537 | * expose them separately to allow possible asymmetric links in the future. | |
1538 | */ | |
1539 | static DEVICE_ATTR(rx_lanes, 0444, lanes_show, NULL); | |
1540 | static DEVICE_ATTR(tx_lanes, 0444, lanes_show, NULL); | |
1541 | ||
e6b245cc MW |
1542 | static ssize_t nvm_authenticate_show(struct device *dev, |
1543 | struct device_attribute *attr, char *buf) | |
1544 | { | |
1545 | struct tb_switch *sw = tb_to_switch(dev); | |
1546 | u32 status; | |
1547 | ||
1548 | nvm_get_auth_status(sw, &status); | |
1549 | return sprintf(buf, "%#x\n", status); | |
1550 | } | |
1551 | ||
1cb36293 ML |
1552 | static ssize_t nvm_authenticate_sysfs(struct device *dev, const char *buf, |
1553 | bool disconnect) | |
e6b245cc MW |
1554 | { |
1555 | struct tb_switch *sw = tb_to_switch(dev); | |
4b794f80 | 1556 | int val; |
e6b245cc MW |
1557 | int ret; |
1558 | ||
4f7c2e0d MW |
1559 | pm_runtime_get_sync(&sw->dev); |
1560 | ||
1561 | if (!mutex_trylock(&sw->tb->lock)) { | |
1562 | ret = restart_syscall(); | |
1563 | goto exit_rpm; | |
1564 | } | |
e6b245cc MW |
1565 | |
1566 | /* If NVMem devices are not yet added */ | |
1567 | if (!sw->nvm) { | |
1568 | ret = -EAGAIN; | |
1569 | goto exit_unlock; | |
1570 | } | |
1571 | ||
4b794f80 | 1572 | ret = kstrtoint(buf, 10, &val); |
e6b245cc MW |
1573 | if (ret) |
1574 | goto exit_unlock; | |
1575 | ||
1576 | /* Always clear the authentication status */ | |
1577 | nvm_clear_auth_status(sw); | |
1578 | ||
4b794f80 ML |
1579 | if (val > 0) { |
1580 | if (!sw->nvm->flushed) { | |
1581 | if (!sw->nvm->buf) { | |
1582 | ret = -EINVAL; | |
1583 | goto exit_unlock; | |
1584 | } | |
e6b245cc | 1585 | |
4b794f80 ML |
1586 | ret = nvm_validate_and_write(sw); |
1587 | if (ret || val == WRITE_ONLY) | |
1588 | goto exit_unlock; | |
1589 | } | |
1590 | if (val == WRITE_AND_AUTHENTICATE) { | |
1cb36293 ML |
1591 | if (disconnect) { |
1592 | ret = tb_lc_force_power(sw); | |
1593 | } else { | |
1594 | sw->nvm->authenticating = true; | |
1595 | ret = nvm_authenticate(sw); | |
1596 | } | |
4b794f80 | 1597 | } |
e6b245cc MW |
1598 | } |
1599 | ||
1600 | exit_unlock: | |
09f11b6c | 1601 | mutex_unlock(&sw->tb->lock); |
4f7c2e0d MW |
1602 | exit_rpm: |
1603 | pm_runtime_mark_last_busy(&sw->dev); | |
1604 | pm_runtime_put_autosuspend(&sw->dev); | |
e6b245cc | 1605 | |
1cb36293 ML |
1606 | return ret; |
1607 | } | |
1608 | ||
1609 | static ssize_t nvm_authenticate_store(struct device *dev, | |
1610 | struct device_attribute *attr, const char *buf, size_t count) | |
1611 | { | |
1612 | int ret = nvm_authenticate_sysfs(dev, buf, false); | |
e6b245cc MW |
1613 | if (ret) |
1614 | return ret; | |
1615 | return count; | |
1616 | } | |
1617 | static DEVICE_ATTR_RW(nvm_authenticate); | |
1618 | ||
1cb36293 ML |
1619 | static ssize_t nvm_authenticate_on_disconnect_show(struct device *dev, |
1620 | struct device_attribute *attr, char *buf) | |
1621 | { | |
1622 | return nvm_authenticate_show(dev, attr, buf); | |
1623 | } | |
1624 | ||
1625 | static ssize_t nvm_authenticate_on_disconnect_store(struct device *dev, | |
1626 | struct device_attribute *attr, const char *buf, size_t count) | |
1627 | { | |
1628 | int ret; | |
1629 | ||
1630 | ret = nvm_authenticate_sysfs(dev, buf, true); | |
1631 | return ret ? ret : count; | |
1632 | } | |
1633 | static DEVICE_ATTR_RW(nvm_authenticate_on_disconnect); | |
1634 | ||
e6b245cc MW |
1635 | static ssize_t nvm_version_show(struct device *dev, |
1636 | struct device_attribute *attr, char *buf) | |
1637 | { | |
1638 | struct tb_switch *sw = tb_to_switch(dev); | |
1639 | int ret; | |
1640 | ||
09f11b6c MW |
1641 | if (!mutex_trylock(&sw->tb->lock)) |
1642 | return restart_syscall(); | |
e6b245cc MW |
1643 | |
1644 | if (sw->safe_mode) | |
1645 | ret = -ENODATA; | |
1646 | else if (!sw->nvm) | |
1647 | ret = -EAGAIN; | |
1648 | else | |
1649 | ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor); | |
1650 | ||
09f11b6c | 1651 | mutex_unlock(&sw->tb->lock); |
e6b245cc MW |
1652 | |
1653 | return ret; | |
1654 | } | |
1655 | static DEVICE_ATTR_RO(nvm_version); | |
1656 | ||
bfe778ac MW |
1657 | static ssize_t vendor_show(struct device *dev, struct device_attribute *attr, |
1658 | char *buf) | |
a25c8b2f | 1659 | { |
bfe778ac | 1660 | struct tb_switch *sw = tb_to_switch(dev); |
a25c8b2f | 1661 | |
bfe778ac MW |
1662 | return sprintf(buf, "%#x\n", sw->vendor); |
1663 | } | |
1664 | static DEVICE_ATTR_RO(vendor); | |
1665 | ||
72ee3390 MW |
1666 | static ssize_t |
1667 | vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf) | |
1668 | { | |
1669 | struct tb_switch *sw = tb_to_switch(dev); | |
1670 | ||
1671 | return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : ""); | |
1672 | } | |
1673 | static DEVICE_ATTR_RO(vendor_name); | |
1674 | ||
bfe778ac MW |
1675 | static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr, |
1676 | char *buf) | |
1677 | { | |
1678 | struct tb_switch *sw = tb_to_switch(dev); | |
1679 | ||
1680 | return sprintf(buf, "%pUb\n", sw->uuid); | |
1681 | } | |
1682 | static DEVICE_ATTR_RO(unique_id); | |
1683 | ||
1684 | static struct attribute *switch_attrs[] = { | |
f67cf491 | 1685 | &dev_attr_authorized.attr, |
14862ee3 | 1686 | &dev_attr_boot.attr, |
bfe778ac | 1687 | &dev_attr_device.attr, |
72ee3390 | 1688 | &dev_attr_device_name.attr, |
b406357c | 1689 | &dev_attr_generation.attr, |
f67cf491 | 1690 | &dev_attr_key.attr, |
e6b245cc | 1691 | &dev_attr_nvm_authenticate.attr, |
1cb36293 | 1692 | &dev_attr_nvm_authenticate_on_disconnect.attr, |
e6b245cc | 1693 | &dev_attr_nvm_version.attr, |
91c0c120 MW |
1694 | &dev_attr_rx_speed.attr, |
1695 | &dev_attr_rx_lanes.attr, | |
1696 | &dev_attr_tx_speed.attr, | |
1697 | &dev_attr_tx_lanes.attr, | |
bfe778ac | 1698 | &dev_attr_vendor.attr, |
72ee3390 | 1699 | &dev_attr_vendor_name.attr, |
bfe778ac MW |
1700 | &dev_attr_unique_id.attr, |
1701 | NULL, | |
1702 | }; | |
1703 | ||
f67cf491 MW |
1704 | static umode_t switch_attr_is_visible(struct kobject *kobj, |
1705 | struct attribute *attr, int n) | |
1706 | { | |
fff15f23 | 1707 | struct device *dev = kobj_to_dev(kobj); |
f67cf491 MW |
1708 | struct tb_switch *sw = tb_to_switch(dev); |
1709 | ||
58f414fa MW |
1710 | if (attr == &dev_attr_device.attr) { |
1711 | if (!sw->device) | |
1712 | return 0; | |
1713 | } else if (attr == &dev_attr_device_name.attr) { | |
1714 | if (!sw->device_name) | |
1715 | return 0; | |
1716 | } else if (attr == &dev_attr_vendor.attr) { | |
1717 | if (!sw->vendor) | |
1718 | return 0; | |
1719 | } else if (attr == &dev_attr_vendor_name.attr) { | |
1720 | if (!sw->vendor_name) | |
1721 | return 0; | |
1722 | } else if (attr == &dev_attr_key.attr) { | |
f67cf491 MW |
1723 | if (tb_route(sw) && |
1724 | sw->tb->security_level == TB_SECURITY_SECURE && | |
1725 | sw->security_level == TB_SECURITY_SECURE) | |
1726 | return attr->mode; | |
1727 | return 0; | |
91c0c120 MW |
1728 | } else if (attr == &dev_attr_rx_speed.attr || |
1729 | attr == &dev_attr_rx_lanes.attr || | |
1730 | attr == &dev_attr_tx_speed.attr || | |
1731 | attr == &dev_attr_tx_lanes.attr) { | |
1732 | if (tb_route(sw)) | |
1733 | return attr->mode; | |
1734 | return 0; | |
3f415e5e | 1735 | } else if (attr == &dev_attr_nvm_authenticate.attr) { |
b0407983 | 1736 | if (nvm_upgradeable(sw)) |
3f415e5e MW |
1737 | return attr->mode; |
1738 | return 0; | |
1739 | } else if (attr == &dev_attr_nvm_version.attr) { | |
b0407983 | 1740 | if (nvm_readable(sw)) |
e6b245cc MW |
1741 | return attr->mode; |
1742 | return 0; | |
14862ee3 YB |
1743 | } else if (attr == &dev_attr_boot.attr) { |
1744 | if (tb_route(sw)) | |
1745 | return attr->mode; | |
1746 | return 0; | |
1cb36293 ML |
1747 | } else if (attr == &dev_attr_nvm_authenticate_on_disconnect.attr) { |
1748 | if (sw->quirks & QUIRK_FORCE_POWER_LINK_CONTROLLER) | |
1749 | return attr->mode; | |
1750 | return 0; | |
f67cf491 MW |
1751 | } |
1752 | ||
e6b245cc | 1753 | return sw->safe_mode ? 0 : attr->mode; |
f67cf491 MW |
1754 | } |
1755 | ||
bfe778ac | 1756 | static struct attribute_group switch_group = { |
f67cf491 | 1757 | .is_visible = switch_attr_is_visible, |
bfe778ac MW |
1758 | .attrs = switch_attrs, |
1759 | }; | |
ca389f71 | 1760 | |
bfe778ac MW |
1761 | static const struct attribute_group *switch_groups[] = { |
1762 | &switch_group, | |
1763 | NULL, | |
1764 | }; | |
1765 | ||
1766 | static void tb_switch_release(struct device *dev) | |
1767 | { | |
1768 | struct tb_switch *sw = tb_to_switch(dev); | |
b433d010 | 1769 | struct tb_port *port; |
bfe778ac | 1770 | |
3e136768 MW |
1771 | dma_port_free(sw->dma_port); |
1772 | ||
b433d010 MW |
1773 | tb_switch_for_each_port(sw, port) { |
1774 | if (!port->disabled) { | |
1775 | ida_destroy(&port->in_hopids); | |
1776 | ida_destroy(&port->out_hopids); | |
0b2863ac MW |
1777 | } |
1778 | } | |
1779 | ||
bfe778ac | 1780 | kfree(sw->uuid); |
72ee3390 MW |
1781 | kfree(sw->device_name); |
1782 | kfree(sw->vendor_name); | |
a25c8b2f | 1783 | kfree(sw->ports); |
343fcb8c | 1784 | kfree(sw->drom); |
f67cf491 | 1785 | kfree(sw->key); |
a25c8b2f AN |
1786 | kfree(sw); |
1787 | } | |
1788 | ||
2d8ff0b5 MW |
1789 | /* |
1790 | * Currently only need to provide the callbacks. Everything else is handled | |
1791 | * in the connection manager. | |
1792 | */ | |
1793 | static int __maybe_unused tb_switch_runtime_suspend(struct device *dev) | |
1794 | { | |
4f7c2e0d MW |
1795 | struct tb_switch *sw = tb_to_switch(dev); |
1796 | const struct tb_cm_ops *cm_ops = sw->tb->cm_ops; | |
1797 | ||
1798 | if (cm_ops->runtime_suspend_switch) | |
1799 | return cm_ops->runtime_suspend_switch(sw); | |
1800 | ||
2d8ff0b5 MW |
1801 | return 0; |
1802 | } | |
1803 | ||
1804 | static int __maybe_unused tb_switch_runtime_resume(struct device *dev) | |
1805 | { | |
4f7c2e0d MW |
1806 | struct tb_switch *sw = tb_to_switch(dev); |
1807 | const struct tb_cm_ops *cm_ops = sw->tb->cm_ops; | |
1808 | ||
1809 | if (cm_ops->runtime_resume_switch) | |
1810 | return cm_ops->runtime_resume_switch(sw); | |
2d8ff0b5 MW |
1811 | return 0; |
1812 | } | |
1813 | ||
1814 | static const struct dev_pm_ops tb_switch_pm_ops = { | |
1815 | SET_RUNTIME_PM_OPS(tb_switch_runtime_suspend, tb_switch_runtime_resume, | |
1816 | NULL) | |
1817 | }; | |
1818 | ||
bfe778ac MW |
1819 | struct device_type tb_switch_type = { |
1820 | .name = "thunderbolt_device", | |
1821 | .release = tb_switch_release, | |
2d8ff0b5 | 1822 | .pm = &tb_switch_pm_ops, |
bfe778ac MW |
1823 | }; |
1824 | ||
2c3c4197 MW |
1825 | static int tb_switch_get_generation(struct tb_switch *sw) |
1826 | { | |
1827 | switch (sw->config.device_id) { | |
1828 | case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE: | |
1829 | case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE: | |
1830 | case PCI_DEVICE_ID_INTEL_LIGHT_PEAK: | |
1831 | case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C: | |
1832 | case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C: | |
1833 | case PCI_DEVICE_ID_INTEL_PORT_RIDGE: | |
1834 | case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE: | |
1835 | case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE: | |
1836 | return 1; | |
1837 | ||
1838 | case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE: | |
1839 | case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE: | |
1840 | case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE: | |
1841 | return 2; | |
1842 | ||
1843 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE: | |
1844 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE: | |
1845 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE: | |
1846 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE: | |
1847 | case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE: | |
4bac471d RM |
1848 | case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE: |
1849 | case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE: | |
1850 | case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE: | |
3cdb9446 MW |
1851 | case PCI_DEVICE_ID_INTEL_ICL_NHI0: |
1852 | case PCI_DEVICE_ID_INTEL_ICL_NHI1: | |
2c3c4197 MW |
1853 | return 3; |
1854 | ||
1855 | default: | |
b0407983 MW |
1856 | if (tb_switch_is_usb4(sw)) |
1857 | return 4; | |
1858 | ||
2c3c4197 MW |
1859 | /* |
1860 | * For unknown switches assume generation to be 1 to be | |
1861 | * on the safe side. | |
1862 | */ | |
1863 | tb_sw_warn(sw, "unsupported switch device id %#x\n", | |
1864 | sw->config.device_id); | |
1865 | return 1; | |
1866 | } | |
1867 | } | |
1868 | ||
b0407983 MW |
1869 | static bool tb_switch_exceeds_max_depth(const struct tb_switch *sw, int depth) |
1870 | { | |
1871 | int max_depth; | |
1872 | ||
1873 | if (tb_switch_is_usb4(sw) || | |
1874 | (sw->tb->root_switch && tb_switch_is_usb4(sw->tb->root_switch))) | |
1875 | max_depth = USB4_SWITCH_MAX_DEPTH; | |
1876 | else | |
1877 | max_depth = TB_SWITCH_MAX_DEPTH; | |
1878 | ||
1879 | return depth > max_depth; | |
1880 | } | |
1881 | ||
a25c8b2f | 1882 | /** |
bfe778ac MW |
1883 | * tb_switch_alloc() - allocate a switch |
1884 | * @tb: Pointer to the owning domain | |
1885 | * @parent: Parent device for this switch | |
1886 | * @route: Route string for this switch | |
a25c8b2f | 1887 | * |
bfe778ac MW |
1888 | * Allocates and initializes a switch. Will not upload configuration to |
1889 | * the switch. For that you need to call tb_switch_configure() | |
1890 | * separately. The returned switch should be released by calling | |
1891 | * tb_switch_put(). | |
1892 | * | |
444ac384 MW |
1893 | * Return: Pointer to the allocated switch or ERR_PTR() in case of |
1894 | * failure. | |
a25c8b2f | 1895 | */ |
bfe778ac MW |
1896 | struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent, |
1897 | u64 route) | |
a25c8b2f | 1898 | { |
a25c8b2f | 1899 | struct tb_switch *sw; |
f0342e75 | 1900 | int upstream_port; |
444ac384 | 1901 | int i, ret, depth; |
f0342e75 | 1902 | |
b0407983 MW |
1903 | /* Unlock the downstream port so we can access the switch below */ |
1904 | if (route) { | |
1905 | struct tb_switch *parent_sw = tb_to_switch(parent); | |
1906 | struct tb_port *down; | |
1907 | ||
1908 | down = tb_port_at(route, parent_sw); | |
1909 | tb_port_unlock(down); | |
1910 | } | |
1911 | ||
f0342e75 | 1912 | depth = tb_route_length(route); |
f0342e75 MW |
1913 | |
1914 | upstream_port = tb_cfg_get_upstream_port(tb->ctl, route); | |
a25c8b2f | 1915 | if (upstream_port < 0) |
444ac384 | 1916 | return ERR_PTR(upstream_port); |
a25c8b2f AN |
1917 | |
1918 | sw = kzalloc(sizeof(*sw), GFP_KERNEL); | |
1919 | if (!sw) | |
444ac384 | 1920 | return ERR_PTR(-ENOMEM); |
a25c8b2f AN |
1921 | |
1922 | sw->tb = tb; | |
444ac384 MW |
1923 | ret = tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5); |
1924 | if (ret) | |
bfe778ac MW |
1925 | goto err_free_sw_ports; |
1926 | ||
b0407983 MW |
1927 | sw->generation = tb_switch_get_generation(sw); |
1928 | ||
daa5140f | 1929 | tb_dbg(tb, "current switch config:\n"); |
b0407983 | 1930 | tb_dump_switch(tb, sw); |
a25c8b2f AN |
1931 | |
1932 | /* configure switch */ | |
1933 | sw->config.upstream_port_number = upstream_port; | |
f0342e75 MW |
1934 | sw->config.depth = depth; |
1935 | sw->config.route_hi = upper_32_bits(route); | |
1936 | sw->config.route_lo = lower_32_bits(route); | |
bfe778ac | 1937 | sw->config.enabled = 0; |
a25c8b2f | 1938 | |
b0407983 | 1939 | /* Make sure we do not exceed maximum topology limit */ |
704a940d CIK |
1940 | if (tb_switch_exceeds_max_depth(sw, depth)) { |
1941 | ret = -EADDRNOTAVAIL; | |
1942 | goto err_free_sw_ports; | |
1943 | } | |
b0407983 | 1944 | |
a25c8b2f AN |
1945 | /* initialize ports */ |
1946 | sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports), | |
343fcb8c | 1947 | GFP_KERNEL); |
444ac384 MW |
1948 | if (!sw->ports) { |
1949 | ret = -ENOMEM; | |
bfe778ac | 1950 | goto err_free_sw_ports; |
444ac384 | 1951 | } |
a25c8b2f AN |
1952 | |
1953 | for (i = 0; i <= sw->config.max_port_number; i++) { | |
343fcb8c AN |
1954 | /* minimum setup for tb_find_cap and tb_drom_read to work */ |
1955 | sw->ports[i].sw = sw; | |
1956 | sw->ports[i].port = i; | |
a25c8b2f AN |
1957 | } |
1958 | ||
444ac384 | 1959 | ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS); |
b0407983 MW |
1960 | if (ret > 0) |
1961 | sw->cap_plug_events = ret; | |
ca389f71 | 1962 | |
444ac384 MW |
1963 | ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER); |
1964 | if (ret > 0) | |
1965 | sw->cap_lc = ret; | |
a9be5582 | 1966 | |
f67cf491 MW |
1967 | /* Root switch is always authorized */ |
1968 | if (!route) | |
1969 | sw->authorized = true; | |
1970 | ||
bfe778ac MW |
1971 | device_initialize(&sw->dev); |
1972 | sw->dev.parent = parent; | |
1973 | sw->dev.bus = &tb_bus_type; | |
1974 | sw->dev.type = &tb_switch_type; | |
1975 | sw->dev.groups = switch_groups; | |
1976 | dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); | |
1977 | ||
1978 | return sw; | |
1979 | ||
1980 | err_free_sw_ports: | |
1981 | kfree(sw->ports); | |
1982 | kfree(sw); | |
1983 | ||
444ac384 | 1984 | return ERR_PTR(ret); |
bfe778ac MW |
1985 | } |
1986 | ||
e6b245cc MW |
1987 | /** |
1988 | * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode | |
1989 | * @tb: Pointer to the owning domain | |
1990 | * @parent: Parent device for this switch | |
1991 | * @route: Route string for this switch | |
1992 | * | |
1993 | * This creates a switch in safe mode. This means the switch pretty much | |
1994 | * lacks all capabilities except DMA configuration port before it is | |
1995 | * flashed with a valid NVM firmware. | |
1996 | * | |
1997 | * The returned switch must be released by calling tb_switch_put(). | |
1998 | * | |
444ac384 | 1999 | * Return: Pointer to the allocated switch or ERR_PTR() in case of failure |
e6b245cc MW |
2000 | */ |
2001 | struct tb_switch * | |
2002 | tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route) | |
2003 | { | |
2004 | struct tb_switch *sw; | |
2005 | ||
2006 | sw = kzalloc(sizeof(*sw), GFP_KERNEL); | |
2007 | if (!sw) | |
444ac384 | 2008 | return ERR_PTR(-ENOMEM); |
e6b245cc MW |
2009 | |
2010 | sw->tb = tb; | |
2011 | sw->config.depth = tb_route_length(route); | |
2012 | sw->config.route_hi = upper_32_bits(route); | |
2013 | sw->config.route_lo = lower_32_bits(route); | |
2014 | sw->safe_mode = true; | |
2015 | ||
2016 | device_initialize(&sw->dev); | |
2017 | sw->dev.parent = parent; | |
2018 | sw->dev.bus = &tb_bus_type; | |
2019 | sw->dev.type = &tb_switch_type; | |
2020 | sw->dev.groups = switch_groups; | |
2021 | dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); | |
2022 | ||
2023 | return sw; | |
2024 | } | |
2025 | ||
bfe778ac MW |
2026 | /** |
2027 | * tb_switch_configure() - Uploads configuration to the switch | |
2028 | * @sw: Switch to configure | |
2029 | * | |
2030 | * Call this function before the switch is added to the system. It will | |
2031 | * upload configuration to the switch and makes it available for the | |
b0407983 MW |
2032 | * connection manager to use. Can be called to the switch again after |
2033 | * resume from low power states to re-initialize it. | |
bfe778ac MW |
2034 | * |
2035 | * Return: %0 in case of success and negative errno in case of failure | |
2036 | */ | |
2037 | int tb_switch_configure(struct tb_switch *sw) | |
2038 | { | |
2039 | struct tb *tb = sw->tb; | |
2040 | u64 route; | |
2041 | int ret; | |
2042 | ||
2043 | route = tb_route(sw); | |
bfe778ac | 2044 | |
b0407983 | 2045 | tb_dbg(tb, "%s Switch at %#llx (depth: %d, up port: %d)\n", |
b2911a59 | 2046 | sw->config.enabled ? "restoring" : "initializing", route, |
b0407983 | 2047 | tb_route_length(route), sw->config.upstream_port_number); |
bfe778ac | 2048 | |
bfe778ac MW |
2049 | sw->config.enabled = 1; |
2050 | ||
b0407983 MW |
2051 | if (tb_switch_is_usb4(sw)) { |
2052 | /* | |
2053 | * For USB4 devices, we need to program the CM version | |
2054 | * accordingly so that it knows to expose all the | |
2055 | * additional capabilities. | |
2056 | */ | |
2057 | sw->config.cmuv = USB4_VERSION_1_0; | |
2058 | ||
2059 | /* Enumerate the switch */ | |
2060 | ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH, | |
2061 | ROUTER_CS_1, 4); | |
2062 | if (ret) | |
2063 | return ret; | |
bfe778ac | 2064 | |
b0407983 | 2065 | ret = usb4_switch_setup(sw); |
b0407983 MW |
2066 | } else { |
2067 | if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL) | |
2068 | tb_sw_warn(sw, "unknown switch vendor id %#x\n", | |
2069 | sw->config.vendor_id); | |
2070 | ||
2071 | if (!sw->cap_plug_events) { | |
2072 | tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n"); | |
2073 | return -ENODEV; | |
2074 | } | |
2075 | ||
2076 | /* Enumerate the switch */ | |
2077 | ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH, | |
2078 | ROUTER_CS_1, 3); | |
b0407983 | 2079 | } |
e879a709 MW |
2080 | if (ret) |
2081 | return ret; | |
2082 | ||
bfe778ac MW |
2083 | return tb_plug_events_active(sw, true); |
2084 | } | |
2085 | ||
2cc12751 | 2086 | static int tb_switch_set_uuid(struct tb_switch *sw) |
bfe778ac | 2087 | { |
b0407983 | 2088 | bool uid = false; |
bfe778ac | 2089 | u32 uuid[4]; |
a9be5582 | 2090 | int ret; |
bfe778ac MW |
2091 | |
2092 | if (sw->uuid) | |
a9be5582 | 2093 | return 0; |
bfe778ac | 2094 | |
b0407983 MW |
2095 | if (tb_switch_is_usb4(sw)) { |
2096 | ret = usb4_switch_read_uid(sw, &sw->uid); | |
2097 | if (ret) | |
2098 | return ret; | |
2099 | uid = true; | |
2100 | } else { | |
2101 | /* | |
2102 | * The newer controllers include fused UUID as part of | |
2103 | * link controller specific registers | |
2104 | */ | |
2105 | ret = tb_lc_read_uuid(sw, uuid); | |
2106 | if (ret) { | |
2107 | if (ret != -EINVAL) | |
2108 | return ret; | |
2109 | uid = true; | |
2110 | } | |
2111 | } | |
2112 | ||
2113 | if (uid) { | |
bfe778ac MW |
2114 | /* |
2115 | * ICM generates UUID based on UID and fills the upper | |
2116 | * two words with ones. This is not strictly following | |
2117 | * UUID format but we want to be compatible with it so | |
2118 | * we do the same here. | |
2119 | */ | |
2120 | uuid[0] = sw->uid & 0xffffffff; | |
2121 | uuid[1] = (sw->uid >> 32) & 0xffffffff; | |
2122 | uuid[2] = 0xffffffff; | |
2123 | uuid[3] = 0xffffffff; | |
2124 | } | |
2125 | ||
2126 | sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL); | |
2cc12751 | 2127 | if (!sw->uuid) |
a9be5582 MW |
2128 | return -ENOMEM; |
2129 | return 0; | |
bfe778ac MW |
2130 | } |
2131 | ||
e6b245cc | 2132 | static int tb_switch_add_dma_port(struct tb_switch *sw) |
3e136768 | 2133 | { |
e6b245cc MW |
2134 | u32 status; |
2135 | int ret; | |
2136 | ||
3e136768 | 2137 | switch (sw->generation) { |
3e136768 MW |
2138 | case 2: |
2139 | /* Only root switch can be upgraded */ | |
2140 | if (tb_route(sw)) | |
e6b245cc | 2141 | return 0; |
7a7ebfa8 | 2142 | |
df561f66 | 2143 | fallthrough; |
7a7ebfa8 MW |
2144 | case 3: |
2145 | ret = tb_switch_set_uuid(sw); | |
2146 | if (ret) | |
2147 | return ret; | |
3e136768 MW |
2148 | break; |
2149 | ||
2150 | default: | |
e6b245cc MW |
2151 | /* |
2152 | * DMA port is the only thing available when the switch | |
2153 | * is in safe mode. | |
2154 | */ | |
2155 | if (!sw->safe_mode) | |
2156 | return 0; | |
2157 | break; | |
3e136768 MW |
2158 | } |
2159 | ||
3f415e5e | 2160 | /* Root switch DMA port requires running firmware */ |
f07a3608 | 2161 | if (!tb_route(sw) && !tb_switch_is_icm(sw)) |
e6b245cc MW |
2162 | return 0; |
2163 | ||
3e136768 | 2164 | sw->dma_port = dma_port_alloc(sw); |
e6b245cc MW |
2165 | if (!sw->dma_port) |
2166 | return 0; | |
2167 | ||
3f415e5e MW |
2168 | if (sw->no_nvm_upgrade) |
2169 | return 0; | |
2170 | ||
7a7ebfa8 MW |
2171 | /* |
2172 | * If there is status already set then authentication failed | |
2173 | * when the dma_port_flash_update_auth() returned. Power cycling | |
2174 | * is not needed (it was done already) so only thing we do here | |
2175 | * is to unblock runtime PM of the root port. | |
2176 | */ | |
2177 | nvm_get_auth_status(sw, &status); | |
2178 | if (status) { | |
2179 | if (!tb_route(sw)) | |
b0407983 | 2180 | nvm_authenticate_complete_dma_port(sw); |
7a7ebfa8 MW |
2181 | return 0; |
2182 | } | |
2183 | ||
e6b245cc MW |
2184 | /* |
2185 | * Check status of the previous flash authentication. If there | |
2186 | * is one we need to power cycle the switch in any case to make | |
2187 | * it functional again. | |
2188 | */ | |
2189 | ret = dma_port_flash_update_auth_status(sw->dma_port, &status); | |
2190 | if (ret <= 0) | |
2191 | return ret; | |
2192 | ||
1830b6ee MW |
2193 | /* Now we can allow root port to suspend again */ |
2194 | if (!tb_route(sw)) | |
b0407983 | 2195 | nvm_authenticate_complete_dma_port(sw); |
1830b6ee | 2196 | |
e6b245cc MW |
2197 | if (status) { |
2198 | tb_sw_info(sw, "switch flash authentication failed\n"); | |
e6b245cc MW |
2199 | nvm_set_auth_status(sw, status); |
2200 | } | |
2201 | ||
2202 | tb_sw_info(sw, "power cycling the switch now\n"); | |
2203 | dma_port_power_cycle(sw->dma_port); | |
2204 | ||
2205 | /* | |
2206 | * We return error here which causes the switch adding failure. | |
2207 | * It should appear back after power cycle is complete. | |
2208 | */ | |
2209 | return -ESHUTDOWN; | |
3e136768 MW |
2210 | } |
2211 | ||
0d46c08d MW |
2212 | static void tb_switch_default_link_ports(struct tb_switch *sw) |
2213 | { | |
2214 | int i; | |
2215 | ||
2216 | for (i = 1; i <= sw->config.max_port_number; i += 2) { | |
2217 | struct tb_port *port = &sw->ports[i]; | |
2218 | struct tb_port *subordinate; | |
2219 | ||
2220 | if (!tb_port_is_null(port)) | |
2221 | continue; | |
2222 | ||
2223 | /* Check for the subordinate port */ | |
2224 | if (i == sw->config.max_port_number || | |
2225 | !tb_port_is_null(&sw->ports[i + 1])) | |
2226 | continue; | |
2227 | ||
2228 | /* Link them if not already done so (by DROM) */ | |
2229 | subordinate = &sw->ports[i + 1]; | |
2230 | if (!port->dual_link_port && !subordinate->dual_link_port) { | |
2231 | port->link_nr = 0; | |
2232 | port->dual_link_port = subordinate; | |
2233 | subordinate->link_nr = 1; | |
2234 | subordinate->dual_link_port = port; | |
2235 | ||
2236 | tb_sw_dbg(sw, "linked ports %d <-> %d\n", | |
2237 | port->port, subordinate->port); | |
2238 | } | |
2239 | } | |
2240 | } | |
2241 | ||
91c0c120 MW |
2242 | static bool tb_switch_lane_bonding_possible(struct tb_switch *sw) |
2243 | { | |
2244 | const struct tb_port *up = tb_upstream_port(sw); | |
2245 | ||
2246 | if (!up->dual_link_port || !up->dual_link_port->remote) | |
2247 | return false; | |
2248 | ||
b0407983 MW |
2249 | if (tb_switch_is_usb4(sw)) |
2250 | return usb4_switch_lane_bonding_possible(sw); | |
91c0c120 MW |
2251 | return tb_lc_lane_bonding_possible(sw); |
2252 | } | |
2253 | ||
2254 | static int tb_switch_update_link_attributes(struct tb_switch *sw) | |
2255 | { | |
2256 | struct tb_port *up; | |
2257 | bool change = false; | |
2258 | int ret; | |
2259 | ||
2260 | if (!tb_route(sw) || tb_switch_is_icm(sw)) | |
2261 | return 0; | |
2262 | ||
2263 | up = tb_upstream_port(sw); | |
2264 | ||
2265 | ret = tb_port_get_link_speed(up); | |
2266 | if (ret < 0) | |
2267 | return ret; | |
2268 | if (sw->link_speed != ret) | |
2269 | change = true; | |
2270 | sw->link_speed = ret; | |
2271 | ||
2272 | ret = tb_port_get_link_width(up); | |
2273 | if (ret < 0) | |
2274 | return ret; | |
2275 | if (sw->link_width != ret) | |
2276 | change = true; | |
2277 | sw->link_width = ret; | |
2278 | ||
2279 | /* Notify userspace that there is possible link attribute change */ | |
2280 | if (device_is_registered(&sw->dev) && change) | |
2281 | kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE); | |
2282 | ||
2283 | return 0; | |
2284 | } | |
2285 | ||
2286 | /** | |
2287 | * tb_switch_lane_bonding_enable() - Enable lane bonding | |
2288 | * @sw: Switch to enable lane bonding | |
2289 | * | |
2290 | * Connection manager can call this function to enable lane bonding of a | |
2291 | * switch. If conditions are correct and both switches support the feature, | |
2292 | * lanes are bonded. It is safe to call this to any switch. | |
2293 | */ | |
2294 | int tb_switch_lane_bonding_enable(struct tb_switch *sw) | |
2295 | { | |
2296 | struct tb_switch *parent = tb_to_switch(sw->dev.parent); | |
2297 | struct tb_port *up, *down; | |
2298 | u64 route = tb_route(sw); | |
2299 | int ret; | |
2300 | ||
2301 | if (!route) | |
2302 | return 0; | |
2303 | ||
2304 | if (!tb_switch_lane_bonding_possible(sw)) | |
2305 | return 0; | |
2306 | ||
2307 | up = tb_upstream_port(sw); | |
2308 | down = tb_port_at(route, parent); | |
2309 | ||
2310 | if (!tb_port_is_width_supported(up, 2) || | |
2311 | !tb_port_is_width_supported(down, 2)) | |
2312 | return 0; | |
2313 | ||
2314 | ret = tb_port_lane_bonding_enable(up); | |
2315 | if (ret) { | |
2316 | tb_port_warn(up, "failed to enable lane bonding\n"); | |
2317 | return ret; | |
2318 | } | |
2319 | ||
2320 | ret = tb_port_lane_bonding_enable(down); | |
2321 | if (ret) { | |
2322 | tb_port_warn(down, "failed to enable lane bonding\n"); | |
2323 | tb_port_lane_bonding_disable(up); | |
2324 | return ret; | |
2325 | } | |
2326 | ||
2327 | tb_switch_update_link_attributes(sw); | |
2328 | ||
2329 | tb_sw_dbg(sw, "lane bonding enabled\n"); | |
2330 | return ret; | |
2331 | } | |
2332 | ||
2333 | /** | |
2334 | * tb_switch_lane_bonding_disable() - Disable lane bonding | |
2335 | * @sw: Switch whose lane bonding to disable | |
2336 | * | |
2337 | * Disables lane bonding between @sw and parent. This can be called even | |
2338 | * if lanes were not bonded originally. | |
2339 | */ | |
2340 | void tb_switch_lane_bonding_disable(struct tb_switch *sw) | |
2341 | { | |
2342 | struct tb_switch *parent = tb_to_switch(sw->dev.parent); | |
2343 | struct tb_port *up, *down; | |
2344 | ||
2345 | if (!tb_route(sw)) | |
2346 | return; | |
2347 | ||
2348 | up = tb_upstream_port(sw); | |
2349 | if (!up->bonded) | |
2350 | return; | |
2351 | ||
2352 | down = tb_port_at(tb_route(sw), parent); | |
2353 | ||
2354 | tb_port_lane_bonding_disable(up); | |
2355 | tb_port_lane_bonding_disable(down); | |
2356 | ||
2357 | tb_switch_update_link_attributes(sw); | |
2358 | tb_sw_dbg(sw, "lane bonding disabled\n"); | |
2359 | } | |
2360 | ||
de462039 MW |
2361 | /** |
2362 | * tb_switch_configure_link() - Set link configured | |
2363 | * @sw: Switch whose link is configured | |
2364 | * | |
2365 | * Sets the link upstream from @sw configured (from both ends) so that | |
2366 | * it will not be disconnected when the domain exits sleep. Can be | |
2367 | * called for any switch. | |
2368 | * | |
2369 | * It is recommended that this is called after lane bonding is enabled. | |
2370 | * | |
2371 | * Returns %0 on success and negative errno in case of error. | |
2372 | */ | |
2373 | int tb_switch_configure_link(struct tb_switch *sw) | |
2374 | { | |
e28178bf MW |
2375 | struct tb_port *up, *down; |
2376 | int ret; | |
2377 | ||
de462039 MW |
2378 | if (!tb_route(sw) || tb_switch_is_icm(sw)) |
2379 | return 0; | |
2380 | ||
e28178bf MW |
2381 | up = tb_upstream_port(sw); |
2382 | if (tb_switch_is_usb4(up->sw)) | |
2383 | ret = usb4_port_configure(up); | |
2384 | else | |
2385 | ret = tb_lc_configure_port(up); | |
2386 | if (ret) | |
2387 | return ret; | |
2388 | ||
2389 | down = up->remote; | |
2390 | if (tb_switch_is_usb4(down->sw)) | |
2391 | return usb4_port_configure(down); | |
2392 | return tb_lc_configure_port(down); | |
de462039 MW |
2393 | } |
2394 | ||
2395 | /** | |
2396 | * tb_switch_unconfigure_link() - Unconfigure link | |
2397 | * @sw: Switch whose link is unconfigured | |
2398 | * | |
2399 | * Sets the link unconfigured so the @sw will be disconnected if the | |
2400 | * domain exists sleep. | |
2401 | */ | |
2402 | void tb_switch_unconfigure_link(struct tb_switch *sw) | |
2403 | { | |
e28178bf MW |
2404 | struct tb_port *up, *down; |
2405 | ||
de462039 MW |
2406 | if (sw->is_unplugged) |
2407 | return; | |
2408 | if (!tb_route(sw) || tb_switch_is_icm(sw)) | |
2409 | return; | |
2410 | ||
e28178bf MW |
2411 | up = tb_upstream_port(sw); |
2412 | if (tb_switch_is_usb4(up->sw)) | |
2413 | usb4_port_unconfigure(up); | |
2414 | else | |
2415 | tb_lc_unconfigure_port(up); | |
2416 | ||
2417 | down = up->remote; | |
2418 | if (tb_switch_is_usb4(down->sw)) | |
2419 | usb4_port_unconfigure(down); | |
de462039 | 2420 | else |
e28178bf | 2421 | tb_lc_unconfigure_port(down); |
de462039 MW |
2422 | } |
2423 | ||
bfe778ac MW |
2424 | /** |
2425 | * tb_switch_add() - Add a switch to the domain | |
2426 | * @sw: Switch to add | |
2427 | * | |
2428 | * This is the last step in adding switch to the domain. It will read | |
2429 | * identification information from DROM and initializes ports so that | |
2430 | * they can be used to connect other switches. The switch will be | |
2431 | * exposed to the userspace when this function successfully returns. To | |
2432 | * remove and release the switch, call tb_switch_remove(). | |
2433 | * | |
2434 | * Return: %0 in case of success and negative errno in case of failure | |
2435 | */ | |
2436 | int tb_switch_add(struct tb_switch *sw) | |
2437 | { | |
2438 | int i, ret; | |
2439 | ||
3e136768 MW |
2440 | /* |
2441 | * Initialize DMA control port now before we read DROM. Recent | |
2442 | * host controllers have more complete DROM on NVM that includes | |
2443 | * vendor and model identification strings which we then expose | |
2444 | * to the userspace. NVM can be accessed through DMA | |
2445 | * configuration based mailbox. | |
2446 | */ | |
e6b245cc | 2447 | ret = tb_switch_add_dma_port(sw); |
af99f696 MW |
2448 | if (ret) { |
2449 | dev_err(&sw->dev, "failed to add DMA port\n"); | |
f53e7676 | 2450 | return ret; |
af99f696 | 2451 | } |
343fcb8c | 2452 | |
e6b245cc MW |
2453 | if (!sw->safe_mode) { |
2454 | /* read drom */ | |
2455 | ret = tb_drom_read(sw); | |
2456 | if (ret) { | |
af99f696 | 2457 | dev_err(&sw->dev, "reading DROM failed\n"); |
e6b245cc MW |
2458 | return ret; |
2459 | } | |
daa5140f | 2460 | tb_sw_dbg(sw, "uid: %#llx\n", sw->uid); |
bfe778ac | 2461 | |
2cc12751 | 2462 | ret = tb_switch_set_uuid(sw); |
af99f696 MW |
2463 | if (ret) { |
2464 | dev_err(&sw->dev, "failed to set UUID\n"); | |
2cc12751 | 2465 | return ret; |
af99f696 | 2466 | } |
e6b245cc MW |
2467 | |
2468 | for (i = 0; i <= sw->config.max_port_number; i++) { | |
2469 | if (sw->ports[i].disabled) { | |
daa5140f | 2470 | tb_port_dbg(&sw->ports[i], "disabled by eeprom\n"); |
e6b245cc MW |
2471 | continue; |
2472 | } | |
2473 | ret = tb_init_port(&sw->ports[i]); | |
af99f696 MW |
2474 | if (ret) { |
2475 | dev_err(&sw->dev, "failed to initialize port %d\n", i); | |
e6b245cc | 2476 | return ret; |
af99f696 | 2477 | } |
343fcb8c | 2478 | } |
91c0c120 | 2479 | |
0d46c08d MW |
2480 | tb_switch_default_link_ports(sw); |
2481 | ||
91c0c120 MW |
2482 | ret = tb_switch_update_link_attributes(sw); |
2483 | if (ret) | |
2484 | return ret; | |
cf29b9af RM |
2485 | |
2486 | ret = tb_switch_tmu_init(sw); | |
2487 | if (ret) | |
2488 | return ret; | |
343fcb8c AN |
2489 | } |
2490 | ||
e6b245cc | 2491 | ret = device_add(&sw->dev); |
af99f696 MW |
2492 | if (ret) { |
2493 | dev_err(&sw->dev, "failed to add device: %d\n", ret); | |
e6b245cc | 2494 | return ret; |
af99f696 | 2495 | } |
e6b245cc | 2496 | |
a83bc4a5 MW |
2497 | if (tb_route(sw)) { |
2498 | dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n", | |
2499 | sw->vendor, sw->device); | |
2500 | if (sw->vendor_name && sw->device_name) | |
2501 | dev_info(&sw->dev, "%s %s\n", sw->vendor_name, | |
2502 | sw->device_name); | |
2503 | } | |
2504 | ||
e6b245cc | 2505 | ret = tb_switch_nvm_add(sw); |
2d8ff0b5 | 2506 | if (ret) { |
af99f696 | 2507 | dev_err(&sw->dev, "failed to add NVM devices\n"); |
e6b245cc | 2508 | device_del(&sw->dev); |
2d8ff0b5 MW |
2509 | return ret; |
2510 | } | |
e6b245cc | 2511 | |
b2911a59 MW |
2512 | /* |
2513 | * Thunderbolt routers do not generate wakeups themselves but | |
2514 | * they forward wakeups from tunneled protocols, so enable it | |
2515 | * here. | |
2516 | */ | |
2517 | device_init_wakeup(&sw->dev, true); | |
2518 | ||
2d8ff0b5 MW |
2519 | pm_runtime_set_active(&sw->dev); |
2520 | if (sw->rpm) { | |
2521 | pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY); | |
2522 | pm_runtime_use_autosuspend(&sw->dev); | |
2523 | pm_runtime_mark_last_busy(&sw->dev); | |
2524 | pm_runtime_enable(&sw->dev); | |
2525 | pm_request_autosuspend(&sw->dev); | |
2526 | } | |
2527 | ||
54e41810 | 2528 | tb_switch_debugfs_init(sw); |
2d8ff0b5 | 2529 | return 0; |
bfe778ac | 2530 | } |
c90553b3 | 2531 | |
bfe778ac MW |
2532 | /** |
2533 | * tb_switch_remove() - Remove and release a switch | |
2534 | * @sw: Switch to remove | |
2535 | * | |
2536 | * This will remove the switch from the domain and release it after last | |
2537 | * reference count drops to zero. If there are switches connected below | |
2538 | * this switch, they will be removed as well. | |
2539 | */ | |
2540 | void tb_switch_remove(struct tb_switch *sw) | |
2541 | { | |
b433d010 | 2542 | struct tb_port *port; |
ca389f71 | 2543 | |
54e41810 GF |
2544 | tb_switch_debugfs_remove(sw); |
2545 | ||
2d8ff0b5 MW |
2546 | if (sw->rpm) { |
2547 | pm_runtime_get_sync(&sw->dev); | |
2548 | pm_runtime_disable(&sw->dev); | |
2549 | } | |
2550 | ||
bfe778ac | 2551 | /* port 0 is the switch itself and never has a remote */ |
b433d010 MW |
2552 | tb_switch_for_each_port(sw, port) { |
2553 | if (tb_port_has_remote(port)) { | |
2554 | tb_switch_remove(port->remote->sw); | |
2555 | port->remote = NULL; | |
2556 | } else if (port->xdomain) { | |
2557 | tb_xdomain_remove(port->xdomain); | |
2558 | port->xdomain = NULL; | |
dfe40ca4 | 2559 | } |
dacb1287 KK |
2560 | |
2561 | /* Remove any downstream retimers */ | |
2562 | tb_retimer_remove_all(port); | |
bfe778ac MW |
2563 | } |
2564 | ||
2565 | if (!sw->is_unplugged) | |
2566 | tb_plug_events_active(sw, false); | |
b0407983 | 2567 | |
e6b245cc | 2568 | tb_switch_nvm_remove(sw); |
a83bc4a5 MW |
2569 | |
2570 | if (tb_route(sw)) | |
2571 | dev_info(&sw->dev, "device disconnected\n"); | |
bfe778ac | 2572 | device_unregister(&sw->dev); |
a25c8b2f AN |
2573 | } |
2574 | ||
053596d9 | 2575 | /** |
aae20bb6 | 2576 | * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches |
053596d9 | 2577 | */ |
aae20bb6 | 2578 | void tb_sw_set_unplugged(struct tb_switch *sw) |
053596d9 | 2579 | { |
b433d010 MW |
2580 | struct tb_port *port; |
2581 | ||
053596d9 AN |
2582 | if (sw == sw->tb->root_switch) { |
2583 | tb_sw_WARN(sw, "cannot unplug root switch\n"); | |
2584 | return; | |
2585 | } | |
2586 | if (sw->is_unplugged) { | |
2587 | tb_sw_WARN(sw, "is_unplugged already set\n"); | |
2588 | return; | |
2589 | } | |
2590 | sw->is_unplugged = true; | |
b433d010 MW |
2591 | tb_switch_for_each_port(sw, port) { |
2592 | if (tb_port_has_remote(port)) | |
2593 | tb_sw_set_unplugged(port->remote->sw); | |
2594 | else if (port->xdomain) | |
2595 | port->xdomain->is_unplugged = true; | |
053596d9 AN |
2596 | } |
2597 | } | |
2598 | ||
b2911a59 MW |
2599 | static int tb_switch_set_wake(struct tb_switch *sw, unsigned int flags) |
2600 | { | |
2601 | if (flags) | |
2602 | tb_sw_dbg(sw, "enabling wakeup: %#x\n", flags); | |
2603 | else | |
2604 | tb_sw_dbg(sw, "disabling wakeup\n"); | |
2605 | ||
2606 | if (tb_switch_is_usb4(sw)) | |
2607 | return usb4_switch_set_wake(sw, flags); | |
2608 | return tb_lc_set_wake(sw, flags); | |
2609 | } | |
2610 | ||
23dd5bb4 AN |
2611 | int tb_switch_resume(struct tb_switch *sw) |
2612 | { | |
b433d010 MW |
2613 | struct tb_port *port; |
2614 | int err; | |
2615 | ||
daa5140f | 2616 | tb_sw_dbg(sw, "resuming switch\n"); |
23dd5bb4 | 2617 | |
08a5e4ce MW |
2618 | /* |
2619 | * Check for UID of the connected switches except for root | |
2620 | * switch which we assume cannot be removed. | |
2621 | */ | |
2622 | if (tb_route(sw)) { | |
2623 | u64 uid; | |
2624 | ||
7ea4cd6b MW |
2625 | /* |
2626 | * Check first that we can still read the switch config | |
2627 | * space. It may be that there is now another domain | |
2628 | * connected. | |
2629 | */ | |
2630 | err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw)); | |
2631 | if (err < 0) { | |
2632 | tb_sw_info(sw, "switch not present anymore\n"); | |
2633 | return err; | |
2634 | } | |
2635 | ||
b0407983 MW |
2636 | if (tb_switch_is_usb4(sw)) |
2637 | err = usb4_switch_read_uid(sw, &uid); | |
2638 | else | |
2639 | err = tb_drom_read_uid_only(sw, &uid); | |
08a5e4ce MW |
2640 | if (err) { |
2641 | tb_sw_warn(sw, "uid read failed\n"); | |
2642 | return err; | |
2643 | } | |
2644 | if (sw->uid != uid) { | |
2645 | tb_sw_info(sw, | |
2646 | "changed while suspended (uid %#llx -> %#llx)\n", | |
2647 | sw->uid, uid); | |
2648 | return -ENODEV; | |
2649 | } | |
23dd5bb4 AN |
2650 | } |
2651 | ||
b0407983 | 2652 | err = tb_switch_configure(sw); |
23dd5bb4 AN |
2653 | if (err) |
2654 | return err; | |
2655 | ||
b2911a59 MW |
2656 | /* Disable wakes */ |
2657 | tb_switch_set_wake(sw, 0); | |
2658 | ||
8145c435 MW |
2659 | err = tb_switch_tmu_init(sw); |
2660 | if (err) | |
2661 | return err; | |
2662 | ||
23dd5bb4 | 2663 | /* check for surviving downstream switches */ |
b433d010 | 2664 | tb_switch_for_each_port(sw, port) { |
7ea4cd6b | 2665 | if (!tb_port_has_remote(port) && !port->xdomain) |
23dd5bb4 | 2666 | continue; |
dfe40ca4 | 2667 | |
7ea4cd6b | 2668 | if (tb_wait_for_port(port, true) <= 0) { |
23dd5bb4 AN |
2669 | tb_port_warn(port, |
2670 | "lost during suspend, disconnecting\n"); | |
7ea4cd6b MW |
2671 | if (tb_port_has_remote(port)) |
2672 | tb_sw_set_unplugged(port->remote->sw); | |
2673 | else if (port->xdomain) | |
2674 | port->xdomain->is_unplugged = true; | |
b0407983 MW |
2675 | } else if (tb_port_has_remote(port) || port->xdomain) { |
2676 | /* | |
2677 | * Always unlock the port so the downstream | |
2678 | * switch/domain is accessible. | |
2679 | */ | |
2680 | if (tb_port_unlock(port)) | |
2681 | tb_port_warn(port, "failed to unlock port\n"); | |
2682 | if (port->remote && tb_switch_resume(port->remote->sw)) { | |
7ea4cd6b MW |
2683 | tb_port_warn(port, |
2684 | "lost during suspend, disconnecting\n"); | |
2685 | tb_sw_set_unplugged(port->remote->sw); | |
2686 | } | |
23dd5bb4 AN |
2687 | } |
2688 | } | |
2689 | return 0; | |
2690 | } | |
2691 | ||
6ac6faee MW |
2692 | /** |
2693 | * tb_switch_suspend() - Put a switch to sleep | |
2694 | * @sw: Switch to suspend | |
2695 | * @runtime: Is this runtime suspend or system sleep | |
2696 | * | |
2697 | * Suspends router and all its children. Enables wakes according to | |
2698 | * value of @runtime and then sets sleep bit for the router. If @sw is | |
2699 | * host router the domain is ready to go to sleep once this function | |
2700 | * returns. | |
2701 | */ | |
2702 | void tb_switch_suspend(struct tb_switch *sw, bool runtime) | |
23dd5bb4 | 2703 | { |
b2911a59 | 2704 | unsigned int flags = 0; |
b433d010 MW |
2705 | struct tb_port *port; |
2706 | int err; | |
2707 | ||
6ac6faee MW |
2708 | tb_sw_dbg(sw, "suspending switch\n"); |
2709 | ||
23dd5bb4 AN |
2710 | err = tb_plug_events_active(sw, false); |
2711 | if (err) | |
2712 | return; | |
2713 | ||
b433d010 MW |
2714 | tb_switch_for_each_port(sw, port) { |
2715 | if (tb_port_has_remote(port)) | |
6ac6faee | 2716 | tb_switch_suspend(port->remote->sw, runtime); |
23dd5bb4 | 2717 | } |
5480dfc2 | 2718 | |
6ac6faee MW |
2719 | if (runtime) { |
2720 | /* Trigger wake when something is plugged in/out */ | |
2721 | flags |= TB_WAKE_ON_CONNECT | TB_WAKE_ON_DISCONNECT; | |
2722 | flags |= TB_WAKE_ON_USB4 | TB_WAKE_ON_USB3 | TB_WAKE_ON_PCIE; | |
2723 | } else if (device_may_wakeup(&sw->dev)) { | |
2724 | flags |= TB_WAKE_ON_USB4 | TB_WAKE_ON_USB3 | TB_WAKE_ON_PCIE; | |
2725 | } | |
b2911a59 MW |
2726 | |
2727 | tb_switch_set_wake(sw, flags); | |
2728 | ||
b0407983 MW |
2729 | if (tb_switch_is_usb4(sw)) |
2730 | usb4_switch_set_sleep(sw); | |
2731 | else | |
2732 | tb_lc_set_sleep(sw); | |
23dd5bb4 | 2733 | } |
f67cf491 | 2734 | |
8afe909b MW |
2735 | /** |
2736 | * tb_switch_query_dp_resource() - Query availability of DP resource | |
2737 | * @sw: Switch whose DP resource is queried | |
2738 | * @in: DP IN port | |
2739 | * | |
2740 | * Queries availability of DP resource for DP tunneling using switch | |
2741 | * specific means. Returns %true if resource is available. | |
2742 | */ | |
2743 | bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in) | |
2744 | { | |
b0407983 MW |
2745 | if (tb_switch_is_usb4(sw)) |
2746 | return usb4_switch_query_dp_resource(sw, in); | |
8afe909b MW |
2747 | return tb_lc_dp_sink_query(sw, in); |
2748 | } | |
2749 | ||
2750 | /** | |
2751 | * tb_switch_alloc_dp_resource() - Allocate available DP resource | |
2752 | * @sw: Switch whose DP resource is allocated | |
2753 | * @in: DP IN port | |
2754 | * | |
2755 | * Allocates DP resource for DP tunneling. The resource must be | |
2756 | * available for this to succeed (see tb_switch_query_dp_resource()). | |
2757 | * Returns %0 in success and negative errno otherwise. | |
2758 | */ | |
2759 | int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in) | |
2760 | { | |
b0407983 MW |
2761 | if (tb_switch_is_usb4(sw)) |
2762 | return usb4_switch_alloc_dp_resource(sw, in); | |
8afe909b MW |
2763 | return tb_lc_dp_sink_alloc(sw, in); |
2764 | } | |
2765 | ||
2766 | /** | |
2767 | * tb_switch_dealloc_dp_resource() - De-allocate DP resource | |
2768 | * @sw: Switch whose DP resource is de-allocated | |
2769 | * @in: DP IN port | |
2770 | * | |
2771 | * De-allocates DP resource that was previously allocated for DP | |
2772 | * tunneling. | |
2773 | */ | |
2774 | void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in) | |
2775 | { | |
b0407983 MW |
2776 | int ret; |
2777 | ||
2778 | if (tb_switch_is_usb4(sw)) | |
2779 | ret = usb4_switch_dealloc_dp_resource(sw, in); | |
2780 | else | |
2781 | ret = tb_lc_dp_sink_dealloc(sw, in); | |
2782 | ||
2783 | if (ret) | |
8afe909b MW |
2784 | tb_sw_warn(sw, "failed to de-allocate DP resource for port %d\n", |
2785 | in->port); | |
8afe909b MW |
2786 | } |
2787 | ||
f67cf491 MW |
2788 | struct tb_sw_lookup { |
2789 | struct tb *tb; | |
2790 | u8 link; | |
2791 | u8 depth; | |
7c39ffe7 | 2792 | const uuid_t *uuid; |
8e9267bb | 2793 | u64 route; |
f67cf491 MW |
2794 | }; |
2795 | ||
418e3ea1 | 2796 | static int tb_switch_match(struct device *dev, const void *data) |
f67cf491 MW |
2797 | { |
2798 | struct tb_switch *sw = tb_to_switch(dev); | |
418e3ea1 | 2799 | const struct tb_sw_lookup *lookup = data; |
f67cf491 MW |
2800 | |
2801 | if (!sw) | |
2802 | return 0; | |
2803 | if (sw->tb != lookup->tb) | |
2804 | return 0; | |
2805 | ||
2806 | if (lookup->uuid) | |
2807 | return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid)); | |
2808 | ||
8e9267bb RM |
2809 | if (lookup->route) { |
2810 | return sw->config.route_lo == lower_32_bits(lookup->route) && | |
2811 | sw->config.route_hi == upper_32_bits(lookup->route); | |
2812 | } | |
2813 | ||
f67cf491 MW |
2814 | /* Root switch is matched only by depth */ |
2815 | if (!lookup->depth) | |
2816 | return !sw->depth; | |
2817 | ||
2818 | return sw->link == lookup->link && sw->depth == lookup->depth; | |
2819 | } | |
2820 | ||
2821 | /** | |
2822 | * tb_switch_find_by_link_depth() - Find switch by link and depth | |
2823 | * @tb: Domain the switch belongs | |
2824 | * @link: Link number the switch is connected | |
2825 | * @depth: Depth of the switch in link | |
2826 | * | |
2827 | * Returned switch has reference count increased so the caller needs to | |
2828 | * call tb_switch_put() when done with the switch. | |
2829 | */ | |
2830 | struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth) | |
2831 | { | |
2832 | struct tb_sw_lookup lookup; | |
2833 | struct device *dev; | |
2834 | ||
2835 | memset(&lookup, 0, sizeof(lookup)); | |
2836 | lookup.tb = tb; | |
2837 | lookup.link = link; | |
2838 | lookup.depth = depth; | |
2839 | ||
2840 | dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match); | |
2841 | if (dev) | |
2842 | return tb_to_switch(dev); | |
2843 | ||
2844 | return NULL; | |
2845 | } | |
2846 | ||
2847 | /** | |
432019d6 | 2848 | * tb_switch_find_by_uuid() - Find switch by UUID |
f67cf491 MW |
2849 | * @tb: Domain the switch belongs |
2850 | * @uuid: UUID to look for | |
2851 | * | |
2852 | * Returned switch has reference count increased so the caller needs to | |
2853 | * call tb_switch_put() when done with the switch. | |
2854 | */ | |
7c39ffe7 | 2855 | struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid) |
f67cf491 MW |
2856 | { |
2857 | struct tb_sw_lookup lookup; | |
2858 | struct device *dev; | |
2859 | ||
2860 | memset(&lookup, 0, sizeof(lookup)); | |
2861 | lookup.tb = tb; | |
2862 | lookup.uuid = uuid; | |
2863 | ||
2864 | dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match); | |
2865 | if (dev) | |
2866 | return tb_to_switch(dev); | |
2867 | ||
2868 | return NULL; | |
2869 | } | |
e6b245cc | 2870 | |
8e9267bb RM |
2871 | /** |
2872 | * tb_switch_find_by_route() - Find switch by route string | |
2873 | * @tb: Domain the switch belongs | |
2874 | * @route: Route string to look for | |
2875 | * | |
2876 | * Returned switch has reference count increased so the caller needs to | |
2877 | * call tb_switch_put() when done with the switch. | |
2878 | */ | |
2879 | struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route) | |
2880 | { | |
2881 | struct tb_sw_lookup lookup; | |
2882 | struct device *dev; | |
2883 | ||
2884 | if (!route) | |
2885 | return tb_switch_get(tb->root_switch); | |
2886 | ||
2887 | memset(&lookup, 0, sizeof(lookup)); | |
2888 | lookup.tb = tb; | |
2889 | lookup.route = route; | |
2890 | ||
2891 | dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match); | |
2892 | if (dev) | |
2893 | return tb_to_switch(dev); | |
2894 | ||
2895 | return NULL; | |
2896 | } | |
2897 | ||
386e5e29 MW |
2898 | /** |
2899 | * tb_switch_find_port() - return the first port of @type on @sw or NULL | |
2900 | * @sw: Switch to find the port from | |
2901 | * @type: Port type to look for | |
2902 | */ | |
2903 | struct tb_port *tb_switch_find_port(struct tb_switch *sw, | |
2904 | enum tb_port_type type) | |
2905 | { | |
2906 | struct tb_port *port; | |
2907 | ||
2908 | tb_switch_for_each_port(sw, port) { | |
2909 | if (port->config.type == type) | |
2910 | return port; | |
2911 | } | |
2912 | ||
2913 | return NULL; | |
2914 | } |