blob: 0d50e7e7b29b4ce2de093bea0c2a29942407962d [file] [log] [blame]
Andreas Noevera25c8b22014-06-03 22:04:02 +02001/*
2 * Thunderbolt Cactus Ridge driver - switch/port utility functions
3 *
4 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
5 */
6
7#include <linux/delay.h>
8
9#include "tb.h"
10
11/* port utility functions */
12
13static const char *tb_port_type(struct tb_regs_port_header *port)
14{
15 switch (port->type >> 16) {
16 case 0:
17 switch ((u8) port->type) {
18 case 0:
19 return "Inactive";
20 case 1:
21 return "Port";
22 case 2:
23 return "NHI";
24 default:
25 return "unknown";
26 }
27 case 0x2:
28 return "Ethernet";
29 case 0x8:
30 return "SATA";
31 case 0xe:
32 return "DP/HDMI";
33 case 0x10:
34 return "PCIe";
35 case 0x20:
36 return "USB";
37 default:
38 return "unknown";
39 }
40}
41
42static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port)
43{
44 tb_info(tb,
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),
48 port->type);
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);
53}
54
55/**
Andreas Noever9da672a2014-06-03 22:04:05 +020056 * tb_port_state() - get connectedness state of a port
57 *
58 * The port must have a TB_CAP_PHY (i.e. it should be a real port).
59 *
60 * Return: Returns an enum tb_port_state on success or an error code on failure.
61 */
62static int tb_port_state(struct tb_port *port)
63{
64 struct tb_cap_phy phy;
65 int res;
66 if (port->cap_phy == 0) {
67 tb_port_WARN(port, "does not have a PHY\n");
68 return -EINVAL;
69 }
70 res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2);
71 if (res)
72 return res;
73 return phy.state;
74}
75
76/**
77 * tb_wait_for_port() - wait for a port to become ready
78 *
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.
84 *
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.
88 */
89int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
90{
91 int retries = 10;
92 int state;
93 if (!port->cap_phy) {
94 tb_port_WARN(port, "does not have PHY\n");
95 return -EINVAL;
96 }
97 if (tb_is_upstream_port(port)) {
98 tb_port_WARN(port, "is the upstream port\n");
99 return -EINVAL;
100 }
101
102 while (retries--) {
103 state = tb_port_state(port);
104 if (state < 0)
105 return state;
106 if (state == TB_PORT_DISABLED) {
107 tb_port_info(port, "is disabled (state: 0)\n");
108 return 0;
109 }
110 if (state == TB_PORT_UNPLUGGED) {
111 if (wait_if_unplugged) {
112 /* used during resume */
113 tb_port_info(port,
114 "is unplugged (state: 7), retrying...\n");
115 msleep(100);
116 continue;
117 }
118 tb_port_info(port, "is unplugged (state: 7)\n");
119 return 0;
120 }
121 if (state == TB_PORT_UP) {
122 tb_port_info(port,
123 "is connected, link is up (state: 2)\n");
124 return 1;
125 }
126
127 /*
128 * After plug-in the state is TB_PORT_CONNECTING. Give it some
129 * time.
130 */
131 tb_port_info(port,
132 "is connected, link is not up (state: %d), retrying...\n",
133 state);
134 msleep(100);
135 }
136 tb_port_warn(port,
137 "failed to reach state TB_PORT_UP. Ignoring port...\n");
138 return 0;
139}
140
141/**
Andreas Noever520b6702014-06-03 22:04:07 +0200142 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
143 *
144 * Change the number of NFC credits allocated to @port by @credits. To remove
145 * NFC credits pass a negative amount of credits.
146 *
147 * Return: Returns 0 on success or an error code on failure.
148 */
149int tb_port_add_nfc_credits(struct tb_port *port, int credits)
150{
151 if (credits == 0)
152 return 0;
153 tb_port_info(port,
154 "adding %#x NFC credits (%#x -> %#x)",
155 credits,
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,
160 TB_CFG_PORT, 4, 1);
161}
162
163/**
164 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
165 *
166 * Return: Returns 0 on success or an error code on failure.
167 */
168int tb_port_clear_counter(struct tb_port *port, int counter)
169{
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);
173}
174
175/**
Andreas Noevera25c8b22014-06-03 22:04:02 +0200176 * tb_init_port() - initialize a port
177 *
178 * This is a helper method for tb_switch_alloc. Does not check or initialize
179 * any downstream switches.
180 *
181 * Return: Returns 0 on success or an error code on failure.
182 */
Andreas Noever343fcb82014-06-12 23:11:47 +0200183static int tb_init_port(struct tb_port *port)
Andreas Noevera25c8b22014-06-03 22:04:02 +0200184{
185 int res;
Andreas Noever9da672a2014-06-03 22:04:05 +0200186 int cap;
Andreas Noever343fcb82014-06-12 23:11:47 +0200187
Andreas Noevera25c8b22014-06-03 22:04:02 +0200188 res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8);
189 if (res)
190 return res;
191
Andreas Noever9da672a2014-06-03 22:04:05 +0200192 /* Port 0 is the switch itself and has no PHY. */
Andreas Noever343fcb82014-06-12 23:11:47 +0200193 if (port->config.type == TB_TYPE_PORT && port->port != 0) {
Andreas Noever9da672a2014-06-03 22:04:05 +0200194 cap = tb_find_cap(port, TB_CFG_PORT, TB_CAP_PHY);
195
196 if (cap > 0)
197 port->cap_phy = cap;
198 else
199 tb_port_WARN(port, "non switch port without a PHY\n");
200 }
201
Andreas Noever343fcb82014-06-12 23:11:47 +0200202 tb_dump_port(port->sw->tb, &port->config);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200203
204 /* TODO: Read dual link port, DP port and more from EEPROM. */
205 return 0;
206
207}
208
209/* switch utility functions */
210
211static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw)
212{
213 tb_info(tb,
214 " Switch: %x:%x (Revision: %d, TB Version: %d)\n",
215 sw->vendor_id, sw->device_id, sw->revision,
216 sw->thunderbolt_version);
217 tb_info(tb, " Max Port Number: %d\n", sw->max_port_number);
218 tb_info(tb, " Config:\n");
219 tb_info(tb,
220 " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
221 sw->upstream_port_number, sw->depth,
222 (((u64) sw->route_hi) << 32) | sw->route_lo,
223 sw->enabled, sw->plug_events_delay);
224 tb_info(tb,
225 " unknown1: %#x unknown4: %#x\n",
226 sw->__unknown1, sw->__unknown4);
227}
228
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200229/**
230 * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
231 *
232 * Return: Returns 0 on success or an error code on failure.
233 */
234int tb_switch_reset(struct tb *tb, u64 route)
235{
236 struct tb_cfg_result res;
237 struct tb_regs_switch_header header = {
238 header.route_hi = route >> 32,
239 header.route_lo = route,
240 header.enabled = true,
241 };
242 tb_info(tb, "resetting switch at %llx\n", route);
243 res.err = tb_cfg_write(tb->ctl, ((u32 *) &header) + 2, route,
244 0, 2, 2, 2);
245 if (res.err)
246 return res.err;
247 res = tb_cfg_reset(tb->ctl, route, TB_CFG_DEFAULT_TIMEOUT);
248 if (res.err > 0)
249 return -EIO;
250 return res.err;
251}
252
Andreas Noever053596d2014-06-03 22:04:06 +0200253struct tb_switch *get_switch_at_route(struct tb_switch *sw, u64 route)
254{
255 u8 next_port = route; /*
256 * Routes use a stride of 8 bits,
257 * eventhough a port index has 6 bits at most.
258 * */
259 if (route == 0)
260 return sw;
261 if (next_port > sw->config.max_port_number)
262 return 0;
263 if (tb_is_upstream_port(&sw->ports[next_port]))
264 return 0;
265 if (!sw->ports[next_port].remote)
266 return 0;
267 return get_switch_at_route(sw->ports[next_port].remote->sw,
268 route >> TB_ROUTE_SHIFT);
269}
270
Andreas Noevera25c8b22014-06-03 22:04:02 +0200271/**
Andreas Noeverca389f72014-06-03 22:04:04 +0200272 * tb_plug_events_active() - enable/disable plug events on a switch
273 *
274 * Also configures a sane plug_events_delay of 255ms.
275 *
276 * Return: Returns 0 on success or an error code on failure.
277 */
278static int tb_plug_events_active(struct tb_switch *sw, bool active)
279{
280 u32 data;
281 int res;
282
283 sw->config.plug_events_delay = 0xff;
284 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1);
285 if (res)
286 return res;
287
288 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
289 if (res)
290 return res;
291
292 if (active) {
293 data = data & 0xFFFFFF83;
294 switch (sw->config.device_id) {
295 case 0x1513:
296 case 0x151a:
297 case 0x1549:
298 break;
299 default:
300 data |= 4;
301 }
302 } else {
303 data = data | 0x7c;
304 }
305 return tb_sw_write(sw, &data, TB_CFG_SWITCH,
306 sw->cap_plug_events + 1, 1);
307}
308
309
310/**
Andreas Noevera25c8b22014-06-03 22:04:02 +0200311 * tb_switch_free() - free a tb_switch and all downstream switches
312 */
313void tb_switch_free(struct tb_switch *sw)
314{
315 int i;
316 /* port 0 is the switch itself and never has a remote */
317 for (i = 1; i <= sw->config.max_port_number; i++) {
318 if (tb_is_upstream_port(&sw->ports[i]))
319 continue;
320 if (sw->ports[i].remote)
321 tb_switch_free(sw->ports[i].remote->sw);
322 sw->ports[i].remote = NULL;
323 }
324
Andreas Noever053596d2014-06-03 22:04:06 +0200325 if (!sw->is_unplugged)
326 tb_plug_events_active(sw, false);
Andreas Noeverca389f72014-06-03 22:04:04 +0200327
Andreas Noevera25c8b22014-06-03 22:04:02 +0200328 kfree(sw->ports);
Andreas Noever343fcb82014-06-12 23:11:47 +0200329 kfree(sw->drom);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200330 kfree(sw);
331}
332
333/**
334 * tb_switch_alloc() - allocate and initialize a switch
335 *
336 * Return: Returns a NULL on failure.
337 */
338struct tb_switch *tb_switch_alloc(struct tb *tb, u64 route)
339{
340 int i;
Andreas Noeverca389f72014-06-03 22:04:04 +0200341 int cap;
Andreas Noevera25c8b22014-06-03 22:04:02 +0200342 struct tb_switch *sw;
343 int upstream_port = tb_cfg_get_upstream_port(tb->ctl, route);
344 if (upstream_port < 0)
345 return NULL;
346
347 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
348 if (!sw)
349 return NULL;
350
351 sw->tb = tb;
352 if (tb_cfg_read(tb->ctl, &sw->config, route, 0, 2, 0, 5))
353 goto err;
354 tb_info(tb,
355 "initializing Switch at %#llx (depth: %d, up port: %d)\n",
356 route, tb_route_length(route), upstream_port);
357 tb_info(tb, "old switch config:\n");
358 tb_dump_switch(tb, &sw->config);
359
360 /* configure switch */
361 sw->config.upstream_port_number = upstream_port;
362 sw->config.depth = tb_route_length(route);
363 sw->config.route_lo = route;
364 sw->config.route_hi = route >> 32;
365 sw->config.enabled = 1;
366 /* from here on we may use the tb_sw_* functions & macros */
367
368 if (sw->config.vendor_id != 0x8086)
369 tb_sw_warn(sw, "unknown switch vendor id %#x\n",
370 sw->config.vendor_id);
371
372 if (sw->config.device_id != 0x1547 && sw->config.device_id != 0x1549)
373 tb_sw_warn(sw, "unsupported switch device id %#x\n",
374 sw->config.device_id);
375
376 /* upload configuration */
377 if (tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3))
378 goto err;
379
380 /* initialize ports */
381 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports),
Andreas Noever343fcb82014-06-12 23:11:47 +0200382 GFP_KERNEL);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200383 if (!sw->ports)
384 goto err;
385
386 for (i = 0; i <= sw->config.max_port_number; i++) {
Andreas Noever343fcb82014-06-12 23:11:47 +0200387 /* minimum setup for tb_find_cap and tb_drom_read to work */
388 sw->ports[i].sw = sw;
389 sw->ports[i].port = i;
Andreas Noevera25c8b22014-06-03 22:04:02 +0200390 }
391
Andreas Noeverca389f72014-06-03 22:04:04 +0200392 cap = tb_find_cap(&sw->ports[0], TB_CFG_SWITCH, TB_CAP_PLUG_EVENTS);
393 if (cap < 0) {
394 tb_sw_warn(sw, "cannot find TB_CAP_PLUG_EVENTS aborting\n");
395 goto err;
396 }
397 sw->cap_plug_events = cap;
398
Andreas Noever343fcb82014-06-12 23:11:47 +0200399 /* read drom */
400 if (tb_drom_read(sw))
401 tb_sw_warn(sw, "tb_eeprom_read_rom failed, continuing\n");
402 tb_sw_info(sw, "uid: %#llx\n", sw->uid);
403
404 for (i = 0; i <= sw->config.max_port_number; i++) {
405 if (sw->ports[i].disabled) {
406 tb_port_info(&sw->ports[i], "disabled by eeprom\n");
407 continue;
408 }
409 if (tb_init_port(&sw->ports[i]))
410 goto err;
411 }
412
413 /* TODO: I2C, IECS, link controller */
Andreas Noeverc90553b2014-06-03 22:04:11 +0200414
Andreas Noeverca389f72014-06-03 22:04:04 +0200415 if (tb_plug_events_active(sw, true))
416 goto err;
417
Andreas Noevera25c8b22014-06-03 22:04:02 +0200418 return sw;
419err:
420 kfree(sw->ports);
Andreas Noever343fcb82014-06-12 23:11:47 +0200421 kfree(sw->drom);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200422 kfree(sw);
423 return NULL;
424}
425
Andreas Noever053596d2014-06-03 22:04:06 +0200426/**
427 * tb_sw_set_unpplugged() - set is_unplugged on switch and downstream switches
428 */
429void tb_sw_set_unpplugged(struct tb_switch *sw)
430{
431 int i;
432 if (sw == sw->tb->root_switch) {
433 tb_sw_WARN(sw, "cannot unplug root switch\n");
434 return;
435 }
436 if (sw->is_unplugged) {
437 tb_sw_WARN(sw, "is_unplugged already set\n");
438 return;
439 }
440 sw->is_unplugged = true;
441 for (i = 0; i <= sw->config.max_port_number; i++) {
442 if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
443 tb_sw_set_unpplugged(sw->ports[i].remote->sw);
444 }
445}
446
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200447int tb_switch_resume(struct tb_switch *sw)
448{
449 int i, err;
450 u64 uid;
451 tb_sw_info(sw, "resuming switch\n");
452
Andreas Noevercd22e732014-06-12 23:11:46 +0200453 err = tb_drom_read_uid_only(sw, &uid);
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200454 if (err) {
455 tb_sw_warn(sw, "uid read failed\n");
456 return err;
457 }
458 if (sw->uid != uid) {
459 tb_sw_info(sw,
460 "changed while suspended (uid %#llx -> %#llx)\n",
461 sw->uid, uid);
462 return -ENODEV;
463 }
464
465 /* upload configuration */
466 err = tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3);
467 if (err)
468 return err;
469
470 err = tb_plug_events_active(sw, true);
471 if (err)
472 return err;
473
474 /* check for surviving downstream switches */
475 for (i = 1; i <= sw->config.max_port_number; i++) {
476 struct tb_port *port = &sw->ports[i];
477 if (tb_is_upstream_port(port))
478 continue;
479 if (!port->remote)
480 continue;
481 if (tb_wait_for_port(port, true) <= 0
482 || tb_switch_resume(port->remote->sw)) {
483 tb_port_warn(port,
484 "lost during suspend, disconnecting\n");
485 tb_sw_set_unpplugged(port->remote->sw);
486 }
487 }
488 return 0;
489}
490
491void tb_switch_suspend(struct tb_switch *sw)
492{
493 int i, err;
494 err = tb_plug_events_active(sw, false);
495 if (err)
496 return;
497
498 for (i = 1; i <= sw->config.max_port_number; i++) {
499 if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
500 tb_switch_suspend(sw->ports[i].remote->sw);
501 }
502 /*
503 * TODO: invoke tb_cfg_prepare_to_sleep here? does not seem to have any
504 * effect?
505 */
506}