switch.c 39 KB

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