20 #include <linux/kernel.h>
21 #include <linux/module.h>
25 #include <linux/pci.h>
26 #include <linux/slab.h>
39 #include <asm/kmap_types.h>
44 #define dprintk(f, a...) printk(f, ##a)
46 #define dprintk(f, a...) do {} while (0)
49 static char hifn_pll_ref[
sizeof(
"extNNN")] =
"ext";
52 "PLL reference clock (pci[freq] or ext[freq], default ext)");
56 #define ACRYPTO_OP_DECRYPT 0
57 #define ACRYPTO_OP_ENCRYPT 1
58 #define ACRYPTO_OP_HMAC 2
59 #define ACRYPTO_OP_RNG 3
61 #define ACRYPTO_MODE_ECB 0
62 #define ACRYPTO_MODE_CBC 1
63 #define ACRYPTO_MODE_CFB 2
64 #define ACRYPTO_MODE_OFB 3
66 #define ACRYPTO_TYPE_AES_128 0
67 #define ACRYPTO_TYPE_AES_192 1
68 #define ACRYPTO_TYPE_AES_256 2
69 #define ACRYPTO_TYPE_3DES 3
70 #define ACRYPTO_TYPE_DES 4
72 #define PCI_VENDOR_ID_HIFN 0x13A3
73 #define PCI_DEVICE_ID_HIFN_7955 0x0020
74 #define PCI_DEVICE_ID_HIFN_7956 0x001d
78 #define HIFN_BAR0_SIZE 0x1000
79 #define HIFN_BAR1_SIZE 0x2000
80 #define HIFN_BAR2_SIZE 0x8000
84 #define HIFN_DMA_CRA 0x0C
85 #define HIFN_DMA_SDRA 0x1C
86 #define HIFN_DMA_RRA 0x2C
87 #define HIFN_DMA_DDRA 0x3C
88 #define HIFN_DMA_STCTL 0x40
89 #define HIFN_DMA_INTREN 0x44
90 #define HIFN_DMA_CFG1 0x48
91 #define HIFN_DMA_CFG2 0x6C
92 #define HIFN_CHIP_ID 0x98
97 #define HIFN_0_PUDATA 0x00
98 #define HIFN_0_PUCTRL 0x04
99 #define HIFN_0_PUISR 0x08
100 #define HIFN_0_PUCNFG 0x0c
101 #define HIFN_0_PUIER 0x10
102 #define HIFN_0_PUSTAT 0x14
103 #define HIFN_0_FIFOSTAT 0x18
104 #define HIFN_0_FIFOCNFG 0x1c
105 #define HIFN_0_SPACESIZE 0x20
108 #define HIFN_PUCTRL_CLRSRCFIFO 0x0010
109 #define HIFN_PUCTRL_STOP 0x0008
110 #define HIFN_PUCTRL_LOCKRAM 0x0004
111 #define HIFN_PUCTRL_DMAENA 0x0002
112 #define HIFN_PUCTRL_RESET 0x0001
115 #define HIFN_PUISR_CMDINVAL 0x8000
116 #define HIFN_PUISR_DATAERR 0x4000
117 #define HIFN_PUISR_SRCFIFO 0x2000
118 #define HIFN_PUISR_DSTFIFO 0x1000
119 #define HIFN_PUISR_DSTOVER 0x0200
120 #define HIFN_PUISR_SRCCMD 0x0080
121 #define HIFN_PUISR_SRCCTX 0x0040
122 #define HIFN_PUISR_SRCDATA 0x0020
123 #define HIFN_PUISR_DSTDATA 0x0010
124 #define HIFN_PUISR_DSTRESULT 0x0004
127 #define HIFN_PUCNFG_DRAMMASK 0xe000
128 #define HIFN_PUCNFG_DSZ_256K 0x0000
129 #define HIFN_PUCNFG_DSZ_512K 0x2000
130 #define HIFN_PUCNFG_DSZ_1M 0x4000
131 #define HIFN_PUCNFG_DSZ_2M 0x6000
132 #define HIFN_PUCNFG_DSZ_4M 0x8000
133 #define HIFN_PUCNFG_DSZ_8M 0xa000
134 #define HIFN_PUNCFG_DSZ_16M 0xc000
135 #define HIFN_PUCNFG_DSZ_32M 0xe000
136 #define HIFN_PUCNFG_DRAMREFRESH 0x1800
137 #define HIFN_PUCNFG_DRFR_512 0x0000
138 #define HIFN_PUCNFG_DRFR_256 0x0800
139 #define HIFN_PUCNFG_DRFR_128 0x1000
140 #define HIFN_PUCNFG_TCALLPHASES 0x0200
141 #define HIFN_PUCNFG_TCDRVTOTEM 0x0100
142 #define HIFN_PUCNFG_BIGENDIAN 0x0080
143 #define HIFN_PUCNFG_BUS32 0x0040
144 #define HIFN_PUCNFG_BUS16 0x0000
145 #define HIFN_PUCNFG_CHIPID 0x0020
146 #define HIFN_PUCNFG_DRAM 0x0010
147 #define HIFN_PUCNFG_SRAM 0x0000
148 #define HIFN_PUCNFG_COMPSING 0x0004
149 #define HIFN_PUCNFG_ENCCNFG 0x0002
152 #define HIFN_PUIER_CMDINVAL 0x8000
153 #define HIFN_PUIER_DATAERR 0x4000
154 #define HIFN_PUIER_SRCFIFO 0x2000
155 #define HIFN_PUIER_DSTFIFO 0x1000
156 #define HIFN_PUIER_DSTOVER 0x0200
157 #define HIFN_PUIER_SRCCMD 0x0080
158 #define HIFN_PUIER_SRCCTX 0x0040
159 #define HIFN_PUIER_SRCDATA 0x0020
160 #define HIFN_PUIER_DSTDATA 0x0010
161 #define HIFN_PUIER_DSTRESULT 0x0004
164 #define HIFN_PUSTAT_CMDINVAL 0x8000
165 #define HIFN_PUSTAT_DATAERR 0x4000
166 #define HIFN_PUSTAT_SRCFIFO 0x2000
167 #define HIFN_PUSTAT_DSTFIFO 0x1000
168 #define HIFN_PUSTAT_DSTOVER 0x0200
169 #define HIFN_PUSTAT_SRCCMD 0x0080
170 #define HIFN_PUSTAT_SRCCTX 0x0040
171 #define HIFN_PUSTAT_SRCDATA 0x0020
172 #define HIFN_PUSTAT_DSTDATA 0x0010
173 #define HIFN_PUSTAT_DSTRESULT 0x0004
174 #define HIFN_PUSTAT_CHIPREV 0x00ff
175 #define HIFN_PUSTAT_CHIPENA 0xff00
176 #define HIFN_PUSTAT_ENA_2 0x1100
177 #define HIFN_PUSTAT_ENA_1 0x1000
178 #define HIFN_PUSTAT_ENA_0 0x3000
179 #define HIFN_PUSTAT_REV_2 0x0020
180 #define HIFN_PUSTAT_REV_3 0x0030
183 #define HIFN_FIFOSTAT_SRC 0x7f00
184 #define HIFN_FIFOSTAT_DST 0x007f
187 #define HIFN_FIFOCNFG_THRESHOLD 0x0400
192 #define HIFN_1_DMA_CRAR 0x0c
193 #define HIFN_1_DMA_SRAR 0x1c
194 #define HIFN_1_DMA_RRAR 0x2c
195 #define HIFN_1_DMA_DRAR 0x3c
196 #define HIFN_1_DMA_CSR 0x40
197 #define HIFN_1_DMA_IER 0x44
198 #define HIFN_1_DMA_CNFG 0x48
199 #define HIFN_1_PLL 0x4c
200 #define HIFN_1_7811_RNGENA 0x60
201 #define HIFN_1_7811_RNGCFG 0x64
202 #define HIFN_1_7811_RNGDAT 0x68
203 #define HIFN_1_7811_RNGSTS 0x6c
204 #define HIFN_1_7811_MIPSRST 0x94
205 #define HIFN_1_REVID 0x98
206 #define HIFN_1_UNLOCK_SECRET1 0xf4
207 #define HIFN_1_UNLOCK_SECRET2 0xfc
208 #define HIFN_1_PUB_RESET 0x204
209 #define HIFN_1_PUB_BASE 0x300
210 #define HIFN_1_PUB_OPLEN 0x304
211 #define HIFN_1_PUB_OP 0x308
212 #define HIFN_1_PUB_STATUS 0x30c
213 #define HIFN_1_PUB_IEN 0x310
214 #define HIFN_1_RNG_CONFIG 0x314
215 #define HIFN_1_RNG_DATA 0x318
216 #define HIFN_1_PUB_MEM 0x400
217 #define HIFN_1_PUB_MEMEND 0xbff
220 #define HIFN_DMACSR_D_CTRLMASK 0xc0000000
221 #define HIFN_DMACSR_D_CTRL_NOP 0x00000000
222 #define HIFN_DMACSR_D_CTRL_DIS 0x40000000
223 #define HIFN_DMACSR_D_CTRL_ENA 0x80000000
224 #define HIFN_DMACSR_D_ABORT 0x20000000
225 #define HIFN_DMACSR_D_DONE 0x10000000
226 #define HIFN_DMACSR_D_LAST 0x08000000
227 #define HIFN_DMACSR_D_WAIT 0x04000000
228 #define HIFN_DMACSR_D_OVER 0x02000000
229 #define HIFN_DMACSR_R_CTRL 0x00c00000
230 #define HIFN_DMACSR_R_CTRL_NOP 0x00000000
231 #define HIFN_DMACSR_R_CTRL_DIS 0x00400000
232 #define HIFN_DMACSR_R_CTRL_ENA 0x00800000
233 #define HIFN_DMACSR_R_ABORT 0x00200000
234 #define HIFN_DMACSR_R_DONE 0x00100000
235 #define HIFN_DMACSR_R_LAST 0x00080000
236 #define HIFN_DMACSR_R_WAIT 0x00040000
237 #define HIFN_DMACSR_R_OVER 0x00020000
238 #define HIFN_DMACSR_S_CTRL 0x0000c000
239 #define HIFN_DMACSR_S_CTRL_NOP 0x00000000
240 #define HIFN_DMACSR_S_CTRL_DIS 0x00004000
241 #define HIFN_DMACSR_S_CTRL_ENA 0x00008000
242 #define HIFN_DMACSR_S_ABORT 0x00002000
243 #define HIFN_DMACSR_S_DONE 0x00001000
244 #define HIFN_DMACSR_S_LAST 0x00000800
245 #define HIFN_DMACSR_S_WAIT 0x00000400
246 #define HIFN_DMACSR_ILLW 0x00000200
247 #define HIFN_DMACSR_ILLR 0x00000100
248 #define HIFN_DMACSR_C_CTRL 0x000000c0
249 #define HIFN_DMACSR_C_CTRL_NOP 0x00000000
250 #define HIFN_DMACSR_C_CTRL_DIS 0x00000040
251 #define HIFN_DMACSR_C_CTRL_ENA 0x00000080
252 #define HIFN_DMACSR_C_ABORT 0x00000020
253 #define HIFN_DMACSR_C_DONE 0x00000010
254 #define HIFN_DMACSR_C_LAST 0x00000008
255 #define HIFN_DMACSR_C_WAIT 0x00000004
256 #define HIFN_DMACSR_PUBDONE 0x00000002
257 #define HIFN_DMACSR_ENGINE 0x00000001
260 #define HIFN_DMAIER_D_ABORT 0x20000000
261 #define HIFN_DMAIER_D_DONE 0x10000000
262 #define HIFN_DMAIER_D_LAST 0x08000000
263 #define HIFN_DMAIER_D_WAIT 0x04000000
264 #define HIFN_DMAIER_D_OVER 0x02000000
265 #define HIFN_DMAIER_R_ABORT 0x00200000
266 #define HIFN_DMAIER_R_DONE 0x00100000
267 #define HIFN_DMAIER_R_LAST 0x00080000
268 #define HIFN_DMAIER_R_WAIT 0x00040000
269 #define HIFN_DMAIER_R_OVER 0x00020000
270 #define HIFN_DMAIER_S_ABORT 0x00002000
271 #define HIFN_DMAIER_S_DONE 0x00001000
272 #define HIFN_DMAIER_S_LAST 0x00000800
273 #define HIFN_DMAIER_S_WAIT 0x00000400
274 #define HIFN_DMAIER_ILLW 0x00000200
275 #define HIFN_DMAIER_ILLR 0x00000100
276 #define HIFN_DMAIER_C_ABORT 0x00000020
277 #define HIFN_DMAIER_C_DONE 0x00000010
278 #define HIFN_DMAIER_C_LAST 0x00000008
279 #define HIFN_DMAIER_C_WAIT 0x00000004
280 #define HIFN_DMAIER_PUBDONE 0x00000002
281 #define HIFN_DMAIER_ENGINE 0x00000001
284 #define HIFN_DMACNFG_BIGENDIAN 0x10000000
285 #define HIFN_DMACNFG_POLLFREQ 0x00ff0000
286 #define HIFN_DMACNFG_UNLOCK 0x00000800
287 #define HIFN_DMACNFG_POLLINVAL 0x00000700
288 #define HIFN_DMACNFG_LAST 0x00000010
289 #define HIFN_DMACNFG_MODE 0x00000004
290 #define HIFN_DMACNFG_DMARESET 0x00000002
291 #define HIFN_DMACNFG_MSTRESET 0x00000001
294 #define HIFN_PLL_REF_CLK_HBI 0x00000000
295 #define HIFN_PLL_REF_CLK_PLL 0x00000001
296 #define HIFN_PLL_BP 0x00000002
297 #define HIFN_PLL_PK_CLK_HBI 0x00000000
298 #define HIFN_PLL_PK_CLK_PLL 0x00000008
299 #define HIFN_PLL_PE_CLK_HBI 0x00000000
300 #define HIFN_PLL_PE_CLK_PLL 0x00000010
301 #define HIFN_PLL_RESERVED_1 0x00000400
302 #define HIFN_PLL_ND_SHIFT 11
303 #define HIFN_PLL_ND_MULT_2 0x00000000
304 #define HIFN_PLL_ND_MULT_4 0x00000800
305 #define HIFN_PLL_ND_MULT_6 0x00001000
306 #define HIFN_PLL_ND_MULT_8 0x00001800
307 #define HIFN_PLL_ND_MULT_10 0x00002000
308 #define HIFN_PLL_ND_MULT_12 0x00002800
309 #define HIFN_PLL_IS_1_8 0x00000000
310 #define HIFN_PLL_IS_9_12 0x00010000
312 #define HIFN_PLL_FCK_MAX 266
315 #define HIFN_PUBRST_RESET 0x00000001
318 #define HIFN_PUBBASE_ADDR 0x00003fff
321 #define HIFN_PUBOPLEN_MOD_M 0x0000007f
322 #define HIFN_PUBOPLEN_MOD_S 0
323 #define HIFN_PUBOPLEN_EXP_M 0x0003ff80
324 #define HIFN_PUBOPLEN_EXP_S 7
325 #define HIFN_PUBOPLEN_RED_M 0x003c0000
326 #define HIFN_PUBOPLEN_RED_S 18
329 #define HIFN_PUBOP_AOFFSET_M 0x0000007f
330 #define HIFN_PUBOP_AOFFSET_S 0
331 #define HIFN_PUBOP_BOFFSET_M 0x00000f80
332 #define HIFN_PUBOP_BOFFSET_S 7
333 #define HIFN_PUBOP_MOFFSET_M 0x0003f000
334 #define HIFN_PUBOP_MOFFSET_S 12
335 #define HIFN_PUBOP_OP_MASK 0x003c0000
336 #define HIFN_PUBOP_OP_NOP 0x00000000
337 #define HIFN_PUBOP_OP_ADD 0x00040000
338 #define HIFN_PUBOP_OP_ADDC 0x00080000
339 #define HIFN_PUBOP_OP_SUB 0x000c0000
340 #define HIFN_PUBOP_OP_SUBC 0x00100000
341 #define HIFN_PUBOP_OP_MODADD 0x00140000
342 #define HIFN_PUBOP_OP_MODSUB 0x00180000
343 #define HIFN_PUBOP_OP_INCA 0x001c0000
344 #define HIFN_PUBOP_OP_DECA 0x00200000
345 #define HIFN_PUBOP_OP_MULT 0x00240000
346 #define HIFN_PUBOP_OP_MODMULT 0x00280000
347 #define HIFN_PUBOP_OP_MODRED 0x002c0000
348 #define HIFN_PUBOP_OP_MODEXP 0x00300000
351 #define HIFN_PUBSTS_DONE 0x00000001
352 #define HIFN_PUBSTS_CARRY 0x00000002
355 #define HIFN_PUBIEN_DONE 0x00000001
358 #define HIFN_RNGCFG_ENA 0x00000001
360 #define HIFN_NAMESIZE 32
361 #define HIFN_MAX_RESULT_ORDER 5
363 #define HIFN_D_CMD_RSIZE 24*1
364 #define HIFN_D_SRC_RSIZE 80*1
365 #define HIFN_D_DST_RSIZE 80*1
366 #define HIFN_D_RES_RSIZE 24*1
368 #define HIFN_D_DST_DALIGN 4
370 #define HIFN_QUEUE_LENGTH (HIFN_D_CMD_RSIZE - 1)
372 #define AES_MIN_KEY_SIZE 16
373 #define AES_MAX_KEY_SIZE 32
375 #define HIFN_DES_KEY_LENGTH 8
376 #define HIFN_3DES_KEY_LENGTH 24
377 #define HIFN_MAX_CRYPT_KEY_LENGTH AES_MAX_KEY_SIZE
378 #define HIFN_IV_LENGTH 8
379 #define HIFN_AES_IV_LENGTH 16
380 #define HIFN_MAX_IV_LENGTH HIFN_AES_IV_LENGTH
382 #define HIFN_MAC_KEY_LENGTH 64
383 #define HIFN_MD5_LENGTH 16
384 #define HIFN_SHA1_LENGTH 20
385 #define HIFN_MAC_TRUNC_LENGTH 12
387 #define HIFN_MAX_COMMAND (8 + 8 + 8 + 64 + 260)
388 #define HIFN_MAX_RESULT (8 + 4 + 4 + 20 + 4)
389 #define HIFN_USED_RESULT 12
415 #define HIFN_FLAG_CMD_BUSY (1<<0)
416 #define HIFN_FLAG_SRC_BUSY (1<<1)
417 #define HIFN_FLAG_DST_BUSY (1<<2)
418 #define HIFN_FLAG_RES_BUSY (1<<3)
419 #define HIFN_FLAG_OLD_KEY (1<<4)
421 #define HIFN_DEFAULT_ACTIVE_NUM 5
457 #ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
458 unsigned int rng_wait_time;
464 #define HIFN_D_LENGTH 0x0000ffff
465 #define HIFN_D_NOINVALID 0x01000000
466 #define HIFN_D_MASKDONEIRQ 0x02000000
467 #define HIFN_D_DESTOVER 0x04000000
468 #define HIFN_D_OVER 0x08000000
469 #define HIFN_D_LAST 0x20000000
470 #define HIFN_D_JUMP 0x40000000
471 #define HIFN_D_VALID 0x80000000
481 #define HIFN_BASE_CMD_COMP 0x0100
482 #define HIFN_BASE_CMD_PAD 0x0200
483 #define HIFN_BASE_CMD_MAC 0x0400
484 #define HIFN_BASE_CMD_CRYPT 0x0800
485 #define HIFN_BASE_CMD_DECODE 0x2000
486 #define HIFN_BASE_CMD_SRCLEN_M 0xc000
487 #define HIFN_BASE_CMD_SRCLEN_S 14
488 #define HIFN_BASE_CMD_DSTLEN_M 0x3000
489 #define HIFN_BASE_CMD_DSTLEN_S 12
490 #define HIFN_BASE_CMD_LENMASK_HI 0x30000
491 #define HIFN_BASE_CMD_LENMASK_LO 0x0ffff
504 #define HIFN_CRYPT_CMD_ALG_MASK 0x0003
505 #define HIFN_CRYPT_CMD_ALG_DES 0x0000
506 #define HIFN_CRYPT_CMD_ALG_3DES 0x0001
507 #define HIFN_CRYPT_CMD_ALG_RC4 0x0002
508 #define HIFN_CRYPT_CMD_ALG_AES 0x0003
509 #define HIFN_CRYPT_CMD_MODE_MASK 0x0018
510 #define HIFN_CRYPT_CMD_MODE_ECB 0x0000
511 #define HIFN_CRYPT_CMD_MODE_CBC 0x0008
512 #define HIFN_CRYPT_CMD_MODE_CFB 0x0010
513 #define HIFN_CRYPT_CMD_MODE_OFB 0x0018
514 #define HIFN_CRYPT_CMD_CLR_CTX 0x0040
515 #define HIFN_CRYPT_CMD_KSZ_MASK 0x0600
516 #define HIFN_CRYPT_CMD_KSZ_128 0x0000
517 #define HIFN_CRYPT_CMD_KSZ_192 0x0200
518 #define HIFN_CRYPT_CMD_KSZ_256 0x0400
519 #define HIFN_CRYPT_CMD_NEW_KEY 0x0800
520 #define HIFN_CRYPT_CMD_NEW_IV 0x1000
521 #define HIFN_CRYPT_CMD_SRCLEN_M 0xc000
522 #define HIFN_CRYPT_CMD_SRCLEN_S 14
535 #define HIFN_MAC_CMD_ALG_MASK 0x0001
536 #define HIFN_MAC_CMD_ALG_SHA1 0x0000
537 #define HIFN_MAC_CMD_ALG_MD5 0x0001
538 #define HIFN_MAC_CMD_MODE_MASK 0x000c
539 #define HIFN_MAC_CMD_MODE_HMAC 0x0000
540 #define HIFN_MAC_CMD_MODE_SSL_MAC 0x0004
541 #define HIFN_MAC_CMD_MODE_HASH 0x0008
542 #define HIFN_MAC_CMD_MODE_FULL 0x0004
543 #define HIFN_MAC_CMD_TRUNC 0x0010
544 #define HIFN_MAC_CMD_RESULT 0x0020
545 #define HIFN_MAC_CMD_APPEND 0x0040
546 #define HIFN_MAC_CMD_SRCLEN_M 0xc000
547 #define HIFN_MAC_CMD_SRCLEN_S 14
553 #define HIFN_MAC_CMD_POS_IPSEC 0x0200
554 #define HIFN_MAC_CMD_NEW_KEY 0x0800
564 #define HIFN_COMP_CMD_SRCLEN_M 0xc000
565 #define HIFN_COMP_CMD_SRCLEN_S 14
566 #define HIFN_COMP_CMD_ONE 0x0100
567 #define HIFN_COMP_CMD_CLEARHIST 0x0010
568 #define HIFN_COMP_CMD_UPDATEHIST 0x0008
569 #define HIFN_COMP_CMD_LZS_STRIP0 0x0004
570 #define HIFN_COMP_CMD_MPPC_RESTART 0x0004
571 #define HIFN_COMP_CMD_ALG_MASK 0x0001
572 #define HIFN_COMP_CMD_ALG_MPPC 0x0001
573 #define HIFN_COMP_CMD_ALG_LZS 0x0000
583 #define HIFN_BASE_RES_DSTOVERRUN 0x0200
584 #define HIFN_BASE_RES_SRCLEN_M 0xc000
585 #define HIFN_BASE_RES_SRCLEN_S 14
586 #define HIFN_BASE_RES_DSTLEN_M 0x3000
587 #define HIFN_BASE_RES_DSTLEN_S 12
595 #define HIFN_COMP_RES_LCB_M 0xff00
596 #define HIFN_COMP_RES_LCB_S 8
597 #define HIFN_COMP_RES_RESTART 0x0004
598 #define HIFN_COMP_RES_ENDMARKER 0x0002
599 #define HIFN_COMP_RES_SRC_NOTZERO 0x0001
608 #define HIFN_MAC_RES_MISCOMPARE 0x0002
609 #define HIFN_MAC_RES_SRC_NOTZERO 0x0001
617 #define HIFN_CRYPT_RES_SRC_NOTZERO 0x0001
619 #ifndef HIFN_POLL_FREQUENCY
620 #define HIFN_POLL_FREQUENCY 0x1
623 #ifndef HIFN_POLL_SCALAR
624 #define HIFN_POLL_SCALAR 0x0
627 #define HIFN_MAX_SEGLEN 0xffff
628 #define HIFN_MAX_DMALEN 0x3ffff
637 #define ASYNC_SCATTERLIST_CACHE 16
639 #define ASYNC_FLAGS_MISALIGNED (1<<0)
663 #define crypto_alg_to_hifn(a) container_of(a, struct hifn_crypto_alg, alg)
698 for (i=10000; i > 0; --
i) {
707 dprintk(
"%s: Failed to reset PUC unit.\n", dev->
name);
710 static void hifn_reset_puc(
struct hifn_device *dev)
716 static void hifn_stop_device(
struct hifn_device *dev)
727 hifn_stop_device(dev);
759 for (i = 0; i <
cnt; i++) {
769 a = (v & 1) ^ (a << 1);
775 static struct pci2id {
783 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
784 0x00, 0x00, 0x00, 0x00, 0x00 }
789 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
790 0x00, 0x00, 0x00, 0x00, 0x00 }
794 #ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
795 static int hifn_rng_data_present(
struct hwrng *rng,
int wait)
801 nsec -= dev->rng_wait_time;
810 static int hifn_rng_data_read(
struct hwrng *rng,
u32 *
data)
828 dev->rng.data_present = hifn_rng_data_present,
829 dev->rng.data_read = hifn_rng_data_read,
830 dev->rng.priv = (
unsigned long)dev;
840 #define hifn_register_rng(dev) 0
841 #define hifn_unregister_rng(dev)
844 static int hifn_init_pubrng(
struct hifn_device *dev)
851 for (i=100; i > 0; --
i) {
859 dprintk(
"Chip %s: Failed to initialise public key engine.\n",
866 dprintk(
"Chip %s: Public key engine has been successfully "
867 "initialised.\n", dev->
name);
876 dprintk(
"Chip %s: RNG engine has been successfully initialised.\n",
879 #ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
887 static int hifn_enable_crypto(
struct hifn_device *dev)
894 if (pci2id[i].pci_vendor == dev->
pdev->vendor &&
895 pci2id[i].pci_prod == dev->
pdev->device) {
896 offtbl = pci2id[
i].card_id;
901 if (offtbl ==
NULL) {
917 for (i=0; i<12; ++
i) {
918 addr = hifn_next_signature(addr, offtbl[i] + 0x101);
975 unsigned int freq,
m;
980 if (
strncmp(hifn_pll_ref,
"ext", 3) == 0)
985 if (hifn_pll_ref[3] !=
'\0')
990 "override with hifn_pll_ref=%.3s<frequency>\n",
1026 static void hifn_init_registers(
struct hifn_device *dev)
1124 static int hifn_setup_crypto_command(
struct hifn_device *dev,
1125 u8 *buf,
unsigned dlen,
unsigned slen,
1140 cry_cmd->header_skip = 0;
1141 cry_cmd->reserved = 0;
1146 if (dma->
cmdu > 1) {
1152 memcpy(buf_pos, key, keylen);
1156 memcpy(buf_pos, iv, ivsize);
1165 static int hifn_setup_cmd_desc(
struct hifn_device *dev,
1192 buf_pos += hifn_setup_base_command(dev, buf_pos, nbytes,
1193 nbytes, mask, dev->
snum);
1203 switch (rctx->
mode) {
1220 switch (rctx->
type) {
1253 buf_pos += hifn_setup_crypto_command(dev, buf_pos,
1261 cmd_len = buf_pos -
buf;
1265 if (++dma->
cmdi == HIFN_D_CMD_RSIZE) {
1284 unsigned int offset,
unsigned int size,
int last)
1316 static void hifn_setup_res_desc(
struct hifn_device *dev)
1327 if (++dma->
resi == HIFN_D_RES_RSIZE) {
1341 static void hifn_setup_dst_desc(
struct hifn_device *dev,
struct page *page,
1342 unsigned offset,
unsigned size,
int last)
1370 static int hifn_setup_dma(
struct hifn_device *dev,
1373 unsigned int nbytes,
void *priv)
1376 struct page *spage, *dpage;
1377 unsigned int soff, doff;
1378 unsigned int n,
len;
1382 spage = sg_page(src);
1386 hifn_setup_src_desc(dev, spage, soff, len, n - len == 0);
1392 t = &rctx->
walk.cache[0];
1402 dpage = sg_page(dst);
1408 hifn_setup_dst_desc(dev, dpage, doff, len, n - len == 0);
1415 hifn_setup_cmd_desc(dev, ctx, rctx, priv, nbytes);
1416 hifn_setup_res_desc(dev);
1421 int num,
gfp_t gfp_flags)
1429 for (i=0; i<num; ++
i) {
1449 for (i=0; i<w->
num; ++
i) {
1460 static int ablkcipher_add(
unsigned int *drestp,
struct scatterlist *dst,
1461 unsigned int size,
unsigned int *nbytesp)
1463 unsigned int copy, drest = *drestp, nbytes = *nbytesp;
1466 if (drest < size || size > nbytes)
1476 dprintk(
"%s: copy: %u, size: %u, drest: %u, nbytes: %u.\n",
1477 __func__, copy, size, drest, nbytes);
1504 dprintk(
"\n%s: dlen: %u, doff: %u, offset: %u, nbytes: %u.\n",
1510 unsigned slen =
min(dst->
length - offset, nbytes);
1515 err = ablkcipher_add(&dlen, dst, slen, &nbytes);
1524 if (dlen < nbytes) {
1542 "slen: %u, offset: %u.\n",
1543 __func__, dlen, nbytes, slen, offset);
1545 "issue, generally you should not catch "
1546 "this path under any condition but who "
1547 "knows how did you use crypto code.\n"
1548 "Thank you.\n", __func__);
1555 err = ablkcipher_add(&dlen, dst, nbytes, &nbytes);
1581 unsigned int nbytes = req->
nbytes, idx = 0;
1582 int err = -
EINVAL, sg_num;
1588 rctx->
walk.flags = 0;
1620 err = hifn_setup_dma(dev, ctx, rctx, req->
src, req->
dst, req->
nbytes, req);
1627 spin_unlock_irqrestore(&dev->
lock, flags);
1632 spin_unlock_irqrestore(&dev->
lock, flags);
1635 printk(
"%s: iv: %p [%d], key: %p [%d], mode: %u, op: %u, "
1636 "type: %u, err: %d.\n",
1645 static int hifn_test(
struct hifn_device *dev,
int encdec,
u8 snum)
1651 u8 fips_aes_ecb_from_zero[16] = {
1652 0x66, 0xE9, 0x4B, 0xD4,
1653 0xEF, 0x8A, 0x2C, 0x3B,
1654 0x88, 0x4C, 0xFA, 0x59,
1655 0xCA, 0x34, 0x2B, 0x2E};
1658 memset(src, 0,
sizeof(src));
1668 rctx.
walk.cache[0].length = 0;
1672 err = hifn_setup_dma(dev, &ctx, &rctx, &
sg, &
sg,
sizeof(src),
NULL);
1680 for (n=0; n<
sizeof(
src); ++
n)
1684 for (n=0; n<
sizeof(fips_aes_ecb_from_zero); ++
n)
1685 dprintk(
"%02x ", fips_aes_ecb_from_zero[n]);
1688 if (!
memcmp(src, fips_aes_ecb_from_zero,
sizeof(fips_aes_ecb_from_zero))) {
1690 "passed.\n", dev->
name);
1699 static int hifn_start_device(
struct hifn_device *dev)
1704 hifn_reset_dma(dev, 1);
1706 err = hifn_enable_crypto(dev);
1710 hifn_reset_puc(dev);
1714 hifn_init_registers(dev);
1716 hifn_init_pubrng(dev);
1721 static int ablkcipher_get(
void *
saddr,
unsigned int *srestp,
unsigned int offset,
1722 struct scatterlist *dst,
unsigned int size,
unsigned int *nbytesp)
1724 unsigned int srest = *srestp, nbytes = *nbytesp, copy;
1728 if (srest < size || size > nbytes)
1744 dprintk(
"%s: copy: %u, size: %u, srest: %u, nbytes: %u.\n",
1745 __func__, copy, size, srest, nbytes);
1757 static inline void hifn_complete_sa(
struct hifn_device *dev,
int i)
1759 unsigned long flags;
1766 spin_unlock_irqrestore(&dev->
lock, flags);
1775 unsigned int nbytes = req->
nbytes;
1784 dprintk(
"\n%s: sg_page(t): %p, t->length: %u, "
1785 "sg_page(dst): %p, dst->length: %u, "
1787 __func__, sg_page(t), t->
length,
1788 sg_page(dst), dst->
length, nbytes);
1799 dst, nbytes, &nbytes);
1809 hifn_cipher_walk_exit(&rctx->
walk);
1812 req->
base.complete(&req->
base, error);
1815 static void hifn_clear_rings(
struct hifn_device *dev,
int error)
1820 dprintk(
"%s: ring cleanup 1: i: %d.%d.%d.%d, u: %d.%d.%d.%d, "
1821 "k: %d.%d.%d.%d.\n",
1835 hifn_process_ready(dev->
sa[i], error);
1836 hifn_complete_sa(dev, i);
1839 if (++i == HIFN_D_RES_RSIZE)
1859 if (++i == HIFN_D_CMD_RSIZE)
1875 dprintk(
"%s: ring cleanup 2: i: %d.%d.%d.%d, u: %d.%d.%d.%d, "
1876 "k: %d.%d.%d.%d.\n",
1887 unsigned long flags;
1919 spin_unlock_irqrestore(&dev->
lock, flags);
1922 if (++dev->
reset >= 5) {
1926 printk(
"%s: r: %08x, active: %d, started: %d, "
1927 "success: %lu: qlen: %u/%u, reset: %d.\n",
1932 printk(
"%s: res: ", __func__);
1936 hifn_process_ready(dev->
sa[i], -
ENODEV);
1937 hifn_complete_sa(dev, i);
1942 hifn_reset_dma(dev, 1);
1943 hifn_stop_device(dev);
1944 hifn_start_device(dev);
1948 tasklet_schedule(&dev->
tasklet);
1954 static irqreturn_t hifn_interrupt(
int irq,
void *data)
1962 dprintk(
"%s: 1 dmacsr: %08x, dmareg: %08x, res: %08x [%d], "
1963 "i: %d.%d.%d.%d, u: %d.%d.%d.%d.\n",
1968 if ((dmacsr & dev->
dmareg) == 0)
2001 hifn_reset_dma(dev, 1);
2003 hifn_init_registers(dev);
2012 tasklet_schedule(&dev->
tasklet);
2019 unsigned long flags;
2029 hifn_process_ready(dev->
sa[i],
2031 hifn_complete_sa(dev, i);
2038 spin_unlock_irqrestore(&dev->
lock, flags);
2040 hifn_process_ready(req, -
ENODEV);
2044 spin_unlock_irqrestore(&dev->
lock, flags);
2050 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
2084 err = hifn_setup_session(req);
2087 unsigned long flags;
2090 err = ablkcipher_enqueue_request(&dev->
queue, req);
2091 spin_unlock_irqrestore(&dev->
lock, flags);
2104 ivsize = crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(req));
2134 return hifn_handle_req(req);
2137 static int hifn_process_queue(
struct hifn_device *dev)
2141 unsigned long flags;
2146 backlog = crypto_get_backlog(&dev->
queue);
2148 spin_unlock_irqrestore(&dev->
lock, flags);
2158 err = hifn_handle_req(req);
2173 err = hifn_setup_crypto_req(req, op, type, mode);
2178 hifn_process_queue(dev);
2340 .name =
"cfb(des3_ede)", .drv_name =
"cfb-3des", .bsize = 8,
2344 .setkey = hifn_setkey,
2345 .encrypt = hifn_encrypt_3des_cfb,
2346 .decrypt = hifn_decrypt_3des_cfb,
2350 .name =
"ofb(des3_ede)", .drv_name =
"ofb-3des", .bsize = 8,
2354 .setkey = hifn_setkey,
2355 .encrypt = hifn_encrypt_3des_ofb,
2356 .decrypt = hifn_decrypt_3des_ofb,
2360 .name =
"cbc(des3_ede)", .drv_name =
"cbc-3des", .bsize = 8,
2365 .setkey = hifn_setkey,
2366 .encrypt = hifn_encrypt_3des_cbc,
2367 .decrypt = hifn_decrypt_3des_cbc,
2371 .name =
"ecb(des3_ede)", .drv_name =
"ecb-3des", .bsize = 8,
2375 .setkey = hifn_setkey,
2376 .encrypt = hifn_encrypt_3des_ecb,
2377 .decrypt = hifn_decrypt_3des_ecb,
2385 .name =
"cfb(des)", .drv_name =
"cfb-des", .bsize = 8,
2389 .setkey = hifn_setkey,
2390 .encrypt = hifn_encrypt_des_cfb,
2391 .decrypt = hifn_decrypt_des_cfb,
2395 .name =
"ofb(des)", .drv_name =
"ofb-des", .bsize = 8,
2399 .setkey = hifn_setkey,
2400 .encrypt = hifn_encrypt_des_ofb,
2401 .decrypt = hifn_decrypt_des_ofb,
2405 .name =
"cbc(des)", .drv_name =
"cbc-des", .bsize = 8,
2410 .setkey = hifn_setkey,
2411 .encrypt = hifn_encrypt_des_cbc,
2412 .decrypt = hifn_decrypt_des_cbc,
2416 .name =
"ecb(des)", .drv_name =
"ecb-des", .bsize = 8,
2420 .setkey = hifn_setkey,
2421 .encrypt = hifn_encrypt_des_ecb,
2422 .decrypt = hifn_decrypt_des_ecb,
2430 .name =
"ecb(aes)", .drv_name =
"ecb-aes", .bsize = 16,
2434 .setkey = hifn_setkey,
2435 .encrypt = hifn_encrypt_aes_ecb,
2436 .decrypt = hifn_decrypt_aes_ecb,
2440 .name =
"cbc(aes)", .drv_name =
"cbc-aes", .bsize = 16,
2445 .setkey = hifn_setkey,
2446 .encrypt = hifn_encrypt_aes_cbc,
2447 .decrypt = hifn_decrypt_aes_cbc,
2451 .name =
"cfb(aes)", .drv_name =
"cfb-aes", .bsize = 16,
2455 .setkey = hifn_setkey,
2456 .encrypt = hifn_encrypt_aes_cfb,
2457 .decrypt = hifn_decrypt_aes_cfb,
2461 .name =
"ofb(aes)", .drv_name =
"ofb-aes", .bsize = 16,
2465 .setkey = hifn_setkey,
2466 .encrypt = hifn_encrypt_aes_ofb,
2467 .decrypt = hifn_decrypt_aes_ofb,
2472 static int hifn_cra_init(
struct crypto_tfm *tfm)
2496 alg->
alg.cra_priority = 300;
2501 alg->
alg.cra_alignmask = 0;
2505 alg->
alg.cra_init = hifn_cra_init;
2520 static void hifn_unregister_alg(
struct hifn_device *dev)
2531 static int hifn_register_alg(
struct hifn_device *dev)
2535 for (i=0; i<
ARRAY_SIZE(hifn_alg_templates); ++
i) {
2536 err = hifn_alg_alloc(dev, &hifn_alg_templates[i]);
2544 hifn_unregister_alg(dev);
2548 static void hifn_tasklet_callback(
unsigned long data)
2558 hifn_clear_rings(dev, 0);
2561 hifn_process_queue(dev);
2577 goto err_out_disable_pci_device;
2579 snprintf(name,
sizeof(name),
"hifn%d",
2584 goto err_out_disable_pci_device;
2589 dprintk(
"%s: Broken hardware - I/O regions are too small.\n",
2592 goto err_out_free_regions;
2599 goto err_out_free_regions;
2607 for (i=0; i<3; ++
i) {
2616 goto err_out_unmap_bars;
2623 dprintk(
"Failed to allocate descriptor rings.\n");
2625 goto err_out_unmap_bars;
2635 pci_set_drvdata(pdev, dev);
2643 dprintk(
"Failed to request IRQ%d: err: %d.\n", dev->
irq, err);
2645 goto err_out_free_desc;
2648 err = hifn_start_device(dev);
2650 goto err_out_free_irq;
2652 err = hifn_test(dev, 1, 0);
2654 goto err_out_stop_device;
2658 goto err_out_stop_device;
2660 err = hifn_register_alg(dev);
2662 goto err_out_unregister_rng;
2667 dprintk(
"HIFN crypto accelerator card at %s has been "
2668 "successfully registered as %s.\n",
2669 pci_name(pdev), dev->
name);
2673 err_out_unregister_rng:
2675 err_out_stop_device:
2676 hifn_reset_dma(dev, 1);
2677 hifn_stop_device(dev);
2690 err_out_free_regions:
2693 err_out_disable_pci_device:
2704 dev = pci_get_drvdata(pdev);
2710 hifn_unregister_alg(dev);
2711 hifn_reset_dma(dev, 1);
2712 hifn_stop_device(dev);
2741 .id_table = hifn_pci_tbl,
2742 .probe = hifn_probe,
2746 static int __init hifn_init(
void)
2754 if (
strncmp(hifn_pll_ref,
"ext", 3) &&
2755 strncmp(hifn_pll_ref,
"pci", 3)) {
2757 "must be pci or ext");
2766 if (hifn_pll_ref[3] !=
'\0') {
2768 if (freq < 20 || freq > 100) {
2770 "frequency, must be in the range "
2776 err = pci_register_driver(&hifn_pci_driver);
2778 dprintk(
"Failed to register PCI driver for %s device.\n",
2779 hifn_pci_driver.
name);
2784 "has been successfully registered.\n");
2789 static void __exit hifn_fini(
void)
2794 "has been successfully unregistered.\n");