i40e_nvm.c 28 KB

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