Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 13 | static 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 | |
| 42 | static 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 | /** |
| 56 | * tb_init_port() - initialize a port |
| 57 | * |
| 58 | * This is a helper method for tb_switch_alloc. Does not check or initialize |
| 59 | * any downstream switches. |
| 60 | * |
| 61 | * Return: Returns 0 on success or an error code on failure. |
| 62 | */ |
| 63 | static int tb_init_port(struct tb_switch *sw, u8 port_nr) |
| 64 | { |
| 65 | int res; |
| 66 | struct tb_port *port = &sw->ports[port_nr]; |
| 67 | port->sw = sw; |
| 68 | port->port = port_nr; |
| 69 | port->remote = NULL; |
| 70 | res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8); |
| 71 | if (res) |
| 72 | return res; |
| 73 | |
| 74 | tb_dump_port(sw->tb, &port->config); |
| 75 | |
| 76 | /* TODO: Read dual link port, DP port and more from EEPROM. */ |
| 77 | return 0; |
| 78 | |
| 79 | } |
| 80 | |
| 81 | /* switch utility functions */ |
| 82 | |
| 83 | static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw) |
| 84 | { |
| 85 | tb_info(tb, |
| 86 | " Switch: %x:%x (Revision: %d, TB Version: %d)\n", |
| 87 | sw->vendor_id, sw->device_id, sw->revision, |
| 88 | sw->thunderbolt_version); |
| 89 | tb_info(tb, " Max Port Number: %d\n", sw->max_port_number); |
| 90 | tb_info(tb, " Config:\n"); |
| 91 | tb_info(tb, |
| 92 | " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n", |
| 93 | sw->upstream_port_number, sw->depth, |
| 94 | (((u64) sw->route_hi) << 32) | sw->route_lo, |
| 95 | sw->enabled, sw->plug_events_delay); |
| 96 | tb_info(tb, |
| 97 | " unknown1: %#x unknown4: %#x\n", |
| 98 | sw->__unknown1, sw->__unknown4); |
| 99 | } |
| 100 | |
| 101 | /** |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame^] | 102 | * tb_plug_events_active() - enable/disable plug events on a switch |
| 103 | * |
| 104 | * Also configures a sane plug_events_delay of 255ms. |
| 105 | * |
| 106 | * Return: Returns 0 on success or an error code on failure. |
| 107 | */ |
| 108 | static int tb_plug_events_active(struct tb_switch *sw, bool active) |
| 109 | { |
| 110 | u32 data; |
| 111 | int res; |
| 112 | |
| 113 | sw->config.plug_events_delay = 0xff; |
| 114 | res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1); |
| 115 | if (res) |
| 116 | return res; |
| 117 | |
| 118 | res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1); |
| 119 | if (res) |
| 120 | return res; |
| 121 | |
| 122 | if (active) { |
| 123 | data = data & 0xFFFFFF83; |
| 124 | switch (sw->config.device_id) { |
| 125 | case 0x1513: |
| 126 | case 0x151a: |
| 127 | case 0x1549: |
| 128 | break; |
| 129 | default: |
| 130 | data |= 4; |
| 131 | } |
| 132 | } else { |
| 133 | data = data | 0x7c; |
| 134 | } |
| 135 | return tb_sw_write(sw, &data, TB_CFG_SWITCH, |
| 136 | sw->cap_plug_events + 1, 1); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | /** |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 141 | * tb_switch_free() - free a tb_switch and all downstream switches |
| 142 | */ |
| 143 | void tb_switch_free(struct tb_switch *sw) |
| 144 | { |
| 145 | int i; |
| 146 | /* port 0 is the switch itself and never has a remote */ |
| 147 | for (i = 1; i <= sw->config.max_port_number; i++) { |
| 148 | if (tb_is_upstream_port(&sw->ports[i])) |
| 149 | continue; |
| 150 | if (sw->ports[i].remote) |
| 151 | tb_switch_free(sw->ports[i].remote->sw); |
| 152 | sw->ports[i].remote = NULL; |
| 153 | } |
| 154 | |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame^] | 155 | tb_plug_events_active(sw, false); |
| 156 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 157 | kfree(sw->ports); |
| 158 | kfree(sw); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * tb_switch_alloc() - allocate and initialize a switch |
| 163 | * |
| 164 | * Return: Returns a NULL on failure. |
| 165 | */ |
| 166 | struct tb_switch *tb_switch_alloc(struct tb *tb, u64 route) |
| 167 | { |
| 168 | int i; |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame^] | 169 | int cap; |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 170 | struct tb_switch *sw; |
| 171 | int upstream_port = tb_cfg_get_upstream_port(tb->ctl, route); |
| 172 | if (upstream_port < 0) |
| 173 | return NULL; |
| 174 | |
| 175 | sw = kzalloc(sizeof(*sw), GFP_KERNEL); |
| 176 | if (!sw) |
| 177 | return NULL; |
| 178 | |
| 179 | sw->tb = tb; |
| 180 | if (tb_cfg_read(tb->ctl, &sw->config, route, 0, 2, 0, 5)) |
| 181 | goto err; |
| 182 | tb_info(tb, |
| 183 | "initializing Switch at %#llx (depth: %d, up port: %d)\n", |
| 184 | route, tb_route_length(route), upstream_port); |
| 185 | tb_info(tb, "old switch config:\n"); |
| 186 | tb_dump_switch(tb, &sw->config); |
| 187 | |
| 188 | /* configure switch */ |
| 189 | sw->config.upstream_port_number = upstream_port; |
| 190 | sw->config.depth = tb_route_length(route); |
| 191 | sw->config.route_lo = route; |
| 192 | sw->config.route_hi = route >> 32; |
| 193 | sw->config.enabled = 1; |
| 194 | /* from here on we may use the tb_sw_* functions & macros */ |
| 195 | |
| 196 | if (sw->config.vendor_id != 0x8086) |
| 197 | tb_sw_warn(sw, "unknown switch vendor id %#x\n", |
| 198 | sw->config.vendor_id); |
| 199 | |
| 200 | if (sw->config.device_id != 0x1547 && sw->config.device_id != 0x1549) |
| 201 | tb_sw_warn(sw, "unsupported switch device id %#x\n", |
| 202 | sw->config.device_id); |
| 203 | |
| 204 | /* upload configuration */ |
| 205 | if (tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3)) |
| 206 | goto err; |
| 207 | |
| 208 | /* initialize ports */ |
| 209 | sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports), |
| 210 | GFP_KERNEL); |
| 211 | if (!sw->ports) |
| 212 | goto err; |
| 213 | |
| 214 | for (i = 0; i <= sw->config.max_port_number; i++) { |
| 215 | if (tb_init_port(sw, i)) |
| 216 | goto err; |
| 217 | /* TODO: check if port is disabled (EEPROM) */ |
| 218 | } |
| 219 | |
| 220 | /* TODO: I2C, IECS, EEPROM, link controller */ |
| 221 | |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame^] | 222 | cap = tb_find_cap(&sw->ports[0], TB_CFG_SWITCH, TB_CAP_PLUG_EVENTS); |
| 223 | if (cap < 0) { |
| 224 | tb_sw_warn(sw, "cannot find TB_CAP_PLUG_EVENTS aborting\n"); |
| 225 | goto err; |
| 226 | } |
| 227 | sw->cap_plug_events = cap; |
| 228 | |
| 229 | if (tb_plug_events_active(sw, true)) |
| 230 | goto err; |
| 231 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 232 | return sw; |
| 233 | err: |
| 234 | kfree(sw->ports); |
| 235 | kfree(sw); |
| 236 | return NULL; |
| 237 | } |
| 238 | |