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>
15 /* enumeration & hot plug handling */
18 static void tb_scan_port(struct tb_port *port);
21 * tb_scan_switch() - scan for and initialize downstream switches
23 static void tb_scan_switch(struct tb_switch *sw)
26 for (i = 1; i <= sw->config.max_port_number; i++)
27 tb_scan_port(&sw->ports[i]);
31 * tb_scan_port() - check for and initialize switches below port
33 static void tb_scan_port(struct tb_port *port)
36 if (tb_is_upstream_port(port))
38 if (port->config.type != TB_TYPE_PORT)
40 if (tb_wait_for_port(port, false) <= 0)
43 tb_port_WARN(port, "port already has a remote!\n");
46 sw = tb_switch_alloc(port->sw->tb, tb_downstream_route(port));
49 port->remote = tb_upstream_port(sw);
50 tb_upstream_port(sw)->remote = port;
55 /* hotplug handling */
57 struct tb_hotplug_event {
58 struct work_struct work;
66 * tb_handle_hotplug() - handle hotplug event
70 static void tb_handle_hotplug(struct work_struct *work)
72 struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
73 struct tb *tb = ev->tb;
76 mutex_lock(&tb->lock);
77 if (!tb->hotplug_active)
78 goto out; /* during init, suspend or shutdown */
80 sw = get_switch_at_route(tb->root_switch, ev->route);
83 "hotplug event from non existent switch %llx:%x (unplug: %d)\n",
84 ev->route, ev->port, ev->unplug);
87 if (ev->port > sw->config.max_port_number) {
89 "hotplug event from non existent port %llx:%x (unplug: %d)\n",
90 ev->route, ev->port, ev->unplug);
93 port = &sw->ports[ev->port];
94 if (tb_is_upstream_port(port)) {
96 "hotplug event for upstream port %llx:%x (unplug: %d)\n",
97 ev->route, ev->port, ev->unplug);
102 tb_port_info(port, "unplugged\n");
103 tb_sw_set_unpplugged(port->remote->sw);
104 tb_switch_free(port->remote->sw);
108 "got unplug event for disconnected port, ignoring\n");
110 } else if (port->remote) {
112 "got plug event for connected port, ignoring\n");
114 tb_port_info(port, "hotplug: scanning\n");
117 tb_port_info(port, "hotplug: no switch found\n");
118 } else if (port->remote->sw->config.depth > 1) {
119 tb_sw_warn(port->remote->sw,
120 "hotplug: chaining not supported\n");
124 mutex_unlock(&tb->lock);
129 * tb_schedule_hotplug_handler() - callback function for the control channel
131 * Delegates to tb_handle_hotplug.
133 static void tb_schedule_hotplug_handler(void *data, u64 route, u8 port,
136 struct tb *tb = data;
137 struct tb_hotplug_event *ev = kmalloc(sizeof(*ev), GFP_KERNEL);
140 INIT_WORK(&ev->work, tb_handle_hotplug);
145 queue_work(tb->wq, &ev->work);
149 * thunderbolt_shutdown_and_free() - shutdown everything
151 * Free all switches and the config channel.
153 * Used in the error path of thunderbolt_alloc_and_start.
155 void thunderbolt_shutdown_and_free(struct tb *tb)
157 mutex_lock(&tb->lock);
160 tb_switch_free(tb->root_switch);
161 tb->root_switch = NULL;
164 tb_ctl_stop(tb->ctl);
165 tb_ctl_free(tb->ctl);
168 tb->hotplug_active = false; /* signal tb_handle_hotplug to quit */
170 /* allow tb_handle_hotplug to acquire the lock */
171 mutex_unlock(&tb->lock);
173 flush_workqueue(tb->wq);
174 destroy_workqueue(tb->wq);
177 mutex_destroy(&tb->lock);
182 * thunderbolt_alloc_and_start() - setup the thunderbolt bus
184 * Allocates a tb_cfg control channel, initializes the root switch, enables
185 * plug events and activates pci devices.
187 * Return: Returns NULL on error.
189 struct tb *thunderbolt_alloc_and_start(struct tb_nhi *nhi)
193 BUILD_BUG_ON(sizeof(struct tb_regs_switch_header) != 5 * 4);
194 BUILD_BUG_ON(sizeof(struct tb_regs_port_header) != 8 * 4);
195 BUILD_BUG_ON(sizeof(struct tb_regs_hop) != 2 * 4);
197 tb = kzalloc(sizeof(*tb), GFP_KERNEL);
202 mutex_init(&tb->lock);
203 mutex_lock(&tb->lock);
205 tb->wq = alloc_ordered_workqueue("thunderbolt", 0);
209 tb->ctl = tb_ctl_alloc(tb->nhi, tb_schedule_hotplug_handler, tb);
213 * tb_schedule_hotplug_handler may be called as soon as the config
214 * channel is started. Thats why we have to hold the lock here.
216 tb_ctl_start(tb->ctl);
218 tb->root_switch = tb_switch_alloc(tb, 0);
219 if (!tb->root_switch)
222 /* Full scan to discover devices added before the driver was loaded. */
223 tb_scan_switch(tb->root_switch);
225 /* Allow tb_handle_hotplug to progress events */
226 tb->hotplug_active = true;
227 mutex_unlock(&tb->lock);
231 mutex_unlock(&tb->lock);
232 thunderbolt_shutdown_and_free(tb);