i40e_nvm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * The full GNU General Public License is included in this distribution in
  20. * the file called "COPYING".
  21. *
  22. * Contact Information:
  23. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  24. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  25. *
  26. ******************************************************************************/
  27. #include "i40e_prototype.h"
  28. /**
  29. * i40e_init_nvm_ops - Initialize NVM function pointers.
  30. * @hw: pointer to the HW structure.
  31. *
  32. * Setups the function pointers and the NVM info structure. Should be called
  33. * once per NVM initialization, e.g. inside the i40e_init_shared_code().
  34. * Please notice that the NVM term is used here (& in all methods covered
  35. * in this file) as an equivalent of the FLASH part mapped into the SR.
  36. * We are accessing FLASH always thru the Shadow RAM.
  37. **/
  38. i40e_status i40e_init_nvm(struct i40e_hw *hw)
  39. {
  40. struct i40e_nvm_info *nvm = &hw->nvm;
  41. i40e_status ret_code = 0;
  42. u32 fla, gens;
  43. u8 sr_size;
  44. /* The SR size is stored regardless of the nvm programming mode
  45. * as the blank mode may be used in the factory line.
  46. */
  47. gens = rd32(hw, I40E_GLNVM_GENS);
  48. sr_size = ((gens & I40E_GLNVM_GENS_SR_SIZE_MASK) >>
  49. I40E_GLNVM_GENS_SR_SIZE_SHIFT);
  50. /* Switching to words (sr_size contains power of 2KB). */
  51. nvm->sr_size = (1 << sr_size) * I40E_SR_WORDS_IN_1KB;
  52. /* Check if we are in the normal or blank NVM programming mode. */
  53. fla = rd32(hw, I40E_GLNVM_FLA);
  54. if (fla & I40E_GLNVM_FLA_LOCKED_MASK) { /* Normal programming mode. */
  55. /* Max NVM timeout. */
  56. nvm->timeout = I40E_MAX_NVM_TIMEOUT;
  57. nvm->blank_nvm_mode = false;
  58. } else { /* Blank programming mode. */
  59. nvm->blank_nvm_mode = true;
  60. ret_code = I40E_ERR_NVM_BLANK_MODE;
  61. hw_dbg(hw, "NVM init error: unsupported blank mode.\n");
  62. }
  63. return ret_code;
  64. }
  65. /**
  66. * i40e_acquire_nvm - Generic request for acquiring the NVM ownership.
  67. * @hw: pointer to the HW structure.
  68. * @access: NVM access type (read or write).
  69. *
  70. * This function will request NVM ownership for reading
  71. * via the proper Admin Command.
  72. **/
  73. i40e_status i40e_acquire_nvm(struct i40e_hw *hw,
  74. enum i40e_aq_resource_access_type access)
  75. {
  76. i40e_status ret_code = 0;
  77. u64 gtime, timeout;
  78. u64 time = 0;
  79. if (hw->nvm.blank_nvm_mode)
  80. goto i40e_i40e_acquire_nvm_exit;
  81. ret_code = i40e_aq_request_resource(hw, I40E_NVM_RESOURCE_ID, access,
  82. 0, &time, NULL);
  83. /* Reading the Global Device Timer. */
  84. gtime = rd32(hw, I40E_GLVFGEN_TIMER);
  85. /* Store the timeout. */
  86. hw->nvm.hw_semaphore_timeout = I40E_MS_TO_GTIME(time) + gtime;
  87. if (ret_code) {
  88. /* Set the polling timeout. */
  89. if (time > I40E_MAX_NVM_TIMEOUT)
  90. timeout = I40E_MS_TO_GTIME(I40E_MAX_NVM_TIMEOUT)
  91. + gtime;
  92. else
  93. timeout = hw->nvm.hw_semaphore_timeout;
  94. /* Poll until the current NVM owner timeouts. */
  95. while (gtime < timeout) {
  96. usleep_range(10000, 20000);
  97. ret_code = i40e_aq_request_resource(hw,
  98. I40E_NVM_RESOURCE_ID,
  99. access, 0, &time,
  100. NULL);
  101. if (!ret_code) {
  102. hw->nvm.hw_semaphore_timeout =
  103. I40E_MS_TO_GTIME(time) + gtime;
  104. break;
  105. }
  106. gtime = rd32(hw, I40E_GLVFGEN_TIMER);
  107. }
  108. if (ret_code) {
  109. hw->nvm.hw_semaphore_timeout = 0;
  110. hw->nvm.hw_semaphore_wait =
  111. I40E_MS_TO_GTIME(time) + gtime;
  112. hw_dbg(hw, "NVM acquire timed out, wait %llu ms before trying again.\n",
  113. time);
  114. }
  115. }
  116. i40e_i40e_acquire_nvm_exit:
  117. return ret_code;
  118. }
  119. /**
  120. * i40e_release_nvm - Generic request for releasing the NVM ownership.
  121. * @hw: pointer to the HW structure.
  122. *
  123. * This function will release NVM resource via the proper Admin Command.
  124. **/
  125. void i40e_release_nvm(struct i40e_hw *hw)
  126. {
  127. if (!hw->nvm.blank_nvm_mode)
  128. i40e_aq_release_resource(hw, I40E_NVM_RESOURCE_ID, 0, NULL);
  129. }
  130. /**
  131. * i40e_poll_sr_srctl_done_bit - Polls the GLNVM_SRCTL done bit.
  132. * @hw: pointer to the HW structure.
  133. *
  134. * Polls the SRCTL Shadow RAM register done bit.
  135. **/
  136. static i40e_status i40e_poll_sr_srctl_done_bit(struct i40e_hw *hw)
  137. {
  138. i40e_status ret_code = I40E_ERR_TIMEOUT;
  139. u32 srctl, wait_cnt;
  140. /* Poll the I40E_GLNVM_SRCTL until the done bit is set. */
  141. for (wait_cnt = 0; wait_cnt < I40E_SRRD_SRCTL_ATTEMPTS; wait_cnt++) {
  142. srctl = rd32(hw, I40E_GLNVM_SRCTL);
  143. if (srctl & I40E_GLNVM_SRCTL_DONE_MASK) {
  144. ret_code = 0;
  145. break;
  146. }
  147. udelay(5);
  148. }
  149. if (ret_code == I40E_ERR_TIMEOUT)
  150. hw_dbg(hw, "Done bit in GLNVM_SRCTL not set");
  151. return ret_code;
  152. }
  153. /**
  154. * i40e_read_nvm_srctl - Reads Shadow RAM.
  155. * @hw: pointer to the HW structure.
  156. * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
  157. * @data: word read from the Shadow RAM.
  158. *
  159. * Reads 16 bit word from the Shadow RAM using the GLNVM_SRCTL register.
  160. **/
  161. static i40e_status i40e_read_nvm_srctl(struct i40e_hw *hw, u16 offset,
  162. u16 *data)
  163. {
  164. i40e_status ret_code = I40E_ERR_TIMEOUT;
  165. u32 sr_reg;
  166. if (offset >= hw->nvm.sr_size) {
  167. hw_dbg(hw, "NVM read error: Offset beyond Shadow RAM limit.\n");
  168. ret_code = I40E_ERR_PARAM;
  169. goto read_nvm_exit;
  170. }
  171. /* Poll the done bit first. */
  172. ret_code = i40e_poll_sr_srctl_done_bit(hw);
  173. if (!ret_code) {
  174. /* Write the address and start reading. */
  175. sr_reg = (u32)(offset << I40E_GLNVM_SRCTL_ADDR_SHIFT) |
  176. (1 << I40E_GLNVM_SRCTL_START_SHIFT);
  177. wr32(hw, I40E_GLNVM_SRCTL, sr_reg);
  178. /* Poll I40E_GLNVM_SRCTL until the done bit is set. */
  179. ret_code = i40e_poll_sr_srctl_done_bit(hw);
  180. if (!ret_code) {
  181. sr_reg = rd32(hw, I40E_GLNVM_SRDATA);
  182. *data = (u16)((sr_reg &
  183. I40E_GLNVM_SRDATA_RDDATA_MASK)
  184. >> I40E_GLNVM_SRDATA_RDDATA_SHIFT);
  185. }
  186. }
  187. if (ret_code)
  188. hw_dbg(hw, "NVM read error: Couldn't access Shadow RAM address: 0x%x\n",
  189. offset);
  190. read_nvm_exit:
  191. return ret_code;
  192. }
  193. /**
  194. * i40e_read_nvm_word - Reads Shadow RAM word.
  195. * @hw: pointer to the HW structure.
  196. * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
  197. * @data: word read from the Shadow RAM.
  198. *
  199. * Reads 16 bit word from the Shadow RAM. Each read is preceded
  200. * with the NVM ownership taking and followed by the release.
  201. **/
  202. i40e_status i40e_read_nvm_word(struct i40e_hw *hw, u16 offset,
  203. u16 *data)
  204. {
  205. i40e_status ret_code = 0;
  206. ret_code = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
  207. if (!ret_code) {
  208. ret_code = i40e_read_nvm_srctl(hw, offset, data);
  209. i40e_release_nvm(hw);
  210. }
  211. return ret_code;
  212. }
  213. /**
  214. * i40e_read_nvm_buffer - Reads Shadow RAM buffer.
  215. * @hw: pointer to the HW structure.
  216. * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
  217. * @words: number of words to read (in) &
  218. * number of words read before the NVM ownership timeout (out).
  219. * @data: words read from the Shadow RAM.
  220. *
  221. * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_srrd()
  222. * method. The buffer read is preceded by the NVM ownership take
  223. * and followed by the release.
  224. **/
  225. i40e_status i40e_read_nvm_buffer(struct i40e_hw *hw, u16 offset,
  226. u16 *words, u16 *data)
  227. {
  228. i40e_status ret_code = 0;
  229. u16 index, word;
  230. u32 time;
  231. ret_code = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
  232. if (!ret_code) {
  233. /* Loop thru the selected region. */
  234. for (word = 0; word < *words; word++) {
  235. index = offset + word;
  236. ret_code = i40e_read_nvm_srctl(hw, index, &data[word]);
  237. if (ret_code)
  238. break;
  239. /* Check if we didn't exceeded the semaphore timeout. */
  240. time = rd32(hw, I40E_GLVFGEN_TIMER);
  241. if (time >= hw->nvm.hw_semaphore_timeout) {
  242. ret_code = I40E_ERR_TIMEOUT;
  243. hw_dbg(hw, "NVM read error: timeout.\n");
  244. break;
  245. }
  246. }
  247. /* Update the number of words read from the Shadow RAM. */
  248. *words = word;
  249. /* Release the NVM ownership. */
  250. i40e_release_nvm(hw);
  251. }
  252. return ret_code;
  253. }
  254. /**
  255. * i40e_calc_nvm_checksum - Calculates and returns the checksum
  256. * @hw: pointer to hardware structure
  257. *
  258. * This function calculate SW Checksum that covers the whole 64kB shadow RAM
  259. * except the VPD and PCIe ALT Auto-load modules. The structure and size of VPD
  260. * is customer specific and unknown. Therefore, this function skips all maximum
  261. * possible size of VPD (1kB).
  262. **/
  263. static i40e_status i40e_calc_nvm_checksum(struct i40e_hw *hw,
  264. u16 *checksum)
  265. {
  266. i40e_status ret_code = 0;
  267. u16 pcie_alt_module = 0;
  268. u16 checksum_local = 0;
  269. u16 vpd_module = 0;
  270. u16 word = 0;
  271. u32 i = 0;
  272. /* read pointer to VPD area */
  273. ret_code = i40e_read_nvm_srctl(hw, I40E_SR_VPD_PTR, &vpd_module);
  274. if (ret_code) {
  275. ret_code = I40E_ERR_NVM_CHECKSUM;
  276. goto i40e_calc_nvm_checksum_exit;
  277. }
  278. /* read pointer to PCIe Alt Auto-load module */
  279. ret_code = i40e_read_nvm_srctl(hw, I40E_SR_PCIE_ALT_AUTO_LOAD_PTR,
  280. &pcie_alt_module);
  281. if (ret_code) {
  282. ret_code = I40E_ERR_NVM_CHECKSUM;
  283. goto i40e_calc_nvm_checksum_exit;
  284. }
  285. /* Calculate SW checksum that covers the whole 64kB shadow RAM
  286. * except the VPD and PCIe ALT Auto-load modules
  287. */
  288. for (i = 0; i < hw->nvm.sr_size; i++) {
  289. /* Skip Checksum word */
  290. if (i == I40E_SR_SW_CHECKSUM_WORD)
  291. i++;
  292. /* Skip VPD module (convert byte size to word count) */
  293. if (i == (u32)vpd_module) {
  294. i += (I40E_SR_VPD_MODULE_MAX_SIZE / 2);
  295. if (i >= hw->nvm.sr_size)
  296. break;
  297. }
  298. /* Skip PCIe ALT module (convert byte size to word count) */
  299. if (i == (u32)pcie_alt_module) {
  300. i += (I40E_SR_PCIE_ALT_MODULE_MAX_SIZE / 2);
  301. if (i >= hw->nvm.sr_size)
  302. break;
  303. }
  304. ret_code = i40e_read_nvm_srctl(hw, (u16)i, &word);
  305. if (ret_code) {
  306. ret_code = I40E_ERR_NVM_CHECKSUM;
  307. goto i40e_calc_nvm_checksum_exit;
  308. }
  309. checksum_local += word;
  310. }
  311. *checksum = (u16)I40E_SR_SW_CHECKSUM_BASE - checksum_local;
  312. i40e_calc_nvm_checksum_exit:
  313. return ret_code;
  314. }
  315. /**
  316. * i40e_validate_nvm_checksum - Validate EEPROM checksum
  317. * @hw: pointer to hardware structure
  318. * @checksum: calculated checksum
  319. *
  320. * Performs checksum calculation and validates the NVM SW checksum. If the
  321. * caller does not need checksum, the value can be NULL.
  322. **/
  323. i40e_status i40e_validate_nvm_checksum(struct i40e_hw *hw,
  324. u16 *checksum)
  325. {
  326. i40e_status ret_code = 0;
  327. u16 checksum_sr = 0;
  328. u16 checksum_local;
  329. ret_code = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
  330. if (ret_code)
  331. goto i40e_validate_nvm_checksum_exit;
  332. ret_code = i40e_calc_nvm_checksum(hw, &checksum_local);
  333. if (ret_code)
  334. goto i40e_validate_nvm_checksum_free;
  335. /* Do not use i40e_read_nvm_word() because we do not want to take
  336. * the synchronization semaphores twice here.
  337. */
  338. i40e_read_nvm_srctl(hw, I40E_SR_SW_CHECKSUM_WORD, &checksum_sr);
  339. /* Verify read checksum from EEPROM is the same as
  340. * calculated checksum
  341. */
  342. if (checksum_local != checksum_sr)
  343. ret_code = I40E_ERR_NVM_CHECKSUM;
  344. /* If the user cares, return the calculated checksum */
  345. if (checksum)
  346. *checksum = checksum_local;
  347. i40e_validate_nvm_checksum_free:
  348. i40e_release_nvm(hw);
  349. i40e_validate_nvm_checksum_exit:
  350. return ret_code;
  351. }