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$");
41 static __checkReturn efx_rc_t
45 __deref_out_bcount_opt(*sizep) caddr_t *svpdp,
48 siena_mc_static_config_hdr_t *scfg;
52 unsigned int vpd_offset;
53 unsigned int vpd_length;
54 unsigned int hdr_length;
59 EFSYS_ASSERT(partn == MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT0 ||
60 partn == MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT1);
62 /* Allocate sufficient memory for the entire static cfg area */
63 if ((rc = siena_nvram_partn_size(enp, partn, &size)) != 0)
66 EFSYS_KMEM_ALLOC(enp->en_esip, size, scfg);
72 if ((rc = siena_nvram_partn_read(enp, partn, 0,
73 (caddr_t)scfg, SIENA_NVRAM_CHUNK)) != 0)
76 /* Verify the magic number */
77 if (EFX_DWORD_FIELD(scfg->magic, EFX_DWORD_0) !=
78 SIENA_MC_STATIC_CONFIG_MAGIC) {
83 /* All future versions of the structure must be backwards compatible */
84 EFX_STATIC_ASSERT(SIENA_MC_STATIC_CONFIG_VERSION == 0);
86 hdr_length = EFX_WORD_FIELD(scfg->length, EFX_WORD_0);
87 vpd_offset = EFX_DWORD_FIELD(scfg->static_vpd_offset, EFX_DWORD_0);
88 vpd_length = EFX_DWORD_FIELD(scfg->static_vpd_length, EFX_DWORD_0);
90 /* Verify the hdr doesn't overflow the sector size */
91 if (hdr_length > size || vpd_offset > size || vpd_length > size ||
92 vpd_length + vpd_offset > size) {
97 /* Read the remainder of scfg + static vpd */
98 region = vpd_offset + vpd_length;
99 if (region > SIENA_NVRAM_CHUNK) {
100 if ((rc = siena_nvram_partn_read(enp, partn, SIENA_NVRAM_CHUNK,
101 (caddr_t)scfg + SIENA_NVRAM_CHUNK,
102 region - SIENA_NVRAM_CHUNK)) != 0)
106 /* Verify checksum */
108 for (pos = 0; pos < hdr_length; pos++)
109 cksum += ((uint8_t *)scfg)[pos];
118 /* Copy the vpd data out */
119 EFSYS_KMEM_ALLOC(enp->en_esip, vpd_length, svpd);
124 memcpy(svpd, (caddr_t)scfg + vpd_offset, vpd_length);
127 EFSYS_KMEM_FREE(enp->en_esip, size, scfg);
147 EFSYS_KMEM_FREE(enp->en_esip, size, scfg);
152 EFSYS_PROBE1(fail1, efx_rc_t, rc);
157 __checkReturn efx_rc_t
161 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
167 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
169 partn = (emip->emi_port == 1)
170 ? MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT0
171 : MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT1;
174 * We need the static VPD sector to present a unified static+dynamic
175 * VPD, that is, basically on every read, write, verify cycle. Since
176 * it should *never* change we can just cache it here.
178 if ((rc = siena_vpd_get_static(enp, partn, &svpd, &size)) != 0)
181 if (svpd != NULL && size > 0) {
182 if ((rc = efx_vpd_hunk_verify(svpd, size, NULL)) != 0)
186 enp->en_u.siena.enu_svpd = svpd;
187 enp->en_u.siena.enu_svpd_length = size;
194 EFSYS_KMEM_FREE(enp->en_esip, size, svpd);
196 EFSYS_PROBE1(fail1, efx_rc_t, rc);
201 __checkReturn efx_rc_t
206 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
210 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
213 * This function returns the total size the user should allocate
214 * for all VPD operations. We've already cached the static vpd,
215 * so we just need to return an upper bound on the dynamic vpd.
216 * Since the dynamic_config structure can change under our feet,
217 * (as version numbers are inserted), just be safe and return the
218 * total size of the dynamic_config *sector*
220 partn = (emip->emi_port == 1)
221 ? MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0
222 : MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1;
224 if ((rc = siena_nvram_partn_size(enp, partn, sizep)) != 0)
230 EFSYS_PROBE1(fail1, efx_rc_t, rc);
235 __checkReturn efx_rc_t
238 __out_bcount(size) caddr_t data,
241 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
242 siena_mc_dynamic_config_hdr_t *dcfg = NULL;
243 unsigned int vpd_length;
244 unsigned int vpd_offset;
245 unsigned int dcfg_partn;
249 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
251 dcfg_partn = (emip->emi_port == 1)
252 ? MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0
253 : MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1;
255 if ((rc = siena_nvram_get_dynamic_cfg(enp, dcfg_partn,
256 B_TRUE, &dcfg, &dcfg_size)) != 0)
259 vpd_length = EFX_DWORD_FIELD(dcfg->dynamic_vpd_length, EFX_DWORD_0);
260 vpd_offset = EFX_DWORD_FIELD(dcfg->dynamic_vpd_offset, EFX_DWORD_0);
262 if (vpd_length > size) {
263 rc = EFAULT; /* Invalid dcfg: header bigger than sector */
267 EFSYS_ASSERT3U(vpd_length, <=, size);
268 memcpy(data, (caddr_t)dcfg + vpd_offset, vpd_length);
270 /* Pad data with all-1s, consistent with update operations */
271 memset(data + vpd_length, 0xff, size - vpd_length);
273 EFSYS_KMEM_FREE(enp->en_esip, dcfg_size, dcfg);
280 EFSYS_KMEM_FREE(enp->en_esip, dcfg_size, dcfg);
282 EFSYS_PROBE1(fail1, efx_rc_t, rc);
287 __checkReturn efx_rc_t
290 __in_bcount(size) caddr_t data,
295 efx_vpd_keyword_t skey;
296 efx_vpd_keyword_t dkey;
302 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
305 * Strictly you could take the view that dynamic vpd is optional.
306 * Instead, to conform more closely to the read/verify/reinit()
307 * paradigm, we require dynamic vpd. siena_vpd_reinit() will
308 * reinitialize it as required.
310 if ((rc = efx_vpd_hunk_verify(data, size, NULL)) != 0)
314 * Verify that there is no duplication between the static and
315 * dynamic cfg sectors.
317 if (enp->en_u.siena.enu_svpd_length == 0)
321 _NOTE(CONSTANTCONDITION)
323 if ((rc = efx_vpd_hunk_next(data, size, &dtag,
324 &dkey, NULL, NULL, &dcont)) != 0)
330 * Skip the RV keyword. It should be present in both the static
331 * and dynamic cfg sectors.
333 if (dtag == EFX_VPD_RO && dkey == EFX_VPD_KEYWORD('R', 'V'))
337 _NOTE(CONSTANTCONDITION)
339 if ((rc = efx_vpd_hunk_next(
340 enp->en_u.siena.enu_svpd,
341 enp->en_u.siena.enu_svpd_length, &stag, &skey,
342 NULL, NULL, &scont)) != 0)
347 if (stag == dtag && skey == dkey) {
364 EFSYS_PROBE1(fail1, efx_rc_t, rc);
369 __checkReturn efx_rc_t
372 __in_bcount(size) caddr_t data,
379 * Only create a PID if the dynamic cfg doesn't have one
381 if (enp->en_u.siena.enu_svpd_length == 0)
387 rc = efx_vpd_hunk_get(enp->en_u.siena.enu_svpd,
388 enp->en_u.siena.enu_svpd_length,
389 EFX_VPD_ID, 0, &offset, &length);
392 else if (rc == ENOENT)
398 if ((rc = efx_vpd_hunk_reinit(data, size, wantpid)) != 0)
406 EFSYS_PROBE1(fail1, efx_rc_t, rc);
411 __checkReturn efx_rc_t
414 __in_bcount(size) caddr_t data,
416 __inout efx_vpd_value_t *evvp)
422 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
424 /* Attempt to satisfy the request from svpd first */
425 if (enp->en_u.siena.enu_svpd_length > 0) {
426 if ((rc = efx_vpd_hunk_get(enp->en_u.siena.enu_svpd,
427 enp->en_u.siena.enu_svpd_length, evvp->evv_tag,
428 evvp->evv_keyword, &offset, &length)) == 0) {
429 evvp->evv_length = length;
430 memcpy(evvp->evv_value,
431 enp->en_u.siena.enu_svpd + offset, length);
433 } else if (rc != ENOENT)
437 /* And then from the provided data buffer */
438 if ((rc = efx_vpd_hunk_get(data, size, evvp->evv_tag,
439 evvp->evv_keyword, &offset, &length)) != 0) {
446 evvp->evv_length = length;
447 memcpy(evvp->evv_value, data + offset, length);
454 EFSYS_PROBE1(fail1, efx_rc_t, rc);
459 __checkReturn efx_rc_t
462 __in_bcount(size) caddr_t data,
464 __in efx_vpd_value_t *evvp)
468 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
470 /* If the provided (tag,keyword) exists in svpd, then it is readonly */
471 if (enp->en_u.siena.enu_svpd_length > 0) {
475 if ((rc = efx_vpd_hunk_get(enp->en_u.siena.enu_svpd,
476 enp->en_u.siena.enu_svpd_length, evvp->evv_tag,
477 evvp->evv_keyword, &offset, &length)) == 0) {
483 if ((rc = efx_vpd_hunk_set(data, size, evvp)) != 0)
491 EFSYS_PROBE1(fail1, efx_rc_t, rc);
496 __checkReturn efx_rc_t
499 __in_bcount(size) caddr_t data,
501 __out efx_vpd_value_t *evvp,
502 __inout unsigned int *contp)
504 _NOTE(ARGUNUSED(enp, data, size, evvp, contp))
509 __checkReturn efx_rc_t
512 __in_bcount(size) caddr_t data,
515 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
516 siena_mc_dynamic_config_hdr_t *dcfg = NULL;
517 unsigned int vpd_offset;
518 unsigned int dcfg_partn;
519 unsigned int hdr_length;
522 size_t partn_size, dcfg_size;
526 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
528 /* Determine total length of all tags */
529 if ((rc = efx_vpd_hunk_length(data, size, &vpd_length)) != 0)
532 /* Lock dynamic config sector for write, and read structure only */
533 dcfg_partn = (emip->emi_port == 1)
534 ? MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0
535 : MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1;
537 if ((rc = siena_nvram_partn_size(enp, dcfg_partn, &partn_size)) != 0)
540 if ((rc = siena_nvram_partn_lock(enp, dcfg_partn)) != 0)
543 if ((rc = siena_nvram_get_dynamic_cfg(enp, dcfg_partn,
544 B_FALSE, &dcfg, &dcfg_size)) != 0)
547 hdr_length = EFX_WORD_FIELD(dcfg->length, EFX_WORD_0);
549 /* Allocated memory should have room for the new VPD */
550 if (hdr_length + vpd_length > dcfg_size) {
555 /* Copy in new vpd and update header */
556 vpd_offset = dcfg_size - vpd_length;
557 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_offset, EFX_DWORD_0, vpd_offset);
558 memcpy((caddr_t)dcfg + vpd_offset, data, vpd_length);
559 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_length, EFX_DWORD_0, vpd_length);
561 /* Update the checksum */
563 for (pos = 0; pos < hdr_length; pos++)
564 cksum += ((uint8_t *)dcfg)[pos];
565 dcfg->csum.eb_u8[0] -= cksum;
567 /* Erase and write the new sector */
568 if ((rc = siena_nvram_partn_erase(enp, dcfg_partn, 0, partn_size)) != 0)
571 /* Write out the new structure to nvram */
572 if ((rc = siena_nvram_partn_write(enp, dcfg_partn, 0, (caddr_t)dcfg,
573 vpd_offset + vpd_length)) != 0)
576 EFSYS_KMEM_FREE(enp->en_esip, dcfg_size, dcfg);
578 siena_nvram_partn_unlock(enp, dcfg_partn);
589 EFSYS_KMEM_FREE(enp->en_esip, dcfg_size, dcfg);
593 siena_nvram_partn_unlock(enp, dcfg_partn);
599 EFSYS_PROBE1(fail1, efx_rc_t, rc);
608 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
610 if (enp->en_u.siena.enu_svpd_length > 0) {
611 EFSYS_KMEM_FREE(enp->en_esip, enp->en_u.siena.enu_svpd_length,
612 enp->en_u.siena.enu_svpd);
614 enp->en_u.siena.enu_svpd = NULL;
615 enp->en_u.siena.enu_svpd_length = 0;
619 #endif /* EFSYS_OPT_SIENA */
621 #endif /* EFSYS_OPT_VPD */