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