1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - switch/port utility functions
6 * Copyright (C) 2018, Intel Corporation
9 #include <linux/delay.h>
10 #include <linux/idr.h>
11 #include <linux/nvmem-provider.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/sched/signal.h>
14 #include <linux/sizes.h>
15 #include <linux/slab.h>
16 #include <linux/vmalloc.h>
20 /* Switch NVM support */
22 #define NVM_DEVID 0x05
23 #define NVM_VERSION 0x08
25 #define NVM_FLASH_SIZE 0x45
27 #define NVM_MIN_SIZE SZ_32K
28 #define NVM_MAX_SIZE SZ_512K
30 static DEFINE_IDA(nvm_ida);
32 struct nvm_auth_status {
33 struct list_head list;
39 * Hold NVM authentication failure status per switch This information
40 * needs to stay around even when the switch gets power cycled so we
43 static LIST_HEAD(nvm_auth_status_cache);
44 static DEFINE_MUTEX(nvm_auth_status_lock);
46 static struct nvm_auth_status *__nvm_get_auth_status(const struct tb_switch *sw)
48 struct nvm_auth_status *st;
50 list_for_each_entry(st, &nvm_auth_status_cache, list) {
51 if (uuid_equal(&st->uuid, sw->uuid))
58 static void nvm_get_auth_status(const struct tb_switch *sw, u32 *status)
60 struct nvm_auth_status *st;
62 mutex_lock(&nvm_auth_status_lock);
63 st = __nvm_get_auth_status(sw);
64 mutex_unlock(&nvm_auth_status_lock);
66 *status = st ? st->status : 0;
69 static void nvm_set_auth_status(const struct tb_switch *sw, u32 status)
71 struct nvm_auth_status *st;
73 if (WARN_ON(!sw->uuid))
76 mutex_lock(&nvm_auth_status_lock);
77 st = __nvm_get_auth_status(sw);
80 st = kzalloc(sizeof(*st), GFP_KERNEL);
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);
91 mutex_unlock(&nvm_auth_status_lock);
94 static void nvm_clear_auth_status(const struct tb_switch *sw)
96 struct nvm_auth_status *st;
98 mutex_lock(&nvm_auth_status_lock);
99 st = __nvm_get_auth_status(sw);
104 mutex_unlock(&nvm_auth_status_lock);
107 static int nvm_validate_and_write(struct tb_switch *sw)
109 unsigned int image_size, hdr_size;
110 const u8 *buf = sw->nvm->buf;
117 image_size = sw->nvm->buf_data_size;
118 if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE)
122 * FARB pointer must point inside the image and must at least
123 * contain parts of the digital section we will be reading here.
125 hdr_size = (*(u32 *)buf) & 0xffffff;
126 if (hdr_size + NVM_DEVID + 2 >= image_size)
129 /* Digital section start should be aligned to 4k page */
130 if (!IS_ALIGNED(hdr_size, SZ_4K))
134 * Read digital section size and check that it also fits inside
137 ds_size = *(u16 *)(buf + hdr_size);
138 if (ds_size >= image_size)
141 if (!sw->safe_mode) {
145 * Make sure the device ID in the image matches the one
146 * we read from the switch config space.
148 device_id = *(u16 *)(buf + hdr_size + NVM_DEVID);
149 if (device_id != sw->config.device_id)
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);
161 /* Skip headers in the image */
163 image_size -= hdr_size;
166 return dma_port_flash_write(sw->dma_port, 0, buf, image_size);
169 static int nvm_authenticate_host(struct tb_switch *sw)
174 * Root switch NVM upgrade requires that we disconnect the
175 * existing paths first (in case it is not in safe mode
178 if (!sw->safe_mode) {
179 ret = tb_domain_disconnect_all_paths(sw->tb);
183 * The host controller goes away pretty soon after this if
184 * everything goes well so getting timeout is expected.
186 ret = dma_port_flash_update_auth(sw->dma_port);
187 return ret == -ETIMEDOUT ? 0 : ret;
191 * From safe mode we can get out by just power cycling the
194 dma_port_power_cycle(sw->dma_port);
198 static int nvm_authenticate_device(struct tb_switch *sw)
200 int ret, retries = 10;
202 ret = dma_port_flash_update_auth(sw->dma_port);
203 if (ret && ret != -ETIMEDOUT)
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.
215 ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
216 if (ret < 0 && ret != -ETIMEDOUT)
220 tb_sw_warn(sw, "failed to authenticate NVM\n");
221 nvm_set_auth_status(sw, status);
224 tb_sw_info(sw, "power cycling the switch now\n");
225 dma_port_power_cycle(sw->dma_port);
235 static int tb_switch_nvm_read(void *priv, unsigned int offset, void *val,
238 struct tb_switch *sw = priv;
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);
249 static int tb_switch_nvm_write(void *priv, unsigned int offset, void *val,
252 struct tb_switch *sw = priv;
255 if (!mutex_trylock(&sw->tb->lock))
256 return restart_syscall();
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.
265 sw->nvm->buf = vmalloc(NVM_MAX_SIZE);
272 sw->nvm->buf_data_size = offset + bytes;
273 memcpy(sw->nvm->buf + offset, val, bytes);
276 mutex_unlock(&sw->tb->lock);
281 static struct nvmem_device *register_nvmem(struct tb_switch *sw, int id,
282 size_t size, bool active)
284 struct nvmem_config config;
286 memset(&config, 0, sizeof(config));
289 config.name = "nvm_active";
290 config.reg_read = tb_switch_nvm_read;
291 config.read_only = true;
293 config.name = "nvm_non_active";
294 config.reg_write = tb_switch_nvm_write;
295 config.root_only = true;
300 config.word_size = 4;
302 config.dev = &sw->dev;
303 config.owner = THIS_MODULE;
306 return nvmem_register(&config);
309 static int tb_switch_nvm_add(struct tb_switch *sw)
311 struct nvmem_device *nvm_dev;
312 struct tb_switch_nvm *nvm;
319 nvm = kzalloc(sizeof(*nvm), GFP_KERNEL);
323 nvm->id = ida_simple_get(&nvm_ida, 0, 0, GFP_KERNEL);
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.
330 if (!sw->safe_mode) {
331 u32 nvm_size, hdr_size;
333 ret = dma_port_flash_read(sw->dma_port, NVM_FLASH_SIZE, &val,
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;
342 ret = dma_port_flash_read(sw->dma_port, NVM_VERSION, &val,
347 nvm->major = val >> 16;
348 nvm->minor = val >> 8;
350 nvm_dev = register_nvmem(sw, nvm->id, nvm_size, true);
351 if (IS_ERR(nvm_dev)) {
352 ret = PTR_ERR(nvm_dev);
355 nvm->active = nvm_dev;
358 nvm_dev = register_nvmem(sw, nvm->id, NVM_MAX_SIZE, false);
359 if (IS_ERR(nvm_dev)) {
360 ret = PTR_ERR(nvm_dev);
363 nvm->non_active = nvm_dev;
370 nvmem_unregister(nvm->active);
372 ida_simple_remove(&nvm_ida, nvm->id);
378 static void tb_switch_nvm_remove(struct tb_switch *sw)
380 struct tb_switch_nvm *nvm;
388 /* Remove authentication status in case the switch is unplugged */
389 if (!nvm->authenticating)
390 nvm_clear_auth_status(sw);
392 nvmem_unregister(nvm->non_active);
394 nvmem_unregister(nvm->active);
395 ida_simple_remove(&nvm_ida, nvm->id);
400 /* port utility functions */
402 static const char *tb_port_type(struct tb_regs_port_header *port)
404 switch (port->type >> 16) {
406 switch ((u8) port->type) {
431 static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port)
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),
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);
445 * tb_port_state() - get connectedness state of a port
447 * The port must have a TB_CAP_PHY (i.e. it should be a real port).
449 * Return: Returns an enum tb_port_state on success or an error code on failure.
451 static int tb_port_state(struct tb_port *port)
453 struct tb_cap_phy phy;
455 if (port->cap_phy == 0) {
456 tb_port_WARN(port, "does not have a PHY\n");
459 res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2);
466 * tb_wait_for_port() - wait for a port to become ready
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.
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.
478 int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
482 if (!port->cap_phy) {
483 tb_port_WARN(port, "does not have PHY\n");
486 if (tb_is_upstream_port(port)) {
487 tb_port_WARN(port, "is the upstream port\n");
492 state = tb_port_state(port);
495 if (state == TB_PORT_DISABLED) {
496 tb_port_dbg(port, "is disabled (state: 0)\n");
499 if (state == TB_PORT_UNPLUGGED) {
500 if (wait_if_unplugged) {
501 /* used during resume */
503 "is unplugged (state: 7), retrying...\n");
507 tb_port_dbg(port, "is unplugged (state: 7)\n");
510 if (state == TB_PORT_UP) {
511 tb_port_dbg(port, "is connected, link is up (state: 2)\n");
516 * After plug-in the state is TB_PORT_CONNECTING. Give it some
520 "is connected, link is not up (state: %d), retrying...\n",
525 "failed to reach state TB_PORT_UP. Ignoring port...\n");
530 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
532 * Change the number of NFC credits allocated to @port by @credits. To remove
533 * NFC credits pass a negative amount of credits.
535 * Return: Returns 0 on success or an error code on failure.
537 int tb_port_add_nfc_credits(struct tb_port *port, int credits)
541 if (credits == 0 || port->sw->is_unplugged)
544 nfc_credits = port->config.nfc_credits & TB_PORT_NFC_CREDITS_MASK;
545 nfc_credits += credits;
547 tb_port_dbg(port, "adding %d NFC credits to %lu",
548 credits, port->config.nfc_credits & TB_PORT_NFC_CREDITS_MASK);
550 port->config.nfc_credits &= ~TB_PORT_NFC_CREDITS_MASK;
551 port->config.nfc_credits |= nfc_credits;
553 return tb_port_write(port, &port->config.nfc_credits,
558 * tb_port_set_initial_credits() - Set initial port link credits allocated
559 * @port: Port to set the initial credits
560 * @credits: Number of credits to to allocate
562 * Set initial credits value to be used for ingress shared buffering.
564 int tb_port_set_initial_credits(struct tb_port *port, u32 credits)
569 ret = tb_port_read(port, &data, TB_CFG_PORT, 5, 1);
573 data &= ~TB_PORT_LCA_MASK;
574 data |= (credits << TB_PORT_LCA_SHIFT) & TB_PORT_LCA_MASK;
576 return tb_port_write(port, &data, TB_CFG_PORT, 5, 1);
580 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
582 * Return: Returns 0 on success or an error code on failure.
584 int tb_port_clear_counter(struct tb_port *port, int counter)
586 u32 zero[3] = { 0, 0, 0 };
587 tb_port_dbg(port, "clearing counter %d\n", counter);
588 return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
592 * tb_init_port() - initialize a port
594 * This is a helper method for tb_switch_alloc. Does not check or initialize
595 * any downstream switches.
597 * Return: Returns 0 on success or an error code on failure.
599 static int tb_init_port(struct tb_port *port)
604 res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8);
608 /* Port 0 is the switch itself and has no PHY. */
609 if (port->config.type == TB_TYPE_PORT && port->port != 0) {
610 cap = tb_port_find_cap(port, TB_PORT_CAP_PHY);
615 tb_port_WARN(port, "non switch port without a PHY\n");
616 } else if (port->port != 0) {
617 cap = tb_port_find_cap(port, TB_PORT_CAP_ADAP);
619 port->cap_adap = cap;
622 tb_dump_port(port->sw->tb, &port->config);
624 /* Control port does not need HopID allocation */
626 ida_init(&port->in_hopids);
627 ida_init(&port->out_hopids);
634 static int tb_port_alloc_hopid(struct tb_port *port, bool in, int min_hopid,
641 port_max_hopid = port->config.max_in_hop_id;
642 ida = &port->in_hopids;
644 port_max_hopid = port->config.max_out_hop_id;
645 ida = &port->out_hopids;
648 /* HopIDs 0-7 are reserved */
649 if (min_hopid < TB_PATH_MIN_HOPID)
650 min_hopid = TB_PATH_MIN_HOPID;
652 if (max_hopid < 0 || max_hopid > port_max_hopid)
653 max_hopid = port_max_hopid;
655 return ida_simple_get(ida, min_hopid, max_hopid + 1, GFP_KERNEL);
659 * tb_port_alloc_in_hopid() - Allocate input HopID from port
660 * @port: Port to allocate HopID for
661 * @min_hopid: Minimum acceptable input HopID
662 * @max_hopid: Maximum acceptable input HopID
664 * Return: HopID between @min_hopid and @max_hopid or negative errno in
667 int tb_port_alloc_in_hopid(struct tb_port *port, int min_hopid, int max_hopid)
669 return tb_port_alloc_hopid(port, true, min_hopid, max_hopid);
673 * tb_port_alloc_out_hopid() - Allocate output HopID from port
674 * @port: Port to allocate HopID for
675 * @min_hopid: Minimum acceptable output HopID
676 * @max_hopid: Maximum acceptable output HopID
678 * Return: HopID between @min_hopid and @max_hopid or negative errno in
681 int tb_port_alloc_out_hopid(struct tb_port *port, int min_hopid, int max_hopid)
683 return tb_port_alloc_hopid(port, false, min_hopid, max_hopid);
687 * tb_port_release_in_hopid() - Release allocated input HopID from port
688 * @port: Port whose HopID to release
689 * @hopid: HopID to release
691 void tb_port_release_in_hopid(struct tb_port *port, int hopid)
693 ida_simple_remove(&port->in_hopids, hopid);
697 * tb_port_release_out_hopid() - Release allocated output HopID from port
698 * @port: Port whose HopID to release
699 * @hopid: HopID to release
701 void tb_port_release_out_hopid(struct tb_port *port, int hopid)
703 ida_simple_remove(&port->out_hopids, hopid);
707 * tb_next_port_on_path() - Return next port for given port on a path
708 * @start: Start port of the walk
709 * @end: End port of the walk
710 * @prev: Previous port (%NULL if this is the first)
712 * This function can be used to walk from one port to another if they
713 * are connected through zero or more switches. If the @prev is dual
714 * link port, the function follows that link and returns another end on
717 * If the @end port has been reached, return %NULL.
719 * Domain tb->lock must be held when this function is called.
721 struct tb_port *tb_next_port_on_path(struct tb_port *start, struct tb_port *end,
722 struct tb_port *prev)
724 struct tb_port *next;
729 if (prev->sw == end->sw) {
735 if (start->sw->config.depth < end->sw->config.depth) {
737 prev->remote->sw->config.depth > prev->sw->config.depth)
740 next = tb_port_at(tb_route(end->sw), prev->sw);
742 if (tb_is_upstream_port(prev)) {
745 next = tb_upstream_port(prev->sw);
747 * Keep the same link if prev and next are both
750 if (next->dual_link_port &&
751 next->link_nr != prev->link_nr) {
752 next = next->dual_link_port;
761 * tb_port_is_enabled() - Is the adapter port enabled
762 * @port: Port to check
764 bool tb_port_is_enabled(struct tb_port *port)
766 switch (port->config.type) {
767 case TB_TYPE_PCIE_UP:
768 case TB_TYPE_PCIE_DOWN:
769 return tb_pci_port_is_enabled(port);
771 case TB_TYPE_DP_HDMI_IN:
772 case TB_TYPE_DP_HDMI_OUT:
773 return tb_dp_port_is_enabled(port);
781 * tb_pci_port_is_enabled() - Is the PCIe adapter port enabled
782 * @port: PCIe port to check
784 bool tb_pci_port_is_enabled(struct tb_port *port)
788 if (tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap, 1))
791 return !!(data & TB_PCI_EN);
795 * tb_pci_port_enable() - Enable PCIe adapter port
796 * @port: PCIe port to enable
797 * @enable: Enable/disable the PCIe adapter
799 int tb_pci_port_enable(struct tb_port *port, bool enable)
801 u32 word = enable ? TB_PCI_EN : 0x0;
804 return tb_port_write(port, &word, TB_CFG_PORT, port->cap_adap, 1);
808 * tb_dp_port_hpd_is_active() - Is HPD already active
809 * @port: DP out port to check
811 * Checks if the DP OUT adapter port has HDP bit already set.
813 int tb_dp_port_hpd_is_active(struct tb_port *port)
818 ret = tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap + 2, 1);
822 return !!(data & TB_DP_HDP);
826 * tb_dp_port_hpd_clear() - Clear HPD from DP IN port
827 * @port: Port to clear HPD
829 * If the DP IN port has HDP set, this function can be used to clear it.
831 int tb_dp_port_hpd_clear(struct tb_port *port)
836 ret = tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap + 3, 1);
841 return tb_port_write(port, &data, TB_CFG_PORT, port->cap_adap + 3, 1);
845 * tb_dp_port_set_hops() - Set video/aux Hop IDs for DP port
846 * @port: DP IN/OUT port to set hops
847 * @video: Video Hop ID
848 * @aux_tx: AUX TX Hop ID
849 * @aux_rx: AUX RX Hop ID
851 * Programs specified Hop IDs for DP IN/OUT port.
853 int tb_dp_port_set_hops(struct tb_port *port, unsigned int video,
854 unsigned int aux_tx, unsigned int aux_rx)
859 ret = tb_port_read(port, data, TB_CFG_PORT, port->cap_adap,
864 data[0] &= ~TB_DP_VIDEO_HOPID_MASK;
865 data[1] &= ~(TB_DP_AUX_RX_HOPID_MASK | TB_DP_AUX_TX_HOPID_MASK);
867 data[0] |= (video << TB_DP_VIDEO_HOPID_SHIFT) & TB_DP_VIDEO_HOPID_MASK;
868 data[1] |= aux_tx & TB_DP_AUX_TX_HOPID_MASK;
869 data[1] |= (aux_rx << TB_DP_AUX_RX_HOPID_SHIFT) & TB_DP_AUX_RX_HOPID_MASK;
871 return tb_port_write(port, data, TB_CFG_PORT, port->cap_adap,
876 * tb_dp_port_is_enabled() - Is DP adapter port enabled
877 * @port: DP adapter port to check
879 bool tb_dp_port_is_enabled(struct tb_port *port)
883 if (tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap, 1))
886 return !!(data & (TB_DP_VIDEO_EN | TB_DP_AUX_EN));
890 * tb_dp_port_enable() - Enables/disables DP paths of a port
891 * @port: DP IN/OUT port
892 * @enable: Enable/disable DP path
894 * Once Hop IDs are programmed DP paths can be enabled or disabled by
895 * calling this function.
897 int tb_dp_port_enable(struct tb_port *port, bool enable)
902 ret = tb_port_read(port, &data, TB_CFG_PORT, port->cap_adap, 1);
907 data |= TB_DP_VIDEO_EN | TB_DP_AUX_EN;
909 data &= ~(TB_DP_VIDEO_EN | TB_DP_AUX_EN);
911 return tb_port_write(port, &data, TB_CFG_PORT, port->cap_adap, 1);
914 /* switch utility functions */
916 static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw)
918 tb_dbg(tb, " Switch: %x:%x (Revision: %d, TB Version: %d)\n",
919 sw->vendor_id, sw->device_id, sw->revision,
920 sw->thunderbolt_version);
921 tb_dbg(tb, " Max Port Number: %d\n", sw->max_port_number);
922 tb_dbg(tb, " Config:\n");
924 " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
925 sw->upstream_port_number, sw->depth,
926 (((u64) sw->route_hi) << 32) | sw->route_lo,
927 sw->enabled, sw->plug_events_delay);
928 tb_dbg(tb, " unknown1: %#x unknown4: %#x\n",
929 sw->__unknown1, sw->__unknown4);
933 * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
935 * Return: Returns 0 on success or an error code on failure.
937 int tb_switch_reset(struct tb *tb, u64 route)
939 struct tb_cfg_result res;
940 struct tb_regs_switch_header header = {
941 header.route_hi = route >> 32,
942 header.route_lo = route,
943 header.enabled = true,
945 tb_dbg(tb, "resetting switch at %llx\n", route);
946 res.err = tb_cfg_write(tb->ctl, ((u32 *) &header) + 2, route,
950 res = tb_cfg_reset(tb->ctl, route, TB_CFG_DEFAULT_TIMEOUT);
957 * tb_plug_events_active() - enable/disable plug events on a switch
959 * Also configures a sane plug_events_delay of 255ms.
961 * Return: Returns 0 on success or an error code on failure.
963 static int tb_plug_events_active(struct tb_switch *sw, bool active)
968 if (!sw->config.enabled)
971 sw->config.plug_events_delay = 0xff;
972 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1);
976 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
981 data = data & 0xFFFFFF83;
982 switch (sw->config.device_id) {
983 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
984 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
985 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
993 return tb_sw_write(sw, &data, TB_CFG_SWITCH,
994 sw->cap_plug_events + 1, 1);
997 static ssize_t authorized_show(struct device *dev,
998 struct device_attribute *attr,
1001 struct tb_switch *sw = tb_to_switch(dev);
1003 return sprintf(buf, "%u\n", sw->authorized);
1006 static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
1010 if (!mutex_trylock(&sw->tb->lock))
1011 return restart_syscall();
1017 * Make sure there is no PCIe rescan ongoing when a new PCIe
1018 * tunnel is created. Otherwise the PCIe rescan code might find
1019 * the new tunnel too early.
1021 pci_lock_rescan_remove();
1022 pm_runtime_get_sync(&sw->dev);
1025 /* Approve switch */
1028 ret = tb_domain_approve_switch_key(sw->tb, sw);
1030 ret = tb_domain_approve_switch(sw->tb, sw);
1033 /* Challenge switch */
1036 ret = tb_domain_challenge_switch_key(sw->tb, sw);
1043 pm_runtime_mark_last_busy(&sw->dev);
1044 pm_runtime_put_autosuspend(&sw->dev);
1045 pci_unlock_rescan_remove();
1048 sw->authorized = val;
1049 /* Notify status change to the userspace */
1050 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
1054 mutex_unlock(&sw->tb->lock);
1058 static ssize_t authorized_store(struct device *dev,
1059 struct device_attribute *attr,
1060 const char *buf, size_t count)
1062 struct tb_switch *sw = tb_to_switch(dev);
1066 ret = kstrtouint(buf, 0, &val);
1072 ret = tb_switch_set_authorized(sw, val);
1074 return ret ? ret : count;
1076 static DEVICE_ATTR_RW(authorized);
1078 static ssize_t boot_show(struct device *dev, struct device_attribute *attr,
1081 struct tb_switch *sw = tb_to_switch(dev);
1083 return sprintf(buf, "%u\n", sw->boot);
1085 static DEVICE_ATTR_RO(boot);
1087 static ssize_t device_show(struct device *dev, struct device_attribute *attr,
1090 struct tb_switch *sw = tb_to_switch(dev);
1092 return sprintf(buf, "%#x\n", sw->device);
1094 static DEVICE_ATTR_RO(device);
1097 device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1099 struct tb_switch *sw = tb_to_switch(dev);
1101 return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : "");
1103 static DEVICE_ATTR_RO(device_name);
1105 static ssize_t key_show(struct device *dev, struct device_attribute *attr,
1108 struct tb_switch *sw = tb_to_switch(dev);
1111 if (!mutex_trylock(&sw->tb->lock))
1112 return restart_syscall();
1115 ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key);
1117 ret = sprintf(buf, "\n");
1119 mutex_unlock(&sw->tb->lock);
1123 static ssize_t key_store(struct device *dev, struct device_attribute *attr,
1124 const char *buf, size_t count)
1126 struct tb_switch *sw = tb_to_switch(dev);
1127 u8 key[TB_SWITCH_KEY_SIZE];
1128 ssize_t ret = count;
1131 if (!strcmp(buf, "\n"))
1133 else if (hex2bin(key, buf, sizeof(key)))
1136 if (!mutex_trylock(&sw->tb->lock))
1137 return restart_syscall();
1139 if (sw->authorized) {
1146 sw->key = kmemdup(key, sizeof(key), GFP_KERNEL);
1152 mutex_unlock(&sw->tb->lock);
1155 static DEVICE_ATTR(key, 0600, key_show, key_store);
1157 static void nvm_authenticate_start(struct tb_switch *sw)
1159 struct pci_dev *root_port;
1162 * During host router NVM upgrade we should not allow root port to
1163 * go into D3cold because some root ports cannot trigger PME
1164 * itself. To be on the safe side keep the root port in D0 during
1165 * the whole upgrade process.
1167 root_port = pci_find_pcie_root_port(sw->tb->nhi->pdev);
1169 pm_runtime_get_noresume(&root_port->dev);
1172 static void nvm_authenticate_complete(struct tb_switch *sw)
1174 struct pci_dev *root_port;
1176 root_port = pci_find_pcie_root_port(sw->tb->nhi->pdev);
1178 pm_runtime_put(&root_port->dev);
1181 static ssize_t nvm_authenticate_show(struct device *dev,
1182 struct device_attribute *attr, char *buf)
1184 struct tb_switch *sw = tb_to_switch(dev);
1187 nvm_get_auth_status(sw, &status);
1188 return sprintf(buf, "%#x\n", status);
1191 static ssize_t nvm_authenticate_store(struct device *dev,
1192 struct device_attribute *attr, const char *buf, size_t count)
1194 struct tb_switch *sw = tb_to_switch(dev);
1198 if (!mutex_trylock(&sw->tb->lock))
1199 return restart_syscall();
1201 /* If NVMem devices are not yet added */
1207 ret = kstrtobool(buf, &val);
1211 /* Always clear the authentication status */
1212 nvm_clear_auth_status(sw);
1215 if (!sw->nvm->buf) {
1220 pm_runtime_get_sync(&sw->dev);
1221 ret = nvm_validate_and_write(sw);
1223 pm_runtime_mark_last_busy(&sw->dev);
1224 pm_runtime_put_autosuspend(&sw->dev);
1228 sw->nvm->authenticating = true;
1230 if (!tb_route(sw)) {
1232 * Keep root port from suspending as long as the
1233 * NVM upgrade process is running.
1235 nvm_authenticate_start(sw);
1236 ret = nvm_authenticate_host(sw);
1238 nvm_authenticate_complete(sw);
1240 ret = nvm_authenticate_device(sw);
1242 pm_runtime_mark_last_busy(&sw->dev);
1243 pm_runtime_put_autosuspend(&sw->dev);
1247 mutex_unlock(&sw->tb->lock);
1253 static DEVICE_ATTR_RW(nvm_authenticate);
1255 static ssize_t nvm_version_show(struct device *dev,
1256 struct device_attribute *attr, char *buf)
1258 struct tb_switch *sw = tb_to_switch(dev);
1261 if (!mutex_trylock(&sw->tb->lock))
1262 return restart_syscall();
1269 ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);
1271 mutex_unlock(&sw->tb->lock);
1275 static DEVICE_ATTR_RO(nvm_version);
1277 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
1280 struct tb_switch *sw = tb_to_switch(dev);
1282 return sprintf(buf, "%#x\n", sw->vendor);
1284 static DEVICE_ATTR_RO(vendor);
1287 vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1289 struct tb_switch *sw = tb_to_switch(dev);
1291 return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : "");
1293 static DEVICE_ATTR_RO(vendor_name);
1295 static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
1298 struct tb_switch *sw = tb_to_switch(dev);
1300 return sprintf(buf, "%pUb\n", sw->uuid);
1302 static DEVICE_ATTR_RO(unique_id);
1304 static struct attribute *switch_attrs[] = {
1305 &dev_attr_authorized.attr,
1306 &dev_attr_boot.attr,
1307 &dev_attr_device.attr,
1308 &dev_attr_device_name.attr,
1310 &dev_attr_nvm_authenticate.attr,
1311 &dev_attr_nvm_version.attr,
1312 &dev_attr_vendor.attr,
1313 &dev_attr_vendor_name.attr,
1314 &dev_attr_unique_id.attr,
1318 static umode_t switch_attr_is_visible(struct kobject *kobj,
1319 struct attribute *attr, int n)
1321 struct device *dev = container_of(kobj, struct device, kobj);
1322 struct tb_switch *sw = tb_to_switch(dev);
1324 if (attr == &dev_attr_key.attr) {
1326 sw->tb->security_level == TB_SECURITY_SECURE &&
1327 sw->security_level == TB_SECURITY_SECURE)
1330 } else if (attr == &dev_attr_nvm_authenticate.attr ||
1331 attr == &dev_attr_nvm_version.attr) {
1335 } else if (attr == &dev_attr_boot.attr) {
1341 return sw->safe_mode ? 0 : attr->mode;
1344 static struct attribute_group switch_group = {
1345 .is_visible = switch_attr_is_visible,
1346 .attrs = switch_attrs,
1349 static const struct attribute_group *switch_groups[] = {
1354 static void tb_switch_release(struct device *dev)
1356 struct tb_switch *sw = tb_to_switch(dev);
1359 dma_port_free(sw->dma_port);
1361 for (i = 1; i <= sw->config.max_port_number; i++) {
1362 if (!sw->ports[i].disabled) {
1363 ida_destroy(&sw->ports[i].in_hopids);
1364 ida_destroy(&sw->ports[i].out_hopids);
1369 kfree(sw->device_name);
1370 kfree(sw->vendor_name);
1378 * Currently only need to provide the callbacks. Everything else is handled
1379 * in the connection manager.
1381 static int __maybe_unused tb_switch_runtime_suspend(struct device *dev)
1386 static int __maybe_unused tb_switch_runtime_resume(struct device *dev)
1391 static const struct dev_pm_ops tb_switch_pm_ops = {
1392 SET_RUNTIME_PM_OPS(tb_switch_runtime_suspend, tb_switch_runtime_resume,
1396 struct device_type tb_switch_type = {
1397 .name = "thunderbolt_device",
1398 .release = tb_switch_release,
1399 .pm = &tb_switch_pm_ops,
1402 static int tb_switch_get_generation(struct tb_switch *sw)
1404 switch (sw->config.device_id) {
1405 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
1406 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
1407 case PCI_DEVICE_ID_INTEL_LIGHT_PEAK:
1408 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
1409 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
1410 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
1411 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE:
1412 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE:
1415 case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE:
1416 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
1417 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
1420 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
1421 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
1422 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
1423 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
1424 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
1425 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
1426 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
1427 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
1432 * For unknown switches assume generation to be 1 to be
1435 tb_sw_warn(sw, "unsupported switch device id %#x\n",
1436 sw->config.device_id);
1442 * tb_switch_alloc() - allocate a switch
1443 * @tb: Pointer to the owning domain
1444 * @parent: Parent device for this switch
1445 * @route: Route string for this switch
1447 * Allocates and initializes a switch. Will not upload configuration to
1448 * the switch. For that you need to call tb_switch_configure()
1449 * separately. The returned switch should be released by calling
1452 * Return: Pointer to the allocated switch or ERR_PTR() in case of
1455 struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
1458 struct tb_switch *sw;
1462 /* Make sure we do not exceed maximum topology limit */
1463 depth = tb_route_length(route);
1464 if (depth > TB_SWITCH_MAX_DEPTH)
1465 return ERR_PTR(-EADDRNOTAVAIL);
1467 upstream_port = tb_cfg_get_upstream_port(tb->ctl, route);
1468 if (upstream_port < 0)
1469 return ERR_PTR(upstream_port);
1471 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
1473 return ERR_PTR(-ENOMEM);
1476 ret = tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5);
1478 goto err_free_sw_ports;
1480 tb_dbg(tb, "current switch config:\n");
1481 tb_dump_switch(tb, &sw->config);
1483 /* configure switch */
1484 sw->config.upstream_port_number = upstream_port;
1485 sw->config.depth = depth;
1486 sw->config.route_hi = upper_32_bits(route);
1487 sw->config.route_lo = lower_32_bits(route);
1488 sw->config.enabled = 0;
1490 /* initialize ports */
1491 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports),
1495 goto err_free_sw_ports;
1498 for (i = 0; i <= sw->config.max_port_number; i++) {
1499 /* minimum setup for tb_find_cap and tb_drom_read to work */
1500 sw->ports[i].sw = sw;
1501 sw->ports[i].port = i;
1504 sw->generation = tb_switch_get_generation(sw);
1506 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS);
1508 tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
1509 goto err_free_sw_ports;
1511 sw->cap_plug_events = ret;
1513 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER);
1517 /* Root switch is always authorized */
1519 sw->authorized = true;
1521 device_initialize(&sw->dev);
1522 sw->dev.parent = parent;
1523 sw->dev.bus = &tb_bus_type;
1524 sw->dev.type = &tb_switch_type;
1525 sw->dev.groups = switch_groups;
1526 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
1534 return ERR_PTR(ret);
1538 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
1539 * @tb: Pointer to the owning domain
1540 * @parent: Parent device for this switch
1541 * @route: Route string for this switch
1543 * This creates a switch in safe mode. This means the switch pretty much
1544 * lacks all capabilities except DMA configuration port before it is
1545 * flashed with a valid NVM firmware.
1547 * The returned switch must be released by calling tb_switch_put().
1549 * Return: Pointer to the allocated switch or ERR_PTR() in case of failure
1552 tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route)
1554 struct tb_switch *sw;
1556 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
1558 return ERR_PTR(-ENOMEM);
1561 sw->config.depth = tb_route_length(route);
1562 sw->config.route_hi = upper_32_bits(route);
1563 sw->config.route_lo = lower_32_bits(route);
1564 sw->safe_mode = true;
1566 device_initialize(&sw->dev);
1567 sw->dev.parent = parent;
1568 sw->dev.bus = &tb_bus_type;
1569 sw->dev.type = &tb_switch_type;
1570 sw->dev.groups = switch_groups;
1571 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
1577 * tb_switch_configure() - Uploads configuration to the switch
1578 * @sw: Switch to configure
1580 * Call this function before the switch is added to the system. It will
1581 * upload configuration to the switch and makes it available for the
1582 * connection manager to use.
1584 * Return: %0 in case of success and negative errno in case of failure
1586 int tb_switch_configure(struct tb_switch *sw)
1588 struct tb *tb = sw->tb;
1592 route = tb_route(sw);
1593 tb_dbg(tb, "initializing Switch at %#llx (depth: %d, up port: %d)\n",
1594 route, tb_route_length(route), sw->config.upstream_port_number);
1596 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL)
1597 tb_sw_warn(sw, "unknown switch vendor id %#x\n",
1598 sw->config.vendor_id);
1600 sw->config.enabled = 1;
1602 /* upload configuration */
1603 ret = tb_sw_write(sw, 1 + (u32 *)&sw->config, TB_CFG_SWITCH, 1, 3);
1607 ret = tb_lc_configure_link(sw);
1611 return tb_plug_events_active(sw, true);
1614 static int tb_switch_set_uuid(struct tb_switch *sw)
1623 * The newer controllers include fused UUID as part of link
1624 * controller specific registers
1626 ret = tb_lc_read_uuid(sw, uuid);
1629 * ICM generates UUID based on UID and fills the upper
1630 * two words with ones. This is not strictly following
1631 * UUID format but we want to be compatible with it so
1632 * we do the same here.
1634 uuid[0] = sw->uid & 0xffffffff;
1635 uuid[1] = (sw->uid >> 32) & 0xffffffff;
1636 uuid[2] = 0xffffffff;
1637 uuid[3] = 0xffffffff;
1640 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
1646 static int tb_switch_add_dma_port(struct tb_switch *sw)
1651 switch (sw->generation) {
1656 /* Only root switch can be upgraded */
1663 * DMA port is the only thing available when the switch
1671 if (sw->no_nvm_upgrade)
1674 sw->dma_port = dma_port_alloc(sw);
1679 * Check status of the previous flash authentication. If there
1680 * is one we need to power cycle the switch in any case to make
1681 * it functional again.
1683 ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
1687 /* Now we can allow root port to suspend again */
1689 nvm_authenticate_complete(sw);
1692 tb_sw_info(sw, "switch flash authentication failed\n");
1693 ret = tb_switch_set_uuid(sw);
1696 nvm_set_auth_status(sw, status);
1699 tb_sw_info(sw, "power cycling the switch now\n");
1700 dma_port_power_cycle(sw->dma_port);
1703 * We return error here which causes the switch adding failure.
1704 * It should appear back after power cycle is complete.
1710 * tb_switch_add() - Add a switch to the domain
1711 * @sw: Switch to add
1713 * This is the last step in adding switch to the domain. It will read
1714 * identification information from DROM and initializes ports so that
1715 * they can be used to connect other switches. The switch will be
1716 * exposed to the userspace when this function successfully returns. To
1717 * remove and release the switch, call tb_switch_remove().
1719 * Return: %0 in case of success and negative errno in case of failure
1721 int tb_switch_add(struct tb_switch *sw)
1726 * Initialize DMA control port now before we read DROM. Recent
1727 * host controllers have more complete DROM on NVM that includes
1728 * vendor and model identification strings which we then expose
1729 * to the userspace. NVM can be accessed through DMA
1730 * configuration based mailbox.
1732 ret = tb_switch_add_dma_port(sw);
1736 if (!sw->safe_mode) {
1738 ret = tb_drom_read(sw);
1740 tb_sw_warn(sw, "tb_eeprom_read_rom failed\n");
1743 tb_sw_dbg(sw, "uid: %#llx\n", sw->uid);
1745 ret = tb_switch_set_uuid(sw);
1749 for (i = 0; i <= sw->config.max_port_number; i++) {
1750 if (sw->ports[i].disabled) {
1751 tb_port_dbg(&sw->ports[i], "disabled by eeprom\n");
1754 ret = tb_init_port(&sw->ports[i]);
1760 ret = device_add(&sw->dev);
1765 dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n",
1766 sw->vendor, sw->device);
1767 if (sw->vendor_name && sw->device_name)
1768 dev_info(&sw->dev, "%s %s\n", sw->vendor_name,
1772 ret = tb_switch_nvm_add(sw);
1774 device_del(&sw->dev);
1778 pm_runtime_set_active(&sw->dev);
1780 pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY);
1781 pm_runtime_use_autosuspend(&sw->dev);
1782 pm_runtime_mark_last_busy(&sw->dev);
1783 pm_runtime_enable(&sw->dev);
1784 pm_request_autosuspend(&sw->dev);
1791 * tb_switch_remove() - Remove and release a switch
1792 * @sw: Switch to remove
1794 * This will remove the switch from the domain and release it after last
1795 * reference count drops to zero. If there are switches connected below
1796 * this switch, they will be removed as well.
1798 void tb_switch_remove(struct tb_switch *sw)
1803 pm_runtime_get_sync(&sw->dev);
1804 pm_runtime_disable(&sw->dev);
1807 /* port 0 is the switch itself and never has a remote */
1808 for (i = 1; i <= sw->config.max_port_number; i++) {
1809 if (tb_port_has_remote(&sw->ports[i])) {
1810 tb_switch_remove(sw->ports[i].remote->sw);
1811 sw->ports[i].remote = NULL;
1812 } else if (sw->ports[i].xdomain) {
1813 tb_xdomain_remove(sw->ports[i].xdomain);
1814 sw->ports[i].xdomain = NULL;
1818 if (!sw->is_unplugged)
1819 tb_plug_events_active(sw, false);
1820 tb_lc_unconfigure_link(sw);
1822 tb_switch_nvm_remove(sw);
1825 dev_info(&sw->dev, "device disconnected\n");
1826 device_unregister(&sw->dev);
1830 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
1832 void tb_sw_set_unplugged(struct tb_switch *sw)
1835 if (sw == sw->tb->root_switch) {
1836 tb_sw_WARN(sw, "cannot unplug root switch\n");
1839 if (sw->is_unplugged) {
1840 tb_sw_WARN(sw, "is_unplugged already set\n");
1843 sw->is_unplugged = true;
1844 for (i = 0; i <= sw->config.max_port_number; i++) {
1845 if (tb_port_has_remote(&sw->ports[i]))
1846 tb_sw_set_unplugged(sw->ports[i].remote->sw);
1847 else if (sw->ports[i].xdomain)
1848 sw->ports[i].xdomain->is_unplugged = true;
1852 int tb_switch_resume(struct tb_switch *sw)
1855 tb_sw_dbg(sw, "resuming switch\n");
1858 * Check for UID of the connected switches except for root
1859 * switch which we assume cannot be removed.
1865 * Check first that we can still read the switch config
1866 * space. It may be that there is now another domain
1869 err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw));
1871 tb_sw_info(sw, "switch not present anymore\n");
1875 err = tb_drom_read_uid_only(sw, &uid);
1877 tb_sw_warn(sw, "uid read failed\n");
1880 if (sw->uid != uid) {
1882 "changed while suspended (uid %#llx -> %#llx)\n",
1888 /* upload configuration */
1889 err = tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3);
1893 err = tb_lc_configure_link(sw);
1897 err = tb_plug_events_active(sw, true);
1901 /* check for surviving downstream switches */
1902 for (i = 1; i <= sw->config.max_port_number; i++) {
1903 struct tb_port *port = &sw->ports[i];
1905 if (!tb_port_has_remote(port) && !port->xdomain)
1908 if (tb_wait_for_port(port, true) <= 0) {
1910 "lost during suspend, disconnecting\n");
1911 if (tb_port_has_remote(port))
1912 tb_sw_set_unplugged(port->remote->sw);
1913 else if (port->xdomain)
1914 port->xdomain->is_unplugged = true;
1915 } else if (tb_port_has_remote(port)) {
1916 if (tb_switch_resume(port->remote->sw)) {
1918 "lost during suspend, disconnecting\n");
1919 tb_sw_set_unplugged(port->remote->sw);
1926 void tb_switch_suspend(struct tb_switch *sw)
1929 err = tb_plug_events_active(sw, false);
1933 for (i = 1; i <= sw->config.max_port_number; i++) {
1934 if (tb_port_has_remote(&sw->ports[i]))
1935 tb_switch_suspend(sw->ports[i].remote->sw);
1938 tb_lc_set_sleep(sw);
1941 struct tb_sw_lookup {
1949 static int tb_switch_match(struct device *dev, void *data)
1951 struct tb_switch *sw = tb_to_switch(dev);
1952 struct tb_sw_lookup *lookup = data;
1956 if (sw->tb != lookup->tb)
1960 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid));
1962 if (lookup->route) {
1963 return sw->config.route_lo == lower_32_bits(lookup->route) &&
1964 sw->config.route_hi == upper_32_bits(lookup->route);
1967 /* Root switch is matched only by depth */
1971 return sw->link == lookup->link && sw->depth == lookup->depth;
1975 * tb_switch_find_by_link_depth() - Find switch by link and depth
1976 * @tb: Domain the switch belongs
1977 * @link: Link number the switch is connected
1978 * @depth: Depth of the switch in link
1980 * Returned switch has reference count increased so the caller needs to
1981 * call tb_switch_put() when done with the switch.
1983 struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth)
1985 struct tb_sw_lookup lookup;
1988 memset(&lookup, 0, sizeof(lookup));
1991 lookup.depth = depth;
1993 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
1995 return tb_to_switch(dev);
2001 * tb_switch_find_by_uuid() - Find switch by UUID
2002 * @tb: Domain the switch belongs
2003 * @uuid: UUID to look for
2005 * Returned switch has reference count increased so the caller needs to
2006 * call tb_switch_put() when done with the switch.
2008 struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid)
2010 struct tb_sw_lookup lookup;
2013 memset(&lookup, 0, sizeof(lookup));
2017 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
2019 return tb_to_switch(dev);
2025 * tb_switch_find_by_route() - Find switch by route string
2026 * @tb: Domain the switch belongs
2027 * @route: Route string to look for
2029 * Returned switch has reference count increased so the caller needs to
2030 * call tb_switch_put() when done with the switch.
2032 struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route)
2034 struct tb_sw_lookup lookup;
2038 return tb_switch_get(tb->root_switch);
2040 memset(&lookup, 0, sizeof(lookup));
2042 lookup.route = route;
2044 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
2046 return tb_to_switch(dev);
2051 void tb_switch_exit(void)
2053 ida_destroy(&nvm_ida);