Lines Matching refs:port
3 * Thunderbolt driver - switch/port utility functions
216 * During host router NVM upgrade we should not allow root port to
218 * itself. To be on the safe side keep the root port in D0 during
247 /* Thunderbolt 2 and 3 devices support NVM through DMA port */
412 /* port utility functions */
414 static const char *tb_port_type(const struct tb_regs_port_header *port)
416 switch (port->type >> 16) {
418 switch ((u8) port->type) {
443 static void tb_dump_port(struct tb *tb, const struct tb_port *port)
445 const struct tb_regs_port_header *regs = &port->config;
456 tb_dbg(tb, " Credits (total/control): %u/%u\n", port->total_credits,
457 port->ctl_credits);
461 * tb_port_state() - get connectedness state of a port
462 * @port: the port to check
464 * The port must have a TB_CAP_PHY (i.e. it should be a real port).
468 int tb_port_state(struct tb_port *port)
472 if (port->cap_phy == 0) {
473 tb_port_WARN(port, "does not have a PHY\n");
476 res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2);
483 * tb_wait_for_port() - wait for a port to become ready
484 * @port: Port to wait
485 * @wait_if_unplugged: Wait also when port is unplugged
487 * Wait up to 1 second for a port to reach state TB_PORT_UP. If
488 * wait_if_unplugged is set then we also wait if the port is in state
493 * Return: Returns an error code on failure. Returns 0 if the port is not
495 * if the port is connected and in state TB_PORT_UP.
497 int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
501 if (!port->cap_phy) {
502 tb_port_WARN(port, "does not have PHY\n");
505 if (tb_is_upstream_port(port)) {
506 tb_port_WARN(port, "is the upstream port\n");
511 state = tb_port_state(port);
514 tb_port_dbg(port, "is disabled (state: 0)\n");
520 tb_port_dbg(port,
525 tb_port_dbg(port, "is unplugged (state: 7)\n");
533 tb_port_dbg(port, "is connected, link is up (state: %d)\n", state);
544 tb_port_dbg(port,
551 tb_port_warn(port,
552 "failed to reach state TB_PORT_UP. Ignoring port...\n");
557 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
558 * @port: Port to add/remove NFC credits
561 * Change the number of NFC credits allocated to @port by @credits. To remove
566 int tb_port_add_nfc_credits(struct tb_port *port, int credits)
570 if (credits == 0 || port->sw->is_unplugged)
577 if (tb_switch_is_usb4(port->sw) && !tb_port_is_null(port))
580 nfc_credits = port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK;
586 tb_port_dbg(port, "adding %d NFC credits to %lu", credits,
587 port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK);
589 port->config.nfc_credits &= ~ADP_CS_4_NFC_BUFFERS_MASK;
590 port->config.nfc_credits |= nfc_credits;
592 return tb_port_write(port, &port->config.nfc_credits,
598 * @port: Port whose counters to clear
603 int tb_port_clear_counter(struct tb_port *port, int counter)
606 tb_port_dbg(port, "clearing counter %d\n", counter);
607 return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
611 * tb_port_unlock() - Unlock downstream port
612 * @port: Port to unlock
617 int tb_port_unlock(struct tb_port *port)
619 if (tb_switch_is_icm(port->sw))
621 if (!tb_port_is_null(port))
623 if (tb_switch_is_usb4(port->sw))
624 return usb4_port_unlock(port);
628 static int __tb_port_enable(struct tb_port *port, bool enable)
633 if (!tb_port_is_null(port))
636 ret = tb_port_read(port, &phy, TB_CFG_PORT,
637 port->cap_phy + LANE_ADP_CS_1, 1);
647 ret = tb_port_write(port, &phy, TB_CFG_PORT,
648 port->cap_phy + LANE_ADP_CS_1, 1);
652 tb_port_dbg(port, "lane %s\n", str_enabled_disabled(enable));
658 * @port: Port to enable (can be %NULL)
662 int tb_port_enable(struct tb_port *port)
664 return __tb_port_enable(port, true);
669 * @port: Port to disable (can be %NULL)
673 int tb_port_disable(struct tb_port *port)
675 return __tb_port_enable(port, false);
679 * tb_init_port() - initialize a port
686 static int tb_init_port(struct tb_port *port)
691 INIT_LIST_HEAD(&port->list);
694 if (!port->port)
697 res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8);
700 tb_dbg(port->sw->tb, " Port %d: not implemented\n",
701 port->port);
702 port->disabled = true;
709 if (port->config.type == TB_TYPE_PORT) {
710 cap = tb_port_find_cap(port, TB_PORT_CAP_PHY);
713 port->cap_phy = cap;
715 tb_port_WARN(port, "non switch port without a PHY\n");
717 cap = tb_port_find_cap(port, TB_PORT_CAP_USB4);
719 port->cap_usb4 = cap;
726 if (port->cap_usb4) {
729 if (!tb_port_read(port, &hop, TB_CFG_HOPS, 0, 2))
730 port->ctl_credits = hop.initial_credits;
732 if (!port->ctl_credits)
733 port->ctl_credits = 2;
736 cap = tb_port_find_cap(port, TB_PORT_CAP_ADAP);
738 port->cap_adap = cap;
741 port->total_credits =
742 (port->config.nfc_credits & ADP_CS_4_TOTAL_BUFFERS_MASK) >>
745 tb_dump_port(port->sw->tb, port);
749 static int tb_port_alloc_hopid(struct tb_port *port, bool in, int min_hopid,
756 port_max_hopid = port->config.max_in_hop_id;
757 ida = &port->in_hopids;
759 port_max_hopid = port->config.max_out_hop_id;
760 ida = &port->out_hopids;
767 if (!tb_port_is_nhi(port) && min_hopid < TB_PATH_MIN_HOPID)
777 * tb_port_alloc_in_hopid() - Allocate input HopID from port
778 * @port: Port to allocate HopID for
785 int tb_port_alloc_in_hopid(struct tb_port *port, int min_hopid, int max_hopid)
787 return tb_port_alloc_hopid(port, true, min_hopid, max_hopid);
791 * tb_port_alloc_out_hopid() - Allocate output HopID from port
792 * @port: Port to allocate HopID for
799 int tb_port_alloc_out_hopid(struct tb_port *port, int min_hopid, int max_hopid)
801 return tb_port_alloc_hopid(port, false, min_hopid, max_hopid);
805 * tb_port_release_in_hopid() - Release allocated input HopID from port
806 * @port: Port whose HopID to release
809 void tb_port_release_in_hopid(struct tb_port *port, int hopid)
811 ida_simple_remove(&port->in_hopids, hopid);
815 * tb_port_release_out_hopid() - Release allocated output HopID from port
816 * @port: Port whose HopID to release
819 void tb_port_release_out_hopid(struct tb_port *port, int hopid)
821 ida_simple_remove(&port->out_hopids, hopid);
832 * tb_next_port_on_path() - Return next port for given port on a path
833 * @start: Start port of the walk
834 * @end: End port of the walk
835 * @prev: Previous port (%NULL if this is the first)
837 * This function can be used to walk from one port to another if they
839 * link port, the function follows that link and returns another end on
842 * If the @end port has been reached, return %NULL.
887 * @port: Port to check (USB4 or CIO)
891 int tb_port_get_link_speed(struct tb_port *port)
896 if (!port->cap_phy)
899 ret = tb_port_read(port, &val, TB_CFG_PORT,
900 port->cap_phy + LANE_ADP_CS_1, 1);
919 * @port: Port to check (USB4 or CIO)
924 int tb_port_get_link_width(struct tb_port *port)
929 if (!port->cap_phy)
932 ret = tb_port_read(port, &val, TB_CFG_PORT,
933 port->cap_phy + LANE_ADP_CS_1, 1);
942 static bool tb_port_is_width_supported(struct tb_port *port,
948 if (!port->cap_phy)
951 ret = tb_port_read(port, &phy, TB_CFG_PORT,
952 port->cap_phy + LANE_ADP_CS_0, 1);
962 static bool is_gen4_link(struct tb_port *port)
964 return tb_port_get_link_speed(port) > 20;
969 * @port: Lane adapter
977 int tb_port_set_link_width(struct tb_port *port, enum tb_link_width width)
982 if (!port->cap_phy)
985 ret = tb_port_read(port, &val, TB_CFG_PORT,
986 port->cap_phy + LANE_ADP_CS_1, 1);
994 if (is_gen4_link(port))
1007 return tb_port_write(port, &val, TB_CFG_PORT,
1008 port->cap_phy + LANE_ADP_CS_1, 1);
1013 * @port: Lane adapter
1023 static int tb_port_set_lane_bonding(struct tb_port *port, bool bonding)
1028 if (!port->cap_phy)
1031 ret = tb_port_read(port, &val, TB_CFG_PORT,
1032 port->cap_phy + LANE_ADP_CS_1, 1);
1041 return tb_port_write(port, &val, TB_CFG_PORT,
1042 port->cap_phy + LANE_ADP_CS_1, 1);
1046 * tb_port_lane_bonding_enable() - Enable bonding on port
1047 * @port: port to enable
1049 * Enable bonding by setting the link width of the port and the other
1050 * port in case of dual link port. Does not wait for the link to
1057 int tb_port_lane_bonding_enable(struct tb_port *port)
1066 width = tb_port_get_link_width(port);
1068 ret = tb_port_set_link_width(port, TB_LINK_WIDTH_DUAL);
1073 width = tb_port_get_link_width(port->dual_link_port);
1075 ret = tb_port_set_link_width(port->dual_link_port,
1085 if (width == TB_LINK_WIDTH_SINGLE && !tb_is_upstream_port(port)) {
1086 ret = tb_port_set_lane_bonding(port, true);
1095 port->bonded = true;
1096 port->dual_link_port->bonded = true;
1101 tb_port_set_link_width(port->dual_link_port, TB_LINK_WIDTH_SINGLE);
1103 tb_port_set_link_width(port, TB_LINK_WIDTH_SINGLE);
1109 * tb_port_lane_bonding_disable() - Disable bonding on port
1110 * @port: port to disable
1112 * Disable bonding by setting the link width of the port and the
1113 * other port in case of dual link port.
1115 void tb_port_lane_bonding_disable(struct tb_port *port)
1117 tb_port_set_lane_bonding(port, false);
1118 tb_port_set_link_width(port->dual_link_port, TB_LINK_WIDTH_SINGLE);
1119 tb_port_set_link_width(port, TB_LINK_WIDTH_SINGLE);
1120 port->dual_link_port->bonded = false;
1121 port->bonded = false;
1126 * @port: Port to wait for
1136 int tb_port_wait_for_link_width(struct tb_port *port, unsigned int width_mask,
1143 if ((width_mask & TB_LINK_WIDTH_SINGLE) && is_gen4_link(port))
1147 ret = tb_port_get_link_width(port);
1150 * Sometimes we get port locked error when
1166 static int tb_port_do_update_credits(struct tb_port *port)
1171 ret = tb_port_read(port, &nfc_credits, TB_CFG_PORT, ADP_CS_4, 1);
1175 if (nfc_credits != port->config.nfc_credits) {
1181 tb_port_dbg(port, "total credits changed %u -> %u\n",
1182 port->total_credits, total);
1184 port->config.nfc_credits = nfc_credits;
1185 port->total_credits = total;
1192 * tb_port_update_credits() - Re-read port total credits
1193 * @port: Port to update
1195 * After the link is bonded (or bonding was disabled) the port total
1199 int tb_port_update_credits(struct tb_port *port)
1203 ret = tb_port_do_update_credits(port);
1206 return tb_port_do_update_credits(port->dual_link_port);
1209 static int tb_port_start_lane_initialization(struct tb_port *port)
1213 if (tb_switch_is_usb4(port->sw))
1216 ret = tb_lc_start_lane_initialization(port);
1221 * Returns true if the port had something (router, XDomain) connected
1224 static bool tb_port_resume(struct tb_port *port)
1226 bool has_remote = tb_port_has_remote(port);
1228 if (port->usb4) {
1229 usb4_port_device_resume(port->usb4);
1240 if (!tb_is_upstream_port(port) || port->xdomain)
1241 tb_port_start_lane_initialization(port);
1244 return has_remote || port->xdomain;
1248 * tb_port_is_enabled() - Is the adapter port enabled
1249 * @port: Port to check
1251 bool tb_port_is_enabled(struct tb_port *port)
1253 switch (port->config.type) {
1256 return tb_pci_port_is_enabled(port);
1260 return tb_dp_port_is_enabled(port);
1264 return tb_usb3_port_is_enabled(port);
1272 * tb_usb3_port_is_enabled() - Is the USB3 adapter port enabled
1273 * @port: USB3 adapter port to check
1275 bool tb_usb3_port_is_enabled(struct tb_port *port)
1279 if (tb_port_read(port, &data, TB_CFG_PORT,
1280 port->cap_adap + ADP_USB3_CS_0, 1))
1287 * tb_usb3_port_enable() - Enable USB3 adapter port
1288 * @port: USB3 adapter port to enable
1291 int tb_usb3_port_enable(struct tb_port *port, bool enable)
1296 if (!port->cap_adap)
1298 return tb_port_write(port, &word, TB_CFG_PORT,
1299 port->cap_adap + ADP_USB3_CS_0, 1);
1303 * tb_pci_port_is_enabled() - Is the PCIe adapter port enabled
1304 * @port: PCIe port to check
1306 bool tb_pci_port_is_enabled(struct tb_port *port)
1310 if (tb_port_read(port, &data, TB_CFG_PORT,
1311 port->cap_adap + ADP_PCIE_CS_0, 1))
1318 * tb_pci_port_enable() - Enable PCIe adapter port
1319 * @port: PCIe port to enable
1322 int tb_pci_port_enable(struct tb_port *port, bool enable)
1325 if (!port->cap_adap)
1327 return tb_port_write(port, &word, TB_CFG_PORT,
1328 port->cap_adap + ADP_PCIE_CS_0, 1);
1333 * @port: DP out port to check
1335 * Checks if the DP OUT adapter port has HDP bit already set.
1337 int tb_dp_port_hpd_is_active(struct tb_port *port)
1342 ret = tb_port_read(port, &data, TB_CFG_PORT,
1343 port->cap_adap + ADP_DP_CS_2, 1);
1351 * tb_dp_port_hpd_clear() - Clear HPD from DP IN port
1352 * @port: Port to clear HPD
1354 * If the DP IN port has HDP set, this function can be used to clear it.
1356 int tb_dp_port_hpd_clear(struct tb_port *port)
1361 ret = tb_port_read(port, &data, TB_CFG_PORT,
1362 port->cap_adap + ADP_DP_CS_3, 1);
1367 return tb_port_write(port, &data, TB_CFG_PORT,
1368 port->cap_adap + ADP_DP_CS_3, 1);
1372 * tb_dp_port_set_hops() - Set video/aux Hop IDs for DP port
1373 * @port: DP IN/OUT port to set hops
1378 * Programs specified Hop IDs for DP IN/OUT port. Can be called for USB4
1382 int tb_dp_port_set_hops(struct tb_port *port, unsigned int video,
1388 if (tb_switch_is_usb4(port->sw))
1391 ret = tb_port_read(port, data, TB_CFG_PORT,
1392 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
1406 return tb_port_write(port, data, TB_CFG_PORT,
1407 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
1411 * tb_dp_port_is_enabled() - Is DP adapter port enabled
1412 * @port: DP adapter port to check
1414 bool tb_dp_port_is_enabled(struct tb_port *port)
1418 if (tb_port_read(port, data, TB_CFG_PORT, port->cap_adap + ADP_DP_CS_0,
1426 * tb_dp_port_enable() - Enables/disables DP paths of a port
1427 * @port: DP IN/OUT port
1433 int tb_dp_port_enable(struct tb_port *port, bool enable)
1438 ret = tb_port_read(port, data, TB_CFG_PORT,
1439 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
1448 return tb_port_write(port, data, TB_CFG_PORT,
1449 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
2100 struct tb_port *port;
2104 tb_switch_for_each_port(sw, port) {
2105 ida_destroy(&port->in_hopids);
2106 ida_destroy(&port->out_hopids);
2132 const struct tb_port *port;
2136 tb_switch_for_each_port(sw, port) {
2137 if (!port->disabled && !tb_is_upstream_port(port) &&
2138 tb_port_is_null(port)) {
2268 /* Unlock the downstream port so we can access the switch below */
2321 sw->ports[i].port = i;
2323 /* Control port does not need HopID allocation */
2373 * lacks all capabilities except DMA configuration port before it is
2424 tb_dbg(tb, "%s Switch at %#llx (depth: %d, up port: %d)\n",
2553 * DMA port is the only thing available when the switch
2577 /* Root switch DMA port requires running firmware */
2589 * is to unblock runtime PM of the root port.
2607 /* Now we can allow root port to suspend again */
2631 struct tb_port *port = &sw->ports[i];
2634 if (!tb_port_is_null(port))
2637 /* Check for the subordinate port */
2644 if (!port->dual_link_port && !subordinate->dual_link_port) {
2645 port->link_nr = 0;
2646 port->dual_link_port = subordinate;
2648 subordinate->dual_link_port = port;
2651 port->port, subordinate->port);
2880 struct tb_port *port;
2885 tb_switch_for_each_port(sw, port) {
2888 if (!port->cap_usb4)
2891 res = usb4_port_hotplug_enable(port);
2915 * Initialize DMA control port now before we read DROM. Recent
2923 dev_err(&sw->dev, "failed to add DMA port\n");
2949 dev_err(&sw->dev, "failed to initialize port %d\n", i);
3038 struct tb_port *port;
3047 /* port 0 is the switch itself and never has a remote */
3048 tb_switch_for_each_port(sw, port) {
3049 if (tb_port_has_remote(port)) {
3050 tb_switch_remove(port->remote->sw);
3051 port->remote = NULL;
3052 } else if (port->xdomain) {
3053 tb_xdomain_remove(port->xdomain);
3054 port->xdomain = NULL;
3058 tb_retimer_remove_all(port);
3078 struct tb_port *port;
3089 tb_switch_for_each_port(sw, port) {
3090 if (tb_port_has_remote(port))
3091 tb_sw_set_unplugged(port->remote->sw);
3092 else if (port->xdomain)
3093 port->xdomain->is_unplugged = true;
3111 struct tb_port *port;
3166 tb_switch_for_each_port(sw, port) {
3167 if (!tb_port_is_null(port))
3170 if (!tb_port_resume(port))
3173 if (tb_wait_for_port(port, true) <= 0) {
3174 tb_port_warn(port,
3176 if (tb_port_has_remote(port))
3177 tb_sw_set_unplugged(port->remote->sw);
3178 else if (port->xdomain)
3179 port->xdomain->is_unplugged = true;
3182 * Always unlock the port so the downstream
3185 if (tb_port_unlock(port))
3186 tb_port_warn(port, "failed to unlock port\n");
3187 if (port->remote && tb_switch_resume(port->remote->sw)) {
3188 tb_port_warn(port,
3190 tb_sw_set_unplugged(port->remote->sw);
3210 struct tb_port *port;
3225 tb_switch_for_each_port(sw, port) {
3226 if (tb_port_has_remote(port))
3227 tb_switch_suspend(port->remote->sw, runtime);
3250 * @in: DP IN port
3265 * @in: DP IN port
3281 tb_sw_warn(sw, "failed to allocate DP resource for port %d\n",
3282 in->port);
3284 tb_sw_dbg(sw, "allocated DP resource for port %d\n", in->port);
3292 * @in: DP IN port
3307 tb_sw_warn(sw, "failed to de-allocate DP resource for port %d\n",
3308 in->port);
3310 tb_sw_dbg(sw, "released DP resource for port %d\n", in->port);
3424 * tb_switch_find_port() - return the first port of @type on @sw or NULL
3425 * @sw: Switch to find the port from
3431 struct tb_port *port;
3433 tb_switch_for_each_port(sw, port) {
3434 if (port->config.type == type)
3435 return port;
3526 * connected to the type-C port. Call only after PCIe tunnel has been
3549 /* Figure out correct USB port to connect */