xref: /illumos-gate/usr/src/uts/sun4v/sys/n2rng.h (revision 6ea3c060)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 #ifndef	_SYS_N2RNG_H
26 #define	_SYS_N2RNG_H
27 
28 /* skip following stuff when included in n2rng_hcall.s */
29 #ifndef _ASM
30 #include <sys/types.h>
31 #include <sys/mutex.h>
32 #include <sys/ksynch.h>
33 #include <sys/sunddi.h>
34 #include <sys/param.h>
35 #include <sys/crypto/common.h>
36 #include <sys/crypto/spi.h>
37 #include <sys/mdesc.h>
38 
39 #endif /* !_ASM */
40 
41 #ifdef	__cplusplus
42 extern "C" {
43 #endif
44 
45 #define	HV_RNG_GET_DIAG_CONTROL	0x130
46 #define	HV_RNG_CTL_READ		0x131
47 #define	HV_RNG_CTL_WRITE	0x132
48 #define	HV_RNG_DATA_READ_DIAG	0x133
49 #define	HV_RNG_DATA_READ	0x134
50 
51 #define	CTL_STATE_UNCONFIGURED	0
52 #define	CTL_STATE_CONFIGURED	1
53 #define	CTL_STATE_HEALTHCHECK	2
54 #define	CTL_STATE_ERROR		3
55 
56 #define	NRNGCTL			4
57 #define	N2RNG_MAX_READ		(128 * 1024)	/* 128K bytes */
58 
59 #define	DRIVER			"n2rng"
60 #define	N2RNG_MANUFACTURER_ID	"SUNWn2rng"
61 
62 #define	N2RNG_BINDNAME_N2	"SUNW,n2-rng"
63 #define	N2RNG_BINDNAME_VF	"SUNW,vf-rng"
64 #define	N2RNG_BINDNAME_KT	"SUNW,kt-rng"
65 
66 #define	N2RNG_MAX_RNGS		4
67 #define	N2RNG_INVALID_ID	(-1)
68 
69 #ifndef _ASM
70 
71 typedef enum {
72 	N2RNG_CPU_UNKNOWN,
73 	N2RNG_CPU_N2,
74 	N2RNG_CPU_VF,
75 	N2RNG_CPU_KT
76 } n2rng_binding_t;
77 
78 typedef union n2rngctl {
79 	uint64_t	word;
80 	struct {
81 		uint64_t rnc_res : 39;
82 		uint64_t rnc_cnt : 16;
83 		uint64_t rnc_bypass : 1;
84 		uint64_t rnc_vcoctl : 2;
85 		uint64_t rnc_anlg_sel : 2;
86 		uint64_t rnc_mode : 1;
87 		uint64_t rnc_selbits : 3;
88 	} fields;
89 } n2rng_ctl_t;
90 
91 typedef struct {
92 	n2rng_ctl_t ctlwds[NRNGCTL];
93 } n2rng_setup_t;
94 
95 #if defined(_KERNEL)
96 
97 /*
98  * Our contiguous memory alignment requirement is
99  * only for 8 bytes, however contig mem allocation
100  * routines requirement minimum of 64.
101  */
102 #define	CONTIG_ALIGNMENT	64
103 
104 /*
105  * Returns 1 only if the address range of a variable of type type at
106  * ptr falls entirely on one page.  Based on page size of 4K.  May
107  * give some false negatives on larger page sizes.
108  */
109 #define	CONTIGUOUS(ptr, type)	\
110 	(((((uint64_t)(ptr)) ^ ((uint64_t)(ptr) + sizeof (type) -1))	\
111 	& PAGEMASK) == 0)
112 
113 /*
114  * The RNG hardware can send certain internal analog signals to an
115  * external pin on the chip.  Setting the rnc_anlg_sel bit to
116  * N2RNG_NOANALOGOUT deselects all analog signals (perhaps selects
117  * ground).  Choosing any other value would aid an attacker with
118  * physical access to the chip.
119  */
120 #define	N2RNG_NOANALOGOUT	0x2
121 
122 /*
123  * n2rng: config variable in the n2rng.conf file
124  */
125 #define	N2RNG_FIPS_STRING	"n2rng-fips-140"
126 
127 /*
128  * There can only be N2_RNG_FIPS_INSTANCES concurrent RNG requsts from
129  * the framework.  Making this value large helps benchmarks.  It
130  * should probably come from a conf file, but for now it is hard
131  * coded.  The code computes i % N2RNG_FIPS_INSTANCES, which is more
132  * efficient when N2RNG_FIPS_INSTANCES is a power of 2.
133  */
134 #define	N2RNG_FIPS_INSTANCES 8
135 
136 typedef struct fipsrandomstruct fipsrandomstruct_t;
137 struct fipsrandomstruct {
138 	kmutex_t	mtx;
139 	uint64_t	entropyhunger;  /* RNGs generated with no entropy */
140 	uint32_t	XKEY[6]; /* one extra word for getentropy */
141 	uint32_t	x_jminus1[5];	/* store the last output */
142 };
143 
144 typedef struct {
145 	/*
146 	 * volatile, since it is not protected by a mutex.  (That is
147 	 * okay since it is operated on and accessed via atomic ops.)
148 	 */
149 	volatile unsigned int	fips_round_robin_j;
150 	fipsrandomstruct_t	fipsarray[N2RNG_FIPS_INSTANCES];
151 } fips_ensemble_t;
152 
153 /*
154  * Device flags (n2rng_t.n_flags)
155  */
156 #define	N2RNG_CONTROL		0x00000001
157 #define	N2RNG_FAILED		0x00000002
158 #define	N2RNG_CONFIGURED	0x00000004
159 #define	N2RNG_INITIALIZED	0x00000008
160 #define	N2RNG_REGISTERED	0x00000010
161 
162 #define	n2rng_setcontrol(n2rng)		((n2rng)->n_flags |= N2RNG_CONTROL)
163 #define	n2rng_clrcontrol(n2rng)		((n2rng)->n_flags &= ~N2RNG_CONTROL)
164 #define	n2rng_iscontrol(n2rng)		((n2rng)->n_flags & N2RNG_CONTROL)
165 
166 #define	n2rng_setfailed(n2rng)		((n2rng)->n_flags |= N2RNG_FAILED)
167 #define	n2rng_clrfailed(n2rng)		((n2rng)->n_flags &= ~N2RNG_FAILED)
168 #define	n2rng_isfailed(n2rng)		((n2rng)->n_flags & N2RNG_FAILED)
169 
170 #define	n2rng_setconfigured(n2rng)	((n2rng)->n_flags |= N2RNG_CONFIGURED)
171 #define	n2rng_clrconfigured(n2rng)	((n2rng)->n_flags &= ~N2RNG_CONFIGURED)
172 #define	n2rng_isconfigured(n2rng)	((n2rng)->n_flags & N2RNG_CONFIGURED)
173 
174 #define	n2rng_setinitialized(n2rng)	((n2rng)->n_flags |= N2RNG_INITIALIZED)
175 #define	n2rng_clrinitialized(n2rng)	((n2rng)->n_flags &= ~N2RNG_INITIALIZED)
176 #define	n2rng_isinitialized(n2rng)	((n2rng)->n_flags & N2RNG_INITIALIZED)
177 
178 #define	n2rng_setregistered(n2rng)	((n2rng)->n_flags |= N2RNG_REGISTERED)
179 #define	n2rng_clrregistered(n2rng)	((n2rng)->n_flags &= ~N2RNG_REGISTERED)
180 #define	n2rng_isregistered(n2rng)	((n2rng)->n_flags & N2RNG_REGISTERED)
181 
182 #define	DS_RNGBYTES		0
183 #define	DS_RNGJOBS		1
184 #define	DS_RNGHEALTHCHECKS	2
185 #define	DS_MAX			3
186 
187 #define	N2RNG_NOSC		3
188 #define	N2RNG_BIASBITS		2
189 #define	N2RNG_NBIASES		(1 << N2RNG_BIASBITS)
190 #define	N2RNG_CTLOPS		(N2RNG_OSC + 1)
191 
192 #define	N2RNG_PROP_NUM_UNITS	"rng-#units"
193 #define	SECOND			1000000		/* micro seconds */
194 
195 typedef struct {
196 	uint64_t	numvals;
197 	uint64_t	H1;	/* in bits per bit << LOG_VAL_SCALE */
198 	uint64_t	H2;
199 	uint64_t	Hinf;
200 } n2rng_osc_perf_t;
201 
202 typedef n2rng_osc_perf_t n2rng_osc_perf_table_t[N2RNG_NOSC][N2RNG_NBIASES];
203 
204 typedef struct {
205 	uint64_t	bias;
206 	uint64_t	entropy;
207 } n2rng_bias_info_t;
208 
209 typedef struct {
210 	n2rng_bias_info_t	n_bias_info[N2RNG_NOSC];
211 	n2rng_osc_perf_table_t	n_perftable;
212 	n2rng_setup_t		n_preferred_config;
213 	uint64_t		n_rng_state; /* as last known in this drvr. */
214 } rng_entry_t;
215 
216 typedef struct {
217 	int			n_num_rngs;
218 	int			n_num_rngs_online;
219 	rng_entry_t		*n_rngs;
220 	clock_t			n_hc_secs;
221 	uint64_t		n_watchdog_cycles;
222 	uint64_t		n_accumulate_cycles;
223 } rng_ctl_data_t;
224 
225 typedef struct n2rng {
226 	kmutex_t		n_lock;
227 	dev_info_t		*n_dip;
228 	unsigned		n_flags;	/* dev state flags */
229 	uint_t			n_hvapi_major_version;
230 	uint_t			n_hvapi_minor_version;
231 	n2rng_binding_t		n_binding;
232 	char			*n_binding_name;
233 	rng_ctl_data_t		*n_ctl_data;	/* Only valid in ctl domain */
234 	kstat_t			*n_ksp;
235 	uint64_t		n_stats[DS_MAX];
236 	crypto_kcf_provider_handle_t	n_prov;
237 	fips_ensemble_t		n_frs;
238 	timeout_id_t		n_timeout_id;
239 	md_t			*n_mdp;
240 	uint64_t		n_sticks_per_usec;
241 	ddi_taskq_t		*n_taskq;
242 	boolean_t		n_is_fips;
243 } n2rng_t;
244 
245 typedef kstat_named_t n2rng_kstat_bias_t[N2RNG_MAX_RNGS][N2RNG_NOSC];
246 
247 typedef struct n2rng_stat n2rng_stat_t;
248 struct n2rng_stat {
249 	kstat_named_t		ns_status;
250 	kstat_named_t		ns_algs[DS_MAX];
251 	kstat_named_t		ns_rngstate[N2RNG_MAX_RNGS];
252 	n2rng_kstat_bias_t	ns_rngbias;
253 	n2rng_kstat_bias_t	ns_rngentropy;
254 };
255 
256 #define	RNG_MODE_NORMAL			1
257 #define	RNG_MODE_DIAGNOSTIC		0
258 
259 #define	RNG_DIAG_CHUNK_SIZE		(N2RNG_MAX_READ / 8) /* as words */
260 #define	RNG_MAX_DATA_READ_ATTEMPTS	100
261 #define	RNG_RETRY_HLCHK_USECS		100000	/* retry every .1 seconds */
262 
263 #define	RNG_MAX_LOGIC_TEST_ATTEMPTS	3
264 #define	RNG_MAX_BUSY_ATTEMPTS		100
265 #define	RNG_MAX_BLOCK_ATTEMPTS		50000
266 #define	RNG_RETRY_BUSY_DELAY		1
267 
268 #define	RNG_DEFAULT_ACCUMULATE_CYCLES	2048
269 #define	RNG_CFG_RETRY_SECS		60 /* seconds between cfg retries */
270 
271 #define	RNG_DEFAULT_HC_SECS		0  /* seconds between health checks */
272 #define	RNG_EXTRA_WATCHDOG_SECS		60 /* added to hc time for watchdog */
273 
274 #define	LOG_ARG_SCALE			49
275 #define	LOG_VAL_SCALE			32
276 
277 void n2rng_sort(uint64_t *data, int log2_size);
278 int n2rng_noise_gen_preferred(n2rng_t *n2rng, int rngid);
279 int n2rng_config_test(n2rng_t *n2rng);
280 int n2rng_collect_diag_bits(n2rng_t *n2rng, int rngid,
281     n2rng_setup_t *collect_setupp, void *buffer, int numbytes,
282     n2rng_setup_t *exit_setupp, uint64_t exitstate);
283 int n2rng_getentropy(n2rng_t *n2rng, void *buffer, size_t size);
284 int n2rng_fips_random_init(n2rng_t *n2rng, fipsrandomstruct_t *frsp);
285 void n2rng_fips_random_fini(fipsrandomstruct_t *frsp);
286 int n2rng_do_health_check(n2rng_t *n2rng, int rngid);
287 void n2rng_renyi_entropy(uint64_t *buffer, int log2samples,
288     n2rng_osc_perf_t *metricp);
289 uint64_t n2rng_read_ctl(n2rng_t *n2rng, int rngid, uint64_t ctlregs_pa,
290     uint64_t *state, uint64_t *tdelta, uint64_t *wdelta);
291 uint64_t n2rng_ctl_wait(n2rng_t *n2rng, int rngid);
292 uint64_t n2rng_ctl_write(n2rng_t *n2rng, int rngid, uint64_t ctlregs_pa,
293     uint64_t newstate, uint64_t wtimeout, uint64_t *tdelta);
294 uint64_t n2rng_data_read_diag(n2rng_t *n2rng, int rngid, uint64_t data_pa,
295     size_t  datalen, uint64_t *tdelta);
296 uint64_t n2rng_check_ctl_access(n2rng_t *n2rng);
297 void n2rng_config_retry(n2rng_t *n2rng, clock_t seconds);
298 
299 #if defined(DEBUG)
300 
301 #define	DWARN		0x00000001
302 #define	DMA_ARGS	0x00000002
303 #define	DMA_LDST	0x00000004
304 #define	DNCS_QTAIL	0x00000008
305 #define	DATTACH		0x00000010
306 #define	DCFG		0x00000020
307 #define	DMOD		0x00000040  /* _init/_fini/_info/attach/detach */
308 #define	DENTRY		0x00000080  /* crypto routine entry/exit points */
309 #define	DHEALTH		0x00000100
310 #define	DCHATTY		0x00000200
311 #define	DKCF		0x00000400
312 #define	DALL		0xFFFFFFFF
313 
314 #define	DBG0	n2rng_dprintf
315 #define	DBG1	n2rng_dprintf
316 #define	DBG2	n2rng_dprintf
317 #define	DBG3	n2rng_dprintf
318 #define	DBG4	n2rng_dprintf
319 #define	DBG5	n2rng_dprintf
320 #define	DBG6	n2rng_dprintf
321 #define	DBGCALL(flag, func)	{ if (n2rng_dflagset(flag)) (void) func; }
322 
323 void	n2rng_dprintf(n2rng_t *, int, const char *, ...);
324 void	n2rng_dumphex(void *, int);
325 int	n2rng_dflagset(int);
326 
327 #else	/* !defined(DEBUG) */
328 
329 #define	DBG0(vca, lvl, fmt)
330 #define	DBG1(vca, lvl, fmt, arg1)
331 #define	DBG2(vca, lvl, fmt, arg1, arg2)
332 #define	DBG3(vca, lvl, fmt, arg1, arg2, arg3)
333 #define	DBG4(vca, lvl, fmt, arg1, arg2, arg3, arg4)
334 #define	DBG5(vca, lvl, fmt, arg1, arg2, arg3, arg4, arg5)
335 #define	DBG6(vca, lvl, fmt, arg1, arg2, arg3, arg4, arg5, arg6)
336 #define	DBGCALL(flag, func)
337 
338 #endif	/* !defined(DEBUG) */
339 
340 /*
341  * n2rng_kcf.c
342  */
343 int n2rng_herr2kerr(uint64_t);
344 int n2rng_logic_test(n2rng_t *, int);
345 int n2rng_noise_gen_test_set(void);
346 int n2rng_init(n2rng_t *n2rng);
347 int n2rng_uninit(n2rng_t *n2rng);
348 int n2rng_register_provider(n2rng_t *n2rng);
349 int n2rng_unregister_provider(n2rng_t *n2rng);
350 void n2rng_failure(n2rng_t *n2rng);
351 void n2rng_unconfigured(n2rng_t *n2rng);
352 
353 /*
354  * n2rng_debug.c
355  */
356 void n2rng_error(n2rng_t *, const char *, ...);
357 void n2rng_diperror(dev_info_t *, const char *, ...);
358 void n2rng_dipverror(dev_info_t *, const char *, va_list);
359 
360 uint64_t hv_rng_get_diag_control(void);
361 uint64_t hv_rng_ctl_read(uint64_t ctlregs_pa, uint64_t *state,
362     uint64_t *tdelta);
363 uint64_t hv_rng_ctl_read_v2(uint64_t ctlregs_pa, uint64_t rngid,
364     uint64_t *state, uint64_t *tdelta, uint64_t *wdelta, uint64_t *wstate);
365 uint64_t hv_rng_ctl_write(uint64_t ctlregs_pa,
366     uint64_t newstate, uint64_t wtimeout, uint64_t *tdelta);
367 uint64_t hv_rng_ctl_write_v2(uint64_t ctlregs_pa,
368     uint64_t newstate, uint64_t wtimeout, uint64_t rngid);
369 uint64_t hv_rng_data_read_diag(uint64_t data_pa,
370     size_t  datalen, uint64_t *tdelta);
371 uint64_t hv_rng_data_read_diag_v2(uint64_t data_pa,
372     size_t  datalen, uint64_t rngid, uint64_t *tdelta);
373 uint64_t hv_rng_data_read(uint64_t data_pa, uint64_t *tdelta);
374 
375 /*
376  * n2rng_post.c
377  */
378 int n2rng_fips_rng_post(void);
379 
380 #endif /* _KERNEL */
381 #endif /* !_ASM */
382 
383 #ifdef	__cplusplus
384 }
385 #endif
386 
387 #endif	/* _SYS_N2RNG_H */
388