switch.c 40 KB

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