i40e_nvm.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  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->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->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->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->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->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->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_check_wait_event - handle NVM update operation events
  929. * @hw: pointer to the hardware structure
  930. * @opcode: the event that just happened
  931. **/
  932. void i40e_nvmupd_check_wait_event(struct i40e_hw *hw, u16 opcode)
  933. {
  934. if (opcode == i40e_aqc_opc_nvm_erase ||
  935. opcode == i40e_aqc_opc_nvm_update) {
  936. i40e_debug(hw, I40E_DEBUG_NVM,
  937. "NVMUPD: clearing wait on opcode 0x%04x\n", opcode);
  938. if (hw->nvm_release_on_done) {
  939. i40e_release_nvm(hw);
  940. hw->nvm_release_on_done = false;
  941. }
  942. switch (hw->nvmupd_state) {
  943. case I40E_NVMUPD_STATE_INIT_WAIT:
  944. hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
  945. break;
  946. case I40E_NVMUPD_STATE_WRITE_WAIT:
  947. hw->nvmupd_state = I40E_NVMUPD_STATE_WRITING;
  948. break;
  949. default:
  950. break;
  951. }
  952. }
  953. }
  954. /**
  955. * i40e_nvmupd_validate_command - Validate given command
  956. * @hw: pointer to hardware structure
  957. * @cmd: pointer to nvm update command buffer
  958. * @perrno: pointer to return error code
  959. *
  960. * Return one of the valid command types or I40E_NVMUPD_INVALID
  961. **/
  962. static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
  963. struct i40e_nvm_access *cmd,
  964. int *perrno)
  965. {
  966. enum i40e_nvmupd_cmd upd_cmd;
  967. u8 module, transaction;
  968. /* anything that doesn't match a recognized case is an error */
  969. upd_cmd = I40E_NVMUPD_INVALID;
  970. transaction = i40e_nvmupd_get_transaction(cmd->config);
  971. module = i40e_nvmupd_get_module(cmd->config);
  972. /* limits on data size */
  973. if ((cmd->data_size < 1) ||
  974. (cmd->data_size > I40E_NVMUPD_MAX_DATA)) {
  975. i40e_debug(hw, I40E_DEBUG_NVM,
  976. "i40e_nvmupd_validate_command data_size %d\n",
  977. cmd->data_size);
  978. *perrno = -EFAULT;
  979. return I40E_NVMUPD_INVALID;
  980. }
  981. switch (cmd->command) {
  982. case I40E_NVM_READ:
  983. switch (transaction) {
  984. case I40E_NVM_CON:
  985. upd_cmd = I40E_NVMUPD_READ_CON;
  986. break;
  987. case I40E_NVM_SNT:
  988. upd_cmd = I40E_NVMUPD_READ_SNT;
  989. break;
  990. case I40E_NVM_LCB:
  991. upd_cmd = I40E_NVMUPD_READ_LCB;
  992. break;
  993. case I40E_NVM_SA:
  994. upd_cmd = I40E_NVMUPD_READ_SA;
  995. break;
  996. case I40E_NVM_EXEC:
  997. if (module == 0xf)
  998. upd_cmd = I40E_NVMUPD_STATUS;
  999. else if (module == 0)
  1000. upd_cmd = I40E_NVMUPD_GET_AQ_RESULT;
  1001. break;
  1002. }
  1003. break;
  1004. case I40E_NVM_WRITE:
  1005. switch (transaction) {
  1006. case I40E_NVM_CON:
  1007. upd_cmd = I40E_NVMUPD_WRITE_CON;
  1008. break;
  1009. case I40E_NVM_SNT:
  1010. upd_cmd = I40E_NVMUPD_WRITE_SNT;
  1011. break;
  1012. case I40E_NVM_LCB:
  1013. upd_cmd = I40E_NVMUPD_WRITE_LCB;
  1014. break;
  1015. case I40E_NVM_SA:
  1016. upd_cmd = I40E_NVMUPD_WRITE_SA;
  1017. break;
  1018. case I40E_NVM_ERA:
  1019. upd_cmd = I40E_NVMUPD_WRITE_ERA;
  1020. break;
  1021. case I40E_NVM_CSUM:
  1022. upd_cmd = I40E_NVMUPD_CSUM_CON;
  1023. break;
  1024. case (I40E_NVM_CSUM|I40E_NVM_SA):
  1025. upd_cmd = I40E_NVMUPD_CSUM_SA;
  1026. break;
  1027. case (I40E_NVM_CSUM|I40E_NVM_LCB):
  1028. upd_cmd = I40E_NVMUPD_CSUM_LCB;
  1029. break;
  1030. case I40E_NVM_EXEC:
  1031. if (module == 0)
  1032. upd_cmd = I40E_NVMUPD_EXEC_AQ;
  1033. break;
  1034. }
  1035. break;
  1036. }
  1037. return upd_cmd;
  1038. }
  1039. /**
  1040. * i40e_nvmupd_exec_aq - Run an AQ command
  1041. * @hw: pointer to hardware structure
  1042. * @cmd: pointer to nvm update command buffer
  1043. * @bytes: pointer to the data buffer
  1044. * @perrno: pointer to return error code
  1045. *
  1046. * cmd structure contains identifiers and data buffer
  1047. **/
  1048. static i40e_status i40e_nvmupd_exec_aq(struct i40e_hw *hw,
  1049. struct i40e_nvm_access *cmd,
  1050. u8 *bytes, int *perrno)
  1051. {
  1052. struct i40e_asq_cmd_details cmd_details;
  1053. i40e_status status;
  1054. struct i40e_aq_desc *aq_desc;
  1055. u32 buff_size = 0;
  1056. u8 *buff = NULL;
  1057. u32 aq_desc_len;
  1058. u32 aq_data_len;
  1059. i40e_debug(hw, I40E_DEBUG_NVM, "NVMUPD: %s\n", __func__);
  1060. memset(&cmd_details, 0, sizeof(cmd_details));
  1061. cmd_details.wb_desc = &hw->nvm_wb_desc;
  1062. aq_desc_len = sizeof(struct i40e_aq_desc);
  1063. memset(&hw->nvm_wb_desc, 0, aq_desc_len);
  1064. /* get the aq descriptor */
  1065. if (cmd->data_size < aq_desc_len) {
  1066. i40e_debug(hw, I40E_DEBUG_NVM,
  1067. "NVMUPD: not enough aq desc bytes for exec, size %d < %d\n",
  1068. cmd->data_size, aq_desc_len);
  1069. *perrno = -EINVAL;
  1070. return I40E_ERR_PARAM;
  1071. }
  1072. aq_desc = (struct i40e_aq_desc *)bytes;
  1073. /* if data buffer needed, make sure it's ready */
  1074. aq_data_len = cmd->data_size - aq_desc_len;
  1075. buff_size = max_t(u32, aq_data_len, le16_to_cpu(aq_desc->datalen));
  1076. if (buff_size) {
  1077. if (!hw->nvm_buff.va) {
  1078. status = i40e_allocate_virt_mem(hw, &hw->nvm_buff,
  1079. hw->aq.asq_buf_size);
  1080. if (status)
  1081. i40e_debug(hw, I40E_DEBUG_NVM,
  1082. "NVMUPD: i40e_allocate_virt_mem for exec buff failed, %d\n",
  1083. status);
  1084. }
  1085. if (hw->nvm_buff.va) {
  1086. buff = hw->nvm_buff.va;
  1087. memcpy(buff, &bytes[aq_desc_len], aq_data_len);
  1088. }
  1089. }
  1090. /* and away we go! */
  1091. status = i40e_asq_send_command(hw, aq_desc, buff,
  1092. buff_size, &cmd_details);
  1093. if (status) {
  1094. i40e_debug(hw, I40E_DEBUG_NVM,
  1095. "i40e_nvmupd_exec_aq err %s aq_err %s\n",
  1096. i40e_stat_str(hw, status),
  1097. i40e_aq_str(hw, hw->aq.asq_last_status));
  1098. *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
  1099. }
  1100. return status;
  1101. }
  1102. /**
  1103. * i40e_nvmupd_get_aq_result - Get the results from the previous exec_aq
  1104. * @hw: pointer to hardware structure
  1105. * @cmd: pointer to nvm update command buffer
  1106. * @bytes: pointer to the data buffer
  1107. * @perrno: pointer to return error code
  1108. *
  1109. * cmd structure contains identifiers and data buffer
  1110. **/
  1111. static i40e_status i40e_nvmupd_get_aq_result(struct i40e_hw *hw,
  1112. struct i40e_nvm_access *cmd,
  1113. u8 *bytes, int *perrno)
  1114. {
  1115. u32 aq_total_len;
  1116. u32 aq_desc_len;
  1117. int remainder;
  1118. u8 *buff;
  1119. i40e_debug(hw, I40E_DEBUG_NVM, "NVMUPD: %s\n", __func__);
  1120. aq_desc_len = sizeof(struct i40e_aq_desc);
  1121. aq_total_len = aq_desc_len + le16_to_cpu(hw->nvm_wb_desc.datalen);
  1122. /* check offset range */
  1123. if (cmd->offset > aq_total_len) {
  1124. i40e_debug(hw, I40E_DEBUG_NVM, "%s: offset too big %d > %d\n",
  1125. __func__, cmd->offset, aq_total_len);
  1126. *perrno = -EINVAL;
  1127. return I40E_ERR_PARAM;
  1128. }
  1129. /* check copylength range */
  1130. if (cmd->data_size > (aq_total_len - cmd->offset)) {
  1131. int new_len = aq_total_len - cmd->offset;
  1132. i40e_debug(hw, I40E_DEBUG_NVM, "%s: copy length %d too big, trimming to %d\n",
  1133. __func__, cmd->data_size, new_len);
  1134. cmd->data_size = new_len;
  1135. }
  1136. remainder = cmd->data_size;
  1137. if (cmd->offset < aq_desc_len) {
  1138. u32 len = aq_desc_len - cmd->offset;
  1139. len = min(len, cmd->data_size);
  1140. i40e_debug(hw, I40E_DEBUG_NVM, "%s: aq_desc bytes %d to %d\n",
  1141. __func__, cmd->offset, cmd->offset + len);
  1142. buff = ((u8 *)&hw->nvm_wb_desc) + cmd->offset;
  1143. memcpy(bytes, buff, len);
  1144. bytes += len;
  1145. remainder -= len;
  1146. buff = hw->nvm_buff.va;
  1147. } else {
  1148. buff = hw->nvm_buff.va + (cmd->offset - aq_desc_len);
  1149. }
  1150. if (remainder > 0) {
  1151. int start_byte = buff - (u8 *)hw->nvm_buff.va;
  1152. i40e_debug(hw, I40E_DEBUG_NVM, "%s: databuf bytes %d to %d\n",
  1153. __func__, start_byte, start_byte + remainder);
  1154. memcpy(bytes, buff, remainder);
  1155. }
  1156. return 0;
  1157. }
  1158. /**
  1159. * i40e_nvmupd_nvm_read - Read NVM
  1160. * @hw: pointer to hardware structure
  1161. * @cmd: pointer to nvm update command buffer
  1162. * @bytes: pointer to the data buffer
  1163. * @perrno: pointer to return error code
  1164. *
  1165. * cmd structure contains identifiers and data buffer
  1166. **/
  1167. static i40e_status i40e_nvmupd_nvm_read(struct i40e_hw *hw,
  1168. struct i40e_nvm_access *cmd,
  1169. u8 *bytes, int *perrno)
  1170. {
  1171. struct i40e_asq_cmd_details cmd_details;
  1172. i40e_status status;
  1173. u8 module, transaction;
  1174. bool last;
  1175. transaction = i40e_nvmupd_get_transaction(cmd->config);
  1176. module = i40e_nvmupd_get_module(cmd->config);
  1177. last = (transaction == I40E_NVM_LCB) || (transaction == I40E_NVM_SA);
  1178. memset(&cmd_details, 0, sizeof(cmd_details));
  1179. cmd_details.wb_desc = &hw->nvm_wb_desc;
  1180. status = i40e_aq_read_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
  1181. bytes, last, &cmd_details);
  1182. if (status) {
  1183. i40e_debug(hw, I40E_DEBUG_NVM,
  1184. "i40e_nvmupd_nvm_read mod 0x%x off 0x%x len 0x%x\n",
  1185. module, cmd->offset, cmd->data_size);
  1186. i40e_debug(hw, I40E_DEBUG_NVM,
  1187. "i40e_nvmupd_nvm_read status %d aq %d\n",
  1188. status, hw->aq.asq_last_status);
  1189. *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
  1190. }
  1191. return status;
  1192. }
  1193. /**
  1194. * i40e_nvmupd_nvm_erase - Erase an NVM module
  1195. * @hw: pointer to hardware structure
  1196. * @cmd: pointer to nvm update command buffer
  1197. * @perrno: pointer to return error code
  1198. *
  1199. * module, offset, data_size and data are in cmd structure
  1200. **/
  1201. static i40e_status i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
  1202. struct i40e_nvm_access *cmd,
  1203. int *perrno)
  1204. {
  1205. i40e_status status = 0;
  1206. struct i40e_asq_cmd_details cmd_details;
  1207. u8 module, transaction;
  1208. bool last;
  1209. transaction = i40e_nvmupd_get_transaction(cmd->config);
  1210. module = i40e_nvmupd_get_module(cmd->config);
  1211. last = (transaction & I40E_NVM_LCB);
  1212. memset(&cmd_details, 0, sizeof(cmd_details));
  1213. cmd_details.wb_desc = &hw->nvm_wb_desc;
  1214. status = i40e_aq_erase_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
  1215. last, &cmd_details);
  1216. if (status) {
  1217. i40e_debug(hw, I40E_DEBUG_NVM,
  1218. "i40e_nvmupd_nvm_erase mod 0x%x off 0x%x len 0x%x\n",
  1219. module, cmd->offset, cmd->data_size);
  1220. i40e_debug(hw, I40E_DEBUG_NVM,
  1221. "i40e_nvmupd_nvm_erase status %d aq %d\n",
  1222. status, hw->aq.asq_last_status);
  1223. *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
  1224. }
  1225. return status;
  1226. }
  1227. /**
  1228. * i40e_nvmupd_nvm_write - Write NVM
  1229. * @hw: pointer to hardware structure
  1230. * @cmd: pointer to nvm update command buffer
  1231. * @bytes: pointer to the data buffer
  1232. * @perrno: pointer to return error code
  1233. *
  1234. * module, offset, data_size and data are in cmd structure
  1235. **/
  1236. static i40e_status i40e_nvmupd_nvm_write(struct i40e_hw *hw,
  1237. struct i40e_nvm_access *cmd,
  1238. u8 *bytes, int *perrno)
  1239. {
  1240. i40e_status status = 0;
  1241. struct i40e_asq_cmd_details cmd_details;
  1242. u8 module, transaction;
  1243. bool last;
  1244. transaction = i40e_nvmupd_get_transaction(cmd->config);
  1245. module = i40e_nvmupd_get_module(cmd->config);
  1246. last = (transaction & I40E_NVM_LCB);
  1247. memset(&cmd_details, 0, sizeof(cmd_details));
  1248. cmd_details.wb_desc = &hw->nvm_wb_desc;
  1249. status = i40e_aq_update_nvm(hw, module, cmd->offset,
  1250. (u16)cmd->data_size, bytes, last,
  1251. &cmd_details);
  1252. if (status) {
  1253. i40e_debug(hw, I40E_DEBUG_NVM,
  1254. "i40e_nvmupd_nvm_write mod 0x%x off 0x%x len 0x%x\n",
  1255. module, cmd->offset, cmd->data_size);
  1256. i40e_debug(hw, I40E_DEBUG_NVM,
  1257. "i40e_nvmupd_nvm_write status %d aq %d\n",
  1258. status, hw->aq.asq_last_status);
  1259. *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
  1260. }
  1261. return status;
  1262. }