eprom.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Copyright(c) 2015, 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/delay.h>
  48. #include "hfi.h"
  49. #include "common.h"
  50. #include "eprom.h"
  51. /*
  52. * The EPROM is logically divided into three partitions:
  53. * partition 0: the first 128K, visible from PCI ROM BAR
  54. * partition 1: 4K config file (sector size)
  55. * partition 2: the rest
  56. */
  57. #define P0_SIZE (128 * 1024)
  58. #define P1_SIZE (4 * 1024)
  59. #define P1_START P0_SIZE
  60. #define P2_START (P0_SIZE + P1_SIZE)
  61. /* controller page size, in bytes */
  62. #define EP_PAGE_SIZE 256
  63. #define EP_PAGE_MASK (EP_PAGE_SIZE - 1)
  64. #define EP_PAGE_DWORDS (EP_PAGE_SIZE / sizeof(u32))
  65. /* controller commands */
  66. #define CMD_SHIFT 24
  67. #define CMD_NOP (0)
  68. #define CMD_READ_DATA(addr) ((0x03 << CMD_SHIFT) | addr)
  69. #define CMD_RELEASE_POWERDOWN_NOID ((0xab << CMD_SHIFT))
  70. /* controller interface speeds */
  71. #define EP_SPEED_FULL 0x2 /* full speed */
  72. /*
  73. * How long to wait for the EPROM to become available, in ms.
  74. * The spec 32 Mb EPROM takes around 40s to erase then write.
  75. * Double it for safety.
  76. */
  77. #define EPROM_TIMEOUT 80000 /* ms */
  78. /*
  79. * Read a 256 byte (64 dword) EPROM page.
  80. * All callers have verified the offset is at a page boundary.
  81. */
  82. static void read_page(struct hfi1_devdata *dd, u32 offset, u32 *result)
  83. {
  84. int i;
  85. write_csr(dd, ASIC_EEP_ADDR_CMD, CMD_READ_DATA(offset));
  86. for (i = 0; i < EP_PAGE_DWORDS; i++)
  87. result[i] = (u32)read_csr(dd, ASIC_EEP_DATA);
  88. write_csr(dd, ASIC_EEP_ADDR_CMD, CMD_NOP); /* close open page */
  89. }
  90. /*
  91. * Read length bytes starting at offset from the start of the EPROM.
  92. */
  93. static int read_length(struct hfi1_devdata *dd, u32 start, u32 len, void *dest)
  94. {
  95. u32 buffer[EP_PAGE_DWORDS];
  96. u32 end;
  97. u32 start_offset;
  98. u32 read_start;
  99. u32 bytes;
  100. if (len == 0)
  101. return 0;
  102. end = start + len;
  103. /*
  104. * Make sure the read range is not outside of the controller read
  105. * command address range. Note that '>' is correct below - the end
  106. * of the range is OK if it stops at the limit, but no higher.
  107. */
  108. if (end > (1 << CMD_SHIFT))
  109. return -EINVAL;
  110. /* read the first partial page */
  111. start_offset = start & EP_PAGE_MASK;
  112. if (start_offset) {
  113. /* partial starting page */
  114. /* align and read the page that contains the start */
  115. read_start = start & ~EP_PAGE_MASK;
  116. read_page(dd, read_start, buffer);
  117. /* the rest of the page is available data */
  118. bytes = EP_PAGE_SIZE - start_offset;
  119. if (len <= bytes) {
  120. /* end is within this page */
  121. memcpy(dest, (u8 *)buffer + start_offset, len);
  122. return 0;
  123. }
  124. memcpy(dest, (u8 *)buffer + start_offset, bytes);
  125. start += bytes;
  126. len -= bytes;
  127. dest += bytes;
  128. }
  129. /* start is now page aligned */
  130. /* read whole pages */
  131. while (len >= EP_PAGE_SIZE) {
  132. read_page(dd, start, buffer);
  133. memcpy(dest, buffer, EP_PAGE_SIZE);
  134. start += EP_PAGE_SIZE;
  135. len -= EP_PAGE_SIZE;
  136. dest += EP_PAGE_SIZE;
  137. }
  138. /* read the last partial page */
  139. if (len) {
  140. read_page(dd, start, buffer);
  141. memcpy(dest, buffer, len);
  142. }
  143. return 0;
  144. }
  145. /*
  146. * Initialize the EPROM handler.
  147. */
  148. int eprom_init(struct hfi1_devdata *dd)
  149. {
  150. int ret = 0;
  151. /* only the discrete chip has an EPROM */
  152. if (dd->pcidev->device != PCI_DEVICE_ID_INTEL0)
  153. return 0;
  154. /*
  155. * It is OK if both HFIs reset the EPROM as long as they don't
  156. * do it at the same time.
  157. */
  158. ret = acquire_chip_resource(dd, CR_EPROM, EPROM_TIMEOUT);
  159. if (ret) {
  160. dd_dev_err(dd,
  161. "%s: unable to acquire EPROM resource, no EPROM support\n",
  162. __func__);
  163. goto done_asic;
  164. }
  165. /* reset EPROM to be sure it is in a good state */
  166. /* set reset */
  167. write_csr(dd, ASIC_EEP_CTL_STAT, ASIC_EEP_CTL_STAT_EP_RESET_SMASK);
  168. /* clear reset, set speed */
  169. write_csr(dd, ASIC_EEP_CTL_STAT,
  170. EP_SPEED_FULL << ASIC_EEP_CTL_STAT_RATE_SPI_SHIFT);
  171. /* wake the device with command "release powerdown NoID" */
  172. write_csr(dd, ASIC_EEP_ADDR_CMD, CMD_RELEASE_POWERDOWN_NOID);
  173. dd->eprom_available = true;
  174. release_chip_resource(dd, CR_EPROM);
  175. done_asic:
  176. return ret;
  177. }
  178. /* magic character sequence that trails an image */
  179. #define IMAGE_TRAIL_MAGIC "egamiAPO"
  180. /* EPROM file types */
  181. #define HFI1_EFT_PLATFORM_CONFIG 2
  182. /* segment size - 128 KiB */
  183. #define SEG_SIZE (128 * 1024)
  184. struct hfi1_eprom_footer {
  185. u32 oprom_size; /* size of the oprom, in bytes */
  186. u16 num_table_entries;
  187. u16 version; /* version of this footer */
  188. u32 magic; /* must be last */
  189. };
  190. struct hfi1_eprom_table_entry {
  191. u32 type; /* file type */
  192. u32 offset; /* file offset from start of EPROM */
  193. u32 size; /* file size, in bytes */
  194. };
  195. /*
  196. * Calculate the max number of table entries that will fit within a directory
  197. * buffer of size 'dir_size'.
  198. */
  199. #define MAX_TABLE_ENTRIES(dir_size) \
  200. (((dir_size) - sizeof(struct hfi1_eprom_footer)) / \
  201. sizeof(struct hfi1_eprom_table_entry))
  202. #define DIRECTORY_SIZE(n) (sizeof(struct hfi1_eprom_footer) + \
  203. (sizeof(struct hfi1_eprom_table_entry) * (n)))
  204. #define MAGIC4(a, b, c, d) ((d) << 24 | (c) << 16 | (b) << 8 | (a))
  205. #define FOOTER_MAGIC MAGIC4('e', 'p', 'r', 'm')
  206. #define FOOTER_VERSION 1
  207. /*
  208. * Read all of partition 1. The actual file is at the front. Adjust
  209. * the returned size if a trailing image magic is found.
  210. */
  211. static int read_partition_platform_config(struct hfi1_devdata *dd, void **data,
  212. u32 *size)
  213. {
  214. void *buffer;
  215. void *p;
  216. u32 length;
  217. int ret;
  218. buffer = kmalloc(P1_SIZE, GFP_KERNEL);
  219. if (!buffer)
  220. return -ENOMEM;
  221. ret = read_length(dd, P1_START, P1_SIZE, buffer);
  222. if (ret) {
  223. kfree(buffer);
  224. return ret;
  225. }
  226. /* scan for image magic that may trail the actual data */
  227. p = strnstr(buffer, IMAGE_TRAIL_MAGIC, P1_SIZE);
  228. if (p)
  229. length = p - buffer;
  230. else
  231. length = P1_SIZE;
  232. *data = buffer;
  233. *size = length;
  234. return 0;
  235. }
  236. /*
  237. * The segment magic has been checked. There is a footer and table of
  238. * contents present.
  239. *
  240. * directory is a u32 aligned buffer of size EP_PAGE_SIZE.
  241. */
  242. static int read_segment_platform_config(struct hfi1_devdata *dd,
  243. void *directory, void **data, u32 *size)
  244. {
  245. struct hfi1_eprom_footer *footer;
  246. struct hfi1_eprom_table_entry *table;
  247. struct hfi1_eprom_table_entry *entry;
  248. void *buffer = NULL;
  249. void *table_buffer = NULL;
  250. int ret, i;
  251. u32 directory_size;
  252. u32 seg_base, seg_offset;
  253. u32 bytes_available, ncopied, to_copy;
  254. /* the footer is at the end of the directory */
  255. footer = (struct hfi1_eprom_footer *)
  256. (directory + EP_PAGE_SIZE - sizeof(*footer));
  257. /* make sure the structure version is supported */
  258. if (footer->version != FOOTER_VERSION)
  259. return -EINVAL;
  260. /* oprom size cannot be larger than a segment */
  261. if (footer->oprom_size >= SEG_SIZE)
  262. return -EINVAL;
  263. /* the file table must fit in a segment with the oprom */
  264. if (footer->num_table_entries >
  265. MAX_TABLE_ENTRIES(SEG_SIZE - footer->oprom_size))
  266. return -EINVAL;
  267. /* find the file table start, which precedes the footer */
  268. directory_size = DIRECTORY_SIZE(footer->num_table_entries);
  269. if (directory_size <= EP_PAGE_SIZE) {
  270. /* the file table fits into the directory buffer handed in */
  271. table = (struct hfi1_eprom_table_entry *)
  272. (directory + EP_PAGE_SIZE - directory_size);
  273. } else {
  274. /* need to allocate and read more */
  275. table_buffer = kmalloc(directory_size, GFP_KERNEL);
  276. if (!table_buffer)
  277. return -ENOMEM;
  278. ret = read_length(dd, SEG_SIZE - directory_size,
  279. directory_size, table_buffer);
  280. if (ret)
  281. goto done;
  282. table = table_buffer;
  283. }
  284. /* look for the platform configuration file in the table */
  285. for (entry = NULL, i = 0; i < footer->num_table_entries; i++) {
  286. if (table[i].type == HFI1_EFT_PLATFORM_CONFIG) {
  287. entry = &table[i];
  288. break;
  289. }
  290. }
  291. if (!entry) {
  292. ret = -ENOENT;
  293. goto done;
  294. }
  295. /*
  296. * Sanity check on the configuration file size - it should never
  297. * be larger than 4 KiB.
  298. */
  299. if (entry->size > (4 * 1024)) {
  300. dd_dev_err(dd, "Bad configuration file size 0x%x\n",
  301. entry->size);
  302. ret = -EINVAL;
  303. goto done;
  304. }
  305. /* check for bogus offset and size that wrap when added together */
  306. if (entry->offset + entry->size < entry->offset) {
  307. dd_dev_err(dd,
  308. "Bad configuration file start + size 0x%x+0x%x\n",
  309. entry->offset, entry->size);
  310. ret = -EINVAL;
  311. goto done;
  312. }
  313. /* allocate the buffer to return */
  314. buffer = kmalloc(entry->size, GFP_KERNEL);
  315. if (!buffer) {
  316. ret = -ENOMEM;
  317. goto done;
  318. }
  319. /*
  320. * Extract the file by looping over segments until it is fully read.
  321. */
  322. seg_offset = entry->offset % SEG_SIZE;
  323. seg_base = entry->offset - seg_offset;
  324. ncopied = 0;
  325. while (ncopied < entry->size) {
  326. /* calculate data bytes available in this segment */
  327. /* start with the bytes from the current offset to the end */
  328. bytes_available = SEG_SIZE - seg_offset;
  329. /* subtract off footer and table from segment 0 */
  330. if (seg_base == 0) {
  331. /*
  332. * Sanity check: should not have a starting point
  333. * at or within the directory.
  334. */
  335. if (bytes_available <= directory_size) {
  336. dd_dev_err(dd,
  337. "Bad configuration file - offset 0x%x within footer+table\n",
  338. entry->offset);
  339. ret = -EINVAL;
  340. goto done;
  341. }
  342. bytes_available -= directory_size;
  343. }
  344. /* calculate bytes wanted */
  345. to_copy = entry->size - ncopied;
  346. /* max out at the available bytes in this segment */
  347. if (to_copy > bytes_available)
  348. to_copy = bytes_available;
  349. /*
  350. * Read from the EPROM.
  351. *
  352. * The sanity check for entry->offset is done in read_length().
  353. * The EPROM offset is validated against what the hardware
  354. * addressing supports. In addition, if the offset is larger
  355. * than the actual EPROM, it silently wraps. It will work
  356. * fine, though the reader may not get what they expected
  357. * from the EPROM.
  358. */
  359. ret = read_length(dd, seg_base + seg_offset, to_copy,
  360. buffer + ncopied);
  361. if (ret)
  362. goto done;
  363. ncopied += to_copy;
  364. /* set up for next segment */
  365. seg_offset = footer->oprom_size;
  366. seg_base += SEG_SIZE;
  367. }
  368. /* success */
  369. ret = 0;
  370. *data = buffer;
  371. *size = entry->size;
  372. done:
  373. kfree(table_buffer);
  374. if (ret)
  375. kfree(buffer);
  376. return ret;
  377. }
  378. /*
  379. * Read the platform configuration file from the EPROM.
  380. *
  381. * On success, an allocated buffer containing the data and its size are
  382. * returned. It is up to the caller to free this buffer.
  383. *
  384. * Return value:
  385. * 0 - success
  386. * -ENXIO - no EPROM is available
  387. * -EBUSY - not able to acquire access to the EPROM
  388. * -ENOENT - no recognizable file written
  389. * -ENOMEM - buffer could not be allocated
  390. * -EINVAL - invalid EPROM contentents found
  391. */
  392. int eprom_read_platform_config(struct hfi1_devdata *dd, void **data, u32 *size)
  393. {
  394. u32 directory[EP_PAGE_DWORDS]; /* aligned buffer */
  395. int ret;
  396. if (!dd->eprom_available)
  397. return -ENXIO;
  398. ret = acquire_chip_resource(dd, CR_EPROM, EPROM_TIMEOUT);
  399. if (ret)
  400. return -EBUSY;
  401. /* read the last page of the segment for the EPROM format magic */
  402. ret = read_length(dd, SEG_SIZE - EP_PAGE_SIZE, EP_PAGE_SIZE, directory);
  403. if (ret)
  404. goto done;
  405. /* last dword of the segment contains a magic value */
  406. if (directory[EP_PAGE_DWORDS - 1] == FOOTER_MAGIC) {
  407. /* segment format */
  408. ret = read_segment_platform_config(dd, directory, data, size);
  409. } else {
  410. /* partition format */
  411. ret = read_partition_platform_config(dd, data, size);
  412. }
  413. done:
  414. release_chip_resource(dd, CR_EPROM);
  415. return ret;
  416. }