2 * Thunderbolt Cactus Ridge driver - switch/port utility functions
4 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
7 #include <linux/delay.h>
11 /* port utility functions */
13 static const char *tb_port_type(struct tb_regs_port_header *port)
15 switch (port->type >> 16) {
17 switch ((u8) port->type) {
42 static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port)
45 " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n",
46 port->port_number, port->vendor_id, port->device_id,
47 port->revision, port->thunderbolt_version, tb_port_type(port),
49 tb_info(tb, " Max hop id (in/out): %d/%d\n",
50 port->max_in_hop_id, port->max_out_hop_id);
51 tb_info(tb, " Max counters: %d\n", port->max_counters);
52 tb_info(tb, " NFC Credits: %#x\n", port->nfc_credits);
56 * tb_port_state() - get connectedness state of a port
58 * The port must have a TB_CAP_PHY (i.e. it should be a real port).
60 * Return: Returns an enum tb_port_state on success or an error code on failure.
62 static int tb_port_state(struct tb_port *port)
64 struct tb_cap_phy phy;
66 if (port->cap_phy == 0) {
67 tb_port_WARN(port, "does not have a PHY\n");
70 res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2);
77 * tb_wait_for_port() - wait for a port to become ready
79 * Wait up to 1 second for a port to reach state TB_PORT_UP. If
80 * wait_if_unplugged is set then we also wait if the port is in state
81 * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
82 * switch resume). Otherwise we only wait if a device is registered but the link
83 * has not yet been established.
85 * Return: Returns an error code on failure. Returns 0 if the port is not
86 * connected or failed to reach state TB_PORT_UP within one second. Returns 1
87 * if the port is connected and in state TB_PORT_UP.
89 int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
94 tb_port_WARN(port, "does not have PHY\n");
97 if (tb_is_upstream_port(port)) {
98 tb_port_WARN(port, "is the upstream port\n");
103 state = tb_port_state(port);
106 if (state == TB_PORT_DISABLED) {
107 tb_port_info(port, "is disabled (state: 0)\n");
110 if (state == TB_PORT_UNPLUGGED) {
111 if (wait_if_unplugged) {
112 /* used during resume */
114 "is unplugged (state: 7), retrying...\n");
118 tb_port_info(port, "is unplugged (state: 7)\n");
121 if (state == TB_PORT_UP) {
123 "is connected, link is up (state: 2)\n");
128 * After plug-in the state is TB_PORT_CONNECTING. Give it some
132 "is connected, link is not up (state: %d), retrying...\n",
137 "failed to reach state TB_PORT_UP. Ignoring port...\n");
142 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
144 * Change the number of NFC credits allocated to @port by @credits. To remove
145 * NFC credits pass a negative amount of credits.
147 * Return: Returns 0 on success or an error code on failure.
149 int tb_port_add_nfc_credits(struct tb_port *port, int credits)
154 "adding %#x NFC credits (%#x -> %#x)",
156 port->config.nfc_credits,
157 port->config.nfc_credits + credits);
158 port->config.nfc_credits += credits;
159 return tb_port_write(port, &port->config.nfc_credits,
164 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
166 * Return: Returns 0 on success or an error code on failure.
168 int tb_port_clear_counter(struct tb_port *port, int counter)
170 u32 zero[3] = { 0, 0, 0 };
171 tb_port_info(port, "clearing counter %d\n", counter);
172 return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
176 * tb_init_port() - initialize a port
178 * This is a helper method for tb_switch_alloc. Does not check or initialize
179 * any downstream switches.
181 * Return: Returns 0 on success or an error code on failure.
183 static int tb_init_port(struct tb_switch *sw, u8 port_nr)
187 struct tb_port *port = &sw->ports[port_nr];
189 port->port = port_nr;
191 res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8);
195 /* Port 0 is the switch itself and has no PHY. */
196 if (port->config.type == TB_TYPE_PORT && port_nr != 0) {
197 cap = tb_find_cap(port, TB_CFG_PORT, TB_CAP_PHY);
202 tb_port_WARN(port, "non switch port without a PHY\n");
205 tb_dump_port(sw->tb, &port->config);
207 /* TODO: Read dual link port, DP port and more from EEPROM. */
212 /* switch utility functions */
214 static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw)
217 " Switch: %x:%x (Revision: %d, TB Version: %d)\n",
218 sw->vendor_id, sw->device_id, sw->revision,
219 sw->thunderbolt_version);
220 tb_info(tb, " Max Port Number: %d\n", sw->max_port_number);
221 tb_info(tb, " Config:\n");
223 " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
224 sw->upstream_port_number, sw->depth,
225 (((u64) sw->route_hi) << 32) | sw->route_lo,
226 sw->enabled, sw->plug_events_delay);
228 " unknown1: %#x unknown4: %#x\n",
229 sw->__unknown1, sw->__unknown4);
233 * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
235 * Return: Returns 0 on success or an error code on failure.
237 int tb_switch_reset(struct tb *tb, u64 route)
239 struct tb_cfg_result res;
240 struct tb_regs_switch_header header = {
241 header.route_hi = route >> 32,
242 header.route_lo = route,
243 header.enabled = true,
245 tb_info(tb, "resetting switch at %llx\n", route);
246 res.err = tb_cfg_write(tb->ctl, ((u32 *) &header) + 2, route,
250 res = tb_cfg_reset(tb->ctl, route, TB_CFG_DEFAULT_TIMEOUT);
256 struct tb_switch *get_switch_at_route(struct tb_switch *sw, u64 route)
258 u8 next_port = route; /*
259 * Routes use a stride of 8 bits,
260 * eventhough a port index has 6 bits at most.
264 if (next_port > sw->config.max_port_number)
266 if (tb_is_upstream_port(&sw->ports[next_port]))
268 if (!sw->ports[next_port].remote)
270 return get_switch_at_route(sw->ports[next_port].remote->sw,
271 route >> TB_ROUTE_SHIFT);
275 * tb_plug_events_active() - enable/disable plug events on a switch
277 * Also configures a sane plug_events_delay of 255ms.
279 * Return: Returns 0 on success or an error code on failure.
281 static int tb_plug_events_active(struct tb_switch *sw, bool active)
286 sw->config.plug_events_delay = 0xff;
287 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1);
291 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
296 data = data & 0xFFFFFF83;
297 switch (sw->config.device_id) {
308 return tb_sw_write(sw, &data, TB_CFG_SWITCH,
309 sw->cap_plug_events + 1, 1);
314 * tb_switch_free() - free a tb_switch and all downstream switches
316 void tb_switch_free(struct tb_switch *sw)
319 /* port 0 is the switch itself and never has a remote */
320 for (i = 1; i <= sw->config.max_port_number; i++) {
321 if (tb_is_upstream_port(&sw->ports[i]))
323 if (sw->ports[i].remote)
324 tb_switch_free(sw->ports[i].remote->sw);
325 sw->ports[i].remote = NULL;
328 if (!sw->is_unplugged)
329 tb_plug_events_active(sw, false);
336 * tb_switch_alloc() - allocate and initialize a switch
338 * Return: Returns a NULL on failure.
340 struct tb_switch *tb_switch_alloc(struct tb *tb, u64 route)
344 struct tb_switch *sw;
345 int upstream_port = tb_cfg_get_upstream_port(tb->ctl, route);
346 if (upstream_port < 0)
349 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
354 if (tb_cfg_read(tb->ctl, &sw->config, route, 0, 2, 0, 5))
357 "initializing Switch at %#llx (depth: %d, up port: %d)\n",
358 route, tb_route_length(route), upstream_port);
359 tb_info(tb, "old switch config:\n");
360 tb_dump_switch(tb, &sw->config);
362 /* configure switch */
363 sw->config.upstream_port_number = upstream_port;
364 sw->config.depth = tb_route_length(route);
365 sw->config.route_lo = route;
366 sw->config.route_hi = route >> 32;
367 sw->config.enabled = 1;
368 /* from here on we may use the tb_sw_* functions & macros */
370 if (sw->config.vendor_id != 0x8086)
371 tb_sw_warn(sw, "unknown switch vendor id %#x\n",
372 sw->config.vendor_id);
374 if (sw->config.device_id != 0x1547 && sw->config.device_id != 0x1549)
375 tb_sw_warn(sw, "unsupported switch device id %#x\n",
376 sw->config.device_id);
378 /* upload configuration */
379 if (tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3))
382 /* initialize ports */
383 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports),
388 for (i = 0; i <= sw->config.max_port_number; i++) {
389 if (tb_init_port(sw, i))
391 /* TODO: check if port is disabled (EEPROM) */
394 /* TODO: I2C, IECS, EEPROM, link controller */
396 cap = tb_find_cap(&sw->ports[0], TB_CFG_SWITCH, TB_CAP_PLUG_EVENTS);
398 tb_sw_warn(sw, "cannot find TB_CAP_PLUG_EVENTS aborting\n");
401 sw->cap_plug_events = cap;
403 if (tb_eeprom_read_uid(sw, &sw->uid))
404 tb_sw_warn(sw, "could not read uid from eeprom\n");
406 tb_sw_info(sw, "uid: %#llx\n", sw->uid);
408 if (tb_plug_events_active(sw, true))
419 * tb_sw_set_unpplugged() - set is_unplugged on switch and downstream switches
421 void tb_sw_set_unpplugged(struct tb_switch *sw)
424 if (sw == sw->tb->root_switch) {
425 tb_sw_WARN(sw, "cannot unplug root switch\n");
428 if (sw->is_unplugged) {
429 tb_sw_WARN(sw, "is_unplugged already set\n");
432 sw->is_unplugged = true;
433 for (i = 0; i <= sw->config.max_port_number; i++) {
434 if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
435 tb_sw_set_unpplugged(sw->ports[i].remote->sw);
439 int tb_switch_resume(struct tb_switch *sw)
443 tb_sw_info(sw, "resuming switch\n");
445 err = tb_eeprom_read_uid(sw, &uid);
447 tb_sw_warn(sw, "uid read failed\n");
450 if (sw->uid != uid) {
452 "changed while suspended (uid %#llx -> %#llx)\n",
457 /* upload configuration */
458 err = tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3);
462 err = tb_plug_events_active(sw, true);
466 /* check for surviving downstream switches */
467 for (i = 1; i <= sw->config.max_port_number; i++) {
468 struct tb_port *port = &sw->ports[i];
469 if (tb_is_upstream_port(port))
473 if (tb_wait_for_port(port, true) <= 0
474 || tb_switch_resume(port->remote->sw)) {
476 "lost during suspend, disconnecting\n");
477 tb_sw_set_unpplugged(port->remote->sw);
483 void tb_switch_suspend(struct tb_switch *sw)
486 err = tb_plug_events_active(sw, false);
490 for (i = 1; i <= sw->config.max_port_number; i++) {
491 if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
492 tb_switch_suspend(sw->ports[i].remote->sw);
495 * TODO: invoke tb_cfg_prepare_to_sleep here? does not seem to have any