blob: d674e06767a560e9ffa3afc23f9127da2cd32779 [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/*
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 Wunner630b3af2017-08-01 14:10:41 +020011#include <linux/platform_data/x86/apple.h>
Andreas Noeverd6cc51c2014-06-03 22:04:00 +020012
13#include "tb.h"
Andreas Noever7adf6092014-06-03 22:04:01 +020014#include "tb_regs.h"
Andreas Noever3364f0c2014-06-03 22:04:08 +020015#include "tunnel_pci.h"
Andreas Noeverd6cc51c2014-06-03 22:04:00 +020016
Mika Westerberg9d3cce02017-06-06 15:25:00 +030017/**
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 */
25struct tb_cm {
26 struct list_head tunnel_list;
27 bool hotplug_active;
28};
Andreas Noever9da672a2014-06-03 22:04:05 +020029
30/* enumeration & hot plug handling */
31
32
33static void tb_scan_port(struct tb_port *port);
34
35/**
36 * tb_scan_switch() - scan for and initialize downstream switches
37 */
38static 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 */
48static 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 Noever343fcb82014-06-12 23:11:47 +020055 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 Noever9da672a2014-06-03 22:04:05 +020060 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 Westerbergbfe778a2017-06-06 15:25:01 +030066 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
67 tb_downstream_route(port));
Andreas Noever9da672a2014-06-03 22:04:05 +020068 if (!sw)
69 return;
Mika Westerbergbfe778a2017-06-06 15:25:01 +030070
71 if (tb_switch_configure(sw)) {
72 tb_switch_put(sw);
73 return;
74 }
75
Mika Westerbergf67cf492017-06-06 15:25:16 +030076 sw->authorized = true;
77
Mika Westerbergbfe778a2017-06-06 15:25:01 +030078 if (tb_switch_add(sw)) {
79 tb_switch_put(sw);
80 return;
81 }
82
Andreas Noever9da672a2014-06-03 22:04:05 +020083 port->remote = tb_upstream_port(sw);
84 tb_upstream_port(sw)->remote = port;
85 tb_scan_switch(sw);
86}
87
Andreas Noever3364f0c2014-06-03 22:04:08 +020088/**
89 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
90 */
91static void tb_free_invalid_tunnels(struct tb *tb)
92{
Mika Westerberg9d3cce02017-06-06 15:25:00 +030093 struct tb_cm *tcm = tb_priv(tb);
Andreas Noever3364f0c2014-06-03 22:04:08 +020094 struct tb_pci_tunnel *tunnel;
95 struct tb_pci_tunnel *n;
Mika Westerberg9d3cce02017-06-06 15:25:00 +030096
97 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
Andreas Noever3364f0c2014-06-03 22:04:08 +020098 if (tb_pci_is_invalid(tunnel)) {
99 tb_pci_deactivate(tunnel);
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300100 list_del(&tunnel->list);
Andreas Noever3364f0c2014-06-03 22:04:08 +0200101 tb_pci_free(tunnel);
102 }
103 }
104}
105
106/**
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200107 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
108 */
109static 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 Westerbergbfe778a2017-06-06 15:25:01 +0300119 tb_switch_remove(port->remote->sw);
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200120 port->remote = NULL;
121 } else {
122 tb_free_unplugged_children(port->remote->sw);
123 }
124 }
125}
126
127
128/**
Andreas Noever3364f0c2014-06-03 22:04:08 +0200129 * find_pci_up_port() - return the first PCIe up port on @sw or NULL
130 */
131static 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 */
143static 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 Westerbergda2da042017-06-06 15:24:58 +0300154 cap = tb_port_find_cap(&sw->ports[i], TB_PORT_CAP_ADAP);
155 if (cap < 0)
Andreas Noever3364f0c2014-06-03 22:04:08 +0200156 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 */
173static 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;
181 struct tb_pci_tunnel *tunnel;
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300182 struct tb_cm *tcm = tb_priv(tb);
183
Andreas Noever3364f0c2014-06-03 22:04:08 +0200184 /* 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 Westerbergda2da042017-06-06 15:24:58 +0300200 cap = tb_port_find_cap(up_port, TB_PORT_CAP_ADAP);
201 if (cap < 0)
Andreas Noever3364f0c2014-06-03 22:04:08 +0200202 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 }
217 tunnel = tb_pci_alloc(tb, up_port, down_port);
218 if (!tunnel) {
219 tb_port_info(up_port,
220 "PCIe tunnel allocation failed, aborting\n");
221 continue;
222 }
223
224 if (tb_pci_activate(tunnel)) {
225 tb_port_info(up_port,
226 "PCIe tunnel activation failed, aborting\n");
227 tb_pci_free(tunnel);
228 }
229
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300230 list_add(&tunnel->list, &tcm->tunnel_list);
Andreas Noever3364f0c2014-06-03 22:04:08 +0200231 }
232}
Andreas Noever9da672a2014-06-03 22:04:05 +0200233
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200234/* hotplug handling */
235
236struct tb_hotplug_event {
237 struct work_struct work;
238 struct tb *tb;
239 u64 route;
240 u8 port;
241 bool unplug;
242};
243
244/**
245 * tb_handle_hotplug() - handle hotplug event
246 *
247 * Executes on tb->wq.
248 */
249static void tb_handle_hotplug(struct work_struct *work)
250{
251 struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
252 struct tb *tb = ev->tb;
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300253 struct tb_cm *tcm = tb_priv(tb);
Andreas Noever053596d2014-06-03 22:04:06 +0200254 struct tb_switch *sw;
255 struct tb_port *port;
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200256 mutex_lock(&tb->lock);
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300257 if (!tcm->hotplug_active)
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200258 goto out; /* during init, suspend or shutdown */
259
Andreas Noever053596d2014-06-03 22:04:06 +0200260 sw = get_switch_at_route(tb->root_switch, ev->route);
261 if (!sw) {
262 tb_warn(tb,
263 "hotplug event from non existent switch %llx:%x (unplug: %d)\n",
264 ev->route, ev->port, ev->unplug);
265 goto out;
266 }
267 if (ev->port > sw->config.max_port_number) {
268 tb_warn(tb,
269 "hotplug event from non existent port %llx:%x (unplug: %d)\n",
270 ev->route, ev->port, ev->unplug);
271 goto out;
272 }
273 port = &sw->ports[ev->port];
274 if (tb_is_upstream_port(port)) {
275 tb_warn(tb,
276 "hotplug event for upstream port %llx:%x (unplug: %d)\n",
277 ev->route, ev->port, ev->unplug);
278 goto out;
279 }
280 if (ev->unplug) {
281 if (port->remote) {
282 tb_port_info(port, "unplugged\n");
Lukas Wunneraae20bb2016-03-20 13:57:20 +0100283 tb_sw_set_unplugged(port->remote->sw);
Andreas Noever3364f0c2014-06-03 22:04:08 +0200284 tb_free_invalid_tunnels(tb);
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300285 tb_switch_remove(port->remote->sw);
Andreas Noever053596d2014-06-03 22:04:06 +0200286 port->remote = NULL;
287 } else {
288 tb_port_info(port,
289 "got unplug event for disconnected port, ignoring\n");
290 }
291 } else if (port->remote) {
292 tb_port_info(port,
293 "got plug event for connected port, ignoring\n");
294 } else {
295 tb_port_info(port, "hotplug: scanning\n");
296 tb_scan_port(port);
297 if (!port->remote) {
298 tb_port_info(port, "hotplug: no switch found\n");
299 } else if (port->remote->sw->config.depth > 1) {
300 tb_sw_warn(port->remote->sw,
301 "hotplug: chaining not supported\n");
Andreas Noever3364f0c2014-06-03 22:04:08 +0200302 } else {
303 tb_sw_info(port->remote->sw,
304 "hotplug: activating pcie devices\n");
305 tb_activate_pcie_devices(tb);
Andreas Noever053596d2014-06-03 22:04:06 +0200306 }
307 }
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200308out:
309 mutex_unlock(&tb->lock);
310 kfree(ev);
311}
312
313/**
314 * tb_schedule_hotplug_handler() - callback function for the control channel
315 *
316 * Delegates to tb_handle_hotplug.
317 */
Mika Westerberg81a54b52017-06-06 15:25:09 +0300318static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
319 const void *buf, size_t size)
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200320{
Mika Westerberg81a54b52017-06-06 15:25:09 +0300321 const struct cfg_event_pkg *pkg = buf;
322 struct tb_hotplug_event *ev;
323 u64 route;
324
325 if (type != TB_CFG_PKG_EVENT) {
326 tb_warn(tb, "unexpected event %#x, ignoring\n", type);
327 return;
328 }
329
330 route = tb_cfg_get_route(&pkg->header);
331
332 if (tb_cfg_error(tb->ctl, route, pkg->port,
333 TB_CFG_ERROR_ACK_PLUG_EVENT)) {
334 tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
335 pkg->port);
336 }
337
338 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200339 if (!ev)
340 return;
341 INIT_WORK(&ev->work, tb_handle_hotplug);
342 ev->tb = tb;
343 ev->route = route;
Mika Westerberg81a54b52017-06-06 15:25:09 +0300344 ev->port = pkg->port;
345 ev->unplug = pkg->unplug;
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200346 queue_work(tb->wq, &ev->work);
347}
348
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300349static void tb_stop(struct tb *tb)
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200350{
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300351 struct tb_cm *tcm = tb_priv(tb);
Andreas Noever3364f0c2014-06-03 22:04:08 +0200352 struct tb_pci_tunnel *tunnel;
353 struct tb_pci_tunnel *n;
354
Andreas Noever3364f0c2014-06-03 22:04:08 +0200355 /* tunnels are only present after everything has been initialized */
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300356 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
Andreas Noever3364f0c2014-06-03 22:04:08 +0200357 tb_pci_deactivate(tunnel);
358 tb_pci_free(tunnel);
359 }
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300360 tb_switch_remove(tb->root_switch);
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300361 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200362}
363
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300364static int tb_start(struct tb *tb)
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200365{
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300366 struct tb_cm *tcm = tb_priv(tb);
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300367 int ret;
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200368
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300369 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200370 if (!tb->root_switch)
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300371 return -ENOMEM;
Andreas Noevera25c8b22014-06-03 22:04:02 +0200372
Mika Westerberge6b245c2017-06-06 15:25:17 +0300373 /*
374 * ICM firmware upgrade needs running firmware and in native
375 * mode that is not available so disable firmware upgrade of the
376 * root switch.
377 */
378 tb->root_switch->no_nvm_upgrade = true;
379
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300380 ret = tb_switch_configure(tb->root_switch);
381 if (ret) {
382 tb_switch_put(tb->root_switch);
383 return ret;
384 }
385
386 /* Announce the switch to the world */
387 ret = tb_switch_add(tb->root_switch);
388 if (ret) {
389 tb_switch_put(tb->root_switch);
390 return ret;
391 }
392
Andreas Noever9da672a2014-06-03 22:04:05 +0200393 /* Full scan to discover devices added before the driver was loaded. */
394 tb_scan_switch(tb->root_switch);
Andreas Noever3364f0c2014-06-03 22:04:08 +0200395 tb_activate_pcie_devices(tb);
Andreas Noever9da672a2014-06-03 22:04:05 +0200396
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200397 /* Allow tb_handle_hotplug to progress events */
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300398 tcm->hotplug_active = true;
399 return 0;
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200400}
401
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300402static int tb_suspend_noirq(struct tb *tb)
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200403{
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300404 struct tb_cm *tcm = tb_priv(tb);
405
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200406 tb_info(tb, "suspending...\n");
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200407 tb_switch_suspend(tb->root_switch);
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300408 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200409 tb_info(tb, "suspend finished\n");
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300410
411 return 0;
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200412}
413
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300414static int tb_resume_noirq(struct tb *tb)
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200415{
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300416 struct tb_cm *tcm = tb_priv(tb);
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200417 struct tb_pci_tunnel *tunnel, *n;
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300418
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200419 tb_info(tb, "resuming...\n");
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200420
421 /* remove any pci devices the firmware might have setup */
422 tb_switch_reset(tb, 0);
423
424 tb_switch_resume(tb->root_switch);
425 tb_free_invalid_tunnels(tb);
426 tb_free_unplugged_children(tb->root_switch);
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300427 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200428 tb_pci_restart(tunnel);
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300429 if (!list_empty(&tcm->tunnel_list)) {
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200430 /*
431 * the pcie links need some time to get going.
432 * 100ms works for me...
433 */
434 tb_info(tb, "tunnels restarted, sleeping for 100ms\n");
435 msleep(100);
436 }
437 /* Allow tb_handle_hotplug to progress events */
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300438 tcm->hotplug_active = true;
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200439 tb_info(tb, "resume finished\n");
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300440
441 return 0;
442}
443
444static const struct tb_cm_ops tb_cm_ops = {
445 .start = tb_start,
446 .stop = tb_stop,
447 .suspend_noirq = tb_suspend_noirq,
448 .resume_noirq = tb_resume_noirq,
Mika Westerberg81a54b52017-06-06 15:25:09 +0300449 .handle_event = tb_handle_event,
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300450};
451
452struct tb *tb_probe(struct tb_nhi *nhi)
453{
454 struct tb_cm *tcm;
455 struct tb *tb;
456
Lukas Wunner630b3af2017-08-01 14:10:41 +0200457 if (!x86_apple_machine)
Mika Westerbergf67cf492017-06-06 15:25:16 +0300458 return NULL;
459
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300460 tb = tb_domain_alloc(nhi, sizeof(*tcm));
461 if (!tb)
462 return NULL;
463
Mika Westerbergf67cf492017-06-06 15:25:16 +0300464 tb->security_level = TB_SECURITY_NONE;
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300465 tb->cm_ops = &tb_cm_ops;
466
467 tcm = tb_priv(tb);
468 INIT_LIST_HEAD(&tcm->tunnel_list);
469
470 return tb;
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200471}