xref: /titanic_44/usr/src/uts/sun4v/sys/n2rng.h (revision 7014882c6a3672fd0e5d60200af8643ae53c5928)
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  * There can only be N2_RNG_FIPS_INSTANCES concurrent RNG requsts from
124  * the framework.  Making this value large helps benchmarks.  It
125  * should probably come from a conf file, but for now it is hard
126  * coded.  The code computes i % N2RNG_FIPS_INSTANCES, which is more
127  * efficient when N2RNG_FIPS_INSTANCES is a power of 2.
128  */
129 #define	N2RNG_FIPS_INSTANCES 8
130 
131 typedef struct fipsrandomstruct fipsrandomstruct_t;
132 struct fipsrandomstruct {
133 	kmutex_t	mtx;
134 	uint64_t	entropyhunger;  /* RNGs generated with no entropy */
135 	uint32_t	XKEY[6]; /* one extra word for getentropy */
136 	uint32_t	x_jminus1[5];	/* store the last output */
137 };
138 
139 typedef struct {
140 	/*
141 	 * volatile, since it is not protected by a mutex.  (That is
142 	 * okay since it is operated on and accessed via atomic ops.)
143 	 */
144 	volatile unsigned int	fips_round_robin_j;
145 	fipsrandomstruct_t	fipsarray[N2RNG_FIPS_INSTANCES];
146 } fips_ensemble_t;
147 
148 /*
149  * Device flags (n2rng_t.n_flags)
150  */
151 #define	N2RNG_CONTROL		0x00000001
152 #define	N2RNG_FAILED		0x00000002
153 #define	N2RNG_CONFIGURED	0x00000004
154 #define	N2RNG_INITIALIZED	0x00000008
155 #define	N2RNG_REGISTERED	0x00000010
156 
157 #define	n2rng_setcontrol(n2rng)		((n2rng)->n_flags |= N2RNG_CONTROL)
158 #define	n2rng_clrcontrol(n2rng)		((n2rng)->n_flags &= ~N2RNG_CONTROL)
159 #define	n2rng_iscontrol(n2rng)		((n2rng)->n_flags & N2RNG_CONTROL)
160 
161 #define	n2rng_setfailed(n2rng)		((n2rng)->n_flags |= N2RNG_FAILED)
162 #define	n2rng_clrfailed(n2rng)		((n2rng)->n_flags &= ~N2RNG_FAILED)
163 #define	n2rng_isfailed(n2rng)		((n2rng)->n_flags & N2RNG_FAILED)
164 
165 #define	n2rng_setconfigured(n2rng)	((n2rng)->n_flags |= N2RNG_CONFIGURED)
166 #define	n2rng_clrconfigured(n2rng)	((n2rng)->n_flags &= ~N2RNG_CONFIGURED)
167 #define	n2rng_isconfigured(n2rng)	((n2rng)->n_flags & N2RNG_CONFIGURED)
168 
169 #define	n2rng_setinitialized(n2rng)	((n2rng)->n_flags |= N2RNG_INITIALIZED)
170 #define	n2rng_clrinitialized(n2rng)	((n2rng)->n_flags &= ~N2RNG_INITIALIZED)
171 #define	n2rng_isinitialized(n2rng)	((n2rng)->n_flags & N2RNG_INITIALIZED)
172 
173 #define	n2rng_setregistered(n2rng)	((n2rng)->n_flags |= N2RNG_REGISTERED)
174 #define	n2rng_clrregistered(n2rng)	((n2rng)->n_flags &= ~N2RNG_REGISTERED)
175 #define	n2rng_isregistered(n2rng)	((n2rng)->n_flags & N2RNG_REGISTERED)
176 
177 #define	DS_RNGBYTES		0
178 #define	DS_RNGJOBS		1
179 #define	DS_RNGHEALTHCHECKS	2
180 #define	DS_MAX			3
181 
182 #define	N2RNG_NOSC		3
183 #define	N2RNG_BIASBITS		2
184 #define	N2RNG_NBIASES		(1 << N2RNG_BIASBITS)
185 #define	N2RNG_CTLOPS		(N2RNG_OSC + 1)
186 
187 #define	N2RNG_PROP_NUM_UNITS	"rng-#units"
188 #define	SECOND			1000000		/* micro seconds */
189 
190 typedef struct {
191 	uint64_t	numvals;
192 	uint64_t	H1;	/* in bits per bit << LOG_VAL_SCALE */
193 	uint64_t	H2;
194 	uint64_t	Hinf;
195 } n2rng_osc_perf_t;
196 
197 typedef n2rng_osc_perf_t n2rng_osc_perf_table_t[N2RNG_NOSC][N2RNG_NBIASES];
198 
199 typedef struct {
200 	uint64_t	bias;
201 	uint64_t	entropy;
202 } n2rng_bias_info_t;
203 
204 typedef struct {
205 	n2rng_bias_info_t	n_bias_info[N2RNG_NOSC];
206 	n2rng_osc_perf_table_t	n_perftable;
207 	n2rng_setup_t		n_preferred_config;
208 	uint64_t		n_rng_state; /* as last known in this drvr. */
209 } rng_entry_t;
210 
211 typedef struct {
212 	int			n_num_rngs;
213 	int			n_num_rngs_online;
214 	rng_entry_t		*n_rngs;
215 	clock_t			n_hc_secs;
216 	uint64_t		n_watchdog_cycles;
217 	uint64_t		n_accumulate_cycles;
218 } rng_ctl_data_t;
219 
220 typedef struct n2rng {
221 	kmutex_t		n_lock;
222 	dev_info_t		*n_dip;
223 	unsigned		n_flags;	/* dev state flags */
224 	uint_t			n_hvapi_major_version;
225 	uint_t			n_hvapi_minor_version;
226 	n2rng_binding_t		n_binding;
227 	char			*n_binding_name;
228 	rng_ctl_data_t		*n_ctl_data;	/* Only valid in ctl domain */
229 	kstat_t			*n_ksp;
230 	uint64_t		n_stats[DS_MAX];
231 	crypto_kcf_provider_handle_t	n_prov;
232 	fips_ensemble_t		n_frs;
233 	timeout_id_t		n_timeout_id;
234 	md_t			*n_mdp;
235 	uint64_t		n_sticks_per_usec;
236 	ddi_taskq_t		*n_taskq;
237 } n2rng_t;
238 
239 typedef kstat_named_t n2rng_kstat_bias_t[N2RNG_MAX_RNGS][N2RNG_NOSC];
240 
241 typedef struct n2rng_stat n2rng_stat_t;
242 struct n2rng_stat {
243 	kstat_named_t		ns_status;
244 	kstat_named_t		ns_algs[DS_MAX];
245 	kstat_named_t		ns_rngstate[N2RNG_MAX_RNGS];
246 	n2rng_kstat_bias_t	ns_rngbias;
247 	n2rng_kstat_bias_t	ns_rngentropy;
248 };
249 
250 #define	RNG_MODE_NORMAL			1
251 #define	RNG_MODE_DIAGNOSTIC		0
252 
253 #define	RNG_DIAG_CHUNK_SIZE		(N2RNG_MAX_READ / 8) /* as words */
254 #define	RNG_MAX_DATA_READ_ATTEMPTS	100
255 #define	RNG_RETRY_HLCHK_USECS		100000	/* retry every .1 seconds */
256 
257 #define	RNG_MAX_LOGIC_TEST_ATTEMPTS	3
258 #define	RNG_MAX_BUSY_ATTEMPTS		100
259 #define	RNG_MAX_BLOCK_ATTEMPTS		50000
260 #define	RNG_RETRY_BUSY_DELAY		1
261 
262 #define	RNG_DEFAULT_ACCUMULATE_CYCLES	2048
263 #define	RNG_CFG_RETRY_SECS		60 /* seconds between cfg retries */
264 
265 #define	RNG_DEFAULT_HC_SECS		0  /* seconds between health checks */
266 #define	RNG_EXTRA_WATCHDOG_SECS		60 /* added to hc time for watchdog */
267 
268 #define	LOG_ARG_SCALE			49
269 #define	LOG_VAL_SCALE			32
270 
271 void n2rng_sort(uint64_t *data, int log2_size);
272 int n2rng_noise_gen_preferred(n2rng_t *n2rng, int rngid);
273 int n2rng_config_test(n2rng_t *n2rng);
274 int n2rng_collect_diag_bits(n2rng_t *n2rng, int rngid,
275     n2rng_setup_t *collect_setupp, void *buffer, int numbytes,
276     n2rng_setup_t *exit_setupp, uint64_t exitstate);
277 int n2rng_getentropy(n2rng_t *n2rng, void *buffer, size_t size);
278 int n2rng_fips_random_init(n2rng_t *n2rng, fipsrandomstruct_t *frsp);
279 void n2rng_fips_random_fini(fipsrandomstruct_t *frsp);
280 int n2rng_do_health_check(n2rng_t *n2rng, int rngid);
281 void n2rng_renyi_entropy(uint64_t *buffer, int log2samples,
282     n2rng_osc_perf_t *metricp);
283 uint64_t n2rng_read_ctl(n2rng_t *n2rng, int rngid, uint64_t ctlregs_pa,
284     uint64_t *state, uint64_t *tdelta, uint64_t *wdelta);
285 uint64_t n2rng_ctl_wait(n2rng_t *n2rng, int rngid);
286 uint64_t n2rng_ctl_write(n2rng_t *n2rng, int rngid, uint64_t ctlregs_pa,
287     uint64_t newstate, uint64_t wtimeout, uint64_t *tdelta);
288 uint64_t n2rng_data_read_diag(n2rng_t *n2rng, int rngid, uint64_t data_pa,
289     size_t  datalen, uint64_t *tdelta);
290 uint64_t n2rng_check_ctl_access(n2rng_t *n2rng);
291 void n2rng_config_retry(n2rng_t *n2rng, clock_t seconds);
292 
293 #if defined(DEBUG)
294 
295 #define	DWARN		0x00000001
296 #define	DMA_ARGS	0x00000002
297 #define	DMA_LDST	0x00000004
298 #define	DNCS_QTAIL	0x00000008
299 #define	DATTACH		0x00000010
300 #define	DCFG		0x00000020
301 #define	DMOD		0x00000040  /* _init/_fini/_info/attach/detach */
302 #define	DENTRY		0x00000080  /* crypto routine entry/exit points */
303 #define	DHEALTH		0x00000100
304 #define	DCHATTY		0x00000200
305 #define	DKCF		0x00000400
306 #define	DALL		0xFFFFFFFF
307 
308 #define	DBG0	n2rng_dprintf
309 #define	DBG1	n2rng_dprintf
310 #define	DBG2	n2rng_dprintf
311 #define	DBG3	n2rng_dprintf
312 #define	DBG4	n2rng_dprintf
313 #define	DBG5	n2rng_dprintf
314 #define	DBG6	n2rng_dprintf
315 #define	DBGCALL(flag, func)	{ if (n2rng_dflagset(flag)) (void) func; }
316 
317 void	n2rng_dprintf(n2rng_t *, int, const char *, ...);
318 void	n2rng_dumphex(void *, int);
319 int	n2rng_dflagset(int);
320 
321 #else	/* !defined(DEBUG) */
322 
323 #define	DBG0(vca, lvl, fmt)
324 #define	DBG1(vca, lvl, fmt, arg1)
325 #define	DBG2(vca, lvl, fmt, arg1, arg2)
326 #define	DBG3(vca, lvl, fmt, arg1, arg2, arg3)
327 #define	DBG4(vca, lvl, fmt, arg1, arg2, arg3, arg4)
328 #define	DBG5(vca, lvl, fmt, arg1, arg2, arg3, arg4, arg5)
329 #define	DBG6(vca, lvl, fmt, arg1, arg2, arg3, arg4, arg5, arg6)
330 #define	DBGCALL(flag, func)
331 
332 #endif	/* !defined(DEBUG) */
333 
334 /*
335  * n2rng_kcf.c
336  */
337 int n2rng_herr2kerr(uint64_t);
338 int n2rng_logic_test(n2rng_t *, int);
339 int n2rng_noise_gen_test_set(void);
340 int n2rng_init(n2rng_t *n2rng);
341 int n2rng_uninit(n2rng_t *n2rng);
342 int n2rng_register_provider(n2rng_t *n2rng);
343 int n2rng_unregister_provider(n2rng_t *n2rng);
344 void n2rng_failure(n2rng_t *n2rng);
345 void n2rng_unconfigured(n2rng_t *n2rng);
346 
347 /*
348  * n2rng_debug.c
349  */
350 void n2rng_error(n2rng_t *, const char *, ...);
351 void n2rng_diperror(dev_info_t *, const char *, ...);
352 void n2rng_dipverror(dev_info_t *, const char *, va_list);
353 
354 uint64_t hv_rng_get_diag_control(void);
355 uint64_t hv_rng_ctl_read(uint64_t ctlregs_pa, uint64_t *state,
356     uint64_t *tdelta);
357 uint64_t hv_rng_ctl_read_v2(uint64_t ctlregs_pa, uint64_t rngid,
358     uint64_t *state, uint64_t *tdelta, uint64_t *wdelta, uint64_t *wstate);
359 uint64_t hv_rng_ctl_write(uint64_t ctlregs_pa,
360     uint64_t newstate, uint64_t wtimeout, uint64_t *tdelta);
361 uint64_t hv_rng_ctl_write_v2(uint64_t ctlregs_pa,
362     uint64_t newstate, uint64_t wtimeout, uint64_t rngid);
363 uint64_t hv_rng_data_read_diag(uint64_t data_pa,
364     size_t  datalen, uint64_t *tdelta);
365 uint64_t hv_rng_data_read_diag_v2(uint64_t data_pa,
366     size_t  datalen, uint64_t rngid, uint64_t *tdelta);
367 uint64_t hv_rng_data_read(uint64_t data_pa, uint64_t *tdelta);
368 
369 /*
370  * n2rng_post.c
371  */
372 int n2rng_fips_rng_post(void);
373 
374 #endif /* _KERNEL */
375 #endif /* !_ASM */
376 
377 #ifdef	__cplusplus
378 }
379 #endif
380 
381 #endif	/* _SYS_N2RNG_H */
382