switch.c 35 KB

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