i40e_nvm.c 43 KB

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