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) {
181 ret = tb_domain_disconnect_all_paths(sw->tb);
185 * The host controller goes away pretty soon after this if
186 * everything goes well so getting timeout is expected.
188 ret = dma_port_flash_update_auth(sw->dma_port);
189 if (!ret || ret == -ETIMEDOUT)
193 * Any error from update auth operation requires power
194 * cycling of the host router.
196 tb_sw_warn(sw, "failed to authenticate NVM, power cycling\n");
197 if (dma_port_flash_update_auth_status(sw->dma_port, &status) > 0)
198 nvm_set_auth_status(sw, status);
202 * From safe mode we can get out by just power cycling the
205 dma_port_power_cycle(sw->dma_port);
209 static int nvm_authenticate_device(struct tb_switch *sw)
211 int ret, retries = 10;
213 ret = dma_port_flash_update_auth(sw->dma_port);
219 /* Power cycle is required */
226 * Poll here for the authentication status. It takes some time
227 * for the device to respond (we get timeout for a while). Once
228 * we get response the device needs to be power cycled in order
229 * to the new NVM to be taken into use.
234 ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
235 if (ret < 0 && ret != -ETIMEDOUT)
239 tb_sw_warn(sw, "failed to authenticate NVM\n");
240 nvm_set_auth_status(sw, status);
243 tb_sw_info(sw, "power cycling the switch now\n");
244 dma_port_power_cycle(sw->dma_port);
254 static int tb_switch_nvm_read(void *priv, unsigned int offset, void *val,
257 struct tb_switch *sw = priv;
260 pm_runtime_get_sync(&sw->dev);
262 if (!mutex_trylock(&sw->tb->lock)) {
263 ret = restart_syscall();
267 ret = dma_port_flash_read(sw->dma_port, offset, val, bytes);
268 mutex_unlock(&sw->tb->lock);
271 pm_runtime_mark_last_busy(&sw->dev);
272 pm_runtime_put_autosuspend(&sw->dev);
277 static int tb_switch_nvm_write(void *priv, unsigned int offset, void *val,
280 struct tb_switch *sw = priv;
283 if (!mutex_trylock(&sw->tb->lock))
284 return restart_syscall();
287 * Since writing the NVM image might require some special steps,
288 * for example when CSS headers are written, we cache the image
289 * locally here and handle the special cases when the user asks
290 * us to authenticate the image.
293 sw->nvm->buf = vmalloc(NVM_MAX_SIZE);
300 sw->nvm->buf_data_size = offset + bytes;
301 memcpy(sw->nvm->buf + offset, val, bytes);
304 mutex_unlock(&sw->tb->lock);
309 static struct nvmem_device *register_nvmem(struct tb_switch *sw, int id,
310 size_t size, bool active)
312 struct nvmem_config config;
314 memset(&config, 0, sizeof(config));
317 config.name = "nvm_active";
318 config.reg_read = tb_switch_nvm_read;
319 config.read_only = true;
321 config.name = "nvm_non_active";
322 config.reg_write = tb_switch_nvm_write;
323 config.root_only = true;
328 config.word_size = 4;
330 config.dev = &sw->dev;
331 config.owner = THIS_MODULE;
334 return nvmem_register(&config);
337 static int tb_switch_nvm_add(struct tb_switch *sw)
339 struct nvmem_device *nvm_dev;
340 struct tb_switch_nvm *nvm;
347 nvm = kzalloc(sizeof(*nvm), GFP_KERNEL);
351 nvm->id = ida_simple_get(&nvm_ida, 0, 0, GFP_KERNEL);
354 * If the switch is in safe-mode the only accessible portion of
355 * the NVM is the non-active one where userspace is expected to
356 * write new functional NVM.
358 if (!sw->safe_mode) {
359 u32 nvm_size, hdr_size;
361 ret = dma_port_flash_read(sw->dma_port, NVM_FLASH_SIZE, &val,
366 hdr_size = sw->generation < 3 ? SZ_8K : SZ_16K;
367 nvm_size = (SZ_1M << (val & 7)) / 8;
368 nvm_size = (nvm_size - hdr_size) / 2;
370 ret = dma_port_flash_read(sw->dma_port, NVM_VERSION, &val,
375 nvm->major = val >> 16;
376 nvm->minor = val >> 8;
378 nvm_dev = register_nvmem(sw, nvm->id, nvm_size, true);
379 if (IS_ERR(nvm_dev)) {
380 ret = PTR_ERR(nvm_dev);
383 nvm->active = nvm_dev;
386 if (!sw->no_nvm_upgrade) {
387 nvm_dev = register_nvmem(sw, nvm->id, NVM_MAX_SIZE, false);
388 if (IS_ERR(nvm_dev)) {
389 ret = PTR_ERR(nvm_dev);
392 nvm->non_active = nvm_dev;
400 nvmem_unregister(nvm->active);
402 ida_simple_remove(&nvm_ida, nvm->id);
408 static void tb_switch_nvm_remove(struct tb_switch *sw)
410 struct tb_switch_nvm *nvm;
418 /* Remove authentication status in case the switch is unplugged */
419 if (!nvm->authenticating)
420 nvm_clear_auth_status(sw);
423 nvmem_unregister(nvm->non_active);
425 nvmem_unregister(nvm->active);
426 ida_simple_remove(&nvm_ida, nvm->id);
431 /* port utility functions */
433 static const char *tb_port_type(struct tb_regs_port_header *port)
435 switch (port->type >> 16) {
437 switch ((u8) port->type) {
462 static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port)
465 " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n",
466 port->port_number, port->vendor_id, port->device_id,
467 port->revision, port->thunderbolt_version, tb_port_type(port),
469 tb_dbg(tb, " Max hop id (in/out): %d/%d\n",
470 port->max_in_hop_id, port->max_out_hop_id);
471 tb_dbg(tb, " Max counters: %d\n", port->max_counters);
472 tb_dbg(tb, " NFC Credits: %#x\n", port->nfc_credits);
476 * tb_port_state() - get connectedness state of a port
478 * The port must have a TB_CAP_PHY (i.e. it should be a real port).
480 * Return: Returns an enum tb_port_state on success or an error code on failure.
482 static int tb_port_state(struct tb_port *port)
484 struct tb_cap_phy phy;
486 if (port->cap_phy == 0) {
487 tb_port_WARN(port, "does not have a PHY\n");
490 res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2);
497 * tb_wait_for_port() - wait for a port to become ready
499 * Wait up to 1 second for a port to reach state TB_PORT_UP. If
500 * wait_if_unplugged is set then we also wait if the port is in state
501 * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
502 * switch resume). Otherwise we only wait if a device is registered but the link
503 * has not yet been established.
505 * Return: Returns an error code on failure. Returns 0 if the port is not
506 * connected or failed to reach state TB_PORT_UP within one second. Returns 1
507 * if the port is connected and in state TB_PORT_UP.
509 int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
513 if (!port->cap_phy) {
514 tb_port_WARN(port, "does not have PHY\n");
517 if (tb_is_upstream_port(port)) {
518 tb_port_WARN(port, "is the upstream port\n");
523 state = tb_port_state(port);
526 if (state == TB_PORT_DISABLED) {
527 tb_port_dbg(port, "is disabled (state: 0)\n");
530 if (state == TB_PORT_UNPLUGGED) {
531 if (wait_if_unplugged) {
532 /* used during resume */
534 "is unplugged (state: 7), retrying...\n");
538 tb_port_dbg(port, "is unplugged (state: 7)\n");
541 if (state == TB_PORT_UP) {
542 tb_port_dbg(port, "is connected, link is up (state: 2)\n");
547 * After plug-in the state is TB_PORT_CONNECTING. Give it some
551 "is connected, link is not up (state: %d), retrying...\n",
556 "failed to reach state TB_PORT_UP. Ignoring port...\n");
561 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
563 * Change the number of NFC credits allocated to @port by @credits. To remove
564 * NFC credits pass a negative amount of credits.
566 * Return: Returns 0 on success or an error code on failure.
568 int tb_port_add_nfc_credits(struct tb_port *port, int credits)
572 if (credits == 0 || port->sw->is_unplugged)
575 nfc_credits = port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK;
576 nfc_credits += credits;
578 tb_port_dbg(port, "adding %d NFC credits to %lu", credits,
579 port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK);
581 port->config.nfc_credits &= ~ADP_CS_4_NFC_BUFFERS_MASK;
582 port->config.nfc_credits |= nfc_credits;
584 return tb_port_write(port, &port->config.nfc_credits,
585 TB_CFG_PORT, ADP_CS_4, 1);
589 * tb_port_set_initial_credits() - Set initial port link credits allocated
590 * @port: Port to set the initial credits
591 * @credits: Number of credits to to allocate
593 * Set initial credits value to be used for ingress shared buffering.
595 int tb_port_set_initial_credits(struct tb_port *port, u32 credits)
600 ret = tb_port_read(port, &data, TB_CFG_PORT, ADP_CS_5, 1);
604 data &= ~ADP_CS_5_LCA_MASK;
605 data |= (credits << ADP_CS_5_LCA_SHIFT) & ADP_CS_5_LCA_MASK;
607 return tb_port_write(port, &data, TB_CFG_PORT, ADP_CS_5, 1);
611 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
613 * Return: Returns 0 on success or an error code on failure.
615 int tb_port_clear_counter(struct tb_port *port, int counter)
617 u32 zero[3] = { 0, 0, 0 };
618 tb_port_dbg(port, "clearing counter %d\n", counter);
619 return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
623 * tb_init_port() - initialize a port
625 * This is a helper method for tb_switch_alloc. Does not check or initialize
626 * any downstream switches.
628 * Return: Returns 0 on success or an error code on failure.
630 static int tb_init_port(struct tb_port *port)
635 res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8);
637 if (res == -ENODEV) {
638 tb_dbg(port->sw->tb, " Port %d: not implemented\n",
645 /* Port 0 is the switch itself and has no PHY. */
646 if (port->config.type == TB_TYPE_PORT && port->port != 0) {
647 cap = tb_port_find_cap(port, TB_PORT_CAP_PHY);
652 tb_port_WARN(port, "non switch port without a PHY\n");
653 } else if (port->port != 0) {
654 cap = tb_port_find_cap(port, TB_PORT_CAP_ADAP);
656 port->cap_adap = cap;
659 tb_dump_port(port->sw->tb, &port->config);
661 /* Control port does not need HopID allocation */
663 ida_init(&port->in_hopids);
664 ida_init(&port->out_hopids);
667 INIT_LIST_HEAD(&port->list);
672 static int tb_port_alloc_hopid(struct tb_port *port, bool in, int min_hopid,
679 port_max_hopid = port->config.max_in_hop_id;
680 ida = &port->in_hopids;
682 port_max_hopid = port->config.max_out_hop_id;
683 ida = &port->out_hopids;
686 /* HopIDs 0-7 are reserved */
687 if (min_hopid < TB_PATH_MIN_HOPID)
688 min_hopid = TB_PATH_MIN_HOPID;
690 if (max_hopid < 0 || max_hopid > port_max_hopid)
691 max_hopid = port_max_hopid;
693 return ida_simple_get(ida, min_hopid, max_hopid + 1, GFP_KERNEL);
697 * tb_port_alloc_in_hopid() - Allocate input HopID from port
698 * @port: Port to allocate HopID for
699 * @min_hopid: Minimum acceptable input HopID
700 * @max_hopid: Maximum acceptable input HopID
702 * Return: HopID between @min_hopid and @max_hopid or negative errno in
705 int tb_port_alloc_in_hopid(struct tb_port *port, int min_hopid, int max_hopid)
707 return tb_port_alloc_hopid(port, true, min_hopid, max_hopid);
711 * tb_port_alloc_out_hopid() - Allocate output HopID from port
712 * @port: Port to allocate HopID for
713 * @min_hopid: Minimum acceptable output HopID
714 * @max_hopid: Maximum acceptable output HopID
716 * Return: HopID between @min_hopid and @max_hopid or negative errno in
719 int tb_port_alloc_out_hopid(struct tb_port *port, int min_hopid, int max_hopid)
721 return tb_port_alloc_hopid(port, false, min_hopid, max_hopid);
725 * tb_port_release_in_hopid() - Release allocated input HopID from port
726 * @port: Port whose HopID to release
727 * @hopid: HopID to release
729 void tb_port_release_in_hopid(struct tb_port *port, int hopid)
731 ida_simple_remove(&port->in_hopids, hopid);
735 * tb_port_release_out_hopid() - Release allocated output HopID from port
736 * @port: Port whose HopID to release
737 * @hopid: HopID to release
739 void tb_port_release_out_hopid(struct tb_port *port, int hopid)
741 ida_simple_remove(&port->out_hopids, hopid);
745 * tb_next_port_on_path() - Return next port for given port on a path
746 * @start: Start port of the walk
747 * @end: End port of the walk
748 * @prev: Previous port (%NULL if this is the first)
750 * This function can be used to walk from one port to another if they
751 * are connected through zero or more switches. If the @prev is dual
752 * link port, the function follows that link and returns another end on
755 * If the @end port has been reached, return %NULL.
757 * Domain tb->lock must be held when this function is called.
759 struct tb_port *tb_next_port_on_path(struct tb_port *start, struct tb_port *end,
760 struct tb_port *prev)
762 struct tb_port *next;
767 if (prev->sw == end->sw) {
773 if (start->sw->config.depth < end->sw->config.depth) {
775 prev->remote->sw->config.depth > prev->sw->config.depth)
778 next = tb_port_at(tb_route(end->sw), prev->sw);
780 if (tb_is_upstream_port(prev)) {
783 next = tb_upstream_port(prev->sw);
785 * Keep the same link if prev and next are both
788 if (next->dual_link_port &&
789 next->link_nr != prev->link_nr) {
790 next = next->dual_link_port;
798 static int tb_port_get_link_speed(struct tb_port *port)
806 ret = tb_port_read(port, &val, TB_CFG_PORT,
807 port->cap_phy + LANE_ADP_CS_1, 1);
811 speed = (val & LANE_ADP_CS_1_CURRENT_SPEED_MASK) >>
812 LANE_ADP_CS_1_CURRENT_SPEED_SHIFT;
813 return speed == LANE_ADP_CS_1_CURRENT_SPEED_GEN3 ? 20 : 10;
816 static int tb_port_get_link_width(struct tb_port *port)
824 ret = tb_port_read(port, &val, TB_CFG_PORT,
825 port->cap_phy + LANE_ADP_CS_1, 1);
829 return (val & LANE_ADP_CS_1_CURRENT_WIDTH_MASK) >>
830 LANE_ADP_CS_1_CURRENT_WIDTH_SHIFT;
833 static bool tb_port_is_width_supported(struct tb_port *port, int width)
841 ret = tb_port_read(port, &phy, TB_CFG_PORT,
842 port->cap_phy + LANE_ADP_CS_0, 1);
846 widths = (phy & LANE_ADP_CS_0_SUPPORTED_WIDTH_MASK) >>
847 LANE_ADP_CS_0_SUPPORTED_WIDTH_SHIFT;
849 return !!(widths & width);
852 static int tb_port_set_link_width(struct tb_port *port, unsigned int width)
860 ret = tb_port_read(port, &val, TB_CFG_PORT,
861 port->cap_phy + LANE_ADP_CS_1, 1);
865 val &= ~LANE_ADP_CS_1_TARGET_WIDTH_MASK;
868 val |= LANE_ADP_CS_1_TARGET_WIDTH_SINGLE <<
869 LANE_ADP_CS_1_TARGET_WIDTH_SHIFT;
872 val |= LANE_ADP_CS_1_TARGET_WIDTH_DUAL <<
873 LANE_ADP_CS_1_TARGET_WIDTH_SHIFT;
879 val |= LANE_ADP_CS_1_LB;
881 return tb_port_write(port, &val, TB_CFG_PORT,
882 port->cap_phy + LANE_ADP_CS_1, 1);
885 static int tb_port_lane_bonding_enable(struct tb_port *port)
890 * Enable lane bonding for both links if not already enabled by
891 * for example the boot firmware.
893 ret = tb_port_get_link_width(port);
895 ret = tb_port_set_link_width(port, 2);
900 ret = tb_port_get_link_width(port->dual_link_port);
902 ret = tb_port_set_link_width(port->dual_link_port, 2);
904 tb_port_set_link_width(port, 1);
910 port->dual_link_port->bonded = true;
915 static void tb_port_lane_bonding_disable(struct tb_port *port)
917 port->dual_link_port->bonded = false;
918 port->bonded = false;
920 tb_port_set_link_width(port->dual_link_port, 1);
921 tb_port_set_link_width(port, 1);
925 * tb_port_is_enabled() - Is the adapter port enabled
926 * @port: Port to check
928 bool tb_port_is_enabled(struct tb_port *port)
930 switch (port->config.type) {
931 case TB_TYPE_PCIE_UP:
932 case TB_TYPE_PCIE_DOWN:
933 return tb_pci_port_is_enabled(port);
935 case TB_TYPE_DP_HDMI_IN:
936 case TB_TYPE_DP_HDMI_OUT:
937 return tb_dp_port_is_enabled(port);
945 * tb_pci_port_is_enabled() - Is the PCIe adapter port enabled
946 * @port: PCIe port to check
948 bool tb_pci_port_is_enabled(struct tb_port *port)
952 if (tb_port_read(port, &data, TB_CFG_PORT,
953 port->cap_adap + ADP_PCIE_CS_0, 1))
956 return !!(data & ADP_PCIE_CS_0_PE);
960 * tb_pci_port_enable() - Enable PCIe adapter port
961 * @port: PCIe port to enable
962 * @enable: Enable/disable the PCIe adapter
964 int tb_pci_port_enable(struct tb_port *port, bool enable)
966 u32 word = enable ? ADP_PCIE_CS_0_PE : 0x0;
969 return tb_port_write(port, &word, TB_CFG_PORT,
970 port->cap_adap + ADP_PCIE_CS_0, 1);
974 * tb_dp_port_hpd_is_active() - Is HPD already active
975 * @port: DP out port to check
977 * Checks if the DP OUT adapter port has HDP bit already set.
979 int tb_dp_port_hpd_is_active(struct tb_port *port)
984 ret = tb_port_read(port, &data, TB_CFG_PORT,
985 port->cap_adap + ADP_DP_CS_2, 1);
989 return !!(data & ADP_DP_CS_2_HDP);
993 * tb_dp_port_hpd_clear() - Clear HPD from DP IN port
994 * @port: Port to clear HPD
996 * If the DP IN port has HDP set, this function can be used to clear it.
998 int tb_dp_port_hpd_clear(struct tb_port *port)
1003 ret = tb_port_read(port, &data, TB_CFG_PORT,
1004 port->cap_adap + ADP_DP_CS_3, 1);
1008 data |= ADP_DP_CS_3_HDPC;
1009 return tb_port_write(port, &data, TB_CFG_PORT,
1010 port->cap_adap + ADP_DP_CS_3, 1);
1014 * tb_dp_port_set_hops() - Set video/aux Hop IDs for DP port
1015 * @port: DP IN/OUT port to set hops
1016 * @video: Video Hop ID
1017 * @aux_tx: AUX TX Hop ID
1018 * @aux_rx: AUX RX Hop ID
1020 * Programs specified Hop IDs for DP IN/OUT port.
1022 int tb_dp_port_set_hops(struct tb_port *port, unsigned int video,
1023 unsigned int aux_tx, unsigned int aux_rx)
1028 ret = tb_port_read(port, data, TB_CFG_PORT,
1029 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
1033 data[0] &= ~ADP_DP_CS_0_VIDEO_HOPID_MASK;
1034 data[1] &= ~ADP_DP_CS_1_AUX_RX_HOPID_MASK;
1035 data[1] &= ~ADP_DP_CS_1_AUX_RX_HOPID_MASK;
1037 data[0] |= (video << ADP_DP_CS_0_VIDEO_HOPID_SHIFT) &
1038 ADP_DP_CS_0_VIDEO_HOPID_MASK;
1039 data[1] |= aux_tx & ADP_DP_CS_1_AUX_TX_HOPID_MASK;
1040 data[1] |= (aux_rx << ADP_DP_CS_1_AUX_RX_HOPID_SHIFT) &
1041 ADP_DP_CS_1_AUX_RX_HOPID_MASK;
1043 return tb_port_write(port, data, TB_CFG_PORT,
1044 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
1048 * tb_dp_port_is_enabled() - Is DP adapter port enabled
1049 * @port: DP adapter port to check
1051 bool tb_dp_port_is_enabled(struct tb_port *port)
1055 if (tb_port_read(port, data, TB_CFG_PORT, port->cap_adap + ADP_DP_CS_0,
1059 return !!(data[0] & (ADP_DP_CS_0_VE | ADP_DP_CS_0_AE));
1063 * tb_dp_port_enable() - Enables/disables DP paths of a port
1064 * @port: DP IN/OUT port
1065 * @enable: Enable/disable DP path
1067 * Once Hop IDs are programmed DP paths can be enabled or disabled by
1068 * calling this function.
1070 int tb_dp_port_enable(struct tb_port *port, bool enable)
1075 ret = tb_port_read(port, data, TB_CFG_PORT,
1076 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
1081 data[0] |= ADP_DP_CS_0_VE | ADP_DP_CS_0_AE;
1083 data[0] &= ~(ADP_DP_CS_0_VE | ADP_DP_CS_0_AE);
1085 return tb_port_write(port, data, TB_CFG_PORT,
1086 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
1089 /* switch utility functions */
1091 static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw)
1093 tb_dbg(tb, " Switch: %x:%x (Revision: %d, TB Version: %d)\n",
1094 sw->vendor_id, sw->device_id, sw->revision,
1095 sw->thunderbolt_version);
1096 tb_dbg(tb, " Max Port Number: %d\n", sw->max_port_number);
1097 tb_dbg(tb, " Config:\n");
1099 " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
1100 sw->upstream_port_number, sw->depth,
1101 (((u64) sw->route_hi) << 32) | sw->route_lo,
1102 sw->enabled, sw->plug_events_delay);
1103 tb_dbg(tb, " unknown1: %#x unknown4: %#x\n",
1104 sw->__unknown1, sw->__unknown4);
1108 * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
1110 * Return: Returns 0 on success or an error code on failure.
1112 int tb_switch_reset(struct tb *tb, u64 route)
1114 struct tb_cfg_result res;
1115 struct tb_regs_switch_header header = {
1116 header.route_hi = route >> 32,
1117 header.route_lo = route,
1118 header.enabled = true,
1120 tb_dbg(tb, "resetting switch at %llx\n", route);
1121 res.err = tb_cfg_write(tb->ctl, ((u32 *) &header) + 2, route,
1125 res = tb_cfg_reset(tb->ctl, route, TB_CFG_DEFAULT_TIMEOUT);
1132 * tb_plug_events_active() - enable/disable plug events on a switch
1134 * Also configures a sane plug_events_delay of 255ms.
1136 * Return: Returns 0 on success or an error code on failure.
1138 static int tb_plug_events_active(struct tb_switch *sw, bool active)
1143 if (tb_switch_is_icm(sw))
1146 sw->config.plug_events_delay = 0xff;
1147 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1);
1151 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
1156 data = data & 0xFFFFFF83;
1157 switch (sw->config.device_id) {
1158 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
1159 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
1160 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
1168 return tb_sw_write(sw, &data, TB_CFG_SWITCH,
1169 sw->cap_plug_events + 1, 1);
1172 static ssize_t authorized_show(struct device *dev,
1173 struct device_attribute *attr,
1176 struct tb_switch *sw = tb_to_switch(dev);
1178 return sprintf(buf, "%u\n", sw->authorized);
1181 static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
1185 if (!mutex_trylock(&sw->tb->lock))
1186 return restart_syscall();
1192 /* Approve switch */
1195 ret = tb_domain_approve_switch_key(sw->tb, sw);
1197 ret = tb_domain_approve_switch(sw->tb, sw);
1200 /* Challenge switch */
1203 ret = tb_domain_challenge_switch_key(sw->tb, sw);
1211 sw->authorized = val;
1212 /* Notify status change to the userspace */
1213 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
1217 mutex_unlock(&sw->tb->lock);
1221 static ssize_t authorized_store(struct device *dev,
1222 struct device_attribute *attr,
1223 const char *buf, size_t count)
1225 struct tb_switch *sw = tb_to_switch(dev);
1229 ret = kstrtouint(buf, 0, &val);
1235 pm_runtime_get_sync(&sw->dev);
1236 ret = tb_switch_set_authorized(sw, val);
1237 pm_runtime_mark_last_busy(&sw->dev);
1238 pm_runtime_put_autosuspend(&sw->dev);
1240 return ret ? ret : count;
1242 static DEVICE_ATTR_RW(authorized);
1244 static ssize_t boot_show(struct device *dev, struct device_attribute *attr,
1247 struct tb_switch *sw = tb_to_switch(dev);
1249 return sprintf(buf, "%u\n", sw->boot);
1251 static DEVICE_ATTR_RO(boot);
1253 static ssize_t device_show(struct device *dev, struct device_attribute *attr,
1256 struct tb_switch *sw = tb_to_switch(dev);
1258 return sprintf(buf, "%#x\n", sw->device);
1260 static DEVICE_ATTR_RO(device);
1263 device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1265 struct tb_switch *sw = tb_to_switch(dev);
1267 return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : "");
1269 static DEVICE_ATTR_RO(device_name);
1272 generation_show(struct device *dev, struct device_attribute *attr, char *buf)
1274 struct tb_switch *sw = tb_to_switch(dev);
1276 return sprintf(buf, "%u\n", sw->generation);
1278 static DEVICE_ATTR_RO(generation);
1280 static ssize_t key_show(struct device *dev, struct device_attribute *attr,
1283 struct tb_switch *sw = tb_to_switch(dev);
1286 if (!mutex_trylock(&sw->tb->lock))
1287 return restart_syscall();
1290 ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key);
1292 ret = sprintf(buf, "\n");
1294 mutex_unlock(&sw->tb->lock);
1298 static ssize_t key_store(struct device *dev, struct device_attribute *attr,
1299 const char *buf, size_t count)
1301 struct tb_switch *sw = tb_to_switch(dev);
1302 u8 key[TB_SWITCH_KEY_SIZE];
1303 ssize_t ret = count;
1306 if (!strcmp(buf, "\n"))
1308 else if (hex2bin(key, buf, sizeof(key)))
1311 if (!mutex_trylock(&sw->tb->lock))
1312 return restart_syscall();
1314 if (sw->authorized) {
1321 sw->key = kmemdup(key, sizeof(key), GFP_KERNEL);
1327 mutex_unlock(&sw->tb->lock);
1330 static DEVICE_ATTR(key, 0600, key_show, key_store);
1332 static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
1335 struct tb_switch *sw = tb_to_switch(dev);
1337 return sprintf(buf, "%u.0 Gb/s\n", sw->link_speed);
1341 * Currently all lanes must run at the same speed but we expose here
1342 * both directions to allow possible asymmetric links in the future.
1344 static DEVICE_ATTR(rx_speed, 0444, speed_show, NULL);
1345 static DEVICE_ATTR(tx_speed, 0444, speed_show, NULL);
1347 static ssize_t lanes_show(struct device *dev, struct device_attribute *attr,
1350 struct tb_switch *sw = tb_to_switch(dev);
1352 return sprintf(buf, "%u\n", sw->link_width);
1356 * Currently link has same amount of lanes both directions (1 or 2) but
1357 * expose them separately to allow possible asymmetric links in the future.
1359 static DEVICE_ATTR(rx_lanes, 0444, lanes_show, NULL);
1360 static DEVICE_ATTR(tx_lanes, 0444, lanes_show, NULL);
1362 static void nvm_authenticate_start(struct tb_switch *sw)
1364 struct pci_dev *root_port;
1367 * During host router NVM upgrade we should not allow root port to
1368 * go into D3cold because some root ports cannot trigger PME
1369 * itself. To be on the safe side keep the root port in D0 during
1370 * the whole upgrade process.
1372 root_port = pci_find_pcie_root_port(sw->tb->nhi->pdev);
1374 pm_runtime_get_noresume(&root_port->dev);
1377 static void nvm_authenticate_complete(struct tb_switch *sw)
1379 struct pci_dev *root_port;
1381 root_port = pci_find_pcie_root_port(sw->tb->nhi->pdev);
1383 pm_runtime_put(&root_port->dev);
1386 static ssize_t nvm_authenticate_show(struct device *dev,
1387 struct device_attribute *attr, char *buf)
1389 struct tb_switch *sw = tb_to_switch(dev);
1392 nvm_get_auth_status(sw, &status);
1393 return sprintf(buf, "%#x\n", status);
1396 static ssize_t nvm_authenticate_store(struct device *dev,
1397 struct device_attribute *attr, const char *buf, size_t count)
1399 struct tb_switch *sw = tb_to_switch(dev);
1403 pm_runtime_get_sync(&sw->dev);
1405 if (!mutex_trylock(&sw->tb->lock)) {
1406 ret = restart_syscall();
1410 /* If NVMem devices are not yet added */
1416 ret = kstrtobool(buf, &val);
1420 /* Always clear the authentication status */
1421 nvm_clear_auth_status(sw);
1424 if (!sw->nvm->buf) {
1429 ret = nvm_validate_and_write(sw);
1433 sw->nvm->authenticating = true;
1435 if (!tb_route(sw)) {
1437 * Keep root port from suspending as long as the
1438 * NVM upgrade process is running.
1440 nvm_authenticate_start(sw);
1441 ret = nvm_authenticate_host(sw);
1443 ret = nvm_authenticate_device(sw);
1448 mutex_unlock(&sw->tb->lock);
1450 pm_runtime_mark_last_busy(&sw->dev);
1451 pm_runtime_put_autosuspend(&sw->dev);
1457 static DEVICE_ATTR_RW(nvm_authenticate);
1459 static ssize_t nvm_version_show(struct device *dev,
1460 struct device_attribute *attr, char *buf)
1462 struct tb_switch *sw = tb_to_switch(dev);
1465 if (!mutex_trylock(&sw->tb->lock))
1466 return restart_syscall();
1473 ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);
1475 mutex_unlock(&sw->tb->lock);
1479 static DEVICE_ATTR_RO(nvm_version);
1481 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
1484 struct tb_switch *sw = tb_to_switch(dev);
1486 return sprintf(buf, "%#x\n", sw->vendor);
1488 static DEVICE_ATTR_RO(vendor);
1491 vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1493 struct tb_switch *sw = tb_to_switch(dev);
1495 return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : "");
1497 static DEVICE_ATTR_RO(vendor_name);
1499 static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
1502 struct tb_switch *sw = tb_to_switch(dev);
1504 return sprintf(buf, "%pUb\n", sw->uuid);
1506 static DEVICE_ATTR_RO(unique_id);
1508 static struct attribute *switch_attrs[] = {
1509 &dev_attr_authorized.attr,
1510 &dev_attr_boot.attr,
1511 &dev_attr_device.attr,
1512 &dev_attr_device_name.attr,
1513 &dev_attr_generation.attr,
1515 &dev_attr_nvm_authenticate.attr,
1516 &dev_attr_nvm_version.attr,
1517 &dev_attr_rx_speed.attr,
1518 &dev_attr_rx_lanes.attr,
1519 &dev_attr_tx_speed.attr,
1520 &dev_attr_tx_lanes.attr,
1521 &dev_attr_vendor.attr,
1522 &dev_attr_vendor_name.attr,
1523 &dev_attr_unique_id.attr,
1527 static umode_t switch_attr_is_visible(struct kobject *kobj,
1528 struct attribute *attr, int n)
1530 struct device *dev = container_of(kobj, struct device, kobj);
1531 struct tb_switch *sw = tb_to_switch(dev);
1533 if (attr == &dev_attr_device.attr) {
1536 } else if (attr == &dev_attr_device_name.attr) {
1537 if (!sw->device_name)
1539 } else if (attr == &dev_attr_vendor.attr) {
1542 } else if (attr == &dev_attr_vendor_name.attr) {
1543 if (!sw->vendor_name)
1545 } else if (attr == &dev_attr_key.attr) {
1547 sw->tb->security_level == TB_SECURITY_SECURE &&
1548 sw->security_level == TB_SECURITY_SECURE)
1551 } else if (attr == &dev_attr_rx_speed.attr ||
1552 attr == &dev_attr_rx_lanes.attr ||
1553 attr == &dev_attr_tx_speed.attr ||
1554 attr == &dev_attr_tx_lanes.attr) {
1558 } else if (attr == &dev_attr_nvm_authenticate.attr) {
1559 if (sw->dma_port && !sw->no_nvm_upgrade)
1562 } else if (attr == &dev_attr_nvm_version.attr) {
1566 } else if (attr == &dev_attr_boot.attr) {
1572 return sw->safe_mode ? 0 : attr->mode;
1575 static struct attribute_group switch_group = {
1576 .is_visible = switch_attr_is_visible,
1577 .attrs = switch_attrs,
1580 static const struct attribute_group *switch_groups[] = {
1585 static void tb_switch_release(struct device *dev)
1587 struct tb_switch *sw = tb_to_switch(dev);
1588 struct tb_port *port;
1590 dma_port_free(sw->dma_port);
1592 tb_switch_for_each_port(sw, port) {
1593 if (!port->disabled) {
1594 ida_destroy(&port->in_hopids);
1595 ida_destroy(&port->out_hopids);
1600 kfree(sw->device_name);
1601 kfree(sw->vendor_name);
1609 * Currently only need to provide the callbacks. Everything else is handled
1610 * in the connection manager.
1612 static int __maybe_unused tb_switch_runtime_suspend(struct device *dev)
1614 struct tb_switch *sw = tb_to_switch(dev);
1615 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
1617 if (cm_ops->runtime_suspend_switch)
1618 return cm_ops->runtime_suspend_switch(sw);
1623 static int __maybe_unused tb_switch_runtime_resume(struct device *dev)
1625 struct tb_switch *sw = tb_to_switch(dev);
1626 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
1628 if (cm_ops->runtime_resume_switch)
1629 return cm_ops->runtime_resume_switch(sw);
1633 static const struct dev_pm_ops tb_switch_pm_ops = {
1634 SET_RUNTIME_PM_OPS(tb_switch_runtime_suspend, tb_switch_runtime_resume,
1638 struct device_type tb_switch_type = {
1639 .name = "thunderbolt_device",
1640 .release = tb_switch_release,
1641 .pm = &tb_switch_pm_ops,
1644 static int tb_switch_get_generation(struct tb_switch *sw)
1646 switch (sw->config.device_id) {
1647 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
1648 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
1649 case PCI_DEVICE_ID_INTEL_LIGHT_PEAK:
1650 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
1651 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
1652 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
1653 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE:
1654 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE:
1657 case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE:
1658 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
1659 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
1662 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
1663 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
1664 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
1665 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
1666 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
1667 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
1668 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
1669 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
1670 case PCI_DEVICE_ID_INTEL_ICL_NHI0:
1671 case PCI_DEVICE_ID_INTEL_ICL_NHI1:
1676 * For unknown switches assume generation to be 1 to be
1679 tb_sw_warn(sw, "unsupported switch device id %#x\n",
1680 sw->config.device_id);
1686 * tb_switch_alloc() - allocate a switch
1687 * @tb: Pointer to the owning domain
1688 * @parent: Parent device for this switch
1689 * @route: Route string for this switch
1691 * Allocates and initializes a switch. Will not upload configuration to
1692 * the switch. For that you need to call tb_switch_configure()
1693 * separately. The returned switch should be released by calling
1696 * Return: Pointer to the allocated switch or ERR_PTR() in case of
1699 struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
1702 struct tb_switch *sw;
1706 /* Make sure we do not exceed maximum topology limit */
1707 depth = tb_route_length(route);
1708 if (depth > TB_SWITCH_MAX_DEPTH)
1709 return ERR_PTR(-EADDRNOTAVAIL);
1711 upstream_port = tb_cfg_get_upstream_port(tb->ctl, route);
1712 if (upstream_port < 0)
1713 return ERR_PTR(upstream_port);
1715 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
1717 return ERR_PTR(-ENOMEM);
1720 ret = tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5);
1722 goto err_free_sw_ports;
1724 tb_dbg(tb, "current switch config:\n");
1725 tb_dump_switch(tb, &sw->config);
1727 /* configure switch */
1728 sw->config.upstream_port_number = upstream_port;
1729 sw->config.depth = depth;
1730 sw->config.route_hi = upper_32_bits(route);
1731 sw->config.route_lo = lower_32_bits(route);
1732 sw->config.enabled = 0;
1734 /* initialize ports */
1735 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports),
1739 goto err_free_sw_ports;
1742 for (i = 0; i <= sw->config.max_port_number; i++) {
1743 /* minimum setup for tb_find_cap and tb_drom_read to work */
1744 sw->ports[i].sw = sw;
1745 sw->ports[i].port = i;
1748 sw->generation = tb_switch_get_generation(sw);
1750 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS);
1752 tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
1753 goto err_free_sw_ports;
1755 sw->cap_plug_events = ret;
1757 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER);
1761 /* Root switch is always authorized */
1763 sw->authorized = true;
1765 device_initialize(&sw->dev);
1766 sw->dev.parent = parent;
1767 sw->dev.bus = &tb_bus_type;
1768 sw->dev.type = &tb_switch_type;
1769 sw->dev.groups = switch_groups;
1770 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
1778 return ERR_PTR(ret);
1782 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
1783 * @tb: Pointer to the owning domain
1784 * @parent: Parent device for this switch
1785 * @route: Route string for this switch
1787 * This creates a switch in safe mode. This means the switch pretty much
1788 * lacks all capabilities except DMA configuration port before it is
1789 * flashed with a valid NVM firmware.
1791 * The returned switch must be released by calling tb_switch_put().
1793 * Return: Pointer to the allocated switch or ERR_PTR() in case of failure
1796 tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route)
1798 struct tb_switch *sw;
1800 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
1802 return ERR_PTR(-ENOMEM);
1805 sw->config.depth = tb_route_length(route);
1806 sw->config.route_hi = upper_32_bits(route);
1807 sw->config.route_lo = lower_32_bits(route);
1808 sw->safe_mode = true;
1810 device_initialize(&sw->dev);
1811 sw->dev.parent = parent;
1812 sw->dev.bus = &tb_bus_type;
1813 sw->dev.type = &tb_switch_type;
1814 sw->dev.groups = switch_groups;
1815 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
1821 * tb_switch_configure() - Uploads configuration to the switch
1822 * @sw: Switch to configure
1824 * Call this function before the switch is added to the system. It will
1825 * upload configuration to the switch and makes it available for the
1826 * connection manager to use.
1828 * Return: %0 in case of success and negative errno in case of failure
1830 int tb_switch_configure(struct tb_switch *sw)
1832 struct tb *tb = sw->tb;
1836 route = tb_route(sw);
1837 tb_dbg(tb, "initializing Switch at %#llx (depth: %d, up port: %d)\n",
1838 route, tb_route_length(route), sw->config.upstream_port_number);
1840 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL)
1841 tb_sw_warn(sw, "unknown switch vendor id %#x\n",
1842 sw->config.vendor_id);
1844 sw->config.enabled = 1;
1846 /* upload configuration */
1847 ret = tb_sw_write(sw, 1 + (u32 *)&sw->config, TB_CFG_SWITCH, 1, 3);
1851 ret = tb_lc_configure_link(sw);
1855 return tb_plug_events_active(sw, true);
1858 static int tb_switch_set_uuid(struct tb_switch *sw)
1867 * The newer controllers include fused UUID as part of link
1868 * controller specific registers
1870 ret = tb_lc_read_uuid(sw, uuid);
1873 * ICM generates UUID based on UID and fills the upper
1874 * two words with ones. This is not strictly following
1875 * UUID format but we want to be compatible with it so
1876 * we do the same here.
1878 uuid[0] = sw->uid & 0xffffffff;
1879 uuid[1] = (sw->uid >> 32) & 0xffffffff;
1880 uuid[2] = 0xffffffff;
1881 uuid[3] = 0xffffffff;
1884 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
1890 static int tb_switch_add_dma_port(struct tb_switch *sw)
1895 switch (sw->generation) {
1897 /* Only root switch can be upgraded */
1903 ret = tb_switch_set_uuid(sw);
1910 * DMA port is the only thing available when the switch
1918 /* Root switch DMA port requires running firmware */
1919 if (!tb_route(sw) && !tb_switch_is_icm(sw))
1922 sw->dma_port = dma_port_alloc(sw);
1926 if (sw->no_nvm_upgrade)
1930 * If there is status already set then authentication failed
1931 * when the dma_port_flash_update_auth() returned. Power cycling
1932 * is not needed (it was done already) so only thing we do here
1933 * is to unblock runtime PM of the root port.
1935 nvm_get_auth_status(sw, &status);
1938 nvm_authenticate_complete(sw);
1943 * Check status of the previous flash authentication. If there
1944 * is one we need to power cycle the switch in any case to make
1945 * it functional again.
1947 ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
1951 /* Now we can allow root port to suspend again */
1953 nvm_authenticate_complete(sw);
1956 tb_sw_info(sw, "switch flash authentication failed\n");
1957 nvm_set_auth_status(sw, status);
1960 tb_sw_info(sw, "power cycling the switch now\n");
1961 dma_port_power_cycle(sw->dma_port);
1964 * We return error here which causes the switch adding failure.
1965 * It should appear back after power cycle is complete.
1970 static void tb_switch_default_link_ports(struct tb_switch *sw)
1974 for (i = 1; i <= sw->config.max_port_number; i += 2) {
1975 struct tb_port *port = &sw->ports[i];
1976 struct tb_port *subordinate;
1978 if (!tb_port_is_null(port))
1981 /* Check for the subordinate port */
1982 if (i == sw->config.max_port_number ||
1983 !tb_port_is_null(&sw->ports[i + 1]))
1986 /* Link them if not already done so (by DROM) */
1987 subordinate = &sw->ports[i + 1];
1988 if (!port->dual_link_port && !subordinate->dual_link_port) {
1990 port->dual_link_port = subordinate;
1991 subordinate->link_nr = 1;
1992 subordinate->dual_link_port = port;
1994 tb_sw_dbg(sw, "linked ports %d <-> %d\n",
1995 port->port, subordinate->port);
2000 static bool tb_switch_lane_bonding_possible(struct tb_switch *sw)
2002 const struct tb_port *up = tb_upstream_port(sw);
2004 if (!up->dual_link_port || !up->dual_link_port->remote)
2007 return tb_lc_lane_bonding_possible(sw);
2010 static int tb_switch_update_link_attributes(struct tb_switch *sw)
2013 bool change = false;
2016 if (!tb_route(sw) || tb_switch_is_icm(sw))
2019 up = tb_upstream_port(sw);
2021 ret = tb_port_get_link_speed(up);
2024 if (sw->link_speed != ret)
2026 sw->link_speed = ret;
2028 ret = tb_port_get_link_width(up);
2031 if (sw->link_width != ret)
2033 sw->link_width = ret;
2035 /* Notify userspace that there is possible link attribute change */
2036 if (device_is_registered(&sw->dev) && change)
2037 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
2043 * tb_switch_lane_bonding_enable() - Enable lane bonding
2044 * @sw: Switch to enable lane bonding
2046 * Connection manager can call this function to enable lane bonding of a
2047 * switch. If conditions are correct and both switches support the feature,
2048 * lanes are bonded. It is safe to call this to any switch.
2050 int tb_switch_lane_bonding_enable(struct tb_switch *sw)
2052 struct tb_switch *parent = tb_to_switch(sw->dev.parent);
2053 struct tb_port *up, *down;
2054 u64 route = tb_route(sw);
2060 if (!tb_switch_lane_bonding_possible(sw))
2063 up = tb_upstream_port(sw);
2064 down = tb_port_at(route, parent);
2066 if (!tb_port_is_width_supported(up, 2) ||
2067 !tb_port_is_width_supported(down, 2))
2070 ret = tb_port_lane_bonding_enable(up);
2072 tb_port_warn(up, "failed to enable lane bonding\n");
2076 ret = tb_port_lane_bonding_enable(down);
2078 tb_port_warn(down, "failed to enable lane bonding\n");
2079 tb_port_lane_bonding_disable(up);
2083 tb_switch_update_link_attributes(sw);
2085 tb_sw_dbg(sw, "lane bonding enabled\n");
2090 * tb_switch_lane_bonding_disable() - Disable lane bonding
2091 * @sw: Switch whose lane bonding to disable
2093 * Disables lane bonding between @sw and parent. This can be called even
2094 * if lanes were not bonded originally.
2096 void tb_switch_lane_bonding_disable(struct tb_switch *sw)
2098 struct tb_switch *parent = tb_to_switch(sw->dev.parent);
2099 struct tb_port *up, *down;
2104 up = tb_upstream_port(sw);
2108 down = tb_port_at(tb_route(sw), parent);
2110 tb_port_lane_bonding_disable(up);
2111 tb_port_lane_bonding_disable(down);
2113 tb_switch_update_link_attributes(sw);
2114 tb_sw_dbg(sw, "lane bonding disabled\n");
2118 * tb_switch_add() - Add a switch to the domain
2119 * @sw: Switch to add
2121 * This is the last step in adding switch to the domain. It will read
2122 * identification information from DROM and initializes ports so that
2123 * they can be used to connect other switches. The switch will be
2124 * exposed to the userspace when this function successfully returns. To
2125 * remove and release the switch, call tb_switch_remove().
2127 * Return: %0 in case of success and negative errno in case of failure
2129 int tb_switch_add(struct tb_switch *sw)
2134 * Initialize DMA control port now before we read DROM. Recent
2135 * host controllers have more complete DROM on NVM that includes
2136 * vendor and model identification strings which we then expose
2137 * to the userspace. NVM can be accessed through DMA
2138 * configuration based mailbox.
2140 ret = tb_switch_add_dma_port(sw);
2142 dev_err(&sw->dev, "failed to add DMA port\n");
2146 if (!sw->safe_mode) {
2148 ret = tb_drom_read(sw);
2150 dev_err(&sw->dev, "reading DROM failed\n");
2153 tb_sw_dbg(sw, "uid: %#llx\n", sw->uid);
2155 ret = tb_switch_set_uuid(sw);
2157 dev_err(&sw->dev, "failed to set UUID\n");
2161 for (i = 0; i <= sw->config.max_port_number; i++) {
2162 if (sw->ports[i].disabled) {
2163 tb_port_dbg(&sw->ports[i], "disabled by eeprom\n");
2166 ret = tb_init_port(&sw->ports[i]);
2168 dev_err(&sw->dev, "failed to initialize port %d\n", i);
2173 tb_switch_default_link_ports(sw);
2175 ret = tb_switch_update_link_attributes(sw);
2180 ret = device_add(&sw->dev);
2182 dev_err(&sw->dev, "failed to add device: %d\n", ret);
2187 dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n",
2188 sw->vendor, sw->device);
2189 if (sw->vendor_name && sw->device_name)
2190 dev_info(&sw->dev, "%s %s\n", sw->vendor_name,
2194 ret = tb_switch_nvm_add(sw);
2196 dev_err(&sw->dev, "failed to add NVM devices\n");
2197 device_del(&sw->dev);
2201 pm_runtime_set_active(&sw->dev);
2203 pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY);
2204 pm_runtime_use_autosuspend(&sw->dev);
2205 pm_runtime_mark_last_busy(&sw->dev);
2206 pm_runtime_enable(&sw->dev);
2207 pm_request_autosuspend(&sw->dev);
2214 * tb_switch_remove() - Remove and release a switch
2215 * @sw: Switch to remove
2217 * This will remove the switch from the domain and release it after last
2218 * reference count drops to zero. If there are switches connected below
2219 * this switch, they will be removed as well.
2221 void tb_switch_remove(struct tb_switch *sw)
2223 struct tb_port *port;
2226 pm_runtime_get_sync(&sw->dev);
2227 pm_runtime_disable(&sw->dev);
2230 /* port 0 is the switch itself and never has a remote */
2231 tb_switch_for_each_port(sw, port) {
2232 if (tb_port_has_remote(port)) {
2233 tb_switch_remove(port->remote->sw);
2234 port->remote = NULL;
2235 } else if (port->xdomain) {
2236 tb_xdomain_remove(port->xdomain);
2237 port->xdomain = NULL;
2241 if (!sw->is_unplugged)
2242 tb_plug_events_active(sw, false);
2243 tb_lc_unconfigure_link(sw);
2245 tb_switch_nvm_remove(sw);
2248 dev_info(&sw->dev, "device disconnected\n");
2249 device_unregister(&sw->dev);
2253 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
2255 void tb_sw_set_unplugged(struct tb_switch *sw)
2257 struct tb_port *port;
2259 if (sw == sw->tb->root_switch) {
2260 tb_sw_WARN(sw, "cannot unplug root switch\n");
2263 if (sw->is_unplugged) {
2264 tb_sw_WARN(sw, "is_unplugged already set\n");
2267 sw->is_unplugged = true;
2268 tb_switch_for_each_port(sw, port) {
2269 if (tb_port_has_remote(port))
2270 tb_sw_set_unplugged(port->remote->sw);
2271 else if (port->xdomain)
2272 port->xdomain->is_unplugged = true;
2276 int tb_switch_resume(struct tb_switch *sw)
2278 struct tb_port *port;
2281 tb_sw_dbg(sw, "resuming switch\n");
2284 * Check for UID of the connected switches except for root
2285 * switch which we assume cannot be removed.
2291 * Check first that we can still read the switch config
2292 * space. It may be that there is now another domain
2295 err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw));
2297 tb_sw_info(sw, "switch not present anymore\n");
2301 err = tb_drom_read_uid_only(sw, &uid);
2303 tb_sw_warn(sw, "uid read failed\n");
2306 if (sw->uid != uid) {
2308 "changed while suspended (uid %#llx -> %#llx)\n",
2314 /* upload configuration */
2315 err = tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3);
2319 err = tb_lc_configure_link(sw);
2323 err = tb_plug_events_active(sw, true);
2327 /* check for surviving downstream switches */
2328 tb_switch_for_each_port(sw, port) {
2329 if (!tb_port_has_remote(port) && !port->xdomain)
2332 if (tb_wait_for_port(port, true) <= 0) {
2334 "lost during suspend, disconnecting\n");
2335 if (tb_port_has_remote(port))
2336 tb_sw_set_unplugged(port->remote->sw);
2337 else if (port->xdomain)
2338 port->xdomain->is_unplugged = true;
2339 } else if (tb_port_has_remote(port)) {
2340 if (tb_switch_resume(port->remote->sw)) {
2342 "lost during suspend, disconnecting\n");
2343 tb_sw_set_unplugged(port->remote->sw);
2350 void tb_switch_suspend(struct tb_switch *sw)
2352 struct tb_port *port;
2355 err = tb_plug_events_active(sw, false);
2359 tb_switch_for_each_port(sw, port) {
2360 if (tb_port_has_remote(port))
2361 tb_switch_suspend(port->remote->sw);
2364 tb_lc_set_sleep(sw);
2368 * tb_switch_query_dp_resource() - Query availability of DP resource
2369 * @sw: Switch whose DP resource is queried
2372 * Queries availability of DP resource for DP tunneling using switch
2373 * specific means. Returns %true if resource is available.
2375 bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in)
2377 return tb_lc_dp_sink_query(sw, in);
2381 * tb_switch_alloc_dp_resource() - Allocate available DP resource
2382 * @sw: Switch whose DP resource is allocated
2385 * Allocates DP resource for DP tunneling. The resource must be
2386 * available for this to succeed (see tb_switch_query_dp_resource()).
2387 * Returns %0 in success and negative errno otherwise.
2389 int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
2391 return tb_lc_dp_sink_alloc(sw, in);
2395 * tb_switch_dealloc_dp_resource() - De-allocate DP resource
2396 * @sw: Switch whose DP resource is de-allocated
2399 * De-allocates DP resource that was previously allocated for DP
2402 void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
2404 if (tb_lc_dp_sink_dealloc(sw, in)) {
2405 tb_sw_warn(sw, "failed to de-allocate DP resource for port %d\n",
2410 struct tb_sw_lookup {
2418 static int tb_switch_match(struct device *dev, const void *data)
2420 struct tb_switch *sw = tb_to_switch(dev);
2421 const struct tb_sw_lookup *lookup = data;
2425 if (sw->tb != lookup->tb)
2429 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid));
2431 if (lookup->route) {
2432 return sw->config.route_lo == lower_32_bits(lookup->route) &&
2433 sw->config.route_hi == upper_32_bits(lookup->route);
2436 /* Root switch is matched only by depth */
2440 return sw->link == lookup->link && sw->depth == lookup->depth;
2444 * tb_switch_find_by_link_depth() - Find switch by link and depth
2445 * @tb: Domain the switch belongs
2446 * @link: Link number the switch is connected
2447 * @depth: Depth of the switch in link
2449 * Returned switch has reference count increased so the caller needs to
2450 * call tb_switch_put() when done with the switch.
2452 struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth)
2454 struct tb_sw_lookup lookup;
2457 memset(&lookup, 0, sizeof(lookup));
2460 lookup.depth = depth;
2462 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
2464 return tb_to_switch(dev);
2470 * tb_switch_find_by_uuid() - Find switch by UUID
2471 * @tb: Domain the switch belongs
2472 * @uuid: UUID to look for
2474 * Returned switch has reference count increased so the caller needs to
2475 * call tb_switch_put() when done with the switch.
2477 struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid)
2479 struct tb_sw_lookup lookup;
2482 memset(&lookup, 0, sizeof(lookup));
2486 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
2488 return tb_to_switch(dev);
2494 * tb_switch_find_by_route() - Find switch by route string
2495 * @tb: Domain the switch belongs
2496 * @route: Route string to look for
2498 * Returned switch has reference count increased so the caller needs to
2499 * call tb_switch_put() when done with the switch.
2501 struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route)
2503 struct tb_sw_lookup lookup;
2507 return tb_switch_get(tb->root_switch);
2509 memset(&lookup, 0, sizeof(lookup));
2511 lookup.route = route;
2513 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
2515 return tb_to_switch(dev);
2520 void tb_switch_exit(void)
2522 ida_destroy(&nvm_ida);