tb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Thunderbolt Cactus Ridge driver - bus logic (NHI independent)
  3. *
  4. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/errno.h>
  8. #include <linux/delay.h>
  9. #include "tb.h"
  10. #include "tb_regs.h"
  11. #include "tunnel_pci.h"
  12. /* enumeration & hot plug handling */
  13. static void tb_scan_port(struct tb_port *port);
  14. /**
  15. * tb_scan_switch() - scan for and initialize downstream switches
  16. */
  17. static void tb_scan_switch(struct tb_switch *sw)
  18. {
  19. int i;
  20. for (i = 1; i <= sw->config.max_port_number; i++)
  21. tb_scan_port(&sw->ports[i]);
  22. }
  23. /**
  24. * tb_scan_port() - check for and initialize switches below port
  25. */
  26. static void tb_scan_port(struct tb_port *port)
  27. {
  28. struct tb_switch *sw;
  29. if (tb_is_upstream_port(port))
  30. return;
  31. if (port->config.type != TB_TYPE_PORT)
  32. return;
  33. if (port->dual_link_port && port->link_nr)
  34. return; /*
  35. * Downstream switch is reachable through two ports.
  36. * Only scan on the primary port (link_nr == 0).
  37. */
  38. if (tb_wait_for_port(port, false) <= 0)
  39. return;
  40. if (port->remote) {
  41. tb_port_WARN(port, "port already has a remote!\n");
  42. return;
  43. }
  44. sw = tb_switch_alloc(port->sw->tb, tb_downstream_route(port));
  45. if (!sw)
  46. return;
  47. port->remote = tb_upstream_port(sw);
  48. tb_upstream_port(sw)->remote = port;
  49. tb_scan_switch(sw);
  50. }
  51. /**
  52. * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
  53. */
  54. static void tb_free_invalid_tunnels(struct tb *tb)
  55. {
  56. struct tb_pci_tunnel *tunnel;
  57. struct tb_pci_tunnel *n;
  58. list_for_each_entry_safe(tunnel, n, &tb->tunnel_list, list)
  59. {
  60. if (tb_pci_is_invalid(tunnel)) {
  61. tb_pci_deactivate(tunnel);
  62. tb_pci_free(tunnel);
  63. }
  64. }
  65. }
  66. /**
  67. * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
  68. */
  69. static void tb_free_unplugged_children(struct tb_switch *sw)
  70. {
  71. int i;
  72. for (i = 1; i <= sw->config.max_port_number; i++) {
  73. struct tb_port *port = &sw->ports[i];
  74. if (tb_is_upstream_port(port))
  75. continue;
  76. if (!port->remote)
  77. continue;
  78. if (port->remote->sw->is_unplugged) {
  79. tb_switch_free(port->remote->sw);
  80. port->remote = NULL;
  81. } else {
  82. tb_free_unplugged_children(port->remote->sw);
  83. }
  84. }
  85. }
  86. /**
  87. * find_pci_up_port() - return the first PCIe up port on @sw or NULL
  88. */
  89. static struct tb_port *tb_find_pci_up_port(struct tb_switch *sw)
  90. {
  91. int i;
  92. for (i = 1; i <= sw->config.max_port_number; i++)
  93. if (sw->ports[i].config.type == TB_TYPE_PCIE_UP)
  94. return &sw->ports[i];
  95. return NULL;
  96. }
  97. /**
  98. * find_unused_down_port() - return the first inactive PCIe down port on @sw
  99. */
  100. static struct tb_port *tb_find_unused_down_port(struct tb_switch *sw)
  101. {
  102. int i;
  103. int cap;
  104. int res;
  105. int data;
  106. for (i = 1; i <= sw->config.max_port_number; i++) {
  107. if (tb_is_upstream_port(&sw->ports[i]))
  108. continue;
  109. if (sw->ports[i].config.type != TB_TYPE_PCIE_DOWN)
  110. continue;
  111. cap = tb_find_cap(&sw->ports[i], TB_CFG_PORT, TB_CAP_PCIE);
  112. if (cap <= 0)
  113. continue;
  114. res = tb_port_read(&sw->ports[i], &data, TB_CFG_PORT, cap, 1);
  115. if (res < 0)
  116. continue;
  117. if (data & 0x80000000)
  118. continue;
  119. return &sw->ports[i];
  120. }
  121. return NULL;
  122. }
  123. /**
  124. * tb_activate_pcie_devices() - scan for and activate PCIe devices
  125. *
  126. * This method is somewhat ad hoc. For now it only supports one device
  127. * per port and only devices at depth 1.
  128. */
  129. static void tb_activate_pcie_devices(struct tb *tb)
  130. {
  131. int i;
  132. int cap;
  133. u32 data;
  134. struct tb_switch *sw;
  135. struct tb_port *up_port;
  136. struct tb_port *down_port;
  137. struct tb_pci_tunnel *tunnel;
  138. /* scan for pcie devices at depth 1*/
  139. for (i = 1; i <= tb->root_switch->config.max_port_number; i++) {
  140. if (tb_is_upstream_port(&tb->root_switch->ports[i]))
  141. continue;
  142. if (tb->root_switch->ports[i].config.type != TB_TYPE_PORT)
  143. continue;
  144. if (!tb->root_switch->ports[i].remote)
  145. continue;
  146. sw = tb->root_switch->ports[i].remote->sw;
  147. up_port = tb_find_pci_up_port(sw);
  148. if (!up_port) {
  149. tb_sw_info(sw, "no PCIe devices found, aborting\n");
  150. continue;
  151. }
  152. /* check whether port is already activated */
  153. cap = tb_find_cap(up_port, TB_CFG_PORT, TB_CAP_PCIE);
  154. if (cap <= 0)
  155. continue;
  156. if (tb_port_read(up_port, &data, TB_CFG_PORT, cap, 1))
  157. continue;
  158. if (data & 0x80000000) {
  159. tb_port_info(up_port,
  160. "PCIe port already activated, aborting\n");
  161. continue;
  162. }
  163. down_port = tb_find_unused_down_port(tb->root_switch);
  164. if (!down_port) {
  165. tb_port_info(up_port,
  166. "All PCIe down ports are occupied, aborting\n");
  167. continue;
  168. }
  169. tunnel = tb_pci_alloc(tb, up_port, down_port);
  170. if (!tunnel) {
  171. tb_port_info(up_port,
  172. "PCIe tunnel allocation failed, aborting\n");
  173. continue;
  174. }
  175. if (tb_pci_activate(tunnel)) {
  176. tb_port_info(up_port,
  177. "PCIe tunnel activation failed, aborting\n");
  178. tb_pci_free(tunnel);
  179. }
  180. }
  181. }
  182. /* hotplug handling */
  183. struct tb_hotplug_event {
  184. struct work_struct work;
  185. struct tb *tb;
  186. u64 route;
  187. u8 port;
  188. bool unplug;
  189. };
  190. /**
  191. * tb_handle_hotplug() - handle hotplug event
  192. *
  193. * Executes on tb->wq.
  194. */
  195. static void tb_handle_hotplug(struct work_struct *work)
  196. {
  197. struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
  198. struct tb *tb = ev->tb;
  199. struct tb_switch *sw;
  200. struct tb_port *port;
  201. mutex_lock(&tb->lock);
  202. if (!tb->hotplug_active)
  203. goto out; /* during init, suspend or shutdown */
  204. sw = get_switch_at_route(tb->root_switch, ev->route);
  205. if (!sw) {
  206. tb_warn(tb,
  207. "hotplug event from non existent switch %llx:%x (unplug: %d)\n",
  208. ev->route, ev->port, ev->unplug);
  209. goto out;
  210. }
  211. if (ev->port > sw->config.max_port_number) {
  212. tb_warn(tb,
  213. "hotplug event from non existent port %llx:%x (unplug: %d)\n",
  214. ev->route, ev->port, ev->unplug);
  215. goto out;
  216. }
  217. port = &sw->ports[ev->port];
  218. if (tb_is_upstream_port(port)) {
  219. tb_warn(tb,
  220. "hotplug event for upstream port %llx:%x (unplug: %d)\n",
  221. ev->route, ev->port, ev->unplug);
  222. goto out;
  223. }
  224. if (ev->unplug) {
  225. if (port->remote) {
  226. tb_port_info(port, "unplugged\n");
  227. tb_sw_set_unplugged(port->remote->sw);
  228. tb_free_invalid_tunnels(tb);
  229. tb_switch_free(port->remote->sw);
  230. port->remote = NULL;
  231. } else {
  232. tb_port_info(port,
  233. "got unplug event for disconnected port, ignoring\n");
  234. }
  235. } else if (port->remote) {
  236. tb_port_info(port,
  237. "got plug event for connected port, ignoring\n");
  238. } else {
  239. tb_port_info(port, "hotplug: scanning\n");
  240. tb_scan_port(port);
  241. if (!port->remote) {
  242. tb_port_info(port, "hotplug: no switch found\n");
  243. } else if (port->remote->sw->config.depth > 1) {
  244. tb_sw_warn(port->remote->sw,
  245. "hotplug: chaining not supported\n");
  246. } else {
  247. tb_sw_info(port->remote->sw,
  248. "hotplug: activating pcie devices\n");
  249. tb_activate_pcie_devices(tb);
  250. }
  251. }
  252. out:
  253. mutex_unlock(&tb->lock);
  254. kfree(ev);
  255. }
  256. /**
  257. * tb_schedule_hotplug_handler() - callback function for the control channel
  258. *
  259. * Delegates to tb_handle_hotplug.
  260. */
  261. static void tb_schedule_hotplug_handler(void *data, u64 route, u8 port,
  262. bool unplug)
  263. {
  264. struct tb *tb = data;
  265. struct tb_hotplug_event *ev = kmalloc(sizeof(*ev), GFP_KERNEL);
  266. if (!ev)
  267. return;
  268. INIT_WORK(&ev->work, tb_handle_hotplug);
  269. ev->tb = tb;
  270. ev->route = route;
  271. ev->port = port;
  272. ev->unplug = unplug;
  273. queue_work(tb->wq, &ev->work);
  274. }
  275. /**
  276. * thunderbolt_shutdown_and_free() - shutdown everything
  277. *
  278. * Free all switches and the config channel.
  279. *
  280. * Used in the error path of thunderbolt_alloc_and_start.
  281. */
  282. void thunderbolt_shutdown_and_free(struct tb *tb)
  283. {
  284. struct tb_pci_tunnel *tunnel;
  285. struct tb_pci_tunnel *n;
  286. mutex_lock(&tb->lock);
  287. /* tunnels are only present after everything has been initialized */
  288. list_for_each_entry_safe(tunnel, n, &tb->tunnel_list, list) {
  289. tb_pci_deactivate(tunnel);
  290. tb_pci_free(tunnel);
  291. }
  292. if (tb->root_switch)
  293. tb_switch_free(tb->root_switch);
  294. tb->root_switch = NULL;
  295. if (tb->ctl) {
  296. tb_ctl_stop(tb->ctl);
  297. tb_ctl_free(tb->ctl);
  298. }
  299. tb->ctl = NULL;
  300. tb->hotplug_active = false; /* signal tb_handle_hotplug to quit */
  301. /* allow tb_handle_hotplug to acquire the lock */
  302. mutex_unlock(&tb->lock);
  303. if (tb->wq) {
  304. flush_workqueue(tb->wq);
  305. destroy_workqueue(tb->wq);
  306. tb->wq = NULL;
  307. }
  308. mutex_destroy(&tb->lock);
  309. kfree(tb);
  310. }
  311. /**
  312. * thunderbolt_alloc_and_start() - setup the thunderbolt bus
  313. *
  314. * Allocates a tb_cfg control channel, initializes the root switch, enables
  315. * plug events and activates pci devices.
  316. *
  317. * Return: Returns NULL on error.
  318. */
  319. struct tb *thunderbolt_alloc_and_start(struct tb_nhi *nhi)
  320. {
  321. struct tb *tb;
  322. BUILD_BUG_ON(sizeof(struct tb_regs_switch_header) != 5 * 4);
  323. BUILD_BUG_ON(sizeof(struct tb_regs_port_header) != 8 * 4);
  324. BUILD_BUG_ON(sizeof(struct tb_regs_hop) != 2 * 4);
  325. tb = kzalloc(sizeof(*tb), GFP_KERNEL);
  326. if (!tb)
  327. return NULL;
  328. tb->nhi = nhi;
  329. mutex_init(&tb->lock);
  330. mutex_lock(&tb->lock);
  331. INIT_LIST_HEAD(&tb->tunnel_list);
  332. tb->wq = alloc_ordered_workqueue("thunderbolt", 0);
  333. if (!tb->wq)
  334. goto err_locked;
  335. tb->ctl = tb_ctl_alloc(tb->nhi, tb_schedule_hotplug_handler, tb);
  336. if (!tb->ctl)
  337. goto err_locked;
  338. /*
  339. * tb_schedule_hotplug_handler may be called as soon as the config
  340. * channel is started. Thats why we have to hold the lock here.
  341. */
  342. tb_ctl_start(tb->ctl);
  343. tb->root_switch = tb_switch_alloc(tb, 0);
  344. if (!tb->root_switch)
  345. goto err_locked;
  346. /* Full scan to discover devices added before the driver was loaded. */
  347. tb_scan_switch(tb->root_switch);
  348. tb_activate_pcie_devices(tb);
  349. /* Allow tb_handle_hotplug to progress events */
  350. tb->hotplug_active = true;
  351. mutex_unlock(&tb->lock);
  352. return tb;
  353. err_locked:
  354. mutex_unlock(&tb->lock);
  355. thunderbolt_shutdown_and_free(tb);
  356. return NULL;
  357. }
  358. void thunderbolt_suspend(struct tb *tb)
  359. {
  360. tb_info(tb, "suspending...\n");
  361. mutex_lock(&tb->lock);
  362. tb_switch_suspend(tb->root_switch);
  363. tb_ctl_stop(tb->ctl);
  364. tb->hotplug_active = false; /* signal tb_handle_hotplug to quit */
  365. mutex_unlock(&tb->lock);
  366. tb_info(tb, "suspend finished\n");
  367. }
  368. void thunderbolt_resume(struct tb *tb)
  369. {
  370. struct tb_pci_tunnel *tunnel, *n;
  371. tb_info(tb, "resuming...\n");
  372. mutex_lock(&tb->lock);
  373. tb_ctl_start(tb->ctl);
  374. /* remove any pci devices the firmware might have setup */
  375. tb_switch_reset(tb, 0);
  376. tb_switch_resume(tb->root_switch);
  377. tb_free_invalid_tunnels(tb);
  378. tb_free_unplugged_children(tb->root_switch);
  379. list_for_each_entry_safe(tunnel, n, &tb->tunnel_list, list)
  380. tb_pci_restart(tunnel);
  381. if (!list_empty(&tb->tunnel_list)) {
  382. /*
  383. * the pcie links need some time to get going.
  384. * 100ms works for me...
  385. */
  386. tb_info(tb, "tunnels restarted, sleeping for 100ms\n");
  387. msleep(100);
  388. }
  389. /* Allow tb_handle_hotplug to progress events */
  390. tb->hotplug_active = true;
  391. mutex_unlock(&tb->lock);
  392. tb_info(tb, "resume finished\n");
  393. }