switch.c 35 KB

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