2 * Thunderbolt Cactus Ridge driver - switch/port utility functions
4 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
7 #include <linux/delay.h>
8 #include <linux/slab.h>
12 /* port utility functions */
14 static const char *tb_port_type(struct tb_regs_port_header *port)
16 switch (port->type >> 16) {
18 switch ((u8) port->type) {
43 static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port)
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),
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);
57 * tb_port_state() - get connectedness state of a port
59 * The port must have a TB_CAP_PHY (i.e. it should be a real port).
61 * Return: Returns an enum tb_port_state on success or an error code on failure.
63 static int tb_port_state(struct tb_port *port)
65 struct tb_cap_phy phy;
67 if (port->cap_phy == 0) {
68 tb_port_WARN(port, "does not have a PHY\n");
71 res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2);
78 * tb_wait_for_port() - wait for a port to become ready
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.
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.
90 int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
95 tb_port_WARN(port, "does not have PHY\n");
98 if (tb_is_upstream_port(port)) {
99 tb_port_WARN(port, "is the upstream port\n");
104 state = tb_port_state(port);
107 if (state == TB_PORT_DISABLED) {
108 tb_port_info(port, "is disabled (state: 0)\n");
111 if (state == TB_PORT_UNPLUGGED) {
112 if (wait_if_unplugged) {
113 /* used during resume */
115 "is unplugged (state: 7), retrying...\n");
119 tb_port_info(port, "is unplugged (state: 7)\n");
122 if (state == TB_PORT_UP) {
124 "is connected, link is up (state: 2)\n");
129 * After plug-in the state is TB_PORT_CONNECTING. Give it some
133 "is connected, link is not up (state: %d), retrying...\n",
138 "failed to reach state TB_PORT_UP. Ignoring port...\n");
143 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
145 * Change the number of NFC credits allocated to @port by @credits. To remove
146 * NFC credits pass a negative amount of credits.
148 * Return: Returns 0 on success or an error code on failure.
150 int tb_port_add_nfc_credits(struct tb_port *port, int credits)
155 "adding %#x NFC credits (%#x -> %#x)",
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,
165 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
167 * Return: Returns 0 on success or an error code on failure.
169 int tb_port_clear_counter(struct tb_port *port, int counter)
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);
177 * tb_init_port() - initialize a port
179 * This is a helper method for tb_switch_alloc. Does not check or initialize
180 * any downstream switches.
182 * Return: Returns 0 on success or an error code on failure.
184 static int tb_init_port(struct tb_port *port)
189 res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8);
193 /* Port 0 is the switch itself and has no PHY. */
194 if (port->config.type == TB_TYPE_PORT && port->port != 0) {
195 cap = tb_port_find_cap(port, TB_PORT_CAP_PHY);
200 tb_port_WARN(port, "non switch port without a PHY\n");
203 tb_dump_port(port->sw->tb, &port->config);
205 /* TODO: Read dual link port, DP port and more from EEPROM. */
210 /* switch utility functions */
212 static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw)
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");
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);
226 " unknown1: %#x unknown4: %#x\n",
227 sw->__unknown1, sw->__unknown4);
231 * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
233 * Return: Returns 0 on success or an error code on failure.
235 int tb_switch_reset(struct tb *tb, u64 route)
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,
243 tb_info(tb, "resetting switch at %llx\n", route);
244 res.err = tb_cfg_write(tb->ctl, ((u32 *) &header) + 2, route,
248 res = tb_cfg_reset(tb->ctl, route, TB_CFG_DEFAULT_TIMEOUT);
254 struct tb_switch *get_switch_at_route(struct tb_switch *sw, u64 route)
256 u8 next_port = route; /*
257 * Routes use a stride of 8 bits,
258 * eventhough a port index has 6 bits at most.
262 if (next_port > sw->config.max_port_number)
264 if (tb_is_upstream_port(&sw->ports[next_port]))
266 if (!sw->ports[next_port].remote)
268 return get_switch_at_route(sw->ports[next_port].remote->sw,
269 route >> TB_ROUTE_SHIFT);
273 * tb_plug_events_active() - enable/disable plug events on a switch
275 * Also configures a sane plug_events_delay of 255ms.
277 * Return: Returns 0 on success or an error code on failure.
279 static int tb_plug_events_active(struct tb_switch *sw, bool active)
284 if (!sw->config.enabled)
287 sw->config.plug_events_delay = 0xff;
288 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1);
292 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
297 data = data & 0xFFFFFF83;
298 switch (sw->config.device_id) {
299 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
300 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
301 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
309 return tb_sw_write(sw, &data, TB_CFG_SWITCH,
310 sw->cap_plug_events + 1, 1);
313 static ssize_t device_show(struct device *dev, struct device_attribute *attr,
316 struct tb_switch *sw = tb_to_switch(dev);
318 return sprintf(buf, "%#x\n", sw->device);
320 static DEVICE_ATTR_RO(device);
323 device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
325 struct tb_switch *sw = tb_to_switch(dev);
327 return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : "");
329 static DEVICE_ATTR_RO(device_name);
331 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
334 struct tb_switch *sw = tb_to_switch(dev);
336 return sprintf(buf, "%#x\n", sw->vendor);
338 static DEVICE_ATTR_RO(vendor);
341 vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
343 struct tb_switch *sw = tb_to_switch(dev);
345 return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : "");
347 static DEVICE_ATTR_RO(vendor_name);
349 static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
352 struct tb_switch *sw = tb_to_switch(dev);
354 return sprintf(buf, "%pUb\n", sw->uuid);
356 static DEVICE_ATTR_RO(unique_id);
358 static struct attribute *switch_attrs[] = {
359 &dev_attr_device.attr,
360 &dev_attr_device_name.attr,
361 &dev_attr_vendor.attr,
362 &dev_attr_vendor_name.attr,
363 &dev_attr_unique_id.attr,
367 static struct attribute_group switch_group = {
368 .attrs = switch_attrs,
371 static const struct attribute_group *switch_groups[] = {
376 static void tb_switch_release(struct device *dev)
378 struct tb_switch *sw = tb_to_switch(dev);
381 kfree(sw->device_name);
382 kfree(sw->vendor_name);
388 struct device_type tb_switch_type = {
389 .name = "thunderbolt_device",
390 .release = tb_switch_release,
394 * tb_switch_alloc() - allocate a switch
395 * @tb: Pointer to the owning domain
396 * @parent: Parent device for this switch
397 * @route: Route string for this switch
399 * Allocates and initializes a switch. Will not upload configuration to
400 * the switch. For that you need to call tb_switch_configure()
401 * separately. The returned switch should be released by calling
404 * Return: Pointer to the allocated switch or %NULL in case of failure
406 struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
411 struct tb_switch *sw;
412 int upstream_port = tb_cfg_get_upstream_port(tb->ctl, route);
413 if (upstream_port < 0)
416 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
421 if (tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5))
422 goto err_free_sw_ports;
424 tb_info(tb, "current switch config:\n");
425 tb_dump_switch(tb, &sw->config);
427 /* configure switch */
428 sw->config.upstream_port_number = upstream_port;
429 sw->config.depth = tb_route_length(route);
430 sw->config.route_lo = route;
431 sw->config.route_hi = route >> 32;
432 sw->config.enabled = 0;
434 /* initialize ports */
435 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports),
438 goto err_free_sw_ports;
440 for (i = 0; i <= sw->config.max_port_number; i++) {
441 /* minimum setup for tb_find_cap and tb_drom_read to work */
442 sw->ports[i].sw = sw;
443 sw->ports[i].port = i;
446 cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS);
448 tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
449 goto err_free_sw_ports;
451 sw->cap_plug_events = cap;
453 device_initialize(&sw->dev);
454 sw->dev.parent = parent;
455 sw->dev.bus = &tb_bus_type;
456 sw->dev.type = &tb_switch_type;
457 sw->dev.groups = switch_groups;
458 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
470 * tb_switch_configure() - Uploads configuration to the switch
471 * @sw: Switch to configure
473 * Call this function before the switch is added to the system. It will
474 * upload configuration to the switch and makes it available for the
475 * connection manager to use.
477 * Return: %0 in case of success and negative errno in case of failure
479 int tb_switch_configure(struct tb_switch *sw)
481 struct tb *tb = sw->tb;
485 route = tb_route(sw);
487 "initializing Switch at %#llx (depth: %d, up port: %d)\n",
488 route, tb_route_length(route), sw->config.upstream_port_number);
490 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL)
491 tb_sw_warn(sw, "unknown switch vendor id %#x\n",
492 sw->config.vendor_id);
494 switch (sw->config.device_id) {
495 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
496 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
497 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
498 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
499 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
500 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
501 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
502 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
503 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
507 tb_sw_warn(sw, "unsupported switch device id %#x\n",
508 sw->config.device_id);
511 sw->config.enabled = 1;
513 /* upload configuration */
514 ret = tb_sw_write(sw, 1 + (u32 *)&sw->config, TB_CFG_SWITCH, 1, 3);
518 return tb_plug_events_active(sw, true);
521 static void tb_switch_set_uuid(struct tb_switch *sw)
530 * The newer controllers include fused UUID as part of link
531 * controller specific registers
533 cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER);
535 tb_sw_read(sw, uuid, TB_CFG_SWITCH, cap + 3, 4);
538 * ICM generates UUID based on UID and fills the upper
539 * two words with ones. This is not strictly following
540 * UUID format but we want to be compatible with it so
541 * we do the same here.
543 uuid[0] = sw->uid & 0xffffffff;
544 uuid[1] = (sw->uid >> 32) & 0xffffffff;
545 uuid[2] = 0xffffffff;
546 uuid[3] = 0xffffffff;
549 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
553 * tb_switch_add() - Add a switch to the domain
556 * This is the last step in adding switch to the domain. It will read
557 * identification information from DROM and initializes ports so that
558 * they can be used to connect other switches. The switch will be
559 * exposed to the userspace when this function successfully returns. To
560 * remove and release the switch, call tb_switch_remove().
562 * Return: %0 in case of success and negative errno in case of failure
564 int tb_switch_add(struct tb_switch *sw)
569 ret = tb_drom_read(sw);
571 tb_sw_warn(sw, "tb_eeprom_read_rom failed\n");
574 tb_sw_info(sw, "uid: %#llx\n", sw->uid);
576 tb_switch_set_uuid(sw);
578 for (i = 0; i <= sw->config.max_port_number; i++) {
579 if (sw->ports[i].disabled) {
580 tb_port_info(&sw->ports[i], "disabled by eeprom\n");
583 ret = tb_init_port(&sw->ports[i]);
588 return device_add(&sw->dev);
592 * tb_switch_remove() - Remove and release a switch
593 * @sw: Switch to remove
595 * This will remove the switch from the domain and release it after last
596 * reference count drops to zero. If there are switches connected below
597 * this switch, they will be removed as well.
599 void tb_switch_remove(struct tb_switch *sw)
603 /* port 0 is the switch itself and never has a remote */
604 for (i = 1; i <= sw->config.max_port_number; i++) {
605 if (tb_is_upstream_port(&sw->ports[i]))
607 if (sw->ports[i].remote)
608 tb_switch_remove(sw->ports[i].remote->sw);
609 sw->ports[i].remote = NULL;
612 if (!sw->is_unplugged)
613 tb_plug_events_active(sw, false);
615 device_unregister(&sw->dev);
619 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
621 void tb_sw_set_unplugged(struct tb_switch *sw)
624 if (sw == sw->tb->root_switch) {
625 tb_sw_WARN(sw, "cannot unplug root switch\n");
628 if (sw->is_unplugged) {
629 tb_sw_WARN(sw, "is_unplugged already set\n");
632 sw->is_unplugged = true;
633 for (i = 0; i <= sw->config.max_port_number; i++) {
634 if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
635 tb_sw_set_unplugged(sw->ports[i].remote->sw);
639 int tb_switch_resume(struct tb_switch *sw)
642 tb_sw_info(sw, "resuming switch\n");
645 * Check for UID of the connected switches except for root
646 * switch which we assume cannot be removed.
651 err = tb_drom_read_uid_only(sw, &uid);
653 tb_sw_warn(sw, "uid read failed\n");
656 if (sw->uid != uid) {
658 "changed while suspended (uid %#llx -> %#llx)\n",
664 /* upload configuration */
665 err = tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3);
669 err = tb_plug_events_active(sw, true);
673 /* check for surviving downstream switches */
674 for (i = 1; i <= sw->config.max_port_number; i++) {
675 struct tb_port *port = &sw->ports[i];
676 if (tb_is_upstream_port(port))
680 if (tb_wait_for_port(port, true) <= 0
681 || tb_switch_resume(port->remote->sw)) {
683 "lost during suspend, disconnecting\n");
684 tb_sw_set_unplugged(port->remote->sw);
690 void tb_switch_suspend(struct tb_switch *sw)
693 err = tb_plug_events_active(sw, false);
697 for (i = 1; i <= sw->config.max_port_number; i++) {
698 if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
699 tb_switch_suspend(sw->ports[i].remote->sw);
702 * TODO: invoke tb_cfg_prepare_to_sleep here? does not seem to have any