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.
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);
173 __checkReturn efx_rc_t
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 rc = efx_mcdi_nvram_update_finish(enp, partn, reboot, NULL);
196 EFSYS_PROBE1(fail1, efx_rc_t, 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},
232 {MC_CMD_NVRAM_TYPE_LICENSE, 1, EFX_NVRAM_LICENSE},
233 {MC_CMD_NVRAM_TYPE_LICENSE, 2, EFX_NVRAM_LICENSE}
236 __checkReturn efx_rc_t
237 siena_nvram_type_to_partn(
239 __in efx_nvram_type_t type,
240 __out uint32_t *partnp)
242 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
245 EFSYS_ASSERT3U(type, <, EFX_NVRAM_NTYPES);
246 EFSYS_ASSERT(partnp != NULL);
248 for (i = 0; i < EFX_ARRAY_SIZE(siena_parttbl); i++) {
249 siena_parttbl_entry_t *entry = &siena_parttbl[i];
251 if (entry->port == emip->emi_port && entry->nvtype == type) {
252 *partnp = entry->partn;
263 __checkReturn efx_rc_t
267 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
268 siena_parttbl_entry_t *entry;
273 * Iterate over the list of supported partition types
274 * applicable to *this* port
276 for (i = 0; i < EFX_ARRAY_SIZE(siena_parttbl); i++) {
277 entry = &siena_parttbl[i];
279 if (entry->port != emip->emi_port ||
280 !(enp->en_u.siena.enu_partn_mask & (1 << entry->partn)))
283 if ((rc = efx_mcdi_nvram_test(enp, entry->partn)) != 0) {
291 EFSYS_PROBE1(fail1, efx_rc_t, rc);
296 #endif /* EFSYS_OPT_DIAG */
299 #define SIENA_DYNAMIC_CFG_SIZE(_nitems) \
300 (sizeof (siena_mc_dynamic_config_hdr_t) + ((_nitems) * \
301 sizeof (((siena_mc_dynamic_config_hdr_t *)NULL)->fw_version[0])))
303 __checkReturn efx_rc_t
304 siena_nvram_get_dynamic_cfg(
308 __out siena_mc_dynamic_config_hdr_t **dcfgp,
311 siena_mc_dynamic_config_hdr_t *dcfg = NULL;
314 unsigned int vpd_offset;
315 unsigned int vpd_length;
316 unsigned int hdr_length;
317 unsigned int nversions;
322 EFSYS_ASSERT(partn == MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0 ||
323 partn == MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1);
326 * Allocate sufficient memory for the entire dynamiccfg area, even
327 * if we're not actually going to read in the VPD.
329 if ((rc = siena_nvram_partn_size(enp, partn, &size)) != 0)
332 EFSYS_KMEM_ALLOC(enp->en_esip, size, dcfg);
338 if ((rc = siena_nvram_partn_read(enp, partn, 0,
339 (caddr_t)dcfg, SIENA_NVRAM_CHUNK)) != 0)
342 /* Verify the magic */
343 if (EFX_DWORD_FIELD(dcfg->magic, EFX_DWORD_0)
344 != SIENA_MC_DYNAMIC_CONFIG_MAGIC)
347 /* All future versions of the structure must be backwards compatible */
348 EFX_STATIC_ASSERT(SIENA_MC_DYNAMIC_CONFIG_VERSION == 0);
350 hdr_length = EFX_WORD_FIELD(dcfg->length, EFX_WORD_0);
351 nversions = EFX_DWORD_FIELD(dcfg->num_fw_version_items, EFX_DWORD_0);
352 vpd_offset = EFX_DWORD_FIELD(dcfg->dynamic_vpd_offset, EFX_DWORD_0);
353 vpd_length = EFX_DWORD_FIELD(dcfg->dynamic_vpd_length, EFX_DWORD_0);
355 /* Verify the hdr doesn't overflow the partn size */
356 if (hdr_length > size || vpd_offset > size || vpd_length > size ||
357 vpd_length + vpd_offset > size)
360 /* Verify the header has room for all it's versions */
361 if (hdr_length < SIENA_DYNAMIC_CFG_SIZE(0) ||
362 hdr_length < SIENA_DYNAMIC_CFG_SIZE(nversions))
366 * Read the remaining portion of the dcfg, either including
367 * the whole of VPD (there is no vpd length in this structure,
368 * so we have to parse each tag), or just the dcfg header itself
370 region = vpd ? vpd_offset + vpd_length : hdr_length;
371 if (region > SIENA_NVRAM_CHUNK) {
372 if ((rc = siena_nvram_partn_read(enp, partn, SIENA_NVRAM_CHUNK,
373 (caddr_t)dcfg + SIENA_NVRAM_CHUNK,
374 region - SIENA_NVRAM_CHUNK)) != 0)
378 /* Verify checksum */
380 for (pos = 0; pos < hdr_length; pos++)
381 cksum += ((uint8_t *)dcfg)[pos];
388 EFSYS_PROBE(invalid4);
390 EFSYS_PROBE(invalid3);
392 EFSYS_PROBE(invalid2);
394 EFSYS_PROBE(invalid1);
397 * Construct a new "null" dcfg, with an empty version vector,
398 * and an empty VPD chunk trailing. This has the neat side effect
399 * of testing the exception paths in the write path.
401 EFX_POPULATE_DWORD_1(dcfg->magic,
402 EFX_DWORD_0, SIENA_MC_DYNAMIC_CONFIG_MAGIC);
403 EFX_POPULATE_WORD_1(dcfg->length, EFX_WORD_0, sizeof (*dcfg));
404 EFX_POPULATE_BYTE_1(dcfg->version, EFX_BYTE_0,
405 SIENA_MC_DYNAMIC_CONFIG_VERSION);
406 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_offset,
407 EFX_DWORD_0, sizeof (*dcfg));
408 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_length, EFX_DWORD_0, 0);
409 EFX_POPULATE_DWORD_1(dcfg->num_fw_version_items, EFX_DWORD_0, 0);
422 EFSYS_KMEM_FREE(enp->en_esip, size, dcfg);
427 EFSYS_PROBE1(fail1, efx_rc_t, rc);
432 __checkReturn efx_rc_t
433 siena_nvram_get_subtype(
436 __out uint32_t *subtypep)
439 uint8_t payload[MAX(MC_CMD_GET_BOARD_CFG_IN_LEN,
440 MC_CMD_GET_BOARD_CFG_OUT_LENMAX)];
444 (void) memset(payload, 0, sizeof (payload));
445 req.emr_cmd = MC_CMD_GET_BOARD_CFG;
446 req.emr_in_buf = payload;
447 req.emr_in_length = MC_CMD_GET_BOARD_CFG_IN_LEN;
448 req.emr_out_buf = payload;
449 req.emr_out_length = MC_CMD_GET_BOARD_CFG_OUT_LENMAX;
451 efx_mcdi_execute(enp, &req);
453 if (req.emr_rc != 0) {
458 if (req.emr_out_length_used < MC_CMD_GET_BOARD_CFG_OUT_LENMIN) {
463 if (req.emr_out_length_used <
464 MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_OFST +
465 (partn + 1) * sizeof (efx_word_t)) {
470 fw_list = MCDI_OUT2(req, efx_word_t,
471 GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST);
472 *subtypep = EFX_WORD_FIELD(fw_list[partn], EFX_WORD_0);
481 EFSYS_PROBE1(fail1, efx_rc_t, rc);
486 __checkReturn efx_rc_t
487 siena_nvram_partn_get_version(
490 __out uint32_t *subtypep,
491 __out_ecount(4) uint16_t version[4])
493 siena_mc_dynamic_config_hdr_t *dcfg;
494 siena_parttbl_entry_t *entry;
499 if ((1 << partn) & ~enp->en_u.siena.enu_partn_mask) {
504 if ((rc = siena_nvram_get_subtype(enp, partn, subtypep)) != 0)
508 * Some partitions are accessible from both ports (for instance BOOTROM)
509 * Find the highest version reported by all dcfg structures on ports
510 * that have access to this partition.
512 version[0] = version[1] = version[2] = version[3] = 0;
513 for (i = 0; i < EFX_ARRAY_SIZE(siena_parttbl); i++) {
514 siena_mc_fw_version_t *verp;
519 entry = &siena_parttbl[i];
520 if (entry->partn != partn)
523 dcfg_partn = (entry->port == 1)
524 ? MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0
525 : MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1;
527 * Ingore missing partitions on port 2, assuming they're due
528 * to to running on a single port part.
530 if ((1 << dcfg_partn) & ~enp->en_u.siena.enu_partn_mask) {
531 if (entry->port == 2)
535 if ((rc = siena_nvram_get_dynamic_cfg(enp, dcfg_partn,
536 B_FALSE, &dcfg, &length)) != 0)
539 nitems = EFX_DWORD_FIELD(dcfg->num_fw_version_items,
541 if (nitems < entry->partn)
544 verp = &dcfg->fw_version[partn];
545 temp[0] = EFX_WORD_FIELD(verp->version_w, EFX_WORD_0);
546 temp[1] = EFX_WORD_FIELD(verp->version_x, EFX_WORD_0);
547 temp[2] = EFX_WORD_FIELD(verp->version_y, EFX_WORD_0);
548 temp[3] = EFX_WORD_FIELD(verp->version_z, EFX_WORD_0);
549 if (memcmp(version, temp, sizeof (temp)) < 0)
550 memcpy(version, temp, sizeof (temp));
553 EFSYS_KMEM_FREE(enp->en_esip, length, dcfg);
563 EFSYS_PROBE1(fail1, efx_rc_t, rc);
568 __checkReturn efx_rc_t
569 siena_nvram_partn_rw_start(
572 __out size_t *chunk_sizep)
576 if ((rc = siena_nvram_partn_lock(enp, partn)) != 0)
579 if (chunk_sizep != NULL)
580 *chunk_sizep = SIENA_NVRAM_CHUNK;
585 EFSYS_PROBE1(fail1, efx_rc_t, rc);
590 __checkReturn efx_rc_t
591 siena_nvram_partn_rw_finish(
597 if ((rc = siena_nvram_partn_unlock(enp, partn)) != 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);
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 */