hifn7751var.h revision 1.3 1 /* $NetBSD: hifn7751var.h,v 1.3 2003/07/30 18:49:28 jonathan Exp $ */
2 /* $OpenBSD: hifn7751var.h,v 1.18 2000/06/02 22:36:45 deraadt Exp $ */
3
4 /*
5 * Invertex AEON / Hifn 7751 driver
6 * Copyright (c) 1999 Invertex Inc. All rights reserved.
7 * Copyright (c) 1999 Theo de Raadt
8 * Copyright (c) 2000-2001 Network Security Technologies, Inc.
9 * http://www.netsec.net
10 *
11 * Please send any comments, feedback, bug-fixes, or feature requests to
12 * software (at) invertex.com.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. The name of the author may not be used to endorse or promote products
24 * derived from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Effort sponsored in part by the Defense Advanced Research Projects
38 * Agency (DARPA) and Air Force Research Laboratory, Air Force
39 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
40 *
41 */
42
43 #ifndef __DEV_PCI_HIFN7751VAR_H__
44 #define __DEV_PCI_HIFN7751VAR_H__
45
46 #ifdef _KERNEL
47
48 /*
49 * Some configurable values for the driver
50 */
51 #define HIFN_D_CMD_RSIZE 24 /* command descriptors */
52 #define HIFN_D_SRC_RSIZE 80 /* source descriptors */
53 #define HIFN_D_DST_RSIZE 80 /* destination descriptors */
54 #define HIFN_D_RES_RSIZE 24 /* result descriptors */
55
56 /*
57 * Length values for cryptography
58 */
59 #define HIFN_DES_KEY_LENGTH 8
60 #define HIFN_3DES_KEY_LENGTH 24
61 #define HIFN_MAX_CRYPT_KEY_LENGTH HIFN_3DES_KEY_LENGTH
62 #define HIFN_IV_LENGTH 8
63
64 /*
65 * Length values for authentication
66 */
67 #define HIFN_MAC_KEY_LENGTH 64
68 #define HIFN_MD5_LENGTH 16
69 #define HIFN_SHA1_LENGTH 20
70 #define HIFN_MAC_TRUNC_LENGTH 12
71
72 #define MAX_SCATTER 64
73
74 /*
75 * Data structure to hold all 4 rings and any other ring related data.
76 */
77 struct hifn_dma {
78 /*
79 * Descriptor rings. We add +1 to the size to accommodate the
80 * jump descriptor.
81 */
82 struct hifn_desc cmdr[HIFN_D_CMD_RSIZE+1];
83 struct hifn_desc srcr[HIFN_D_SRC_RSIZE+1];
84 struct hifn_desc dstr[HIFN_D_DST_RSIZE+1];
85 struct hifn_desc resr[HIFN_D_RES_RSIZE+1];
86
87 struct hifn_command *hifn_commands[HIFN_D_RES_RSIZE];
88
89 u_char command_bufs[HIFN_D_CMD_RSIZE][HIFN_MAX_COMMAND];
90 u_char result_bufs[HIFN_D_CMD_RSIZE][HIFN_MAX_RESULT];
91 u_int32_t slop[HIFN_D_CMD_RSIZE];
92
93 u_int64_t test_src, test_dst;
94
95 /*
96 * Our current positions for insertion and removal from the descriptor
97 * rings.
98 */
99 int cmdi, srci, dsti, resi;
100 volatile int cmdu, srcu, dstu, resu;
101 int cmdk, srck, dstk, resk;
102 };
103
104 struct hifn_session {
105 int hs_state;
106 int hs_prev_op; /* XXX collapse into hs_flags? */
107 u_int8_t hs_iv[HIFN_IV_LENGTH];
108 };
109
110 /* We use a state machine on sessions */
111 #define HS_STATE_FREE 0 /* unused session entry */
112 #define HS_STATE_USED 1 /* allocated, but key not on card */
113 #define HS_STATE_KEY 2 /* allocated and key is on card */
114
115 #define HIFN_RING_SYNC(sc, r, i, f) \
116 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_dmamap, \
117 offsetof(struct hifn_dma, r[i]), sizeof(struct hifn_desc), (f))
118
119 #define HIFN_CMDR_SYNC(sc, i, f) HIFN_RING_SYNC((sc), cmdr, (i), (f))
120 #define HIFN_RESR_SYNC(sc, i, f) HIFN_RING_SYNC((sc), resr, (i), (f))
121 #define HIFN_SRCR_SYNC(sc, i, f) HIFN_RING_SYNC((sc), srcr, (i), (f))
122 #define HIFN_DSTR_SYNC(sc, i, f) HIFN_RING_SYNC((sc), dstr, (i), (f))
123
124 #define HIFN_CMD_SYNC(sc, i, f) \
125 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_dmamap, \
126 offsetof(struct hifn_dma, command_bufs[(i)][0]), \
127 HIFN_MAX_COMMAND, (f))
128
129 #define HIFN_RES_SYNC(sc, i, f) \
130 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_dmamap, \
131 offsetof(struct hifn_dma, result_bufs[(i)][0]), \
132 HIFN_MAX_RESULT, (f))
133
134 /*
135 * Holds data specific to a single HIFN board.
136 */
137 struct hifn_softc {
138 struct device sc_dv; /* generic device */
139 void * sc_ih; /* interrupt handler cookie */
140 u_int32_t sc_dmaier;
141 u_int32_t sc_drammodel; /* 1=dram, 0=sram */
142
143 bus_space_handle_t sc_sh0, sc_sh1;
144 bus_space_tag_t sc_st0, sc_st1;
145 bus_dma_tag_t sc_dmat;
146
147 struct hifn_dma *sc_dma;
148 bus_dmamap_t sc_dmamap;
149 bus_dma_segment_t sc_dmasegs[1];
150 int sc_dmansegs;
151 int32_t sc_cid;
152 int sc_maxses;
153 int sc_ramsize;
154 int sc_flags;
155 #define HIFN_HAS_RNG 1
156 #define HIFN_HAS_PUBLIC 2
157 #define HIFN_IS_7811 4
158 #define HIFN_NO_BURSTWRITE 8
159 #define HIFN_HAS_LEDS 16
160 struct callout sc_rngto; /* rng timeout */
161 struct callout sc_tickto; /* led-clear timeout */
162 int sc_rngfirst;
163 int sc_rnghz;
164 int sc_c_busy; /* command ring busy */
165 int sc_s_busy; /* source data ring busy */
166 int sc_d_busy; /* destination data ring busy */
167 int sc_r_busy; /* result ring busy */
168 int sc_active; /* for initial countdown */
169 int sc_needwakeup; /* ops q'd wating on resources */
170 int sc_curbatch; /* # ops submitted w/o int */
171 int sc_suspended;
172 struct hifn_session sc_sessions[2048];
173 pci_chipset_tag_t sc_pci_pc;
174 pcitag_t sc_pci_tag;
175 bus_size_t sc_waw_lastreg;
176 int sc_waw_lastgroup;
177 };
178
179 #define WRITE_REG_0(sc,reg,val) hifn_write_4((sc), 0, (reg), (val))
180 #define WRITE_REG_1(sc,reg,val) hifn_write_4((sc), 1, (reg), (val))
181 #define READ_REG_0(sc,reg) hifn_read_4((sc), 0, (reg))
182 #define READ_REG_1(sc,reg) hifn_read_4((sc), 1, (reg))
183
184 #define SET_LED(sc,v) \
185 if (sc->sc_flags & HIFN_HAS_LEDS) \
186 WRITE_REG_1(sc, HIFN_1_7811_MIPSRST, \
187 READ_REG_1(sc, HIFN_1_7811_MIPSRST) | (v))
188 #define CLR_LED(sc,v) \
189 if (sc->sc_flags & HIFN_HAS_LEDS) \
190 WRITE_REG_1(sc, HIFN_1_7811_MIPSRST, \
191 READ_REG_1(sc, HIFN_1_7811_MIPSRST) & ~(v))
192
193 /*
194 * struct hifn_command
195 *
196 * This is the control structure used to pass commands to hifn_encrypt().
197 *
198 * flags
199 * -----
200 * Flags is the bitwise "or" values for command configuration. A single
201 * encrypt direction needs to be set:
202 *
203 * HIFN_ENCODE or HIFN_DECODE
204 *
205 * To use cryptography, a single crypto algorithm must be included:
206 *
207 * HIFN_CRYPT_3DES or HIFN_CRYPT_DES
208 *
209 * To use authentication, a single MAC algorithm must be included:
210 *
211 * HIFN_MAC_MD5 or HIFN_MAC_SHA1
212 *
213 * By default MD5 uses a 16 byte hash and SHA-1 uses a 20 byte hash.
214 * If the value below is set, hash values are truncated or assumed
215 * truncated to 12 bytes:
216 *
217 * HIFN_MAC_TRUNC
218 *
219 * Keys for encryption and authentication can be sent as part of a command,
220 * or the last key value used with a particular session can be retrieved
221 * and used again if either of these flags are not specified.
222 *
223 * HIFN_CRYPT_NEW_KEY, HIFN_MAC_NEW_KEY
224 *
225 * session_num
226 * -----------
227 * A number between 0 and 2048 (for DRAM models) or a number between
228 * 0 and 768 (for SRAM models). Those who don't want to use session
229 * numbers should leave value at zero and send a new crypt key and/or
230 * new MAC key on every command. If you use session numbers and
231 * don't send a key with a command, the last key sent for that same
232 * session number will be used.
233 *
234 * Warning: Using session numbers and multiboard at the same time
235 * is currently broken.
236 *
237 * mbuf
238 * ----
239 * Either fill in the mbuf pointer and npa=0 or
240 * fill packp[] and packl[] and set npa to > 0
241 *
242 * mac_header_skip
243 * ---------------
244 * The number of bytes of the source_buf that are skipped over before
245 * authentication begins. This must be a number between 0 and 2^16-1
246 * and can be used by IPsec implementers to skip over IP headers.
247 * *** Value ignored if authentication not used ***
248 *
249 * crypt_header_skip
250 * -----------------
251 * The number of bytes of the source_buf that are skipped over before
252 * the cryptographic operation begins. This must be a number between 0
253 * and 2^16-1. For IPsec, this number will always be 8 bytes larger
254 * than the auth_header_skip (to skip over the ESP header).
255 * *** Value ignored if cryptography not used ***
256 *
257 */
258 struct hifn_command {
259 u_int16_t session_num;
260 u_int16_t base_masks, cry_masks, mac_masks, comp_masks;
261 u_int8_t iv[HIFN_IV_LENGTH], *ck, mac[HIFN_MAC_KEY_LENGTH];
262 int cklen;
263 int sloplen, slopidx;
264
265 union {
266 struct mbuf *src_m;
267 struct uio *src_io;
268 } srcu;
269 bus_dmamap_t src_map;
270
271 union {
272 struct mbuf *dst_m;
273 struct uio *dst_io;
274 } dstu;
275 bus_dmamap_t dst_map;
276
277 u_short mac_header_skip, mac_process_len;
278 u_short crypt_header_skip, crypt_process_len;
279
280 struct hifn_softc *softc;
281 struct cryptop *crp;
282 struct cryptodesc *enccrd, *maccrd, *compcrd;
283
284 };
285
286 /*
287 * Return values for hifn_crypto()
288 */
289 #define HIFN_CRYPTO_SUCCESS 0
290 #define HIFN_CRYPTO_BAD_INPUT (-1)
291 #define HIFN_CRYPTO_RINGS_FULL (-2)
292
293
294 /**************************************************************************
295 *
296 * Function: hifn_crypto
297 *
298 * Purpose: Called by external drivers to begin an encryption on the
299 * HIFN board.
300 *
301 * Blocking/Non-blocking Issues
302 * ============================
303 * The driver cannot block in hifn_crypto (no calls to tsleep) currently.
304 * hifn_crypto() returns HIFN_CRYPTO_RINGS_FULL if there is not enough
305 * room in any of the rings for the request to proceed.
306 *
307 * Return Values
308 * =============
309 * 0 for success, negative values on error
310 *
311 * Defines for negative error codes are:
312 *
313 * HIFN_CRYPTO_BAD_INPUT : The passed in command had invalid settings.
314 * HIFN_CRYPTO_RINGS_FULL : All DMA rings were full and non-blocking
315 * behaviour was requested.
316 *
317 *************************************************************************/
318
319 /*
320 * Convert back and forth from 'sid' to 'card' and 'session'
321 */
322 #define HIFN_CARD(sid) (((sid) & 0xf0000000) >> 28)
323 #define HIFN_SESSION(sid) ((sid) & 0x000007ff)
324 #define HIFN_SID(crd,ses) (((crd) << 28) | ((ses) & 0x7ff))
325
326 #endif /* _KERNEL */
327
328 struct hifn_stats {
329 u_int64_t hst_ibytes;
330 u_int64_t hst_obytes;
331 u_int32_t hst_ipackets;
332 u_int32_t hst_opackets;
333 u_int32_t hst_invalid;
334 u_int32_t hst_nomem;
335 u_int32_t hst_abort;
336 };
337
338 #endif /* __DEV_PCI_HIFN7751VAR_H__ */
339