Lines Matching refs:tb

14 #include "tb.h"
36 static inline struct tb *tcm_to_tb(struct tb_cm *tcm)
38 return ((void *)tcm - sizeof(struct tb));
43 struct tb *tb;
51 static void tb_queue_hotplug(struct tb *tb, u64 route, u8 port, bool unplug)
59 ev->tb = tb;
64 queue_work(tb->wq, &ev->work);
71 struct tb_cm *tcm = tb_priv(sw->tb);
88 struct tb_cm *tcm = tb_priv(sw->tb);
107 struct tb *tb = sw->tb;
108 struct tb_cm *tcm = tb_priv(tb);
116 tunnel = tb_tunnel_discover_dp(tb, port);
120 tunnel = tb_tunnel_discover_pci(tb, port);
124 tunnel = tb_tunnel_discover_usb3(tb, port);
182 struct tb *tb = sw->tb;
187 xd = tb_xdomain_find_by_route(tb, route);
193 xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid,
256 static struct tb_tunnel *tb_find_tunnel(struct tb *tb, enum tb_tunnel_type type,
260 struct tb_cm *tcm = tb_priv(tb);
274 static struct tb_tunnel *tb_find_first_usb3_tunnel(struct tb *tb,
288 if (sw == tb->root_switch)
292 port = tb_port_at(tb_route(sw), tb->root_switch);
294 usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port);
298 return tb_find_tunnel(tb, TB_TUNNEL_USB3, usb3_down, NULL);
301 static int tb_available_bandwidth(struct tb *tb, struct tb_port *src_port,
305 struct tb_cm *tcm = tb_priv(tb);
311 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
394 static int tb_release_unused_usb3_bandwidth(struct tb *tb,
400 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
404 static void tb_reclaim_usb3_bandwidth(struct tb *tb, struct tb_port *src_port,
410 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
414 tb_dbg(tb, "reclaiming unused bandwidth for USB3\n");
420 ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port,
423 tb_warn(tb, "failed to calculate available bandwidth\n");
427 tb_dbg(tb, "available bandwidth for USB3 %d/%d Mb/s\n",
433 static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw)
438 struct tb_cm *tcm = tb_priv(tb);
469 ret = tb_release_unused_usb3_bandwidth(tb, down, up);
474 ret = tb_available_bandwidth(tb, down, up, &available_up,
482 tunnel = tb_tunnel_alloc_usb3(tb, up, down, available_up,
498 tb_reclaim_usb3_bandwidth(tb, down, up);
506 tb_reclaim_usb3_bandwidth(tb, down, up);
517 ret = tb_tunnel_usb3(sw->tb, sw);
556 struct tb_cm *tcm = tb_priv(port->sw->tb);
566 tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port,
587 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
660 if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw))
670 struct tb *tb;
678 tb = tunnel->tb;
697 tb_reclaim_usb3_bandwidth(tb, src_port, dst_port);
714 static void tb_free_invalid_tunnels(struct tb *tb)
716 struct tb_cm *tcm = tb_priv(tb);
801 static struct tb_port *tb_find_dp_out(struct tb *tb, struct tb_port *in)
804 struct tb_cm *tcm = tb_priv(tb);
807 tb_port_at(tb_route(in->sw), tb->root_switch) : NULL;
827 p = tb_port_at(tb_route(port->sw), tb->root_switch);
838 static void tb_tunnel_dp(struct tb *tb)
841 struct tb_cm *tcm = tb_priv(tb);
849 tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n");
864 out = tb_find_dp_out(tb, port);
872 tb_dbg(tb, "no suitable DP IN adapter available, not tunneling\n");
876 tb_dbg(tb, "no suitable DP OUT adapter available, not tunneling\n");
897 ret = tb_release_unused_usb3_bandwidth(tb, in, out);
899 tb_warn(tb, "failed to release unused bandwidth\n");
903 ret = tb_available_bandwidth(tb, in, out, &available_up,
908 tb_dbg(tb, "available bandwidth for new DP tunnel %u/%u Mb/s\n",
911 tunnel = tb_tunnel_alloc_dp(tb, in, out, available_up, available_down);
923 tb_reclaim_usb3_bandwidth(tb, in, out);
929 tb_reclaim_usb3_bandwidth(tb, in, out);
939 static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port)
954 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, out);
962 tb_tunnel_dp(tb);
965 static void tb_dp_resource_available(struct tb *tb, struct tb_port *port)
967 struct tb_cm *tcm = tb_priv(tb);
983 tb_tunnel_dp(tb);
986 static void tb_disconnect_and_release_dp(struct tb *tb)
988 struct tb_cm *tcm = tb_priv(tb);
1009 static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
1012 struct tb_cm *tcm = tb_priv(tb);
1030 tunnel = tb_tunnel_alloc_pci(tb, up, down);
1045 static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1047 struct tb_cm *tcm = tb_priv(tb);
1054 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
1056 mutex_lock(&tb->lock);
1057 tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, xd->transmit_ring,
1061 mutex_unlock(&tb->lock);
1069 mutex_unlock(&tb->lock);
1074 mutex_unlock(&tb->lock);
1078 static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1092 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DMA, NULL, dst_port);
1096 static int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1099 mutex_lock(&tb->lock);
1100 __tb_disconnect_xdomain_paths(tb, xd);
1101 mutex_unlock(&tb->lock);
1111 * Executes on tb->wq.
1116 struct tb *tb = ev->tb;
1117 struct tb_cm *tcm = tb_priv(tb);
1122 pm_runtime_get_sync(&tb->dev);
1124 mutex_lock(&tb->lock);
1128 sw = tb_switch_find_by_route(tb, ev->route);
1130 tb_warn(tb,
1136 tb_warn(tb,
1143 tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n",
1156 tb_free_invalid_tunnels(tb);
1166 tb_tunnel_dp(tb);
1181 __tb_disconnect_xdomain_paths(tb, xd);
1185 tb_dp_resource_unavailable(tb, port);
1199 tb_dp_resource_available(tb, port);
1209 mutex_unlock(&tb->lock);
1211 pm_runtime_mark_last_busy(&tb->dev);
1212 pm_runtime_put_autosuspend(&tb->dev);
1222 static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1229 tb_warn(tb, "unexpected event %#x, ignoring\n", type);
1235 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) {
1236 tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
1240 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug);
1243 static void tb_stop(struct tb *tb)
1245 struct tb_cm *tcm = tb_priv(tb);
1261 tb_switch_remove(tb->root_switch);
1286 static int tb_start(struct tb *tb)
1288 struct tb_cm *tcm = tb_priv(tb);
1291 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
1292 if (IS_ERR(tb->root_switch))
1293 return PTR_ERR(tb->root_switch);
1300 tb->root_switch->no_nvm_upgrade = true;
1302 tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch);
1304 ret = tb_switch_configure(tb->root_switch);
1306 tb_switch_put(tb->root_switch);
1311 ret = tb_switch_add(tb->root_switch);
1313 tb_switch_put(tb->root_switch);
1318 tb_switch_tmu_enable(tb->root_switch);
1320 tb_scan_switch(tb->root_switch);
1322 tb_discover_tunnels(tb->root_switch);
1327 tb_create_usb3_tunnels(tb->root_switch);
1329 tb_add_dp_resources(tb->root_switch);
1331 device_for_each_child(&tb->root_switch->dev, NULL,
1339 static int tb_suspend_noirq(struct tb *tb)
1341 struct tb_cm *tcm = tb_priv(tb);
1343 tb_dbg(tb, "suspending...\n");
1344 tb_disconnect_and_release_dp(tb);
1345 tb_switch_suspend(tb->root_switch, false);
1347 tb_dbg(tb, "suspend finished\n");
1378 static int tb_resume_noirq(struct tb *tb)
1380 struct tb_cm *tcm = tb_priv(tb);
1383 tb_dbg(tb, "resuming...\n");
1386 tb_switch_reset(tb->root_switch);
1388 tb_switch_resume(tb->root_switch);
1389 tb_free_invalid_tunnels(tb);
1390 tb_free_unplugged_children(tb->root_switch);
1391 tb_restore_children(tb->root_switch);
1399 tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n");
1404 tb_dbg(tb, "resume finished\n");
1431 static int tb_freeze_noirq(struct tb *tb)
1433 struct tb_cm *tcm = tb_priv(tb);
1439 static int tb_thaw_noirq(struct tb *tb)
1441 struct tb_cm *tcm = tb_priv(tb);
1447 static void tb_complete(struct tb *tb)
1454 mutex_lock(&tb->lock);
1455 if (tb_free_unplugged_xdomains(tb->root_switch))
1456 tb_scan_switch(tb->root_switch);
1457 mutex_unlock(&tb->lock);
1460 static int tb_runtime_suspend(struct tb *tb)
1462 struct tb_cm *tcm = tb_priv(tb);
1464 mutex_lock(&tb->lock);
1465 tb_switch_suspend(tb->root_switch, true);
1467 mutex_unlock(&tb->lock);
1475 struct tb *tb = tcm_to_tb(tcm);
1477 mutex_lock(&tb->lock);
1478 if (tb->root_switch) {
1479 tb_free_unplugged_children(tb->root_switch);
1480 tb_free_unplugged_xdomains(tb->root_switch);
1482 mutex_unlock(&tb->lock);
1485 static int tb_runtime_resume(struct tb *tb)
1487 struct tb_cm *tcm = tb_priv(tb);
1490 mutex_lock(&tb->lock);
1491 tb_switch_resume(tb->root_switch);
1492 tb_free_invalid_tunnels(tb);
1493 tb_restore_children(tb->root_switch);
1497 mutex_unlock(&tb->lock);
1504 queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50));
1524 struct tb *tb_probe(struct tb_nhi *nhi)
1527 struct tb *tb;
1529 tb = tb_domain_alloc(nhi, sizeof(*tcm));
1530 if (!tb)
1533 tb->security_level = TB_SECURITY_USER;
1534 tb->cm_ops = &tb_cm_ops;
1536 tcm = tb_priv(tb);
1541 return tb;