blob: 214fbc92c1b7d8333bddd3e2a1bc5d0e15b3bad6 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02002/*
Mika Westerberg99cabbb2018-12-30 21:34:08 +02003 * Thunderbolt driver - bus logic (NHI independent)
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02004 *
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
Mika Westerberg99cabbb2018-12-30 21:34:08 +02006 * Copyright (C) 2019, Intel Corporation
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02007 */
8
9#include <linux/slab.h>
10#include <linux/errno.h>
11#include <linux/delay.h>
Mika Westerberg6ac6fae2020-06-05 14:25:02 +030012#include <linux/pm_runtime.h>
Andreas Noeverd6cc51c2014-06-03 22:04:00 +020013
14#include "tb.h"
Andreas Noever7adf6092014-06-03 22:04:01 +020015#include "tb_regs.h"
Mika Westerberg1752b9f2017-02-19 10:58:35 +020016#include "tunnel.h"
Andreas Noeverd6cc51c2014-06-03 22:04:00 +020017
Mika Westerberg9d3cce02017-06-06 15:25:00 +030018/**
19 * struct tb_cm - Simple Thunderbolt connection manager
20 * @tunnel_list: List of active tunnels
Mika Westerberg8afe9092019-03-26 15:52:30 +030021 * @dp_resources: List of available DP resources for DP tunneling
Mika Westerberg9d3cce02017-06-06 15:25:00 +030022 * @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 Westerberg6ac6fae2020-06-05 14:25:02 +030026 * @remove_work: Work used to remove any unplugged routers after
27 * runtime resume
Mika Westerberg9d3cce02017-06-06 15:25:00 +030028 */
29struct tb_cm {
30 struct list_head tunnel_list;
Mika Westerberg8afe9092019-03-26 15:52:30 +030031 struct list_head dp_resources;
Mika Westerberg9d3cce02017-06-06 15:25:00 +030032 bool hotplug_active;
Mika Westerberg6ac6fae2020-06-05 14:25:02 +030033 struct delayed_work remove_work;
Mika Westerberg9d3cce02017-06-06 15:25:00 +030034};
Andreas Noever9da672a2014-06-03 22:04:05 +020035
Mika Westerberg6ac6fae2020-06-05 14:25:02 +030036static inline struct tb *tcm_to_tb(struct tb_cm *tcm)
37{
38 return ((void *)tcm - sizeof(struct tb));
39}
40
Mika Westerberg4f807e42018-09-17 16:30:49 +030041struct tb_hotplug_event {
42 struct work_struct work;
43 struct tb *tb;
44 u64 route;
45 u8 port;
46 bool unplug;
47};
48
49static void tb_handle_hotplug(struct work_struct *work);
50
51static 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 Noever9da672a2014-06-03 22:04:05 +020067/* enumeration & hot plug handling */
68
Mika Westerberg8afe9092019-03-26 15:52:30 +030069static 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
86static 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 Westerberg0414bec2017-02-19 23:43:26 +0200105static 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 Westerberg0414bec2017-02-19 23:43:26 +0200110
Mika Westerbergb433d012019-09-30 14:07:22 +0300111 tb_switch_for_each_port(sw, port) {
Mika Westerberg0414bec2017-02-19 23:43:26 +0200112 struct tb_tunnel *tunnel = NULL;
113
Mika Westerberg0414bec2017-02-19 23:43:26 +0200114 switch (port->config.type) {
Mika Westerberg4f807e42018-09-17 16:30:49 +0300115 case TB_TYPE_DP_HDMI_IN:
116 tunnel = tb_tunnel_discover_dp(tb, port);
117 break;
118
Mika Westerberg0414bec2017-02-19 23:43:26 +0200119 case TB_TYPE_PCIE_DOWN:
120 tunnel = tb_tunnel_discover_pci(tb, port);
121 break;
122
Rajmohan Manie6f81852019-12-17 15:33:44 +0300123 case TB_TYPE_USB3_DOWN:
124 tunnel = tb_tunnel_discover_usb3(tb, port);
125 break;
126
Mika Westerberg0414bec2017-02-19 23:43:26 +0200127 default:
128 break;
129 }
130
Mika Westerberg4f807e42018-09-17 16:30:49 +0300131 if (!tunnel)
132 continue;
133
134 if (tb_tunnel_is_pci(tunnel)) {
Mika Westerberg0414bec2017-02-19 23:43:26 +0200135 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 Westerberg0414bec2017-02-19 23:43:26 +0200141 }
Mika Westerberg4f807e42018-09-17 16:30:49 +0300142
143 list_add_tail(&tunnel->list, &tcm->tunnel_list);
Mika Westerberg0414bec2017-02-19 23:43:26 +0200144 }
145
Mika Westerbergb433d012019-09-30 14:07:22 +0300146 tb_switch_for_each_port(sw, port) {
147 if (tb_port_has_remote(port))
148 tb_discover_tunnels(port->remote->sw);
Mika Westerberg0414bec2017-02-19 23:43:26 +0200149 }
150}
Andreas Noever9da672a2014-06-03 22:04:05 +0200151
Mika Westerberg284652a2020-04-09 14:23:32 +0300152static int tb_port_configure_xdomain(struct tb_port *port)
153{
Mika Westerberg341d4512020-02-21 12:11:54 +0200154 /*
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 Westerberg284652a2020-04-09 14:23:32 +0300160 if (tb_switch_is_usb4(port->sw))
161 return usb4_port_configure_xdomain(port);
162 return tb_lc_configure_xdomain(port);
163}
164
165static 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 Westerberg341d4512020-02-21 12:11:54 +0200171
172 tb_port_enable(port->dual_link_port);
Mika Westerberg284652a2020-04-09 14:23:32 +0300173}
174
Mika Westerberg7ea4cd62018-09-28 16:41:01 +0300175static 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 Westerberg284652a2020-04-09 14:23:32 +0300193 tb_port_configure_xdomain(port);
Mika Westerberg7ea4cd62018-09-28 16:41:01 +0300194 tb_xdomain_add(xd);
195 }
196}
197
Rajmohan Manicf29b9af2019-12-17 15:33:43 +0300198static 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 Manie6f81852019-12-17 15:33:44 +0300217/**
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 */
222static 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
241static struct tb_port *tb_find_usb3_down(struct tb_switch *sw,
Mika Westerberg77cfa402020-03-11 16:00:46 +0300242 const struct tb_port *port)
Rajmohan Manie6f81852019-12-17 15:33:44 +0300243{
244 struct tb_port *down;
245
246 down = usb4_switch_map_usb3_down(sw, port);
Mika Westerberg77cfa402020-03-11 16:00:46 +0300247 if (down && !tb_usb3_port_is_enabled(down))
Rajmohan Manie6f81852019-12-17 15:33:44 +0300248 return down;
Mika Westerberg77cfa402020-03-11 16:00:46 +0300249 return NULL;
Rajmohan Manie6f81852019-12-17 15:33:44 +0300250}
251
Mika Westerberg0bd680c2020-03-24 14:44:13 +0200252static 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
270static 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
297static 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
390static 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
400static 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 Manie6f81852019-12-17 15:33:44 +0300429static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw)
430{
431 struct tb_switch *parent = tb_switch_parent(sw);
Mika Westerberg0bd680c2020-03-24 14:44:13 +0200432 int ret, available_up, available_down;
Rajmohan Manie6f81852019-12-17 15:33:44 +0300433 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 Westerbergbbcf40b2020-03-04 17:09:14 +0200441 if (!sw->link_usb4)
442 return 0;
443
Rajmohan Manie6f81852019-12-17 15:33:44 +0300444 /*
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 Westerberg0bd680c2020-03-24 14:44:13 +0200463
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 Manie6f81852019-12-17 15:33:44 +0300468 }
469
Mika Westerberg0bd680c2020-03-24 14:44:13 +0200470 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 Manie6f81852019-12-17 15:33:44 +0300484
485 if (tb_tunnel_activate(tunnel)) {
486 tb_port_info(up,
487 "USB3 tunnel activation failed, aborting\n");
Mika Westerberg0bd680c2020-03-24 14:44:13 +0200488 ret = -EIO;
489 goto err_free;
Rajmohan Manie6f81852019-12-17 15:33:44 +0300490 }
491
492 list_add_tail(&tunnel->list, &tcm->tunnel_list);
Mika Westerberg0bd680c2020-03-24 14:44:13 +0200493 if (tb_route(parent))
494 tb_reclaim_usb3_bandwidth(tb, down, up);
495
Rajmohan Manie6f81852019-12-17 15:33:44 +0300496 return 0;
Mika Westerberg0bd680c2020-03-24 14:44:13 +0200497
498err_free:
499 tb_tunnel_free(tunnel);
500err_reclaim:
501 if (tb_route(parent))
502 tb_reclaim_usb3_bandwidth(tb, down, up);
503
504 return ret;
Rajmohan Manie6f81852019-12-17 15:33:44 +0300505}
506
507static 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 Noever9da672a2014-06-03 22:04:05 +0200529static void tb_scan_port(struct tb_port *port);
530
531/**
532 * tb_scan_switch() - scan for and initialize downstream switches
533 */
534static void tb_scan_switch(struct tb_switch *sw)
535{
Mika Westerbergb433d012019-09-30 14:07:22 +0300536 struct tb_port *port;
537
Mika Westerberg6ac6fae2020-06-05 14:25:02 +0300538 pm_runtime_get_sync(&sw->dev);
539
Mika Westerbergb433d012019-09-30 14:07:22 +0300540 tb_switch_for_each_port(sw, port)
541 tb_scan_port(port);
Mika Westerberg6ac6fae2020-06-05 14:25:02 +0300542
543 pm_runtime_mark_last_busy(&sw->dev);
544 pm_runtime_put_autosuspend(&sw->dev);
Andreas Noever9da672a2014-06-03 22:04:05 +0200545}
546
547/**
548 * tb_scan_port() - check for and initialize switches below port
549 */
550static void tb_scan_port(struct tb_port *port)
551{
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200552 struct tb_cm *tcm = tb_priv(port->sw->tb);
Mika Westerbergdfe40ca2019-03-07 15:26:45 +0200553 struct tb_port *upstream_port;
Andreas Noever9da672a2014-06-03 22:04:05 +0200554 struct tb_switch *sw;
Mika Westerbergdfe40ca2019-03-07 15:26:45 +0200555
Andreas Noever9da672a2014-06-03 22:04:05 +0200556 if (tb_is_upstream_port(port))
557 return;
Mika Westerberg4f807e42018-09-17 16:30:49 +0300558
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 Noever9da672a2014-06-03 22:04:05 +0200567 if (port->config.type != TB_TYPE_PORT)
568 return;
Andreas Noever343fcb82014-06-12 23:11:47 +0200569 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 Noever9da672a2014-06-03 22:04:05 +0200574 if (tb_wait_for_port(port, false) <= 0)
575 return;
576 if (port->remote) {
Mika Westerberg7ea4cd62018-09-28 16:41:01 +0300577 tb_port_dbg(port, "port already has a remote\n");
Andreas Noever9da672a2014-06-03 22:04:05 +0200578 return;
579 }
Kranthi Kuntaladacb1282020-03-05 16:39:58 +0200580
581 tb_retimer_scan(port);
582
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300583 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
584 tb_downstream_route(port));
Mika Westerberg7ea4cd62018-09-28 16:41:01 +0300585 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 Noever9da672a2014-06-03 22:04:05 +0200593 return;
Mika Westerberg7ea4cd62018-09-28 16:41:01 +0300594 }
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300595
596 if (tb_switch_configure(sw)) {
597 tb_switch_put(sw);
598 return;
599 }
600
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200601 /*
Mika Westerberg7ea4cd62018-09-28 16:41:01 +0300602 * If there was previously another domain connected remove it
603 * first.
604 */
605 if (port->xdomain) {
606 tb_xdomain_remove(port->xdomain);
Mika Westerberg284652a2020-04-09 14:23:32 +0300607 tb_port_unconfigure_xdomain(port);
Mika Westerberg7ea4cd62018-09-28 16:41:01 +0300608 port->xdomain = NULL;
609 }
610
611 /*
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200612 * 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 Westerbergf67cf492017-06-06 15:25:16 +0300618
Mika Westerberg6ac6fae2020-06-05 14:25:02 +0300619 /*
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 Westerbergbfe778a2017-06-06 15:25:01 +0300625 if (tb_switch_add(sw)) {
626 tb_switch_put(sw);
627 return;
628 }
629
Mika Westerbergdfe40ca2019-03-07 15:26:45 +0200630 /* 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 Westerberg91c0c122019-03-21 19:03:00 +0200639 /* Enable lane bonding if supported */
Mika Westerberg2ca32632020-04-02 14:28:18 +0300640 tb_switch_lane_bonding_enable(sw);
Mika Westerbergde462032020-04-02 14:50:52 +0300641 /* Set the link configured */
642 tb_switch_configure_link(sw);
Mika Westerberg91c0c122019-03-21 19:03:00 +0200643
Rajmohan Manicf29b9af2019-12-17 15:33:43 +0300644 if (tb_enable_tmu(sw))
645 tb_sw_warn(sw, "failed to enable TMU\n");
646
Kranthi Kuntaladacb1282020-03-05 16:39:58 +0200647 /* Scan upstream retimers */
648 tb_retimer_scan(upstream_port);
649
Rajmohan Manie6f81852019-12-17 15:33:44 +0300650 /*
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 Westerberge876f342020-04-02 12:53:14 +0300659 tb_add_dp_resources(sw);
Andreas Noever9da672a2014-06-03 22:04:05 +0200660 tb_scan_switch(sw);
661}
662
Mika Westerberg8afe9092019-03-26 15:52:30 +0300663static void tb_deactivate_and_free_tunnel(struct tb_tunnel *tunnel)
664{
Mika Westerberg0bd680c2020-03-24 14:44:13 +0200665 struct tb_port *src_port, *dst_port;
666 struct tb *tb;
667
Mika Westerberg8afe9092019-03-26 15:52:30 +0300668 if (!tunnel)
669 return;
670
671 tb_tunnel_deactivate(tunnel);
672 list_del(&tunnel->list);
673
Mika Westerberg0bd680c2020-03-24 14:44:13 +0200674 tb = tunnel->tb;
675 src_port = tunnel->src_port;
676 dst_port = tunnel->dst_port;
Mika Westerberg8afe9092019-03-26 15:52:30 +0300677
Mika Westerberg0bd680c2020-03-24 14:44:13 +0200678 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 Westerberg6ac6fae2020-06-05 14:25:02 +0300685 /* 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 Westerberg0bd680c2020-03-24 14:44:13 +0200690 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 Westerberg8afe9092019-03-26 15:52:30 +0300702 }
703
704 tb_tunnel_free(tunnel);
Mika Westerberg4f807e42018-09-17 16:30:49 +0300705}
706
Andreas Noever3364f0c2014-06-03 22:04:08 +0200707/**
708 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
709 */
710static void tb_free_invalid_tunnels(struct tb *tb)
711{
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300712 struct tb_cm *tcm = tb_priv(tb);
Mika Westerberg93f36ad2017-02-19 13:48:29 +0200713 struct tb_tunnel *tunnel;
714 struct tb_tunnel *n;
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300715
716 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
Mika Westerberg8afe9092019-03-26 15:52:30 +0300717 if (tb_tunnel_is_invalid(tunnel))
718 tb_deactivate_and_free_tunnel(tunnel);
Andreas Noever3364f0c2014-06-03 22:04:08 +0200719 }
720}
721
722/**
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200723 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
724 */
725static void tb_free_unplugged_children(struct tb_switch *sw)
726{
Mika Westerbergb433d012019-09-30 14:07:22 +0300727 struct tb_port *port;
Mika Westerbergdfe40ca2019-03-07 15:26:45 +0200728
Mika Westerbergb433d012019-09-30 14:07:22 +0300729 tb_switch_for_each_port(sw, port) {
Mika Westerbergdfe40ca2019-03-07 15:26:45 +0200730 if (!tb_port_has_remote(port))
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200731 continue;
Mika Westerbergdfe40ca2019-03-07 15:26:45 +0200732
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200733 if (port->remote->sw->is_unplugged) {
Kranthi Kuntaladacb1282020-03-05 16:39:58 +0200734 tb_retimer_remove_all(port);
Mika Westerberg8afe9092019-03-26 15:52:30 +0300735 tb_remove_dp_resources(port->remote->sw);
Mika Westerbergde462032020-04-02 14:50:52 +0300736 tb_switch_unconfigure_link(port->remote->sw);
Mika Westerberg91c0c122019-03-21 19:03:00 +0200737 tb_switch_lane_bonding_disable(port->remote->sw);
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300738 tb_switch_remove(port->remote->sw);
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200739 port->remote = NULL;
Mika Westerbergdfe40ca2019-03-07 15:26:45 +0200740 if (port->dual_link_port)
741 port->dual_link_port->remote = NULL;
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200742 } else {
743 tb_free_unplugged_children(port->remote->sw);
744 }
745 }
746}
747
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200748static struct tb_port *tb_find_pcie_down(struct tb_switch *sw,
749 const struct tb_port *port)
Andreas Noever3364f0c2014-06-03 22:04:08 +0200750{
Mika Westerbergb0407982019-12-17 15:33:40 +0300751 struct tb_port *down = NULL;
752
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200753 /*
754 * To keep plugging devices consistently in the same PCIe
Mika Westerbergb0407982019-12-17 15:33:40 +0300755 * hierarchy, do mapping here for switch downstream PCIe ports.
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200756 */
Mika Westerbergb0407982019-12-17 15:33:40 +0300757 if (tb_switch_is_usb4(sw)) {
758 down = usb4_switch_map_pcie_down(sw, port);
759 } else if (!tb_route(sw)) {
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200760 int phy_port = tb_phy_port_from_link(port->port);
761 int index;
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300762
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200763 /*
764 * Hard-coded Thunderbolt port to PCIe down port mapping
765 * per controller.
766 */
Mika Westerberg7bffd97e2019-03-22 15:16:53 +0200767 if (tb_switch_is_cactus_ridge(sw) ||
768 tb_switch_is_alpine_ridge(sw))
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200769 index = !phy_port ? 6 : 7;
Mika Westerberg17a8f812019-10-08 16:42:47 +0300770 else if (tb_switch_is_falcon_ridge(sw))
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200771 index = !phy_port ? 6 : 8;
Mika Westerberg7bffd97e2019-03-22 15:16:53 +0200772 else if (tb_switch_is_titan_ridge(sw))
773 index = !phy_port ? 8 : 9;
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200774 else
775 goto out;
Andreas Noever3364f0c2014-06-03 22:04:08 +0200776
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200777 /* Validate the hard-coding */
778 if (WARN_ON(index > sw->config.max_port_number))
779 goto out;
Mika Westerbergb0407982019-12-17 15:33:40 +0300780
781 down = &sw->ports[index];
782 }
783
784 if (down) {
785 if (WARN_ON(!tb_port_is_pcie_down(down)))
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200786 goto out;
Mika Westerberg9cac51a2020-03-11 16:12:50 +0300787 if (tb_pci_port_is_enabled(down))
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200788 goto out;
Andreas Noever3364f0c2014-06-03 22:04:08 +0200789
Mika Westerbergb0407982019-12-17 15:33:40 +0300790 return down;
Andreas Noever3364f0c2014-06-03 22:04:08 +0200791 }
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200792
793out:
Mika Westerberge78db6f2017-10-12 16:45:50 +0300794 return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN);
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200795}
796
Mika Westerberge876f342020-04-02 12:53:14 +0300797static 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 Westerberg8afe9092019-03-26 15:52:30 +0300834static void tb_tunnel_dp(struct tb *tb)
Mika Westerberg4f807e42018-09-17 16:30:49 +0300835{
Mika Westerberg0bd680c2020-03-24 14:44:13 +0200836 int available_up, available_down, ret;
Mika Westerberg4f807e42018-09-17 16:30:49 +0300837 struct tb_cm *tcm = tb_priv(tb);
Mika Westerberg8afe9092019-03-26 15:52:30 +0300838 struct tb_port *port, *in, *out;
Mika Westerberg4f807e42018-09-17 16:30:49 +0300839 struct tb_tunnel *tunnel;
Mika Westerberg4f807e42018-09-17 16:30:49 +0300840
Mika Westerberg8afe9092019-03-26 15:52:30 +0300841 /*
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 Westerberg4f807e42018-09-17 16:30:49 +0300846
Mika Westerberg8afe9092019-03-26 15:52:30 +0300847 in = NULL;
848 out = NULL;
849 list_for_each_entry(port, &tcm->dp_resources, list) {
Mika Westerberge876f342020-04-02 12:53:14 +0300850 if (!tb_port_is_dpin(port))
851 continue;
852
Mika Westerberg8afe9092019-03-26 15:52:30 +0300853 if (tb_port_is_enabled(port)) {
854 tb_port_dbg(port, "in use\n");
855 continue;
856 }
857
Mika Westerberge876f342020-04-02 12:53:14 +0300858 tb_port_dbg(port, "DP IN available\n");
Mika Westerberg8afe9092019-03-26 15:52:30 +0300859
Mika Westerberge876f342020-04-02 12:53:14 +0300860 out = tb_find_dp_out(tb, port);
861 if (out) {
Mika Westerberg8afe9092019-03-26 15:52:30 +0300862 in = port;
Mika Westerberge876f342020-04-02 12:53:14 +0300863 break;
864 }
Mika Westerberg8afe9092019-03-26 15:52:30 +0300865 }
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 Westerberg6ac6fae2020-06-05 14:25:02 +0300876 /*
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 Westerberg8afe9092019-03-26 15:52:30 +0300887 if (tb_switch_alloc_dp_resource(in->sw, in)) {
888 tb_port_dbg(in, "no resource available for DP IN, not tunneling\n");
Mika Westerberg6ac6fae2020-06-05 14:25:02 +0300889 goto err_rpm_put;
Mika Westerberg8afe9092019-03-26 15:52:30 +0300890 }
Mika Westerberg4f807e42018-09-17 16:30:49 +0300891
Mika Westerberg0bd680c2020-03-24 14:44:13 +0200892 /* 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 Westerberga11b88a2019-03-26 16:03:48 +0300897 }
898
Mika Westerberg0bd680c2020-03-24 14:44:13 +0200899 ret = tb_available_bandwidth(tb, in, out, &available_up,
900 &available_down);
901 if (ret)
902 goto err_reclaim;
Mika Westerberga11b88a2019-03-26 16:03:48 +0300903
Mika Westerberg0bd680c2020-03-24 14:44:13 +0200904 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 Westerberg4f807e42018-09-17 16:30:49 +0300908 if (!tunnel) {
Mika Westerberg8afe9092019-03-26 15:52:30 +0300909 tb_port_dbg(out, "could not allocate DP tunnel\n");
Mika Westerberg0bd680c2020-03-24 14:44:13 +0200910 goto err_reclaim;
Mika Westerberg4f807e42018-09-17 16:30:49 +0300911 }
912
913 if (tb_tunnel_activate(tunnel)) {
914 tb_port_info(out, "DP tunnel activation failed, aborting\n");
Mika Westerberg0bd680c2020-03-24 14:44:13 +0200915 goto err_free;
Mika Westerberg4f807e42018-09-17 16:30:49 +0300916 }
917
918 list_add_tail(&tunnel->list, &tcm->tunnel_list);
Mika Westerberg0bd680c2020-03-24 14:44:13 +0200919 tb_reclaim_usb3_bandwidth(tb, in, out);
Mika Westerberg8afe9092019-03-26 15:52:30 +0300920 return;
921
Mika Westerberg0bd680c2020-03-24 14:44:13 +0200922err_free:
923 tb_tunnel_free(tunnel);
924err_reclaim:
925 tb_reclaim_usb3_bandwidth(tb, in, out);
926err_dealloc_dp:
Mika Westerberg8afe9092019-03-26 15:52:30 +0300927 tb_switch_dealloc_dp_resource(in->sw, in);
Mika Westerberg6ac6fae2020-06-05 14:25:02 +0300928err_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 Westerberg4f807e42018-09-17 16:30:49 +0300933}
934
Mika Westerberg8afe9092019-03-26 15:52:30 +0300935static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port)
Mika Westerberg4f807e42018-09-17 16:30:49 +0300936{
Mika Westerberg8afe9092019-03-26 15:52:30 +0300937 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
961static 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 Westerberg4f807e42018-09-17 16:30:49 +0300980}
981
Mika Westerberg81a2e3e2020-05-16 16:20:39 +0300982static 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 Westerberg99cabbb2018-12-30 21:34:08 +02001005static 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 Westerberg386e5e22019-12-17 15:33:37 +03001012 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
Mika Westerberg99cabbb2018-12-30 21:34:08 +02001013 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 Noever3364f0c2014-06-03 22:04:08 +02001039}
Andreas Noever9da672a2014-06-03 22:04:05 +02001040
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001041static 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 Westerberg386e5e22019-12-17 15:33:37 +03001050 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001051
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
1074static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1075{
1076 struct tb_port *dst_port;
Mika Westerberg8afe9092019-03-26 15:52:30 +03001077 struct tb_tunnel *tunnel;
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001078 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 Westerberg8afe9092019-03-26 15:52:30 +03001088 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DMA, NULL, dst_port);
1089 tb_deactivate_and_free_tunnel(tunnel);
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001090}
1091
1092static 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 Noeverd6cc51c2014-06-03 22:04:00 +02001102/* hotplug handling */
1103
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02001104/**
1105 * tb_handle_hotplug() - handle hotplug event
1106 *
1107 * Executes on tb->wq.
1108 */
1109static 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 Westerberg9d3cce02017-06-06 15:25:00 +03001113 struct tb_cm *tcm = tb_priv(tb);
Andreas Noever053596d2014-06-03 22:04:06 +02001114 struct tb_switch *sw;
1115 struct tb_port *port;
Mika Westerberg284652a2020-04-09 14:23:32 +03001116
Mika Westerberg6ac6fae2020-06-05 14:25:02 +03001117 /* Bring the domain back from sleep if it was suspended */
1118 pm_runtime_get_sync(&tb->dev);
1119
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02001120 mutex_lock(&tb->lock);
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001121 if (!tcm->hotplug_active)
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02001122 goto out; /* during init, suspend or shutdown */
1123
Mika Westerberg8f965ef2019-03-15 14:56:21 +02001124 sw = tb_switch_find_by_route(tb, ev->route);
Andreas Noever053596d2014-06-03 22:04:06 +02001125 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 Westerberg8f965ef2019-03-15 14:56:21 +02001135 goto put_sw;
Andreas Noever053596d2014-06-03 22:04:06 +02001136 }
1137 port = &sw->ports[ev->port];
1138 if (tb_is_upstream_port(port)) {
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02001139 tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n",
1140 ev->route, ev->port, ev->unplug);
Mika Westerberg8f965ef2019-03-15 14:56:21 +02001141 goto put_sw;
Andreas Noever053596d2014-06-03 22:04:06 +02001142 }
Mika Westerberg6ac6fae2020-06-05 14:25:02 +03001143
1144 pm_runtime_get_sync(&sw->dev);
1145
Andreas Noever053596d2014-06-03 22:04:06 +02001146 if (ev->unplug) {
Kranthi Kuntaladacb1282020-03-05 16:39:58 +02001147 tb_retimer_remove_all(port);
1148
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02001149 if (tb_port_has_remote(port)) {
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001150 tb_port_dbg(port, "switch unplugged\n");
Lukas Wunneraae20bb2016-03-20 13:57:20 +01001151 tb_sw_set_unplugged(port->remote->sw);
Andreas Noever3364f0c2014-06-03 22:04:08 +02001152 tb_free_invalid_tunnels(tb);
Mika Westerberg8afe9092019-03-26 15:52:30 +03001153 tb_remove_dp_resources(port->remote->sw);
Rajmohan Manicf29b9af2019-12-17 15:33:43 +03001154 tb_switch_tmu_disable(port->remote->sw);
Mika Westerbergde462032020-04-02 14:50:52 +03001155 tb_switch_unconfigure_link(port->remote->sw);
Mika Westerberg91c0c122019-03-21 19:03:00 +02001156 tb_switch_lane_bonding_disable(port->remote->sw);
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001157 tb_switch_remove(port->remote->sw);
Andreas Noever053596d2014-06-03 22:04:06 +02001158 port->remote = NULL;
Mika Westerbergdfe40ca2019-03-07 15:26:45 +02001159 if (port->dual_link_port)
1160 port->dual_link_port->remote = NULL;
Mika Westerberg8afe9092019-03-26 15:52:30 +03001161 /* Maybe we can create another DP tunnel */
1162 tb_tunnel_dp(tb);
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001163 } 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 Westerberg284652a2020-04-09 14:23:32 +03001179 tb_port_unconfigure_xdomain(port);
Mika Westerberg8afe9092019-03-26 15:52:30 +03001180 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1181 tb_dp_resource_unavailable(tb, port);
Andreas Noever053596d2014-06-03 22:04:06 +02001182 } else {
Mika Westerberg62efe692018-09-17 16:32:13 +03001183 tb_port_dbg(port,
1184 "got unplug event for disconnected port, ignoring\n");
Andreas Noever053596d2014-06-03 22:04:06 +02001185 }
1186 } else if (port->remote) {
Mika Westerberg62efe692018-09-17 16:32:13 +03001187 tb_port_dbg(port, "got plug event for connected port, ignoring\n");
Andreas Noever053596d2014-06-03 22:04:06 +02001188 } else {
Mika Westerberg344e0642017-10-11 17:19:54 +03001189 if (tb_port_is_null(port)) {
Mika Westerberg62efe692018-09-17 16:32:13 +03001190 tb_port_dbg(port, "hotplug: scanning\n");
Mika Westerberg344e0642017-10-11 17:19:54 +03001191 tb_scan_port(port);
1192 if (!port->remote)
Mika Westerberg62efe692018-09-17 16:32:13 +03001193 tb_port_dbg(port, "hotplug: no switch found\n");
Mika Westerberg8afe9092019-03-26 15:52:30 +03001194 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1195 tb_dp_resource_available(tb, port);
Mika Westerberg344e0642017-10-11 17:19:54 +03001196 }
Andreas Noever053596d2014-06-03 22:04:06 +02001197 }
Mika Westerberg8f965ef2019-03-15 14:56:21 +02001198
Mika Westerberg6ac6fae2020-06-05 14:25:02 +03001199 pm_runtime_mark_last_busy(&sw->dev);
1200 pm_runtime_put_autosuspend(&sw->dev);
1201
Mika Westerberg8f965ef2019-03-15 14:56:21 +02001202put_sw:
1203 tb_switch_put(sw);
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02001204out:
1205 mutex_unlock(&tb->lock);
Mika Westerberg6ac6fae2020-06-05 14:25:02 +03001206
1207 pm_runtime_mark_last_busy(&tb->dev);
1208 pm_runtime_put_autosuspend(&tb->dev);
1209
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02001210 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 Westerberg81a54b52017-06-06 15:25:09 +03001218static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1219 const void *buf, size_t size)
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02001220{
Mika Westerberg81a54b52017-06-06 15:25:09 +03001221 const struct cfg_event_pkg *pkg = buf;
Mika Westerberg81a54b52017-06-06 15:25:09 +03001222 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 Westerberg210e9f52019-12-17 15:33:39 +03001231 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) {
Mika Westerberg81a54b52017-06-06 15:25:09 +03001232 tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
1233 pkg->port);
1234 }
1235
Mika Westerberg4f807e42018-09-17 16:30:49 +03001236 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug);
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02001237}
1238
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001239static void tb_stop(struct tb *tb)
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02001240{
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001241 struct tb_cm *tcm = tb_priv(tb);
Mika Westerberg93f36ad2017-02-19 13:48:29 +02001242 struct tb_tunnel *tunnel;
1243 struct tb_tunnel *n;
Andreas Noever3364f0c2014-06-03 22:04:08 +02001244
Mika Westerberg6ac6fae2020-06-05 14:25:02 +03001245 cancel_delayed_work(&tcm->remove_work);
Andreas Noever3364f0c2014-06-03 22:04:08 +02001246 /* tunnels are only present after everything has been initialized */
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001247 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 Westerberg93f36ad2017-02-19 13:48:29 +02001255 tb_tunnel_free(tunnel);
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001256 }
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001257 tb_switch_remove(tb->root_switch);
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001258 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02001259}
1260
Mika Westerberg99cabbb2018-12-30 21:34:08 +02001261static 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 Westerberg9d3cce02017-06-06 15:25:00 +03001282static int tb_start(struct tb *tb)
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02001283{
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001284 struct tb_cm *tcm = tb_priv(tb);
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001285 int ret;
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02001286
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001287 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
Mika Westerberg444ac382018-12-30 12:17:52 +02001288 if (IS_ERR(tb->root_switch))
1289 return PTR_ERR(tb->root_switch);
Andreas Noevera25c8b22014-06-03 22:04:02 +02001290
Mika Westerberge6b245c2017-06-06 15:25:17 +03001291 /*
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 Westerberg6ac6fae2020-06-05 14:25:02 +03001297 /* All USB4 routers support runtime PM */
1298 tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch);
Mika Westerberge6b245c2017-06-06 15:25:17 +03001299
Mika Westerbergbfe778a2017-06-06 15:25:01 +03001300 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 Manicf29b9af2019-12-17 15:33:43 +03001313 /* Enable TMU if it is off */
1314 tb_switch_tmu_enable(tb->root_switch);
Andreas Noever9da672a2014-06-03 22:04:05 +02001315 /* Full scan to discover devices added before the driver was loaded. */
1316 tb_scan_switch(tb->root_switch);
Mika Westerberg0414bec2017-02-19 23:43:26 +02001317 /* Find out tunnels created by the boot firmware */
1318 tb_discover_tunnels(tb->root_switch);
Rajmohan Manie6f81852019-12-17 15:33:44 +03001319 /*
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 Westerberg8afe9092019-03-26 15:52:30 +03001324 /* Add DP IN resources for the root switch */
1325 tb_add_dp_resources(tb->root_switch);
Mika Westerberg99cabbb2018-12-30 21:34:08 +02001326 /* Make the discovered switches available to the userspace */
1327 device_for_each_child(&tb->root_switch->dev, NULL,
1328 tb_scan_finalize_switch);
Andreas Noever9da672a2014-06-03 22:04:05 +02001329
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02001330 /* Allow tb_handle_hotplug to progress events */
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001331 tcm->hotplug_active = true;
1332 return 0;
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02001333}
1334
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001335static int tb_suspend_noirq(struct tb *tb)
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001336{
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001337 struct tb_cm *tcm = tb_priv(tb);
1338
Mika Westerbergdaa51402018-10-01 12:31:19 +03001339 tb_dbg(tb, "suspending...\n");
Mika Westerberg81a2e3e2020-05-16 16:20:39 +03001340 tb_disconnect_and_release_dp(tb);
Mika Westerberg6ac6fae2020-06-05 14:25:02 +03001341 tb_switch_suspend(tb->root_switch, false);
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001342 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
Mika Westerbergdaa51402018-10-01 12:31:19 +03001343 tb_dbg(tb, "suspend finished\n");
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001344
1345 return 0;
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001346}
1347
Mika Westerberg91c0c122019-03-21 19:03:00 +02001348static void tb_restore_children(struct tb_switch *sw)
1349{
1350 struct tb_port *port;
1351
Mika Westerberg6ac6fae2020-06-05 14:25:02 +03001352 /* No need to restore if the router is already unplugged */
1353 if (sw->is_unplugged)
1354 return;
1355
Rajmohan Manicf29b9af2019-12-17 15:33:43 +03001356 if (tb_enable_tmu(sw))
1357 tb_sw_warn(sw, "failed to restore TMU configuration\n");
1358
Mika Westerberg91c0c122019-03-21 19:03:00 +02001359 tb_switch_for_each_port(sw, port) {
Mika Westerberg284652a2020-04-09 14:23:32 +03001360 if (!tb_port_has_remote(port) && !port->xdomain)
Mika Westerberg91c0c122019-03-21 19:03:00 +02001361 continue;
1362
Mika Westerberg284652a2020-04-09 14:23:32 +03001363 if (port->remote) {
1364 tb_switch_lane_bonding_enable(port->remote->sw);
1365 tb_switch_configure_link(port->remote->sw);
Mika Westerberg91c0c122019-03-21 19:03:00 +02001366
Mika Westerberg284652a2020-04-09 14:23:32 +03001367 tb_restore_children(port->remote->sw);
1368 } else if (port->xdomain) {
1369 tb_port_configure_xdomain(port);
1370 }
Mika Westerberg91c0c122019-03-21 19:03:00 +02001371 }
1372}
1373
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001374static int tb_resume_noirq(struct tb *tb)
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001375{
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001376 struct tb_cm *tcm = tb_priv(tb);
Mika Westerberg93f36ad2017-02-19 13:48:29 +02001377 struct tb_tunnel *tunnel, *n;
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001378
Mika Westerbergdaa51402018-10-01 12:31:19 +03001379 tb_dbg(tb, "resuming...\n");
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001380
1381 /* remove any pci devices the firmware might have setup */
Mika Westerberg356b6c42019-09-19 15:25:30 +03001382 tb_switch_reset(tb->root_switch);
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001383
1384 tb_switch_resume(tb->root_switch);
1385 tb_free_invalid_tunnels(tb);
1386 tb_free_unplugged_children(tb->root_switch);
Mika Westerberg91c0c122019-03-21 19:03:00 +02001387 tb_restore_children(tb->root_switch);
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001388 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
Mika Westerberg93f36ad2017-02-19 13:48:29 +02001389 tb_tunnel_restart(tunnel);
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001390 if (!list_empty(&tcm->tunnel_list)) {
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001391 /*
1392 * the pcie links need some time to get going.
1393 * 100ms works for me...
1394 */
Mika Westerbergdaa51402018-10-01 12:31:19 +03001395 tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n");
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001396 msleep(100);
1397 }
1398 /* Allow tb_handle_hotplug to progress events */
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001399 tcm->hotplug_active = true;
Mika Westerbergdaa51402018-10-01 12:31:19 +03001400 tb_dbg(tb, "resume finished\n");
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001401
1402 return 0;
1403}
1404
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001405static int tb_free_unplugged_xdomains(struct tb_switch *sw)
1406{
Mika Westerbergb433d012019-09-30 14:07:22 +03001407 struct tb_port *port;
1408 int ret = 0;
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001409
Mika Westerbergb433d012019-09-30 14:07:22 +03001410 tb_switch_for_each_port(sw, port) {
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001411 if (tb_is_upstream_port(port))
1412 continue;
1413 if (port->xdomain && port->xdomain->is_unplugged) {
Kranthi Kuntaladacb1282020-03-05 16:39:58 +02001414 tb_retimer_remove_all(port);
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001415 tb_xdomain_remove(port->xdomain);
Mika Westerberg284652a2020-04-09 14:23:32 +03001416 tb_port_unconfigure_xdomain(port);
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001417 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 Westerberg884e4d52020-08-31 13:05:14 +03001427static 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
1435static 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 Westerberg7ea4cd62018-09-28 16:41:01 +03001443static 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 Westerberg6ac6fae2020-06-05 14:25:02 +03001456static 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
1468static 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
1481static 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 Westerberg9d3cce02017-06-06 15:25:00 +03001504static 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 Westerberg884e4d52020-08-31 13:05:14 +03001509 .freeze_noirq = tb_freeze_noirq,
1510 .thaw_noirq = tb_thaw_noirq,
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001511 .complete = tb_complete,
Mika Westerberg6ac6fae2020-06-05 14:25:02 +03001512 .runtime_suspend = tb_runtime_suspend,
1513 .runtime_resume = tb_runtime_resume,
Mika Westerberg81a54b52017-06-06 15:25:09 +03001514 .handle_event = tb_handle_event,
Mika Westerberg99cabbb2018-12-30 21:34:08 +02001515 .approve_switch = tb_tunnel_pci,
Mika Westerberg7ea4cd62018-09-28 16:41:01 +03001516 .approve_xdomain_paths = tb_approve_xdomain_paths,
1517 .disconnect_xdomain_paths = tb_disconnect_xdomain_paths,
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001518};
1519
1520struct 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 Westerberg99cabbb2018-12-30 21:34:08 +02001529 tb->security_level = TB_SECURITY_USER;
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001530 tb->cm_ops = &tb_cm_ops;
1531
1532 tcm = tb_priv(tb);
1533 INIT_LIST_HEAD(&tcm->tunnel_list);
Mika Westerberg8afe9092019-03-26 15:52:30 +03001534 INIT_LIST_HEAD(&tcm->dp_resources);
Mika Westerberg6ac6fae2020-06-05 14:25:02 +03001535 INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work);
Mika Westerberg9d3cce02017-06-06 15:25:00 +03001536
1537 return tb;
Andreas Noever23dd5bb2014-06-03 22:04:12 +02001538}