Path: blob/main/sys/dev/bhnd/nvram/bhnd_nvram_data_spromvar.h
39536 views
/*-1* Copyright (c) 2015-2016 Landon Fuller <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer,9* without modification.10* 2. Redistributions in binary form must reproduce at minimum a disclaimer11* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any12* redistribution must be conditioned upon including a substantially13* similar Disclaimer requirement for further binary redistribution.14*15* NO WARRANTY16* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18* LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY19* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL20* THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,21* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF22* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS23* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER24* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)25* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF26* THE POSSIBILITY OF SUCH DAMAGES.27*28*/2930#ifndef _BHND_NVRAM_BHND_NVRAM_SPROMVAR_H_31#define _BHND_NVRAM_BHND_NVRAM_SPROMVAR_H_3233#ifdef _KERNEL34#include <sys/bitstring.h>35#else36#include <bitstring.h>37#endif3839#include "bhnd_nvram_private.h"4041#include "bhnd_nvram_datavar.h"42#include "bhnd_nvram_io.h"4344/** The maximum number of array elements encoded in a single SPROM variable */45#define BHND_SPROM_ARRAY_MAXLEN 124647typedef struct bhnd_sprom_opcode_state bhnd_sprom_opcode_state;48typedef struct bhnd_sprom_opcode_bind bhnd_sprom_opcode_bind;49typedef struct bhnd_sprom_opcode_var bhnd_sprom_opcode_var;50typedef struct bhnd_sprom_opcode_idx_entry bhnd_sprom_opcode_idx_entry;5152int bhnd_sprom_opcode_init(53bhnd_sprom_opcode_state *state,54const bhnd_sprom_layout *layout);55void bhnd_sprom_opcode_fini(56bhnd_sprom_opcode_state *state);5758bhnd_sprom_opcode_idx_entry *bhnd_sprom_opcode_index_find(59bhnd_sprom_opcode_state *state,60const char *name);61bhnd_sprom_opcode_idx_entry *bhnd_sprom_opcode_index_next(62bhnd_sprom_opcode_state *state,63bhnd_sprom_opcode_idx_entry *prev);6465int bhnd_sprom_opcode_init_entry(66bhnd_sprom_opcode_state *state,67bhnd_sprom_opcode_idx_entry *entry);6869int bhnd_sprom_opcode_eval_var(70bhnd_sprom_opcode_state *state,71bhnd_sprom_opcode_idx_entry *entry);7273int bhnd_sprom_opcode_seek(74bhnd_sprom_opcode_state *state,75bhnd_sprom_opcode_idx_entry *entry);76int bhnd_sprom_opcode_next_var(77bhnd_sprom_opcode_state *state);78int bhnd_sprom_opcode_next_binding(79bhnd_sprom_opcode_state *state);80int bhnd_sprom_opcode_apply_scale(81bhnd_sprom_opcode_state *state,82uint32_t *value);8384/**85* SPROM opcode per-bind evaluation state.86*/87struct bhnd_sprom_opcode_bind {88uint8_t count;89uint32_t skip_in; /**< input element skips */90bool skip_in_negative; /**< skip_in should be subtracted */91uint32_t skip_out; /**< output element skip */92};9394/**95* SPROM opcode per-variable evaluation state.96*/97struct bhnd_sprom_opcode_var {98uint8_t nelem; /**< variable array length */99uint32_t mask; /**< current bind input mask */100int8_t shift; /**< current bind input shift */101bhnd_nvram_type base_type; /**< current bind input type */102uint32_t scale; /**< current scale to apply to scaled encodings */103bhnd_sprom_opcode_bind bind; /**< current bind state */104bool have_bind; /**< if bind state is defined */105size_t bind_total; /**< total count of bind operations performed */106};107108/**109* SPROM opcode variable definition states.110*111* Ordered to support inequality comparisons112* (e.g. >= SPROM_OPCODE_VAR_STATE_OPEN)113*/114typedef enum {115SPROM_OPCODE_VAR_STATE_NONE = 1, /**< no variable entry available */116SPROM_OPCODE_VAR_STATE_OPEN = 2, /**< currently parsing a variable entry */117SPROM_OPCODE_VAR_STATE_DONE = 3 /**< full variable entry has been parsed */118} bhnd_sprom_opcode_var_state;119120/**121* SPROM opcode evaluation state122*/123struct bhnd_sprom_opcode_state {124const bhnd_sprom_layout *layout; /**< SPROM layout */125126bhnd_sprom_opcode_idx_entry *idx; /**< variable index (NULL during initialization) */127size_t num_idx; /**< variable index entry count */128129/** Current SPROM revision range */130bitstr_t bit_decl(revs, SPROM_OP_REV_MAX);131132const uint8_t *input; /**< opcode input position */133134/* State preserved across variable definitions */135uint32_t offset; /**< SPROM offset */136size_t vid; /**< Variable ID */137138/* State reset after end of each variable definition */139bhnd_sprom_opcode_var var; /**< variable record (if any) */140bhnd_sprom_opcode_var_state var_state; /**< variable record state */141};142143/**144* SPROM opcode variable index entry145*/146struct bhnd_sprom_opcode_idx_entry {147uint16_t vid; /**< SPROM variable ID */148uint16_t offset; /**< SPROM input offset */149uint16_t opcodes; /**< SPROM opcode offset */150};151152/**153* SPROM value storage.154*155* Sufficient for representing the native encoding of any defined SPROM156* variable.157*/158union bhnd_nvram_sprom_storage {159uint8_t u8[BHND_SPROM_ARRAY_MAXLEN];160uint16_t u16[BHND_SPROM_ARRAY_MAXLEN];161uint32_t u32[BHND_SPROM_ARRAY_MAXLEN];162int8_t i8[BHND_SPROM_ARRAY_MAXLEN];163int16_t i16[BHND_SPROM_ARRAY_MAXLEN];164int32_t i32[BHND_SPROM_ARRAY_MAXLEN];165char ch[BHND_SPROM_ARRAY_MAXLEN];166};167168/**169* SPROM data class instance state.170*/171struct bhnd_nvram_sprom {172struct bhnd_nvram_data nv; /**< common instance state */173struct bhnd_nvram_io *data; /**< backing SPROM image */174const bhnd_sprom_layout *layout; /**< layout definition */175bhnd_sprom_opcode_state state; /**< opcode eval state */176};177178#endif /* _BHND_NVRAM_BHND_NVRAM_SPROMVAR_H_ */179180181