Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 2 | /* |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 3 | * Thunderbolt driver - bus logic (NHI independent) |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 6 | * Copyright (C) 2019, Intel Corporation |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/errno.h> |
| 11 | #include <linux/delay.h> |
Lukas Wunner | 630b3af | 2017-08-01 14:10:41 +0200 | [diff] [blame] | 12 | #include <linux/platform_data/x86/apple.h> |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 13 | |
| 14 | #include "tb.h" |
Andreas Noever | 7adf609 | 2014-06-03 22:04:01 +0200 | [diff] [blame] | 15 | #include "tb_regs.h" |
Mika Westerberg | 1752b9f | 2017-02-19 10:58:35 +0200 | [diff] [blame] | 16 | #include "tunnel.h" |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 17 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 18 | /** |
| 19 | * struct tb_cm - Simple Thunderbolt connection manager |
| 20 | * @tunnel_list: List of active tunnels |
| 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 | */ |
| 26 | struct tb_cm { |
| 27 | struct list_head tunnel_list; |
| 28 | bool hotplug_active; |
| 29 | }; |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 30 | |
| 31 | /* enumeration & hot plug handling */ |
| 32 | |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 33 | static 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 Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 70 | |
| 71 | static void tb_scan_port(struct tb_port *port); |
| 72 | |
| 73 | /** |
| 74 | * tb_scan_switch() - scan for and initialize downstream switches |
| 75 | */ |
| 76 | static 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 | */ |
| 86 | static void tb_scan_port(struct tb_port *port) |
| 87 | { |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 88 | struct tb_cm *tcm = tb_priv(port->sw->tb); |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 89 | struct tb_port *upstream_port; |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 90 | struct tb_switch *sw; |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 91 | |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 92 | if (tb_is_upstream_port(port)) |
| 93 | return; |
| 94 | if (port->config.type != TB_TYPE_PORT) |
| 95 | return; |
Andreas Noever | 343fcb8 | 2014-06-12 23:11:47 +0200 | [diff] [blame] | 96 | 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 Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 101 | 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 Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 107 | sw = tb_switch_alloc(port->sw->tb, &port->sw->dev, |
| 108 | tb_downstream_route(port)); |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 109 | if (!sw) |
| 110 | return; |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 111 | |
| 112 | if (tb_switch_configure(sw)) { |
| 113 | tb_switch_put(sw); |
| 114 | return; |
| 115 | } |
| 116 | |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 117 | /* |
| 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 Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 124 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 125 | if (tb_switch_add(sw)) { |
| 126 | tb_switch_put(sw); |
| 127 | return; |
| 128 | } |
| 129 | |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 130 | /* 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 Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 139 | tb_scan_switch(sw); |
| 140 | } |
| 141 | |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 142 | /** |
| 143 | * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away |
| 144 | */ |
| 145 | static void tb_free_invalid_tunnels(struct tb *tb) |
| 146 | { |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 147 | struct tb_cm *tcm = tb_priv(tb); |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 148 | struct tb_tunnel *tunnel; |
| 149 | struct tb_tunnel *n; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 150 | |
| 151 | list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 152 | if (tb_tunnel_is_invalid(tunnel)) { |
| 153 | tb_tunnel_deactivate(tunnel); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 154 | list_del(&tunnel->list); |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 155 | tb_tunnel_free(tunnel); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 161 | * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches |
| 162 | */ |
| 163 | static 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 Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 168 | |
| 169 | if (!tb_port_has_remote(port)) |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 170 | continue; |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 171 | |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 172 | if (port->remote->sw->is_unplugged) { |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 173 | tb_switch_remove(port->remote->sw); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 174 | port->remote = NULL; |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 175 | if (port->dual_link_port) |
| 176 | port->dual_link_port->remote = NULL; |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 177 | } else { |
| 178 | tb_free_unplugged_children(port->remote->sw); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 183 | /** |
Mika Westerberg | e78db6f | 2017-10-12 16:45:50 +0300 | [diff] [blame^] | 184 | * tb_find_port() - return the first port of @type on @sw or NULL |
| 185 | * @sw: Switch to find the port from |
| 186 | * @type: Port type to look for |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 187 | */ |
Mika Westerberg | e78db6f | 2017-10-12 16:45:50 +0300 | [diff] [blame^] | 188 | static struct tb_port *tb_find_port(struct tb_switch *sw, |
| 189 | enum tb_port_type type) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 190 | { |
| 191 | int i; |
| 192 | for (i = 1; i <= sw->config.max_port_number; i++) |
Mika Westerberg | e78db6f | 2017-10-12 16:45:50 +0300 | [diff] [blame^] | 193 | if (sw->ports[i].config.type == type) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 194 | return &sw->ports[i]; |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | /** |
Mika Westerberg | e78db6f | 2017-10-12 16:45:50 +0300 | [diff] [blame^] | 199 | * tb_find_unused_port() - return the first inactive port on @sw |
| 200 | * @sw: Switch to find the port on |
| 201 | * @type: Port type to look for |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 202 | */ |
Mika Westerberg | e78db6f | 2017-10-12 16:45:50 +0300 | [diff] [blame^] | 203 | static struct tb_port *tb_find_unused_port(struct tb_switch *sw, |
| 204 | enum tb_port_type type) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 205 | { |
| 206 | int i; |
Mika Westerberg | e78db6f | 2017-10-12 16:45:50 +0300 | [diff] [blame^] | 207 | |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 208 | for (i = 1; i <= sw->config.max_port_number; i++) { |
| 209 | if (tb_is_upstream_port(&sw->ports[i])) |
| 210 | continue; |
Mika Westerberg | e78db6f | 2017-10-12 16:45:50 +0300 | [diff] [blame^] | 211 | if (sw->ports[i].config.type != type) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 212 | continue; |
Mika Westerberg | e78db6f | 2017-10-12 16:45:50 +0300 | [diff] [blame^] | 213 | if (!sw->ports[i].cap_adap) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 214 | continue; |
Mika Westerberg | e78db6f | 2017-10-12 16:45:50 +0300 | [diff] [blame^] | 215 | if (tb_port_is_enabled(&sw->ports[i])) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 216 | continue; |
| 217 | return &sw->ports[i]; |
| 218 | } |
| 219 | return NULL; |
| 220 | } |
| 221 | |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 222 | static struct tb_port *tb_find_pcie_down(struct tb_switch *sw, |
| 223 | const struct tb_port *port) |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 224 | { |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 225 | /* |
| 226 | * To keep plugging devices consistently in the same PCIe |
| 227 | * hierarchy, do mapping here for root switch downstream PCIe |
| 228 | * ports. |
| 229 | */ |
| 230 | if (!tb_route(sw)) { |
| 231 | int phy_port = tb_phy_port_from_link(port->port); |
| 232 | int index; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 233 | |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 234 | /* |
| 235 | * Hard-coded Thunderbolt port to PCIe down port mapping |
| 236 | * per controller. |
| 237 | */ |
| 238 | if (tb_switch_is_cr(sw)) |
| 239 | index = !phy_port ? 6 : 7; |
| 240 | else if (tb_switch_is_fr(sw)) |
| 241 | index = !phy_port ? 6 : 8; |
| 242 | else |
| 243 | goto out; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 244 | |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 245 | /* Validate the hard-coding */ |
| 246 | if (WARN_ON(index > sw->config.max_port_number)) |
| 247 | goto out; |
| 248 | if (WARN_ON(!tb_port_is_pcie_down(&sw->ports[index]))) |
| 249 | goto out; |
| 250 | if (WARN_ON(tb_pci_port_is_enabled(&sw->ports[index]))) |
| 251 | goto out; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 252 | |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 253 | return &sw->ports[index]; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 254 | } |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 255 | |
| 256 | out: |
Mika Westerberg | e78db6f | 2017-10-12 16:45:50 +0300 | [diff] [blame^] | 257 | return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN); |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw) |
| 261 | { |
| 262 | struct tb_port *up, *down, *port; |
| 263 | struct tb_cm *tcm = tb_priv(tb); |
| 264 | struct tb_switch *parent_sw; |
| 265 | struct tb_tunnel *tunnel; |
| 266 | |
Mika Westerberg | e78db6f | 2017-10-12 16:45:50 +0300 | [diff] [blame^] | 267 | up = tb_find_port(sw, TB_TYPE_PCIE_UP); |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 268 | if (!up) |
| 269 | return 0; |
| 270 | |
| 271 | /* |
| 272 | * Look up available down port. Since we are chaining it should |
| 273 | * be found right above this switch. |
| 274 | */ |
| 275 | parent_sw = tb_to_switch(sw->dev.parent); |
| 276 | port = tb_port_at(tb_route(sw), parent_sw); |
| 277 | down = tb_find_pcie_down(parent_sw, port); |
| 278 | if (!down) |
| 279 | return 0; |
| 280 | |
| 281 | tunnel = tb_tunnel_alloc_pci(tb, up, down); |
| 282 | if (!tunnel) |
| 283 | return -ENOMEM; |
| 284 | |
| 285 | if (tb_tunnel_activate(tunnel)) { |
| 286 | tb_port_info(up, |
| 287 | "PCIe tunnel activation failed, aborting\n"); |
| 288 | tb_tunnel_free(tunnel); |
| 289 | return -EIO; |
| 290 | } |
| 291 | |
| 292 | list_add_tail(&tunnel->list, &tcm->tunnel_list); |
| 293 | return 0; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 294 | } |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 295 | |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 296 | /* hotplug handling */ |
| 297 | |
| 298 | struct tb_hotplug_event { |
| 299 | struct work_struct work; |
| 300 | struct tb *tb; |
| 301 | u64 route; |
| 302 | u8 port; |
| 303 | bool unplug; |
| 304 | }; |
| 305 | |
| 306 | /** |
| 307 | * tb_handle_hotplug() - handle hotplug event |
| 308 | * |
| 309 | * Executes on tb->wq. |
| 310 | */ |
| 311 | static void tb_handle_hotplug(struct work_struct *work) |
| 312 | { |
| 313 | struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work); |
| 314 | struct tb *tb = ev->tb; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 315 | struct tb_cm *tcm = tb_priv(tb); |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 316 | struct tb_switch *sw; |
| 317 | struct tb_port *port; |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 318 | mutex_lock(&tb->lock); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 319 | if (!tcm->hotplug_active) |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 320 | goto out; /* during init, suspend or shutdown */ |
| 321 | |
Mika Westerberg | 8f965ef | 2019-03-15 14:56:21 +0200 | [diff] [blame] | 322 | sw = tb_switch_find_by_route(tb, ev->route); |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 323 | if (!sw) { |
| 324 | tb_warn(tb, |
| 325 | "hotplug event from non existent switch %llx:%x (unplug: %d)\n", |
| 326 | ev->route, ev->port, ev->unplug); |
| 327 | goto out; |
| 328 | } |
| 329 | if (ev->port > sw->config.max_port_number) { |
| 330 | tb_warn(tb, |
| 331 | "hotplug event from non existent port %llx:%x (unplug: %d)\n", |
| 332 | ev->route, ev->port, ev->unplug); |
Mika Westerberg | 8f965ef | 2019-03-15 14:56:21 +0200 | [diff] [blame] | 333 | goto put_sw; |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 334 | } |
| 335 | port = &sw->ports[ev->port]; |
| 336 | if (tb_is_upstream_port(port)) { |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 337 | tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n", |
| 338 | ev->route, ev->port, ev->unplug); |
Mika Westerberg | 8f965ef | 2019-03-15 14:56:21 +0200 | [diff] [blame] | 339 | goto put_sw; |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 340 | } |
| 341 | if (ev->unplug) { |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 342 | if (tb_port_has_remote(port)) { |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 343 | tb_port_info(port, "unplugged\n"); |
Lukas Wunner | aae20bb | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 344 | tb_sw_set_unplugged(port->remote->sw); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 345 | tb_free_invalid_tunnels(tb); |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 346 | tb_switch_remove(port->remote->sw); |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 347 | port->remote = NULL; |
Mika Westerberg | dfe40ca | 2019-03-07 15:26:45 +0200 | [diff] [blame] | 348 | if (port->dual_link_port) |
| 349 | port->dual_link_port->remote = NULL; |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 350 | } else { |
| 351 | tb_port_info(port, |
| 352 | "got unplug event for disconnected port, ignoring\n"); |
| 353 | } |
| 354 | } else if (port->remote) { |
| 355 | tb_port_info(port, |
| 356 | "got plug event for connected port, ignoring\n"); |
| 357 | } else { |
Mika Westerberg | 344e064 | 2017-10-11 17:19:54 +0300 | [diff] [blame] | 358 | if (tb_port_is_null(port)) { |
| 359 | tb_port_info(port, "hotplug: scanning\n"); |
| 360 | tb_scan_port(port); |
| 361 | if (!port->remote) |
| 362 | tb_port_info(port, "hotplug: no switch found\n"); |
| 363 | } |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 364 | } |
Mika Westerberg | 8f965ef | 2019-03-15 14:56:21 +0200 | [diff] [blame] | 365 | |
| 366 | put_sw: |
| 367 | tb_switch_put(sw); |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 368 | out: |
| 369 | mutex_unlock(&tb->lock); |
| 370 | kfree(ev); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * tb_schedule_hotplug_handler() - callback function for the control channel |
| 375 | * |
| 376 | * Delegates to tb_handle_hotplug. |
| 377 | */ |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 378 | static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type, |
| 379 | const void *buf, size_t size) |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 380 | { |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 381 | const struct cfg_event_pkg *pkg = buf; |
| 382 | struct tb_hotplug_event *ev; |
| 383 | u64 route; |
| 384 | |
| 385 | if (type != TB_CFG_PKG_EVENT) { |
| 386 | tb_warn(tb, "unexpected event %#x, ignoring\n", type); |
| 387 | return; |
| 388 | } |
| 389 | |
| 390 | route = tb_cfg_get_route(&pkg->header); |
| 391 | |
| 392 | if (tb_cfg_error(tb->ctl, route, pkg->port, |
| 393 | TB_CFG_ERROR_ACK_PLUG_EVENT)) { |
| 394 | tb_warn(tb, "could not ack plug event on %llx:%x\n", route, |
| 395 | pkg->port); |
| 396 | } |
| 397 | |
| 398 | ev = kmalloc(sizeof(*ev), GFP_KERNEL); |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 399 | if (!ev) |
| 400 | return; |
| 401 | INIT_WORK(&ev->work, tb_handle_hotplug); |
| 402 | ev->tb = tb; |
| 403 | ev->route = route; |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 404 | ev->port = pkg->port; |
| 405 | ev->unplug = pkg->unplug; |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 406 | queue_work(tb->wq, &ev->work); |
| 407 | } |
| 408 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 409 | static void tb_stop(struct tb *tb) |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 410 | { |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 411 | struct tb_cm *tcm = tb_priv(tb); |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 412 | struct tb_tunnel *tunnel; |
| 413 | struct tb_tunnel *n; |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 414 | |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 415 | /* tunnels are only present after everything has been initialized */ |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 416 | list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 417 | tb_tunnel_deactivate(tunnel); |
| 418 | tb_tunnel_free(tunnel); |
Andreas Noever | 3364f0c | 2014-06-03 22:04:08 +0200 | [diff] [blame] | 419 | } |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 420 | tb_switch_remove(tb->root_switch); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 421 | tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 422 | } |
| 423 | |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 424 | static int tb_scan_finalize_switch(struct device *dev, void *data) |
| 425 | { |
| 426 | if (tb_is_switch(dev)) { |
| 427 | struct tb_switch *sw = tb_to_switch(dev); |
| 428 | |
| 429 | /* |
| 430 | * If we found that the switch was already setup by the |
| 431 | * boot firmware, mark it as authorized now before we |
| 432 | * send uevent to userspace. |
| 433 | */ |
| 434 | if (sw->boot) |
| 435 | sw->authorized = 1; |
| 436 | |
| 437 | dev_set_uevent_suppress(dev, false); |
| 438 | kobject_uevent(&dev->kobj, KOBJ_ADD); |
| 439 | device_for_each_child(dev, NULL, tb_scan_finalize_switch); |
| 440 | } |
| 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 445 | static int tb_start(struct tb *tb) |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 446 | { |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 447 | struct tb_cm *tcm = tb_priv(tb); |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 448 | int ret; |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 449 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 450 | tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 451 | if (!tb->root_switch) |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 452 | return -ENOMEM; |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 453 | |
Mika Westerberg | e6b245c | 2017-06-06 15:25:17 +0300 | [diff] [blame] | 454 | /* |
| 455 | * ICM firmware upgrade needs running firmware and in native |
| 456 | * mode that is not available so disable firmware upgrade of the |
| 457 | * root switch. |
| 458 | */ |
| 459 | tb->root_switch->no_nvm_upgrade = true; |
| 460 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 461 | ret = tb_switch_configure(tb->root_switch); |
| 462 | if (ret) { |
| 463 | tb_switch_put(tb->root_switch); |
| 464 | return ret; |
| 465 | } |
| 466 | |
| 467 | /* Announce the switch to the world */ |
| 468 | ret = tb_switch_add(tb->root_switch); |
| 469 | if (ret) { |
| 470 | tb_switch_put(tb->root_switch); |
| 471 | return ret; |
| 472 | } |
| 473 | |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 474 | /* Full scan to discover devices added before the driver was loaded. */ |
| 475 | tb_scan_switch(tb->root_switch); |
Mika Westerberg | 0414bec | 2017-02-19 23:43:26 +0200 | [diff] [blame] | 476 | /* Find out tunnels created by the boot firmware */ |
| 477 | tb_discover_tunnels(tb->root_switch); |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 478 | /* Make the discovered switches available to the userspace */ |
| 479 | device_for_each_child(&tb->root_switch->dev, NULL, |
| 480 | tb_scan_finalize_switch); |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 481 | |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 482 | /* Allow tb_handle_hotplug to progress events */ |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 483 | tcm->hotplug_active = true; |
| 484 | return 0; |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 485 | } |
| 486 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 487 | static int tb_suspend_noirq(struct tb *tb) |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 488 | { |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 489 | struct tb_cm *tcm = tb_priv(tb); |
| 490 | |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 491 | tb_dbg(tb, "suspending...\n"); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 492 | tb_switch_suspend(tb->root_switch); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 493 | tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 494 | tb_dbg(tb, "suspend finished\n"); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 495 | |
| 496 | return 0; |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 497 | } |
| 498 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 499 | static int tb_resume_noirq(struct tb *tb) |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 500 | { |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 501 | struct tb_cm *tcm = tb_priv(tb); |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 502 | struct tb_tunnel *tunnel, *n; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 503 | |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 504 | tb_dbg(tb, "resuming...\n"); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 505 | |
| 506 | /* remove any pci devices the firmware might have setup */ |
| 507 | tb_switch_reset(tb, 0); |
| 508 | |
| 509 | tb_switch_resume(tb->root_switch); |
| 510 | tb_free_invalid_tunnels(tb); |
| 511 | tb_free_unplugged_children(tb->root_switch); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 512 | list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) |
Mika Westerberg | 93f36ad | 2017-02-19 13:48:29 +0200 | [diff] [blame] | 513 | tb_tunnel_restart(tunnel); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 514 | if (!list_empty(&tcm->tunnel_list)) { |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 515 | /* |
| 516 | * the pcie links need some time to get going. |
| 517 | * 100ms works for me... |
| 518 | */ |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 519 | tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n"); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 520 | msleep(100); |
| 521 | } |
| 522 | /* Allow tb_handle_hotplug to progress events */ |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 523 | tcm->hotplug_active = true; |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 524 | tb_dbg(tb, "resume finished\n"); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | static const struct tb_cm_ops tb_cm_ops = { |
| 530 | .start = tb_start, |
| 531 | .stop = tb_stop, |
| 532 | .suspend_noirq = tb_suspend_noirq, |
| 533 | .resume_noirq = tb_resume_noirq, |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 534 | .handle_event = tb_handle_event, |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 535 | .approve_switch = tb_tunnel_pci, |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 536 | }; |
| 537 | |
| 538 | struct tb *tb_probe(struct tb_nhi *nhi) |
| 539 | { |
| 540 | struct tb_cm *tcm; |
| 541 | struct tb *tb; |
| 542 | |
Lukas Wunner | 630b3af | 2017-08-01 14:10:41 +0200 | [diff] [blame] | 543 | if (!x86_apple_machine) |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 544 | return NULL; |
| 545 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 546 | tb = tb_domain_alloc(nhi, sizeof(*tcm)); |
| 547 | if (!tb) |
| 548 | return NULL; |
| 549 | |
Mika Westerberg | 99cabbb | 2018-12-30 21:34:08 +0200 | [diff] [blame] | 550 | tb->security_level = TB_SECURITY_USER; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 551 | tb->cm_ops = &tb_cm_ops; |
| 552 | |
| 553 | tcm = tb_priv(tb); |
| 554 | INIT_LIST_HEAD(&tcm->tunnel_list); |
| 555 | |
| 556 | return tb; |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 557 | } |