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>
13 #include "tunnel_pci.h"
16 /* enumeration & hot plug handling */
19 static void tb_scan_port(struct tb_port
*port
);
22 * tb_scan_switch() - scan for and initialize downstream switches
24 static void tb_scan_switch(struct tb_switch
*sw
)
27 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++)
28 tb_scan_port(&sw
->ports
[i
]);
32 * tb_scan_port() - check for and initialize switches below port
34 static void tb_scan_port(struct tb_port
*port
)
37 if (tb_is_upstream_port(port
))
39 if (port
->config
.type
!= TB_TYPE_PORT
)
41 if (port
->dual_link_port
&& port
->link_nr
)
43 * Downstream switch is reachable through two ports.
44 * Only scan on the primary port (link_nr == 0).
46 if (tb_wait_for_port(port
, false) <= 0)
49 tb_port_WARN(port
, "port already has a remote!\n");
52 sw
= tb_switch_alloc(port
->sw
->tb
, tb_downstream_route(port
));
55 port
->remote
= tb_upstream_port(sw
);
56 tb_upstream_port(sw
)->remote
= port
;
61 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
63 static void tb_free_invalid_tunnels(struct tb
*tb
)
65 struct tb_pci_tunnel
*tunnel
;
66 struct tb_pci_tunnel
*n
;
67 list_for_each_entry_safe(tunnel
, n
, &tb
->tunnel_list
, list
)
69 if (tb_pci_is_invalid(tunnel
)) {
70 tb_pci_deactivate(tunnel
);
77 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
79 static void tb_free_unplugged_children(struct tb_switch
*sw
)
82 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
83 struct tb_port
*port
= &sw
->ports
[i
];
84 if (tb_is_upstream_port(port
))
88 if (port
->remote
->sw
->is_unplugged
) {
89 tb_switch_free(port
->remote
->sw
);
92 tb_free_unplugged_children(port
->remote
->sw
);
99 * find_pci_up_port() - return the first PCIe up port on @sw or NULL
101 static struct tb_port
*tb_find_pci_up_port(struct tb_switch
*sw
)
104 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++)
105 if (sw
->ports
[i
].config
.type
== TB_TYPE_PCIE_UP
)
106 return &sw
->ports
[i
];
111 * find_unused_down_port() - return the first inactive PCIe down port on @sw
113 static struct tb_port
*tb_find_unused_down_port(struct tb_switch
*sw
)
119 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
120 if (tb_is_upstream_port(&sw
->ports
[i
]))
122 if (sw
->ports
[i
].config
.type
!= TB_TYPE_PCIE_DOWN
)
124 cap
= tb_find_cap(&sw
->ports
[i
], TB_CFG_PORT
, TB_CAP_PCIE
);
127 res
= tb_port_read(&sw
->ports
[i
], &data
, TB_CFG_PORT
, cap
, 1);
130 if (data
& 0x80000000)
132 return &sw
->ports
[i
];
138 * tb_activate_pcie_devices() - scan for and activate PCIe devices
140 * This method is somewhat ad hoc. For now it only supports one device
141 * per port and only devices at depth 1.
143 static void tb_activate_pcie_devices(struct tb
*tb
)
148 struct tb_switch
*sw
;
149 struct tb_port
*up_port
;
150 struct tb_port
*down_port
;
151 struct tb_pci_tunnel
*tunnel
;
152 /* scan for pcie devices at depth 1*/
153 for (i
= 1; i
<= tb
->root_switch
->config
.max_port_number
; i
++) {
154 if (tb_is_upstream_port(&tb
->root_switch
->ports
[i
]))
156 if (tb
->root_switch
->ports
[i
].config
.type
!= TB_TYPE_PORT
)
158 if (!tb
->root_switch
->ports
[i
].remote
)
160 sw
= tb
->root_switch
->ports
[i
].remote
->sw
;
161 up_port
= tb_find_pci_up_port(sw
);
163 tb_sw_info(sw
, "no PCIe devices found, aborting\n");
167 /* check whether port is already activated */
168 cap
= tb_find_cap(up_port
, TB_CFG_PORT
, TB_CAP_PCIE
);
171 if (tb_port_read(up_port
, &data
, TB_CFG_PORT
, cap
, 1))
173 if (data
& 0x80000000) {
174 tb_port_info(up_port
,
175 "PCIe port already activated, aborting\n");
179 down_port
= tb_find_unused_down_port(tb
->root_switch
);
181 tb_port_info(up_port
,
182 "All PCIe down ports are occupied, aborting\n");
185 tunnel
= tb_pci_alloc(tb
, up_port
, down_port
);
187 tb_port_info(up_port
,
188 "PCIe tunnel allocation failed, aborting\n");
192 if (tb_pci_activate(tunnel
)) {
193 tb_port_info(up_port
,
194 "PCIe tunnel activation failed, aborting\n");
201 /* hotplug handling */
203 struct tb_hotplug_event
{
204 struct work_struct work
;
212 * tb_handle_hotplug() - handle hotplug event
214 * Executes on tb->wq.
216 static void tb_handle_hotplug(struct work_struct
*work
)
218 struct tb_hotplug_event
*ev
= container_of(work
, typeof(*ev
), work
);
219 struct tb
*tb
= ev
->tb
;
220 struct tb_switch
*sw
;
221 struct tb_port
*port
;
222 mutex_lock(&tb
->lock
);
223 if (!tb
->hotplug_active
)
224 goto out
; /* during init, suspend or shutdown */
226 sw
= get_switch_at_route(tb
->root_switch
, ev
->route
);
229 "hotplug event from non existent switch %llx:%x (unplug: %d)\n",
230 ev
->route
, ev
->port
, ev
->unplug
);
233 if (ev
->port
> sw
->config
.max_port_number
) {
235 "hotplug event from non existent port %llx:%x (unplug: %d)\n",
236 ev
->route
, ev
->port
, ev
->unplug
);
239 port
= &sw
->ports
[ev
->port
];
240 if (tb_is_upstream_port(port
)) {
242 "hotplug event for upstream port %llx:%x (unplug: %d)\n",
243 ev
->route
, ev
->port
, ev
->unplug
);
248 tb_port_info(port
, "unplugged\n");
249 tb_sw_set_unpplugged(port
->remote
->sw
);
250 tb_free_invalid_tunnels(tb
);
251 tb_switch_free(port
->remote
->sw
);
255 "got unplug event for disconnected port, ignoring\n");
257 } else if (port
->remote
) {
259 "got plug event for connected port, ignoring\n");
261 tb_port_info(port
, "hotplug: scanning\n");
264 tb_port_info(port
, "hotplug: no switch found\n");
265 } else if (port
->remote
->sw
->config
.depth
> 1) {
266 tb_sw_warn(port
->remote
->sw
,
267 "hotplug: chaining not supported\n");
269 tb_sw_info(port
->remote
->sw
,
270 "hotplug: activating pcie devices\n");
271 tb_activate_pcie_devices(tb
);
275 mutex_unlock(&tb
->lock
);
280 * tb_schedule_hotplug_handler() - callback function for the control channel
282 * Delegates to tb_handle_hotplug.
284 static void tb_schedule_hotplug_handler(void *data
, u64 route
, u8 port
,
287 struct tb
*tb
= data
;
288 struct tb_hotplug_event
*ev
= kmalloc(sizeof(*ev
), GFP_KERNEL
);
291 INIT_WORK(&ev
->work
, tb_handle_hotplug
);
296 queue_work(tb
->wq
, &ev
->work
);
300 * thunderbolt_shutdown_and_free() - shutdown everything
302 * Free all switches and the config channel.
304 * Used in the error path of thunderbolt_alloc_and_start.
306 void thunderbolt_shutdown_and_free(struct tb
*tb
)
308 struct tb_pci_tunnel
*tunnel
;
309 struct tb_pci_tunnel
*n
;
311 mutex_lock(&tb
->lock
);
313 /* tunnels are only present after everything has been initialized */
314 list_for_each_entry_safe(tunnel
, n
, &tb
->tunnel_list
, list
) {
315 tb_pci_deactivate(tunnel
);
320 tb_switch_free(tb
->root_switch
);
321 tb
->root_switch
= NULL
;
324 tb_ctl_stop(tb
->ctl
);
325 tb_ctl_free(tb
->ctl
);
328 tb
->hotplug_active
= false; /* signal tb_handle_hotplug to quit */
330 /* allow tb_handle_hotplug to acquire the lock */
331 mutex_unlock(&tb
->lock
);
333 flush_workqueue(tb
->wq
);
334 destroy_workqueue(tb
->wq
);
337 mutex_destroy(&tb
->lock
);
342 * thunderbolt_alloc_and_start() - setup the thunderbolt bus
344 * Allocates a tb_cfg control channel, initializes the root switch, enables
345 * plug events and activates pci devices.
347 * Return: Returns NULL on error.
349 struct tb
*thunderbolt_alloc_and_start(struct tb_nhi
*nhi
)
353 BUILD_BUG_ON(sizeof(struct tb_regs_switch_header
) != 5 * 4);
354 BUILD_BUG_ON(sizeof(struct tb_regs_port_header
) != 8 * 4);
355 BUILD_BUG_ON(sizeof(struct tb_regs_hop
) != 2 * 4);
357 tb
= kzalloc(sizeof(*tb
), GFP_KERNEL
);
362 mutex_init(&tb
->lock
);
363 mutex_lock(&tb
->lock
);
364 INIT_LIST_HEAD(&tb
->tunnel_list
);
366 tb
->wq
= alloc_ordered_workqueue("thunderbolt", 0);
370 tb
->ctl
= tb_ctl_alloc(tb
->nhi
, tb_schedule_hotplug_handler
, tb
);
374 * tb_schedule_hotplug_handler may be called as soon as the config
375 * channel is started. Thats why we have to hold the lock here.
377 tb_ctl_start(tb
->ctl
);
379 tb
->root_switch
= tb_switch_alloc(tb
, 0);
380 if (!tb
->root_switch
)
383 /* Full scan to discover devices added before the driver was loaded. */
384 tb_scan_switch(tb
->root_switch
);
385 tb_activate_pcie_devices(tb
);
387 /* Allow tb_handle_hotplug to progress events */
388 tb
->hotplug_active
= true;
389 mutex_unlock(&tb
->lock
);
393 mutex_unlock(&tb
->lock
);
394 thunderbolt_shutdown_and_free(tb
);
398 void thunderbolt_suspend(struct tb
*tb
)
400 tb_info(tb
, "suspending...\n");
401 mutex_lock(&tb
->lock
);
402 tb_switch_suspend(tb
->root_switch
);
403 tb_ctl_stop(tb
->ctl
);
404 tb
->hotplug_active
= false; /* signal tb_handle_hotplug to quit */
405 mutex_unlock(&tb
->lock
);
406 tb_info(tb
, "suspend finished\n");
409 void thunderbolt_resume(struct tb
*tb
)
411 struct tb_pci_tunnel
*tunnel
, *n
;
412 tb_info(tb
, "resuming...\n");
413 mutex_lock(&tb
->lock
);
414 tb_ctl_start(tb
->ctl
);
416 /* remove any pci devices the firmware might have setup */
417 tb_switch_reset(tb
, 0);
419 tb_switch_resume(tb
->root_switch
);
420 tb_free_invalid_tunnels(tb
);
421 tb_free_unplugged_children(tb
->root_switch
);
422 list_for_each_entry_safe(tunnel
, n
, &tb
->tunnel_list
, list
)
423 tb_pci_restart(tunnel
);
424 if (!list_empty(&tb
->tunnel_list
)) {
426 * the pcie links need some time to get going.
427 * 100ms works for me...
429 tb_info(tb
, "tunnels restarted, sleeping for 100ms\n");
432 /* Allow tb_handle_hotplug to progress events */
433 tb
->hotplug_active
= true;
434 mutex_unlock(&tb
->lock
);
435 tb_info(tb
, "resume finished\n");