switch.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * Thunderbolt Cactus Ridge driver - switch/port utility functions
  3. *
  4. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/slab.h>
  8. #include "tb.h"
  9. /* port utility functions */
  10. static const char *tb_port_type(struct tb_regs_port_header *port)
  11. {
  12. switch (port->type >> 16) {
  13. case 0:
  14. switch ((u8) port->type) {
  15. case 0:
  16. return "Inactive";
  17. case 1:
  18. return "Port";
  19. case 2:
  20. return "NHI";
  21. default:
  22. return "unknown";
  23. }
  24. case 0x2:
  25. return "Ethernet";
  26. case 0x8:
  27. return "SATA";
  28. case 0xe:
  29. return "DP/HDMI";
  30. case 0x10:
  31. return "PCIe";
  32. case 0x20:
  33. return "USB";
  34. default:
  35. return "unknown";
  36. }
  37. }
  38. static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port)
  39. {
  40. tb_info(tb,
  41. " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n",
  42. port->port_number, port->vendor_id, port->device_id,
  43. port->revision, port->thunderbolt_version, tb_port_type(port),
  44. port->type);
  45. tb_info(tb, " Max hop id (in/out): %d/%d\n",
  46. port->max_in_hop_id, port->max_out_hop_id);
  47. tb_info(tb, " Max counters: %d\n", port->max_counters);
  48. tb_info(tb, " NFC Credits: %#x\n", port->nfc_credits);
  49. }
  50. /**
  51. * tb_port_state() - get connectedness state of a port
  52. *
  53. * The port must have a TB_CAP_PHY (i.e. it should be a real port).
  54. *
  55. * Return: Returns an enum tb_port_state on success or an error code on failure.
  56. */
  57. static int tb_port_state(struct tb_port *port)
  58. {
  59. struct tb_cap_phy phy;
  60. int res;
  61. if (port->cap_phy == 0) {
  62. tb_port_WARN(port, "does not have a PHY\n");
  63. return -EINVAL;
  64. }
  65. res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2);
  66. if (res)
  67. return res;
  68. return phy.state;
  69. }
  70. /**
  71. * tb_wait_for_port() - wait for a port to become ready
  72. *
  73. * Wait up to 1 second for a port to reach state TB_PORT_UP. If
  74. * wait_if_unplugged is set then we also wait if the port is in state
  75. * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
  76. * switch resume). Otherwise we only wait if a device is registered but the link
  77. * has not yet been established.
  78. *
  79. * Return: Returns an error code on failure. Returns 0 if the port is not
  80. * connected or failed to reach state TB_PORT_UP within one second. Returns 1
  81. * if the port is connected and in state TB_PORT_UP.
  82. */
  83. int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
  84. {
  85. int retries = 10;
  86. int state;
  87. if (!port->cap_phy) {
  88. tb_port_WARN(port, "does not have PHY\n");
  89. return -EINVAL;
  90. }
  91. if (tb_is_upstream_port(port)) {
  92. tb_port_WARN(port, "is the upstream port\n");
  93. return -EINVAL;
  94. }
  95. while (retries--) {
  96. state = tb_port_state(port);
  97. if (state < 0)
  98. return state;
  99. if (state == TB_PORT_DISABLED) {
  100. tb_port_info(port, "is disabled (state: 0)\n");
  101. return 0;
  102. }
  103. if (state == TB_PORT_UNPLUGGED) {
  104. if (wait_if_unplugged) {
  105. /* used during resume */
  106. tb_port_info(port,
  107. "is unplugged (state: 7), retrying...\n");
  108. msleep(100);
  109. continue;
  110. }
  111. tb_port_info(port, "is unplugged (state: 7)\n");
  112. return 0;
  113. }
  114. if (state == TB_PORT_UP) {
  115. tb_port_info(port,
  116. "is connected, link is up (state: 2)\n");
  117. return 1;
  118. }
  119. /*
  120. * After plug-in the state is TB_PORT_CONNECTING. Give it some
  121. * time.
  122. */
  123. tb_port_info(port,
  124. "is connected, link is not up (state: %d), retrying...\n",
  125. state);
  126. msleep(100);
  127. }
  128. tb_port_warn(port,
  129. "failed to reach state TB_PORT_UP. Ignoring port...\n");
  130. return 0;
  131. }
  132. /**
  133. * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
  134. *
  135. * Change the number of NFC credits allocated to @port by @credits. To remove
  136. * NFC credits pass a negative amount of credits.
  137. *
  138. * Return: Returns 0 on success or an error code on failure.
  139. */
  140. int tb_port_add_nfc_credits(struct tb_port *port, int credits)
  141. {
  142. if (credits == 0)
  143. return 0;
  144. tb_port_info(port,
  145. "adding %#x NFC credits (%#x -> %#x)",
  146. credits,
  147. port->config.nfc_credits,
  148. port->config.nfc_credits + credits);
  149. port->config.nfc_credits += credits;
  150. return tb_port_write(port, &port->config.nfc_credits,
  151. TB_CFG_PORT, 4, 1);
  152. }
  153. /**
  154. * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
  155. *
  156. * Return: Returns 0 on success or an error code on failure.
  157. */
  158. int tb_port_clear_counter(struct tb_port *port, int counter)
  159. {
  160. u32 zero[3] = { 0, 0, 0 };
  161. tb_port_info(port, "clearing counter %d\n", counter);
  162. return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
  163. }
  164. /**
  165. * tb_init_port() - initialize a port
  166. *
  167. * This is a helper method for tb_switch_alloc. Does not check or initialize
  168. * any downstream switches.
  169. *
  170. * Return: Returns 0 on success or an error code on failure.
  171. */
  172. static int tb_init_port(struct tb_port *port)
  173. {
  174. int res;
  175. int cap;
  176. res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8);
  177. if (res)
  178. return res;
  179. /* Port 0 is the switch itself and has no PHY. */
  180. if (port->config.type == TB_TYPE_PORT && port->port != 0) {
  181. cap = tb_find_cap(port, TB_CFG_PORT, TB_CAP_PHY);
  182. if (cap > 0)
  183. port->cap_phy = cap;
  184. else
  185. tb_port_WARN(port, "non switch port without a PHY\n");
  186. }
  187. tb_dump_port(port->sw->tb, &port->config);
  188. /* TODO: Read dual link port, DP port and more from EEPROM. */
  189. return 0;
  190. }
  191. /* switch utility functions */
  192. static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw)
  193. {
  194. tb_info(tb,
  195. " Switch: %x:%x (Revision: %d, TB Version: %d)\n",
  196. sw->vendor_id, sw->device_id, sw->revision,
  197. sw->thunderbolt_version);
  198. tb_info(tb, " Max Port Number: %d\n", sw->max_port_number);
  199. tb_info(tb, " Config:\n");
  200. tb_info(tb,
  201. " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
  202. sw->upstream_port_number, sw->depth,
  203. (((u64) sw->route_hi) << 32) | sw->route_lo,
  204. sw->enabled, sw->plug_events_delay);
  205. tb_info(tb,
  206. " unknown1: %#x unknown4: %#x\n",
  207. sw->__unknown1, sw->__unknown4);
  208. }
  209. /**
  210. * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
  211. *
  212. * Return: Returns 0 on success or an error code on failure.
  213. */
  214. int tb_switch_reset(struct tb *tb, u64 route)
  215. {
  216. struct tb_cfg_result res;
  217. struct tb_regs_switch_header header = {
  218. header.route_hi = route >> 32,
  219. header.route_lo = route,
  220. header.enabled = true,
  221. };
  222. tb_info(tb, "resetting switch at %llx\n", route);
  223. res.err = tb_cfg_write(tb->ctl, ((u32 *) &header) + 2, route,
  224. 0, 2, 2, 2);
  225. if (res.err)
  226. return res.err;
  227. res = tb_cfg_reset(tb->ctl, route, TB_CFG_DEFAULT_TIMEOUT);
  228. if (res.err > 0)
  229. return -EIO;
  230. return res.err;
  231. }
  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 NULL;
  242. if (tb_is_upstream_port(&sw->ports[next_port]))
  243. return NULL;
  244. if (!sw->ports[next_port].remote)
  245. return NULL;
  246. return get_switch_at_route(sw->ports[next_port].remote->sw,
  247. route >> TB_ROUTE_SHIFT);
  248. }
  249. /**
  250. * tb_plug_events_active() - enable/disable plug events on a switch
  251. *
  252. * Also configures a sane plug_events_delay of 255ms.
  253. *
  254. * Return: Returns 0 on success or an error code on failure.
  255. */
  256. static int tb_plug_events_active(struct tb_switch *sw, bool active)
  257. {
  258. u32 data;
  259. int res;
  260. sw->config.plug_events_delay = 0xff;
  261. res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1);
  262. if (res)
  263. return res;
  264. res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
  265. if (res)
  266. return res;
  267. if (active) {
  268. data = data & 0xFFFFFF83;
  269. switch (sw->config.device_id) {
  270. case 0x1513:
  271. case 0x151a:
  272. case 0x1549:
  273. break;
  274. default:
  275. data |= 4;
  276. }
  277. } else {
  278. data = data | 0x7c;
  279. }
  280. return tb_sw_write(sw, &data, TB_CFG_SWITCH,
  281. sw->cap_plug_events + 1, 1);
  282. }
  283. /**
  284. * tb_switch_free() - free a tb_switch and all downstream switches
  285. */
  286. void tb_switch_free(struct tb_switch *sw)
  287. {
  288. int i;
  289. /* port 0 is the switch itself and never has a remote */
  290. for (i = 1; i <= sw->config.max_port_number; i++) {
  291. if (tb_is_upstream_port(&sw->ports[i]))
  292. continue;
  293. if (sw->ports[i].remote)
  294. tb_switch_free(sw->ports[i].remote->sw);
  295. sw->ports[i].remote = NULL;
  296. }
  297. if (!sw->is_unplugged)
  298. tb_plug_events_active(sw, false);
  299. kfree(sw->ports);
  300. kfree(sw->drom);
  301. kfree(sw);
  302. }
  303. /**
  304. * tb_switch_alloc() - allocate and initialize a switch
  305. *
  306. * Return: Returns a NULL on failure.
  307. */
  308. struct tb_switch *tb_switch_alloc(struct tb *tb, u64 route)
  309. {
  310. int i;
  311. int cap;
  312. struct tb_switch *sw;
  313. int upstream_port = tb_cfg_get_upstream_port(tb->ctl, route);
  314. if (upstream_port < 0)
  315. return NULL;
  316. sw = kzalloc(sizeof(*sw), GFP_KERNEL);
  317. if (!sw)
  318. return NULL;
  319. sw->tb = tb;
  320. if (tb_cfg_read(tb->ctl, &sw->config, route, 0, 2, 0, 5))
  321. goto err;
  322. tb_info(tb,
  323. "initializing Switch at %#llx (depth: %d, up port: %d)\n",
  324. route, tb_route_length(route), upstream_port);
  325. tb_info(tb, "old switch config:\n");
  326. tb_dump_switch(tb, &sw->config);
  327. /* configure switch */
  328. sw->config.upstream_port_number = upstream_port;
  329. sw->config.depth = tb_route_length(route);
  330. sw->config.route_lo = route;
  331. sw->config.route_hi = route >> 32;
  332. sw->config.enabled = 1;
  333. /* from here on we may use the tb_sw_* functions & macros */
  334. if (sw->config.vendor_id != 0x8086)
  335. tb_sw_warn(sw, "unknown switch vendor id %#x\n",
  336. sw->config.vendor_id);
  337. if (sw->config.device_id != 0x1547 && sw->config.device_id != 0x1549)
  338. tb_sw_warn(sw, "unsupported switch device id %#x\n",
  339. sw->config.device_id);
  340. /* upload configuration */
  341. if (tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3))
  342. goto err;
  343. /* initialize ports */
  344. sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports),
  345. GFP_KERNEL);
  346. if (!sw->ports)
  347. goto err;
  348. for (i = 0; i <= sw->config.max_port_number; i++) {
  349. /* minimum setup for tb_find_cap and tb_drom_read to work */
  350. sw->ports[i].sw = sw;
  351. sw->ports[i].port = i;
  352. }
  353. cap = tb_find_cap(&sw->ports[0], TB_CFG_SWITCH, TB_CAP_PLUG_EVENTS);
  354. if (cap < 0) {
  355. tb_sw_warn(sw, "cannot find TB_CAP_PLUG_EVENTS aborting\n");
  356. goto err;
  357. }
  358. sw->cap_plug_events = cap;
  359. /* read drom */
  360. if (tb_drom_read(sw))
  361. tb_sw_warn(sw, "tb_eeprom_read_rom failed, continuing\n");
  362. tb_sw_info(sw, "uid: %#llx\n", sw->uid);
  363. for (i = 0; i <= sw->config.max_port_number; i++) {
  364. if (sw->ports[i].disabled) {
  365. tb_port_info(&sw->ports[i], "disabled by eeprom\n");
  366. continue;
  367. }
  368. if (tb_init_port(&sw->ports[i]))
  369. goto err;
  370. }
  371. /* TODO: I2C, IECS, link controller */
  372. if (tb_plug_events_active(sw, true))
  373. goto err;
  374. return sw;
  375. err:
  376. kfree(sw->ports);
  377. kfree(sw->drom);
  378. kfree(sw);
  379. return NULL;
  380. }
  381. /**
  382. * tb_sw_set_unpplugged() - set is_unplugged on switch and downstream switches
  383. */
  384. void tb_sw_set_unpplugged(struct tb_switch *sw)
  385. {
  386. int i;
  387. if (sw == sw->tb->root_switch) {
  388. tb_sw_WARN(sw, "cannot unplug root switch\n");
  389. return;
  390. }
  391. if (sw->is_unplugged) {
  392. tb_sw_WARN(sw, "is_unplugged already set\n");
  393. return;
  394. }
  395. sw->is_unplugged = true;
  396. for (i = 0; i <= sw->config.max_port_number; i++) {
  397. if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
  398. tb_sw_set_unpplugged(sw->ports[i].remote->sw);
  399. }
  400. }
  401. int tb_switch_resume(struct tb_switch *sw)
  402. {
  403. int i, err;
  404. u64 uid;
  405. tb_sw_info(sw, "resuming switch\n");
  406. err = tb_drom_read_uid_only(sw, &uid);
  407. if (err) {
  408. tb_sw_warn(sw, "uid read failed\n");
  409. return err;
  410. }
  411. if (sw->uid != uid) {
  412. tb_sw_info(sw,
  413. "changed while suspended (uid %#llx -> %#llx)\n",
  414. sw->uid, uid);
  415. return -ENODEV;
  416. }
  417. /* upload configuration */
  418. err = tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3);
  419. if (err)
  420. return err;
  421. err = tb_plug_events_active(sw, true);
  422. if (err)
  423. return err;
  424. /* check for surviving downstream switches */
  425. for (i = 1; i <= sw->config.max_port_number; i++) {
  426. struct tb_port *port = &sw->ports[i];
  427. if (tb_is_upstream_port(port))
  428. continue;
  429. if (!port->remote)
  430. continue;
  431. if (tb_wait_for_port(port, true) <= 0
  432. || tb_switch_resume(port->remote->sw)) {
  433. tb_port_warn(port,
  434. "lost during suspend, disconnecting\n");
  435. tb_sw_set_unpplugged(port->remote->sw);
  436. }
  437. }
  438. return 0;
  439. }
  440. void tb_switch_suspend(struct tb_switch *sw)
  441. {
  442. int i, err;
  443. err = tb_plug_events_active(sw, false);
  444. if (err)
  445. return;
  446. for (i = 1; i <= sw->config.max_port_number; i++) {
  447. if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
  448. tb_switch_suspend(sw->ports[i].remote->sw);
  449. }
  450. /*
  451. * TODO: invoke tb_cfg_prepare_to_sleep here? does not seem to have any
  452. * effect?
  453. */
  454. }