eeprom.c 13 KB

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