xref: /freebsd/sys/dev/bhnd/nvram/bhnd_nvram_datavar.h (revision 7f9dff23d3092aa33ad45b2b63e52469b3c13a6e)
1 /*-
2  * Copyright (c) 2016 Landon Fuller <landonf@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
15  *
16  * NO WARRANTY
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.
28  *
29  * $FreeBSD$
30  */
31 
32 #ifndef _BHND_NVRAM_BHND_NVRAM_DATAVAR_H_
33 #define _BHND_NVRAM_BHND_NVRAM_DATAVAR_H_
34 
35 #include <sys/param.h>
36 #include <sys/linker_set.h>
37 #include <sys/refcount.h>
38 
39 #include "bhnd_nvram_io.h"
40 
41 #include "bhnd_nvram_data.h"
42 
43 /** Registered NVRAM parser class instances. */
44 SET_DECLARE(bhnd_nvram_data_class_set, bhnd_nvram_data_class_t);
45 
46 void	*bhnd_nvram_data_generic_find(struct bhnd_nvram_data *nv,
47 	     const char *name);
48 int	 bhnd_nvram_data_generic_rp_getvar(struct bhnd_nvram_data *nv,
49 	     void *cookiep, void *outp, size_t *olen, bhnd_nvram_type otype);
50 
51 /** @see bhnd_nvram_data_class_desc() */
52 typedef const char	*(bhnd_nvram_data_op_class_desc)(void);
53 
54 /** @see bhnd_nvram_data_probe() */
55 typedef int		 (bhnd_nvram_data_op_probe)(struct bhnd_nvram_io *io);
56 
57 /** @see bhnd_nvram_data_new() */
58 typedef int		 (bhnd_nvram_data_op_new)(struct bhnd_nvram_data *nv,
59 			     struct bhnd_nvram_io *io);
60 
61 /** Free all resources associated with @p nv. Called by
62  *  bhnd_nvram_data_release() when the reference count reaches zero. */
63 typedef void		 (bhnd_nvram_data_op_free)(struct bhnd_nvram_data *nv);
64 
65 /** @see bhnd_nvram_data_count() */
66 typedef size_t		 (bhnd_nvram_data_op_count)(struct bhnd_nvram_data *nv);
67 
68 /** @see bhnd_nvram_data_size() */
69 typedef int		 (bhnd_nvram_data_op_size)(struct bhnd_nvram_data *nv,
70 			     size_t *len);
71 
72 /** @see bhnd_nvram_data_serialize() */
73 typedef int		 (bhnd_nvram_data_op_serialize)(
74 			     struct bhnd_nvram_data *nv, void *buf,
75 			     size_t *len);
76 
77 /** @see bhnd_nvram_data_caps() */
78 typedef uint32_t	 (bhnd_nvram_data_op_caps)(struct bhnd_nvram_data *nv);
79 
80 /** @see bhnd_nvram_data_next() */
81 typedef const char	*(bhnd_nvram_data_op_next)(struct bhnd_nvram_data *nv,
82 			     void **cookiep);
83 
84 /** @see bhnd_nvram_data_find() */
85 typedef void		*(bhnd_nvram_data_op_find)(struct bhnd_nvram_data *nv,
86 			     const char *name);
87 
88 /** @see bhnd_nvram_data_getvar_name() */
89 typedef const char	*(bhnd_nvram_data_op_getvar_name)(
90 			     struct bhnd_nvram_data *nv, void *cookiep);
91 
92 /** @see bhnd_nvram_data_getvar() */
93 typedef int		 (bhnd_nvram_data_op_getvar)(struct bhnd_nvram_data *nv,
94 			     void *cookiep, void *buf, size_t *len,
95 			     bhnd_nvram_type type);
96 
97 /** @see bhnd_nvram_data_getvar_ptr() */
98 typedef const void	*(bhnd_nvram_data_op_getvar_ptr)(
99 			     struct bhnd_nvram_data *nv, void *cookiep,
100 			     size_t *len, bhnd_nvram_type *type);
101 
102 /**
103  * NVRAM data class.
104  */
105 struct bhnd_nvram_data_class {
106 	const char			*desc;		/**< description */
107 	size_t				 size;		/**< instance size */
108 	bhnd_nvram_data_op_probe	*op_probe;
109 	bhnd_nvram_data_op_new		*op_new;
110 	bhnd_nvram_data_op_free		*op_free;
111 	bhnd_nvram_data_op_count	*op_count;
112 	bhnd_nvram_data_op_size		*op_size;
113 	bhnd_nvram_data_op_serialize	*op_serialize;
114 	bhnd_nvram_data_op_caps		*op_caps;
115 	bhnd_nvram_data_op_next		*op_next;
116 	bhnd_nvram_data_op_find		*op_find;
117 	bhnd_nvram_data_op_getvar	*op_getvar;
118 	bhnd_nvram_data_op_getvar_ptr	*op_getvar_ptr;
119 	bhnd_nvram_data_op_getvar_name	*op_getvar_name;
120 };
121 
122 /**
123  * NVRAM data instance.
124  */
125 struct bhnd_nvram_data {
126 	struct bhnd_nvram_data_class	*cls;
127 	volatile u_int			 refs;
128 };
129 
130 /*
131  * Helper macro for BHND_NVRAM_DATA_CLASS_DEFN().
132  *
133  * Declares a bhnd_nvram_data_class method implementation with class name
134  * _cname and method name _mname
135  */
136 #define	BHND_NVRAM_DATA_CLASS_DECL_METHOD(_cname, _mname)		\
137 	static bhnd_nvram_data_op_ ## _mname				\
138 	    bhnd_nvram_ ## _cname ## _ ## _mname;			\
139 
140 /*
141  * Helper macro for BHND_NVRAM_DATA_CLASS_DEFN().
142  *
143  * Assign a bhnd_nvram_data_class method implementation with class name
144  * @p _cname and method name @p _mname
145  */
146 #define	BHND_NVRAM_DATA_CLASS_ASSIGN_METHOD(_cname, _mname)		\
147 	.op_ ## _mname = bhnd_nvram_ ## _cname ## _ ## _mname,
148 
149 /*
150  * Helper macro for BHND_NVRAM_DATA_CLASS_DEFN().
151  *
152  * Iterate over all bhnd_nvram_data_class method names, calling
153  * _macro with the class name _cname as the first argument, and
154  * a bhnd_nvram_data_class method name as the second.
155  */
156 #define	BHND_NVRAM_DATA_CLASS_ITER_METHODS(_cname, _macro)	\
157 	_macro(_cname, probe)					\
158 	_macro(_cname, new)					\
159 	_macro(_cname, free)					\
160 	_macro(_cname, count)					\
161 	_macro(_cname, size)					\
162 	_macro(_cname, serialize)				\
163 	_macro(_cname, caps)					\
164 	_macro(_cname, next)					\
165 	_macro(_cname, find)					\
166 	_macro(_cname, getvar)					\
167 	_macro(_cname, getvar_ptr)				\
168 	_macro(_cname, getvar_name)
169 
170 /**
171  * Define a bhnd_nvram_data_class with class name @p _n and description
172  * @p _desc, and register with bhnd_nvram_data_class_set.
173  */
174 #define	BHND_NVRAM_DATA_CLASS_DEFN(_cname, _desc, _size)		\
175 	BHND_NVRAM_DATA_CLASS_ITER_METHODS(_cname,			\
176 	    BHND_NVRAM_DATA_CLASS_DECL_METHOD)				\
177 									\
178 	struct bhnd_nvram_data_class bhnd_nvram_## _cname ## _class = {	\
179 		.desc		= (_desc),				\
180 		.size		= (_size),				\
181 		BHND_NVRAM_DATA_CLASS_ITER_METHODS(_cname,		\
182 		    BHND_NVRAM_DATA_CLASS_ASSIGN_METHOD)		\
183 	};								\
184 									\
185 	DATA_SET(bhnd_nvram_data_class_set,				\
186 	    bhnd_nvram_## _cname ## _class);
187 
188 #endif /* _BHND_NVRAM_BHND_NVRAM_DATAVAR_H_ */
189