2 * Thunderbolt Cactus Ridge driver - bus logic (NHI independent)
4 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
7 #include <linux/slab.h>
8 #include <linux/errno.h>
9 #include <linux/delay.h>
10 #include <linux/dmi.h>
14 #include "tunnel_pci.h"
17 * struct tb_cm - Simple Thunderbolt connection manager
18 * @tunnel_list: List of active tunnels
19 * @hotplug_active: tb_handle_hotplug will stop progressing plug
20 * events and exit if this is not set (it needs to
21 * acquire the lock one more time). Used to drain wq
22 * after cfg has been paused.
25 struct list_head tunnel_list
;
29 /* enumeration & hot plug handling */
32 static void tb_scan_port(struct tb_port
*port
);
35 * tb_scan_switch() - scan for and initialize downstream switches
37 static void tb_scan_switch(struct tb_switch
*sw
)
40 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++)
41 tb_scan_port(&sw
->ports
[i
]);
45 * tb_scan_port() - check for and initialize switches below port
47 static void tb_scan_port(struct tb_port
*port
)
50 if (tb_is_upstream_port(port
))
52 if (port
->config
.type
!= TB_TYPE_PORT
)
54 if (port
->dual_link_port
&& port
->link_nr
)
56 * Downstream switch is reachable through two ports.
57 * Only scan on the primary port (link_nr == 0).
59 if (tb_wait_for_port(port
, false) <= 0)
62 tb_port_WARN(port
, "port already has a remote!\n");
65 sw
= tb_switch_alloc(port
->sw
->tb
, &port
->sw
->dev
,
66 tb_downstream_route(port
));
70 if (tb_switch_configure(sw
)) {
75 sw
->authorized
= true;
77 if (tb_switch_add(sw
)) {
82 port
->remote
= tb_upstream_port(sw
);
83 tb_upstream_port(sw
)->remote
= port
;
88 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
90 static void tb_free_invalid_tunnels(struct tb
*tb
)
92 struct tb_cm
*tcm
= tb_priv(tb
);
93 struct tb_pci_tunnel
*tunnel
;
94 struct tb_pci_tunnel
*n
;
96 list_for_each_entry_safe(tunnel
, n
, &tcm
->tunnel_list
, list
) {
97 if (tb_pci_is_invalid(tunnel
)) {
98 tb_pci_deactivate(tunnel
);
99 list_del(&tunnel
->list
);
106 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
108 static void tb_free_unplugged_children(struct tb_switch
*sw
)
111 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
112 struct tb_port
*port
= &sw
->ports
[i
];
113 if (tb_is_upstream_port(port
))
117 if (port
->remote
->sw
->is_unplugged
) {
118 tb_switch_remove(port
->remote
->sw
);
121 tb_free_unplugged_children(port
->remote
->sw
);
128 * find_pci_up_port() - return the first PCIe up port on @sw or NULL
130 static struct tb_port
*tb_find_pci_up_port(struct tb_switch
*sw
)
133 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++)
134 if (sw
->ports
[i
].config
.type
== TB_TYPE_PCIE_UP
)
135 return &sw
->ports
[i
];
140 * find_unused_down_port() - return the first inactive PCIe down port on @sw
142 static struct tb_port
*tb_find_unused_down_port(struct tb_switch
*sw
)
148 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
149 if (tb_is_upstream_port(&sw
->ports
[i
]))
151 if (sw
->ports
[i
].config
.type
!= TB_TYPE_PCIE_DOWN
)
153 cap
= tb_port_find_cap(&sw
->ports
[i
], TB_PORT_CAP_ADAP
);
156 res
= tb_port_read(&sw
->ports
[i
], &data
, TB_CFG_PORT
, cap
, 1);
159 if (data
& 0x80000000)
161 return &sw
->ports
[i
];
167 * tb_activate_pcie_devices() - scan for and activate PCIe devices
169 * This method is somewhat ad hoc. For now it only supports one device
170 * per port and only devices at depth 1.
172 static void tb_activate_pcie_devices(struct tb
*tb
)
177 struct tb_switch
*sw
;
178 struct tb_port
*up_port
;
179 struct tb_port
*down_port
;
180 struct tb_pci_tunnel
*tunnel
;
181 struct tb_cm
*tcm
= tb_priv(tb
);
183 /* scan for pcie devices at depth 1*/
184 for (i
= 1; i
<= tb
->root_switch
->config
.max_port_number
; i
++) {
185 if (tb_is_upstream_port(&tb
->root_switch
->ports
[i
]))
187 if (tb
->root_switch
->ports
[i
].config
.type
!= TB_TYPE_PORT
)
189 if (!tb
->root_switch
->ports
[i
].remote
)
191 sw
= tb
->root_switch
->ports
[i
].remote
->sw
;
192 up_port
= tb_find_pci_up_port(sw
);
194 tb_sw_info(sw
, "no PCIe devices found, aborting\n");
198 /* check whether port is already activated */
199 cap
= tb_port_find_cap(up_port
, TB_PORT_CAP_ADAP
);
202 if (tb_port_read(up_port
, &data
, TB_CFG_PORT
, cap
, 1))
204 if (data
& 0x80000000) {
205 tb_port_info(up_port
,
206 "PCIe port already activated, aborting\n");
210 down_port
= tb_find_unused_down_port(tb
->root_switch
);
212 tb_port_info(up_port
,
213 "All PCIe down ports are occupied, aborting\n");
216 tunnel
= tb_pci_alloc(tb
, up_port
, down_port
);
218 tb_port_info(up_port
,
219 "PCIe tunnel allocation failed, aborting\n");
223 if (tb_pci_activate(tunnel
)) {
224 tb_port_info(up_port
,
225 "PCIe tunnel activation failed, aborting\n");
229 list_add(&tunnel
->list
, &tcm
->tunnel_list
);
233 /* hotplug handling */
235 struct tb_hotplug_event
{
236 struct work_struct work
;
244 * tb_handle_hotplug() - handle hotplug event
246 * Executes on tb->wq.
248 static void tb_handle_hotplug(struct work_struct
*work
)
250 struct tb_hotplug_event
*ev
= container_of(work
, typeof(*ev
), work
);
251 struct tb
*tb
= ev
->tb
;
252 struct tb_cm
*tcm
= tb_priv(tb
);
253 struct tb_switch
*sw
;
254 struct tb_port
*port
;
255 mutex_lock(&tb
->lock
);
256 if (!tcm
->hotplug_active
)
257 goto out
; /* during init, suspend or shutdown */
259 sw
= get_switch_at_route(tb
->root_switch
, ev
->route
);
262 "hotplug event from non existent switch %llx:%x (unplug: %d)\n",
263 ev
->route
, ev
->port
, ev
->unplug
);
266 if (ev
->port
> sw
->config
.max_port_number
) {
268 "hotplug event from non existent port %llx:%x (unplug: %d)\n",
269 ev
->route
, ev
->port
, ev
->unplug
);
272 port
= &sw
->ports
[ev
->port
];
273 if (tb_is_upstream_port(port
)) {
275 "hotplug event for upstream port %llx:%x (unplug: %d)\n",
276 ev
->route
, ev
->port
, ev
->unplug
);
281 tb_port_info(port
, "unplugged\n");
282 tb_sw_set_unplugged(port
->remote
->sw
);
283 tb_free_invalid_tunnels(tb
);
284 tb_switch_remove(port
->remote
->sw
);
288 "got unplug event for disconnected port, ignoring\n");
290 } else if (port
->remote
) {
292 "got plug event for connected port, ignoring\n");
294 tb_port_info(port
, "hotplug: scanning\n");
297 tb_port_info(port
, "hotplug: no switch found\n");
298 } else if (port
->remote
->sw
->config
.depth
> 1) {
299 tb_sw_warn(port
->remote
->sw
,
300 "hotplug: chaining not supported\n");
302 tb_sw_info(port
->remote
->sw
,
303 "hotplug: activating pcie devices\n");
304 tb_activate_pcie_devices(tb
);
308 mutex_unlock(&tb
->lock
);
313 * tb_schedule_hotplug_handler() - callback function for the control channel
315 * Delegates to tb_handle_hotplug.
317 static void tb_handle_event(struct tb
*tb
, enum tb_cfg_pkg_type type
,
318 const void *buf
, size_t size
)
320 const struct cfg_event_pkg
*pkg
= buf
;
321 struct tb_hotplug_event
*ev
;
324 if (type
!= TB_CFG_PKG_EVENT
) {
325 tb_warn(tb
, "unexpected event %#x, ignoring\n", type
);
329 route
= tb_cfg_get_route(&pkg
->header
);
331 if (tb_cfg_error(tb
->ctl
, route
, pkg
->port
,
332 TB_CFG_ERROR_ACK_PLUG_EVENT
)) {
333 tb_warn(tb
, "could not ack plug event on %llx:%x\n", route
,
337 ev
= kmalloc(sizeof(*ev
), GFP_KERNEL
);
340 INIT_WORK(&ev
->work
, tb_handle_hotplug
);
343 ev
->port
= pkg
->port
;
344 ev
->unplug
= pkg
->unplug
;
345 queue_work(tb
->wq
, &ev
->work
);
348 static void tb_stop(struct tb
*tb
)
350 struct tb_cm
*tcm
= tb_priv(tb
);
351 struct tb_pci_tunnel
*tunnel
;
352 struct tb_pci_tunnel
*n
;
354 /* tunnels are only present after everything has been initialized */
355 list_for_each_entry_safe(tunnel
, n
, &tcm
->tunnel_list
, list
) {
356 tb_pci_deactivate(tunnel
);
359 tb_switch_remove(tb
->root_switch
);
360 tcm
->hotplug_active
= false; /* signal tb_handle_hotplug to quit */
363 static int tb_start(struct tb
*tb
)
365 struct tb_cm
*tcm
= tb_priv(tb
);
368 tb
->root_switch
= tb_switch_alloc(tb
, &tb
->dev
, 0);
369 if (!tb
->root_switch
)
373 * ICM firmware upgrade needs running firmware and in native
374 * mode that is not available so disable firmware upgrade of the
377 tb
->root_switch
->no_nvm_upgrade
= true;
379 ret
= tb_switch_configure(tb
->root_switch
);
381 tb_switch_put(tb
->root_switch
);
385 /* Announce the switch to the world */
386 ret
= tb_switch_add(tb
->root_switch
);
388 tb_switch_put(tb
->root_switch
);
392 /* Full scan to discover devices added before the driver was loaded. */
393 tb_scan_switch(tb
->root_switch
);
394 tb_activate_pcie_devices(tb
);
396 /* Allow tb_handle_hotplug to progress events */
397 tcm
->hotplug_active
= true;
401 static int tb_suspend_noirq(struct tb
*tb
)
403 struct tb_cm
*tcm
= tb_priv(tb
);
405 tb_info(tb
, "suspending...\n");
406 tb_switch_suspend(tb
->root_switch
);
407 tcm
->hotplug_active
= false; /* signal tb_handle_hotplug to quit */
408 tb_info(tb
, "suspend finished\n");
413 static int tb_resume_noirq(struct tb
*tb
)
415 struct tb_cm
*tcm
= tb_priv(tb
);
416 struct tb_pci_tunnel
*tunnel
, *n
;
418 tb_info(tb
, "resuming...\n");
420 /* remove any pci devices the firmware might have setup */
421 tb_switch_reset(tb
, 0);
423 tb_switch_resume(tb
->root_switch
);
424 tb_free_invalid_tunnels(tb
);
425 tb_free_unplugged_children(tb
->root_switch
);
426 list_for_each_entry_safe(tunnel
, n
, &tcm
->tunnel_list
, list
)
427 tb_pci_restart(tunnel
);
428 if (!list_empty(&tcm
->tunnel_list
)) {
430 * the pcie links need some time to get going.
431 * 100ms works for me...
433 tb_info(tb
, "tunnels restarted, sleeping for 100ms\n");
436 /* Allow tb_handle_hotplug to progress events */
437 tcm
->hotplug_active
= true;
438 tb_info(tb
, "resume finished\n");
443 static const struct tb_cm_ops tb_cm_ops
= {
446 .suspend_noirq
= tb_suspend_noirq
,
447 .resume_noirq
= tb_resume_noirq
,
448 .handle_event
= tb_handle_event
,
451 struct tb
*tb_probe(struct tb_nhi
*nhi
)
456 if (!dmi_match(DMI_BOARD_VENDOR
, "Apple Inc."))
459 tb
= tb_domain_alloc(nhi
, sizeof(*tcm
));
463 tb
->security_level
= TB_SECURITY_NONE
;
464 tb
->cm_ops
= &tb_cm_ops
;
467 INIT_LIST_HEAD(&tcm
->tunnel_list
);