1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/systm.h>
#include <sys/kstat.h>
#include <sys/crypto/common.h>
#include <sys/crypto/spi.h>
#include <sys/n2rng.h>
/*
* Kernel statistics.
*/
static int n2rng_ksupdate(kstat_t *, int);
/*
* Initialize Kstats.
*/
void
n2rng_ksinit(n2rng_t *n2rng)
{
int instance;
int i;
int j;
char buf[64];
if (ddi_getprop(DDI_DEV_T_ANY, n2rng->n_dip,
DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS, "nostats", 0) != 0) {
/*
* sysadmin has explicity disabled stats to prevent
* covert channel.
*/
return;
}
instance = ddi_get_instance(n2rng->n_dip);
/*
* Named kstats.
*/
n2rng->n_ksp = kstat_create(DRIVER, instance, NULL, "misc",
KSTAT_TYPE_NAMED,
sizeof (n2rng_stat_t) / sizeof (kstat_named_t),
KSTAT_FLAG_WRITABLE);
if (n2rng->n_ksp == NULL) {
n2rng_error(n2rng, "unable to create kstats");
} else {
n2rng_stat_t *dkp = (n2rng_stat_t *)n2rng->n_ksp->ks_data;
kstat_named_init(&dkp->ns_status, "status", KSTAT_DATA_CHAR);
kstat_named_init(&dkp->ns_algs[DS_RNGJOBS], "rngjobs",
KSTAT_DATA_ULONGLONG);
kstat_named_init(&dkp->ns_algs[DS_RNGBYTES], "rngbytes",
KSTAT_DATA_ULONGLONG);
if (n2rng_iscontrol(n2rng)) {
for (i = 0; i < n2rng->n_ctl_data->n_num_rngs; i++) {
(void) sprintf(buf, "rng%d-state", i);
kstat_named_init(&dkp->ns_rngstate[i],
buf, KSTAT_DATA_CHAR);
for (j = 0; j < N2RNG_NOSC; j++) {
(void) sprintf(buf,
"rng%d-cell%d-bias", i, j);
kstat_named_init
(&dkp->ns_rngbias[i][j],
buf, KSTAT_DATA_ULONGLONG);
(void) sprintf(buf,
"rng%d-cell%d-entropy", i, j);
kstat_named_init
(&dkp->ns_rngentropy[i][j],
buf, KSTAT_DATA_ULONGLONG);
}
}
}
n2rng->n_ksp->ks_update = n2rng_ksupdate;
n2rng->n_ksp->ks_private = n2rng;
kstat_install(n2rng->n_ksp);
}
}
/*
* Deinitialize Kstats.
*/
void
n2rng_ksdeinit(n2rng_t *n2rng)
{
if (n2rng->n_ksp != NULL) {
kstat_delete(n2rng->n_ksp);
n2rng->n_ksp = NULL;
}
}
/*
* Update Kstats.
*/
int
n2rng_ksupdate(kstat_t *ksp, int rw)
{
n2rng_t *n2rng;
n2rng_stat_t *dkp;
int i;
int j;
n2rng = (n2rng_t *)ksp->ks_private;
dkp = (n2rng_stat_t *)ksp->ks_data;
if (rw == KSTAT_WRITE) {
for (i = 0; i < DS_MAX; i++) {
n2rng->n_stats[i] = dkp->ns_algs[i].value.ull;
}
} else {
/* handy status value */
if (n2rng_isfailed(n2rng)) {
/* device has failed */
(void) strcpy(dkp->ns_status.value.c, "failed");
} else if (!n2rng_isconfigured(n2rng)) {
/* device is not configured */
(void) strcpy(dkp->ns_status.value.c, "offline");
} else {
/* everything looks good */
(void) strcpy(dkp->ns_status.value.c, "online");
}
for (i = 0; i < DS_MAX; i++) {
dkp->ns_algs[i].value.ull = n2rng->n_stats[i];
}
if (n2rng_iscontrol(n2rng)) {
rng_entry_t *rng;
for (i = 0; i < n2rng->n_ctl_data->n_num_rngs; i++) {
rng = &n2rng->n_ctl_data->n_rngs[i];
switch (rng->n_rng_state) {
case CTL_STATE_ERROR:
(void) strcpy(
dkp->ns_rngstate[i].value.c,
"error");
break;
case CTL_STATE_HEALTHCHECK:
(void) strcpy(
dkp->ns_rngstate[i].value.c,
"healthcheck");
break;
case CTL_STATE_CONFIGURED:
(void) strcpy(
dkp->ns_rngstate[i].value.c,
"online");
break;
case CTL_STATE_UNCONFIGURED:
(void) strcpy(
dkp->ns_rngstate[i].value.c,
"offline");
break;
default:
(void) strcpy(
dkp->ns_rngstate[i].value.c,
"unknown");
break;
}
for (j = 0; j < N2RNG_NOSC; j++) {
dkp->ns_rngbias[i][j].value.ull =
rng->n_bias_info[j].bias;
dkp->ns_rngentropy[i][j].value.ull =
rng->n_bias_info[j].entropy;
}
}
}
}
return (0);
}
|