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