2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009-2016 Solarflare Communications Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * The views and conclusions contained in the software and documentation are
29 * those of the authors and should not be interpreted as representing official
30 * policies, either expressed or implied, of the FreeBSD Project.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
41 #if EFSYS_OPT_VPD || EFSYS_OPT_NVRAM
43 __checkReturn efx_rc_t
44 siena_nvram_partn_size(
51 if ((1 << partn) & ~enp->en_u.siena.enu_partn_mask) {
56 if ((rc = efx_mcdi_nvram_info(enp, partn, sizep,
57 NULL, NULL, NULL)) != 0) {
66 EFSYS_PROBE1(fail1, efx_rc_t, rc);
71 __checkReturn efx_rc_t
72 siena_nvram_partn_lock(
78 if ((rc = efx_mcdi_nvram_update_start(enp, partn)) != 0) {
85 EFSYS_PROBE1(fail1, efx_rc_t, rc);
90 __checkReturn efx_rc_t
91 siena_nvram_partn_read(
94 __in unsigned int offset,
95 __out_bcount(size) caddr_t data,
102 chunk = MIN(size, SIENA_NVRAM_CHUNK);
104 if ((rc = efx_mcdi_nvram_read(enp, partn, offset, data, chunk,
105 MC_CMD_NVRAM_READ_IN_V2_DEFAULT)) != 0) {
117 EFSYS_PROBE1(fail1, efx_rc_t, rc);
122 __checkReturn efx_rc_t
123 siena_nvram_partn_erase(
126 __in unsigned int offset,
131 if ((rc = efx_mcdi_nvram_erase(enp, partn, offset, size)) != 0) {
138 EFSYS_PROBE1(fail1, efx_rc_t, rc);
143 __checkReturn efx_rc_t
144 siena_nvram_partn_write(
147 __in unsigned int offset,
148 __out_bcount(size) caddr_t data,
155 chunk = MIN(size, SIENA_NVRAM_CHUNK);
157 if ((rc = efx_mcdi_nvram_write(enp, partn, offset,
158 data, chunk)) != 0) {
170 EFSYS_PROBE1(fail1, efx_rc_t, rc);
175 __checkReturn efx_rc_t
176 siena_nvram_partn_unlock(
179 __out_opt uint32_t *verify_resultp)
185 * Reboot into the new image only for PHYs. The driver has to
186 * explicitly cope with an MC reboot after a firmware update.
188 reboot = (partn == MC_CMD_NVRAM_TYPE_PHY_PORT0 ||
189 partn == MC_CMD_NVRAM_TYPE_PHY_PORT1 ||
190 partn == MC_CMD_NVRAM_TYPE_DISABLED_CALLISTO);
192 rc = efx_mcdi_nvram_update_finish(enp, partn, reboot, verify_resultp);
199 EFSYS_PROBE1(fail1, efx_rc_t, rc);
204 #endif /* EFSYS_OPT_VPD || EFSYS_OPT_NVRAM */
208 typedef struct siena_parttbl_entry_s {
211 efx_nvram_type_t nvtype;
212 } siena_parttbl_entry_t;
214 static siena_parttbl_entry_t siena_parttbl[] = {
215 {MC_CMD_NVRAM_TYPE_DISABLED_CALLISTO, 1, EFX_NVRAM_NULLPHY},
216 {MC_CMD_NVRAM_TYPE_DISABLED_CALLISTO, 2, EFX_NVRAM_NULLPHY},
217 {MC_CMD_NVRAM_TYPE_MC_FW, 1, EFX_NVRAM_MC_FIRMWARE},
218 {MC_CMD_NVRAM_TYPE_MC_FW, 2, EFX_NVRAM_MC_FIRMWARE},
219 {MC_CMD_NVRAM_TYPE_MC_FW_BACKUP, 1, EFX_NVRAM_MC_GOLDEN},
220 {MC_CMD_NVRAM_TYPE_MC_FW_BACKUP, 2, EFX_NVRAM_MC_GOLDEN},
221 {MC_CMD_NVRAM_TYPE_EXP_ROM, 1, EFX_NVRAM_BOOTROM},
222 {MC_CMD_NVRAM_TYPE_EXP_ROM, 2, EFX_NVRAM_BOOTROM},
223 {MC_CMD_NVRAM_TYPE_EXP_ROM_CFG_PORT0, 1, EFX_NVRAM_BOOTROM_CFG},
224 {MC_CMD_NVRAM_TYPE_EXP_ROM_CFG_PORT1, 2, EFX_NVRAM_BOOTROM_CFG},
225 {MC_CMD_NVRAM_TYPE_PHY_PORT0, 1, EFX_NVRAM_PHY},
226 {MC_CMD_NVRAM_TYPE_PHY_PORT1, 2, EFX_NVRAM_PHY},
227 {MC_CMD_NVRAM_TYPE_FPGA, 1, EFX_NVRAM_FPGA},
228 {MC_CMD_NVRAM_TYPE_FPGA, 2, EFX_NVRAM_FPGA},
229 {MC_CMD_NVRAM_TYPE_FPGA_BACKUP, 1, EFX_NVRAM_FPGA_BACKUP},
230 {MC_CMD_NVRAM_TYPE_FPGA_BACKUP, 2, EFX_NVRAM_FPGA_BACKUP},
231 {MC_CMD_NVRAM_TYPE_FC_FW, 1, EFX_NVRAM_FCFW},
232 {MC_CMD_NVRAM_TYPE_FC_FW, 2, EFX_NVRAM_FCFW},
233 {MC_CMD_NVRAM_TYPE_CPLD, 1, EFX_NVRAM_CPLD},
234 {MC_CMD_NVRAM_TYPE_CPLD, 2, EFX_NVRAM_CPLD},
235 {MC_CMD_NVRAM_TYPE_LICENSE, 1, EFX_NVRAM_LICENSE},
236 {MC_CMD_NVRAM_TYPE_LICENSE, 2, EFX_NVRAM_LICENSE}
239 __checkReturn efx_rc_t
240 siena_nvram_type_to_partn(
242 __in efx_nvram_type_t type,
243 __out uint32_t *partnp)
245 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
248 EFSYS_ASSERT3U(type, !=, EFX_NVRAM_INVALID);
249 EFSYS_ASSERT3U(type, <, EFX_NVRAM_NTYPES);
250 EFSYS_ASSERT(partnp != NULL);
252 for (i = 0; i < EFX_ARRAY_SIZE(siena_parttbl); i++) {
253 siena_parttbl_entry_t *entry = &siena_parttbl[i];
255 if (entry->port == emip->emi_port && entry->nvtype == type) {
256 *partnp = entry->partn;
267 __checkReturn efx_rc_t
271 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
272 siena_parttbl_entry_t *entry;
277 * Iterate over the list of supported partition types
278 * applicable to *this* port
280 for (i = 0; i < EFX_ARRAY_SIZE(siena_parttbl); i++) {
281 entry = &siena_parttbl[i];
283 if (entry->port != emip->emi_port ||
284 !(enp->en_u.siena.enu_partn_mask & (1 << entry->partn)))
287 if ((rc = efx_mcdi_nvram_test(enp, entry->partn)) != 0) {
295 EFSYS_PROBE1(fail1, efx_rc_t, rc);
300 #endif /* EFSYS_OPT_DIAG */
303 #define SIENA_DYNAMIC_CFG_SIZE(_nitems) \
304 (sizeof (siena_mc_dynamic_config_hdr_t) + ((_nitems) * \
305 sizeof (((siena_mc_dynamic_config_hdr_t *)NULL)->fw_version[0])))
307 __checkReturn efx_rc_t
308 siena_nvram_get_dynamic_cfg(
312 __out siena_mc_dynamic_config_hdr_t **dcfgp,
315 siena_mc_dynamic_config_hdr_t *dcfg = NULL;
318 unsigned int vpd_offset;
319 unsigned int vpd_length;
320 unsigned int hdr_length;
321 unsigned int nversions;
326 EFSYS_ASSERT(partn == MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0 ||
327 partn == MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1);
330 * Allocate sufficient memory for the entire dynamiccfg area, even
331 * if we're not actually going to read in the VPD.
333 if ((rc = siena_nvram_partn_size(enp, partn, &size)) != 0)
336 if (size < SIENA_NVRAM_CHUNK) {
341 EFSYS_KMEM_ALLOC(enp->en_esip, size, dcfg);
347 if ((rc = siena_nvram_partn_read(enp, partn, 0,
348 (caddr_t)dcfg, SIENA_NVRAM_CHUNK)) != 0)
351 /* Verify the magic */
352 if (EFX_DWORD_FIELD(dcfg->magic, EFX_DWORD_0)
353 != SIENA_MC_DYNAMIC_CONFIG_MAGIC)
356 /* All future versions of the structure must be backwards compatible */
357 EFX_STATIC_ASSERT(SIENA_MC_DYNAMIC_CONFIG_VERSION == 0);
359 hdr_length = EFX_WORD_FIELD(dcfg->length, EFX_WORD_0);
360 nversions = EFX_DWORD_FIELD(dcfg->num_fw_version_items, EFX_DWORD_0);
361 vpd_offset = EFX_DWORD_FIELD(dcfg->dynamic_vpd_offset, EFX_DWORD_0);
362 vpd_length = EFX_DWORD_FIELD(dcfg->dynamic_vpd_length, EFX_DWORD_0);
364 /* Verify the hdr doesn't overflow the partn size */
365 if (hdr_length > size || vpd_offset > size || vpd_length > size ||
366 vpd_length + vpd_offset > size)
369 /* Verify the header has room for all it's versions */
370 if (hdr_length < SIENA_DYNAMIC_CFG_SIZE(0) ||
371 hdr_length < SIENA_DYNAMIC_CFG_SIZE(nversions))
375 * Read the remaining portion of the dcfg, either including
376 * the whole of VPD (there is no vpd length in this structure,
377 * so we have to parse each tag), or just the dcfg header itself
379 region = vpd ? vpd_offset + vpd_length : hdr_length;
380 if (region > SIENA_NVRAM_CHUNK) {
381 if ((rc = siena_nvram_partn_read(enp, partn, SIENA_NVRAM_CHUNK,
382 (caddr_t)dcfg + SIENA_NVRAM_CHUNK,
383 region - SIENA_NVRAM_CHUNK)) != 0)
387 /* Verify checksum */
389 for (pos = 0; pos < hdr_length; pos++)
390 cksum += ((uint8_t *)dcfg)[pos];
397 EFSYS_PROBE(invalid4);
399 EFSYS_PROBE(invalid3);
401 EFSYS_PROBE(invalid2);
403 EFSYS_PROBE(invalid1);
406 * Construct a new "null" dcfg, with an empty version vector,
407 * and an empty VPD chunk trailing. This has the neat side effect
408 * of testing the exception paths in the write path.
410 EFX_POPULATE_DWORD_1(dcfg->magic,
411 EFX_DWORD_0, SIENA_MC_DYNAMIC_CONFIG_MAGIC);
412 EFX_POPULATE_WORD_1(dcfg->length, EFX_WORD_0, sizeof (*dcfg));
413 EFX_POPULATE_BYTE_1(dcfg->version, EFX_BYTE_0,
414 SIENA_MC_DYNAMIC_CONFIG_VERSION);
415 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_offset,
416 EFX_DWORD_0, sizeof (*dcfg));
417 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_length, EFX_DWORD_0, 0);
418 EFX_POPULATE_DWORD_1(dcfg->num_fw_version_items, EFX_DWORD_0, 0);
431 EFSYS_KMEM_FREE(enp->en_esip, size, dcfg);
438 EFSYS_PROBE1(fail1, efx_rc_t, rc);
443 __checkReturn efx_rc_t
444 siena_nvram_get_subtype(
447 __out uint32_t *subtypep)
450 uint8_t payload[MAX(MC_CMD_GET_BOARD_CFG_IN_LEN,
451 MC_CMD_GET_BOARD_CFG_OUT_LENMAX)];
455 (void) memset(payload, 0, sizeof (payload));
456 req.emr_cmd = MC_CMD_GET_BOARD_CFG;
457 req.emr_in_buf = payload;
458 req.emr_in_length = MC_CMD_GET_BOARD_CFG_IN_LEN;
459 req.emr_out_buf = payload;
460 req.emr_out_length = MC_CMD_GET_BOARD_CFG_OUT_LENMAX;
462 efx_mcdi_execute(enp, &req);
464 if (req.emr_rc != 0) {
469 if (req.emr_out_length_used < MC_CMD_GET_BOARD_CFG_OUT_LENMIN) {
474 if (req.emr_out_length_used <
475 MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_OFST +
476 (partn + 1) * sizeof (efx_word_t)) {
481 fw_list = MCDI_OUT2(req, efx_word_t,
482 GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST);
483 *subtypep = EFX_WORD_FIELD(fw_list[partn], EFX_WORD_0);
492 EFSYS_PROBE1(fail1, efx_rc_t, rc);
497 __checkReturn efx_rc_t
498 siena_nvram_partn_get_version(
501 __out uint32_t *subtypep,
502 __out_ecount(4) uint16_t version[4])
504 siena_mc_dynamic_config_hdr_t *dcfg;
505 siena_parttbl_entry_t *entry;
510 if ((1 << partn) & ~enp->en_u.siena.enu_partn_mask) {
515 if ((rc = siena_nvram_get_subtype(enp, partn, subtypep)) != 0)
519 * Some partitions are accessible from both ports (for instance BOOTROM)
520 * Find the highest version reported by all dcfg structures on ports
521 * that have access to this partition.
523 version[0] = version[1] = version[2] = version[3] = 0;
524 for (i = 0; i < EFX_ARRAY_SIZE(siena_parttbl); i++) {
525 siena_mc_fw_version_t *verp;
530 entry = &siena_parttbl[i];
531 if (entry->partn != partn)
534 dcfg_partn = (entry->port == 1)
535 ? MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0
536 : MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1;
538 * Ingore missing partitions on port 2, assuming they're due
539 * to running on a single port part.
541 if ((1 << dcfg_partn) & ~enp->en_u.siena.enu_partn_mask) {
542 if (entry->port == 2)
546 if ((rc = siena_nvram_get_dynamic_cfg(enp, dcfg_partn,
547 B_FALSE, &dcfg, &length)) != 0)
550 nitems = EFX_DWORD_FIELD(dcfg->num_fw_version_items,
552 if (nitems < entry->partn)
555 verp = &dcfg->fw_version[partn];
556 temp[0] = EFX_WORD_FIELD(verp->version_w, EFX_WORD_0);
557 temp[1] = EFX_WORD_FIELD(verp->version_x, EFX_WORD_0);
558 temp[2] = EFX_WORD_FIELD(verp->version_y, EFX_WORD_0);
559 temp[3] = EFX_WORD_FIELD(verp->version_z, EFX_WORD_0);
560 if (memcmp(version, temp, sizeof (temp)) < 0)
561 memcpy(version, temp, sizeof (temp));
564 EFSYS_KMEM_FREE(enp->en_esip, length, dcfg);
574 EFSYS_PROBE1(fail1, efx_rc_t, rc);
579 __checkReturn efx_rc_t
580 siena_nvram_partn_rw_start(
583 __out size_t *chunk_sizep)
587 if ((rc = siena_nvram_partn_lock(enp, partn)) != 0)
590 if (chunk_sizep != NULL)
591 *chunk_sizep = SIENA_NVRAM_CHUNK;
596 EFSYS_PROBE1(fail1, efx_rc_t, rc);
601 __checkReturn efx_rc_t
602 siena_nvram_partn_rw_finish(
605 __out_opt uint32_t *verify_resultp)
609 if ((rc = siena_nvram_partn_unlock(enp, partn, verify_resultp)) != 0)
615 EFSYS_PROBE1(fail1, efx_rc_t, rc);
620 __checkReturn efx_rc_t
621 siena_nvram_partn_set_version(
624 __in_ecount(4) uint16_t version[4])
626 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
627 siena_mc_dynamic_config_hdr_t *dcfg = NULL;
628 siena_mc_fw_version_t *fwverp;
631 unsigned int hdr_length;
632 unsigned int vpd_length;
633 unsigned int vpd_offset;
635 unsigned int required_hdr_length;
642 dcfg_partn = (emip->emi_port == 1)
643 ? MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0
644 : MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1;
646 if ((rc = siena_nvram_partn_size(enp, dcfg_partn, &dcfg_size)) != 0)
649 if ((rc = siena_nvram_partn_lock(enp, dcfg_partn)) != 0)
652 if ((rc = siena_nvram_get_dynamic_cfg(enp, dcfg_partn,
653 B_TRUE, &dcfg, &length)) != 0)
656 hdr_length = EFX_WORD_FIELD(dcfg->length, EFX_WORD_0);
657 nitems = EFX_DWORD_FIELD(dcfg->num_fw_version_items, EFX_DWORD_0);
658 vpd_length = EFX_DWORD_FIELD(dcfg->dynamic_vpd_length, EFX_DWORD_0);
659 vpd_offset = EFX_DWORD_FIELD(dcfg->dynamic_vpd_offset, EFX_DWORD_0);
662 * NOTE: This function will blatt any fields trailing the version
663 * vector, or the VPD chunk.
665 required_hdr_length = SIENA_DYNAMIC_CFG_SIZE(partn + 1);
666 if (required_hdr_length + vpd_length > length) {
671 if (vpd_offset < required_hdr_length) {
672 (void) memmove((caddr_t)dcfg + required_hdr_length,
673 (caddr_t)dcfg + vpd_offset, vpd_length);
674 vpd_offset = required_hdr_length;
675 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_offset,
676 EFX_DWORD_0, vpd_offset);
679 if (hdr_length < required_hdr_length) {
680 (void) memset((caddr_t)dcfg + hdr_length, 0,
681 required_hdr_length - hdr_length);
682 hdr_length = required_hdr_length;
683 EFX_POPULATE_WORD_1(dcfg->length,
684 EFX_WORD_0, hdr_length);
687 /* Get the subtype to insert into the fw_subtype array */
688 if ((rc = siena_nvram_get_subtype(enp, partn, &subtype)) != 0)
691 /* Fill out the new version */
692 fwverp = &dcfg->fw_version[partn];
693 EFX_POPULATE_DWORD_1(fwverp->fw_subtype, EFX_DWORD_0, subtype);
694 EFX_POPULATE_WORD_1(fwverp->version_w, EFX_WORD_0, version[0]);
695 EFX_POPULATE_WORD_1(fwverp->version_x, EFX_WORD_0, version[1]);
696 EFX_POPULATE_WORD_1(fwverp->version_y, EFX_WORD_0, version[2]);
697 EFX_POPULATE_WORD_1(fwverp->version_z, EFX_WORD_0, version[3]);
699 /* Update the version count */
700 if (nitems < partn + 1) {
702 EFX_POPULATE_DWORD_1(dcfg->num_fw_version_items,
703 EFX_DWORD_0, nitems);
706 /* Update the checksum */
708 for (pos = 0; pos < hdr_length; pos++)
709 cksum += ((uint8_t *)dcfg)[pos];
710 dcfg->csum.eb_u8[0] -= cksum;
712 /* Erase and write the new partition */
713 if ((rc = siena_nvram_partn_erase(enp, dcfg_partn, 0, dcfg_size)) != 0)
716 /* Write out the new structure to nvram */
717 if ((rc = siena_nvram_partn_write(enp, dcfg_partn, 0,
718 (caddr_t)dcfg, vpd_offset + vpd_length)) != 0)
721 EFSYS_KMEM_FREE(enp->en_esip, length, dcfg);
723 siena_nvram_partn_unlock(enp, dcfg_partn, NULL);
736 EFSYS_KMEM_FREE(enp->en_esip, length, dcfg);
742 EFSYS_PROBE1(fail1, efx_rc_t, rc);
747 #endif /* EFSYS_OPT_NVRAM */
749 #endif /* EFSYS_OPT_SIENA */