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$");
36 #include "efx_types.h"
44 static __checkReturn int
47 __in unsigned int partn,
48 __deref_out_bcount_opt(*sizep) caddr_t *svpdp,
51 siena_mc_static_config_hdr_t *scfg;
55 unsigned int vpd_offset;
56 unsigned int vpd_length;
57 unsigned int hdr_length;
62 EFSYS_ASSERT(partn == MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT0 ||
63 partn == MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT1);
65 /* Allocate sufficient memory for the entire static cfg area */
66 if ((rc = siena_nvram_partn_size(enp, partn, &size)) != 0)
69 EFSYS_KMEM_ALLOC(enp->en_esip, size, scfg);
75 if ((rc = siena_nvram_partn_read(enp, partn, 0,
76 (caddr_t)scfg, SIENA_NVRAM_CHUNK)) != 0)
79 /* Verify the magic number */
80 if (EFX_DWORD_FIELD(scfg->magic, EFX_DWORD_0) !=
81 SIENA_MC_STATIC_CONFIG_MAGIC) {
86 /* All future versions of the structure must be backwards compatable */
87 EFX_STATIC_ASSERT(SIENA_MC_STATIC_CONFIG_VERSION == 0);
89 hdr_length = EFX_WORD_FIELD(scfg->length, EFX_WORD_0);
90 vpd_offset = EFX_DWORD_FIELD(scfg->static_vpd_offset, EFX_DWORD_0);
91 vpd_length = EFX_DWORD_FIELD(scfg->static_vpd_length, EFX_DWORD_0);
93 /* Verify the hdr doesn't overflow the sector size */
94 if (hdr_length > size || vpd_offset > size || vpd_length > size ||
95 vpd_length + vpd_offset > size) {
100 /* Read the remainder of scfg + static vpd */
101 region = vpd_offset + vpd_length;
102 if (region > SIENA_NVRAM_CHUNK) {
103 if ((rc = siena_nvram_partn_read(enp, partn, SIENA_NVRAM_CHUNK,
104 (caddr_t)scfg + SIENA_NVRAM_CHUNK,
105 region - SIENA_NVRAM_CHUNK)) != 0)
109 /* Verify checksum */
111 for (pos = 0; pos < hdr_length; pos++)
112 cksum += ((uint8_t *)scfg)[pos];
121 /* Copy the vpd data out */
122 EFSYS_KMEM_ALLOC(enp->en_esip, vpd_length, svpd);
127 memcpy(svpd, (caddr_t)scfg + vpd_offset, vpd_length);
130 EFSYS_KMEM_FREE(enp->en_esip, size, scfg);
150 EFSYS_KMEM_FREE(enp->en_esip, size, scfg);
155 EFSYS_PROBE1(fail1, int, rc);
164 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
170 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
172 partn = (emip->emi_port == 1)
173 ? MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT0
174 : MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT1;
177 * We need the static VPD sector to present a unified static+dynamic
178 * VPD, that is, basically on every read, write, verify cycle. Since
179 * it should *never* change we can just cache it here.
181 if ((rc = siena_vpd_get_static(enp, partn, &svpd, &size)) != 0)
184 if (svpd != NULL && size > 0) {
185 if ((rc = efx_vpd_hunk_verify(svpd, size, NULL)) != 0)
189 enp->en_u.siena.enu_svpd = svpd;
190 enp->en_u.siena.enu_svpd_length = size;
197 EFSYS_KMEM_FREE(enp->en_esip, size, svpd);
199 EFSYS_PROBE1(fail1, int, rc);
209 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
213 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
216 * This function returns the total size the user should allocate
217 * for all VPD operations. We've already cached the static vpd,
218 * so we just need to return an upper bound on the dynamic vpd.
219 * Since the dynamic_config structure can change under our feet,
220 * (as version numbers are inserted), just be safe and return the
221 * total size of the dynamic_config *sector*
223 partn = (emip->emi_port == 1)
224 ? MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0
225 : MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1;
227 if ((rc = siena_nvram_partn_size(enp, partn, sizep)) != 0)
233 EFSYS_PROBE1(fail1, int, rc);
241 __out_bcount(size) caddr_t data,
244 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
245 siena_mc_dynamic_config_hdr_t *dcfg = NULL;
246 unsigned int vpd_length;
247 unsigned int vpd_offset;
248 unsigned int dcfg_partn;
252 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
254 dcfg_partn = (emip->emi_port == 1)
255 ? MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0
256 : MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1;
258 if ((rc = siena_nvram_get_dynamic_cfg(enp, dcfg_partn,
259 B_TRUE, &dcfg, &dcfg_size)) != 0)
262 vpd_length = EFX_DWORD_FIELD(dcfg->dynamic_vpd_length, EFX_DWORD_0);
263 vpd_offset = EFX_DWORD_FIELD(dcfg->dynamic_vpd_offset, EFX_DWORD_0);
265 if (vpd_length > size) {
266 rc = EFAULT; /* Invalid dcfg: header bigger than sector */
270 EFSYS_ASSERT3U(vpd_length, <=, size);
271 memcpy(data, (caddr_t)dcfg + vpd_offset, vpd_length);
273 /* Pad data with all-1s, consistent with update operations */
274 memset(data + vpd_length, 0xff, size - vpd_length);
276 EFSYS_KMEM_FREE(enp->en_esip, dcfg_size, dcfg);
283 EFSYS_KMEM_FREE(enp->en_esip, dcfg_size, dcfg);
285 EFSYS_PROBE1(fail1, int, rc);
293 __in_bcount(size) caddr_t data,
298 efx_vpd_keyword_t skey;
299 efx_vpd_keyword_t dkey;
305 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
308 * Strictly you could take the view that dynamic vpd is optional.
309 * Instead, to conform more closely to the read/verify/reinit()
310 * paradigm, we require dynamic vpd. siena_vpd_reinit() will
311 * reinitialize it as required.
313 if ((rc = efx_vpd_hunk_verify(data, size, NULL)) != 0)
317 * Verify that there is no duplication between the static and
318 * dynamic cfg sectors.
320 if (enp->en_u.siena.enu_svpd_length == 0)
324 _NOTE(CONSTANTCONDITION)
326 if ((rc = efx_vpd_hunk_next(data, size, &dtag,
327 &dkey, NULL, NULL, &dcont)) != 0)
333 _NOTE(CONSTANTCONDITION)
335 if ((rc = efx_vpd_hunk_next(
336 enp->en_u.siena.enu_svpd,
337 enp->en_u.siena.enu_svpd_length, &stag, &skey,
338 NULL, NULL, &scont)) != 0)
343 if (stag == dtag && skey == dkey) {
360 EFSYS_PROBE1(fail1, int, rc);
368 __in_bcount(size) caddr_t data,
375 * Only create a PID if the dynamic cfg doesn't have one
377 if (enp->en_u.siena.enu_svpd_length == 0)
383 rc = efx_vpd_hunk_get(enp->en_u.siena.enu_svpd,
384 enp->en_u.siena.enu_svpd_length,
385 EFX_VPD_ID, 0, &offset, &length);
388 else if (rc == ENOENT)
394 if ((rc = efx_vpd_hunk_reinit(data, size, wantpid)) != 0)
402 EFSYS_PROBE1(fail1, int, rc);
410 __in_bcount(size) caddr_t data,
412 __inout efx_vpd_value_t *evvp)
418 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
420 /* Attempt to satisfy the request from svpd first */
421 if (enp->en_u.siena.enu_svpd_length > 0) {
422 if ((rc = efx_vpd_hunk_get(enp->en_u.siena.enu_svpd,
423 enp->en_u.siena.enu_svpd_length, evvp->evv_tag,
424 evvp->evv_keyword, &offset, &length)) == 0) {
425 evvp->evv_length = length;
426 memcpy(evvp->evv_value,
427 enp->en_u.siena.enu_svpd + offset, length);
429 } else if (rc != ENOENT)
433 /* And then from the provided data buffer */
434 if ((rc = efx_vpd_hunk_get(data, size, evvp->evv_tag,
435 evvp->evv_keyword, &offset, &length)) != 0)
438 evvp->evv_length = length;
439 memcpy(evvp->evv_value, data + offset, length);
446 EFSYS_PROBE1(fail1, int, rc);
454 __in_bcount(size) caddr_t data,
456 __in efx_vpd_value_t *evvp)
460 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
462 /* If the provided (tag,keyword) exists in svpd, then it is readonly */
463 if (enp->en_u.siena.enu_svpd_length > 0) {
467 if ((rc = efx_vpd_hunk_get(enp->en_u.siena.enu_svpd,
468 enp->en_u.siena.enu_svpd_length, evvp->evv_tag,
469 evvp->evv_keyword, &offset, &length)) == 0) {
475 if ((rc = efx_vpd_hunk_set(data, size, evvp)) != 0)
483 EFSYS_PROBE1(fail1, int, rc);
491 __in_bcount(size) caddr_t data,
493 __out efx_vpd_value_t *evvp,
494 __inout unsigned int *contp)
496 _NOTE(ARGUNUSED(enp, data, size, evvp, contp))
504 __in_bcount(size) caddr_t data,
507 efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
508 siena_mc_dynamic_config_hdr_t *dcfg = NULL;
509 unsigned int vpd_offset;
510 unsigned int dcfg_partn;
511 unsigned int hdr_length;
514 size_t partn_size, dcfg_size;
518 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
520 /* Determine total length of all tags */
521 if ((rc = efx_vpd_hunk_length(data, size, &vpd_length)) != 0)
524 /* Lock dynamic config sector for write, and read structure only */
525 dcfg_partn = (emip->emi_port == 1)
526 ? MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0
527 : MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1;
529 if ((rc = siena_nvram_partn_size(enp, dcfg_partn, &partn_size)) != 0)
532 if ((rc = siena_nvram_partn_lock(enp, dcfg_partn)) != 0)
535 if ((rc = siena_nvram_get_dynamic_cfg(enp, dcfg_partn,
536 B_FALSE, &dcfg, &dcfg_size)) != 0)
539 hdr_length = EFX_WORD_FIELD(dcfg->length, EFX_WORD_0);
541 /* Allocated memory should have room for the new VPD */
542 if (hdr_length + vpd_length > dcfg_size) {
547 /* Copy in new vpd and update header */
548 vpd_offset = dcfg_size - vpd_length;
549 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_offset, EFX_DWORD_0, vpd_offset);
550 memcpy((caddr_t)dcfg + vpd_offset, data, vpd_length);
551 EFX_POPULATE_DWORD_1(dcfg->dynamic_vpd_length, EFX_DWORD_0, vpd_length);
553 /* Update the checksum */
555 for (pos = 0; pos < hdr_length; pos++)
556 cksum += ((uint8_t *)dcfg)[pos];
557 dcfg->csum.eb_u8[0] -= cksum;
559 /* Erase and write the new sector */
560 if ((rc = siena_nvram_partn_erase(enp, dcfg_partn, 0, partn_size)) != 0)
563 /* Write out the new structure to nvram */
564 if ((rc = siena_nvram_partn_write(enp, dcfg_partn, 0, (caddr_t)dcfg,
565 vpd_offset + vpd_length)) != 0)
568 EFSYS_KMEM_FREE(enp->en_esip, dcfg_size, dcfg);
570 siena_nvram_partn_unlock(enp, dcfg_partn);
581 EFSYS_KMEM_FREE(enp->en_esip, dcfg_size, dcfg);
585 siena_nvram_partn_unlock(enp, dcfg_partn);
591 EFSYS_PROBE1(fail1, int, rc);
600 EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
602 if (enp->en_u.siena.enu_svpd_length > 0) {
603 EFSYS_KMEM_FREE(enp->en_esip, enp->en_u.siena.enu_svpd_length,
604 enp->en_u.siena.enu_svpd);
606 enp->en_u.siena.enu_svpd = NULL;
607 enp->en_u.siena.enu_svpd_length = 0;
611 #endif /* EFSYS_OPT_SIENA */
613 #endif /* EFSYS_OPT_VPD */