49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * Copyright (c) 2009-2015 Solarflare Communications Inc.
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * All rights reserved.
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * Redistribution and use in source and binary forms, with or without
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * modification, are permitted provided that the following conditions are met:
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * 1. Redistributions of source code must retain the above copyright notice,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * this list of conditions and the following disclaimer.
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright notice,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * this list of conditions and the following disclaimer in the documentation
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * and/or other materials provided with the distribution.
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * The views and conclusions contained in the software and documentation are
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * those of the authors and should not be interpreted as representing official
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * policies, either expressed or implied, of the FreeBSD Project.
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore __deref_out_bcount_opt(*sizep) caddr_t *svpdp,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore EFSYS_ASSERT(partn == MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT0 ||
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore partn == MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT1);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore /* Allocate sufficient memory for the entire static cfg area */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = siena_nvram_partn_size(enp, partn, &size)) != 0)
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = siena_nvram_partn_read(enp, partn, 0,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore /* Verify the magic number */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if (EFX_DWORD_FIELD(scfg->magic, EFX_DWORD_0) !=
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore /* All future versions of the structure must be backwards compatable */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore EFX_STATIC_ASSERT(SIENA_MC_STATIC_CONFIG_VERSION == 0);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore hdr_length = EFX_WORD_FIELD(scfg->length, EFX_WORD_0);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore vpd_offset = EFX_DWORD_FIELD(scfg->static_vpd_offset, EFX_DWORD_0);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore vpd_length = EFX_DWORD_FIELD(scfg->static_vpd_length, EFX_DWORD_0);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore /* Verify the hdr doesn't overflow the sector size */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if (hdr_length > size || vpd_offset > size || vpd_length > size ||
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore /* Read the remainder of scfg + static vpd */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = siena_nvram_partn_read(enp, partn, SIENA_NVRAM_CHUNK,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore /* Verify checksum */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore /* Copy the vpd data out */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore EFSYS_KMEM_ALLOC(enp->en_esip, vpd_length, svpd);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore (void) memcpy(svpd, (caddr_t)scfg + vpd_offset, vpd_length);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * We need the static VPD sector to present a unified static+dynamic
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * VPD, that is, basically on every read, write, verify cycle. Since
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * it should *never* change we can just cache it here.
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = siena_vpd_get_static(enp, partn, &svpd, &size)) != 0)
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = efx_vpd_hunk_verify(svpd, size, NULL)) != 0)
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * This function returns the total size the user should allocate
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * for all VPD operations. We've already cached the static vpd,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * so we just need to return an upper bound on the dynamic vpd.
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * Since the dynamic_config structure can change under our feet,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * (as version numbers are inserted), just be safe and return the
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * total size of the dynamic_config *sector*
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = siena_nvram_partn_size(enp, partn, sizep)) != 0)
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = siena_nvram_get_dynamic_cfg(enp, dcfg_partn,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore vpd_length = EFX_DWORD_FIELD(dcfg->dynamic_vpd_length, EFX_DWORD_0);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore vpd_offset = EFX_DWORD_FIELD(dcfg->dynamic_vpd_offset, EFX_DWORD_0);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore rc = EFAULT; /* Invalid dcfg: header bigger than sector */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore (void) memcpy(data, (caddr_t)dcfg + vpd_offset, vpd_length);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore /* Pad data with all-1s, consistent with update operations */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore (void) memset(data + vpd_length, 0xff, size - vpd_length);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore EFSYS_KMEM_FREE(enp->en_esip, dcfg_size, dcfg);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore EFSYS_KMEM_FREE(enp->en_esip, dcfg_size, dcfg);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * Strictly you could take the view that dynamic vpd is optional.
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * Instead, to conform more closely to the read/verify/reinit()
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * paradigm, we require dynamic vpd. siena_vpd_reinit() will
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * reinitialize it as required.
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = efx_vpd_hunk_verify(data, size, NULL)) != 0)
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * Verify that there is no duplication between the static and
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * dynamic cfg sectors.
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = efx_vpd_hunk_next(data, size, &dtag,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * Skip the RV keyword. It should be present in both the static
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * and dynamic cfg sectors.
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if (dtag == EFX_VPD_RO && dkey == EFX_VPD_KEYWORD('R', 'V'))
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore enp->en_u.siena.enu_svpd_length, &stag, &skey,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore * Only create a PID if the dynamic cfg doesn't have one
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore rc = efx_vpd_hunk_get(enp->en_u.siena.enu_svpd,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = efx_vpd_hunk_reinit(data, size, wantpid)) != 0)
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore /* Attempt to satisfy the request from svpd first */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = efx_vpd_hunk_get(enp->en_u.siena.enu_svpd,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore enp->en_u.siena.enu_svpd_length, evvp->evv_tag,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore evvp->evv_keyword, &offset, &length)) == 0) {
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore /* And then from the provided data buffer */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = efx_vpd_hunk_get(data, size, evvp->evv_tag,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore (void) memcpy(evvp->evv_value, data + offset, length);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore /* If the provided (tag,keyword) exists in svpd, then it is readonly */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = efx_vpd_hunk_get(enp->en_u.siena.enu_svpd,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore enp->en_u.siena.enu_svpd_length, evvp->evv_tag,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore evvp->evv_keyword, &offset, &length)) == 0) {
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = efx_vpd_hunk_set(data, size, evvp)) != 0)
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore _NOTE(ARGUNUSED(enp, data, size, evvp, contp))
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore /* Determine total length of all tags */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = efx_vpd_hunk_length(data, size, &vpd_length)) != 0)
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore /* Lock dynamic config sector for write, and read structure only */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = siena_nvram_partn_size(enp, dcfg_partn, &partn_size)) != 0)
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = siena_nvram_partn_lock(enp, dcfg_partn)) != 0)
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = siena_nvram_get_dynamic_cfg(enp, dcfg_partn,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore hdr_length = EFX_WORD_FIELD(dcfg->length, EFX_WORD_0);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore /* Allocated memory should have room for the new VPD */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore /* Copy in new vpd and update header */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_offset, EFX_DWORD_0, vpd_offset);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore (void) memcpy((caddr_t)dcfg + vpd_offset, data, vpd_length);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_length, EFX_DWORD_0, vpd_length);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore /* Update the checksum */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore /* Erase and write the new sector */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = siena_nvram_partn_erase(enp, dcfg_partn, 0, partn_size)) != 0)
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore /* Write out the new structure to nvram */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore if ((rc = siena_nvram_partn_write(enp, dcfg_partn, 0, (caddr_t)dcfg,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore EFSYS_KMEM_FREE(enp->en_esip, dcfg_size, dcfg);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore EFSYS_KMEM_FREE(enp->en_esip, dcfg_size, dcfg);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore EFSYS_KMEM_FREE(enp->en_esip, enp->en_u.siena.enu_svpd_length,
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore#endif /* EFSYS_OPT_SIENA */
49ef7e0638c8b771d8a136eae78b1c0f99acc8e0Garrett D'Amore#endif /* EFSYS_OPT_VPD */