i40e_nvm.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  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. if (hw->flags & I40E_HW_FLAG_AQ_SRCTL_ACCESS_ENABLE) {
  267. ret_code = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
  268. if (!ret_code) {
  269. ret_code = i40e_read_nvm_word_aq(hw, offset, data);
  270. i40e_release_nvm(hw);
  271. }
  272. } else {
  273. ret_code = i40e_read_nvm_word_srctl(hw, offset, data);
  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 cmd 0x%08x config 0x%08x offset 0x%08x data_size 0x%08x\n",
  626. i40e_nvm_update_state_str[upd_cmd],
  627. hw->nvmupd_state,
  628. hw->aq.nvm_release_on_done,
  629. cmd->command, cmd->config, cmd->offset, cmd->data_size);
  630. if (upd_cmd == I40E_NVMUPD_INVALID) {
  631. *perrno = -EFAULT;
  632. i40e_debug(hw, I40E_DEBUG_NVM,
  633. "i40e_nvmupd_validate_command returns %d errno %d\n",
  634. upd_cmd, *perrno);
  635. }
  636. /* a status request returns immediately rather than
  637. * going into the state machine
  638. */
  639. if (upd_cmd == I40E_NVMUPD_STATUS) {
  640. bytes[0] = hw->nvmupd_state;
  641. return 0;
  642. }
  643. switch (hw->nvmupd_state) {
  644. case I40E_NVMUPD_STATE_INIT:
  645. status = i40e_nvmupd_state_init(hw, cmd, bytes, perrno);
  646. break;
  647. case I40E_NVMUPD_STATE_READING:
  648. status = i40e_nvmupd_state_reading(hw, cmd, bytes, perrno);
  649. break;
  650. case I40E_NVMUPD_STATE_WRITING:
  651. status = i40e_nvmupd_state_writing(hw, cmd, bytes, perrno);
  652. break;
  653. case I40E_NVMUPD_STATE_INIT_WAIT:
  654. case I40E_NVMUPD_STATE_WRITE_WAIT:
  655. status = I40E_ERR_NOT_READY;
  656. *perrno = -EBUSY;
  657. break;
  658. default:
  659. /* invalid state, should never happen */
  660. i40e_debug(hw, I40E_DEBUG_NVM,
  661. "NVMUPD: no such state %d\n", hw->nvmupd_state);
  662. status = I40E_NOT_SUPPORTED;
  663. *perrno = -ESRCH;
  664. break;
  665. }
  666. return status;
  667. }
  668. /**
  669. * i40e_nvmupd_state_init - Handle NVM update state Init
  670. * @hw: pointer to hardware structure
  671. * @cmd: pointer to nvm update command buffer
  672. * @bytes: pointer to the data buffer
  673. * @perrno: pointer to return error code
  674. *
  675. * Process legitimate commands of the Init state and conditionally set next
  676. * state. Reject all other commands.
  677. **/
  678. static i40e_status i40e_nvmupd_state_init(struct i40e_hw *hw,
  679. struct i40e_nvm_access *cmd,
  680. u8 *bytes, int *perrno)
  681. {
  682. i40e_status status = 0;
  683. enum i40e_nvmupd_cmd upd_cmd;
  684. upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
  685. switch (upd_cmd) {
  686. case I40E_NVMUPD_READ_SA:
  687. status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
  688. if (status) {
  689. *perrno = i40e_aq_rc_to_posix(status,
  690. hw->aq.asq_last_status);
  691. } else {
  692. status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
  693. i40e_release_nvm(hw);
  694. }
  695. break;
  696. case I40E_NVMUPD_READ_SNT:
  697. status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
  698. if (status) {
  699. *perrno = i40e_aq_rc_to_posix(status,
  700. hw->aq.asq_last_status);
  701. } else {
  702. status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
  703. if (status)
  704. i40e_release_nvm(hw);
  705. else
  706. hw->nvmupd_state = I40E_NVMUPD_STATE_READING;
  707. }
  708. break;
  709. case I40E_NVMUPD_WRITE_ERA:
  710. status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
  711. if (status) {
  712. *perrno = i40e_aq_rc_to_posix(status,
  713. hw->aq.asq_last_status);
  714. } else {
  715. status = i40e_nvmupd_nvm_erase(hw, cmd, perrno);
  716. if (status) {
  717. i40e_release_nvm(hw);
  718. } else {
  719. hw->aq.nvm_release_on_done = true;
  720. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
  721. }
  722. }
  723. break;
  724. case I40E_NVMUPD_WRITE_SA:
  725. status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
  726. if (status) {
  727. *perrno = i40e_aq_rc_to_posix(status,
  728. hw->aq.asq_last_status);
  729. } else {
  730. status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
  731. if (status) {
  732. i40e_release_nvm(hw);
  733. } else {
  734. hw->aq.nvm_release_on_done = true;
  735. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
  736. }
  737. }
  738. break;
  739. case I40E_NVMUPD_WRITE_SNT:
  740. status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
  741. if (status) {
  742. *perrno = i40e_aq_rc_to_posix(status,
  743. hw->aq.asq_last_status);
  744. } else {
  745. status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
  746. if (status)
  747. i40e_release_nvm(hw);
  748. else
  749. hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
  750. }
  751. break;
  752. case I40E_NVMUPD_CSUM_SA:
  753. status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
  754. if (status) {
  755. *perrno = i40e_aq_rc_to_posix(status,
  756. hw->aq.asq_last_status);
  757. } else {
  758. status = i40e_update_nvm_checksum(hw);
  759. if (status) {
  760. *perrno = hw->aq.asq_last_status ?
  761. i40e_aq_rc_to_posix(status,
  762. hw->aq.asq_last_status) :
  763. -EIO;
  764. i40e_release_nvm(hw);
  765. } else {
  766. hw->aq.nvm_release_on_done = true;
  767. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
  768. }
  769. }
  770. break;
  771. case I40E_NVMUPD_EXEC_AQ:
  772. status = i40e_nvmupd_exec_aq(hw, cmd, bytes, perrno);
  773. break;
  774. case I40E_NVMUPD_GET_AQ_RESULT:
  775. status = i40e_nvmupd_get_aq_result(hw, cmd, bytes, perrno);
  776. break;
  777. default:
  778. i40e_debug(hw, I40E_DEBUG_NVM,
  779. "NVMUPD: bad cmd %s in init state\n",
  780. i40e_nvm_update_state_str[upd_cmd]);
  781. status = I40E_ERR_NVM;
  782. *perrno = -ESRCH;
  783. break;
  784. }
  785. return status;
  786. }
  787. /**
  788. * i40e_nvmupd_state_reading - Handle NVM update state Reading
  789. * @hw: pointer to hardware structure
  790. * @cmd: pointer to nvm update command buffer
  791. * @bytes: pointer to the data buffer
  792. * @perrno: pointer to return error code
  793. *
  794. * NVM ownership is already held. Process legitimate commands and set any
  795. * change in state; reject all other commands.
  796. **/
  797. static i40e_status i40e_nvmupd_state_reading(struct i40e_hw *hw,
  798. struct i40e_nvm_access *cmd,
  799. u8 *bytes, int *perrno)
  800. {
  801. i40e_status status = 0;
  802. enum i40e_nvmupd_cmd upd_cmd;
  803. upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
  804. switch (upd_cmd) {
  805. case I40E_NVMUPD_READ_SA:
  806. case I40E_NVMUPD_READ_CON:
  807. status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
  808. break;
  809. case I40E_NVMUPD_READ_LCB:
  810. status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
  811. i40e_release_nvm(hw);
  812. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
  813. break;
  814. default:
  815. i40e_debug(hw, I40E_DEBUG_NVM,
  816. "NVMUPD: bad cmd %s in reading state.\n",
  817. i40e_nvm_update_state_str[upd_cmd]);
  818. status = I40E_NOT_SUPPORTED;
  819. *perrno = -ESRCH;
  820. break;
  821. }
  822. return status;
  823. }
  824. /**
  825. * i40e_nvmupd_state_writing - Handle NVM update state Writing
  826. * @hw: pointer to hardware structure
  827. * @cmd: pointer to nvm update command buffer
  828. * @bytes: pointer to the data buffer
  829. * @perrno: pointer to return error code
  830. *
  831. * NVM ownership is already held. Process legitimate commands and set any
  832. * change in state; reject all other commands
  833. **/
  834. static i40e_status i40e_nvmupd_state_writing(struct i40e_hw *hw,
  835. struct i40e_nvm_access *cmd,
  836. u8 *bytes, int *perrno)
  837. {
  838. i40e_status status = 0;
  839. enum i40e_nvmupd_cmd upd_cmd;
  840. bool retry_attempt = false;
  841. upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
  842. retry:
  843. switch (upd_cmd) {
  844. case I40E_NVMUPD_WRITE_CON:
  845. status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
  846. if (!status)
  847. hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
  848. break;
  849. case I40E_NVMUPD_WRITE_LCB:
  850. status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
  851. if (status) {
  852. *perrno = hw->aq.asq_last_status ?
  853. i40e_aq_rc_to_posix(status,
  854. hw->aq.asq_last_status) :
  855. -EIO;
  856. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
  857. } else {
  858. hw->aq.nvm_release_on_done = true;
  859. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
  860. }
  861. break;
  862. case I40E_NVMUPD_CSUM_CON:
  863. status = i40e_update_nvm_checksum(hw);
  864. if (status) {
  865. *perrno = hw->aq.asq_last_status ?
  866. i40e_aq_rc_to_posix(status,
  867. hw->aq.asq_last_status) :
  868. -EIO;
  869. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
  870. } else {
  871. hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
  872. }
  873. break;
  874. case I40E_NVMUPD_CSUM_LCB:
  875. status = i40e_update_nvm_checksum(hw);
  876. if (status) {
  877. *perrno = hw->aq.asq_last_status ?
  878. i40e_aq_rc_to_posix(status,
  879. hw->aq.asq_last_status) :
  880. -EIO;
  881. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
  882. } else {
  883. hw->aq.nvm_release_on_done = true;
  884. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
  885. }
  886. break;
  887. default:
  888. i40e_debug(hw, I40E_DEBUG_NVM,
  889. "NVMUPD: bad cmd %s in writing state.\n",
  890. i40e_nvm_update_state_str[upd_cmd]);
  891. status = I40E_NOT_SUPPORTED;
  892. *perrno = -ESRCH;
  893. break;
  894. }
  895. /* In some circumstances, a multi-write transaction takes longer
  896. * than the default 3 minute timeout on the write semaphore. If
  897. * the write failed with an EBUSY status, this is likely the problem,
  898. * so here we try to reacquire the semaphore then retry the write.
  899. * We only do one retry, then give up.
  900. */
  901. if (status && (hw->aq.asq_last_status == I40E_AQ_RC_EBUSY) &&
  902. !retry_attempt) {
  903. i40e_status old_status = status;
  904. u32 old_asq_status = hw->aq.asq_last_status;
  905. u32 gtime;
  906. gtime = rd32(hw, I40E_GLVFGEN_TIMER);
  907. if (gtime >= hw->nvm.hw_semaphore_timeout) {
  908. i40e_debug(hw, I40E_DEBUG_ALL,
  909. "NVMUPD: write semaphore expired (%d >= %lld), retrying\n",
  910. gtime, hw->nvm.hw_semaphore_timeout);
  911. i40e_release_nvm(hw);
  912. status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
  913. if (status) {
  914. i40e_debug(hw, I40E_DEBUG_ALL,
  915. "NVMUPD: write semaphore reacquire failed aq_err = %d\n",
  916. hw->aq.asq_last_status);
  917. status = old_status;
  918. hw->aq.asq_last_status = old_asq_status;
  919. } else {
  920. retry_attempt = true;
  921. goto retry;
  922. }
  923. }
  924. }
  925. return status;
  926. }
  927. /**
  928. * i40e_nvmupd_validate_command - Validate given command
  929. * @hw: pointer to hardware structure
  930. * @cmd: pointer to nvm update command buffer
  931. * @perrno: pointer to return error code
  932. *
  933. * Return one of the valid command types or I40E_NVMUPD_INVALID
  934. **/
  935. static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
  936. struct i40e_nvm_access *cmd,
  937. int *perrno)
  938. {
  939. enum i40e_nvmupd_cmd upd_cmd;
  940. u8 module, transaction;
  941. /* anything that doesn't match a recognized case is an error */
  942. upd_cmd = I40E_NVMUPD_INVALID;
  943. transaction = i40e_nvmupd_get_transaction(cmd->config);
  944. module = i40e_nvmupd_get_module(cmd->config);
  945. /* limits on data size */
  946. if ((cmd->data_size < 1) ||
  947. (cmd->data_size > I40E_NVMUPD_MAX_DATA)) {
  948. i40e_debug(hw, I40E_DEBUG_NVM,
  949. "i40e_nvmupd_validate_command data_size %d\n",
  950. cmd->data_size);
  951. *perrno = -EFAULT;
  952. return I40E_NVMUPD_INVALID;
  953. }
  954. switch (cmd->command) {
  955. case I40E_NVM_READ:
  956. switch (transaction) {
  957. case I40E_NVM_CON:
  958. upd_cmd = I40E_NVMUPD_READ_CON;
  959. break;
  960. case I40E_NVM_SNT:
  961. upd_cmd = I40E_NVMUPD_READ_SNT;
  962. break;
  963. case I40E_NVM_LCB:
  964. upd_cmd = I40E_NVMUPD_READ_LCB;
  965. break;
  966. case I40E_NVM_SA:
  967. upd_cmd = I40E_NVMUPD_READ_SA;
  968. break;
  969. case I40E_NVM_EXEC:
  970. if (module == 0xf)
  971. upd_cmd = I40E_NVMUPD_STATUS;
  972. else if (module == 0)
  973. upd_cmd = I40E_NVMUPD_GET_AQ_RESULT;
  974. break;
  975. }
  976. break;
  977. case I40E_NVM_WRITE:
  978. switch (transaction) {
  979. case I40E_NVM_CON:
  980. upd_cmd = I40E_NVMUPD_WRITE_CON;
  981. break;
  982. case I40E_NVM_SNT:
  983. upd_cmd = I40E_NVMUPD_WRITE_SNT;
  984. break;
  985. case I40E_NVM_LCB:
  986. upd_cmd = I40E_NVMUPD_WRITE_LCB;
  987. break;
  988. case I40E_NVM_SA:
  989. upd_cmd = I40E_NVMUPD_WRITE_SA;
  990. break;
  991. case I40E_NVM_ERA:
  992. upd_cmd = I40E_NVMUPD_WRITE_ERA;
  993. break;
  994. case I40E_NVM_CSUM:
  995. upd_cmd = I40E_NVMUPD_CSUM_CON;
  996. break;
  997. case (I40E_NVM_CSUM|I40E_NVM_SA):
  998. upd_cmd = I40E_NVMUPD_CSUM_SA;
  999. break;
  1000. case (I40E_NVM_CSUM|I40E_NVM_LCB):
  1001. upd_cmd = I40E_NVMUPD_CSUM_LCB;
  1002. break;
  1003. case I40E_NVM_EXEC:
  1004. if (module == 0)
  1005. upd_cmd = I40E_NVMUPD_EXEC_AQ;
  1006. break;
  1007. }
  1008. break;
  1009. }
  1010. return upd_cmd;
  1011. }
  1012. /**
  1013. * i40e_nvmupd_exec_aq - Run an AQ command
  1014. * @hw: pointer to hardware structure
  1015. * @cmd: pointer to nvm update command buffer
  1016. * @bytes: pointer to the data buffer
  1017. * @perrno: pointer to return error code
  1018. *
  1019. * cmd structure contains identifiers and data buffer
  1020. **/
  1021. static i40e_status i40e_nvmupd_exec_aq(struct i40e_hw *hw,
  1022. struct i40e_nvm_access *cmd,
  1023. u8 *bytes, int *perrno)
  1024. {
  1025. struct i40e_asq_cmd_details cmd_details;
  1026. i40e_status status;
  1027. struct i40e_aq_desc *aq_desc;
  1028. u32 buff_size = 0;
  1029. u8 *buff = NULL;
  1030. u32 aq_desc_len;
  1031. u32 aq_data_len;
  1032. i40e_debug(hw, I40E_DEBUG_NVM, "NVMUPD: %s\n", __func__);
  1033. memset(&cmd_details, 0, sizeof(cmd_details));
  1034. cmd_details.wb_desc = &hw->nvm_wb_desc;
  1035. aq_desc_len = sizeof(struct i40e_aq_desc);
  1036. memset(&hw->nvm_wb_desc, 0, aq_desc_len);
  1037. /* get the aq descriptor */
  1038. if (cmd->data_size < aq_desc_len) {
  1039. i40e_debug(hw, I40E_DEBUG_NVM,
  1040. "NVMUPD: not enough aq desc bytes for exec, size %d < %d\n",
  1041. cmd->data_size, aq_desc_len);
  1042. *perrno = -EINVAL;
  1043. return I40E_ERR_PARAM;
  1044. }
  1045. aq_desc = (struct i40e_aq_desc *)bytes;
  1046. /* if data buffer needed, make sure it's ready */
  1047. aq_data_len = cmd->data_size - aq_desc_len;
  1048. buff_size = max_t(u32, aq_data_len, le16_to_cpu(aq_desc->datalen));
  1049. if (buff_size) {
  1050. if (!hw->nvm_buff.va) {
  1051. status = i40e_allocate_virt_mem(hw, &hw->nvm_buff,
  1052. hw->aq.asq_buf_size);
  1053. if (status)
  1054. i40e_debug(hw, I40E_DEBUG_NVM,
  1055. "NVMUPD: i40e_allocate_virt_mem for exec buff failed, %d\n",
  1056. status);
  1057. }
  1058. if (hw->nvm_buff.va) {
  1059. buff = hw->nvm_buff.va;
  1060. memcpy(buff, &bytes[aq_desc_len], aq_data_len);
  1061. }
  1062. }
  1063. /* and away we go! */
  1064. status = i40e_asq_send_command(hw, aq_desc, buff,
  1065. buff_size, &cmd_details);
  1066. if (status) {
  1067. i40e_debug(hw, I40E_DEBUG_NVM,
  1068. "i40e_nvmupd_exec_aq err %s aq_err %s\n",
  1069. i40e_stat_str(hw, status),
  1070. i40e_aq_str(hw, hw->aq.asq_last_status));
  1071. *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
  1072. }
  1073. return status;
  1074. }
  1075. /**
  1076. * i40e_nvmupd_get_aq_result - Get the results from the previous exec_aq
  1077. * @hw: pointer to hardware structure
  1078. * @cmd: pointer to nvm update command buffer
  1079. * @bytes: pointer to the data buffer
  1080. * @perrno: pointer to return error code
  1081. *
  1082. * cmd structure contains identifiers and data buffer
  1083. **/
  1084. static i40e_status i40e_nvmupd_get_aq_result(struct i40e_hw *hw,
  1085. struct i40e_nvm_access *cmd,
  1086. u8 *bytes, int *perrno)
  1087. {
  1088. u32 aq_total_len;
  1089. u32 aq_desc_len;
  1090. int remainder;
  1091. u8 *buff;
  1092. i40e_debug(hw, I40E_DEBUG_NVM, "NVMUPD: %s\n", __func__);
  1093. aq_desc_len = sizeof(struct i40e_aq_desc);
  1094. aq_total_len = aq_desc_len + le16_to_cpu(hw->nvm_wb_desc.datalen);
  1095. /* check offset range */
  1096. if (cmd->offset > aq_total_len) {
  1097. i40e_debug(hw, I40E_DEBUG_NVM, "%s: offset too big %d > %d\n",
  1098. __func__, cmd->offset, aq_total_len);
  1099. *perrno = -EINVAL;
  1100. return I40E_ERR_PARAM;
  1101. }
  1102. /* check copylength range */
  1103. if (cmd->data_size > (aq_total_len - cmd->offset)) {
  1104. int new_len = aq_total_len - cmd->offset;
  1105. i40e_debug(hw, I40E_DEBUG_NVM, "%s: copy length %d too big, trimming to %d\n",
  1106. __func__, cmd->data_size, new_len);
  1107. cmd->data_size = new_len;
  1108. }
  1109. remainder = cmd->data_size;
  1110. if (cmd->offset < aq_desc_len) {
  1111. u32 len = aq_desc_len - cmd->offset;
  1112. len = min(len, cmd->data_size);
  1113. i40e_debug(hw, I40E_DEBUG_NVM, "%s: aq_desc bytes %d to %d\n",
  1114. __func__, cmd->offset, cmd->offset + len);
  1115. buff = ((u8 *)&hw->nvm_wb_desc) + cmd->offset;
  1116. memcpy(bytes, buff, len);
  1117. bytes += len;
  1118. remainder -= len;
  1119. buff = hw->nvm_buff.va;
  1120. } else {
  1121. buff = hw->nvm_buff.va + (cmd->offset - aq_desc_len);
  1122. }
  1123. if (remainder > 0) {
  1124. int start_byte = buff - (u8 *)hw->nvm_buff.va;
  1125. i40e_debug(hw, I40E_DEBUG_NVM, "%s: databuf bytes %d to %d\n",
  1126. __func__, start_byte, start_byte + remainder);
  1127. memcpy(bytes, buff, remainder);
  1128. }
  1129. return 0;
  1130. }
  1131. /**
  1132. * i40e_nvmupd_nvm_read - Read NVM
  1133. * @hw: pointer to hardware structure
  1134. * @cmd: pointer to nvm update command buffer
  1135. * @bytes: pointer to the data buffer
  1136. * @perrno: pointer to return error code
  1137. *
  1138. * cmd structure contains identifiers and data buffer
  1139. **/
  1140. static i40e_status i40e_nvmupd_nvm_read(struct i40e_hw *hw,
  1141. struct i40e_nvm_access *cmd,
  1142. u8 *bytes, int *perrno)
  1143. {
  1144. struct i40e_asq_cmd_details cmd_details;
  1145. i40e_status status;
  1146. u8 module, transaction;
  1147. bool last;
  1148. transaction = i40e_nvmupd_get_transaction(cmd->config);
  1149. module = i40e_nvmupd_get_module(cmd->config);
  1150. last = (transaction == I40E_NVM_LCB) || (transaction == I40E_NVM_SA);
  1151. memset(&cmd_details, 0, sizeof(cmd_details));
  1152. cmd_details.wb_desc = &hw->nvm_wb_desc;
  1153. status = i40e_aq_read_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
  1154. bytes, last, &cmd_details);
  1155. if (status) {
  1156. i40e_debug(hw, I40E_DEBUG_NVM,
  1157. "i40e_nvmupd_nvm_read mod 0x%x off 0x%x len 0x%x\n",
  1158. module, cmd->offset, cmd->data_size);
  1159. i40e_debug(hw, I40E_DEBUG_NVM,
  1160. "i40e_nvmupd_nvm_read status %d aq %d\n",
  1161. status, hw->aq.asq_last_status);
  1162. *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
  1163. }
  1164. return status;
  1165. }
  1166. /**
  1167. * i40e_nvmupd_nvm_erase - Erase an NVM module
  1168. * @hw: pointer to hardware structure
  1169. * @cmd: pointer to nvm update command buffer
  1170. * @perrno: pointer to return error code
  1171. *
  1172. * module, offset, data_size and data are in cmd structure
  1173. **/
  1174. static i40e_status i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
  1175. struct i40e_nvm_access *cmd,
  1176. int *perrno)
  1177. {
  1178. i40e_status status = 0;
  1179. struct i40e_asq_cmd_details cmd_details;
  1180. u8 module, transaction;
  1181. bool last;
  1182. transaction = i40e_nvmupd_get_transaction(cmd->config);
  1183. module = i40e_nvmupd_get_module(cmd->config);
  1184. last = (transaction & I40E_NVM_LCB);
  1185. memset(&cmd_details, 0, sizeof(cmd_details));
  1186. cmd_details.wb_desc = &hw->nvm_wb_desc;
  1187. status = i40e_aq_erase_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
  1188. last, &cmd_details);
  1189. if (status) {
  1190. i40e_debug(hw, I40E_DEBUG_NVM,
  1191. "i40e_nvmupd_nvm_erase mod 0x%x off 0x%x len 0x%x\n",
  1192. module, cmd->offset, cmd->data_size);
  1193. i40e_debug(hw, I40E_DEBUG_NVM,
  1194. "i40e_nvmupd_nvm_erase status %d aq %d\n",
  1195. status, hw->aq.asq_last_status);
  1196. *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
  1197. }
  1198. return status;
  1199. }
  1200. /**
  1201. * i40e_nvmupd_nvm_write - Write NVM
  1202. * @hw: pointer to hardware structure
  1203. * @cmd: pointer to nvm update command buffer
  1204. * @bytes: pointer to the data buffer
  1205. * @perrno: pointer to return error code
  1206. *
  1207. * module, offset, data_size and data are in cmd structure
  1208. **/
  1209. static i40e_status i40e_nvmupd_nvm_write(struct i40e_hw *hw,
  1210. struct i40e_nvm_access *cmd,
  1211. u8 *bytes, int *perrno)
  1212. {
  1213. i40e_status status = 0;
  1214. struct i40e_asq_cmd_details cmd_details;
  1215. u8 module, transaction;
  1216. bool last;
  1217. transaction = i40e_nvmupd_get_transaction(cmd->config);
  1218. module = i40e_nvmupd_get_module(cmd->config);
  1219. last = (transaction & I40E_NVM_LCB);
  1220. memset(&cmd_details, 0, sizeof(cmd_details));
  1221. cmd_details.wb_desc = &hw->nvm_wb_desc;
  1222. status = i40e_aq_update_nvm(hw, module, cmd->offset,
  1223. (u16)cmd->data_size, bytes, last,
  1224. &cmd_details);
  1225. if (status) {
  1226. i40e_debug(hw, I40E_DEBUG_NVM,
  1227. "i40e_nvmupd_nvm_write mod 0x%x off 0x%x len 0x%x\n",
  1228. module, cmd->offset, cmd->data_size);
  1229. i40e_debug(hw, I40E_DEBUG_NVM,
  1230. "i40e_nvmupd_nvm_write status %d aq %d\n",
  1231. status, hw->aq.asq_last_status);
  1232. *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
  1233. }
  1234. return status;
  1235. }