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