switch.c 37 KB

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