1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt Cactus Ridge driver - bus logic (NHI independent)
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
8 #include <linux/slab.h>
9 #include <linux/errno.h>
10 #include <linux/delay.h>
11 #include <linux/platform_data/x86/apple.h>
15 #include "tunnel_pci.h"
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.
26 struct list_head tunnel_list
;
30 /* enumeration & hot plug handling */
33 static void tb_scan_port(struct tb_port
*port
);
36 * tb_scan_switch() - scan for and initialize downstream switches
38 static void tb_scan_switch(struct tb_switch
*sw
)
41 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++)
42 tb_scan_port(&sw
->ports
[i
]);
46 * tb_scan_port() - check for and initialize switches below port
48 static void tb_scan_port(struct tb_port
*port
)
51 if (tb_is_upstream_port(port
))
53 if (port
->config
.type
!= TB_TYPE_PORT
)
55 if (port
->dual_link_port
&& port
->link_nr
)
57 * Downstream switch is reachable through two ports.
58 * Only scan on the primary port (link_nr == 0).
60 if (tb_wait_for_port(port
, false) <= 0)
63 tb_port_WARN(port
, "port already has a remote!\n");
66 sw
= tb_switch_alloc(port
->sw
->tb
, &port
->sw
->dev
,
67 tb_downstream_route(port
));
71 if (tb_switch_configure(sw
)) {
76 sw
->authorized
= true;
78 if (tb_switch_add(sw
)) {
83 port
->remote
= tb_upstream_port(sw
);
84 tb_upstream_port(sw
)->remote
= port
;
89 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
91 static void tb_free_invalid_tunnels(struct tb
*tb
)
93 struct tb_cm
*tcm
= tb_priv(tb
);
94 struct tb_pci_tunnel
*tunnel
;
95 struct tb_pci_tunnel
*n
;
97 list_for_each_entry_safe(tunnel
, n
, &tcm
->tunnel_list
, list
) {
98 if (tb_pci_is_invalid(tunnel
)) {
99 tb_pci_deactivate(tunnel
);
100 list_del(&tunnel
->list
);
107 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
109 static void tb_free_unplugged_children(struct tb_switch
*sw
)
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
))
118 if (port
->remote
->sw
->is_unplugged
) {
119 tb_switch_remove(port
->remote
->sw
);
122 tb_free_unplugged_children(port
->remote
->sw
);
129 * find_pci_up_port() - return the first PCIe up port on @sw or NULL
131 static struct tb_port
*tb_find_pci_up_port(struct tb_switch
*sw
)
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
];
141 * find_unused_down_port() - return the first inactive PCIe down port on @sw
143 static struct tb_port
*tb_find_unused_down_port(struct tb_switch
*sw
)
149 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
150 if (tb_is_upstream_port(&sw
->ports
[i
]))
152 if (sw
->ports
[i
].config
.type
!= TB_TYPE_PCIE_DOWN
)
154 cap
= tb_port_find_cap(&sw
->ports
[i
], TB_PORT_CAP_ADAP
);
157 res
= tb_port_read(&sw
->ports
[i
], &data
, TB_CFG_PORT
, cap
, 1);
160 if (data
& 0x80000000)
162 return &sw
->ports
[i
];
168 * tb_activate_pcie_devices() - scan for and activate PCIe devices
170 * This method is somewhat ad hoc. For now it only supports one device
171 * per port and only devices at depth 1.
173 static void tb_activate_pcie_devices(struct tb
*tb
)
178 struct tb_switch
*sw
;
179 struct tb_port
*up_port
;
180 struct tb_port
*down_port
;
181 struct tb_pci_tunnel
*tunnel
;
182 struct tb_cm
*tcm
= tb_priv(tb
);
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
]))
188 if (tb
->root_switch
->ports
[i
].config
.type
!= TB_TYPE_PORT
)
190 if (!tb
->root_switch
->ports
[i
].remote
)
192 sw
= tb
->root_switch
->ports
[i
].remote
->sw
;
193 up_port
= tb_find_pci_up_port(sw
);
195 tb_sw_info(sw
, "no PCIe devices found, aborting\n");
199 /* check whether port is already activated */
200 cap
= tb_port_find_cap(up_port
, TB_PORT_CAP_ADAP
);
203 if (tb_port_read(up_port
, &data
, TB_CFG_PORT
, cap
, 1))
205 if (data
& 0x80000000) {
206 tb_port_info(up_port
,
207 "PCIe port already activated, aborting\n");
211 down_port
= tb_find_unused_down_port(tb
->root_switch
);
213 tb_port_info(up_port
,
214 "All PCIe down ports are occupied, aborting\n");
217 tunnel
= tb_pci_alloc(tb
, up_port
, down_port
);
219 tb_port_info(up_port
,
220 "PCIe tunnel allocation failed, aborting\n");
224 if (tb_pci_activate(tunnel
)) {
225 tb_port_info(up_port
,
226 "PCIe tunnel activation failed, aborting\n");
231 list_add(&tunnel
->list
, &tcm
->tunnel_list
);
235 /* hotplug handling */
237 struct tb_hotplug_event
{
238 struct work_struct work
;
246 * tb_handle_hotplug() - handle hotplug event
248 * Executes on tb->wq.
250 static void tb_handle_hotplug(struct work_struct
*work
)
252 struct tb_hotplug_event
*ev
= container_of(work
, typeof(*ev
), work
);
253 struct tb
*tb
= ev
->tb
;
254 struct tb_cm
*tcm
= tb_priv(tb
);
255 struct tb_switch
*sw
;
256 struct tb_port
*port
;
257 mutex_lock(&tb
->lock
);
258 if (!tcm
->hotplug_active
)
259 goto out
; /* during init, suspend or shutdown */
261 sw
= tb_switch_find_by_route(tb
, ev
->route
);
264 "hotplug event from non existent switch %llx:%x (unplug: %d)\n",
265 ev
->route
, ev
->port
, ev
->unplug
);
268 if (ev
->port
> sw
->config
.max_port_number
) {
270 "hotplug event from non existent port %llx:%x (unplug: %d)\n",
271 ev
->route
, ev
->port
, ev
->unplug
);
274 port
= &sw
->ports
[ev
->port
];
275 if (tb_is_upstream_port(port
)) {
277 "hotplug event for upstream port %llx:%x (unplug: %d)\n",
278 ev
->route
, ev
->port
, ev
->unplug
);
283 tb_port_info(port
, "unplugged\n");
284 tb_sw_set_unplugged(port
->remote
->sw
);
285 tb_free_invalid_tunnels(tb
);
286 tb_switch_remove(port
->remote
->sw
);
290 "got unplug event for disconnected port, ignoring\n");
292 } else if (port
->remote
) {
294 "got plug event for connected port, ignoring\n");
296 tb_port_info(port
, "hotplug: scanning\n");
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");
304 tb_sw_info(port
->remote
->sw
,
305 "hotplug: activating pcie devices\n");
306 tb_activate_pcie_devices(tb
);
313 mutex_unlock(&tb
->lock
);
318 * tb_schedule_hotplug_handler() - callback function for the control channel
320 * Delegates to tb_handle_hotplug.
322 static void tb_handle_event(struct tb
*tb
, enum tb_cfg_pkg_type type
,
323 const void *buf
, size_t size
)
325 const struct cfg_event_pkg
*pkg
= buf
;
326 struct tb_hotplug_event
*ev
;
329 if (type
!= TB_CFG_PKG_EVENT
) {
330 tb_warn(tb
, "unexpected event %#x, ignoring\n", type
);
334 route
= tb_cfg_get_route(&pkg
->header
);
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
,
342 ev
= kmalloc(sizeof(*ev
), GFP_KERNEL
);
345 INIT_WORK(&ev
->work
, tb_handle_hotplug
);
348 ev
->port
= pkg
->port
;
349 ev
->unplug
= pkg
->unplug
;
350 queue_work(tb
->wq
, &ev
->work
);
353 static void tb_stop(struct tb
*tb
)
355 struct tb_cm
*tcm
= tb_priv(tb
);
356 struct tb_pci_tunnel
*tunnel
;
357 struct tb_pci_tunnel
*n
;
359 /* tunnels are only present after everything has been initialized */
360 list_for_each_entry_safe(tunnel
, n
, &tcm
->tunnel_list
, list
) {
361 tb_pci_deactivate(tunnel
);
364 tb_switch_remove(tb
->root_switch
);
365 tcm
->hotplug_active
= false; /* signal tb_handle_hotplug to quit */
368 static int tb_start(struct tb
*tb
)
370 struct tb_cm
*tcm
= tb_priv(tb
);
373 tb
->root_switch
= tb_switch_alloc(tb
, &tb
->dev
, 0);
374 if (!tb
->root_switch
)
378 * ICM firmware upgrade needs running firmware and in native
379 * mode that is not available so disable firmware upgrade of the
382 tb
->root_switch
->no_nvm_upgrade
= true;
384 ret
= tb_switch_configure(tb
->root_switch
);
386 tb_switch_put(tb
->root_switch
);
390 /* Announce the switch to the world */
391 ret
= tb_switch_add(tb
->root_switch
);
393 tb_switch_put(tb
->root_switch
);
397 /* Full scan to discover devices added before the driver was loaded. */
398 tb_scan_switch(tb
->root_switch
);
399 tb_activate_pcie_devices(tb
);
401 /* Allow tb_handle_hotplug to progress events */
402 tcm
->hotplug_active
= true;
406 static int tb_suspend_noirq(struct tb
*tb
)
408 struct tb_cm
*tcm
= tb_priv(tb
);
410 tb_info(tb
, "suspending...\n");
411 tb_switch_suspend(tb
->root_switch
);
412 tcm
->hotplug_active
= false; /* signal tb_handle_hotplug to quit */
413 tb_info(tb
, "suspend finished\n");
418 static int tb_resume_noirq(struct tb
*tb
)
420 struct tb_cm
*tcm
= tb_priv(tb
);
421 struct tb_pci_tunnel
*tunnel
, *n
;
423 tb_info(tb
, "resuming...\n");
425 /* remove any pci devices the firmware might have setup */
426 tb_switch_reset(tb
, 0);
428 tb_switch_resume(tb
->root_switch
);
429 tb_free_invalid_tunnels(tb
);
430 tb_free_unplugged_children(tb
->root_switch
);
431 list_for_each_entry_safe(tunnel
, n
, &tcm
->tunnel_list
, list
)
432 tb_pci_restart(tunnel
);
433 if (!list_empty(&tcm
->tunnel_list
)) {
435 * the pcie links need some time to get going.
436 * 100ms works for me...
438 tb_info(tb
, "tunnels restarted, sleeping for 100ms\n");
441 /* Allow tb_handle_hotplug to progress events */
442 tcm
->hotplug_active
= true;
443 tb_info(tb
, "resume finished\n");
448 static const struct tb_cm_ops tb_cm_ops
= {
451 .suspend_noirq
= tb_suspend_noirq
,
452 .resume_noirq
= tb_resume_noirq
,
453 .handle_event
= tb_handle_event
,
456 struct tb
*tb_probe(struct tb_nhi
*nhi
)
461 if (!x86_apple_machine
)
464 tb
= tb_domain_alloc(nhi
, sizeof(*tcm
));
468 tb
->security_level
= TB_SECURITY_NONE
;
469 tb
->cm_ops
= &tb_cm_ops
;
472 INIT_LIST_HEAD(&tcm
->tunnel_list
);