blob: 6384061100b0c98ec9024d96ee5fd017a1c4cb9c [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>
Sachin Kamat10fefe52014-06-20 14:32:30 +05308#include <linux/slab.h>
Andreas Noevera25c8b22014-06-03 22:04:02 +02009
10#include "tb.h"
11
12/* port utility functions */
13
14static const char *tb_port_type(struct tb_regs_port_header *port)
15{
16 switch (port->type >> 16) {
17 case 0:
18 switch ((u8) port->type) {
19 case 0:
20 return "Inactive";
21 case 1:
22 return "Port";
23 case 2:
24 return "NHI";
25 default:
26 return "unknown";
27 }
28 case 0x2:
29 return "Ethernet";
30 case 0x8:
31 return "SATA";
32 case 0xe:
33 return "DP/HDMI";
34 case 0x10:
35 return "PCIe";
36 case 0x20:
37 return "USB";
38 default:
39 return "unknown";
40 }
41}
42
43static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port)
44{
45 tb_info(tb,
46 " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n",
47 port->port_number, port->vendor_id, port->device_id,
48 port->revision, port->thunderbolt_version, tb_port_type(port),
49 port->type);
50 tb_info(tb, " Max hop id (in/out): %d/%d\n",
51 port->max_in_hop_id, port->max_out_hop_id);
52 tb_info(tb, " Max counters: %d\n", port->max_counters);
53 tb_info(tb, " NFC Credits: %#x\n", port->nfc_credits);
54}
55
56/**
Andreas Noever9da672a2014-06-03 22:04:05 +020057 * tb_port_state() - get connectedness state of a port
58 *
59 * The port must have a TB_CAP_PHY (i.e. it should be a real port).
60 *
61 * Return: Returns an enum tb_port_state on success or an error code on failure.
62 */
63static int tb_port_state(struct tb_port *port)
64{
65 struct tb_cap_phy phy;
66 int res;
67 if (port->cap_phy == 0) {
68 tb_port_WARN(port, "does not have a PHY\n");
69 return -EINVAL;
70 }
71 res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2);
72 if (res)
73 return res;
74 return phy.state;
75}
76
77/**
78 * tb_wait_for_port() - wait for a port to become ready
79 *
80 * Wait up to 1 second for a port to reach state TB_PORT_UP. If
81 * wait_if_unplugged is set then we also wait if the port is in state
82 * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
83 * switch resume). Otherwise we only wait if a device is registered but the link
84 * has not yet been established.
85 *
86 * Return: Returns an error code on failure. Returns 0 if the port is not
87 * connected or failed to reach state TB_PORT_UP within one second. Returns 1
88 * if the port is connected and in state TB_PORT_UP.
89 */
90int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
91{
92 int retries = 10;
93 int state;
94 if (!port->cap_phy) {
95 tb_port_WARN(port, "does not have PHY\n");
96 return -EINVAL;
97 }
98 if (tb_is_upstream_port(port)) {
99 tb_port_WARN(port, "is the upstream port\n");
100 return -EINVAL;
101 }
102
103 while (retries--) {
104 state = tb_port_state(port);
105 if (state < 0)
106 return state;
107 if (state == TB_PORT_DISABLED) {
108 tb_port_info(port, "is disabled (state: 0)\n");
109 return 0;
110 }
111 if (state == TB_PORT_UNPLUGGED) {
112 if (wait_if_unplugged) {
113 /* used during resume */
114 tb_port_info(port,
115 "is unplugged (state: 7), retrying...\n");
116 msleep(100);
117 continue;
118 }
119 tb_port_info(port, "is unplugged (state: 7)\n");
120 return 0;
121 }
122 if (state == TB_PORT_UP) {
123 tb_port_info(port,
124 "is connected, link is up (state: 2)\n");
125 return 1;
126 }
127
128 /*
129 * After plug-in the state is TB_PORT_CONNECTING. Give it some
130 * time.
131 */
132 tb_port_info(port,
133 "is connected, link is not up (state: %d), retrying...\n",
134 state);
135 msleep(100);
136 }
137 tb_port_warn(port,
138 "failed to reach state TB_PORT_UP. Ignoring port...\n");
139 return 0;
140}
141
142/**
Andreas Noever520b6702014-06-03 22:04:07 +0200143 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
144 *
145 * Change the number of NFC credits allocated to @port by @credits. To remove
146 * NFC credits pass a negative amount of credits.
147 *
148 * Return: Returns 0 on success or an error code on failure.
149 */
150int tb_port_add_nfc_credits(struct tb_port *port, int credits)
151{
152 if (credits == 0)
153 return 0;
154 tb_port_info(port,
155 "adding %#x NFC credits (%#x -> %#x)",
156 credits,
157 port->config.nfc_credits,
158 port->config.nfc_credits + credits);
159 port->config.nfc_credits += credits;
160 return tb_port_write(port, &port->config.nfc_credits,
161 TB_CFG_PORT, 4, 1);
162}
163
164/**
165 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
166 *
167 * Return: Returns 0 on success or an error code on failure.
168 */
169int tb_port_clear_counter(struct tb_port *port, int counter)
170{
171 u32 zero[3] = { 0, 0, 0 };
172 tb_port_info(port, "clearing counter %d\n", counter);
173 return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
174}
175
176/**
Andreas Noevera25c8b22014-06-03 22:04:02 +0200177 * tb_init_port() - initialize a port
178 *
179 * This is a helper method for tb_switch_alloc. Does not check or initialize
180 * any downstream switches.
181 *
182 * Return: Returns 0 on success or an error code on failure.
183 */
Andreas Noever343fcb82014-06-12 23:11:47 +0200184static int tb_init_port(struct tb_port *port)
Andreas Noevera25c8b22014-06-03 22:04:02 +0200185{
186 int res;
Andreas Noever9da672a2014-06-03 22:04:05 +0200187 int cap;
Andreas Noever343fcb82014-06-12 23:11:47 +0200188
Andreas Noevera25c8b22014-06-03 22:04:02 +0200189 res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8);
190 if (res)
191 return res;
192
Andreas Noever9da672a2014-06-03 22:04:05 +0200193 /* Port 0 is the switch itself and has no PHY. */
Andreas Noever343fcb82014-06-12 23:11:47 +0200194 if (port->config.type == TB_TYPE_PORT && port->port != 0) {
Mika Westerbergda2da042017-06-06 15:24:58 +0300195 cap = tb_port_find_cap(port, TB_PORT_CAP_PHY);
Andreas Noever9da672a2014-06-03 22:04:05 +0200196
197 if (cap > 0)
198 port->cap_phy = cap;
199 else
200 tb_port_WARN(port, "non switch port without a PHY\n");
201 }
202
Andreas Noever343fcb82014-06-12 23:11:47 +0200203 tb_dump_port(port->sw->tb, &port->config);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200204
205 /* TODO: Read dual link port, DP port and more from EEPROM. */
206 return 0;
207
208}
209
210/* switch utility functions */
211
212static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw)
213{
214 tb_info(tb,
215 " Switch: %x:%x (Revision: %d, TB Version: %d)\n",
216 sw->vendor_id, sw->device_id, sw->revision,
217 sw->thunderbolt_version);
218 tb_info(tb, " Max Port Number: %d\n", sw->max_port_number);
219 tb_info(tb, " Config:\n");
220 tb_info(tb,
221 " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
222 sw->upstream_port_number, sw->depth,
223 (((u64) sw->route_hi) << 32) | sw->route_lo,
224 sw->enabled, sw->plug_events_delay);
225 tb_info(tb,
226 " unknown1: %#x unknown4: %#x\n",
227 sw->__unknown1, sw->__unknown4);
228}
229
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200230/**
231 * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
232 *
233 * Return: Returns 0 on success or an error code on failure.
234 */
235int tb_switch_reset(struct tb *tb, u64 route)
236{
237 struct tb_cfg_result res;
238 struct tb_regs_switch_header header = {
239 header.route_hi = route >> 32,
240 header.route_lo = route,
241 header.enabled = true,
242 };
243 tb_info(tb, "resetting switch at %llx\n", route);
244 res.err = tb_cfg_write(tb->ctl, ((u32 *) &header) + 2, route,
245 0, 2, 2, 2);
246 if (res.err)
247 return res.err;
248 res = tb_cfg_reset(tb->ctl, route, TB_CFG_DEFAULT_TIMEOUT);
249 if (res.err > 0)
250 return -EIO;
251 return res.err;
252}
253
Andreas Noever053596d2014-06-03 22:04:06 +0200254struct tb_switch *get_switch_at_route(struct tb_switch *sw, u64 route)
255{
256 u8 next_port = route; /*
257 * Routes use a stride of 8 bits,
258 * eventhough a port index has 6 bits at most.
259 * */
260 if (route == 0)
261 return sw;
262 if (next_port > sw->config.max_port_number)
Sachin Kamatc9c2dee2014-06-20 14:32:31 +0530263 return NULL;
Andreas Noever053596d2014-06-03 22:04:06 +0200264 if (tb_is_upstream_port(&sw->ports[next_port]))
Sachin Kamatc9c2dee2014-06-20 14:32:31 +0530265 return NULL;
Andreas Noever053596d2014-06-03 22:04:06 +0200266 if (!sw->ports[next_port].remote)
Sachin Kamatc9c2dee2014-06-20 14:32:31 +0530267 return NULL;
Andreas Noever053596d2014-06-03 22:04:06 +0200268 return get_switch_at_route(sw->ports[next_port].remote->sw,
269 route >> TB_ROUTE_SHIFT);
270}
271
Andreas Noevera25c8b22014-06-03 22:04:02 +0200272/**
Andreas Noeverca389f72014-06-03 22:04:04 +0200273 * tb_plug_events_active() - enable/disable plug events on a switch
274 *
275 * Also configures a sane plug_events_delay of 255ms.
276 *
277 * Return: Returns 0 on success or an error code on failure.
278 */
279static int tb_plug_events_active(struct tb_switch *sw, bool active)
280{
281 u32 data;
282 int res;
283
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300284 if (!sw->config.enabled)
285 return 0;
286
Andreas Noeverca389f72014-06-03 22:04:04 +0200287 sw->config.plug_events_delay = 0xff;
288 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1);
289 if (res)
290 return res;
291
292 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
293 if (res)
294 return res;
295
296 if (active) {
297 data = data & 0xFFFFFF83;
298 switch (sw->config.device_id) {
Lukas Wunner1d111402016-03-20 13:57:20 +0100299 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
300 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
301 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
Andreas Noeverca389f72014-06-03 22:04:04 +0200302 break;
303 default:
304 data |= 4;
305 }
306 } else {
307 data = data | 0x7c;
308 }
309 return tb_sw_write(sw, &data, TB_CFG_SWITCH,
310 sw->cap_plug_events + 1, 1);
311}
312
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300313static ssize_t device_show(struct device *dev, struct device_attribute *attr,
314 char *buf)
Andreas Noevera25c8b22014-06-03 22:04:02 +0200315{
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300316 struct tb_switch *sw = tb_to_switch(dev);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200317
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300318 return sprintf(buf, "%#x\n", sw->device);
319}
320static DEVICE_ATTR_RO(device);
Andreas Noeverca389f72014-06-03 22:04:04 +0200321
Mika Westerberg72ee3392017-06-06 15:25:05 +0300322static ssize_t
323device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
324{
325 struct tb_switch *sw = tb_to_switch(dev);
326
327 return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : "");
328}
329static DEVICE_ATTR_RO(device_name);
330
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300331static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
332 char *buf)
333{
334 struct tb_switch *sw = tb_to_switch(dev);
335
336 return sprintf(buf, "%#x\n", sw->vendor);
337}
338static DEVICE_ATTR_RO(vendor);
339
Mika Westerberg72ee3392017-06-06 15:25:05 +0300340static ssize_t
341vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
342{
343 struct tb_switch *sw = tb_to_switch(dev);
344
345 return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : "");
346}
347static DEVICE_ATTR_RO(vendor_name);
348
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300349static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
350 char *buf)
351{
352 struct tb_switch *sw = tb_to_switch(dev);
353
354 return sprintf(buf, "%pUb\n", sw->uuid);
355}
356static DEVICE_ATTR_RO(unique_id);
357
358static struct attribute *switch_attrs[] = {
359 &dev_attr_device.attr,
Mika Westerberg72ee3392017-06-06 15:25:05 +0300360 &dev_attr_device_name.attr,
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300361 &dev_attr_vendor.attr,
Mika Westerberg72ee3392017-06-06 15:25:05 +0300362 &dev_attr_vendor_name.attr,
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300363 &dev_attr_unique_id.attr,
364 NULL,
365};
366
367static struct attribute_group switch_group = {
368 .attrs = switch_attrs,
369};
370
371static const struct attribute_group *switch_groups[] = {
372 &switch_group,
373 NULL,
374};
375
376static void tb_switch_release(struct device *dev)
377{
378 struct tb_switch *sw = tb_to_switch(dev);
379
380 kfree(sw->uuid);
Mika Westerberg72ee3392017-06-06 15:25:05 +0300381 kfree(sw->device_name);
382 kfree(sw->vendor_name);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200383 kfree(sw->ports);
Andreas Noever343fcb82014-06-12 23:11:47 +0200384 kfree(sw->drom);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200385 kfree(sw);
386}
387
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300388struct device_type tb_switch_type = {
389 .name = "thunderbolt_device",
390 .release = tb_switch_release,
391};
392
Mika Westerberg2c3c4192017-06-06 15:25:13 +0300393static int tb_switch_get_generation(struct tb_switch *sw)
394{
395 switch (sw->config.device_id) {
396 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
397 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
398 case PCI_DEVICE_ID_INTEL_LIGHT_PEAK:
399 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
400 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
401 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
402 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE:
403 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE:
404 return 1;
405
406 case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE:
407 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
408 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
409 return 2;
410
411 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
412 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
413 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
414 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
415 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
416 return 3;
417
418 default:
419 /*
420 * For unknown switches assume generation to be 1 to be
421 * on the safe side.
422 */
423 tb_sw_warn(sw, "unsupported switch device id %#x\n",
424 sw->config.device_id);
425 return 1;
426 }
427}
428
Andreas Noevera25c8b22014-06-03 22:04:02 +0200429/**
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300430 * tb_switch_alloc() - allocate a switch
431 * @tb: Pointer to the owning domain
432 * @parent: Parent device for this switch
433 * @route: Route string for this switch
Andreas Noevera25c8b22014-06-03 22:04:02 +0200434 *
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300435 * Allocates and initializes a switch. Will not upload configuration to
436 * the switch. For that you need to call tb_switch_configure()
437 * separately. The returned switch should be released by calling
438 * tb_switch_put().
439 *
440 * Return: Pointer to the allocated switch or %NULL in case of failure
Andreas Noevera25c8b22014-06-03 22:04:02 +0200441 */
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300442struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
443 u64 route)
Andreas Noevera25c8b22014-06-03 22:04:02 +0200444{
445 int i;
Andreas Noeverca389f72014-06-03 22:04:04 +0200446 int cap;
Andreas Noevera25c8b22014-06-03 22:04:02 +0200447 struct tb_switch *sw;
448 int upstream_port = tb_cfg_get_upstream_port(tb->ctl, route);
449 if (upstream_port < 0)
450 return NULL;
451
452 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
453 if (!sw)
454 return NULL;
455
456 sw->tb = tb;
Lukas Wunneraae20bb2016-03-20 13:57:20 +0100457 if (tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5))
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300458 goto err_free_sw_ports;
459
460 tb_info(tb, "current switch config:\n");
Andreas Noevera25c8b22014-06-03 22:04:02 +0200461 tb_dump_switch(tb, &sw->config);
462
463 /* configure switch */
464 sw->config.upstream_port_number = upstream_port;
465 sw->config.depth = tb_route_length(route);
466 sw->config.route_lo = route;
467 sw->config.route_hi = route >> 32;
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300468 sw->config.enabled = 0;
Andreas Noevera25c8b22014-06-03 22:04:02 +0200469
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300470 /* initialize ports */
471 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports),
472 GFP_KERNEL);
473 if (!sw->ports)
474 goto err_free_sw_ports;
475
476 for (i = 0; i <= sw->config.max_port_number; i++) {
477 /* minimum setup for tb_find_cap and tb_drom_read to work */
478 sw->ports[i].sw = sw;
479 sw->ports[i].port = i;
480 }
481
Mika Westerberg2c3c4192017-06-06 15:25:13 +0300482 sw->generation = tb_switch_get_generation(sw);
483
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300484 cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS);
485 if (cap < 0) {
486 tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
487 goto err_free_sw_ports;
488 }
489 sw->cap_plug_events = cap;
490
491 device_initialize(&sw->dev);
492 sw->dev.parent = parent;
493 sw->dev.bus = &tb_bus_type;
494 sw->dev.type = &tb_switch_type;
495 sw->dev.groups = switch_groups;
496 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
497
498 return sw;
499
500err_free_sw_ports:
501 kfree(sw->ports);
502 kfree(sw);
503
504 return NULL;
505}
506
507/**
508 * tb_switch_configure() - Uploads configuration to the switch
509 * @sw: Switch to configure
510 *
511 * Call this function before the switch is added to the system. It will
512 * upload configuration to the switch and makes it available for the
513 * connection manager to use.
514 *
515 * Return: %0 in case of success and negative errno in case of failure
516 */
517int tb_switch_configure(struct tb_switch *sw)
518{
519 struct tb *tb = sw->tb;
520 u64 route;
521 int ret;
522
523 route = tb_route(sw);
524 tb_info(tb,
525 "initializing Switch at %#llx (depth: %d, up port: %d)\n",
526 route, tb_route_length(route), sw->config.upstream_port_number);
527
528 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL)
Andreas Noevera25c8b22014-06-03 22:04:02 +0200529 tb_sw_warn(sw, "unknown switch vendor id %#x\n",
530 sw->config.vendor_id);
531
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300532 sw->config.enabled = 1;
533
Andreas Noevera25c8b22014-06-03 22:04:02 +0200534 /* upload configuration */
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300535 ret = tb_sw_write(sw, 1 + (u32 *)&sw->config, TB_CFG_SWITCH, 1, 3);
536 if (ret)
537 return ret;
Andreas Noevera25c8b22014-06-03 22:04:02 +0200538
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300539 return tb_plug_events_active(sw, true);
540}
Andreas Noevera25c8b22014-06-03 22:04:02 +0200541
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300542static void tb_switch_set_uuid(struct tb_switch *sw)
543{
544 u32 uuid[4];
545 int cap;
546
547 if (sw->uuid)
548 return;
549
550 /*
551 * The newer controllers include fused UUID as part of link
552 * controller specific registers
553 */
554 cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER);
555 if (cap > 0) {
556 tb_sw_read(sw, uuid, TB_CFG_SWITCH, cap + 3, 4);
557 } else {
558 /*
559 * ICM generates UUID based on UID and fills the upper
560 * two words with ones. This is not strictly following
561 * UUID format but we want to be compatible with it so
562 * we do the same here.
563 */
564 uuid[0] = sw->uid & 0xffffffff;
565 uuid[1] = (sw->uid >> 32) & 0xffffffff;
566 uuid[2] = 0xffffffff;
567 uuid[3] = 0xffffffff;
Andreas Noevera25c8b22014-06-03 22:04:02 +0200568 }
569
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300570 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
571}
572
573/**
574 * tb_switch_add() - Add a switch to the domain
575 * @sw: Switch to add
576 *
577 * This is the last step in adding switch to the domain. It will read
578 * identification information from DROM and initializes ports so that
579 * they can be used to connect other switches. The switch will be
580 * exposed to the userspace when this function successfully returns. To
581 * remove and release the switch, call tb_switch_remove().
582 *
583 * Return: %0 in case of success and negative errno in case of failure
584 */
585int tb_switch_add(struct tb_switch *sw)
586{
587 int i, ret;
Andreas Noeverca389f72014-06-03 22:04:04 +0200588
Andreas Noever343fcb82014-06-12 23:11:47 +0200589 /* read drom */
Mika Westerbergf53e7672017-06-06 15:25:02 +0300590 ret = tb_drom_read(sw);
591 if (ret) {
592 tb_sw_warn(sw, "tb_eeprom_read_rom failed\n");
593 return ret;
594 }
Andreas Noever343fcb82014-06-12 23:11:47 +0200595 tb_sw_info(sw, "uid: %#llx\n", sw->uid);
596
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300597 tb_switch_set_uuid(sw);
598
Andreas Noever343fcb82014-06-12 23:11:47 +0200599 for (i = 0; i <= sw->config.max_port_number; i++) {
600 if (sw->ports[i].disabled) {
601 tb_port_info(&sw->ports[i], "disabled by eeprom\n");
602 continue;
603 }
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300604 ret = tb_init_port(&sw->ports[i]);
605 if (ret)
606 return ret;
Andreas Noever343fcb82014-06-12 23:11:47 +0200607 }
608
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300609 return device_add(&sw->dev);
610}
Andreas Noeverc90553b2014-06-03 22:04:11 +0200611
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300612/**
613 * tb_switch_remove() - Remove and release a switch
614 * @sw: Switch to remove
615 *
616 * This will remove the switch from the domain and release it after last
617 * reference count drops to zero. If there are switches connected below
618 * this switch, they will be removed as well.
619 */
620void tb_switch_remove(struct tb_switch *sw)
621{
622 int i;
Andreas Noeverca389f72014-06-03 22:04:04 +0200623
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300624 /* port 0 is the switch itself and never has a remote */
625 for (i = 1; i <= sw->config.max_port_number; i++) {
626 if (tb_is_upstream_port(&sw->ports[i]))
627 continue;
628 if (sw->ports[i].remote)
629 tb_switch_remove(sw->ports[i].remote->sw);
630 sw->ports[i].remote = NULL;
631 }
632
633 if (!sw->is_unplugged)
634 tb_plug_events_active(sw, false);
635
636 device_unregister(&sw->dev);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200637}
638
Andreas Noever053596d2014-06-03 22:04:06 +0200639/**
Lukas Wunneraae20bb2016-03-20 13:57:20 +0100640 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
Andreas Noever053596d2014-06-03 22:04:06 +0200641 */
Lukas Wunneraae20bb2016-03-20 13:57:20 +0100642void tb_sw_set_unplugged(struct tb_switch *sw)
Andreas Noever053596d2014-06-03 22:04:06 +0200643{
644 int i;
645 if (sw == sw->tb->root_switch) {
646 tb_sw_WARN(sw, "cannot unplug root switch\n");
647 return;
648 }
649 if (sw->is_unplugged) {
650 tb_sw_WARN(sw, "is_unplugged already set\n");
651 return;
652 }
653 sw->is_unplugged = true;
654 for (i = 0; i <= sw->config.max_port_number; i++) {
655 if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
Lukas Wunneraae20bb2016-03-20 13:57:20 +0100656 tb_sw_set_unplugged(sw->ports[i].remote->sw);
Andreas Noever053596d2014-06-03 22:04:06 +0200657 }
658}
659
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200660int tb_switch_resume(struct tb_switch *sw)
661{
662 int i, err;
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200663 tb_sw_info(sw, "resuming switch\n");
664
Mika Westerberg08a5e4c2017-06-06 15:24:54 +0300665 /*
666 * Check for UID of the connected switches except for root
667 * switch which we assume cannot be removed.
668 */
669 if (tb_route(sw)) {
670 u64 uid;
671
672 err = tb_drom_read_uid_only(sw, &uid);
673 if (err) {
674 tb_sw_warn(sw, "uid read failed\n");
675 return err;
676 }
677 if (sw->uid != uid) {
678 tb_sw_info(sw,
679 "changed while suspended (uid %#llx -> %#llx)\n",
680 sw->uid, uid);
681 return -ENODEV;
682 }
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200683 }
684
685 /* upload configuration */
686 err = tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3);
687 if (err)
688 return err;
689
690 err = tb_plug_events_active(sw, true);
691 if (err)
692 return err;
693
694 /* check for surviving downstream switches */
695 for (i = 1; i <= sw->config.max_port_number; i++) {
696 struct tb_port *port = &sw->ports[i];
697 if (tb_is_upstream_port(port))
698 continue;
699 if (!port->remote)
700 continue;
701 if (tb_wait_for_port(port, true) <= 0
702 || tb_switch_resume(port->remote->sw)) {
703 tb_port_warn(port,
704 "lost during suspend, disconnecting\n");
Lukas Wunneraae20bb2016-03-20 13:57:20 +0100705 tb_sw_set_unplugged(port->remote->sw);
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200706 }
707 }
708 return 0;
709}
710
711void tb_switch_suspend(struct tb_switch *sw)
712{
713 int i, err;
714 err = tb_plug_events_active(sw, false);
715 if (err)
716 return;
717
718 for (i = 1; i <= sw->config.max_port_number; i++) {
719 if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
720 tb_switch_suspend(sw->ports[i].remote->sw);
721 }
722 /*
723 * TODO: invoke tb_cfg_prepare_to_sleep here? does not seem to have any
724 * effect?
725 */
726}