switch.c 39 KB

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