xref: /freebsd/sys/dev/bhnd/nvram/bhnd_nvram_datavar.h (revision 2ff63af9b88c7413b7d71715b5532625752a248e)
177cb4d3eSLandon J. Fuller /*-
277cb4d3eSLandon J. Fuller  * Copyright (c) 2016 Landon Fuller <landonf@FreeBSD.org>
377cb4d3eSLandon J. Fuller  * All rights reserved.
477cb4d3eSLandon J. Fuller  *
577cb4d3eSLandon J. Fuller  * Redistribution and use in source and binary forms, with or without
677cb4d3eSLandon J. Fuller  * modification, are permitted provided that the following conditions
777cb4d3eSLandon J. Fuller  * are met:
877cb4d3eSLandon J. Fuller  * 1. Redistributions of source code must retain the above copyright
977cb4d3eSLandon J. Fuller  *    notice, this list of conditions and the following disclaimer,
1077cb4d3eSLandon J. Fuller  *    without modification.
1177cb4d3eSLandon J. Fuller  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1277cb4d3eSLandon J. Fuller  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
1377cb4d3eSLandon J. Fuller  *    redistribution must be conditioned upon including a substantially
1477cb4d3eSLandon J. Fuller  *    similar Disclaimer requirement for further binary redistribution.
1577cb4d3eSLandon J. Fuller  *
1677cb4d3eSLandon J. Fuller  * NO WARRANTY
1777cb4d3eSLandon J. Fuller  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1877cb4d3eSLandon J. Fuller  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1977cb4d3eSLandon J. Fuller  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
2077cb4d3eSLandon J. Fuller  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
2177cb4d3eSLandon J. Fuller  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
2277cb4d3eSLandon J. Fuller  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2377cb4d3eSLandon J. Fuller  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2477cb4d3eSLandon J. Fuller  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2577cb4d3eSLandon J. Fuller  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2677cb4d3eSLandon J. Fuller  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
2777cb4d3eSLandon J. Fuller  * THE POSSIBILITY OF SUCH DAMAGES.
2877cb4d3eSLandon J. Fuller  *
2977cb4d3eSLandon J. Fuller  */
3077cb4d3eSLandon J. Fuller 
3177cb4d3eSLandon J. Fuller #ifndef _BHND_NVRAM_BHND_NVRAM_DATAVAR_H_
3277cb4d3eSLandon J. Fuller #define _BHND_NVRAM_BHND_NVRAM_DATAVAR_H_
3377cb4d3eSLandon J. Fuller 
3477cb4d3eSLandon J. Fuller #include <sys/param.h>
3577cb4d3eSLandon J. Fuller #include <sys/linker_set.h>
3677cb4d3eSLandon J. Fuller #include <sys/refcount.h>
3777cb4d3eSLandon J. Fuller 
3877cb4d3eSLandon J. Fuller #include "bhnd_nvram_io.h"
3977cb4d3eSLandon J. Fuller 
4077cb4d3eSLandon J. Fuller #include "bhnd_nvram_data.h"
4177cb4d3eSLandon J. Fuller 
4277cb4d3eSLandon J. Fuller /** Registered NVRAM parser class instances. */
4358efe686SLandon J. Fuller SET_DECLARE(bhnd_nvram_data_class_set, bhnd_nvram_data_class);
4477cb4d3eSLandon J. Fuller 
4519be09f3SLandon J. Fuller void			*bhnd_nvram_data_generic_find(
4619be09f3SLandon J. Fuller 			     struct bhnd_nvram_data *nv, const char *name);
4719be09f3SLandon J. Fuller int			 bhnd_nvram_data_generic_rp_getvar(
4819be09f3SLandon J. Fuller 			     struct bhnd_nvram_data *nv, void *cookiep,
4919be09f3SLandon J. Fuller 			     void *outp, size_t *olen, bhnd_nvram_type otype);
5019be09f3SLandon J. Fuller int			 bhnd_nvram_data_generic_rp_copy_val(
5119be09f3SLandon J. Fuller 			     struct bhnd_nvram_data *nv, void *cookiep,
5219be09f3SLandon J. Fuller 			     bhnd_nvram_val **val);
5377cb4d3eSLandon J. Fuller 
5477cb4d3eSLandon J. Fuller /** @see bhnd_nvram_data_probe() */
5577cb4d3eSLandon J. Fuller typedef int		 (bhnd_nvram_data_op_probe)(struct bhnd_nvram_io *io);
5677cb4d3eSLandon J. Fuller 
57*591e79bcSLandon J. Fuller /** @see bhnd_nvram_data_probe() */
58*591e79bcSLandon J. Fuller typedef int		 (bhnd_nvram_data_op_getvar_direct)(
59*591e79bcSLandon J. Fuller 			      struct bhnd_nvram_io *io, const char *name,
60*591e79bcSLandon J. Fuller 			      void *outp, size_t *olen, bhnd_nvram_type otype);
61*591e79bcSLandon J. Fuller 
62c283839dSLandon J. Fuller /** @see bhnd_nvram_data_serialize() */
63c283839dSLandon J. Fuller typedef int		 (bhnd_nvram_data_op_serialize)(
64c283839dSLandon J. Fuller 			      bhnd_nvram_data_class *cls,
65c283839dSLandon J. Fuller 			      bhnd_nvram_plist *props,
66c283839dSLandon J. Fuller 			      bhnd_nvram_plist *options, void *outp,
67c283839dSLandon J. Fuller 			      size_t *olen);
68c283839dSLandon J. Fuller 
6977cb4d3eSLandon J. Fuller /** @see bhnd_nvram_data_new() */
7077cb4d3eSLandon J. Fuller typedef int		 (bhnd_nvram_data_op_new)(struct bhnd_nvram_data *nv,
7177cb4d3eSLandon J. Fuller 			      struct bhnd_nvram_io *io);
7277cb4d3eSLandon J. Fuller 
7377cb4d3eSLandon J. Fuller /** Free all resources associated with @p nv. Called by
7477cb4d3eSLandon J. Fuller  *  bhnd_nvram_data_release() when the reference count reaches zero. */
7577cb4d3eSLandon J. Fuller typedef void		 (bhnd_nvram_data_op_free)(struct bhnd_nvram_data *nv);
7677cb4d3eSLandon J. Fuller 
7777cb4d3eSLandon J. Fuller /** @see bhnd_nvram_data_count() */
7877cb4d3eSLandon J. Fuller typedef size_t		 (bhnd_nvram_data_op_count)(struct bhnd_nvram_data *nv);
7977cb4d3eSLandon J. Fuller 
80a7c43ebdSLandon J. Fuller /** @see bhnd_nvram_data_options() */
81a7c43ebdSLandon J. Fuller typedef bhnd_nvram_plist*(bhnd_nvram_data_op_options)(
82a7c43ebdSLandon J. Fuller 			      struct bhnd_nvram_data *nv);
83a7c43ebdSLandon J. Fuller 
8477cb4d3eSLandon J. Fuller /** @see bhnd_nvram_data_caps() */
8577cb4d3eSLandon J. Fuller typedef uint32_t	 (bhnd_nvram_data_op_caps)(struct bhnd_nvram_data *nv);
8677cb4d3eSLandon J. Fuller 
8777cb4d3eSLandon J. Fuller /** @see bhnd_nvram_data_next() */
8877cb4d3eSLandon J. Fuller typedef const char	*(bhnd_nvram_data_op_next)(struct bhnd_nvram_data *nv,
8977cb4d3eSLandon J. Fuller 			      void **cookiep);
9077cb4d3eSLandon J. Fuller 
9177cb4d3eSLandon J. Fuller /** @see bhnd_nvram_data_find() */
9277cb4d3eSLandon J. Fuller typedef void		*(bhnd_nvram_data_op_find)(struct bhnd_nvram_data *nv,
9377cb4d3eSLandon J. Fuller 			      const char *name);
9477cb4d3eSLandon J. Fuller 
9519be09f3SLandon J. Fuller /** @see bhnd_nvram_data_copy_val() */
9619be09f3SLandon J. Fuller typedef int		 (bhnd_nvram_data_op_copy_val)(
9719be09f3SLandon J. Fuller 			      struct bhnd_nvram_data *nv, void *cookiep,
9819be09f3SLandon J. Fuller 			      bhnd_nvram_val **value);
9919be09f3SLandon J. Fuller 
10019be09f3SLandon J. Fuller /** @see bhnd_nvram_data_getvar_order() */
10119be09f3SLandon J. Fuller typedef int		 (bhnd_nvram_data_op_getvar_order)(
10219be09f3SLandon J. Fuller 			      struct bhnd_nvram_data *nv, void *cookiep1,
10319be09f3SLandon J. Fuller 			      void *cookiep2);
10419be09f3SLandon J. Fuller 
10577cb4d3eSLandon J. Fuller /** @see bhnd_nvram_data_getvar_name() */
10677cb4d3eSLandon J. Fuller typedef const char	*(bhnd_nvram_data_op_getvar_name)(
10719be09f3SLandon J. Fuller 			      struct bhnd_nvram_data *nv,
10819be09f3SLandon J. Fuller 			      void *cookiep);
10977cb4d3eSLandon J. Fuller 
11077cb4d3eSLandon J. Fuller /** @see bhnd_nvram_data_getvar() */
11177cb4d3eSLandon J. Fuller typedef int		 (bhnd_nvram_data_op_getvar)(struct bhnd_nvram_data *nv,
11277cb4d3eSLandon J. Fuller 			      void *cookiep, void *buf, size_t *len,
11377cb4d3eSLandon J. Fuller 			      bhnd_nvram_type type);
11477cb4d3eSLandon J. Fuller 
11577cb4d3eSLandon J. Fuller /** @see bhnd_nvram_data_getvar_ptr() */
11677cb4d3eSLandon J. Fuller typedef const void	*(bhnd_nvram_data_op_getvar_ptr)(
11777cb4d3eSLandon J. Fuller 			      struct bhnd_nvram_data *nv, void *cookiep,
11877cb4d3eSLandon J. Fuller 			      size_t *len, bhnd_nvram_type *type);
11977cb4d3eSLandon J. Fuller 
12019be09f3SLandon J. Fuller /** @see bhnd_nvram_data_filter_setvar() */
12119be09f3SLandon J. Fuller typedef int		 (bhnd_nvram_data_op_filter_setvar)(
12219be09f3SLandon J. Fuller 			      struct bhnd_nvram_data *nv, const char *name,
12319be09f3SLandon J. Fuller 			      bhnd_nvram_val *value, bhnd_nvram_val **result);
12419be09f3SLandon J. Fuller 
12519be09f3SLandon J. Fuller /** @see bhnd_nvram_data_filter_unsetvar() */
12619be09f3SLandon J. Fuller typedef int		 (bhnd_nvram_data_op_filter_unsetvar)(
12719be09f3SLandon J. Fuller 			      struct bhnd_nvram_data *nv, const char *name);
12819be09f3SLandon J. Fuller 
12977cb4d3eSLandon J. Fuller /**
13077cb4d3eSLandon J. Fuller  * NVRAM data class.
13177cb4d3eSLandon J. Fuller  */
13277cb4d3eSLandon J. Fuller struct bhnd_nvram_data_class {
13377cb4d3eSLandon J. Fuller 	const char			*desc;		/**< description */
134c283839dSLandon J. Fuller 	uint32_t			 caps;		/**< capabilities (BHND_NVRAM_DATA_CAP_*) */
13577cb4d3eSLandon J. Fuller 	size_t				 size;		/**< instance size */
13619be09f3SLandon J. Fuller 
13777cb4d3eSLandon J. Fuller 	bhnd_nvram_data_op_probe		*op_probe;
138*591e79bcSLandon J. Fuller 	bhnd_nvram_data_op_getvar_direct	*op_getvar_direct;
139c283839dSLandon J. Fuller 	bhnd_nvram_data_op_serialize		*op_serialize;
14077cb4d3eSLandon J. Fuller 	bhnd_nvram_data_op_new			*op_new;
14177cb4d3eSLandon J. Fuller 	bhnd_nvram_data_op_free			*op_free;
14277cb4d3eSLandon J. Fuller 	bhnd_nvram_data_op_count		*op_count;
143a7c43ebdSLandon J. Fuller 	bhnd_nvram_data_op_options		*op_options;
14477cb4d3eSLandon J. Fuller 	bhnd_nvram_data_op_caps			*op_caps;
14577cb4d3eSLandon J. Fuller 	bhnd_nvram_data_op_next			*op_next;
14677cb4d3eSLandon J. Fuller 	bhnd_nvram_data_op_find			*op_find;
14719be09f3SLandon J. Fuller 	bhnd_nvram_data_op_copy_val		*op_copy_val;
14819be09f3SLandon J. Fuller 	bhnd_nvram_data_op_getvar_order		*op_getvar_order;
14977cb4d3eSLandon J. Fuller 	bhnd_nvram_data_op_getvar		*op_getvar;
15077cb4d3eSLandon J. Fuller 	bhnd_nvram_data_op_getvar_ptr		*op_getvar_ptr;
15177cb4d3eSLandon J. Fuller 	bhnd_nvram_data_op_getvar_name		*op_getvar_name;
15219be09f3SLandon J. Fuller 	bhnd_nvram_data_op_filter_setvar	*op_filter_setvar;
15319be09f3SLandon J. Fuller 	bhnd_nvram_data_op_filter_unsetvar	*op_filter_unsetvar;
15477cb4d3eSLandon J. Fuller };
15577cb4d3eSLandon J. Fuller 
15677cb4d3eSLandon J. Fuller /**
15777cb4d3eSLandon J. Fuller  * NVRAM data instance.
15877cb4d3eSLandon J. Fuller  */
15977cb4d3eSLandon J. Fuller struct bhnd_nvram_data {
16077cb4d3eSLandon J. Fuller 	struct bhnd_nvram_data_class	*cls;
16177cb4d3eSLandon J. Fuller 	volatile u_int			 refs;
16277cb4d3eSLandon J. Fuller };
16377cb4d3eSLandon J. Fuller 
16477cb4d3eSLandon J. Fuller /*
16577cb4d3eSLandon J. Fuller  * Helper macro for BHND_NVRAM_DATA_CLASS_DEFN().
16677cb4d3eSLandon J. Fuller  *
16777cb4d3eSLandon J. Fuller  * Declares a bhnd_nvram_data_class method implementation with class name
16877cb4d3eSLandon J. Fuller  * _cname and method name _mname
16977cb4d3eSLandon J. Fuller  */
17077cb4d3eSLandon J. Fuller #define	BHND_NVRAM_DATA_CLASS_DECL_METHOD(_cname, _mname)		\
17177cb4d3eSLandon J. Fuller 	static bhnd_nvram_data_op_ ## _mname				\
17277cb4d3eSLandon J. Fuller 	    bhnd_nvram_ ## _cname ## _ ## _mname;			\
17377cb4d3eSLandon J. Fuller 
17477cb4d3eSLandon J. Fuller /*
17577cb4d3eSLandon J. Fuller  * Helper macro for BHND_NVRAM_DATA_CLASS_DEFN().
17677cb4d3eSLandon J. Fuller  *
17777cb4d3eSLandon J. Fuller  * Assign a bhnd_nvram_data_class method implementation with class name
17877cb4d3eSLandon J. Fuller  * @p _cname and method name @p _mname
17977cb4d3eSLandon J. Fuller  */
18077cb4d3eSLandon J. Fuller #define	BHND_NVRAM_DATA_CLASS_ASSIGN_METHOD(_cname, _mname)		\
18177cb4d3eSLandon J. Fuller 	.op_ ## _mname = bhnd_nvram_ ## _cname ## _ ## _mname,
18277cb4d3eSLandon J. Fuller 
18377cb4d3eSLandon J. Fuller /*
18477cb4d3eSLandon J. Fuller  * Helper macro for BHND_NVRAM_DATA_CLASS_DEFN().
18577cb4d3eSLandon J. Fuller  *
18677cb4d3eSLandon J. Fuller  * Iterate over all bhnd_nvram_data_class method names, calling
18777cb4d3eSLandon J. Fuller  * _macro with the class name _cname as the first argument, and
18877cb4d3eSLandon J. Fuller  * a bhnd_nvram_data_class method name as the second.
18977cb4d3eSLandon J. Fuller  */
19077cb4d3eSLandon J. Fuller #define	BHND_NVRAM_DATA_CLASS_ITER_METHODS(_cname, _macro)	\
19177cb4d3eSLandon J. Fuller 	_macro(_cname, probe)					\
192*591e79bcSLandon J. Fuller 	_macro(_cname, getvar_direct)				\
193c283839dSLandon J. Fuller 	_macro(_cname, serialize)				\
19477cb4d3eSLandon J. Fuller 	_macro(_cname, new)					\
19577cb4d3eSLandon J. Fuller 	_macro(_cname, free)					\
19677cb4d3eSLandon J. Fuller 	_macro(_cname, count)					\
197a7c43ebdSLandon J. Fuller 	_macro(_cname, options)					\
19877cb4d3eSLandon J. Fuller 	_macro(_cname, caps)					\
19977cb4d3eSLandon J. Fuller 	_macro(_cname, next)					\
20077cb4d3eSLandon J. Fuller 	_macro(_cname, find)					\
20119be09f3SLandon J. Fuller 	_macro(_cname, copy_val)				\
20219be09f3SLandon J. Fuller 	_macro(_cname, getvar_order)				\
20377cb4d3eSLandon J. Fuller 	_macro(_cname, getvar)					\
20477cb4d3eSLandon J. Fuller 	_macro(_cname, getvar_ptr)				\
20519be09f3SLandon J. Fuller 	_macro(_cname, getvar_name)				\
20619be09f3SLandon J. Fuller 	_macro(_cname, filter_setvar)				\
20719be09f3SLandon J. Fuller 	_macro(_cname, filter_unsetvar)
20877cb4d3eSLandon J. Fuller 
20977cb4d3eSLandon J. Fuller /**
21077cb4d3eSLandon J. Fuller  * Define a bhnd_nvram_data_class with class name @p _n and description
21177cb4d3eSLandon J. Fuller  * @p _desc, and register with bhnd_nvram_data_class_set.
21277cb4d3eSLandon J. Fuller  */
213c283839dSLandon J. Fuller #define	BHND_NVRAM_DATA_CLASS_DEFN(_cname, _desc, _caps, _size)		\
21477cb4d3eSLandon J. Fuller 	BHND_NVRAM_DATA_CLASS_ITER_METHODS(_cname,			\
21577cb4d3eSLandon J. Fuller 	    BHND_NVRAM_DATA_CLASS_DECL_METHOD)				\
21677cb4d3eSLandon J. Fuller 									\
21777cb4d3eSLandon J. Fuller 	struct bhnd_nvram_data_class bhnd_nvram_## _cname ## _class = {	\
21877cb4d3eSLandon J. Fuller 		.desc		= (_desc),				\
219c283839dSLandon J. Fuller 		.caps		= (_caps),				\
22077cb4d3eSLandon J. Fuller 		.size		= (_size),				\
22177cb4d3eSLandon J. Fuller 		BHND_NVRAM_DATA_CLASS_ITER_METHODS(_cname,		\
22277cb4d3eSLandon J. Fuller 		    BHND_NVRAM_DATA_CLASS_ASSIGN_METHOD)		\
22377cb4d3eSLandon J. Fuller 	};								\
22477cb4d3eSLandon J. Fuller 									\
22577cb4d3eSLandon J. Fuller 	DATA_SET(bhnd_nvram_data_class_set,				\
22677cb4d3eSLandon J. Fuller 	    bhnd_nvram_## _cname ## _class);
22777cb4d3eSLandon J. Fuller 
22877cb4d3eSLandon J. Fuller #endif /* _BHND_NVRAM_BHND_NVRAM_DATAVAR_H_ */
229