1/*
2 * Thunderbolt Cactus Ridge driver - bus logic (NHI independent)
3 *
4 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
5 */
6
7#include <linux/slab.h>
8#include <linux/errno.h>
9#include <linux/delay.h>
10
11#include "tb.h"
12#include "tb_regs.h"
13#include "tunnel_pci.h"
14
15
16/* enumeration & hot plug handling */
17
18
19static void tb_scan_port(struct tb_port *port);
20
21/**
22 * tb_scan_switch() - scan for and initialize downstream switches
23 */
24static void tb_scan_switch(struct tb_switch *sw)
25{
26	int i;
27	for (i = 1; i <= sw->config.max_port_number; i++)
28		tb_scan_port(&sw->ports[i]);
29}
30
31/**
32 * tb_scan_port() - check for and initialize switches below port
33 */
34static void tb_scan_port(struct tb_port *port)
35{
36	struct tb_switch *sw;
37	if (tb_is_upstream_port(port))
38		return;
39	if (port->config.type != TB_TYPE_PORT)
40		return;
41	if (port->dual_link_port && port->link_nr)
42		return; /*
43			 * Downstream switch is reachable through two ports.
44			 * Only scan on the primary port (link_nr == 0).
45			 */
46	if (tb_wait_for_port(port, false) <= 0)
47		return;
48	if (port->remote) {
49		tb_port_WARN(port, "port already has a remote!\n");
50		return;
51	}
52	sw = tb_switch_alloc(port->sw->tb, tb_downstream_route(port));
53	if (!sw)
54		return;
55	port->remote = tb_upstream_port(sw);
56	tb_upstream_port(sw)->remote = port;
57	tb_scan_switch(sw);
58}
59
60/**
61 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
62 */
63static void tb_free_invalid_tunnels(struct tb *tb)
64{
65	struct tb_pci_tunnel *tunnel;
66	struct tb_pci_tunnel *n;
67	list_for_each_entry_safe(tunnel, n, &tb->tunnel_list, list)
68	{
69		if (tb_pci_is_invalid(tunnel)) {
70			tb_pci_deactivate(tunnel);
71			tb_pci_free(tunnel);
72		}
73	}
74}
75
76/**
77 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
78 */
79static void tb_free_unplugged_children(struct tb_switch *sw)
80{
81	int i;
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))
85			continue;
86		if (!port->remote)
87			continue;
88		if (port->remote->sw->is_unplugged) {
89			tb_switch_free(port->remote->sw);
90			port->remote = NULL;
91		} else {
92			tb_free_unplugged_children(port->remote->sw);
93		}
94	}
95}
96
97
98/**
99 * find_pci_up_port() - return the first PCIe up port on @sw or NULL
100 */
101static struct tb_port *tb_find_pci_up_port(struct tb_switch *sw)
102{
103	int i;
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];
107	return NULL;
108}
109
110/**
111 * find_unused_down_port() - return the first inactive PCIe down port on @sw
112 */
113static struct tb_port *tb_find_unused_down_port(struct tb_switch *sw)
114{
115	int i;
116	int cap;
117	int res;
118	int data;
119	for (i = 1; i <= sw->config.max_port_number; i++) {
120		if (tb_is_upstream_port(&sw->ports[i]))
121			continue;
122		if (sw->ports[i].config.type != TB_TYPE_PCIE_DOWN)
123			continue;
124		cap = tb_find_cap(&sw->ports[i], TB_CFG_PORT, TB_CAP_PCIE);
125		if (cap <= 0)
126			continue;
127		res = tb_port_read(&sw->ports[i], &data, TB_CFG_PORT, cap, 1);
128		if (res < 0)
129			continue;
130		if (data & 0x80000000)
131			continue;
132		return &sw->ports[i];
133	}
134	return NULL;
135}
136
137/**
138 * tb_activate_pcie_devices() - scan for and activate PCIe devices
139 *
140 * This method is somewhat ad hoc. For now it only supports one device
141 * per port and only devices at depth 1.
142 */
143static void tb_activate_pcie_devices(struct tb *tb)
144{
145	int i;
146	int cap;
147	u32 data;
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]))
155			continue;
156		if (tb->root_switch->ports[i].config.type != TB_TYPE_PORT)
157			continue;
158		if (!tb->root_switch->ports[i].remote)
159			continue;
160		sw = tb->root_switch->ports[i].remote->sw;
161		up_port = tb_find_pci_up_port(sw);
162		if (!up_port) {
163			tb_sw_info(sw, "no PCIe devices found, aborting\n");
164			continue;
165		}
166
167		/* check whether port is already activated */
168		cap = tb_find_cap(up_port, TB_CFG_PORT, TB_CAP_PCIE);
169		if (cap <= 0)
170			continue;
171		if (tb_port_read(up_port, &data, TB_CFG_PORT, cap, 1))
172			continue;
173		if (data & 0x80000000) {
174			tb_port_info(up_port,
175				     "PCIe port already activated, aborting\n");
176			continue;
177		}
178
179		down_port = tb_find_unused_down_port(tb->root_switch);
180		if (!down_port) {
181			tb_port_info(up_port,
182				     "All PCIe down ports are occupied, aborting\n");
183			continue;
184		}
185		tunnel = tb_pci_alloc(tb, up_port, down_port);
186		if (!tunnel) {
187			tb_port_info(up_port,
188				     "PCIe tunnel allocation failed, aborting\n");
189			continue;
190		}
191
192		if (tb_pci_activate(tunnel)) {
193			tb_port_info(up_port,
194				     "PCIe tunnel activation failed, aborting\n");
195			tb_pci_free(tunnel);
196		}
197
198	}
199}
200
201/* hotplug handling */
202
203struct tb_hotplug_event {
204	struct work_struct work;
205	struct tb *tb;
206	u64 route;
207	u8 port;
208	bool unplug;
209};
210
211/**
212 * tb_handle_hotplug() - handle hotplug event
213 *
214 * Executes on tb->wq.
215 */
216static void tb_handle_hotplug(struct work_struct *work)
217{
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 */
225
226	sw = get_switch_at_route(tb->root_switch, ev->route);
227	if (!sw) {
228		tb_warn(tb,
229			"hotplug event from non existent switch %llx:%x (unplug: %d)\n",
230			ev->route, ev->port, ev->unplug);
231		goto out;
232	}
233	if (ev->port > sw->config.max_port_number) {
234		tb_warn(tb,
235			"hotplug event from non existent port %llx:%x (unplug: %d)\n",
236			ev->route, ev->port, ev->unplug);
237		goto out;
238	}
239	port = &sw->ports[ev->port];
240	if (tb_is_upstream_port(port)) {
241		tb_warn(tb,
242			"hotplug event for upstream port %llx:%x (unplug: %d)\n",
243			ev->route, ev->port, ev->unplug);
244		goto out;
245	}
246	if (ev->unplug) {
247		if (port->remote) {
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);
252			port->remote = NULL;
253		} else {
254			tb_port_info(port,
255				     "got unplug event for disconnected port, ignoring\n");
256		}
257	} else if (port->remote) {
258		tb_port_info(port,
259			     "got plug event for connected port, ignoring\n");
260	} else {
261		tb_port_info(port, "hotplug: scanning\n");
262		tb_scan_port(port);
263		if (!port->remote) {
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");
268		} else {
269			tb_sw_info(port->remote->sw,
270				   "hotplug: activating pcie devices\n");
271			tb_activate_pcie_devices(tb);
272		}
273	}
274out:
275	mutex_unlock(&tb->lock);
276	kfree(ev);
277}
278
279/**
280 * tb_schedule_hotplug_handler() - callback function for the control channel
281 *
282 * Delegates to tb_handle_hotplug.
283 */
284static void tb_schedule_hotplug_handler(void *data, u64 route, u8 port,
285					bool unplug)
286{
287	struct tb *tb = data;
288	struct tb_hotplug_event *ev = kmalloc(sizeof(*ev), GFP_KERNEL);
289	if (!ev)
290		return;
291	INIT_WORK(&ev->work, tb_handle_hotplug);
292	ev->tb = tb;
293	ev->route = route;
294	ev->port = port;
295	ev->unplug = unplug;
296	queue_work(tb->wq, &ev->work);
297}
298
299/**
300 * thunderbolt_shutdown_and_free() - shutdown everything
301 *
302 * Free all switches and the config channel.
303 *
304 * Used in the error path of thunderbolt_alloc_and_start.
305 */
306void thunderbolt_shutdown_and_free(struct tb *tb)
307{
308	struct tb_pci_tunnel *tunnel;
309	struct tb_pci_tunnel *n;
310
311	mutex_lock(&tb->lock);
312
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);
316		tb_pci_free(tunnel);
317	}
318
319	if (tb->root_switch)
320		tb_switch_free(tb->root_switch);
321	tb->root_switch = NULL;
322
323	if (tb->ctl) {
324		tb_ctl_stop(tb->ctl);
325		tb_ctl_free(tb->ctl);
326	}
327	tb->ctl = NULL;
328	tb->hotplug_active = false; /* signal tb_handle_hotplug to quit */
329
330	/* allow tb_handle_hotplug to acquire the lock */
331	mutex_unlock(&tb->lock);
332	if (tb->wq) {
333		flush_workqueue(tb->wq);
334		destroy_workqueue(tb->wq);
335		tb->wq = NULL;
336	}
337	mutex_destroy(&tb->lock);
338	kfree(tb);
339}
340
341/**
342 * thunderbolt_alloc_and_start() - setup the thunderbolt bus
343 *
344 * Allocates a tb_cfg control channel, initializes the root switch, enables
345 * plug events and activates pci devices.
346 *
347 * Return: Returns NULL on error.
348 */
349struct tb *thunderbolt_alloc_and_start(struct tb_nhi *nhi)
350{
351	struct tb *tb;
352
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);
356
357	tb = kzalloc(sizeof(*tb), GFP_KERNEL);
358	if (!tb)
359		return NULL;
360
361	tb->nhi = nhi;
362	mutex_init(&tb->lock);
363	mutex_lock(&tb->lock);
364	INIT_LIST_HEAD(&tb->tunnel_list);
365
366	tb->wq = alloc_ordered_workqueue("thunderbolt", 0);
367	if (!tb->wq)
368		goto err_locked;
369
370	tb->ctl = tb_ctl_alloc(tb->nhi, tb_schedule_hotplug_handler, tb);
371	if (!tb->ctl)
372		goto err_locked;
373	/*
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.
376	 */
377	tb_ctl_start(tb->ctl);
378
379	tb->root_switch = tb_switch_alloc(tb, 0);
380	if (!tb->root_switch)
381		goto err_locked;
382
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);
386
387	/* Allow tb_handle_hotplug to progress events */
388	tb->hotplug_active = true;
389	mutex_unlock(&tb->lock);
390	return tb;
391
392err_locked:
393	mutex_unlock(&tb->lock);
394	thunderbolt_shutdown_and_free(tb);
395	return NULL;
396}
397
398void thunderbolt_suspend(struct tb *tb)
399{
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");
407}
408
409void thunderbolt_resume(struct tb *tb)
410{
411	struct tb_pci_tunnel *tunnel, *n;
412	tb_info(tb, "resuming...\n");
413	mutex_lock(&tb->lock);
414	tb_ctl_start(tb->ctl);
415
416	/* remove any pci devices the firmware might have setup */
417	tb_switch_reset(tb, 0);
418
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)) {
425		/*
426		 * the pcie links need some time to get going.
427		 * 100ms works for me...
428		 */
429		tb_info(tb, "tunnels restarted, sleeping for 100ms\n");
430		msleep(100);
431	}
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");
436}
437