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 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #ifndef _SYS_N2RNG_H 27 #define _SYS_N2RNG_H 28 29 #pragma ident "%Z%%M% %I% %E% SMI" 30 31 /* skip following stuff when included in n2rng_hcall.s */ 32 #ifndef _ASM 33 #include <sys/types.h> 34 #include <sys/mutex.h> 35 #include <sys/ksynch.h> 36 #include <sys/sunddi.h> 37 #include <sys/param.h> 38 #include <sys/crypto/common.h> 39 #include <sys/crypto/spi.h> 40 #include <sys/mdesc.h> 41 42 #endif /* !_ASM */ 43 44 #ifdef __cplusplus 45 extern "C" { 46 #endif 47 48 #define HV_RNG_GET_DIAG_CONTROL 0x130 49 #define HV_RNG_CTL_READ 0x131 50 #define HV_RNG_CTL_WRITE 0x132 51 #define HV_RNG_DATA_READ_DIAG 0x133 52 #define HV_RNG_DATA_READ 0x134 53 54 #define CTL_STATE_UNCONFIGURED 0 55 #define CTL_STATE_CONFIGURED 1 56 #define CTL_STATE_HEALTHCHECK 2 57 #define CTL_STATE_ERROR 3 58 59 #define NRNGCTL 4 60 #define N2RNG_MAX_READ (128 * 1024) /* 128K bytes */ 61 62 #define DRIVER "n2rng" 63 #define N2RNG_MANUFACTURER_ID "SUNWn2rng" 64 65 #define N2RNG_BINDNAME_N2 "SUNW,n2-rng" 66 #define N2RNG_BINDNAME_VF "SUNW,vf-rng" 67 68 #define N2RNG_MAX_RNGS 4 69 #define N2RNG_INVALID_ID (-1) 70 71 #ifndef _ASM 72 73 typedef enum { 74 N2RNG_CPU_UNKNOWN, 75 N2RNG_CPU_N2, 76 N2RNG_CPU_VF 77 } n2rng_binding_t; 78 79 typedef union n2rngctl { 80 uint64_t word; 81 struct { 82 uint64_t rnc_res : 39; 83 uint64_t rnc_cnt : 16; 84 uint64_t rnc_bypass : 1; 85 uint64_t rnc_vcoctl : 2; 86 uint64_t rnc_anlg_sel : 2; 87 uint64_t rnc_mode : 1; 88 uint64_t rnc_selbits : 3; 89 } fields; 90 } n2rng_ctl_t; 91 92 typedef struct { 93 n2rng_ctl_t ctlwds[NRNGCTL]; 94 } n2rng_setup_t; 95 96 #if defined(_KERNEL) 97 98 /* 99 * Our contiguous memory alignment requirement is 100 * only for 8 bytes, however contig mem allocation 101 * routines requirement minimum of 64. 102 */ 103 #define CONTIG_ALIGNMENT 64 104 105 /* 106 * Returns 1 only if the address range of a variable of type type at 107 * ptr falls entirely on one page. Based on page size of 4K. May 108 * give some false negatives on larger page sizes. 109 */ 110 #define CONTIGUOUS(ptr, type) \ 111 (((((uint64_t)(ptr)) ^ ((uint64_t)(ptr) + sizeof (type) -1)) \ 112 & PAGEMASK) == 0) 113 114 /* 115 * The RNG hardware can send certain internal analog signals to an 116 * external pin on the chip. Setting the rnc_anlg_sel bit to 117 * N2RNG_NOANALOGOUT deselects all analog signals (perhaps selects 118 * ground). Choosing any other value would aid an attacker with 119 * physical access to the chip. 120 */ 121 #define N2RNG_NOANALOGOUT 0x2 122 123 /* 124 * There can only be N2_RNG_FIPS_INSTANCES concurrent RNG requsts from 125 * the framework. Making this value large helps benchmarks. It 126 * should probably come from a conf file, but for now it is hard 127 * coded. The code computes i % N2RNG_FIPS_INSTANCES, which is more 128 * efficient when N2RNG_FIPS_INSTANCES is a power of 2. 129 */ 130 #define N2RNG_FIPS_INSTANCES 8 131 132 typedef struct fipsrandomstruct fipsrandomstruct_t; 133 struct fipsrandomstruct { 134 kmutex_t mtx; 135 uint64_t entropyhunger; /* RNGs generated with no entropy */ 136 uint32_t XKEY[6]; /* one extra word for getentropy */ 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_NOSC][N2RNG_NBIASES]; 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 #endif /* _KERNEL */ 370 #endif /* !_ASM */ 371 372 #ifdef __cplusplus 373 } 374 #endif 375 376 #endif /* _SYS_N2RNG_H */ 377