2 * Copyright (c) 2015-2016 Landon Fuller <landonf@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
32 #ifndef _BHND_NVRAM_BHND_NVRAM_STOREVAR_H_
33 #define _BHND_NVRAM_BHND_NVRAM_STOREVAR_H_
35 #include <sys/types.h>
41 #include "bhnd_nvram_plist.h"
43 #include "bhnd_nvram_store.h"
45 /** Index is only generated if minimum variable count is met */
46 #define BHND_NV_IDX_VAR_THRESHOLD 15
48 #define BHND_NVSTORE_ROOT_PATH "/"
49 #define BHND_NVSTORE_ROOT_PATH_LEN sizeof(BHND_NVSTORE_ROOT_PATH)
51 #define BHND_NVSTORE_GET_FLAG(_value, _flag) \
52 (((_value) & BHND_NVSTORE_ ## _flag) != 0)
53 #define BHND_NVSTORE_GET_BITS(_value, _field) \
54 ((_value) & BHND_NVSTORE_ ## _field ## _MASK)
56 /* Forward declarations */
57 typedef struct bhnd_nvstore_name_info bhnd_nvstore_name_info;
58 typedef struct bhnd_nvstore_index bhnd_nvstore_index;
59 typedef struct bhnd_nvstore_path bhnd_nvstore_path;
61 typedef struct bhnd_nvstore_alias bhnd_nvstore_alias;
63 typedef struct bhnd_nvstore_alias_list bhnd_nvstore_alias_list;
64 typedef struct bhnd_nvstore_update_list bhnd_nvstore_update_list;
65 typedef struct bhnd_nvstore_path_list bhnd_nvstore_path_list;
67 LIST_HEAD(bhnd_nvstore_alias_list, bhnd_nvstore_alias);
68 LIST_HEAD(bhnd_nvstore_update_list, bhnd_nvstore_update);
69 LIST_HEAD(bhnd_nvstore_path_list, bhnd_nvstore_path);
72 * NVRAM store variable entry types.
75 BHND_NVSTORE_VAR = 0, /**< simple variable (var=...) */
76 BHND_NVSTORE_ALIAS_DECL = 1, /**< alias declaration ('devpath0=pci/1/1') */
77 } bhnd_nvstore_var_type;
80 * NVRAM path descriptor types.
83 BHND_NVSTORE_PATH_STRING = 0, /**< path is a string value */
84 BHND_NVSTORE_PATH_ALIAS = 1 /**< path is an alias reference */
85 } bhnd_nvstore_path_type;
88 * NVRAM variable namespaces.
91 BHND_NVSTORE_NAME_INTERNAL = 1, /**< internal namespace. permits
92 use of reserved devpath and
93 alias name prefixes. */
94 BHND_NVSTORE_NAME_EXTERNAL = 2, /**< external namespace. forbids
95 use of name prefixes used
96 for device path handling */
97 } bhnd_nvstore_name_type;
99 bhnd_nvstore_path *bhnd_nvstore_path_new(const char *path_str,
101 void bhnd_nvstore_path_free(struct bhnd_nvstore_path *path);
103 bhnd_nvstore_index *bhnd_nvstore_index_new(size_t capacity);
104 void bhnd_nvstore_index_free(bhnd_nvstore_index *index);
105 int bhnd_nvstore_index_append(struct bhnd_nvram_store *sc,
106 bhnd_nvstore_index *index,
108 int bhnd_nvstore_index_prepare(
109 struct bhnd_nvram_store *sc,
110 bhnd_nvstore_index *index);
111 void *bhnd_nvstore_index_lookup(struct bhnd_nvram_store *sc,
112 bhnd_nvstore_index *index, const char *name);
114 bhnd_nvstore_path *bhnd_nvstore_get_root_path(
115 struct bhnd_nvram_store *sc);
116 bool bhnd_nvstore_is_root_path(struct bhnd_nvram_store *sc,
117 bhnd_nvstore_path *path);
119 void *bhnd_nvstore_path_data_next(
120 struct bhnd_nvram_store *sc,
121 bhnd_nvstore_path *path, void **indexp);
122 void *bhnd_nvstore_path_data_lookup(
123 struct bhnd_nvram_store *sc,
124 bhnd_nvstore_path *path, const char *name);
125 bhnd_nvram_prop *bhnd_nvstore_path_get_update(
126 struct bhnd_nvram_store *sc,
127 bhnd_nvstore_path *path, const char *name);
128 int bhnd_nvstore_path_register_update(
129 struct bhnd_nvram_store *sc,
130 bhnd_nvstore_path *path, const char *name,
131 bhnd_nvram_val *value);
133 bhnd_nvstore_alias *bhnd_nvstore_find_alias(struct bhnd_nvram_store *sc,
135 bhnd_nvstore_alias *bhnd_nvstore_get_alias(struct bhnd_nvram_store *sc,
138 bhnd_nvstore_path *bhnd_nvstore_get_path(struct bhnd_nvram_store *sc,
139 const char *path, size_t path_len);
140 bhnd_nvstore_path *bhnd_nvstore_resolve_path_alias(
141 struct bhnd_nvram_store *sc, u_long aval);
143 bhnd_nvstore_path *bhnd_nvstore_var_get_path(struct bhnd_nvram_store *sc,
144 bhnd_nvstore_name_info *info);
145 int bhnd_nvstore_var_register_path(
146 struct bhnd_nvram_store *sc,
147 bhnd_nvstore_name_info *info, void *cookiep);
149 int bhnd_nvstore_register_path(struct bhnd_nvram_store *sc,
150 const char *path, size_t path_len);
151 int bhnd_nvstore_register_alias(
152 struct bhnd_nvram_store *sc,
153 const bhnd_nvstore_name_info *info, void *cookiep);
155 const char *bhnd_nvstore_parse_relpath(const char *parent,
157 int bhnd_nvstore_parse_name_info(const char *name,
158 bhnd_nvstore_name_type name_type,
159 uint32_t data_caps, bhnd_nvstore_name_info *info);
162 * NVRAM variable name descriptor.
164 * For NVRAM data instances supporting BHND_NVRAM_DATA_CAP_DEVPATHS, the
165 * NVRAM-vended variable name will be in one of four formats:
171 * - Device Alias Variable:
173 * - Device Path Alias Definition:
174 * 'devpath0=pci/1/1/variable'
178 * The device path format is device class-specific; the known supported device
180 * - sb: BCMA/SIBA SoC core device path.
181 * - pci: PCI device path (and PCIe on some earlier devices).
182 * - pcie: PCIe device path.
183 * - usb: USB device path.
185 * The device path format is loosely defined as '[class]/[domain]/[bus]/[slot]',
186 * with missing values either assumed to be zero, a value specific to the
187 * device class, or irrelevant to the device class in question.
190 * sb/1 BCMA/SIBA backplane 0, core 1.
191 * pc/1/1 PCMCIA bus 1, slot 1
192 * pci/1/1 PCI/PCIe domain 0, bus 1, device 1
193 * pcie/1/1 PCIe domain 0, bus 1, device 1
194 * usb/0xbd17 USB PID 0xbd17 (VID defaults to Broadcom 0x0a5c)
196 * Device Path Aliases:
198 * Device path aliases reduce duplication of device paths in the flash encoding
199 * of NVRAM data; a single devpath[alias]=[devpath] variable entry is defined,
200 * and then later variables may reference the device path via its alias:
201 * devpath1=usb/0xbd17
204 * Alias values are always positive, base 10 integers.
206 struct bhnd_nvstore_name_info {
207 const char *name; /**< variable name */
208 bhnd_nvstore_var_type type; /**< variable type */
209 bhnd_nvstore_path_type path_type; /**< path type */
211 /** Path information */
213 /* BHND_NVSTORE_PATH_STRING */
215 const char *value; /**< device path */
216 size_t value_len; /**< device path length */
219 /** BHND_NVSTORE_PATH_ALIAS */
221 u_long value; /**< device alias */
227 * NVRAM variable index.
229 * Provides effecient name-based lookup by maintaining an array of cached
230 * cookiep values, sorted lexicographically by relative variable name.
232 struct bhnd_nvstore_index {
233 size_t count; /**< entry count */
234 size_t capacity; /**< entry capacity */
235 void *cookiep[]; /**< cookiep values */
241 struct bhnd_nvstore_path {
242 char *path_str; /**< canonical path string */
243 size_t num_vars; /**< per-path count of committed
244 (non-pending) variables */
245 bhnd_nvstore_index *index; /**< per-path index, or NULL if
246 this is a root path for
247 which the data source
248 may be queried directly. */
249 bhnd_nvram_plist *pending; /**< pending changes */
251 LIST_ENTRY(bhnd_nvstore_path) np_link;
255 * NVRAM device path alias.
257 struct bhnd_nvstore_alias {
258 bhnd_nvstore_path *path; /**< borrowed path reference */
259 void *cookiep; /**< NVRAM variable's cookiep value */
260 u_long alias; /**< alias value */
262 LIST_ENTRY(bhnd_nvstore_alias) na_link;
265 /** bhnd nvram store instance state */
266 struct bhnd_nvram_store {
272 struct bhnd_nvram_data *data; /**< backing data */
273 uint32_t data_caps; /**< data capability flags */
274 bhnd_nvram_plist *data_opts; /**< data serialization options */
276 bhnd_nvstore_alias_list aliases[4]; /**< path alias hash table */
277 size_t num_aliases; /**< alias count */
279 bhnd_nvstore_path *root_path; /**< root path instance */
280 bhnd_nvstore_path_list paths[4]; /**< path hash table */
281 size_t num_paths; /**< path count */
286 #define BHND_NVSTORE_LOCK_INIT(sc) \
287 mtx_init(&(sc)->mtx, "BHND NVRAM store lock", NULL, MTX_DEF)
288 #define BHND_NVSTORE_LOCK(sc) mtx_lock(&(sc)->mtx)
289 #define BHND_NVSTORE_UNLOCK(sc) mtx_unlock(&(sc)->mtx)
290 #define BHND_NVSTORE_LOCK_ASSERT(sc, what) mtx_assert(&(sc)->mtx, what)
291 #define BHND_NVSTORE_LOCK_DESTROY(sc) mtx_destroy(&(sc)->mtx)
295 #define BHND_NVSTORE_LOCK_INIT(sc) do { \
296 int error = pthread_mutex_init(&(sc)->mtx, NULL); \
298 BHND_NV_PANIC("pthread_mutex_init() failed: %d", \
302 #define BHND_NVSTORE_LOCK(sc) pthread_mutex_lock(&(sc)->mtx)
303 #define BHND_NVSTORE_UNLOCK(sc) pthread_mutex_unlock(&(sc)->mtx)
304 #define BHND_NVSTORE_LOCK_DESTROY(sc) pthread_mutex_destroy(&(sc)->mtx)
305 #define BHND_NVSTORE_LOCK_ASSERT(sc, what)
309 #endif /* _BHND_NVRAM_BHND_NVRAM_STOREVAR_H_ */