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$");
39 #if EFSYS_OPT_VPD || EFSYS_OPT_NVRAM
41 __checkReturn efx_rc_t
42 siena_nvram_partn_size(
49 if ((1 << partn) & ~enp->en_u.siena.enu_partn_mask) {
54 if ((rc = efx_mcdi_nvram_info(enp, partn, sizep,
55 NULL, NULL, NULL)) != 0) {
64 EFSYS_PROBE1(fail1, efx_rc_t, rc);
69 __checkReturn efx_rc_t
70 siena_nvram_partn_lock(
76 if ((rc = efx_mcdi_nvram_update_start(enp, partn)) != 0) {
83 EFSYS_PROBE1(fail1, efx_rc_t, rc);
88 __checkReturn efx_rc_t
89 siena_nvram_partn_read(
92 __in unsigned int offset,
93 __out_bcount(size) caddr_t data,
100 chunk = MIN(size, SIENA_NVRAM_CHUNK);
102 if ((rc = efx_mcdi_nvram_read(enp, partn, offset, data, chunk,
103 MC_CMD_NVRAM_READ_IN_V2_DEFAULT)) != 0) {
115 EFSYS_PROBE1(fail1, efx_rc_t, rc);
120 __checkReturn efx_rc_t
121 siena_nvram_partn_erase(
124 __in unsigned int offset,
129 if ((rc = efx_mcdi_nvram_erase(enp, partn, offset, size)) != 0) {
136 EFSYS_PROBE1(fail1, efx_rc_t, rc);
141 __checkReturn efx_rc_t
142 siena_nvram_partn_write(
145 __in unsigned int offset,
146 __out_bcount(size) caddr_t data,
153 chunk = MIN(size, SIENA_NVRAM_CHUNK);
155 if ((rc = efx_mcdi_nvram_write(enp, partn, offset,
156 data, chunk)) != 0) {
168 EFSYS_PROBE1(fail1, efx_rc_t, rc);
174 siena_nvram_partn_unlock(
182 * Reboot into the new image only for PHYs. The driver has to
183 * explicitly cope with an MC reboot after a firmware update.
185 reboot = (partn == MC_CMD_NVRAM_TYPE_PHY_PORT0 ||
186 partn == MC_CMD_NVRAM_TYPE_PHY_PORT1 ||
187 partn == MC_CMD_NVRAM_TYPE_DISABLED_CALLISTO);
189 if ((rc = efx_mcdi_nvram_update_finish(enp, partn, reboot)) != 0) {
196 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_NTYPES);
244 EFSYS_ASSERT(partnp != NULL);
246 for (i = 0; i < EFX_ARRAY_SIZE(siena_parttbl); i++) {
247 siena_parttbl_entry_t *entry = &siena_parttbl[i];
249 if (entry->port == emip->emi_port && entry->nvtype == type) {
250 *partnp = entry->partn;
261 __checkReturn efx_rc_t
265 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
266 siena_parttbl_entry_t *entry;
271 * Iterate over the list of supported partition types
272 * applicable to *this* port
274 for (i = 0; i < EFX_ARRAY_SIZE(siena_parttbl); i++) {
275 entry = &siena_parttbl[i];
277 if (entry->port != emip->emi_port ||
278 !(enp->en_u.siena.enu_partn_mask & (1 << entry->partn)))
281 if ((rc = efx_mcdi_nvram_test(enp, entry->partn)) != 0) {
289 EFSYS_PROBE1(fail1, efx_rc_t, rc);
294 #endif /* EFSYS_OPT_DIAG */
297 #define SIENA_DYNAMIC_CFG_SIZE(_nitems) \
298 (sizeof (siena_mc_dynamic_config_hdr_t) + ((_nitems) * \
299 sizeof (((siena_mc_dynamic_config_hdr_t *)NULL)->fw_version[0])))
301 __checkReturn efx_rc_t
302 siena_nvram_get_dynamic_cfg(
306 __out siena_mc_dynamic_config_hdr_t **dcfgp,
309 siena_mc_dynamic_config_hdr_t *dcfg = NULL;
312 unsigned int vpd_offset;
313 unsigned int vpd_length;
314 unsigned int hdr_length;
315 unsigned int nversions;
320 EFSYS_ASSERT(partn == MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0 ||
321 partn == MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1);
324 * Allocate sufficient memory for the entire dynamiccfg area, even
325 * if we're not actually going to read in the VPD.
327 if ((rc = siena_nvram_partn_size(enp, partn, &size)) != 0)
330 EFSYS_KMEM_ALLOC(enp->en_esip, size, dcfg);
336 if ((rc = siena_nvram_partn_read(enp, partn, 0,
337 (caddr_t)dcfg, SIENA_NVRAM_CHUNK)) != 0)
340 /* Verify the magic */
341 if (EFX_DWORD_FIELD(dcfg->magic, EFX_DWORD_0)
342 != SIENA_MC_DYNAMIC_CONFIG_MAGIC)
345 /* All future versions of the structure must be backwards compatable */
346 EFX_STATIC_ASSERT(SIENA_MC_DYNAMIC_CONFIG_VERSION == 0);
348 hdr_length = EFX_WORD_FIELD(dcfg->length, EFX_WORD_0);
349 nversions = EFX_DWORD_FIELD(dcfg->num_fw_version_items, EFX_DWORD_0);
350 vpd_offset = EFX_DWORD_FIELD(dcfg->dynamic_vpd_offset, EFX_DWORD_0);
351 vpd_length = EFX_DWORD_FIELD(dcfg->dynamic_vpd_length, EFX_DWORD_0);
353 /* Verify the hdr doesn't overflow the partn size */
354 if (hdr_length > size || vpd_offset > size || vpd_length > size ||
355 vpd_length + vpd_offset > size)
358 /* Verify the header has room for all it's versions */
359 if (hdr_length < SIENA_DYNAMIC_CFG_SIZE(0) ||
360 hdr_length < SIENA_DYNAMIC_CFG_SIZE(nversions))
364 * Read the remaining portion of the dcfg, either including
365 * the whole of VPD (there is no vpd length in this structure,
366 * so we have to parse each tag), or just the dcfg header itself
368 region = vpd ? vpd_offset + vpd_length : hdr_length;
369 if (region > SIENA_NVRAM_CHUNK) {
370 if ((rc = siena_nvram_partn_read(enp, partn, SIENA_NVRAM_CHUNK,
371 (caddr_t)dcfg + SIENA_NVRAM_CHUNK,
372 region - SIENA_NVRAM_CHUNK)) != 0)
376 /* Verify checksum */
378 for (pos = 0; pos < hdr_length; pos++)
379 cksum += ((uint8_t *)dcfg)[pos];
386 EFSYS_PROBE(invalid4);
388 EFSYS_PROBE(invalid3);
390 EFSYS_PROBE(invalid2);
392 EFSYS_PROBE(invalid1);
395 * Construct a new "null" dcfg, with an empty version vector,
396 * and an empty VPD chunk trailing. This has the neat side effect
397 * of testing the exception paths in the write path.
399 EFX_POPULATE_DWORD_1(dcfg->magic,
400 EFX_DWORD_0, SIENA_MC_DYNAMIC_CONFIG_MAGIC);
401 EFX_POPULATE_WORD_1(dcfg->length, EFX_WORD_0, sizeof (*dcfg));
402 EFX_POPULATE_BYTE_1(dcfg->version, EFX_BYTE_0,
403 SIENA_MC_DYNAMIC_CONFIG_VERSION);
404 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_offset,
405 EFX_DWORD_0, sizeof (*dcfg));
406 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_length, EFX_DWORD_0, 0);
407 EFX_POPULATE_DWORD_1(dcfg->num_fw_version_items, EFX_DWORD_0, 0);
420 EFSYS_KMEM_FREE(enp->en_esip, size, dcfg);
425 EFSYS_PROBE1(fail1, efx_rc_t, rc);
430 __checkReturn efx_rc_t
431 siena_nvram_get_subtype(
434 __out uint32_t *subtypep)
437 uint8_t payload[MAX(MC_CMD_GET_BOARD_CFG_IN_LEN,
438 MC_CMD_GET_BOARD_CFG_OUT_LENMAX)];
442 (void) memset(payload, 0, sizeof (payload));
443 req.emr_cmd = MC_CMD_GET_BOARD_CFG;
444 req.emr_in_buf = payload;
445 req.emr_in_length = MC_CMD_GET_BOARD_CFG_IN_LEN;
446 req.emr_out_buf = payload;
447 req.emr_out_length = MC_CMD_GET_BOARD_CFG_OUT_LENMAX;
449 efx_mcdi_execute(enp, &req);
451 if (req.emr_rc != 0) {
456 if (req.emr_out_length_used < MC_CMD_GET_BOARD_CFG_OUT_LENMIN) {
461 if (req.emr_out_length_used <
462 MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_OFST +
463 (partn + 1) * sizeof (efx_word_t)) {
468 fw_list = MCDI_OUT2(req, efx_word_t,
469 GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST);
470 *subtypep = EFX_WORD_FIELD(fw_list[partn], EFX_WORD_0);
479 EFSYS_PROBE1(fail1, efx_rc_t, rc);
484 __checkReturn efx_rc_t
485 siena_nvram_partn_get_version(
488 __out uint32_t *subtypep,
489 __out_ecount(4) uint16_t version[4])
491 siena_mc_dynamic_config_hdr_t *dcfg;
492 siena_parttbl_entry_t *entry;
497 if ((1 << partn) & ~enp->en_u.siena.enu_partn_mask) {
502 if ((rc = siena_nvram_get_subtype(enp, partn, subtypep)) != 0)
506 * Some partitions are accessible from both ports (for instance BOOTROM)
507 * Find the highest version reported by all dcfg structures on ports
508 * that have access to this partition.
510 version[0] = version[1] = version[2] = version[3] = 0;
511 for (i = 0; i < EFX_ARRAY_SIZE(siena_parttbl); i++) {
512 siena_mc_fw_version_t *verp;
517 entry = &siena_parttbl[i];
518 if (entry->partn != partn)
521 dcfg_partn = (entry->port == 1)
522 ? MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0
523 : MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1;
525 * Ingore missing partitions on port 2, assuming they're due
526 * to to running on a single port part.
528 if ((1 << dcfg_partn) & ~enp->en_u.siena.enu_partn_mask) {
529 if (entry->port == 2)
533 if ((rc = siena_nvram_get_dynamic_cfg(enp, dcfg_partn,
534 B_FALSE, &dcfg, &length)) != 0)
537 nitems = EFX_DWORD_FIELD(dcfg->num_fw_version_items,
539 if (nitems < entry->partn)
542 verp = &dcfg->fw_version[partn];
543 temp[0] = EFX_WORD_FIELD(verp->version_w, EFX_WORD_0);
544 temp[1] = EFX_WORD_FIELD(verp->version_x, EFX_WORD_0);
545 temp[2] = EFX_WORD_FIELD(verp->version_y, EFX_WORD_0);
546 temp[3] = EFX_WORD_FIELD(verp->version_z, EFX_WORD_0);
547 if (memcmp(version, temp, sizeof (temp)) < 0)
548 memcpy(version, temp, sizeof (temp));
551 EFSYS_KMEM_FREE(enp->en_esip, length, dcfg);
561 EFSYS_PROBE1(fail1, efx_rc_t, rc);
566 __checkReturn efx_rc_t
567 siena_nvram_partn_rw_start(
570 __out size_t *chunk_sizep)
574 if ((rc = siena_nvram_partn_lock(enp, partn)) != 0)
577 if (chunk_sizep != NULL)
578 *chunk_sizep = SIENA_NVRAM_CHUNK;
583 EFSYS_PROBE1(fail1, efx_rc_t, rc);
589 siena_nvram_partn_rw_finish(
593 siena_nvram_partn_unlock(enp, partn);
596 __checkReturn efx_rc_t
597 siena_nvram_partn_set_version(
600 __in_ecount(4) uint16_t version[4])
602 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
603 siena_mc_dynamic_config_hdr_t *dcfg = NULL;
604 siena_mc_fw_version_t *fwverp;
607 unsigned int hdr_length;
608 unsigned int vpd_length;
609 unsigned int vpd_offset;
611 unsigned int required_hdr_length;
618 dcfg_partn = (emip->emi_port == 1)
619 ? MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0
620 : MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1;
622 if ((rc = siena_nvram_partn_size(enp, dcfg_partn, &dcfg_size)) != 0)
625 if ((rc = siena_nvram_partn_lock(enp, dcfg_partn)) != 0)
628 if ((rc = siena_nvram_get_dynamic_cfg(enp, dcfg_partn,
629 B_TRUE, &dcfg, &length)) != 0)
632 hdr_length = EFX_WORD_FIELD(dcfg->length, EFX_WORD_0);
633 nitems = EFX_DWORD_FIELD(dcfg->num_fw_version_items, EFX_DWORD_0);
634 vpd_length = EFX_DWORD_FIELD(dcfg->dynamic_vpd_length, EFX_DWORD_0);
635 vpd_offset = EFX_DWORD_FIELD(dcfg->dynamic_vpd_offset, EFX_DWORD_0);
638 * NOTE: This function will blatt any fields trailing the version
639 * vector, or the VPD chunk.
641 required_hdr_length = SIENA_DYNAMIC_CFG_SIZE(partn + 1);
642 if (required_hdr_length + vpd_length > length) {
647 if (vpd_offset < required_hdr_length) {
648 (void) memmove((caddr_t)dcfg + required_hdr_length,
649 (caddr_t)dcfg + vpd_offset, vpd_length);
650 vpd_offset = required_hdr_length;
651 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_offset,
652 EFX_DWORD_0, vpd_offset);
655 if (hdr_length < required_hdr_length) {
656 (void) memset((caddr_t)dcfg + hdr_length, 0,
657 required_hdr_length - hdr_length);
658 hdr_length = required_hdr_length;
659 EFX_POPULATE_WORD_1(dcfg->length,
660 EFX_WORD_0, hdr_length);
663 /* Get the subtype to insert into the fw_subtype array */
664 if ((rc = siena_nvram_get_subtype(enp, partn, &subtype)) != 0)
667 /* Fill out the new version */
668 fwverp = &dcfg->fw_version[partn];
669 EFX_POPULATE_DWORD_1(fwverp->fw_subtype, EFX_DWORD_0, subtype);
670 EFX_POPULATE_WORD_1(fwverp->version_w, EFX_WORD_0, version[0]);
671 EFX_POPULATE_WORD_1(fwverp->version_x, EFX_WORD_0, version[1]);
672 EFX_POPULATE_WORD_1(fwverp->version_y, EFX_WORD_0, version[2]);
673 EFX_POPULATE_WORD_1(fwverp->version_z, EFX_WORD_0, version[3]);
675 /* Update the version count */
676 if (nitems < partn + 1) {
678 EFX_POPULATE_DWORD_1(dcfg->num_fw_version_items,
679 EFX_DWORD_0, nitems);
682 /* Update the checksum */
684 for (pos = 0; pos < hdr_length; pos++)
685 cksum += ((uint8_t *)dcfg)[pos];
686 dcfg->csum.eb_u8[0] -= cksum;
688 /* Erase and write the new partition */
689 if ((rc = siena_nvram_partn_erase(enp, dcfg_partn, 0, dcfg_size)) != 0)
692 /* Write out the new structure to nvram */
693 if ((rc = siena_nvram_partn_write(enp, dcfg_partn, 0,
694 (caddr_t)dcfg, vpd_offset + vpd_length)) != 0)
697 EFSYS_KMEM_FREE(enp->en_esip, length, dcfg);
699 siena_nvram_partn_unlock(enp, dcfg_partn);
712 EFSYS_KMEM_FREE(enp->en_esip, length, dcfg);
718 EFSYS_PROBE1(fail1, efx_rc_t, rc);
723 #endif /* EFSYS_OPT_NVRAM */
725 #endif /* EFSYS_OPT_SIENA */