i40e_nvm.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. i40e_debug(hw, I40E_DEBUG_NVM, "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_left = 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_left, 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_left) + gtime;
  86. if (ret_code)
  87. i40e_debug(hw, I40E_DEBUG_NVM,
  88. "NVM acquire type %d failed time_left=%llu ret=%d aq_err=%d\n",
  89. access, time_left, ret_code, hw->aq.asq_last_status);
  90. if (ret_code && time_left) {
  91. /* Poll until the current NVM owner timeouts */
  92. timeout = I40E_MS_TO_GTIME(I40E_MAX_NVM_TIMEOUT) + gtime;
  93. while ((gtime < timeout) && time_left) {
  94. usleep_range(10000, 20000);
  95. gtime = rd32(hw, I40E_GLVFGEN_TIMER);
  96. ret_code = i40e_aq_request_resource(hw,
  97. I40E_NVM_RESOURCE_ID,
  98. access, 0, &time_left,
  99. NULL);
  100. if (!ret_code) {
  101. hw->nvm.hw_semaphore_timeout =
  102. I40E_MS_TO_GTIME(time_left) + gtime;
  103. break;
  104. }
  105. }
  106. if (ret_code) {
  107. hw->nvm.hw_semaphore_timeout = 0;
  108. i40e_debug(hw, I40E_DEBUG_NVM,
  109. "NVM acquire timed out, wait %llu ms before trying again. status=%d aq_err=%d\n",
  110. time_left, ret_code, hw->aq.asq_last_status);
  111. }
  112. }
  113. i40e_i40e_acquire_nvm_exit:
  114. return ret_code;
  115. }
  116. /**
  117. * i40e_release_nvm - Generic request for releasing the NVM ownership
  118. * @hw: pointer to the HW structure
  119. *
  120. * This function will release NVM resource via the proper Admin Command.
  121. **/
  122. void i40e_release_nvm(struct i40e_hw *hw)
  123. {
  124. if (!hw->nvm.blank_nvm_mode)
  125. i40e_aq_release_resource(hw, I40E_NVM_RESOURCE_ID, 0, NULL);
  126. }
  127. /**
  128. * i40e_poll_sr_srctl_done_bit - Polls the GLNVM_SRCTL done bit
  129. * @hw: pointer to the HW structure
  130. *
  131. * Polls the SRCTL Shadow RAM register done bit.
  132. **/
  133. static i40e_status i40e_poll_sr_srctl_done_bit(struct i40e_hw *hw)
  134. {
  135. i40e_status ret_code = I40E_ERR_TIMEOUT;
  136. u32 srctl, wait_cnt;
  137. /* Poll the I40E_GLNVM_SRCTL until the done bit is set */
  138. for (wait_cnt = 0; wait_cnt < I40E_SRRD_SRCTL_ATTEMPTS; wait_cnt++) {
  139. srctl = rd32(hw, I40E_GLNVM_SRCTL);
  140. if (srctl & I40E_GLNVM_SRCTL_DONE_MASK) {
  141. ret_code = 0;
  142. break;
  143. }
  144. udelay(5);
  145. }
  146. if (ret_code == I40E_ERR_TIMEOUT)
  147. i40e_debug(hw, I40E_DEBUG_NVM, "Done bit in GLNVM_SRCTL not set");
  148. return ret_code;
  149. }
  150. /**
  151. * i40e_read_nvm_word_srctl - Reads Shadow RAM via SRCTL register
  152. * @hw: pointer to the HW structure
  153. * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
  154. * @data: word read from the Shadow RAM
  155. *
  156. * Reads one 16 bit word from the Shadow RAM using the GLNVM_SRCTL register.
  157. **/
  158. static i40e_status i40e_read_nvm_word_srctl(struct i40e_hw *hw, u16 offset,
  159. u16 *data)
  160. {
  161. i40e_status ret_code = I40E_ERR_TIMEOUT;
  162. u32 sr_reg;
  163. if (offset >= hw->nvm.sr_size) {
  164. i40e_debug(hw, I40E_DEBUG_NVM,
  165. "NVM read error: offset %d beyond Shadow RAM limit %d\n",
  166. offset, hw->nvm.sr_size);
  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. i40e_debug(hw, I40E_DEBUG_NVM,
  188. "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
  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 one 16 bit word from the Shadow RAM using the GLNVM_SRCTL register.
  200. **/
  201. i40e_status i40e_read_nvm_word(struct i40e_hw *hw, u16 offset,
  202. u16 *data)
  203. {
  204. return i40e_read_nvm_word_srctl(hw, offset, data);
  205. }
  206. /**
  207. * i40e_read_nvm_buffer_srctl - Reads Shadow RAM buffer via SRCTL register
  208. * @hw: pointer to the HW structure
  209. * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
  210. * @words: (in) number of words to read; (out) number of words actually read
  211. * @data: words read from the Shadow RAM
  212. *
  213. * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_srrd()
  214. * method. The buffer read is preceded by the NVM ownership take
  215. * and followed by the release.
  216. **/
  217. static i40e_status i40e_read_nvm_buffer_srctl(struct i40e_hw *hw, u16 offset,
  218. u16 *words, u16 *data)
  219. {
  220. i40e_status ret_code = 0;
  221. u16 index, word;
  222. /* Loop thru the selected region */
  223. for (word = 0; word < *words; word++) {
  224. index = offset + word;
  225. ret_code = i40e_read_nvm_word_srctl(hw, index, &data[word]);
  226. if (ret_code)
  227. break;
  228. }
  229. /* Update the number of words read from the Shadow RAM */
  230. *words = word;
  231. return ret_code;
  232. }
  233. /**
  234. * i40e_read_nvm_buffer - Reads Shadow RAM buffer
  235. * @hw: pointer to the HW structure
  236. * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
  237. * @words: (in) number of words to read; (out) number of words actually read
  238. * @data: words read from the Shadow RAM
  239. *
  240. * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_srrd()
  241. * method. The buffer read is preceded by the NVM ownership take
  242. * and followed by the release.
  243. **/
  244. i40e_status i40e_read_nvm_buffer(struct i40e_hw *hw, u16 offset,
  245. u16 *words, u16 *data)
  246. {
  247. return i40e_read_nvm_buffer_srctl(hw, offset, words, data);
  248. }
  249. /**
  250. * i40e_write_nvm_aq - Writes Shadow RAM.
  251. * @hw: pointer to the HW structure.
  252. * @module_pointer: module pointer location in words from the NVM beginning
  253. * @offset: offset in words from module start
  254. * @words: number of words to write
  255. * @data: buffer with words to write to the Shadow RAM
  256. * @last_command: tells the AdminQ that this is the last command
  257. *
  258. * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
  259. **/
  260. static i40e_status i40e_write_nvm_aq(struct i40e_hw *hw, u8 module_pointer,
  261. u32 offset, u16 words, void *data,
  262. bool last_command)
  263. {
  264. i40e_status ret_code = I40E_ERR_NVM;
  265. /* Here we are checking the SR limit only for the flat memory model.
  266. * We cannot do it for the module-based model, as we did not acquire
  267. * the NVM resource yet (we cannot get the module pointer value).
  268. * Firmware will check the module-based model.
  269. */
  270. if ((offset + words) > hw->nvm.sr_size)
  271. i40e_debug(hw, I40E_DEBUG_NVM,
  272. "NVM write error: offset %d beyond Shadow RAM limit %d\n",
  273. (offset + words), hw->nvm.sr_size);
  274. else if (words > I40E_SR_SECTOR_SIZE_IN_WORDS)
  275. /* We can write only up to 4KB (one sector), in one AQ write */
  276. i40e_debug(hw, I40E_DEBUG_NVM,
  277. "NVM write fail error: tried to write %d words, limit is %d.\n",
  278. words, I40E_SR_SECTOR_SIZE_IN_WORDS);
  279. else if (((offset + (words - 1)) / I40E_SR_SECTOR_SIZE_IN_WORDS)
  280. != (offset / I40E_SR_SECTOR_SIZE_IN_WORDS))
  281. /* A single write cannot spread over two sectors */
  282. i40e_debug(hw, I40E_DEBUG_NVM,
  283. "NVM write error: cannot spread over two sectors in a single write offset=%d words=%d\n",
  284. offset, words);
  285. else
  286. ret_code = i40e_aq_update_nvm(hw, module_pointer,
  287. 2 * offset, /*bytes*/
  288. 2 * words, /*bytes*/
  289. data, last_command, NULL);
  290. return ret_code;
  291. }
  292. /**
  293. * i40e_calc_nvm_checksum - Calculates and returns the checksum
  294. * @hw: pointer to hardware structure
  295. * @checksum: pointer to the checksum
  296. *
  297. * This function calculates SW Checksum that covers the whole 64kB shadow RAM
  298. * except the VPD and PCIe ALT Auto-load modules. The structure and size of VPD
  299. * is customer specific and unknown. Therefore, this function skips all maximum
  300. * possible size of VPD (1kB).
  301. **/
  302. static i40e_status i40e_calc_nvm_checksum(struct i40e_hw *hw,
  303. u16 *checksum)
  304. {
  305. i40e_status ret_code = 0;
  306. struct i40e_virt_mem vmem;
  307. u16 pcie_alt_module = 0;
  308. u16 checksum_local = 0;
  309. u16 vpd_module = 0;
  310. u16 *data;
  311. u16 i = 0;
  312. ret_code = i40e_allocate_virt_mem(hw, &vmem,
  313. I40E_SR_SECTOR_SIZE_IN_WORDS * sizeof(u16));
  314. if (ret_code)
  315. goto i40e_calc_nvm_checksum_exit;
  316. data = (u16 *)vmem.va;
  317. /* read pointer to VPD area */
  318. ret_code = i40e_read_nvm_word(hw, I40E_SR_VPD_PTR, &vpd_module);
  319. if (ret_code) {
  320. ret_code = I40E_ERR_NVM_CHECKSUM;
  321. goto i40e_calc_nvm_checksum_exit;
  322. }
  323. /* read pointer to PCIe Alt Auto-load module */
  324. ret_code = i40e_read_nvm_word(hw, I40E_SR_PCIE_ALT_AUTO_LOAD_PTR,
  325. &pcie_alt_module);
  326. if (ret_code) {
  327. ret_code = I40E_ERR_NVM_CHECKSUM;
  328. goto i40e_calc_nvm_checksum_exit;
  329. }
  330. /* Calculate SW checksum that covers the whole 64kB shadow RAM
  331. * except the VPD and PCIe ALT Auto-load modules
  332. */
  333. for (i = 0; i < hw->nvm.sr_size; i++) {
  334. /* Read SR page */
  335. if ((i % I40E_SR_SECTOR_SIZE_IN_WORDS) == 0) {
  336. u16 words = I40E_SR_SECTOR_SIZE_IN_WORDS;
  337. ret_code = i40e_read_nvm_buffer(hw, i, &words, data);
  338. if (ret_code) {
  339. ret_code = I40E_ERR_NVM_CHECKSUM;
  340. goto i40e_calc_nvm_checksum_exit;
  341. }
  342. }
  343. /* Skip Checksum word */
  344. if (i == I40E_SR_SW_CHECKSUM_WORD)
  345. continue;
  346. /* Skip VPD module (convert byte size to word count) */
  347. if ((i >= (u32)vpd_module) &&
  348. (i < ((u32)vpd_module +
  349. (I40E_SR_VPD_MODULE_MAX_SIZE / 2)))) {
  350. continue;
  351. }
  352. /* Skip PCIe ALT module (convert byte size to word count) */
  353. if ((i >= (u32)pcie_alt_module) &&
  354. (i < ((u32)pcie_alt_module +
  355. (I40E_SR_PCIE_ALT_MODULE_MAX_SIZE / 2)))) {
  356. continue;
  357. }
  358. checksum_local += data[i % I40E_SR_SECTOR_SIZE_IN_WORDS];
  359. }
  360. *checksum = (u16)I40E_SR_SW_CHECKSUM_BASE - checksum_local;
  361. i40e_calc_nvm_checksum_exit:
  362. i40e_free_virt_mem(hw, &vmem);
  363. return ret_code;
  364. }
  365. /**
  366. * i40e_update_nvm_checksum - Updates the NVM checksum
  367. * @hw: pointer to hardware structure
  368. *
  369. * NVM ownership must be acquired before calling this function and released
  370. * on ARQ completion event reception by caller.
  371. * This function will commit SR to NVM.
  372. **/
  373. i40e_status i40e_update_nvm_checksum(struct i40e_hw *hw)
  374. {
  375. i40e_status ret_code = 0;
  376. u16 checksum;
  377. ret_code = i40e_calc_nvm_checksum(hw, &checksum);
  378. if (!ret_code)
  379. ret_code = i40e_write_nvm_aq(hw, 0x00, I40E_SR_SW_CHECKSUM_WORD,
  380. 1, &checksum, true);
  381. return ret_code;
  382. }
  383. /**
  384. * i40e_validate_nvm_checksum - Validate EEPROM checksum
  385. * @hw: pointer to hardware structure
  386. * @checksum: calculated checksum
  387. *
  388. * Performs checksum calculation and validates the NVM SW checksum. If the
  389. * caller does not need checksum, the value can be NULL.
  390. **/
  391. i40e_status i40e_validate_nvm_checksum(struct i40e_hw *hw,
  392. u16 *checksum)
  393. {
  394. i40e_status ret_code = 0;
  395. u16 checksum_sr = 0;
  396. u16 checksum_local = 0;
  397. ret_code = i40e_calc_nvm_checksum(hw, &checksum_local);
  398. if (ret_code)
  399. goto i40e_validate_nvm_checksum_exit;
  400. /* Do not use i40e_read_nvm_word() because we do not want to take
  401. * the synchronization semaphores twice here.
  402. */
  403. i40e_read_nvm_word(hw, I40E_SR_SW_CHECKSUM_WORD, &checksum_sr);
  404. /* Verify read checksum from EEPROM is the same as
  405. * calculated checksum
  406. */
  407. if (checksum_local != checksum_sr)
  408. ret_code = I40E_ERR_NVM_CHECKSUM;
  409. /* If the user cares, return the calculated checksum */
  410. if (checksum)
  411. *checksum = checksum_local;
  412. i40e_validate_nvm_checksum_exit:
  413. return ret_code;
  414. }
  415. static i40e_status i40e_nvmupd_state_init(struct i40e_hw *hw,
  416. struct i40e_nvm_access *cmd,
  417. u8 *bytes, int *errno);
  418. static i40e_status i40e_nvmupd_state_reading(struct i40e_hw *hw,
  419. struct i40e_nvm_access *cmd,
  420. u8 *bytes, int *errno);
  421. static i40e_status i40e_nvmupd_state_writing(struct i40e_hw *hw,
  422. struct i40e_nvm_access *cmd,
  423. u8 *bytes, int *errno);
  424. static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
  425. struct i40e_nvm_access *cmd,
  426. int *errno);
  427. static i40e_status i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
  428. struct i40e_nvm_access *cmd,
  429. int *errno);
  430. static i40e_status i40e_nvmupd_nvm_write(struct i40e_hw *hw,
  431. struct i40e_nvm_access *cmd,
  432. u8 *bytes, int *errno);
  433. static i40e_status i40e_nvmupd_nvm_read(struct i40e_hw *hw,
  434. struct i40e_nvm_access *cmd,
  435. u8 *bytes, int *errno);
  436. static inline u8 i40e_nvmupd_get_module(u32 val)
  437. {
  438. return (u8)(val & I40E_NVM_MOD_PNT_MASK);
  439. }
  440. static inline u8 i40e_nvmupd_get_transaction(u32 val)
  441. {
  442. return (u8)((val & I40E_NVM_TRANS_MASK) >> I40E_NVM_TRANS_SHIFT);
  443. }
  444. static char *i40e_nvm_update_state_str[] = {
  445. "I40E_NVMUPD_INVALID",
  446. "I40E_NVMUPD_READ_CON",
  447. "I40E_NVMUPD_READ_SNT",
  448. "I40E_NVMUPD_READ_LCB",
  449. "I40E_NVMUPD_READ_SA",
  450. "I40E_NVMUPD_WRITE_ERA",
  451. "I40E_NVMUPD_WRITE_CON",
  452. "I40E_NVMUPD_WRITE_SNT",
  453. "I40E_NVMUPD_WRITE_LCB",
  454. "I40E_NVMUPD_WRITE_SA",
  455. "I40E_NVMUPD_CSUM_CON",
  456. "I40E_NVMUPD_CSUM_SA",
  457. "I40E_NVMUPD_CSUM_LCB",
  458. };
  459. /**
  460. * i40e_nvmupd_command - Process an NVM update command
  461. * @hw: pointer to hardware structure
  462. * @cmd: pointer to nvm update command
  463. * @bytes: pointer to the data buffer
  464. * @errno: pointer to return error code
  465. *
  466. * Dispatches command depending on what update state is current
  467. **/
  468. i40e_status i40e_nvmupd_command(struct i40e_hw *hw,
  469. struct i40e_nvm_access *cmd,
  470. u8 *bytes, int *errno)
  471. {
  472. i40e_status status;
  473. /* assume success */
  474. *errno = 0;
  475. switch (hw->nvmupd_state) {
  476. case I40E_NVMUPD_STATE_INIT:
  477. status = i40e_nvmupd_state_init(hw, cmd, bytes, errno);
  478. break;
  479. case I40E_NVMUPD_STATE_READING:
  480. status = i40e_nvmupd_state_reading(hw, cmd, bytes, errno);
  481. break;
  482. case I40E_NVMUPD_STATE_WRITING:
  483. status = i40e_nvmupd_state_writing(hw, cmd, bytes, errno);
  484. break;
  485. default:
  486. /* invalid state, should never happen */
  487. i40e_debug(hw, I40E_DEBUG_NVM,
  488. "NVMUPD: no such state %d\n", hw->nvmupd_state);
  489. status = I40E_NOT_SUPPORTED;
  490. *errno = -ESRCH;
  491. break;
  492. }
  493. return status;
  494. }
  495. /**
  496. * i40e_nvmupd_state_init - Handle NVM update state Init
  497. * @hw: pointer to hardware structure
  498. * @cmd: pointer to nvm update command buffer
  499. * @bytes: pointer to the data buffer
  500. * @errno: pointer to return error code
  501. *
  502. * Process legitimate commands of the Init state and conditionally set next
  503. * state. Reject all other commands.
  504. **/
  505. static i40e_status i40e_nvmupd_state_init(struct i40e_hw *hw,
  506. struct i40e_nvm_access *cmd,
  507. u8 *bytes, int *errno)
  508. {
  509. i40e_status status = 0;
  510. enum i40e_nvmupd_cmd upd_cmd;
  511. upd_cmd = i40e_nvmupd_validate_command(hw, cmd, errno);
  512. switch (upd_cmd) {
  513. case I40E_NVMUPD_READ_SA:
  514. status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
  515. if (status) {
  516. *errno = i40e_aq_rc_to_posix(status,
  517. hw->aq.asq_last_status);
  518. } else {
  519. status = i40e_nvmupd_nvm_read(hw, cmd, bytes, errno);
  520. i40e_release_nvm(hw);
  521. }
  522. break;
  523. case I40E_NVMUPD_READ_SNT:
  524. status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
  525. if (status) {
  526. *errno = i40e_aq_rc_to_posix(status,
  527. hw->aq.asq_last_status);
  528. } else {
  529. status = i40e_nvmupd_nvm_read(hw, cmd, bytes, errno);
  530. if (status)
  531. i40e_release_nvm(hw);
  532. else
  533. hw->nvmupd_state = I40E_NVMUPD_STATE_READING;
  534. }
  535. break;
  536. case I40E_NVMUPD_WRITE_ERA:
  537. status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
  538. if (status) {
  539. *errno = i40e_aq_rc_to_posix(status,
  540. hw->aq.asq_last_status);
  541. } else {
  542. status = i40e_nvmupd_nvm_erase(hw, cmd, errno);
  543. if (status)
  544. i40e_release_nvm(hw);
  545. else
  546. hw->aq.nvm_release_on_done = true;
  547. }
  548. break;
  549. case I40E_NVMUPD_WRITE_SA:
  550. status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
  551. if (status) {
  552. *errno = i40e_aq_rc_to_posix(status,
  553. hw->aq.asq_last_status);
  554. } else {
  555. status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
  556. if (status)
  557. i40e_release_nvm(hw);
  558. else
  559. hw->aq.nvm_release_on_done = true;
  560. }
  561. break;
  562. case I40E_NVMUPD_WRITE_SNT:
  563. status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
  564. if (status) {
  565. *errno = i40e_aq_rc_to_posix(status,
  566. hw->aq.asq_last_status);
  567. } else {
  568. status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
  569. if (status)
  570. i40e_release_nvm(hw);
  571. else
  572. hw->nvmupd_state = I40E_NVMUPD_STATE_WRITING;
  573. }
  574. break;
  575. case I40E_NVMUPD_CSUM_SA:
  576. status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
  577. if (status) {
  578. *errno = i40e_aq_rc_to_posix(status,
  579. hw->aq.asq_last_status);
  580. } else {
  581. status = i40e_update_nvm_checksum(hw);
  582. if (status) {
  583. *errno = hw->aq.asq_last_status ?
  584. i40e_aq_rc_to_posix(status,
  585. hw->aq.asq_last_status) :
  586. -EIO;
  587. i40e_release_nvm(hw);
  588. } else {
  589. hw->aq.nvm_release_on_done = true;
  590. }
  591. }
  592. break;
  593. default:
  594. i40e_debug(hw, I40E_DEBUG_NVM,
  595. "NVMUPD: bad cmd %s in init state\n",
  596. i40e_nvm_update_state_str[upd_cmd]);
  597. status = I40E_ERR_NVM;
  598. *errno = -ESRCH;
  599. break;
  600. }
  601. return status;
  602. }
  603. /**
  604. * i40e_nvmupd_state_reading - Handle NVM update state Reading
  605. * @hw: pointer to hardware structure
  606. * @cmd: pointer to nvm update command buffer
  607. * @bytes: pointer to the data buffer
  608. * @errno: pointer to return error code
  609. *
  610. * NVM ownership is already held. Process legitimate commands and set any
  611. * change in state; reject all other commands.
  612. **/
  613. static i40e_status i40e_nvmupd_state_reading(struct i40e_hw *hw,
  614. struct i40e_nvm_access *cmd,
  615. u8 *bytes, int *errno)
  616. {
  617. i40e_status status;
  618. enum i40e_nvmupd_cmd upd_cmd;
  619. upd_cmd = i40e_nvmupd_validate_command(hw, cmd, errno);
  620. switch (upd_cmd) {
  621. case I40E_NVMUPD_READ_SA:
  622. case I40E_NVMUPD_READ_CON:
  623. status = i40e_nvmupd_nvm_read(hw, cmd, bytes, errno);
  624. break;
  625. case I40E_NVMUPD_READ_LCB:
  626. status = i40e_nvmupd_nvm_read(hw, cmd, bytes, errno);
  627. i40e_release_nvm(hw);
  628. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
  629. break;
  630. default:
  631. i40e_debug(hw, I40E_DEBUG_NVM,
  632. "NVMUPD: bad cmd %s in reading state.\n",
  633. i40e_nvm_update_state_str[upd_cmd]);
  634. status = I40E_NOT_SUPPORTED;
  635. *errno = -ESRCH;
  636. break;
  637. }
  638. return status;
  639. }
  640. /**
  641. * i40e_nvmupd_state_writing - Handle NVM update state Writing
  642. * @hw: pointer to hardware structure
  643. * @cmd: pointer to nvm update command buffer
  644. * @bytes: pointer to the data buffer
  645. * @errno: pointer to return error code
  646. *
  647. * NVM ownership is already held. Process legitimate commands and set any
  648. * change in state; reject all other commands
  649. **/
  650. static i40e_status i40e_nvmupd_state_writing(struct i40e_hw *hw,
  651. struct i40e_nvm_access *cmd,
  652. u8 *bytes, int *errno)
  653. {
  654. i40e_status status;
  655. enum i40e_nvmupd_cmd upd_cmd;
  656. bool retry_attempt = false;
  657. upd_cmd = i40e_nvmupd_validate_command(hw, cmd, errno);
  658. retry:
  659. switch (upd_cmd) {
  660. case I40E_NVMUPD_WRITE_CON:
  661. status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
  662. break;
  663. case I40E_NVMUPD_WRITE_LCB:
  664. status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
  665. if (!status)
  666. hw->aq.nvm_release_on_done = true;
  667. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
  668. break;
  669. case I40E_NVMUPD_CSUM_CON:
  670. status = i40e_update_nvm_checksum(hw);
  671. if (status) {
  672. *errno = hw->aq.asq_last_status ?
  673. i40e_aq_rc_to_posix(status,
  674. hw->aq.asq_last_status) :
  675. -EIO;
  676. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
  677. }
  678. break;
  679. case I40E_NVMUPD_CSUM_LCB:
  680. status = i40e_update_nvm_checksum(hw);
  681. if (status)
  682. *errno = hw->aq.asq_last_status ?
  683. i40e_aq_rc_to_posix(status,
  684. hw->aq.asq_last_status) :
  685. -EIO;
  686. else
  687. hw->aq.nvm_release_on_done = true;
  688. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
  689. break;
  690. default:
  691. i40e_debug(hw, I40E_DEBUG_NVM,
  692. "NVMUPD: bad cmd %s in writing state.\n",
  693. i40e_nvm_update_state_str[upd_cmd]);
  694. status = I40E_NOT_SUPPORTED;
  695. *errno = -ESRCH;
  696. break;
  697. }
  698. /* In some circumstances, a multi-write transaction takes longer
  699. * than the default 3 minute timeout on the write semaphore. If
  700. * the write failed with an EBUSY status, this is likely the problem,
  701. * so here we try to reacquire the semaphore then retry the write.
  702. * We only do one retry, then give up.
  703. */
  704. if (status && (hw->aq.asq_last_status == I40E_AQ_RC_EBUSY) &&
  705. !retry_attempt) {
  706. i40e_status old_status = status;
  707. u32 old_asq_status = hw->aq.asq_last_status;
  708. u32 gtime;
  709. gtime = rd32(hw, I40E_GLVFGEN_TIMER);
  710. if (gtime >= hw->nvm.hw_semaphore_timeout) {
  711. i40e_debug(hw, I40E_DEBUG_ALL,
  712. "NVMUPD: write semaphore expired (%d >= %lld), retrying\n",
  713. gtime, hw->nvm.hw_semaphore_timeout);
  714. i40e_release_nvm(hw);
  715. status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
  716. if (status) {
  717. i40e_debug(hw, I40E_DEBUG_ALL,
  718. "NVMUPD: write semaphore reacquire failed aq_err = %d\n",
  719. hw->aq.asq_last_status);
  720. status = old_status;
  721. hw->aq.asq_last_status = old_asq_status;
  722. } else {
  723. retry_attempt = true;
  724. goto retry;
  725. }
  726. }
  727. }
  728. return status;
  729. }
  730. /**
  731. * i40e_nvmupd_validate_command - Validate given command
  732. * @hw: pointer to hardware structure
  733. * @cmd: pointer to nvm update command buffer
  734. * @errno: pointer to return error code
  735. *
  736. * Return one of the valid command types or I40E_NVMUPD_INVALID
  737. **/
  738. static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
  739. struct i40e_nvm_access *cmd,
  740. int *errno)
  741. {
  742. enum i40e_nvmupd_cmd upd_cmd;
  743. u8 transaction;
  744. /* anything that doesn't match a recognized case is an error */
  745. upd_cmd = I40E_NVMUPD_INVALID;
  746. transaction = i40e_nvmupd_get_transaction(cmd->config);
  747. /* limits on data size */
  748. if ((cmd->data_size < 1) ||
  749. (cmd->data_size > I40E_NVMUPD_MAX_DATA)) {
  750. i40e_debug(hw, I40E_DEBUG_NVM,
  751. "i40e_nvmupd_validate_command data_size %d\n",
  752. cmd->data_size);
  753. *errno = -EFAULT;
  754. return I40E_NVMUPD_INVALID;
  755. }
  756. switch (cmd->command) {
  757. case I40E_NVM_READ:
  758. switch (transaction) {
  759. case I40E_NVM_CON:
  760. upd_cmd = I40E_NVMUPD_READ_CON;
  761. break;
  762. case I40E_NVM_SNT:
  763. upd_cmd = I40E_NVMUPD_READ_SNT;
  764. break;
  765. case I40E_NVM_LCB:
  766. upd_cmd = I40E_NVMUPD_READ_LCB;
  767. break;
  768. case I40E_NVM_SA:
  769. upd_cmd = I40E_NVMUPD_READ_SA;
  770. break;
  771. }
  772. break;
  773. case I40E_NVM_WRITE:
  774. switch (transaction) {
  775. case I40E_NVM_CON:
  776. upd_cmd = I40E_NVMUPD_WRITE_CON;
  777. break;
  778. case I40E_NVM_SNT:
  779. upd_cmd = I40E_NVMUPD_WRITE_SNT;
  780. break;
  781. case I40E_NVM_LCB:
  782. upd_cmd = I40E_NVMUPD_WRITE_LCB;
  783. break;
  784. case I40E_NVM_SA:
  785. upd_cmd = I40E_NVMUPD_WRITE_SA;
  786. break;
  787. case I40E_NVM_ERA:
  788. upd_cmd = I40E_NVMUPD_WRITE_ERA;
  789. break;
  790. case I40E_NVM_CSUM:
  791. upd_cmd = I40E_NVMUPD_CSUM_CON;
  792. break;
  793. case (I40E_NVM_CSUM|I40E_NVM_SA):
  794. upd_cmd = I40E_NVMUPD_CSUM_SA;
  795. break;
  796. case (I40E_NVM_CSUM|I40E_NVM_LCB):
  797. upd_cmd = I40E_NVMUPD_CSUM_LCB;
  798. break;
  799. }
  800. break;
  801. }
  802. i40e_debug(hw, I40E_DEBUG_NVM, "%s state %d nvm_release_on_hold %d\n",
  803. i40e_nvm_update_state_str[upd_cmd],
  804. hw->nvmupd_state,
  805. hw->aq.nvm_release_on_done);
  806. if (upd_cmd == I40E_NVMUPD_INVALID) {
  807. *errno = -EFAULT;
  808. i40e_debug(hw, I40E_DEBUG_NVM,
  809. "i40e_nvmupd_validate_command returns %d errno %d\n",
  810. upd_cmd, *errno);
  811. }
  812. return upd_cmd;
  813. }
  814. /**
  815. * i40e_nvmupd_nvm_read - Read NVM
  816. * @hw: pointer to hardware structure
  817. * @cmd: pointer to nvm update command buffer
  818. * @bytes: pointer to the data buffer
  819. * @errno: pointer to return error code
  820. *
  821. * cmd structure contains identifiers and data buffer
  822. **/
  823. static i40e_status i40e_nvmupd_nvm_read(struct i40e_hw *hw,
  824. struct i40e_nvm_access *cmd,
  825. u8 *bytes, int *errno)
  826. {
  827. i40e_status status;
  828. u8 module, transaction;
  829. bool last;
  830. transaction = i40e_nvmupd_get_transaction(cmd->config);
  831. module = i40e_nvmupd_get_module(cmd->config);
  832. last = (transaction == I40E_NVM_LCB) || (transaction == I40E_NVM_SA);
  833. status = i40e_aq_read_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
  834. bytes, last, NULL);
  835. if (status) {
  836. i40e_debug(hw, I40E_DEBUG_NVM,
  837. "i40e_nvmupd_nvm_read mod 0x%x off 0x%x len 0x%x\n",
  838. module, cmd->offset, cmd->data_size);
  839. i40e_debug(hw, I40E_DEBUG_NVM,
  840. "i40e_nvmupd_nvm_read status %d aq %d\n",
  841. status, hw->aq.asq_last_status);
  842. *errno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
  843. }
  844. return status;
  845. }
  846. /**
  847. * i40e_nvmupd_nvm_erase - Erase an NVM module
  848. * @hw: pointer to hardware structure
  849. * @cmd: pointer to nvm update command buffer
  850. * @errno: pointer to return error code
  851. *
  852. * module, offset, data_size and data are in cmd structure
  853. **/
  854. static i40e_status i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
  855. struct i40e_nvm_access *cmd,
  856. int *errno)
  857. {
  858. i40e_status status = 0;
  859. u8 module, transaction;
  860. bool last;
  861. transaction = i40e_nvmupd_get_transaction(cmd->config);
  862. module = i40e_nvmupd_get_module(cmd->config);
  863. last = (transaction & I40E_NVM_LCB);
  864. status = i40e_aq_erase_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
  865. last, NULL);
  866. if (status) {
  867. i40e_debug(hw, I40E_DEBUG_NVM,
  868. "i40e_nvmupd_nvm_erase mod 0x%x off 0x%x len 0x%x\n",
  869. module, cmd->offset, cmd->data_size);
  870. i40e_debug(hw, I40E_DEBUG_NVM,
  871. "i40e_nvmupd_nvm_erase status %d aq %d\n",
  872. status, hw->aq.asq_last_status);
  873. *errno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
  874. }
  875. return status;
  876. }
  877. /**
  878. * i40e_nvmupd_nvm_write - Write NVM
  879. * @hw: pointer to hardware structure
  880. * @cmd: pointer to nvm update command buffer
  881. * @bytes: pointer to the data buffer
  882. * @errno: pointer to return error code
  883. *
  884. * module, offset, data_size and data are in cmd structure
  885. **/
  886. static i40e_status i40e_nvmupd_nvm_write(struct i40e_hw *hw,
  887. struct i40e_nvm_access *cmd,
  888. u8 *bytes, int *errno)
  889. {
  890. i40e_status status = 0;
  891. u8 module, transaction;
  892. bool last;
  893. transaction = i40e_nvmupd_get_transaction(cmd->config);
  894. module = i40e_nvmupd_get_module(cmd->config);
  895. last = (transaction & I40E_NVM_LCB);
  896. status = i40e_aq_update_nvm(hw, module, cmd->offset,
  897. (u16)cmd->data_size, bytes, last, NULL);
  898. if (status) {
  899. i40e_debug(hw, I40E_DEBUG_NVM,
  900. "i40e_nvmupd_nvm_write mod 0x%x off 0x%x len 0x%x\n",
  901. module, cmd->offset, cmd->data_size);
  902. i40e_debug(hw, I40E_DEBUG_NVM,
  903. "i40e_nvmupd_nvm_write status %d aq %d\n",
  904. status, hw->aq.asq_last_status);
  905. *errno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
  906. }
  907. return status;
  908. }