1 /* $NetBSD: hifn7751var.h,v 1.7 2005/12/11 12:22:49 christos 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@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 #define HIFN_AES_IV_LENGTH 16 64 #define HIFN_MAX_IV_LENGTH HIFN_AES_IV_LENGTH 65 66 /* 67 * Length values for authentication 68 */ 69 #define HIFN_MAC_KEY_LENGTH 64 70 #define HIFN_MD5_LENGTH 16 71 #define HIFN_SHA1_LENGTH 20 72 #define HIFN_MAC_TRUNC_LENGTH 12 73 74 #define MAX_SCATTER 64 75 76 /* 77 * Data structure to hold all 4 rings and any other ring related data. 78 */ 79 struct hifn_dma { 80 /* 81 * Descriptor rings. We add +1 to the size to accommodate the 82 * jump descriptor. 83 */ 84 struct hifn_desc cmdr[HIFN_D_CMD_RSIZE+1]; 85 struct hifn_desc srcr[HIFN_D_SRC_RSIZE+1]; 86 struct hifn_desc dstr[HIFN_D_DST_RSIZE+1]; 87 struct hifn_desc resr[HIFN_D_RES_RSIZE+1]; 88 89 struct hifn_command *hifn_commands[HIFN_D_RES_RSIZE]; 90 91 u_char command_bufs[HIFN_D_CMD_RSIZE][HIFN_MAX_COMMAND]; 92 u_char result_bufs[HIFN_D_CMD_RSIZE][HIFN_MAX_RESULT]; 93 u_int32_t slop[HIFN_D_CMD_RSIZE]; 94 95 u_int64_t test_src, test_dst; 96 97 /* 98 * Our current positions for insertion and removal from the descriptor 99 * rings. 100 */ 101 int cmdi, srci, dsti, resi; 102 volatile int cmdu, srcu, dstu, resu; 103 int cmdk, srck, dstk, resk; 104 }; 105 106 struct hifn_session { 107 int hs_state; 108 int hs_prev_op; /* XXX collapse into hs_flags? */ 109 u_int8_t hs_iv[HIFN_MAX_IV_LENGTH]; 110 }; 111 112 /* We use a state machine on sessions */ 113 #define HS_STATE_FREE 0 /* unused session entry */ 114 #define HS_STATE_USED 1 /* allocated, but key not on card */ 115 #define HS_STATE_KEY 2 /* allocated and key is on card */ 116 117 #define HIFN_RING_SYNC(sc, r, i, f) \ 118 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_dmamap, \ 119 offsetof(struct hifn_dma, r[i]), sizeof(struct hifn_desc), (f)) 120 121 #define HIFN_CMDR_SYNC(sc, i, f) HIFN_RING_SYNC((sc), cmdr, (i), (f)) 122 #define HIFN_RESR_SYNC(sc, i, f) HIFN_RING_SYNC((sc), resr, (i), (f)) 123 #define HIFN_SRCR_SYNC(sc, i, f) HIFN_RING_SYNC((sc), srcr, (i), (f)) 124 #define HIFN_DSTR_SYNC(sc, i, f) HIFN_RING_SYNC((sc), dstr, (i), (f)) 125 126 #define HIFN_CMD_SYNC(sc, i, f) \ 127 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_dmamap, \ 128 offsetof(struct hifn_dma, command_bufs[(i)][0]), \ 129 HIFN_MAX_COMMAND, (f)) 130 131 #define HIFN_RES_SYNC(sc, i, f) \ 132 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_dmamap, \ 133 offsetof(struct hifn_dma, result_bufs[(i)][0]), \ 134 HIFN_MAX_RESULT, (f)) 135 136 /* 137 * Holds data specific to a single HIFN board. 138 */ 139 struct hifn_softc { 140 struct device sc_dv; /* generic device */ 141 void * sc_ih; /* interrupt handler cookie */ 142 u_int32_t sc_dmaier; 143 u_int32_t sc_drammodel; /* 1=dram, 0=sram */ 144 145 bus_space_handle_t sc_sh0, sc_sh1; 146 bus_space_tag_t sc_st0, sc_st1; 147 bus_dma_tag_t sc_dmat; 148 149 struct hifn_dma *sc_dma; 150 bus_dmamap_t sc_dmamap; 151 bus_dma_segment_t sc_dmasegs[1]; 152 int sc_dmansegs; 153 int32_t sc_cid; 154 int sc_maxses; 155 int sc_ramsize; 156 int sc_flags; 157 #define HIFN_HAS_RNG 0x01 158 #define HIFN_HAS_PUBLIC 0x02 159 #define HIFN_HAS_AES 0x04 /* includes AES support */ 160 #define HIFN_IS_7811 0x08 /* Hifn 7811 part */ 161 #define HIFN_IS_7956 0x10 /* Hifn 7956/7955 don't have SDRAM */ 162 #define HIFN_NO_BURSTWRITE 0x20 163 #define HIFN_HAS_LEDS 0x40 164 165 #define HIFN_RNG_BITSPER 17 /* From Hifn 6500 paper: 0.06 bits 166 of entropy per RNG register bit 167 worst-case */ 168 169 struct callout sc_rngto; /* rng timeout */ 170 struct callout sc_tickto; /* led-clear timeout */ 171 rndsource_element_t sc_rnd_source; 172 int sc_rngfirst; 173 int sc_rnghz; 174 int sc_c_busy; /* command ring busy */ 175 int sc_s_busy; /* source data ring busy */ 176 int sc_d_busy; /* destination data ring busy */ 177 int sc_r_busy; /* result ring busy */ 178 int sc_active; /* for initial countdown */ 179 int sc_needwakeup; /* ops q'd wating on resources */ 180 int sc_curbatch; /* # ops submitted w/o int */ 181 int sc_suspended; 182 struct hifn_session sc_sessions[2048]; 183 pci_chipset_tag_t sc_pci_pc; 184 pcitag_t sc_pci_tag; 185 bus_size_t sc_waw_lastreg; 186 int sc_waw_lastgroup; 187 }; 188 189 #define WRITE_REG_0(sc,reg,val) hifn_write_4((sc), 0, (reg), (val)) 190 #define WRITE_REG_1(sc,reg,val) hifn_write_4((sc), 1, (reg), (val)) 191 #define READ_REG_0(sc,reg) hifn_read_4((sc), 0, (reg)) 192 #define READ_REG_1(sc,reg) hifn_read_4((sc), 1, (reg)) 193 194 #define SET_LED(sc,v) \ 195 if (sc->sc_flags & HIFN_HAS_LEDS) \ 196 WRITE_REG_1(sc, HIFN_1_7811_MIPSRST, \ 197 READ_REG_1(sc, HIFN_1_7811_MIPSRST) | (v)) 198 #define CLR_LED(sc,v) \ 199 if (sc->sc_flags & HIFN_HAS_LEDS) \ 200 WRITE_REG_1(sc, HIFN_1_7811_MIPSRST, \ 201 READ_REG_1(sc, HIFN_1_7811_MIPSRST) & ~(v)) 202 203 /* 204 * struct hifn_command 205 * 206 * This is the control structure used to pass commands to hifn_encrypt(). 207 * 208 * flags 209 * ----- 210 * Flags is the bitwise "or" values for command configuration. A single 211 * encrypt direction needs to be set: 212 * 213 * HIFN_ENCODE or HIFN_DECODE 214 * 215 * To use cryptography, a single crypto algorithm must be included: 216 * 217 * HIFN_CRYPT_3DES or HIFN_CRYPT_DES 218 * 219 * To use authentication, a single MAC algorithm must be included: 220 * 221 * HIFN_MAC_MD5 or HIFN_MAC_SHA1 222 * 223 * By default MD5 uses a 16 byte hash and SHA-1 uses a 20 byte hash. 224 * If the value below is set, hash values are truncated or assumed 225 * truncated to 12 bytes: 226 * 227 * HIFN_MAC_TRUNC 228 * 229 * Keys for encryption and authentication can be sent as part of a command, 230 * or the last key value used with a particular session can be retrieved 231 * and used again if either of these flags are not specified. 232 * 233 * HIFN_CRYPT_NEW_KEY, HIFN_MAC_NEW_KEY 234 * 235 * session_num 236 * ----------- 237 * A number between 0 and 2048 (for DRAM models) or a number between 238 * 0 and 768 (for SRAM models). Those who don't want to use session 239 * numbers should leave value at zero and send a new crypt key and/or 240 * new MAC key on every command. If you use session numbers and 241 * don't send a key with a command, the last key sent for that same 242 * session number will be used. 243 * 244 * Warning: Using session numbers and multiboard at the same time 245 * is currently broken. 246 * 247 * mbuf 248 * ---- 249 * Either fill in the mbuf pointer and npa=0 or 250 * fill packp[] and packl[] and set npa to > 0 251 * 252 * mac_header_skip 253 * --------------- 254 * The number of bytes of the source_buf that are skipped over before 255 * authentication begins. This must be a number between 0 and 2^16-1 256 * and can be used by IPsec implementers to skip over IP headers. 257 * *** Value ignored if authentication not used *** 258 * 259 * crypt_header_skip 260 * ----------------- 261 * The number of bytes of the source_buf that are skipped over before 262 * the cryptographic operation begins. This must be a number between 0 263 * and 2^16-1. For IPsec, this number will always be 8 bytes larger 264 * than the auth_header_skip (to skip over the ESP header). 265 * *** Value ignored if cryptography not used *** 266 * 267 */ 268 struct hifn_command { 269 u_int16_t session_num; 270 u_int16_t base_masks, cry_masks, mac_masks, comp_masks; 271 u_int8_t iv[HIFN_MAX_IV_LENGTH], *ck, mac[HIFN_MAC_KEY_LENGTH]; 272 int cklen; 273 int sloplen, slopidx; 274 275 union { 276 struct mbuf *src_m; 277 struct uio *src_io; 278 } srcu; 279 bus_dmamap_t src_map; 280 281 union { 282 struct mbuf *dst_m; 283 struct uio *dst_io; 284 } dstu; 285 bus_dmamap_t dst_map; 286 287 u_short mac_header_skip, mac_process_len; 288 u_short crypt_header_skip, crypt_process_len; 289 290 struct hifn_softc *softc; 291 struct cryptop *crp; 292 struct cryptodesc *enccrd, *maccrd, *compcrd; 293 294 }; 295 296 /* 297 * Return values for hifn_crypto() 298 */ 299 #define HIFN_CRYPTO_SUCCESS 0 300 #define HIFN_CRYPTO_BAD_INPUT (-1) 301 #define HIFN_CRYPTO_RINGS_FULL (-2) 302 303 304 /************************************************************************** 305 * 306 * Function: hifn_crypto 307 * 308 * Purpose: Called by external drivers to begin an encryption on the 309 * HIFN board. 310 * 311 * Blocking/Non-blocking Issues 312 * ============================ 313 * The driver cannot block in hifn_crypto (no calls to tsleep) currently. 314 * hifn_crypto() returns HIFN_CRYPTO_RINGS_FULL if there is not enough 315 * room in any of the rings for the request to proceed. 316 * 317 * Return Values 318 * ============= 319 * 0 for success, negative values on error 320 * 321 * Defines for negative error codes are: 322 * 323 * HIFN_CRYPTO_BAD_INPUT : The passed in command had invalid settings. 324 * HIFN_CRYPTO_RINGS_FULL : All DMA rings were full and non-blocking 325 * behaviour was requested. 326 * 327 *************************************************************************/ 328 329 /* 330 * Convert back and forth from 'sid' to 'card' and 'session' 331 */ 332 #define HIFN_CARD(sid) (((sid) & 0xf0000000) >> 28) 333 #define HIFN_SESSION(sid) ((sid) & 0x000007ff) 334 #define HIFN_SID(crd,ses) (((crd) << 28) | ((ses) & 0x7ff)) 335 336 #endif /* _KERNEL */ 337 338 struct hifn_stats { 339 u_int64_t hst_ibytes; 340 u_int64_t hst_obytes; 341 u_int32_t hst_ipackets; 342 u_int32_t hst_opackets; 343 u_int32_t hst_invalid; 344 u_int32_t hst_nomem; 345 u_int32_t hst_abort; 346 }; 347 348 #endif /* __DEV_PCI_HIFN7751VAR_H__ */ 349