eprom.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 begins an image */
  179. #define IMAGE_START_MAGIC "APO="
  180. /* magic character sequence that might trail an image */
  181. #define IMAGE_TRAIL_MAGIC "egamiAPO"
  182. /* EPROM file types */
  183. #define HFI1_EFT_PLATFORM_CONFIG 2
  184. /* segment size - 128 KiB */
  185. #define SEG_SIZE (128 * 1024)
  186. struct hfi1_eprom_footer {
  187. u32 oprom_size; /* size of the oprom, in bytes */
  188. u16 num_table_entries;
  189. u16 version; /* version of this footer */
  190. u32 magic; /* must be last */
  191. };
  192. struct hfi1_eprom_table_entry {
  193. u32 type; /* file type */
  194. u32 offset; /* file offset from start of EPROM */
  195. u32 size; /* file size, in bytes */
  196. };
  197. /*
  198. * Calculate the max number of table entries that will fit within a directory
  199. * buffer of size 'dir_size'.
  200. */
  201. #define MAX_TABLE_ENTRIES(dir_size) \
  202. (((dir_size) - sizeof(struct hfi1_eprom_footer)) / \
  203. sizeof(struct hfi1_eprom_table_entry))
  204. #define DIRECTORY_SIZE(n) (sizeof(struct hfi1_eprom_footer) + \
  205. (sizeof(struct hfi1_eprom_table_entry) * (n)))
  206. #define MAGIC4(a, b, c, d) ((d) << 24 | (c) << 16 | (b) << 8 | (a))
  207. #define FOOTER_MAGIC MAGIC4('e', 'p', 'r', 'm')
  208. #define FOOTER_VERSION 1
  209. /*
  210. * Read all of partition 1. The actual file is at the front. Adjust
  211. * the returned size if a trailing image magic is found.
  212. */
  213. static int read_partition_platform_config(struct hfi1_devdata *dd, void **data,
  214. u32 *size)
  215. {
  216. void *buffer;
  217. void *p;
  218. u32 length;
  219. int ret;
  220. buffer = kmalloc(P1_SIZE, GFP_KERNEL);
  221. if (!buffer)
  222. return -ENOMEM;
  223. ret = read_length(dd, P1_START, P1_SIZE, buffer);
  224. if (ret) {
  225. kfree(buffer);
  226. return ret;
  227. }
  228. /* config partition is valid only if it starts with IMAGE_START_MAGIC */
  229. if (memcmp(buffer, IMAGE_START_MAGIC, strlen(IMAGE_START_MAGIC))) {
  230. kfree(buffer);
  231. return -ENOENT;
  232. }
  233. /* scan for image magic that may trail the actual data */
  234. p = strnstr(buffer, IMAGE_TRAIL_MAGIC, P1_SIZE);
  235. if (p)
  236. length = p - buffer;
  237. else
  238. length = P1_SIZE;
  239. *data = buffer;
  240. *size = length;
  241. return 0;
  242. }
  243. /*
  244. * The segment magic has been checked. There is a footer and table of
  245. * contents present.
  246. *
  247. * directory is a u32 aligned buffer of size EP_PAGE_SIZE.
  248. */
  249. static int read_segment_platform_config(struct hfi1_devdata *dd,
  250. void *directory, void **data, u32 *size)
  251. {
  252. struct hfi1_eprom_footer *footer;
  253. struct hfi1_eprom_table_entry *table;
  254. struct hfi1_eprom_table_entry *entry;
  255. void *buffer = NULL;
  256. void *table_buffer = NULL;
  257. int ret, i;
  258. u32 directory_size;
  259. u32 seg_base, seg_offset;
  260. u32 bytes_available, ncopied, to_copy;
  261. /* the footer is at the end of the directory */
  262. footer = (struct hfi1_eprom_footer *)
  263. (directory + EP_PAGE_SIZE - sizeof(*footer));
  264. /* make sure the structure version is supported */
  265. if (footer->version != FOOTER_VERSION)
  266. return -EINVAL;
  267. /* oprom size cannot be larger than a segment */
  268. if (footer->oprom_size >= SEG_SIZE)
  269. return -EINVAL;
  270. /* the file table must fit in a segment with the oprom */
  271. if (footer->num_table_entries >
  272. MAX_TABLE_ENTRIES(SEG_SIZE - footer->oprom_size))
  273. return -EINVAL;
  274. /* find the file table start, which precedes the footer */
  275. directory_size = DIRECTORY_SIZE(footer->num_table_entries);
  276. if (directory_size <= EP_PAGE_SIZE) {
  277. /* the file table fits into the directory buffer handed in */
  278. table = (struct hfi1_eprom_table_entry *)
  279. (directory + EP_PAGE_SIZE - directory_size);
  280. } else {
  281. /* need to allocate and read more */
  282. table_buffer = kmalloc(directory_size, GFP_KERNEL);
  283. if (!table_buffer)
  284. return -ENOMEM;
  285. ret = read_length(dd, SEG_SIZE - directory_size,
  286. directory_size, table_buffer);
  287. if (ret)
  288. goto done;
  289. table = table_buffer;
  290. }
  291. /* look for the platform configuration file in the table */
  292. for (entry = NULL, i = 0; i < footer->num_table_entries; i++) {
  293. if (table[i].type == HFI1_EFT_PLATFORM_CONFIG) {
  294. entry = &table[i];
  295. break;
  296. }
  297. }
  298. if (!entry) {
  299. ret = -ENOENT;
  300. goto done;
  301. }
  302. /*
  303. * Sanity check on the configuration file size - it should never
  304. * be larger than 4 KiB.
  305. */
  306. if (entry->size > (4 * 1024)) {
  307. dd_dev_err(dd, "Bad configuration file size 0x%x\n",
  308. entry->size);
  309. ret = -EINVAL;
  310. goto done;
  311. }
  312. /* check for bogus offset and size that wrap when added together */
  313. if (entry->offset + entry->size < entry->offset) {
  314. dd_dev_err(dd,
  315. "Bad configuration file start + size 0x%x+0x%x\n",
  316. entry->offset, entry->size);
  317. ret = -EINVAL;
  318. goto done;
  319. }
  320. /* allocate the buffer to return */
  321. buffer = kmalloc(entry->size, GFP_KERNEL);
  322. if (!buffer) {
  323. ret = -ENOMEM;
  324. goto done;
  325. }
  326. /*
  327. * Extract the file by looping over segments until it is fully read.
  328. */
  329. seg_offset = entry->offset % SEG_SIZE;
  330. seg_base = entry->offset - seg_offset;
  331. ncopied = 0;
  332. while (ncopied < entry->size) {
  333. /* calculate data bytes available in this segment */
  334. /* start with the bytes from the current offset to the end */
  335. bytes_available = SEG_SIZE - seg_offset;
  336. /* subtract off footer and table from segment 0 */
  337. if (seg_base == 0) {
  338. /*
  339. * Sanity check: should not have a starting point
  340. * at or within the directory.
  341. */
  342. if (bytes_available <= directory_size) {
  343. dd_dev_err(dd,
  344. "Bad configuration file - offset 0x%x within footer+table\n",
  345. entry->offset);
  346. ret = -EINVAL;
  347. goto done;
  348. }
  349. bytes_available -= directory_size;
  350. }
  351. /* calculate bytes wanted */
  352. to_copy = entry->size - ncopied;
  353. /* max out at the available bytes in this segment */
  354. if (to_copy > bytes_available)
  355. to_copy = bytes_available;
  356. /*
  357. * Read from the EPROM.
  358. *
  359. * The sanity check for entry->offset is done in read_length().
  360. * The EPROM offset is validated against what the hardware
  361. * addressing supports. In addition, if the offset is larger
  362. * than the actual EPROM, it silently wraps. It will work
  363. * fine, though the reader may not get what they expected
  364. * from the EPROM.
  365. */
  366. ret = read_length(dd, seg_base + seg_offset, to_copy,
  367. buffer + ncopied);
  368. if (ret)
  369. goto done;
  370. ncopied += to_copy;
  371. /* set up for next segment */
  372. seg_offset = footer->oprom_size;
  373. seg_base += SEG_SIZE;
  374. }
  375. /* success */
  376. ret = 0;
  377. *data = buffer;
  378. *size = entry->size;
  379. done:
  380. kfree(table_buffer);
  381. if (ret)
  382. kfree(buffer);
  383. return ret;
  384. }
  385. /*
  386. * Read the platform configuration file from the EPROM.
  387. *
  388. * On success, an allocated buffer containing the data and its size are
  389. * returned. It is up to the caller to free this buffer.
  390. *
  391. * Return value:
  392. * 0 - success
  393. * -ENXIO - no EPROM is available
  394. * -EBUSY - not able to acquire access to the EPROM
  395. * -ENOENT - no recognizable file written
  396. * -ENOMEM - buffer could not be allocated
  397. * -EINVAL - invalid EPROM contentents found
  398. */
  399. int eprom_read_platform_config(struct hfi1_devdata *dd, void **data, u32 *size)
  400. {
  401. u32 directory[EP_PAGE_DWORDS]; /* aligned buffer */
  402. int ret;
  403. if (!dd->eprom_available)
  404. return -ENXIO;
  405. ret = acquire_chip_resource(dd, CR_EPROM, EPROM_TIMEOUT);
  406. if (ret)
  407. return -EBUSY;
  408. /* read the last page of the segment for the EPROM format magic */
  409. ret = read_length(dd, SEG_SIZE - EP_PAGE_SIZE, EP_PAGE_SIZE, directory);
  410. if (ret)
  411. goto done;
  412. /* last dword of the segment contains a magic value */
  413. if (directory[EP_PAGE_DWORDS - 1] == FOOTER_MAGIC) {
  414. /* segment format */
  415. ret = read_segment_platform_config(dd, directory, data, size);
  416. } else {
  417. /* partition format */
  418. ret = read_partition_platform_config(dd, data, size);
  419. }
  420. done:
  421. release_chip_resource(dd, CR_EPROM);
  422. return ret;
  423. }