i40e_nvm.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  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 = BIT(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. BIT(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_aq - Read Shadow RAM.
  195. * @hw: pointer to the HW structure.
  196. * @module_pointer: module pointer location in words from the NVM beginning
  197. * @offset: offset in words from module start
  198. * @words: number of words to write
  199. * @data: buffer with words to write to the Shadow RAM
  200. * @last_command: tells the AdminQ that this is the last command
  201. *
  202. * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
  203. **/
  204. static i40e_status i40e_read_nvm_aq(struct i40e_hw *hw, u8 module_pointer,
  205. u32 offset, u16 words, void *data,
  206. bool last_command)
  207. {
  208. i40e_status ret_code = I40E_ERR_NVM;
  209. struct i40e_asq_cmd_details cmd_details;
  210. memset(&cmd_details, 0, sizeof(cmd_details));
  211. /* Here we are checking the SR limit only for the flat memory model.
  212. * We cannot do it for the module-based model, as we did not acquire
  213. * the NVM resource yet (we cannot get the module pointer value).
  214. * Firmware will check the module-based model.
  215. */
  216. if ((offset + words) > hw->nvm.sr_size)
  217. i40e_debug(hw, I40E_DEBUG_NVM,
  218. "NVM write error: offset %d beyond Shadow RAM limit %d\n",
  219. (offset + words), hw->nvm.sr_size);
  220. else if (words > I40E_SR_SECTOR_SIZE_IN_WORDS)
  221. /* We can write only up to 4KB (one sector), in one AQ write */
  222. i40e_debug(hw, I40E_DEBUG_NVM,
  223. "NVM write fail error: tried to write %d words, limit is %d.\n",
  224. words, I40E_SR_SECTOR_SIZE_IN_WORDS);
  225. else if (((offset + (words - 1)) / I40E_SR_SECTOR_SIZE_IN_WORDS)
  226. != (offset / I40E_SR_SECTOR_SIZE_IN_WORDS))
  227. /* A single write cannot spread over two sectors */
  228. i40e_debug(hw, I40E_DEBUG_NVM,
  229. "NVM write error: cannot spread over two sectors in a single write offset=%d words=%d\n",
  230. offset, words);
  231. else
  232. ret_code = i40e_aq_read_nvm(hw, module_pointer,
  233. 2 * offset, /*bytes*/
  234. 2 * words, /*bytes*/
  235. data, last_command, &cmd_details);
  236. return ret_code;
  237. }
  238. /**
  239. * i40e_read_nvm_word_aq - Reads Shadow RAM via AQ
  240. * @hw: pointer to the HW structure
  241. * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
  242. * @data: word read from the Shadow RAM
  243. *
  244. * Reads one 16 bit word from the Shadow RAM using the GLNVM_SRCTL register.
  245. **/
  246. static i40e_status i40e_read_nvm_word_aq(struct i40e_hw *hw, u16 offset,
  247. u16 *data)
  248. {
  249. i40e_status ret_code = I40E_ERR_TIMEOUT;
  250. ret_code = i40e_read_nvm_aq(hw, 0x0, offset, 1, data, true);
  251. *data = le16_to_cpu(*(__le16 *)data);
  252. return ret_code;
  253. }
  254. /**
  255. * i40e_read_nvm_word - Reads Shadow RAM
  256. * @hw: pointer to the HW structure
  257. * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
  258. * @data: word read from the Shadow RAM
  259. *
  260. * Reads one 16 bit word from the Shadow RAM using the GLNVM_SRCTL register.
  261. **/
  262. i40e_status i40e_read_nvm_word(struct i40e_hw *hw, u16 offset,
  263. u16 *data)
  264. {
  265. enum i40e_status_code ret_code = 0;
  266. ret_code = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
  267. if (!ret_code) {
  268. if (hw->flags & I40E_HW_FLAG_AQ_SRCTL_ACCESS_ENABLE) {
  269. ret_code = i40e_read_nvm_word_aq(hw, offset, data);
  270. } else {
  271. ret_code = i40e_read_nvm_word_srctl(hw, offset, data);
  272. }
  273. i40e_release_nvm(hw);
  274. }
  275. return ret_code;
  276. }
  277. /**
  278. * i40e_read_nvm_buffer_srctl - Reads Shadow RAM buffer via SRCTL register
  279. * @hw: pointer to the HW structure
  280. * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
  281. * @words: (in) number of words to read; (out) number of words actually read
  282. * @data: words read from the Shadow RAM
  283. *
  284. * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_srrd()
  285. * method. The buffer read is preceded by the NVM ownership take
  286. * and followed by the release.
  287. **/
  288. static i40e_status i40e_read_nvm_buffer_srctl(struct i40e_hw *hw, u16 offset,
  289. u16 *words, u16 *data)
  290. {
  291. i40e_status ret_code = 0;
  292. u16 index, word;
  293. /* Loop thru the selected region */
  294. for (word = 0; word < *words; word++) {
  295. index = offset + word;
  296. ret_code = i40e_read_nvm_word_srctl(hw, index, &data[word]);
  297. if (ret_code)
  298. break;
  299. }
  300. /* Update the number of words read from the Shadow RAM */
  301. *words = word;
  302. return ret_code;
  303. }
  304. /**
  305. * i40e_read_nvm_buffer_aq - Reads Shadow RAM buffer via AQ
  306. * @hw: pointer to the HW structure
  307. * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
  308. * @words: (in) number of words to read; (out) number of words actually read
  309. * @data: words read from the Shadow RAM
  310. *
  311. * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_aq()
  312. * method. The buffer read is preceded by the NVM ownership take
  313. * and followed by the release.
  314. **/
  315. static i40e_status i40e_read_nvm_buffer_aq(struct i40e_hw *hw, u16 offset,
  316. u16 *words, u16 *data)
  317. {
  318. i40e_status ret_code;
  319. u16 read_size = *words;
  320. bool last_cmd = false;
  321. u16 words_read = 0;
  322. u16 i = 0;
  323. do {
  324. /* Calculate number of bytes we should read in this step.
  325. * FVL AQ do not allow to read more than one page at a time or
  326. * to cross page boundaries.
  327. */
  328. if (offset % I40E_SR_SECTOR_SIZE_IN_WORDS)
  329. read_size = min(*words,
  330. (u16)(I40E_SR_SECTOR_SIZE_IN_WORDS -
  331. (offset % I40E_SR_SECTOR_SIZE_IN_WORDS)));
  332. else
  333. read_size = min((*words - words_read),
  334. I40E_SR_SECTOR_SIZE_IN_WORDS);
  335. /* Check if this is last command, if so set proper flag */
  336. if ((words_read + read_size) >= *words)
  337. last_cmd = true;
  338. ret_code = i40e_read_nvm_aq(hw, 0x0, offset, read_size,
  339. data + words_read, last_cmd);
  340. if (ret_code)
  341. goto read_nvm_buffer_aq_exit;
  342. /* Increment counter for words already read and move offset to
  343. * new read location
  344. */
  345. words_read += read_size;
  346. offset += read_size;
  347. } while (words_read < *words);
  348. for (i = 0; i < *words; i++)
  349. data[i] = le16_to_cpu(((__le16 *)data)[i]);
  350. read_nvm_buffer_aq_exit:
  351. *words = words_read;
  352. return ret_code;
  353. }
  354. /**
  355. * i40e_read_nvm_buffer - Reads Shadow RAM buffer
  356. * @hw: pointer to the HW structure
  357. * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
  358. * @words: (in) number of words to read; (out) number of words actually read
  359. * @data: words read from the Shadow RAM
  360. *
  361. * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_srrd()
  362. * method. The buffer read is preceded by the NVM ownership take
  363. * and followed by the release.
  364. **/
  365. i40e_status i40e_read_nvm_buffer(struct i40e_hw *hw, u16 offset,
  366. u16 *words, u16 *data)
  367. {
  368. enum i40e_status_code ret_code = 0;
  369. if (hw->flags & I40E_HW_FLAG_AQ_SRCTL_ACCESS_ENABLE) {
  370. ret_code = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
  371. if (!ret_code) {
  372. ret_code = i40e_read_nvm_buffer_aq(hw, offset, words,
  373. data);
  374. i40e_release_nvm(hw);
  375. }
  376. } else {
  377. ret_code = i40e_read_nvm_buffer_srctl(hw, offset, words, data);
  378. }
  379. return ret_code;
  380. }
  381. /**
  382. * i40e_write_nvm_aq - Writes Shadow RAM.
  383. * @hw: pointer to the HW structure.
  384. * @module_pointer: module pointer location in words from the NVM beginning
  385. * @offset: offset in words from module start
  386. * @words: number of words to write
  387. * @data: buffer with words to write to the Shadow RAM
  388. * @last_command: tells the AdminQ that this is the last command
  389. *
  390. * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
  391. **/
  392. static i40e_status i40e_write_nvm_aq(struct i40e_hw *hw, u8 module_pointer,
  393. u32 offset, u16 words, void *data,
  394. bool last_command)
  395. {
  396. i40e_status ret_code = I40E_ERR_NVM;
  397. struct i40e_asq_cmd_details cmd_details;
  398. memset(&cmd_details, 0, sizeof(cmd_details));
  399. cmd_details.wb_desc = &hw->nvm_wb_desc;
  400. /* Here we are checking the SR limit only for the flat memory model.
  401. * We cannot do it for the module-based model, as we did not acquire
  402. * the NVM resource yet (we cannot get the module pointer value).
  403. * Firmware will check the module-based model.
  404. */
  405. if ((offset + words) > hw->nvm.sr_size)
  406. i40e_debug(hw, I40E_DEBUG_NVM,
  407. "NVM write error: offset %d beyond Shadow RAM limit %d\n",
  408. (offset + words), hw->nvm.sr_size);
  409. else if (words > I40E_SR_SECTOR_SIZE_IN_WORDS)
  410. /* We can write only up to 4KB (one sector), in one AQ write */
  411. i40e_debug(hw, I40E_DEBUG_NVM,
  412. "NVM write fail error: tried to write %d words, limit is %d.\n",
  413. words, I40E_SR_SECTOR_SIZE_IN_WORDS);
  414. else if (((offset + (words - 1)) / I40E_SR_SECTOR_SIZE_IN_WORDS)
  415. != (offset / I40E_SR_SECTOR_SIZE_IN_WORDS))
  416. /* A single write cannot spread over two sectors */
  417. i40e_debug(hw, I40E_DEBUG_NVM,
  418. "NVM write error: cannot spread over two sectors in a single write offset=%d words=%d\n",
  419. offset, words);
  420. else
  421. ret_code = i40e_aq_update_nvm(hw, module_pointer,
  422. 2 * offset, /*bytes*/
  423. 2 * words, /*bytes*/
  424. data, last_command, &cmd_details);
  425. return ret_code;
  426. }
  427. /**
  428. * i40e_calc_nvm_checksum - Calculates and returns the checksum
  429. * @hw: pointer to hardware structure
  430. * @checksum: pointer to the checksum
  431. *
  432. * This function calculates SW Checksum that covers the whole 64kB shadow RAM
  433. * except the VPD and PCIe ALT Auto-load modules. The structure and size of VPD
  434. * is customer specific and unknown. Therefore, this function skips all maximum
  435. * possible size of VPD (1kB).
  436. **/
  437. static i40e_status i40e_calc_nvm_checksum(struct i40e_hw *hw,
  438. u16 *checksum)
  439. {
  440. i40e_status ret_code;
  441. struct i40e_virt_mem vmem;
  442. u16 pcie_alt_module = 0;
  443. u16 checksum_local = 0;
  444. u16 vpd_module = 0;
  445. u16 *data;
  446. u16 i = 0;
  447. ret_code = i40e_allocate_virt_mem(hw, &vmem,
  448. I40E_SR_SECTOR_SIZE_IN_WORDS * sizeof(u16));
  449. if (ret_code)
  450. goto i40e_calc_nvm_checksum_exit;
  451. data = (u16 *)vmem.va;
  452. /* read pointer to VPD area */
  453. ret_code = i40e_read_nvm_word(hw, I40E_SR_VPD_PTR, &vpd_module);
  454. if (ret_code) {
  455. ret_code = I40E_ERR_NVM_CHECKSUM;
  456. goto i40e_calc_nvm_checksum_exit;
  457. }
  458. /* read pointer to PCIe Alt Auto-load module */
  459. ret_code = i40e_read_nvm_word(hw, I40E_SR_PCIE_ALT_AUTO_LOAD_PTR,
  460. &pcie_alt_module);
  461. if (ret_code) {
  462. ret_code = I40E_ERR_NVM_CHECKSUM;
  463. goto i40e_calc_nvm_checksum_exit;
  464. }
  465. /* Calculate SW checksum that covers the whole 64kB shadow RAM
  466. * except the VPD and PCIe ALT Auto-load modules
  467. */
  468. for (i = 0; i < hw->nvm.sr_size; i++) {
  469. /* Read SR page */
  470. if ((i % I40E_SR_SECTOR_SIZE_IN_WORDS) == 0) {
  471. u16 words = I40E_SR_SECTOR_SIZE_IN_WORDS;
  472. ret_code = i40e_read_nvm_buffer(hw, i, &words, data);
  473. if (ret_code) {
  474. ret_code = I40E_ERR_NVM_CHECKSUM;
  475. goto i40e_calc_nvm_checksum_exit;
  476. }
  477. }
  478. /* Skip Checksum word */
  479. if (i == I40E_SR_SW_CHECKSUM_WORD)
  480. continue;
  481. /* Skip VPD module (convert byte size to word count) */
  482. if ((i >= (u32)vpd_module) &&
  483. (i < ((u32)vpd_module +
  484. (I40E_SR_VPD_MODULE_MAX_SIZE / 2)))) {
  485. continue;
  486. }
  487. /* Skip PCIe ALT module (convert byte size to word count) */
  488. if ((i >= (u32)pcie_alt_module) &&
  489. (i < ((u32)pcie_alt_module +
  490. (I40E_SR_PCIE_ALT_MODULE_MAX_SIZE / 2)))) {
  491. continue;
  492. }
  493. checksum_local += data[i % I40E_SR_SECTOR_SIZE_IN_WORDS];
  494. }
  495. *checksum = (u16)I40E_SR_SW_CHECKSUM_BASE - checksum_local;
  496. i40e_calc_nvm_checksum_exit:
  497. i40e_free_virt_mem(hw, &vmem);
  498. return ret_code;
  499. }
  500. /**
  501. * i40e_update_nvm_checksum - Updates the NVM checksum
  502. * @hw: pointer to hardware structure
  503. *
  504. * NVM ownership must be acquired before calling this function and released
  505. * on ARQ completion event reception by caller.
  506. * This function will commit SR to NVM.
  507. **/
  508. i40e_status i40e_update_nvm_checksum(struct i40e_hw *hw)
  509. {
  510. i40e_status ret_code;
  511. u16 checksum;
  512. __le16 le_sum;
  513. ret_code = i40e_calc_nvm_checksum(hw, &checksum);
  514. if (!ret_code) {
  515. le_sum = cpu_to_le16(checksum);
  516. ret_code = i40e_write_nvm_aq(hw, 0x00, I40E_SR_SW_CHECKSUM_WORD,
  517. 1, &le_sum, true);
  518. }
  519. return ret_code;
  520. }
  521. /**
  522. * i40e_validate_nvm_checksum - Validate EEPROM checksum
  523. * @hw: pointer to hardware structure
  524. * @checksum: calculated checksum
  525. *
  526. * Performs checksum calculation and validates the NVM SW checksum. If the
  527. * caller does not need checksum, the value can be NULL.
  528. **/
  529. i40e_status i40e_validate_nvm_checksum(struct i40e_hw *hw,
  530. u16 *checksum)
  531. {
  532. i40e_status ret_code = 0;
  533. u16 checksum_sr = 0;
  534. u16 checksum_local = 0;
  535. ret_code = i40e_calc_nvm_checksum(hw, &checksum_local);
  536. if (ret_code)
  537. goto i40e_validate_nvm_checksum_exit;
  538. /* Do not use i40e_read_nvm_word() because we do not want to take
  539. * the synchronization semaphores twice here.
  540. */
  541. i40e_read_nvm_word(hw, I40E_SR_SW_CHECKSUM_WORD, &checksum_sr);
  542. /* Verify read checksum from EEPROM is the same as
  543. * calculated checksum
  544. */
  545. if (checksum_local != checksum_sr)
  546. ret_code = I40E_ERR_NVM_CHECKSUM;
  547. /* If the user cares, return the calculated checksum */
  548. if (checksum)
  549. *checksum = checksum_local;
  550. i40e_validate_nvm_checksum_exit:
  551. return ret_code;
  552. }
  553. static i40e_status i40e_nvmupd_state_init(struct i40e_hw *hw,
  554. struct i40e_nvm_access *cmd,
  555. u8 *bytes, int *perrno);
  556. static i40e_status i40e_nvmupd_state_reading(struct i40e_hw *hw,
  557. struct i40e_nvm_access *cmd,
  558. u8 *bytes, int *perrno);
  559. static i40e_status i40e_nvmupd_state_writing(struct i40e_hw *hw,
  560. struct i40e_nvm_access *cmd,
  561. u8 *bytes, int *errno);
  562. static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
  563. struct i40e_nvm_access *cmd,
  564. int *perrno);
  565. static i40e_status i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
  566. struct i40e_nvm_access *cmd,
  567. int *perrno);
  568. static i40e_status i40e_nvmupd_nvm_write(struct i40e_hw *hw,
  569. struct i40e_nvm_access *cmd,
  570. u8 *bytes, int *perrno);
  571. static i40e_status i40e_nvmupd_nvm_read(struct i40e_hw *hw,
  572. struct i40e_nvm_access *cmd,
  573. u8 *bytes, int *perrno);
  574. static i40e_status i40e_nvmupd_exec_aq(struct i40e_hw *hw,
  575. struct i40e_nvm_access *cmd,
  576. u8 *bytes, int *perrno);
  577. static i40e_status i40e_nvmupd_get_aq_result(struct i40e_hw *hw,
  578. struct i40e_nvm_access *cmd,
  579. u8 *bytes, int *perrno);
  580. static inline u8 i40e_nvmupd_get_module(u32 val)
  581. {
  582. return (u8)(val & I40E_NVM_MOD_PNT_MASK);
  583. }
  584. static inline u8 i40e_nvmupd_get_transaction(u32 val)
  585. {
  586. return (u8)((val & I40E_NVM_TRANS_MASK) >> I40E_NVM_TRANS_SHIFT);
  587. }
  588. static const char * const i40e_nvm_update_state_str[] = {
  589. "I40E_NVMUPD_INVALID",
  590. "I40E_NVMUPD_READ_CON",
  591. "I40E_NVMUPD_READ_SNT",
  592. "I40E_NVMUPD_READ_LCB",
  593. "I40E_NVMUPD_READ_SA",
  594. "I40E_NVMUPD_WRITE_ERA",
  595. "I40E_NVMUPD_WRITE_CON",
  596. "I40E_NVMUPD_WRITE_SNT",
  597. "I40E_NVMUPD_WRITE_LCB",
  598. "I40E_NVMUPD_WRITE_SA",
  599. "I40E_NVMUPD_CSUM_CON",
  600. "I40E_NVMUPD_CSUM_SA",
  601. "I40E_NVMUPD_CSUM_LCB",
  602. "I40E_NVMUPD_STATUS",
  603. "I40E_NVMUPD_EXEC_AQ",
  604. "I40E_NVMUPD_GET_AQ_RESULT",
  605. };
  606. /**
  607. * i40e_nvmupd_command - Process an NVM update command
  608. * @hw: pointer to hardware structure
  609. * @cmd: pointer to nvm update command
  610. * @bytes: pointer to the data buffer
  611. * @perrno: pointer to return error code
  612. *
  613. * Dispatches command depending on what update state is current
  614. **/
  615. i40e_status i40e_nvmupd_command(struct i40e_hw *hw,
  616. struct i40e_nvm_access *cmd,
  617. u8 *bytes, int *perrno)
  618. {
  619. i40e_status status;
  620. enum i40e_nvmupd_cmd upd_cmd;
  621. /* assume success */
  622. *perrno = 0;
  623. /* early check for status command and debug msgs */
  624. upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
  625. i40e_debug(hw, I40E_DEBUG_NVM, "%s state %d nvm_release_on_hold %d\n",
  626. i40e_nvm_update_state_str[upd_cmd],
  627. hw->nvmupd_state,
  628. hw->aq.nvm_release_on_done);
  629. if (upd_cmd == I40E_NVMUPD_INVALID) {
  630. *perrno = -EFAULT;
  631. i40e_debug(hw, I40E_DEBUG_NVM,
  632. "i40e_nvmupd_validate_command returns %d errno %d\n",
  633. upd_cmd, *perrno);
  634. }
  635. /* a status request returns immediately rather than
  636. * going into the state machine
  637. */
  638. if (upd_cmd == I40E_NVMUPD_STATUS) {
  639. bytes[0] = hw->nvmupd_state;
  640. return 0;
  641. }
  642. switch (hw->nvmupd_state) {
  643. case I40E_NVMUPD_STATE_INIT:
  644. status = i40e_nvmupd_state_init(hw, cmd, bytes, perrno);
  645. break;
  646. case I40E_NVMUPD_STATE_READING:
  647. status = i40e_nvmupd_state_reading(hw, cmd, bytes, perrno);
  648. break;
  649. case I40E_NVMUPD_STATE_WRITING:
  650. status = i40e_nvmupd_state_writing(hw, cmd, bytes, perrno);
  651. break;
  652. case I40E_NVMUPD_STATE_INIT_WAIT:
  653. case I40E_NVMUPD_STATE_WRITE_WAIT:
  654. status = I40E_ERR_NOT_READY;
  655. *perrno = -EBUSY;
  656. break;
  657. default:
  658. /* invalid state, should never happen */
  659. i40e_debug(hw, I40E_DEBUG_NVM,
  660. "NVMUPD: no such state %d\n", hw->nvmupd_state);
  661. status = I40E_NOT_SUPPORTED;
  662. *perrno = -ESRCH;
  663. break;
  664. }
  665. return status;
  666. }
  667. /**
  668. * i40e_nvmupd_state_init - Handle NVM update state Init
  669. * @hw: pointer to hardware structure
  670. * @cmd: pointer to nvm update command buffer
  671. * @bytes: pointer to the data buffer
  672. * @perrno: pointer to return error code
  673. *
  674. * Process legitimate commands of the Init state and conditionally set next
  675. * state. Reject all other commands.
  676. **/
  677. static i40e_status i40e_nvmupd_state_init(struct i40e_hw *hw,
  678. struct i40e_nvm_access *cmd,
  679. u8 *bytes, int *perrno)
  680. {
  681. i40e_status status = 0;
  682. enum i40e_nvmupd_cmd upd_cmd;
  683. upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
  684. switch (upd_cmd) {
  685. case I40E_NVMUPD_READ_SA:
  686. status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
  687. if (status) {
  688. *perrno = i40e_aq_rc_to_posix(status,
  689. hw->aq.asq_last_status);
  690. } else {
  691. status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
  692. i40e_release_nvm(hw);
  693. }
  694. break;
  695. case I40E_NVMUPD_READ_SNT:
  696. status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
  697. if (status) {
  698. *perrno = i40e_aq_rc_to_posix(status,
  699. hw->aq.asq_last_status);
  700. } else {
  701. status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
  702. if (status)
  703. i40e_release_nvm(hw);
  704. else
  705. hw->nvmupd_state = I40E_NVMUPD_STATE_READING;
  706. }
  707. break;
  708. case I40E_NVMUPD_WRITE_ERA:
  709. status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
  710. if (status) {
  711. *perrno = i40e_aq_rc_to_posix(status,
  712. hw->aq.asq_last_status);
  713. } else {
  714. status = i40e_nvmupd_nvm_erase(hw, cmd, perrno);
  715. if (status) {
  716. i40e_release_nvm(hw);
  717. } else {
  718. hw->aq.nvm_release_on_done = true;
  719. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
  720. }
  721. }
  722. break;
  723. case I40E_NVMUPD_WRITE_SA:
  724. status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
  725. if (status) {
  726. *perrno = i40e_aq_rc_to_posix(status,
  727. hw->aq.asq_last_status);
  728. } else {
  729. status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
  730. if (status) {
  731. i40e_release_nvm(hw);
  732. } else {
  733. hw->aq.nvm_release_on_done = true;
  734. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
  735. }
  736. }
  737. break;
  738. case I40E_NVMUPD_WRITE_SNT:
  739. status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
  740. if (status) {
  741. *perrno = i40e_aq_rc_to_posix(status,
  742. hw->aq.asq_last_status);
  743. } else {
  744. status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
  745. if (status)
  746. i40e_release_nvm(hw);
  747. else
  748. hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
  749. }
  750. break;
  751. case I40E_NVMUPD_CSUM_SA:
  752. status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
  753. if (status) {
  754. *perrno = i40e_aq_rc_to_posix(status,
  755. hw->aq.asq_last_status);
  756. } else {
  757. status = i40e_update_nvm_checksum(hw);
  758. if (status) {
  759. *perrno = hw->aq.asq_last_status ?
  760. i40e_aq_rc_to_posix(status,
  761. hw->aq.asq_last_status) :
  762. -EIO;
  763. i40e_release_nvm(hw);
  764. } else {
  765. hw->aq.nvm_release_on_done = true;
  766. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
  767. }
  768. }
  769. break;
  770. case I40E_NVMUPD_EXEC_AQ:
  771. status = i40e_nvmupd_exec_aq(hw, cmd, bytes, perrno);
  772. break;
  773. case I40E_NVMUPD_GET_AQ_RESULT:
  774. status = i40e_nvmupd_get_aq_result(hw, cmd, bytes, perrno);
  775. break;
  776. default:
  777. i40e_debug(hw, I40E_DEBUG_NVM,
  778. "NVMUPD: bad cmd %s in init state\n",
  779. i40e_nvm_update_state_str[upd_cmd]);
  780. status = I40E_ERR_NVM;
  781. *perrno = -ESRCH;
  782. break;
  783. }
  784. return status;
  785. }
  786. /**
  787. * i40e_nvmupd_state_reading - Handle NVM update state Reading
  788. * @hw: pointer to hardware structure
  789. * @cmd: pointer to nvm update command buffer
  790. * @bytes: pointer to the data buffer
  791. * @perrno: pointer to return error code
  792. *
  793. * NVM ownership is already held. Process legitimate commands and set any
  794. * change in state; reject all other commands.
  795. **/
  796. static i40e_status i40e_nvmupd_state_reading(struct i40e_hw *hw,
  797. struct i40e_nvm_access *cmd,
  798. u8 *bytes, int *perrno)
  799. {
  800. i40e_status status = 0;
  801. enum i40e_nvmupd_cmd upd_cmd;
  802. upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
  803. switch (upd_cmd) {
  804. case I40E_NVMUPD_READ_SA:
  805. case I40E_NVMUPD_READ_CON:
  806. status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
  807. break;
  808. case I40E_NVMUPD_READ_LCB:
  809. status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
  810. i40e_release_nvm(hw);
  811. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
  812. break;
  813. default:
  814. i40e_debug(hw, I40E_DEBUG_NVM,
  815. "NVMUPD: bad cmd %s in reading state.\n",
  816. i40e_nvm_update_state_str[upd_cmd]);
  817. status = I40E_NOT_SUPPORTED;
  818. *perrno = -ESRCH;
  819. break;
  820. }
  821. return status;
  822. }
  823. /**
  824. * i40e_nvmupd_state_writing - Handle NVM update state Writing
  825. * @hw: pointer to hardware structure
  826. * @cmd: pointer to nvm update command buffer
  827. * @bytes: pointer to the data buffer
  828. * @perrno: pointer to return error code
  829. *
  830. * NVM ownership is already held. Process legitimate commands and set any
  831. * change in state; reject all other commands
  832. **/
  833. static i40e_status i40e_nvmupd_state_writing(struct i40e_hw *hw,
  834. struct i40e_nvm_access *cmd,
  835. u8 *bytes, int *perrno)
  836. {
  837. i40e_status status = 0;
  838. enum i40e_nvmupd_cmd upd_cmd;
  839. bool retry_attempt = false;
  840. upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
  841. retry:
  842. switch (upd_cmd) {
  843. case I40E_NVMUPD_WRITE_CON:
  844. status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
  845. if (!status)
  846. hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
  847. break;
  848. case I40E_NVMUPD_WRITE_LCB:
  849. status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
  850. if (status) {
  851. *perrno = hw->aq.asq_last_status ?
  852. i40e_aq_rc_to_posix(status,
  853. hw->aq.asq_last_status) :
  854. -EIO;
  855. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
  856. } else {
  857. hw->aq.nvm_release_on_done = true;
  858. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
  859. }
  860. break;
  861. case I40E_NVMUPD_CSUM_CON:
  862. status = i40e_update_nvm_checksum(hw);
  863. if (status) {
  864. *perrno = hw->aq.asq_last_status ?
  865. i40e_aq_rc_to_posix(status,
  866. hw->aq.asq_last_status) :
  867. -EIO;
  868. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
  869. } else {
  870. hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
  871. }
  872. break;
  873. case I40E_NVMUPD_CSUM_LCB:
  874. status = i40e_update_nvm_checksum(hw);
  875. if (status) {
  876. *perrno = hw->aq.asq_last_status ?
  877. i40e_aq_rc_to_posix(status,
  878. hw->aq.asq_last_status) :
  879. -EIO;
  880. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
  881. } else {
  882. hw->aq.nvm_release_on_done = true;
  883. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
  884. }
  885. break;
  886. default:
  887. i40e_debug(hw, I40E_DEBUG_NVM,
  888. "NVMUPD: bad cmd %s in writing state.\n",
  889. i40e_nvm_update_state_str[upd_cmd]);
  890. status = I40E_NOT_SUPPORTED;
  891. *perrno = -ESRCH;
  892. break;
  893. }
  894. /* In some circumstances, a multi-write transaction takes longer
  895. * than the default 3 minute timeout on the write semaphore. If
  896. * the write failed with an EBUSY status, this is likely the problem,
  897. * so here we try to reacquire the semaphore then retry the write.
  898. * We only do one retry, then give up.
  899. */
  900. if (status && (hw->aq.asq_last_status == I40E_AQ_RC_EBUSY) &&
  901. !retry_attempt) {
  902. i40e_status old_status = status;
  903. u32 old_asq_status = hw->aq.asq_last_status;
  904. u32 gtime;
  905. gtime = rd32(hw, I40E_GLVFGEN_TIMER);
  906. if (gtime >= hw->nvm.hw_semaphore_timeout) {
  907. i40e_debug(hw, I40E_DEBUG_ALL,
  908. "NVMUPD: write semaphore expired (%d >= %lld), retrying\n",
  909. gtime, hw->nvm.hw_semaphore_timeout);
  910. i40e_release_nvm(hw);
  911. status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
  912. if (status) {
  913. i40e_debug(hw, I40E_DEBUG_ALL,
  914. "NVMUPD: write semaphore reacquire failed aq_err = %d\n",
  915. hw->aq.asq_last_status);
  916. status = old_status;
  917. hw->aq.asq_last_status = old_asq_status;
  918. } else {
  919. retry_attempt = true;
  920. goto retry;
  921. }
  922. }
  923. }
  924. return status;
  925. }
  926. /**
  927. * i40e_nvmupd_validate_command - Validate given command
  928. * @hw: pointer to hardware structure
  929. * @cmd: pointer to nvm update command buffer
  930. * @perrno: pointer to return error code
  931. *
  932. * Return one of the valid command types or I40E_NVMUPD_INVALID
  933. **/
  934. static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
  935. struct i40e_nvm_access *cmd,
  936. int *perrno)
  937. {
  938. enum i40e_nvmupd_cmd upd_cmd;
  939. u8 module, transaction;
  940. /* anything that doesn't match a recognized case is an error */
  941. upd_cmd = I40E_NVMUPD_INVALID;
  942. transaction = i40e_nvmupd_get_transaction(cmd->config);
  943. module = i40e_nvmupd_get_module(cmd->config);
  944. /* limits on data size */
  945. if ((cmd->data_size < 1) ||
  946. (cmd->data_size > I40E_NVMUPD_MAX_DATA)) {
  947. i40e_debug(hw, I40E_DEBUG_NVM,
  948. "i40e_nvmupd_validate_command data_size %d\n",
  949. cmd->data_size);
  950. *perrno = -EFAULT;
  951. return I40E_NVMUPD_INVALID;
  952. }
  953. switch (cmd->command) {
  954. case I40E_NVM_READ:
  955. switch (transaction) {
  956. case I40E_NVM_CON:
  957. upd_cmd = I40E_NVMUPD_READ_CON;
  958. break;
  959. case I40E_NVM_SNT:
  960. upd_cmd = I40E_NVMUPD_READ_SNT;
  961. break;
  962. case I40E_NVM_LCB:
  963. upd_cmd = I40E_NVMUPD_READ_LCB;
  964. break;
  965. case I40E_NVM_SA:
  966. upd_cmd = I40E_NVMUPD_READ_SA;
  967. break;
  968. case I40E_NVM_EXEC:
  969. if (module == 0xf)
  970. upd_cmd = I40E_NVMUPD_STATUS;
  971. else if (module == 0)
  972. upd_cmd = I40E_NVMUPD_GET_AQ_RESULT;
  973. break;
  974. }
  975. break;
  976. case I40E_NVM_WRITE:
  977. switch (transaction) {
  978. case I40E_NVM_CON:
  979. upd_cmd = I40E_NVMUPD_WRITE_CON;
  980. break;
  981. case I40E_NVM_SNT:
  982. upd_cmd = I40E_NVMUPD_WRITE_SNT;
  983. break;
  984. case I40E_NVM_LCB:
  985. upd_cmd = I40E_NVMUPD_WRITE_LCB;
  986. break;
  987. case I40E_NVM_SA:
  988. upd_cmd = I40E_NVMUPD_WRITE_SA;
  989. break;
  990. case I40E_NVM_ERA:
  991. upd_cmd = I40E_NVMUPD_WRITE_ERA;
  992. break;
  993. case I40E_NVM_CSUM:
  994. upd_cmd = I40E_NVMUPD_CSUM_CON;
  995. break;
  996. case (I40E_NVM_CSUM|I40E_NVM_SA):
  997. upd_cmd = I40E_NVMUPD_CSUM_SA;
  998. break;
  999. case (I40E_NVM_CSUM|I40E_NVM_LCB):
  1000. upd_cmd = I40E_NVMUPD_CSUM_LCB;
  1001. break;
  1002. case I40E_NVM_EXEC:
  1003. if (module == 0)
  1004. upd_cmd = I40E_NVMUPD_EXEC_AQ;
  1005. break;
  1006. }
  1007. break;
  1008. }
  1009. return upd_cmd;
  1010. }
  1011. /**
  1012. * i40e_nvmupd_exec_aq - Run an AQ command
  1013. * @hw: pointer to hardware structure
  1014. * @cmd: pointer to nvm update command buffer
  1015. * @bytes: pointer to the data buffer
  1016. * @perrno: pointer to return error code
  1017. *
  1018. * cmd structure contains identifiers and data buffer
  1019. **/
  1020. static i40e_status i40e_nvmupd_exec_aq(struct i40e_hw *hw,
  1021. struct i40e_nvm_access *cmd,
  1022. u8 *bytes, int *perrno)
  1023. {
  1024. struct i40e_asq_cmd_details cmd_details;
  1025. i40e_status status;
  1026. struct i40e_aq_desc *aq_desc;
  1027. u32 buff_size = 0;
  1028. u8 *buff = NULL;
  1029. u32 aq_desc_len;
  1030. u32 aq_data_len;
  1031. i40e_debug(hw, I40E_DEBUG_NVM, "NVMUPD: %s\n", __func__);
  1032. memset(&cmd_details, 0, sizeof(cmd_details));
  1033. cmd_details.wb_desc = &hw->nvm_wb_desc;
  1034. aq_desc_len = sizeof(struct i40e_aq_desc);
  1035. memset(&hw->nvm_wb_desc, 0, aq_desc_len);
  1036. /* get the aq descriptor */
  1037. if (cmd->data_size < aq_desc_len) {
  1038. i40e_debug(hw, I40E_DEBUG_NVM,
  1039. "NVMUPD: not enough aq desc bytes for exec, size %d < %d\n",
  1040. cmd->data_size, aq_desc_len);
  1041. *perrno = -EINVAL;
  1042. return I40E_ERR_PARAM;
  1043. }
  1044. aq_desc = (struct i40e_aq_desc *)bytes;
  1045. /* if data buffer needed, make sure it's ready */
  1046. aq_data_len = cmd->data_size - aq_desc_len;
  1047. buff_size = max_t(u32, aq_data_len, le16_to_cpu(aq_desc->datalen));
  1048. if (buff_size) {
  1049. if (!hw->nvm_buff.va) {
  1050. status = i40e_allocate_virt_mem(hw, &hw->nvm_buff,
  1051. hw->aq.asq_buf_size);
  1052. if (status)
  1053. i40e_debug(hw, I40E_DEBUG_NVM,
  1054. "NVMUPD: i40e_allocate_virt_mem for exec buff failed, %d\n",
  1055. status);
  1056. }
  1057. if (hw->nvm_buff.va) {
  1058. buff = hw->nvm_buff.va;
  1059. memcpy(buff, &bytes[aq_desc_len], aq_data_len);
  1060. }
  1061. }
  1062. /* and away we go! */
  1063. status = i40e_asq_send_command(hw, aq_desc, buff,
  1064. buff_size, &cmd_details);
  1065. if (status) {
  1066. i40e_debug(hw, I40E_DEBUG_NVM,
  1067. "i40e_nvmupd_exec_aq err %s aq_err %s\n",
  1068. i40e_stat_str(hw, status),
  1069. i40e_aq_str(hw, hw->aq.asq_last_status));
  1070. *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
  1071. }
  1072. return status;
  1073. }
  1074. /**
  1075. * i40e_nvmupd_get_aq_result - Get the results from the previous exec_aq
  1076. * @hw: pointer to hardware structure
  1077. * @cmd: pointer to nvm update command buffer
  1078. * @bytes: pointer to the data buffer
  1079. * @perrno: pointer to return error code
  1080. *
  1081. * cmd structure contains identifiers and data buffer
  1082. **/
  1083. static i40e_status i40e_nvmupd_get_aq_result(struct i40e_hw *hw,
  1084. struct i40e_nvm_access *cmd,
  1085. u8 *bytes, int *perrno)
  1086. {
  1087. u32 aq_total_len;
  1088. u32 aq_desc_len;
  1089. int remainder;
  1090. u8 *buff;
  1091. i40e_debug(hw, I40E_DEBUG_NVM, "NVMUPD: %s\n", __func__);
  1092. aq_desc_len = sizeof(struct i40e_aq_desc);
  1093. aq_total_len = aq_desc_len + le16_to_cpu(hw->nvm_wb_desc.datalen);
  1094. /* check offset range */
  1095. if (cmd->offset > aq_total_len) {
  1096. i40e_debug(hw, I40E_DEBUG_NVM, "%s: offset too big %d > %d\n",
  1097. __func__, cmd->offset, aq_total_len);
  1098. *perrno = -EINVAL;
  1099. return I40E_ERR_PARAM;
  1100. }
  1101. /* check copylength range */
  1102. if (cmd->data_size > (aq_total_len - cmd->offset)) {
  1103. int new_len = aq_total_len - cmd->offset;
  1104. i40e_debug(hw, I40E_DEBUG_NVM, "%s: copy length %d too big, trimming to %d\n",
  1105. __func__, cmd->data_size, new_len);
  1106. cmd->data_size = new_len;
  1107. }
  1108. remainder = cmd->data_size;
  1109. if (cmd->offset < aq_desc_len) {
  1110. u32 len = aq_desc_len - cmd->offset;
  1111. len = min(len, cmd->data_size);
  1112. i40e_debug(hw, I40E_DEBUG_NVM, "%s: aq_desc bytes %d to %d\n",
  1113. __func__, cmd->offset, cmd->offset + len);
  1114. buff = ((u8 *)&hw->nvm_wb_desc) + cmd->offset;
  1115. memcpy(bytes, buff, len);
  1116. bytes += len;
  1117. remainder -= len;
  1118. buff = hw->nvm_buff.va;
  1119. } else {
  1120. buff = hw->nvm_buff.va + (cmd->offset - aq_desc_len);
  1121. }
  1122. if (remainder > 0) {
  1123. int start_byte = buff - (u8 *)hw->nvm_buff.va;
  1124. i40e_debug(hw, I40E_DEBUG_NVM, "%s: databuf bytes %d to %d\n",
  1125. __func__, start_byte, start_byte + remainder);
  1126. memcpy(bytes, buff, remainder);
  1127. }
  1128. return 0;
  1129. }
  1130. /**
  1131. * i40e_nvmupd_nvm_read - Read NVM
  1132. * @hw: pointer to hardware structure
  1133. * @cmd: pointer to nvm update command buffer
  1134. * @bytes: pointer to the data buffer
  1135. * @perrno: pointer to return error code
  1136. *
  1137. * cmd structure contains identifiers and data buffer
  1138. **/
  1139. static i40e_status i40e_nvmupd_nvm_read(struct i40e_hw *hw,
  1140. struct i40e_nvm_access *cmd,
  1141. u8 *bytes, int *perrno)
  1142. {
  1143. struct i40e_asq_cmd_details cmd_details;
  1144. i40e_status status;
  1145. u8 module, transaction;
  1146. bool last;
  1147. transaction = i40e_nvmupd_get_transaction(cmd->config);
  1148. module = i40e_nvmupd_get_module(cmd->config);
  1149. last = (transaction == I40E_NVM_LCB) || (transaction == I40E_NVM_SA);
  1150. memset(&cmd_details, 0, sizeof(cmd_details));
  1151. cmd_details.wb_desc = &hw->nvm_wb_desc;
  1152. status = i40e_aq_read_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
  1153. bytes, last, &cmd_details);
  1154. if (status) {
  1155. i40e_debug(hw, I40E_DEBUG_NVM,
  1156. "i40e_nvmupd_nvm_read mod 0x%x off 0x%x len 0x%x\n",
  1157. module, cmd->offset, cmd->data_size);
  1158. i40e_debug(hw, I40E_DEBUG_NVM,
  1159. "i40e_nvmupd_nvm_read status %d aq %d\n",
  1160. status, hw->aq.asq_last_status);
  1161. *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
  1162. }
  1163. return status;
  1164. }
  1165. /**
  1166. * i40e_nvmupd_nvm_erase - Erase an NVM module
  1167. * @hw: pointer to hardware structure
  1168. * @cmd: pointer to nvm update command buffer
  1169. * @perrno: pointer to return error code
  1170. *
  1171. * module, offset, data_size and data are in cmd structure
  1172. **/
  1173. static i40e_status i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
  1174. struct i40e_nvm_access *cmd,
  1175. int *perrno)
  1176. {
  1177. i40e_status status = 0;
  1178. struct i40e_asq_cmd_details cmd_details;
  1179. u8 module, transaction;
  1180. bool last;
  1181. transaction = i40e_nvmupd_get_transaction(cmd->config);
  1182. module = i40e_nvmupd_get_module(cmd->config);
  1183. last = (transaction & I40E_NVM_LCB);
  1184. memset(&cmd_details, 0, sizeof(cmd_details));
  1185. cmd_details.wb_desc = &hw->nvm_wb_desc;
  1186. status = i40e_aq_erase_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
  1187. last, &cmd_details);
  1188. if (status) {
  1189. i40e_debug(hw, I40E_DEBUG_NVM,
  1190. "i40e_nvmupd_nvm_erase mod 0x%x off 0x%x len 0x%x\n",
  1191. module, cmd->offset, cmd->data_size);
  1192. i40e_debug(hw, I40E_DEBUG_NVM,
  1193. "i40e_nvmupd_nvm_erase status %d aq %d\n",
  1194. status, hw->aq.asq_last_status);
  1195. *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
  1196. }
  1197. return status;
  1198. }
  1199. /**
  1200. * i40e_nvmupd_nvm_write - Write NVM
  1201. * @hw: pointer to hardware structure
  1202. * @cmd: pointer to nvm update command buffer
  1203. * @bytes: pointer to the data buffer
  1204. * @perrno: pointer to return error code
  1205. *
  1206. * module, offset, data_size and data are in cmd structure
  1207. **/
  1208. static i40e_status i40e_nvmupd_nvm_write(struct i40e_hw *hw,
  1209. struct i40e_nvm_access *cmd,
  1210. u8 *bytes, int *perrno)
  1211. {
  1212. i40e_status status = 0;
  1213. struct i40e_asq_cmd_details cmd_details;
  1214. u8 module, transaction;
  1215. bool last;
  1216. transaction = i40e_nvmupd_get_transaction(cmd->config);
  1217. module = i40e_nvmupd_get_module(cmd->config);
  1218. last = (transaction & I40E_NVM_LCB);
  1219. memset(&cmd_details, 0, sizeof(cmd_details));
  1220. cmd_details.wb_desc = &hw->nvm_wb_desc;
  1221. status = i40e_aq_update_nvm(hw, module, cmd->offset,
  1222. (u16)cmd->data_size, bytes, last,
  1223. &cmd_details);
  1224. if (status) {
  1225. i40e_debug(hw, I40E_DEBUG_NVM,
  1226. "i40e_nvmupd_nvm_write mod 0x%x off 0x%x len 0x%x\n",
  1227. module, cmd->offset, cmd->data_size);
  1228. i40e_debug(hw, I40E_DEBUG_NVM,
  1229. "i40e_nvmupd_nvm_write status %d aq %d\n",
  1230. status, hw->aq.asq_last_status);
  1231. *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
  1232. }
  1233. return status;
  1234. }