i40e_nvm.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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_write_nvm_aq - Writes Shadow RAM.
  221. * @hw: pointer to the HW structure.
  222. * @module_pointer: module pointer location in words from the NVM beginning
  223. * @offset: offset in words from module start
  224. * @words: number of words to write
  225. * @data: buffer with words to write to the Shadow RAM
  226. * @last_command: tells the AdminQ that this is the last command
  227. *
  228. * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
  229. **/
  230. static i40e_status i40e_write_nvm_aq(struct i40e_hw *hw, u8 module_pointer,
  231. u32 offset, u16 words, void *data,
  232. bool last_command)
  233. {
  234. i40e_status ret_code = I40E_ERR_NVM;
  235. /* Here we are checking the SR limit only for the flat memory model.
  236. * We cannot do it for the module-based model, as we did not acquire
  237. * the NVM resource yet (we cannot get the module pointer value).
  238. * Firmware will check the module-based model.
  239. */
  240. if ((offset + words) > hw->nvm.sr_size)
  241. hw_dbg(hw, "NVM write error: offset beyond Shadow RAM limit.\n");
  242. else if (words > I40E_SR_SECTOR_SIZE_IN_WORDS)
  243. /* We can write only up to 4KB (one sector), in one AQ write */
  244. hw_dbg(hw, "NVM write fail error: cannot write more than 4KB in a single write.\n");
  245. else if (((offset + (words - 1)) / I40E_SR_SECTOR_SIZE_IN_WORDS)
  246. != (offset / I40E_SR_SECTOR_SIZE_IN_WORDS))
  247. /* A single write cannot spread over two sectors */
  248. hw_dbg(hw, "NVM write error: cannot spread over two sectors in a single write.\n");
  249. else
  250. ret_code = i40e_aq_update_nvm(hw, module_pointer,
  251. 2 * offset, /*bytes*/
  252. 2 * words, /*bytes*/
  253. data, last_command, NULL);
  254. return ret_code;
  255. }
  256. /**
  257. * i40e_calc_nvm_checksum - Calculates and returns the checksum
  258. * @hw: pointer to hardware structure
  259. * @checksum: pointer to the checksum
  260. *
  261. * This function calculates SW Checksum that covers the whole 64kB shadow RAM
  262. * except the VPD and PCIe ALT Auto-load modules. The structure and size of VPD
  263. * is customer specific and unknown. Therefore, this function skips all maximum
  264. * possible size of VPD (1kB).
  265. **/
  266. static i40e_status i40e_calc_nvm_checksum(struct i40e_hw *hw,
  267. u16 *checksum)
  268. {
  269. i40e_status ret_code = 0;
  270. u16 pcie_alt_module = 0;
  271. u16 checksum_local = 0;
  272. u16 vpd_module = 0;
  273. u16 word = 0;
  274. u32 i = 0;
  275. /* read pointer to VPD area */
  276. ret_code = i40e_read_nvm_word(hw, I40E_SR_VPD_PTR, &vpd_module);
  277. if (ret_code) {
  278. ret_code = I40E_ERR_NVM_CHECKSUM;
  279. goto i40e_calc_nvm_checksum_exit;
  280. }
  281. /* read pointer to PCIe Alt Auto-load module */
  282. ret_code = i40e_read_nvm_word(hw, I40E_SR_PCIE_ALT_AUTO_LOAD_PTR,
  283. &pcie_alt_module);
  284. if (ret_code) {
  285. ret_code = I40E_ERR_NVM_CHECKSUM;
  286. goto i40e_calc_nvm_checksum_exit;
  287. }
  288. /* Calculate SW checksum that covers the whole 64kB shadow RAM
  289. * except the VPD and PCIe ALT Auto-load modules
  290. */
  291. for (i = 0; i < hw->nvm.sr_size; i++) {
  292. /* Skip Checksum word */
  293. if (i == I40E_SR_SW_CHECKSUM_WORD)
  294. i++;
  295. /* Skip VPD module (convert byte size to word count) */
  296. if (i == (u32)vpd_module) {
  297. i += (I40E_SR_VPD_MODULE_MAX_SIZE / 2);
  298. if (i >= hw->nvm.sr_size)
  299. break;
  300. }
  301. /* Skip PCIe ALT module (convert byte size to word count) */
  302. if (i == (u32)pcie_alt_module) {
  303. i += (I40E_SR_PCIE_ALT_MODULE_MAX_SIZE / 2);
  304. if (i >= hw->nvm.sr_size)
  305. break;
  306. }
  307. ret_code = i40e_read_nvm_word(hw, (u16)i, &word);
  308. if (ret_code) {
  309. ret_code = I40E_ERR_NVM_CHECKSUM;
  310. goto i40e_calc_nvm_checksum_exit;
  311. }
  312. checksum_local += word;
  313. }
  314. *checksum = (u16)I40E_SR_SW_CHECKSUM_BASE - checksum_local;
  315. i40e_calc_nvm_checksum_exit:
  316. return ret_code;
  317. }
  318. /**
  319. * i40e_update_nvm_checksum - Updates the NVM checksum
  320. * @hw: pointer to hardware structure
  321. *
  322. * NVM ownership must be acquired before calling this function and released
  323. * on ARQ completion event reception by caller.
  324. * This function will commit SR to NVM.
  325. **/
  326. i40e_status i40e_update_nvm_checksum(struct i40e_hw *hw)
  327. {
  328. i40e_status ret_code = 0;
  329. u16 checksum;
  330. ret_code = i40e_calc_nvm_checksum(hw, &checksum);
  331. if (!ret_code)
  332. ret_code = i40e_write_nvm_aq(hw, 0x00, I40E_SR_SW_CHECKSUM_WORD,
  333. 1, &checksum, true);
  334. return ret_code;
  335. }
  336. /**
  337. * i40e_validate_nvm_checksum - Validate EEPROM checksum
  338. * @hw: pointer to hardware structure
  339. * @checksum: calculated checksum
  340. *
  341. * Performs checksum calculation and validates the NVM SW checksum. If the
  342. * caller does not need checksum, the value can be NULL.
  343. **/
  344. i40e_status i40e_validate_nvm_checksum(struct i40e_hw *hw,
  345. u16 *checksum)
  346. {
  347. i40e_status ret_code = 0;
  348. u16 checksum_sr = 0;
  349. u16 checksum_local = 0;
  350. ret_code = i40e_calc_nvm_checksum(hw, &checksum_local);
  351. if (ret_code)
  352. goto i40e_validate_nvm_checksum_exit;
  353. /* Do not use i40e_read_nvm_word() because we do not want to take
  354. * the synchronization semaphores twice here.
  355. */
  356. i40e_read_nvm_word(hw, I40E_SR_SW_CHECKSUM_WORD, &checksum_sr);
  357. /* Verify read checksum from EEPROM is the same as
  358. * calculated checksum
  359. */
  360. if (checksum_local != checksum_sr)
  361. ret_code = I40E_ERR_NVM_CHECKSUM;
  362. /* If the user cares, return the calculated checksum */
  363. if (checksum)
  364. *checksum = checksum_local;
  365. i40e_validate_nvm_checksum_exit:
  366. return ret_code;
  367. }
  368. static i40e_status i40e_nvmupd_state_init(struct i40e_hw *hw,
  369. struct i40e_nvm_access *cmd,
  370. u8 *bytes, int *errno);
  371. static i40e_status i40e_nvmupd_state_reading(struct i40e_hw *hw,
  372. struct i40e_nvm_access *cmd,
  373. u8 *bytes, int *errno);
  374. static i40e_status i40e_nvmupd_state_writing(struct i40e_hw *hw,
  375. struct i40e_nvm_access *cmd,
  376. u8 *bytes, int *errno);
  377. static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
  378. struct i40e_nvm_access *cmd,
  379. int *errno);
  380. static i40e_status i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
  381. struct i40e_nvm_access *cmd,
  382. int *errno);
  383. static i40e_status i40e_nvmupd_nvm_write(struct i40e_hw *hw,
  384. struct i40e_nvm_access *cmd,
  385. u8 *bytes, int *errno);
  386. static i40e_status i40e_nvmupd_nvm_read(struct i40e_hw *hw,
  387. struct i40e_nvm_access *cmd,
  388. u8 *bytes, int *errno);
  389. static inline u8 i40e_nvmupd_get_module(u32 val)
  390. {
  391. return (u8)(val & I40E_NVM_MOD_PNT_MASK);
  392. }
  393. static inline u8 i40e_nvmupd_get_transaction(u32 val)
  394. {
  395. return (u8)((val & I40E_NVM_TRANS_MASK) >> I40E_NVM_TRANS_SHIFT);
  396. }
  397. /**
  398. * i40e_nvmupd_command - Process an NVM update command
  399. * @hw: pointer to hardware structure
  400. * @cmd: pointer to nvm update command
  401. * @bytes: pointer to the data buffer
  402. * @errno: pointer to return error code
  403. *
  404. * Dispatches command depending on what update state is current
  405. **/
  406. i40e_status i40e_nvmupd_command(struct i40e_hw *hw,
  407. struct i40e_nvm_access *cmd,
  408. u8 *bytes, int *errno)
  409. {
  410. i40e_status status;
  411. /* assume success */
  412. *errno = 0;
  413. switch (hw->nvmupd_state) {
  414. case I40E_NVMUPD_STATE_INIT:
  415. status = i40e_nvmupd_state_init(hw, cmd, bytes, errno);
  416. break;
  417. case I40E_NVMUPD_STATE_READING:
  418. status = i40e_nvmupd_state_reading(hw, cmd, bytes, errno);
  419. break;
  420. case I40E_NVMUPD_STATE_WRITING:
  421. status = i40e_nvmupd_state_writing(hw, cmd, bytes, errno);
  422. break;
  423. default:
  424. /* invalid state, should never happen */
  425. status = I40E_NOT_SUPPORTED;
  426. *errno = -ESRCH;
  427. break;
  428. }
  429. return status;
  430. }
  431. /**
  432. * i40e_nvmupd_state_init - Handle NVM update state Init
  433. * @hw: pointer to hardware structure
  434. * @cmd: pointer to nvm update command buffer
  435. * @bytes: pointer to the data buffer
  436. * @errno: pointer to return error code
  437. *
  438. * Process legitimate commands of the Init state and conditionally set next
  439. * state. Reject all other commands.
  440. **/
  441. static i40e_status i40e_nvmupd_state_init(struct i40e_hw *hw,
  442. struct i40e_nvm_access *cmd,
  443. u8 *bytes, int *errno)
  444. {
  445. i40e_status status = 0;
  446. enum i40e_nvmupd_cmd upd_cmd;
  447. upd_cmd = i40e_nvmupd_validate_command(hw, cmd, errno);
  448. switch (upd_cmd) {
  449. case I40E_NVMUPD_READ_SA:
  450. status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
  451. if (status) {
  452. *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
  453. } else {
  454. status = i40e_nvmupd_nvm_read(hw, cmd, bytes, errno);
  455. i40e_release_nvm(hw);
  456. }
  457. break;
  458. case I40E_NVMUPD_READ_SNT:
  459. status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
  460. if (status) {
  461. *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
  462. } else {
  463. status = i40e_nvmupd_nvm_read(hw, cmd, bytes, errno);
  464. hw->nvmupd_state = I40E_NVMUPD_STATE_READING;
  465. }
  466. break;
  467. case I40E_NVMUPD_WRITE_ERA:
  468. status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
  469. if (status) {
  470. *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
  471. } else {
  472. status = i40e_nvmupd_nvm_erase(hw, cmd, errno);
  473. if (status)
  474. i40e_release_nvm(hw);
  475. else
  476. hw->aq.nvm_release_on_done = true;
  477. }
  478. break;
  479. case I40E_NVMUPD_WRITE_SA:
  480. status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
  481. if (status) {
  482. *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
  483. } else {
  484. status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
  485. if (status)
  486. i40e_release_nvm(hw);
  487. else
  488. hw->aq.nvm_release_on_done = true;
  489. }
  490. break;
  491. case I40E_NVMUPD_WRITE_SNT:
  492. status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
  493. if (status) {
  494. *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
  495. } else {
  496. status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
  497. hw->nvmupd_state = I40E_NVMUPD_STATE_WRITING;
  498. }
  499. break;
  500. case I40E_NVMUPD_CSUM_SA:
  501. status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
  502. if (status) {
  503. *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
  504. } else {
  505. status = i40e_update_nvm_checksum(hw);
  506. if (status) {
  507. *errno = hw->aq.asq_last_status ?
  508. i40e_aq_rc_to_posix(hw->aq.asq_last_status) :
  509. -EIO;
  510. i40e_release_nvm(hw);
  511. } else {
  512. hw->aq.nvm_release_on_done = true;
  513. }
  514. }
  515. break;
  516. default:
  517. status = I40E_ERR_NVM;
  518. *errno = -ESRCH;
  519. break;
  520. }
  521. return status;
  522. }
  523. /**
  524. * i40e_nvmupd_state_reading - Handle NVM update state Reading
  525. * @hw: pointer to hardware structure
  526. * @cmd: pointer to nvm update command buffer
  527. * @bytes: pointer to the data buffer
  528. * @errno: pointer to return error code
  529. *
  530. * NVM ownership is already held. Process legitimate commands and set any
  531. * change in state; reject all other commands.
  532. **/
  533. static i40e_status i40e_nvmupd_state_reading(struct i40e_hw *hw,
  534. struct i40e_nvm_access *cmd,
  535. u8 *bytes, int *errno)
  536. {
  537. i40e_status status;
  538. enum i40e_nvmupd_cmd upd_cmd;
  539. upd_cmd = i40e_nvmupd_validate_command(hw, cmd, errno);
  540. switch (upd_cmd) {
  541. case I40E_NVMUPD_READ_SA:
  542. case I40E_NVMUPD_READ_CON:
  543. status = i40e_nvmupd_nvm_read(hw, cmd, bytes, errno);
  544. break;
  545. case I40E_NVMUPD_READ_LCB:
  546. status = i40e_nvmupd_nvm_read(hw, cmd, bytes, errno);
  547. i40e_release_nvm(hw);
  548. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
  549. break;
  550. default:
  551. status = I40E_NOT_SUPPORTED;
  552. *errno = -ESRCH;
  553. break;
  554. }
  555. return status;
  556. }
  557. /**
  558. * i40e_nvmupd_state_writing - Handle NVM update state Writing
  559. * @hw: pointer to hardware structure
  560. * @cmd: pointer to nvm update command buffer
  561. * @bytes: pointer to the data buffer
  562. * @errno: pointer to return error code
  563. *
  564. * NVM ownership is already held. Process legitimate commands and set any
  565. * change in state; reject all other commands
  566. **/
  567. static i40e_status i40e_nvmupd_state_writing(struct i40e_hw *hw,
  568. struct i40e_nvm_access *cmd,
  569. u8 *bytes, int *errno)
  570. {
  571. i40e_status status;
  572. enum i40e_nvmupd_cmd upd_cmd;
  573. upd_cmd = i40e_nvmupd_validate_command(hw, cmd, errno);
  574. switch (upd_cmd) {
  575. case I40E_NVMUPD_WRITE_CON:
  576. status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
  577. break;
  578. case I40E_NVMUPD_WRITE_LCB:
  579. status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
  580. if (!status) {
  581. hw->aq.nvm_release_on_done = true;
  582. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
  583. }
  584. break;
  585. case I40E_NVMUPD_CSUM_CON:
  586. status = i40e_update_nvm_checksum(hw);
  587. if (status)
  588. *errno = hw->aq.asq_last_status ?
  589. i40e_aq_rc_to_posix(hw->aq.asq_last_status) :
  590. -EIO;
  591. break;
  592. case I40E_NVMUPD_CSUM_LCB:
  593. status = i40e_update_nvm_checksum(hw);
  594. if (status) {
  595. *errno = hw->aq.asq_last_status ?
  596. i40e_aq_rc_to_posix(hw->aq.asq_last_status) :
  597. -EIO;
  598. } else {
  599. hw->aq.nvm_release_on_done = true;
  600. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
  601. }
  602. break;
  603. default:
  604. status = I40E_NOT_SUPPORTED;
  605. *errno = -ESRCH;
  606. break;
  607. }
  608. return status;
  609. }
  610. /**
  611. * i40e_nvmupd_validate_command - Validate given command
  612. * @hw: pointer to hardware structure
  613. * @cmd: pointer to nvm update command buffer
  614. * @errno: pointer to return error code
  615. *
  616. * Return one of the valid command types or I40E_NVMUPD_INVALID
  617. **/
  618. static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
  619. struct i40e_nvm_access *cmd,
  620. int *errno)
  621. {
  622. enum i40e_nvmupd_cmd upd_cmd;
  623. u8 transaction, module;
  624. /* anything that doesn't match a recognized case is an error */
  625. upd_cmd = I40E_NVMUPD_INVALID;
  626. transaction = i40e_nvmupd_get_transaction(cmd->config);
  627. module = i40e_nvmupd_get_module(cmd->config);
  628. /* limits on data size */
  629. if ((cmd->data_size < 1) ||
  630. (cmd->data_size > I40E_NVMUPD_MAX_DATA)) {
  631. hw_dbg(hw, "i40e_nvmupd_validate_command data_size %d\n",
  632. cmd->data_size);
  633. *errno = -EFAULT;
  634. return I40E_NVMUPD_INVALID;
  635. }
  636. switch (cmd->command) {
  637. case I40E_NVM_READ:
  638. switch (transaction) {
  639. case I40E_NVM_CON:
  640. upd_cmd = I40E_NVMUPD_READ_CON;
  641. break;
  642. case I40E_NVM_SNT:
  643. upd_cmd = I40E_NVMUPD_READ_SNT;
  644. break;
  645. case I40E_NVM_LCB:
  646. upd_cmd = I40E_NVMUPD_READ_LCB;
  647. break;
  648. case I40E_NVM_SA:
  649. upd_cmd = I40E_NVMUPD_READ_SA;
  650. break;
  651. }
  652. break;
  653. case I40E_NVM_WRITE:
  654. switch (transaction) {
  655. case I40E_NVM_CON:
  656. upd_cmd = I40E_NVMUPD_WRITE_CON;
  657. break;
  658. case I40E_NVM_SNT:
  659. upd_cmd = I40E_NVMUPD_WRITE_SNT;
  660. break;
  661. case I40E_NVM_LCB:
  662. upd_cmd = I40E_NVMUPD_WRITE_LCB;
  663. break;
  664. case I40E_NVM_SA:
  665. upd_cmd = I40E_NVMUPD_WRITE_SA;
  666. break;
  667. case I40E_NVM_ERA:
  668. upd_cmd = I40E_NVMUPD_WRITE_ERA;
  669. break;
  670. case I40E_NVM_CSUM:
  671. upd_cmd = I40E_NVMUPD_CSUM_CON;
  672. break;
  673. case (I40E_NVM_CSUM|I40E_NVM_SA):
  674. upd_cmd = I40E_NVMUPD_CSUM_SA;
  675. break;
  676. case (I40E_NVM_CSUM|I40E_NVM_LCB):
  677. upd_cmd = I40E_NVMUPD_CSUM_LCB;
  678. break;
  679. }
  680. break;
  681. }
  682. if (upd_cmd == I40E_NVMUPD_INVALID) {
  683. *errno = -EFAULT;
  684. hw_dbg(hw,
  685. "i40e_nvmupd_validate_command returns %d errno: %d\n",
  686. upd_cmd, *errno);
  687. }
  688. return upd_cmd;
  689. }
  690. /**
  691. * i40e_nvmupd_nvm_read - Read NVM
  692. * @hw: pointer to hardware structure
  693. * @cmd: pointer to nvm update command buffer
  694. * @bytes: pointer to the data buffer
  695. * @errno: pointer to return error code
  696. *
  697. * cmd structure contains identifiers and data buffer
  698. **/
  699. static i40e_status i40e_nvmupd_nvm_read(struct i40e_hw *hw,
  700. struct i40e_nvm_access *cmd,
  701. u8 *bytes, int *errno)
  702. {
  703. i40e_status status;
  704. u8 module, transaction;
  705. bool last;
  706. transaction = i40e_nvmupd_get_transaction(cmd->config);
  707. module = i40e_nvmupd_get_module(cmd->config);
  708. last = (transaction == I40E_NVM_LCB) || (transaction == I40E_NVM_SA);
  709. hw_dbg(hw, "i40e_nvmupd_nvm_read mod 0x%x off 0x%x len 0x%x\n",
  710. module, cmd->offset, cmd->data_size);
  711. status = i40e_aq_read_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
  712. bytes, last, NULL);
  713. hw_dbg(hw, "i40e_nvmupd_nvm_read status %d\n", status);
  714. if (status)
  715. *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
  716. return status;
  717. }
  718. /**
  719. * i40e_nvmupd_nvm_erase - Erase an NVM module
  720. * @hw: pointer to hardware structure
  721. * @cmd: pointer to nvm update command buffer
  722. * @errno: pointer to return error code
  723. *
  724. * module, offset, data_size and data are in cmd structure
  725. **/
  726. static i40e_status i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
  727. struct i40e_nvm_access *cmd,
  728. int *errno)
  729. {
  730. i40e_status status = 0;
  731. u8 module, transaction;
  732. bool last;
  733. transaction = i40e_nvmupd_get_transaction(cmd->config);
  734. module = i40e_nvmupd_get_module(cmd->config);
  735. last = (transaction & I40E_NVM_LCB);
  736. hw_dbg(hw, "i40e_nvmupd_nvm_erase mod 0x%x off 0x%x len 0x%x\n",
  737. module, cmd->offset, cmd->data_size);
  738. status = i40e_aq_erase_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
  739. last, NULL);
  740. hw_dbg(hw, "i40e_nvmupd_nvm_erase status %d\n", status);
  741. if (status)
  742. *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
  743. return status;
  744. }
  745. /**
  746. * i40e_nvmupd_nvm_write - Write NVM
  747. * @hw: pointer to hardware structure
  748. * @cmd: pointer to nvm update command buffer
  749. * @bytes: pointer to the data buffer
  750. * @errno: pointer to return error code
  751. *
  752. * module, offset, data_size and data are in cmd structure
  753. **/
  754. static i40e_status i40e_nvmupd_nvm_write(struct i40e_hw *hw,
  755. struct i40e_nvm_access *cmd,
  756. u8 *bytes, int *errno)
  757. {
  758. i40e_status status = 0;
  759. u8 module, transaction;
  760. bool last;
  761. transaction = i40e_nvmupd_get_transaction(cmd->config);
  762. module = i40e_nvmupd_get_module(cmd->config);
  763. last = (transaction & I40E_NVM_LCB);
  764. hw_dbg(hw, "i40e_nvmupd_nvm_write mod 0x%x off 0x%x len 0x%x\n",
  765. module, cmd->offset, cmd->data_size);
  766. status = i40e_aq_update_nvm(hw, module, cmd->offset,
  767. (u16)cmd->data_size, bytes, last, NULL);
  768. hw_dbg(hw, "i40e_nvmupd_nvm_write status %d\n", status);
  769. if (status)
  770. *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
  771. return status;
  772. }