tb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 <linux/platform_data/x86/apple.h>
  10. #include "tb.h"
  11. #include "tb_regs.h"
  12. #include "tunnel_pci.h"
  13. /**
  14. * struct tb_cm - Simple Thunderbolt connection manager
  15. * @tunnel_list: List of active tunnels
  16. * @hotplug_active: tb_handle_hotplug will stop progressing plug
  17. * events and exit if this is not set (it needs to
  18. * acquire the lock one more time). Used to drain wq
  19. * after cfg has been paused.
  20. */
  21. struct tb_cm {
  22. struct list_head tunnel_list;
  23. bool hotplug_active;
  24. };
  25. /* enumeration & hot plug handling */
  26. static void tb_scan_port(struct tb_port *port);
  27. /**
  28. * tb_scan_switch() - scan for and initialize downstream switches
  29. */
  30. static void tb_scan_switch(struct tb_switch *sw)
  31. {
  32. int i;
  33. for (i = 1; i <= sw->config.max_port_number; i++)
  34. tb_scan_port(&sw->ports[i]);
  35. }
  36. /**
  37. * tb_scan_port() - check for and initialize switches below port
  38. */
  39. static void tb_scan_port(struct tb_port *port)
  40. {
  41. struct tb_switch *sw;
  42. if (tb_is_upstream_port(port))
  43. return;
  44. if (port->config.type != TB_TYPE_PORT)
  45. return;
  46. if (port->dual_link_port && port->link_nr)
  47. return; /*
  48. * Downstream switch is reachable through two ports.
  49. * Only scan on the primary port (link_nr == 0).
  50. */
  51. if (tb_wait_for_port(port, false) <= 0)
  52. return;
  53. if (port->remote) {
  54. tb_port_WARN(port, "port already has a remote!\n");
  55. return;
  56. }
  57. sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
  58. tb_downstream_route(port));
  59. if (!sw)
  60. return;
  61. if (tb_switch_configure(sw)) {
  62. tb_switch_put(sw);
  63. return;
  64. }
  65. sw->authorized = true;
  66. if (tb_switch_add(sw)) {
  67. tb_switch_put(sw);
  68. return;
  69. }
  70. port->remote = tb_upstream_port(sw);
  71. tb_upstream_port(sw)->remote = port;
  72. tb_scan_switch(sw);
  73. }
  74. /**
  75. * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
  76. */
  77. static void tb_free_invalid_tunnels(struct tb *tb)
  78. {
  79. struct tb_cm *tcm = tb_priv(tb);
  80. struct tb_pci_tunnel *tunnel;
  81. struct tb_pci_tunnel *n;
  82. list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
  83. if (tb_pci_is_invalid(tunnel)) {
  84. tb_pci_deactivate(tunnel);
  85. list_del(&tunnel->list);
  86. tb_pci_free(tunnel);
  87. }
  88. }
  89. }
  90. /**
  91. * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
  92. */
  93. static void tb_free_unplugged_children(struct tb_switch *sw)
  94. {
  95. int i;
  96. for (i = 1; i <= sw->config.max_port_number; i++) {
  97. struct tb_port *port = &sw->ports[i];
  98. if (tb_is_upstream_port(port))
  99. continue;
  100. if (!port->remote)
  101. continue;
  102. if (port->remote->sw->is_unplugged) {
  103. tb_switch_remove(port->remote->sw);
  104. port->remote = NULL;
  105. } else {
  106. tb_free_unplugged_children(port->remote->sw);
  107. }
  108. }
  109. }
  110. /**
  111. * find_pci_up_port() - return the first PCIe up port on @sw or NULL
  112. */
  113. static struct tb_port *tb_find_pci_up_port(struct tb_switch *sw)
  114. {
  115. int i;
  116. for (i = 1; i <= sw->config.max_port_number; i++)
  117. if (sw->ports[i].config.type == TB_TYPE_PCIE_UP)
  118. return &sw->ports[i];
  119. return NULL;
  120. }
  121. /**
  122. * find_unused_down_port() - return the first inactive PCIe down port on @sw
  123. */
  124. static struct tb_port *tb_find_unused_down_port(struct tb_switch *sw)
  125. {
  126. int i;
  127. int cap;
  128. int res;
  129. int data;
  130. for (i = 1; i <= sw->config.max_port_number; i++) {
  131. if (tb_is_upstream_port(&sw->ports[i]))
  132. continue;
  133. if (sw->ports[i].config.type != TB_TYPE_PCIE_DOWN)
  134. continue;
  135. cap = tb_port_find_cap(&sw->ports[i], TB_PORT_CAP_ADAP);
  136. if (cap < 0)
  137. continue;
  138. res = tb_port_read(&sw->ports[i], &data, TB_CFG_PORT, cap, 1);
  139. if (res < 0)
  140. continue;
  141. if (data & 0x80000000)
  142. continue;
  143. return &sw->ports[i];
  144. }
  145. return NULL;
  146. }
  147. /**
  148. * tb_activate_pcie_devices() - scan for and activate PCIe devices
  149. *
  150. * This method is somewhat ad hoc. For now it only supports one device
  151. * per port and only devices at depth 1.
  152. */
  153. static void tb_activate_pcie_devices(struct tb *tb)
  154. {
  155. int i;
  156. int cap;
  157. u32 data;
  158. struct tb_switch *sw;
  159. struct tb_port *up_port;
  160. struct tb_port *down_port;
  161. struct tb_pci_tunnel *tunnel;
  162. struct tb_cm *tcm = tb_priv(tb);
  163. /* scan for pcie devices at depth 1*/
  164. for (i = 1; i <= tb->root_switch->config.max_port_number; i++) {
  165. if (tb_is_upstream_port(&tb->root_switch->ports[i]))
  166. continue;
  167. if (tb->root_switch->ports[i].config.type != TB_TYPE_PORT)
  168. continue;
  169. if (!tb->root_switch->ports[i].remote)
  170. continue;
  171. sw = tb->root_switch->ports[i].remote->sw;
  172. up_port = tb_find_pci_up_port(sw);
  173. if (!up_port) {
  174. tb_sw_info(sw, "no PCIe devices found, aborting\n");
  175. continue;
  176. }
  177. /* check whether port is already activated */
  178. cap = tb_port_find_cap(up_port, TB_PORT_CAP_ADAP);
  179. if (cap < 0)
  180. continue;
  181. if (tb_port_read(up_port, &data, TB_CFG_PORT, cap, 1))
  182. continue;
  183. if (data & 0x80000000) {
  184. tb_port_info(up_port,
  185. "PCIe port already activated, aborting\n");
  186. continue;
  187. }
  188. down_port = tb_find_unused_down_port(tb->root_switch);
  189. if (!down_port) {
  190. tb_port_info(up_port,
  191. "All PCIe down ports are occupied, aborting\n");
  192. continue;
  193. }
  194. tunnel = tb_pci_alloc(tb, up_port, down_port);
  195. if (!tunnel) {
  196. tb_port_info(up_port,
  197. "PCIe tunnel allocation failed, aborting\n");
  198. continue;
  199. }
  200. if (tb_pci_activate(tunnel)) {
  201. tb_port_info(up_port,
  202. "PCIe tunnel activation failed, aborting\n");
  203. tb_pci_free(tunnel);
  204. }
  205. list_add(&tunnel->list, &tcm->tunnel_list);
  206. }
  207. }
  208. /* hotplug handling */
  209. struct tb_hotplug_event {
  210. struct work_struct work;
  211. struct tb *tb;
  212. u64 route;
  213. u8 port;
  214. bool unplug;
  215. };
  216. /**
  217. * tb_handle_hotplug() - handle hotplug event
  218. *
  219. * Executes on tb->wq.
  220. */
  221. static void tb_handle_hotplug(struct work_struct *work)
  222. {
  223. struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
  224. struct tb *tb = ev->tb;
  225. struct tb_cm *tcm = tb_priv(tb);
  226. struct tb_switch *sw;
  227. struct tb_port *port;
  228. mutex_lock(&tb->lock);
  229. if (!tcm->hotplug_active)
  230. goto out; /* during init, suspend or shutdown */
  231. sw = get_switch_at_route(tb->root_switch, ev->route);
  232. if (!sw) {
  233. tb_warn(tb,
  234. "hotplug event from non existent switch %llx:%x (unplug: %d)\n",
  235. ev->route, ev->port, ev->unplug);
  236. goto out;
  237. }
  238. if (ev->port > sw->config.max_port_number) {
  239. tb_warn(tb,
  240. "hotplug event from non existent port %llx:%x (unplug: %d)\n",
  241. ev->route, ev->port, ev->unplug);
  242. goto out;
  243. }
  244. port = &sw->ports[ev->port];
  245. if (tb_is_upstream_port(port)) {
  246. tb_warn(tb,
  247. "hotplug event for upstream port %llx:%x (unplug: %d)\n",
  248. ev->route, ev->port, ev->unplug);
  249. goto out;
  250. }
  251. if (ev->unplug) {
  252. if (port->remote) {
  253. tb_port_info(port, "unplugged\n");
  254. tb_sw_set_unplugged(port->remote->sw);
  255. tb_free_invalid_tunnels(tb);
  256. tb_switch_remove(port->remote->sw);
  257. port->remote = NULL;
  258. } else {
  259. tb_port_info(port,
  260. "got unplug event for disconnected port, ignoring\n");
  261. }
  262. } else if (port->remote) {
  263. tb_port_info(port,
  264. "got plug event for connected port, ignoring\n");
  265. } else {
  266. tb_port_info(port, "hotplug: scanning\n");
  267. tb_scan_port(port);
  268. if (!port->remote) {
  269. tb_port_info(port, "hotplug: no switch found\n");
  270. } else if (port->remote->sw->config.depth > 1) {
  271. tb_sw_warn(port->remote->sw,
  272. "hotplug: chaining not supported\n");
  273. } else {
  274. tb_sw_info(port->remote->sw,
  275. "hotplug: activating pcie devices\n");
  276. tb_activate_pcie_devices(tb);
  277. }
  278. }
  279. out:
  280. mutex_unlock(&tb->lock);
  281. kfree(ev);
  282. }
  283. /**
  284. * tb_schedule_hotplug_handler() - callback function for the control channel
  285. *
  286. * Delegates to tb_handle_hotplug.
  287. */
  288. static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
  289. const void *buf, size_t size)
  290. {
  291. const struct cfg_event_pkg *pkg = buf;
  292. struct tb_hotplug_event *ev;
  293. u64 route;
  294. if (type != TB_CFG_PKG_EVENT) {
  295. tb_warn(tb, "unexpected event %#x, ignoring\n", type);
  296. return;
  297. }
  298. route = tb_cfg_get_route(&pkg->header);
  299. if (tb_cfg_error(tb->ctl, route, pkg->port,
  300. TB_CFG_ERROR_ACK_PLUG_EVENT)) {
  301. tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
  302. pkg->port);
  303. }
  304. ev = kmalloc(sizeof(*ev), GFP_KERNEL);
  305. if (!ev)
  306. return;
  307. INIT_WORK(&ev->work, tb_handle_hotplug);
  308. ev->tb = tb;
  309. ev->route = route;
  310. ev->port = pkg->port;
  311. ev->unplug = pkg->unplug;
  312. queue_work(tb->wq, &ev->work);
  313. }
  314. static void tb_stop(struct tb *tb)
  315. {
  316. struct tb_cm *tcm = tb_priv(tb);
  317. struct tb_pci_tunnel *tunnel;
  318. struct tb_pci_tunnel *n;
  319. /* tunnels are only present after everything has been initialized */
  320. list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
  321. tb_pci_deactivate(tunnel);
  322. tb_pci_free(tunnel);
  323. }
  324. tb_switch_remove(tb->root_switch);
  325. tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
  326. }
  327. static int tb_start(struct tb *tb)
  328. {
  329. struct tb_cm *tcm = tb_priv(tb);
  330. int ret;
  331. tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
  332. if (!tb->root_switch)
  333. return -ENOMEM;
  334. /*
  335. * ICM firmware upgrade needs running firmware and in native
  336. * mode that is not available so disable firmware upgrade of the
  337. * root switch.
  338. */
  339. tb->root_switch->no_nvm_upgrade = true;
  340. ret = tb_switch_configure(tb->root_switch);
  341. if (ret) {
  342. tb_switch_put(tb->root_switch);
  343. return ret;
  344. }
  345. /* Announce the switch to the world */
  346. ret = tb_switch_add(tb->root_switch);
  347. if (ret) {
  348. tb_switch_put(tb->root_switch);
  349. return ret;
  350. }
  351. /* Full scan to discover devices added before the driver was loaded. */
  352. tb_scan_switch(tb->root_switch);
  353. tb_activate_pcie_devices(tb);
  354. /* Allow tb_handle_hotplug to progress events */
  355. tcm->hotplug_active = true;
  356. return 0;
  357. }
  358. static int tb_suspend_noirq(struct tb *tb)
  359. {
  360. struct tb_cm *tcm = tb_priv(tb);
  361. tb_info(tb, "suspending...\n");
  362. tb_switch_suspend(tb->root_switch);
  363. tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
  364. tb_info(tb, "suspend finished\n");
  365. return 0;
  366. }
  367. static int tb_resume_noirq(struct tb *tb)
  368. {
  369. struct tb_cm *tcm = tb_priv(tb);
  370. struct tb_pci_tunnel *tunnel, *n;
  371. tb_info(tb, "resuming...\n");
  372. /* remove any pci devices the firmware might have setup */
  373. tb_switch_reset(tb, 0);
  374. tb_switch_resume(tb->root_switch);
  375. tb_free_invalid_tunnels(tb);
  376. tb_free_unplugged_children(tb->root_switch);
  377. list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
  378. tb_pci_restart(tunnel);
  379. if (!list_empty(&tcm->tunnel_list)) {
  380. /*
  381. * the pcie links need some time to get going.
  382. * 100ms works for me...
  383. */
  384. tb_info(tb, "tunnels restarted, sleeping for 100ms\n");
  385. msleep(100);
  386. }
  387. /* Allow tb_handle_hotplug to progress events */
  388. tcm->hotplug_active = true;
  389. tb_info(tb, "resume finished\n");
  390. return 0;
  391. }
  392. static const struct tb_cm_ops tb_cm_ops = {
  393. .start = tb_start,
  394. .stop = tb_stop,
  395. .suspend_noirq = tb_suspend_noirq,
  396. .resume_noirq = tb_resume_noirq,
  397. .handle_event = tb_handle_event,
  398. };
  399. struct tb *tb_probe(struct tb_nhi *nhi)
  400. {
  401. struct tb_cm *tcm;
  402. struct tb *tb;
  403. if (!x86_apple_machine)
  404. return NULL;
  405. tb = tb_domain_alloc(nhi, sizeof(*tcm));
  406. if (!tb)
  407. return NULL;
  408. tb->security_level = TB_SECURITY_NONE;
  409. tb->cm_ops = &tb_cm_ops;
  410. tcm = tb_priv(tb);
  411. INIT_LIST_HEAD(&tcm->tunnel_list);
  412. return tb;
  413. }