switch.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  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. /*
  612. * Make sure there is no PCIe rescan ongoing when a new PCIe
  613. * tunnel is created. Otherwise the PCIe rescan code might find
  614. * the new tunnel too early.
  615. */
  616. pci_lock_rescan_remove();
  617. switch (val) {
  618. /* Approve switch */
  619. case 1:
  620. if (sw->key)
  621. ret = tb_domain_approve_switch_key(sw->tb, sw);
  622. else
  623. ret = tb_domain_approve_switch(sw->tb, sw);
  624. break;
  625. /* Challenge switch */
  626. case 2:
  627. if (sw->key)
  628. ret = tb_domain_challenge_switch_key(sw->tb, sw);
  629. break;
  630. default:
  631. break;
  632. }
  633. pci_unlock_rescan_remove();
  634. if (!ret) {
  635. sw->authorized = val;
  636. /* Notify status change to the userspace */
  637. kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
  638. }
  639. unlock:
  640. mutex_unlock(&switch_lock);
  641. return ret;
  642. }
  643. static ssize_t authorized_store(struct device *dev,
  644. struct device_attribute *attr,
  645. const char *buf, size_t count)
  646. {
  647. struct tb_switch *sw = tb_to_switch(dev);
  648. unsigned int val;
  649. ssize_t ret;
  650. ret = kstrtouint(buf, 0, &val);
  651. if (ret)
  652. return ret;
  653. if (val > 2)
  654. return -EINVAL;
  655. ret = tb_switch_set_authorized(sw, val);
  656. return ret ? ret : count;
  657. }
  658. static DEVICE_ATTR_RW(authorized);
  659. static ssize_t device_show(struct device *dev, struct device_attribute *attr,
  660. char *buf)
  661. {
  662. struct tb_switch *sw = tb_to_switch(dev);
  663. return sprintf(buf, "%#x\n", sw->device);
  664. }
  665. static DEVICE_ATTR_RO(device);
  666. static ssize_t
  667. device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
  668. {
  669. struct tb_switch *sw = tb_to_switch(dev);
  670. return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : "");
  671. }
  672. static DEVICE_ATTR_RO(device_name);
  673. static ssize_t key_show(struct device *dev, struct device_attribute *attr,
  674. char *buf)
  675. {
  676. struct tb_switch *sw = tb_to_switch(dev);
  677. ssize_t ret;
  678. if (mutex_lock_interruptible(&switch_lock))
  679. return -ERESTARTSYS;
  680. if (sw->key)
  681. ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key);
  682. else
  683. ret = sprintf(buf, "\n");
  684. mutex_unlock(&switch_lock);
  685. return ret;
  686. }
  687. static ssize_t key_store(struct device *dev, struct device_attribute *attr,
  688. const char *buf, size_t count)
  689. {
  690. struct tb_switch *sw = tb_to_switch(dev);
  691. u8 key[TB_SWITCH_KEY_SIZE];
  692. ssize_t ret = count;
  693. bool clear = false;
  694. if (!strcmp(buf, "\n"))
  695. clear = true;
  696. else if (hex2bin(key, buf, sizeof(key)))
  697. return -EINVAL;
  698. if (mutex_lock_interruptible(&switch_lock))
  699. return -ERESTARTSYS;
  700. if (sw->authorized) {
  701. ret = -EBUSY;
  702. } else {
  703. kfree(sw->key);
  704. if (clear) {
  705. sw->key = NULL;
  706. } else {
  707. sw->key = kmemdup(key, sizeof(key), GFP_KERNEL);
  708. if (!sw->key)
  709. ret = -ENOMEM;
  710. }
  711. }
  712. mutex_unlock(&switch_lock);
  713. return ret;
  714. }
  715. static DEVICE_ATTR(key, 0600, key_show, key_store);
  716. static ssize_t nvm_authenticate_show(struct device *dev,
  717. struct device_attribute *attr, char *buf)
  718. {
  719. struct tb_switch *sw = tb_to_switch(dev);
  720. u32 status;
  721. nvm_get_auth_status(sw, &status);
  722. return sprintf(buf, "%#x\n", status);
  723. }
  724. static ssize_t nvm_authenticate_store(struct device *dev,
  725. struct device_attribute *attr, const char *buf, size_t count)
  726. {
  727. struct tb_switch *sw = tb_to_switch(dev);
  728. bool val;
  729. int ret;
  730. if (mutex_lock_interruptible(&switch_lock))
  731. return -ERESTARTSYS;
  732. /* If NVMem devices are not yet added */
  733. if (!sw->nvm) {
  734. ret = -EAGAIN;
  735. goto exit_unlock;
  736. }
  737. ret = kstrtobool(buf, &val);
  738. if (ret)
  739. goto exit_unlock;
  740. /* Always clear the authentication status */
  741. nvm_clear_auth_status(sw);
  742. if (val) {
  743. ret = nvm_validate_and_write(sw);
  744. if (ret)
  745. goto exit_unlock;
  746. sw->nvm->authenticating = true;
  747. if (!tb_route(sw))
  748. ret = nvm_authenticate_host(sw);
  749. else
  750. ret = nvm_authenticate_device(sw);
  751. }
  752. exit_unlock:
  753. mutex_unlock(&switch_lock);
  754. if (ret)
  755. return ret;
  756. return count;
  757. }
  758. static DEVICE_ATTR_RW(nvm_authenticate);
  759. static ssize_t nvm_version_show(struct device *dev,
  760. struct device_attribute *attr, char *buf)
  761. {
  762. struct tb_switch *sw = tb_to_switch(dev);
  763. int ret;
  764. if (mutex_lock_interruptible(&switch_lock))
  765. return -ERESTARTSYS;
  766. if (sw->safe_mode)
  767. ret = -ENODATA;
  768. else if (!sw->nvm)
  769. ret = -EAGAIN;
  770. else
  771. ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);
  772. mutex_unlock(&switch_lock);
  773. return ret;
  774. }
  775. static DEVICE_ATTR_RO(nvm_version);
  776. static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
  777. char *buf)
  778. {
  779. struct tb_switch *sw = tb_to_switch(dev);
  780. return sprintf(buf, "%#x\n", sw->vendor);
  781. }
  782. static DEVICE_ATTR_RO(vendor);
  783. static ssize_t
  784. vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
  785. {
  786. struct tb_switch *sw = tb_to_switch(dev);
  787. return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : "");
  788. }
  789. static DEVICE_ATTR_RO(vendor_name);
  790. static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
  791. char *buf)
  792. {
  793. struct tb_switch *sw = tb_to_switch(dev);
  794. return sprintf(buf, "%pUb\n", sw->uuid);
  795. }
  796. static DEVICE_ATTR_RO(unique_id);
  797. static struct attribute *switch_attrs[] = {
  798. &dev_attr_authorized.attr,
  799. &dev_attr_device.attr,
  800. &dev_attr_device_name.attr,
  801. &dev_attr_key.attr,
  802. &dev_attr_nvm_authenticate.attr,
  803. &dev_attr_nvm_version.attr,
  804. &dev_attr_vendor.attr,
  805. &dev_attr_vendor_name.attr,
  806. &dev_attr_unique_id.attr,
  807. NULL,
  808. };
  809. static umode_t switch_attr_is_visible(struct kobject *kobj,
  810. struct attribute *attr, int n)
  811. {
  812. struct device *dev = container_of(kobj, struct device, kobj);
  813. struct tb_switch *sw = tb_to_switch(dev);
  814. if (attr == &dev_attr_key.attr) {
  815. if (tb_route(sw) &&
  816. sw->tb->security_level == TB_SECURITY_SECURE &&
  817. sw->security_level == TB_SECURITY_SECURE)
  818. return attr->mode;
  819. return 0;
  820. } else if (attr == &dev_attr_nvm_authenticate.attr ||
  821. attr == &dev_attr_nvm_version.attr) {
  822. if (sw->dma_port)
  823. return attr->mode;
  824. return 0;
  825. }
  826. return sw->safe_mode ? 0 : attr->mode;
  827. }
  828. static struct attribute_group switch_group = {
  829. .is_visible = switch_attr_is_visible,
  830. .attrs = switch_attrs,
  831. };
  832. static const struct attribute_group *switch_groups[] = {
  833. &switch_group,
  834. NULL,
  835. };
  836. static void tb_switch_release(struct device *dev)
  837. {
  838. struct tb_switch *sw = tb_to_switch(dev);
  839. dma_port_free(sw->dma_port);
  840. kfree(sw->uuid);
  841. kfree(sw->device_name);
  842. kfree(sw->vendor_name);
  843. kfree(sw->ports);
  844. kfree(sw->drom);
  845. kfree(sw->key);
  846. kfree(sw);
  847. }
  848. struct device_type tb_switch_type = {
  849. .name = "thunderbolt_device",
  850. .release = tb_switch_release,
  851. };
  852. static int tb_switch_get_generation(struct tb_switch *sw)
  853. {
  854. switch (sw->config.device_id) {
  855. case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
  856. case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
  857. case PCI_DEVICE_ID_INTEL_LIGHT_PEAK:
  858. case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
  859. case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
  860. case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
  861. case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE:
  862. case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE:
  863. return 1;
  864. case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE:
  865. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
  866. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
  867. return 2;
  868. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
  869. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
  870. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
  871. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
  872. case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
  873. return 3;
  874. default:
  875. /*
  876. * For unknown switches assume generation to be 1 to be
  877. * on the safe side.
  878. */
  879. tb_sw_warn(sw, "unsupported switch device id %#x\n",
  880. sw->config.device_id);
  881. return 1;
  882. }
  883. }
  884. /**
  885. * tb_switch_alloc() - allocate a switch
  886. * @tb: Pointer to the owning domain
  887. * @parent: Parent device for this switch
  888. * @route: Route string for this switch
  889. *
  890. * Allocates and initializes a switch. Will not upload configuration to
  891. * the switch. For that you need to call tb_switch_configure()
  892. * separately. The returned switch should be released by calling
  893. * tb_switch_put().
  894. *
  895. * Return: Pointer to the allocated switch or %NULL in case of failure
  896. */
  897. struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
  898. u64 route)
  899. {
  900. int i;
  901. int cap;
  902. struct tb_switch *sw;
  903. int upstream_port = tb_cfg_get_upstream_port(tb->ctl, route);
  904. if (upstream_port < 0)
  905. return NULL;
  906. sw = kzalloc(sizeof(*sw), GFP_KERNEL);
  907. if (!sw)
  908. return NULL;
  909. sw->tb = tb;
  910. if (tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5))
  911. goto err_free_sw_ports;
  912. tb_info(tb, "current switch config:\n");
  913. tb_dump_switch(tb, &sw->config);
  914. /* configure switch */
  915. sw->config.upstream_port_number = upstream_port;
  916. sw->config.depth = tb_route_length(route);
  917. sw->config.route_lo = route;
  918. sw->config.route_hi = route >> 32;
  919. sw->config.enabled = 0;
  920. /* initialize ports */
  921. sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports),
  922. GFP_KERNEL);
  923. if (!sw->ports)
  924. goto err_free_sw_ports;
  925. for (i = 0; i <= sw->config.max_port_number; i++) {
  926. /* minimum setup for tb_find_cap and tb_drom_read to work */
  927. sw->ports[i].sw = sw;
  928. sw->ports[i].port = i;
  929. }
  930. sw->generation = tb_switch_get_generation(sw);
  931. cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS);
  932. if (cap < 0) {
  933. tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
  934. goto err_free_sw_ports;
  935. }
  936. sw->cap_plug_events = cap;
  937. /* Root switch is always authorized */
  938. if (!route)
  939. sw->authorized = true;
  940. device_initialize(&sw->dev);
  941. sw->dev.parent = parent;
  942. sw->dev.bus = &tb_bus_type;
  943. sw->dev.type = &tb_switch_type;
  944. sw->dev.groups = switch_groups;
  945. dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
  946. return sw;
  947. err_free_sw_ports:
  948. kfree(sw->ports);
  949. kfree(sw);
  950. return NULL;
  951. }
  952. /**
  953. * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
  954. * @tb: Pointer to the owning domain
  955. * @parent: Parent device for this switch
  956. * @route: Route string for this switch
  957. *
  958. * This creates a switch in safe mode. This means the switch pretty much
  959. * lacks all capabilities except DMA configuration port before it is
  960. * flashed with a valid NVM firmware.
  961. *
  962. * The returned switch must be released by calling tb_switch_put().
  963. *
  964. * Return: Pointer to the allocated switch or %NULL in case of failure
  965. */
  966. struct tb_switch *
  967. tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route)
  968. {
  969. struct tb_switch *sw;
  970. sw = kzalloc(sizeof(*sw), GFP_KERNEL);
  971. if (!sw)
  972. return NULL;
  973. sw->tb = tb;
  974. sw->config.depth = tb_route_length(route);
  975. sw->config.route_hi = upper_32_bits(route);
  976. sw->config.route_lo = lower_32_bits(route);
  977. sw->safe_mode = true;
  978. device_initialize(&sw->dev);
  979. sw->dev.parent = parent;
  980. sw->dev.bus = &tb_bus_type;
  981. sw->dev.type = &tb_switch_type;
  982. sw->dev.groups = switch_groups;
  983. dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
  984. return sw;
  985. }
  986. /**
  987. * tb_switch_configure() - Uploads configuration to the switch
  988. * @sw: Switch to configure
  989. *
  990. * Call this function before the switch is added to the system. It will
  991. * upload configuration to the switch and makes it available for the
  992. * connection manager to use.
  993. *
  994. * Return: %0 in case of success and negative errno in case of failure
  995. */
  996. int tb_switch_configure(struct tb_switch *sw)
  997. {
  998. struct tb *tb = sw->tb;
  999. u64 route;
  1000. int ret;
  1001. route = tb_route(sw);
  1002. tb_info(tb,
  1003. "initializing Switch at %#llx (depth: %d, up port: %d)\n",
  1004. route, tb_route_length(route), sw->config.upstream_port_number);
  1005. if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL)
  1006. tb_sw_warn(sw, "unknown switch vendor id %#x\n",
  1007. sw->config.vendor_id);
  1008. sw->config.enabled = 1;
  1009. /* upload configuration */
  1010. ret = tb_sw_write(sw, 1 + (u32 *)&sw->config, TB_CFG_SWITCH, 1, 3);
  1011. if (ret)
  1012. return ret;
  1013. return tb_plug_events_active(sw, true);
  1014. }
  1015. static void tb_switch_set_uuid(struct tb_switch *sw)
  1016. {
  1017. u32 uuid[4];
  1018. int cap;
  1019. if (sw->uuid)
  1020. return;
  1021. /*
  1022. * The newer controllers include fused UUID as part of link
  1023. * controller specific registers
  1024. */
  1025. cap = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER);
  1026. if (cap > 0) {
  1027. tb_sw_read(sw, uuid, TB_CFG_SWITCH, cap + 3, 4);
  1028. } else {
  1029. /*
  1030. * ICM generates UUID based on UID and fills the upper
  1031. * two words with ones. This is not strictly following
  1032. * UUID format but we want to be compatible with it so
  1033. * we do the same here.
  1034. */
  1035. uuid[0] = sw->uid & 0xffffffff;
  1036. uuid[1] = (sw->uid >> 32) & 0xffffffff;
  1037. uuid[2] = 0xffffffff;
  1038. uuid[3] = 0xffffffff;
  1039. }
  1040. sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
  1041. }
  1042. static int tb_switch_add_dma_port(struct tb_switch *sw)
  1043. {
  1044. u32 status;
  1045. int ret;
  1046. switch (sw->generation) {
  1047. case 3:
  1048. break;
  1049. case 2:
  1050. /* Only root switch can be upgraded */
  1051. if (tb_route(sw))
  1052. return 0;
  1053. break;
  1054. default:
  1055. /*
  1056. * DMA port is the only thing available when the switch
  1057. * is in safe mode.
  1058. */
  1059. if (!sw->safe_mode)
  1060. return 0;
  1061. break;
  1062. }
  1063. if (sw->no_nvm_upgrade)
  1064. return 0;
  1065. sw->dma_port = dma_port_alloc(sw);
  1066. if (!sw->dma_port)
  1067. return 0;
  1068. /*
  1069. * Check status of the previous flash authentication. If there
  1070. * is one we need to power cycle the switch in any case to make
  1071. * it functional again.
  1072. */
  1073. ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
  1074. if (ret <= 0)
  1075. return ret;
  1076. if (status) {
  1077. tb_sw_info(sw, "switch flash authentication failed\n");
  1078. tb_switch_set_uuid(sw);
  1079. nvm_set_auth_status(sw, status);
  1080. }
  1081. tb_sw_info(sw, "power cycling the switch now\n");
  1082. dma_port_power_cycle(sw->dma_port);
  1083. /*
  1084. * We return error here which causes the switch adding failure.
  1085. * It should appear back after power cycle is complete.
  1086. */
  1087. return -ESHUTDOWN;
  1088. }
  1089. /**
  1090. * tb_switch_add() - Add a switch to the domain
  1091. * @sw: Switch to add
  1092. *
  1093. * This is the last step in adding switch to the domain. It will read
  1094. * identification information from DROM and initializes ports so that
  1095. * they can be used to connect other switches. The switch will be
  1096. * exposed to the userspace when this function successfully returns. To
  1097. * remove and release the switch, call tb_switch_remove().
  1098. *
  1099. * Return: %0 in case of success and negative errno in case of failure
  1100. */
  1101. int tb_switch_add(struct tb_switch *sw)
  1102. {
  1103. int i, ret;
  1104. /*
  1105. * Initialize DMA control port now before we read DROM. Recent
  1106. * host controllers have more complete DROM on NVM that includes
  1107. * vendor and model identification strings which we then expose
  1108. * to the userspace. NVM can be accessed through DMA
  1109. * configuration based mailbox.
  1110. */
  1111. ret = tb_switch_add_dma_port(sw);
  1112. if (ret)
  1113. return ret;
  1114. if (!sw->safe_mode) {
  1115. /* read drom */
  1116. ret = tb_drom_read(sw);
  1117. if (ret) {
  1118. tb_sw_warn(sw, "tb_eeprom_read_rom failed\n");
  1119. return ret;
  1120. }
  1121. tb_sw_info(sw, "uid: %#llx\n", sw->uid);
  1122. tb_switch_set_uuid(sw);
  1123. for (i = 0; i <= sw->config.max_port_number; i++) {
  1124. if (sw->ports[i].disabled) {
  1125. tb_port_info(&sw->ports[i], "disabled by eeprom\n");
  1126. continue;
  1127. }
  1128. ret = tb_init_port(&sw->ports[i]);
  1129. if (ret)
  1130. return ret;
  1131. }
  1132. }
  1133. ret = device_add(&sw->dev);
  1134. if (ret)
  1135. return ret;
  1136. ret = tb_switch_nvm_add(sw);
  1137. if (ret)
  1138. device_del(&sw->dev);
  1139. return ret;
  1140. }
  1141. /**
  1142. * tb_switch_remove() - Remove and release a switch
  1143. * @sw: Switch to remove
  1144. *
  1145. * This will remove the switch from the domain and release it after last
  1146. * reference count drops to zero. If there are switches connected below
  1147. * this switch, they will be removed as well.
  1148. */
  1149. void tb_switch_remove(struct tb_switch *sw)
  1150. {
  1151. int i;
  1152. /* port 0 is the switch itself and never has a remote */
  1153. for (i = 1; i <= sw->config.max_port_number; i++) {
  1154. if (tb_is_upstream_port(&sw->ports[i]))
  1155. continue;
  1156. if (sw->ports[i].remote)
  1157. tb_switch_remove(sw->ports[i].remote->sw);
  1158. sw->ports[i].remote = NULL;
  1159. if (sw->ports[i].xdomain)
  1160. tb_xdomain_remove(sw->ports[i].xdomain);
  1161. sw->ports[i].xdomain = NULL;
  1162. }
  1163. if (!sw->is_unplugged)
  1164. tb_plug_events_active(sw, false);
  1165. tb_switch_nvm_remove(sw);
  1166. device_unregister(&sw->dev);
  1167. }
  1168. /**
  1169. * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
  1170. */
  1171. void tb_sw_set_unplugged(struct tb_switch *sw)
  1172. {
  1173. int i;
  1174. if (sw == sw->tb->root_switch) {
  1175. tb_sw_WARN(sw, "cannot unplug root switch\n");
  1176. return;
  1177. }
  1178. if (sw->is_unplugged) {
  1179. tb_sw_WARN(sw, "is_unplugged already set\n");
  1180. return;
  1181. }
  1182. sw->is_unplugged = true;
  1183. for (i = 0; i <= sw->config.max_port_number; i++) {
  1184. if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
  1185. tb_sw_set_unplugged(sw->ports[i].remote->sw);
  1186. }
  1187. }
  1188. int tb_switch_resume(struct tb_switch *sw)
  1189. {
  1190. int i, err;
  1191. tb_sw_info(sw, "resuming switch\n");
  1192. /*
  1193. * Check for UID of the connected switches except for root
  1194. * switch which we assume cannot be removed.
  1195. */
  1196. if (tb_route(sw)) {
  1197. u64 uid;
  1198. err = tb_drom_read_uid_only(sw, &uid);
  1199. if (err) {
  1200. tb_sw_warn(sw, "uid read failed\n");
  1201. return err;
  1202. }
  1203. if (sw->uid != uid) {
  1204. tb_sw_info(sw,
  1205. "changed while suspended (uid %#llx -> %#llx)\n",
  1206. sw->uid, uid);
  1207. return -ENODEV;
  1208. }
  1209. }
  1210. /* upload configuration */
  1211. err = tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3);
  1212. if (err)
  1213. return err;
  1214. err = tb_plug_events_active(sw, true);
  1215. if (err)
  1216. return err;
  1217. /* check for surviving downstream switches */
  1218. for (i = 1; i <= sw->config.max_port_number; i++) {
  1219. struct tb_port *port = &sw->ports[i];
  1220. if (tb_is_upstream_port(port))
  1221. continue;
  1222. if (!port->remote)
  1223. continue;
  1224. if (tb_wait_for_port(port, true) <= 0
  1225. || tb_switch_resume(port->remote->sw)) {
  1226. tb_port_warn(port,
  1227. "lost during suspend, disconnecting\n");
  1228. tb_sw_set_unplugged(port->remote->sw);
  1229. }
  1230. }
  1231. return 0;
  1232. }
  1233. void tb_switch_suspend(struct tb_switch *sw)
  1234. {
  1235. int i, err;
  1236. err = tb_plug_events_active(sw, false);
  1237. if (err)
  1238. return;
  1239. for (i = 1; i <= sw->config.max_port_number; i++) {
  1240. if (!tb_is_upstream_port(&sw->ports[i]) && sw->ports[i].remote)
  1241. tb_switch_suspend(sw->ports[i].remote->sw);
  1242. }
  1243. /*
  1244. * TODO: invoke tb_cfg_prepare_to_sleep here? does not seem to have any
  1245. * effect?
  1246. */
  1247. }
  1248. struct tb_sw_lookup {
  1249. struct tb *tb;
  1250. u8 link;
  1251. u8 depth;
  1252. const uuid_t *uuid;
  1253. };
  1254. static int tb_switch_match(struct device *dev, void *data)
  1255. {
  1256. struct tb_switch *sw = tb_to_switch(dev);
  1257. struct tb_sw_lookup *lookup = data;
  1258. if (!sw)
  1259. return 0;
  1260. if (sw->tb != lookup->tb)
  1261. return 0;
  1262. if (lookup->uuid)
  1263. return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid));
  1264. /* Root switch is matched only by depth */
  1265. if (!lookup->depth)
  1266. return !sw->depth;
  1267. return sw->link == lookup->link && sw->depth == lookup->depth;
  1268. }
  1269. /**
  1270. * tb_switch_find_by_link_depth() - Find switch by link and depth
  1271. * @tb: Domain the switch belongs
  1272. * @link: Link number the switch is connected
  1273. * @depth: Depth of the switch in link
  1274. *
  1275. * Returned switch has reference count increased so the caller needs to
  1276. * call tb_switch_put() when done with the switch.
  1277. */
  1278. struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth)
  1279. {
  1280. struct tb_sw_lookup lookup;
  1281. struct device *dev;
  1282. memset(&lookup, 0, sizeof(lookup));
  1283. lookup.tb = tb;
  1284. lookup.link = link;
  1285. lookup.depth = depth;
  1286. dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
  1287. if (dev)
  1288. return tb_to_switch(dev);
  1289. return NULL;
  1290. }
  1291. /**
  1292. * tb_switch_find_by_link_depth() - Find switch by UUID
  1293. * @tb: Domain the switch belongs
  1294. * @uuid: UUID to look for
  1295. *
  1296. * Returned switch has reference count increased so the caller needs to
  1297. * call tb_switch_put() when done with the switch.
  1298. */
  1299. struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid)
  1300. {
  1301. struct tb_sw_lookup lookup;
  1302. struct device *dev;
  1303. memset(&lookup, 0, sizeof(lookup));
  1304. lookup.tb = tb;
  1305. lookup.uuid = uuid;
  1306. dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
  1307. if (dev)
  1308. return tb_to_switch(dev);
  1309. return NULL;
  1310. }
  1311. void tb_switch_exit(void)
  1312. {
  1313. ida_destroy(&nvm_ida);
  1314. }