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>
39 static char hifn_pll_ref[sizeof("extNNN")] = "ext";
40 module_param_string(hifn_pll_ref, hifn_pll_ref, sizeof(hifn_pll_ref), 0444);
41 MODULE_PARM_DESC(hifn_pll_ref,
42 "PLL reference clock (pci[freq] or ext[freq], default ext)");
44 static atomic_t hifn_dev_number;
46 #define ACRYPTO_OP_DECRYPT 0
47 #define ACRYPTO_OP_ENCRYPT 1
48 #define ACRYPTO_OP_HMAC 2
49 #define ACRYPTO_OP_RNG 3
51 #define ACRYPTO_MODE_ECB 0
52 #define ACRYPTO_MODE_CBC 1
53 #define ACRYPTO_MODE_CFB 2
54 #define ACRYPTO_MODE_OFB 3
56 #define ACRYPTO_TYPE_AES_128 0
57 #define ACRYPTO_TYPE_AES_192 1
58 #define ACRYPTO_TYPE_AES_256 2
59 #define ACRYPTO_TYPE_3DES 3
60 #define ACRYPTO_TYPE_DES 4
62 #define PCI_VENDOR_ID_HIFN 0x13A3
63 #define PCI_DEVICE_ID_HIFN_7955 0x0020
64 #define PCI_DEVICE_ID_HIFN_7956 0x001d
66 /* I/O region sizes */
68 #define HIFN_BAR0_SIZE 0x1000
69 #define HIFN_BAR1_SIZE 0x2000
70 #define HIFN_BAR2_SIZE 0x8000
74 #define HIFN_DMA_CRA 0x0C /* DMA Command Ring Address */
75 #define HIFN_DMA_SDRA 0x1C /* DMA Source Data Ring Address */
76 #define HIFN_DMA_RRA 0x2C /* DMA Result Ring Address */
77 #define HIFN_DMA_DDRA 0x3C /* DMA Destination Data Ring Address */
78 #define HIFN_DMA_STCTL 0x40 /* DMA Status and Control */
79 #define HIFN_DMA_INTREN 0x44 /* DMA Interrupt Enable */
80 #define HIFN_DMA_CFG1 0x48 /* DMA Configuration #1 */
81 #define HIFN_DMA_CFG2 0x6C /* DMA Configuration #2 */
82 #define HIFN_CHIP_ID 0x98 /* Chip ID */
85 * Processing Unit Registers (offset from BASEREG0)
87 #define HIFN_0_PUDATA 0x00 /* Processing Unit Data */
88 #define HIFN_0_PUCTRL 0x04 /* Processing Unit Control */
89 #define HIFN_0_PUISR 0x08 /* Processing Unit Interrupt Status */
90 #define HIFN_0_PUCNFG 0x0c /* Processing Unit Configuration */
91 #define HIFN_0_PUIER 0x10 /* Processing Unit Interrupt Enable */
92 #define HIFN_0_PUSTAT 0x14 /* Processing Unit Status/Chip ID */
93 #define HIFN_0_FIFOSTAT 0x18 /* FIFO Status */
94 #define HIFN_0_FIFOCNFG 0x1c /* FIFO Configuration */
95 #define HIFN_0_SPACESIZE 0x20 /* Register space size */
97 /* Processing Unit Control Register (HIFN_0_PUCTRL) */
98 #define HIFN_PUCTRL_CLRSRCFIFO 0x0010 /* clear source fifo */
99 #define HIFN_PUCTRL_STOP 0x0008 /* stop pu */
100 #define HIFN_PUCTRL_LOCKRAM 0x0004 /* lock ram */
101 #define HIFN_PUCTRL_DMAENA 0x0002 /* enable dma */
102 #define HIFN_PUCTRL_RESET 0x0001 /* Reset processing unit */
104 /* Processing Unit Interrupt Status Register (HIFN_0_PUISR) */
105 #define HIFN_PUISR_CMDINVAL 0x8000 /* Invalid command interrupt */
106 #define HIFN_PUISR_DATAERR 0x4000 /* Data error interrupt */
107 #define HIFN_PUISR_SRCFIFO 0x2000 /* Source FIFO ready interrupt */
108 #define HIFN_PUISR_DSTFIFO 0x1000 /* Destination FIFO ready interrupt */
109 #define HIFN_PUISR_DSTOVER 0x0200 /* Destination overrun interrupt */
110 #define HIFN_PUISR_SRCCMD 0x0080 /* Source command interrupt */
111 #define HIFN_PUISR_SRCCTX 0x0040 /* Source context interrupt */
112 #define HIFN_PUISR_SRCDATA 0x0020 /* Source data interrupt */
113 #define HIFN_PUISR_DSTDATA 0x0010 /* Destination data interrupt */
114 #define HIFN_PUISR_DSTRESULT 0x0004 /* Destination result interrupt */
116 /* Processing Unit Configuration Register (HIFN_0_PUCNFG) */
117 #define HIFN_PUCNFG_DRAMMASK 0xe000 /* DRAM size mask */
118 #define HIFN_PUCNFG_DSZ_256K 0x0000 /* 256k dram */
119 #define HIFN_PUCNFG_DSZ_512K 0x2000 /* 512k dram */
120 #define HIFN_PUCNFG_DSZ_1M 0x4000 /* 1m dram */
121 #define HIFN_PUCNFG_DSZ_2M 0x6000 /* 2m dram */
122 #define HIFN_PUCNFG_DSZ_4M 0x8000 /* 4m dram */
123 #define HIFN_PUCNFG_DSZ_8M 0xa000 /* 8m dram */
124 #define HIFN_PUNCFG_DSZ_16M 0xc000 /* 16m dram */
125 #define HIFN_PUCNFG_DSZ_32M 0xe000 /* 32m dram */
126 #define HIFN_PUCNFG_DRAMREFRESH 0x1800 /* DRAM refresh rate mask */
127 #define HIFN_PUCNFG_DRFR_512 0x0000 /* 512 divisor of ECLK */
128 #define HIFN_PUCNFG_DRFR_256 0x0800 /* 256 divisor of ECLK */
129 #define HIFN_PUCNFG_DRFR_128 0x1000 /* 128 divisor of ECLK */
130 #define HIFN_PUCNFG_TCALLPHASES 0x0200 /* your guess is as good as mine... */
131 #define HIFN_PUCNFG_TCDRVTOTEM 0x0100 /* your guess is as good as mine... */
132 #define HIFN_PUCNFG_BIGENDIAN 0x0080 /* DMA big endian mode */
133 #define HIFN_PUCNFG_BUS32 0x0040 /* Bus width 32bits */
134 #define HIFN_PUCNFG_BUS16 0x0000 /* Bus width 16 bits */
135 #define HIFN_PUCNFG_CHIPID 0x0020 /* Allow chipid from PUSTAT */
136 #define HIFN_PUCNFG_DRAM 0x0010 /* Context RAM is DRAM */
137 #define HIFN_PUCNFG_SRAM 0x0000 /* Context RAM is SRAM */
138 #define HIFN_PUCNFG_COMPSING 0x0004 /* Enable single compression context */
139 #define HIFN_PUCNFG_ENCCNFG 0x0002 /* Encryption configuration */
141 /* Processing Unit Interrupt Enable Register (HIFN_0_PUIER) */
142 #define HIFN_PUIER_CMDINVAL 0x8000 /* Invalid command interrupt */
143 #define HIFN_PUIER_DATAERR 0x4000 /* Data error interrupt */
144 #define HIFN_PUIER_SRCFIFO 0x2000 /* Source FIFO ready interrupt */
145 #define HIFN_PUIER_DSTFIFO 0x1000 /* Destination FIFO ready interrupt */
146 #define HIFN_PUIER_DSTOVER 0x0200 /* Destination overrun interrupt */
147 #define HIFN_PUIER_SRCCMD 0x0080 /* Source command interrupt */
148 #define HIFN_PUIER_SRCCTX 0x0040 /* Source context interrupt */
149 #define HIFN_PUIER_SRCDATA 0x0020 /* Source data interrupt */
150 #define HIFN_PUIER_DSTDATA 0x0010 /* Destination data interrupt */
151 #define HIFN_PUIER_DSTRESULT 0x0004 /* Destination result interrupt */
153 /* Processing Unit Status Register/Chip ID (HIFN_0_PUSTAT) */
154 #define HIFN_PUSTAT_CMDINVAL 0x8000 /* Invalid command interrupt */
155 #define HIFN_PUSTAT_DATAERR 0x4000 /* Data error interrupt */
156 #define HIFN_PUSTAT_SRCFIFO 0x2000 /* Source FIFO ready interrupt */
157 #define HIFN_PUSTAT_DSTFIFO 0x1000 /* Destination FIFO ready interrupt */
158 #define HIFN_PUSTAT_DSTOVER 0x0200 /* Destination overrun interrupt */
159 #define HIFN_PUSTAT_SRCCMD 0x0080 /* Source command interrupt */
160 #define HIFN_PUSTAT_SRCCTX 0x0040 /* Source context interrupt */
161 #define HIFN_PUSTAT_SRCDATA 0x0020 /* Source data interrupt */
162 #define HIFN_PUSTAT_DSTDATA 0x0010 /* Destination data interrupt */
163 #define HIFN_PUSTAT_DSTRESULT 0x0004 /* Destination result interrupt */
164 #define HIFN_PUSTAT_CHIPREV 0x00ff /* Chip revision mask */
165 #define HIFN_PUSTAT_CHIPENA 0xff00 /* Chip enabled mask */
166 #define HIFN_PUSTAT_ENA_2 0x1100 /* Level 2 enabled */
167 #define HIFN_PUSTAT_ENA_1 0x1000 /* Level 1 enabled */
168 #define HIFN_PUSTAT_ENA_0 0x3000 /* Level 0 enabled */
169 #define HIFN_PUSTAT_REV_2 0x0020 /* 7751 PT6/2 */
170 #define HIFN_PUSTAT_REV_3 0x0030 /* 7751 PT6/3 */
172 /* FIFO Status Register (HIFN_0_FIFOSTAT) */
173 #define HIFN_FIFOSTAT_SRC 0x7f00 /* Source FIFO available */
174 #define HIFN_FIFOSTAT_DST 0x007f /* Destination FIFO available */
176 /* FIFO Configuration Register (HIFN_0_FIFOCNFG) */
177 #define HIFN_FIFOCNFG_THRESHOLD 0x0400 /* must be written as 1 */
180 * DMA Interface Registers (offset from BASEREG1)
182 #define HIFN_1_DMA_CRAR 0x0c /* DMA Command Ring Address */
183 #define HIFN_1_DMA_SRAR 0x1c /* DMA Source Ring Address */
184 #define HIFN_1_DMA_RRAR 0x2c /* DMA Result Ring Address */
185 #define HIFN_1_DMA_DRAR 0x3c /* DMA Destination Ring Address */
186 #define HIFN_1_DMA_CSR 0x40 /* DMA Status and Control */
187 #define HIFN_1_DMA_IER 0x44 /* DMA Interrupt Enable */
188 #define HIFN_1_DMA_CNFG 0x48 /* DMA Configuration */
189 #define HIFN_1_PLL 0x4c /* 795x: PLL config */
190 #define HIFN_1_7811_RNGENA 0x60 /* 7811: rng enable */
191 #define HIFN_1_7811_RNGCFG 0x64 /* 7811: rng config */
192 #define HIFN_1_7811_RNGDAT 0x68 /* 7811: rng data */
193 #define HIFN_1_7811_RNGSTS 0x6c /* 7811: rng status */
194 #define HIFN_1_7811_MIPSRST 0x94 /* 7811: MIPS reset */
195 #define HIFN_1_REVID 0x98 /* Revision ID */
196 #define HIFN_1_UNLOCK_SECRET1 0xf4
197 #define HIFN_1_UNLOCK_SECRET2 0xfc
198 #define HIFN_1_PUB_RESET 0x204 /* Public/RNG Reset */
199 #define HIFN_1_PUB_BASE 0x300 /* Public Base Address */
200 #define HIFN_1_PUB_OPLEN 0x304 /* Public Operand Length */
201 #define HIFN_1_PUB_OP 0x308 /* Public Operand */
202 #define HIFN_1_PUB_STATUS 0x30c /* Public Status */
203 #define HIFN_1_PUB_IEN 0x310 /* Public Interrupt enable */
204 #define HIFN_1_RNG_CONFIG 0x314 /* RNG config */
205 #define HIFN_1_RNG_DATA 0x318 /* RNG data */
206 #define HIFN_1_PUB_MEM 0x400 /* start of Public key memory */
207 #define HIFN_1_PUB_MEMEND 0xbff /* end of Public key memory */
209 /* DMA Status and Control Register (HIFN_1_DMA_CSR) */
210 #define HIFN_DMACSR_D_CTRLMASK 0xc0000000 /* Destinition Ring Control */
211 #define HIFN_DMACSR_D_CTRL_NOP 0x00000000 /* Dest. Control: no-op */
212 #define HIFN_DMACSR_D_CTRL_DIS 0x40000000 /* Dest. Control: disable */
213 #define HIFN_DMACSR_D_CTRL_ENA 0x80000000 /* Dest. Control: enable */
214 #define HIFN_DMACSR_D_ABORT 0x20000000 /* Destinition Ring PCIAbort */
215 #define HIFN_DMACSR_D_DONE 0x10000000 /* Destinition Ring Done */
216 #define HIFN_DMACSR_D_LAST 0x08000000 /* Destinition Ring Last */
217 #define HIFN_DMACSR_D_WAIT 0x04000000 /* Destinition Ring Waiting */
218 #define HIFN_DMACSR_D_OVER 0x02000000 /* Destinition Ring Overflow */
219 #define HIFN_DMACSR_R_CTRL 0x00c00000 /* Result Ring Control */
220 #define HIFN_DMACSR_R_CTRL_NOP 0x00000000 /* Result Control: no-op */
221 #define HIFN_DMACSR_R_CTRL_DIS 0x00400000 /* Result Control: disable */
222 #define HIFN_DMACSR_R_CTRL_ENA 0x00800000 /* Result Control: enable */
223 #define HIFN_DMACSR_R_ABORT 0x00200000 /* Result Ring PCI Abort */
224 #define HIFN_DMACSR_R_DONE 0x00100000 /* Result Ring Done */
225 #define HIFN_DMACSR_R_LAST 0x00080000 /* Result Ring Last */
226 #define HIFN_DMACSR_R_WAIT 0x00040000 /* Result Ring Waiting */
227 #define HIFN_DMACSR_R_OVER 0x00020000 /* Result Ring Overflow */
228 #define HIFN_DMACSR_S_CTRL 0x0000c000 /* Source Ring Control */
229 #define HIFN_DMACSR_S_CTRL_NOP 0x00000000 /* Source Control: no-op */
230 #define HIFN_DMACSR_S_CTRL_DIS 0x00004000 /* Source Control: disable */
231 #define HIFN_DMACSR_S_CTRL_ENA 0x00008000 /* Source Control: enable */
232 #define HIFN_DMACSR_S_ABORT 0x00002000 /* Source Ring PCI Abort */
233 #define HIFN_DMACSR_S_DONE 0x00001000 /* Source Ring Done */
234 #define HIFN_DMACSR_S_LAST 0x00000800 /* Source Ring Last */
235 #define HIFN_DMACSR_S_WAIT 0x00000400 /* Source Ring Waiting */
236 #define HIFN_DMACSR_ILLW 0x00000200 /* Illegal write (7811 only) */
237 #define HIFN_DMACSR_ILLR 0x00000100 /* Illegal read (7811 only) */
238 #define HIFN_DMACSR_C_CTRL 0x000000c0 /* Command Ring Control */
239 #define HIFN_DMACSR_C_CTRL_NOP 0x00000000 /* Command Control: no-op */
240 #define HIFN_DMACSR_C_CTRL_DIS 0x00000040 /* Command Control: disable */
241 #define HIFN_DMACSR_C_CTRL_ENA 0x00000080 /* Command Control: enable */
242 #define HIFN_DMACSR_C_ABORT 0x00000020 /* Command Ring PCI Abort */
243 #define HIFN_DMACSR_C_DONE 0x00000010 /* Command Ring Done */
244 #define HIFN_DMACSR_C_LAST 0x00000008 /* Command Ring Last */
245 #define HIFN_DMACSR_C_WAIT 0x00000004 /* Command Ring Waiting */
246 #define HIFN_DMACSR_PUBDONE 0x00000002 /* Public op done (7951 only) */
247 #define HIFN_DMACSR_ENGINE 0x00000001 /* Command Ring Engine IRQ */
249 /* DMA Interrupt Enable Register (HIFN_1_DMA_IER) */
250 #define HIFN_DMAIER_D_ABORT 0x20000000 /* Destination Ring PCIAbort */
251 #define HIFN_DMAIER_D_DONE 0x10000000 /* Destination Ring Done */
252 #define HIFN_DMAIER_D_LAST 0x08000000 /* Destination Ring Last */
253 #define HIFN_DMAIER_D_WAIT 0x04000000 /* Destination Ring Waiting */
254 #define HIFN_DMAIER_D_OVER 0x02000000 /* Destination Ring Overflow */
255 #define HIFN_DMAIER_R_ABORT 0x00200000 /* Result Ring PCI Abort */
256 #define HIFN_DMAIER_R_DONE 0x00100000 /* Result Ring Done */
257 #define HIFN_DMAIER_R_LAST 0x00080000 /* Result Ring Last */
258 #define HIFN_DMAIER_R_WAIT 0x00040000 /* Result Ring Waiting */
259 #define HIFN_DMAIER_R_OVER 0x00020000 /* Result Ring Overflow */
260 #define HIFN_DMAIER_S_ABORT 0x00002000 /* Source Ring PCI Abort */
261 #define HIFN_DMAIER_S_DONE 0x00001000 /* Source Ring Done */
262 #define HIFN_DMAIER_S_LAST 0x00000800 /* Source Ring Last */
263 #define HIFN_DMAIER_S_WAIT 0x00000400 /* Source Ring Waiting */
264 #define HIFN_DMAIER_ILLW 0x00000200 /* Illegal write (7811 only) */
265 #define HIFN_DMAIER_ILLR 0x00000100 /* Illegal read (7811 only) */
266 #define HIFN_DMAIER_C_ABORT 0x00000020 /* Command Ring PCI Abort */
267 #define HIFN_DMAIER_C_DONE 0x00000010 /* Command Ring Done */
268 #define HIFN_DMAIER_C_LAST 0x00000008 /* Command Ring Last */
269 #define HIFN_DMAIER_C_WAIT 0x00000004 /* Command Ring Waiting */
270 #define HIFN_DMAIER_PUBDONE 0x00000002 /* public op done (7951 only) */
271 #define HIFN_DMAIER_ENGINE 0x00000001 /* Engine IRQ */
273 /* DMA Configuration Register (HIFN_1_DMA_CNFG) */
274 #define HIFN_DMACNFG_BIGENDIAN 0x10000000 /* big endian mode */
275 #define HIFN_DMACNFG_POLLFREQ 0x00ff0000 /* Poll frequency mask */
276 #define HIFN_DMACNFG_UNLOCK 0x00000800
277 #define HIFN_DMACNFG_POLLINVAL 0x00000700 /* Invalid Poll Scalar */
278 #define HIFN_DMACNFG_LAST 0x00000010 /* Host control LAST bit */
279 #define HIFN_DMACNFG_MODE 0x00000004 /* DMA mode */
280 #define HIFN_DMACNFG_DMARESET 0x00000002 /* DMA Reset # */
281 #define HIFN_DMACNFG_MSTRESET 0x00000001 /* Master Reset # */
283 /* PLL configuration register */
284 #define HIFN_PLL_REF_CLK_HBI 0x00000000 /* HBI reference clock */
285 #define HIFN_PLL_REF_CLK_PLL 0x00000001 /* PLL reference clock */
286 #define HIFN_PLL_BP 0x00000002 /* Reference clock bypass */
287 #define HIFN_PLL_PK_CLK_HBI 0x00000000 /* PK engine HBI clock */
288 #define HIFN_PLL_PK_CLK_PLL 0x00000008 /* PK engine PLL clock */
289 #define HIFN_PLL_PE_CLK_HBI 0x00000000 /* PE engine HBI clock */
290 #define HIFN_PLL_PE_CLK_PLL 0x00000010 /* PE engine PLL clock */
291 #define HIFN_PLL_RESERVED_1 0x00000400 /* Reserved bit, must be 1 */
292 #define HIFN_PLL_ND_SHIFT 11 /* Clock multiplier shift */
293 #define HIFN_PLL_ND_MULT_2 0x00000000 /* PLL clock multiplier 2 */
294 #define HIFN_PLL_ND_MULT_4 0x00000800 /* PLL clock multiplier 4 */
295 #define HIFN_PLL_ND_MULT_6 0x00001000 /* PLL clock multiplier 6 */
296 #define HIFN_PLL_ND_MULT_8 0x00001800 /* PLL clock multiplier 8 */
297 #define HIFN_PLL_ND_MULT_10 0x00002000 /* PLL clock multiplier 10 */
298 #define HIFN_PLL_ND_MULT_12 0x00002800 /* PLL clock multiplier 12 */
299 #define HIFN_PLL_IS_1_8 0x00000000 /* charge pump (mult. 1-8) */
300 #define HIFN_PLL_IS_9_12 0x00010000 /* charge pump (mult. 9-12) */
302 #define HIFN_PLL_FCK_MAX 266 /* Maximum PLL frequency */
304 /* Public key reset register (HIFN_1_PUB_RESET) */
305 #define HIFN_PUBRST_RESET 0x00000001 /* reset public/rng unit */
307 /* Public base address register (HIFN_1_PUB_BASE) */
308 #define HIFN_PUBBASE_ADDR 0x00003fff /* base address */
310 /* Public operand length register (HIFN_1_PUB_OPLEN) */
311 #define HIFN_PUBOPLEN_MOD_M 0x0000007f /* modulus length mask */
312 #define HIFN_PUBOPLEN_MOD_S 0 /* modulus length shift */
313 #define HIFN_PUBOPLEN_EXP_M 0x0003ff80 /* exponent length mask */
314 #define HIFN_PUBOPLEN_EXP_S 7 /* exponent length shift */
315 #define HIFN_PUBOPLEN_RED_M 0x003c0000 /* reducend length mask */
316 #define HIFN_PUBOPLEN_RED_S 18 /* reducend length shift */
318 /* Public operation register (HIFN_1_PUB_OP) */
319 #define HIFN_PUBOP_AOFFSET_M 0x0000007f /* A offset mask */
320 #define HIFN_PUBOP_AOFFSET_S 0 /* A offset shift */
321 #define HIFN_PUBOP_BOFFSET_M 0x00000f80 /* B offset mask */
322 #define HIFN_PUBOP_BOFFSET_S 7 /* B offset shift */
323 #define HIFN_PUBOP_MOFFSET_M 0x0003f000 /* M offset mask */
324 #define HIFN_PUBOP_MOFFSET_S 12 /* M offset shift */
325 #define HIFN_PUBOP_OP_MASK 0x003c0000 /* Opcode: */
326 #define HIFN_PUBOP_OP_NOP 0x00000000 /* NOP */
327 #define HIFN_PUBOP_OP_ADD 0x00040000 /* ADD */
328 #define HIFN_PUBOP_OP_ADDC 0x00080000 /* ADD w/carry */
329 #define HIFN_PUBOP_OP_SUB 0x000c0000 /* SUB */
330 #define HIFN_PUBOP_OP_SUBC 0x00100000 /* SUB w/carry */
331 #define HIFN_PUBOP_OP_MODADD 0x00140000 /* Modular ADD */
332 #define HIFN_PUBOP_OP_MODSUB 0x00180000 /* Modular SUB */
333 #define HIFN_PUBOP_OP_INCA 0x001c0000 /* INC A */
334 #define HIFN_PUBOP_OP_DECA 0x00200000 /* DEC A */
335 #define HIFN_PUBOP_OP_MULT 0x00240000 /* MULT */
336 #define HIFN_PUBOP_OP_MODMULT 0x00280000 /* Modular MULT */
337 #define HIFN_PUBOP_OP_MODRED 0x002c0000 /* Modular RED */
338 #define HIFN_PUBOP_OP_MODEXP 0x00300000 /* Modular EXP */
340 /* Public status register (HIFN_1_PUB_STATUS) */
341 #define HIFN_PUBSTS_DONE 0x00000001 /* operation done */
342 #define HIFN_PUBSTS_CARRY 0x00000002 /* carry */
344 /* Public interrupt enable register (HIFN_1_PUB_IEN) */
345 #define HIFN_PUBIEN_DONE 0x00000001 /* operation done interrupt */
347 /* Random number generator config register (HIFN_1_RNG_CONFIG) */
348 #define HIFN_RNGCFG_ENA 0x00000001 /* enable rng */
350 #define HIFN_NAMESIZE 32
351 #define HIFN_MAX_RESULT_ORDER 5
353 #define HIFN_D_CMD_RSIZE 24*1
354 #define HIFN_D_SRC_RSIZE 80*1
355 #define HIFN_D_DST_RSIZE 80*1
356 #define HIFN_D_RES_RSIZE 24*1
358 #define HIFN_D_DST_DALIGN 4
360 #define HIFN_QUEUE_LENGTH (HIFN_D_CMD_RSIZE - 1)
362 #define AES_MIN_KEY_SIZE 16
363 #define AES_MAX_KEY_SIZE 32
365 #define HIFN_DES_KEY_LENGTH 8
366 #define HIFN_3DES_KEY_LENGTH 24
367 #define HIFN_MAX_CRYPT_KEY_LENGTH AES_MAX_KEY_SIZE
368 #define HIFN_IV_LENGTH 8
369 #define HIFN_AES_IV_LENGTH 16
370 #define HIFN_MAX_IV_LENGTH HIFN_AES_IV_LENGTH
372 #define HIFN_MAC_KEY_LENGTH 64
373 #define HIFN_MD5_LENGTH 16
374 #define HIFN_SHA1_LENGTH 20
375 #define HIFN_MAC_TRUNC_LENGTH 12
377 #define HIFN_MAX_COMMAND (8 + 8 + 8 + 64 + 260)
378 #define HIFN_MAX_RESULT (8 + 4 + 4 + 20 + 4)
379 #define HIFN_USED_RESULT 12
388 struct hifn_desc cmdr[HIFN_D_CMD_RSIZE+1];
389 struct hifn_desc srcr[HIFN_D_SRC_RSIZE+1];
390 struct hifn_desc dstr[HIFN_D_DST_RSIZE+1];
391 struct hifn_desc resr[HIFN_D_RES_RSIZE+1];
393 u8 command_bufs[HIFN_D_CMD_RSIZE][HIFN_MAX_COMMAND];
394 u8 result_bufs[HIFN_D_CMD_RSIZE][HIFN_MAX_RESULT];
397 * Our current positions for insertion and removal from the descriptor
400 volatile int cmdi, srci, dsti, resi;
401 volatile int cmdu, srcu, dstu, resu;
402 int cmdk, srck, dstk, resk;
405 #define HIFN_FLAG_CMD_BUSY (1<<0)
406 #define HIFN_FLAG_SRC_BUSY (1<<1)
407 #define HIFN_FLAG_DST_BUSY (1<<2)
408 #define HIFN_FLAG_RES_BUSY (1<<3)
409 #define HIFN_FLAG_OLD_KEY (1<<4)
411 #define HIFN_DEFAULT_ACTIVE_NUM 5
415 char name[HIFN_NAMESIZE];
419 struct pci_dev *pdev;
420 void __iomem *bar[3];
427 void *sa[HIFN_D_RES_RSIZE];
433 struct delayed_work work;
435 unsigned long success;
436 unsigned long prev_success;
440 struct tasklet_struct tasklet;
442 struct crypto_queue queue;
443 struct list_head alg_list;
445 unsigned int pk_clk_freq;
447 #ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
448 unsigned int rng_wait_time;
454 #define HIFN_D_LENGTH 0x0000ffff
455 #define HIFN_D_NOINVALID 0x01000000
456 #define HIFN_D_MASKDONEIRQ 0x02000000
457 #define HIFN_D_DESTOVER 0x04000000
458 #define HIFN_D_OVER 0x08000000
459 #define HIFN_D_LAST 0x20000000
460 #define HIFN_D_JUMP 0x40000000
461 #define HIFN_D_VALID 0x80000000
463 struct hifn_base_command
465 volatile __le16 masks;
466 volatile __le16 session_num;
467 volatile __le16 total_source_count;
468 volatile __le16 total_dest_count;
471 #define HIFN_BASE_CMD_COMP 0x0100 /* enable compression engine */
472 #define HIFN_BASE_CMD_PAD 0x0200 /* enable padding engine */
473 #define HIFN_BASE_CMD_MAC 0x0400 /* enable MAC engine */
474 #define HIFN_BASE_CMD_CRYPT 0x0800 /* enable crypt engine */
475 #define HIFN_BASE_CMD_DECODE 0x2000
476 #define HIFN_BASE_CMD_SRCLEN_M 0xc000
477 #define HIFN_BASE_CMD_SRCLEN_S 14
478 #define HIFN_BASE_CMD_DSTLEN_M 0x3000
479 #define HIFN_BASE_CMD_DSTLEN_S 12
480 #define HIFN_BASE_CMD_LENMASK_HI 0x30000
481 #define HIFN_BASE_CMD_LENMASK_LO 0x0ffff
484 * Structure to help build up the command data structure.
486 struct hifn_crypt_command
488 volatile __le16 masks;
489 volatile __le16 header_skip;
490 volatile __le16 source_count;
491 volatile __le16 reserved;
494 #define HIFN_CRYPT_CMD_ALG_MASK 0x0003 /* algorithm: */
495 #define HIFN_CRYPT_CMD_ALG_DES 0x0000 /* DES */
496 #define HIFN_CRYPT_CMD_ALG_3DES 0x0001 /* 3DES */
497 #define HIFN_CRYPT_CMD_ALG_RC4 0x0002 /* RC4 */
498 #define HIFN_CRYPT_CMD_ALG_AES 0x0003 /* AES */
499 #define HIFN_CRYPT_CMD_MODE_MASK 0x0018 /* Encrypt mode: */
500 #define HIFN_CRYPT_CMD_MODE_ECB 0x0000 /* ECB */
501 #define HIFN_CRYPT_CMD_MODE_CBC 0x0008 /* CBC */
502 #define HIFN_CRYPT_CMD_MODE_CFB 0x0010 /* CFB */
503 #define HIFN_CRYPT_CMD_MODE_OFB 0x0018 /* OFB */
504 #define HIFN_CRYPT_CMD_CLR_CTX 0x0040 /* clear context */
505 #define HIFN_CRYPT_CMD_KSZ_MASK 0x0600 /* AES key size: */
506 #define HIFN_CRYPT_CMD_KSZ_128 0x0000 /* 128 bit */
507 #define HIFN_CRYPT_CMD_KSZ_192 0x0200 /* 192 bit */
508 #define HIFN_CRYPT_CMD_KSZ_256 0x0400 /* 256 bit */
509 #define HIFN_CRYPT_CMD_NEW_KEY 0x0800 /* expect new key */
510 #define HIFN_CRYPT_CMD_NEW_IV 0x1000 /* expect new iv */
511 #define HIFN_CRYPT_CMD_SRCLEN_M 0xc000
512 #define HIFN_CRYPT_CMD_SRCLEN_S 14
515 * Structure to help build up the command data structure.
517 struct hifn_mac_command
519 volatile __le16 masks;
520 volatile __le16 header_skip;
521 volatile __le16 source_count;
522 volatile __le16 reserved;
525 #define HIFN_MAC_CMD_ALG_MASK 0x0001
526 #define HIFN_MAC_CMD_ALG_SHA1 0x0000
527 #define HIFN_MAC_CMD_ALG_MD5 0x0001
528 #define HIFN_MAC_CMD_MODE_MASK 0x000c
529 #define HIFN_MAC_CMD_MODE_HMAC 0x0000
530 #define HIFN_MAC_CMD_MODE_SSL_MAC 0x0004
531 #define HIFN_MAC_CMD_MODE_HASH 0x0008
532 #define HIFN_MAC_CMD_MODE_FULL 0x0004
533 #define HIFN_MAC_CMD_TRUNC 0x0010
534 #define HIFN_MAC_CMD_RESULT 0x0020
535 #define HIFN_MAC_CMD_APPEND 0x0040
536 #define HIFN_MAC_CMD_SRCLEN_M 0xc000
537 #define HIFN_MAC_CMD_SRCLEN_S 14
540 * MAC POS IPsec initiates authentication after encryption on encodes
541 * and before decryption on decodes.
543 #define HIFN_MAC_CMD_POS_IPSEC 0x0200
544 #define HIFN_MAC_CMD_NEW_KEY 0x0800
546 struct hifn_comp_command
548 volatile __le16 masks;
549 volatile __le16 header_skip;
550 volatile __le16 source_count;
551 volatile __le16 reserved;
554 #define HIFN_COMP_CMD_SRCLEN_M 0xc000
555 #define HIFN_COMP_CMD_SRCLEN_S 14
556 #define HIFN_COMP_CMD_ONE 0x0100 /* must be one */
557 #define HIFN_COMP_CMD_CLEARHIST 0x0010 /* clear history */
558 #define HIFN_COMP_CMD_UPDATEHIST 0x0008 /* update history */
559 #define HIFN_COMP_CMD_LZS_STRIP0 0x0004 /* LZS: strip zero */
560 #define HIFN_COMP_CMD_MPPC_RESTART 0x0004 /* MPPC: restart */
561 #define HIFN_COMP_CMD_ALG_MASK 0x0001 /* compression mode: */
562 #define HIFN_COMP_CMD_ALG_MPPC 0x0001 /* MPPC */
563 #define HIFN_COMP_CMD_ALG_LZS 0x0000 /* LZS */
565 struct hifn_base_result
567 volatile __le16 flags;
568 volatile __le16 session;
569 volatile __le16 src_cnt; /* 15:0 of source count */
570 volatile __le16 dst_cnt; /* 15:0 of dest count */
573 #define HIFN_BASE_RES_DSTOVERRUN 0x0200 /* destination overrun */
574 #define HIFN_BASE_RES_SRCLEN_M 0xc000 /* 17:16 of source count */
575 #define HIFN_BASE_RES_SRCLEN_S 14
576 #define HIFN_BASE_RES_DSTLEN_M 0x3000 /* 17:16 of dest count */
577 #define HIFN_BASE_RES_DSTLEN_S 12
579 struct hifn_comp_result
581 volatile __le16 flags;
585 #define HIFN_COMP_RES_LCB_M 0xff00 /* longitudinal check byte */
586 #define HIFN_COMP_RES_LCB_S 8
587 #define HIFN_COMP_RES_RESTART 0x0004 /* MPPC: restart */
588 #define HIFN_COMP_RES_ENDMARKER 0x0002 /* LZS: end marker seen */
589 #define HIFN_COMP_RES_SRC_NOTZERO 0x0001 /* source expired */
591 struct hifn_mac_result
593 volatile __le16 flags;
594 volatile __le16 reserved;
595 /* followed by 0, 6, 8, or 10 u16's of the MAC, then crypt */
598 #define HIFN_MAC_RES_MISCOMPARE 0x0002 /* compare failed */
599 #define HIFN_MAC_RES_SRC_NOTZERO 0x0001 /* source expired */
601 struct hifn_crypt_result
603 volatile __le16 flags;
604 volatile __le16 reserved;
607 #define HIFN_CRYPT_RES_SRC_NOTZERO 0x0001 /* source expired */
609 #ifndef HIFN_POLL_FREQUENCY
610 #define HIFN_POLL_FREQUENCY 0x1
613 #ifndef HIFN_POLL_SCALAR
614 #define HIFN_POLL_SCALAR 0x0
617 #define HIFN_MAX_SEGLEN 0xffff /* maximum dma segment len */
618 #define HIFN_MAX_DMALEN 0x3ffff /* maximum dma length */
620 struct hifn_crypto_alg
622 struct list_head entry;
623 struct crypto_alg alg;
624 struct hifn_device *dev;
627 #define ASYNC_SCATTERLIST_CACHE 16
629 #define ASYNC_FLAGS_MISALIGNED (1<<0)
631 struct hifn_cipher_walk
633 struct scatterlist cache[ASYNC_SCATTERLIST_CACHE];
640 u8 key[HIFN_MAX_CRYPT_KEY_LENGTH];
641 struct hifn_device *dev;
642 unsigned int keysize;
645 struct hifn_request_context
649 u8 op, type, mode, unused;
650 struct hifn_cipher_walk walk;
653 #define crypto_alg_to_hifn(a) container_of(a, struct hifn_crypto_alg, alg)
655 static inline u32 hifn_read_0(struct hifn_device *dev, u32 reg)
659 ret = readl(dev->bar[0] + reg);
664 static inline u32 hifn_read_1(struct hifn_device *dev, u32 reg)
668 ret = readl(dev->bar[1] + reg);
673 static inline void hifn_write_0(struct hifn_device *dev, u32 reg, u32 val)
675 writel((__force u32)cpu_to_le32(val), dev->bar[0] + reg);
678 static inline void hifn_write_1(struct hifn_device *dev, u32 reg, u32 val)
680 writel((__force u32)cpu_to_le32(val), dev->bar[1] + reg);
683 static void hifn_wait_puc(struct hifn_device *dev)
688 for (i=10000; i > 0; --i) {
689 ret = hifn_read_0(dev, HIFN_0_PUCTRL);
690 if (!(ret & HIFN_PUCTRL_RESET))
697 dev_err(&dev->pdev->dev, "Failed to reset PUC unit.\n");
700 static void hifn_reset_puc(struct hifn_device *dev)
702 hifn_write_0(dev, HIFN_0_PUCTRL, HIFN_PUCTRL_DMAENA);
706 static void hifn_stop_device(struct hifn_device *dev)
708 hifn_write_1(dev, HIFN_1_DMA_CSR,
709 HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS |
710 HIFN_DMACSR_S_CTRL_DIS | HIFN_DMACSR_C_CTRL_DIS);
711 hifn_write_0(dev, HIFN_0_PUIER, 0);
712 hifn_write_1(dev, HIFN_1_DMA_IER, 0);
715 static void hifn_reset_dma(struct hifn_device *dev, int full)
717 hifn_stop_device(dev);
720 * Setting poll frequency and others to 0.
722 hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
723 HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
730 hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MODE);
733 hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MODE |
734 HIFN_DMACNFG_MSTRESET);
738 hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
739 HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
744 static u32 hifn_next_signature(u_int32_t a, u_int cnt)
749 for (i = 0; i < cnt; i++) {
759 a = (v & 1) ^ (a << 1);
765 static struct pci2id {
772 PCI_DEVICE_ID_HIFN_7955,
773 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
774 0x00, 0x00, 0x00, 0x00, 0x00 }
778 PCI_DEVICE_ID_HIFN_7956,
779 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
780 0x00, 0x00, 0x00, 0x00, 0x00 }
784 #ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
785 static int hifn_rng_data_present(struct hwrng *rng, int wait)
787 struct hifn_device *dev = (struct hifn_device *)rng->priv;
790 nsec = ktime_to_ns(ktime_sub(ktime_get(), dev->rngtime));
791 nsec -= dev->rng_wait_time;
800 static int hifn_rng_data_read(struct hwrng *rng, u32 *data)
802 struct hifn_device *dev = (struct hifn_device *)rng->priv;
804 *data = hifn_read_1(dev, HIFN_1_RNG_DATA);
805 dev->rngtime = ktime_get();
809 static int hifn_register_rng(struct hifn_device *dev)
812 * We must wait at least 256 Pk_clk cycles between two reads of the rng.
814 dev->rng_wait_time = DIV_ROUND_UP_ULL(NSEC_PER_SEC,
815 dev->pk_clk_freq) * 256;
817 dev->rng.name = dev->name;
818 dev->rng.data_present = hifn_rng_data_present,
819 dev->rng.data_read = hifn_rng_data_read,
820 dev->rng.priv = (unsigned long)dev;
822 return hwrng_register(&dev->rng);
825 static void hifn_unregister_rng(struct hifn_device *dev)
827 hwrng_unregister(&dev->rng);
830 #define hifn_register_rng(dev) 0
831 #define hifn_unregister_rng(dev)
834 static int hifn_init_pubrng(struct hifn_device *dev)
838 hifn_write_1(dev, HIFN_1_PUB_RESET, hifn_read_1(dev, HIFN_1_PUB_RESET) |
841 for (i=100; i > 0; --i) {
844 if ((hifn_read_1(dev, HIFN_1_PUB_RESET) & HIFN_PUBRST_RESET) == 0)
849 dev_err(&dev->pdev->dev, "Failed to initialise public key engine.\n");
851 hifn_write_1(dev, HIFN_1_PUB_IEN, HIFN_PUBIEN_DONE);
852 dev->dmareg |= HIFN_DMAIER_PUBDONE;
853 hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
855 dev_dbg(&dev->pdev->dev, "Public key engine has been successfully initialised.\n");
862 hifn_write_1(dev, HIFN_1_RNG_CONFIG,
863 hifn_read_1(dev, HIFN_1_RNG_CONFIG) | HIFN_RNGCFG_ENA);
864 dev_dbg(&dev->pdev->dev, "RNG engine has been successfully initialised.\n");
866 #ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
867 /* First value must be discarded */
868 hifn_read_1(dev, HIFN_1_RNG_DATA);
869 dev->rngtime = ktime_get();
874 static int hifn_enable_crypto(struct hifn_device *dev)
880 for (i = 0; i < ARRAY_SIZE(pci2id); i++) {
881 if (pci2id[i].pci_vendor == dev->pdev->vendor &&
882 pci2id[i].pci_prod == dev->pdev->device) {
883 offtbl = pci2id[i].card_id;
888 if (offtbl == NULL) {
889 dev_err(&dev->pdev->dev, "Unknown card!\n");
893 dmacfg = hifn_read_1(dev, HIFN_1_DMA_CNFG);
895 hifn_write_1(dev, HIFN_1_DMA_CNFG,
896 HIFN_DMACNFG_UNLOCK | HIFN_DMACNFG_MSTRESET |
897 HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
899 addr = hifn_read_1(dev, HIFN_1_UNLOCK_SECRET1);
901 hifn_write_1(dev, HIFN_1_UNLOCK_SECRET2, 0);
904 for (i=0; i<12; ++i) {
905 addr = hifn_next_signature(addr, offtbl[i] + 0x101);
906 hifn_write_1(dev, HIFN_1_UNLOCK_SECRET2, addr);
910 hifn_write_1(dev, HIFN_1_DMA_CNFG, dmacfg);
912 dev_dbg(&dev->pdev->dev, "%s %s.\n", dev->name, pci_name(dev->pdev));
917 static void hifn_init_dma(struct hifn_device *dev)
919 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
920 u32 dptr = dev->desc_dma;
923 for (i=0; i<HIFN_D_CMD_RSIZE; ++i)
924 dma->cmdr[i].p = __cpu_to_le32(dptr +
925 offsetof(struct hifn_dma, command_bufs[i][0]));
926 for (i=0; i<HIFN_D_RES_RSIZE; ++i)
927 dma->resr[i].p = __cpu_to_le32(dptr +
928 offsetof(struct hifn_dma, result_bufs[i][0]));
931 * Setup LAST descriptors.
933 dma->cmdr[HIFN_D_CMD_RSIZE].p = __cpu_to_le32(dptr +
934 offsetof(struct hifn_dma, cmdr[0]));
935 dma->srcr[HIFN_D_SRC_RSIZE].p = __cpu_to_le32(dptr +
936 offsetof(struct hifn_dma, srcr[0]));
937 dma->dstr[HIFN_D_DST_RSIZE].p = __cpu_to_le32(dptr +
938 offsetof(struct hifn_dma, dstr[0]));
939 dma->resr[HIFN_D_RES_RSIZE].p = __cpu_to_le32(dptr +
940 offsetof(struct hifn_dma, resr[0]));
942 dma->cmdu = dma->srcu = dma->dstu = dma->resu = 0;
943 dma->cmdi = dma->srci = dma->dsti = dma->resi = 0;
944 dma->cmdk = dma->srck = dma->dstk = dma->resk = 0;
948 * Initialize the PLL. We need to know the frequency of the reference clock
949 * to calculate the optimal multiplier. For PCI we assume 66MHz, since that
950 * allows us to operate without the risk of overclocking the chip. If it
951 * actually uses 33MHz, the chip will operate at half the speed, this can be
952 * overriden by specifying the frequency as module parameter (pci33).
954 * Unfortunately the PCI clock is not very suitable since the HIFN needs a
955 * stable clock and the PCI clock frequency may vary, so the default is the
956 * external clock. There is no way to find out its frequency, we default to
957 * 66MHz since according to Mike Ham of HiFn, almost every board in existence
958 * has an external crystal populated at 66MHz.
960 static void hifn_init_pll(struct hifn_device *dev)
962 unsigned int freq, m;
965 pllcfg = HIFN_1_PLL | HIFN_PLL_RESERVED_1;
967 if (strncmp(hifn_pll_ref, "ext", 3) == 0)
968 pllcfg |= HIFN_PLL_REF_CLK_PLL;
970 pllcfg |= HIFN_PLL_REF_CLK_HBI;
972 if (hifn_pll_ref[3] != '\0')
973 freq = simple_strtoul(hifn_pll_ref + 3, NULL, 10);
976 dev_info(&dev->pdev->dev, "assuming %uMHz clock speed, override with hifn_pll_ref=%.3s<frequency>\n",
980 m = HIFN_PLL_FCK_MAX / freq;
982 pllcfg |= (m / 2 - 1) << HIFN_PLL_ND_SHIFT;
984 pllcfg |= HIFN_PLL_IS_1_8;
986 pllcfg |= HIFN_PLL_IS_9_12;
988 /* Select clock source and enable clock bypass */
989 hifn_write_1(dev, HIFN_1_PLL, pllcfg |
990 HIFN_PLL_PK_CLK_HBI | HIFN_PLL_PE_CLK_HBI | HIFN_PLL_BP);
992 /* Let the chip lock to the input clock */
995 /* Disable clock bypass */
996 hifn_write_1(dev, HIFN_1_PLL, pllcfg |
997 HIFN_PLL_PK_CLK_HBI | HIFN_PLL_PE_CLK_HBI);
999 /* Switch the engines to the PLL */
1000 hifn_write_1(dev, HIFN_1_PLL, pllcfg |
1001 HIFN_PLL_PK_CLK_PLL | HIFN_PLL_PE_CLK_PLL);
1004 * The Fpk_clk runs at half the total speed. Its frequency is needed to
1005 * calculate the minimum time between two reads of the rng. Since 33MHz
1006 * is actually 33.333... we overestimate the frequency here, resulting
1007 * in slightly larger intervals.
1009 dev->pk_clk_freq = 1000000 * (freq + 1) * m / 2;
1012 static void hifn_init_registers(struct hifn_device *dev)
1014 u32 dptr = dev->desc_dma;
1016 /* Initialization magic... */
1017 hifn_write_0(dev, HIFN_0_PUCTRL, HIFN_PUCTRL_DMAENA);
1018 hifn_write_0(dev, HIFN_0_FIFOCNFG, HIFN_FIFOCNFG_THRESHOLD);
1019 hifn_write_0(dev, HIFN_0_PUIER, HIFN_PUIER_DSTOVER);
1021 /* write all 4 ring address registers */
1022 hifn_write_1(dev, HIFN_1_DMA_CRAR, dptr +
1023 offsetof(struct hifn_dma, cmdr[0]));
1024 hifn_write_1(dev, HIFN_1_DMA_SRAR, dptr +
1025 offsetof(struct hifn_dma, srcr[0]));
1026 hifn_write_1(dev, HIFN_1_DMA_DRAR, dptr +
1027 offsetof(struct hifn_dma, dstr[0]));
1028 hifn_write_1(dev, HIFN_1_DMA_RRAR, dptr +
1029 offsetof(struct hifn_dma, resr[0]));
1033 hifn_write_1(dev, HIFN_1_DMA_CSR,
1034 HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS |
1035 HIFN_DMACSR_S_CTRL_DIS | HIFN_DMACSR_C_CTRL_DIS |
1036 HIFN_DMACSR_D_ABORT | HIFN_DMACSR_D_DONE | HIFN_DMACSR_D_LAST |
1037 HIFN_DMACSR_D_WAIT | HIFN_DMACSR_D_OVER |
1038 HIFN_DMACSR_R_ABORT | HIFN_DMACSR_R_DONE | HIFN_DMACSR_R_LAST |
1039 HIFN_DMACSR_R_WAIT | HIFN_DMACSR_R_OVER |
1040 HIFN_DMACSR_S_ABORT | HIFN_DMACSR_S_DONE | HIFN_DMACSR_S_LAST |
1041 HIFN_DMACSR_S_WAIT |
1042 HIFN_DMACSR_C_ABORT | HIFN_DMACSR_C_DONE | HIFN_DMACSR_C_LAST |
1043 HIFN_DMACSR_C_WAIT |
1044 HIFN_DMACSR_ENGINE |
1045 HIFN_DMACSR_PUBDONE);
1047 hifn_write_1(dev, HIFN_1_DMA_CSR,
1048 HIFN_DMACSR_C_CTRL_ENA | HIFN_DMACSR_S_CTRL_ENA |
1049 HIFN_DMACSR_D_CTRL_ENA | HIFN_DMACSR_R_CTRL_ENA |
1050 HIFN_DMACSR_D_ABORT | HIFN_DMACSR_D_DONE | HIFN_DMACSR_D_LAST |
1051 HIFN_DMACSR_D_WAIT | HIFN_DMACSR_D_OVER |
1052 HIFN_DMACSR_R_ABORT | HIFN_DMACSR_R_DONE | HIFN_DMACSR_R_LAST |
1053 HIFN_DMACSR_R_WAIT | HIFN_DMACSR_R_OVER |
1054 HIFN_DMACSR_S_ABORT | HIFN_DMACSR_S_DONE | HIFN_DMACSR_S_LAST |
1055 HIFN_DMACSR_S_WAIT |
1056 HIFN_DMACSR_C_ABORT | HIFN_DMACSR_C_DONE | HIFN_DMACSR_C_LAST |
1057 HIFN_DMACSR_C_WAIT |
1058 HIFN_DMACSR_ENGINE |
1059 HIFN_DMACSR_PUBDONE);
1061 hifn_read_1(dev, HIFN_1_DMA_CSR);
1063 dev->dmareg |= HIFN_DMAIER_R_DONE | HIFN_DMAIER_C_ABORT |
1064 HIFN_DMAIER_D_OVER | HIFN_DMAIER_R_OVER |
1065 HIFN_DMAIER_S_ABORT | HIFN_DMAIER_D_ABORT | HIFN_DMAIER_R_ABORT |
1067 dev->dmareg &= ~HIFN_DMAIER_C_WAIT;
1069 hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
1070 hifn_read_1(dev, HIFN_1_DMA_IER);
1072 hifn_write_0(dev, HIFN_0_PUCNFG, HIFN_PUCNFG_ENCCNFG |
1073 HIFN_PUCNFG_DRFR_128 | HIFN_PUCNFG_TCALLPHASES |
1074 HIFN_PUCNFG_TCDRVTOTEM | HIFN_PUCNFG_BUS32 |
1077 hifn_write_0(dev, HIFN_0_PUCNFG, 0x10342);
1081 hifn_write_0(dev, HIFN_0_PUISR, HIFN_PUISR_DSTOVER);
1082 hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
1083 HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE | HIFN_DMACNFG_LAST |
1084 ((HIFN_POLL_FREQUENCY << 16 ) & HIFN_DMACNFG_POLLFREQ) |
1085 ((HIFN_POLL_SCALAR << 8) & HIFN_DMACNFG_POLLINVAL));
1088 static int hifn_setup_base_command(struct hifn_device *dev, u8 *buf,
1089 unsigned dlen, unsigned slen, u16 mask, u8 snum)
1091 struct hifn_base_command *base_cmd;
1094 base_cmd = (struct hifn_base_command *)buf_pos;
1095 base_cmd->masks = __cpu_to_le16(mask);
1096 base_cmd->total_source_count =
1097 __cpu_to_le16(slen & HIFN_BASE_CMD_LENMASK_LO);
1098 base_cmd->total_dest_count =
1099 __cpu_to_le16(dlen & HIFN_BASE_CMD_LENMASK_LO);
1103 base_cmd->session_num = __cpu_to_le16(snum |
1104 ((slen << HIFN_BASE_CMD_SRCLEN_S) & HIFN_BASE_CMD_SRCLEN_M) |
1105 ((dlen << HIFN_BASE_CMD_DSTLEN_S) & HIFN_BASE_CMD_DSTLEN_M));
1107 return sizeof(struct hifn_base_command);
1110 static int hifn_setup_crypto_command(struct hifn_device *dev,
1111 u8 *buf, unsigned dlen, unsigned slen,
1112 u8 *key, int keylen, u8 *iv, int ivsize, u16 mode)
1114 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1115 struct hifn_crypt_command *cry_cmd;
1119 cry_cmd = (struct hifn_crypt_command *)buf_pos;
1121 cry_cmd->source_count = __cpu_to_le16(dlen & 0xffff);
1123 cry_cmd->masks = __cpu_to_le16(mode |
1124 ((dlen << HIFN_CRYPT_CMD_SRCLEN_S) &
1125 HIFN_CRYPT_CMD_SRCLEN_M));
1126 cry_cmd->header_skip = 0;
1127 cry_cmd->reserved = 0;
1129 buf_pos += sizeof(struct hifn_crypt_command);
1132 if (dma->cmdu > 1) {
1133 dev->dmareg |= HIFN_DMAIER_C_WAIT;
1134 hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
1138 memcpy(buf_pos, key, keylen);
1142 memcpy(buf_pos, iv, ivsize);
1146 cmd_len = buf_pos - buf;
1151 static int hifn_setup_cmd_desc(struct hifn_device *dev,
1152 struct hifn_context *ctx, struct hifn_request_context *rctx,
1153 void *priv, unsigned int nbytes)
1155 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1156 int cmd_len, sa_idx;
1161 buf_pos = buf = dma->command_bufs[dma->cmdi];
1165 case ACRYPTO_OP_DECRYPT:
1166 mask = HIFN_BASE_CMD_CRYPT | HIFN_BASE_CMD_DECODE;
1168 case ACRYPTO_OP_ENCRYPT:
1169 mask = HIFN_BASE_CMD_CRYPT;
1171 case ACRYPTO_OP_HMAC:
1172 mask = HIFN_BASE_CMD_MAC;
1178 buf_pos += hifn_setup_base_command(dev, buf_pos, nbytes,
1179 nbytes, mask, dev->snum);
1181 if (rctx->op == ACRYPTO_OP_ENCRYPT || rctx->op == ACRYPTO_OP_DECRYPT) {
1185 md |= HIFN_CRYPT_CMD_NEW_KEY;
1186 if (rctx->iv && rctx->mode != ACRYPTO_MODE_ECB)
1187 md |= HIFN_CRYPT_CMD_NEW_IV;
1189 switch (rctx->mode) {
1190 case ACRYPTO_MODE_ECB:
1191 md |= HIFN_CRYPT_CMD_MODE_ECB;
1193 case ACRYPTO_MODE_CBC:
1194 md |= HIFN_CRYPT_CMD_MODE_CBC;
1196 case ACRYPTO_MODE_CFB:
1197 md |= HIFN_CRYPT_CMD_MODE_CFB;
1199 case ACRYPTO_MODE_OFB:
1200 md |= HIFN_CRYPT_CMD_MODE_OFB;
1206 switch (rctx->type) {
1207 case ACRYPTO_TYPE_AES_128:
1208 if (ctx->keysize != 16)
1210 md |= HIFN_CRYPT_CMD_KSZ_128 |
1211 HIFN_CRYPT_CMD_ALG_AES;
1213 case ACRYPTO_TYPE_AES_192:
1214 if (ctx->keysize != 24)
1216 md |= HIFN_CRYPT_CMD_KSZ_192 |
1217 HIFN_CRYPT_CMD_ALG_AES;
1219 case ACRYPTO_TYPE_AES_256:
1220 if (ctx->keysize != 32)
1222 md |= HIFN_CRYPT_CMD_KSZ_256 |
1223 HIFN_CRYPT_CMD_ALG_AES;
1225 case ACRYPTO_TYPE_3DES:
1226 if (ctx->keysize != 24)
1228 md |= HIFN_CRYPT_CMD_ALG_3DES;
1230 case ACRYPTO_TYPE_DES:
1231 if (ctx->keysize != 8)
1233 md |= HIFN_CRYPT_CMD_ALG_DES;
1239 buf_pos += hifn_setup_crypto_command(dev, buf_pos,
1240 nbytes, nbytes, ctx->key, ctx->keysize,
1241 rctx->iv, rctx->ivsize, md);
1244 dev->sa[sa_idx] = priv;
1247 cmd_len = buf_pos - buf;
1248 dma->cmdr[dma->cmdi].l = __cpu_to_le32(cmd_len | HIFN_D_VALID |
1249 HIFN_D_LAST | HIFN_D_MASKDONEIRQ);
1251 if (++dma->cmdi == HIFN_D_CMD_RSIZE) {
1252 dma->cmdr[dma->cmdi].l = __cpu_to_le32(
1253 HIFN_D_VALID | HIFN_D_LAST |
1254 HIFN_D_MASKDONEIRQ | HIFN_D_JUMP);
1257 dma->cmdr[dma->cmdi-1].l |= __cpu_to_le32(HIFN_D_VALID);
1259 if (!(dev->flags & HIFN_FLAG_CMD_BUSY)) {
1260 hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_C_CTRL_ENA);
1261 dev->flags |= HIFN_FLAG_CMD_BUSY;
1269 static int hifn_setup_src_desc(struct hifn_device *dev, struct page *page,
1270 unsigned int offset, unsigned int size, int last)
1272 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1276 addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_TODEVICE);
1280 dma->srcr[idx].p = __cpu_to_le32(addr);
1281 dma->srcr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
1282 HIFN_D_MASKDONEIRQ | (last ? HIFN_D_LAST : 0));
1284 if (++idx == HIFN_D_SRC_RSIZE) {
1285 dma->srcr[idx].l = __cpu_to_le32(HIFN_D_VALID |
1286 HIFN_D_JUMP | HIFN_D_MASKDONEIRQ |
1287 (last ? HIFN_D_LAST : 0));
1294 if (!(dev->flags & HIFN_FLAG_SRC_BUSY)) {
1295 hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_S_CTRL_ENA);
1296 dev->flags |= HIFN_FLAG_SRC_BUSY;
1302 static void hifn_setup_res_desc(struct hifn_device *dev)
1304 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1306 dma->resr[dma->resi].l = __cpu_to_le32(HIFN_USED_RESULT |
1307 HIFN_D_VALID | HIFN_D_LAST);
1309 * dma->resr[dma->resi].l = __cpu_to_le32(HIFN_MAX_RESULT | HIFN_D_VALID |
1313 if (++dma->resi == HIFN_D_RES_RSIZE) {
1314 dma->resr[HIFN_D_RES_RSIZE].l = __cpu_to_le32(HIFN_D_VALID |
1315 HIFN_D_JUMP | HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
1321 if (!(dev->flags & HIFN_FLAG_RES_BUSY)) {
1322 hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_R_CTRL_ENA);
1323 dev->flags |= HIFN_FLAG_RES_BUSY;
1327 static void hifn_setup_dst_desc(struct hifn_device *dev, struct page *page,
1328 unsigned offset, unsigned size, int last)
1330 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1334 addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_FROMDEVICE);
1337 dma->dstr[idx].p = __cpu_to_le32(addr);
1338 dma->dstr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
1339 HIFN_D_MASKDONEIRQ | (last ? HIFN_D_LAST : 0));
1341 if (++idx == HIFN_D_DST_RSIZE) {
1342 dma->dstr[idx].l = __cpu_to_le32(HIFN_D_VALID |
1343 HIFN_D_JUMP | HIFN_D_MASKDONEIRQ |
1344 (last ? HIFN_D_LAST : 0));
1350 if (!(dev->flags & HIFN_FLAG_DST_BUSY)) {
1351 hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_D_CTRL_ENA);
1352 dev->flags |= HIFN_FLAG_DST_BUSY;
1356 static int hifn_setup_dma(struct hifn_device *dev,
1357 struct hifn_context *ctx, struct hifn_request_context *rctx,
1358 struct scatterlist *src, struct scatterlist *dst,
1359 unsigned int nbytes, void *priv)
1361 struct scatterlist *t;
1362 struct page *spage, *dpage;
1363 unsigned int soff, doff;
1364 unsigned int n, len;
1368 spage = sg_page(src);
1370 len = min(src->length, n);
1372 hifn_setup_src_desc(dev, spage, soff, len, n - len == 0);
1378 t = &rctx->walk.cache[0];
1381 if (t->length && rctx->walk.flags & ASYNC_FLAGS_MISALIGNED) {
1382 BUG_ON(!sg_page(t));
1387 BUG_ON(!sg_page(dst));
1388 dpage = sg_page(dst);
1394 hifn_setup_dst_desc(dev, dpage, doff, len, n - len == 0);
1401 hifn_setup_cmd_desc(dev, ctx, rctx, priv, nbytes);
1402 hifn_setup_res_desc(dev);
1406 static int hifn_cipher_walk_init(struct hifn_cipher_walk *w,
1407 int num, gfp_t gfp_flags)
1411 num = min(ASYNC_SCATTERLIST_CACHE, num);
1412 sg_init_table(w->cache, num);
1415 for (i=0; i<num; ++i) {
1416 struct page *page = alloc_page(gfp_flags);
1417 struct scatterlist *s;
1424 sg_set_page(s, page, PAGE_SIZE, 0);
1431 static void hifn_cipher_walk_exit(struct hifn_cipher_walk *w)
1435 for (i=0; i<w->num; ++i) {
1436 struct scatterlist *s = &w->cache[i];
1438 __free_page(sg_page(s));
1446 static int ablkcipher_add(unsigned int *drestp, struct scatterlist *dst,
1447 unsigned int size, unsigned int *nbytesp)
1449 unsigned int copy, drest = *drestp, nbytes = *nbytesp;
1452 if (drest < size || size > nbytes)
1456 copy = min3(drest, size, dst->length);
1462 pr_debug("%s: copy: %u, size: %u, drest: %u, nbytes: %u.\n",
1463 __func__, copy, size, drest, nbytes);
1475 static int hifn_cipher_walk(struct ablkcipher_request *req,
1476 struct hifn_cipher_walk *w)
1478 struct scatterlist *dst, *t;
1479 unsigned int nbytes = req->nbytes, offset, copy, diff;
1485 if (idx >= w->num && (w->flags & ASYNC_FLAGS_MISALIGNED))
1488 dst = &req->dst[idx];
1490 pr_debug("\n%s: dlen: %u, doff: %u, offset: %u, nbytes: %u.\n",
1491 __func__, dst->length, dst->offset, offset, nbytes);
1493 if (!IS_ALIGNED(dst->offset, HIFN_D_DST_DALIGN) ||
1494 !IS_ALIGNED(dst->length, HIFN_D_DST_DALIGN) ||
1496 unsigned slen = min(dst->length - offset, nbytes);
1497 unsigned dlen = PAGE_SIZE;
1501 err = ablkcipher_add(&dlen, dst, slen, &nbytes);
1507 copy = slen & ~(HIFN_D_DST_DALIGN - 1);
1508 diff = slen & (HIFN_D_DST_DALIGN - 1);
1510 if (dlen < nbytes) {
1512 * Destination page does not have enough space
1513 * to put there additional blocksized chunk,
1514 * so we mark that page as containing only
1515 * blocksize aligned chunks:
1516 * t->length = (slen & ~(HIFN_D_DST_DALIGN - 1));
1517 * and increase number of bytes to be processed
1524 * Temporary of course...
1525 * Kick author if you will catch this one.
1527 pr_err("%s: dlen: %u, nbytes: %u, slen: %u, offset: %u.\n",
1528 __func__, dlen, nbytes, slen, offset);
1529 pr_err("%s: please contact author to fix this "
1530 "issue, generally you should not catch "
1531 "this path under any condition but who "
1532 "knows how did you use crypto code.\n"
1533 "Thank you.\n", __func__);
1536 copy += diff + nbytes;
1538 dst = &req->dst[idx];
1540 err = ablkcipher_add(&dlen, dst, nbytes, &nbytes);
1550 nbytes -= min(dst->length, nbytes);
1560 static int hifn_setup_session(struct ablkcipher_request *req)
1562 struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
1563 struct hifn_request_context *rctx = ablkcipher_request_ctx(req);
1564 struct hifn_device *dev = ctx->dev;
1565 unsigned long dlen, flags;
1566 unsigned int nbytes = req->nbytes, idx = 0;
1567 int err = -EINVAL, sg_num;
1568 struct scatterlist *dst;
1570 if (rctx->iv && !rctx->ivsize && rctx->mode != ACRYPTO_MODE_ECB)
1573 rctx->walk.flags = 0;
1576 dst = &req->dst[idx];
1577 dlen = min(dst->length, nbytes);
1579 if (!IS_ALIGNED(dst->offset, HIFN_D_DST_DALIGN) ||
1580 !IS_ALIGNED(dlen, HIFN_D_DST_DALIGN))
1581 rctx->walk.flags |= ASYNC_FLAGS_MISALIGNED;
1587 if (rctx->walk.flags & ASYNC_FLAGS_MISALIGNED) {
1588 err = hifn_cipher_walk_init(&rctx->walk, idx, GFP_ATOMIC);
1593 sg_num = hifn_cipher_walk(req, &rctx->walk);
1599 spin_lock_irqsave(&dev->lock, flags);
1600 if (dev->started + sg_num > HIFN_QUEUE_LENGTH) {
1605 err = hifn_setup_dma(dev, ctx, rctx, req->src, req->dst, req->nbytes, req);
1611 dev->active = HIFN_DEFAULT_ACTIVE_NUM;
1612 spin_unlock_irqrestore(&dev->lock, flags);
1617 spin_unlock_irqrestore(&dev->lock, flags);
1620 dev_info(&dev->pdev->dev, "iv: %p [%d], key: %p [%d], mode: %u, op: %u, "
1621 "type: %u, err: %d.\n",
1622 rctx->iv, rctx->ivsize,
1623 ctx->key, ctx->keysize,
1624 rctx->mode, rctx->op, rctx->type, err);
1630 static int hifn_start_device(struct hifn_device *dev)
1634 dev->started = dev->active = 0;
1635 hifn_reset_dma(dev, 1);
1637 err = hifn_enable_crypto(dev);
1641 hifn_reset_puc(dev);
1645 hifn_init_registers(dev);
1647 hifn_init_pubrng(dev);
1652 static int ablkcipher_get(void *saddr, unsigned int *srestp, unsigned int offset,
1653 struct scatterlist *dst, unsigned int size, unsigned int *nbytesp)
1655 unsigned int srest = *srestp, nbytes = *nbytesp, copy;
1659 if (srest < size || size > nbytes)
1663 copy = min3(srest, dst->length, size);
1665 daddr = kmap_atomic(sg_page(dst));
1666 memcpy(daddr + dst->offset + offset, saddr, copy);
1667 kunmap_atomic(daddr);
1675 pr_debug("%s: copy: %u, size: %u, srest: %u, nbytes: %u.\n",
1676 __func__, copy, size, srest, nbytes);
1688 static inline void hifn_complete_sa(struct hifn_device *dev, int i)
1690 unsigned long flags;
1692 spin_lock_irqsave(&dev->lock, flags);
1695 if (dev->started < 0)
1696 dev_info(&dev->pdev->dev, "%s: started: %d.\n", __func__,
1698 spin_unlock_irqrestore(&dev->lock, flags);
1699 BUG_ON(dev->started < 0);
1702 static void hifn_process_ready(struct ablkcipher_request *req, int error)
1704 struct hifn_request_context *rctx = ablkcipher_request_ctx(req);
1706 if (rctx->walk.flags & ASYNC_FLAGS_MISALIGNED) {
1707 unsigned int nbytes = req->nbytes;
1709 struct scatterlist *dst, *t;
1713 t = &rctx->walk.cache[idx];
1714 dst = &req->dst[idx];
1716 pr_debug("\n%s: sg_page(t): %p, t->length: %u, "
1717 "sg_page(dst): %p, dst->length: %u, "
1719 __func__, sg_page(t), t->length,
1720 sg_page(dst), dst->length, nbytes);
1723 nbytes -= min(dst->length, nbytes);
1728 saddr = kmap_atomic(sg_page(t));
1730 err = ablkcipher_get(saddr, &t->length, t->offset,
1731 dst, nbytes, &nbytes);
1733 kunmap_atomic(saddr);
1738 kunmap_atomic(saddr);
1741 hifn_cipher_walk_exit(&rctx->walk);
1744 req->base.complete(&req->base, error);
1747 static void hifn_clear_rings(struct hifn_device *dev, int error)
1749 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1752 dev_dbg(&dev->pdev->dev, "ring cleanup 1: i: %d.%d.%d.%d, u: %d.%d.%d.%d, "
1753 "k: %d.%d.%d.%d.\n",
1754 dma->cmdi, dma->srci, dma->dsti, dma->resi,
1755 dma->cmdu, dma->srcu, dma->dstu, dma->resu,
1756 dma->cmdk, dma->srck, dma->dstk, dma->resk);
1758 i = dma->resk; u = dma->resu;
1760 if (dma->resr[i].l & __cpu_to_le32(HIFN_D_VALID))
1766 hifn_process_ready(dev->sa[i], error);
1767 hifn_complete_sa(dev, i);
1770 if (++i == HIFN_D_RES_RSIZE)
1774 dma->resk = i; dma->resu = u;
1776 i = dma->srck; u = dma->srcu;
1778 if (dma->srcr[i].l & __cpu_to_le32(HIFN_D_VALID))
1780 if (++i == HIFN_D_SRC_RSIZE)
1784 dma->srck = i; dma->srcu = u;
1786 i = dma->cmdk; u = dma->cmdu;
1788 if (dma->cmdr[i].l & __cpu_to_le32(HIFN_D_VALID))
1790 if (++i == HIFN_D_CMD_RSIZE)
1794 dma->cmdk = i; dma->cmdu = u;
1796 i = dma->dstk; u = dma->dstu;
1798 if (dma->dstr[i].l & __cpu_to_le32(HIFN_D_VALID))
1800 if (++i == HIFN_D_DST_RSIZE)
1804 dma->dstk = i; dma->dstu = u;
1806 dev_dbg(&dev->pdev->dev, "ring cleanup 2: i: %d.%d.%d.%d, u: %d.%d.%d.%d, "
1807 "k: %d.%d.%d.%d.\n",
1808 dma->cmdi, dma->srci, dma->dsti, dma->resi,
1809 dma->cmdu, dma->srcu, dma->dstu, dma->resu,
1810 dma->cmdk, dma->srck, dma->dstk, dma->resk);
1813 static void hifn_work(struct work_struct *work)
1815 struct delayed_work *dw = to_delayed_work(work);
1816 struct hifn_device *dev = container_of(dw, struct hifn_device, work);
1817 unsigned long flags;
1821 spin_lock_irqsave(&dev->lock, flags);
1822 if (dev->active == 0) {
1823 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1825 if (dma->cmdu == 0 && (dev->flags & HIFN_FLAG_CMD_BUSY)) {
1826 dev->flags &= ~HIFN_FLAG_CMD_BUSY;
1827 r |= HIFN_DMACSR_C_CTRL_DIS;
1829 if (dma->srcu == 0 && (dev->flags & HIFN_FLAG_SRC_BUSY)) {
1830 dev->flags &= ~HIFN_FLAG_SRC_BUSY;
1831 r |= HIFN_DMACSR_S_CTRL_DIS;
1833 if (dma->dstu == 0 && (dev->flags & HIFN_FLAG_DST_BUSY)) {
1834 dev->flags &= ~HIFN_FLAG_DST_BUSY;
1835 r |= HIFN_DMACSR_D_CTRL_DIS;
1837 if (dma->resu == 0 && (dev->flags & HIFN_FLAG_RES_BUSY)) {
1838 dev->flags &= ~HIFN_FLAG_RES_BUSY;
1839 r |= HIFN_DMACSR_R_CTRL_DIS;
1842 hifn_write_1(dev, HIFN_1_DMA_CSR, r);
1846 if ((dev->prev_success == dev->success) && dev->started)
1848 dev->prev_success = dev->success;
1849 spin_unlock_irqrestore(&dev->lock, flags);
1852 if (++dev->reset >= 5) {
1854 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1856 dev_info(&dev->pdev->dev,
1857 "r: %08x, active: %d, started: %d, "
1858 "success: %lu: qlen: %u/%u, reset: %d.\n",
1859 r, dev->active, dev->started,
1860 dev->success, dev->queue.qlen, dev->queue.max_qlen,
1863 dev_info(&dev->pdev->dev, "%s: res: ", __func__);
1864 for (i=0; i<HIFN_D_RES_RSIZE; ++i) {
1865 pr_info("%x.%p ", dma->resr[i].l, dev->sa[i]);
1867 hifn_process_ready(dev->sa[i], -ENODEV);
1868 hifn_complete_sa(dev, i);
1873 hifn_reset_dma(dev, 1);
1874 hifn_stop_device(dev);
1875 hifn_start_device(dev);
1879 tasklet_schedule(&dev->tasklet);
1882 schedule_delayed_work(&dev->work, HZ);
1885 static irqreturn_t hifn_interrupt(int irq, void *data)
1887 struct hifn_device *dev = (struct hifn_device *)data;
1888 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1889 u32 dmacsr, restart;
1891 dmacsr = hifn_read_1(dev, HIFN_1_DMA_CSR);
1893 dev_dbg(&dev->pdev->dev, "1 dmacsr: %08x, dmareg: %08x, res: %08x [%d], "
1894 "i: %d.%d.%d.%d, u: %d.%d.%d.%d.\n",
1895 dmacsr, dev->dmareg, dmacsr & dev->dmareg, dma->cmdi,
1896 dma->cmdi, dma->srci, dma->dsti, dma->resi,
1897 dma->cmdu, dma->srcu, dma->dstu, dma->resu);
1899 if ((dmacsr & dev->dmareg) == 0)
1902 hifn_write_1(dev, HIFN_1_DMA_CSR, dmacsr & dev->dmareg);
1904 if (dmacsr & HIFN_DMACSR_ENGINE)
1905 hifn_write_0(dev, HIFN_0_PUISR, hifn_read_0(dev, HIFN_0_PUISR));
1906 if (dmacsr & HIFN_DMACSR_PUBDONE)
1907 hifn_write_1(dev, HIFN_1_PUB_STATUS,
1908 hifn_read_1(dev, HIFN_1_PUB_STATUS) | HIFN_PUBSTS_DONE);
1910 restart = dmacsr & (HIFN_DMACSR_R_OVER | HIFN_DMACSR_D_OVER);
1912 u32 puisr = hifn_read_0(dev, HIFN_0_PUISR);
1914 dev_warn(&dev->pdev->dev, "overflow: r: %d, d: %d, puisr: %08x, d: %u.\n",
1915 !!(dmacsr & HIFN_DMACSR_R_OVER),
1916 !!(dmacsr & HIFN_DMACSR_D_OVER),
1917 puisr, !!(puisr & HIFN_PUISR_DSTOVER));
1918 if (!!(puisr & HIFN_PUISR_DSTOVER))
1919 hifn_write_0(dev, HIFN_0_PUISR, HIFN_PUISR_DSTOVER);
1920 hifn_write_1(dev, HIFN_1_DMA_CSR, dmacsr & (HIFN_DMACSR_R_OVER |
1921 HIFN_DMACSR_D_OVER));
1924 restart = dmacsr & (HIFN_DMACSR_C_ABORT | HIFN_DMACSR_S_ABORT |
1925 HIFN_DMACSR_D_ABORT | HIFN_DMACSR_R_ABORT);
1927 dev_warn(&dev->pdev->dev, "abort: c: %d, s: %d, d: %d, r: %d.\n",
1928 !!(dmacsr & HIFN_DMACSR_C_ABORT),
1929 !!(dmacsr & HIFN_DMACSR_S_ABORT),
1930 !!(dmacsr & HIFN_DMACSR_D_ABORT),
1931 !!(dmacsr & HIFN_DMACSR_R_ABORT));
1932 hifn_reset_dma(dev, 1);
1934 hifn_init_registers(dev);
1937 if ((dmacsr & HIFN_DMACSR_C_WAIT) && (dma->cmdu == 0)) {
1938 dev_dbg(&dev->pdev->dev, "wait on command.\n");
1939 dev->dmareg &= ~(HIFN_DMAIER_C_WAIT);
1940 hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
1943 tasklet_schedule(&dev->tasklet);
1948 static void hifn_flush(struct hifn_device *dev)
1950 unsigned long flags;
1951 struct crypto_async_request *async_req;
1952 struct ablkcipher_request *req;
1953 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1956 for (i=0; i<HIFN_D_RES_RSIZE; ++i) {
1957 struct hifn_desc *d = &dma->resr[i];
1960 hifn_process_ready(dev->sa[i],
1961 (d->l & __cpu_to_le32(HIFN_D_VALID))?-ENODEV:0);
1962 hifn_complete_sa(dev, i);
1966 spin_lock_irqsave(&dev->lock, flags);
1967 while ((async_req = crypto_dequeue_request(&dev->queue))) {
1968 req = container_of(async_req, struct ablkcipher_request, base);
1969 spin_unlock_irqrestore(&dev->lock, flags);
1971 hifn_process_ready(req, -ENODEV);
1973 spin_lock_irqsave(&dev->lock, flags);
1975 spin_unlock_irqrestore(&dev->lock, flags);
1978 static int hifn_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
1981 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
1982 struct hifn_context *ctx = crypto_tfm_ctx(tfm);
1983 struct hifn_device *dev = ctx->dev;
1985 if (len > HIFN_MAX_CRYPT_KEY_LENGTH) {
1986 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
1990 if (len == HIFN_DES_KEY_LENGTH) {
1991 u32 tmp[DES_EXPKEY_WORDS];
1992 int ret = des_ekey(tmp, key);
1994 if (unlikely(ret == 0) && (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
1995 tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
2000 dev->flags &= ~HIFN_FLAG_OLD_KEY;
2002 memcpy(ctx->key, key, len);
2008 static int hifn_handle_req(struct ablkcipher_request *req)
2010 struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
2011 struct hifn_device *dev = ctx->dev;
2014 if (dev->started + DIV_ROUND_UP(req->nbytes, PAGE_SIZE) <= HIFN_QUEUE_LENGTH)
2015 err = hifn_setup_session(req);
2017 if (err == -EAGAIN) {
2018 unsigned long flags;
2020 spin_lock_irqsave(&dev->lock, flags);
2021 err = ablkcipher_enqueue_request(&dev->queue, req);
2022 spin_unlock_irqrestore(&dev->lock, flags);
2028 static int hifn_setup_crypto_req(struct ablkcipher_request *req, u8 op,
2031 struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
2032 struct hifn_request_context *rctx = ablkcipher_request_ctx(req);
2035 ivsize = crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(req));
2037 if (req->info && mode != ACRYPTO_MODE_ECB) {
2038 if (type == ACRYPTO_TYPE_AES_128)
2039 ivsize = HIFN_AES_IV_LENGTH;
2040 else if (type == ACRYPTO_TYPE_DES)
2041 ivsize = HIFN_DES_KEY_LENGTH;
2042 else if (type == ACRYPTO_TYPE_3DES)
2043 ivsize = HIFN_3DES_KEY_LENGTH;
2046 if (ctx->keysize != 16 && type == ACRYPTO_TYPE_AES_128) {
2047 if (ctx->keysize == 24)
2048 type = ACRYPTO_TYPE_AES_192;
2049 else if (ctx->keysize == 32)
2050 type = ACRYPTO_TYPE_AES_256;
2056 rctx->iv = req->info;
2057 rctx->ivsize = ivsize;
2060 * HEAVY TODO: needs to kick Herbert XU to write documentation.
2061 * HEAVY TODO: needs to kick Herbert XU to write documentation.
2062 * HEAVY TODO: needs to kick Herbert XU to write documentation.
2065 return hifn_handle_req(req);
2068 static int hifn_process_queue(struct hifn_device *dev)
2070 struct crypto_async_request *async_req, *backlog;
2071 struct ablkcipher_request *req;
2072 unsigned long flags;
2075 while (dev->started < HIFN_QUEUE_LENGTH) {
2076 spin_lock_irqsave(&dev->lock, flags);
2077 backlog = crypto_get_backlog(&dev->queue);
2078 async_req = crypto_dequeue_request(&dev->queue);
2079 spin_unlock_irqrestore(&dev->lock, flags);
2085 backlog->complete(backlog, -EINPROGRESS);
2087 req = container_of(async_req, struct ablkcipher_request, base);
2089 err = hifn_handle_req(req);
2097 static int hifn_setup_crypto(struct ablkcipher_request *req, u8 op,
2101 struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
2102 struct hifn_device *dev = ctx->dev;
2104 err = hifn_setup_crypto_req(req, op, type, mode);
2108 if (dev->started < HIFN_QUEUE_LENGTH && dev->queue.qlen)
2109 hifn_process_queue(dev);
2111 return -EINPROGRESS;
2115 * AES ecryption functions.
2117 static inline int hifn_encrypt_aes_ecb(struct ablkcipher_request *req)
2119 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2120 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_ECB);
2122 static inline int hifn_encrypt_aes_cbc(struct ablkcipher_request *req)
2124 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2125 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CBC);
2127 static inline int hifn_encrypt_aes_cfb(struct ablkcipher_request *req)
2129 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2130 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CFB);
2132 static inline int hifn_encrypt_aes_ofb(struct ablkcipher_request *req)
2134 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2135 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_OFB);
2139 * AES decryption functions.
2141 static inline int hifn_decrypt_aes_ecb(struct ablkcipher_request *req)
2143 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2144 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_ECB);
2146 static inline int hifn_decrypt_aes_cbc(struct ablkcipher_request *req)
2148 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2149 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CBC);
2151 static inline int hifn_decrypt_aes_cfb(struct ablkcipher_request *req)
2153 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2154 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CFB);
2156 static inline int hifn_decrypt_aes_ofb(struct ablkcipher_request *req)
2158 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2159 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_OFB);
2163 * DES ecryption functions.
2165 static inline int hifn_encrypt_des_ecb(struct ablkcipher_request *req)
2167 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2168 ACRYPTO_TYPE_DES, ACRYPTO_MODE_ECB);
2170 static inline int hifn_encrypt_des_cbc(struct ablkcipher_request *req)
2172 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2173 ACRYPTO_TYPE_DES, ACRYPTO_MODE_CBC);
2175 static inline int hifn_encrypt_des_cfb(struct ablkcipher_request *req)
2177 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2178 ACRYPTO_TYPE_DES, ACRYPTO_MODE_CFB);
2180 static inline int hifn_encrypt_des_ofb(struct ablkcipher_request *req)
2182 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2183 ACRYPTO_TYPE_DES, ACRYPTO_MODE_OFB);
2187 * DES decryption functions.
2189 static inline int hifn_decrypt_des_ecb(struct ablkcipher_request *req)
2191 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2192 ACRYPTO_TYPE_DES, ACRYPTO_MODE_ECB);
2194 static inline int hifn_decrypt_des_cbc(struct ablkcipher_request *req)
2196 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2197 ACRYPTO_TYPE_DES, ACRYPTO_MODE_CBC);
2199 static inline int hifn_decrypt_des_cfb(struct ablkcipher_request *req)
2201 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2202 ACRYPTO_TYPE_DES, ACRYPTO_MODE_CFB);
2204 static inline int hifn_decrypt_des_ofb(struct ablkcipher_request *req)
2206 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2207 ACRYPTO_TYPE_DES, ACRYPTO_MODE_OFB);
2211 * 3DES ecryption functions.
2213 static inline int hifn_encrypt_3des_ecb(struct ablkcipher_request *req)
2215 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2216 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_ECB);
2218 static inline int hifn_encrypt_3des_cbc(struct ablkcipher_request *req)
2220 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2221 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CBC);
2223 static inline int hifn_encrypt_3des_cfb(struct ablkcipher_request *req)
2225 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2226 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CFB);
2228 static inline int hifn_encrypt_3des_ofb(struct ablkcipher_request *req)
2230 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2231 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_OFB);
2235 * 3DES decryption functions.
2237 static inline int hifn_decrypt_3des_ecb(struct ablkcipher_request *req)
2239 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2240 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_ECB);
2242 static inline int hifn_decrypt_3des_cbc(struct ablkcipher_request *req)
2244 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2245 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CBC);
2247 static inline int hifn_decrypt_3des_cfb(struct ablkcipher_request *req)
2249 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2250 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CFB);
2252 static inline int hifn_decrypt_3des_ofb(struct ablkcipher_request *req)
2254 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2255 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_OFB);
2258 struct hifn_alg_template
2260 char name[CRYPTO_MAX_ALG_NAME];
2261 char drv_name[CRYPTO_MAX_ALG_NAME];
2263 struct ablkcipher_alg ablkcipher;
2266 static struct hifn_alg_template hifn_alg_templates[] = {
2268 * 3DES ECB, CBC, CFB and OFB modes.
2271 .name = "cfb(des3_ede)", .drv_name = "cfb-3des", .bsize = 8,
2273 .min_keysize = HIFN_3DES_KEY_LENGTH,
2274 .max_keysize = HIFN_3DES_KEY_LENGTH,
2275 .setkey = hifn_setkey,
2276 .encrypt = hifn_encrypt_3des_cfb,
2277 .decrypt = hifn_decrypt_3des_cfb,
2281 .name = "ofb(des3_ede)", .drv_name = "ofb-3des", .bsize = 8,
2283 .min_keysize = HIFN_3DES_KEY_LENGTH,
2284 .max_keysize = HIFN_3DES_KEY_LENGTH,
2285 .setkey = hifn_setkey,
2286 .encrypt = hifn_encrypt_3des_ofb,
2287 .decrypt = hifn_decrypt_3des_ofb,
2291 .name = "cbc(des3_ede)", .drv_name = "cbc-3des", .bsize = 8,
2293 .ivsize = HIFN_IV_LENGTH,
2294 .min_keysize = HIFN_3DES_KEY_LENGTH,
2295 .max_keysize = HIFN_3DES_KEY_LENGTH,
2296 .setkey = hifn_setkey,
2297 .encrypt = hifn_encrypt_3des_cbc,
2298 .decrypt = hifn_decrypt_3des_cbc,
2302 .name = "ecb(des3_ede)", .drv_name = "ecb-3des", .bsize = 8,
2304 .min_keysize = HIFN_3DES_KEY_LENGTH,
2305 .max_keysize = HIFN_3DES_KEY_LENGTH,
2306 .setkey = hifn_setkey,
2307 .encrypt = hifn_encrypt_3des_ecb,
2308 .decrypt = hifn_decrypt_3des_ecb,
2313 * DES ECB, CBC, CFB and OFB modes.
2316 .name = "cfb(des)", .drv_name = "cfb-des", .bsize = 8,
2318 .min_keysize = HIFN_DES_KEY_LENGTH,
2319 .max_keysize = HIFN_DES_KEY_LENGTH,
2320 .setkey = hifn_setkey,
2321 .encrypt = hifn_encrypt_des_cfb,
2322 .decrypt = hifn_decrypt_des_cfb,
2326 .name = "ofb(des)", .drv_name = "ofb-des", .bsize = 8,
2328 .min_keysize = HIFN_DES_KEY_LENGTH,
2329 .max_keysize = HIFN_DES_KEY_LENGTH,
2330 .setkey = hifn_setkey,
2331 .encrypt = hifn_encrypt_des_ofb,
2332 .decrypt = hifn_decrypt_des_ofb,
2336 .name = "cbc(des)", .drv_name = "cbc-des", .bsize = 8,
2338 .ivsize = HIFN_IV_LENGTH,
2339 .min_keysize = HIFN_DES_KEY_LENGTH,
2340 .max_keysize = HIFN_DES_KEY_LENGTH,
2341 .setkey = hifn_setkey,
2342 .encrypt = hifn_encrypt_des_cbc,
2343 .decrypt = hifn_decrypt_des_cbc,
2347 .name = "ecb(des)", .drv_name = "ecb-des", .bsize = 8,
2349 .min_keysize = HIFN_DES_KEY_LENGTH,
2350 .max_keysize = HIFN_DES_KEY_LENGTH,
2351 .setkey = hifn_setkey,
2352 .encrypt = hifn_encrypt_des_ecb,
2353 .decrypt = hifn_decrypt_des_ecb,
2358 * AES ECB, CBC, CFB and OFB modes.
2361 .name = "ecb(aes)", .drv_name = "ecb-aes", .bsize = 16,
2363 .min_keysize = AES_MIN_KEY_SIZE,
2364 .max_keysize = AES_MAX_KEY_SIZE,
2365 .setkey = hifn_setkey,
2366 .encrypt = hifn_encrypt_aes_ecb,
2367 .decrypt = hifn_decrypt_aes_ecb,
2371 .name = "cbc(aes)", .drv_name = "cbc-aes", .bsize = 16,
2373 .ivsize = HIFN_AES_IV_LENGTH,
2374 .min_keysize = AES_MIN_KEY_SIZE,
2375 .max_keysize = AES_MAX_KEY_SIZE,
2376 .setkey = hifn_setkey,
2377 .encrypt = hifn_encrypt_aes_cbc,
2378 .decrypt = hifn_decrypt_aes_cbc,
2382 .name = "cfb(aes)", .drv_name = "cfb-aes", .bsize = 16,
2384 .min_keysize = AES_MIN_KEY_SIZE,
2385 .max_keysize = AES_MAX_KEY_SIZE,
2386 .setkey = hifn_setkey,
2387 .encrypt = hifn_encrypt_aes_cfb,
2388 .decrypt = hifn_decrypt_aes_cfb,
2392 .name = "ofb(aes)", .drv_name = "ofb-aes", .bsize = 16,
2394 .min_keysize = AES_MIN_KEY_SIZE,
2395 .max_keysize = AES_MAX_KEY_SIZE,
2396 .setkey = hifn_setkey,
2397 .encrypt = hifn_encrypt_aes_ofb,
2398 .decrypt = hifn_decrypt_aes_ofb,
2403 static int hifn_cra_init(struct crypto_tfm *tfm)
2405 struct crypto_alg *alg = tfm->__crt_alg;
2406 struct hifn_crypto_alg *ha = crypto_alg_to_hifn(alg);
2407 struct hifn_context *ctx = crypto_tfm_ctx(tfm);
2410 tfm->crt_ablkcipher.reqsize = sizeof(struct hifn_request_context);
2414 static int hifn_alg_alloc(struct hifn_device *dev, struct hifn_alg_template *t)
2416 struct hifn_crypto_alg *alg;
2419 alg = kzalloc(sizeof(struct hifn_crypto_alg), GFP_KERNEL);
2423 snprintf(alg->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s", t->name);
2424 snprintf(alg->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s-%s",
2425 t->drv_name, dev->name);
2427 alg->alg.cra_priority = 300;
2428 alg->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2429 CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC;
2430 alg->alg.cra_blocksize = t->bsize;
2431 alg->alg.cra_ctxsize = sizeof(struct hifn_context);
2432 alg->alg.cra_alignmask = 0;
2433 alg->alg.cra_type = &crypto_ablkcipher_type;
2434 alg->alg.cra_module = THIS_MODULE;
2435 alg->alg.cra_u.ablkcipher = t->ablkcipher;
2436 alg->alg.cra_init = hifn_cra_init;
2440 list_add_tail(&alg->entry, &dev->alg_list);
2442 err = crypto_register_alg(&alg->alg);
2444 list_del(&alg->entry);
2451 static void hifn_unregister_alg(struct hifn_device *dev)
2453 struct hifn_crypto_alg *a, *n;
2455 list_for_each_entry_safe(a, n, &dev->alg_list, entry) {
2456 list_del(&a->entry);
2457 crypto_unregister_alg(&a->alg);
2462 static int hifn_register_alg(struct hifn_device *dev)
2466 for (i=0; i<ARRAY_SIZE(hifn_alg_templates); ++i) {
2467 err = hifn_alg_alloc(dev, &hifn_alg_templates[i]);
2475 hifn_unregister_alg(dev);
2479 static void hifn_tasklet_callback(unsigned long data)
2481 struct hifn_device *dev = (struct hifn_device *)data;
2484 * This is ok to call this without lock being held,
2485 * althogh it modifies some parameters used in parallel,
2486 * (like dev->success), but they are used in process
2487 * context or update is atomic (like setting dev->sa[i] to NULL).
2489 hifn_clear_rings(dev, 0);
2491 if (dev->started < HIFN_QUEUE_LENGTH && dev->queue.qlen)
2492 hifn_process_queue(dev);
2495 static int hifn_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2498 struct hifn_device *dev;
2501 err = pci_enable_device(pdev);
2504 pci_set_master(pdev);
2506 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2508 goto err_out_disable_pci_device;
2510 snprintf(name, sizeof(name), "hifn%d",
2511 atomic_inc_return(&hifn_dev_number)-1);
2513 err = pci_request_regions(pdev, name);
2515 goto err_out_disable_pci_device;
2517 if (pci_resource_len(pdev, 0) < HIFN_BAR0_SIZE ||
2518 pci_resource_len(pdev, 1) < HIFN_BAR1_SIZE ||
2519 pci_resource_len(pdev, 2) < HIFN_BAR2_SIZE) {
2520 dev_err(&pdev->dev, "Broken hardware - I/O regions are too small.\n");
2522 goto err_out_free_regions;
2525 dev = kzalloc(sizeof(struct hifn_device) + sizeof(struct crypto_alg),
2529 goto err_out_free_regions;
2532 INIT_LIST_HEAD(&dev->alg_list);
2534 snprintf(dev->name, sizeof(dev->name), "%s", name);
2535 spin_lock_init(&dev->lock);
2537 for (i=0; i<3; ++i) {
2538 unsigned long addr, size;
2540 addr = pci_resource_start(pdev, i);
2541 size = pci_resource_len(pdev, i);
2543 dev->bar[i] = ioremap_nocache(addr, size);
2546 goto err_out_unmap_bars;
2550 dev->desc_virt = pci_zalloc_consistent(pdev, sizeof(struct hifn_dma),
2552 if (!dev->desc_virt) {
2553 dev_err(&pdev->dev, "Failed to allocate descriptor rings.\n");
2555 goto err_out_unmap_bars;
2559 dev->irq = pdev->irq;
2561 for (i=0; i<HIFN_D_RES_RSIZE; ++i)
2564 pci_set_drvdata(pdev, dev);
2566 tasklet_init(&dev->tasklet, hifn_tasklet_callback, (unsigned long)dev);
2568 crypto_init_queue(&dev->queue, 1);
2570 err = request_irq(dev->irq, hifn_interrupt, IRQF_SHARED, dev->name, dev);
2572 dev_err(&pdev->dev, "Failed to request IRQ%d: err: %d.\n",
2575 goto err_out_free_desc;
2578 err = hifn_start_device(dev);
2580 goto err_out_free_irq;
2582 err = hifn_register_rng(dev);
2584 goto err_out_stop_device;
2586 err = hifn_register_alg(dev);
2588 goto err_out_unregister_rng;
2590 INIT_DELAYED_WORK(&dev->work, hifn_work);
2591 schedule_delayed_work(&dev->work, HZ);
2593 dev_dbg(&pdev->dev, "HIFN crypto accelerator card at %s has been "
2594 "successfully registered as %s.\n",
2595 pci_name(pdev), dev->name);
2599 err_out_unregister_rng:
2600 hifn_unregister_rng(dev);
2601 err_out_stop_device:
2602 hifn_reset_dma(dev, 1);
2603 hifn_stop_device(dev);
2605 free_irq(dev->irq, dev);
2606 tasklet_kill(&dev->tasklet);
2608 pci_free_consistent(pdev, sizeof(struct hifn_dma),
2609 dev->desc_virt, dev->desc_dma);
2614 iounmap(dev->bar[i]);
2616 err_out_free_regions:
2617 pci_release_regions(pdev);
2619 err_out_disable_pci_device:
2620 pci_disable_device(pdev);
2625 static void hifn_remove(struct pci_dev *pdev)
2628 struct hifn_device *dev;
2630 dev = pci_get_drvdata(pdev);
2633 cancel_delayed_work_sync(&dev->work);
2635 hifn_unregister_rng(dev);
2636 hifn_unregister_alg(dev);
2637 hifn_reset_dma(dev, 1);
2638 hifn_stop_device(dev);
2640 free_irq(dev->irq, dev);
2641 tasklet_kill(&dev->tasklet);
2645 pci_free_consistent(pdev, sizeof(struct hifn_dma),
2646 dev->desc_virt, dev->desc_dma);
2649 iounmap(dev->bar[i]);
2654 pci_release_regions(pdev);
2655 pci_disable_device(pdev);
2658 static struct pci_device_id hifn_pci_tbl[] = {
2659 { PCI_DEVICE(PCI_VENDOR_ID_HIFN, PCI_DEVICE_ID_HIFN_7955) },
2660 { PCI_DEVICE(PCI_VENDOR_ID_HIFN, PCI_DEVICE_ID_HIFN_7956) },
2663 MODULE_DEVICE_TABLE(pci, hifn_pci_tbl);
2665 static struct pci_driver hifn_pci_driver = {
2667 .id_table = hifn_pci_tbl,
2668 .probe = hifn_probe,
2669 .remove = hifn_remove,
2672 static int __init hifn_init(void)
2677 /* HIFN supports only 32-bit addresses */
2678 BUILD_BUG_ON(sizeof(dma_addr_t) != 4);
2680 if (strncmp(hifn_pll_ref, "ext", 3) &&
2681 strncmp(hifn_pll_ref, "pci", 3)) {
2682 pr_err("hifn795x: invalid hifn_pll_ref clock, must be pci or ext");
2687 * For the 7955/7956 the reference clock frequency must be in the
2688 * range of 20MHz-100MHz. For the 7954 the upper bound is 66.67MHz,
2689 * but this chip is currently not supported.
2691 if (hifn_pll_ref[3] != '\0') {
2692 freq = simple_strtoul(hifn_pll_ref + 3, NULL, 10);
2693 if (freq < 20 || freq > 100) {
2694 pr_err("hifn795x: invalid hifn_pll_ref frequency, must"
2695 "be in the range of 20-100");
2700 err = pci_register_driver(&hifn_pci_driver);
2702 pr_err("Failed to register PCI driver for %s device.\n",
2703 hifn_pci_driver.name);
2707 pr_info("Driver for HIFN 795x crypto accelerator chip "
2708 "has been successfully registered.\n");
2713 static void __exit hifn_fini(void)
2715 pci_unregister_driver(&hifn_pci_driver);
2717 pr_info("Driver for HIFN 795x crypto accelerator chip "
2718 "has been successfully unregistered.\n");
2721 module_init(hifn_init);
2722 module_exit(hifn_fini);
2724 MODULE_LICENSE("GPL");
2725 MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
2726 MODULE_DESCRIPTION("Driver for HIFN 795x crypto accelerator chip.");