xref: /freebsd/sys/dev/hifn/hifn7751var.h (revision 405c3050f102b8c74782f0366c8ead927bd07b68)
1 /* $FreeBSD$ */
2 /*	$OpenBSD: hifn7751var.h,v 1.42 2002/04/08 17:49:42 jason Exp $	*/
3 
4 /*-
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Invertex AEON / Hifn 7751 driver
8  * Copyright (c) 1999 Invertex Inc. All rights reserved.
9  * Copyright (c) 1999 Theo de Raadt
10  * Copyright (c) 2000-2001 Network Security Technologies, Inc.
11  *			http://www.netsec.net
12  *
13  * Please send any comments, feedback, bug-fixes, or feature requests to
14  * software@invertex.com.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  *
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  * 3. The name of the author may not be used to endorse or promote products
26  *    derived from this software without specific prior written permission.
27  *
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
34  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  *
40  * Effort sponsored in part by the Defense Advanced Research Projects
41  * Agency (DARPA) and Air Force Research Laboratory, Air Force
42  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
43  *
44  */
45 
46 #ifndef __HIFN7751VAR_H__
47 #define __HIFN7751VAR_H__
48 
49 #ifdef _KERNEL
50 
51 /*
52  * Some configurable values for the driver.  By default command+result
53  * descriptor rings are the same size.  The src+dst descriptor rings
54  * are sized at 3.5x the number of potential commands.  Slower parts
55  * (e.g. 7951) tend to run out of src descriptors; faster parts (7811)
56  * src+cmd/result descriptors.  It's not clear that increasing the size
57  * of the descriptor rings helps performance significantly as other
58  * factors tend to come into play (e.g. copying misaligned packets).
59  */
60 #define	HIFN_D_CMD_RSIZE	24	/* command descriptors */
61 #define	HIFN_D_SRC_RSIZE	((HIFN_D_CMD_RSIZE * 7) / 2)	/* source descriptors */
62 #define	HIFN_D_RES_RSIZE	HIFN_D_CMD_RSIZE	/* result descriptors */
63 #define	HIFN_D_DST_RSIZE	HIFN_D_SRC_RSIZE	/* destination descriptors */
64 
65 /*
66  *  Length values for cryptography
67  */
68 #define HIFN_DES_KEY_LENGTH		8
69 #define HIFN_3DES_KEY_LENGTH		24
70 #define HIFN_MAX_CRYPT_KEY_LENGTH	HIFN_3DES_KEY_LENGTH
71 #define HIFN_IV_LENGTH			8
72 #define	HIFN_AES_IV_LENGTH		16
73 #define HIFN_MAX_IV_LENGTH		HIFN_AES_IV_LENGTH
74 
75 /*
76  *  Length values for authentication
77  */
78 #define HIFN_MAC_KEY_LENGTH		64
79 #define HIFN_MD5_LENGTH			16
80 #define HIFN_SHA1_LENGTH		20
81 #define HIFN_MAC_TRUNC_LENGTH		12
82 
83 #define MAX_SCATTER 64
84 
85 /*
86  * Data structure to hold all 4 rings and any other ring related data
87  * that should reside in DMA.
88  */
89 struct hifn_dma {
90 	/*
91 	 *  Descriptor rings.  We add +1 to the size to accomidate the
92 	 *  jump descriptor.
93 	 */
94 	struct hifn_desc	cmdr[HIFN_D_CMD_RSIZE+1];
95 	struct hifn_desc	srcr[HIFN_D_SRC_RSIZE+1];
96 	struct hifn_desc	dstr[HIFN_D_DST_RSIZE+1];
97 	struct hifn_desc	resr[HIFN_D_RES_RSIZE+1];
98 
99 
100 	u_char			command_bufs[HIFN_D_CMD_RSIZE][HIFN_MAX_COMMAND];
101 	u_char			result_bufs[HIFN_D_CMD_RSIZE][HIFN_MAX_RESULT];
102 	u_int32_t		slop[HIFN_D_CMD_RSIZE];
103 	u_int64_t		test_src, test_dst;
104 } ;
105 
106 
107 struct hifn_session {
108 	u_int8_t hs_iv[HIFN_MAX_IV_LENGTH];
109 	int hs_mlen;
110 };
111 
112 #define	HIFN_RING_SYNC(sc, r, i, f)					\
113 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_dmamap, (f))
114 
115 #define	HIFN_CMDR_SYNC(sc, i, f)	HIFN_RING_SYNC((sc), cmdr, (i), (f))
116 #define	HIFN_RESR_SYNC(sc, i, f)	HIFN_RING_SYNC((sc), resr, (i), (f))
117 #define	HIFN_SRCR_SYNC(sc, i, f)	HIFN_RING_SYNC((sc), srcr, (i), (f))
118 #define	HIFN_DSTR_SYNC(sc, i, f)	HIFN_RING_SYNC((sc), dstr, (i), (f))
119 
120 #define	HIFN_CMD_SYNC(sc, i, f)						\
121 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_dmamap, (f))
122 
123 #define	HIFN_RES_SYNC(sc, i, f)						\
124 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_dmamap, (f))
125 
126 /*
127  * Holds data specific to a single HIFN board.
128  */
129 struct hifn_softc {
130 	device_t		sc_dev;		/* device backpointer */
131 	struct mtx		sc_mtx;		/* per-instance lock */
132 	bus_dma_tag_t		sc_dmat;	/* parent DMA tag descriptor */
133 	struct resource		*sc_bar0res;
134 	bus_space_handle_t	sc_sh0;		/* bar0 bus space handle */
135 	bus_space_tag_t		sc_st0;		/* bar0 bus space tag */
136 	bus_size_t		sc_bar0_lastreg;/* bar0 last reg written */
137 	struct resource		*sc_bar1res;
138 	bus_space_handle_t	sc_sh1;		/* bar1 bus space handle */
139 	bus_space_tag_t		sc_st1;		/* bar1 bus space tag */
140 	bus_size_t		sc_bar1_lastreg;/* bar1 last reg written */
141 	struct resource		*sc_irq;
142 	void			*sc_intrhand;	/* interrupt handle */
143 
144 	u_int32_t		sc_dmaier;
145 	u_int32_t		sc_drammodel;	/* 1=dram, 0=sram */
146 	u_int32_t		sc_pllconfig;	/* 7954/7955/7956 PLL config */
147 
148 	struct hifn_dma		*sc_dma;
149 	bus_dmamap_t		sc_dmamap;
150 	bus_dma_segment_t 	sc_dmasegs[1];
151 	bus_addr_t		sc_dma_physaddr;/* physical address of sc_dma */
152 	int			sc_dmansegs;
153 	struct hifn_command	*sc_hifn_commands[HIFN_D_RES_RSIZE];
154 	/*
155 	 *  Our current positions for insertion and removal from the desriptor
156 	 *  rings.
157 	 */
158 	int			sc_cmdi, sc_srci, sc_dsti, sc_resi;
159 	volatile int		sc_cmdu, sc_srcu, sc_dstu, sc_resu;
160 	int			sc_cmdk, sc_srck, sc_dstk, sc_resk;
161 
162 	int32_t			sc_cid;
163 	int			sc_maxses;
164 	int			sc_ramsize;
165 	int			sc_flags;
166 #define	HIFN_HAS_RNG		0x1	/* includes random number generator */
167 #define	HIFN_HAS_PUBLIC		0x2	/* includes public key support */
168 #define	HIFN_HAS_AES		0x4	/* includes AES support */
169 #define	HIFN_IS_7811		0x8	/* Hifn 7811 part */
170 #define	HIFN_IS_7956		0x10	/* Hifn 7956/7955 don't have SDRAM */
171 	struct callout		sc_rngto;	/* for polling RNG */
172 	struct callout		sc_tickto;	/* for managing DMA */
173 	int			sc_rngfirst;
174 	int			sc_rnghz;	/* RNG polling frequency */
175 	struct rndtest_state	*sc_rndtest;	/* RNG test state */
176 	void			(*sc_harvest)(struct rndtest_state *,
177 					void *, u_int);
178 	int			sc_c_busy;	/* command ring busy */
179 	int			sc_s_busy;	/* source data ring busy */
180 	int			sc_d_busy;	/* destination data ring busy */
181 	int			sc_r_busy;	/* result ring busy */
182 	int			sc_active;	/* for initial countdown */
183 	int			sc_needwakeup;	/* ops q'd wating on resources */
184 	int			sc_curbatch;	/* # ops submitted w/o int */
185 	int			sc_suspended;
186 #ifdef HIFN_VULCANDEV
187 	struct cdev            *sc_pkdev;
188 #endif
189 };
190 
191 #define	HIFN_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
192 #define	HIFN_UNLOCK(_sc)	mtx_unlock(&(_sc)->sc_mtx)
193 
194 /*
195  *  hifn_command_t
196  *
197  *  This is the control structure used to pass commands to hifn_encrypt().
198  *
199  *  flags
200  *  -----
201  *  Flags is the bitwise "or" values for command configuration.  A single
202  *  encrypt direction needs to be set:
203  *
204  *	HIFN_ENCODE or HIFN_DECODE
205  *
206  *  To use cryptography, a single crypto algorithm must be included:
207  *
208  *	HIFN_CRYPT_3DES or HIFN_CRYPT_DES
209  *
210  *  To use authentication is used, a single MAC algorithm must be included:
211  *
212  *	HIFN_MAC_MD5 or HIFN_MAC_SHA1
213  *
214  *  By default MD5 uses a 16 byte hash and SHA-1 uses a 20 byte hash.
215  *  If the value below is set, hash values are truncated or assumed
216  *  truncated to 12 bytes:
217  *
218  *	HIFN_MAC_TRUNC
219  *
220  *  Keys for encryption and authentication can be sent as part of a command,
221  *  or the last key value used with a particular session can be retrieved
222  *  and used again if either of these flags are not specified.
223  *
224  *	HIFN_CRYPT_NEW_KEY, HIFN_MAC_NEW_KEY
225  *
226  *  session_num
227  *  -----------
228  *  A number between 0 and 2048 (for DRAM models) or a number between
229  *  0 and 768 (for SRAM models).  Those who don't want to use session
230  *  numbers should leave value at zero and send a new crypt key and/or
231  *  new MAC key on every command.  If you use session numbers and
232  *  don't send a key with a command, the last key sent for that same
233  *  session number will be used.
234  *
235  *  Warning:  Using session numbers and multiboard at the same time
236  *            is currently broken.
237  *
238  *  mbuf
239  *  ----
240  *  Either fill in the mbuf pointer and npa=0 or
241  *	 fill packp[] and packl[] and set npa to > 0
242  *
243  *  mac_header_skip
244  *  ---------------
245  *  The number of bytes of the source_buf that are skipped over before
246  *  authentication begins.  This must be a number between 0 and 2^16-1
247  *  and can be used by IPsec implementers to skip over IP headers.
248  *  *** Value ignored if authentication not used ***
249  *
250  *  crypt_header_skip
251  *  -----------------
252  *  The number of bytes of the source_buf that are skipped over before
253  *  the cryptographic operation begins.  This must be a number between 0
254  *  and 2^16-1.  For IPsec, this number will always be 8 bytes larger
255  *  than the auth_header_skip (to skip over the ESP header).
256  *  *** Value ignored if cryptography not used ***
257  *
258  */
259 struct hifn_operand {
260 	union {
261 		struct mbuf *m;
262 		struct uio *io;
263 	} u;
264 	bus_dmamap_t	map;
265 	bus_size_t	mapsize;
266 	int		nsegs;
267 	bus_dma_segment_t segs[MAX_SCATTER];
268 };
269 struct hifn_command {
270 	struct hifn_session *session;
271 	u_int16_t base_masks, cry_masks, mac_masks;
272 	u_int8_t iv[HIFN_MAX_IV_LENGTH], *ck, mac[HIFN_MAC_KEY_LENGTH];
273 	int cklen;
274 	int sloplen, slopidx;
275 
276 	struct hifn_operand src;
277 	struct hifn_operand dst;
278 
279 	struct hifn_softc *softc;
280 	struct cryptop *crp;
281 	struct cryptodesc *enccrd, *maccrd;
282 };
283 
284 #define	src_m		src.u.m
285 #define	src_io		src.u.io
286 #define	src_map		src.map
287 #define	src_mapsize	src.mapsize
288 #define	src_segs	src.segs
289 #define	src_nsegs	src.nsegs
290 
291 #define	dst_m		dst.u.m
292 #define	dst_io		dst.u.io
293 #define	dst_map		dst.map
294 #define	dst_mapsize	dst.mapsize
295 #define	dst_segs	dst.segs
296 #define	dst_nsegs	dst.nsegs
297 
298 /*
299  *  Return values for hifn_crypto()
300  */
301 #define HIFN_CRYPTO_SUCCESS	0
302 #define HIFN_CRYPTO_BAD_INPUT	(-1)
303 #define HIFN_CRYPTO_RINGS_FULL	(-2)
304 
305 /**************************************************************************
306  *
307  *  Function:  hifn_crypto
308  *
309  *  Purpose:   Called by external drivers to begin an encryption on the
310  *             HIFN board.
311  *
312  *  Blocking/Non-blocking Issues
313  *  ============================
314  *  The driver cannot block in hifn_crypto (no calls to tsleep) currently.
315  *  hifn_crypto() returns HIFN_CRYPTO_RINGS_FULL if there is not enough
316  *  room in any of the rings for the request to proceed.
317  *
318  *  Return Values
319  *  =============
320  *  0 for success, negative values on error
321  *
322  *  Defines for negative error codes are:
323  *
324  *    HIFN_CRYPTO_BAD_INPUT  :  The passed in command had invalid settings.
325  *    HIFN_CRYPTO_RINGS_FULL :  All DMA rings were full and non-blocking
326  *                              behaviour was requested.
327  *
328  *************************************************************************/
329 #endif /* _KERNEL */
330 
331 struct hifn_stats {
332 	u_int64_t hst_ibytes;
333 	u_int64_t hst_obytes;
334 	u_int32_t hst_ipackets;
335 	u_int32_t hst_opackets;
336 	u_int32_t hst_invalid;
337 	u_int32_t hst_nomem;		/* malloc or one of hst_nomem_* */
338 	u_int32_t hst_abort;
339 	u_int32_t hst_noirq;		/* IRQ for no reason */
340 	u_int32_t hst_totbatch;		/* ops submitted w/o interrupt */
341 	u_int32_t hst_maxbatch;		/* max ops submitted together */
342 	u_int32_t hst_unaligned;	/* unaligned src caused copy */
343 	/*
344 	 * The following divides hst_nomem into more specific buckets.
345 	 */
346 	u_int32_t hst_nomem_map;	/* bus_dmamap_create failed */
347 	u_int32_t hst_nomem_load;	/* bus_dmamap_load_* failed */
348 	u_int32_t hst_nomem_mbuf;	/* MGET* failed */
349 	u_int32_t hst_nomem_mcl;	/* MCLGET* failed */
350 	u_int32_t hst_nomem_cr;		/* out of command/result descriptor */
351 	u_int32_t hst_nomem_sd;		/* out of src/dst descriptors */
352 };
353 
354 #endif /* __HIFN7751VAR_H__ */
355