switch.c 38 KB

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