tb.c 11 KB

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