switch.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Thunderbolt Cactus Ridge driver - switch/port utility functions
  4. *
  5. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/idr.h>
  9. #include <linux/nvmem-provider.h>
  10. #include <linux/pm_runtime.h>
  11. #include <linux/sched/signal.h>
  12. #include <linux/sizes.h>
  13. #include <linux/slab.h>
  14. #include <linux/vmalloc.h>
  15. #include "tb.h"
  16. /* Switch NVM support */
  17. #define NVM_DEVID 0x05
  18. #define NVM_VERSION 0x08
  19. #define NVM_CSS 0x10
  20. #define NVM_FLASH_SIZE 0x45
  21. #define NVM_MIN_SIZE SZ_32K
  22. #define NVM_MAX_SIZE SZ_512K
  23. static DEFINE_IDA(nvm_ida);
  24. struct nvm_auth_status {
  25. struct list_head list;
  26. uuid_t uuid;
  27. u32 status;
  28. };
  29. /*
  30. * Hold NVM authentication failure status per switch This information
  31. * needs to stay around even when the switch gets power cycled so we
  32. * keep it separately.
  33. */
  34. static LIST_HEAD(nvm_auth_status_cache);
  35. static DEFINE_MUTEX(nvm_auth_status_lock);
  36. static struct nvm_auth_status *__nvm_get_auth_status(const struct tb_switch *sw)
  37. {
  38. struct nvm_auth_status *st;
  39. list_for_each_entry(st, &nvm_auth_status_cache, list) {
  40. if (uuid_equal(&st->uuid, sw->uuid))
  41. return st;
  42. }
  43. return NULL;
  44. }
  45. static void nvm_get_auth_status(const struct tb_switch *sw, u32 *status)
  46. {
  47. struct nvm_auth_status *st;
  48. mutex_lock(&nvm_auth_status_lock);
  49. st = __nvm_get_auth_status(sw);
  50. mutex_unlock(&nvm_auth_status_lock);
  51. *status = st ? st->status : 0;
  52. }
  53. static void nvm_set_auth_status(const struct tb_switch *sw, u32 status)
  54. {
  55. struct nvm_auth_status *st;
  56. if (WARN_ON(!sw->uuid))
  57. return;
  58. mutex_lock(&nvm_auth_status_lock);
  59. st = __nvm_get_auth_status(sw);
  60. if (!st) {
  61. st = kzalloc(sizeof(*st), GFP_KERNEL);
  62. if (!st)
  63. goto unlock;
  64. memcpy(&st->uuid, sw->uuid, sizeof(st->uuid));
  65. INIT_LIST_HEAD(&st->list);
  66. list_add_tail(&st->list, &nvm_auth_status_cache);
  67. }
  68. st->status = status;
  69. unlock:
  70. mutex_unlock(&nvm_auth_status_lock);
  71. }
  72. static void nvm_clear_auth_status(const struct tb_switch *sw)
  73. {
  74. struct nvm_auth_status *st;
  75. mutex_lock(&nvm_auth_status_lock);
  76. st = __nvm_get_auth_status(sw);
  77. if (st) {
  78. list_del(&st->list);
  79. kfree(st);
  80. }
  81. mutex_unlock(&nvm_auth_status_lock);
  82. }
  83. static int nvm_validate_and_write(struct tb_switch *sw)
  84. {
  85. unsigned int image_size, hdr_size;
  86. const u8 *buf = sw->nvm->buf;
  87. u16 ds_size;
  88. int ret;
  89. if (!buf)
  90. return -EINVAL;
  91. image_size = sw->nvm->buf_data_size;
  92. if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE)
  93. return -EINVAL;
  94. /*
  95. * FARB pointer must point inside the image and must at least
  96. * contain parts of the digital section we will be reading here.
  97. */
  98. hdr_size = (*(u32 *)buf) & 0xffffff;
  99. if (hdr_size + NVM_DEVID + 2 >= image_size)
  100. return -EINVAL;
  101. /* Digital section start should be aligned to 4k page */
  102. if (!IS_ALIGNED(hdr_size, SZ_4K))
  103. return -EINVAL;
  104. /*
  105. * Read digital section size and check that it also fits inside
  106. * the image.
  107. */
  108. ds_size = *(u16 *)(buf + hdr_size);
  109. if (ds_size >= image_size)
  110. return -EINVAL;
  111. if (!sw->safe_mode) {
  112. u16 device_id;
  113. /*
  114. * Make sure the device ID in the image matches the one
  115. * we read from the switch config space.
  116. */
  117. device_id = *(u16 *)(buf + hdr_size + NVM_DEVID);
  118. if (device_id != sw->config.device_id)
  119. return -EINVAL;
  120. if (sw->generation < 3) {
  121. /* Write CSS headers first */
  122. ret = dma_port_flash_write(sw->dma_port,
  123. DMA_PORT_CSS_ADDRESS, buf + NVM_CSS,
  124. DMA_PORT_CSS_MAX_SIZE);
  125. if (ret)
  126. return ret;
  127. }
  128. /* Skip headers in the image */
  129. buf += hdr_size;
  130. image_size -= hdr_size;
  131. }
  132. return dma_port_flash_write(sw->dma_port, 0, buf, image_size);
  133. }
  134. static int nvm_authenticate_host(struct tb_switch *sw)
  135. {
  136. int ret = 0;
  137. /*
  138. * Root switch NVM upgrade requires that we disconnect the
  139. * existing paths first (in case it is not in safe mode
  140. * already).
  141. */
  142. if (!sw->safe_mode) {
  143. u32 status;
  144. ret = tb_domain_disconnect_all_paths(sw->tb);
  145. if (ret)
  146. return ret;
  147. /*
  148. * The host controller goes away pretty soon after this if
  149. * everything goes well so getting timeout is expected.
  150. */
  151. ret = dma_port_flash_update_auth(sw->dma_port);
  152. if (!ret || ret == -ETIMEDOUT)
  153. return 0;
  154. /*
  155. * Any error from update auth operation requires power
  156. * cycling of the host router.
  157. */
  158. tb_sw_warn(sw, "failed to authenticate NVM, power cycling\n");
  159. if (dma_port_flash_update_auth_status(sw->dma_port, &status) > 0)
  160. nvm_set_auth_status(sw, status);
  161. }
  162. /*
  163. * From safe mode we can get out by just power cycling the
  164. * switch.
  165. */
  166. dma_port_power_cycle(sw->dma_port);
  167. return ret;
  168. }
  169. static int nvm_authenticate_device(struct tb_switch *sw)
  170. {
  171. int ret, retries = 10;
  172. ret = dma_port_flash_update_auth(sw->dma_port);
  173. switch (ret) {
  174. case 0:
  175. case -ETIMEDOUT:
  176. case -EACCES:
  177. case -EINVAL:
  178. /* Power cycle is required */
  179. break;
  180. default:
  181. return ret;
  182. }
  183. /*
  184. * Poll here for the authentication status. It takes some time
  185. * for the device to respond (we get timeout for a while). Once
  186. * we get response the device needs to be power cycled in order
  187. * to the new NVM to be taken into use.
  188. */
  189. do {
  190. u32 status;
  191. ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
  192. if (ret < 0 && ret != -ETIMEDOUT)
  193. return ret;
  194. if (ret > 0) {
  195. if (status) {
  196. tb_sw_warn(sw, "failed to authenticate NVM\n");
  197. nvm_set_auth_status(sw, status);
  198. }
  199. tb_sw_info(sw, "power cycling the switch now\n");
  200. dma_port_power_cycle(sw->dma_port);
  201. return 0;
  202. }
  203. msleep(500);
  204. } while (--retries);
  205. return -ETIMEDOUT;
  206. }
  207. static int tb_switch_nvm_read(void *priv, unsigned int offset, void *val,
  208. size_t bytes)
  209. {
  210. struct tb_switch *sw = priv;
  211. int ret;
  212. pm_runtime_get_sync(&sw->dev);
  213. ret = dma_port_flash_read(sw->dma_port, offset, val, bytes);
  214. pm_runtime_mark_last_busy(&sw->dev);
  215. pm_runtime_put_autosuspend(&sw->dev);
  216. return ret;
  217. }
  218. static int tb_switch_nvm_no_read(void *priv, unsigned int offset, void *val,
  219. size_t bytes)
  220. {
  221. return -EPERM;
  222. }
  223. static int tb_switch_nvm_write(void *priv, unsigned int offset, void *val,
  224. size_t bytes)
  225. {
  226. struct tb_switch *sw = priv;
  227. int ret = 0;
  228. if (!mutex_trylock(&sw->tb->lock))
  229. return restart_syscall();
  230. /*
  231. * Since writing the NVM image might require some special steps,
  232. * for example when CSS headers are written, we cache the image
  233. * locally here and handle the special cases when the user asks
  234. * us to authenticate the image.
  235. */
  236. if (!sw->nvm->buf) {
  237. sw->nvm->buf = vmalloc(NVM_MAX_SIZE);
  238. if (!sw->nvm->buf) {
  239. ret = -ENOMEM;
  240. goto unlock;
  241. }
  242. }
  243. sw->nvm->buf_data_size = offset + bytes;
  244. memcpy(sw->nvm->buf + offset, val, bytes);
  245. unlock:
  246. mutex_unlock(&sw->tb->lock);
  247. return ret;
  248. }
  249. static struct nvmem_device *register_nvmem(struct tb_switch *sw, int id,
  250. size_t size, bool active)
  251. {
  252. struct nvmem_config config;
  253. memset(&config, 0, sizeof(config));
  254. if (active) {
  255. config.name = "nvm_active";
  256. config.reg_read = tb_switch_nvm_read;
  257. config.read_only = true;
  258. } else {
  259. config.name = "nvm_non_active";
  260. config.reg_read = tb_switch_nvm_no_read;
  261. config.reg_write = tb_switch_nvm_write;
  262. config.root_only = true;
  263. }
  264. config.id = id;
  265. config.stride = 4;
  266. config.word_size = 4;
  267. config.size = size;
  268. config.dev = &sw->dev;
  269. config.owner = THIS_MODULE;
  270. config.priv = sw;
  271. return nvmem_register(&config);
  272. }
  273. static int tb_switch_nvm_add(struct tb_switch *sw)
  274. {
  275. struct nvmem_device *nvm_dev;
  276. struct tb_switch_nvm *nvm;
  277. u32 val;
  278. int ret;
  279. if (!sw->dma_port)
  280. return 0;
  281. nvm = kzalloc(sizeof(*nvm), GFP_KERNEL);
  282. if (!nvm)
  283. return -ENOMEM;
  284. nvm->id = ida_simple_get(&nvm_ida, 0, 0, GFP_KERNEL);
  285. /*
  286. * If the switch is in safe-mode the only accessible portion of
  287. * the NVM is the non-active one where userspace is expected to
  288. * write new functional NVM.
  289. */
  290. if (!sw->safe_mode) {
  291. u32 nvm_size, hdr_size;
  292. ret = dma_port_flash_read(sw->dma_port, NVM_FLASH_SIZE, &val,
  293. sizeof(val));
  294. if (ret)
  295. goto err_ida;
  296. hdr_size = sw->generation < 3 ? SZ_8K : SZ_16K;
  297. nvm_size = (SZ_1M << (val & 7)) / 8;
  298. nvm_size = (nvm_size - hdr_size) / 2;
  299. ret = dma_port_flash_read(sw->dma_port, NVM_VERSION, &val,
  300. sizeof(val));
  301. if (ret)
  302. goto err_ida;
  303. nvm->major = val >> 16;
  304. nvm->minor = val >> 8;
  305. nvm_dev = register_nvmem(sw, nvm->id, nvm_size, true);
  306. if (IS_ERR(nvm_dev)) {
  307. ret = PTR_ERR(nvm_dev);
  308. goto err_ida;
  309. }
  310. nvm->active = nvm_dev;
  311. }
  312. nvm_dev = register_nvmem(sw, nvm->id, NVM_MAX_SIZE, false);
  313. if (IS_ERR(nvm_dev)) {
  314. ret = PTR_ERR(nvm_dev);
  315. goto err_nvm_active;
  316. }
  317. nvm->non_active = nvm_dev;
  318. sw->nvm = nvm;
  319. return 0;
  320. err_nvm_active:
  321. if (nvm->active)
  322. nvmem_unregister(nvm->active);
  323. err_ida:
  324. ida_simple_remove(&nvm_ida, nvm->id);
  325. kfree(nvm);
  326. return ret;
  327. }
  328. static void tb_switch_nvm_remove(struct tb_switch *sw)
  329. {
  330. struct tb_switch_nvm *nvm;
  331. nvm = sw->nvm;
  332. sw->nvm = NULL;
  333. if (!nvm)
  334. return;
  335. /* Remove authentication status in case the switch is unplugged */
  336. if (!nvm->authenticating)
  337. nvm_clear_auth_status(sw);
  338. nvmem_unregister(nvm->non_active);
  339. if (nvm->active)
  340. nvmem_unregister(nvm->active);
  341. ida_simple_remove(&nvm_ida, nvm->id);
  342. vfree(nvm->buf);
  343. kfree(nvm);
  344. }
  345. /* port utility functions */
  346. static const char *tb_port_type(struct tb_regs_port_header *port)
  347. {
  348. switch (port->type >> 16) {
  349. case 0:
  350. switch ((u8) port->type) {
  351. case 0:
  352. return "Inactive";
  353. case 1:
  354. return "Port";
  355. case 2:
  356. return "NHI";
  357. default:
  358. return "unknown";
  359. }
  360. case 0x2:
  361. return "Ethernet";
  362. case 0x8:
  363. return "SATA";
  364. case 0xe:
  365. return "DP/HDMI";
  366. case 0x10:
  367. return "PCIe";
  368. case 0x20:
  369. return "USB";
  370. default:
  371. return "unknown";
  372. }
  373. }
  374. static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port)
  375. {
  376. tb_info(tb,
  377. " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n",
  378. port->port_number, port->vendor_id, port->device_id,
  379. port->revision, port->thunderbolt_version, tb_port_type(port),
  380. port->type);
  381. tb_info(tb, " Max hop id (in/out): %d/%d\n",
  382. port->max_in_hop_id, port->max_out_hop_id);
  383. tb_info(tb, " Max counters: %d\n", port->max_counters);
  384. tb_info(tb, " NFC Credits: %#x\n", port->nfc_credits);
  385. }
  386. /**
  387. * tb_port_state() - get connectedness state of a port
  388. *
  389. * The port must have a TB_CAP_PHY (i.e. it should be a real port).
  390. *
  391. * Return: Returns an enum tb_port_state on success or an error code on failure.
  392. */
  393. static int tb_port_state(struct tb_port *port)
  394. {
  395. struct tb_cap_phy phy;
  396. int res;
  397. if (port->cap_phy == 0) {
  398. tb_port_WARN(port, "does not have a PHY\n");
  399. return -EINVAL;
  400. }
  401. res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2);
  402. if (res)
  403. return res;
  404. return phy.state;
  405. }
  406. /**
  407. * tb_wait_for_port() - wait for a port to become ready
  408. *
  409. * Wait up to 1 second for a port to reach state TB_PORT_UP. If
  410. * wait_if_unplugged is set then we also wait if the port is in state
  411. * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
  412. * switch resume). Otherwise we only wait if a device is registered but the link
  413. * has not yet been established.
  414. *
  415. * Return: Returns an error code on failure. Returns 0 if the port is not
  416. * connected or failed to reach state TB_PORT_UP within one second. Returns 1
  417. * if the port is connected and in state TB_PORT_UP.
  418. */
  419. int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
  420. {
  421. int retries = 10;
  422. int state;
  423. if (!port->cap_phy) {
  424. tb_port_WARN(port, "does not have PHY\n");
  425. return -EINVAL;
  426. }
  427. if (tb_is_upstream_port(port)) {
  428. tb_port_WARN(port, "is the upstream port\n");
  429. return -EINVAL;
  430. }
  431. while (retries--) {
  432. state = tb_port_state(port);
  433. if (state < 0)
  434. return state;
  435. if (state == TB_PORT_DISABLED) {
  436. tb_port_info(port, "is disabled (state: 0)\n");
  437. return 0;
  438. }
  439. if (state == TB_PORT_UNPLUGGED) {
  440. if (wait_if_unplugged) {
  441. /* used during resume */
  442. tb_port_info(port,
  443. "is unplugged (state: 7), retrying...\n");
  444. msleep(100);
  445. continue;
  446. }
  447. tb_port_info(port, "is unplugged (state: 7)\n");
  448. return 0;
  449. }
  450. if (state == TB_PORT_UP) {
  451. tb_port_info(port,
  452. "is connected, link is up (state: 2)\n");
  453. return 1;
  454. }
  455. /*
  456. * After plug-in the state is TB_PORT_CONNECTING. Give it some
  457. * time.
  458. */
  459. tb_port_info(port,
  460. "is connected, link is not up (state: %d), retrying...\n",
  461. state);
  462. msleep(100);
  463. }
  464. tb_port_warn(port,
  465. "failed to reach state TB_PORT_UP. Ignoring port...\n");
  466. return 0;
  467. }
  468. /**
  469. * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
  470. *
  471. * Change the number of NFC credits allocated to @port by @credits. To remove
  472. * NFC credits pass a negative amount of credits.
  473. *
  474. * Return: Returns 0 on success or an error code on failure.
  475. */
  476. int tb_port_add_nfc_credits(struct tb_port *port, int credits)
  477. {
  478. if (credits == 0)
  479. return 0;
  480. tb_port_info(port,
  481. "adding %#x NFC credits (%#x -> %#x)",
  482. credits,
  483. port->config.nfc_credits,
  484. port->config.nfc_credits + credits);
  485. port->config.nfc_credits += credits;
  486. return tb_port_write(port, &port->config.nfc_credits,
  487. TB_CFG_PORT, 4, 1);
  488. }
  489. /**
  490. * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
  491. *
  492. * Return: Returns 0 on success or an error code on failure.
  493. */
  494. int tb_port_clear_counter(struct tb_port *port, int counter)
  495. {
  496. u32 zero[3] = { 0, 0, 0 };
  497. tb_port_info(port, "clearing counter %d\n", counter);
  498. return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
  499. }
  500. /**
  501. * tb_init_port() - initialize a port
  502. *
  503. * This is a helper method for tb_switch_alloc. Does not check or initialize
  504. * any downstream switches.
  505. *
  506. * Return: Returns 0 on success or an error code on failure.
  507. */
  508. static int tb_init_port(struct tb_port *port)
  509. {
  510. int res;
  511. int cap;
  512. res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8);
  513. if (res)
  514. return res;
  515. /* Port 0 is the switch itself and has no PHY. */
  516. if (port->config.type == TB_TYPE_PORT && port->port != 0) {
  517. cap = tb_port_find_cap(port, TB_PORT_CAP_PHY);
  518. if (cap > 0)
  519. port->cap_phy = cap;
  520. else
  521. tb_port_WARN(port, "non switch port without a PHY\n");
  522. }
  523. tb_dump_port(port->sw->tb, &port->config);
  524. /* TODO: Read dual link port, DP port and more from EEPROM. */
  525. return 0;
  526. }
  527. /* switch utility functions */
  528. static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw)
  529. {
  530. tb_info(tb,
  531. " Switch: %x:%x (Revision: %d, TB Version: %d)\n",
  532. sw->vendor_id, sw->device_id, sw->revision,
  533. sw->thunderbolt_version);
  534. tb_info(tb, " Max Port Number: %d\n", sw->max_port_number);
  535. tb_info(tb, " Config:\n");
  536. tb_info(tb,
  537. " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
  538. sw->upstream_port_number, sw->depth,
  539. (((u64) sw->route_hi) << 32) | sw->route_lo,
  540. sw->enabled, sw->plug_events_delay);
  541. tb_info(tb,
  542. " unknown1: %#x unknown4: %#x\n",
  543. sw->__unknown1, sw->__unknown4);
  544. }
  545. /**
  546. * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
  547. *
  548. * Return: Returns 0 on success or an error code on failure.
  549. */
  550. int tb_switch_reset(struct tb *tb, u64 route)
  551. {
  552. struct tb_cfg_result res;
  553. struct tb_regs_switch_header header = {
  554. header.route_hi = route >> 32,
  555. header.route_lo = route,
  556. header.enabled = true,
  557. };
  558. tb_info(tb, "resetting switch at %llx\n", route);
  559. res.err = tb_cfg_write(tb->ctl, ((u32 *) &header) + 2, route,
  560. 0, 2, 2, 2);
  561. if (res.err)
  562. return res.err;
  563. res = tb_cfg_reset(tb->ctl, route, TB_CFG_DEFAULT_TIMEOUT);
  564. if (res.err > 0)
  565. return -EIO;
  566. return res.err;
  567. }
  568. struct tb_switch *get_switch_at_route(struct tb_switch *sw, u64 route)
  569. {
  570. u8 next_port = route; /*
  571. * Routes use a stride of 8 bits,
  572. * eventhough a port index has 6 bits at most.
  573. * */
  574. if (route == 0)
  575. return sw;
  576. if (next_port > sw->config.max_port_number)
  577. return NULL;
  578. if (tb_is_upstream_port(&sw->ports[next_port]))
  579. return NULL;
  580. if (!sw->ports[next_port].remote)
  581. return NULL;
  582. return get_switch_at_route(sw->ports[next_port].remote->sw,
  583. route >> TB_ROUTE_SHIFT);
  584. }
  585. /**
  586. * tb_plug_events_active() - enable/disable plug events on a switch
  587. *
  588. * Also configures a sane plug_events_delay of 255ms.
  589. *
  590. * Return: Returns 0 on success or an error code on failure.
  591. */
  592. static int tb_plug_events_active(struct tb_switch *sw, bool active)
  593. {
  594. u32 data;
  595. int res;
  596. if (!sw->config.enabled)
  597. return 0;
  598. sw->config.plug_events_delay = 0xff;
  599. res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1);
  600. if (res)
  601. return res;
  602. res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
  603. if (res)
  604. return res;
  605. if (active) {
  606. data = data & 0xFFFFFF83;
  607. switch (sw->config.device_id) {
  608. case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
  609. case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
  610. case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
  611. break;
  612. default:
  613. data |= 4;
  614. }
  615. } else {
  616. data = data | 0x7c;
  617. }
  618. return tb_sw_write(sw, &data, TB_CFG_SWITCH,
  619. sw->cap_plug_events + 1, 1);
  620. }
  621. static ssize_t authorized_show(struct device *dev,
  622. struct device_attribute *attr,
  623. char *buf)
  624. {
  625. struct tb_switch *sw = tb_to_switch(dev);
  626. return sprintf(buf, "%u\n", sw->authorized);
  627. }
  628. static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
  629. {
  630. int ret = -EINVAL;
  631. if (!mutex_trylock(&sw->tb->lock))
  632. return restart_syscall();
  633. if (sw->authorized)
  634. goto unlock;
  635. /*
  636. * Make sure there is no PCIe rescan ongoing when a new PCIe
  637. * tunnel is created. Otherwise the PCIe rescan code might find
  638. * the new tunnel too early.
  639. */
  640. pci_lock_rescan_remove();
  641. pm_runtime_get_sync(&sw->dev);
  642. switch (val) {
  643. /* Approve switch */
  644. case 1:
  645. if (sw->key)
  646. ret = tb_domain_approve_switch_key(sw->tb, sw);
  647. else
  648. ret = tb_domain_approve_switch(sw->tb, sw);
  649. break;
  650. /* Challenge switch */
  651. case 2:
  652. if (sw->key)
  653. ret = tb_domain_challenge_switch_key(sw->tb, sw);
  654. break;
  655. default:
  656. break;
  657. }
  658. pm_runtime_mark_last_busy(&sw->dev);
  659. pm_runtime_put_autosuspend(&sw->dev);
  660. pci_unlock_rescan_remove();
  661. if (!ret) {
  662. sw->authorized = val;
  663. /* Notify status change to the userspace */
  664. kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
  665. }
  666. unlock:
  667. mutex_unlock(&sw->tb->lock);
  668. return ret;
  669. }
  670. static ssize_t authorized_store(struct device *dev,
  671. struct device_attribute *attr,
  672. const char *buf, size_t count)
  673. {
  674. struct tb_switch *sw = tb_to_switch(dev);
  675. unsigned int val;
  676. ssize_t ret;
  677. ret = kstrtouint(buf, 0, &val);
  678. if (ret)
  679. return ret;
  680. if (val > 2)
  681. return -EINVAL;
  682. ret = tb_switch_set_authorized(sw, val);
  683. return ret ? ret : count;
  684. }
  685. static DEVICE_ATTR_RW(authorized);
  686. static ssize_t boot_show(struct device *dev, struct device_attribute *attr,
  687. char *buf)
  688. {
  689. struct tb_switch *sw = tb_to_switch(dev);
  690. return sprintf(buf, "%u\n", sw->boot);
  691. }
  692. static DEVICE_ATTR_RO(boot);
  693. static ssize_t device_show(struct device *dev, struct device_attribute *attr,
  694. char *buf)
  695. {
  696. struct tb_switch *sw = tb_to_switch(dev);
  697. return sprintf(buf, "%#x\n", sw->device);
  698. }
  699. static DEVICE_ATTR_RO(device);
  700. static ssize_t
  701. device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
  702. {
  703. struct tb_switch *sw = tb_to_switch(dev);
  704. return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : "");
  705. }
  706. static DEVICE_ATTR_RO(device_name);
  707. static ssize_t key_show(struct device *dev, struct device_attribute *attr,
  708. char *buf)
  709. {
  710. struct tb_switch *sw = tb_to_switch(dev);
  711. ssize_t ret;
  712. if (!mutex_trylock(&sw->tb->lock))
  713. return restart_syscall();
  714. if (sw->key)
  715. ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key);
  716. else
  717. ret = sprintf(buf, "\n");
  718. mutex_unlock(&sw->tb->lock);
  719. return ret;
  720. }
  721. static ssize_t key_store(struct device *dev, struct device_attribute *attr,
  722. const char *buf, size_t count)
  723. {
  724. struct tb_switch *sw = tb_to_switch(dev);
  725. u8 key[TB_SWITCH_KEY_SIZE];
  726. ssize_t ret = count;
  727. bool clear = false;
  728. if (!strcmp(buf, "\n"))
  729. clear = true;
  730. else if (hex2bin(key, buf, sizeof(key)))
  731. return -EINVAL;
  732. if (!mutex_trylock(&sw->tb->lock))
  733. return restart_syscall();
  734. if (sw->authorized) {
  735. ret = -EBUSY;
  736. } else {
  737. kfree(sw->key);
  738. if (clear) {
  739. sw->key = NULL;
  740. } else {
  741. sw->key = kmemdup(key, sizeof(key), GFP_KERNEL);
  742. if (!sw->key)
  743. ret = -ENOMEM;
  744. }
  745. }
  746. mutex_unlock(&sw->tb->lock);
  747. return ret;
  748. }
  749. static DEVICE_ATTR(key, 0600, key_show, key_store);
  750. static void nvm_authenticate_start(struct tb_switch *sw)
  751. {
  752. struct pci_dev *root_port;
  753. /*
  754. * During host router NVM upgrade we should not allow root port to
  755. * go into D3cold because some root ports cannot trigger PME
  756. * itself. To be on the safe side keep the root port in D0 during
  757. * the whole upgrade process.
  758. */
  759. root_port = pci_find_pcie_root_port(sw->tb->nhi->pdev);
  760. if (root_port)
  761. pm_runtime_get_noresume(&root_port->dev);
  762. }
  763. static void nvm_authenticate_complete(struct tb_switch *sw)
  764. {
  765. struct pci_dev *root_port;
  766. root_port = pci_find_pcie_root_port(sw->tb->nhi->pdev);
  767. if (root_port)
  768. pm_runtime_put(&root_port->dev);
  769. }
  770. static ssize_t nvm_authenticate_show(struct device *dev,
  771. struct device_attribute *attr, char *buf)
  772. {
  773. struct tb_switch *sw = tb_to_switch(dev);
  774. u32 status;
  775. nvm_get_auth_status(sw, &status);
  776. return sprintf(buf, "%#x\n", status);
  777. }
  778. static ssize_t nvm_authenticate_store(struct device *dev,
  779. struct device_attribute *attr, const char *buf, size_t count)
  780. {
  781. struct tb_switch *sw = tb_to_switch(dev);
  782. bool val;
  783. int ret;
  784. if (!mutex_trylock(&sw->tb->lock))
  785. return restart_syscall();
  786. /* If NVMem devices are not yet added */
  787. if (!sw->nvm) {
  788. ret = -EAGAIN;
  789. goto exit_unlock;
  790. }
  791. ret = kstrtobool(buf, &val);
  792. if (ret)
  793. goto exit_unlock;
  794. /* Always clear the authentication status */
  795. nvm_clear_auth_status(sw);
  796. if (val) {
  797. if (!sw->nvm->buf) {
  798. ret = -EINVAL;
  799. goto exit_unlock;
  800. }
  801. pm_runtime_get_sync(&sw->dev);
  802. ret = nvm_validate_and_write(sw);
  803. if (ret) {
  804. pm_runtime_mark_last_busy(&sw->dev);
  805. pm_runtime_put_autosuspend(&sw->dev);
  806. goto exit_unlock;
  807. }
  808. sw->nvm->authenticating = true;
  809. if (!tb_route(sw)) {
  810. /*
  811. * Keep root port from suspending as long as the
  812. * NVM upgrade process is running.
  813. */
  814. nvm_authenticate_start(sw);
  815. ret = nvm_authenticate_host(sw);
  816. } else {
  817. ret = nvm_authenticate_device(sw);
  818. }
  819. pm_runtime_mark_last_busy(&sw->dev);
  820. pm_runtime_put_autosuspend(&sw->dev);
  821. }
  822. exit_unlock:
  823. mutex_unlock(&sw->tb->lock);
  824. if (ret)
  825. return ret;
  826. return count;
  827. }
  828. static DEVICE_ATTR_RW(nvm_authenticate);
  829. static ssize_t nvm_version_show(struct device *dev,
  830. struct device_attribute *attr, char *buf)
  831. {
  832. struct tb_switch *sw = tb_to_switch(dev);
  833. int ret;
  834. if (!mutex_trylock(&sw->tb->lock))
  835. return restart_syscall();
  836. if (sw->safe_mode)
  837. ret = -ENODATA;
  838. else if (!sw->nvm)
  839. ret = -EAGAIN;
  840. else
  841. ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);
  842. mutex_unlock(&sw->tb->lock);
  843. return ret;
  844. }
  845. static DEVICE_ATTR_RO(nvm_version);
  846. static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
  847. char *buf)
  848. {
  849. struct tb_switch *sw = tb_to_switch(dev);
  850. return sprintf(buf, "%#x\n", sw->vendor);
  851. }
  852. static DEVICE_ATTR_RO(vendor);
  853. static ssize_t
  854. vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
  855. {
  856. struct tb_switch *sw = tb_to_switch(dev);
  857. return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : "");
  858. }
  859. static DEVICE_ATTR_RO(vendor_name);
  860. static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
  861. char *buf)
  862. {
  863. struct tb_switch *sw = tb_to_switch(dev);
  864. return sprintf(buf, "%pUb\n", sw->uuid);
  865. }
  866. static DEVICE_ATTR_RO(unique_id);
  867. static struct attribute *switch_attrs[] = {
  868. &dev_attr_authorized.attr,
  869. &dev_attr_boot.attr,
  870. &dev_attr_device.attr,
  871. &dev_attr_device_name.attr,
  872. &dev_attr_key.attr,
  873. &dev_attr_nvm_authenticate.attr,
  874. &dev_attr_nvm_version.attr,
  875. &dev_attr_vendor.attr,
  876. &dev_attr_vendor_name.attr,
  877. &dev_attr_unique_id.attr,
  878. NULL,
  879. };
  880. static umode_t switch_attr_is_visible(struct kobject *kobj,
  881. struct attribute *attr, int n)
  882. {
  883. struct device *dev = container_of(kobj, struct device, kobj);
  884. struct tb_switch *sw = tb_to_switch(dev);
  885. if (attr == &dev_attr_key.attr) {
  886. if (tb_route(sw) &&
  887. sw->tb->security_level == TB_SECURITY_SECURE &&
  888. sw->security_level == TB_SECURITY_SECURE)
  889. return attr->mode;
  890. return 0;
  891. } else if (attr == &dev_attr_nvm_authenticate.attr ||
  892. attr == &dev_attr_nvm_version.attr) {
  893. if (sw->dma_port)
  894. return attr->mode;
  895. return 0;
  896. } else if (attr == &dev_attr_boot.attr) {
  897. if (tb_route(sw))
  898. return attr->mode;
  899. return 0;
  900. }
  901. return sw->safe_mode ? 0 : attr->mode;
  902. }
  903. static struct attribute_group switch_group = {
  904. .is_visible = switch_attr_is_visible,
  905. .attrs = switch_attrs,
  906. };
  907. static const struct attribute_group *switch_groups[] = {
  908. &switch_group,
  909. NULL,
  910. };
  911. static void tb_switch_release(struct device *dev)
  912. {
  913. struct tb_switch *sw = tb_to_switch(dev);
  914. dma_port_free(sw->dma_port);
  915. kfree(sw->uuid);
  916. kfree(sw->device_name);
  917. kfree(sw->vendor_name);
  918. kfree(sw->ports);
  919. kfree(sw->drom);
  920. kfree(sw->key);
  921. kfree(sw);
  922. }
  923. /*
  924. * Currently only need to provide the callbacks. Everything else is handled
  925. * in the connection manager.
  926. */
  927. static int __maybe_unused tb_switch_runtime_suspend(struct device *dev)
  928. {
  929. return 0;
  930. }
  931. static int __maybe_unused tb_switch_runtime_resume(struct device *dev)
  932. {
  933. return 0;
  934. }
  935. static const struct dev_pm_ops tb_switch_pm_ops = {
  936. SET_RUNTIME_PM_OPS(tb_switch_runtime_suspend, tb_switch_runtime_resume,
  937. NULL)
  938. };
  939. struct device_type tb_switch_type = {
  940. .name = "thunderbolt_device",
  941. .release = tb_switch_release,
  942. .pm = &tb_switch_pm_ops,
  943. };
  944. static int tb_switch_get_generation(struct tb_switch *sw)
  945. {
  946. switch (sw->config.device_id) {
  947. case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
  948. case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
  949. case PCI_DEVICE_ID_INTEL_LIGHT_PEAK:
  950. case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
  951. case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
  952. case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
  953. case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE:
  954. case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE:
  955. return 1;
  956. case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE:
  957. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
  958. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
  959. return 2;
  960. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
  961. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
  962. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
  963. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
  964. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
  965. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
  966. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
  967. case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
  968. return 3;
  969. default:
  970. /*
  971. * For unknown switches assume generation to be 1 to be
  972. * on the safe side.
  973. */
  974. tb_sw_warn(sw, "unsupported switch device id %#x\n",
  975. sw->config.device_id);
  976. return 1;
  977. }
  978. }
  979. /**
  980. * tb_switch_alloc() - allocate a switch
  981. * @tb: Pointer to the owning domain
  982. * @parent: Parent device for this switch
  983. * @route: Route string for this switch
  984. *
  985. * Allocates and initializes a switch. Will not upload configuration to
  986. * the switch. For that you need to call tb_switch_configure()
  987. * separately. The returned switch should be released by calling
  988. * tb_switch_put().
  989. *
  990. * Return: Pointer to the allocated switch or %NULL in case of failure
  991. */
  992. struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
  993. u64 route)
  994. {
  995. int i;
  996. int cap;
  997. struct tb_switch *sw;
  998. int upstream_port = tb_cfg_get_upstream_port(tb->ctl, route);
  999. if (upstream_port < 0)
  1000. return NULL;
  1001. sw = kzalloc(sizeof(*sw), GFP_KERNEL);
  1002. if (!sw)
  1003. return NULL;
  1004. sw->tb = tb;
  1005. if (tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5))
  1006. goto err_free_sw_ports;
  1007. tb_info(tb, "current switch config:\n");
  1008. tb_dump_switch(tb, &sw->config);
  1009. /* configure switch */
  1010. sw->config.upstream_port_number = upstream_port;
  1011. sw->config.depth = tb_route_length(route);
  1012. sw->config.route_lo = route;
  1013. sw->config.route_hi = route >> 32;
  1014. sw->config.enabled = 0;
  1015. /* initialize ports */
  1016. sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports),
  1017. GFP_KERNEL);
  1018. if (!sw->ports)
  1019. goto err_free_sw_ports;
  1020. for (i = 0; i <= sw->config.max_port_number; i++) {
  1021. /* minimum setup for tb_find_cap and tb_drom_read to work */
  1022. sw->ports[i].sw = sw;
  1023. sw->ports[i].port = i;
  1024. }
  1025. sw->generation = tb_switch_get_generation(sw);
  1026. cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS);
  1027. if (cap < 0) {
  1028. tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
  1029. goto err_free_sw_ports;
  1030. }
  1031. sw->cap_plug_events = cap;
  1032. /* Root switch is always authorized */
  1033. if (!route)
  1034. sw->authorized = true;
  1035. device_initialize(&sw->dev);
  1036. sw->dev.parent = parent;
  1037. sw->dev.bus = &tb_bus_type;
  1038. sw->dev.type = &tb_switch_type;
  1039. sw->dev.groups = switch_groups;
  1040. dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
  1041. return sw;
  1042. err_free_sw_ports:
  1043. kfree(sw->ports);
  1044. kfree(sw);
  1045. return NULL;
  1046. }
  1047. /**
  1048. * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
  1049. * @tb: Pointer to the owning domain
  1050. * @parent: Parent device for this switch
  1051. * @route: Route string for this switch
  1052. *
  1053. * This creates a switch in safe mode. This means the switch pretty much
  1054. * lacks all capabilities except DMA configuration port before it is
  1055. * flashed with a valid NVM firmware.
  1056. *
  1057. * The returned switch must be released by calling tb_switch_put().
  1058. *
  1059. * Return: Pointer to the allocated switch or %NULL in case of failure
  1060. */
  1061. struct tb_switch *
  1062. tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route)
  1063. {
  1064. struct tb_switch *sw;
  1065. sw = kzalloc(sizeof(*sw), GFP_KERNEL);
  1066. if (!sw)
  1067. return NULL;
  1068. sw->tb = tb;
  1069. sw->config.depth = tb_route_length(route);
  1070. sw->config.route_hi = upper_32_bits(route);
  1071. sw->config.route_lo = lower_32_bits(route);
  1072. sw->safe_mode = true;
  1073. device_initialize(&sw->dev);
  1074. sw->dev.parent = parent;
  1075. sw->dev.bus = &tb_bus_type;
  1076. sw->dev.type = &tb_switch_type;
  1077. sw->dev.groups = switch_groups;
  1078. dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
  1079. return sw;
  1080. }
  1081. /**
  1082. * tb_switch_configure() - Uploads configuration to the switch
  1083. * @sw: Switch to configure
  1084. *
  1085. * Call this function before the switch is added to the system. It will
  1086. * upload configuration to the switch and makes it available for the
  1087. * connection manager to use.
  1088. *
  1089. * Return: %0 in case of success and negative errno in case of failure
  1090. */
  1091. int tb_switch_configure(struct tb_switch *sw)
  1092. {
  1093. struct tb *tb = sw->tb;
  1094. u64 route;
  1095. int ret;
  1096. route = tb_route(sw);
  1097. tb_info(tb,
  1098. "initializing Switch at %#llx (depth: %d, up port: %d)\n",
  1099. route, tb_route_length(route), sw->config.upstream_port_number);
  1100. if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL)
  1101. tb_sw_warn(sw, "unknown switch vendor id %#x\n",
  1102. sw->config.vendor_id);
  1103. sw->config.enabled = 1;
  1104. /* upload configuration */
  1105. ret = tb_sw_write(sw, 1 + (u32 *)&sw->config, TB_CFG_SWITCH, 1, 3);
  1106. if (ret)
  1107. return ret;
  1108. return tb_plug_events_active(sw, true);
  1109. }
  1110. static int tb_switch_set_uuid(struct tb_switch *sw)
  1111. {
  1112. u32 uuid[4];
  1113. int cap, ret;
  1114. ret = 0;
  1115. if (sw->uuid)
  1116. return ret;
  1117. /*
  1118. * The newer controllers include fused UUID as part of link
  1119. * controller specific registers
  1120. */
  1121. cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER);
  1122. if (cap > 0) {
  1123. ret = tb_sw_read(sw, uuid, TB_CFG_SWITCH, cap + 3, 4);
  1124. if (ret)
  1125. return ret;
  1126. } else {
  1127. /*
  1128. * ICM generates UUID based on UID and fills the upper
  1129. * two words with ones. This is not strictly following
  1130. * UUID format but we want to be compatible with it so
  1131. * we do the same here.
  1132. */
  1133. uuid[0] = sw->uid & 0xffffffff;
  1134. uuid[1] = (sw->uid >> 32) & 0xffffffff;
  1135. uuid[2] = 0xffffffff;
  1136. uuid[3] = 0xffffffff;
  1137. }
  1138. sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
  1139. if (!sw->uuid)
  1140. ret = -ENOMEM;
  1141. return ret;
  1142. }
  1143. static int tb_switch_add_dma_port(struct tb_switch *sw)
  1144. {
  1145. u32 status;
  1146. int ret;
  1147. switch (sw->generation) {
  1148. case 2:
  1149. /* Only root switch can be upgraded */
  1150. if (tb_route(sw))
  1151. return 0;
  1152. /* fallthrough */
  1153. case 3:
  1154. ret = tb_switch_set_uuid(sw);
  1155. if (ret)
  1156. return ret;
  1157. break;
  1158. default:
  1159. /*
  1160. * DMA port is the only thing available when the switch
  1161. * is in safe mode.
  1162. */
  1163. if (!sw->safe_mode)
  1164. return 0;
  1165. break;
  1166. }
  1167. if (sw->no_nvm_upgrade)
  1168. return 0;
  1169. sw->dma_port = dma_port_alloc(sw);
  1170. if (!sw->dma_port)
  1171. return 0;
  1172. /*
  1173. * If there is status already set then authentication failed
  1174. * when the dma_port_flash_update_auth() returned. Power cycling
  1175. * is not needed (it was done already) so only thing we do here
  1176. * is to unblock runtime PM of the root port.
  1177. */
  1178. nvm_get_auth_status(sw, &status);
  1179. if (status) {
  1180. if (!tb_route(sw))
  1181. nvm_authenticate_complete(sw);
  1182. return 0;
  1183. }
  1184. /*
  1185. * Check status of the previous flash authentication. If there
  1186. * is one we need to power cycle the switch in any case to make
  1187. * it functional again.
  1188. */
  1189. ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
  1190. if (ret <= 0)
  1191. return ret;
  1192. /* Now we can allow root port to suspend again */
  1193. if (!tb_route(sw))
  1194. nvm_authenticate_complete(sw);
  1195. if (status) {
  1196. tb_sw_info(sw, "switch flash authentication failed\n");
  1197. nvm_set_auth_status(sw, status);
  1198. }
  1199. tb_sw_info(sw, "power cycling the switch now\n");
  1200. dma_port_power_cycle(sw->dma_port);
  1201. /*
  1202. * We return error here which causes the switch adding failure.
  1203. * It should appear back after power cycle is complete.
  1204. */
  1205. return -ESHUTDOWN;
  1206. }
  1207. /**
  1208. * tb_switch_add() - Add a switch to the domain
  1209. * @sw: Switch to add
  1210. *
  1211. * This is the last step in adding switch to the domain. It will read
  1212. * identification information from DROM and initializes ports so that
  1213. * they can be used to connect other switches. The switch will be
  1214. * exposed to the userspace when this function successfully returns. To
  1215. * remove and release the switch, call tb_switch_remove().
  1216. *
  1217. * Return: %0 in case of success and negative errno in case of failure
  1218. */
  1219. int tb_switch_add(struct tb_switch *sw)
  1220. {
  1221. int i, ret;
  1222. /*
  1223. * Initialize DMA control port now before we read DROM. Recent
  1224. * host controllers have more complete DROM on NVM that includes
  1225. * vendor and model identification strings which we then expose
  1226. * to the userspace. NVM can be accessed through DMA
  1227. * configuration based mailbox.
  1228. */
  1229. ret = tb_switch_add_dma_port(sw);
  1230. if (ret)
  1231. return ret;
  1232. if (!sw->safe_mode) {
  1233. /* read drom */
  1234. ret = tb_drom_read(sw);
  1235. if (ret) {
  1236. tb_sw_warn(sw, "tb_eeprom_read_rom failed\n");
  1237. return ret;
  1238. }
  1239. tb_sw_info(sw, "uid: %#llx\n", sw->uid);
  1240. ret = tb_switch_set_uuid(sw);
  1241. if (ret)
  1242. return ret;
  1243. for (i = 0; i <= sw->config.max_port_number; i++) {
  1244. if (sw->ports[i].disabled) {
  1245. tb_port_info(&sw->ports[i], "disabled by eeprom\n");
  1246. continue;
  1247. }
  1248. ret = tb_init_port(&sw->ports[i]);
  1249. if (ret)
  1250. return ret;
  1251. }
  1252. }
  1253. ret = device_add(&sw->dev);
  1254. if (ret)
  1255. return ret;
  1256. ret = tb_switch_nvm_add(sw);
  1257. if (ret) {
  1258. device_del(&sw->dev);
  1259. return ret;
  1260. }
  1261. pm_runtime_set_active(&sw->dev);
  1262. if (sw->rpm) {
  1263. pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY);
  1264. pm_runtime_use_autosuspend(&sw->dev);
  1265. pm_runtime_mark_last_busy(&sw->dev);
  1266. pm_runtime_enable(&sw->dev);
  1267. pm_request_autosuspend(&sw->dev);
  1268. }
  1269. return 0;
  1270. }
  1271. /**
  1272. * tb_switch_remove() - Remove and release a switch
  1273. * @sw: Switch to remove
  1274. *
  1275. * This will remove the switch from the domain and release it after last
  1276. * reference count drops to zero. If there are switches connected below
  1277. * this switch, they will be removed as well.
  1278. */
  1279. void tb_switch_remove(struct tb_switch *sw)
  1280. {
  1281. int i;
  1282. if (sw->rpm) {
  1283. pm_runtime_get_sync(&sw->dev);
  1284. pm_runtime_disable(&sw->dev);
  1285. }
  1286. /* port 0 is the switch itself and never has a remote */
  1287. for (i = 1; i <= sw->config.max_port_number; i++) {
  1288. if (tb_is_upstream_port(&sw->ports[i]))
  1289. continue;
  1290. if (sw->ports[i].remote)
  1291. tb_switch_remove(sw->ports[i].remote->sw);
  1292. sw->ports[i].remote = NULL;
  1293. if (sw->ports[i].xdomain)
  1294. tb_xdomain_remove(sw->ports[i].xdomain);
  1295. sw->ports[i].xdomain = NULL;
  1296. }
  1297. if (!sw->is_unplugged)
  1298. tb_plug_events_active(sw, false);
  1299. tb_switch_nvm_remove(sw);
  1300. device_unregister(&sw->dev);
  1301. }
  1302. /**
  1303. * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
  1304. */
  1305. void tb_sw_set_unplugged(struct tb_switch *sw)
  1306. {
  1307. int i;
  1308. if (sw == sw->tb->root_switch) {
  1309. tb_sw_WARN(sw, "cannot unplug root switch\n");
  1310. return;
  1311. }
  1312. if (sw->is_unplugged) {
  1313. tb_sw_WARN(sw, "is_unplugged already set\n");
  1314. return;
  1315. }
  1316. sw->is_unplugged = true;
  1317. for (i = 0; i <= sw->config.max_port_number; i++) {
  1318. if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
  1319. tb_sw_set_unplugged(sw->ports[i].remote->sw);
  1320. }
  1321. }
  1322. int tb_switch_resume(struct tb_switch *sw)
  1323. {
  1324. int i, err;
  1325. tb_sw_info(sw, "resuming switch\n");
  1326. /*
  1327. * Check for UID of the connected switches except for root
  1328. * switch which we assume cannot be removed.
  1329. */
  1330. if (tb_route(sw)) {
  1331. u64 uid;
  1332. err = tb_drom_read_uid_only(sw, &uid);
  1333. if (err) {
  1334. tb_sw_warn(sw, "uid read failed\n");
  1335. return err;
  1336. }
  1337. if (sw->uid != uid) {
  1338. tb_sw_info(sw,
  1339. "changed while suspended (uid %#llx -> %#llx)\n",
  1340. sw->uid, uid);
  1341. return -ENODEV;
  1342. }
  1343. }
  1344. /* upload configuration */
  1345. err = tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3);
  1346. if (err)
  1347. return err;
  1348. err = tb_plug_events_active(sw, true);
  1349. if (err)
  1350. return err;
  1351. /* check for surviving downstream switches */
  1352. for (i = 1; i <= sw->config.max_port_number; i++) {
  1353. struct tb_port *port = &sw->ports[i];
  1354. if (tb_is_upstream_port(port))
  1355. continue;
  1356. if (!port->remote)
  1357. continue;
  1358. if (tb_wait_for_port(port, true) <= 0
  1359. || tb_switch_resume(port->remote->sw)) {
  1360. tb_port_warn(port,
  1361. "lost during suspend, disconnecting\n");
  1362. tb_sw_set_unplugged(port->remote->sw);
  1363. }
  1364. }
  1365. return 0;
  1366. }
  1367. void tb_switch_suspend(struct tb_switch *sw)
  1368. {
  1369. int i, err;
  1370. err = tb_plug_events_active(sw, false);
  1371. if (err)
  1372. return;
  1373. for (i = 1; i <= sw->config.max_port_number; i++) {
  1374. if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
  1375. tb_switch_suspend(sw->ports[i].remote->sw);
  1376. }
  1377. /*
  1378. * TODO: invoke tb_cfg_prepare_to_sleep here? does not seem to have any
  1379. * effect?
  1380. */
  1381. }
  1382. struct tb_sw_lookup {
  1383. struct tb *tb;
  1384. u8 link;
  1385. u8 depth;
  1386. const uuid_t *uuid;
  1387. u64 route;
  1388. };
  1389. static int tb_switch_match(struct device *dev, void *data)
  1390. {
  1391. struct tb_switch *sw = tb_to_switch(dev);
  1392. struct tb_sw_lookup *lookup = data;
  1393. if (!sw)
  1394. return 0;
  1395. if (sw->tb != lookup->tb)
  1396. return 0;
  1397. if (lookup->uuid)
  1398. return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid));
  1399. if (lookup->route) {
  1400. return sw->config.route_lo == lower_32_bits(lookup->route) &&
  1401. sw->config.route_hi == upper_32_bits(lookup->route);
  1402. }
  1403. /* Root switch is matched only by depth */
  1404. if (!lookup->depth)
  1405. return !sw->depth;
  1406. return sw->link == lookup->link && sw->depth == lookup->depth;
  1407. }
  1408. /**
  1409. * tb_switch_find_by_link_depth() - Find switch by link and depth
  1410. * @tb: Domain the switch belongs
  1411. * @link: Link number the switch is connected
  1412. * @depth: Depth of the switch in link
  1413. *
  1414. * Returned switch has reference count increased so the caller needs to
  1415. * call tb_switch_put() when done with the switch.
  1416. */
  1417. struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth)
  1418. {
  1419. struct tb_sw_lookup lookup;
  1420. struct device *dev;
  1421. memset(&lookup, 0, sizeof(lookup));
  1422. lookup.tb = tb;
  1423. lookup.link = link;
  1424. lookup.depth = depth;
  1425. dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
  1426. if (dev)
  1427. return tb_to_switch(dev);
  1428. return NULL;
  1429. }
  1430. /**
  1431. * tb_switch_find_by_uuid() - Find switch by UUID
  1432. * @tb: Domain the switch belongs
  1433. * @uuid: UUID to look for
  1434. *
  1435. * Returned switch has reference count increased so the caller needs to
  1436. * call tb_switch_put() when done with the switch.
  1437. */
  1438. struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid)
  1439. {
  1440. struct tb_sw_lookup lookup;
  1441. struct device *dev;
  1442. memset(&lookup, 0, sizeof(lookup));
  1443. lookup.tb = tb;
  1444. lookup.uuid = uuid;
  1445. dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
  1446. if (dev)
  1447. return tb_to_switch(dev);
  1448. return NULL;
  1449. }
  1450. /**
  1451. * tb_switch_find_by_route() - Find switch by route string
  1452. * @tb: Domain the switch belongs
  1453. * @route: Route string to look for
  1454. *
  1455. * Returned switch has reference count increased so the caller needs to
  1456. * call tb_switch_put() when done with the switch.
  1457. */
  1458. struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route)
  1459. {
  1460. struct tb_sw_lookup lookup;
  1461. struct device *dev;
  1462. if (!route)
  1463. return tb_switch_get(tb->root_switch);
  1464. memset(&lookup, 0, sizeof(lookup));
  1465. lookup.tb = tb;
  1466. lookup.route = route;
  1467. dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
  1468. if (dev)
  1469. return tb_to_switch(dev);
  1470. return NULL;
  1471. }
  1472. void tb_switch_exit(void)
  1473. {
  1474. ida_destroy(&nvm_ida);
  1475. }