blob: 1524edf42ee8fd7afe9ce44a3317e0125c1db437 [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
Mika Westerbergf67cf492017-06-06 15:25:16 +030012/* Switch authorization from userspace is serialized by this lock */
13static DEFINE_MUTEX(switch_lock);
14
Andreas Noevera25c8b22014-06-03 22:04:02 +020015/* port utility functions */
16
17static const char *tb_port_type(struct tb_regs_port_header *port)
18{
19 switch (port->type >> 16) {
20 case 0:
21 switch ((u8) port->type) {
22 case 0:
23 return "Inactive";
24 case 1:
25 return "Port";
26 case 2:
27 return "NHI";
28 default:
29 return "unknown";
30 }
31 case 0x2:
32 return "Ethernet";
33 case 0x8:
34 return "SATA";
35 case 0xe:
36 return "DP/HDMI";
37 case 0x10:
38 return "PCIe";
39 case 0x20:
40 return "USB";
41 default:
42 return "unknown";
43 }
44}
45
46static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port)
47{
48 tb_info(tb,
49 " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n",
50 port->port_number, port->vendor_id, port->device_id,
51 port->revision, port->thunderbolt_version, tb_port_type(port),
52 port->type);
53 tb_info(tb, " Max hop id (in/out): %d/%d\n",
54 port->max_in_hop_id, port->max_out_hop_id);
55 tb_info(tb, " Max counters: %d\n", port->max_counters);
56 tb_info(tb, " NFC Credits: %#x\n", port->nfc_credits);
57}
58
59/**
Andreas Noever9da672a2014-06-03 22:04:05 +020060 * tb_port_state() - get connectedness state of a port
61 *
62 * The port must have a TB_CAP_PHY (i.e. it should be a real port).
63 *
64 * Return: Returns an enum tb_port_state on success or an error code on failure.
65 */
66static int tb_port_state(struct tb_port *port)
67{
68 struct tb_cap_phy phy;
69 int res;
70 if (port->cap_phy == 0) {
71 tb_port_WARN(port, "does not have a PHY\n");
72 return -EINVAL;
73 }
74 res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2);
75 if (res)
76 return res;
77 return phy.state;
78}
79
80/**
81 * tb_wait_for_port() - wait for a port to become ready
82 *
83 * Wait up to 1 second for a port to reach state TB_PORT_UP. If
84 * wait_if_unplugged is set then we also wait if the port is in state
85 * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
86 * switch resume). Otherwise we only wait if a device is registered but the link
87 * has not yet been established.
88 *
89 * Return: Returns an error code on failure. Returns 0 if the port is not
90 * connected or failed to reach state TB_PORT_UP within one second. Returns 1
91 * if the port is connected and in state TB_PORT_UP.
92 */
93int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
94{
95 int retries = 10;
96 int state;
97 if (!port->cap_phy) {
98 tb_port_WARN(port, "does not have PHY\n");
99 return -EINVAL;
100 }
101 if (tb_is_upstream_port(port)) {
102 tb_port_WARN(port, "is the upstream port\n");
103 return -EINVAL;
104 }
105
106 while (retries--) {
107 state = tb_port_state(port);
108 if (state < 0)
109 return state;
110 if (state == TB_PORT_DISABLED) {
111 tb_port_info(port, "is disabled (state: 0)\n");
112 return 0;
113 }
114 if (state == TB_PORT_UNPLUGGED) {
115 if (wait_if_unplugged) {
116 /* used during resume */
117 tb_port_info(port,
118 "is unplugged (state: 7), retrying...\n");
119 msleep(100);
120 continue;
121 }
122 tb_port_info(port, "is unplugged (state: 7)\n");
123 return 0;
124 }
125 if (state == TB_PORT_UP) {
126 tb_port_info(port,
127 "is connected, link is up (state: 2)\n");
128 return 1;
129 }
130
131 /*
132 * After plug-in the state is TB_PORT_CONNECTING. Give it some
133 * time.
134 */
135 tb_port_info(port,
136 "is connected, link is not up (state: %d), retrying...\n",
137 state);
138 msleep(100);
139 }
140 tb_port_warn(port,
141 "failed to reach state TB_PORT_UP. Ignoring port...\n");
142 return 0;
143}
144
145/**
Andreas Noever520b6702014-06-03 22:04:07 +0200146 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
147 *
148 * Change the number of NFC credits allocated to @port by @credits. To remove
149 * NFC credits pass a negative amount of credits.
150 *
151 * Return: Returns 0 on success or an error code on failure.
152 */
153int tb_port_add_nfc_credits(struct tb_port *port, int credits)
154{
155 if (credits == 0)
156 return 0;
157 tb_port_info(port,
158 "adding %#x NFC credits (%#x -> %#x)",
159 credits,
160 port->config.nfc_credits,
161 port->config.nfc_credits + credits);
162 port->config.nfc_credits += credits;
163 return tb_port_write(port, &port->config.nfc_credits,
164 TB_CFG_PORT, 4, 1);
165}
166
167/**
168 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
169 *
170 * Return: Returns 0 on success or an error code on failure.
171 */
172int tb_port_clear_counter(struct tb_port *port, int counter)
173{
174 u32 zero[3] = { 0, 0, 0 };
175 tb_port_info(port, "clearing counter %d\n", counter);
176 return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
177}
178
179/**
Andreas Noevera25c8b22014-06-03 22:04:02 +0200180 * tb_init_port() - initialize a port
181 *
182 * This is a helper method for tb_switch_alloc. Does not check or initialize
183 * any downstream switches.
184 *
185 * Return: Returns 0 on success or an error code on failure.
186 */
Andreas Noever343fcb82014-06-12 23:11:47 +0200187static int tb_init_port(struct tb_port *port)
Andreas Noevera25c8b22014-06-03 22:04:02 +0200188{
189 int res;
Andreas Noever9da672a2014-06-03 22:04:05 +0200190 int cap;
Andreas Noever343fcb82014-06-12 23:11:47 +0200191
Andreas Noevera25c8b22014-06-03 22:04:02 +0200192 res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8);
193 if (res)
194 return res;
195
Andreas Noever9da672a2014-06-03 22:04:05 +0200196 /* Port 0 is the switch itself and has no PHY. */
Andreas Noever343fcb82014-06-12 23:11:47 +0200197 if (port->config.type == TB_TYPE_PORT && port->port != 0) {
Mika Westerbergda2da042017-06-06 15:24:58 +0300198 cap = tb_port_find_cap(port, TB_PORT_CAP_PHY);
Andreas Noever9da672a2014-06-03 22:04:05 +0200199
200 if (cap > 0)
201 port->cap_phy = cap;
202 else
203 tb_port_WARN(port, "non switch port without a PHY\n");
204 }
205
Andreas Noever343fcb82014-06-12 23:11:47 +0200206 tb_dump_port(port->sw->tb, &port->config);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200207
208 /* TODO: Read dual link port, DP port and more from EEPROM. */
209 return 0;
210
211}
212
213/* switch utility functions */
214
215static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw)
216{
217 tb_info(tb,
218 " Switch: %x:%x (Revision: %d, TB Version: %d)\n",
219 sw->vendor_id, sw->device_id, sw->revision,
220 sw->thunderbolt_version);
221 tb_info(tb, " Max Port Number: %d\n", sw->max_port_number);
222 tb_info(tb, " Config:\n");
223 tb_info(tb,
224 " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
225 sw->upstream_port_number, sw->depth,
226 (((u64) sw->route_hi) << 32) | sw->route_lo,
227 sw->enabled, sw->plug_events_delay);
228 tb_info(tb,
229 " unknown1: %#x unknown4: %#x\n",
230 sw->__unknown1, sw->__unknown4);
231}
232
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200233/**
234 * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
235 *
236 * Return: Returns 0 on success or an error code on failure.
237 */
238int tb_switch_reset(struct tb *tb, u64 route)
239{
240 struct tb_cfg_result res;
241 struct tb_regs_switch_header header = {
242 header.route_hi = route >> 32,
243 header.route_lo = route,
244 header.enabled = true,
245 };
246 tb_info(tb, "resetting switch at %llx\n", route);
247 res.err = tb_cfg_write(tb->ctl, ((u32 *) &header) + 2, route,
248 0, 2, 2, 2);
249 if (res.err)
250 return res.err;
251 res = tb_cfg_reset(tb->ctl, route, TB_CFG_DEFAULT_TIMEOUT);
252 if (res.err > 0)
253 return -EIO;
254 return res.err;
255}
256
Andreas Noever053596d2014-06-03 22:04:06 +0200257struct tb_switch *get_switch_at_route(struct tb_switch *sw, u64 route)
258{
259 u8 next_port = route; /*
260 * Routes use a stride of 8 bits,
261 * eventhough a port index has 6 bits at most.
262 * */
263 if (route == 0)
264 return sw;
265 if (next_port > sw->config.max_port_number)
Sachin Kamatc9c2dee2014-06-20 14:32:31 +0530266 return NULL;
Andreas Noever053596d2014-06-03 22:04:06 +0200267 if (tb_is_upstream_port(&sw->ports[next_port]))
Sachin Kamatc9c2dee2014-06-20 14:32:31 +0530268 return NULL;
Andreas Noever053596d2014-06-03 22:04:06 +0200269 if (!sw->ports[next_port].remote)
Sachin Kamatc9c2dee2014-06-20 14:32:31 +0530270 return NULL;
Andreas Noever053596d2014-06-03 22:04:06 +0200271 return get_switch_at_route(sw->ports[next_port].remote->sw,
272 route >> TB_ROUTE_SHIFT);
273}
274
Andreas Noevera25c8b22014-06-03 22:04:02 +0200275/**
Andreas Noeverca389f72014-06-03 22:04:04 +0200276 * tb_plug_events_active() - enable/disable plug events on a switch
277 *
278 * Also configures a sane plug_events_delay of 255ms.
279 *
280 * Return: Returns 0 on success or an error code on failure.
281 */
282static int tb_plug_events_active(struct tb_switch *sw, bool active)
283{
284 u32 data;
285 int res;
286
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300287 if (!sw->config.enabled)
288 return 0;
289
Andreas Noeverca389f72014-06-03 22:04:04 +0200290 sw->config.plug_events_delay = 0xff;
291 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1);
292 if (res)
293 return res;
294
295 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
296 if (res)
297 return res;
298
299 if (active) {
300 data = data & 0xFFFFFF83;
301 switch (sw->config.device_id) {
Lukas Wunner1d111402016-03-20 13:57:20 +0100302 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
303 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
304 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
Andreas Noeverca389f72014-06-03 22:04:04 +0200305 break;
306 default:
307 data |= 4;
308 }
309 } else {
310 data = data | 0x7c;
311 }
312 return tb_sw_write(sw, &data, TB_CFG_SWITCH,
313 sw->cap_plug_events + 1, 1);
314}
315
Mika Westerbergf67cf492017-06-06 15:25:16 +0300316static ssize_t authorized_show(struct device *dev,
317 struct device_attribute *attr,
318 char *buf)
319{
320 struct tb_switch *sw = tb_to_switch(dev);
321
322 return sprintf(buf, "%u\n", sw->authorized);
323}
324
325static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
326{
327 int ret = -EINVAL;
328
329 if (mutex_lock_interruptible(&switch_lock))
330 return -ERESTARTSYS;
331
332 if (sw->authorized)
333 goto unlock;
334
335 switch (val) {
336 /* Approve switch */
337 case 1:
338 if (sw->key)
339 ret = tb_domain_approve_switch_key(sw->tb, sw);
340 else
341 ret = tb_domain_approve_switch(sw->tb, sw);
342 break;
343
344 /* Challenge switch */
345 case 2:
346 if (sw->key)
347 ret = tb_domain_challenge_switch_key(sw->tb, sw);
348 break;
349
350 default:
351 break;
352 }
353
354 if (!ret) {
355 sw->authorized = val;
356 /* Notify status change to the userspace */
357 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
358 }
359
360unlock:
361 mutex_unlock(&switch_lock);
362 return ret;
363}
364
365static ssize_t authorized_store(struct device *dev,
366 struct device_attribute *attr,
367 const char *buf, size_t count)
368{
369 struct tb_switch *sw = tb_to_switch(dev);
370 unsigned int val;
371 ssize_t ret;
372
373 ret = kstrtouint(buf, 0, &val);
374 if (ret)
375 return ret;
376 if (val > 2)
377 return -EINVAL;
378
379 ret = tb_switch_set_authorized(sw, val);
380
381 return ret ? ret : count;
382}
383static DEVICE_ATTR_RW(authorized);
384
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300385static ssize_t device_show(struct device *dev, struct device_attribute *attr,
386 char *buf)
Andreas Noevera25c8b22014-06-03 22:04:02 +0200387{
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300388 struct tb_switch *sw = tb_to_switch(dev);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200389
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300390 return sprintf(buf, "%#x\n", sw->device);
391}
392static DEVICE_ATTR_RO(device);
Andreas Noeverca389f72014-06-03 22:04:04 +0200393
Mika Westerberg72ee3392017-06-06 15:25:05 +0300394static ssize_t
395device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
396{
397 struct tb_switch *sw = tb_to_switch(dev);
398
399 return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : "");
400}
401static DEVICE_ATTR_RO(device_name);
402
Mika Westerbergf67cf492017-06-06 15:25:16 +0300403static ssize_t key_show(struct device *dev, struct device_attribute *attr,
404 char *buf)
405{
406 struct tb_switch *sw = tb_to_switch(dev);
407 ssize_t ret;
408
409 if (mutex_lock_interruptible(&switch_lock))
410 return -ERESTARTSYS;
411
412 if (sw->key)
413 ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key);
414 else
415 ret = sprintf(buf, "\n");
416
417 mutex_unlock(&switch_lock);
418 return ret;
419}
420
421static ssize_t key_store(struct device *dev, struct device_attribute *attr,
422 const char *buf, size_t count)
423{
424 struct tb_switch *sw = tb_to_switch(dev);
425 u8 key[TB_SWITCH_KEY_SIZE];
426 ssize_t ret = count;
427
428 if (count < 64)
429 return -EINVAL;
430
431 if (hex2bin(key, buf, sizeof(key)))
432 return -EINVAL;
433
434 if (mutex_lock_interruptible(&switch_lock))
435 return -ERESTARTSYS;
436
437 if (sw->authorized) {
438 ret = -EBUSY;
439 } else {
440 kfree(sw->key);
441 sw->key = kmemdup(key, sizeof(key), GFP_KERNEL);
442 if (!sw->key)
443 ret = -ENOMEM;
444 }
445
446 mutex_unlock(&switch_lock);
447 return ret;
448}
449static DEVICE_ATTR_RW(key);
450
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300451static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
452 char *buf)
453{
454 struct tb_switch *sw = tb_to_switch(dev);
455
456 return sprintf(buf, "%#x\n", sw->vendor);
457}
458static DEVICE_ATTR_RO(vendor);
459
Mika Westerberg72ee3392017-06-06 15:25:05 +0300460static ssize_t
461vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
462{
463 struct tb_switch *sw = tb_to_switch(dev);
464
465 return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : "");
466}
467static DEVICE_ATTR_RO(vendor_name);
468
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300469static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
470 char *buf)
471{
472 struct tb_switch *sw = tb_to_switch(dev);
473
474 return sprintf(buf, "%pUb\n", sw->uuid);
475}
476static DEVICE_ATTR_RO(unique_id);
477
478static struct attribute *switch_attrs[] = {
Mika Westerbergf67cf492017-06-06 15:25:16 +0300479 &dev_attr_authorized.attr,
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300480 &dev_attr_device.attr,
Mika Westerberg72ee3392017-06-06 15:25:05 +0300481 &dev_attr_device_name.attr,
Mika Westerbergf67cf492017-06-06 15:25:16 +0300482 &dev_attr_key.attr,
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300483 &dev_attr_vendor.attr,
Mika Westerberg72ee3392017-06-06 15:25:05 +0300484 &dev_attr_vendor_name.attr,
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300485 &dev_attr_unique_id.attr,
486 NULL,
487};
488
Mika Westerbergf67cf492017-06-06 15:25:16 +0300489static umode_t switch_attr_is_visible(struct kobject *kobj,
490 struct attribute *attr, int n)
491{
492 struct device *dev = container_of(kobj, struct device, kobj);
493 struct tb_switch *sw = tb_to_switch(dev);
494
495 if (attr == &dev_attr_key.attr) {
496 if (tb_route(sw) &&
497 sw->tb->security_level == TB_SECURITY_SECURE &&
498 sw->security_level == TB_SECURITY_SECURE)
499 return attr->mode;
500 return 0;
501 }
502
503 return attr->mode;
504}
505
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300506static struct attribute_group switch_group = {
Mika Westerbergf67cf492017-06-06 15:25:16 +0300507 .is_visible = switch_attr_is_visible,
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300508 .attrs = switch_attrs,
509};
510
511static const struct attribute_group *switch_groups[] = {
512 &switch_group,
513 NULL,
514};
515
516static void tb_switch_release(struct device *dev)
517{
518 struct tb_switch *sw = tb_to_switch(dev);
519
Mika Westerberg3e136762017-06-06 15:25:14 +0300520 dma_port_free(sw->dma_port);
521
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300522 kfree(sw->uuid);
Mika Westerberg72ee3392017-06-06 15:25:05 +0300523 kfree(sw->device_name);
524 kfree(sw->vendor_name);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200525 kfree(sw->ports);
Andreas Noever343fcb82014-06-12 23:11:47 +0200526 kfree(sw->drom);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300527 kfree(sw->key);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200528 kfree(sw);
529}
530
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300531struct device_type tb_switch_type = {
532 .name = "thunderbolt_device",
533 .release = tb_switch_release,
534};
535
Mika Westerberg2c3c4192017-06-06 15:25:13 +0300536static int tb_switch_get_generation(struct tb_switch *sw)
537{
538 switch (sw->config.device_id) {
539 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
540 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
541 case PCI_DEVICE_ID_INTEL_LIGHT_PEAK:
542 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
543 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
544 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
545 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE:
546 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE:
547 return 1;
548
549 case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE:
550 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
551 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
552 return 2;
553
554 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
555 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
556 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
557 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
558 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
559 return 3;
560
561 default:
562 /*
563 * For unknown switches assume generation to be 1 to be
564 * on the safe side.
565 */
566 tb_sw_warn(sw, "unsupported switch device id %#x\n",
567 sw->config.device_id);
568 return 1;
569 }
570}
571
Andreas Noevera25c8b22014-06-03 22:04:02 +0200572/**
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300573 * tb_switch_alloc() - allocate a switch
574 * @tb: Pointer to the owning domain
575 * @parent: Parent device for this switch
576 * @route: Route string for this switch
Andreas Noevera25c8b22014-06-03 22:04:02 +0200577 *
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300578 * Allocates and initializes a switch. Will not upload configuration to
579 * the switch. For that you need to call tb_switch_configure()
580 * separately. The returned switch should be released by calling
581 * tb_switch_put().
582 *
583 * Return: Pointer to the allocated switch or %NULL in case of failure
Andreas Noevera25c8b22014-06-03 22:04:02 +0200584 */
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300585struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
586 u64 route)
Andreas Noevera25c8b22014-06-03 22:04:02 +0200587{
588 int i;
Andreas Noeverca389f72014-06-03 22:04:04 +0200589 int cap;
Andreas Noevera25c8b22014-06-03 22:04:02 +0200590 struct tb_switch *sw;
591 int upstream_port = tb_cfg_get_upstream_port(tb->ctl, route);
592 if (upstream_port < 0)
593 return NULL;
594
595 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
596 if (!sw)
597 return NULL;
598
599 sw->tb = tb;
Lukas Wunneraae20bb2016-03-20 13:57:20 +0100600 if (tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5))
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300601 goto err_free_sw_ports;
602
603 tb_info(tb, "current switch config:\n");
Andreas Noevera25c8b22014-06-03 22:04:02 +0200604 tb_dump_switch(tb, &sw->config);
605
606 /* configure switch */
607 sw->config.upstream_port_number = upstream_port;
608 sw->config.depth = tb_route_length(route);
609 sw->config.route_lo = route;
610 sw->config.route_hi = route >> 32;
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300611 sw->config.enabled = 0;
Andreas Noevera25c8b22014-06-03 22:04:02 +0200612
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300613 /* initialize ports */
614 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports),
615 GFP_KERNEL);
616 if (!sw->ports)
617 goto err_free_sw_ports;
618
619 for (i = 0; i <= sw->config.max_port_number; i++) {
620 /* minimum setup for tb_find_cap and tb_drom_read to work */
621 sw->ports[i].sw = sw;
622 sw->ports[i].port = i;
623 }
624
Mika Westerberg2c3c4192017-06-06 15:25:13 +0300625 sw->generation = tb_switch_get_generation(sw);
626
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300627 cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS);
628 if (cap < 0) {
629 tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
630 goto err_free_sw_ports;
631 }
632 sw->cap_plug_events = cap;
633
Mika Westerbergf67cf492017-06-06 15:25:16 +0300634 /* Root switch is always authorized */
635 if (!route)
636 sw->authorized = true;
637
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300638 device_initialize(&sw->dev);
639 sw->dev.parent = parent;
640 sw->dev.bus = &tb_bus_type;
641 sw->dev.type = &tb_switch_type;
642 sw->dev.groups = switch_groups;
643 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
644
645 return sw;
646
647err_free_sw_ports:
648 kfree(sw->ports);
649 kfree(sw);
650
651 return NULL;
652}
653
654/**
655 * tb_switch_configure() - Uploads configuration to the switch
656 * @sw: Switch to configure
657 *
658 * Call this function before the switch is added to the system. It will
659 * upload configuration to the switch and makes it available for the
660 * connection manager to use.
661 *
662 * Return: %0 in case of success and negative errno in case of failure
663 */
664int tb_switch_configure(struct tb_switch *sw)
665{
666 struct tb *tb = sw->tb;
667 u64 route;
668 int ret;
669
670 route = tb_route(sw);
671 tb_info(tb,
672 "initializing Switch at %#llx (depth: %d, up port: %d)\n",
673 route, tb_route_length(route), sw->config.upstream_port_number);
674
675 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL)
Andreas Noevera25c8b22014-06-03 22:04:02 +0200676 tb_sw_warn(sw, "unknown switch vendor id %#x\n",
677 sw->config.vendor_id);
678
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300679 sw->config.enabled = 1;
680
Andreas Noevera25c8b22014-06-03 22:04:02 +0200681 /* upload configuration */
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300682 ret = tb_sw_write(sw, 1 + (u32 *)&sw->config, TB_CFG_SWITCH, 1, 3);
683 if (ret)
684 return ret;
Andreas Noevera25c8b22014-06-03 22:04:02 +0200685
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300686 return tb_plug_events_active(sw, true);
687}
Andreas Noevera25c8b22014-06-03 22:04:02 +0200688
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300689static void tb_switch_set_uuid(struct tb_switch *sw)
690{
691 u32 uuid[4];
692 int cap;
693
694 if (sw->uuid)
695 return;
696
697 /*
698 * The newer controllers include fused UUID as part of link
699 * controller specific registers
700 */
701 cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER);
702 if (cap > 0) {
703 tb_sw_read(sw, uuid, TB_CFG_SWITCH, cap + 3, 4);
704 } else {
705 /*
706 * ICM generates UUID based on UID and fills the upper
707 * two words with ones. This is not strictly following
708 * UUID format but we want to be compatible with it so
709 * we do the same here.
710 */
711 uuid[0] = sw->uid & 0xffffffff;
712 uuid[1] = (sw->uid >> 32) & 0xffffffff;
713 uuid[2] = 0xffffffff;
714 uuid[3] = 0xffffffff;
Andreas Noevera25c8b22014-06-03 22:04:02 +0200715 }
716
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300717 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
718}
719
Mika Westerberg3e136762017-06-06 15:25:14 +0300720static void tb_switch_add_dma_port(struct tb_switch *sw)
721{
722 switch (sw->generation) {
723 case 3:
724 break;
725
726 case 2:
727 /* Only root switch can be upgraded */
728 if (tb_route(sw))
729 return;
730 break;
731
732 default:
733 return;
734 }
735
736 sw->dma_port = dma_port_alloc(sw);
737}
738
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300739/**
740 * tb_switch_add() - Add a switch to the domain
741 * @sw: Switch to add
742 *
743 * This is the last step in adding switch to the domain. It will read
744 * identification information from DROM and initializes ports so that
745 * they can be used to connect other switches. The switch will be
746 * exposed to the userspace when this function successfully returns. To
747 * remove and release the switch, call tb_switch_remove().
748 *
749 * Return: %0 in case of success and negative errno in case of failure
750 */
751int tb_switch_add(struct tb_switch *sw)
752{
753 int i, ret;
Andreas Noeverca389f72014-06-03 22:04:04 +0200754
Mika Westerberg3e136762017-06-06 15:25:14 +0300755 /*
756 * Initialize DMA control port now before we read DROM. Recent
757 * host controllers have more complete DROM on NVM that includes
758 * vendor and model identification strings which we then expose
759 * to the userspace. NVM can be accessed through DMA
760 * configuration based mailbox.
761 */
762 tb_switch_add_dma_port(sw);
763
Andreas Noever343fcb82014-06-12 23:11:47 +0200764 /* read drom */
Mika Westerbergf53e7672017-06-06 15:25:02 +0300765 ret = tb_drom_read(sw);
766 if (ret) {
767 tb_sw_warn(sw, "tb_eeprom_read_rom failed\n");
768 return ret;
769 }
Andreas Noever343fcb82014-06-12 23:11:47 +0200770 tb_sw_info(sw, "uid: %#llx\n", sw->uid);
771
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300772 tb_switch_set_uuid(sw);
773
Andreas Noever343fcb82014-06-12 23:11:47 +0200774 for (i = 0; i <= sw->config.max_port_number; i++) {
775 if (sw->ports[i].disabled) {
776 tb_port_info(&sw->ports[i], "disabled by eeprom\n");
777 continue;
778 }
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300779 ret = tb_init_port(&sw->ports[i]);
780 if (ret)
781 return ret;
Andreas Noever343fcb82014-06-12 23:11:47 +0200782 }
783
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300784 return device_add(&sw->dev);
785}
Andreas Noeverc90553b2014-06-03 22:04:11 +0200786
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300787/**
788 * tb_switch_remove() - Remove and release a switch
789 * @sw: Switch to remove
790 *
791 * This will remove the switch from the domain and release it after last
792 * reference count drops to zero. If there are switches connected below
793 * this switch, they will be removed as well.
794 */
795void tb_switch_remove(struct tb_switch *sw)
796{
797 int i;
Andreas Noeverca389f72014-06-03 22:04:04 +0200798
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300799 /* port 0 is the switch itself and never has a remote */
800 for (i = 1; i <= sw->config.max_port_number; i++) {
801 if (tb_is_upstream_port(&sw->ports[i]))
802 continue;
803 if (sw->ports[i].remote)
804 tb_switch_remove(sw->ports[i].remote->sw);
805 sw->ports[i].remote = NULL;
806 }
807
808 if (!sw->is_unplugged)
809 tb_plug_events_active(sw, false);
810
811 device_unregister(&sw->dev);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200812}
813
Andreas Noever053596d2014-06-03 22:04:06 +0200814/**
Lukas Wunneraae20bb2016-03-20 13:57:20 +0100815 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
Andreas Noever053596d2014-06-03 22:04:06 +0200816 */
Lukas Wunneraae20bb2016-03-20 13:57:20 +0100817void tb_sw_set_unplugged(struct tb_switch *sw)
Andreas Noever053596d2014-06-03 22:04:06 +0200818{
819 int i;
820 if (sw == sw->tb->root_switch) {
821 tb_sw_WARN(sw, "cannot unplug root switch\n");
822 return;
823 }
824 if (sw->is_unplugged) {
825 tb_sw_WARN(sw, "is_unplugged already set\n");
826 return;
827 }
828 sw->is_unplugged = true;
829 for (i = 0; i <= sw->config.max_port_number; i++) {
830 if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
Lukas Wunneraae20bb2016-03-20 13:57:20 +0100831 tb_sw_set_unplugged(sw->ports[i].remote->sw);
Andreas Noever053596d2014-06-03 22:04:06 +0200832 }
833}
834
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200835int tb_switch_resume(struct tb_switch *sw)
836{
837 int i, err;
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200838 tb_sw_info(sw, "resuming switch\n");
839
Mika Westerberg08a5e4c2017-06-06 15:24:54 +0300840 /*
841 * Check for UID of the connected switches except for root
842 * switch which we assume cannot be removed.
843 */
844 if (tb_route(sw)) {
845 u64 uid;
846
847 err = tb_drom_read_uid_only(sw, &uid);
848 if (err) {
849 tb_sw_warn(sw, "uid read failed\n");
850 return err;
851 }
852 if (sw->uid != uid) {
853 tb_sw_info(sw,
854 "changed while suspended (uid %#llx -> %#llx)\n",
855 sw->uid, uid);
856 return -ENODEV;
857 }
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200858 }
859
860 /* upload configuration */
861 err = tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3);
862 if (err)
863 return err;
864
865 err = tb_plug_events_active(sw, true);
866 if (err)
867 return err;
868
869 /* check for surviving downstream switches */
870 for (i = 1; i <= sw->config.max_port_number; i++) {
871 struct tb_port *port = &sw->ports[i];
872 if (tb_is_upstream_port(port))
873 continue;
874 if (!port->remote)
875 continue;
876 if (tb_wait_for_port(port, true) <= 0
877 || tb_switch_resume(port->remote->sw)) {
878 tb_port_warn(port,
879 "lost during suspend, disconnecting\n");
Lukas Wunneraae20bb2016-03-20 13:57:20 +0100880 tb_sw_set_unplugged(port->remote->sw);
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200881 }
882 }
883 return 0;
884}
885
886void tb_switch_suspend(struct tb_switch *sw)
887{
888 int i, err;
889 err = tb_plug_events_active(sw, false);
890 if (err)
891 return;
892
893 for (i = 1; i <= sw->config.max_port_number; i++) {
894 if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
895 tb_switch_suspend(sw->ports[i].remote->sw);
896 }
897 /*
898 * TODO: invoke tb_cfg_prepare_to_sleep here? does not seem to have any
899 * effect?
900 */
901}
Mika Westerbergf67cf492017-06-06 15:25:16 +0300902
903struct tb_sw_lookup {
904 struct tb *tb;
905 u8 link;
906 u8 depth;
907 const uuid_be *uuid;
908};
909
910static int tb_switch_match(struct device *dev, void *data)
911{
912 struct tb_switch *sw = tb_to_switch(dev);
913 struct tb_sw_lookup *lookup = data;
914
915 if (!sw)
916 return 0;
917 if (sw->tb != lookup->tb)
918 return 0;
919
920 if (lookup->uuid)
921 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid));
922
923 /* Root switch is matched only by depth */
924 if (!lookup->depth)
925 return !sw->depth;
926
927 return sw->link == lookup->link && sw->depth == lookup->depth;
928}
929
930/**
931 * tb_switch_find_by_link_depth() - Find switch by link and depth
932 * @tb: Domain the switch belongs
933 * @link: Link number the switch is connected
934 * @depth: Depth of the switch in link
935 *
936 * Returned switch has reference count increased so the caller needs to
937 * call tb_switch_put() when done with the switch.
938 */
939struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth)
940{
941 struct tb_sw_lookup lookup;
942 struct device *dev;
943
944 memset(&lookup, 0, sizeof(lookup));
945 lookup.tb = tb;
946 lookup.link = link;
947 lookup.depth = depth;
948
949 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
950 if (dev)
951 return tb_to_switch(dev);
952
953 return NULL;
954}
955
956/**
957 * tb_switch_find_by_link_depth() - Find switch by UUID
958 * @tb: Domain the switch belongs
959 * @uuid: UUID to look for
960 *
961 * Returned switch has reference count increased so the caller needs to
962 * call tb_switch_put() when done with the switch.
963 */
964struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_be *uuid)
965{
966 struct tb_sw_lookup lookup;
967 struct device *dev;
968
969 memset(&lookup, 0, sizeof(lookup));
970 lookup.tb = tb;
971 lookup.uuid = uuid;
972
973 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
974 if (dev)
975 return tb_to_switch(dev);
976
977 return NULL;
978}