2 * 2007+ Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/mod_devicetable.h>
24 #include <linux/interrupt.h>
25 #include <linux/pci.h>
26 #include <linux/slab.h>
27 #include <linux/delay.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/scatterlist.h>
31 #include <linux/highmem.h>
32 #include <linux/crypto.h>
33 #include <linux/hw_random.h>
34 #include <linux/ktime.h>
36 #include <crypto/algapi.h>
37 #include <crypto/des.h>
42 #define dprintk(f, a...) printk(f, ##a)
44 #define dprintk(f, a...) do {} while (0)
47 static char hifn_pll_ref
[sizeof("extNNN")] = "ext";
48 module_param_string(hifn_pll_ref
, hifn_pll_ref
, sizeof(hifn_pll_ref
), 0444);
49 MODULE_PARM_DESC(hifn_pll_ref
,
50 "PLL reference clock (pci[freq] or ext[freq], default ext)");
52 static atomic_t hifn_dev_number
;
54 #define ACRYPTO_OP_DECRYPT 0
55 #define ACRYPTO_OP_ENCRYPT 1
56 #define ACRYPTO_OP_HMAC 2
57 #define ACRYPTO_OP_RNG 3
59 #define ACRYPTO_MODE_ECB 0
60 #define ACRYPTO_MODE_CBC 1
61 #define ACRYPTO_MODE_CFB 2
62 #define ACRYPTO_MODE_OFB 3
64 #define ACRYPTO_TYPE_AES_128 0
65 #define ACRYPTO_TYPE_AES_192 1
66 #define ACRYPTO_TYPE_AES_256 2
67 #define ACRYPTO_TYPE_3DES 3
68 #define ACRYPTO_TYPE_DES 4
70 #define PCI_VENDOR_ID_HIFN 0x13A3
71 #define PCI_DEVICE_ID_HIFN_7955 0x0020
72 #define PCI_DEVICE_ID_HIFN_7956 0x001d
74 /* I/O region sizes */
76 #define HIFN_BAR0_SIZE 0x1000
77 #define HIFN_BAR1_SIZE 0x2000
78 #define HIFN_BAR2_SIZE 0x8000
82 #define HIFN_DMA_CRA 0x0C /* DMA Command Ring Address */
83 #define HIFN_DMA_SDRA 0x1C /* DMA Source Data Ring Address */
84 #define HIFN_DMA_RRA 0x2C /* DMA Result Ring Address */
85 #define HIFN_DMA_DDRA 0x3C /* DMA Destination Data Ring Address */
86 #define HIFN_DMA_STCTL 0x40 /* DMA Status and Control */
87 #define HIFN_DMA_INTREN 0x44 /* DMA Interrupt Enable */
88 #define HIFN_DMA_CFG1 0x48 /* DMA Configuration #1 */
89 #define HIFN_DMA_CFG2 0x6C /* DMA Configuration #2 */
90 #define HIFN_CHIP_ID 0x98 /* Chip ID */
93 * Processing Unit Registers (offset from BASEREG0)
95 #define HIFN_0_PUDATA 0x00 /* Processing Unit Data */
96 #define HIFN_0_PUCTRL 0x04 /* Processing Unit Control */
97 #define HIFN_0_PUISR 0x08 /* Processing Unit Interrupt Status */
98 #define HIFN_0_PUCNFG 0x0c /* Processing Unit Configuration */
99 #define HIFN_0_PUIER 0x10 /* Processing Unit Interrupt Enable */
100 #define HIFN_0_PUSTAT 0x14 /* Processing Unit Status/Chip ID */
101 #define HIFN_0_FIFOSTAT 0x18 /* FIFO Status */
102 #define HIFN_0_FIFOCNFG 0x1c /* FIFO Configuration */
103 #define HIFN_0_SPACESIZE 0x20 /* Register space size */
105 /* Processing Unit Control Register (HIFN_0_PUCTRL) */
106 #define HIFN_PUCTRL_CLRSRCFIFO 0x0010 /* clear source fifo */
107 #define HIFN_PUCTRL_STOP 0x0008 /* stop pu */
108 #define HIFN_PUCTRL_LOCKRAM 0x0004 /* lock ram */
109 #define HIFN_PUCTRL_DMAENA 0x0002 /* enable dma */
110 #define HIFN_PUCTRL_RESET 0x0001 /* Reset processing unit */
112 /* Processing Unit Interrupt Status Register (HIFN_0_PUISR) */
113 #define HIFN_PUISR_CMDINVAL 0x8000 /* Invalid command interrupt */
114 #define HIFN_PUISR_DATAERR 0x4000 /* Data error interrupt */
115 #define HIFN_PUISR_SRCFIFO 0x2000 /* Source FIFO ready interrupt */
116 #define HIFN_PUISR_DSTFIFO 0x1000 /* Destination FIFO ready interrupt */
117 #define HIFN_PUISR_DSTOVER 0x0200 /* Destination overrun interrupt */
118 #define HIFN_PUISR_SRCCMD 0x0080 /* Source command interrupt */
119 #define HIFN_PUISR_SRCCTX 0x0040 /* Source context interrupt */
120 #define HIFN_PUISR_SRCDATA 0x0020 /* Source data interrupt */
121 #define HIFN_PUISR_DSTDATA 0x0010 /* Destination data interrupt */
122 #define HIFN_PUISR_DSTRESULT 0x0004 /* Destination result interrupt */
124 /* Processing Unit Configuration Register (HIFN_0_PUCNFG) */
125 #define HIFN_PUCNFG_DRAMMASK 0xe000 /* DRAM size mask */
126 #define HIFN_PUCNFG_DSZ_256K 0x0000 /* 256k dram */
127 #define HIFN_PUCNFG_DSZ_512K 0x2000 /* 512k dram */
128 #define HIFN_PUCNFG_DSZ_1M 0x4000 /* 1m dram */
129 #define HIFN_PUCNFG_DSZ_2M 0x6000 /* 2m dram */
130 #define HIFN_PUCNFG_DSZ_4M 0x8000 /* 4m dram */
131 #define HIFN_PUCNFG_DSZ_8M 0xa000 /* 8m dram */
132 #define HIFN_PUNCFG_DSZ_16M 0xc000 /* 16m dram */
133 #define HIFN_PUCNFG_DSZ_32M 0xe000 /* 32m dram */
134 #define HIFN_PUCNFG_DRAMREFRESH 0x1800 /* DRAM refresh rate mask */
135 #define HIFN_PUCNFG_DRFR_512 0x0000 /* 512 divisor of ECLK */
136 #define HIFN_PUCNFG_DRFR_256 0x0800 /* 256 divisor of ECLK */
137 #define HIFN_PUCNFG_DRFR_128 0x1000 /* 128 divisor of ECLK */
138 #define HIFN_PUCNFG_TCALLPHASES 0x0200 /* your guess is as good as mine... */
139 #define HIFN_PUCNFG_TCDRVTOTEM 0x0100 /* your guess is as good as mine... */
140 #define HIFN_PUCNFG_BIGENDIAN 0x0080 /* DMA big endian mode */
141 #define HIFN_PUCNFG_BUS32 0x0040 /* Bus width 32bits */
142 #define HIFN_PUCNFG_BUS16 0x0000 /* Bus width 16 bits */
143 #define HIFN_PUCNFG_CHIPID 0x0020 /* Allow chipid from PUSTAT */
144 #define HIFN_PUCNFG_DRAM 0x0010 /* Context RAM is DRAM */
145 #define HIFN_PUCNFG_SRAM 0x0000 /* Context RAM is SRAM */
146 #define HIFN_PUCNFG_COMPSING 0x0004 /* Enable single compression context */
147 #define HIFN_PUCNFG_ENCCNFG 0x0002 /* Encryption configuration */
149 /* Processing Unit Interrupt Enable Register (HIFN_0_PUIER) */
150 #define HIFN_PUIER_CMDINVAL 0x8000 /* Invalid command interrupt */
151 #define HIFN_PUIER_DATAERR 0x4000 /* Data error interrupt */
152 #define HIFN_PUIER_SRCFIFO 0x2000 /* Source FIFO ready interrupt */
153 #define HIFN_PUIER_DSTFIFO 0x1000 /* Destination FIFO ready interrupt */
154 #define HIFN_PUIER_DSTOVER 0x0200 /* Destination overrun interrupt */
155 #define HIFN_PUIER_SRCCMD 0x0080 /* Source command interrupt */
156 #define HIFN_PUIER_SRCCTX 0x0040 /* Source context interrupt */
157 #define HIFN_PUIER_SRCDATA 0x0020 /* Source data interrupt */
158 #define HIFN_PUIER_DSTDATA 0x0010 /* Destination data interrupt */
159 #define HIFN_PUIER_DSTRESULT 0x0004 /* Destination result interrupt */
161 /* Processing Unit Status Register/Chip ID (HIFN_0_PUSTAT) */
162 #define HIFN_PUSTAT_CMDINVAL 0x8000 /* Invalid command interrupt */
163 #define HIFN_PUSTAT_DATAERR 0x4000 /* Data error interrupt */
164 #define HIFN_PUSTAT_SRCFIFO 0x2000 /* Source FIFO ready interrupt */
165 #define HIFN_PUSTAT_DSTFIFO 0x1000 /* Destination FIFO ready interrupt */
166 #define HIFN_PUSTAT_DSTOVER 0x0200 /* Destination overrun interrupt */
167 #define HIFN_PUSTAT_SRCCMD 0x0080 /* Source command interrupt */
168 #define HIFN_PUSTAT_SRCCTX 0x0040 /* Source context interrupt */
169 #define HIFN_PUSTAT_SRCDATA 0x0020 /* Source data interrupt */
170 #define HIFN_PUSTAT_DSTDATA 0x0010 /* Destination data interrupt */
171 #define HIFN_PUSTAT_DSTRESULT 0x0004 /* Destination result interrupt */
172 #define HIFN_PUSTAT_CHIPREV 0x00ff /* Chip revision mask */
173 #define HIFN_PUSTAT_CHIPENA 0xff00 /* Chip enabled mask */
174 #define HIFN_PUSTAT_ENA_2 0x1100 /* Level 2 enabled */
175 #define HIFN_PUSTAT_ENA_1 0x1000 /* Level 1 enabled */
176 #define HIFN_PUSTAT_ENA_0 0x3000 /* Level 0 enabled */
177 #define HIFN_PUSTAT_REV_2 0x0020 /* 7751 PT6/2 */
178 #define HIFN_PUSTAT_REV_3 0x0030 /* 7751 PT6/3 */
180 /* FIFO Status Register (HIFN_0_FIFOSTAT) */
181 #define HIFN_FIFOSTAT_SRC 0x7f00 /* Source FIFO available */
182 #define HIFN_FIFOSTAT_DST 0x007f /* Destination FIFO available */
184 /* FIFO Configuration Register (HIFN_0_FIFOCNFG) */
185 #define HIFN_FIFOCNFG_THRESHOLD 0x0400 /* must be written as 1 */
188 * DMA Interface Registers (offset from BASEREG1)
190 #define HIFN_1_DMA_CRAR 0x0c /* DMA Command Ring Address */
191 #define HIFN_1_DMA_SRAR 0x1c /* DMA Source Ring Address */
192 #define HIFN_1_DMA_RRAR 0x2c /* DMA Result Ring Address */
193 #define HIFN_1_DMA_DRAR 0x3c /* DMA Destination Ring Address */
194 #define HIFN_1_DMA_CSR 0x40 /* DMA Status and Control */
195 #define HIFN_1_DMA_IER 0x44 /* DMA Interrupt Enable */
196 #define HIFN_1_DMA_CNFG 0x48 /* DMA Configuration */
197 #define HIFN_1_PLL 0x4c /* 795x: PLL config */
198 #define HIFN_1_7811_RNGENA 0x60 /* 7811: rng enable */
199 #define HIFN_1_7811_RNGCFG 0x64 /* 7811: rng config */
200 #define HIFN_1_7811_RNGDAT 0x68 /* 7811: rng data */
201 #define HIFN_1_7811_RNGSTS 0x6c /* 7811: rng status */
202 #define HIFN_1_7811_MIPSRST 0x94 /* 7811: MIPS reset */
203 #define HIFN_1_REVID 0x98 /* Revision ID */
204 #define HIFN_1_UNLOCK_SECRET1 0xf4
205 #define HIFN_1_UNLOCK_SECRET2 0xfc
206 #define HIFN_1_PUB_RESET 0x204 /* Public/RNG Reset */
207 #define HIFN_1_PUB_BASE 0x300 /* Public Base Address */
208 #define HIFN_1_PUB_OPLEN 0x304 /* Public Operand Length */
209 #define HIFN_1_PUB_OP 0x308 /* Public Operand */
210 #define HIFN_1_PUB_STATUS 0x30c /* Public Status */
211 #define HIFN_1_PUB_IEN 0x310 /* Public Interrupt enable */
212 #define HIFN_1_RNG_CONFIG 0x314 /* RNG config */
213 #define HIFN_1_RNG_DATA 0x318 /* RNG data */
214 #define HIFN_1_PUB_MEM 0x400 /* start of Public key memory */
215 #define HIFN_1_PUB_MEMEND 0xbff /* end of Public key memory */
217 /* DMA Status and Control Register (HIFN_1_DMA_CSR) */
218 #define HIFN_DMACSR_D_CTRLMASK 0xc0000000 /* Destinition Ring Control */
219 #define HIFN_DMACSR_D_CTRL_NOP 0x00000000 /* Dest. Control: no-op */
220 #define HIFN_DMACSR_D_CTRL_DIS 0x40000000 /* Dest. Control: disable */
221 #define HIFN_DMACSR_D_CTRL_ENA 0x80000000 /* Dest. Control: enable */
222 #define HIFN_DMACSR_D_ABORT 0x20000000 /* Destinition Ring PCIAbort */
223 #define HIFN_DMACSR_D_DONE 0x10000000 /* Destinition Ring Done */
224 #define HIFN_DMACSR_D_LAST 0x08000000 /* Destinition Ring Last */
225 #define HIFN_DMACSR_D_WAIT 0x04000000 /* Destinition Ring Waiting */
226 #define HIFN_DMACSR_D_OVER 0x02000000 /* Destinition Ring Overflow */
227 #define HIFN_DMACSR_R_CTRL 0x00c00000 /* Result Ring Control */
228 #define HIFN_DMACSR_R_CTRL_NOP 0x00000000 /* Result Control: no-op */
229 #define HIFN_DMACSR_R_CTRL_DIS 0x00400000 /* Result Control: disable */
230 #define HIFN_DMACSR_R_CTRL_ENA 0x00800000 /* Result Control: enable */
231 #define HIFN_DMACSR_R_ABORT 0x00200000 /* Result Ring PCI Abort */
232 #define HIFN_DMACSR_R_DONE 0x00100000 /* Result Ring Done */
233 #define HIFN_DMACSR_R_LAST 0x00080000 /* Result Ring Last */
234 #define HIFN_DMACSR_R_WAIT 0x00040000 /* Result Ring Waiting */
235 #define HIFN_DMACSR_R_OVER 0x00020000 /* Result Ring Overflow */
236 #define HIFN_DMACSR_S_CTRL 0x0000c000 /* Source Ring Control */
237 #define HIFN_DMACSR_S_CTRL_NOP 0x00000000 /* Source Control: no-op */
238 #define HIFN_DMACSR_S_CTRL_DIS 0x00004000 /* Source Control: disable */
239 #define HIFN_DMACSR_S_CTRL_ENA 0x00008000 /* Source Control: enable */
240 #define HIFN_DMACSR_S_ABORT 0x00002000 /* Source Ring PCI Abort */
241 #define HIFN_DMACSR_S_DONE 0x00001000 /* Source Ring Done */
242 #define HIFN_DMACSR_S_LAST 0x00000800 /* Source Ring Last */
243 #define HIFN_DMACSR_S_WAIT 0x00000400 /* Source Ring Waiting */
244 #define HIFN_DMACSR_ILLW 0x00000200 /* Illegal write (7811 only) */
245 #define HIFN_DMACSR_ILLR 0x00000100 /* Illegal read (7811 only) */
246 #define HIFN_DMACSR_C_CTRL 0x000000c0 /* Command Ring Control */
247 #define HIFN_DMACSR_C_CTRL_NOP 0x00000000 /* Command Control: no-op */
248 #define HIFN_DMACSR_C_CTRL_DIS 0x00000040 /* Command Control: disable */
249 #define HIFN_DMACSR_C_CTRL_ENA 0x00000080 /* Command Control: enable */
250 #define HIFN_DMACSR_C_ABORT 0x00000020 /* Command Ring PCI Abort */
251 #define HIFN_DMACSR_C_DONE 0x00000010 /* Command Ring Done */
252 #define HIFN_DMACSR_C_LAST 0x00000008 /* Command Ring Last */
253 #define HIFN_DMACSR_C_WAIT 0x00000004 /* Command Ring Waiting */
254 #define HIFN_DMACSR_PUBDONE 0x00000002 /* Public op done (7951 only) */
255 #define HIFN_DMACSR_ENGINE 0x00000001 /* Command Ring Engine IRQ */
257 /* DMA Interrupt Enable Register (HIFN_1_DMA_IER) */
258 #define HIFN_DMAIER_D_ABORT 0x20000000 /* Destination Ring PCIAbort */
259 #define HIFN_DMAIER_D_DONE 0x10000000 /* Destination Ring Done */
260 #define HIFN_DMAIER_D_LAST 0x08000000 /* Destination Ring Last */
261 #define HIFN_DMAIER_D_WAIT 0x04000000 /* Destination Ring Waiting */
262 #define HIFN_DMAIER_D_OVER 0x02000000 /* Destination Ring Overflow */
263 #define HIFN_DMAIER_R_ABORT 0x00200000 /* Result Ring PCI Abort */
264 #define HIFN_DMAIER_R_DONE 0x00100000 /* Result Ring Done */
265 #define HIFN_DMAIER_R_LAST 0x00080000 /* Result Ring Last */
266 #define HIFN_DMAIER_R_WAIT 0x00040000 /* Result Ring Waiting */
267 #define HIFN_DMAIER_R_OVER 0x00020000 /* Result Ring Overflow */
268 #define HIFN_DMAIER_S_ABORT 0x00002000 /* Source Ring PCI Abort */
269 #define HIFN_DMAIER_S_DONE 0x00001000 /* Source Ring Done */
270 #define HIFN_DMAIER_S_LAST 0x00000800 /* Source Ring Last */
271 #define HIFN_DMAIER_S_WAIT 0x00000400 /* Source Ring Waiting */
272 #define HIFN_DMAIER_ILLW 0x00000200 /* Illegal write (7811 only) */
273 #define HIFN_DMAIER_ILLR 0x00000100 /* Illegal read (7811 only) */
274 #define HIFN_DMAIER_C_ABORT 0x00000020 /* Command Ring PCI Abort */
275 #define HIFN_DMAIER_C_DONE 0x00000010 /* Command Ring Done */
276 #define HIFN_DMAIER_C_LAST 0x00000008 /* Command Ring Last */
277 #define HIFN_DMAIER_C_WAIT 0x00000004 /* Command Ring Waiting */
278 #define HIFN_DMAIER_PUBDONE 0x00000002 /* public op done (7951 only) */
279 #define HIFN_DMAIER_ENGINE 0x00000001 /* Engine IRQ */
281 /* DMA Configuration Register (HIFN_1_DMA_CNFG) */
282 #define HIFN_DMACNFG_BIGENDIAN 0x10000000 /* big endian mode */
283 #define HIFN_DMACNFG_POLLFREQ 0x00ff0000 /* Poll frequency mask */
284 #define HIFN_DMACNFG_UNLOCK 0x00000800
285 #define HIFN_DMACNFG_POLLINVAL 0x00000700 /* Invalid Poll Scalar */
286 #define HIFN_DMACNFG_LAST 0x00000010 /* Host control LAST bit */
287 #define HIFN_DMACNFG_MODE 0x00000004 /* DMA mode */
288 #define HIFN_DMACNFG_DMARESET 0x00000002 /* DMA Reset # */
289 #define HIFN_DMACNFG_MSTRESET 0x00000001 /* Master Reset # */
291 /* PLL configuration register */
292 #define HIFN_PLL_REF_CLK_HBI 0x00000000 /* HBI reference clock */
293 #define HIFN_PLL_REF_CLK_PLL 0x00000001 /* PLL reference clock */
294 #define HIFN_PLL_BP 0x00000002 /* Reference clock bypass */
295 #define HIFN_PLL_PK_CLK_HBI 0x00000000 /* PK engine HBI clock */
296 #define HIFN_PLL_PK_CLK_PLL 0x00000008 /* PK engine PLL clock */
297 #define HIFN_PLL_PE_CLK_HBI 0x00000000 /* PE engine HBI clock */
298 #define HIFN_PLL_PE_CLK_PLL 0x00000010 /* PE engine PLL clock */
299 #define HIFN_PLL_RESERVED_1 0x00000400 /* Reserved bit, must be 1 */
300 #define HIFN_PLL_ND_SHIFT 11 /* Clock multiplier shift */
301 #define HIFN_PLL_ND_MULT_2 0x00000000 /* PLL clock multiplier 2 */
302 #define HIFN_PLL_ND_MULT_4 0x00000800 /* PLL clock multiplier 4 */
303 #define HIFN_PLL_ND_MULT_6 0x00001000 /* PLL clock multiplier 6 */
304 #define HIFN_PLL_ND_MULT_8 0x00001800 /* PLL clock multiplier 8 */
305 #define HIFN_PLL_ND_MULT_10 0x00002000 /* PLL clock multiplier 10 */
306 #define HIFN_PLL_ND_MULT_12 0x00002800 /* PLL clock multiplier 12 */
307 #define HIFN_PLL_IS_1_8 0x00000000 /* charge pump (mult. 1-8) */
308 #define HIFN_PLL_IS_9_12 0x00010000 /* charge pump (mult. 9-12) */
310 #define HIFN_PLL_FCK_MAX 266 /* Maximum PLL frequency */
312 /* Public key reset register (HIFN_1_PUB_RESET) */
313 #define HIFN_PUBRST_RESET 0x00000001 /* reset public/rng unit */
315 /* Public base address register (HIFN_1_PUB_BASE) */
316 #define HIFN_PUBBASE_ADDR 0x00003fff /* base address */
318 /* Public operand length register (HIFN_1_PUB_OPLEN) */
319 #define HIFN_PUBOPLEN_MOD_M 0x0000007f /* modulus length mask */
320 #define HIFN_PUBOPLEN_MOD_S 0 /* modulus length shift */
321 #define HIFN_PUBOPLEN_EXP_M 0x0003ff80 /* exponent length mask */
322 #define HIFN_PUBOPLEN_EXP_S 7 /* exponent length shift */
323 #define HIFN_PUBOPLEN_RED_M 0x003c0000 /* reducend length mask */
324 #define HIFN_PUBOPLEN_RED_S 18 /* reducend length shift */
326 /* Public operation register (HIFN_1_PUB_OP) */
327 #define HIFN_PUBOP_AOFFSET_M 0x0000007f /* A offset mask */
328 #define HIFN_PUBOP_AOFFSET_S 0 /* A offset shift */
329 #define HIFN_PUBOP_BOFFSET_M 0x00000f80 /* B offset mask */
330 #define HIFN_PUBOP_BOFFSET_S 7 /* B offset shift */
331 #define HIFN_PUBOP_MOFFSET_M 0x0003f000 /* M offset mask */
332 #define HIFN_PUBOP_MOFFSET_S 12 /* M offset shift */
333 #define HIFN_PUBOP_OP_MASK 0x003c0000 /* Opcode: */
334 #define HIFN_PUBOP_OP_NOP 0x00000000 /* NOP */
335 #define HIFN_PUBOP_OP_ADD 0x00040000 /* ADD */
336 #define HIFN_PUBOP_OP_ADDC 0x00080000 /* ADD w/carry */
337 #define HIFN_PUBOP_OP_SUB 0x000c0000 /* SUB */
338 #define HIFN_PUBOP_OP_SUBC 0x00100000 /* SUB w/carry */
339 #define HIFN_PUBOP_OP_MODADD 0x00140000 /* Modular ADD */
340 #define HIFN_PUBOP_OP_MODSUB 0x00180000 /* Modular SUB */
341 #define HIFN_PUBOP_OP_INCA 0x001c0000 /* INC A */
342 #define HIFN_PUBOP_OP_DECA 0x00200000 /* DEC A */
343 #define HIFN_PUBOP_OP_MULT 0x00240000 /* MULT */
344 #define HIFN_PUBOP_OP_MODMULT 0x00280000 /* Modular MULT */
345 #define HIFN_PUBOP_OP_MODRED 0x002c0000 /* Modular RED */
346 #define HIFN_PUBOP_OP_MODEXP 0x00300000 /* Modular EXP */
348 /* Public status register (HIFN_1_PUB_STATUS) */
349 #define HIFN_PUBSTS_DONE 0x00000001 /* operation done */
350 #define HIFN_PUBSTS_CARRY 0x00000002 /* carry */
352 /* Public interrupt enable register (HIFN_1_PUB_IEN) */
353 #define HIFN_PUBIEN_DONE 0x00000001 /* operation done interrupt */
355 /* Random number generator config register (HIFN_1_RNG_CONFIG) */
356 #define HIFN_RNGCFG_ENA 0x00000001 /* enable rng */
358 #define HIFN_NAMESIZE 32
359 #define HIFN_MAX_RESULT_ORDER 5
361 #define HIFN_D_CMD_RSIZE 24*1
362 #define HIFN_D_SRC_RSIZE 80*1
363 #define HIFN_D_DST_RSIZE 80*1
364 #define HIFN_D_RES_RSIZE 24*1
366 #define HIFN_D_DST_DALIGN 4
368 #define HIFN_QUEUE_LENGTH (HIFN_D_CMD_RSIZE - 1)
370 #define AES_MIN_KEY_SIZE 16
371 #define AES_MAX_KEY_SIZE 32
373 #define HIFN_DES_KEY_LENGTH 8
374 #define HIFN_3DES_KEY_LENGTH 24
375 #define HIFN_MAX_CRYPT_KEY_LENGTH AES_MAX_KEY_SIZE
376 #define HIFN_IV_LENGTH 8
377 #define HIFN_AES_IV_LENGTH 16
378 #define HIFN_MAX_IV_LENGTH HIFN_AES_IV_LENGTH
380 #define HIFN_MAC_KEY_LENGTH 64
381 #define HIFN_MD5_LENGTH 16
382 #define HIFN_SHA1_LENGTH 20
383 #define HIFN_MAC_TRUNC_LENGTH 12
385 #define HIFN_MAX_COMMAND (8 + 8 + 8 + 64 + 260)
386 #define HIFN_MAX_RESULT (8 + 4 + 4 + 20 + 4)
387 #define HIFN_USED_RESULT 12
396 struct hifn_desc cmdr
[HIFN_D_CMD_RSIZE
+1];
397 struct hifn_desc srcr
[HIFN_D_SRC_RSIZE
+1];
398 struct hifn_desc dstr
[HIFN_D_DST_RSIZE
+1];
399 struct hifn_desc resr
[HIFN_D_RES_RSIZE
+1];
401 u8 command_bufs
[HIFN_D_CMD_RSIZE
][HIFN_MAX_COMMAND
];
402 u8 result_bufs
[HIFN_D_CMD_RSIZE
][HIFN_MAX_RESULT
];
405 * Our current positions for insertion and removal from the descriptor
408 volatile int cmdi
, srci
, dsti
, resi
;
409 volatile int cmdu
, srcu
, dstu
, resu
;
410 int cmdk
, srck
, dstk
, resk
;
413 #define HIFN_FLAG_CMD_BUSY (1<<0)
414 #define HIFN_FLAG_SRC_BUSY (1<<1)
415 #define HIFN_FLAG_DST_BUSY (1<<2)
416 #define HIFN_FLAG_RES_BUSY (1<<3)
417 #define HIFN_FLAG_OLD_KEY (1<<4)
419 #define HIFN_DEFAULT_ACTIVE_NUM 5
423 char name
[HIFN_NAMESIZE
];
427 struct pci_dev
*pdev
;
428 void __iomem
*bar
[3];
435 void *sa
[HIFN_D_RES_RSIZE
];
441 struct delayed_work work
;
443 unsigned long success
;
444 unsigned long prev_success
;
448 struct tasklet_struct tasklet
;
450 struct crypto_queue queue
;
451 struct list_head alg_list
;
453 unsigned int pk_clk_freq
;
455 #ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
456 unsigned int rng_wait_time
;
462 #define HIFN_D_LENGTH 0x0000ffff
463 #define HIFN_D_NOINVALID 0x01000000
464 #define HIFN_D_MASKDONEIRQ 0x02000000
465 #define HIFN_D_DESTOVER 0x04000000
466 #define HIFN_D_OVER 0x08000000
467 #define HIFN_D_LAST 0x20000000
468 #define HIFN_D_JUMP 0x40000000
469 #define HIFN_D_VALID 0x80000000
471 struct hifn_base_command
473 volatile __le16 masks
;
474 volatile __le16 session_num
;
475 volatile __le16 total_source_count
;
476 volatile __le16 total_dest_count
;
479 #define HIFN_BASE_CMD_COMP 0x0100 /* enable compression engine */
480 #define HIFN_BASE_CMD_PAD 0x0200 /* enable padding engine */
481 #define HIFN_BASE_CMD_MAC 0x0400 /* enable MAC engine */
482 #define HIFN_BASE_CMD_CRYPT 0x0800 /* enable crypt engine */
483 #define HIFN_BASE_CMD_DECODE 0x2000
484 #define HIFN_BASE_CMD_SRCLEN_M 0xc000
485 #define HIFN_BASE_CMD_SRCLEN_S 14
486 #define HIFN_BASE_CMD_DSTLEN_M 0x3000
487 #define HIFN_BASE_CMD_DSTLEN_S 12
488 #define HIFN_BASE_CMD_LENMASK_HI 0x30000
489 #define HIFN_BASE_CMD_LENMASK_LO 0x0ffff
492 * Structure to help build up the command data structure.
494 struct hifn_crypt_command
496 volatile __le16 masks
;
497 volatile __le16 header_skip
;
498 volatile __le16 source_count
;
499 volatile __le16 reserved
;
502 #define HIFN_CRYPT_CMD_ALG_MASK 0x0003 /* algorithm: */
503 #define HIFN_CRYPT_CMD_ALG_DES 0x0000 /* DES */
504 #define HIFN_CRYPT_CMD_ALG_3DES 0x0001 /* 3DES */
505 #define HIFN_CRYPT_CMD_ALG_RC4 0x0002 /* RC4 */
506 #define HIFN_CRYPT_CMD_ALG_AES 0x0003 /* AES */
507 #define HIFN_CRYPT_CMD_MODE_MASK 0x0018 /* Encrypt mode: */
508 #define HIFN_CRYPT_CMD_MODE_ECB 0x0000 /* ECB */
509 #define HIFN_CRYPT_CMD_MODE_CBC 0x0008 /* CBC */
510 #define HIFN_CRYPT_CMD_MODE_CFB 0x0010 /* CFB */
511 #define HIFN_CRYPT_CMD_MODE_OFB 0x0018 /* OFB */
512 #define HIFN_CRYPT_CMD_CLR_CTX 0x0040 /* clear context */
513 #define HIFN_CRYPT_CMD_KSZ_MASK 0x0600 /* AES key size: */
514 #define HIFN_CRYPT_CMD_KSZ_128 0x0000 /* 128 bit */
515 #define HIFN_CRYPT_CMD_KSZ_192 0x0200 /* 192 bit */
516 #define HIFN_CRYPT_CMD_KSZ_256 0x0400 /* 256 bit */
517 #define HIFN_CRYPT_CMD_NEW_KEY 0x0800 /* expect new key */
518 #define HIFN_CRYPT_CMD_NEW_IV 0x1000 /* expect new iv */
519 #define HIFN_CRYPT_CMD_SRCLEN_M 0xc000
520 #define HIFN_CRYPT_CMD_SRCLEN_S 14
523 * Structure to help build up the command data structure.
525 struct hifn_mac_command
527 volatile __le16 masks
;
528 volatile __le16 header_skip
;
529 volatile __le16 source_count
;
530 volatile __le16 reserved
;
533 #define HIFN_MAC_CMD_ALG_MASK 0x0001
534 #define HIFN_MAC_CMD_ALG_SHA1 0x0000
535 #define HIFN_MAC_CMD_ALG_MD5 0x0001
536 #define HIFN_MAC_CMD_MODE_MASK 0x000c
537 #define HIFN_MAC_CMD_MODE_HMAC 0x0000
538 #define HIFN_MAC_CMD_MODE_SSL_MAC 0x0004
539 #define HIFN_MAC_CMD_MODE_HASH 0x0008
540 #define HIFN_MAC_CMD_MODE_FULL 0x0004
541 #define HIFN_MAC_CMD_TRUNC 0x0010
542 #define HIFN_MAC_CMD_RESULT 0x0020
543 #define HIFN_MAC_CMD_APPEND 0x0040
544 #define HIFN_MAC_CMD_SRCLEN_M 0xc000
545 #define HIFN_MAC_CMD_SRCLEN_S 14
548 * MAC POS IPsec initiates authentication after encryption on encodes
549 * and before decryption on decodes.
551 #define HIFN_MAC_CMD_POS_IPSEC 0x0200
552 #define HIFN_MAC_CMD_NEW_KEY 0x0800
554 struct hifn_comp_command
556 volatile __le16 masks
;
557 volatile __le16 header_skip
;
558 volatile __le16 source_count
;
559 volatile __le16 reserved
;
562 #define HIFN_COMP_CMD_SRCLEN_M 0xc000
563 #define HIFN_COMP_CMD_SRCLEN_S 14
564 #define HIFN_COMP_CMD_ONE 0x0100 /* must be one */
565 #define HIFN_COMP_CMD_CLEARHIST 0x0010 /* clear history */
566 #define HIFN_COMP_CMD_UPDATEHIST 0x0008 /* update history */
567 #define HIFN_COMP_CMD_LZS_STRIP0 0x0004 /* LZS: strip zero */
568 #define HIFN_COMP_CMD_MPPC_RESTART 0x0004 /* MPPC: restart */
569 #define HIFN_COMP_CMD_ALG_MASK 0x0001 /* compression mode: */
570 #define HIFN_COMP_CMD_ALG_MPPC 0x0001 /* MPPC */
571 #define HIFN_COMP_CMD_ALG_LZS 0x0000 /* LZS */
573 struct hifn_base_result
575 volatile __le16 flags
;
576 volatile __le16 session
;
577 volatile __le16 src_cnt
; /* 15:0 of source count */
578 volatile __le16 dst_cnt
; /* 15:0 of dest count */
581 #define HIFN_BASE_RES_DSTOVERRUN 0x0200 /* destination overrun */
582 #define HIFN_BASE_RES_SRCLEN_M 0xc000 /* 17:16 of source count */
583 #define HIFN_BASE_RES_SRCLEN_S 14
584 #define HIFN_BASE_RES_DSTLEN_M 0x3000 /* 17:16 of dest count */
585 #define HIFN_BASE_RES_DSTLEN_S 12
587 struct hifn_comp_result
589 volatile __le16 flags
;
593 #define HIFN_COMP_RES_LCB_M 0xff00 /* longitudinal check byte */
594 #define HIFN_COMP_RES_LCB_S 8
595 #define HIFN_COMP_RES_RESTART 0x0004 /* MPPC: restart */
596 #define HIFN_COMP_RES_ENDMARKER 0x0002 /* LZS: end marker seen */
597 #define HIFN_COMP_RES_SRC_NOTZERO 0x0001 /* source expired */
599 struct hifn_mac_result
601 volatile __le16 flags
;
602 volatile __le16 reserved
;
603 /* followed by 0, 6, 8, or 10 u16's of the MAC, then crypt */
606 #define HIFN_MAC_RES_MISCOMPARE 0x0002 /* compare failed */
607 #define HIFN_MAC_RES_SRC_NOTZERO 0x0001 /* source expired */
609 struct hifn_crypt_result
611 volatile __le16 flags
;
612 volatile __le16 reserved
;
615 #define HIFN_CRYPT_RES_SRC_NOTZERO 0x0001 /* source expired */
617 #ifndef HIFN_POLL_FREQUENCY
618 #define HIFN_POLL_FREQUENCY 0x1
621 #ifndef HIFN_POLL_SCALAR
622 #define HIFN_POLL_SCALAR 0x0
625 #define HIFN_MAX_SEGLEN 0xffff /* maximum dma segment len */
626 #define HIFN_MAX_DMALEN 0x3ffff /* maximum dma length */
628 struct hifn_crypto_alg
630 struct list_head entry
;
631 struct crypto_alg alg
;
632 struct hifn_device
*dev
;
635 #define ASYNC_SCATTERLIST_CACHE 16
637 #define ASYNC_FLAGS_MISALIGNED (1<<0)
639 struct hifn_cipher_walk
641 struct scatterlist cache
[ASYNC_SCATTERLIST_CACHE
];
648 u8 key
[HIFN_MAX_CRYPT_KEY_LENGTH
];
649 struct hifn_device
*dev
;
650 unsigned int keysize
;
653 struct hifn_request_context
657 u8 op
, type
, mode
, unused
;
658 struct hifn_cipher_walk walk
;
661 #define crypto_alg_to_hifn(a) container_of(a, struct hifn_crypto_alg, alg)
663 static inline u32
hifn_read_0(struct hifn_device
*dev
, u32 reg
)
667 ret
= readl(dev
->bar
[0] + reg
);
672 static inline u32
hifn_read_1(struct hifn_device
*dev
, u32 reg
)
676 ret
= readl(dev
->bar
[1] + reg
);
681 static inline void hifn_write_0(struct hifn_device
*dev
, u32 reg
, u32 val
)
683 writel((__force u32
)cpu_to_le32(val
), dev
->bar
[0] + reg
);
686 static inline void hifn_write_1(struct hifn_device
*dev
, u32 reg
, u32 val
)
688 writel((__force u32
)cpu_to_le32(val
), dev
->bar
[1] + reg
);
691 static void hifn_wait_puc(struct hifn_device
*dev
)
696 for (i
=10000; i
> 0; --i
) {
697 ret
= hifn_read_0(dev
, HIFN_0_PUCTRL
);
698 if (!(ret
& HIFN_PUCTRL_RESET
))
705 dprintk("%s: Failed to reset PUC unit.\n", dev
->name
);
708 static void hifn_reset_puc(struct hifn_device
*dev
)
710 hifn_write_0(dev
, HIFN_0_PUCTRL
, HIFN_PUCTRL_DMAENA
);
714 static void hifn_stop_device(struct hifn_device
*dev
)
716 hifn_write_1(dev
, HIFN_1_DMA_CSR
,
717 HIFN_DMACSR_D_CTRL_DIS
| HIFN_DMACSR_R_CTRL_DIS
|
718 HIFN_DMACSR_S_CTRL_DIS
| HIFN_DMACSR_C_CTRL_DIS
);
719 hifn_write_0(dev
, HIFN_0_PUIER
, 0);
720 hifn_write_1(dev
, HIFN_1_DMA_IER
, 0);
723 static void hifn_reset_dma(struct hifn_device
*dev
, int full
)
725 hifn_stop_device(dev
);
728 * Setting poll frequency and others to 0.
730 hifn_write_1(dev
, HIFN_1_DMA_CNFG
, HIFN_DMACNFG_MSTRESET
|
731 HIFN_DMACNFG_DMARESET
| HIFN_DMACNFG_MODE
);
738 hifn_write_1(dev
, HIFN_1_DMA_CNFG
, HIFN_DMACNFG_MODE
);
741 hifn_write_1(dev
, HIFN_1_DMA_CNFG
, HIFN_DMACNFG_MODE
|
742 HIFN_DMACNFG_MSTRESET
);
746 hifn_write_1(dev
, HIFN_1_DMA_CNFG
, HIFN_DMACNFG_MSTRESET
|
747 HIFN_DMACNFG_DMARESET
| HIFN_DMACNFG_MODE
);
752 static u32
hifn_next_signature(u_int32_t a
, u_int cnt
)
757 for (i
= 0; i
< cnt
; i
++) {
767 a
= (v
& 1) ^ (a
<< 1);
773 static struct pci2id
{
780 PCI_DEVICE_ID_HIFN_7955
,
781 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
782 0x00, 0x00, 0x00, 0x00, 0x00 }
786 PCI_DEVICE_ID_HIFN_7956
,
787 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
788 0x00, 0x00, 0x00, 0x00, 0x00 }
792 #ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
793 static int hifn_rng_data_present(struct hwrng
*rng
, int wait
)
795 struct hifn_device
*dev
= (struct hifn_device
*)rng
->priv
;
798 nsec
= ktime_to_ns(ktime_sub(ktime_get(), dev
->rngtime
));
799 nsec
-= dev
->rng_wait_time
;
808 static int hifn_rng_data_read(struct hwrng
*rng
, u32
*data
)
810 struct hifn_device
*dev
= (struct hifn_device
*)rng
->priv
;
812 *data
= hifn_read_1(dev
, HIFN_1_RNG_DATA
);
813 dev
->rngtime
= ktime_get();
817 static int hifn_register_rng(struct hifn_device
*dev
)
820 * We must wait at least 256 Pk_clk cycles between two reads of the rng.
822 dev
->rng_wait_time
= DIV_ROUND_UP_ULL(NSEC_PER_SEC
,
823 dev
->pk_clk_freq
) * 256;
825 dev
->rng
.name
= dev
->name
;
826 dev
->rng
.data_present
= hifn_rng_data_present
,
827 dev
->rng
.data_read
= hifn_rng_data_read
,
828 dev
->rng
.priv
= (unsigned long)dev
;
830 return hwrng_register(&dev
->rng
);
833 static void hifn_unregister_rng(struct hifn_device
*dev
)
835 hwrng_unregister(&dev
->rng
);
838 #define hifn_register_rng(dev) 0
839 #define hifn_unregister_rng(dev)
842 static int hifn_init_pubrng(struct hifn_device
*dev
)
846 hifn_write_1(dev
, HIFN_1_PUB_RESET
, hifn_read_1(dev
, HIFN_1_PUB_RESET
) |
849 for (i
=100; i
> 0; --i
) {
852 if ((hifn_read_1(dev
, HIFN_1_PUB_RESET
) & HIFN_PUBRST_RESET
) == 0)
857 dprintk("Chip %s: Failed to initialise public key engine.\n",
860 hifn_write_1(dev
, HIFN_1_PUB_IEN
, HIFN_PUBIEN_DONE
);
861 dev
->dmareg
|= HIFN_DMAIER_PUBDONE
;
862 hifn_write_1(dev
, HIFN_1_DMA_IER
, dev
->dmareg
);
864 dprintk("Chip %s: Public key engine has been successfully "
865 "initialised.\n", dev
->name
);
872 hifn_write_1(dev
, HIFN_1_RNG_CONFIG
,
873 hifn_read_1(dev
, HIFN_1_RNG_CONFIG
) | HIFN_RNGCFG_ENA
);
874 dprintk("Chip %s: RNG engine has been successfully initialised.\n",
877 #ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
878 /* First value must be discarded */
879 hifn_read_1(dev
, HIFN_1_RNG_DATA
);
880 dev
->rngtime
= ktime_get();
885 static int hifn_enable_crypto(struct hifn_device
*dev
)
891 for (i
= 0; i
< ARRAY_SIZE(pci2id
); i
++) {
892 if (pci2id
[i
].pci_vendor
== dev
->pdev
->vendor
&&
893 pci2id
[i
].pci_prod
== dev
->pdev
->device
) {
894 offtbl
= pci2id
[i
].card_id
;
899 if (offtbl
== NULL
) {
900 dprintk("Chip %s: Unknown card!\n", dev
->name
);
904 dmacfg
= hifn_read_1(dev
, HIFN_1_DMA_CNFG
);
906 hifn_write_1(dev
, HIFN_1_DMA_CNFG
,
907 HIFN_DMACNFG_UNLOCK
| HIFN_DMACNFG_MSTRESET
|
908 HIFN_DMACNFG_DMARESET
| HIFN_DMACNFG_MODE
);
910 addr
= hifn_read_1(dev
, HIFN_1_UNLOCK_SECRET1
);
912 hifn_write_1(dev
, HIFN_1_UNLOCK_SECRET2
, 0);
915 for (i
=0; i
<12; ++i
) {
916 addr
= hifn_next_signature(addr
, offtbl
[i
] + 0x101);
917 hifn_write_1(dev
, HIFN_1_UNLOCK_SECRET2
, addr
);
921 hifn_write_1(dev
, HIFN_1_DMA_CNFG
, dmacfg
);
923 dprintk("Chip %s: %s.\n", dev
->name
, pci_name(dev
->pdev
));
928 static void hifn_init_dma(struct hifn_device
*dev
)
930 struct hifn_dma
*dma
= (struct hifn_dma
*)dev
->desc_virt
;
931 u32 dptr
= dev
->desc_dma
;
934 for (i
=0; i
<HIFN_D_CMD_RSIZE
; ++i
)
935 dma
->cmdr
[i
].p
= __cpu_to_le32(dptr
+
936 offsetof(struct hifn_dma
, command_bufs
[i
][0]));
937 for (i
=0; i
<HIFN_D_RES_RSIZE
; ++i
)
938 dma
->resr
[i
].p
= __cpu_to_le32(dptr
+
939 offsetof(struct hifn_dma
, result_bufs
[i
][0]));
942 * Setup LAST descriptors.
944 dma
->cmdr
[HIFN_D_CMD_RSIZE
].p
= __cpu_to_le32(dptr
+
945 offsetof(struct hifn_dma
, cmdr
[0]));
946 dma
->srcr
[HIFN_D_SRC_RSIZE
].p
= __cpu_to_le32(dptr
+
947 offsetof(struct hifn_dma
, srcr
[0]));
948 dma
->dstr
[HIFN_D_DST_RSIZE
].p
= __cpu_to_le32(dptr
+
949 offsetof(struct hifn_dma
, dstr
[0]));
950 dma
->resr
[HIFN_D_RES_RSIZE
].p
= __cpu_to_le32(dptr
+
951 offsetof(struct hifn_dma
, resr
[0]));
953 dma
->cmdu
= dma
->srcu
= dma
->dstu
= dma
->resu
= 0;
954 dma
->cmdi
= dma
->srci
= dma
->dsti
= dma
->resi
= 0;
955 dma
->cmdk
= dma
->srck
= dma
->dstk
= dma
->resk
= 0;
959 * Initialize the PLL. We need to know the frequency of the reference clock
960 * to calculate the optimal multiplier. For PCI we assume 66MHz, since that
961 * allows us to operate without the risk of overclocking the chip. If it
962 * actually uses 33MHz, the chip will operate at half the speed, this can be
963 * overriden by specifying the frequency as module parameter (pci33).
965 * Unfortunately the PCI clock is not very suitable since the HIFN needs a
966 * stable clock and the PCI clock frequency may vary, so the default is the
967 * external clock. There is no way to find out its frequency, we default to
968 * 66MHz since according to Mike Ham of HiFn, almost every board in existence
969 * has an external crystal populated at 66MHz.
971 static void hifn_init_pll(struct hifn_device
*dev
)
973 unsigned int freq
, m
;
976 pllcfg
= HIFN_1_PLL
| HIFN_PLL_RESERVED_1
;
978 if (strncmp(hifn_pll_ref
, "ext", 3) == 0)
979 pllcfg
|= HIFN_PLL_REF_CLK_PLL
;
981 pllcfg
|= HIFN_PLL_REF_CLK_HBI
;
983 if (hifn_pll_ref
[3] != '\0')
984 freq
= simple_strtoul(hifn_pll_ref
+ 3, NULL
, 10);
987 printk(KERN_INFO
"hifn795x: assuming %uMHz clock speed, "
988 "override with hifn_pll_ref=%.3s<frequency>\n",
992 m
= HIFN_PLL_FCK_MAX
/ freq
;
994 pllcfg
|= (m
/ 2 - 1) << HIFN_PLL_ND_SHIFT
;
996 pllcfg
|= HIFN_PLL_IS_1_8
;
998 pllcfg
|= HIFN_PLL_IS_9_12
;
1000 /* Select clock source and enable clock bypass */
1001 hifn_write_1(dev
, HIFN_1_PLL
, pllcfg
|
1002 HIFN_PLL_PK_CLK_HBI
| HIFN_PLL_PE_CLK_HBI
| HIFN_PLL_BP
);
1004 /* Let the chip lock to the input clock */
1007 /* Disable clock bypass */
1008 hifn_write_1(dev
, HIFN_1_PLL
, pllcfg
|
1009 HIFN_PLL_PK_CLK_HBI
| HIFN_PLL_PE_CLK_HBI
);
1011 /* Switch the engines to the PLL */
1012 hifn_write_1(dev
, HIFN_1_PLL
, pllcfg
|
1013 HIFN_PLL_PK_CLK_PLL
| HIFN_PLL_PE_CLK_PLL
);
1016 * The Fpk_clk runs at half the total speed. Its frequency is needed to
1017 * calculate the minimum time between two reads of the rng. Since 33MHz
1018 * is actually 33.333... we overestimate the frequency here, resulting
1019 * in slightly larger intervals.
1021 dev
->pk_clk_freq
= 1000000 * (freq
+ 1) * m
/ 2;
1024 static void hifn_init_registers(struct hifn_device
*dev
)
1026 u32 dptr
= dev
->desc_dma
;
1028 /* Initialization magic... */
1029 hifn_write_0(dev
, HIFN_0_PUCTRL
, HIFN_PUCTRL_DMAENA
);
1030 hifn_write_0(dev
, HIFN_0_FIFOCNFG
, HIFN_FIFOCNFG_THRESHOLD
);
1031 hifn_write_0(dev
, HIFN_0_PUIER
, HIFN_PUIER_DSTOVER
);
1033 /* write all 4 ring address registers */
1034 hifn_write_1(dev
, HIFN_1_DMA_CRAR
, dptr
+
1035 offsetof(struct hifn_dma
, cmdr
[0]));
1036 hifn_write_1(dev
, HIFN_1_DMA_SRAR
, dptr
+
1037 offsetof(struct hifn_dma
, srcr
[0]));
1038 hifn_write_1(dev
, HIFN_1_DMA_DRAR
, dptr
+
1039 offsetof(struct hifn_dma
, dstr
[0]));
1040 hifn_write_1(dev
, HIFN_1_DMA_RRAR
, dptr
+
1041 offsetof(struct hifn_dma
, resr
[0]));
1045 hifn_write_1(dev
, HIFN_1_DMA_CSR
,
1046 HIFN_DMACSR_D_CTRL_DIS
| HIFN_DMACSR_R_CTRL_DIS
|
1047 HIFN_DMACSR_S_CTRL_DIS
| HIFN_DMACSR_C_CTRL_DIS
|
1048 HIFN_DMACSR_D_ABORT
| HIFN_DMACSR_D_DONE
| HIFN_DMACSR_D_LAST
|
1049 HIFN_DMACSR_D_WAIT
| HIFN_DMACSR_D_OVER
|
1050 HIFN_DMACSR_R_ABORT
| HIFN_DMACSR_R_DONE
| HIFN_DMACSR_R_LAST
|
1051 HIFN_DMACSR_R_WAIT
| HIFN_DMACSR_R_OVER
|
1052 HIFN_DMACSR_S_ABORT
| HIFN_DMACSR_S_DONE
| HIFN_DMACSR_S_LAST
|
1053 HIFN_DMACSR_S_WAIT
|
1054 HIFN_DMACSR_C_ABORT
| HIFN_DMACSR_C_DONE
| HIFN_DMACSR_C_LAST
|
1055 HIFN_DMACSR_C_WAIT
|
1056 HIFN_DMACSR_ENGINE
|
1057 HIFN_DMACSR_PUBDONE
);
1059 hifn_write_1(dev
, HIFN_1_DMA_CSR
,
1060 HIFN_DMACSR_C_CTRL_ENA
| HIFN_DMACSR_S_CTRL_ENA
|
1061 HIFN_DMACSR_D_CTRL_ENA
| HIFN_DMACSR_R_CTRL_ENA
|
1062 HIFN_DMACSR_D_ABORT
| HIFN_DMACSR_D_DONE
| HIFN_DMACSR_D_LAST
|
1063 HIFN_DMACSR_D_WAIT
| HIFN_DMACSR_D_OVER
|
1064 HIFN_DMACSR_R_ABORT
| HIFN_DMACSR_R_DONE
| HIFN_DMACSR_R_LAST
|
1065 HIFN_DMACSR_R_WAIT
| HIFN_DMACSR_R_OVER
|
1066 HIFN_DMACSR_S_ABORT
| HIFN_DMACSR_S_DONE
| HIFN_DMACSR_S_LAST
|
1067 HIFN_DMACSR_S_WAIT
|
1068 HIFN_DMACSR_C_ABORT
| HIFN_DMACSR_C_DONE
| HIFN_DMACSR_C_LAST
|
1069 HIFN_DMACSR_C_WAIT
|
1070 HIFN_DMACSR_ENGINE
|
1071 HIFN_DMACSR_PUBDONE
);
1073 hifn_read_1(dev
, HIFN_1_DMA_CSR
);
1075 dev
->dmareg
|= HIFN_DMAIER_R_DONE
| HIFN_DMAIER_C_ABORT
|
1076 HIFN_DMAIER_D_OVER
| HIFN_DMAIER_R_OVER
|
1077 HIFN_DMAIER_S_ABORT
| HIFN_DMAIER_D_ABORT
| HIFN_DMAIER_R_ABORT
|
1079 dev
->dmareg
&= ~HIFN_DMAIER_C_WAIT
;
1081 hifn_write_1(dev
, HIFN_1_DMA_IER
, dev
->dmareg
);
1082 hifn_read_1(dev
, HIFN_1_DMA_IER
);
1084 hifn_write_0(dev
, HIFN_0_PUCNFG
, HIFN_PUCNFG_ENCCNFG
|
1085 HIFN_PUCNFG_DRFR_128
| HIFN_PUCNFG_TCALLPHASES
|
1086 HIFN_PUCNFG_TCDRVTOTEM
| HIFN_PUCNFG_BUS32
|
1089 hifn_write_0(dev
, HIFN_0_PUCNFG
, 0x10342);
1093 hifn_write_0(dev
, HIFN_0_PUISR
, HIFN_PUISR_DSTOVER
);
1094 hifn_write_1(dev
, HIFN_1_DMA_CNFG
, HIFN_DMACNFG_MSTRESET
|
1095 HIFN_DMACNFG_DMARESET
| HIFN_DMACNFG_MODE
| HIFN_DMACNFG_LAST
|
1096 ((HIFN_POLL_FREQUENCY
<< 16 ) & HIFN_DMACNFG_POLLFREQ
) |
1097 ((HIFN_POLL_SCALAR
<< 8) & HIFN_DMACNFG_POLLINVAL
));
1100 static int hifn_setup_base_command(struct hifn_device
*dev
, u8
*buf
,
1101 unsigned dlen
, unsigned slen
, u16 mask
, u8 snum
)
1103 struct hifn_base_command
*base_cmd
;
1106 base_cmd
= (struct hifn_base_command
*)buf_pos
;
1107 base_cmd
->masks
= __cpu_to_le16(mask
);
1108 base_cmd
->total_source_count
=
1109 __cpu_to_le16(slen
& HIFN_BASE_CMD_LENMASK_LO
);
1110 base_cmd
->total_dest_count
=
1111 __cpu_to_le16(dlen
& HIFN_BASE_CMD_LENMASK_LO
);
1115 base_cmd
->session_num
= __cpu_to_le16(snum
|
1116 ((slen
<< HIFN_BASE_CMD_SRCLEN_S
) & HIFN_BASE_CMD_SRCLEN_M
) |
1117 ((dlen
<< HIFN_BASE_CMD_DSTLEN_S
) & HIFN_BASE_CMD_DSTLEN_M
));
1119 return sizeof(struct hifn_base_command
);
1122 static int hifn_setup_crypto_command(struct hifn_device
*dev
,
1123 u8
*buf
, unsigned dlen
, unsigned slen
,
1124 u8
*key
, int keylen
, u8
*iv
, int ivsize
, u16 mode
)
1126 struct hifn_dma
*dma
= (struct hifn_dma
*)dev
->desc_virt
;
1127 struct hifn_crypt_command
*cry_cmd
;
1131 cry_cmd
= (struct hifn_crypt_command
*)buf_pos
;
1133 cry_cmd
->source_count
= __cpu_to_le16(dlen
& 0xffff);
1135 cry_cmd
->masks
= __cpu_to_le16(mode
|
1136 ((dlen
<< HIFN_CRYPT_CMD_SRCLEN_S
) &
1137 HIFN_CRYPT_CMD_SRCLEN_M
));
1138 cry_cmd
->header_skip
= 0;
1139 cry_cmd
->reserved
= 0;
1141 buf_pos
+= sizeof(struct hifn_crypt_command
);
1144 if (dma
->cmdu
> 1) {
1145 dev
->dmareg
|= HIFN_DMAIER_C_WAIT
;
1146 hifn_write_1(dev
, HIFN_1_DMA_IER
, dev
->dmareg
);
1150 memcpy(buf_pos
, key
, keylen
);
1154 memcpy(buf_pos
, iv
, ivsize
);
1158 cmd_len
= buf_pos
- buf
;
1163 static int hifn_setup_cmd_desc(struct hifn_device
*dev
,
1164 struct hifn_context
*ctx
, struct hifn_request_context
*rctx
,
1165 void *priv
, unsigned int nbytes
)
1167 struct hifn_dma
*dma
= (struct hifn_dma
*)dev
->desc_virt
;
1168 int cmd_len
, sa_idx
;
1173 buf_pos
= buf
= dma
->command_bufs
[dma
->cmdi
];
1177 case ACRYPTO_OP_DECRYPT
:
1178 mask
= HIFN_BASE_CMD_CRYPT
| HIFN_BASE_CMD_DECODE
;
1180 case ACRYPTO_OP_ENCRYPT
:
1181 mask
= HIFN_BASE_CMD_CRYPT
;
1183 case ACRYPTO_OP_HMAC
:
1184 mask
= HIFN_BASE_CMD_MAC
;
1190 buf_pos
+= hifn_setup_base_command(dev
, buf_pos
, nbytes
,
1191 nbytes
, mask
, dev
->snum
);
1193 if (rctx
->op
== ACRYPTO_OP_ENCRYPT
|| rctx
->op
== ACRYPTO_OP_DECRYPT
) {
1197 md
|= HIFN_CRYPT_CMD_NEW_KEY
;
1198 if (rctx
->iv
&& rctx
->mode
!= ACRYPTO_MODE_ECB
)
1199 md
|= HIFN_CRYPT_CMD_NEW_IV
;
1201 switch (rctx
->mode
) {
1202 case ACRYPTO_MODE_ECB
:
1203 md
|= HIFN_CRYPT_CMD_MODE_ECB
;
1205 case ACRYPTO_MODE_CBC
:
1206 md
|= HIFN_CRYPT_CMD_MODE_CBC
;
1208 case ACRYPTO_MODE_CFB
:
1209 md
|= HIFN_CRYPT_CMD_MODE_CFB
;
1211 case ACRYPTO_MODE_OFB
:
1212 md
|= HIFN_CRYPT_CMD_MODE_OFB
;
1218 switch (rctx
->type
) {
1219 case ACRYPTO_TYPE_AES_128
:
1220 if (ctx
->keysize
!= 16)
1222 md
|= HIFN_CRYPT_CMD_KSZ_128
|
1223 HIFN_CRYPT_CMD_ALG_AES
;
1225 case ACRYPTO_TYPE_AES_192
:
1226 if (ctx
->keysize
!= 24)
1228 md
|= HIFN_CRYPT_CMD_KSZ_192
|
1229 HIFN_CRYPT_CMD_ALG_AES
;
1231 case ACRYPTO_TYPE_AES_256
:
1232 if (ctx
->keysize
!= 32)
1234 md
|= HIFN_CRYPT_CMD_KSZ_256
|
1235 HIFN_CRYPT_CMD_ALG_AES
;
1237 case ACRYPTO_TYPE_3DES
:
1238 if (ctx
->keysize
!= 24)
1240 md
|= HIFN_CRYPT_CMD_ALG_3DES
;
1242 case ACRYPTO_TYPE_DES
:
1243 if (ctx
->keysize
!= 8)
1245 md
|= HIFN_CRYPT_CMD_ALG_DES
;
1251 buf_pos
+= hifn_setup_crypto_command(dev
, buf_pos
,
1252 nbytes
, nbytes
, ctx
->key
, ctx
->keysize
,
1253 rctx
->iv
, rctx
->ivsize
, md
);
1256 dev
->sa
[sa_idx
] = priv
;
1259 cmd_len
= buf_pos
- buf
;
1260 dma
->cmdr
[dma
->cmdi
].l
= __cpu_to_le32(cmd_len
| HIFN_D_VALID
|
1261 HIFN_D_LAST
| HIFN_D_MASKDONEIRQ
);
1263 if (++dma
->cmdi
== HIFN_D_CMD_RSIZE
) {
1264 dma
->cmdr
[dma
->cmdi
].l
= __cpu_to_le32(
1265 HIFN_D_VALID
| HIFN_D_LAST
|
1266 HIFN_D_MASKDONEIRQ
| HIFN_D_JUMP
);
1269 dma
->cmdr
[dma
->cmdi
-1].l
|= __cpu_to_le32(HIFN_D_VALID
);
1271 if (!(dev
->flags
& HIFN_FLAG_CMD_BUSY
)) {
1272 hifn_write_1(dev
, HIFN_1_DMA_CSR
, HIFN_DMACSR_C_CTRL_ENA
);
1273 dev
->flags
|= HIFN_FLAG_CMD_BUSY
;
1281 static int hifn_setup_src_desc(struct hifn_device
*dev
, struct page
*page
,
1282 unsigned int offset
, unsigned int size
, int last
)
1284 struct hifn_dma
*dma
= (struct hifn_dma
*)dev
->desc_virt
;
1288 addr
= pci_map_page(dev
->pdev
, page
, offset
, size
, PCI_DMA_TODEVICE
);
1292 dma
->srcr
[idx
].p
= __cpu_to_le32(addr
);
1293 dma
->srcr
[idx
].l
= __cpu_to_le32(size
| HIFN_D_VALID
|
1294 HIFN_D_MASKDONEIRQ
| (last
? HIFN_D_LAST
: 0));
1296 if (++idx
== HIFN_D_SRC_RSIZE
) {
1297 dma
->srcr
[idx
].l
= __cpu_to_le32(HIFN_D_VALID
|
1298 HIFN_D_JUMP
| HIFN_D_MASKDONEIRQ
|
1299 (last
? HIFN_D_LAST
: 0));
1306 if (!(dev
->flags
& HIFN_FLAG_SRC_BUSY
)) {
1307 hifn_write_1(dev
, HIFN_1_DMA_CSR
, HIFN_DMACSR_S_CTRL_ENA
);
1308 dev
->flags
|= HIFN_FLAG_SRC_BUSY
;
1314 static void hifn_setup_res_desc(struct hifn_device
*dev
)
1316 struct hifn_dma
*dma
= (struct hifn_dma
*)dev
->desc_virt
;
1318 dma
->resr
[dma
->resi
].l
= __cpu_to_le32(HIFN_USED_RESULT
|
1319 HIFN_D_VALID
| HIFN_D_LAST
);
1321 * dma->resr[dma->resi].l = __cpu_to_le32(HIFN_MAX_RESULT | HIFN_D_VALID |
1325 if (++dma
->resi
== HIFN_D_RES_RSIZE
) {
1326 dma
->resr
[HIFN_D_RES_RSIZE
].l
= __cpu_to_le32(HIFN_D_VALID
|
1327 HIFN_D_JUMP
| HIFN_D_MASKDONEIRQ
| HIFN_D_LAST
);
1333 if (!(dev
->flags
& HIFN_FLAG_RES_BUSY
)) {
1334 hifn_write_1(dev
, HIFN_1_DMA_CSR
, HIFN_DMACSR_R_CTRL_ENA
);
1335 dev
->flags
|= HIFN_FLAG_RES_BUSY
;
1339 static void hifn_setup_dst_desc(struct hifn_device
*dev
, struct page
*page
,
1340 unsigned offset
, unsigned size
, int last
)
1342 struct hifn_dma
*dma
= (struct hifn_dma
*)dev
->desc_virt
;
1346 addr
= pci_map_page(dev
->pdev
, page
, offset
, size
, PCI_DMA_FROMDEVICE
);
1349 dma
->dstr
[idx
].p
= __cpu_to_le32(addr
);
1350 dma
->dstr
[idx
].l
= __cpu_to_le32(size
| HIFN_D_VALID
|
1351 HIFN_D_MASKDONEIRQ
| (last
? HIFN_D_LAST
: 0));
1353 if (++idx
== HIFN_D_DST_RSIZE
) {
1354 dma
->dstr
[idx
].l
= __cpu_to_le32(HIFN_D_VALID
|
1355 HIFN_D_JUMP
| HIFN_D_MASKDONEIRQ
|
1356 (last
? HIFN_D_LAST
: 0));
1362 if (!(dev
->flags
& HIFN_FLAG_DST_BUSY
)) {
1363 hifn_write_1(dev
, HIFN_1_DMA_CSR
, HIFN_DMACSR_D_CTRL_ENA
);
1364 dev
->flags
|= HIFN_FLAG_DST_BUSY
;
1368 static int hifn_setup_dma(struct hifn_device
*dev
,
1369 struct hifn_context
*ctx
, struct hifn_request_context
*rctx
,
1370 struct scatterlist
*src
, struct scatterlist
*dst
,
1371 unsigned int nbytes
, void *priv
)
1373 struct scatterlist
*t
;
1374 struct page
*spage
, *dpage
;
1375 unsigned int soff
, doff
;
1376 unsigned int n
, len
;
1380 spage
= sg_page(src
);
1382 len
= min(src
->length
, n
);
1384 hifn_setup_src_desc(dev
, spage
, soff
, len
, n
- len
== 0);
1390 t
= &rctx
->walk
.cache
[0];
1393 if (t
->length
&& rctx
->walk
.flags
& ASYNC_FLAGS_MISALIGNED
) {
1394 BUG_ON(!sg_page(t
));
1399 BUG_ON(!sg_page(dst
));
1400 dpage
= sg_page(dst
);
1406 hifn_setup_dst_desc(dev
, dpage
, doff
, len
, n
- len
== 0);
1413 hifn_setup_cmd_desc(dev
, ctx
, rctx
, priv
, nbytes
);
1414 hifn_setup_res_desc(dev
);
1418 static int hifn_cipher_walk_init(struct hifn_cipher_walk
*w
,
1419 int num
, gfp_t gfp_flags
)
1423 num
= min(ASYNC_SCATTERLIST_CACHE
, num
);
1424 sg_init_table(w
->cache
, num
);
1427 for (i
=0; i
<num
; ++i
) {
1428 struct page
*page
= alloc_page(gfp_flags
);
1429 struct scatterlist
*s
;
1436 sg_set_page(s
, page
, PAGE_SIZE
, 0);
1443 static void hifn_cipher_walk_exit(struct hifn_cipher_walk
*w
)
1447 for (i
=0; i
<w
->num
; ++i
) {
1448 struct scatterlist
*s
= &w
->cache
[i
];
1450 __free_page(sg_page(s
));
1458 static int ablkcipher_add(unsigned int *drestp
, struct scatterlist
*dst
,
1459 unsigned int size
, unsigned int *nbytesp
)
1461 unsigned int copy
, drest
= *drestp
, nbytes
= *nbytesp
;
1464 if (drest
< size
|| size
> nbytes
)
1468 copy
= min3(drest
, size
, dst
->length
);
1474 dprintk("%s: copy: %u, size: %u, drest: %u, nbytes: %u.\n",
1475 __func__
, copy
, size
, drest
, nbytes
);
1487 static int hifn_cipher_walk(struct ablkcipher_request
*req
,
1488 struct hifn_cipher_walk
*w
)
1490 struct scatterlist
*dst
, *t
;
1491 unsigned int nbytes
= req
->nbytes
, offset
, copy
, diff
;
1497 if (idx
>= w
->num
&& (w
->flags
& ASYNC_FLAGS_MISALIGNED
))
1500 dst
= &req
->dst
[idx
];
1502 dprintk("\n%s: dlen: %u, doff: %u, offset: %u, nbytes: %u.\n",
1503 __func__
, dst
->length
, dst
->offset
, offset
, nbytes
);
1505 if (!IS_ALIGNED(dst
->offset
, HIFN_D_DST_DALIGN
) ||
1506 !IS_ALIGNED(dst
->length
, HIFN_D_DST_DALIGN
) ||
1508 unsigned slen
= min(dst
->length
- offset
, nbytes
);
1509 unsigned dlen
= PAGE_SIZE
;
1513 err
= ablkcipher_add(&dlen
, dst
, slen
, &nbytes
);
1519 copy
= slen
& ~(HIFN_D_DST_DALIGN
- 1);
1520 diff
= slen
& (HIFN_D_DST_DALIGN
- 1);
1522 if (dlen
< nbytes
) {
1524 * Destination page does not have enough space
1525 * to put there additional blocksized chunk,
1526 * so we mark that page as containing only
1527 * blocksize aligned chunks:
1528 * t->length = (slen & ~(HIFN_D_DST_DALIGN - 1));
1529 * and increase number of bytes to be processed
1536 * Temporary of course...
1537 * Kick author if you will catch this one.
1539 printk(KERN_ERR
"%s: dlen: %u, nbytes: %u,"
1540 "slen: %u, offset: %u.\n",
1541 __func__
, dlen
, nbytes
, slen
, offset
);
1542 printk(KERN_ERR
"%s: please contact author to fix this "
1543 "issue, generally you should not catch "
1544 "this path under any condition but who "
1545 "knows how did you use crypto code.\n"
1546 "Thank you.\n", __func__
);
1549 copy
+= diff
+ nbytes
;
1551 dst
= &req
->dst
[idx
];
1553 err
= ablkcipher_add(&dlen
, dst
, nbytes
, &nbytes
);
1563 nbytes
-= min(dst
->length
, nbytes
);
1573 static int hifn_setup_session(struct ablkcipher_request
*req
)
1575 struct hifn_context
*ctx
= crypto_tfm_ctx(req
->base
.tfm
);
1576 struct hifn_request_context
*rctx
= ablkcipher_request_ctx(req
);
1577 struct hifn_device
*dev
= ctx
->dev
;
1578 unsigned long dlen
, flags
;
1579 unsigned int nbytes
= req
->nbytes
, idx
= 0;
1580 int err
= -EINVAL
, sg_num
;
1581 struct scatterlist
*dst
;
1583 if (rctx
->iv
&& !rctx
->ivsize
&& rctx
->mode
!= ACRYPTO_MODE_ECB
)
1586 rctx
->walk
.flags
= 0;
1589 dst
= &req
->dst
[idx
];
1590 dlen
= min(dst
->length
, nbytes
);
1592 if (!IS_ALIGNED(dst
->offset
, HIFN_D_DST_DALIGN
) ||
1593 !IS_ALIGNED(dlen
, HIFN_D_DST_DALIGN
))
1594 rctx
->walk
.flags
|= ASYNC_FLAGS_MISALIGNED
;
1600 if (rctx
->walk
.flags
& ASYNC_FLAGS_MISALIGNED
) {
1601 err
= hifn_cipher_walk_init(&rctx
->walk
, idx
, GFP_ATOMIC
);
1606 sg_num
= hifn_cipher_walk(req
, &rctx
->walk
);
1612 spin_lock_irqsave(&dev
->lock
, flags
);
1613 if (dev
->started
+ sg_num
> HIFN_QUEUE_LENGTH
) {
1618 err
= hifn_setup_dma(dev
, ctx
, rctx
, req
->src
, req
->dst
, req
->nbytes
, req
);
1624 dev
->active
= HIFN_DEFAULT_ACTIVE_NUM
;
1625 spin_unlock_irqrestore(&dev
->lock
, flags
);
1630 spin_unlock_irqrestore(&dev
->lock
, flags
);
1633 printk("%s: iv: %p [%d], key: %p [%d], mode: %u, op: %u, "
1634 "type: %u, err: %d.\n",
1635 dev
->name
, rctx
->iv
, rctx
->ivsize
,
1636 ctx
->key
, ctx
->keysize
,
1637 rctx
->mode
, rctx
->op
, rctx
->type
, err
);
1643 static int hifn_test(struct hifn_device
*dev
, int encdec
, u8 snum
)
1647 struct hifn_context ctx
;
1648 struct hifn_request_context rctx
;
1649 u8 fips_aes_ecb_from_zero
[16] = {
1650 0x66, 0xE9, 0x4B, 0xD4,
1651 0xEF, 0x8A, 0x2C, 0x3B,
1652 0x88, 0x4C, 0xFA, 0x59,
1653 0xCA, 0x34, 0x2B, 0x2E};
1654 struct scatterlist sg
;
1656 memset(src
, 0, sizeof(src
));
1657 memset(ctx
.key
, 0, sizeof(ctx
.key
));
1663 rctx
.op
= (encdec
)?ACRYPTO_OP_ENCRYPT
:ACRYPTO_OP_DECRYPT
;
1664 rctx
.mode
= ACRYPTO_MODE_ECB
;
1665 rctx
.type
= ACRYPTO_TYPE_AES_128
;
1666 rctx
.walk
.cache
[0].length
= 0;
1668 sg_init_one(&sg
, &src
, sizeof(src
));
1670 err
= hifn_setup_dma(dev
, &ctx
, &rctx
, &sg
, &sg
, sizeof(src
), NULL
);
1677 dprintk("%s: decoded: ", dev
->name
);
1678 for (n
=0; n
<sizeof(src
); ++n
)
1679 dprintk("%02x ", src
[n
]);
1681 dprintk("%s: FIPS : ", dev
->name
);
1682 for (n
=0; n
<sizeof(fips_aes_ecb_from_zero
); ++n
)
1683 dprintk("%02x ", fips_aes_ecb_from_zero
[n
]);
1686 if (!memcmp(src
, fips_aes_ecb_from_zero
, sizeof(fips_aes_ecb_from_zero
))) {
1687 printk(KERN_INFO
"%s: AES 128 ECB test has been successfully "
1688 "passed.\n", dev
->name
);
1693 printk(KERN_INFO
"%s: AES 128 ECB test has been failed.\n", dev
->name
);
1697 static int hifn_start_device(struct hifn_device
*dev
)
1701 dev
->started
= dev
->active
= 0;
1702 hifn_reset_dma(dev
, 1);
1704 err
= hifn_enable_crypto(dev
);
1708 hifn_reset_puc(dev
);
1712 hifn_init_registers(dev
);
1714 hifn_init_pubrng(dev
);
1719 static int ablkcipher_get(void *saddr
, unsigned int *srestp
, unsigned int offset
,
1720 struct scatterlist
*dst
, unsigned int size
, unsigned int *nbytesp
)
1722 unsigned int srest
= *srestp
, nbytes
= *nbytesp
, copy
;
1726 if (srest
< size
|| size
> nbytes
)
1730 copy
= min3(srest
, dst
->length
, size
);
1732 daddr
= kmap_atomic(sg_page(dst
));
1733 memcpy(daddr
+ dst
->offset
+ offset
, saddr
, copy
);
1734 kunmap_atomic(daddr
);
1742 dprintk("%s: copy: %u, size: %u, srest: %u, nbytes: %u.\n",
1743 __func__
, copy
, size
, srest
, nbytes
);
1755 static inline void hifn_complete_sa(struct hifn_device
*dev
, int i
)
1757 unsigned long flags
;
1759 spin_lock_irqsave(&dev
->lock
, flags
);
1762 if (dev
->started
< 0)
1763 printk("%s: started: %d.\n", __func__
, dev
->started
);
1764 spin_unlock_irqrestore(&dev
->lock
, flags
);
1765 BUG_ON(dev
->started
< 0);
1768 static void hifn_process_ready(struct ablkcipher_request
*req
, int error
)
1770 struct hifn_request_context
*rctx
= ablkcipher_request_ctx(req
);
1772 if (rctx
->walk
.flags
& ASYNC_FLAGS_MISALIGNED
) {
1773 unsigned int nbytes
= req
->nbytes
;
1775 struct scatterlist
*dst
, *t
;
1779 t
= &rctx
->walk
.cache
[idx
];
1780 dst
= &req
->dst
[idx
];
1782 dprintk("\n%s: sg_page(t): %p, t->length: %u, "
1783 "sg_page(dst): %p, dst->length: %u, "
1785 __func__
, sg_page(t
), t
->length
,
1786 sg_page(dst
), dst
->length
, nbytes
);
1789 nbytes
-= min(dst
->length
, nbytes
);
1794 saddr
= kmap_atomic(sg_page(t
));
1796 err
= ablkcipher_get(saddr
, &t
->length
, t
->offset
,
1797 dst
, nbytes
, &nbytes
);
1799 kunmap_atomic(saddr
);
1804 kunmap_atomic(saddr
);
1807 hifn_cipher_walk_exit(&rctx
->walk
);
1810 req
->base
.complete(&req
->base
, error
);
1813 static void hifn_clear_rings(struct hifn_device
*dev
, int error
)
1815 struct hifn_dma
*dma
= (struct hifn_dma
*)dev
->desc_virt
;
1818 dprintk("%s: ring cleanup 1: i: %d.%d.%d.%d, u: %d.%d.%d.%d, "
1819 "k: %d.%d.%d.%d.\n",
1821 dma
->cmdi
, dma
->srci
, dma
->dsti
, dma
->resi
,
1822 dma
->cmdu
, dma
->srcu
, dma
->dstu
, dma
->resu
,
1823 dma
->cmdk
, dma
->srck
, dma
->dstk
, dma
->resk
);
1825 i
= dma
->resk
; u
= dma
->resu
;
1827 if (dma
->resr
[i
].l
& __cpu_to_le32(HIFN_D_VALID
))
1833 hifn_process_ready(dev
->sa
[i
], error
);
1834 hifn_complete_sa(dev
, i
);
1837 if (++i
== HIFN_D_RES_RSIZE
)
1841 dma
->resk
= i
; dma
->resu
= u
;
1843 i
= dma
->srck
; u
= dma
->srcu
;
1845 if (dma
->srcr
[i
].l
& __cpu_to_le32(HIFN_D_VALID
))
1847 if (++i
== HIFN_D_SRC_RSIZE
)
1851 dma
->srck
= i
; dma
->srcu
= u
;
1853 i
= dma
->cmdk
; u
= dma
->cmdu
;
1855 if (dma
->cmdr
[i
].l
& __cpu_to_le32(HIFN_D_VALID
))
1857 if (++i
== HIFN_D_CMD_RSIZE
)
1861 dma
->cmdk
= i
; dma
->cmdu
= u
;
1863 i
= dma
->dstk
; u
= dma
->dstu
;
1865 if (dma
->dstr
[i
].l
& __cpu_to_le32(HIFN_D_VALID
))
1867 if (++i
== HIFN_D_DST_RSIZE
)
1871 dma
->dstk
= i
; dma
->dstu
= u
;
1873 dprintk("%s: ring cleanup 2: i: %d.%d.%d.%d, u: %d.%d.%d.%d, "
1874 "k: %d.%d.%d.%d.\n",
1876 dma
->cmdi
, dma
->srci
, dma
->dsti
, dma
->resi
,
1877 dma
->cmdu
, dma
->srcu
, dma
->dstu
, dma
->resu
,
1878 dma
->cmdk
, dma
->srck
, dma
->dstk
, dma
->resk
);
1881 static void hifn_work(struct work_struct
*work
)
1883 struct delayed_work
*dw
= to_delayed_work(work
);
1884 struct hifn_device
*dev
= container_of(dw
, struct hifn_device
, work
);
1885 unsigned long flags
;
1889 spin_lock_irqsave(&dev
->lock
, flags
);
1890 if (dev
->active
== 0) {
1891 struct hifn_dma
*dma
= (struct hifn_dma
*)dev
->desc_virt
;
1893 if (dma
->cmdu
== 0 && (dev
->flags
& HIFN_FLAG_CMD_BUSY
)) {
1894 dev
->flags
&= ~HIFN_FLAG_CMD_BUSY
;
1895 r
|= HIFN_DMACSR_C_CTRL_DIS
;
1897 if (dma
->srcu
== 0 && (dev
->flags
& HIFN_FLAG_SRC_BUSY
)) {
1898 dev
->flags
&= ~HIFN_FLAG_SRC_BUSY
;
1899 r
|= HIFN_DMACSR_S_CTRL_DIS
;
1901 if (dma
->dstu
== 0 && (dev
->flags
& HIFN_FLAG_DST_BUSY
)) {
1902 dev
->flags
&= ~HIFN_FLAG_DST_BUSY
;
1903 r
|= HIFN_DMACSR_D_CTRL_DIS
;
1905 if (dma
->resu
== 0 && (dev
->flags
& HIFN_FLAG_RES_BUSY
)) {
1906 dev
->flags
&= ~HIFN_FLAG_RES_BUSY
;
1907 r
|= HIFN_DMACSR_R_CTRL_DIS
;
1910 hifn_write_1(dev
, HIFN_1_DMA_CSR
, r
);
1914 if ((dev
->prev_success
== dev
->success
) && dev
->started
)
1916 dev
->prev_success
= dev
->success
;
1917 spin_unlock_irqrestore(&dev
->lock
, flags
);
1920 if (++dev
->reset
>= 5) {
1922 struct hifn_dma
*dma
= (struct hifn_dma
*)dev
->desc_virt
;
1924 printk("%s: r: %08x, active: %d, started: %d, "
1925 "success: %lu: qlen: %u/%u, reset: %d.\n",
1926 dev
->name
, r
, dev
->active
, dev
->started
,
1927 dev
->success
, dev
->queue
.qlen
, dev
->queue
.max_qlen
,
1930 printk("%s: res: ", __func__
);
1931 for (i
=0; i
<HIFN_D_RES_RSIZE
; ++i
) {
1932 printk("%x.%p ", dma
->resr
[i
].l
, dev
->sa
[i
]);
1934 hifn_process_ready(dev
->sa
[i
], -ENODEV
);
1935 hifn_complete_sa(dev
, i
);
1940 hifn_reset_dma(dev
, 1);
1941 hifn_stop_device(dev
);
1942 hifn_start_device(dev
);
1946 tasklet_schedule(&dev
->tasklet
);
1949 schedule_delayed_work(&dev
->work
, HZ
);
1952 static irqreturn_t
hifn_interrupt(int irq
, void *data
)
1954 struct hifn_device
*dev
= (struct hifn_device
*)data
;
1955 struct hifn_dma
*dma
= (struct hifn_dma
*)dev
->desc_virt
;
1956 u32 dmacsr
, restart
;
1958 dmacsr
= hifn_read_1(dev
, HIFN_1_DMA_CSR
);
1960 dprintk("%s: 1 dmacsr: %08x, dmareg: %08x, res: %08x [%d], "
1961 "i: %d.%d.%d.%d, u: %d.%d.%d.%d.\n",
1962 dev
->name
, dmacsr
, dev
->dmareg
, dmacsr
& dev
->dmareg
, dma
->cmdi
,
1963 dma
->cmdi
, dma
->srci
, dma
->dsti
, dma
->resi
,
1964 dma
->cmdu
, dma
->srcu
, dma
->dstu
, dma
->resu
);
1966 if ((dmacsr
& dev
->dmareg
) == 0)
1969 hifn_write_1(dev
, HIFN_1_DMA_CSR
, dmacsr
& dev
->dmareg
);
1971 if (dmacsr
& HIFN_DMACSR_ENGINE
)
1972 hifn_write_0(dev
, HIFN_0_PUISR
, hifn_read_0(dev
, HIFN_0_PUISR
));
1973 if (dmacsr
& HIFN_DMACSR_PUBDONE
)
1974 hifn_write_1(dev
, HIFN_1_PUB_STATUS
,
1975 hifn_read_1(dev
, HIFN_1_PUB_STATUS
) | HIFN_PUBSTS_DONE
);
1977 restart
= dmacsr
& (HIFN_DMACSR_R_OVER
| HIFN_DMACSR_D_OVER
);
1979 u32 puisr
= hifn_read_0(dev
, HIFN_0_PUISR
);
1981 printk(KERN_WARNING
"%s: overflow: r: %d, d: %d, puisr: %08x, d: %u.\n",
1982 dev
->name
, !!(dmacsr
& HIFN_DMACSR_R_OVER
),
1983 !!(dmacsr
& HIFN_DMACSR_D_OVER
),
1984 puisr
, !!(puisr
& HIFN_PUISR_DSTOVER
));
1985 if (!!(puisr
& HIFN_PUISR_DSTOVER
))
1986 hifn_write_0(dev
, HIFN_0_PUISR
, HIFN_PUISR_DSTOVER
);
1987 hifn_write_1(dev
, HIFN_1_DMA_CSR
, dmacsr
& (HIFN_DMACSR_R_OVER
|
1988 HIFN_DMACSR_D_OVER
));
1991 restart
= dmacsr
& (HIFN_DMACSR_C_ABORT
| HIFN_DMACSR_S_ABORT
|
1992 HIFN_DMACSR_D_ABORT
| HIFN_DMACSR_R_ABORT
);
1994 printk(KERN_WARNING
"%s: abort: c: %d, s: %d, d: %d, r: %d.\n",
1995 dev
->name
, !!(dmacsr
& HIFN_DMACSR_C_ABORT
),
1996 !!(dmacsr
& HIFN_DMACSR_S_ABORT
),
1997 !!(dmacsr
& HIFN_DMACSR_D_ABORT
),
1998 !!(dmacsr
& HIFN_DMACSR_R_ABORT
));
1999 hifn_reset_dma(dev
, 1);
2001 hifn_init_registers(dev
);
2004 if ((dmacsr
& HIFN_DMACSR_C_WAIT
) && (dma
->cmdu
== 0)) {
2005 dprintk("%s: wait on command.\n", dev
->name
);
2006 dev
->dmareg
&= ~(HIFN_DMAIER_C_WAIT
);
2007 hifn_write_1(dev
, HIFN_1_DMA_IER
, dev
->dmareg
);
2010 tasklet_schedule(&dev
->tasklet
);
2015 static void hifn_flush(struct hifn_device
*dev
)
2017 unsigned long flags
;
2018 struct crypto_async_request
*async_req
;
2019 struct ablkcipher_request
*req
;
2020 struct hifn_dma
*dma
= (struct hifn_dma
*)dev
->desc_virt
;
2023 for (i
=0; i
<HIFN_D_RES_RSIZE
; ++i
) {
2024 struct hifn_desc
*d
= &dma
->resr
[i
];
2027 hifn_process_ready(dev
->sa
[i
],
2028 (d
->l
& __cpu_to_le32(HIFN_D_VALID
))?-ENODEV
:0);
2029 hifn_complete_sa(dev
, i
);
2033 spin_lock_irqsave(&dev
->lock
, flags
);
2034 while ((async_req
= crypto_dequeue_request(&dev
->queue
))) {
2035 req
= container_of(async_req
, struct ablkcipher_request
, base
);
2036 spin_unlock_irqrestore(&dev
->lock
, flags
);
2038 hifn_process_ready(req
, -ENODEV
);
2040 spin_lock_irqsave(&dev
->lock
, flags
);
2042 spin_unlock_irqrestore(&dev
->lock
, flags
);
2045 static int hifn_setkey(struct crypto_ablkcipher
*cipher
, const u8
*key
,
2048 struct crypto_tfm
*tfm
= crypto_ablkcipher_tfm(cipher
);
2049 struct hifn_context
*ctx
= crypto_tfm_ctx(tfm
);
2050 struct hifn_device
*dev
= ctx
->dev
;
2052 if (len
> HIFN_MAX_CRYPT_KEY_LENGTH
) {
2053 crypto_ablkcipher_set_flags(cipher
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
2057 if (len
== HIFN_DES_KEY_LENGTH
) {
2058 u32 tmp
[DES_EXPKEY_WORDS
];
2059 int ret
= des_ekey(tmp
, key
);
2061 if (unlikely(ret
== 0) && (tfm
->crt_flags
& CRYPTO_TFM_REQ_WEAK_KEY
)) {
2062 tfm
->crt_flags
|= CRYPTO_TFM_RES_WEAK_KEY
;
2067 dev
->flags
&= ~HIFN_FLAG_OLD_KEY
;
2069 memcpy(ctx
->key
, key
, len
);
2075 static int hifn_handle_req(struct ablkcipher_request
*req
)
2077 struct hifn_context
*ctx
= crypto_tfm_ctx(req
->base
.tfm
);
2078 struct hifn_device
*dev
= ctx
->dev
;
2081 if (dev
->started
+ DIV_ROUND_UP(req
->nbytes
, PAGE_SIZE
) <= HIFN_QUEUE_LENGTH
)
2082 err
= hifn_setup_session(req
);
2084 if (err
== -EAGAIN
) {
2085 unsigned long flags
;
2087 spin_lock_irqsave(&dev
->lock
, flags
);
2088 err
= ablkcipher_enqueue_request(&dev
->queue
, req
);
2089 spin_unlock_irqrestore(&dev
->lock
, flags
);
2095 static int hifn_setup_crypto_req(struct ablkcipher_request
*req
, u8 op
,
2098 struct hifn_context
*ctx
= crypto_tfm_ctx(req
->base
.tfm
);
2099 struct hifn_request_context
*rctx
= ablkcipher_request_ctx(req
);
2102 ivsize
= crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(req
));
2104 if (req
->info
&& mode
!= ACRYPTO_MODE_ECB
) {
2105 if (type
== ACRYPTO_TYPE_AES_128
)
2106 ivsize
= HIFN_AES_IV_LENGTH
;
2107 else if (type
== ACRYPTO_TYPE_DES
)
2108 ivsize
= HIFN_DES_KEY_LENGTH
;
2109 else if (type
== ACRYPTO_TYPE_3DES
)
2110 ivsize
= HIFN_3DES_KEY_LENGTH
;
2113 if (ctx
->keysize
!= 16 && type
== ACRYPTO_TYPE_AES_128
) {
2114 if (ctx
->keysize
== 24)
2115 type
= ACRYPTO_TYPE_AES_192
;
2116 else if (ctx
->keysize
== 32)
2117 type
= ACRYPTO_TYPE_AES_256
;
2123 rctx
->iv
= req
->info
;
2124 rctx
->ivsize
= ivsize
;
2127 * HEAVY TODO: needs to kick Herbert XU to write documentation.
2128 * HEAVY TODO: needs to kick Herbert XU to write documentation.
2129 * HEAVY TODO: needs to kick Herbert XU to write documentation.
2132 return hifn_handle_req(req
);
2135 static int hifn_process_queue(struct hifn_device
*dev
)
2137 struct crypto_async_request
*async_req
, *backlog
;
2138 struct ablkcipher_request
*req
;
2139 unsigned long flags
;
2142 while (dev
->started
< HIFN_QUEUE_LENGTH
) {
2143 spin_lock_irqsave(&dev
->lock
, flags
);
2144 backlog
= crypto_get_backlog(&dev
->queue
);
2145 async_req
= crypto_dequeue_request(&dev
->queue
);
2146 spin_unlock_irqrestore(&dev
->lock
, flags
);
2152 backlog
->complete(backlog
, -EINPROGRESS
);
2154 req
= container_of(async_req
, struct ablkcipher_request
, base
);
2156 err
= hifn_handle_req(req
);
2164 static int hifn_setup_crypto(struct ablkcipher_request
*req
, u8 op
,
2168 struct hifn_context
*ctx
= crypto_tfm_ctx(req
->base
.tfm
);
2169 struct hifn_device
*dev
= ctx
->dev
;
2171 err
= hifn_setup_crypto_req(req
, op
, type
, mode
);
2175 if (dev
->started
< HIFN_QUEUE_LENGTH
&& dev
->queue
.qlen
)
2176 hifn_process_queue(dev
);
2178 return -EINPROGRESS
;
2182 * AES ecryption functions.
2184 static inline int hifn_encrypt_aes_ecb(struct ablkcipher_request
*req
)
2186 return hifn_setup_crypto(req
, ACRYPTO_OP_ENCRYPT
,
2187 ACRYPTO_TYPE_AES_128
, ACRYPTO_MODE_ECB
);
2189 static inline int hifn_encrypt_aes_cbc(struct ablkcipher_request
*req
)
2191 return hifn_setup_crypto(req
, ACRYPTO_OP_ENCRYPT
,
2192 ACRYPTO_TYPE_AES_128
, ACRYPTO_MODE_CBC
);
2194 static inline int hifn_encrypt_aes_cfb(struct ablkcipher_request
*req
)
2196 return hifn_setup_crypto(req
, ACRYPTO_OP_ENCRYPT
,
2197 ACRYPTO_TYPE_AES_128
, ACRYPTO_MODE_CFB
);
2199 static inline int hifn_encrypt_aes_ofb(struct ablkcipher_request
*req
)
2201 return hifn_setup_crypto(req
, ACRYPTO_OP_ENCRYPT
,
2202 ACRYPTO_TYPE_AES_128
, ACRYPTO_MODE_OFB
);
2206 * AES decryption functions.
2208 static inline int hifn_decrypt_aes_ecb(struct ablkcipher_request
*req
)
2210 return hifn_setup_crypto(req
, ACRYPTO_OP_DECRYPT
,
2211 ACRYPTO_TYPE_AES_128
, ACRYPTO_MODE_ECB
);
2213 static inline int hifn_decrypt_aes_cbc(struct ablkcipher_request
*req
)
2215 return hifn_setup_crypto(req
, ACRYPTO_OP_DECRYPT
,
2216 ACRYPTO_TYPE_AES_128
, ACRYPTO_MODE_CBC
);
2218 static inline int hifn_decrypt_aes_cfb(struct ablkcipher_request
*req
)
2220 return hifn_setup_crypto(req
, ACRYPTO_OP_DECRYPT
,
2221 ACRYPTO_TYPE_AES_128
, ACRYPTO_MODE_CFB
);
2223 static inline int hifn_decrypt_aes_ofb(struct ablkcipher_request
*req
)
2225 return hifn_setup_crypto(req
, ACRYPTO_OP_DECRYPT
,
2226 ACRYPTO_TYPE_AES_128
, ACRYPTO_MODE_OFB
);
2230 * DES ecryption functions.
2232 static inline int hifn_encrypt_des_ecb(struct ablkcipher_request
*req
)
2234 return hifn_setup_crypto(req
, ACRYPTO_OP_ENCRYPT
,
2235 ACRYPTO_TYPE_DES
, ACRYPTO_MODE_ECB
);
2237 static inline int hifn_encrypt_des_cbc(struct ablkcipher_request
*req
)
2239 return hifn_setup_crypto(req
, ACRYPTO_OP_ENCRYPT
,
2240 ACRYPTO_TYPE_DES
, ACRYPTO_MODE_CBC
);
2242 static inline int hifn_encrypt_des_cfb(struct ablkcipher_request
*req
)
2244 return hifn_setup_crypto(req
, ACRYPTO_OP_ENCRYPT
,
2245 ACRYPTO_TYPE_DES
, ACRYPTO_MODE_CFB
);
2247 static inline int hifn_encrypt_des_ofb(struct ablkcipher_request
*req
)
2249 return hifn_setup_crypto(req
, ACRYPTO_OP_ENCRYPT
,
2250 ACRYPTO_TYPE_DES
, ACRYPTO_MODE_OFB
);
2254 * DES decryption functions.
2256 static inline int hifn_decrypt_des_ecb(struct ablkcipher_request
*req
)
2258 return hifn_setup_crypto(req
, ACRYPTO_OP_DECRYPT
,
2259 ACRYPTO_TYPE_DES
, ACRYPTO_MODE_ECB
);
2261 static inline int hifn_decrypt_des_cbc(struct ablkcipher_request
*req
)
2263 return hifn_setup_crypto(req
, ACRYPTO_OP_DECRYPT
,
2264 ACRYPTO_TYPE_DES
, ACRYPTO_MODE_CBC
);
2266 static inline int hifn_decrypt_des_cfb(struct ablkcipher_request
*req
)
2268 return hifn_setup_crypto(req
, ACRYPTO_OP_DECRYPT
,
2269 ACRYPTO_TYPE_DES
, ACRYPTO_MODE_CFB
);
2271 static inline int hifn_decrypt_des_ofb(struct ablkcipher_request
*req
)
2273 return hifn_setup_crypto(req
, ACRYPTO_OP_DECRYPT
,
2274 ACRYPTO_TYPE_DES
, ACRYPTO_MODE_OFB
);
2278 * 3DES ecryption functions.
2280 static inline int hifn_encrypt_3des_ecb(struct ablkcipher_request
*req
)
2282 return hifn_setup_crypto(req
, ACRYPTO_OP_ENCRYPT
,
2283 ACRYPTO_TYPE_3DES
, ACRYPTO_MODE_ECB
);
2285 static inline int hifn_encrypt_3des_cbc(struct ablkcipher_request
*req
)
2287 return hifn_setup_crypto(req
, ACRYPTO_OP_ENCRYPT
,
2288 ACRYPTO_TYPE_3DES
, ACRYPTO_MODE_CBC
);
2290 static inline int hifn_encrypt_3des_cfb(struct ablkcipher_request
*req
)
2292 return hifn_setup_crypto(req
, ACRYPTO_OP_ENCRYPT
,
2293 ACRYPTO_TYPE_3DES
, ACRYPTO_MODE_CFB
);
2295 static inline int hifn_encrypt_3des_ofb(struct ablkcipher_request
*req
)
2297 return hifn_setup_crypto(req
, ACRYPTO_OP_ENCRYPT
,
2298 ACRYPTO_TYPE_3DES
, ACRYPTO_MODE_OFB
);
2302 * 3DES decryption functions.
2304 static inline int hifn_decrypt_3des_ecb(struct ablkcipher_request
*req
)
2306 return hifn_setup_crypto(req
, ACRYPTO_OP_DECRYPT
,
2307 ACRYPTO_TYPE_3DES
, ACRYPTO_MODE_ECB
);
2309 static inline int hifn_decrypt_3des_cbc(struct ablkcipher_request
*req
)
2311 return hifn_setup_crypto(req
, ACRYPTO_OP_DECRYPT
,
2312 ACRYPTO_TYPE_3DES
, ACRYPTO_MODE_CBC
);
2314 static inline int hifn_decrypt_3des_cfb(struct ablkcipher_request
*req
)
2316 return hifn_setup_crypto(req
, ACRYPTO_OP_DECRYPT
,
2317 ACRYPTO_TYPE_3DES
, ACRYPTO_MODE_CFB
);
2319 static inline int hifn_decrypt_3des_ofb(struct ablkcipher_request
*req
)
2321 return hifn_setup_crypto(req
, ACRYPTO_OP_DECRYPT
,
2322 ACRYPTO_TYPE_3DES
, ACRYPTO_MODE_OFB
);
2325 struct hifn_alg_template
2327 char name
[CRYPTO_MAX_ALG_NAME
];
2328 char drv_name
[CRYPTO_MAX_ALG_NAME
];
2330 struct ablkcipher_alg ablkcipher
;
2333 static struct hifn_alg_template hifn_alg_templates
[] = {
2335 * 3DES ECB, CBC, CFB and OFB modes.
2338 .name
= "cfb(des3_ede)", .drv_name
= "cfb-3des", .bsize
= 8,
2340 .min_keysize
= HIFN_3DES_KEY_LENGTH
,
2341 .max_keysize
= HIFN_3DES_KEY_LENGTH
,
2342 .setkey
= hifn_setkey
,
2343 .encrypt
= hifn_encrypt_3des_cfb
,
2344 .decrypt
= hifn_decrypt_3des_cfb
,
2348 .name
= "ofb(des3_ede)", .drv_name
= "ofb-3des", .bsize
= 8,
2350 .min_keysize
= HIFN_3DES_KEY_LENGTH
,
2351 .max_keysize
= HIFN_3DES_KEY_LENGTH
,
2352 .setkey
= hifn_setkey
,
2353 .encrypt
= hifn_encrypt_3des_ofb
,
2354 .decrypt
= hifn_decrypt_3des_ofb
,
2358 .name
= "cbc(des3_ede)", .drv_name
= "cbc-3des", .bsize
= 8,
2360 .ivsize
= HIFN_IV_LENGTH
,
2361 .min_keysize
= HIFN_3DES_KEY_LENGTH
,
2362 .max_keysize
= HIFN_3DES_KEY_LENGTH
,
2363 .setkey
= hifn_setkey
,
2364 .encrypt
= hifn_encrypt_3des_cbc
,
2365 .decrypt
= hifn_decrypt_3des_cbc
,
2369 .name
= "ecb(des3_ede)", .drv_name
= "ecb-3des", .bsize
= 8,
2371 .min_keysize
= HIFN_3DES_KEY_LENGTH
,
2372 .max_keysize
= HIFN_3DES_KEY_LENGTH
,
2373 .setkey
= hifn_setkey
,
2374 .encrypt
= hifn_encrypt_3des_ecb
,
2375 .decrypt
= hifn_decrypt_3des_ecb
,
2380 * DES ECB, CBC, CFB and OFB modes.
2383 .name
= "cfb(des)", .drv_name
= "cfb-des", .bsize
= 8,
2385 .min_keysize
= HIFN_DES_KEY_LENGTH
,
2386 .max_keysize
= HIFN_DES_KEY_LENGTH
,
2387 .setkey
= hifn_setkey
,
2388 .encrypt
= hifn_encrypt_des_cfb
,
2389 .decrypt
= hifn_decrypt_des_cfb
,
2393 .name
= "ofb(des)", .drv_name
= "ofb-des", .bsize
= 8,
2395 .min_keysize
= HIFN_DES_KEY_LENGTH
,
2396 .max_keysize
= HIFN_DES_KEY_LENGTH
,
2397 .setkey
= hifn_setkey
,
2398 .encrypt
= hifn_encrypt_des_ofb
,
2399 .decrypt
= hifn_decrypt_des_ofb
,
2403 .name
= "cbc(des)", .drv_name
= "cbc-des", .bsize
= 8,
2405 .ivsize
= HIFN_IV_LENGTH
,
2406 .min_keysize
= HIFN_DES_KEY_LENGTH
,
2407 .max_keysize
= HIFN_DES_KEY_LENGTH
,
2408 .setkey
= hifn_setkey
,
2409 .encrypt
= hifn_encrypt_des_cbc
,
2410 .decrypt
= hifn_decrypt_des_cbc
,
2414 .name
= "ecb(des)", .drv_name
= "ecb-des", .bsize
= 8,
2416 .min_keysize
= HIFN_DES_KEY_LENGTH
,
2417 .max_keysize
= HIFN_DES_KEY_LENGTH
,
2418 .setkey
= hifn_setkey
,
2419 .encrypt
= hifn_encrypt_des_ecb
,
2420 .decrypt
= hifn_decrypt_des_ecb
,
2425 * AES ECB, CBC, CFB and OFB modes.
2428 .name
= "ecb(aes)", .drv_name
= "ecb-aes", .bsize
= 16,
2430 .min_keysize
= AES_MIN_KEY_SIZE
,
2431 .max_keysize
= AES_MAX_KEY_SIZE
,
2432 .setkey
= hifn_setkey
,
2433 .encrypt
= hifn_encrypt_aes_ecb
,
2434 .decrypt
= hifn_decrypt_aes_ecb
,
2438 .name
= "cbc(aes)", .drv_name
= "cbc-aes", .bsize
= 16,
2440 .ivsize
= HIFN_AES_IV_LENGTH
,
2441 .min_keysize
= AES_MIN_KEY_SIZE
,
2442 .max_keysize
= AES_MAX_KEY_SIZE
,
2443 .setkey
= hifn_setkey
,
2444 .encrypt
= hifn_encrypt_aes_cbc
,
2445 .decrypt
= hifn_decrypt_aes_cbc
,
2449 .name
= "cfb(aes)", .drv_name
= "cfb-aes", .bsize
= 16,
2451 .min_keysize
= AES_MIN_KEY_SIZE
,
2452 .max_keysize
= AES_MAX_KEY_SIZE
,
2453 .setkey
= hifn_setkey
,
2454 .encrypt
= hifn_encrypt_aes_cfb
,
2455 .decrypt
= hifn_decrypt_aes_cfb
,
2459 .name
= "ofb(aes)", .drv_name
= "ofb-aes", .bsize
= 16,
2461 .min_keysize
= AES_MIN_KEY_SIZE
,
2462 .max_keysize
= AES_MAX_KEY_SIZE
,
2463 .setkey
= hifn_setkey
,
2464 .encrypt
= hifn_encrypt_aes_ofb
,
2465 .decrypt
= hifn_decrypt_aes_ofb
,
2470 static int hifn_cra_init(struct crypto_tfm
*tfm
)
2472 struct crypto_alg
*alg
= tfm
->__crt_alg
;
2473 struct hifn_crypto_alg
*ha
= crypto_alg_to_hifn(alg
);
2474 struct hifn_context
*ctx
= crypto_tfm_ctx(tfm
);
2477 tfm
->crt_ablkcipher
.reqsize
= sizeof(struct hifn_request_context
);
2481 static int hifn_alg_alloc(struct hifn_device
*dev
, struct hifn_alg_template
*t
)
2483 struct hifn_crypto_alg
*alg
;
2486 alg
= kzalloc(sizeof(struct hifn_crypto_alg
), GFP_KERNEL
);
2490 snprintf(alg
->alg
.cra_name
, CRYPTO_MAX_ALG_NAME
, "%s", t
->name
);
2491 snprintf(alg
->alg
.cra_driver_name
, CRYPTO_MAX_ALG_NAME
, "%s-%s",
2492 t
->drv_name
, dev
->name
);
2494 alg
->alg
.cra_priority
= 300;
2495 alg
->alg
.cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
2496 CRYPTO_ALG_KERN_DRIVER_ONLY
| CRYPTO_ALG_ASYNC
;
2497 alg
->alg
.cra_blocksize
= t
->bsize
;
2498 alg
->alg
.cra_ctxsize
= sizeof(struct hifn_context
);
2499 alg
->alg
.cra_alignmask
= 0;
2500 alg
->alg
.cra_type
= &crypto_ablkcipher_type
;
2501 alg
->alg
.cra_module
= THIS_MODULE
;
2502 alg
->alg
.cra_u
.ablkcipher
= t
->ablkcipher
;
2503 alg
->alg
.cra_init
= hifn_cra_init
;
2507 list_add_tail(&alg
->entry
, &dev
->alg_list
);
2509 err
= crypto_register_alg(&alg
->alg
);
2511 list_del(&alg
->entry
);
2518 static void hifn_unregister_alg(struct hifn_device
*dev
)
2520 struct hifn_crypto_alg
*a
, *n
;
2522 list_for_each_entry_safe(a
, n
, &dev
->alg_list
, entry
) {
2523 list_del(&a
->entry
);
2524 crypto_unregister_alg(&a
->alg
);
2529 static int hifn_register_alg(struct hifn_device
*dev
)
2533 for (i
=0; i
<ARRAY_SIZE(hifn_alg_templates
); ++i
) {
2534 err
= hifn_alg_alloc(dev
, &hifn_alg_templates
[i
]);
2542 hifn_unregister_alg(dev
);
2546 static void hifn_tasklet_callback(unsigned long data
)
2548 struct hifn_device
*dev
= (struct hifn_device
*)data
;
2551 * This is ok to call this without lock being held,
2552 * althogh it modifies some parameters used in parallel,
2553 * (like dev->success), but they are used in process
2554 * context or update is atomic (like setting dev->sa[i] to NULL).
2556 hifn_clear_rings(dev
, 0);
2558 if (dev
->started
< HIFN_QUEUE_LENGTH
&& dev
->queue
.qlen
)
2559 hifn_process_queue(dev
);
2562 static int hifn_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
2565 struct hifn_device
*dev
;
2568 err
= pci_enable_device(pdev
);
2571 pci_set_master(pdev
);
2573 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
2575 goto err_out_disable_pci_device
;
2577 snprintf(name
, sizeof(name
), "hifn%d",
2578 atomic_inc_return(&hifn_dev_number
)-1);
2580 err
= pci_request_regions(pdev
, name
);
2582 goto err_out_disable_pci_device
;
2584 if (pci_resource_len(pdev
, 0) < HIFN_BAR0_SIZE
||
2585 pci_resource_len(pdev
, 1) < HIFN_BAR1_SIZE
||
2586 pci_resource_len(pdev
, 2) < HIFN_BAR2_SIZE
) {
2587 dprintk("%s: Broken hardware - I/O regions are too small.\n",
2590 goto err_out_free_regions
;
2593 dev
= kzalloc(sizeof(struct hifn_device
) + sizeof(struct crypto_alg
),
2597 goto err_out_free_regions
;
2600 INIT_LIST_HEAD(&dev
->alg_list
);
2602 snprintf(dev
->name
, sizeof(dev
->name
), "%s", name
);
2603 spin_lock_init(&dev
->lock
);
2605 for (i
=0; i
<3; ++i
) {
2606 unsigned long addr
, size
;
2608 addr
= pci_resource_start(pdev
, i
);
2609 size
= pci_resource_len(pdev
, i
);
2611 dev
->bar
[i
] = ioremap_nocache(addr
, size
);
2614 goto err_out_unmap_bars
;
2618 dev
->desc_virt
= pci_zalloc_consistent(pdev
, sizeof(struct hifn_dma
),
2620 if (!dev
->desc_virt
) {
2621 dprintk("Failed to allocate descriptor rings.\n");
2623 goto err_out_unmap_bars
;
2627 dev
->irq
= pdev
->irq
;
2629 for (i
=0; i
<HIFN_D_RES_RSIZE
; ++i
)
2632 pci_set_drvdata(pdev
, dev
);
2634 tasklet_init(&dev
->tasklet
, hifn_tasklet_callback
, (unsigned long)dev
);
2636 crypto_init_queue(&dev
->queue
, 1);
2638 err
= request_irq(dev
->irq
, hifn_interrupt
, IRQF_SHARED
, dev
->name
, dev
);
2640 dprintk("Failed to request IRQ%d: err: %d.\n", dev
->irq
, err
);
2642 goto err_out_free_desc
;
2645 err
= hifn_start_device(dev
);
2647 goto err_out_free_irq
;
2649 err
= hifn_test(dev
, 1, 0);
2651 goto err_out_stop_device
;
2653 err
= hifn_register_rng(dev
);
2655 goto err_out_stop_device
;
2657 err
= hifn_register_alg(dev
);
2659 goto err_out_unregister_rng
;
2661 INIT_DELAYED_WORK(&dev
->work
, hifn_work
);
2662 schedule_delayed_work(&dev
->work
, HZ
);
2664 dprintk("HIFN crypto accelerator card at %s has been "
2665 "successfully registered as %s.\n",
2666 pci_name(pdev
), dev
->name
);
2670 err_out_unregister_rng
:
2671 hifn_unregister_rng(dev
);
2672 err_out_stop_device
:
2673 hifn_reset_dma(dev
, 1);
2674 hifn_stop_device(dev
);
2676 free_irq(dev
->irq
, dev
);
2677 tasklet_kill(&dev
->tasklet
);
2679 pci_free_consistent(pdev
, sizeof(struct hifn_dma
),
2680 dev
->desc_virt
, dev
->desc_dma
);
2685 iounmap(dev
->bar
[i
]);
2687 err_out_free_regions
:
2688 pci_release_regions(pdev
);
2690 err_out_disable_pci_device
:
2691 pci_disable_device(pdev
);
2696 static void hifn_remove(struct pci_dev
*pdev
)
2699 struct hifn_device
*dev
;
2701 dev
= pci_get_drvdata(pdev
);
2704 cancel_delayed_work_sync(&dev
->work
);
2706 hifn_unregister_rng(dev
);
2707 hifn_unregister_alg(dev
);
2708 hifn_reset_dma(dev
, 1);
2709 hifn_stop_device(dev
);
2711 free_irq(dev
->irq
, dev
);
2712 tasklet_kill(&dev
->tasklet
);
2716 pci_free_consistent(pdev
, sizeof(struct hifn_dma
),
2717 dev
->desc_virt
, dev
->desc_dma
);
2720 iounmap(dev
->bar
[i
]);
2725 pci_release_regions(pdev
);
2726 pci_disable_device(pdev
);
2729 static struct pci_device_id hifn_pci_tbl
[] = {
2730 { PCI_DEVICE(PCI_VENDOR_ID_HIFN
, PCI_DEVICE_ID_HIFN_7955
) },
2731 { PCI_DEVICE(PCI_VENDOR_ID_HIFN
, PCI_DEVICE_ID_HIFN_7956
) },
2734 MODULE_DEVICE_TABLE(pci
, hifn_pci_tbl
);
2736 static struct pci_driver hifn_pci_driver
= {
2738 .id_table
= hifn_pci_tbl
,
2739 .probe
= hifn_probe
,
2740 .remove
= hifn_remove
,
2743 static int __init
hifn_init(void)
2748 /* HIFN supports only 32-bit addresses */
2749 BUILD_BUG_ON(sizeof(dma_addr_t
) != 4);
2751 if (strncmp(hifn_pll_ref
, "ext", 3) &&
2752 strncmp(hifn_pll_ref
, "pci", 3)) {
2753 printk(KERN_ERR
"hifn795x: invalid hifn_pll_ref clock, "
2754 "must be pci or ext");
2759 * For the 7955/7956 the reference clock frequency must be in the
2760 * range of 20MHz-100MHz. For the 7954 the upper bound is 66.67MHz,
2761 * but this chip is currently not supported.
2763 if (hifn_pll_ref
[3] != '\0') {
2764 freq
= simple_strtoul(hifn_pll_ref
+ 3, NULL
, 10);
2765 if (freq
< 20 || freq
> 100) {
2766 printk(KERN_ERR
"hifn795x: invalid hifn_pll_ref "
2767 "frequency, must be in the range "
2773 err
= pci_register_driver(&hifn_pci_driver
);
2775 dprintk("Failed to register PCI driver for %s device.\n",
2776 hifn_pci_driver
.name
);
2780 printk(KERN_INFO
"Driver for HIFN 795x crypto accelerator chip "
2781 "has been successfully registered.\n");
2786 static void __exit
hifn_fini(void)
2788 pci_unregister_driver(&hifn_pci_driver
);
2790 printk(KERN_INFO
"Driver for HIFN 795x crypto accelerator chip "
2791 "has been successfully unregistered.\n");
2794 module_init(hifn_init
);
2795 module_exit(hifn_fini
);
2797 MODULE_LICENSE("GPL");
2798 MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
2799 MODULE_DESCRIPTION("Driver for HIFN 795x crypto accelerator chip.");