eeprom.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Thunderbolt driver - eeprom access
  4. *
  5. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  6. * Copyright (C) 2018, Intel Corporation
  7. */
  8. #include <linux/crc32.h>
  9. #include <linux/delay.h>
  10. #include <linux/property.h>
  11. #include <linux/slab.h>
  12. #include "tb.h"
  13. /*
  14. * tb_eeprom_ctl_write() - write control word
  15. */
  16. static int tb_eeprom_ctl_write(struct tb_switch *sw, struct tb_eeprom_ctl *ctl)
  17. {
  18. return tb_sw_write(sw, ctl, TB_CFG_SWITCH, sw->cap_plug_events + ROUTER_CS_4, 1);
  19. }
  20. /*
  21. * tb_eeprom_ctl_read() - read control word
  22. */
  23. static int tb_eeprom_ctl_read(struct tb_switch *sw, struct tb_eeprom_ctl *ctl)
  24. {
  25. return tb_sw_read(sw, ctl, TB_CFG_SWITCH, sw->cap_plug_events + ROUTER_CS_4, 1);
  26. }
  27. enum tb_eeprom_transfer {
  28. TB_EEPROM_IN,
  29. TB_EEPROM_OUT,
  30. };
  31. /*
  32. * tb_eeprom_active - enable rom access
  33. *
  34. * WARNING: Always disable access after usage. Otherwise the controller will
  35. * fail to reprobe.
  36. */
  37. static int tb_eeprom_active(struct tb_switch *sw, bool enable)
  38. {
  39. struct tb_eeprom_ctl ctl;
  40. int res = tb_eeprom_ctl_read(sw, &ctl);
  41. if (res)
  42. return res;
  43. if (enable) {
  44. ctl.bit_banging_enable = 1;
  45. res = tb_eeprom_ctl_write(sw, &ctl);
  46. if (res)
  47. return res;
  48. ctl.fl_cs = 0;
  49. return tb_eeprom_ctl_write(sw, &ctl);
  50. } else {
  51. ctl.fl_cs = 1;
  52. res = tb_eeprom_ctl_write(sw, &ctl);
  53. if (res)
  54. return res;
  55. ctl.bit_banging_enable = 0;
  56. return tb_eeprom_ctl_write(sw, &ctl);
  57. }
  58. }
  59. /*
  60. * tb_eeprom_transfer - transfer one bit
  61. *
  62. * If TB_EEPROM_IN is passed, then the bit can be retrieved from ctl->fl_do.
  63. * If TB_EEPROM_OUT is passed, then ctl->fl_di will be written.
  64. */
  65. static int tb_eeprom_transfer(struct tb_switch *sw, struct tb_eeprom_ctl *ctl,
  66. enum tb_eeprom_transfer direction)
  67. {
  68. int res;
  69. if (direction == TB_EEPROM_OUT) {
  70. res = tb_eeprom_ctl_write(sw, ctl);
  71. if (res)
  72. return res;
  73. }
  74. ctl->fl_sk = 1;
  75. res = tb_eeprom_ctl_write(sw, ctl);
  76. if (res)
  77. return res;
  78. if (direction == TB_EEPROM_IN) {
  79. res = tb_eeprom_ctl_read(sw, ctl);
  80. if (res)
  81. return res;
  82. }
  83. ctl->fl_sk = 0;
  84. return tb_eeprom_ctl_write(sw, ctl);
  85. }
  86. /*
  87. * tb_eeprom_out - write one byte to the bus
  88. */
  89. static int tb_eeprom_out(struct tb_switch *sw, u8 val)
  90. {
  91. struct tb_eeprom_ctl ctl;
  92. int i;
  93. int res = tb_eeprom_ctl_read(sw, &ctl);
  94. if (res)
  95. return res;
  96. for (i = 0; i < 8; i++) {
  97. ctl.fl_di = val & 0x80;
  98. res = tb_eeprom_transfer(sw, &ctl, TB_EEPROM_OUT);
  99. if (res)
  100. return res;
  101. val <<= 1;
  102. }
  103. return 0;
  104. }
  105. /*
  106. * tb_eeprom_in - read one byte from the bus
  107. */
  108. static int tb_eeprom_in(struct tb_switch *sw, u8 *val)
  109. {
  110. struct tb_eeprom_ctl ctl;
  111. int i;
  112. int res = tb_eeprom_ctl_read(sw, &ctl);
  113. if (res)
  114. return res;
  115. *val = 0;
  116. for (i = 0; i < 8; i++) {
  117. *val <<= 1;
  118. res = tb_eeprom_transfer(sw, &ctl, TB_EEPROM_IN);
  119. if (res)
  120. return res;
  121. *val |= ctl.fl_do;
  122. }
  123. return 0;
  124. }
  125. /*
  126. * tb_eeprom_get_drom_offset - get drom offset within eeprom
  127. */
  128. static int tb_eeprom_get_drom_offset(struct tb_switch *sw, u16 *offset)
  129. {
  130. struct tb_cap_plug_events cap;
  131. int res;
  132. if (!sw->cap_plug_events) {
  133. tb_sw_warn(sw, "no TB_CAP_PLUG_EVENTS, cannot read eeprom\n");
  134. return -ENODEV;
  135. }
  136. res = tb_sw_read(sw, &cap, TB_CFG_SWITCH, sw->cap_plug_events,
  137. sizeof(cap) / 4);
  138. if (res)
  139. return res;
  140. if (!cap.eeprom_ctl.present || cap.eeprom_ctl.not_present) {
  141. tb_sw_warn(sw, "no NVM\n");
  142. return -ENODEV;
  143. }
  144. if (cap.drom_offset > 0xffff) {
  145. tb_sw_warn(sw, "drom offset is larger than 0xffff: %#x\n",
  146. cap.drom_offset);
  147. return -ENXIO;
  148. }
  149. *offset = cap.drom_offset;
  150. return 0;
  151. }
  152. /*
  153. * tb_eeprom_read_n - read count bytes from offset into val
  154. */
  155. static int tb_eeprom_read_n(struct tb_switch *sw, u16 offset, u8 *val,
  156. size_t count)
  157. {
  158. u16 drom_offset;
  159. int i, res;
  160. res = tb_eeprom_get_drom_offset(sw, &drom_offset);
  161. if (res)
  162. return res;
  163. offset += drom_offset;
  164. res = tb_eeprom_active(sw, true);
  165. if (res)
  166. return res;
  167. res = tb_eeprom_out(sw, 3);
  168. if (res)
  169. return res;
  170. res = tb_eeprom_out(sw, offset >> 8);
  171. if (res)
  172. return res;
  173. res = tb_eeprom_out(sw, offset);
  174. if (res)
  175. return res;
  176. for (i = 0; i < count; i++) {
  177. res = tb_eeprom_in(sw, val + i);
  178. if (res)
  179. return res;
  180. }
  181. return tb_eeprom_active(sw, false);
  182. }
  183. static u8 tb_crc8(u8 *data, int len)
  184. {
  185. int i, j;
  186. u8 val = 0xff;
  187. for (i = 0; i < len; i++) {
  188. val ^= data[i];
  189. for (j = 0; j < 8; j++)
  190. val = (val << 1) ^ ((val & 0x80) ? 7 : 0);
  191. }
  192. return val;
  193. }
  194. static u32 tb_crc32(void *data, size_t len)
  195. {
  196. return ~crc32c(~0, data, len);
  197. }
  198. #define TB_DROM_DATA_START 13
  199. #define TB_DROM_HEADER_SIZE 22
  200. #define USB4_DROM_HEADER_SIZE 16
  201. struct tb_drom_header {
  202. /* BYTE 0 */
  203. u8 uid_crc8; /* checksum for uid */
  204. /* BYTES 1-8 */
  205. u64 uid;
  206. /* BYTES 9-12 */
  207. u32 data_crc32; /* checksum for data_len bytes starting at byte 13 */
  208. /* BYTE 13 */
  209. u8 device_rom_revision; /* should be <= 1 */
  210. u16 data_len:12;
  211. u8 reserved:4;
  212. /* BYTES 16-21 - Only for TBT DROM, nonexistent in USB4 DROM */
  213. u16 vendor_id;
  214. u16 model_id;
  215. u8 model_rev;
  216. u8 eeprom_rev;
  217. } __packed;
  218. enum tb_drom_entry_type {
  219. /* force unsigned to prevent "one-bit signed bitfield" warning */
  220. TB_DROM_ENTRY_GENERIC = 0U,
  221. TB_DROM_ENTRY_PORT,
  222. };
  223. struct tb_drom_entry_header {
  224. u8 len;
  225. u8 index:6;
  226. bool port_disabled:1; /* only valid if type is TB_DROM_ENTRY_PORT */
  227. enum tb_drom_entry_type type:1;
  228. } __packed;
  229. struct tb_drom_entry_generic {
  230. struct tb_drom_entry_header header;
  231. u8 data[];
  232. } __packed;
  233. struct tb_drom_entry_port {
  234. /* BYTES 0-1 */
  235. struct tb_drom_entry_header header;
  236. /* BYTE 2 */
  237. u8 dual_link_port_rid:4;
  238. u8 link_nr:1;
  239. u8 unknown1:2;
  240. bool has_dual_link_port:1;
  241. /* BYTE 3 */
  242. u8 dual_link_port_nr:6;
  243. u8 unknown2:2;
  244. /* BYTES 4 - 5 TODO decode */
  245. u8 micro2:4;
  246. u8 micro1:4;
  247. u8 micro3;
  248. /* BYTES 6-7, TODO: verify (find hardware that has these set) */
  249. u8 peer_port_rid:4;
  250. u8 unknown3:3;
  251. bool has_peer_port:1;
  252. u8 peer_port_nr:6;
  253. u8 unknown4:2;
  254. } __packed;
  255. /* USB4 product descriptor */
  256. struct tb_drom_entry_desc {
  257. struct tb_drom_entry_header header;
  258. u16 bcdUSBSpec;
  259. u16 idVendor;
  260. u16 idProduct;
  261. u16 bcdProductFWRevision;
  262. u32 TID;
  263. u8 productHWRevision;
  264. };
  265. /**
  266. * tb_drom_read_uid_only() - Read UID directly from DROM
  267. * @sw: Router whose UID to read
  268. * @uid: UID is placed here
  269. *
  270. * Does not use the cached copy in sw->drom. Used during resume to check switch
  271. * identity.
  272. *
  273. * Return: %0 on success, negative errno otherwise.
  274. */
  275. int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid)
  276. {
  277. u8 data[9];
  278. u8 crc;
  279. int res;
  280. /* read uid */
  281. res = tb_eeprom_read_n(sw, 0, data, 9);
  282. if (res)
  283. return res;
  284. crc = tb_crc8(data + 1, 8);
  285. if (crc != data[0]) {
  286. tb_sw_warn(sw, "uid crc8 mismatch (expected: %#x, got: %#x)\n",
  287. data[0], crc);
  288. return -EIO;
  289. }
  290. *uid = *(u64 *)(data+1);
  291. return 0;
  292. }
  293. static int tb_drom_parse_entry_generic(struct tb_switch *sw,
  294. struct tb_drom_entry_header *header)
  295. {
  296. const struct tb_drom_entry_generic *entry =
  297. (const struct tb_drom_entry_generic *)header;
  298. switch (header->index) {
  299. case 1:
  300. /* Length includes 2 bytes header so remove it before copy */
  301. sw->vendor_name = kstrndup(entry->data,
  302. header->len - sizeof(*header), GFP_KERNEL);
  303. if (!sw->vendor_name)
  304. return -ENOMEM;
  305. break;
  306. case 2:
  307. sw->device_name = kstrndup(entry->data,
  308. header->len - sizeof(*header), GFP_KERNEL);
  309. if (!sw->device_name)
  310. return -ENOMEM;
  311. break;
  312. case 9: {
  313. const struct tb_drom_entry_desc *desc =
  314. (const struct tb_drom_entry_desc *)entry;
  315. if (!sw->vendor && !sw->device) {
  316. sw->vendor = desc->idVendor;
  317. sw->device = desc->idProduct;
  318. }
  319. break;
  320. }
  321. }
  322. return 0;
  323. }
  324. static int tb_drom_parse_entry_port(struct tb_switch *sw,
  325. struct tb_drom_entry_header *header)
  326. {
  327. struct tb_port *port;
  328. int res;
  329. enum tb_port_type type;
  330. /*
  331. * Some DROMs list more ports than the controller actually has
  332. * so we skip those but allow the parser to continue.
  333. */
  334. if (header->index > sw->config.max_port_number) {
  335. dev_info_once(&sw->dev, "ignoring unnecessary extra entries in DROM\n");
  336. return 0;
  337. }
  338. port = &sw->ports[header->index];
  339. port->disabled = header->port_disabled;
  340. if (port->disabled)
  341. return 0;
  342. res = tb_port_read(port, &type, TB_CFG_PORT, 2, 1);
  343. if (res)
  344. return res;
  345. type &= 0xffffff;
  346. if (type == TB_TYPE_PORT) {
  347. struct tb_drom_entry_port *entry = (void *) header;
  348. if (header->len != sizeof(*entry)) {
  349. tb_sw_warn(sw,
  350. "port entry has size %#x (expected %#zx)\n",
  351. header->len, sizeof(struct tb_drom_entry_port));
  352. return -EIO;
  353. }
  354. port->link_nr = entry->link_nr;
  355. if (entry->has_dual_link_port)
  356. port->dual_link_port =
  357. &port->sw->ports[entry->dual_link_port_nr];
  358. }
  359. return 0;
  360. }
  361. /*
  362. * tb_drom_parse_entries - parse the linked list of drom entries
  363. *
  364. * Drom must have been copied to sw->drom.
  365. */
  366. static int tb_drom_parse_entries(struct tb_switch *sw, size_t header_size)
  367. {
  368. struct tb_drom_header *header = (void *) sw->drom;
  369. u16 pos = header_size;
  370. u16 drom_size = header->data_len + TB_DROM_DATA_START;
  371. int res;
  372. while (pos < drom_size) {
  373. struct tb_drom_entry_header *entry = (void *) (sw->drom + pos);
  374. if (pos + 1 == drom_size || pos + entry->len > drom_size
  375. || !entry->len) {
  376. tb_sw_warn(sw, "DROM buffer overrun\n");
  377. return -EIO;
  378. }
  379. switch (entry->type) {
  380. case TB_DROM_ENTRY_GENERIC:
  381. res = tb_drom_parse_entry_generic(sw, entry);
  382. break;
  383. case TB_DROM_ENTRY_PORT:
  384. res = tb_drom_parse_entry_port(sw, entry);
  385. break;
  386. }
  387. if (res)
  388. return res;
  389. pos += entry->len;
  390. }
  391. return 0;
  392. }
  393. static int tb_switch_drom_alloc(struct tb_switch *sw, size_t size)
  394. {
  395. sw->drom = kzalloc(size, GFP_KERNEL);
  396. if (!sw->drom)
  397. return -ENOMEM;
  398. #ifdef CONFIG_DEBUG_FS
  399. sw->drom_blob.data = sw->drom;
  400. sw->drom_blob.size = size;
  401. #endif
  402. return 0;
  403. }
  404. static void tb_switch_drom_free(struct tb_switch *sw)
  405. {
  406. #ifdef CONFIG_DEBUG_FS
  407. sw->drom_blob.data = NULL;
  408. sw->drom_blob.size = 0;
  409. #endif
  410. kfree(sw->drom);
  411. sw->drom = NULL;
  412. }
  413. /*
  414. * tb_drom_copy_efi - copy drom supplied by EFI to sw->drom if present
  415. */
  416. static int tb_drom_copy_efi(struct tb_switch *sw, u16 *size)
  417. {
  418. struct device *dev = &sw->tb->nhi->pdev->dev;
  419. int len, res;
  420. len = device_property_count_u8(dev, "ThunderboltDROM");
  421. if (len < 0 || len < sizeof(struct tb_drom_header))
  422. return -EINVAL;
  423. res = tb_switch_drom_alloc(sw, len);
  424. if (res)
  425. return res;
  426. res = device_property_read_u8_array(dev, "ThunderboltDROM", sw->drom,
  427. len);
  428. if (res)
  429. goto err;
  430. *size = ((struct tb_drom_header *)sw->drom)->data_len +
  431. TB_DROM_DATA_START;
  432. if (*size > len)
  433. goto err;
  434. return 0;
  435. err:
  436. tb_switch_drom_free(sw);
  437. return -EINVAL;
  438. }
  439. static int tb_drom_copy_nvm(struct tb_switch *sw, u16 *size)
  440. {
  441. u16 drom_offset;
  442. int ret;
  443. if (!sw->dma_port)
  444. return -ENODEV;
  445. ret = tb_eeprom_get_drom_offset(sw, &drom_offset);
  446. if (ret)
  447. return ret;
  448. if (!drom_offset)
  449. return -ENODEV;
  450. ret = dma_port_flash_read(sw->dma_port, drom_offset + 14, size,
  451. sizeof(*size));
  452. if (ret)
  453. return ret;
  454. /* Size includes CRC8 + UID + CRC32 */
  455. *size += 1 + 8 + 4;
  456. ret = tb_switch_drom_alloc(sw, *size);
  457. if (ret)
  458. return ret;
  459. ret = dma_port_flash_read(sw->dma_port, drom_offset, sw->drom, *size);
  460. if (ret) {
  461. tb_switch_drom_free(sw);
  462. return ret;
  463. }
  464. /*
  465. * Read UID from the minimal DROM because the one in NVM is just
  466. * a placeholder.
  467. */
  468. tb_drom_read_uid_only(sw, &sw->uid);
  469. return 0;
  470. }
  471. static int usb4_copy_drom(struct tb_switch *sw, u16 *size)
  472. {
  473. int ret;
  474. ret = usb4_switch_drom_read(sw, 14, size, sizeof(*size));
  475. if (ret)
  476. return ret;
  477. /* Size includes CRC8 + UID + CRC32 */
  478. *size += 1 + 8 + 4;
  479. ret = tb_switch_drom_alloc(sw, *size);
  480. if (ret)
  481. return ret;
  482. ret = usb4_switch_drom_read(sw, 0, sw->drom, *size);
  483. if (ret)
  484. tb_switch_drom_free(sw);
  485. return ret;
  486. }
  487. static int tb_drom_bit_bang(struct tb_switch *sw, u16 *size)
  488. {
  489. int ret;
  490. ret = tb_eeprom_read_n(sw, 14, (u8 *)size, 2);
  491. if (ret)
  492. return ret;
  493. *size &= 0x3ff;
  494. *size += TB_DROM_DATA_START;
  495. tb_sw_dbg(sw, "reading DROM (length: %#x)\n", *size);
  496. if (*size < sizeof(struct tb_drom_header)) {
  497. tb_sw_warn(sw, "DROM too small, aborting\n");
  498. return -EIO;
  499. }
  500. ret = tb_switch_drom_alloc(sw, *size);
  501. if (ret)
  502. return ret;
  503. ret = tb_eeprom_read_n(sw, 0, sw->drom, *size);
  504. if (ret)
  505. tb_switch_drom_free(sw);
  506. return ret;
  507. }
  508. static int tb_drom_parse_v1(struct tb_switch *sw)
  509. {
  510. const struct tb_drom_header *header =
  511. (const struct tb_drom_header *)sw->drom;
  512. u32 crc;
  513. crc = tb_crc8((u8 *) &header->uid, 8);
  514. if (crc != header->uid_crc8) {
  515. tb_sw_warn(sw,
  516. "DROM UID CRC8 mismatch (expected: %#x, got: %#x)\n",
  517. header->uid_crc8, crc);
  518. return -EIO;
  519. }
  520. if (!sw->uid)
  521. sw->uid = header->uid;
  522. sw->vendor = header->vendor_id;
  523. sw->device = header->model_id;
  524. crc = tb_crc32(sw->drom + TB_DROM_DATA_START, header->data_len);
  525. if (crc != header->data_crc32) {
  526. tb_sw_warn(sw,
  527. "DROM data CRC32 mismatch (expected: %#x, got: %#x), continuing\n",
  528. header->data_crc32, crc);
  529. }
  530. return tb_drom_parse_entries(sw, TB_DROM_HEADER_SIZE);
  531. }
  532. static int usb4_drom_parse(struct tb_switch *sw)
  533. {
  534. const struct tb_drom_header *header =
  535. (const struct tb_drom_header *)sw->drom;
  536. u32 crc;
  537. crc = tb_crc32(sw->drom + TB_DROM_DATA_START, header->data_len);
  538. if (crc != header->data_crc32) {
  539. tb_sw_warn(sw,
  540. "DROM data CRC32 mismatch (expected: %#x, got: %#x), continuing\n",
  541. header->data_crc32, crc);
  542. }
  543. return tb_drom_parse_entries(sw, USB4_DROM_HEADER_SIZE);
  544. }
  545. static int tb_drom_parse(struct tb_switch *sw, u16 size)
  546. {
  547. const struct tb_drom_header *header = (const void *)sw->drom;
  548. int ret;
  549. if (header->data_len + TB_DROM_DATA_START != size) {
  550. tb_sw_warn(sw, "DROM size mismatch\n");
  551. ret = -EIO;
  552. goto err;
  553. }
  554. tb_sw_dbg(sw, "DROM version: %d\n", header->device_rom_revision);
  555. switch (header->device_rom_revision) {
  556. case 3:
  557. ret = usb4_drom_parse(sw);
  558. break;
  559. default:
  560. tb_sw_warn(sw, "DROM device_rom_revision %#x unknown\n",
  561. header->device_rom_revision);
  562. fallthrough;
  563. case 1:
  564. ret = tb_drom_parse_v1(sw);
  565. break;
  566. }
  567. if (ret) {
  568. tb_sw_warn(sw, "parsing DROM failed\n");
  569. goto err;
  570. }
  571. return 0;
  572. err:
  573. tb_switch_drom_free(sw);
  574. return ret;
  575. }
  576. static int tb_drom_host_read(struct tb_switch *sw)
  577. {
  578. u16 size;
  579. if (tb_switch_is_usb4(sw)) {
  580. usb4_switch_read_uid(sw, &sw->uid);
  581. if (!usb4_copy_drom(sw, &size))
  582. return tb_drom_parse(sw, size);
  583. } else {
  584. if (!tb_drom_copy_efi(sw, &size))
  585. return tb_drom_parse(sw, size);
  586. if (!tb_drom_copy_nvm(sw, &size))
  587. return tb_drom_parse(sw, size);
  588. tb_drom_read_uid_only(sw, &sw->uid);
  589. }
  590. return 0;
  591. }
  592. static int tb_drom_device_read(struct tb_switch *sw)
  593. {
  594. u16 size;
  595. int ret;
  596. if (tb_switch_is_usb4(sw)) {
  597. usb4_switch_read_uid(sw, &sw->uid);
  598. ret = usb4_copy_drom(sw, &size);
  599. } else {
  600. ret = tb_drom_bit_bang(sw, &size);
  601. }
  602. if (ret)
  603. return ret;
  604. return tb_drom_parse(sw, size);
  605. }
  606. /**
  607. * tb_drom_read() - Copy DROM to sw->drom and parse it
  608. * @sw: Router whose DROM to read and parse
  609. *
  610. * This function reads router DROM and if successful parses the entries and
  611. * populates the fields in @sw accordingly. Can be called for any router
  612. * generation.
  613. *
  614. * Return: %0 on success, negative errno otherwise.
  615. */
  616. int tb_drom_read(struct tb_switch *sw)
  617. {
  618. if (sw->drom)
  619. return 0;
  620. if (!tb_route(sw))
  621. return tb_drom_host_read(sw);
  622. return tb_drom_device_read(sw);
  623. }