Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 2 | /* |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 3 | * Thunderbolt driver - bus logic (NHI independent) |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 6 | * Copyright (C) 2019, Intel Corporation |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/errno.h> |
| 11 | #include <linux/delay.h> |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 12 | #include <linux/pm_runtime.h> |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 13 | |
| 14 | #include "tb.h" |
Andreas Noever | 7adf609 | 2014-06-03 22:04:01 +0200 | [diff] [blame] | 15 | #include "tb_regs.h" |
Mika Westerberg | 1752b9f | 2017-02-19 10:58:35 +0200 | [diff] [blame] | 16 | #include "tunnel.h" |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 17 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 18 | /** |
| 19 | * struct tb_cm - Simple Thunderbolt connection manager |
| 20 | * @tunnel_list: List of active tunnels |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 21 | * @dp_resources: List of available DP resources for DP tunneling |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 22 | * @hotplug_active: tb_handle_hotplug will stop progressing plug |
| 23 | * events and exit if this is not set (it needs to |
| 24 | * acquire the lock one more time). Used to drain wq |
| 25 | * after cfg has been paused. |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 26 | * @remove_work: Work used to remove any unplugged routers after |
| 27 | * runtime resume |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 28 | */ |
| 29 | struct tb_cm { |
| 30 | struct list_head tunnel_list; |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 31 | struct list_head dp_resources; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 32 | bool hotplug_active; |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 33 | struct delayed_work remove_work; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 34 | }; |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 35 | |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 36 | static inline struct tb *tcm_to_tb(struct tb_cm *tcm) |
| 37 | { |
| 38 | return ((void *)tcm - sizeof(struct tb)); |
| 39 | } |
| 40 | |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 41 | struct tb_hotplug_event { |
| 42 | struct work_struct work; |
| 43 | struct tb *tb; |
| 44 | u64 route; |
| 45 | u8 port; |
| 46 | bool unplug; |
| 47 | }; |
| 48 | |
| 49 | static void tb_handle_hotplug(struct work_struct *work); |
| 50 | |
| 51 | static void tb_queue_hotplug(struct tb *tb, u64 route, u8 port, bool unplug) |
| 52 | { |
| 53 | struct tb_hotplug_event *ev; |
| 54 | |
| 55 | ev = kmalloc(sizeof(*ev), GFP_KERNEL); |
| 56 | if (!ev) |
| 57 | return; |
| 58 | |
| 59 | ev->tb = tb; |
| 60 | ev->route = route; |
| 61 | ev->port = port; |
| 62 | ev->unplug = unplug; |
| 63 | INIT_WORK(&ev->work, tb_handle_hotplug); |
| 64 | queue_work(tb->wq, &ev->work); |
| 65 | } |
| 66 | |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 67 | /* enumeration & hot plug handling */ |
| 68 | |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 69 | static void tb_add_dp_resources(struct tb_switch *sw) |
| 70 | { |
| 71 | struct tb_cm *tcm = tb_priv(sw->tb); |
| 72 | struct tb_port *port; |
| 73 | |
| 74 | tb_switch_for_each_port(sw, port) { |
| 75 | if (!tb_port_is_dpin(port)) |
| 76 | continue; |
| 77 | |
| 78 | if (!tb_switch_query_dp_resource(sw, port)) |
| 79 | continue; |
| 80 | |
| 81 | list_add_tail(&port->list, &tcm->dp_resources); |
| 82 | tb_port_dbg(port, "DP IN resource available\n"); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | static void tb_remove_dp_resources(struct tb_switch *sw) |
| 87 | { |
| 88 | struct tb_cm *tcm = tb_priv(sw->tb); |
| 89 | struct tb_port *port, *tmp; |
| 90 | |
| 91 | /* Clear children resources first */ |
| 92 | tb_switch_for_each_port(sw, port) { |
| 93 | if (tb_port_has_remote(port)) |
| 94 | tb_remove_dp_resources(port->remote->sw); |
| 95 | } |
| 96 | |
| 97 | list_for_each_entry_safe(port, tmp, &tcm->dp_resources, list) { |
| 98 | if (port->sw == sw) { |
| 99 | tb_port_dbg(port, "DP OUT resource unavailable\n"); |
| 100 | list_del_init(&port->list); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 105 | static void tb_discover_tunnels(struct tb_switch *sw) |
| 106 | { |
| 107 | struct tb *tb = sw->tb; |
| 108 | struct tb_cm *tcm = tb_priv(tb); |
| 109 | struct tb_port *port; |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 110 | |
Mika Westerberg | b433d01 | 2019-09-30 14:07:22 +0300 | [diff] [blame] | 111 | tb_switch_for_each_port(sw, port) { |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 112 | struct tb_tunnel *tunnel = NULL; |
| 113 | |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 114 | switch (port->config.type) { |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 115 | case TB_TYPE_DP_HDMI_IN: |
| 116 | tunnel = tb_tunnel_discover_dp(tb, port); |
| 117 | break; |
| 118 | |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 119 | case TB_TYPE_PCIE_DOWN: |
| 120 | tunnel = tb_tunnel_discover_pci(tb, port); |
| 121 | break; |
| 122 | |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 123 | case TB_TYPE_USB3_DOWN: |
| 124 | tunnel = tb_tunnel_discover_usb3(tb, port); |
| 125 | break; |
| 126 | |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 131 | if (!tunnel) |
| 132 | continue; |
| 133 | |
| 134 | if (tb_tunnel_is_pci(tunnel)) { |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 135 | struct tb_switch *parent = tunnel->dst_port->sw; |
| 136 | |
| 137 | while (parent != tunnel->src_port->sw) { |
| 138 | parent->boot = true; |
| 139 | parent = tb_switch_parent(parent); |
| 140 | } |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 141 | } |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 142 | |
| 143 | list_add_tail(&tunnel->list, &tcm->tunnel_list); |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Mika Westerberg | b433d01 | 2019-09-30 14:07:22 +0300 | [diff] [blame] | 146 | tb_switch_for_each_port(sw, port) { |
| 147 | if (tb_port_has_remote(port)) |
| 148 | tb_discover_tunnels(port->remote->sw); |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 149 | } |
| 150 | } |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 151 | |
Mika Westerberg | 284652a | 2020-04-09 14:23:32 +0300 | [diff] [blame] | 152 | static int tb_port_configure_xdomain(struct tb_port *port) |
| 153 | { |
Mika Westerberg | 341d451 | 2020-02-21 12:11:54 +0200 | [diff] [blame] | 154 | /* |
| 155 | * XDomain paths currently only support single lane so we must |
| 156 | * disable the other lane according to USB4 spec. |
| 157 | */ |
| 158 | tb_port_disable(port->dual_link_port); |
| 159 | |
Mika Westerberg | 284652a | 2020-04-09 14:23:32 +0300 | [diff] [blame] | 160 | if (tb_switch_is_usb4(port->sw)) |
| 161 | return usb4_port_configure_xdomain(port); |
| 162 | return tb_lc_configure_xdomain(port); |
| 163 | } |
| 164 | |
| 165 | static void tb_port_unconfigure_xdomain(struct tb_port *port) |
| 166 | { |
| 167 | if (tb_switch_is_usb4(port->sw)) |
| 168 | usb4_port_unconfigure_xdomain(port); |
| 169 | else |
| 170 | tb_lc_unconfigure_xdomain(port); |
Mika Westerberg | 341d451 | 2020-02-21 12:11:54 +0200 | [diff] [blame] | 171 | |
| 172 | tb_port_enable(port->dual_link_port); |
Mika Westerberg | 284652a | 2020-04-09 14:23:32 +0300 | [diff] [blame] | 173 | } |
| 174 | |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 175 | static void tb_scan_xdomain(struct tb_port *port) |
| 176 | { |
| 177 | struct tb_switch *sw = port->sw; |
| 178 | struct tb *tb = sw->tb; |
| 179 | struct tb_xdomain *xd; |
| 180 | u64 route; |
| 181 | |
| 182 | route = tb_downstream_route(port); |
| 183 | xd = tb_xdomain_find_by_route(tb, route); |
| 184 | if (xd) { |
| 185 | tb_xdomain_put(xd); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid, |
| 190 | NULL); |
| 191 | if (xd) { |
| 192 | tb_port_at(route, sw)->xdomain = xd; |
Mika Westerberg | 284652a | 2020-04-09 14:23:32 +0300 | [diff] [blame] | 193 | tb_port_configure_xdomain(port); |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 194 | tb_xdomain_add(xd); |
| 195 | } |
| 196 | } |
| 197 | |
Rajmohan Mani | cf29b9af | 2019-12-17 15:33:43 +0300 | [diff] [blame] | 198 | static int tb_enable_tmu(struct tb_switch *sw) |
| 199 | { |
| 200 | int ret; |
| 201 | |
| 202 | /* If it is already enabled in correct mode, don't touch it */ |
| 203 | if (tb_switch_tmu_is_enabled(sw)) |
| 204 | return 0; |
| 205 | |
| 206 | ret = tb_switch_tmu_disable(sw); |
| 207 | if (ret) |
| 208 | return ret; |
| 209 | |
| 210 | ret = tb_switch_tmu_post_time(sw); |
| 211 | if (ret) |
| 212 | return ret; |
| 213 | |
| 214 | return tb_switch_tmu_enable(sw); |
| 215 | } |
| 216 | |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 217 | /** |
| 218 | * tb_find_unused_port() - return the first inactive port on @sw |
| 219 | * @sw: Switch to find the port on |
| 220 | * @type: Port type to look for |
| 221 | */ |
| 222 | static struct tb_port *tb_find_unused_port(struct tb_switch *sw, |
| 223 | enum tb_port_type type) |
| 224 | { |
| 225 | struct tb_port *port; |
| 226 | |
| 227 | tb_switch_for_each_port(sw, port) { |
| 228 | if (tb_is_upstream_port(port)) |
| 229 | continue; |
| 230 | if (port->config.type != type) |
| 231 | continue; |
| 232 | if (!port->cap_adap) |
| 233 | continue; |
| 234 | if (tb_port_is_enabled(port)) |
| 235 | continue; |
| 236 | return port; |
| 237 | } |
| 238 | return NULL; |
| 239 | } |
| 240 | |
| 241 | static struct tb_port *tb_find_usb3_down(struct tb_switch *sw, |
Mika Westerberg | 77cfa40 | 2020-03-11 16:00:46 +0300 | [diff] [blame] | 242 | const struct tb_port *port) |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 243 | { |
| 244 | struct tb_port *down; |
| 245 | |
| 246 | down = usb4_switch_map_usb3_down(sw, port); |
Mika Westerberg | 77cfa40 | 2020-03-11 16:00:46 +0300 | [diff] [blame] | 247 | if (down && !tb_usb3_port_is_enabled(down)) |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 248 | return down; |
Mika Westerberg | 77cfa40 | 2020-03-11 16:00:46 +0300 | [diff] [blame] | 249 | return NULL; |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 250 | } |
| 251 | |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 252 | static struct tb_tunnel *tb_find_tunnel(struct tb *tb, enum tb_tunnel_type type, |
| 253 | struct tb_port *src_port, |
| 254 | struct tb_port *dst_port) |
| 255 | { |
| 256 | struct tb_cm *tcm = tb_priv(tb); |
| 257 | struct tb_tunnel *tunnel; |
| 258 | |
| 259 | list_for_each_entry(tunnel, &tcm->tunnel_list, list) { |
| 260 | if (tunnel->type == type && |
| 261 | ((src_port && src_port == tunnel->src_port) || |
| 262 | (dst_port && dst_port == tunnel->dst_port))) { |
| 263 | return tunnel; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | return NULL; |
| 268 | } |
| 269 | |
| 270 | static struct tb_tunnel *tb_find_first_usb3_tunnel(struct tb *tb, |
| 271 | struct tb_port *src_port, |
| 272 | struct tb_port *dst_port) |
| 273 | { |
| 274 | struct tb_port *port, *usb3_down; |
| 275 | struct tb_switch *sw; |
| 276 | |
| 277 | /* Pick the router that is deepest in the topology */ |
| 278 | if (dst_port->sw->config.depth > src_port->sw->config.depth) |
| 279 | sw = dst_port->sw; |
| 280 | else |
| 281 | sw = src_port->sw; |
| 282 | |
| 283 | /* Can't be the host router */ |
| 284 | if (sw == tb->root_switch) |
| 285 | return NULL; |
| 286 | |
| 287 | /* Find the downstream USB4 port that leads to this router */ |
| 288 | port = tb_port_at(tb_route(sw), tb->root_switch); |
| 289 | /* Find the corresponding host router USB3 downstream port */ |
| 290 | usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port); |
| 291 | if (!usb3_down) |
| 292 | return NULL; |
| 293 | |
| 294 | return tb_find_tunnel(tb, TB_TUNNEL_USB3, usb3_down, NULL); |
| 295 | } |
| 296 | |
| 297 | static int tb_available_bandwidth(struct tb *tb, struct tb_port *src_port, |
| 298 | struct tb_port *dst_port, int *available_up, int *available_down) |
| 299 | { |
| 300 | int usb3_consumed_up, usb3_consumed_down, ret; |
| 301 | struct tb_cm *tcm = tb_priv(tb); |
| 302 | struct tb_tunnel *tunnel; |
| 303 | struct tb_port *port; |
| 304 | |
| 305 | tb_port_dbg(dst_port, "calculating available bandwidth\n"); |
| 306 | |
| 307 | tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port); |
| 308 | if (tunnel) { |
| 309 | ret = tb_tunnel_consumed_bandwidth(tunnel, &usb3_consumed_up, |
| 310 | &usb3_consumed_down); |
| 311 | if (ret) |
| 312 | return ret; |
| 313 | } else { |
| 314 | usb3_consumed_up = 0; |
| 315 | usb3_consumed_down = 0; |
| 316 | } |
| 317 | |
| 318 | *available_up = *available_down = 40000; |
| 319 | |
| 320 | /* Find the minimum available bandwidth over all links */ |
| 321 | tb_for_each_port_on_path(src_port, dst_port, port) { |
| 322 | int link_speed, link_width, up_bw, down_bw; |
| 323 | |
| 324 | if (!tb_port_is_null(port)) |
| 325 | continue; |
| 326 | |
| 327 | if (tb_is_upstream_port(port)) { |
| 328 | link_speed = port->sw->link_speed; |
| 329 | } else { |
| 330 | link_speed = tb_port_get_link_speed(port); |
| 331 | if (link_speed < 0) |
| 332 | return link_speed; |
| 333 | } |
| 334 | |
| 335 | link_width = port->bonded ? 2 : 1; |
| 336 | |
| 337 | up_bw = link_speed * link_width * 1000; /* Mb/s */ |
| 338 | /* Leave 10% guard band */ |
| 339 | up_bw -= up_bw / 10; |
| 340 | down_bw = up_bw; |
| 341 | |
| 342 | tb_port_dbg(port, "link total bandwidth %d Mb/s\n", up_bw); |
| 343 | |
| 344 | /* |
| 345 | * Find all DP tunnels that cross the port and reduce |
| 346 | * their consumed bandwidth from the available. |
| 347 | */ |
| 348 | list_for_each_entry(tunnel, &tcm->tunnel_list, list) { |
| 349 | int dp_consumed_up, dp_consumed_down; |
| 350 | |
| 351 | if (!tb_tunnel_is_dp(tunnel)) |
| 352 | continue; |
| 353 | |
| 354 | if (!tb_tunnel_port_on_path(tunnel, port)) |
| 355 | continue; |
| 356 | |
| 357 | ret = tb_tunnel_consumed_bandwidth(tunnel, |
| 358 | &dp_consumed_up, |
| 359 | &dp_consumed_down); |
| 360 | if (ret) |
| 361 | return ret; |
| 362 | |
| 363 | up_bw -= dp_consumed_up; |
| 364 | down_bw -= dp_consumed_down; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * If USB3 is tunneled from the host router down to the |
| 369 | * branch leading to port we need to take USB3 consumed |
| 370 | * bandwidth into account regardless whether it actually |
| 371 | * crosses the port. |
| 372 | */ |
| 373 | up_bw -= usb3_consumed_up; |
| 374 | down_bw -= usb3_consumed_down; |
| 375 | |
| 376 | if (up_bw < *available_up) |
| 377 | *available_up = up_bw; |
| 378 | if (down_bw < *available_down) |
| 379 | *available_down = down_bw; |
| 380 | } |
| 381 | |
| 382 | if (*available_up < 0) |
| 383 | *available_up = 0; |
| 384 | if (*available_down < 0) |
| 385 | *available_down = 0; |
| 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static int tb_release_unused_usb3_bandwidth(struct tb *tb, |
| 391 | struct tb_port *src_port, |
| 392 | struct tb_port *dst_port) |
| 393 | { |
| 394 | struct tb_tunnel *tunnel; |
| 395 | |
| 396 | tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port); |
| 397 | return tunnel ? tb_tunnel_release_unused_bandwidth(tunnel) : 0; |
| 398 | } |
| 399 | |
| 400 | static void tb_reclaim_usb3_bandwidth(struct tb *tb, struct tb_port *src_port, |
| 401 | struct tb_port *dst_port) |
| 402 | { |
| 403 | int ret, available_up, available_down; |
| 404 | struct tb_tunnel *tunnel; |
| 405 | |
| 406 | tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port); |
| 407 | if (!tunnel) |
| 408 | return; |
| 409 | |
| 410 | tb_dbg(tb, "reclaiming unused bandwidth for USB3\n"); |
| 411 | |
| 412 | /* |
| 413 | * Calculate available bandwidth for the first hop USB3 tunnel. |
| 414 | * That determines the whole USB3 bandwidth for this branch. |
| 415 | */ |
| 416 | ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port, |
| 417 | &available_up, &available_down); |
| 418 | if (ret) { |
| 419 | tb_warn(tb, "failed to calculate available bandwidth\n"); |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | tb_dbg(tb, "available bandwidth for USB3 %d/%d Mb/s\n", |
| 424 | available_up, available_down); |
| 425 | |
| 426 | tb_tunnel_reclaim_available_bandwidth(tunnel, &available_up, &available_down); |
| 427 | } |
| 428 | |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 429 | static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw) |
| 430 | { |
| 431 | struct tb_switch *parent = tb_switch_parent(sw); |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 432 | int ret, available_up, available_down; |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 433 | struct tb_port *up, *down, *port; |
| 434 | struct tb_cm *tcm = tb_priv(tb); |
| 435 | struct tb_tunnel *tunnel; |
| 436 | |
| 437 | up = tb_switch_find_port(sw, TB_TYPE_USB3_UP); |
| 438 | if (!up) |
| 439 | return 0; |
| 440 | |
Mika Westerberg | bbcf40b | 2020-03-04 17:09:14 +0200 | [diff] [blame] | 441 | if (!sw->link_usb4) |
| 442 | return 0; |
| 443 | |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 444 | /* |
| 445 | * Look up available down port. Since we are chaining it should |
| 446 | * be found right above this switch. |
| 447 | */ |
| 448 | port = tb_port_at(tb_route(sw), parent); |
| 449 | down = tb_find_usb3_down(parent, port); |
| 450 | if (!down) |
| 451 | return 0; |
| 452 | |
| 453 | if (tb_route(parent)) { |
| 454 | struct tb_port *parent_up; |
| 455 | /* |
| 456 | * Check first that the parent switch has its upstream USB3 |
| 457 | * port enabled. Otherwise the chain is not complete and |
| 458 | * there is no point setting up a new tunnel. |
| 459 | */ |
| 460 | parent_up = tb_switch_find_port(parent, TB_TYPE_USB3_UP); |
| 461 | if (!parent_up || !tb_port_is_enabled(parent_up)) |
| 462 | return 0; |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 463 | |
| 464 | /* Make all unused bandwidth available for the new tunnel */ |
| 465 | ret = tb_release_unused_usb3_bandwidth(tb, down, up); |
| 466 | if (ret) |
| 467 | return ret; |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 468 | } |
| 469 | |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 470 | ret = tb_available_bandwidth(tb, down, up, &available_up, |
| 471 | &available_down); |
| 472 | if (ret) |
| 473 | goto err_reclaim; |
| 474 | |
| 475 | tb_port_dbg(up, "available bandwidth for new USB3 tunnel %d/%d Mb/s\n", |
| 476 | available_up, available_down); |
| 477 | |
| 478 | tunnel = tb_tunnel_alloc_usb3(tb, up, down, available_up, |
| 479 | available_down); |
| 480 | if (!tunnel) { |
| 481 | ret = -ENOMEM; |
| 482 | goto err_reclaim; |
| 483 | } |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 484 | |
| 485 | if (tb_tunnel_activate(tunnel)) { |
| 486 | tb_port_info(up, |
| 487 | "USB3 tunnel activation failed, aborting\n"); |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 488 | ret = -EIO; |
| 489 | goto err_free; |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | list_add_tail(&tunnel->list, &tcm->tunnel_list); |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 493 | if (tb_route(parent)) |
| 494 | tb_reclaim_usb3_bandwidth(tb, down, up); |
| 495 | |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 496 | return 0; |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 497 | |
| 498 | err_free: |
| 499 | tb_tunnel_free(tunnel); |
| 500 | err_reclaim: |
| 501 | if (tb_route(parent)) |
| 502 | tb_reclaim_usb3_bandwidth(tb, down, up); |
| 503 | |
| 504 | return ret; |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | static int tb_create_usb3_tunnels(struct tb_switch *sw) |
| 508 | { |
| 509 | struct tb_port *port; |
| 510 | int ret; |
| 511 | |
| 512 | if (tb_route(sw)) { |
| 513 | ret = tb_tunnel_usb3(sw->tb, sw); |
| 514 | if (ret) |
| 515 | return ret; |
| 516 | } |
| 517 | |
| 518 | tb_switch_for_each_port(sw, port) { |
| 519 | if (!tb_port_has_remote(port)) |
| 520 | continue; |
| 521 | ret = tb_create_usb3_tunnels(port->remote->sw); |
| 522 | if (ret) |
| 523 | return ret; |
| 524 | } |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 529 | static void tb_scan_port(struct tb_port *port); |
| 530 | |
| 531 | /** |
| 532 | * tb_scan_switch() - scan for and initialize downstream switches |
| 533 | */ |
| 534 | static void tb_scan_switch(struct tb_switch *sw) |
| 535 | { |
Mika Westerberg | b433d01 | 2019-09-30 14:07:22 +0300 | [diff] [blame] | 536 | struct tb_port *port; |
| 537 | |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 538 | pm_runtime_get_sync(&sw->dev); |
| 539 | |
Mika Westerberg | b433d01 | 2019-09-30 14:07:22 +0300 | [diff] [blame] | 540 | tb_switch_for_each_port(sw, port) |
| 541 | tb_scan_port(port); |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 542 | |
| 543 | pm_runtime_mark_last_busy(&sw->dev); |
| 544 | pm_runtime_put_autosuspend(&sw->dev); |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /** |
| 548 | * tb_scan_port() - check for and initialize switches below port |
| 549 | */ |
| 550 | static void tb_scan_port(struct tb_port *port) |
| 551 | { |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 552 | struct tb_cm *tcm = tb_priv(port->sw->tb); |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 553 | struct tb_port *upstream_port; |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 554 | struct tb_switch *sw; |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 555 | |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 556 | if (tb_is_upstream_port(port)) |
| 557 | return; |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 558 | |
| 559 | if (tb_port_is_dpout(port) && tb_dp_port_hpd_is_active(port) == 1 && |
| 560 | !tb_dp_port_is_enabled(port)) { |
| 561 | tb_port_dbg(port, "DP adapter HPD set, queuing hotplug\n"); |
| 562 | tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port, |
| 563 | false); |
| 564 | return; |
| 565 | } |
| 566 | |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 567 | if (port->config.type != TB_TYPE_PORT) |
| 568 | return; |
Andreas Noever | 343fcb8 | 2014-06-12 23:11:47 +0200 | [diff] [blame] | 569 | if (port->dual_link_port && port->link_nr) |
| 570 | return; /* |
| 571 | * Downstream switch is reachable through two ports. |
| 572 | * Only scan on the primary port (link_nr == 0). |
| 573 | */ |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 574 | if (tb_wait_for_port(port, false) <= 0) |
| 575 | return; |
| 576 | if (port->remote) { |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 577 | tb_port_dbg(port, "port already has a remote\n"); |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 578 | return; |
| 579 | } |
Kranthi Kuntala | dacb128 | 2020-03-05 16:39:58 +0200 | [diff] [blame] | 580 | |
| 581 | tb_retimer_scan(port); |
| 582 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 583 | sw = tb_switch_alloc(port->sw->tb, &port->sw->dev, |
| 584 | tb_downstream_route(port)); |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 585 | if (IS_ERR(sw)) { |
| 586 | /* |
| 587 | * If there is an error accessing the connected switch |
| 588 | * it may be connected to another domain. Also we allow |
| 589 | * the other domain to be connected to a max depth switch. |
| 590 | */ |
| 591 | if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL) |
| 592 | tb_scan_xdomain(port); |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 593 | return; |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 594 | } |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 595 | |
| 596 | if (tb_switch_configure(sw)) { |
| 597 | tb_switch_put(sw); |
| 598 | return; |
| 599 | } |
| 600 | |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 601 | /* |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 602 | * If there was previously another domain connected remove it |
| 603 | * first. |
| 604 | */ |
| 605 | if (port->xdomain) { |
| 606 | tb_xdomain_remove(port->xdomain); |
Mika Westerberg | 284652a | 2020-04-09 14:23:32 +0300 | [diff] [blame] | 607 | tb_port_unconfigure_xdomain(port); |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 608 | port->xdomain = NULL; |
| 609 | } |
| 610 | |
| 611 | /* |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 612 | * Do not send uevents until we have discovered all existing |
| 613 | * tunnels and know which switches were authorized already by |
| 614 | * the boot firmware. |
| 615 | */ |
| 616 | if (!tcm->hotplug_active) |
| 617 | dev_set_uevent_suppress(&sw->dev, true); |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 618 | |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 619 | /* |
| 620 | * At the moment Thunderbolt 2 and beyond (devices with LC) we |
| 621 | * can support runtime PM. |
| 622 | */ |
| 623 | sw->rpm = sw->generation > 1; |
| 624 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 625 | if (tb_switch_add(sw)) { |
| 626 | tb_switch_put(sw); |
| 627 | return; |
| 628 | } |
| 629 | |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 630 | /* Link the switches using both links if available */ |
| 631 | upstream_port = tb_upstream_port(sw); |
| 632 | port->remote = upstream_port; |
| 633 | upstream_port->remote = port; |
| 634 | if (port->dual_link_port && upstream_port->dual_link_port) { |
| 635 | port->dual_link_port->remote = upstream_port->dual_link_port; |
| 636 | upstream_port->dual_link_port->remote = port->dual_link_port; |
| 637 | } |
| 638 | |
Mika Westerberg | 91c0c12 | 2019-03-21 19:03:00 +0200 | [diff] [blame] | 639 | /* Enable lane bonding if supported */ |
Mika Westerberg | 2ca3263 | 2020-04-02 14:28:18 +0300 | [diff] [blame] | 640 | tb_switch_lane_bonding_enable(sw); |
Mika Westerberg | de46203 | 2020-04-02 14:50:52 +0300 | [diff] [blame] | 641 | /* Set the link configured */ |
| 642 | tb_switch_configure_link(sw); |
Mika Westerberg | 91c0c12 | 2019-03-21 19:03:00 +0200 | [diff] [blame] | 643 | |
Rajmohan Mani | cf29b9af | 2019-12-17 15:33:43 +0300 | [diff] [blame] | 644 | if (tb_enable_tmu(sw)) |
| 645 | tb_sw_warn(sw, "failed to enable TMU\n"); |
| 646 | |
Kranthi Kuntala | dacb128 | 2020-03-05 16:39:58 +0200 | [diff] [blame] | 647 | /* Scan upstream retimers */ |
| 648 | tb_retimer_scan(upstream_port); |
| 649 | |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 650 | /* |
| 651 | * Create USB 3.x tunnels only when the switch is plugged to the |
| 652 | * domain. This is because we scan the domain also during discovery |
| 653 | * and want to discover existing USB 3.x tunnels before we create |
| 654 | * any new. |
| 655 | */ |
| 656 | if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw)) |
| 657 | tb_sw_warn(sw, "USB3 tunnel creation failed\n"); |
| 658 | |
Mika Westerberg | e876f34 | 2020-04-02 12:53:14 +0300 | [diff] [blame] | 659 | tb_add_dp_resources(sw); |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 660 | tb_scan_switch(sw); |
| 661 | } |
| 662 | |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 663 | static void tb_deactivate_and_free_tunnel(struct tb_tunnel *tunnel) |
| 664 | { |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 665 | struct tb_port *src_port, *dst_port; |
| 666 | struct tb *tb; |
| 667 | |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 668 | if (!tunnel) |
| 669 | return; |
| 670 | |
| 671 | tb_tunnel_deactivate(tunnel); |
| 672 | list_del(&tunnel->list); |
| 673 | |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 674 | tb = tunnel->tb; |
| 675 | src_port = tunnel->src_port; |
| 676 | dst_port = tunnel->dst_port; |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 677 | |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 678 | switch (tunnel->type) { |
| 679 | case TB_TUNNEL_DP: |
| 680 | /* |
| 681 | * In case of DP tunnel make sure the DP IN resource is |
| 682 | * deallocated properly. |
| 683 | */ |
| 684 | tb_switch_dealloc_dp_resource(src_port->sw, src_port); |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 685 | /* Now we can allow the domain to runtime suspend again */ |
| 686 | pm_runtime_mark_last_busy(&dst_port->sw->dev); |
| 687 | pm_runtime_put_autosuspend(&dst_port->sw->dev); |
| 688 | pm_runtime_mark_last_busy(&src_port->sw->dev); |
| 689 | pm_runtime_put_autosuspend(&src_port->sw->dev); |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 690 | fallthrough; |
| 691 | |
| 692 | case TB_TUNNEL_USB3: |
| 693 | tb_reclaim_usb3_bandwidth(tb, src_port, dst_port); |
| 694 | break; |
| 695 | |
| 696 | default: |
| 697 | /* |
| 698 | * PCIe and DMA tunnels do not consume guaranteed |
| 699 | * bandwidth. |
| 700 | */ |
| 701 | break; |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | tb_tunnel_free(tunnel); |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 705 | } |
| 706 | |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 707 | /** |
| 708 | * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away |
| 709 | */ |
| 710 | static void tb_free_invalid_tunnels(struct tb *tb) |
| 711 | { |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 712 | struct tb_cm *tcm = tb_priv(tb); |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 713 | struct tb_tunnel *tunnel; |
| 714 | struct tb_tunnel *n; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 715 | |
| 716 | list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 717 | if (tb_tunnel_is_invalid(tunnel)) |
| 718 | tb_deactivate_and_free_tunnel(tunnel); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
| 722 | /** |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 723 | * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches |
| 724 | */ |
| 725 | static void tb_free_unplugged_children(struct tb_switch *sw) |
| 726 | { |
Mika Westerberg | b433d01 | 2019-09-30 14:07:22 +0300 | [diff] [blame] | 727 | struct tb_port *port; |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 728 | |
Mika Westerberg | b433d01 | 2019-09-30 14:07:22 +0300 | [diff] [blame] | 729 | tb_switch_for_each_port(sw, port) { |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 730 | if (!tb_port_has_remote(port)) |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 731 | continue; |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 732 | |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 733 | if (port->remote->sw->is_unplugged) { |
Kranthi Kuntala | dacb128 | 2020-03-05 16:39:58 +0200 | [diff] [blame] | 734 | tb_retimer_remove_all(port); |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 735 | tb_remove_dp_resources(port->remote->sw); |
Mika Westerberg | de46203 | 2020-04-02 14:50:52 +0300 | [diff] [blame] | 736 | tb_switch_unconfigure_link(port->remote->sw); |
Mika Westerberg | 91c0c12 | 2019-03-21 19:03:00 +0200 | [diff] [blame] | 737 | tb_switch_lane_bonding_disable(port->remote->sw); |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 738 | tb_switch_remove(port->remote->sw); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 739 | port->remote = NULL; |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 740 | if (port->dual_link_port) |
| 741 | port->dual_link_port->remote = NULL; |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 742 | } else { |
| 743 | tb_free_unplugged_children(port->remote->sw); |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 748 | static struct tb_port *tb_find_pcie_down(struct tb_switch *sw, |
| 749 | const struct tb_port *port) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 750 | { |
Mika Westerberg | b040798 | 2019-12-17 15:33:40 +0300 | [diff] [blame] | 751 | struct tb_port *down = NULL; |
| 752 | |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 753 | /* |
| 754 | * To keep plugging devices consistently in the same PCIe |
Mika Westerberg | b040798 | 2019-12-17 15:33:40 +0300 | [diff] [blame] | 755 | * hierarchy, do mapping here for switch downstream PCIe ports. |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 756 | */ |
Mika Westerberg | b040798 | 2019-12-17 15:33:40 +0300 | [diff] [blame] | 757 | if (tb_switch_is_usb4(sw)) { |
| 758 | down = usb4_switch_map_pcie_down(sw, port); |
| 759 | } else if (!tb_route(sw)) { |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 760 | int phy_port = tb_phy_port_from_link(port->port); |
| 761 | int index; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 762 | |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 763 | /* |
| 764 | * Hard-coded Thunderbolt port to PCIe down port mapping |
| 765 | * per controller. |
| 766 | */ |
Mika Westerberg | 7bffd97e | 2019-03-22 15:16:53 +0200 | [diff] [blame] | 767 | if (tb_switch_is_cactus_ridge(sw) || |
| 768 | tb_switch_is_alpine_ridge(sw)) |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 769 | index = !phy_port ? 6 : 7; |
Mika Westerberg | 17a8f81 | 2019-10-08 16:42:47 +0300 | [diff] [blame] | 770 | else if (tb_switch_is_falcon_ridge(sw)) |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 771 | index = !phy_port ? 6 : 8; |
Mika Westerberg | 7bffd97e | 2019-03-22 15:16:53 +0200 | [diff] [blame] | 772 | else if (tb_switch_is_titan_ridge(sw)) |
| 773 | index = !phy_port ? 8 : 9; |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 774 | else |
| 775 | goto out; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 776 | |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 777 | /* Validate the hard-coding */ |
| 778 | if (WARN_ON(index > sw->config.max_port_number)) |
| 779 | goto out; |
Mika Westerberg | b040798 | 2019-12-17 15:33:40 +0300 | [diff] [blame] | 780 | |
| 781 | down = &sw->ports[index]; |
| 782 | } |
| 783 | |
| 784 | if (down) { |
| 785 | if (WARN_ON(!tb_port_is_pcie_down(down))) |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 786 | goto out; |
Mika Westerberg | 9cac51a | 2020-03-11 16:12:50 +0300 | [diff] [blame] | 787 | if (tb_pci_port_is_enabled(down)) |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 788 | goto out; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 789 | |
Mika Westerberg | b040798 | 2019-12-17 15:33:40 +0300 | [diff] [blame] | 790 | return down; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 791 | } |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 792 | |
| 793 | out: |
Mika Westerberg | e78db6f | 2017-10-12 16:45:50 +0300 | [diff] [blame] | 794 | return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN); |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 795 | } |
| 796 | |
Mika Westerberg | e876f34 | 2020-04-02 12:53:14 +0300 | [diff] [blame] | 797 | static struct tb_port *tb_find_dp_out(struct tb *tb, struct tb_port *in) |
| 798 | { |
| 799 | struct tb_port *host_port, *port; |
| 800 | struct tb_cm *tcm = tb_priv(tb); |
| 801 | |
| 802 | host_port = tb_route(in->sw) ? |
| 803 | tb_port_at(tb_route(in->sw), tb->root_switch) : NULL; |
| 804 | |
| 805 | list_for_each_entry(port, &tcm->dp_resources, list) { |
| 806 | if (!tb_port_is_dpout(port)) |
| 807 | continue; |
| 808 | |
| 809 | if (tb_port_is_enabled(port)) { |
| 810 | tb_port_dbg(port, "in use\n"); |
| 811 | continue; |
| 812 | } |
| 813 | |
| 814 | tb_port_dbg(port, "DP OUT available\n"); |
| 815 | |
| 816 | /* |
| 817 | * Keep the DP tunnel under the topology starting from |
| 818 | * the same host router downstream port. |
| 819 | */ |
| 820 | if (host_port && tb_route(port->sw)) { |
| 821 | struct tb_port *p; |
| 822 | |
| 823 | p = tb_port_at(tb_route(port->sw), tb->root_switch); |
| 824 | if (p != host_port) |
| 825 | continue; |
| 826 | } |
| 827 | |
| 828 | return port; |
| 829 | } |
| 830 | |
| 831 | return NULL; |
| 832 | } |
| 833 | |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 834 | static void tb_tunnel_dp(struct tb *tb) |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 835 | { |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 836 | int available_up, available_down, ret; |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 837 | struct tb_cm *tcm = tb_priv(tb); |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 838 | struct tb_port *port, *in, *out; |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 839 | struct tb_tunnel *tunnel; |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 840 | |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 841 | /* |
| 842 | * Find pair of inactive DP IN and DP OUT adapters and then |
| 843 | * establish a DP tunnel between them. |
| 844 | */ |
| 845 | tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n"); |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 846 | |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 847 | in = NULL; |
| 848 | out = NULL; |
| 849 | list_for_each_entry(port, &tcm->dp_resources, list) { |
Mika Westerberg | e876f34 | 2020-04-02 12:53:14 +0300 | [diff] [blame] | 850 | if (!tb_port_is_dpin(port)) |
| 851 | continue; |
| 852 | |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 853 | if (tb_port_is_enabled(port)) { |
| 854 | tb_port_dbg(port, "in use\n"); |
| 855 | continue; |
| 856 | } |
| 857 | |
Mika Westerberg | e876f34 | 2020-04-02 12:53:14 +0300 | [diff] [blame] | 858 | tb_port_dbg(port, "DP IN available\n"); |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 859 | |
Mika Westerberg | e876f34 | 2020-04-02 12:53:14 +0300 | [diff] [blame] | 860 | out = tb_find_dp_out(tb, port); |
| 861 | if (out) { |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 862 | in = port; |
Mika Westerberg | e876f34 | 2020-04-02 12:53:14 +0300 | [diff] [blame] | 863 | break; |
| 864 | } |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | if (!in) { |
| 868 | tb_dbg(tb, "no suitable DP IN adapter available, not tunneling\n"); |
| 869 | return; |
| 870 | } |
| 871 | if (!out) { |
| 872 | tb_dbg(tb, "no suitable DP OUT adapter available, not tunneling\n"); |
| 873 | return; |
| 874 | } |
| 875 | |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 876 | /* |
| 877 | * DP stream needs the domain to be active so runtime resume |
| 878 | * both ends of the tunnel. |
| 879 | * |
| 880 | * This should bring the routers in the middle active as well |
| 881 | * and keeps the domain from runtime suspending while the DP |
| 882 | * tunnel is active. |
| 883 | */ |
| 884 | pm_runtime_get_sync(&in->sw->dev); |
| 885 | pm_runtime_get_sync(&out->sw->dev); |
| 886 | |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 887 | if (tb_switch_alloc_dp_resource(in->sw, in)) { |
| 888 | tb_port_dbg(in, "no resource available for DP IN, not tunneling\n"); |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 889 | goto err_rpm_put; |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 890 | } |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 891 | |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 892 | /* Make all unused USB3 bandwidth available for the new DP tunnel */ |
| 893 | ret = tb_release_unused_usb3_bandwidth(tb, in, out); |
| 894 | if (ret) { |
| 895 | tb_warn(tb, "failed to release unused bandwidth\n"); |
| 896 | goto err_dealloc_dp; |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 897 | } |
| 898 | |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 899 | ret = tb_available_bandwidth(tb, in, out, &available_up, |
| 900 | &available_down); |
| 901 | if (ret) |
| 902 | goto err_reclaim; |
Mika Westerberg | a11b88a | 2019-03-26 16:03:48 +0300 | [diff] [blame] | 903 | |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 904 | tb_dbg(tb, "available bandwidth for new DP tunnel %u/%u Mb/s\n", |
| 905 | available_up, available_down); |
| 906 | |
| 907 | tunnel = tb_tunnel_alloc_dp(tb, in, out, available_up, available_down); |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 908 | if (!tunnel) { |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 909 | tb_port_dbg(out, "could not allocate DP tunnel\n"); |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 910 | goto err_reclaim; |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | if (tb_tunnel_activate(tunnel)) { |
| 914 | tb_port_info(out, "DP tunnel activation failed, aborting\n"); |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 915 | goto err_free; |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | list_add_tail(&tunnel->list, &tcm->tunnel_list); |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 919 | tb_reclaim_usb3_bandwidth(tb, in, out); |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 920 | return; |
| 921 | |
Mika Westerberg | 0bd680c | 2020-03-24 14:44:13 +0200 | [diff] [blame] | 922 | err_free: |
| 923 | tb_tunnel_free(tunnel); |
| 924 | err_reclaim: |
| 925 | tb_reclaim_usb3_bandwidth(tb, in, out); |
| 926 | err_dealloc_dp: |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 927 | tb_switch_dealloc_dp_resource(in->sw, in); |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 928 | err_rpm_put: |
| 929 | pm_runtime_mark_last_busy(&out->sw->dev); |
| 930 | pm_runtime_put_autosuspend(&out->sw->dev); |
| 931 | pm_runtime_mark_last_busy(&in->sw->dev); |
| 932 | pm_runtime_put_autosuspend(&in->sw->dev); |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 933 | } |
| 934 | |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 935 | static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port) |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 936 | { |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 937 | struct tb_port *in, *out; |
| 938 | struct tb_tunnel *tunnel; |
| 939 | |
| 940 | if (tb_port_is_dpin(port)) { |
| 941 | tb_port_dbg(port, "DP IN resource unavailable\n"); |
| 942 | in = port; |
| 943 | out = NULL; |
| 944 | } else { |
| 945 | tb_port_dbg(port, "DP OUT resource unavailable\n"); |
| 946 | in = NULL; |
| 947 | out = port; |
| 948 | } |
| 949 | |
| 950 | tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, out); |
| 951 | tb_deactivate_and_free_tunnel(tunnel); |
| 952 | list_del_init(&port->list); |
| 953 | |
| 954 | /* |
| 955 | * See if there is another DP OUT port that can be used for |
| 956 | * to create another tunnel. |
| 957 | */ |
| 958 | tb_tunnel_dp(tb); |
| 959 | } |
| 960 | |
| 961 | static void tb_dp_resource_available(struct tb *tb, struct tb_port *port) |
| 962 | { |
| 963 | struct tb_cm *tcm = tb_priv(tb); |
| 964 | struct tb_port *p; |
| 965 | |
| 966 | if (tb_port_is_enabled(port)) |
| 967 | return; |
| 968 | |
| 969 | list_for_each_entry(p, &tcm->dp_resources, list) { |
| 970 | if (p == port) |
| 971 | return; |
| 972 | } |
| 973 | |
| 974 | tb_port_dbg(port, "DP %s resource available\n", |
| 975 | tb_port_is_dpin(port) ? "IN" : "OUT"); |
| 976 | list_add_tail(&port->list, &tcm->dp_resources); |
| 977 | |
| 978 | /* Look for suitable DP IN <-> DP OUT pairs now */ |
| 979 | tb_tunnel_dp(tb); |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 980 | } |
| 981 | |
Mika Westerberg | 81a2e3e | 2020-05-16 16:20:39 +0300 | [diff] [blame] | 982 | static void tb_disconnect_and_release_dp(struct tb *tb) |
| 983 | { |
| 984 | struct tb_cm *tcm = tb_priv(tb); |
| 985 | struct tb_tunnel *tunnel, *n; |
| 986 | |
| 987 | /* |
| 988 | * Tear down all DP tunnels and release their resources. They |
| 989 | * will be re-established after resume based on plug events. |
| 990 | */ |
| 991 | list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) { |
| 992 | if (tb_tunnel_is_dp(tunnel)) |
| 993 | tb_deactivate_and_free_tunnel(tunnel); |
| 994 | } |
| 995 | |
| 996 | while (!list_empty(&tcm->dp_resources)) { |
| 997 | struct tb_port *port; |
| 998 | |
| 999 | port = list_first_entry(&tcm->dp_resources, |
| 1000 | struct tb_port, list); |
| 1001 | list_del_init(&port->list); |
| 1002 | } |
| 1003 | } |
| 1004 | |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 1005 | static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw) |
| 1006 | { |
| 1007 | struct tb_port *up, *down, *port; |
| 1008 | struct tb_cm *tcm = tb_priv(tb); |
| 1009 | struct tb_switch *parent_sw; |
| 1010 | struct tb_tunnel *tunnel; |
| 1011 | |
Mika Westerberg | 386e5e2 | 2019-12-17 15:33:37 +0300 | [diff] [blame] | 1012 | up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP); |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 1013 | if (!up) |
| 1014 | return 0; |
| 1015 | |
| 1016 | /* |
| 1017 | * Look up available down port. Since we are chaining it should |
| 1018 | * be found right above this switch. |
| 1019 | */ |
| 1020 | parent_sw = tb_to_switch(sw->dev.parent); |
| 1021 | port = tb_port_at(tb_route(sw), parent_sw); |
| 1022 | down = tb_find_pcie_down(parent_sw, port); |
| 1023 | if (!down) |
| 1024 | return 0; |
| 1025 | |
| 1026 | tunnel = tb_tunnel_alloc_pci(tb, up, down); |
| 1027 | if (!tunnel) |
| 1028 | return -ENOMEM; |
| 1029 | |
| 1030 | if (tb_tunnel_activate(tunnel)) { |
| 1031 | tb_port_info(up, |
| 1032 | "PCIe tunnel activation failed, aborting\n"); |
| 1033 | tb_tunnel_free(tunnel); |
| 1034 | return -EIO; |
| 1035 | } |
| 1036 | |
| 1037 | list_add_tail(&tunnel->list, &tcm->tunnel_list); |
| 1038 | return 0; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1039 | } |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 1040 | |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1041 | static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd) |
| 1042 | { |
| 1043 | struct tb_cm *tcm = tb_priv(tb); |
| 1044 | struct tb_port *nhi_port, *dst_port; |
| 1045 | struct tb_tunnel *tunnel; |
| 1046 | struct tb_switch *sw; |
| 1047 | |
| 1048 | sw = tb_to_switch(xd->dev.parent); |
| 1049 | dst_port = tb_port_at(xd->route, sw); |
Mika Westerberg | 386e5e2 | 2019-12-17 15:33:37 +0300 | [diff] [blame] | 1050 | nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI); |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1051 | |
| 1052 | mutex_lock(&tb->lock); |
| 1053 | tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, xd->transmit_ring, |
| 1054 | xd->transmit_path, xd->receive_ring, |
| 1055 | xd->receive_path); |
| 1056 | if (!tunnel) { |
| 1057 | mutex_unlock(&tb->lock); |
| 1058 | return -ENOMEM; |
| 1059 | } |
| 1060 | |
| 1061 | if (tb_tunnel_activate(tunnel)) { |
| 1062 | tb_port_info(nhi_port, |
| 1063 | "DMA tunnel activation failed, aborting\n"); |
| 1064 | tb_tunnel_free(tunnel); |
| 1065 | mutex_unlock(&tb->lock); |
| 1066 | return -EIO; |
| 1067 | } |
| 1068 | |
| 1069 | list_add_tail(&tunnel->list, &tcm->tunnel_list); |
| 1070 | mutex_unlock(&tb->lock); |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
| 1074 | static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd) |
| 1075 | { |
| 1076 | struct tb_port *dst_port; |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 1077 | struct tb_tunnel *tunnel; |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1078 | struct tb_switch *sw; |
| 1079 | |
| 1080 | sw = tb_to_switch(xd->dev.parent); |
| 1081 | dst_port = tb_port_at(xd->route, sw); |
| 1082 | |
| 1083 | /* |
| 1084 | * It is possible that the tunnel was already teared down (in |
| 1085 | * case of cable disconnect) so it is fine if we cannot find it |
| 1086 | * here anymore. |
| 1087 | */ |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 1088 | tunnel = tb_find_tunnel(tb, TB_TUNNEL_DMA, NULL, dst_port); |
| 1089 | tb_deactivate_and_free_tunnel(tunnel); |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | static int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd) |
| 1093 | { |
| 1094 | if (!xd->is_unplugged) { |
| 1095 | mutex_lock(&tb->lock); |
| 1096 | __tb_disconnect_xdomain_paths(tb, xd); |
| 1097 | mutex_unlock(&tb->lock); |
| 1098 | } |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 1102 | /* hotplug handling */ |
| 1103 | |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 1104 | /** |
| 1105 | * tb_handle_hotplug() - handle hotplug event |
| 1106 | * |
| 1107 | * Executes on tb->wq. |
| 1108 | */ |
| 1109 | static void tb_handle_hotplug(struct work_struct *work) |
| 1110 | { |
| 1111 | struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work); |
| 1112 | struct tb *tb = ev->tb; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1113 | struct tb_cm *tcm = tb_priv(tb); |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 1114 | struct tb_switch *sw; |
| 1115 | struct tb_port *port; |
Mika Westerberg | 284652a | 2020-04-09 14:23:32 +0300 | [diff] [blame] | 1116 | |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 1117 | /* Bring the domain back from sleep if it was suspended */ |
| 1118 | pm_runtime_get_sync(&tb->dev); |
| 1119 | |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 1120 | mutex_lock(&tb->lock); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1121 | if (!tcm->hotplug_active) |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 1122 | goto out; /* during init, suspend or shutdown */ |
| 1123 | |
Mika Westerberg | 8f965ef | 2019-03-15 14:56:21 +0200 | [diff] [blame] | 1124 | sw = tb_switch_find_by_route(tb, ev->route); |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 1125 | if (!sw) { |
| 1126 | tb_warn(tb, |
| 1127 | "hotplug event from non existent switch %llx:%x (unplug: %d)\n", |
| 1128 | ev->route, ev->port, ev->unplug); |
| 1129 | goto out; |
| 1130 | } |
| 1131 | if (ev->port > sw->config.max_port_number) { |
| 1132 | tb_warn(tb, |
| 1133 | "hotplug event from non existent port %llx:%x (unplug: %d)\n", |
| 1134 | ev->route, ev->port, ev->unplug); |
Mika Westerberg | 8f965ef | 2019-03-15 14:56:21 +0200 | [diff] [blame] | 1135 | goto put_sw; |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 1136 | } |
| 1137 | port = &sw->ports[ev->port]; |
| 1138 | if (tb_is_upstream_port(port)) { |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 1139 | tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n", |
| 1140 | ev->route, ev->port, ev->unplug); |
Mika Westerberg | 8f965ef | 2019-03-15 14:56:21 +0200 | [diff] [blame] | 1141 | goto put_sw; |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 1142 | } |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 1143 | |
| 1144 | pm_runtime_get_sync(&sw->dev); |
| 1145 | |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 1146 | if (ev->unplug) { |
Kranthi Kuntala | dacb128 | 2020-03-05 16:39:58 +0200 | [diff] [blame] | 1147 | tb_retimer_remove_all(port); |
| 1148 | |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 1149 | if (tb_port_has_remote(port)) { |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1150 | tb_port_dbg(port, "switch unplugged\n"); |
Lukas Wunner | aae20bb | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 1151 | tb_sw_set_unplugged(port->remote->sw); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1152 | tb_free_invalid_tunnels(tb); |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 1153 | tb_remove_dp_resources(port->remote->sw); |
Rajmohan Mani | cf29b9af | 2019-12-17 15:33:43 +0300 | [diff] [blame] | 1154 | tb_switch_tmu_disable(port->remote->sw); |
Mika Westerberg | de46203 | 2020-04-02 14:50:52 +0300 | [diff] [blame] | 1155 | tb_switch_unconfigure_link(port->remote->sw); |
Mika Westerberg | 91c0c12 | 2019-03-21 19:03:00 +0200 | [diff] [blame] | 1156 | tb_switch_lane_bonding_disable(port->remote->sw); |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1157 | tb_switch_remove(port->remote->sw); |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 1158 | port->remote = NULL; |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 1159 | if (port->dual_link_port) |
| 1160 | port->dual_link_port->remote = NULL; |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 1161 | /* Maybe we can create another DP tunnel */ |
| 1162 | tb_tunnel_dp(tb); |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1163 | } else if (port->xdomain) { |
| 1164 | struct tb_xdomain *xd = tb_xdomain_get(port->xdomain); |
| 1165 | |
| 1166 | tb_port_dbg(port, "xdomain unplugged\n"); |
| 1167 | /* |
| 1168 | * Service drivers are unbound during |
| 1169 | * tb_xdomain_remove() so setting XDomain as |
| 1170 | * unplugged here prevents deadlock if they call |
| 1171 | * tb_xdomain_disable_paths(). We will tear down |
| 1172 | * the path below. |
| 1173 | */ |
| 1174 | xd->is_unplugged = true; |
| 1175 | tb_xdomain_remove(xd); |
| 1176 | port->xdomain = NULL; |
| 1177 | __tb_disconnect_xdomain_paths(tb, xd); |
| 1178 | tb_xdomain_put(xd); |
Mika Westerberg | 284652a | 2020-04-09 14:23:32 +0300 | [diff] [blame] | 1179 | tb_port_unconfigure_xdomain(port); |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 1180 | } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) { |
| 1181 | tb_dp_resource_unavailable(tb, port); |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 1182 | } else { |
Mika Westerberg | 62efe69 | 2018-09-17 16:32:13 +0300 | [diff] [blame] | 1183 | tb_port_dbg(port, |
| 1184 | "got unplug event for disconnected port, ignoring\n"); |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 1185 | } |
| 1186 | } else if (port->remote) { |
Mika Westerberg | 62efe69 | 2018-09-17 16:32:13 +0300 | [diff] [blame] | 1187 | tb_port_dbg(port, "got plug event for connected port, ignoring\n"); |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 1188 | } else { |
Mika Westerberg | 344e064 | 2017-10-11 17:19:54 +0300 | [diff] [blame] | 1189 | if (tb_port_is_null(port)) { |
Mika Westerberg | 62efe69 | 2018-09-17 16:32:13 +0300 | [diff] [blame] | 1190 | tb_port_dbg(port, "hotplug: scanning\n"); |
Mika Westerberg | 344e064 | 2017-10-11 17:19:54 +0300 | [diff] [blame] | 1191 | tb_scan_port(port); |
| 1192 | if (!port->remote) |
Mika Westerberg | 62efe69 | 2018-09-17 16:32:13 +0300 | [diff] [blame] | 1193 | tb_port_dbg(port, "hotplug: no switch found\n"); |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 1194 | } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) { |
| 1195 | tb_dp_resource_available(tb, port); |
Mika Westerberg | 344e064 | 2017-10-11 17:19:54 +0300 | [diff] [blame] | 1196 | } |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 1197 | } |
Mika Westerberg | 8f965ef | 2019-03-15 14:56:21 +0200 | [diff] [blame] | 1198 | |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 1199 | pm_runtime_mark_last_busy(&sw->dev); |
| 1200 | pm_runtime_put_autosuspend(&sw->dev); |
| 1201 | |
Mika Westerberg | 8f965ef | 2019-03-15 14:56:21 +0200 | [diff] [blame] | 1202 | put_sw: |
| 1203 | tb_switch_put(sw); |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 1204 | out: |
| 1205 | mutex_unlock(&tb->lock); |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 1206 | |
| 1207 | pm_runtime_mark_last_busy(&tb->dev); |
| 1208 | pm_runtime_put_autosuspend(&tb->dev); |
| 1209 | |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 1210 | kfree(ev); |
| 1211 | } |
| 1212 | |
| 1213 | /** |
| 1214 | * tb_schedule_hotplug_handler() - callback function for the control channel |
| 1215 | * |
| 1216 | * Delegates to tb_handle_hotplug. |
| 1217 | */ |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 1218 | static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type, |
| 1219 | const void *buf, size_t size) |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 1220 | { |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 1221 | const struct cfg_event_pkg *pkg = buf; |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 1222 | u64 route; |
| 1223 | |
| 1224 | if (type != TB_CFG_PKG_EVENT) { |
| 1225 | tb_warn(tb, "unexpected event %#x, ignoring\n", type); |
| 1226 | return; |
| 1227 | } |
| 1228 | |
| 1229 | route = tb_cfg_get_route(&pkg->header); |
| 1230 | |
Mika Westerberg | 210e9f5 | 2019-12-17 15:33:39 +0300 | [diff] [blame] | 1231 | if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) { |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 1232 | tb_warn(tb, "could not ack plug event on %llx:%x\n", route, |
| 1233 | pkg->port); |
| 1234 | } |
| 1235 | |
Mika Westerberg | 4f807e4 | 2018-09-17 16:30:49 +0300 | [diff] [blame] | 1236 | tb_queue_hotplug(tb, route, pkg->port, pkg->unplug); |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 1237 | } |
| 1238 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1239 | static void tb_stop(struct tb *tb) |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 1240 | { |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1241 | struct tb_cm *tcm = tb_priv(tb); |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1242 | struct tb_tunnel *tunnel; |
| 1243 | struct tb_tunnel *n; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1244 | |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 1245 | cancel_delayed_work(&tcm->remove_work); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 1246 | /* tunnels are only present after everything has been initialized */ |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1247 | list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { |
| 1248 | /* |
| 1249 | * DMA tunnels require the driver to be functional so we |
| 1250 | * tear them down. Other protocol tunnels can be left |
| 1251 | * intact. |
| 1252 | */ |
| 1253 | if (tb_tunnel_is_dma(tunnel)) |
| 1254 | tb_tunnel_deactivate(tunnel); |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1255 | tb_tunnel_free(tunnel); |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1256 | } |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1257 | tb_switch_remove(tb->root_switch); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1258 | tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 1259 | } |
| 1260 | |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 1261 | static int tb_scan_finalize_switch(struct device *dev, void *data) |
| 1262 | { |
| 1263 | if (tb_is_switch(dev)) { |
| 1264 | struct tb_switch *sw = tb_to_switch(dev); |
| 1265 | |
| 1266 | /* |
| 1267 | * If we found that the switch was already setup by the |
| 1268 | * boot firmware, mark it as authorized now before we |
| 1269 | * send uevent to userspace. |
| 1270 | */ |
| 1271 | if (sw->boot) |
| 1272 | sw->authorized = 1; |
| 1273 | |
| 1274 | dev_set_uevent_suppress(dev, false); |
| 1275 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
| 1276 | device_for_each_child(dev, NULL, tb_scan_finalize_switch); |
| 1277 | } |
| 1278 | |
| 1279 | return 0; |
| 1280 | } |
| 1281 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1282 | static int tb_start(struct tb *tb) |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 1283 | { |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1284 | struct tb_cm *tcm = tb_priv(tb); |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1285 | int ret; |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 1286 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1287 | tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0); |
Mika Westerberg | 444ac38 | 2018-12-30 12:17:52 +0200 | [diff] [blame] | 1288 | if (IS_ERR(tb->root_switch)) |
| 1289 | return PTR_ERR(tb->root_switch); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1290 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1291 | /* |
| 1292 | * ICM firmware upgrade needs running firmware and in native |
| 1293 | * mode that is not available so disable firmware upgrade of the |
| 1294 | * root switch. |
| 1295 | */ |
| 1296 | tb->root_switch->no_nvm_upgrade = true; |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 1297 | /* All USB4 routers support runtime PM */ |
| 1298 | tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch); |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 1299 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 1300 | ret = tb_switch_configure(tb->root_switch); |
| 1301 | if (ret) { |
| 1302 | tb_switch_put(tb->root_switch); |
| 1303 | return ret; |
| 1304 | } |
| 1305 | |
| 1306 | /* Announce the switch to the world */ |
| 1307 | ret = tb_switch_add(tb->root_switch); |
| 1308 | if (ret) { |
| 1309 | tb_switch_put(tb->root_switch); |
| 1310 | return ret; |
| 1311 | } |
| 1312 | |
Rajmohan Mani | cf29b9af | 2019-12-17 15:33:43 +0300 | [diff] [blame] | 1313 | /* Enable TMU if it is off */ |
| 1314 | tb_switch_tmu_enable(tb->root_switch); |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 1315 | /* Full scan to discover devices added before the driver was loaded. */ |
| 1316 | tb_scan_switch(tb->root_switch); |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 1317 | /* Find out tunnels created by the boot firmware */ |
| 1318 | tb_discover_tunnels(tb->root_switch); |
Rajmohan Mani | e6f8185 | 2019-12-17 15:33:44 +0300 | [diff] [blame] | 1319 | /* |
| 1320 | * If the boot firmware did not create USB 3.x tunnels create them |
| 1321 | * now for the whole topology. |
| 1322 | */ |
| 1323 | tb_create_usb3_tunnels(tb->root_switch); |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 1324 | /* Add DP IN resources for the root switch */ |
| 1325 | tb_add_dp_resources(tb->root_switch); |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 1326 | /* Make the discovered switches available to the userspace */ |
| 1327 | device_for_each_child(&tb->root_switch->dev, NULL, |
| 1328 | tb_scan_finalize_switch); |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 1329 | |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 1330 | /* Allow tb_handle_hotplug to progress events */ |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1331 | tcm->hotplug_active = true; |
| 1332 | return 0; |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 1333 | } |
| 1334 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1335 | static int tb_suspend_noirq(struct tb *tb) |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1336 | { |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1337 | struct tb_cm *tcm = tb_priv(tb); |
| 1338 | |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 1339 | tb_dbg(tb, "suspending...\n"); |
Mika Westerberg | 81a2e3e | 2020-05-16 16:20:39 +0300 | [diff] [blame] | 1340 | tb_disconnect_and_release_dp(tb); |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 1341 | tb_switch_suspend(tb->root_switch, false); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1342 | tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 1343 | tb_dbg(tb, "suspend finished\n"); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1344 | |
| 1345 | return 0; |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1346 | } |
| 1347 | |
Mika Westerberg | 91c0c12 | 2019-03-21 19:03:00 +0200 | [diff] [blame] | 1348 | static void tb_restore_children(struct tb_switch *sw) |
| 1349 | { |
| 1350 | struct tb_port *port; |
| 1351 | |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 1352 | /* No need to restore if the router is already unplugged */ |
| 1353 | if (sw->is_unplugged) |
| 1354 | return; |
| 1355 | |
Rajmohan Mani | cf29b9af | 2019-12-17 15:33:43 +0300 | [diff] [blame] | 1356 | if (tb_enable_tmu(sw)) |
| 1357 | tb_sw_warn(sw, "failed to restore TMU configuration\n"); |
| 1358 | |
Mika Westerberg | 91c0c12 | 2019-03-21 19:03:00 +0200 | [diff] [blame] | 1359 | tb_switch_for_each_port(sw, port) { |
Mika Westerberg | 284652a | 2020-04-09 14:23:32 +0300 | [diff] [blame] | 1360 | if (!tb_port_has_remote(port) && !port->xdomain) |
Mika Westerberg | 91c0c12 | 2019-03-21 19:03:00 +0200 | [diff] [blame] | 1361 | continue; |
| 1362 | |
Mika Westerberg | 284652a | 2020-04-09 14:23:32 +0300 | [diff] [blame] | 1363 | if (port->remote) { |
| 1364 | tb_switch_lane_bonding_enable(port->remote->sw); |
| 1365 | tb_switch_configure_link(port->remote->sw); |
Mika Westerberg | 91c0c12 | 2019-03-21 19:03:00 +0200 | [diff] [blame] | 1366 | |
Mika Westerberg | 284652a | 2020-04-09 14:23:32 +0300 | [diff] [blame] | 1367 | tb_restore_children(port->remote->sw); |
| 1368 | } else if (port->xdomain) { |
| 1369 | tb_port_configure_xdomain(port); |
| 1370 | } |
Mika Westerberg | 91c0c12 | 2019-03-21 19:03:00 +0200 | [diff] [blame] | 1371 | } |
| 1372 | } |
| 1373 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1374 | static int tb_resume_noirq(struct tb *tb) |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1375 | { |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1376 | struct tb_cm *tcm = tb_priv(tb); |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1377 | struct tb_tunnel *tunnel, *n; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1378 | |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 1379 | tb_dbg(tb, "resuming...\n"); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1380 | |
| 1381 | /* remove any pci devices the firmware might have setup */ |
Mika Westerberg | 356b6c4 | 2019-09-19 15:25:30 +0300 | [diff] [blame] | 1382 | tb_switch_reset(tb->root_switch); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1383 | |
| 1384 | tb_switch_resume(tb->root_switch); |
| 1385 | tb_free_invalid_tunnels(tb); |
| 1386 | tb_free_unplugged_children(tb->root_switch); |
Mika Westerberg | 91c0c12 | 2019-03-21 19:03:00 +0200 | [diff] [blame] | 1387 | tb_restore_children(tb->root_switch); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1388 | list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 1389 | tb_tunnel_restart(tunnel); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1390 | if (!list_empty(&tcm->tunnel_list)) { |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1391 | /* |
| 1392 | * the pcie links need some time to get going. |
| 1393 | * 100ms works for me... |
| 1394 | */ |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 1395 | tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n"); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1396 | msleep(100); |
| 1397 | } |
| 1398 | /* Allow tb_handle_hotplug to progress events */ |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1399 | tcm->hotplug_active = true; |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 1400 | tb_dbg(tb, "resume finished\n"); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1401 | |
| 1402 | return 0; |
| 1403 | } |
| 1404 | |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1405 | static int tb_free_unplugged_xdomains(struct tb_switch *sw) |
| 1406 | { |
Mika Westerberg | b433d01 | 2019-09-30 14:07:22 +0300 | [diff] [blame] | 1407 | struct tb_port *port; |
| 1408 | int ret = 0; |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1409 | |
Mika Westerberg | b433d01 | 2019-09-30 14:07:22 +0300 | [diff] [blame] | 1410 | tb_switch_for_each_port(sw, port) { |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1411 | if (tb_is_upstream_port(port)) |
| 1412 | continue; |
| 1413 | if (port->xdomain && port->xdomain->is_unplugged) { |
Kranthi Kuntala | dacb128 | 2020-03-05 16:39:58 +0200 | [diff] [blame] | 1414 | tb_retimer_remove_all(port); |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1415 | tb_xdomain_remove(port->xdomain); |
Mika Westerberg | 284652a | 2020-04-09 14:23:32 +0300 | [diff] [blame] | 1416 | tb_port_unconfigure_xdomain(port); |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1417 | port->xdomain = NULL; |
| 1418 | ret++; |
| 1419 | } else if (port->remote) { |
| 1420 | ret += tb_free_unplugged_xdomains(port->remote->sw); |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | return ret; |
| 1425 | } |
| 1426 | |
Mika Westerberg | 884e4d5 | 2020-08-31 13:05:14 +0300 | [diff] [blame] | 1427 | static int tb_freeze_noirq(struct tb *tb) |
| 1428 | { |
| 1429 | struct tb_cm *tcm = tb_priv(tb); |
| 1430 | |
| 1431 | tcm->hotplug_active = false; |
| 1432 | return 0; |
| 1433 | } |
| 1434 | |
| 1435 | static int tb_thaw_noirq(struct tb *tb) |
| 1436 | { |
| 1437 | struct tb_cm *tcm = tb_priv(tb); |
| 1438 | |
| 1439 | tcm->hotplug_active = true; |
| 1440 | return 0; |
| 1441 | } |
| 1442 | |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1443 | static void tb_complete(struct tb *tb) |
| 1444 | { |
| 1445 | /* |
| 1446 | * Release any unplugged XDomains and if there is a case where |
| 1447 | * another domain is swapped in place of unplugged XDomain we |
| 1448 | * need to run another rescan. |
| 1449 | */ |
| 1450 | mutex_lock(&tb->lock); |
| 1451 | if (tb_free_unplugged_xdomains(tb->root_switch)) |
| 1452 | tb_scan_switch(tb->root_switch); |
| 1453 | mutex_unlock(&tb->lock); |
| 1454 | } |
| 1455 | |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 1456 | static int tb_runtime_suspend(struct tb *tb) |
| 1457 | { |
| 1458 | struct tb_cm *tcm = tb_priv(tb); |
| 1459 | |
| 1460 | mutex_lock(&tb->lock); |
| 1461 | tb_switch_suspend(tb->root_switch, true); |
| 1462 | tcm->hotplug_active = false; |
| 1463 | mutex_unlock(&tb->lock); |
| 1464 | |
| 1465 | return 0; |
| 1466 | } |
| 1467 | |
| 1468 | static void tb_remove_work(struct work_struct *work) |
| 1469 | { |
| 1470 | struct tb_cm *tcm = container_of(work, struct tb_cm, remove_work.work); |
| 1471 | struct tb *tb = tcm_to_tb(tcm); |
| 1472 | |
| 1473 | mutex_lock(&tb->lock); |
| 1474 | if (tb->root_switch) { |
| 1475 | tb_free_unplugged_children(tb->root_switch); |
| 1476 | tb_free_unplugged_xdomains(tb->root_switch); |
| 1477 | } |
| 1478 | mutex_unlock(&tb->lock); |
| 1479 | } |
| 1480 | |
| 1481 | static int tb_runtime_resume(struct tb *tb) |
| 1482 | { |
| 1483 | struct tb_cm *tcm = tb_priv(tb); |
| 1484 | struct tb_tunnel *tunnel, *n; |
| 1485 | |
| 1486 | mutex_lock(&tb->lock); |
| 1487 | tb_switch_resume(tb->root_switch); |
| 1488 | tb_free_invalid_tunnels(tb); |
| 1489 | tb_restore_children(tb->root_switch); |
| 1490 | list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) |
| 1491 | tb_tunnel_restart(tunnel); |
| 1492 | tcm->hotplug_active = true; |
| 1493 | mutex_unlock(&tb->lock); |
| 1494 | |
| 1495 | /* |
| 1496 | * Schedule cleanup of any unplugged devices. Run this in a |
| 1497 | * separate thread to avoid possible deadlock if the device |
| 1498 | * removal runtime resumes the unplugged device. |
| 1499 | */ |
| 1500 | queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50)); |
| 1501 | return 0; |
| 1502 | } |
| 1503 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1504 | static const struct tb_cm_ops tb_cm_ops = { |
| 1505 | .start = tb_start, |
| 1506 | .stop = tb_stop, |
| 1507 | .suspend_noirq = tb_suspend_noirq, |
| 1508 | .resume_noirq = tb_resume_noirq, |
Mika Westerberg | 884e4d5 | 2020-08-31 13:05:14 +0300 | [diff] [blame] | 1509 | .freeze_noirq = tb_freeze_noirq, |
| 1510 | .thaw_noirq = tb_thaw_noirq, |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1511 | .complete = tb_complete, |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 1512 | .runtime_suspend = tb_runtime_suspend, |
| 1513 | .runtime_resume = tb_runtime_resume, |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 1514 | .handle_event = tb_handle_event, |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 1515 | .approve_switch = tb_tunnel_pci, |
Mika Westerberg | 7ea4cd6 | 2018-09-28 16:41:01 +0300 | [diff] [blame] | 1516 | .approve_xdomain_paths = tb_approve_xdomain_paths, |
| 1517 | .disconnect_xdomain_paths = tb_disconnect_xdomain_paths, |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1518 | }; |
| 1519 | |
| 1520 | struct tb *tb_probe(struct tb_nhi *nhi) |
| 1521 | { |
| 1522 | struct tb_cm *tcm; |
| 1523 | struct tb *tb; |
| 1524 | |
| 1525 | tb = tb_domain_alloc(nhi, sizeof(*tcm)); |
| 1526 | if (!tb) |
| 1527 | return NULL; |
| 1528 | |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 1529 | tb->security_level = TB_SECURITY_USER; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1530 | tb->cm_ops = &tb_cm_ops; |
| 1531 | |
| 1532 | tcm = tb_priv(tb); |
| 1533 | INIT_LIST_HEAD(&tcm->tunnel_list); |
Mika Westerberg | 8afe909 | 2019-03-26 15:52:30 +0300 | [diff] [blame] | 1534 | INIT_LIST_HEAD(&tcm->dp_resources); |
Mika Westerberg | 6ac6fae | 2020-06-05 14:25:02 +0300 | [diff] [blame] | 1535 | INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 1536 | |
| 1537 | return tb; |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 1538 | } |