2 * Copyright (c) 2009-2015 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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 #include "efx_types.h"
42 #if EFSYS_OPT_VPD || EFSYS_OPT_NVRAM
45 siena_nvram_partn_size(
47 __in unsigned int partn,
52 if ((1 << partn) & ~enp->en_u.siena.enu_partn_mask) {
57 if ((rc = efx_mcdi_nvram_info(enp, partn, sizep, NULL, NULL)) != 0) {
66 EFSYS_PROBE1(fail1, int, rc);
72 siena_nvram_partn_lock(
74 __in unsigned int partn)
78 if ((rc = efx_mcdi_nvram_update_start(enp, partn)) != 0) {
85 EFSYS_PROBE1(fail1, int, rc);
91 siena_nvram_partn_read(
93 __in unsigned int partn,
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,
105 data, chunk)) != 0) {
117 EFSYS_PROBE1(fail1, int, rc);
123 siena_nvram_partn_erase(
125 __in unsigned int partn,
126 __in unsigned int offset,
131 if ((rc = efx_mcdi_nvram_erase(enp, partn, offset, size)) != 0) {
138 EFSYS_PROBE1(fail1, int, rc);
144 siena_nvram_partn_write(
146 __in unsigned int partn,
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, int, rc);
176 siena_nvram_partn_unlock(
178 __in unsigned int partn)
184 * Reboot into the new image only for PHYs. The driver has to
185 * explicitly cope with an MC reboot after a firmware update.
187 reboot = (partn == MC_CMD_NVRAM_TYPE_PHY_PORT0 ||
188 partn == MC_CMD_NVRAM_TYPE_PHY_PORT1 ||
189 partn == MC_CMD_NVRAM_TYPE_DISABLED_CALLISTO);
191 if ((rc = efx_mcdi_nvram_update_finish(enp, partn, reboot)) != 0) {
198 EFSYS_PROBE1(fail1, int, rc);
201 #endif /* EFSYS_OPT_VPD || EFSYS_OPT_NVRAM */
205 typedef struct siena_parttbl_entry_s {
208 efx_nvram_type_t nvtype;
209 } siena_parttbl_entry_t;
211 static siena_parttbl_entry_t siena_parttbl[] = {
212 {MC_CMD_NVRAM_TYPE_DISABLED_CALLISTO, 1, EFX_NVRAM_NULLPHY},
213 {MC_CMD_NVRAM_TYPE_DISABLED_CALLISTO, 2, EFX_NVRAM_NULLPHY},
214 {MC_CMD_NVRAM_TYPE_MC_FW, 1, EFX_NVRAM_MC_FIRMWARE},
215 {MC_CMD_NVRAM_TYPE_MC_FW, 2, EFX_NVRAM_MC_FIRMWARE},
216 {MC_CMD_NVRAM_TYPE_MC_FW_BACKUP, 1, EFX_NVRAM_MC_GOLDEN},
217 {MC_CMD_NVRAM_TYPE_MC_FW_BACKUP, 2, EFX_NVRAM_MC_GOLDEN},
218 {MC_CMD_NVRAM_TYPE_EXP_ROM, 1, EFX_NVRAM_BOOTROM},
219 {MC_CMD_NVRAM_TYPE_EXP_ROM, 2, EFX_NVRAM_BOOTROM},
220 {MC_CMD_NVRAM_TYPE_EXP_ROM_CFG_PORT0, 1, EFX_NVRAM_BOOTROM_CFG},
221 {MC_CMD_NVRAM_TYPE_EXP_ROM_CFG_PORT1, 2, EFX_NVRAM_BOOTROM_CFG},
222 {MC_CMD_NVRAM_TYPE_PHY_PORT0, 1, EFX_NVRAM_PHY},
223 {MC_CMD_NVRAM_TYPE_PHY_PORT1, 2, EFX_NVRAM_PHY},
224 {MC_CMD_NVRAM_TYPE_FPGA, 1, EFX_NVRAM_FPGA},
225 {MC_CMD_NVRAM_TYPE_FPGA, 2, EFX_NVRAM_FPGA},
226 {MC_CMD_NVRAM_TYPE_FPGA_BACKUP, 1, EFX_NVRAM_FPGA_BACKUP},
227 {MC_CMD_NVRAM_TYPE_FPGA_BACKUP, 2, EFX_NVRAM_FPGA_BACKUP},
228 {MC_CMD_NVRAM_TYPE_FC_FW, 1, EFX_NVRAM_FCFW},
229 {MC_CMD_NVRAM_TYPE_FC_FW, 2, EFX_NVRAM_FCFW},
230 {MC_CMD_NVRAM_TYPE_CPLD, 1, EFX_NVRAM_CPLD},
231 {MC_CMD_NVRAM_TYPE_CPLD, 2, EFX_NVRAM_CPLD},
234 static __checkReturn siena_parttbl_entry_t *
237 __in efx_nvram_type_t type)
239 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
240 siena_parttbl_entry_t *entry;
243 EFSYS_ASSERT3U(type, <, EFX_NVRAM_NTYPES);
245 for (i = 0; i < EFX_ARRAY_SIZE(siena_parttbl); i++) {
246 entry = &siena_parttbl[i];
248 if (entry->port == emip->emi_port && entry->nvtype == type)
261 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
262 siena_parttbl_entry_t *entry;
267 * Iterate over the list of supported partition types
268 * applicable to *this* port
270 for (i = 0; i < EFX_ARRAY_SIZE(siena_parttbl); i++) {
271 entry = &siena_parttbl[i];
273 if (entry->port != emip->emi_port ||
274 !(enp->en_u.siena.enu_partn_mask & (1 << entry->partn)))
277 if ((rc = efx_mcdi_nvram_test(enp, entry->partn)) != 0) {
285 EFSYS_PROBE1(fail1, int, rc);
290 #endif /* EFSYS_OPT_DIAG */
295 __in efx_nvram_type_t type,
298 siena_parttbl_entry_t *entry;
301 if ((entry = siena_parttbl_entry(enp, type)) == NULL) {
306 if ((rc = siena_nvram_partn_size(enp, entry->partn, sizep)) != 0)
314 EFSYS_PROBE1(fail1, int, rc);
321 #define SIENA_DYNAMIC_CFG_SIZE(_nitems) \
322 (sizeof (siena_mc_dynamic_config_hdr_t) + ((_nitems) * \
323 sizeof (((siena_mc_dynamic_config_hdr_t *)NULL)->fw_version[0])))
326 siena_nvram_get_dynamic_cfg(
328 __in unsigned int partn,
330 __out siena_mc_dynamic_config_hdr_t **dcfgp,
333 siena_mc_dynamic_config_hdr_t *dcfg = NULL;
336 unsigned int vpd_offset;
337 unsigned int vpd_length;
338 unsigned int hdr_length;
339 unsigned int nversions;
344 EFSYS_ASSERT(partn == MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0 ||
345 partn == MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1);
348 * Allocate sufficient memory for the entire dynamiccfg area, even
349 * if we're not actually going to read in the VPD.
351 if ((rc = siena_nvram_partn_size(enp, partn, &size)) != 0)
354 EFSYS_KMEM_ALLOC(enp->en_esip, size, dcfg);
360 if ((rc = siena_nvram_partn_read(enp, partn, 0,
361 (caddr_t)dcfg, SIENA_NVRAM_CHUNK)) != 0)
364 /* Verify the magic */
365 if (EFX_DWORD_FIELD(dcfg->magic, EFX_DWORD_0)
366 != SIENA_MC_DYNAMIC_CONFIG_MAGIC)
369 /* All future versions of the structure must be backwards compatable */
370 EFX_STATIC_ASSERT(SIENA_MC_DYNAMIC_CONFIG_VERSION == 0);
372 hdr_length = EFX_WORD_FIELD(dcfg->length, EFX_WORD_0);
373 nversions = EFX_DWORD_FIELD(dcfg->num_fw_version_items, EFX_DWORD_0);
374 vpd_offset = EFX_DWORD_FIELD(dcfg->dynamic_vpd_offset, EFX_DWORD_0);
375 vpd_length = EFX_DWORD_FIELD(dcfg->dynamic_vpd_length, EFX_DWORD_0);
377 /* Verify the hdr doesn't overflow the partn size */
378 if (hdr_length > size || vpd_offset > size || vpd_length > size ||
379 vpd_length + vpd_offset > size)
382 /* Verify the header has room for all it's versions */
383 if (hdr_length < SIENA_DYNAMIC_CFG_SIZE(0) ||
384 hdr_length < SIENA_DYNAMIC_CFG_SIZE(nversions))
388 * Read the remaining portion of the dcfg, either including
389 * the whole of VPD (there is no vpd length in this structure,
390 * so we have to parse each tag), or just the dcfg header itself
392 region = vpd ? vpd_offset + vpd_length : hdr_length;
393 if (region > SIENA_NVRAM_CHUNK) {
394 if ((rc = siena_nvram_partn_read(enp, partn, SIENA_NVRAM_CHUNK,
395 (caddr_t)dcfg + SIENA_NVRAM_CHUNK,
396 region - SIENA_NVRAM_CHUNK)) != 0)
400 /* Verify checksum */
402 for (pos = 0; pos < hdr_length; pos++)
403 cksum += ((uint8_t *)dcfg)[pos];
410 EFSYS_PROBE(invalid4);
412 EFSYS_PROBE(invalid3);
414 EFSYS_PROBE(invalid2);
416 EFSYS_PROBE(invalid1);
419 * Construct a new "null" dcfg, with an empty version vector,
420 * and an empty VPD chunk trailing. This has the neat side effect
421 * of testing the exception paths in the write path.
423 EFX_POPULATE_DWORD_1(dcfg->magic,
424 EFX_DWORD_0, SIENA_MC_DYNAMIC_CONFIG_MAGIC);
425 EFX_POPULATE_WORD_1(dcfg->length, EFX_WORD_0, sizeof (*dcfg));
426 EFX_POPULATE_BYTE_1(dcfg->version, EFX_BYTE_0,
427 SIENA_MC_DYNAMIC_CONFIG_VERSION);
428 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_offset,
429 EFX_DWORD_0, sizeof (*dcfg));
430 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_length, EFX_DWORD_0, 0);
431 EFX_POPULATE_DWORD_1(dcfg->num_fw_version_items, EFX_DWORD_0, 0);
444 EFSYS_KMEM_FREE(enp->en_esip, size, dcfg);
449 EFSYS_PROBE1(fail1, int, rc);
455 siena_nvram_get_subtype(
457 __in unsigned int partn,
458 __out uint32_t *subtypep)
461 uint8_t payload[MAX(MC_CMD_GET_BOARD_CFG_IN_LEN,
462 MC_CMD_GET_BOARD_CFG_OUT_LENMAX)];
466 (void) memset(payload, 0, sizeof (payload));
467 req.emr_cmd = MC_CMD_GET_BOARD_CFG;
468 req.emr_in_buf = payload;
469 req.emr_in_length = MC_CMD_GET_BOARD_CFG_IN_LEN;
470 req.emr_out_buf = payload;
471 req.emr_out_length = MC_CMD_GET_BOARD_CFG_OUT_LENMAX;
473 efx_mcdi_execute(enp, &req);
475 if (req.emr_rc != 0) {
480 if (req.emr_out_length_used < MC_CMD_GET_BOARD_CFG_OUT_LENMIN) {
485 if (req.emr_out_length_used <
486 MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_OFST +
487 (partn + 1) * sizeof (efx_word_t)) {
492 fw_list = MCDI_OUT2(req, efx_word_t,
493 GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST);
494 *subtypep = EFX_WORD_FIELD(fw_list[partn], EFX_WORD_0);
503 EFSYS_PROBE1(fail1, int, rc);
509 siena_nvram_get_version(
511 __in efx_nvram_type_t type,
512 __out uint32_t *subtypep,
513 __out_ecount(4) uint16_t version[4])
515 siena_mc_dynamic_config_hdr_t *dcfg;
516 siena_parttbl_entry_t *entry;
517 unsigned int dcfg_partn;
522 if ((entry = siena_parttbl_entry(enp, type)) == NULL) {
526 partn = entry->partn;
528 if ((1 << partn) & ~enp->en_u.siena.enu_partn_mask) {
533 if ((rc = siena_nvram_get_subtype(enp, partn, subtypep)) != 0)
537 * Some partitions are accessible from both ports (for instance BOOTROM)
538 * Find the highest version reported by all dcfg structures on ports
539 * that have access to this partition.
541 version[0] = version[1] = version[2] = version[3] = 0;
542 for (i = 0; i < EFX_ARRAY_SIZE(siena_parttbl); i++) {
547 entry = &siena_parttbl[i];
548 if (entry->partn != partn)
551 dcfg_partn = (entry->port == 1)
552 ? MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0
553 : MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1;
555 * Ingore missing partitions on port 2, assuming they're due
556 * to to running on a single port part.
558 if ((1 << dcfg_partn) & ~enp->en_u.siena.enu_partn_mask) {
559 if (entry->port == 2)
563 if ((rc = siena_nvram_get_dynamic_cfg(enp, dcfg_partn,
564 B_FALSE, &dcfg, &length)) != 0)
567 nitems = EFX_DWORD_FIELD(dcfg->num_fw_version_items,
569 if (nitems < entry->partn)
572 temp[0] = EFX_WORD_FIELD(dcfg->fw_version[partn].version_w,
574 temp[1] = EFX_WORD_FIELD(dcfg->fw_version[partn].version_x,
576 temp[2] = EFX_WORD_FIELD(dcfg->fw_version[partn].version_y,
578 temp[3] = EFX_WORD_FIELD(dcfg->fw_version[partn].version_z,
580 if (memcmp(version, temp, sizeof (temp)) < 0)
581 memcpy(version, temp, sizeof (temp));
584 EFSYS_KMEM_FREE(enp->en_esip, length, dcfg);
596 EFSYS_PROBE1(fail1, int, rc);
602 siena_nvram_rw_start(
604 __in efx_nvram_type_t type,
605 __out size_t *chunk_sizep)
607 siena_parttbl_entry_t *entry;
610 if ((entry = siena_parttbl_entry(enp, type)) == NULL) {
615 if ((rc = siena_nvram_partn_lock(enp, entry->partn)) != 0)
618 if (chunk_sizep != NULL)
619 *chunk_sizep = SIENA_NVRAM_CHUNK;
626 EFSYS_PROBE1(fail1, int, rc);
632 siena_nvram_read_chunk(
634 __in efx_nvram_type_t type,
635 __in unsigned int offset,
636 __out_bcount(size) caddr_t data,
639 siena_parttbl_entry_t *entry;
642 if ((entry = siena_parttbl_entry(enp, type)) == NULL) {
647 if ((rc = siena_nvram_partn_read(enp, entry->partn,
648 offset, data, size)) != 0)
656 EFSYS_PROBE1(fail1, int, rc);
664 __in efx_nvram_type_t type)
666 siena_parttbl_entry_t *entry;
670 if ((entry = siena_parttbl_entry(enp, type)) == NULL) {
675 if ((rc = siena_nvram_partn_size(enp, entry->partn, &size)) != 0)
678 if ((rc = siena_nvram_partn_erase(enp, entry->partn, 0, size)) != 0)
688 EFSYS_PROBE1(fail1, int, rc);
694 siena_nvram_write_chunk(
696 __in efx_nvram_type_t type,
697 __in unsigned int offset,
698 __in_bcount(size) caddr_t data,
701 siena_parttbl_entry_t *entry;
704 if ((entry = siena_parttbl_entry(enp, type)) == NULL) {
709 if ((rc = siena_nvram_partn_write(enp, entry->partn,
710 offset, data, size)) != 0)
718 EFSYS_PROBE1(fail1, int, rc);
724 siena_nvram_rw_finish(
726 __in efx_nvram_type_t type)
728 siena_parttbl_entry_t *entry;
730 if ((entry = siena_parttbl_entry(enp, type)) != NULL)
731 siena_nvram_partn_unlock(enp, entry->partn);
735 siena_nvram_set_version(
737 __in efx_nvram_type_t type,
738 __in_ecount(4) uint16_t version[4])
740 siena_mc_dynamic_config_hdr_t *dcfg = NULL;
741 siena_parttbl_entry_t *entry;
742 unsigned int dcfg_partn;
744 unsigned int hdr_length;
745 unsigned int vpd_length;
746 unsigned int vpd_offset;
748 unsigned int required_hdr_length;
755 if ((entry = siena_parttbl_entry(enp, type)) == NULL) {
760 dcfg_partn = (entry->port == 1)
761 ? MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0
762 : MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1;
764 if ((rc = siena_nvram_partn_size(enp, dcfg_partn, &partn_size)) != 0)
767 if ((rc = siena_nvram_partn_lock(enp, dcfg_partn)) != 0)
770 if ((rc = siena_nvram_get_dynamic_cfg(enp, dcfg_partn,
771 B_TRUE, &dcfg, &length)) != 0)
774 hdr_length = EFX_WORD_FIELD(dcfg->length, EFX_WORD_0);
775 nitems = EFX_DWORD_FIELD(dcfg->num_fw_version_items, EFX_DWORD_0);
776 vpd_length = EFX_DWORD_FIELD(dcfg->dynamic_vpd_length, EFX_DWORD_0);
777 vpd_offset = EFX_DWORD_FIELD(dcfg->dynamic_vpd_offset, EFX_DWORD_0);
780 * NOTE: This function will blatt any fields trailing the version
781 * vector, or the VPD chunk.
783 required_hdr_length = SIENA_DYNAMIC_CFG_SIZE(entry->partn + 1);
784 if (required_hdr_length + vpd_length > length) {
789 if (vpd_offset < required_hdr_length) {
790 (void) memmove((caddr_t)dcfg + required_hdr_length,
791 (caddr_t)dcfg + vpd_offset, vpd_length);
792 vpd_offset = required_hdr_length;
793 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_offset,
794 EFX_DWORD_0, vpd_offset);
797 if (hdr_length < required_hdr_length) {
798 (void) memset((caddr_t)dcfg + hdr_length, 0,
799 required_hdr_length - hdr_length);
800 hdr_length = required_hdr_length;
801 EFX_POPULATE_WORD_1(dcfg->length,
802 EFX_WORD_0, hdr_length);
805 /* Get the subtype to insert into the fw_subtype array */
806 if ((rc = siena_nvram_get_subtype(enp, entry->partn, &subtype)) != 0)
809 /* Fill out the new version */
810 EFX_POPULATE_DWORD_1(dcfg->fw_version[entry->partn].fw_subtype,
811 EFX_DWORD_0, subtype);
812 EFX_POPULATE_WORD_1(dcfg->fw_version[entry->partn].version_w,
813 EFX_WORD_0, version[0]);
814 EFX_POPULATE_WORD_1(dcfg->fw_version[entry->partn].version_x,
815 EFX_WORD_0, version[1]);
816 EFX_POPULATE_WORD_1(dcfg->fw_version[entry->partn].version_y,
817 EFX_WORD_0, version[2]);
818 EFX_POPULATE_WORD_1(dcfg->fw_version[entry->partn].version_z,
819 EFX_WORD_0, version[3]);
821 /* Update the version count */
822 if (nitems < entry->partn + 1) {
823 nitems = entry->partn + 1;
824 EFX_POPULATE_DWORD_1(dcfg->num_fw_version_items,
825 EFX_DWORD_0, nitems);
828 /* Update the checksum */
830 for (pos = 0; pos < hdr_length; pos++)
831 cksum += ((uint8_t *)dcfg)[pos];
832 dcfg->csum.eb_u8[0] -= cksum;
834 /* Erase and write the new partition */
835 if ((rc = siena_nvram_partn_erase(enp, dcfg_partn, 0, partn_size)) != 0)
838 /* Write out the new structure to nvram */
839 if ((rc = siena_nvram_partn_write(enp, dcfg_partn, 0,
840 (caddr_t)dcfg, vpd_offset + vpd_length)) != 0)
843 EFSYS_KMEM_FREE(enp->en_esip, length, dcfg);
845 siena_nvram_partn_unlock(enp, dcfg_partn);
858 EFSYS_KMEM_FREE(enp->en_esip, length, dcfg);
864 EFSYS_PROBE1(fail1, int, rc);
869 #endif /* EFSYS_OPT_NVRAM */
871 #endif /* EFSYS_OPT_SIENA */