2 * Copyright (c) 2009-2016 Solarflare Communications Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * The views and conclusions contained in the software and documentation are
27 * those of the authors and should not be interpreted as representing official
28 * policies, either expressed or implied, of the FreeBSD Project.
36 #if EFSYS_OPT_VPD || EFSYS_OPT_NVRAM
38 __checkReturn efx_rc_t
39 siena_nvram_partn_size(
46 if ((1 << partn) & ~enp->en_u.siena.enu_partn_mask) {
51 if ((rc = efx_mcdi_nvram_info(enp, partn, sizep,
52 NULL, NULL, NULL)) != 0) {
61 EFSYS_PROBE1(fail1, efx_rc_t, rc);
66 __checkReturn efx_rc_t
67 siena_nvram_partn_lock(
73 if ((rc = efx_mcdi_nvram_update_start(enp, partn)) != 0) {
80 EFSYS_PROBE1(fail1, efx_rc_t, rc);
85 __checkReturn efx_rc_t
86 siena_nvram_partn_read(
89 __in unsigned int offset,
90 __out_bcount(size) caddr_t data,
97 chunk = MIN(size, SIENA_NVRAM_CHUNK);
99 if ((rc = efx_mcdi_nvram_read(enp, partn, offset, data, chunk,
100 MC_CMD_NVRAM_READ_IN_V2_DEFAULT)) != 0) {
112 EFSYS_PROBE1(fail1, efx_rc_t, rc);
117 __checkReturn efx_rc_t
118 siena_nvram_partn_erase(
121 __in unsigned int offset,
126 if ((rc = efx_mcdi_nvram_erase(enp, partn, offset, size)) != 0) {
133 EFSYS_PROBE1(fail1, efx_rc_t, rc);
138 __checkReturn efx_rc_t
139 siena_nvram_partn_write(
142 __in unsigned int offset,
143 __out_bcount(size) caddr_t data,
150 chunk = MIN(size, SIENA_NVRAM_CHUNK);
152 if ((rc = efx_mcdi_nvram_write(enp, partn, offset,
153 data, chunk)) != 0) {
165 EFSYS_PROBE1(fail1, efx_rc_t, rc);
170 __checkReturn efx_rc_t
171 siena_nvram_partn_unlock(
174 __out_opt uint32_t *verify_resultp)
180 * Reboot into the new image only for PHYs. The driver has to
181 * explicitly cope with an MC reboot after a firmware update.
183 reboot = (partn == MC_CMD_NVRAM_TYPE_PHY_PORT0 ||
184 partn == MC_CMD_NVRAM_TYPE_PHY_PORT1 ||
185 partn == MC_CMD_NVRAM_TYPE_DISABLED_CALLISTO);
187 rc = efx_mcdi_nvram_update_finish(enp, partn, reboot, verify_resultp);
194 EFSYS_PROBE1(fail1, efx_rc_t, rc);
199 #endif /* EFSYS_OPT_VPD || EFSYS_OPT_NVRAM */
203 typedef struct siena_parttbl_entry_s {
206 efx_nvram_type_t nvtype;
207 } siena_parttbl_entry_t;
209 static siena_parttbl_entry_t siena_parttbl[] = {
210 {MC_CMD_NVRAM_TYPE_DISABLED_CALLISTO, 1, EFX_NVRAM_NULLPHY},
211 {MC_CMD_NVRAM_TYPE_DISABLED_CALLISTO, 2, EFX_NVRAM_NULLPHY},
212 {MC_CMD_NVRAM_TYPE_MC_FW, 1, EFX_NVRAM_MC_FIRMWARE},
213 {MC_CMD_NVRAM_TYPE_MC_FW, 2, EFX_NVRAM_MC_FIRMWARE},
214 {MC_CMD_NVRAM_TYPE_MC_FW_BACKUP, 1, EFX_NVRAM_MC_GOLDEN},
215 {MC_CMD_NVRAM_TYPE_MC_FW_BACKUP, 2, EFX_NVRAM_MC_GOLDEN},
216 {MC_CMD_NVRAM_TYPE_EXP_ROM, 1, EFX_NVRAM_BOOTROM},
217 {MC_CMD_NVRAM_TYPE_EXP_ROM, 2, EFX_NVRAM_BOOTROM},
218 {MC_CMD_NVRAM_TYPE_EXP_ROM_CFG_PORT0, 1, EFX_NVRAM_BOOTROM_CFG},
219 {MC_CMD_NVRAM_TYPE_EXP_ROM_CFG_PORT1, 2, EFX_NVRAM_BOOTROM_CFG},
220 {MC_CMD_NVRAM_TYPE_PHY_PORT0, 1, EFX_NVRAM_PHY},
221 {MC_CMD_NVRAM_TYPE_PHY_PORT1, 2, EFX_NVRAM_PHY},
222 {MC_CMD_NVRAM_TYPE_FPGA, 1, EFX_NVRAM_FPGA},
223 {MC_CMD_NVRAM_TYPE_FPGA, 2, EFX_NVRAM_FPGA},
224 {MC_CMD_NVRAM_TYPE_FPGA_BACKUP, 1, EFX_NVRAM_FPGA_BACKUP},
225 {MC_CMD_NVRAM_TYPE_FPGA_BACKUP, 2, EFX_NVRAM_FPGA_BACKUP},
226 {MC_CMD_NVRAM_TYPE_FC_FW, 1, EFX_NVRAM_FCFW},
227 {MC_CMD_NVRAM_TYPE_FC_FW, 2, EFX_NVRAM_FCFW},
228 {MC_CMD_NVRAM_TYPE_CPLD, 1, EFX_NVRAM_CPLD},
229 {MC_CMD_NVRAM_TYPE_CPLD, 2, EFX_NVRAM_CPLD},
230 {MC_CMD_NVRAM_TYPE_LICENSE, 1, EFX_NVRAM_LICENSE},
231 {MC_CMD_NVRAM_TYPE_LICENSE, 2, EFX_NVRAM_LICENSE}
234 __checkReturn efx_rc_t
235 siena_nvram_type_to_partn(
237 __in efx_nvram_type_t type,
238 __out uint32_t *partnp)
240 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
243 EFSYS_ASSERT3U(type, !=, EFX_NVRAM_INVALID);
244 EFSYS_ASSERT3U(type, <, EFX_NVRAM_NTYPES);
245 EFSYS_ASSERT(partnp != NULL);
247 for (i = 0; i < EFX_ARRAY_SIZE(siena_parttbl); i++) {
248 siena_parttbl_entry_t *entry = &siena_parttbl[i];
250 if (entry->port == emip->emi_port && entry->nvtype == type) {
251 *partnp = entry->partn;
262 __checkReturn efx_rc_t
266 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
267 siena_parttbl_entry_t *entry;
272 * Iterate over the list of supported partition types
273 * applicable to *this* port
275 for (i = 0; i < EFX_ARRAY_SIZE(siena_parttbl); i++) {
276 entry = &siena_parttbl[i];
278 if (entry->port != emip->emi_port ||
279 !(enp->en_u.siena.enu_partn_mask & (1 << entry->partn)))
282 if ((rc = efx_mcdi_nvram_test(enp, entry->partn)) != 0) {
290 EFSYS_PROBE1(fail1, efx_rc_t, rc);
295 #endif /* EFSYS_OPT_DIAG */
298 #define SIENA_DYNAMIC_CFG_SIZE(_nitems) \
299 (sizeof (siena_mc_dynamic_config_hdr_t) + ((_nitems) * \
300 sizeof (((siena_mc_dynamic_config_hdr_t *)NULL)->fw_version[0])))
302 __checkReturn efx_rc_t
303 siena_nvram_get_dynamic_cfg(
307 __out siena_mc_dynamic_config_hdr_t **dcfgp,
310 siena_mc_dynamic_config_hdr_t *dcfg = NULL;
313 unsigned int vpd_offset;
314 unsigned int vpd_length;
315 unsigned int hdr_length;
316 unsigned int nversions;
321 EFSYS_ASSERT(partn == MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0 ||
322 partn == MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1);
325 * Allocate sufficient memory for the entire dynamiccfg area, even
326 * if we're not actually going to read in the VPD.
328 if ((rc = siena_nvram_partn_size(enp, partn, &size)) != 0)
331 EFSYS_KMEM_ALLOC(enp->en_esip, size, dcfg);
337 if ((rc = siena_nvram_partn_read(enp, partn, 0,
338 (caddr_t)dcfg, SIENA_NVRAM_CHUNK)) != 0)
341 /* Verify the magic */
342 if (EFX_DWORD_FIELD(dcfg->magic, EFX_DWORD_0)
343 != SIENA_MC_DYNAMIC_CONFIG_MAGIC)
346 /* All future versions of the structure must be backwards compatible */
347 EFX_STATIC_ASSERT(SIENA_MC_DYNAMIC_CONFIG_VERSION == 0);
349 hdr_length = EFX_WORD_FIELD(dcfg->length, EFX_WORD_0);
350 nversions = EFX_DWORD_FIELD(dcfg->num_fw_version_items, EFX_DWORD_0);
351 vpd_offset = EFX_DWORD_FIELD(dcfg->dynamic_vpd_offset, EFX_DWORD_0);
352 vpd_length = EFX_DWORD_FIELD(dcfg->dynamic_vpd_length, EFX_DWORD_0);
354 /* Verify the hdr doesn't overflow the partn size */
355 if (hdr_length > size || vpd_offset > size || vpd_length > size ||
356 vpd_length + vpd_offset > size)
359 /* Verify the header has room for all it's versions */
360 if (hdr_length < SIENA_DYNAMIC_CFG_SIZE(0) ||
361 hdr_length < SIENA_DYNAMIC_CFG_SIZE(nversions))
365 * Read the remaining portion of the dcfg, either including
366 * the whole of VPD (there is no vpd length in this structure,
367 * so we have to parse each tag), or just the dcfg header itself
369 region = vpd ? vpd_offset + vpd_length : hdr_length;
370 if (region > SIENA_NVRAM_CHUNK) {
371 if ((rc = siena_nvram_partn_read(enp, partn, SIENA_NVRAM_CHUNK,
372 (caddr_t)dcfg + SIENA_NVRAM_CHUNK,
373 region - SIENA_NVRAM_CHUNK)) != 0)
377 /* Verify checksum */
379 for (pos = 0; pos < hdr_length; pos++)
380 cksum += ((uint8_t *)dcfg)[pos];
387 EFSYS_PROBE(invalid4);
389 EFSYS_PROBE(invalid3);
391 EFSYS_PROBE(invalid2);
393 EFSYS_PROBE(invalid1);
396 * Construct a new "null" dcfg, with an empty version vector,
397 * and an empty VPD chunk trailing. This has the neat side effect
398 * of testing the exception paths in the write path.
400 EFX_POPULATE_DWORD_1(dcfg->magic,
401 EFX_DWORD_0, SIENA_MC_DYNAMIC_CONFIG_MAGIC);
402 EFX_POPULATE_WORD_1(dcfg->length, EFX_WORD_0, sizeof (*dcfg));
403 EFX_POPULATE_BYTE_1(dcfg->version, EFX_BYTE_0,
404 SIENA_MC_DYNAMIC_CONFIG_VERSION);
405 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_offset,
406 EFX_DWORD_0, sizeof (*dcfg));
407 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_length, EFX_DWORD_0, 0);
408 EFX_POPULATE_DWORD_1(dcfg->num_fw_version_items, EFX_DWORD_0, 0);
421 EFSYS_KMEM_FREE(enp->en_esip, size, dcfg);
426 EFSYS_PROBE1(fail1, efx_rc_t, rc);
431 __checkReturn efx_rc_t
432 siena_nvram_get_subtype(
435 __out uint32_t *subtypep)
438 uint8_t payload[MAX(MC_CMD_GET_BOARD_CFG_IN_LEN,
439 MC_CMD_GET_BOARD_CFG_OUT_LENMAX)];
443 (void) memset(payload, 0, sizeof (payload));
444 req.emr_cmd = MC_CMD_GET_BOARD_CFG;
445 req.emr_in_buf = payload;
446 req.emr_in_length = MC_CMD_GET_BOARD_CFG_IN_LEN;
447 req.emr_out_buf = payload;
448 req.emr_out_length = MC_CMD_GET_BOARD_CFG_OUT_LENMAX;
450 efx_mcdi_execute(enp, &req);
452 if (req.emr_rc != 0) {
457 if (req.emr_out_length_used < MC_CMD_GET_BOARD_CFG_OUT_LENMIN) {
462 if (req.emr_out_length_used <
463 MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_OFST +
464 (partn + 1) * sizeof (efx_word_t)) {
469 fw_list = MCDI_OUT2(req, efx_word_t,
470 GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST);
471 *subtypep = EFX_WORD_FIELD(fw_list[partn], EFX_WORD_0);
480 EFSYS_PROBE1(fail1, efx_rc_t, rc);
485 __checkReturn efx_rc_t
486 siena_nvram_partn_get_version(
489 __out uint32_t *subtypep,
490 __out_ecount(4) uint16_t version[4])
492 siena_mc_dynamic_config_hdr_t *dcfg;
493 siena_parttbl_entry_t *entry;
498 if ((1 << partn) & ~enp->en_u.siena.enu_partn_mask) {
503 if ((rc = siena_nvram_get_subtype(enp, partn, subtypep)) != 0)
507 * Some partitions are accessible from both ports (for instance BOOTROM)
508 * Find the highest version reported by all dcfg structures on ports
509 * that have access to this partition.
511 version[0] = version[1] = version[2] = version[3] = 0;
512 for (i = 0; i < EFX_ARRAY_SIZE(siena_parttbl); i++) {
513 siena_mc_fw_version_t *verp;
518 entry = &siena_parttbl[i];
519 if (entry->partn != partn)
522 dcfg_partn = (entry->port == 1)
523 ? MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0
524 : MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1;
526 * Ingore missing partitions on port 2, assuming they're due
527 * to to running on a single port part.
529 if ((1 << dcfg_partn) & ~enp->en_u.siena.enu_partn_mask) {
530 if (entry->port == 2)
534 if ((rc = siena_nvram_get_dynamic_cfg(enp, dcfg_partn,
535 B_FALSE, &dcfg, &length)) != 0)
538 nitems = EFX_DWORD_FIELD(dcfg->num_fw_version_items,
540 if (nitems < entry->partn)
543 verp = &dcfg->fw_version[partn];
544 temp[0] = EFX_WORD_FIELD(verp->version_w, EFX_WORD_0);
545 temp[1] = EFX_WORD_FIELD(verp->version_x, EFX_WORD_0);
546 temp[2] = EFX_WORD_FIELD(verp->version_y, EFX_WORD_0);
547 temp[3] = EFX_WORD_FIELD(verp->version_z, EFX_WORD_0);
548 if (memcmp(version, temp, sizeof (temp)) < 0)
549 memcpy(version, temp, sizeof (temp));
552 EFSYS_KMEM_FREE(enp->en_esip, length, dcfg);
562 EFSYS_PROBE1(fail1, efx_rc_t, rc);
567 __checkReturn efx_rc_t
568 siena_nvram_partn_rw_start(
571 __out size_t *chunk_sizep)
575 if ((rc = siena_nvram_partn_lock(enp, partn)) != 0)
578 if (chunk_sizep != NULL)
579 *chunk_sizep = SIENA_NVRAM_CHUNK;
584 EFSYS_PROBE1(fail1, efx_rc_t, rc);
589 __checkReturn efx_rc_t
590 siena_nvram_partn_rw_finish(
593 __out_opt uint32_t *verify_resultp)
597 if ((rc = siena_nvram_partn_unlock(enp, partn, verify_resultp)) != 0)
603 EFSYS_PROBE1(fail1, efx_rc_t, rc);
608 __checkReturn efx_rc_t
609 siena_nvram_partn_set_version(
612 __in_ecount(4) uint16_t version[4])
614 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
615 siena_mc_dynamic_config_hdr_t *dcfg = NULL;
616 siena_mc_fw_version_t *fwverp;
619 unsigned int hdr_length;
620 unsigned int vpd_length;
621 unsigned int vpd_offset;
623 unsigned int required_hdr_length;
630 dcfg_partn = (emip->emi_port == 1)
631 ? MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0
632 : MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1;
634 if ((rc = siena_nvram_partn_size(enp, dcfg_partn, &dcfg_size)) != 0)
637 if ((rc = siena_nvram_partn_lock(enp, dcfg_partn)) != 0)
640 if ((rc = siena_nvram_get_dynamic_cfg(enp, dcfg_partn,
641 B_TRUE, &dcfg, &length)) != 0)
644 hdr_length = EFX_WORD_FIELD(dcfg->length, EFX_WORD_0);
645 nitems = EFX_DWORD_FIELD(dcfg->num_fw_version_items, EFX_DWORD_0);
646 vpd_length = EFX_DWORD_FIELD(dcfg->dynamic_vpd_length, EFX_DWORD_0);
647 vpd_offset = EFX_DWORD_FIELD(dcfg->dynamic_vpd_offset, EFX_DWORD_0);
650 * NOTE: This function will blatt any fields trailing the version
651 * vector, or the VPD chunk.
653 required_hdr_length = SIENA_DYNAMIC_CFG_SIZE(partn + 1);
654 if (required_hdr_length + vpd_length > length) {
659 if (vpd_offset < required_hdr_length) {
660 (void) memmove((caddr_t)dcfg + required_hdr_length,
661 (caddr_t)dcfg + vpd_offset, vpd_length);
662 vpd_offset = required_hdr_length;
663 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_offset,
664 EFX_DWORD_0, vpd_offset);
667 if (hdr_length < required_hdr_length) {
668 (void) memset((caddr_t)dcfg + hdr_length, 0,
669 required_hdr_length - hdr_length);
670 hdr_length = required_hdr_length;
671 EFX_POPULATE_WORD_1(dcfg->length,
672 EFX_WORD_0, hdr_length);
675 /* Get the subtype to insert into the fw_subtype array */
676 if ((rc = siena_nvram_get_subtype(enp, partn, &subtype)) != 0)
679 /* Fill out the new version */
680 fwverp = &dcfg->fw_version[partn];
681 EFX_POPULATE_DWORD_1(fwverp->fw_subtype, EFX_DWORD_0, subtype);
682 EFX_POPULATE_WORD_1(fwverp->version_w, EFX_WORD_0, version[0]);
683 EFX_POPULATE_WORD_1(fwverp->version_x, EFX_WORD_0, version[1]);
684 EFX_POPULATE_WORD_1(fwverp->version_y, EFX_WORD_0, version[2]);
685 EFX_POPULATE_WORD_1(fwverp->version_z, EFX_WORD_0, version[3]);
687 /* Update the version count */
688 if (nitems < partn + 1) {
690 EFX_POPULATE_DWORD_1(dcfg->num_fw_version_items,
691 EFX_DWORD_0, nitems);
694 /* Update the checksum */
696 for (pos = 0; pos < hdr_length; pos++)
697 cksum += ((uint8_t *)dcfg)[pos];
698 dcfg->csum.eb_u8[0] -= cksum;
700 /* Erase and write the new partition */
701 if ((rc = siena_nvram_partn_erase(enp, dcfg_partn, 0, dcfg_size)) != 0)
704 /* Write out the new structure to nvram */
705 if ((rc = siena_nvram_partn_write(enp, dcfg_partn, 0,
706 (caddr_t)dcfg, vpd_offset + vpd_length)) != 0)
709 EFSYS_KMEM_FREE(enp->en_esip, length, dcfg);
711 siena_nvram_partn_unlock(enp, dcfg_partn, NULL);
724 EFSYS_KMEM_FREE(enp->en_esip, length, dcfg);
730 EFSYS_PROBE1(fail1, efx_rc_t, rc);
735 #endif /* EFSYS_OPT_NVRAM */
737 #endif /* EFSYS_OPT_SIENA */