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 | /* |
| 3 | * Thunderbolt Cactus Ridge driver - bus logic (NHI independent) |
| 4 | * |
| 5 | * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> |
| 6 | */ |
| 7 | |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/errno.h> |
| 10 | #include <linux/delay.h> |
Lukas Wunner | 630b3af | 2017-08-01 14:10:41 +0200 | [diff] [blame] | 11 | #include <linux/platform_data/x86/apple.h> |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 12 | |
| 13 | #include "tb.h" |
Andreas Noever | 7adf609 | 2014-06-03 22:04:01 +0200 | [diff] [blame] | 14 | #include "tb_regs.h" |
Mika Westerberg | 1752b9f | 2017-02-19 10:58:35 +0200 | [diff] [blame] | 15 | #include "tunnel.h" |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 16 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 17 | /** |
| 18 | * struct tb_cm - Simple Thunderbolt connection manager |
| 19 | * @tunnel_list: List of active tunnels |
| 20 | * @hotplug_active: tb_handle_hotplug will stop progressing plug |
| 21 | * events and exit if this is not set (it needs to |
| 22 | * acquire the lock one more time). Used to drain wq |
| 23 | * after cfg has been paused. |
| 24 | */ |
| 25 | struct tb_cm { |
| 26 | struct list_head tunnel_list; |
| 27 | bool hotplug_active; |
| 28 | }; |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 29 | |
| 30 | /* enumeration & hot plug handling */ |
| 31 | |
| 32 | |
| 33 | static void tb_scan_port(struct tb_port *port); |
| 34 | |
| 35 | /** |
| 36 | * tb_scan_switch() - scan for and initialize downstream switches |
| 37 | */ |
| 38 | static void tb_scan_switch(struct tb_switch *sw) |
| 39 | { |
| 40 | int i; |
| 41 | for (i = 1; i <= sw->config.max_port_number; i++) |
| 42 | tb_scan_port(&sw->ports[i]); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * tb_scan_port() - check for and initialize switches below port |
| 47 | */ |
| 48 | static void tb_scan_port(struct tb_port *port) |
| 49 | { |
| 50 | struct tb_switch *sw; |
| 51 | if (tb_is_upstream_port(port)) |
| 52 | return; |
| 53 | if (port->config.type != TB_TYPE_PORT) |
| 54 | return; |
Andreas Noever | 343fcb8 | 2014-06-12 23:11:47 +0200 | [diff] [blame] | 55 | if (port->dual_link_port && port->link_nr) |
| 56 | return; /* |
| 57 | * Downstream switch is reachable through two ports. |
| 58 | * Only scan on the primary port (link_nr == 0). |
| 59 | */ |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 60 | if (tb_wait_for_port(port, false) <= 0) |
| 61 | return; |
| 62 | if (port->remote) { |
| 63 | tb_port_WARN(port, "port already has a remote!\n"); |
| 64 | return; |
| 65 | } |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 66 | sw = tb_switch_alloc(port->sw->tb, &port->sw->dev, |
| 67 | tb_downstream_route(port)); |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 68 | if (!sw) |
| 69 | return; |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 70 | |
| 71 | if (tb_switch_configure(sw)) { |
| 72 | tb_switch_put(sw); |
| 73 | return; |
| 74 | } |
| 75 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 76 | sw->authorized = true; |
| 77 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 78 | if (tb_switch_add(sw)) { |
| 79 | tb_switch_put(sw); |
| 80 | return; |
| 81 | } |
| 82 | |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 83 | port->remote = tb_upstream_port(sw); |
| 84 | tb_upstream_port(sw)->remote = port; |
| 85 | tb_scan_switch(sw); |
| 86 | } |
| 87 | |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 88 | /** |
| 89 | * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away |
| 90 | */ |
| 91 | static void tb_free_invalid_tunnels(struct tb *tb) |
| 92 | { |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 93 | struct tb_cm *tcm = tb_priv(tb); |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame^] | 94 | struct tb_tunnel *tunnel; |
| 95 | struct tb_tunnel *n; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 96 | |
| 97 | list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame^] | 98 | if (tb_tunnel_is_invalid(tunnel)) { |
| 99 | tb_tunnel_deactivate(tunnel); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 100 | list_del(&tunnel->list); |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame^] | 101 | tb_tunnel_free(tunnel); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 107 | * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches |
| 108 | */ |
| 109 | static void tb_free_unplugged_children(struct tb_switch *sw) |
| 110 | { |
| 111 | int i; |
| 112 | for (i = 1; i <= sw->config.max_port_number; i++) { |
| 113 | struct tb_port *port = &sw->ports[i]; |
| 114 | if (tb_is_upstream_port(port)) |
| 115 | continue; |
| 116 | if (!port->remote) |
| 117 | continue; |
| 118 | if (port->remote->sw->is_unplugged) { |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 119 | tb_switch_remove(port->remote->sw); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 120 | port->remote = NULL; |
| 121 | } else { |
| 122 | tb_free_unplugged_children(port->remote->sw); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /** |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 129 | * find_pci_up_port() - return the first PCIe up port on @sw or NULL |
| 130 | */ |
| 131 | static struct tb_port *tb_find_pci_up_port(struct tb_switch *sw) |
| 132 | { |
| 133 | int i; |
| 134 | for (i = 1; i <= sw->config.max_port_number; i++) |
| 135 | if (sw->ports[i].config.type == TB_TYPE_PCIE_UP) |
| 136 | return &sw->ports[i]; |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * find_unused_down_port() - return the first inactive PCIe down port on @sw |
| 142 | */ |
| 143 | static struct tb_port *tb_find_unused_down_port(struct tb_switch *sw) |
| 144 | { |
| 145 | int i; |
| 146 | int cap; |
| 147 | int res; |
| 148 | int data; |
| 149 | for (i = 1; i <= sw->config.max_port_number; i++) { |
| 150 | if (tb_is_upstream_port(&sw->ports[i])) |
| 151 | continue; |
| 152 | if (sw->ports[i].config.type != TB_TYPE_PCIE_DOWN) |
| 153 | continue; |
Mika Westerberg | 56183c8 | 2017-02-19 10:39:34 +0200 | [diff] [blame] | 154 | cap = sw->ports[i].cap_adap; |
| 155 | if (!cap) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 156 | continue; |
| 157 | res = tb_port_read(&sw->ports[i], &data, TB_CFG_PORT, cap, 1); |
| 158 | if (res < 0) |
| 159 | continue; |
| 160 | if (data & 0x80000000) |
| 161 | continue; |
| 162 | return &sw->ports[i]; |
| 163 | } |
| 164 | return NULL; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * tb_activate_pcie_devices() - scan for and activate PCIe devices |
| 169 | * |
| 170 | * This method is somewhat ad hoc. For now it only supports one device |
| 171 | * per port and only devices at depth 1. |
| 172 | */ |
| 173 | static void tb_activate_pcie_devices(struct tb *tb) |
| 174 | { |
| 175 | int i; |
| 176 | int cap; |
| 177 | u32 data; |
| 178 | struct tb_switch *sw; |
| 179 | struct tb_port *up_port; |
| 180 | struct tb_port *down_port; |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame^] | 181 | struct tb_tunnel *tunnel; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 182 | struct tb_cm *tcm = tb_priv(tb); |
| 183 | |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 184 | /* scan for pcie devices at depth 1*/ |
| 185 | for (i = 1; i <= tb->root_switch->config.max_port_number; i++) { |
| 186 | if (tb_is_upstream_port(&tb->root_switch->ports[i])) |
| 187 | continue; |
| 188 | if (tb->root_switch->ports[i].config.type != TB_TYPE_PORT) |
| 189 | continue; |
| 190 | if (!tb->root_switch->ports[i].remote) |
| 191 | continue; |
| 192 | sw = tb->root_switch->ports[i].remote->sw; |
| 193 | up_port = tb_find_pci_up_port(sw); |
| 194 | if (!up_port) { |
| 195 | tb_sw_info(sw, "no PCIe devices found, aborting\n"); |
| 196 | continue; |
| 197 | } |
| 198 | |
| 199 | /* check whether port is already activated */ |
Mika Westerberg | 56183c8 | 2017-02-19 10:39:34 +0200 | [diff] [blame] | 200 | cap = up_port->cap_adap; |
| 201 | if (!cap) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 202 | continue; |
| 203 | if (tb_port_read(up_port, &data, TB_CFG_PORT, cap, 1)) |
| 204 | continue; |
| 205 | if (data & 0x80000000) { |
| 206 | tb_port_info(up_port, |
| 207 | "PCIe port already activated, aborting\n"); |
| 208 | continue; |
| 209 | } |
| 210 | |
| 211 | down_port = tb_find_unused_down_port(tb->root_switch); |
| 212 | if (!down_port) { |
| 213 | tb_port_info(up_port, |
| 214 | "All PCIe down ports are occupied, aborting\n"); |
| 215 | continue; |
| 216 | } |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame^] | 217 | tunnel = tb_tunnel_alloc_pci(tb, up_port, down_port); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 218 | if (!tunnel) { |
| 219 | tb_port_info(up_port, |
| 220 | "PCIe tunnel allocation failed, aborting\n"); |
| 221 | continue; |
| 222 | } |
| 223 | |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame^] | 224 | if (tb_tunnel_activate(tunnel)) { |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 225 | tb_port_info(up_port, |
| 226 | "PCIe tunnel activation failed, aborting\n"); |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame^] | 227 | tb_tunnel_free(tunnel); |
Gustavo A. R. Silva | a2e3734 | 2017-11-04 23:52:54 -0500 | [diff] [blame] | 228 | continue; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 231 | list_add(&tunnel->list, &tcm->tunnel_list); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 232 | } |
| 233 | } |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 234 | |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 235 | /* hotplug handling */ |
| 236 | |
| 237 | struct tb_hotplug_event { |
| 238 | struct work_struct work; |
| 239 | struct tb *tb; |
| 240 | u64 route; |
| 241 | u8 port; |
| 242 | bool unplug; |
| 243 | }; |
| 244 | |
| 245 | /** |
| 246 | * tb_handle_hotplug() - handle hotplug event |
| 247 | * |
| 248 | * Executes on tb->wq. |
| 249 | */ |
| 250 | static void tb_handle_hotplug(struct work_struct *work) |
| 251 | { |
| 252 | struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work); |
| 253 | struct tb *tb = ev->tb; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 254 | struct tb_cm *tcm = tb_priv(tb); |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 255 | struct tb_switch *sw; |
| 256 | struct tb_port *port; |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 257 | mutex_lock(&tb->lock); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 258 | if (!tcm->hotplug_active) |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 259 | goto out; /* during init, suspend or shutdown */ |
| 260 | |
Mika Westerberg | 8f965ef | 2019-03-15 14:56:21 +0200 | [diff] [blame] | 261 | sw = tb_switch_find_by_route(tb, ev->route); |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 262 | if (!sw) { |
| 263 | tb_warn(tb, |
| 264 | "hotplug event from non existent switch %llx:%x (unplug: %d)\n", |
| 265 | ev->route, ev->port, ev->unplug); |
| 266 | goto out; |
| 267 | } |
| 268 | if (ev->port > sw->config.max_port_number) { |
| 269 | tb_warn(tb, |
| 270 | "hotplug event from non existent port %llx:%x (unplug: %d)\n", |
| 271 | ev->route, ev->port, ev->unplug); |
Mika Westerberg | 8f965ef | 2019-03-15 14:56:21 +0200 | [diff] [blame] | 272 | goto put_sw; |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 273 | } |
| 274 | port = &sw->ports[ev->port]; |
| 275 | if (tb_is_upstream_port(port)) { |
| 276 | tb_warn(tb, |
| 277 | "hotplug event for upstream port %llx:%x (unplug: %d)\n", |
| 278 | ev->route, ev->port, ev->unplug); |
Mika Westerberg | 8f965ef | 2019-03-15 14:56:21 +0200 | [diff] [blame] | 279 | goto put_sw; |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 280 | } |
| 281 | if (ev->unplug) { |
| 282 | if (port->remote) { |
| 283 | tb_port_info(port, "unplugged\n"); |
Lukas Wunner | aae20bb | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 284 | tb_sw_set_unplugged(port->remote->sw); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 285 | tb_free_invalid_tunnels(tb); |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 286 | tb_switch_remove(port->remote->sw); |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 287 | port->remote = NULL; |
| 288 | } else { |
| 289 | tb_port_info(port, |
| 290 | "got unplug event for disconnected port, ignoring\n"); |
| 291 | } |
| 292 | } else if (port->remote) { |
| 293 | tb_port_info(port, |
| 294 | "got plug event for connected port, ignoring\n"); |
| 295 | } else { |
| 296 | tb_port_info(port, "hotplug: scanning\n"); |
| 297 | tb_scan_port(port); |
| 298 | if (!port->remote) { |
| 299 | tb_port_info(port, "hotplug: no switch found\n"); |
| 300 | } else if (port->remote->sw->config.depth > 1) { |
| 301 | tb_sw_warn(port->remote->sw, |
| 302 | "hotplug: chaining not supported\n"); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 303 | } else { |
| 304 | tb_sw_info(port->remote->sw, |
| 305 | "hotplug: activating pcie devices\n"); |
| 306 | tb_activate_pcie_devices(tb); |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 307 | } |
| 308 | } |
Mika Westerberg | 8f965ef | 2019-03-15 14:56:21 +0200 | [diff] [blame] | 309 | |
| 310 | put_sw: |
| 311 | tb_switch_put(sw); |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 312 | out: |
| 313 | mutex_unlock(&tb->lock); |
| 314 | kfree(ev); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * tb_schedule_hotplug_handler() - callback function for the control channel |
| 319 | * |
| 320 | * Delegates to tb_handle_hotplug. |
| 321 | */ |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 322 | static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type, |
| 323 | const void *buf, size_t size) |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 324 | { |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 325 | const struct cfg_event_pkg *pkg = buf; |
| 326 | struct tb_hotplug_event *ev; |
| 327 | u64 route; |
| 328 | |
| 329 | if (type != TB_CFG_PKG_EVENT) { |
| 330 | tb_warn(tb, "unexpected event %#x, ignoring\n", type); |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | route = tb_cfg_get_route(&pkg->header); |
| 335 | |
| 336 | if (tb_cfg_error(tb->ctl, route, pkg->port, |
| 337 | TB_CFG_ERROR_ACK_PLUG_EVENT)) { |
| 338 | tb_warn(tb, "could not ack plug event on %llx:%x\n", route, |
| 339 | pkg->port); |
| 340 | } |
| 341 | |
| 342 | ev = kmalloc(sizeof(*ev), GFP_KERNEL); |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 343 | if (!ev) |
| 344 | return; |
| 345 | INIT_WORK(&ev->work, tb_handle_hotplug); |
| 346 | ev->tb = tb; |
| 347 | ev->route = route; |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 348 | ev->port = pkg->port; |
| 349 | ev->unplug = pkg->unplug; |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 350 | queue_work(tb->wq, &ev->work); |
| 351 | } |
| 352 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 353 | static void tb_stop(struct tb *tb) |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 354 | { |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 355 | struct tb_cm *tcm = tb_priv(tb); |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame^] | 356 | struct tb_tunnel *tunnel; |
| 357 | struct tb_tunnel *n; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 358 | |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 359 | /* tunnels are only present after everything has been initialized */ |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 360 | list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame^] | 361 | tb_tunnel_deactivate(tunnel); |
| 362 | tb_tunnel_free(tunnel); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 363 | } |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 364 | tb_switch_remove(tb->root_switch); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 365 | tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 366 | } |
| 367 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 368 | static int tb_start(struct tb *tb) |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 369 | { |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 370 | struct tb_cm *tcm = tb_priv(tb); |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 371 | int ret; |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 372 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 373 | tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 374 | if (!tb->root_switch) |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 375 | return -ENOMEM; |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 376 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 377 | /* |
| 378 | * ICM firmware upgrade needs running firmware and in native |
| 379 | * mode that is not available so disable firmware upgrade of the |
| 380 | * root switch. |
| 381 | */ |
| 382 | tb->root_switch->no_nvm_upgrade = true; |
| 383 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 384 | ret = tb_switch_configure(tb->root_switch); |
| 385 | if (ret) { |
| 386 | tb_switch_put(tb->root_switch); |
| 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | /* Announce the switch to the world */ |
| 391 | ret = tb_switch_add(tb->root_switch); |
| 392 | if (ret) { |
| 393 | tb_switch_put(tb->root_switch); |
| 394 | return ret; |
| 395 | } |
| 396 | |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 397 | /* Full scan to discover devices added before the driver was loaded. */ |
| 398 | tb_scan_switch(tb->root_switch); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 399 | tb_activate_pcie_devices(tb); |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 400 | |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 401 | /* Allow tb_handle_hotplug to progress events */ |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 402 | tcm->hotplug_active = true; |
| 403 | return 0; |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 404 | } |
| 405 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 406 | static int tb_suspend_noirq(struct tb *tb) |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 407 | { |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 408 | struct tb_cm *tcm = tb_priv(tb); |
| 409 | |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 410 | tb_dbg(tb, "suspending...\n"); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 411 | tb_switch_suspend(tb->root_switch); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 412 | tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 413 | tb_dbg(tb, "suspend finished\n"); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 414 | |
| 415 | return 0; |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 416 | } |
| 417 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 418 | static int tb_resume_noirq(struct tb *tb) |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 419 | { |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 420 | struct tb_cm *tcm = tb_priv(tb); |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame^] | 421 | struct tb_tunnel *tunnel, *n; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 422 | |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 423 | tb_dbg(tb, "resuming...\n"); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 424 | |
| 425 | /* remove any pci devices the firmware might have setup */ |
| 426 | tb_switch_reset(tb, 0); |
| 427 | |
| 428 | tb_switch_resume(tb->root_switch); |
| 429 | tb_free_invalid_tunnels(tb); |
| 430 | tb_free_unplugged_children(tb->root_switch); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 431 | list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame^] | 432 | tb_tunnel_restart(tunnel); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 433 | if (!list_empty(&tcm->tunnel_list)) { |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 434 | /* |
| 435 | * the pcie links need some time to get going. |
| 436 | * 100ms works for me... |
| 437 | */ |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 438 | tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n"); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 439 | msleep(100); |
| 440 | } |
| 441 | /* Allow tb_handle_hotplug to progress events */ |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 442 | tcm->hotplug_active = true; |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 443 | tb_dbg(tb, "resume finished\n"); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | static const struct tb_cm_ops tb_cm_ops = { |
| 449 | .start = tb_start, |
| 450 | .stop = tb_stop, |
| 451 | .suspend_noirq = tb_suspend_noirq, |
| 452 | .resume_noirq = tb_resume_noirq, |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 453 | .handle_event = tb_handle_event, |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 454 | }; |
| 455 | |
| 456 | struct tb *tb_probe(struct tb_nhi *nhi) |
| 457 | { |
| 458 | struct tb_cm *tcm; |
| 459 | struct tb *tb; |
| 460 | |
Lukas Wunner | 630b3af | 2017-08-01 14:10:41 +0200 | [diff] [blame] | 461 | if (!x86_apple_machine) |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 462 | return NULL; |
| 463 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 464 | tb = tb_domain_alloc(nhi, sizeof(*tcm)); |
| 465 | if (!tb) |
| 466 | return NULL; |
| 467 | |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 468 | tb->security_level = TB_SECURITY_NONE; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 469 | tb->cm_ops = &tb_cm_ops; |
| 470 | |
| 471 | tcm = tb_priv(tb); |
| 472 | INIT_LIST_HEAD(&tcm->tunnel_list); |
| 473 | |
| 474 | return tb; |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 475 | } |