i40e_nvm.c 10 KB

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