xref: /illumos-gate/usr/src/uts/common/crypto/io/dca_kstat.c (revision f17620a4f72a29025a22655ba8735ccd20ae174f)
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 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Deimos - cryptographic acceleration based upon Broadcom 582x.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/ddi.h>
33 #include <sys/sunddi.h>
34 #include <sys/kstat.h>
35 #include <sys/crypto/dca.h>
36 
37 /*
38  * Kernel statistics.
39  */
40 static int dca_ksupdate(kstat_t *, int);
41 
42 /*
43  * Initialize Kstats.
44  */
45 void
46 dca_ksinit(dca_t *dca)
47 {
48 	char	buf[64];
49 	int	instance;
50 
51 	if (ddi_getprop(DDI_DEV_T_ANY, dca->dca_dip,
52 	    DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS, "nostats", 0) != 0) {
53 		/*
54 		 * sysadmin has explicity disabled stats to prevent
55 		 * covert channel.
56 		 */
57 		return;
58 	}
59 
60 	instance = ddi_get_instance(dca->dca_dip);
61 
62 	/*
63 	 * Interrupt kstats.
64 	 */
65 	(void) sprintf(buf, "%sc%d", DRIVER, instance);
66 	if ((dca->dca_intrstats = kstat_create(DRIVER, instance, buf,
67 	    "controller", KSTAT_TYPE_INTR, 1, 0)) == NULL) {
68 		dca_error(dca, "unable to create interrupt kstat");
69 	} else {
70 		kstat_install(dca->dca_intrstats);
71 	}
72 
73 	/*
74 	 * Named kstats.
75 	 */
76 	if ((dca->dca_ksp = kstat_create(DRIVER, instance, NULL, "misc",
77 	    KSTAT_TYPE_NAMED, sizeof (dca_stat_t) / sizeof (kstat_named_t),
78 	    KSTAT_FLAG_WRITABLE)) == NULL) {
79 		dca_error(dca, "unable to create kstats");
80 	} else {
81 		dca_stat_t *dkp = (dca_stat_t *)dca->dca_ksp->ks_data;
82 		kstat_named_init(&dkp->ds_status, "status", KSTAT_DATA_CHAR);
83 		kstat_named_init(&dkp->ds_mcr[0].ds_submit, "mcr1submit",
84 		    KSTAT_DATA_ULONGLONG);
85 		kstat_named_init(&dkp->ds_mcr[0].ds_flowctl, "mcr1flowctl",
86 		    KSTAT_DATA_ULONGLONG);
87 		kstat_named_init(&dkp->ds_mcr[0].ds_lowater, "mcr1lowater",
88 		    KSTAT_DATA_ULONGLONG);
89 		kstat_named_init(&dkp->ds_mcr[0].ds_hiwater, "mcr1hiwater",
90 		    KSTAT_DATA_ULONGLONG);
91 		kstat_named_init(&dkp->ds_mcr[0].ds_maxreqs, "mcr1maxreqs",
92 		    KSTAT_DATA_ULONGLONG);
93 		kstat_named_init(&dkp->ds_mcr[1].ds_submit, "mcr2submit",
94 		    KSTAT_DATA_ULONGLONG);
95 		kstat_named_init(&dkp->ds_mcr[1].ds_flowctl, "mcr2flowctl",
96 		    KSTAT_DATA_ULONGLONG);
97 		kstat_named_init(&dkp->ds_mcr[1].ds_lowater, "mcr2lowater",
98 		    KSTAT_DATA_ULONGLONG);
99 		kstat_named_init(&dkp->ds_mcr[1].ds_hiwater, "mcr2hiwater",
100 		    KSTAT_DATA_ULONGLONG);
101 		kstat_named_init(&dkp->ds_mcr[1].ds_maxreqs, "mcr2maxreqs",
102 		    KSTAT_DATA_ULONGLONG);
103 #ifdef	DS_RC4JOBS
104 		/* rc4 */
105 		kstat_named_init(&dkp->ds_algs[DS_RC4JOBS], "rc4jobs",
106 		    KSTAT_DATA_ULONGLONG);
107 #endif
108 #ifdef	DS_RC4BYTES
109 		kstat_named_init(&dkp->ds_algs[DS_RC4BYTES], "rc4bytes",
110 		    KSTAT_DATA_ULONGLONG);
111 #endif
112 		/* 3des */
113 		kstat_named_init(&dkp->ds_algs[DS_3DESJOBS], "3desjobs",
114 		    KSTAT_DATA_ULONGLONG);
115 		kstat_named_init(&dkp->ds_algs[DS_3DESBYTES], "3desbytes",
116 		    KSTAT_DATA_ULONGLONG);
117 		/* rsa */
118 		kstat_named_init(&dkp->ds_algs[DS_RSAPUBLIC], "rsapublic",
119 		    KSTAT_DATA_ULONGLONG);
120 		kstat_named_init(&dkp->ds_algs[DS_RSAPRIVATE], "rsaprivate",
121 		    KSTAT_DATA_ULONGLONG);
122 		/* dsa */
123 		kstat_named_init(&dkp->ds_algs[DS_DSASIGN], "dsasign",
124 		    KSTAT_DATA_ULONGLONG);
125 		kstat_named_init(&dkp->ds_algs[DS_DSAVERIFY], "dsaverify",
126 		    KSTAT_DATA_ULONGLONG);
127 #ifdef	DS_DHPUBLIC
128 		/* diffie-hellman */
129 		kstat_named_init(&dkp->ds_algs[DS_DHPUBLIC], "dhpublic",
130 		    KSTAT_DATA_ULONGLONG);
131 #endif
132 #ifdef	DS_DHSECRET
133 		kstat_named_init(&dkp->ds_algs[DS_DHSECRET], "dhsecret",
134 		    KSTAT_DATA_ULONGLONG);
135 #endif
136 		/* random number jobs */
137 		kstat_named_init(&dkp->ds_algs[DS_RNGJOBS], "rngjobs",
138 		    KSTAT_DATA_ULONGLONG);
139 		kstat_named_init(&dkp->ds_algs[DS_RNGBYTES], "rngbytes",
140 		    KSTAT_DATA_ULONGLONG);
141 		kstat_named_init(&dkp->ds_algs[DS_RNGSHA1JOBS], "rngsha1jobs",
142 		    KSTAT_DATA_ULONGLONG);
143 		kstat_named_init(&dkp->ds_algs[DS_RNGSHA1BYTES],
144 		    "rngsha1bytes", KSTAT_DATA_ULONGLONG);
145 		dca->dca_ksp->ks_update = dca_ksupdate;
146 		dca->dca_ksp->ks_private = dca;
147 		kstat_install(dca->dca_ksp);
148 	}
149 }
150 
151 /*
152  * Update Kstats.
153  */
154 int
155 dca_ksupdate(kstat_t *ksp, int rw)
156 {
157 	dca_t		*dca;
158 	dca_stat_t	*dkp;
159 	int		i;
160 
161 	dca = (dca_t *)ksp->ks_private;
162 	dkp = (dca_stat_t *)ksp->ks_data;
163 
164 	if (rw == KSTAT_WRITE) {
165 		for (i = 0; i < DS_MAX; i++) {
166 			dca->dca_stats[i] = dkp->ds_algs[i].value.ull;
167 		}
168 		for (i = MCR1; i <= MCR2; i++) {
169 			WORKLIST(dca, i)->dwl_submit =
170 			    dkp->ds_mcr[i - 1].ds_submit.value.ull;
171 			WORKLIST(dca, i)->dwl_flowctl =
172 			    dkp->ds_mcr[i - 1].ds_flowctl.value.ull;
173 			/* hiwater, lowater, and maxreqs are read only */
174 		}
175 	} else {
176 		/* handy status value */
177 		if (dca->dca_flags & DCA_FAILED) {
178 			/* device has failed */
179 			(void) strcpy(dkp->ds_status.value.c, "fail");
180 		} else if ((WORKLIST(dca, MCR1)->dwl_drain) ||
181 		    (WORKLIST(dca, MCR2)->dwl_drain)) {
182 			/* device is draining for DR */
183 			(void) strcpy(dkp->ds_status.value.c, "drain");
184 		} else {
185 			/* everything looks good */
186 			(void) strcpy(dkp->ds_status.value.c, "online");
187 		}
188 
189 		for (i = 0; i < DS_MAX; i++) {
190 			dkp->ds_algs[i].value.ull = dca->dca_stats[i];
191 		}
192 		for (i = MCR1; i <= MCR2; i++) {
193 			dkp->ds_mcr[i - 1].ds_submit.value.ull =
194 			    WORKLIST(dca, i)->dwl_submit;
195 			dkp->ds_mcr[i - 1].ds_flowctl.value.ull =
196 			    WORKLIST(dca, i)->dwl_flowctl;
197 			dkp->ds_mcr[i - 1].ds_lowater.value.ull =
198 			    WORKLIST(dca, i)->dwl_lowater;
199 			dkp->ds_mcr[i - 1].ds_hiwater.value.ull =
200 			    WORKLIST(dca, i)->dwl_hiwater;
201 			dkp->ds_mcr[i - 1].ds_maxreqs.value.ull =
202 			    WORKLIST(dca, i)->dwl_reqspermcr;
203 		}
204 	}
205 	return (0);
206 }
207