blob: fb01396a62a9d8fda1d3a9ae3736438662e51939 [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>
Lukas Wunner630b3af2017-08-01 14:10:41 +020012#include <linux/platform_data/x86/apple.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
21 * @hotplug_active: tb_handle_hotplug will stop progressing plug
22 * events and exit if this is not set (it needs to
23 * acquire the lock one more time). Used to drain wq
24 * after cfg has been paused.
25 */
26struct tb_cm {
27 struct list_head tunnel_list;
28 bool hotplug_active;
29};
Andreas Noever9da672a2014-06-03 22:04:05 +020030
31/* enumeration & hot plug handling */
32
Mika Westerberg0414bec2017-02-19 23:43:26 +020033static void tb_discover_tunnels(struct tb_switch *sw)
34{
35 struct tb *tb = sw->tb;
36 struct tb_cm *tcm = tb_priv(tb);
37 struct tb_port *port;
38 int i;
39
40 for (i = 1; i <= sw->config.max_port_number; i++) {
41 struct tb_tunnel *tunnel = NULL;
42
43 port = &sw->ports[i];
44 switch (port->config.type) {
45 case TB_TYPE_PCIE_DOWN:
46 tunnel = tb_tunnel_discover_pci(tb, port);
47 break;
48
49 default:
50 break;
51 }
52
53 if (tunnel) {
54 struct tb_switch *parent = tunnel->dst_port->sw;
55
56 while (parent != tunnel->src_port->sw) {
57 parent->boot = true;
58 parent = tb_switch_parent(parent);
59 }
60
61 list_add_tail(&tunnel->list, &tcm->tunnel_list);
62 }
63 }
64
65 for (i = 1; i <= sw->config.max_port_number; i++) {
66 if (tb_port_has_remote(&sw->ports[i]))
67 tb_discover_tunnels(sw->ports[i].remote->sw);
68 }
69}
Andreas Noever9da672a2014-06-03 22:04:05 +020070
71static void tb_scan_port(struct tb_port *port);
72
73/**
74 * tb_scan_switch() - scan for and initialize downstream switches
75 */
76static void tb_scan_switch(struct tb_switch *sw)
77{
78 int i;
79 for (i = 1; i <= sw->config.max_port_number; i++)
80 tb_scan_port(&sw->ports[i]);
81}
82
83/**
84 * tb_scan_port() - check for and initialize switches below port
85 */
86static void tb_scan_port(struct tb_port *port)
87{
Mika Westerberg99cabbb2018-12-30 21:34:08 +020088 struct tb_cm *tcm = tb_priv(port->sw->tb);
Mika Westerbergdfe40ca2019-03-07 15:26:45 +020089 struct tb_port *upstream_port;
Andreas Noever9da672a2014-06-03 22:04:05 +020090 struct tb_switch *sw;
Mika Westerbergdfe40ca2019-03-07 15:26:45 +020091
Andreas Noever9da672a2014-06-03 22:04:05 +020092 if (tb_is_upstream_port(port))
93 return;
94 if (port->config.type != TB_TYPE_PORT)
95 return;
Andreas Noever343fcb82014-06-12 23:11:47 +020096 if (port->dual_link_port && port->link_nr)
97 return; /*
98 * Downstream switch is reachable through two ports.
99 * Only scan on the primary port (link_nr == 0).
100 */
Andreas Noever9da672a2014-06-03 22:04:05 +0200101 if (tb_wait_for_port(port, false) <= 0)
102 return;
103 if (port->remote) {
104 tb_port_WARN(port, "port already has a remote!\n");
105 return;
106 }
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300107 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
108 tb_downstream_route(port));
Andreas Noever9da672a2014-06-03 22:04:05 +0200109 if (!sw)
110 return;
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300111
112 if (tb_switch_configure(sw)) {
113 tb_switch_put(sw);
114 return;
115 }
116
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200117 /*
118 * Do not send uevents until we have discovered all existing
119 * tunnels and know which switches were authorized already by
120 * the boot firmware.
121 */
122 if (!tcm->hotplug_active)
123 dev_set_uevent_suppress(&sw->dev, true);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300124
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300125 if (tb_switch_add(sw)) {
126 tb_switch_put(sw);
127 return;
128 }
129
Mika Westerbergdfe40ca2019-03-07 15:26:45 +0200130 /* Link the switches using both links if available */
131 upstream_port = tb_upstream_port(sw);
132 port->remote = upstream_port;
133 upstream_port->remote = port;
134 if (port->dual_link_port && upstream_port->dual_link_port) {
135 port->dual_link_port->remote = upstream_port->dual_link_port;
136 upstream_port->dual_link_port->remote = port->dual_link_port;
137 }
138
Andreas Noever9da672a2014-06-03 22:04:05 +0200139 tb_scan_switch(sw);
140}
141
Andreas Noever3364f0c2014-06-03 22:04:08 +0200142/**
143 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
144 */
145static void tb_free_invalid_tunnels(struct tb *tb)
146{
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300147 struct tb_cm *tcm = tb_priv(tb);
Mika Westerberg93f36ad2017-02-19 13:48:29 +0200148 struct tb_tunnel *tunnel;
149 struct tb_tunnel *n;
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300150
151 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
Mika Westerberg93f36ad2017-02-19 13:48:29 +0200152 if (tb_tunnel_is_invalid(tunnel)) {
153 tb_tunnel_deactivate(tunnel);
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300154 list_del(&tunnel->list);
Mika Westerberg93f36ad2017-02-19 13:48:29 +0200155 tb_tunnel_free(tunnel);
Andreas Noever3364f0c2014-06-03 22:04:08 +0200156 }
157 }
158}
159
160/**
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200161 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
162 */
163static void tb_free_unplugged_children(struct tb_switch *sw)
164{
165 int i;
166 for (i = 1; i <= sw->config.max_port_number; i++) {
167 struct tb_port *port = &sw->ports[i];
Mika Westerbergdfe40ca2019-03-07 15:26:45 +0200168
169 if (!tb_port_has_remote(port))
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200170 continue;
Mika Westerbergdfe40ca2019-03-07 15:26:45 +0200171
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200172 if (port->remote->sw->is_unplugged) {
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300173 tb_switch_remove(port->remote->sw);
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200174 port->remote = NULL;
Mika Westerbergdfe40ca2019-03-07 15:26:45 +0200175 if (port->dual_link_port)
176 port->dual_link_port->remote = NULL;
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200177 } else {
178 tb_free_unplugged_children(port->remote->sw);
179 }
180 }
181}
182
183
184/**
Andreas Noever3364f0c2014-06-03 22:04:08 +0200185 * find_pci_up_port() - return the first PCIe up port on @sw or NULL
186 */
187static struct tb_port *tb_find_pci_up_port(struct tb_switch *sw)
188{
189 int i;
190 for (i = 1; i <= sw->config.max_port_number; i++)
191 if (sw->ports[i].config.type == TB_TYPE_PCIE_UP)
192 return &sw->ports[i];
193 return NULL;
194}
195
196/**
197 * find_unused_down_port() - return the first inactive PCIe down port on @sw
198 */
199static struct tb_port *tb_find_unused_down_port(struct tb_switch *sw)
200{
201 int i;
202 int cap;
203 int res;
204 int data;
205 for (i = 1; i <= sw->config.max_port_number; i++) {
206 if (tb_is_upstream_port(&sw->ports[i]))
207 continue;
208 if (sw->ports[i].config.type != TB_TYPE_PCIE_DOWN)
209 continue;
Mika Westerberg56183c82017-02-19 10:39:34 +0200210 cap = sw->ports[i].cap_adap;
211 if (!cap)
Andreas Noever3364f0c2014-06-03 22:04:08 +0200212 continue;
213 res = tb_port_read(&sw->ports[i], &data, TB_CFG_PORT, cap, 1);
214 if (res < 0)
215 continue;
216 if (data & 0x80000000)
217 continue;
218 return &sw->ports[i];
219 }
220 return NULL;
221}
222
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200223static struct tb_port *tb_find_pcie_down(struct tb_switch *sw,
224 const struct tb_port *port)
Andreas Noever3364f0c2014-06-03 22:04:08 +0200225{
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200226 /*
227 * To keep plugging devices consistently in the same PCIe
228 * hierarchy, do mapping here for root switch downstream PCIe
229 * ports.
230 */
231 if (!tb_route(sw)) {
232 int phy_port = tb_phy_port_from_link(port->port);
233 int index;
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300234
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200235 /*
236 * Hard-coded Thunderbolt port to PCIe down port mapping
237 * per controller.
238 */
239 if (tb_switch_is_cr(sw))
240 index = !phy_port ? 6 : 7;
241 else if (tb_switch_is_fr(sw))
242 index = !phy_port ? 6 : 8;
243 else
244 goto out;
Andreas Noever3364f0c2014-06-03 22:04:08 +0200245
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200246 /* Validate the hard-coding */
247 if (WARN_ON(index > sw->config.max_port_number))
248 goto out;
249 if (WARN_ON(!tb_port_is_pcie_down(&sw->ports[index])))
250 goto out;
251 if (WARN_ON(tb_pci_port_is_enabled(&sw->ports[index])))
252 goto out;
Andreas Noever3364f0c2014-06-03 22:04:08 +0200253
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200254 return &sw->ports[index];
Andreas Noever3364f0c2014-06-03 22:04:08 +0200255 }
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200256
257out:
258 return tb_find_unused_down_port(sw);
259}
260
261static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
262{
263 struct tb_port *up, *down, *port;
264 struct tb_cm *tcm = tb_priv(tb);
265 struct tb_switch *parent_sw;
266 struct tb_tunnel *tunnel;
267
268 up = tb_find_pci_up_port(sw);
269 if (!up)
270 return 0;
271
272 /*
273 * Look up available down port. Since we are chaining it should
274 * be found right above this switch.
275 */
276 parent_sw = tb_to_switch(sw->dev.parent);
277 port = tb_port_at(tb_route(sw), parent_sw);
278 down = tb_find_pcie_down(parent_sw, port);
279 if (!down)
280 return 0;
281
282 tunnel = tb_tunnel_alloc_pci(tb, up, down);
283 if (!tunnel)
284 return -ENOMEM;
285
286 if (tb_tunnel_activate(tunnel)) {
287 tb_port_info(up,
288 "PCIe tunnel activation failed, aborting\n");
289 tb_tunnel_free(tunnel);
290 return -EIO;
291 }
292
293 list_add_tail(&tunnel->list, &tcm->tunnel_list);
294 return 0;
Andreas Noever3364f0c2014-06-03 22:04:08 +0200295}
Andreas Noever9da672a2014-06-03 22:04:05 +0200296
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200297/* hotplug handling */
298
299struct tb_hotplug_event {
300 struct work_struct work;
301 struct tb *tb;
302 u64 route;
303 u8 port;
304 bool unplug;
305};
306
307/**
308 * tb_handle_hotplug() - handle hotplug event
309 *
310 * Executes on tb->wq.
311 */
312static void tb_handle_hotplug(struct work_struct *work)
313{
314 struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
315 struct tb *tb = ev->tb;
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300316 struct tb_cm *tcm = tb_priv(tb);
Andreas Noever053596d2014-06-03 22:04:06 +0200317 struct tb_switch *sw;
318 struct tb_port *port;
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200319 mutex_lock(&tb->lock);
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300320 if (!tcm->hotplug_active)
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200321 goto out; /* during init, suspend or shutdown */
322
Mika Westerberg8f965ef2019-03-15 14:56:21 +0200323 sw = tb_switch_find_by_route(tb, ev->route);
Andreas Noever053596d2014-06-03 22:04:06 +0200324 if (!sw) {
325 tb_warn(tb,
326 "hotplug event from non existent switch %llx:%x (unplug: %d)\n",
327 ev->route, ev->port, ev->unplug);
328 goto out;
329 }
330 if (ev->port > sw->config.max_port_number) {
331 tb_warn(tb,
332 "hotplug event from non existent port %llx:%x (unplug: %d)\n",
333 ev->route, ev->port, ev->unplug);
Mika Westerberg8f965ef2019-03-15 14:56:21 +0200334 goto put_sw;
Andreas Noever053596d2014-06-03 22:04:06 +0200335 }
336 port = &sw->ports[ev->port];
337 if (tb_is_upstream_port(port)) {
Mika Westerbergdfe40ca2019-03-07 15:26:45 +0200338 tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n",
339 ev->route, ev->port, ev->unplug);
Mika Westerberg8f965ef2019-03-15 14:56:21 +0200340 goto put_sw;
Andreas Noever053596d2014-06-03 22:04:06 +0200341 }
342 if (ev->unplug) {
Mika Westerbergdfe40ca2019-03-07 15:26:45 +0200343 if (tb_port_has_remote(port)) {
Andreas Noever053596d2014-06-03 22:04:06 +0200344 tb_port_info(port, "unplugged\n");
Lukas Wunneraae20bb2016-03-20 13:57:20 +0100345 tb_sw_set_unplugged(port->remote->sw);
Andreas Noever3364f0c2014-06-03 22:04:08 +0200346 tb_free_invalid_tunnels(tb);
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300347 tb_switch_remove(port->remote->sw);
Andreas Noever053596d2014-06-03 22:04:06 +0200348 port->remote = NULL;
Mika Westerbergdfe40ca2019-03-07 15:26:45 +0200349 if (port->dual_link_port)
350 port->dual_link_port->remote = NULL;
Andreas Noever053596d2014-06-03 22:04:06 +0200351 } else {
352 tb_port_info(port,
353 "got unplug event for disconnected port, ignoring\n");
354 }
355 } else if (port->remote) {
356 tb_port_info(port,
357 "got plug event for connected port, ignoring\n");
358 } else {
Mika Westerberg344e0642017-10-11 17:19:54 +0300359 if (tb_port_is_null(port)) {
360 tb_port_info(port, "hotplug: scanning\n");
361 tb_scan_port(port);
362 if (!port->remote)
363 tb_port_info(port, "hotplug: no switch found\n");
364 }
Andreas Noever053596d2014-06-03 22:04:06 +0200365 }
Mika Westerberg8f965ef2019-03-15 14:56:21 +0200366
367put_sw:
368 tb_switch_put(sw);
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200369out:
370 mutex_unlock(&tb->lock);
371 kfree(ev);
372}
373
374/**
375 * tb_schedule_hotplug_handler() - callback function for the control channel
376 *
377 * Delegates to tb_handle_hotplug.
378 */
Mika Westerberg81a54b52017-06-06 15:25:09 +0300379static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
380 const void *buf, size_t size)
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200381{
Mika Westerberg81a54b52017-06-06 15:25:09 +0300382 const struct cfg_event_pkg *pkg = buf;
383 struct tb_hotplug_event *ev;
384 u64 route;
385
386 if (type != TB_CFG_PKG_EVENT) {
387 tb_warn(tb, "unexpected event %#x, ignoring\n", type);
388 return;
389 }
390
391 route = tb_cfg_get_route(&pkg->header);
392
393 if (tb_cfg_error(tb->ctl, route, pkg->port,
394 TB_CFG_ERROR_ACK_PLUG_EVENT)) {
395 tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
396 pkg->port);
397 }
398
399 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200400 if (!ev)
401 return;
402 INIT_WORK(&ev->work, tb_handle_hotplug);
403 ev->tb = tb;
404 ev->route = route;
Mika Westerberg81a54b52017-06-06 15:25:09 +0300405 ev->port = pkg->port;
406 ev->unplug = pkg->unplug;
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200407 queue_work(tb->wq, &ev->work);
408}
409
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300410static void tb_stop(struct tb *tb)
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200411{
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300412 struct tb_cm *tcm = tb_priv(tb);
Mika Westerberg93f36ad2017-02-19 13:48:29 +0200413 struct tb_tunnel *tunnel;
414 struct tb_tunnel *n;
Andreas Noever3364f0c2014-06-03 22:04:08 +0200415
Andreas Noever3364f0c2014-06-03 22:04:08 +0200416 /* tunnels are only present after everything has been initialized */
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300417 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
Mika Westerberg93f36ad2017-02-19 13:48:29 +0200418 tb_tunnel_deactivate(tunnel);
419 tb_tunnel_free(tunnel);
Andreas Noever3364f0c2014-06-03 22:04:08 +0200420 }
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300421 tb_switch_remove(tb->root_switch);
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300422 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200423}
424
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200425static int tb_scan_finalize_switch(struct device *dev, void *data)
426{
427 if (tb_is_switch(dev)) {
428 struct tb_switch *sw = tb_to_switch(dev);
429
430 /*
431 * If we found that the switch was already setup by the
432 * boot firmware, mark it as authorized now before we
433 * send uevent to userspace.
434 */
435 if (sw->boot)
436 sw->authorized = 1;
437
438 dev_set_uevent_suppress(dev, false);
439 kobject_uevent(&dev->kobj, KOBJ_ADD);
440 device_for_each_child(dev, NULL, tb_scan_finalize_switch);
441 }
442
443 return 0;
444}
445
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300446static int tb_start(struct tb *tb)
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200447{
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300448 struct tb_cm *tcm = tb_priv(tb);
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300449 int ret;
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200450
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300451 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200452 if (!tb->root_switch)
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300453 return -ENOMEM;
Andreas Noevera25c8b22014-06-03 22:04:02 +0200454
Mika Westerberge6b245c2017-06-06 15:25:17 +0300455 /*
456 * ICM firmware upgrade needs running firmware and in native
457 * mode that is not available so disable firmware upgrade of the
458 * root switch.
459 */
460 tb->root_switch->no_nvm_upgrade = true;
461
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300462 ret = tb_switch_configure(tb->root_switch);
463 if (ret) {
464 tb_switch_put(tb->root_switch);
465 return ret;
466 }
467
468 /* Announce the switch to the world */
469 ret = tb_switch_add(tb->root_switch);
470 if (ret) {
471 tb_switch_put(tb->root_switch);
472 return ret;
473 }
474
Andreas Noever9da672a2014-06-03 22:04:05 +0200475 /* Full scan to discover devices added before the driver was loaded. */
476 tb_scan_switch(tb->root_switch);
Mika Westerberg0414bec2017-02-19 23:43:26 +0200477 /* Find out tunnels created by the boot firmware */
478 tb_discover_tunnels(tb->root_switch);
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200479 /* Make the discovered switches available to the userspace */
480 device_for_each_child(&tb->root_switch->dev, NULL,
481 tb_scan_finalize_switch);
Andreas Noever9da672a2014-06-03 22:04:05 +0200482
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200483 /* Allow tb_handle_hotplug to progress events */
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300484 tcm->hotplug_active = true;
485 return 0;
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200486}
487
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300488static int tb_suspend_noirq(struct tb *tb)
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200489{
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300490 struct tb_cm *tcm = tb_priv(tb);
491
Mika Westerbergdaa51402018-10-01 12:31:19 +0300492 tb_dbg(tb, "suspending...\n");
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200493 tb_switch_suspend(tb->root_switch);
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300494 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
Mika Westerbergdaa51402018-10-01 12:31:19 +0300495 tb_dbg(tb, "suspend finished\n");
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300496
497 return 0;
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200498}
499
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300500static int tb_resume_noirq(struct tb *tb)
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200501{
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300502 struct tb_cm *tcm = tb_priv(tb);
Mika Westerberg93f36ad2017-02-19 13:48:29 +0200503 struct tb_tunnel *tunnel, *n;
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300504
Mika Westerbergdaa51402018-10-01 12:31:19 +0300505 tb_dbg(tb, "resuming...\n");
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200506
507 /* remove any pci devices the firmware might have setup */
508 tb_switch_reset(tb, 0);
509
510 tb_switch_resume(tb->root_switch);
511 tb_free_invalid_tunnels(tb);
512 tb_free_unplugged_children(tb->root_switch);
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300513 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
Mika Westerberg93f36ad2017-02-19 13:48:29 +0200514 tb_tunnel_restart(tunnel);
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300515 if (!list_empty(&tcm->tunnel_list)) {
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200516 /*
517 * the pcie links need some time to get going.
518 * 100ms works for me...
519 */
Mika Westerbergdaa51402018-10-01 12:31:19 +0300520 tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n");
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200521 msleep(100);
522 }
523 /* Allow tb_handle_hotplug to progress events */
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300524 tcm->hotplug_active = true;
Mika Westerbergdaa51402018-10-01 12:31:19 +0300525 tb_dbg(tb, "resume finished\n");
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300526
527 return 0;
528}
529
530static const struct tb_cm_ops tb_cm_ops = {
531 .start = tb_start,
532 .stop = tb_stop,
533 .suspend_noirq = tb_suspend_noirq,
534 .resume_noirq = tb_resume_noirq,
Mika Westerberg81a54b52017-06-06 15:25:09 +0300535 .handle_event = tb_handle_event,
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200536 .approve_switch = tb_tunnel_pci,
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300537};
538
539struct tb *tb_probe(struct tb_nhi *nhi)
540{
541 struct tb_cm *tcm;
542 struct tb *tb;
543
Lukas Wunner630b3af2017-08-01 14:10:41 +0200544 if (!x86_apple_machine)
Mika Westerbergf67cf492017-06-06 15:25:16 +0300545 return NULL;
546
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300547 tb = tb_domain_alloc(nhi, sizeof(*tcm));
548 if (!tb)
549 return NULL;
550
Mika Westerberg99cabbb2018-12-30 21:34:08 +0200551 tb->security_level = TB_SECURITY_USER;
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300552 tb->cm_ops = &tb_cm_ops;
553
554 tcm = tb_priv(tb);
555 INIT_LIST_HEAD(&tcm->tunnel_list);
556
557 return tb;
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200558}