2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009-2016 Solarflare Communications Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * The views and conclusions contained in the software and documentation are
29 * those of the authors and should not be interpreted as representing official
30 * policies, either expressed or implied, of the FreeBSD Project.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
43 static __checkReturn efx_rc_t
47 __deref_out_bcount_opt(*sizep) caddr_t *svpdp,
50 siena_mc_static_config_hdr_t *scfg;
54 unsigned int vpd_offset;
55 unsigned int vpd_length;
56 unsigned int hdr_length;
61 EFSYS_ASSERT(partn == MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT0 ||
62 partn == MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT1);
64 /* Allocate sufficient memory for the entire static cfg area */
65 if ((rc = siena_nvram_partn_size(enp, partn, &size)) != 0)
68 EFSYS_KMEM_ALLOC(enp->en_esip, size, scfg);
74 if ((rc = siena_nvram_partn_read(enp, partn, 0,
75 (caddr_t)scfg, SIENA_NVRAM_CHUNK)) != 0)
78 /* Verify the magic number */
79 if (EFX_DWORD_FIELD(scfg->magic, EFX_DWORD_0) !=
80 SIENA_MC_STATIC_CONFIG_MAGIC) {
85 /* All future versions of the structure must be backwards compatible */
86 EFX_STATIC_ASSERT(SIENA_MC_STATIC_CONFIG_VERSION == 0);
88 hdr_length = EFX_WORD_FIELD(scfg->length, EFX_WORD_0);
89 vpd_offset = EFX_DWORD_FIELD(scfg->static_vpd_offset, EFX_DWORD_0);
90 vpd_length = EFX_DWORD_FIELD(scfg->static_vpd_length, EFX_DWORD_0);
92 /* Verify the hdr doesn't overflow the sector size */
93 if (hdr_length > size || vpd_offset > size || vpd_length > size ||
94 vpd_length + vpd_offset > size) {
99 /* Read the remainder of scfg + static vpd */
100 region = vpd_offset + vpd_length;
101 if (region > SIENA_NVRAM_CHUNK) {
102 if ((rc = siena_nvram_partn_read(enp, partn, SIENA_NVRAM_CHUNK,
103 (caddr_t)scfg + SIENA_NVRAM_CHUNK,
104 region - SIENA_NVRAM_CHUNK)) != 0)
108 /* Verify checksum */
110 for (pos = 0; pos < hdr_length; pos++)
111 cksum += ((uint8_t *)scfg)[pos];
120 /* Copy the vpd data out */
121 EFSYS_KMEM_ALLOC(enp->en_esip, vpd_length, svpd);
126 memcpy(svpd, (caddr_t)scfg + vpd_offset, vpd_length);
129 EFSYS_KMEM_FREE(enp->en_esip, size, scfg);
149 EFSYS_KMEM_FREE(enp->en_esip, size, scfg);
154 EFSYS_PROBE1(fail1, efx_rc_t, rc);
159 __checkReturn efx_rc_t
163 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
169 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
171 partn = (emip->emi_port == 1)
172 ? MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT0
173 : MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT1;
176 * We need the static VPD sector to present a unified static+dynamic
177 * VPD, that is, basically on every read, write, verify cycle. Since
178 * it should *never* change we can just cache it here.
180 if ((rc = siena_vpd_get_static(enp, partn, &svpd, &size)) != 0)
183 if (svpd != NULL && size > 0) {
184 if ((rc = efx_vpd_hunk_verify(svpd, size, NULL)) != 0)
188 enp->en_u.siena.enu_svpd = svpd;
189 enp->en_u.siena.enu_svpd_length = size;
196 EFSYS_KMEM_FREE(enp->en_esip, size, svpd);
198 EFSYS_PROBE1(fail1, efx_rc_t, rc);
203 __checkReturn efx_rc_t
208 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
212 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
215 * This function returns the total size the user should allocate
216 * for all VPD operations. We've already cached the static vpd,
217 * so we just need to return an upper bound on the dynamic vpd.
218 * Since the dynamic_config structure can change under our feet,
219 * (as version numbers are inserted), just be safe and return the
220 * total size of the dynamic_config *sector*
222 partn = (emip->emi_port == 1)
223 ? MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0
224 : MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1;
226 if ((rc = siena_nvram_partn_size(enp, partn, sizep)) != 0)
232 EFSYS_PROBE1(fail1, efx_rc_t, rc);
237 __checkReturn efx_rc_t
240 __out_bcount(size) caddr_t data,
243 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
244 siena_mc_dynamic_config_hdr_t *dcfg = NULL;
245 unsigned int vpd_length;
246 unsigned int vpd_offset;
247 unsigned int dcfg_partn;
251 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
253 dcfg_partn = (emip->emi_port == 1)
254 ? MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0
255 : MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1;
257 if ((rc = siena_nvram_get_dynamic_cfg(enp, dcfg_partn,
258 B_TRUE, &dcfg, &dcfg_size)) != 0)
261 vpd_length = EFX_DWORD_FIELD(dcfg->dynamic_vpd_length, EFX_DWORD_0);
262 vpd_offset = EFX_DWORD_FIELD(dcfg->dynamic_vpd_offset, EFX_DWORD_0);
264 if (vpd_length > size) {
265 rc = EFAULT; /* Invalid dcfg: header bigger than sector */
269 EFSYS_ASSERT3U(vpd_length, <=, size);
270 memcpy(data, (caddr_t)dcfg + vpd_offset, vpd_length);
272 /* Pad data with all-1s, consistent with update operations */
273 memset(data + vpd_length, 0xff, size - vpd_length);
275 EFSYS_KMEM_FREE(enp->en_esip, dcfg_size, dcfg);
282 EFSYS_KMEM_FREE(enp->en_esip, dcfg_size, dcfg);
284 EFSYS_PROBE1(fail1, efx_rc_t, rc);
289 __checkReturn efx_rc_t
292 __in_bcount(size) caddr_t data,
297 efx_vpd_keyword_t skey;
298 efx_vpd_keyword_t dkey;
304 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
307 * Strictly you could take the view that dynamic vpd is optional.
308 * Instead, to conform more closely to the read/verify/reinit()
309 * paradigm, we require dynamic vpd. siena_vpd_reinit() will
310 * reinitialize it as required.
312 if ((rc = efx_vpd_hunk_verify(data, size, NULL)) != 0)
316 * Verify that there is no duplication between the static and
317 * dynamic cfg sectors.
319 if (enp->en_u.siena.enu_svpd_length == 0)
323 _NOTE(CONSTANTCONDITION)
325 if ((rc = efx_vpd_hunk_next(data, size, &dtag,
326 &dkey, NULL, NULL, &dcont)) != 0)
332 * Skip the RV keyword. It should be present in both the static
333 * and dynamic cfg sectors.
335 if (dtag == EFX_VPD_RO && dkey == EFX_VPD_KEYWORD('R', 'V'))
339 _NOTE(CONSTANTCONDITION)
341 if ((rc = efx_vpd_hunk_next(
342 enp->en_u.siena.enu_svpd,
343 enp->en_u.siena.enu_svpd_length, &stag, &skey,
344 NULL, NULL, &scont)) != 0)
349 if (stag == dtag && skey == dkey) {
366 EFSYS_PROBE1(fail1, efx_rc_t, rc);
371 __checkReturn efx_rc_t
374 __in_bcount(size) caddr_t data,
381 * Only create a PID if the dynamic cfg doesn't have one
383 if (enp->en_u.siena.enu_svpd_length == 0)
389 rc = efx_vpd_hunk_get(enp->en_u.siena.enu_svpd,
390 enp->en_u.siena.enu_svpd_length,
391 EFX_VPD_ID, 0, &offset, &length);
394 else if (rc == ENOENT)
400 if ((rc = efx_vpd_hunk_reinit(data, size, wantpid)) != 0)
408 EFSYS_PROBE1(fail1, efx_rc_t, rc);
413 __checkReturn efx_rc_t
416 __in_bcount(size) caddr_t data,
418 __inout efx_vpd_value_t *evvp)
424 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
426 /* Attempt to satisfy the request from svpd first */
427 if (enp->en_u.siena.enu_svpd_length > 0) {
428 if ((rc = efx_vpd_hunk_get(enp->en_u.siena.enu_svpd,
429 enp->en_u.siena.enu_svpd_length, evvp->evv_tag,
430 evvp->evv_keyword, &offset, &length)) == 0) {
431 evvp->evv_length = length;
432 memcpy(evvp->evv_value,
433 enp->en_u.siena.enu_svpd + offset, length);
435 } else if (rc != ENOENT)
439 /* And then from the provided data buffer */
440 if ((rc = efx_vpd_hunk_get(data, size, evvp->evv_tag,
441 evvp->evv_keyword, &offset, &length)) != 0) {
448 evvp->evv_length = length;
449 memcpy(evvp->evv_value, data + offset, length);
456 EFSYS_PROBE1(fail1, efx_rc_t, rc);
461 __checkReturn efx_rc_t
464 __in_bcount(size) caddr_t data,
466 __in efx_vpd_value_t *evvp)
470 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
472 /* If the provided (tag,keyword) exists in svpd, then it is readonly */
473 if (enp->en_u.siena.enu_svpd_length > 0) {
477 if ((rc = efx_vpd_hunk_get(enp->en_u.siena.enu_svpd,
478 enp->en_u.siena.enu_svpd_length, evvp->evv_tag,
479 evvp->evv_keyword, &offset, &length)) == 0) {
485 if ((rc = efx_vpd_hunk_set(data, size, evvp)) != 0)
493 EFSYS_PROBE1(fail1, efx_rc_t, rc);
498 __checkReturn efx_rc_t
501 __in_bcount(size) caddr_t data,
503 __out efx_vpd_value_t *evvp,
504 __inout unsigned int *contp)
506 _NOTE(ARGUNUSED(enp, data, size, evvp, contp))
511 __checkReturn efx_rc_t
514 __in_bcount(size) caddr_t data,
517 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
518 siena_mc_dynamic_config_hdr_t *dcfg = NULL;
519 unsigned int vpd_offset;
520 unsigned int dcfg_partn;
521 unsigned int hdr_length;
524 size_t partn_size, dcfg_size;
528 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
530 /* Determine total length of all tags */
531 if ((rc = efx_vpd_hunk_length(data, size, &vpd_length)) != 0)
534 /* Lock dynamic config sector for write, and read structure only */
535 dcfg_partn = (emip->emi_port == 1)
536 ? MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0
537 : MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1;
539 if ((rc = siena_nvram_partn_size(enp, dcfg_partn, &partn_size)) != 0)
542 if ((rc = siena_nvram_partn_lock(enp, dcfg_partn)) != 0)
545 if ((rc = siena_nvram_get_dynamic_cfg(enp, dcfg_partn,
546 B_FALSE, &dcfg, &dcfg_size)) != 0)
549 hdr_length = EFX_WORD_FIELD(dcfg->length, EFX_WORD_0);
551 /* Allocated memory should have room for the new VPD */
552 if (hdr_length + vpd_length > dcfg_size) {
557 /* Copy in new vpd and update header */
558 vpd_offset = dcfg_size - vpd_length;
559 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_offset, EFX_DWORD_0, vpd_offset);
560 memcpy((caddr_t)dcfg + vpd_offset, data, vpd_length);
561 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_length, EFX_DWORD_0, vpd_length);
563 /* Update the checksum */
565 for (pos = 0; pos < hdr_length; pos++)
566 cksum += ((uint8_t *)dcfg)[pos];
567 dcfg->csum.eb_u8[0] -= cksum;
569 /* Erase and write the new sector */
570 if ((rc = siena_nvram_partn_erase(enp, dcfg_partn, 0, partn_size)) != 0)
573 /* Write out the new structure to nvram */
574 if ((rc = siena_nvram_partn_write(enp, dcfg_partn, 0, (caddr_t)dcfg,
575 vpd_offset + vpd_length)) != 0)
578 EFSYS_KMEM_FREE(enp->en_esip, dcfg_size, dcfg);
580 siena_nvram_partn_unlock(enp, dcfg_partn);
591 EFSYS_KMEM_FREE(enp->en_esip, dcfg_size, dcfg);
595 siena_nvram_partn_unlock(enp, dcfg_partn);
601 EFSYS_PROBE1(fail1, efx_rc_t, rc);
610 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
612 if (enp->en_u.siena.enu_svpd_length > 0) {
613 EFSYS_KMEM_FREE(enp->en_esip, enp->en_u.siena.enu_svpd_length,
614 enp->en_u.siena.enu_svpd);
616 enp->en_u.siena.enu_svpd = NULL;
617 enp->en_u.siena.enu_svpd_length = 0;
621 #endif /* EFSYS_OPT_SIENA */
623 #endif /* EFSYS_OPT_VPD */