Path: blob/main/sys/dev/bhnd/nvram/bhnd_nvram_storevar.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_STOREVAR_H_31#define _BHND_NVRAM_BHND_NVRAM_STOREVAR_H_3233#include <sys/types.h>3435#ifndef _KERNEL36#include <pthread.h>37#endif3839#include "bhnd_nvram_plist.h"4041#include "bhnd_nvram_store.h"4243/** Index is only generated if minimum variable count is met */44#define BHND_NV_IDX_VAR_THRESHOLD 154546#define BHND_NVSTORE_ROOT_PATH "/"47#define BHND_NVSTORE_ROOT_PATH_LEN sizeof(BHND_NVSTORE_ROOT_PATH)4849#define BHND_NVSTORE_GET_FLAG(_value, _flag) \50(((_value) & BHND_NVSTORE_ ## _flag) != 0)51#define BHND_NVSTORE_GET_BITS(_value, _field) \52((_value) & BHND_NVSTORE_ ## _field ## _MASK)5354/* Forward declarations */55typedef struct bhnd_nvstore_name_info bhnd_nvstore_name_info;56typedef struct bhnd_nvstore_index bhnd_nvstore_index;57typedef struct bhnd_nvstore_path bhnd_nvstore_path;5859typedef struct bhnd_nvstore_alias bhnd_nvstore_alias;6061typedef struct bhnd_nvstore_alias_list bhnd_nvstore_alias_list;62typedef struct bhnd_nvstore_update_list bhnd_nvstore_update_list;63typedef struct bhnd_nvstore_path_list bhnd_nvstore_path_list;6465LIST_HEAD(bhnd_nvstore_alias_list, bhnd_nvstore_alias);66LIST_HEAD(bhnd_nvstore_update_list, bhnd_nvstore_update);67LIST_HEAD(bhnd_nvstore_path_list, bhnd_nvstore_path);6869/**70* NVRAM store variable entry types.71*/72typedef enum {73BHND_NVSTORE_VAR = 0, /**< simple variable (var=...) */74BHND_NVSTORE_ALIAS_DECL = 1, /**< alias declaration ('devpath0=pci/1/1') */75} bhnd_nvstore_var_type;7677/**78* NVRAM path descriptor types.79*/80typedef enum {81BHND_NVSTORE_PATH_STRING = 0, /**< path is a string value */82BHND_NVSTORE_PATH_ALIAS = 1 /**< path is an alias reference */83} bhnd_nvstore_path_type;8485/**86* NVRAM variable namespaces.87*/88typedef enum {89BHND_NVSTORE_NAME_INTERNAL = 1, /**< internal namespace. permits90use of reserved devpath and91alias name prefixes. */92BHND_NVSTORE_NAME_EXTERNAL = 2, /**< external namespace. forbids93use of name prefixes used94for device path handling */95} bhnd_nvstore_name_type;9697bhnd_nvstore_path *bhnd_nvstore_path_new(const char *path_str,98size_t path_len);99void bhnd_nvstore_path_free(struct bhnd_nvstore_path *path);100101bhnd_nvstore_index *bhnd_nvstore_index_new(size_t capacity);102void bhnd_nvstore_index_free(bhnd_nvstore_index *index);103int bhnd_nvstore_index_append(struct bhnd_nvram_store *sc,104bhnd_nvstore_index *index,105void *cookiep);106int bhnd_nvstore_index_prepare(107struct bhnd_nvram_store *sc,108bhnd_nvstore_index *index);109void *bhnd_nvstore_index_lookup(struct bhnd_nvram_store *sc,110bhnd_nvstore_index *index, const char *name);111112bhnd_nvstore_path *bhnd_nvstore_get_root_path(113struct bhnd_nvram_store *sc);114bool bhnd_nvstore_is_root_path(struct bhnd_nvram_store *sc,115bhnd_nvstore_path *path);116117void *bhnd_nvstore_path_data_next(118struct bhnd_nvram_store *sc,119bhnd_nvstore_path *path, void **indexp);120void *bhnd_nvstore_path_data_lookup(121struct bhnd_nvram_store *sc,122bhnd_nvstore_path *path, const char *name);123bhnd_nvram_prop *bhnd_nvstore_path_get_update(124struct bhnd_nvram_store *sc,125bhnd_nvstore_path *path, const char *name);126int bhnd_nvstore_path_register_update(127struct bhnd_nvram_store *sc,128bhnd_nvstore_path *path, const char *name,129bhnd_nvram_val *value);130131bhnd_nvstore_alias *bhnd_nvstore_find_alias(struct bhnd_nvram_store *sc,132const char *path);133bhnd_nvstore_alias *bhnd_nvstore_get_alias(struct bhnd_nvram_store *sc,134u_long alias_val);135136bhnd_nvstore_path *bhnd_nvstore_get_path(struct bhnd_nvram_store *sc,137const char *path, size_t path_len);138bhnd_nvstore_path *bhnd_nvstore_resolve_path_alias(139struct bhnd_nvram_store *sc, u_long aval);140141bhnd_nvstore_path *bhnd_nvstore_var_get_path(struct bhnd_nvram_store *sc,142bhnd_nvstore_name_info *info);143int bhnd_nvstore_var_register_path(144struct bhnd_nvram_store *sc,145bhnd_nvstore_name_info *info, void *cookiep);146147int bhnd_nvstore_register_path(struct bhnd_nvram_store *sc,148const char *path, size_t path_len);149int bhnd_nvstore_register_alias(150struct bhnd_nvram_store *sc,151const bhnd_nvstore_name_info *info, void *cookiep);152153const char *bhnd_nvstore_parse_relpath(const char *parent,154const char *child);155int bhnd_nvstore_parse_name_info(const char *name,156bhnd_nvstore_name_type name_type,157uint32_t data_caps, bhnd_nvstore_name_info *info);158159/**160* NVRAM variable name descriptor.161*162* For NVRAM data instances supporting BHND_NVRAM_DATA_CAP_DEVPATHS, the163* NVRAM-vended variable name will be in one of four formats:164*165* - Simple Variable:166* 'variable'167* - Device Variable:168* 'pci/1/1/variable'169* - Device Alias Variable:170* '0:variable'171* - Device Path Alias Definition:172* 'devpath0=pci/1/1/variable'173*174* Device Paths:175*176* The device path format is device class-specific; the known supported device177* classes are:178* - sb: BCMA/SIBA SoC core device path.179* - pci: PCI device path (and PCIe on some earlier devices).180* - pcie: PCIe device path.181* - usb: USB device path.182*183* The device path format is loosely defined as '[class]/[domain]/[bus]/[slot]',184* with missing values either assumed to be zero, a value specific to the185* device class, or irrelevant to the device class in question.186*187* Examples:188* sb/1 BCMA/SIBA backplane 0, core 1.189* pc/1/1 PCMCIA bus 1, slot 1190* pci/1/1 PCI/PCIe domain 0, bus 1, device 1191* pcie/1/1 PCIe domain 0, bus 1, device 1192* usb/0xbd17 USB PID 0xbd17 (VID defaults to Broadcom 0x0a5c)193*194* Device Path Aliases:195*196* Device path aliases reduce duplication of device paths in the flash encoding197* of NVRAM data; a single devpath[alias]=[devpath] variable entry is defined,198* and then later variables may reference the device path via its alias:199* devpath1=usb/0xbd17200* 1:mcs5gpo0=0x1100201*202* Alias values are always positive, base 10 integers.203*/204struct bhnd_nvstore_name_info {205const char *name; /**< variable name */206bhnd_nvstore_var_type type; /**< variable type */207bhnd_nvstore_path_type path_type; /**< path type */208209/** Path information */210union {211/* BHND_NVSTORE_PATH_STRING */212struct {213const char *value; /**< device path */214size_t value_len; /**< device path length */215} str;216217/** BHND_NVSTORE_PATH_ALIAS */218struct {219u_long value; /**< device alias */220} alias;221} path;222};223224/**225* NVRAM variable index.226*227* Provides effecient name-based lookup by maintaining an array of cached228* cookiep values, sorted lexicographically by relative variable name.229*/230struct bhnd_nvstore_index {231size_t count; /**< entry count */232size_t capacity; /**< entry capacity */233void *cookiep[]; /**< cookiep values */234};235236/**237* NVRAM device path.238*/239struct bhnd_nvstore_path {240char *path_str; /**< canonical path string */241size_t num_vars; /**< per-path count of committed242(non-pending) variables */243bhnd_nvstore_index *index; /**< per-path index, or NULL if244this is a root path for245which the data source246may be queried directly. */247bhnd_nvram_plist *pending; /**< pending changes */248249LIST_ENTRY(bhnd_nvstore_path) np_link;250};251252/**253* NVRAM device path alias.254*/255struct bhnd_nvstore_alias {256bhnd_nvstore_path *path; /**< borrowed path reference */257void *cookiep; /**< NVRAM variable's cookiep value */258u_long alias; /**< alias value */259260LIST_ENTRY(bhnd_nvstore_alias) na_link;261};262263/** bhnd nvram store instance state */264struct bhnd_nvram_store {265#ifdef _KERNEL266struct mtx mtx;267#else268pthread_mutex_t mtx;269#endif270struct bhnd_nvram_data *data; /**< backing data */271uint32_t data_caps; /**< data capability flags */272bhnd_nvram_plist *data_opts; /**< data serialization options */273274bhnd_nvstore_alias_list aliases[4]; /**< path alias hash table */275size_t num_aliases; /**< alias count */276277bhnd_nvstore_path *root_path; /**< root path instance */278bhnd_nvstore_path_list paths[4]; /**< path hash table */279size_t num_paths; /**< path count */280};281282#ifdef _KERNEL283284#define BHND_NVSTORE_LOCK_INIT(sc) \285mtx_init(&(sc)->mtx, "BHND NVRAM store lock", NULL, MTX_DEF)286#define BHND_NVSTORE_LOCK(sc) mtx_lock(&(sc)->mtx)287#define BHND_NVSTORE_UNLOCK(sc) mtx_unlock(&(sc)->mtx)288#define BHND_NVSTORE_LOCK_ASSERT(sc, what) mtx_assert(&(sc)->mtx, what)289#define BHND_NVSTORE_LOCK_DESTROY(sc) mtx_destroy(&(sc)->mtx)290291#else /* !_KERNEL */292293#define BHND_NVSTORE_LOCK_INIT(sc) do { \294int error = pthread_mutex_init(&(sc)->mtx, NULL); \295if (error) \296BHND_NV_PANIC("pthread_mutex_init() failed: %d", \297error); \298} while(0)299300#define BHND_NVSTORE_LOCK(sc) pthread_mutex_lock(&(sc)->mtx)301#define BHND_NVSTORE_UNLOCK(sc) pthread_mutex_unlock(&(sc)->mtx)302#define BHND_NVSTORE_LOCK_DESTROY(sc) pthread_mutex_destroy(&(sc)->mtx)303#define BHND_NVSTORE_LOCK_ASSERT(sc, what)304305#endif /* _KERNEL */306307#endif /* _BHND_NVRAM_BHND_NVRAM_STOREVAR_H_ */308309310