eeprom.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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/property.h>
  10. #include <linux/slab.h>
  11. #include "tb.h"
  12. /**
  13. * tb_eeprom_ctl_write() - write control word
  14. */
  15. static int tb_eeprom_ctl_write(struct tb_switch *sw, struct tb_eeprom_ctl *ctl)
  16. {
  17. return tb_sw_write(sw, ctl, TB_CFG_SWITCH, sw->cap_plug_events + 4, 1);
  18. }
  19. /**
  20. * tb_eeprom_ctl_write() - read control word
  21. */
  22. static int tb_eeprom_ctl_read(struct tb_switch *sw, struct tb_eeprom_ctl *ctl)
  23. {
  24. return tb_sw_read(sw, ctl, TB_CFG_SWITCH, sw->cap_plug_events + 4, 1);
  25. }
  26. enum tb_eeprom_transfer {
  27. TB_EEPROM_IN,
  28. TB_EEPROM_OUT,
  29. };
  30. /**
  31. * tb_eeprom_active - enable rom access
  32. *
  33. * WARNING: Always disable access after usage. Otherwise the controller will
  34. * fail to reprobe.
  35. */
  36. static int tb_eeprom_active(struct tb_switch *sw, bool enable)
  37. {
  38. struct tb_eeprom_ctl ctl;
  39. int res = tb_eeprom_ctl_read(sw, &ctl);
  40. if (res)
  41. return res;
  42. if (enable) {
  43. ctl.access_high = 1;
  44. res = tb_eeprom_ctl_write(sw, &ctl);
  45. if (res)
  46. return res;
  47. ctl.access_low = 0;
  48. return tb_eeprom_ctl_write(sw, &ctl);
  49. } else {
  50. ctl.access_low = 1;
  51. res = tb_eeprom_ctl_write(sw, &ctl);
  52. if (res)
  53. return res;
  54. ctl.access_high = 0;
  55. return tb_eeprom_ctl_write(sw, &ctl);
  56. }
  57. }
  58. /**
  59. * tb_eeprom_transfer - transfer one bit
  60. *
  61. * If TB_EEPROM_IN is passed, then the bit can be retrieved from ctl->data_in.
  62. * If TB_EEPROM_OUT is passed, then ctl->data_out will be written.
  63. */
  64. static int tb_eeprom_transfer(struct tb_switch *sw, struct tb_eeprom_ctl *ctl,
  65. enum tb_eeprom_transfer direction)
  66. {
  67. int res;
  68. if (direction == TB_EEPROM_OUT) {
  69. res = tb_eeprom_ctl_write(sw, ctl);
  70. if (res)
  71. return res;
  72. }
  73. ctl->clock = 1;
  74. res = tb_eeprom_ctl_write(sw, ctl);
  75. if (res)
  76. return res;
  77. if (direction == TB_EEPROM_IN) {
  78. res = tb_eeprom_ctl_read(sw, ctl);
  79. if (res)
  80. return res;
  81. }
  82. ctl->clock = 0;
  83. return tb_eeprom_ctl_write(sw, ctl);
  84. }
  85. /**
  86. * tb_eeprom_out - write one byte to the bus
  87. */
  88. static int tb_eeprom_out(struct tb_switch *sw, u8 val)
  89. {
  90. struct tb_eeprom_ctl ctl;
  91. int i;
  92. int res = tb_eeprom_ctl_read(sw, &ctl);
  93. if (res)
  94. return res;
  95. for (i = 0; i < 8; i++) {
  96. ctl.data_out = val & 0x80;
  97. res = tb_eeprom_transfer(sw, &ctl, TB_EEPROM_OUT);
  98. if (res)
  99. return res;
  100. val <<= 1;
  101. }
  102. return 0;
  103. }
  104. /**
  105. * tb_eeprom_in - read one byte from the bus
  106. */
  107. static int tb_eeprom_in(struct tb_switch *sw, u8 *val)
  108. {
  109. struct tb_eeprom_ctl ctl;
  110. int i;
  111. int res = tb_eeprom_ctl_read(sw, &ctl);
  112. if (res)
  113. return res;
  114. *val = 0;
  115. for (i = 0; i < 8; i++) {
  116. *val <<= 1;
  117. res = tb_eeprom_transfer(sw, &ctl, TB_EEPROM_IN);
  118. if (res)
  119. return res;
  120. *val |= ctl.data_in;
  121. }
  122. return 0;
  123. }
  124. /**
  125. * tb_eeprom_read_n - read count bytes from offset into val
  126. */
  127. static int tb_eeprom_read_n(struct tb_switch *sw, u16 offset, u8 *val,
  128. size_t count)
  129. {
  130. int i, res;
  131. res = tb_eeprom_active(sw, true);
  132. if (res)
  133. return res;
  134. res = tb_eeprom_out(sw, 3);
  135. if (res)
  136. return res;
  137. res = tb_eeprom_out(sw, offset >> 8);
  138. if (res)
  139. return res;
  140. res = tb_eeprom_out(sw, offset);
  141. if (res)
  142. return res;
  143. for (i = 0; i < count; i++) {
  144. res = tb_eeprom_in(sw, val + i);
  145. if (res)
  146. return res;
  147. }
  148. return tb_eeprom_active(sw, false);
  149. }
  150. static u8 tb_crc8(u8 *data, int len)
  151. {
  152. int i, j;
  153. u8 val = 0xff;
  154. for (i = 0; i < len; i++) {
  155. val ^= data[i];
  156. for (j = 0; j < 8; j++)
  157. val = (val << 1) ^ ((val & 0x80) ? 7 : 0);
  158. }
  159. return val;
  160. }
  161. static u32 tb_crc32(void *data, size_t len)
  162. {
  163. return ~__crc32c_le(~0, data, len);
  164. }
  165. #define TB_DROM_DATA_START 13
  166. struct tb_drom_header {
  167. /* BYTE 0 */
  168. u8 uid_crc8; /* checksum for uid */
  169. /* BYTES 1-8 */
  170. u64 uid;
  171. /* BYTES 9-12 */
  172. u32 data_crc32; /* checksum for data_len bytes starting at byte 13 */
  173. /* BYTE 13 */
  174. u8 device_rom_revision; /* should be <= 1 */
  175. u16 data_len:10;
  176. u8 __unknown1:6;
  177. /* BYTES 16-21 */
  178. u16 vendor_id;
  179. u16 model_id;
  180. u8 model_rev;
  181. u8 eeprom_rev;
  182. } __packed;
  183. enum tb_drom_entry_type {
  184. /* force unsigned to prevent "one-bit signed bitfield" warning */
  185. TB_DROM_ENTRY_GENERIC = 0U,
  186. TB_DROM_ENTRY_PORT,
  187. };
  188. struct tb_drom_entry_header {
  189. u8 len;
  190. u8 index:6;
  191. bool port_disabled:1; /* only valid if type is TB_DROM_ENTRY_PORT */
  192. enum tb_drom_entry_type type:1;
  193. } __packed;
  194. struct tb_drom_entry_generic {
  195. struct tb_drom_entry_header header;
  196. u8 data[0];
  197. } __packed;
  198. struct tb_drom_entry_port {
  199. /* BYTES 0-1 */
  200. struct tb_drom_entry_header header;
  201. /* BYTE 2 */
  202. u8 dual_link_port_rid:4;
  203. u8 link_nr:1;
  204. u8 unknown1:2;
  205. bool has_dual_link_port:1;
  206. /* BYTE 3 */
  207. u8 dual_link_port_nr:6;
  208. u8 unknown2:2;
  209. /* BYTES 4 - 5 TODO decode */
  210. u8 micro2:4;
  211. u8 micro1:4;
  212. u8 micro3;
  213. /* BYTES 6-7, TODO: verify (find hardware that has these set) */
  214. u8 peer_port_rid:4;
  215. u8 unknown3:3;
  216. bool has_peer_port:1;
  217. u8 peer_port_nr:6;
  218. u8 unknown4:2;
  219. } __packed;
  220. /**
  221. * tb_eeprom_get_drom_offset - get drom offset within eeprom
  222. */
  223. static int tb_eeprom_get_drom_offset(struct tb_switch *sw, u16 *offset)
  224. {
  225. struct tb_cap_plug_events cap;
  226. int res;
  227. if (!sw->cap_plug_events) {
  228. tb_sw_warn(sw, "no TB_CAP_PLUG_EVENTS, cannot read eeprom\n");
  229. return -ENOSYS;
  230. }
  231. res = tb_sw_read(sw, &cap, TB_CFG_SWITCH, sw->cap_plug_events,
  232. sizeof(cap) / 4);
  233. if (res)
  234. return res;
  235. if (!cap.eeprom_ctl.present || cap.eeprom_ctl.not_present) {
  236. tb_sw_warn(sw, "no NVM\n");
  237. return -ENOSYS;
  238. }
  239. if (cap.drom_offset > 0xffff) {
  240. tb_sw_warn(sw, "drom offset is larger than 0xffff: %#x\n",
  241. cap.drom_offset);
  242. return -ENXIO;
  243. }
  244. *offset = cap.drom_offset;
  245. return 0;
  246. }
  247. /**
  248. * tb_drom_read_uid_only - read uid directly from drom
  249. *
  250. * Does not use the cached copy in sw->drom. Used during resume to check switch
  251. * identity.
  252. */
  253. int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid)
  254. {
  255. u8 data[9];
  256. u16 drom_offset;
  257. u8 crc;
  258. int res = tb_eeprom_get_drom_offset(sw, &drom_offset);
  259. if (res)
  260. return res;
  261. if (drom_offset == 0)
  262. return -ENODEV;
  263. /* read uid */
  264. res = tb_eeprom_read_n(sw, drom_offset, data, 9);
  265. if (res)
  266. return res;
  267. crc = tb_crc8(data + 1, 8);
  268. if (crc != data[0]) {
  269. tb_sw_warn(sw, "uid crc8 mismatch (expected: %#x, got: %#x)\n",
  270. data[0], crc);
  271. return -EIO;
  272. }
  273. *uid = *(u64 *)(data+1);
  274. return 0;
  275. }
  276. static int tb_drom_parse_entry_generic(struct tb_switch *sw,
  277. struct tb_drom_entry_header *header)
  278. {
  279. const struct tb_drom_entry_generic *entry =
  280. (const struct tb_drom_entry_generic *)header;
  281. switch (header->index) {
  282. case 1:
  283. /* Length includes 2 bytes header so remove it before copy */
  284. sw->vendor_name = kstrndup(entry->data,
  285. header->len - sizeof(*header), GFP_KERNEL);
  286. if (!sw->vendor_name)
  287. return -ENOMEM;
  288. break;
  289. case 2:
  290. sw->device_name = kstrndup(entry->data,
  291. header->len - sizeof(*header), GFP_KERNEL);
  292. if (!sw->device_name)
  293. return -ENOMEM;
  294. break;
  295. }
  296. return 0;
  297. }
  298. static int tb_drom_parse_entry_port(struct tb_switch *sw,
  299. struct tb_drom_entry_header *header)
  300. {
  301. struct tb_port *port;
  302. int res;
  303. enum tb_port_type type;
  304. /*
  305. * Some DROMs list more ports than the controller actually has
  306. * so we skip those but allow the parser to continue.
  307. */
  308. if (header->index > sw->config.max_port_number) {
  309. dev_info_once(&sw->dev, "ignoring unnecessary extra entries in DROM\n");
  310. return 0;
  311. }
  312. port = &sw->ports[header->index];
  313. port->disabled = header->port_disabled;
  314. if (port->disabled)
  315. return 0;
  316. res = tb_port_read(port, &type, TB_CFG_PORT, 2, 1);
  317. if (res)
  318. return res;
  319. type &= 0xffffff;
  320. if (type == TB_TYPE_PORT) {
  321. struct tb_drom_entry_port *entry = (void *) header;
  322. if (header->len != sizeof(*entry)) {
  323. tb_sw_warn(sw,
  324. "port entry has size %#x (expected %#zx)\n",
  325. header->len, sizeof(struct tb_drom_entry_port));
  326. return -EIO;
  327. }
  328. port->link_nr = entry->link_nr;
  329. if (entry->has_dual_link_port)
  330. port->dual_link_port =
  331. &port->sw->ports[entry->dual_link_port_nr];
  332. }
  333. return 0;
  334. }
  335. /**
  336. * tb_drom_parse_entries - parse the linked list of drom entries
  337. *
  338. * Drom must have been copied to sw->drom.
  339. */
  340. static int tb_drom_parse_entries(struct tb_switch *sw)
  341. {
  342. struct tb_drom_header *header = (void *) sw->drom;
  343. u16 pos = sizeof(*header);
  344. u16 drom_size = header->data_len + TB_DROM_DATA_START;
  345. int res;
  346. while (pos < drom_size) {
  347. struct tb_drom_entry_header *entry = (void *) (sw->drom + pos);
  348. if (pos + 1 == drom_size || pos + entry->len > drom_size
  349. || !entry->len) {
  350. tb_sw_warn(sw, "drom buffer overrun, aborting\n");
  351. return -EIO;
  352. }
  353. switch (entry->type) {
  354. case TB_DROM_ENTRY_GENERIC:
  355. res = tb_drom_parse_entry_generic(sw, entry);
  356. break;
  357. case TB_DROM_ENTRY_PORT:
  358. res = tb_drom_parse_entry_port(sw, entry);
  359. break;
  360. }
  361. if (res)
  362. return res;
  363. pos += entry->len;
  364. }
  365. return 0;
  366. }
  367. /**
  368. * tb_drom_copy_efi - copy drom supplied by EFI to sw->drom if present
  369. */
  370. static int tb_drom_copy_efi(struct tb_switch *sw, u16 *size)
  371. {
  372. struct device *dev = &sw->tb->nhi->pdev->dev;
  373. int len, res;
  374. len = device_property_read_u8_array(dev, "ThunderboltDROM", NULL, 0);
  375. if (len < 0 || len < sizeof(struct tb_drom_header))
  376. return -EINVAL;
  377. sw->drom = kmalloc(len, GFP_KERNEL);
  378. if (!sw->drom)
  379. return -ENOMEM;
  380. res = device_property_read_u8_array(dev, "ThunderboltDROM", sw->drom,
  381. len);
  382. if (res)
  383. goto err;
  384. *size = ((struct tb_drom_header *)sw->drom)->data_len +
  385. TB_DROM_DATA_START;
  386. if (*size > len)
  387. goto err;
  388. return 0;
  389. err:
  390. kfree(sw->drom);
  391. sw->drom = NULL;
  392. return -EINVAL;
  393. }
  394. static int tb_drom_copy_nvm(struct tb_switch *sw, u16 *size)
  395. {
  396. u32 drom_offset;
  397. int ret;
  398. if (!sw->dma_port)
  399. return -ENODEV;
  400. ret = tb_sw_read(sw, &drom_offset, TB_CFG_SWITCH,
  401. sw->cap_plug_events + 12, 1);
  402. if (ret)
  403. return ret;
  404. if (!drom_offset)
  405. return -ENODEV;
  406. ret = dma_port_flash_read(sw->dma_port, drom_offset + 14, size,
  407. sizeof(*size));
  408. if (ret)
  409. return ret;
  410. /* Size includes CRC8 + UID + CRC32 */
  411. *size += 1 + 8 + 4;
  412. sw->drom = kzalloc(*size, GFP_KERNEL);
  413. if (!sw->drom)
  414. return -ENOMEM;
  415. ret = dma_port_flash_read(sw->dma_port, drom_offset, sw->drom, *size);
  416. if (ret)
  417. goto err_free;
  418. /*
  419. * Read UID from the minimal DROM because the one in NVM is just
  420. * a placeholder.
  421. */
  422. tb_drom_read_uid_only(sw, &sw->uid);
  423. return 0;
  424. err_free:
  425. kfree(sw->drom);
  426. sw->drom = NULL;
  427. return ret;
  428. }
  429. /**
  430. * tb_drom_read - copy drom to sw->drom and parse it
  431. */
  432. int tb_drom_read(struct tb_switch *sw)
  433. {
  434. u16 drom_offset;
  435. u16 size;
  436. u32 crc;
  437. struct tb_drom_header *header;
  438. int res;
  439. if (sw->drom)
  440. return 0;
  441. if (tb_route(sw) == 0) {
  442. /*
  443. * Apple's NHI EFI driver supplies a DROM for the root switch
  444. * in a device property. Use it if available.
  445. */
  446. if (tb_drom_copy_efi(sw, &size) == 0)
  447. goto parse;
  448. /* Non-Apple hardware has the DROM as part of NVM */
  449. if (tb_drom_copy_nvm(sw, &size) == 0)
  450. goto parse;
  451. /*
  452. * The root switch contains only a dummy drom (header only,
  453. * no entries). Hardcode the configuration here.
  454. */
  455. tb_drom_read_uid_only(sw, &sw->uid);
  456. sw->ports[1].link_nr = 0;
  457. sw->ports[2].link_nr = 1;
  458. sw->ports[1].dual_link_port = &sw->ports[2];
  459. sw->ports[2].dual_link_port = &sw->ports[1];
  460. sw->ports[3].link_nr = 0;
  461. sw->ports[4].link_nr = 1;
  462. sw->ports[3].dual_link_port = &sw->ports[4];
  463. sw->ports[4].dual_link_port = &sw->ports[3];
  464. /* Port 5 is inaccessible on this gen 1 controller */
  465. if (sw->config.device_id == PCI_DEVICE_ID_INTEL_LIGHT_RIDGE)
  466. sw->ports[5].disabled = true;
  467. return 0;
  468. }
  469. res = tb_eeprom_get_drom_offset(sw, &drom_offset);
  470. if (res)
  471. return res;
  472. res = tb_eeprom_read_n(sw, drom_offset + 14, (u8 *) &size, 2);
  473. if (res)
  474. return res;
  475. size &= 0x3ff;
  476. size += TB_DROM_DATA_START;
  477. tb_sw_dbg(sw, "reading drom (length: %#x)\n", size);
  478. if (size < sizeof(*header)) {
  479. tb_sw_warn(sw, "drom too small, aborting\n");
  480. return -EIO;
  481. }
  482. sw->drom = kzalloc(size, GFP_KERNEL);
  483. if (!sw->drom)
  484. return -ENOMEM;
  485. res = tb_eeprom_read_n(sw, drom_offset, sw->drom, size);
  486. if (res)
  487. goto err;
  488. parse:
  489. header = (void *) sw->drom;
  490. if (header->data_len + TB_DROM_DATA_START != size) {
  491. tb_sw_warn(sw, "drom size mismatch, aborting\n");
  492. goto err;
  493. }
  494. crc = tb_crc8((u8 *) &header->uid, 8);
  495. if (crc != header->uid_crc8) {
  496. tb_sw_warn(sw,
  497. "drom uid crc8 mismatch (expected: %#x, got: %#x), aborting\n",
  498. header->uid_crc8, crc);
  499. goto err;
  500. }
  501. if (!sw->uid)
  502. sw->uid = header->uid;
  503. sw->vendor = header->vendor_id;
  504. sw->device = header->model_id;
  505. crc = tb_crc32(sw->drom + TB_DROM_DATA_START, header->data_len);
  506. if (crc != header->data_crc32) {
  507. tb_sw_warn(sw,
  508. "drom data crc32 mismatch (expected: %#x, got: %#x), continuing\n",
  509. header->data_crc32, crc);
  510. }
  511. if (header->device_rom_revision > 2)
  512. tb_sw_warn(sw, "drom device_rom_revision %#x unknown\n",
  513. header->device_rom_revision);
  514. return tb_drom_parse_entries(sw);
  515. err:
  516. kfree(sw->drom);
  517. sw->drom = NULL;
  518. return -EIO;
  519. }