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 | /** |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 56 | * 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 | */ |
| 62 | static 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 | */ |
| 89 | int 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 Noever | 520b670 | 2014-06-03 22:04:07 +0200 | [diff] [blame^] | 142 | * 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 | */ |
| 149 | int 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 | */ |
| 168 | int 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 Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 176 | * 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 | */ |
| 183 | static int tb_init_port(struct tb_switch *sw, u8 port_nr) |
| 184 | { |
| 185 | int res; |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 186 | int cap; |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 187 | struct tb_port *port = &sw->ports[port_nr]; |
| 188 | port->sw = sw; |
| 189 | port->port = port_nr; |
| 190 | port->remote = NULL; |
| 191 | res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8); |
| 192 | if (res) |
| 193 | return res; |
| 194 | |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 195 | /* Port 0 is the switch itself and has no PHY. */ |
| 196 | if (port->config.type == TB_TYPE_PORT && port_nr != 0) { |
| 197 | cap = tb_find_cap(port, TB_CFG_PORT, TB_CAP_PHY); |
| 198 | |
| 199 | if (cap > 0) |
| 200 | port->cap_phy = cap; |
| 201 | else |
| 202 | tb_port_WARN(port, "non switch port without a PHY\n"); |
| 203 | } |
| 204 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 205 | tb_dump_port(sw->tb, &port->config); |
| 206 | |
| 207 | /* TODO: Read dual link port, DP port and more from EEPROM. */ |
| 208 | return 0; |
| 209 | |
| 210 | } |
| 211 | |
| 212 | /* switch utility functions */ |
| 213 | |
| 214 | static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw) |
| 215 | { |
| 216 | tb_info(tb, |
| 217 | " Switch: %x:%x (Revision: %d, TB Version: %d)\n", |
| 218 | sw->vendor_id, sw->device_id, sw->revision, |
| 219 | sw->thunderbolt_version); |
| 220 | tb_info(tb, " Max Port Number: %d\n", sw->max_port_number); |
| 221 | tb_info(tb, " Config:\n"); |
| 222 | tb_info(tb, |
| 223 | " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n", |
| 224 | sw->upstream_port_number, sw->depth, |
| 225 | (((u64) sw->route_hi) << 32) | sw->route_lo, |
| 226 | sw->enabled, sw->plug_events_delay); |
| 227 | tb_info(tb, |
| 228 | " unknown1: %#x unknown4: %#x\n", |
| 229 | sw->__unknown1, sw->__unknown4); |
| 230 | } |
| 231 | |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 232 | struct tb_switch *get_switch_at_route(struct tb_switch *sw, u64 route) |
| 233 | { |
| 234 | u8 next_port = route; /* |
| 235 | * Routes use a stride of 8 bits, |
| 236 | * eventhough a port index has 6 bits at most. |
| 237 | * */ |
| 238 | if (route == 0) |
| 239 | return sw; |
| 240 | if (next_port > sw->config.max_port_number) |
| 241 | return 0; |
| 242 | if (tb_is_upstream_port(&sw->ports[next_port])) |
| 243 | return 0; |
| 244 | if (!sw->ports[next_port].remote) |
| 245 | return 0; |
| 246 | return get_switch_at_route(sw->ports[next_port].remote->sw, |
| 247 | route >> TB_ROUTE_SHIFT); |
| 248 | } |
| 249 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 250 | /** |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame] | 251 | * tb_plug_events_active() - enable/disable plug events on a switch |
| 252 | * |
| 253 | * Also configures a sane plug_events_delay of 255ms. |
| 254 | * |
| 255 | * Return: Returns 0 on success or an error code on failure. |
| 256 | */ |
| 257 | static int tb_plug_events_active(struct tb_switch *sw, bool active) |
| 258 | { |
| 259 | u32 data; |
| 260 | int res; |
| 261 | |
| 262 | sw->config.plug_events_delay = 0xff; |
| 263 | res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1); |
| 264 | if (res) |
| 265 | return res; |
| 266 | |
| 267 | res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1); |
| 268 | if (res) |
| 269 | return res; |
| 270 | |
| 271 | if (active) { |
| 272 | data = data & 0xFFFFFF83; |
| 273 | switch (sw->config.device_id) { |
| 274 | case 0x1513: |
| 275 | case 0x151a: |
| 276 | case 0x1549: |
| 277 | break; |
| 278 | default: |
| 279 | data |= 4; |
| 280 | } |
| 281 | } else { |
| 282 | data = data | 0x7c; |
| 283 | } |
| 284 | return tb_sw_write(sw, &data, TB_CFG_SWITCH, |
| 285 | sw->cap_plug_events + 1, 1); |
| 286 | } |
| 287 | |
| 288 | |
| 289 | /** |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 290 | * tb_switch_free() - free a tb_switch and all downstream switches |
| 291 | */ |
| 292 | void tb_switch_free(struct tb_switch *sw) |
| 293 | { |
| 294 | int i; |
| 295 | /* port 0 is the switch itself and never has a remote */ |
| 296 | for (i = 1; i <= sw->config.max_port_number; i++) { |
| 297 | if (tb_is_upstream_port(&sw->ports[i])) |
| 298 | continue; |
| 299 | if (sw->ports[i].remote) |
| 300 | tb_switch_free(sw->ports[i].remote->sw); |
| 301 | sw->ports[i].remote = NULL; |
| 302 | } |
| 303 | |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 304 | if (!sw->is_unplugged) |
| 305 | tb_plug_events_active(sw, false); |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame] | 306 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 307 | kfree(sw->ports); |
| 308 | kfree(sw); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * tb_switch_alloc() - allocate and initialize a switch |
| 313 | * |
| 314 | * Return: Returns a NULL on failure. |
| 315 | */ |
| 316 | struct tb_switch *tb_switch_alloc(struct tb *tb, u64 route) |
| 317 | { |
| 318 | int i; |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame] | 319 | int cap; |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 320 | struct tb_switch *sw; |
| 321 | int upstream_port = tb_cfg_get_upstream_port(tb->ctl, route); |
| 322 | if (upstream_port < 0) |
| 323 | return NULL; |
| 324 | |
| 325 | sw = kzalloc(sizeof(*sw), GFP_KERNEL); |
| 326 | if (!sw) |
| 327 | return NULL; |
| 328 | |
| 329 | sw->tb = tb; |
| 330 | if (tb_cfg_read(tb->ctl, &sw->config, route, 0, 2, 0, 5)) |
| 331 | goto err; |
| 332 | tb_info(tb, |
| 333 | "initializing Switch at %#llx (depth: %d, up port: %d)\n", |
| 334 | route, tb_route_length(route), upstream_port); |
| 335 | tb_info(tb, "old switch config:\n"); |
| 336 | tb_dump_switch(tb, &sw->config); |
| 337 | |
| 338 | /* configure switch */ |
| 339 | sw->config.upstream_port_number = upstream_port; |
| 340 | sw->config.depth = tb_route_length(route); |
| 341 | sw->config.route_lo = route; |
| 342 | sw->config.route_hi = route >> 32; |
| 343 | sw->config.enabled = 1; |
| 344 | /* from here on we may use the tb_sw_* functions & macros */ |
| 345 | |
| 346 | if (sw->config.vendor_id != 0x8086) |
| 347 | tb_sw_warn(sw, "unknown switch vendor id %#x\n", |
| 348 | sw->config.vendor_id); |
| 349 | |
| 350 | if (sw->config.device_id != 0x1547 && sw->config.device_id != 0x1549) |
| 351 | tb_sw_warn(sw, "unsupported switch device id %#x\n", |
| 352 | sw->config.device_id); |
| 353 | |
| 354 | /* upload configuration */ |
| 355 | if (tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3)) |
| 356 | goto err; |
| 357 | |
| 358 | /* initialize ports */ |
| 359 | sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports), |
| 360 | GFP_KERNEL); |
| 361 | if (!sw->ports) |
| 362 | goto err; |
| 363 | |
| 364 | for (i = 0; i <= sw->config.max_port_number; i++) { |
| 365 | if (tb_init_port(sw, i)) |
| 366 | goto err; |
| 367 | /* TODO: check if port is disabled (EEPROM) */ |
| 368 | } |
| 369 | |
| 370 | /* TODO: I2C, IECS, EEPROM, link controller */ |
| 371 | |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame] | 372 | cap = tb_find_cap(&sw->ports[0], TB_CFG_SWITCH, TB_CAP_PLUG_EVENTS); |
| 373 | if (cap < 0) { |
| 374 | tb_sw_warn(sw, "cannot find TB_CAP_PLUG_EVENTS aborting\n"); |
| 375 | goto err; |
| 376 | } |
| 377 | sw->cap_plug_events = cap; |
| 378 | |
| 379 | if (tb_plug_events_active(sw, true)) |
| 380 | goto err; |
| 381 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 382 | return sw; |
| 383 | err: |
| 384 | kfree(sw->ports); |
| 385 | kfree(sw); |
| 386 | return NULL; |
| 387 | } |
| 388 | |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 389 | /** |
| 390 | * tb_sw_set_unpplugged() - set is_unplugged on switch and downstream switches |
| 391 | */ |
| 392 | void tb_sw_set_unpplugged(struct tb_switch *sw) |
| 393 | { |
| 394 | int i; |
| 395 | if (sw == sw->tb->root_switch) { |
| 396 | tb_sw_WARN(sw, "cannot unplug root switch\n"); |
| 397 | return; |
| 398 | } |
| 399 | if (sw->is_unplugged) { |
| 400 | tb_sw_WARN(sw, "is_unplugged already set\n"); |
| 401 | return; |
| 402 | } |
| 403 | sw->is_unplugged = true; |
| 404 | for (i = 0; i <= sw->config.max_port_number; i++) { |
| 405 | if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote) |
| 406 | tb_sw_set_unpplugged(sw->ports[i].remote->sw); |
| 407 | } |
| 408 | } |
| 409 | |