ice_nvm.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018, Intel Corporation. */
  3. #include <linux/vmalloc.h>
  4. #include "ice_common.h"
  5. /**
  6. * ice_aq_read_nvm
  7. * @hw: pointer to the HW struct
  8. * @module_typeid: module pointer location in words from the NVM beginning
  9. * @offset: byte offset from the module beginning
  10. * @length: length of the section to be read (in bytes from the offset)
  11. * @data: command buffer (size [bytes] = length)
  12. * @last_command: tells if this is the last command in a series
  13. * @read_shadow_ram: tell if this is a shadow RAM read
  14. * @cd: pointer to command details structure or NULL
  15. *
  16. * Read the NVM using the admin queue commands (0x0701)
  17. */
  18. int ice_aq_read_nvm(struct ice_hw *hw, u16 module_typeid, u32 offset,
  19. u16 length, void *data, bool last_command,
  20. bool read_shadow_ram, struct ice_sq_cd *cd)
  21. {
  22. struct ice_aq_desc desc;
  23. struct ice_aqc_nvm *cmd;
  24. cmd = &desc.params.nvm;
  25. if (offset > ICE_AQC_NVM_MAX_OFFSET)
  26. return -EINVAL;
  27. ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_read);
  28. if (!read_shadow_ram && module_typeid == ICE_AQC_NVM_START_POINT)
  29. cmd->cmd_flags |= ICE_AQC_NVM_FLASH_ONLY;
  30. /* If this is the last command in a series, set the proper flag. */
  31. if (last_command)
  32. cmd->cmd_flags |= ICE_AQC_NVM_LAST_CMD;
  33. cmd->module_typeid = cpu_to_le16(module_typeid);
  34. cmd->offset_low = cpu_to_le16(offset & 0xFFFF);
  35. cmd->offset_high = (offset >> 16) & 0xFF;
  36. cmd->length = cpu_to_le16(length);
  37. return ice_aq_send_cmd(hw, &desc, data, length, cd);
  38. }
  39. /**
  40. * ice_read_flat_nvm - Read portion of NVM by flat offset
  41. * @hw: pointer to the HW struct
  42. * @offset: offset from beginning of NVM
  43. * @length: (in) number of bytes to read; (out) number of bytes actually read
  44. * @data: buffer to return data in (sized to fit the specified length)
  45. * @read_shadow_ram: if true, read from shadow RAM instead of NVM
  46. *
  47. * Reads a portion of the NVM, as a flat memory space. This function correctly
  48. * breaks read requests across Shadow RAM sectors and ensures that no single
  49. * read request exceeds the maximum 4KB read for a single AdminQ command.
  50. *
  51. * Returns a status code on failure. Note that the data pointer may be
  52. * partially updated if some reads succeed before a failure.
  53. */
  54. int
  55. ice_read_flat_nvm(struct ice_hw *hw, u32 offset, u32 *length, u8 *data,
  56. bool read_shadow_ram)
  57. {
  58. u32 inlen = *length;
  59. u32 bytes_read = 0;
  60. bool last_cmd;
  61. int status;
  62. *length = 0;
  63. /* Verify the length of the read if this is for the Shadow RAM */
  64. if (read_shadow_ram && ((offset + inlen) > (hw->flash.sr_words * 2u))) {
  65. ice_debug(hw, ICE_DBG_NVM, "NVM error: requested offset is beyond Shadow RAM limit\n");
  66. return -EINVAL;
  67. }
  68. do {
  69. u32 read_size, sector_offset;
  70. /* ice_aq_read_nvm cannot read more than 4KB at a time.
  71. * Additionally, a read from the Shadow RAM may not cross over
  72. * a sector boundary. Conveniently, the sector size is also
  73. * 4KB.
  74. */
  75. sector_offset = offset % ICE_AQ_MAX_BUF_LEN;
  76. read_size = min_t(u32, ICE_AQ_MAX_BUF_LEN - sector_offset,
  77. inlen - bytes_read);
  78. last_cmd = !(bytes_read + read_size < inlen);
  79. status = ice_aq_read_nvm(hw, ICE_AQC_NVM_START_POINT,
  80. offset, read_size,
  81. data + bytes_read, last_cmd,
  82. read_shadow_ram, NULL);
  83. if (status)
  84. break;
  85. bytes_read += read_size;
  86. offset += read_size;
  87. } while (!last_cmd);
  88. *length = bytes_read;
  89. return status;
  90. }
  91. /**
  92. * ice_aq_update_nvm
  93. * @hw: pointer to the HW struct
  94. * @module_typeid: module pointer location in words from the NVM beginning
  95. * @offset: byte offset from the module beginning
  96. * @length: length of the section to be written (in bytes from the offset)
  97. * @data: command buffer (size [bytes] = length)
  98. * @last_command: tells if this is the last command in a series
  99. * @command_flags: command parameters
  100. * @cd: pointer to command details structure or NULL
  101. *
  102. * Update the NVM using the admin queue commands (0x0703)
  103. */
  104. int
  105. ice_aq_update_nvm(struct ice_hw *hw, u16 module_typeid, u32 offset,
  106. u16 length, void *data, bool last_command, u8 command_flags,
  107. struct ice_sq_cd *cd)
  108. {
  109. struct ice_aq_desc desc;
  110. struct ice_aqc_nvm *cmd;
  111. cmd = &desc.params.nvm;
  112. /* In offset the highest byte must be zeroed. */
  113. if (offset & 0xFF000000)
  114. return -EINVAL;
  115. ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_write);
  116. cmd->cmd_flags |= command_flags;
  117. /* If this is the last command in a series, set the proper flag. */
  118. if (last_command)
  119. cmd->cmd_flags |= ICE_AQC_NVM_LAST_CMD;
  120. cmd->module_typeid = cpu_to_le16(module_typeid);
  121. cmd->offset_low = cpu_to_le16(offset & 0xFFFF);
  122. cmd->offset_high = (offset >> 16) & 0xFF;
  123. cmd->length = cpu_to_le16(length);
  124. desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
  125. return ice_aq_send_cmd(hw, &desc, data, length, cd);
  126. }
  127. /**
  128. * ice_aq_erase_nvm
  129. * @hw: pointer to the HW struct
  130. * @module_typeid: module pointer location in words from the NVM beginning
  131. * @cd: pointer to command details structure or NULL
  132. *
  133. * Erase the NVM sector using the admin queue commands (0x0702)
  134. */
  135. int ice_aq_erase_nvm(struct ice_hw *hw, u16 module_typeid, struct ice_sq_cd *cd)
  136. {
  137. struct ice_aq_desc desc;
  138. struct ice_aqc_nvm *cmd;
  139. cmd = &desc.params.nvm;
  140. ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_erase);
  141. cmd->module_typeid = cpu_to_le16(module_typeid);
  142. cmd->length = cpu_to_le16(ICE_AQC_NVM_ERASE_LEN);
  143. cmd->offset_low = 0;
  144. cmd->offset_high = 0;
  145. return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
  146. }
  147. /**
  148. * ice_read_sr_word_aq - Reads Shadow RAM via AQ
  149. * @hw: pointer to the HW structure
  150. * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
  151. * @data: word read from the Shadow RAM
  152. *
  153. * Reads one 16 bit word from the Shadow RAM using ice_read_flat_nvm.
  154. */
  155. static int ice_read_sr_word_aq(struct ice_hw *hw, u16 offset, u16 *data)
  156. {
  157. u32 bytes = sizeof(u16);
  158. __le16 data_local;
  159. int status;
  160. /* Note that ice_read_flat_nvm takes into account the 4Kb AdminQ and
  161. * Shadow RAM sector restrictions necessary when reading from the NVM.
  162. */
  163. status = ice_read_flat_nvm(hw, offset * sizeof(u16), &bytes,
  164. (__force u8 *)&data_local, true);
  165. if (status)
  166. return status;
  167. *data = le16_to_cpu(data_local);
  168. return 0;
  169. }
  170. /**
  171. * ice_acquire_nvm - Generic request for acquiring the NVM ownership
  172. * @hw: pointer to the HW structure
  173. * @access: NVM access type (read or write)
  174. *
  175. * This function will request NVM ownership.
  176. */
  177. int ice_acquire_nvm(struct ice_hw *hw, enum ice_aq_res_access_type access)
  178. {
  179. if (hw->flash.blank_nvm_mode)
  180. return 0;
  181. return ice_acquire_res(hw, ICE_NVM_RES_ID, access, ICE_NVM_TIMEOUT);
  182. }
  183. /**
  184. * ice_release_nvm - Generic request for releasing the NVM ownership
  185. * @hw: pointer to the HW structure
  186. *
  187. * This function will release NVM ownership.
  188. */
  189. void ice_release_nvm(struct ice_hw *hw)
  190. {
  191. if (hw->flash.blank_nvm_mode)
  192. return;
  193. ice_release_res(hw, ICE_NVM_RES_ID);
  194. }
  195. /**
  196. * ice_get_flash_bank_offset - Get offset into requested flash bank
  197. * @hw: pointer to the HW structure
  198. * @bank: whether to read from the active or inactive flash bank
  199. * @module: the module to read from
  200. *
  201. * Based on the module, lookup the module offset from the beginning of the
  202. * flash.
  203. *
  204. * Returns the flash offset. Note that a value of zero is invalid and must be
  205. * treated as an error.
  206. */
  207. static u32 ice_get_flash_bank_offset(struct ice_hw *hw, enum ice_bank_select bank, u16 module)
  208. {
  209. struct ice_bank_info *banks = &hw->flash.banks;
  210. enum ice_flash_bank active_bank;
  211. bool second_bank_active;
  212. u32 offset, size;
  213. switch (module) {
  214. case ICE_SR_1ST_NVM_BANK_PTR:
  215. offset = banks->nvm_ptr;
  216. size = banks->nvm_size;
  217. active_bank = banks->nvm_bank;
  218. break;
  219. case ICE_SR_1ST_OROM_BANK_PTR:
  220. offset = banks->orom_ptr;
  221. size = banks->orom_size;
  222. active_bank = banks->orom_bank;
  223. break;
  224. case ICE_SR_NETLIST_BANK_PTR:
  225. offset = banks->netlist_ptr;
  226. size = banks->netlist_size;
  227. active_bank = banks->netlist_bank;
  228. break;
  229. default:
  230. ice_debug(hw, ICE_DBG_NVM, "Unexpected value for flash module: 0x%04x\n", module);
  231. return 0;
  232. }
  233. switch (active_bank) {
  234. case ICE_1ST_FLASH_BANK:
  235. second_bank_active = false;
  236. break;
  237. case ICE_2ND_FLASH_BANK:
  238. second_bank_active = true;
  239. break;
  240. default:
  241. ice_debug(hw, ICE_DBG_NVM, "Unexpected value for active flash bank: %u\n",
  242. active_bank);
  243. return 0;
  244. }
  245. /* The second flash bank is stored immediately following the first
  246. * bank. Based on whether the 1st or 2nd bank is active, and whether
  247. * we want the active or inactive bank, calculate the desired offset.
  248. */
  249. switch (bank) {
  250. case ICE_ACTIVE_FLASH_BANK:
  251. return offset + (second_bank_active ? size : 0);
  252. case ICE_INACTIVE_FLASH_BANK:
  253. return offset + (second_bank_active ? 0 : size);
  254. }
  255. ice_debug(hw, ICE_DBG_NVM, "Unexpected value for flash bank selection: %u\n", bank);
  256. return 0;
  257. }
  258. /**
  259. * ice_read_flash_module - Read a word from one of the main NVM modules
  260. * @hw: pointer to the HW structure
  261. * @bank: which bank of the module to read
  262. * @module: the module to read
  263. * @offset: the offset into the module in bytes
  264. * @data: storage for the word read from the flash
  265. * @length: bytes of data to read
  266. *
  267. * Read data from the specified flash module. The bank parameter indicates
  268. * whether or not to read from the active bank or the inactive bank of that
  269. * module.
  270. *
  271. * The word will be read using flat NVM access, and relies on the
  272. * hw->flash.banks data being setup by ice_determine_active_flash_banks()
  273. * during initialization.
  274. */
  275. static int
  276. ice_read_flash_module(struct ice_hw *hw, enum ice_bank_select bank, u16 module,
  277. u32 offset, u8 *data, u32 length)
  278. {
  279. int status;
  280. u32 start;
  281. start = ice_get_flash_bank_offset(hw, bank, module);
  282. if (!start) {
  283. ice_debug(hw, ICE_DBG_NVM, "Unable to calculate flash bank offset for module 0x%04x\n",
  284. module);
  285. return -EINVAL;
  286. }
  287. status = ice_acquire_nvm(hw, ICE_RES_READ);
  288. if (status)
  289. return status;
  290. status = ice_read_flat_nvm(hw, start + offset, &length, data, false);
  291. ice_release_nvm(hw);
  292. return status;
  293. }
  294. /**
  295. * ice_read_nvm_module - Read from the active main NVM module
  296. * @hw: pointer to the HW structure
  297. * @bank: whether to read from active or inactive NVM module
  298. * @offset: offset into the NVM module to read, in words
  299. * @data: storage for returned word value
  300. *
  301. * Read the specified word from the active NVM module. This includes the CSS
  302. * header at the start of the NVM module.
  303. */
  304. static int
  305. ice_read_nvm_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
  306. {
  307. __le16 data_local;
  308. int status;
  309. status = ice_read_flash_module(hw, bank, ICE_SR_1ST_NVM_BANK_PTR, offset * sizeof(u16),
  310. (__force u8 *)&data_local, sizeof(u16));
  311. if (!status)
  312. *data = le16_to_cpu(data_local);
  313. return status;
  314. }
  315. /**
  316. * ice_read_nvm_sr_copy - Read a word from the Shadow RAM copy in the NVM bank
  317. * @hw: pointer to the HW structure
  318. * @bank: whether to read from the active or inactive NVM module
  319. * @offset: offset into the Shadow RAM copy to read, in words
  320. * @data: storage for returned word value
  321. *
  322. * Read the specified word from the copy of the Shadow RAM found in the
  323. * specified NVM module.
  324. *
  325. * Note that the Shadow RAM copy is always located after the CSS header, and
  326. * is aligned to 64-byte (32-word) offsets.
  327. */
  328. static int
  329. ice_read_nvm_sr_copy(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
  330. {
  331. u32 sr_copy;
  332. switch (bank) {
  333. case ICE_ACTIVE_FLASH_BANK:
  334. sr_copy = roundup(hw->flash.banks.active_css_hdr_len, 32);
  335. break;
  336. case ICE_INACTIVE_FLASH_BANK:
  337. sr_copy = roundup(hw->flash.banks.inactive_css_hdr_len, 32);
  338. break;
  339. }
  340. return ice_read_nvm_module(hw, bank, sr_copy + offset, data);
  341. }
  342. /**
  343. * ice_read_netlist_module - Read data from the netlist module area
  344. * @hw: pointer to the HW structure
  345. * @bank: whether to read from the active or inactive module
  346. * @offset: offset into the netlist to read from
  347. * @data: storage for returned word value
  348. *
  349. * Read a word from the specified netlist bank.
  350. */
  351. static int
  352. ice_read_netlist_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
  353. {
  354. __le16 data_local;
  355. int status;
  356. status = ice_read_flash_module(hw, bank, ICE_SR_NETLIST_BANK_PTR, offset * sizeof(u16),
  357. (__force u8 *)&data_local, sizeof(u16));
  358. if (!status)
  359. *data = le16_to_cpu(data_local);
  360. return status;
  361. }
  362. /**
  363. * ice_read_sr_word - Reads Shadow RAM word and acquire NVM if necessary
  364. * @hw: pointer to the HW structure
  365. * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
  366. * @data: word read from the Shadow RAM
  367. *
  368. * Reads one 16 bit word from the Shadow RAM using the ice_read_sr_word_aq.
  369. */
  370. int ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data)
  371. {
  372. int status;
  373. status = ice_acquire_nvm(hw, ICE_RES_READ);
  374. if (!status) {
  375. status = ice_read_sr_word_aq(hw, offset, data);
  376. ice_release_nvm(hw);
  377. }
  378. return status;
  379. }
  380. /**
  381. * ice_get_pfa_module_tlv - Reads sub module TLV from NVM PFA
  382. * @hw: pointer to hardware structure
  383. * @module_tlv: pointer to module TLV to return
  384. * @module_tlv_len: pointer to module TLV length to return
  385. * @module_type: module type requested
  386. *
  387. * Finds the requested sub module TLV type from the Preserved Field
  388. * Area (PFA) and returns the TLV pointer and length. The caller can
  389. * use these to read the variable length TLV value.
  390. */
  391. int
  392. ice_get_pfa_module_tlv(struct ice_hw *hw, u16 *module_tlv, u16 *module_tlv_len,
  393. u16 module_type)
  394. {
  395. u16 pfa_len, pfa_ptr, next_tlv, max_tlv;
  396. int status;
  397. status = ice_read_sr_word(hw, ICE_SR_PFA_PTR, &pfa_ptr);
  398. if (status) {
  399. ice_debug(hw, ICE_DBG_INIT, "Preserved Field Array pointer.\n");
  400. return status;
  401. }
  402. status = ice_read_sr_word(hw, pfa_ptr, &pfa_len);
  403. if (status) {
  404. ice_debug(hw, ICE_DBG_INIT, "Failed to read PFA length.\n");
  405. return status;
  406. }
  407. /* The Preserved Fields Area contains a sequence of Type-Length-Value
  408. * structures which define its contents. The PFA length includes all
  409. * of the TLVs, plus the initial length word itself, *and* one final
  410. * word at the end after all of the TLVs.
  411. */
  412. if (check_add_overflow(pfa_ptr, pfa_len - 1, &max_tlv)) {
  413. dev_warn(ice_hw_to_dev(hw), "PFA starts at offset %u. PFA length of %u caused 16-bit arithmetic overflow.\n",
  414. pfa_ptr, pfa_len);
  415. return -EINVAL;
  416. }
  417. /* Starting with first TLV after PFA length, iterate through the list
  418. * of TLVs to find the requested one.
  419. */
  420. next_tlv = pfa_ptr + 1;
  421. while (next_tlv < max_tlv) {
  422. u16 tlv_sub_module_type;
  423. u16 tlv_len;
  424. /* Read TLV type */
  425. status = ice_read_sr_word(hw, next_tlv, &tlv_sub_module_type);
  426. if (status) {
  427. ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV type.\n");
  428. break;
  429. }
  430. /* Read TLV length */
  431. status = ice_read_sr_word(hw, next_tlv + 1, &tlv_len);
  432. if (status) {
  433. ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV length.\n");
  434. break;
  435. }
  436. if (tlv_sub_module_type == module_type) {
  437. if (tlv_len) {
  438. *module_tlv = next_tlv;
  439. *module_tlv_len = tlv_len;
  440. return 0;
  441. }
  442. return -EINVAL;
  443. }
  444. if (check_add_overflow(next_tlv, 2, &next_tlv) ||
  445. check_add_overflow(next_tlv, tlv_len, &next_tlv)) {
  446. dev_warn(ice_hw_to_dev(hw), "TLV of type %u and length 0x%04x caused 16-bit arithmetic overflow. The PFA starts at 0x%04x and has length of 0x%04x\n",
  447. tlv_sub_module_type, tlv_len, pfa_ptr, pfa_len);
  448. return -EINVAL;
  449. }
  450. }
  451. /* Module does not exist */
  452. return -ENOENT;
  453. }
  454. /**
  455. * ice_read_pba_string - Reads part number string from NVM
  456. * @hw: pointer to hardware structure
  457. * @pba_num: stores the part number string from the NVM
  458. * @pba_num_size: part number string buffer length
  459. *
  460. * Reads the part number string from the NVM.
  461. */
  462. int ice_read_pba_string(struct ice_hw *hw, u8 *pba_num, u32 pba_num_size)
  463. {
  464. u16 pba_tlv, pba_tlv_len;
  465. u16 pba_word, pba_size;
  466. int status;
  467. u16 i;
  468. status = ice_get_pfa_module_tlv(hw, &pba_tlv, &pba_tlv_len,
  469. ICE_SR_PBA_BLOCK_PTR);
  470. if (status) {
  471. ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block TLV.\n");
  472. return status;
  473. }
  474. /* pba_size is the next word */
  475. status = ice_read_sr_word(hw, (pba_tlv + 2), &pba_size);
  476. if (status) {
  477. ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Section size.\n");
  478. return status;
  479. }
  480. if (pba_tlv_len < pba_size) {
  481. ice_debug(hw, ICE_DBG_INIT, "Invalid PBA Block TLV size.\n");
  482. return -EINVAL;
  483. }
  484. /* Subtract one to get PBA word count (PBA Size word is included in
  485. * total size)
  486. */
  487. pba_size--;
  488. if (pba_num_size < (((u32)pba_size * 2) + 1)) {
  489. ice_debug(hw, ICE_DBG_INIT, "Buffer too small for PBA data.\n");
  490. return -EINVAL;
  491. }
  492. for (i = 0; i < pba_size; i++) {
  493. status = ice_read_sr_word(hw, (pba_tlv + 2 + 1) + i, &pba_word);
  494. if (status) {
  495. ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block word %d.\n", i);
  496. return status;
  497. }
  498. pba_num[(i * 2)] = (pba_word >> 8) & 0xFF;
  499. pba_num[(i * 2) + 1] = pba_word & 0xFF;
  500. }
  501. pba_num[(pba_size * 2)] = '\0';
  502. return status;
  503. }
  504. /**
  505. * ice_get_nvm_ver_info - Read NVM version information
  506. * @hw: pointer to the HW struct
  507. * @bank: whether to read from the active or inactive flash bank
  508. * @nvm: pointer to NVM info structure
  509. *
  510. * Read the NVM EETRACK ID and map version of the main NVM image bank, filling
  511. * in the NVM info structure.
  512. */
  513. static int
  514. ice_get_nvm_ver_info(struct ice_hw *hw, enum ice_bank_select bank, struct ice_nvm_info *nvm)
  515. {
  516. u16 eetrack_lo, eetrack_hi, ver;
  517. int status;
  518. status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_DEV_STARTER_VER, &ver);
  519. if (status) {
  520. ice_debug(hw, ICE_DBG_NVM, "Failed to read DEV starter version.\n");
  521. return status;
  522. }
  523. nvm->major = FIELD_GET(ICE_NVM_VER_HI_MASK, ver);
  524. nvm->minor = FIELD_GET(ICE_NVM_VER_LO_MASK, ver);
  525. status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_EETRACK_LO, &eetrack_lo);
  526. if (status) {
  527. ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK lo.\n");
  528. return status;
  529. }
  530. status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_EETRACK_HI, &eetrack_hi);
  531. if (status) {
  532. ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK hi.\n");
  533. return status;
  534. }
  535. nvm->eetrack = (eetrack_hi << 16) | eetrack_lo;
  536. return 0;
  537. }
  538. /**
  539. * ice_get_inactive_nvm_ver - Read Option ROM version from the inactive bank
  540. * @hw: pointer to the HW structure
  541. * @nvm: storage for Option ROM version information
  542. *
  543. * Reads the NVM EETRACK ID, Map version, and security revision of the
  544. * inactive NVM bank. Used to access version data for a pending update that
  545. * has not yet been activated.
  546. */
  547. int ice_get_inactive_nvm_ver(struct ice_hw *hw, struct ice_nvm_info *nvm)
  548. {
  549. return ice_get_nvm_ver_info(hw, ICE_INACTIVE_FLASH_BANK, nvm);
  550. }
  551. /**
  552. * ice_get_orom_civd_data - Get the combo version information from Option ROM
  553. * @hw: pointer to the HW struct
  554. * @bank: whether to read from the active or inactive flash module
  555. * @civd: storage for the Option ROM CIVD data.
  556. *
  557. * Searches through the Option ROM flash contents to locate the CIVD data for
  558. * the image.
  559. */
  560. static int
  561. ice_get_orom_civd_data(struct ice_hw *hw, enum ice_bank_select bank,
  562. struct ice_orom_civd_info *civd)
  563. {
  564. u8 *orom_data;
  565. int status;
  566. u32 offset;
  567. /* The CIVD section is located in the Option ROM aligned to 512 bytes.
  568. * The first 4 bytes must contain the ASCII characters "$CIV".
  569. * A simple modulo 256 sum of all of the bytes of the structure must
  570. * equal 0.
  571. *
  572. * The exact location is unknown and varies between images but is
  573. * usually somewhere in the middle of the bank. We need to scan the
  574. * Option ROM bank to locate it.
  575. *
  576. * It's significantly faster to read the entire Option ROM up front
  577. * using the maximum page size, than to read each possible location
  578. * with a separate firmware command.
  579. */
  580. orom_data = vzalloc(hw->flash.banks.orom_size);
  581. if (!orom_data)
  582. return -ENOMEM;
  583. status = ice_read_flash_module(hw, bank, ICE_SR_1ST_OROM_BANK_PTR, 0,
  584. orom_data, hw->flash.banks.orom_size);
  585. if (status) {
  586. vfree(orom_data);
  587. ice_debug(hw, ICE_DBG_NVM, "Unable to read Option ROM data\n");
  588. return status;
  589. }
  590. /* Scan the memory buffer to locate the CIVD data section */
  591. for (offset = 0; (offset + 512) <= hw->flash.banks.orom_size; offset += 512) {
  592. struct ice_orom_civd_info *tmp;
  593. u8 sum = 0, i;
  594. tmp = (struct ice_orom_civd_info *)&orom_data[offset];
  595. /* Skip forward until we find a matching signature */
  596. if (memcmp("$CIV", tmp->signature, sizeof(tmp->signature)) != 0)
  597. continue;
  598. ice_debug(hw, ICE_DBG_NVM, "Found CIVD section at offset %u\n",
  599. offset);
  600. /* Verify that the simple checksum is zero */
  601. for (i = 0; i < sizeof(*tmp); i++)
  602. sum += ((u8 *)tmp)[i];
  603. if (sum) {
  604. ice_debug(hw, ICE_DBG_NVM, "Found CIVD data with invalid checksum of %u\n",
  605. sum);
  606. goto err_invalid_checksum;
  607. }
  608. *civd = *tmp;
  609. vfree(orom_data);
  610. return 0;
  611. }
  612. ice_debug(hw, ICE_DBG_NVM, "Unable to locate CIVD data within the Option ROM\n");
  613. err_invalid_checksum:
  614. vfree(orom_data);
  615. return -EIO;
  616. }
  617. /**
  618. * ice_get_orom_ver_info - Read Option ROM version information
  619. * @hw: pointer to the HW struct
  620. * @bank: whether to read from the active or inactive flash module
  621. * @orom: pointer to Option ROM info structure
  622. *
  623. * Read Option ROM version and security revision from the Option ROM flash
  624. * section.
  625. */
  626. static int
  627. ice_get_orom_ver_info(struct ice_hw *hw, enum ice_bank_select bank, struct ice_orom_info *orom)
  628. {
  629. struct ice_orom_civd_info civd;
  630. u32 combo_ver;
  631. int status;
  632. status = ice_get_orom_civd_data(hw, bank, &civd);
  633. if (status) {
  634. ice_debug(hw, ICE_DBG_NVM, "Failed to locate valid Option ROM CIVD data\n");
  635. return status;
  636. }
  637. combo_ver = le32_to_cpu(civd.combo_ver);
  638. orom->major = FIELD_GET(ICE_OROM_VER_MASK, combo_ver);
  639. orom->patch = FIELD_GET(ICE_OROM_VER_PATCH_MASK, combo_ver);
  640. orom->build = FIELD_GET(ICE_OROM_VER_BUILD_MASK, combo_ver);
  641. return 0;
  642. }
  643. /**
  644. * ice_get_inactive_orom_ver - Read Option ROM version from the inactive bank
  645. * @hw: pointer to the HW structure
  646. * @orom: storage for Option ROM version information
  647. *
  648. * Reads the Option ROM version and security revision data for the inactive
  649. * section of flash. Used to access version data for a pending update that has
  650. * not yet been activated.
  651. */
  652. int ice_get_inactive_orom_ver(struct ice_hw *hw, struct ice_orom_info *orom)
  653. {
  654. return ice_get_orom_ver_info(hw, ICE_INACTIVE_FLASH_BANK, orom);
  655. }
  656. /**
  657. * ice_get_netlist_info
  658. * @hw: pointer to the HW struct
  659. * @bank: whether to read from the active or inactive flash bank
  660. * @netlist: pointer to netlist version info structure
  661. *
  662. * Get the netlist version information from the requested bank. Reads the Link
  663. * Topology section to find the Netlist ID block and extract the relevant
  664. * information into the netlist version structure.
  665. */
  666. static int
  667. ice_get_netlist_info(struct ice_hw *hw, enum ice_bank_select bank,
  668. struct ice_netlist_info *netlist)
  669. {
  670. u16 module_id, length, node_count, i;
  671. u16 *id_blk;
  672. int status;
  673. status = ice_read_netlist_module(hw, bank, ICE_NETLIST_TYPE_OFFSET, &module_id);
  674. if (status)
  675. return status;
  676. if (module_id != ICE_NETLIST_LINK_TOPO_MOD_ID) {
  677. ice_debug(hw, ICE_DBG_NVM, "Expected netlist module_id ID of 0x%04x, but got 0x%04x\n",
  678. ICE_NETLIST_LINK_TOPO_MOD_ID, module_id);
  679. return -EIO;
  680. }
  681. status = ice_read_netlist_module(hw, bank, ICE_LINK_TOPO_MODULE_LEN, &length);
  682. if (status)
  683. return status;
  684. /* sanity check that we have at least enough words to store the netlist ID block */
  685. if (length < ICE_NETLIST_ID_BLK_SIZE) {
  686. ice_debug(hw, ICE_DBG_NVM, "Netlist Link Topology module too small. Expected at least %u words, but got %u words.\n",
  687. ICE_NETLIST_ID_BLK_SIZE, length);
  688. return -EIO;
  689. }
  690. status = ice_read_netlist_module(hw, bank, ICE_LINK_TOPO_NODE_COUNT, &node_count);
  691. if (status)
  692. return status;
  693. node_count &= ICE_LINK_TOPO_NODE_COUNT_M;
  694. id_blk = kcalloc(ICE_NETLIST_ID_BLK_SIZE, sizeof(*id_blk), GFP_KERNEL);
  695. if (!id_blk)
  696. return -ENOMEM;
  697. /* Read out the entire Netlist ID Block at once. */
  698. status = ice_read_flash_module(hw, bank, ICE_SR_NETLIST_BANK_PTR,
  699. ICE_NETLIST_ID_BLK_OFFSET(node_count) * sizeof(u16),
  700. (u8 *)id_blk, ICE_NETLIST_ID_BLK_SIZE * sizeof(u16));
  701. if (status)
  702. goto exit_error;
  703. for (i = 0; i < ICE_NETLIST_ID_BLK_SIZE; i++)
  704. id_blk[i] = le16_to_cpu(((__force __le16 *)id_blk)[i]);
  705. netlist->major = id_blk[ICE_NETLIST_ID_BLK_MAJOR_VER_HIGH] << 16 |
  706. id_blk[ICE_NETLIST_ID_BLK_MAJOR_VER_LOW];
  707. netlist->minor = id_blk[ICE_NETLIST_ID_BLK_MINOR_VER_HIGH] << 16 |
  708. id_blk[ICE_NETLIST_ID_BLK_MINOR_VER_LOW];
  709. netlist->type = id_blk[ICE_NETLIST_ID_BLK_TYPE_HIGH] << 16 |
  710. id_blk[ICE_NETLIST_ID_BLK_TYPE_LOW];
  711. netlist->rev = id_blk[ICE_NETLIST_ID_BLK_REV_HIGH] << 16 |
  712. id_blk[ICE_NETLIST_ID_BLK_REV_LOW];
  713. netlist->cust_ver = id_blk[ICE_NETLIST_ID_BLK_CUST_VER];
  714. /* Read the left most 4 bytes of SHA */
  715. netlist->hash = id_blk[ICE_NETLIST_ID_BLK_SHA_HASH_WORD(15)] << 16 |
  716. id_blk[ICE_NETLIST_ID_BLK_SHA_HASH_WORD(14)];
  717. exit_error:
  718. kfree(id_blk);
  719. return status;
  720. }
  721. /**
  722. * ice_get_inactive_netlist_ver
  723. * @hw: pointer to the HW struct
  724. * @netlist: pointer to netlist version info structure
  725. *
  726. * Read the netlist version data from the inactive netlist bank. Used to
  727. * extract version data of a pending flash update in order to display the
  728. * version data.
  729. */
  730. int ice_get_inactive_netlist_ver(struct ice_hw *hw, struct ice_netlist_info *netlist)
  731. {
  732. return ice_get_netlist_info(hw, ICE_INACTIVE_FLASH_BANK, netlist);
  733. }
  734. /**
  735. * ice_discover_flash_size - Discover the available flash size.
  736. * @hw: pointer to the HW struct
  737. *
  738. * The device flash could be up to 16MB in size. However, it is possible that
  739. * the actual size is smaller. Use bisection to determine the accessible size
  740. * of flash memory.
  741. */
  742. static int ice_discover_flash_size(struct ice_hw *hw)
  743. {
  744. u32 min_size = 0, max_size = ICE_AQC_NVM_MAX_OFFSET + 1;
  745. int status;
  746. status = ice_acquire_nvm(hw, ICE_RES_READ);
  747. if (status)
  748. return status;
  749. while ((max_size - min_size) > 1) {
  750. u32 offset = (max_size + min_size) / 2;
  751. u32 len = 1;
  752. u8 data;
  753. status = ice_read_flat_nvm(hw, offset, &len, &data, false);
  754. if (status == -EIO &&
  755. hw->adminq.sq_last_status == ICE_AQ_RC_EINVAL) {
  756. ice_debug(hw, ICE_DBG_NVM, "%s: New upper bound of %u bytes\n",
  757. __func__, offset);
  758. status = 0;
  759. max_size = offset;
  760. } else if (!status) {
  761. ice_debug(hw, ICE_DBG_NVM, "%s: New lower bound of %u bytes\n",
  762. __func__, offset);
  763. min_size = offset;
  764. } else {
  765. /* an unexpected error occurred */
  766. goto err_read_flat_nvm;
  767. }
  768. }
  769. ice_debug(hw, ICE_DBG_NVM, "Predicted flash size is %u bytes\n", max_size);
  770. hw->flash.flash_size = max_size;
  771. err_read_flat_nvm:
  772. ice_release_nvm(hw);
  773. return status;
  774. }
  775. /**
  776. * ice_read_sr_pointer - Read the value of a Shadow RAM pointer word
  777. * @hw: pointer to the HW structure
  778. * @offset: the word offset of the Shadow RAM word to read
  779. * @pointer: pointer value read from Shadow RAM
  780. *
  781. * Read the given Shadow RAM word, and convert it to a pointer value specified
  782. * in bytes. This function assumes the specified offset is a valid pointer
  783. * word.
  784. *
  785. * Each pointer word specifies whether it is stored in word size or 4KB
  786. * sector size by using the highest bit. The reported pointer value will be in
  787. * bytes, intended for flat NVM reads.
  788. */
  789. static int ice_read_sr_pointer(struct ice_hw *hw, u16 offset, u32 *pointer)
  790. {
  791. int status;
  792. u16 value;
  793. status = ice_read_sr_word(hw, offset, &value);
  794. if (status)
  795. return status;
  796. /* Determine if the pointer is in 4KB or word units */
  797. if (value & ICE_SR_NVM_PTR_4KB_UNITS)
  798. *pointer = (value & ~ICE_SR_NVM_PTR_4KB_UNITS) * 4 * 1024;
  799. else
  800. *pointer = value * 2;
  801. return 0;
  802. }
  803. /**
  804. * ice_read_sr_area_size - Read an area size from a Shadow RAM word
  805. * @hw: pointer to the HW structure
  806. * @offset: the word offset of the Shadow RAM to read
  807. * @size: size value read from the Shadow RAM
  808. *
  809. * Read the given Shadow RAM word, and convert it to an area size value
  810. * specified in bytes. This function assumes the specified offset is a valid
  811. * area size word.
  812. *
  813. * Each area size word is specified in 4KB sector units. This function reports
  814. * the size in bytes, intended for flat NVM reads.
  815. */
  816. static int ice_read_sr_area_size(struct ice_hw *hw, u16 offset, u32 *size)
  817. {
  818. int status;
  819. u16 value;
  820. status = ice_read_sr_word(hw, offset, &value);
  821. if (status)
  822. return status;
  823. /* Area sizes are always specified in 4KB units */
  824. *size = value * 4 * 1024;
  825. return 0;
  826. }
  827. /**
  828. * ice_determine_active_flash_banks - Discover active bank for each module
  829. * @hw: pointer to the HW struct
  830. *
  831. * Read the Shadow RAM control word and determine which banks are active for
  832. * the NVM, OROM, and Netlist modules. Also read and calculate the associated
  833. * pointer and size. These values are then cached into the ice_flash_info
  834. * structure for later use in order to calculate the correct offset to read
  835. * from the active module.
  836. */
  837. static int ice_determine_active_flash_banks(struct ice_hw *hw)
  838. {
  839. struct ice_bank_info *banks = &hw->flash.banks;
  840. u16 ctrl_word;
  841. int status;
  842. status = ice_read_sr_word(hw, ICE_SR_NVM_CTRL_WORD, &ctrl_word);
  843. if (status) {
  844. ice_debug(hw, ICE_DBG_NVM, "Failed to read the Shadow RAM control word\n");
  845. return status;
  846. }
  847. /* Check that the control word indicates validity */
  848. if (FIELD_GET(ICE_SR_CTRL_WORD_1_M, ctrl_word) !=
  849. ICE_SR_CTRL_WORD_VALID) {
  850. ice_debug(hw, ICE_DBG_NVM, "Shadow RAM control word is invalid\n");
  851. return -EIO;
  852. }
  853. if (!(ctrl_word & ICE_SR_CTRL_WORD_NVM_BANK))
  854. banks->nvm_bank = ICE_1ST_FLASH_BANK;
  855. else
  856. banks->nvm_bank = ICE_2ND_FLASH_BANK;
  857. if (!(ctrl_word & ICE_SR_CTRL_WORD_OROM_BANK))
  858. banks->orom_bank = ICE_1ST_FLASH_BANK;
  859. else
  860. banks->orom_bank = ICE_2ND_FLASH_BANK;
  861. if (!(ctrl_word & ICE_SR_CTRL_WORD_NETLIST_BANK))
  862. banks->netlist_bank = ICE_1ST_FLASH_BANK;
  863. else
  864. banks->netlist_bank = ICE_2ND_FLASH_BANK;
  865. status = ice_read_sr_pointer(hw, ICE_SR_1ST_NVM_BANK_PTR, &banks->nvm_ptr);
  866. if (status) {
  867. ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank pointer\n");
  868. return status;
  869. }
  870. status = ice_read_sr_area_size(hw, ICE_SR_NVM_BANK_SIZE, &banks->nvm_size);
  871. if (status) {
  872. ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank area size\n");
  873. return status;
  874. }
  875. status = ice_read_sr_pointer(hw, ICE_SR_1ST_OROM_BANK_PTR, &banks->orom_ptr);
  876. if (status) {
  877. ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank pointer\n");
  878. return status;
  879. }
  880. status = ice_read_sr_area_size(hw, ICE_SR_OROM_BANK_SIZE, &banks->orom_size);
  881. if (status) {
  882. ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank area size\n");
  883. return status;
  884. }
  885. status = ice_read_sr_pointer(hw, ICE_SR_NETLIST_BANK_PTR, &banks->netlist_ptr);
  886. if (status) {
  887. ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank pointer\n");
  888. return status;
  889. }
  890. status = ice_read_sr_area_size(hw, ICE_SR_NETLIST_BANK_SIZE, &banks->netlist_size);
  891. if (status) {
  892. ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank area size\n");
  893. return status;
  894. }
  895. return 0;
  896. }
  897. /**
  898. * ice_get_nvm_css_hdr_len - Read the CSS header length from the NVM CSS header
  899. * @hw: pointer to the HW struct
  900. * @bank: whether to read from the active or inactive flash bank
  901. * @hdr_len: storage for header length in words
  902. *
  903. * Read the CSS header length from the NVM CSS header and add the Authentication
  904. * header size, and then convert to words.
  905. *
  906. * Return: zero on success, or a negative error code on failure.
  907. */
  908. static int
  909. ice_get_nvm_css_hdr_len(struct ice_hw *hw, enum ice_bank_select bank,
  910. u32 *hdr_len)
  911. {
  912. u16 hdr_len_l, hdr_len_h;
  913. u32 hdr_len_dword;
  914. int status;
  915. status = ice_read_nvm_module(hw, bank, ICE_NVM_CSS_HDR_LEN_L,
  916. &hdr_len_l);
  917. if (status)
  918. return status;
  919. status = ice_read_nvm_module(hw, bank, ICE_NVM_CSS_HDR_LEN_H,
  920. &hdr_len_h);
  921. if (status)
  922. return status;
  923. /* CSS header length is in DWORD, so convert to words and add
  924. * authentication header size
  925. */
  926. hdr_len_dword = hdr_len_h << 16 | hdr_len_l;
  927. *hdr_len = (hdr_len_dword * 2) + ICE_NVM_AUTH_HEADER_LEN;
  928. return 0;
  929. }
  930. /**
  931. * ice_determine_css_hdr_len - Discover CSS header length for the device
  932. * @hw: pointer to the HW struct
  933. *
  934. * Determine the size of the CSS header at the start of the NVM module. This
  935. * is useful for locating the Shadow RAM copy in the NVM, as the Shadow RAM is
  936. * always located just after the CSS header.
  937. *
  938. * Return: zero on success, or a negative error code on failure.
  939. */
  940. static int ice_determine_css_hdr_len(struct ice_hw *hw)
  941. {
  942. struct ice_bank_info *banks = &hw->flash.banks;
  943. int status;
  944. status = ice_get_nvm_css_hdr_len(hw, ICE_ACTIVE_FLASH_BANK,
  945. &banks->active_css_hdr_len);
  946. if (status)
  947. return status;
  948. status = ice_get_nvm_css_hdr_len(hw, ICE_INACTIVE_FLASH_BANK,
  949. &banks->inactive_css_hdr_len);
  950. if (status)
  951. return status;
  952. return 0;
  953. }
  954. /**
  955. * ice_init_nvm - initializes NVM setting
  956. * @hw: pointer to the HW struct
  957. *
  958. * This function reads and populates NVM settings such as Shadow RAM size,
  959. * max_timeout, and blank_nvm_mode
  960. */
  961. int ice_init_nvm(struct ice_hw *hw)
  962. {
  963. struct ice_flash_info *flash = &hw->flash;
  964. u32 fla, gens_stat;
  965. u8 sr_size;
  966. int status;
  967. /* The SR size is stored regardless of the NVM programming mode
  968. * as the blank mode may be used in the factory line.
  969. */
  970. gens_stat = rd32(hw, GLNVM_GENS);
  971. sr_size = FIELD_GET(GLNVM_GENS_SR_SIZE_M, gens_stat);
  972. /* Switching to words (sr_size contains power of 2) */
  973. flash->sr_words = BIT(sr_size) * ICE_SR_WORDS_IN_1KB;
  974. /* Check if we are in the normal or blank NVM programming mode */
  975. fla = rd32(hw, GLNVM_FLA);
  976. if (fla & GLNVM_FLA_LOCKED_M) { /* Normal programming mode */
  977. flash->blank_nvm_mode = false;
  978. } else {
  979. /* Blank programming mode */
  980. flash->blank_nvm_mode = true;
  981. ice_debug(hw, ICE_DBG_NVM, "NVM init error: unsupported blank mode.\n");
  982. return -EIO;
  983. }
  984. status = ice_discover_flash_size(hw);
  985. if (status) {
  986. ice_debug(hw, ICE_DBG_NVM, "NVM init error: failed to discover flash size.\n");
  987. return status;
  988. }
  989. status = ice_determine_active_flash_banks(hw);
  990. if (status) {
  991. ice_debug(hw, ICE_DBG_NVM, "Failed to determine active flash banks.\n");
  992. return status;
  993. }
  994. status = ice_determine_css_hdr_len(hw);
  995. if (status) {
  996. ice_debug(hw, ICE_DBG_NVM, "Failed to determine Shadow RAM copy offsets.\n");
  997. return status;
  998. }
  999. status = ice_get_nvm_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->nvm);
  1000. if (status) {
  1001. ice_debug(hw, ICE_DBG_INIT, "Failed to read NVM info.\n");
  1002. return status;
  1003. }
  1004. status = ice_get_orom_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->orom);
  1005. if (status)
  1006. ice_debug(hw, ICE_DBG_INIT, "Failed to read Option ROM info.\n");
  1007. /* read the netlist version information */
  1008. status = ice_get_netlist_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->netlist);
  1009. if (status)
  1010. ice_debug(hw, ICE_DBG_INIT, "Failed to read netlist info.\n");
  1011. return 0;
  1012. }
  1013. /**
  1014. * ice_nvm_validate_checksum
  1015. * @hw: pointer to the HW struct
  1016. *
  1017. * Verify NVM PFA checksum validity (0x0706)
  1018. */
  1019. int ice_nvm_validate_checksum(struct ice_hw *hw)
  1020. {
  1021. struct ice_aqc_nvm_checksum *cmd;
  1022. struct ice_aq_desc desc;
  1023. int status;
  1024. status = ice_acquire_nvm(hw, ICE_RES_READ);
  1025. if (status)
  1026. return status;
  1027. cmd = &desc.params.nvm_checksum;
  1028. ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
  1029. cmd->flags = ICE_AQC_NVM_CHECKSUM_VERIFY;
  1030. status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
  1031. ice_release_nvm(hw);
  1032. if (!status)
  1033. if (le16_to_cpu(cmd->checksum) != ICE_AQC_NVM_CHECKSUM_CORRECT)
  1034. status = -EIO;
  1035. return status;
  1036. }
  1037. /**
  1038. * ice_nvm_write_activate
  1039. * @hw: pointer to the HW struct
  1040. * @cmd_flags: flags for write activate command
  1041. * @response_flags: response indicators from firmware
  1042. *
  1043. * Update the control word with the required banks' validity bits
  1044. * and dumps the Shadow RAM to flash (0x0707)
  1045. *
  1046. * cmd_flags controls which banks to activate, the preservation level to use
  1047. * when activating the NVM bank, and whether an EMP reset is required for
  1048. * activation.
  1049. *
  1050. * Note that the 16bit cmd_flags value is split between two separate 1 byte
  1051. * flag values in the descriptor.
  1052. *
  1053. * On successful return of the firmware command, the response_flags variable
  1054. * is updated with the flags reported by firmware indicating certain status,
  1055. * such as whether EMP reset is enabled.
  1056. */
  1057. int ice_nvm_write_activate(struct ice_hw *hw, u16 cmd_flags, u8 *response_flags)
  1058. {
  1059. struct ice_aqc_nvm *cmd;
  1060. struct ice_aq_desc desc;
  1061. int err;
  1062. cmd = &desc.params.nvm;
  1063. ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_write_activate);
  1064. cmd->cmd_flags = (u8)(cmd_flags & 0xFF);
  1065. cmd->offset_high = (u8)((cmd_flags >> 8) & 0xFF);
  1066. err = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
  1067. if (!err && response_flags)
  1068. *response_flags = cmd->cmd_flags;
  1069. return err;
  1070. }
  1071. /**
  1072. * ice_aq_nvm_update_empr
  1073. * @hw: pointer to the HW struct
  1074. *
  1075. * Update empr (0x0709). This command allows SW to
  1076. * request an EMPR to activate new FW.
  1077. */
  1078. int ice_aq_nvm_update_empr(struct ice_hw *hw)
  1079. {
  1080. struct ice_aq_desc desc;
  1081. ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_update_empr);
  1082. return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
  1083. }
  1084. /* ice_nvm_set_pkg_data
  1085. * @hw: pointer to the HW struct
  1086. * @del_pkg_data_flag: If is set then the current pkg_data store by FW
  1087. * is deleted.
  1088. * If bit is set to 1, then buffer should be size 0.
  1089. * @data: pointer to buffer
  1090. * @length: length of the buffer
  1091. * @cd: pointer to command details structure or NULL
  1092. *
  1093. * Set package data (0x070A). This command is equivalent to the reception
  1094. * of a PLDM FW Update GetPackageData cmd. This command should be sent
  1095. * as part of the NVM update as the first cmd in the flow.
  1096. */
  1097. int
  1098. ice_nvm_set_pkg_data(struct ice_hw *hw, bool del_pkg_data_flag, u8 *data,
  1099. u16 length, struct ice_sq_cd *cd)
  1100. {
  1101. struct ice_aqc_nvm_pkg_data *cmd;
  1102. struct ice_aq_desc desc;
  1103. if (length != 0 && !data)
  1104. return -EINVAL;
  1105. cmd = &desc.params.pkg_data;
  1106. ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_pkg_data);
  1107. desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
  1108. if (del_pkg_data_flag)
  1109. cmd->cmd_flags |= ICE_AQC_NVM_PKG_DELETE;
  1110. return ice_aq_send_cmd(hw, &desc, data, length, cd);
  1111. }
  1112. /* ice_nvm_pass_component_tbl
  1113. * @hw: pointer to the HW struct
  1114. * @data: pointer to buffer
  1115. * @length: length of the buffer
  1116. * @transfer_flag: parameter for determining stage of the update
  1117. * @comp_response: a pointer to the response from the 0x070B AQC.
  1118. * @comp_response_code: a pointer to the response code from the 0x070B AQC.
  1119. * @cd: pointer to command details structure or NULL
  1120. *
  1121. * Pass component table (0x070B). This command is equivalent to the reception
  1122. * of a PLDM FW Update PassComponentTable cmd. This command should be sent once
  1123. * per component. It can be only sent after Set Package Data cmd and before
  1124. * actual update. FW will assume these commands are going to be sent until
  1125. * the TransferFlag is set to End or StartAndEnd.
  1126. */
  1127. int
  1128. ice_nvm_pass_component_tbl(struct ice_hw *hw, u8 *data, u16 length,
  1129. u8 transfer_flag, u8 *comp_response,
  1130. u8 *comp_response_code, struct ice_sq_cd *cd)
  1131. {
  1132. struct ice_aqc_nvm_pass_comp_tbl *cmd;
  1133. struct ice_aq_desc desc;
  1134. int status;
  1135. if (!data || !comp_response || !comp_response_code)
  1136. return -EINVAL;
  1137. cmd = &desc.params.pass_comp_tbl;
  1138. ice_fill_dflt_direct_cmd_desc(&desc,
  1139. ice_aqc_opc_nvm_pass_component_tbl);
  1140. desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
  1141. cmd->transfer_flag = transfer_flag;
  1142. status = ice_aq_send_cmd(hw, &desc, data, length, cd);
  1143. if (!status) {
  1144. *comp_response = cmd->component_response;
  1145. *comp_response_code = cmd->component_response_code;
  1146. }
  1147. return status;
  1148. }