eeprom.c 13 KB

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