blob: 4e89cd8f664f3310b212c9eef4d17865156dca8d [file] [log] [blame]
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001/*
2 * 2007+ Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
3 * All rights reserved.
4 *
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.
9 *
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.
14 *
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
18 */
19
20#include <linux/kernel.h>
21#include <linux/module.h>
Patrick McHardy37a80232007-11-21 12:47:13 +080022#include <linux/moduleparam.h>
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +080023#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>
28#include <linux/mm.h>
Andrew Morton102d49d2007-11-13 21:55:28 +080029#include <linux/dma-mapping.h>
30#include <linux/scatterlist.h>
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +080031#include <linux/highmem.h>
Evgeniy Polyakova1e6ef22007-11-10 20:24:18 +080032#include <linux/interrupt.h>
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +080033#include <linux/crypto.h>
Patrick McHardyfcd06752007-11-21 12:51:52 +080034#include <linux/hw_random.h>
35#include <linux/ktime.h>
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +080036
37#include <crypto/algapi.h>
Evgeniy Polyakovc3041f92007-10-11 19:58:16 +080038#include <crypto/des.h>
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +080039
40#include <asm/kmap_types.h>
41
42#undef dprintk
43
44#define HIFN_TEST
45//#define HIFN_DEBUG
46
47#ifdef HIFN_DEBUG
48#define dprintk(f, a...) printk(f, ##a)
49#else
50#define dprintk(f, a...) do {} while (0)
51#endif
52
Patrick McHardy37a80232007-11-21 12:47:13 +080053static char hifn_pll_ref[sizeof("extNNN")] = "ext";
54module_param_string(hifn_pll_ref, hifn_pll_ref, sizeof(hifn_pll_ref), 0444);
55MODULE_PARM_DESC(hifn_pll_ref,
56 "PLL reference clock (pci[freq] or ext[freq], default ext)");
57
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +080058static atomic_t hifn_dev_number;
59
60#define ACRYPTO_OP_DECRYPT 0
61#define ACRYPTO_OP_ENCRYPT 1
62#define ACRYPTO_OP_HMAC 2
63#define ACRYPTO_OP_RNG 3
64
65#define ACRYPTO_MODE_ECB 0
66#define ACRYPTO_MODE_CBC 1
67#define ACRYPTO_MODE_CFB 2
68#define ACRYPTO_MODE_OFB 3
69
70#define ACRYPTO_TYPE_AES_128 0
71#define ACRYPTO_TYPE_AES_192 1
72#define ACRYPTO_TYPE_AES_256 2
73#define ACRYPTO_TYPE_3DES 3
74#define ACRYPTO_TYPE_DES 4
75
76#define PCI_VENDOR_ID_HIFN 0x13A3
77#define PCI_DEVICE_ID_HIFN_7955 0x0020
78#define PCI_DEVICE_ID_HIFN_7956 0x001d
79
80/* I/O region sizes */
81
82#define HIFN_BAR0_SIZE 0x1000
83#define HIFN_BAR1_SIZE 0x2000
84#define HIFN_BAR2_SIZE 0x8000
85
86/* DMA registres */
87
88#define HIFN_DMA_CRA 0x0C /* DMA Command Ring Address */
89#define HIFN_DMA_SDRA 0x1C /* DMA Source Data Ring Address */
90#define HIFN_DMA_RRA 0x2C /* DMA Result Ring Address */
91#define HIFN_DMA_DDRA 0x3C /* DMA Destination Data Ring Address */
92#define HIFN_DMA_STCTL 0x40 /* DMA Status and Control */
93#define HIFN_DMA_INTREN 0x44 /* DMA Interrupt Enable */
94#define HIFN_DMA_CFG1 0x48 /* DMA Configuration #1 */
95#define HIFN_DMA_CFG2 0x6C /* DMA Configuration #2 */
96#define HIFN_CHIP_ID 0x98 /* Chip ID */
97
98/*
99 * Processing Unit Registers (offset from BASEREG0)
100 */
101#define HIFN_0_PUDATA 0x00 /* Processing Unit Data */
102#define HIFN_0_PUCTRL 0x04 /* Processing Unit Control */
103#define HIFN_0_PUISR 0x08 /* Processing Unit Interrupt Status */
104#define HIFN_0_PUCNFG 0x0c /* Processing Unit Configuration */
105#define HIFN_0_PUIER 0x10 /* Processing Unit Interrupt Enable */
106#define HIFN_0_PUSTAT 0x14 /* Processing Unit Status/Chip ID */
107#define HIFN_0_FIFOSTAT 0x18 /* FIFO Status */
108#define HIFN_0_FIFOCNFG 0x1c /* FIFO Configuration */
109#define HIFN_0_SPACESIZE 0x20 /* Register space size */
110
111/* Processing Unit Control Register (HIFN_0_PUCTRL) */
112#define HIFN_PUCTRL_CLRSRCFIFO 0x0010 /* clear source fifo */
113#define HIFN_PUCTRL_STOP 0x0008 /* stop pu */
114#define HIFN_PUCTRL_LOCKRAM 0x0004 /* lock ram */
115#define HIFN_PUCTRL_DMAENA 0x0002 /* enable dma */
116#define HIFN_PUCTRL_RESET 0x0001 /* Reset processing unit */
117
118/* Processing Unit Interrupt Status Register (HIFN_0_PUISR) */
119#define HIFN_PUISR_CMDINVAL 0x8000 /* Invalid command interrupt */
120#define HIFN_PUISR_DATAERR 0x4000 /* Data error interrupt */
121#define HIFN_PUISR_SRCFIFO 0x2000 /* Source FIFO ready interrupt */
122#define HIFN_PUISR_DSTFIFO 0x1000 /* Destination FIFO ready interrupt */
123#define HIFN_PUISR_DSTOVER 0x0200 /* Destination overrun interrupt */
124#define HIFN_PUISR_SRCCMD 0x0080 /* Source command interrupt */
125#define HIFN_PUISR_SRCCTX 0x0040 /* Source context interrupt */
126#define HIFN_PUISR_SRCDATA 0x0020 /* Source data interrupt */
127#define HIFN_PUISR_DSTDATA 0x0010 /* Destination data interrupt */
128#define HIFN_PUISR_DSTRESULT 0x0004 /* Destination result interrupt */
129
130/* Processing Unit Configuration Register (HIFN_0_PUCNFG) */
131#define HIFN_PUCNFG_DRAMMASK 0xe000 /* DRAM size mask */
132#define HIFN_PUCNFG_DSZ_256K 0x0000 /* 256k dram */
133#define HIFN_PUCNFG_DSZ_512K 0x2000 /* 512k dram */
134#define HIFN_PUCNFG_DSZ_1M 0x4000 /* 1m dram */
135#define HIFN_PUCNFG_DSZ_2M 0x6000 /* 2m dram */
136#define HIFN_PUCNFG_DSZ_4M 0x8000 /* 4m dram */
137#define HIFN_PUCNFG_DSZ_8M 0xa000 /* 8m dram */
138#define HIFN_PUNCFG_DSZ_16M 0xc000 /* 16m dram */
139#define HIFN_PUCNFG_DSZ_32M 0xe000 /* 32m dram */
140#define HIFN_PUCNFG_DRAMREFRESH 0x1800 /* DRAM refresh rate mask */
141#define HIFN_PUCNFG_DRFR_512 0x0000 /* 512 divisor of ECLK */
142#define HIFN_PUCNFG_DRFR_256 0x0800 /* 256 divisor of ECLK */
143#define HIFN_PUCNFG_DRFR_128 0x1000 /* 128 divisor of ECLK */
144#define HIFN_PUCNFG_TCALLPHASES 0x0200 /* your guess is as good as mine... */
145#define HIFN_PUCNFG_TCDRVTOTEM 0x0100 /* your guess is as good as mine... */
146#define HIFN_PUCNFG_BIGENDIAN 0x0080 /* DMA big endian mode */
147#define HIFN_PUCNFG_BUS32 0x0040 /* Bus width 32bits */
148#define HIFN_PUCNFG_BUS16 0x0000 /* Bus width 16 bits */
149#define HIFN_PUCNFG_CHIPID 0x0020 /* Allow chipid from PUSTAT */
150#define HIFN_PUCNFG_DRAM 0x0010 /* Context RAM is DRAM */
151#define HIFN_PUCNFG_SRAM 0x0000 /* Context RAM is SRAM */
152#define HIFN_PUCNFG_COMPSING 0x0004 /* Enable single compression context */
153#define HIFN_PUCNFG_ENCCNFG 0x0002 /* Encryption configuration */
154
155/* Processing Unit Interrupt Enable Register (HIFN_0_PUIER) */
156#define HIFN_PUIER_CMDINVAL 0x8000 /* Invalid command interrupt */
157#define HIFN_PUIER_DATAERR 0x4000 /* Data error interrupt */
158#define HIFN_PUIER_SRCFIFO 0x2000 /* Source FIFO ready interrupt */
159#define HIFN_PUIER_DSTFIFO 0x1000 /* Destination FIFO ready interrupt */
160#define HIFN_PUIER_DSTOVER 0x0200 /* Destination overrun interrupt */
161#define HIFN_PUIER_SRCCMD 0x0080 /* Source command interrupt */
162#define HIFN_PUIER_SRCCTX 0x0040 /* Source context interrupt */
163#define HIFN_PUIER_SRCDATA 0x0020 /* Source data interrupt */
164#define HIFN_PUIER_DSTDATA 0x0010 /* Destination data interrupt */
165#define HIFN_PUIER_DSTRESULT 0x0004 /* Destination result interrupt */
166
167/* Processing Unit Status Register/Chip ID (HIFN_0_PUSTAT) */
168#define HIFN_PUSTAT_CMDINVAL 0x8000 /* Invalid command interrupt */
169#define HIFN_PUSTAT_DATAERR 0x4000 /* Data error interrupt */
170#define HIFN_PUSTAT_SRCFIFO 0x2000 /* Source FIFO ready interrupt */
171#define HIFN_PUSTAT_DSTFIFO 0x1000 /* Destination FIFO ready interrupt */
172#define HIFN_PUSTAT_DSTOVER 0x0200 /* Destination overrun interrupt */
173#define HIFN_PUSTAT_SRCCMD 0x0080 /* Source command interrupt */
174#define HIFN_PUSTAT_SRCCTX 0x0040 /* Source context interrupt */
175#define HIFN_PUSTAT_SRCDATA 0x0020 /* Source data interrupt */
176#define HIFN_PUSTAT_DSTDATA 0x0010 /* Destination data interrupt */
177#define HIFN_PUSTAT_DSTRESULT 0x0004 /* Destination result interrupt */
178#define HIFN_PUSTAT_CHIPREV 0x00ff /* Chip revision mask */
179#define HIFN_PUSTAT_CHIPENA 0xff00 /* Chip enabled mask */
180#define HIFN_PUSTAT_ENA_2 0x1100 /* Level 2 enabled */
181#define HIFN_PUSTAT_ENA_1 0x1000 /* Level 1 enabled */
182#define HIFN_PUSTAT_ENA_0 0x3000 /* Level 0 enabled */
183#define HIFN_PUSTAT_REV_2 0x0020 /* 7751 PT6/2 */
184#define HIFN_PUSTAT_REV_3 0x0030 /* 7751 PT6/3 */
185
186/* FIFO Status Register (HIFN_0_FIFOSTAT) */
187#define HIFN_FIFOSTAT_SRC 0x7f00 /* Source FIFO available */
188#define HIFN_FIFOSTAT_DST 0x007f /* Destination FIFO available */
189
190/* FIFO Configuration Register (HIFN_0_FIFOCNFG) */
191#define HIFN_FIFOCNFG_THRESHOLD 0x0400 /* must be written as 1 */
192
193/*
194 * DMA Interface Registers (offset from BASEREG1)
195 */
196#define HIFN_1_DMA_CRAR 0x0c /* DMA Command Ring Address */
197#define HIFN_1_DMA_SRAR 0x1c /* DMA Source Ring Address */
198#define HIFN_1_DMA_RRAR 0x2c /* DMA Result Ring Address */
199#define HIFN_1_DMA_DRAR 0x3c /* DMA Destination Ring Address */
200#define HIFN_1_DMA_CSR 0x40 /* DMA Status and Control */
201#define HIFN_1_DMA_IER 0x44 /* DMA Interrupt Enable */
202#define HIFN_1_DMA_CNFG 0x48 /* DMA Configuration */
203#define HIFN_1_PLL 0x4c /* 795x: PLL config */
204#define HIFN_1_7811_RNGENA 0x60 /* 7811: rng enable */
205#define HIFN_1_7811_RNGCFG 0x64 /* 7811: rng config */
206#define HIFN_1_7811_RNGDAT 0x68 /* 7811: rng data */
207#define HIFN_1_7811_RNGSTS 0x6c /* 7811: rng status */
208#define HIFN_1_7811_MIPSRST 0x94 /* 7811: MIPS reset */
209#define HIFN_1_REVID 0x98 /* Revision ID */
210#define HIFN_1_UNLOCK_SECRET1 0xf4
211#define HIFN_1_UNLOCK_SECRET2 0xfc
212#define HIFN_1_PUB_RESET 0x204 /* Public/RNG Reset */
213#define HIFN_1_PUB_BASE 0x300 /* Public Base Address */
214#define HIFN_1_PUB_OPLEN 0x304 /* Public Operand Length */
215#define HIFN_1_PUB_OP 0x308 /* Public Operand */
216#define HIFN_1_PUB_STATUS 0x30c /* Public Status */
217#define HIFN_1_PUB_IEN 0x310 /* Public Interrupt enable */
218#define HIFN_1_RNG_CONFIG 0x314 /* RNG config */
219#define HIFN_1_RNG_DATA 0x318 /* RNG data */
220#define HIFN_1_PUB_MEM 0x400 /* start of Public key memory */
221#define HIFN_1_PUB_MEMEND 0xbff /* end of Public key memory */
222
223/* DMA Status and Control Register (HIFN_1_DMA_CSR) */
224#define HIFN_DMACSR_D_CTRLMASK 0xc0000000 /* Destinition Ring Control */
225#define HIFN_DMACSR_D_CTRL_NOP 0x00000000 /* Dest. Control: no-op */
226#define HIFN_DMACSR_D_CTRL_DIS 0x40000000 /* Dest. Control: disable */
227#define HIFN_DMACSR_D_CTRL_ENA 0x80000000 /* Dest. Control: enable */
228#define HIFN_DMACSR_D_ABORT 0x20000000 /* Destinition Ring PCIAbort */
229#define HIFN_DMACSR_D_DONE 0x10000000 /* Destinition Ring Done */
230#define HIFN_DMACSR_D_LAST 0x08000000 /* Destinition Ring Last */
231#define HIFN_DMACSR_D_WAIT 0x04000000 /* Destinition Ring Waiting */
232#define HIFN_DMACSR_D_OVER 0x02000000 /* Destinition Ring Overflow */
233#define HIFN_DMACSR_R_CTRL 0x00c00000 /* Result Ring Control */
234#define HIFN_DMACSR_R_CTRL_NOP 0x00000000 /* Result Control: no-op */
235#define HIFN_DMACSR_R_CTRL_DIS 0x00400000 /* Result Control: disable */
236#define HIFN_DMACSR_R_CTRL_ENA 0x00800000 /* Result Control: enable */
237#define HIFN_DMACSR_R_ABORT 0x00200000 /* Result Ring PCI Abort */
238#define HIFN_DMACSR_R_DONE 0x00100000 /* Result Ring Done */
239#define HIFN_DMACSR_R_LAST 0x00080000 /* Result Ring Last */
240#define HIFN_DMACSR_R_WAIT 0x00040000 /* Result Ring Waiting */
241#define HIFN_DMACSR_R_OVER 0x00020000 /* Result Ring Overflow */
242#define HIFN_DMACSR_S_CTRL 0x0000c000 /* Source Ring Control */
243#define HIFN_DMACSR_S_CTRL_NOP 0x00000000 /* Source Control: no-op */
244#define HIFN_DMACSR_S_CTRL_DIS 0x00004000 /* Source Control: disable */
245#define HIFN_DMACSR_S_CTRL_ENA 0x00008000 /* Source Control: enable */
246#define HIFN_DMACSR_S_ABORT 0x00002000 /* Source Ring PCI Abort */
247#define HIFN_DMACSR_S_DONE 0x00001000 /* Source Ring Done */
248#define HIFN_DMACSR_S_LAST 0x00000800 /* Source Ring Last */
249#define HIFN_DMACSR_S_WAIT 0x00000400 /* Source Ring Waiting */
250#define HIFN_DMACSR_ILLW 0x00000200 /* Illegal write (7811 only) */
251#define HIFN_DMACSR_ILLR 0x00000100 /* Illegal read (7811 only) */
252#define HIFN_DMACSR_C_CTRL 0x000000c0 /* Command Ring Control */
253#define HIFN_DMACSR_C_CTRL_NOP 0x00000000 /* Command Control: no-op */
254#define HIFN_DMACSR_C_CTRL_DIS 0x00000040 /* Command Control: disable */
255#define HIFN_DMACSR_C_CTRL_ENA 0x00000080 /* Command Control: enable */
256#define HIFN_DMACSR_C_ABORT 0x00000020 /* Command Ring PCI Abort */
257#define HIFN_DMACSR_C_DONE 0x00000010 /* Command Ring Done */
258#define HIFN_DMACSR_C_LAST 0x00000008 /* Command Ring Last */
259#define HIFN_DMACSR_C_WAIT 0x00000004 /* Command Ring Waiting */
260#define HIFN_DMACSR_PUBDONE 0x00000002 /* Public op done (7951 only) */
261#define HIFN_DMACSR_ENGINE 0x00000001 /* Command Ring Engine IRQ */
262
263/* DMA Interrupt Enable Register (HIFN_1_DMA_IER) */
264#define HIFN_DMAIER_D_ABORT 0x20000000 /* Destination Ring PCIAbort */
265#define HIFN_DMAIER_D_DONE 0x10000000 /* Destination Ring Done */
266#define HIFN_DMAIER_D_LAST 0x08000000 /* Destination Ring Last */
267#define HIFN_DMAIER_D_WAIT 0x04000000 /* Destination Ring Waiting */
268#define HIFN_DMAIER_D_OVER 0x02000000 /* Destination Ring Overflow */
269#define HIFN_DMAIER_R_ABORT 0x00200000 /* Result Ring PCI Abort */
270#define HIFN_DMAIER_R_DONE 0x00100000 /* Result Ring Done */
271#define HIFN_DMAIER_R_LAST 0x00080000 /* Result Ring Last */
272#define HIFN_DMAIER_R_WAIT 0x00040000 /* Result Ring Waiting */
273#define HIFN_DMAIER_R_OVER 0x00020000 /* Result Ring Overflow */
274#define HIFN_DMAIER_S_ABORT 0x00002000 /* Source Ring PCI Abort */
275#define HIFN_DMAIER_S_DONE 0x00001000 /* Source Ring Done */
276#define HIFN_DMAIER_S_LAST 0x00000800 /* Source Ring Last */
277#define HIFN_DMAIER_S_WAIT 0x00000400 /* Source Ring Waiting */
278#define HIFN_DMAIER_ILLW 0x00000200 /* Illegal write (7811 only) */
279#define HIFN_DMAIER_ILLR 0x00000100 /* Illegal read (7811 only) */
280#define HIFN_DMAIER_C_ABORT 0x00000020 /* Command Ring PCI Abort */
281#define HIFN_DMAIER_C_DONE 0x00000010 /* Command Ring Done */
282#define HIFN_DMAIER_C_LAST 0x00000008 /* Command Ring Last */
283#define HIFN_DMAIER_C_WAIT 0x00000004 /* Command Ring Waiting */
284#define HIFN_DMAIER_PUBDONE 0x00000002 /* public op done (7951 only) */
285#define HIFN_DMAIER_ENGINE 0x00000001 /* Engine IRQ */
286
287/* DMA Configuration Register (HIFN_1_DMA_CNFG) */
288#define HIFN_DMACNFG_BIGENDIAN 0x10000000 /* big endian mode */
289#define HIFN_DMACNFG_POLLFREQ 0x00ff0000 /* Poll frequency mask */
290#define HIFN_DMACNFG_UNLOCK 0x00000800
291#define HIFN_DMACNFG_POLLINVAL 0x00000700 /* Invalid Poll Scalar */
292#define HIFN_DMACNFG_LAST 0x00000010 /* Host control LAST bit */
293#define HIFN_DMACNFG_MODE 0x00000004 /* DMA mode */
294#define HIFN_DMACNFG_DMARESET 0x00000002 /* DMA Reset # */
295#define HIFN_DMACNFG_MSTRESET 0x00000001 /* Master Reset # */
296
Patrick McHardy37a80232007-11-21 12:47:13 +0800297/* PLL configuration register */
298#define HIFN_PLL_REF_CLK_HBI 0x00000000 /* HBI reference clock */
299#define HIFN_PLL_REF_CLK_PLL 0x00000001 /* PLL reference clock */
300#define HIFN_PLL_BP 0x00000002 /* Reference clock bypass */
301#define HIFN_PLL_PK_CLK_HBI 0x00000000 /* PK engine HBI clock */
302#define HIFN_PLL_PK_CLK_PLL 0x00000008 /* PK engine PLL clock */
303#define HIFN_PLL_PE_CLK_HBI 0x00000000 /* PE engine HBI clock */
304#define HIFN_PLL_PE_CLK_PLL 0x00000010 /* PE engine PLL clock */
305#define HIFN_PLL_RESERVED_1 0x00000400 /* Reserved bit, must be 1 */
306#define HIFN_PLL_ND_SHIFT 11 /* Clock multiplier shift */
307#define HIFN_PLL_ND_MULT_2 0x00000000 /* PLL clock multiplier 2 */
308#define HIFN_PLL_ND_MULT_4 0x00000800 /* PLL clock multiplier 4 */
309#define HIFN_PLL_ND_MULT_6 0x00001000 /* PLL clock multiplier 6 */
310#define HIFN_PLL_ND_MULT_8 0x00001800 /* PLL clock multiplier 8 */
311#define HIFN_PLL_ND_MULT_10 0x00002000 /* PLL clock multiplier 10 */
312#define HIFN_PLL_ND_MULT_12 0x00002800 /* PLL clock multiplier 12 */
313#define HIFN_PLL_IS_1_8 0x00000000 /* charge pump (mult. 1-8) */
314#define HIFN_PLL_IS_9_12 0x00010000 /* charge pump (mult. 9-12) */
315
316#define HIFN_PLL_FCK_MAX 266 /* Maximum PLL frequency */
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800317
318/* Public key reset register (HIFN_1_PUB_RESET) */
319#define HIFN_PUBRST_RESET 0x00000001 /* reset public/rng unit */
320
321/* Public base address register (HIFN_1_PUB_BASE) */
322#define HIFN_PUBBASE_ADDR 0x00003fff /* base address */
323
324/* Public operand length register (HIFN_1_PUB_OPLEN) */
325#define HIFN_PUBOPLEN_MOD_M 0x0000007f /* modulus length mask */
326#define HIFN_PUBOPLEN_MOD_S 0 /* modulus length shift */
327#define HIFN_PUBOPLEN_EXP_M 0x0003ff80 /* exponent length mask */
328#define HIFN_PUBOPLEN_EXP_S 7 /* exponent lenght shift */
329#define HIFN_PUBOPLEN_RED_M 0x003c0000 /* reducend length mask */
330#define HIFN_PUBOPLEN_RED_S 18 /* reducend length shift */
331
332/* Public operation register (HIFN_1_PUB_OP) */
333#define HIFN_PUBOP_AOFFSET_M 0x0000007f /* A offset mask */
334#define HIFN_PUBOP_AOFFSET_S 0 /* A offset shift */
335#define HIFN_PUBOP_BOFFSET_M 0x00000f80 /* B offset mask */
336#define HIFN_PUBOP_BOFFSET_S 7 /* B offset shift */
337#define HIFN_PUBOP_MOFFSET_M 0x0003f000 /* M offset mask */
338#define HIFN_PUBOP_MOFFSET_S 12 /* M offset shift */
339#define HIFN_PUBOP_OP_MASK 0x003c0000 /* Opcode: */
340#define HIFN_PUBOP_OP_NOP 0x00000000 /* NOP */
341#define HIFN_PUBOP_OP_ADD 0x00040000 /* ADD */
342#define HIFN_PUBOP_OP_ADDC 0x00080000 /* ADD w/carry */
343#define HIFN_PUBOP_OP_SUB 0x000c0000 /* SUB */
344#define HIFN_PUBOP_OP_SUBC 0x00100000 /* SUB w/carry */
345#define HIFN_PUBOP_OP_MODADD 0x00140000 /* Modular ADD */
346#define HIFN_PUBOP_OP_MODSUB 0x00180000 /* Modular SUB */
347#define HIFN_PUBOP_OP_INCA 0x001c0000 /* INC A */
348#define HIFN_PUBOP_OP_DECA 0x00200000 /* DEC A */
349#define HIFN_PUBOP_OP_MULT 0x00240000 /* MULT */
350#define HIFN_PUBOP_OP_MODMULT 0x00280000 /* Modular MULT */
351#define HIFN_PUBOP_OP_MODRED 0x002c0000 /* Modular RED */
352#define HIFN_PUBOP_OP_MODEXP 0x00300000 /* Modular EXP */
353
354/* Public status register (HIFN_1_PUB_STATUS) */
355#define HIFN_PUBSTS_DONE 0x00000001 /* operation done */
356#define HIFN_PUBSTS_CARRY 0x00000002 /* carry */
357
358/* Public interrupt enable register (HIFN_1_PUB_IEN) */
359#define HIFN_PUBIEN_DONE 0x00000001 /* operation done interrupt */
360
361/* Random number generator config register (HIFN_1_RNG_CONFIG) */
362#define HIFN_RNGCFG_ENA 0x00000001 /* enable rng */
363
364#define HIFN_NAMESIZE 32
365#define HIFN_MAX_RESULT_ORDER 5
366
367#define HIFN_D_CMD_RSIZE 24*4
368#define HIFN_D_SRC_RSIZE 80*4
369#define HIFN_D_DST_RSIZE 80*4
370#define HIFN_D_RES_RSIZE 24*4
371
372#define HIFN_QUEUE_LENGTH HIFN_D_CMD_RSIZE-5
373
374#define AES_MIN_KEY_SIZE 16
375#define AES_MAX_KEY_SIZE 32
376
377#define HIFN_DES_KEY_LENGTH 8
378#define HIFN_3DES_KEY_LENGTH 24
379#define HIFN_MAX_CRYPT_KEY_LENGTH AES_MAX_KEY_SIZE
380#define HIFN_IV_LENGTH 8
381#define HIFN_AES_IV_LENGTH 16
382#define HIFN_MAX_IV_LENGTH HIFN_AES_IV_LENGTH
383
384#define HIFN_MAC_KEY_LENGTH 64
385#define HIFN_MD5_LENGTH 16
386#define HIFN_SHA1_LENGTH 20
387#define HIFN_MAC_TRUNC_LENGTH 12
388
389#define HIFN_MAX_COMMAND (8 + 8 + 8 + 64 + 260)
390#define HIFN_MAX_RESULT (8 + 4 + 4 + 20 + 4)
391#define HIFN_USED_RESULT 12
392
393struct hifn_desc
394{
Al Viroe68970c2008-03-29 03:09:58 +0000395 volatile __le32 l;
396 volatile __le32 p;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800397};
398
399struct hifn_dma {
400 struct hifn_desc cmdr[HIFN_D_CMD_RSIZE+1];
401 struct hifn_desc srcr[HIFN_D_SRC_RSIZE+1];
402 struct hifn_desc dstr[HIFN_D_DST_RSIZE+1];
403 struct hifn_desc resr[HIFN_D_RES_RSIZE+1];
404
405 u8 command_bufs[HIFN_D_CMD_RSIZE][HIFN_MAX_COMMAND];
406 u8 result_bufs[HIFN_D_CMD_RSIZE][HIFN_MAX_RESULT];
407
408 u64 test_src, test_dst;
409
410 /*
411 * Our current positions for insertion and removal from the descriptor
412 * rings.
413 */
414 volatile int cmdi, srci, dsti, resi;
415 volatile int cmdu, srcu, dstu, resu;
416 int cmdk, srck, dstk, resk;
417};
418
419#define HIFN_FLAG_CMD_BUSY (1<<0)
420#define HIFN_FLAG_SRC_BUSY (1<<1)
421#define HIFN_FLAG_DST_BUSY (1<<2)
422#define HIFN_FLAG_RES_BUSY (1<<3)
423#define HIFN_FLAG_OLD_KEY (1<<4)
424
425#define HIFN_DEFAULT_ACTIVE_NUM 5
426
427struct hifn_device
428{
429 char name[HIFN_NAMESIZE];
430
431 int irq;
432
433 struct pci_dev *pdev;
434 void __iomem *bar[3];
435
436 unsigned long result_mem;
437 dma_addr_t dst;
438
439 void *desc_virt;
440 dma_addr_t desc_dma;
441
442 u32 dmareg;
443
444 void *sa[HIFN_D_RES_RSIZE];
445
446 spinlock_t lock;
447
448 void *priv;
449
450 u32 flags;
451 int active, started;
452 struct delayed_work work;
453 unsigned long reset;
454 unsigned long success;
455 unsigned long prev_success;
456
457 u8 snum;
458
Evgeniy Polyakova1e6ef22007-11-10 20:24:18 +0800459 struct tasklet_struct tasklet;
460
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800461 struct crypto_queue queue;
462 struct list_head alg_list;
Patrick McHardyfcd06752007-11-21 12:51:52 +0800463
464 unsigned int pk_clk_freq;
465
Patrick McHardyf881d822008-02-15 19:15:05 +0800466#ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
Patrick McHardyfcd06752007-11-21 12:51:52 +0800467 unsigned int rng_wait_time;
468 ktime_t rngtime;
469 struct hwrng rng;
470#endif
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800471};
472
473#define HIFN_D_LENGTH 0x0000ffff
474#define HIFN_D_NOINVALID 0x01000000
475#define HIFN_D_MASKDONEIRQ 0x02000000
476#define HIFN_D_DESTOVER 0x04000000
477#define HIFN_D_OVER 0x08000000
478#define HIFN_D_LAST 0x20000000
479#define HIFN_D_JUMP 0x40000000
480#define HIFN_D_VALID 0x80000000
481
482struct hifn_base_command
483{
Al Viroe68970c2008-03-29 03:09:58 +0000484 volatile __le16 masks;
485 volatile __le16 session_num;
486 volatile __le16 total_source_count;
487 volatile __le16 total_dest_count;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800488};
489
490#define HIFN_BASE_CMD_COMP 0x0100 /* enable compression engine */
491#define HIFN_BASE_CMD_PAD 0x0200 /* enable padding engine */
492#define HIFN_BASE_CMD_MAC 0x0400 /* enable MAC engine */
493#define HIFN_BASE_CMD_CRYPT 0x0800 /* enable crypt engine */
494#define HIFN_BASE_CMD_DECODE 0x2000
495#define HIFN_BASE_CMD_SRCLEN_M 0xc000
496#define HIFN_BASE_CMD_SRCLEN_S 14
497#define HIFN_BASE_CMD_DSTLEN_M 0x3000
498#define HIFN_BASE_CMD_DSTLEN_S 12
499#define HIFN_BASE_CMD_LENMASK_HI 0x30000
500#define HIFN_BASE_CMD_LENMASK_LO 0x0ffff
501
502/*
503 * Structure to help build up the command data structure.
504 */
505struct hifn_crypt_command
506{
Al Viroe68970c2008-03-29 03:09:58 +0000507 volatile __le16 masks;
508 volatile __le16 header_skip;
509 volatile __le16 source_count;
510 volatile __le16 reserved;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800511};
512
513#define HIFN_CRYPT_CMD_ALG_MASK 0x0003 /* algorithm: */
514#define HIFN_CRYPT_CMD_ALG_DES 0x0000 /* DES */
515#define HIFN_CRYPT_CMD_ALG_3DES 0x0001 /* 3DES */
516#define HIFN_CRYPT_CMD_ALG_RC4 0x0002 /* RC4 */
517#define HIFN_CRYPT_CMD_ALG_AES 0x0003 /* AES */
518#define HIFN_CRYPT_CMD_MODE_MASK 0x0018 /* Encrypt mode: */
519#define HIFN_CRYPT_CMD_MODE_ECB 0x0000 /* ECB */
520#define HIFN_CRYPT_CMD_MODE_CBC 0x0008 /* CBC */
521#define HIFN_CRYPT_CMD_MODE_CFB 0x0010 /* CFB */
522#define HIFN_CRYPT_CMD_MODE_OFB 0x0018 /* OFB */
523#define HIFN_CRYPT_CMD_CLR_CTX 0x0040 /* clear context */
524#define HIFN_CRYPT_CMD_KSZ_MASK 0x0600 /* AES key size: */
525#define HIFN_CRYPT_CMD_KSZ_128 0x0000 /* 128 bit */
526#define HIFN_CRYPT_CMD_KSZ_192 0x0200 /* 192 bit */
527#define HIFN_CRYPT_CMD_KSZ_256 0x0400 /* 256 bit */
528#define HIFN_CRYPT_CMD_NEW_KEY 0x0800 /* expect new key */
529#define HIFN_CRYPT_CMD_NEW_IV 0x1000 /* expect new iv */
530#define HIFN_CRYPT_CMD_SRCLEN_M 0xc000
531#define HIFN_CRYPT_CMD_SRCLEN_S 14
532
533/*
534 * Structure to help build up the command data structure.
535 */
536struct hifn_mac_command
537{
Patrick McHardy3c42cbc2008-05-07 22:28:27 +0800538 volatile __le16 masks;
539 volatile __le16 header_skip;
540 volatile __le16 source_count;
541 volatile __le16 reserved;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800542};
543
544#define HIFN_MAC_CMD_ALG_MASK 0x0001
545#define HIFN_MAC_CMD_ALG_SHA1 0x0000
546#define HIFN_MAC_CMD_ALG_MD5 0x0001
547#define HIFN_MAC_CMD_MODE_MASK 0x000c
548#define HIFN_MAC_CMD_MODE_HMAC 0x0000
549#define HIFN_MAC_CMD_MODE_SSL_MAC 0x0004
550#define HIFN_MAC_CMD_MODE_HASH 0x0008
551#define HIFN_MAC_CMD_MODE_FULL 0x0004
552#define HIFN_MAC_CMD_TRUNC 0x0010
553#define HIFN_MAC_CMD_RESULT 0x0020
554#define HIFN_MAC_CMD_APPEND 0x0040
555#define HIFN_MAC_CMD_SRCLEN_M 0xc000
556#define HIFN_MAC_CMD_SRCLEN_S 14
557
558/*
559 * MAC POS IPsec initiates authentication after encryption on encodes
560 * and before decryption on decodes.
561 */
562#define HIFN_MAC_CMD_POS_IPSEC 0x0200
563#define HIFN_MAC_CMD_NEW_KEY 0x0800
564
565struct hifn_comp_command
566{
Patrick McHardy3c42cbc2008-05-07 22:28:27 +0800567 volatile __le16 masks;
568 volatile __le16 header_skip;
569 volatile __le16 source_count;
570 volatile __le16 reserved;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800571};
572
573#define HIFN_COMP_CMD_SRCLEN_M 0xc000
574#define HIFN_COMP_CMD_SRCLEN_S 14
575#define HIFN_COMP_CMD_ONE 0x0100 /* must be one */
576#define HIFN_COMP_CMD_CLEARHIST 0x0010 /* clear history */
577#define HIFN_COMP_CMD_UPDATEHIST 0x0008 /* update history */
578#define HIFN_COMP_CMD_LZS_STRIP0 0x0004 /* LZS: strip zero */
579#define HIFN_COMP_CMD_MPPC_RESTART 0x0004 /* MPPC: restart */
580#define HIFN_COMP_CMD_ALG_MASK 0x0001 /* compression mode: */
581#define HIFN_COMP_CMD_ALG_MPPC 0x0001 /* MPPC */
582#define HIFN_COMP_CMD_ALG_LZS 0x0000 /* LZS */
583
584struct hifn_base_result
585{
Patrick McHardy3c42cbc2008-05-07 22:28:27 +0800586 volatile __le16 flags;
587 volatile __le16 session;
588 volatile __le16 src_cnt; /* 15:0 of source count */
589 volatile __le16 dst_cnt; /* 15:0 of dest count */
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800590};
591
592#define HIFN_BASE_RES_DSTOVERRUN 0x0200 /* destination overrun */
593#define HIFN_BASE_RES_SRCLEN_M 0xc000 /* 17:16 of source count */
594#define HIFN_BASE_RES_SRCLEN_S 14
595#define HIFN_BASE_RES_DSTLEN_M 0x3000 /* 17:16 of dest count */
596#define HIFN_BASE_RES_DSTLEN_S 12
597
598struct hifn_comp_result
599{
Patrick McHardy3c42cbc2008-05-07 22:28:27 +0800600 volatile __le16 flags;
601 volatile __le16 crc;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800602};
603
604#define HIFN_COMP_RES_LCB_M 0xff00 /* longitudinal check byte */
605#define HIFN_COMP_RES_LCB_S 8
606#define HIFN_COMP_RES_RESTART 0x0004 /* MPPC: restart */
607#define HIFN_COMP_RES_ENDMARKER 0x0002 /* LZS: end marker seen */
608#define HIFN_COMP_RES_SRC_NOTZERO 0x0001 /* source expired */
609
610struct hifn_mac_result
611{
Patrick McHardy3c42cbc2008-05-07 22:28:27 +0800612 volatile __le16 flags;
613 volatile __le16 reserved;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800614 /* followed by 0, 6, 8, or 10 u16's of the MAC, then crypt */
615};
616
617#define HIFN_MAC_RES_MISCOMPARE 0x0002 /* compare failed */
618#define HIFN_MAC_RES_SRC_NOTZERO 0x0001 /* source expired */
619
620struct hifn_crypt_result
621{
Patrick McHardy3c42cbc2008-05-07 22:28:27 +0800622 volatile __le16 flags;
623 volatile __le16 reserved;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800624};
625
626#define HIFN_CRYPT_RES_SRC_NOTZERO 0x0001 /* source expired */
627
628#ifndef HIFN_POLL_FREQUENCY
629#define HIFN_POLL_FREQUENCY 0x1
630#endif
631
632#ifndef HIFN_POLL_SCALAR
633#define HIFN_POLL_SCALAR 0x0
634#endif
635
636#define HIFN_MAX_SEGLEN 0xffff /* maximum dma segment len */
637#define HIFN_MAX_DMALEN 0x3ffff /* maximum dma length */
638
639struct hifn_crypto_alg
640{
641 struct list_head entry;
642 struct crypto_alg alg;
643 struct hifn_device *dev;
644};
645
646#define ASYNC_SCATTERLIST_CACHE 16
647
648#define ASYNC_FLAGS_MISALIGNED (1<<0)
649
650struct ablkcipher_walk
651{
652 struct scatterlist cache[ASYNC_SCATTERLIST_CACHE];
653 u32 flags;
654 int num;
655};
656
657struct hifn_context
658{
659 u8 key[HIFN_MAX_CRYPT_KEY_LENGTH], *iv;
660 struct hifn_device *dev;
661 unsigned int keysize, ivsize;
662 u8 op, type, mode, unused;
663 struct ablkcipher_walk walk;
664 atomic_t sg_num;
665};
666
Alexey Dobriyanb966b542008-01-08 21:36:34 +1100667#define crypto_alg_to_hifn(a) container_of(a, struct hifn_crypto_alg, alg)
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800668
669static inline u32 hifn_read_0(struct hifn_device *dev, u32 reg)
670{
671 u32 ret;
672
Al Viroe68970c2008-03-29 03:09:58 +0000673 ret = readl(dev->bar[0] + reg);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800674
675 return ret;
676}
677
678static inline u32 hifn_read_1(struct hifn_device *dev, u32 reg)
679{
680 u32 ret;
681
Al Viroe68970c2008-03-29 03:09:58 +0000682 ret = readl(dev->bar[1] + reg);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800683
684 return ret;
685}
686
687static inline void hifn_write_0(struct hifn_device *dev, u32 reg, u32 val)
688{
Patrick McHardy3c42cbc2008-05-07 22:28:27 +0800689 writel((__force u32)cpu_to_le32(val), dev->bar[0] + reg);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800690}
691
692static inline void hifn_write_1(struct hifn_device *dev, u32 reg, u32 val)
693{
Patrick McHardy3c42cbc2008-05-07 22:28:27 +0800694 writel((__force u32)cpu_to_le32(val), dev->bar[1] + reg);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800695}
696
697static void hifn_wait_puc(struct hifn_device *dev)
698{
699 int i;
700 u32 ret;
701
702 for (i=10000; i > 0; --i) {
703 ret = hifn_read_0(dev, HIFN_0_PUCTRL);
704 if (!(ret & HIFN_PUCTRL_RESET))
705 break;
706
707 udelay(1);
708 }
709
710 if (!i)
711 dprintk("%s: Failed to reset PUC unit.\n", dev->name);
712}
713
714static void hifn_reset_puc(struct hifn_device *dev)
715{
716 hifn_write_0(dev, HIFN_0_PUCTRL, HIFN_PUCTRL_DMAENA);
717 hifn_wait_puc(dev);
718}
719
720static void hifn_stop_device(struct hifn_device *dev)
721{
722 hifn_write_1(dev, HIFN_1_DMA_CSR,
723 HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS |
724 HIFN_DMACSR_S_CTRL_DIS | HIFN_DMACSR_C_CTRL_DIS);
725 hifn_write_0(dev, HIFN_0_PUIER, 0);
726 hifn_write_1(dev, HIFN_1_DMA_IER, 0);
727}
728
729static void hifn_reset_dma(struct hifn_device *dev, int full)
730{
731 hifn_stop_device(dev);
732
733 /*
734 * Setting poll frequency and others to 0.
735 */
736 hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
737 HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
738 mdelay(1);
739
740 /*
741 * Reset DMA.
742 */
743 if (full) {
744 hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MODE);
745 mdelay(1);
746 } else {
747 hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MODE |
748 HIFN_DMACNFG_MSTRESET);
749 hifn_reset_puc(dev);
750 }
751
752 hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
753 HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
754
755 hifn_reset_puc(dev);
756}
757
758static u32 hifn_next_signature(u_int32_t a, u_int cnt)
759{
760 int i;
761 u32 v;
762
763 for (i = 0; i < cnt; i++) {
764
765 /* get the parity */
766 v = a & 0x80080125;
767 v ^= v >> 16;
768 v ^= v >> 8;
769 v ^= v >> 4;
770 v ^= v >> 2;
771 v ^= v >> 1;
772
773 a = (v & 1) ^ (a << 1);
774 }
775
776 return a;
777}
778
779static struct pci2id {
780 u_short pci_vendor;
781 u_short pci_prod;
782 char card_id[13];
783} pci2id[] = {
784 {
785 PCI_VENDOR_ID_HIFN,
786 PCI_DEVICE_ID_HIFN_7955,
787 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
788 0x00, 0x00, 0x00, 0x00, 0x00 }
789 },
790 {
791 PCI_VENDOR_ID_HIFN,
792 PCI_DEVICE_ID_HIFN_7956,
793 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
794 0x00, 0x00, 0x00, 0x00, 0x00 }
795 }
796};
797
Patrick McHardyf881d822008-02-15 19:15:05 +0800798#ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
Patrick McHardyfcd06752007-11-21 12:51:52 +0800799static int hifn_rng_data_present(struct hwrng *rng, int wait)
800{
801 struct hifn_device *dev = (struct hifn_device *)rng->priv;
802 s64 nsec;
803
804 nsec = ktime_to_ns(ktime_sub(ktime_get(), dev->rngtime));
805 nsec -= dev->rng_wait_time;
806 if (nsec <= 0)
807 return 1;
808 if (!wait)
809 return 0;
810 ndelay(nsec);
811 return 1;
812}
813
814static int hifn_rng_data_read(struct hwrng *rng, u32 *data)
815{
816 struct hifn_device *dev = (struct hifn_device *)rng->priv;
817
818 *data = hifn_read_1(dev, HIFN_1_RNG_DATA);
819 dev->rngtime = ktime_get();
820 return 4;
821}
822
823static int hifn_register_rng(struct hifn_device *dev)
824{
825 /*
826 * We must wait at least 256 Pk_clk cycles between two reads of the rng.
827 */
828 dev->rng_wait_time = DIV_ROUND_UP(NSEC_PER_SEC, dev->pk_clk_freq) *
829 256;
830
831 dev->rng.name = dev->name;
832 dev->rng.data_present = hifn_rng_data_present,
833 dev->rng.data_read = hifn_rng_data_read,
834 dev->rng.priv = (unsigned long)dev;
835
836 return hwrng_register(&dev->rng);
837}
838
839static void hifn_unregister_rng(struct hifn_device *dev)
840{
841 hwrng_unregister(&dev->rng);
842}
843#else
844#define hifn_register_rng(dev) 0
845#define hifn_unregister_rng(dev)
846#endif
847
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800848static int hifn_init_pubrng(struct hifn_device *dev)
849{
850 int i;
851
852 hifn_write_1(dev, HIFN_1_PUB_RESET, hifn_read_1(dev, HIFN_1_PUB_RESET) |
853 HIFN_PUBRST_RESET);
854
855 for (i=100; i > 0; --i) {
856 mdelay(1);
857
858 if ((hifn_read_1(dev, HIFN_1_PUB_RESET) & HIFN_PUBRST_RESET) == 0)
859 break;
860 }
861
862 if (!i)
863 dprintk("Chip %s: Failed to initialise public key engine.\n",
864 dev->name);
865 else {
866 hifn_write_1(dev, HIFN_1_PUB_IEN, HIFN_PUBIEN_DONE);
867 dev->dmareg |= HIFN_DMAIER_PUBDONE;
868 hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
869
870 dprintk("Chip %s: Public key engine has been sucessfully "
871 "initialised.\n", dev->name);
872 }
873
874 /*
875 * Enable RNG engine.
876 */
877
878 hifn_write_1(dev, HIFN_1_RNG_CONFIG,
879 hifn_read_1(dev, HIFN_1_RNG_CONFIG) | HIFN_RNGCFG_ENA);
880 dprintk("Chip %s: RNG engine has been successfully initialised.\n",
881 dev->name);
882
Patrick McHardyf881d822008-02-15 19:15:05 +0800883#ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
Patrick McHardyfcd06752007-11-21 12:51:52 +0800884 /* First value must be discarded */
885 hifn_read_1(dev, HIFN_1_RNG_DATA);
886 dev->rngtime = ktime_get();
887#endif
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800888 return 0;
889}
890
891static int hifn_enable_crypto(struct hifn_device *dev)
892{
893 u32 dmacfg, addr;
894 char *offtbl = NULL;
895 int i;
896
897 for (i = 0; i < sizeof(pci2id)/sizeof(pci2id[0]); i++) {
898 if (pci2id[i].pci_vendor == dev->pdev->vendor &&
899 pci2id[i].pci_prod == dev->pdev->device) {
900 offtbl = pci2id[i].card_id;
901 break;
902 }
903 }
904
905 if (offtbl == NULL) {
906 dprintk("Chip %s: Unknown card!\n", dev->name);
907 return -ENODEV;
908 }
909
910 dmacfg = hifn_read_1(dev, HIFN_1_DMA_CNFG);
911
912 hifn_write_1(dev, HIFN_1_DMA_CNFG,
913 HIFN_DMACNFG_UNLOCK | HIFN_DMACNFG_MSTRESET |
914 HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
915 mdelay(1);
916 addr = hifn_read_1(dev, HIFN_1_UNLOCK_SECRET1);
917 mdelay(1);
918 hifn_write_1(dev, HIFN_1_UNLOCK_SECRET2, 0);
919 mdelay(1);
920
921 for (i=0; i<12; ++i) {
922 addr = hifn_next_signature(addr, offtbl[i] + 0x101);
923 hifn_write_1(dev, HIFN_1_UNLOCK_SECRET2, addr);
924
925 mdelay(1);
926 }
927 hifn_write_1(dev, HIFN_1_DMA_CNFG, dmacfg);
928
929 dprintk("Chip %s: %s.\n", dev->name, pci_name(dev->pdev));
930
931 return 0;
932}
933
934static void hifn_init_dma(struct hifn_device *dev)
935{
936 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
937 u32 dptr = dev->desc_dma;
938 int i;
939
940 for (i=0; i<HIFN_D_CMD_RSIZE; ++i)
941 dma->cmdr[i].p = __cpu_to_le32(dptr +
942 offsetof(struct hifn_dma, command_bufs[i][0]));
943 for (i=0; i<HIFN_D_RES_RSIZE; ++i)
944 dma->resr[i].p = __cpu_to_le32(dptr +
945 offsetof(struct hifn_dma, result_bufs[i][0]));
946
947 /*
948 * Setup LAST descriptors.
949 */
950 dma->cmdr[HIFN_D_CMD_RSIZE].p = __cpu_to_le32(dptr +
951 offsetof(struct hifn_dma, cmdr[0]));
952 dma->srcr[HIFN_D_SRC_RSIZE].p = __cpu_to_le32(dptr +
953 offsetof(struct hifn_dma, srcr[0]));
954 dma->dstr[HIFN_D_DST_RSIZE].p = __cpu_to_le32(dptr +
955 offsetof(struct hifn_dma, dstr[0]));
956 dma->resr[HIFN_D_RES_RSIZE].p = __cpu_to_le32(dptr +
957 offsetof(struct hifn_dma, resr[0]));
958
959 dma->cmdu = dma->srcu = dma->dstu = dma->resu = 0;
960 dma->cmdi = dma->srci = dma->dsti = dma->resi = 0;
961 dma->cmdk = dma->srck = dma->dstk = dma->resk = 0;
962}
963
Patrick McHardy37a80232007-11-21 12:47:13 +0800964/*
965 * Initialize the PLL. We need to know the frequency of the reference clock
966 * to calculate the optimal multiplier. For PCI we assume 66MHz, since that
967 * allows us to operate without the risk of overclocking the chip. If it
968 * actually uses 33MHz, the chip will operate at half the speed, this can be
969 * overriden by specifying the frequency as module parameter (pci33).
970 *
971 * Unfortunately the PCI clock is not very suitable since the HIFN needs a
972 * stable clock and the PCI clock frequency may vary, so the default is the
973 * external clock. There is no way to find out its frequency, we default to
974 * 66MHz since according to Mike Ham of HiFn, almost every board in existence
975 * has an external crystal populated at 66MHz.
976 */
977static void hifn_init_pll(struct hifn_device *dev)
978{
979 unsigned int freq, m;
980 u32 pllcfg;
981
982 pllcfg = HIFN_1_PLL | HIFN_PLL_RESERVED_1;
983
984 if (strncmp(hifn_pll_ref, "ext", 3) == 0)
985 pllcfg |= HIFN_PLL_REF_CLK_PLL;
986 else
987 pllcfg |= HIFN_PLL_REF_CLK_HBI;
988
989 if (hifn_pll_ref[3] != '\0')
990 freq = simple_strtoul(hifn_pll_ref + 3, NULL, 10);
991 else {
992 freq = 66;
993 printk(KERN_INFO "hifn795x: assuming %uMHz clock speed, "
994 "override with hifn_pll_ref=%.3s<frequency>\n",
995 freq, hifn_pll_ref);
996 }
997
998 m = HIFN_PLL_FCK_MAX / freq;
999
1000 pllcfg |= (m / 2 - 1) << HIFN_PLL_ND_SHIFT;
1001 if (m <= 8)
1002 pllcfg |= HIFN_PLL_IS_1_8;
1003 else
1004 pllcfg |= HIFN_PLL_IS_9_12;
1005
1006 /* Select clock source and enable clock bypass */
1007 hifn_write_1(dev, HIFN_1_PLL, pllcfg |
1008 HIFN_PLL_PK_CLK_HBI | HIFN_PLL_PE_CLK_HBI | HIFN_PLL_BP);
1009
1010 /* Let the chip lock to the input clock */
1011 mdelay(10);
1012
1013 /* Disable clock bypass */
1014 hifn_write_1(dev, HIFN_1_PLL, pllcfg |
1015 HIFN_PLL_PK_CLK_HBI | HIFN_PLL_PE_CLK_HBI);
1016
1017 /* Switch the engines to the PLL */
1018 hifn_write_1(dev, HIFN_1_PLL, pllcfg |
1019 HIFN_PLL_PK_CLK_PLL | HIFN_PLL_PE_CLK_PLL);
Patrick McHardyfcd06752007-11-21 12:51:52 +08001020
1021 /*
1022 * The Fpk_clk runs at half the total speed. Its frequency is needed to
1023 * calculate the minimum time between two reads of the rng. Since 33MHz
1024 * is actually 33.333... we overestimate the frequency here, resulting
1025 * in slightly larger intervals.
1026 */
1027 dev->pk_clk_freq = 1000000 * (freq + 1) * m / 2;
Patrick McHardy37a80232007-11-21 12:47:13 +08001028}
1029
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001030static void hifn_init_registers(struct hifn_device *dev)
1031{
1032 u32 dptr = dev->desc_dma;
1033
1034 /* Initialization magic... */
1035 hifn_write_0(dev, HIFN_0_PUCTRL, HIFN_PUCTRL_DMAENA);
1036 hifn_write_0(dev, HIFN_0_FIFOCNFG, HIFN_FIFOCNFG_THRESHOLD);
1037 hifn_write_0(dev, HIFN_0_PUIER, HIFN_PUIER_DSTOVER);
1038
1039 /* write all 4 ring address registers */
Patrick McHardy3c42cbc2008-05-07 22:28:27 +08001040 hifn_write_1(dev, HIFN_1_DMA_CRAR, dptr +
1041 offsetof(struct hifn_dma, cmdr[0]));
1042 hifn_write_1(dev, HIFN_1_DMA_SRAR, dptr +
1043 offsetof(struct hifn_dma, srcr[0]));
1044 hifn_write_1(dev, HIFN_1_DMA_DRAR, dptr +
1045 offsetof(struct hifn_dma, dstr[0]));
1046 hifn_write_1(dev, HIFN_1_DMA_RRAR, dptr +
1047 offsetof(struct hifn_dma, resr[0]));
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001048
1049 mdelay(2);
1050#if 0
1051 hifn_write_1(dev, HIFN_1_DMA_CSR,
1052 HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS |
1053 HIFN_DMACSR_S_CTRL_DIS | HIFN_DMACSR_C_CTRL_DIS |
1054 HIFN_DMACSR_D_ABORT | HIFN_DMACSR_D_DONE | HIFN_DMACSR_D_LAST |
1055 HIFN_DMACSR_D_WAIT | HIFN_DMACSR_D_OVER |
1056 HIFN_DMACSR_R_ABORT | HIFN_DMACSR_R_DONE | HIFN_DMACSR_R_LAST |
1057 HIFN_DMACSR_R_WAIT | HIFN_DMACSR_R_OVER |
1058 HIFN_DMACSR_S_ABORT | HIFN_DMACSR_S_DONE | HIFN_DMACSR_S_LAST |
1059 HIFN_DMACSR_S_WAIT |
1060 HIFN_DMACSR_C_ABORT | HIFN_DMACSR_C_DONE | HIFN_DMACSR_C_LAST |
1061 HIFN_DMACSR_C_WAIT |
1062 HIFN_DMACSR_ENGINE |
1063 HIFN_DMACSR_PUBDONE);
1064#else
1065 hifn_write_1(dev, HIFN_1_DMA_CSR,
1066 HIFN_DMACSR_C_CTRL_ENA | HIFN_DMACSR_S_CTRL_ENA |
1067 HIFN_DMACSR_D_CTRL_ENA | HIFN_DMACSR_R_CTRL_ENA |
1068 HIFN_DMACSR_D_ABORT | HIFN_DMACSR_D_DONE | HIFN_DMACSR_D_LAST |
1069 HIFN_DMACSR_D_WAIT | HIFN_DMACSR_D_OVER |
1070 HIFN_DMACSR_R_ABORT | HIFN_DMACSR_R_DONE | HIFN_DMACSR_R_LAST |
1071 HIFN_DMACSR_R_WAIT | HIFN_DMACSR_R_OVER |
1072 HIFN_DMACSR_S_ABORT | HIFN_DMACSR_S_DONE | HIFN_DMACSR_S_LAST |
1073 HIFN_DMACSR_S_WAIT |
1074 HIFN_DMACSR_C_ABORT | HIFN_DMACSR_C_DONE | HIFN_DMACSR_C_LAST |
1075 HIFN_DMACSR_C_WAIT |
1076 HIFN_DMACSR_ENGINE |
1077 HIFN_DMACSR_PUBDONE);
1078#endif
1079 hifn_read_1(dev, HIFN_1_DMA_CSR);
1080
1081 dev->dmareg |= HIFN_DMAIER_R_DONE | HIFN_DMAIER_C_ABORT |
1082 HIFN_DMAIER_D_OVER | HIFN_DMAIER_R_OVER |
1083 HIFN_DMAIER_S_ABORT | HIFN_DMAIER_D_ABORT | HIFN_DMAIER_R_ABORT |
1084 HIFN_DMAIER_ENGINE;
1085 dev->dmareg &= ~HIFN_DMAIER_C_WAIT;
1086
1087 hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
1088 hifn_read_1(dev, HIFN_1_DMA_IER);
1089#if 0
1090 hifn_write_0(dev, HIFN_0_PUCNFG, HIFN_PUCNFG_ENCCNFG |
1091 HIFN_PUCNFG_DRFR_128 | HIFN_PUCNFG_TCALLPHASES |
1092 HIFN_PUCNFG_TCDRVTOTEM | HIFN_PUCNFG_BUS32 |
1093 HIFN_PUCNFG_DRAM);
1094#else
1095 hifn_write_0(dev, HIFN_0_PUCNFG, 0x10342);
1096#endif
Patrick McHardy37a80232007-11-21 12:47:13 +08001097 hifn_init_pll(dev);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001098
1099 hifn_write_0(dev, HIFN_0_PUISR, HIFN_PUISR_DSTOVER);
1100 hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
1101 HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE | HIFN_DMACNFG_LAST |
1102 ((HIFN_POLL_FREQUENCY << 16 ) & HIFN_DMACNFG_POLLFREQ) |
1103 ((HIFN_POLL_SCALAR << 8) & HIFN_DMACNFG_POLLINVAL));
1104}
1105
1106static int hifn_setup_base_command(struct hifn_device *dev, u8 *buf,
1107 unsigned dlen, unsigned slen, u16 mask, u8 snum)
1108{
1109 struct hifn_base_command *base_cmd;
1110 u8 *buf_pos = buf;
1111
1112 base_cmd = (struct hifn_base_command *)buf_pos;
1113 base_cmd->masks = __cpu_to_le16(mask);
1114 base_cmd->total_source_count =
1115 __cpu_to_le16(slen & HIFN_BASE_CMD_LENMASK_LO);
1116 base_cmd->total_dest_count =
1117 __cpu_to_le16(dlen & HIFN_BASE_CMD_LENMASK_LO);
1118
1119 dlen >>= 16;
1120 slen >>= 16;
1121 base_cmd->session_num = __cpu_to_le16(snum |
1122 ((slen << HIFN_BASE_CMD_SRCLEN_S) & HIFN_BASE_CMD_SRCLEN_M) |
1123 ((dlen << HIFN_BASE_CMD_DSTLEN_S) & HIFN_BASE_CMD_DSTLEN_M));
1124
1125 return sizeof(struct hifn_base_command);
1126}
1127
1128static int hifn_setup_crypto_command(struct hifn_device *dev,
1129 u8 *buf, unsigned dlen, unsigned slen,
1130 u8 *key, int keylen, u8 *iv, int ivsize, u16 mode)
1131{
1132 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1133 struct hifn_crypt_command *cry_cmd;
1134 u8 *buf_pos = buf;
1135 u16 cmd_len;
1136
1137 cry_cmd = (struct hifn_crypt_command *)buf_pos;
1138
1139 cry_cmd->source_count = __cpu_to_le16(dlen & 0xffff);
1140 dlen >>= 16;
1141 cry_cmd->masks = __cpu_to_le16(mode |
1142 ((dlen << HIFN_CRYPT_CMD_SRCLEN_S) &
1143 HIFN_CRYPT_CMD_SRCLEN_M));
1144 cry_cmd->header_skip = 0;
1145 cry_cmd->reserved = 0;
1146
1147 buf_pos += sizeof(struct hifn_crypt_command);
1148
1149 dma->cmdu++;
1150 if (dma->cmdu > 1) {
1151 dev->dmareg |= HIFN_DMAIER_C_WAIT;
1152 hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
1153 }
1154
1155 if (keylen) {
1156 memcpy(buf_pos, key, keylen);
1157 buf_pos += keylen;
1158 }
1159 if (ivsize) {
1160 memcpy(buf_pos, iv, ivsize);
1161 buf_pos += ivsize;
1162 }
1163
1164 cmd_len = buf_pos - buf;
1165
1166 return cmd_len;
1167}
1168
1169static int hifn_setup_src_desc(struct hifn_device *dev, struct page *page,
1170 unsigned int offset, unsigned int size)
1171{
1172 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1173 int idx;
1174 dma_addr_t addr;
1175
1176 addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_TODEVICE);
1177
1178 idx = dma->srci;
1179
1180 dma->srcr[idx].p = __cpu_to_le32(addr);
Patrick McHardy3c42cbc2008-05-07 22:28:27 +08001181 dma->srcr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
1182 HIFN_D_MASKDONEIRQ | HIFN_D_NOINVALID | HIFN_D_LAST);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001183
1184 if (++idx == HIFN_D_SRC_RSIZE) {
1185 dma->srcr[idx].l = __cpu_to_le32(HIFN_D_VALID |
1186 HIFN_D_JUMP |
1187 HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
1188 idx = 0;
1189 }
1190
1191 dma->srci = idx;
1192 dma->srcu++;
1193
1194 if (!(dev->flags & HIFN_FLAG_SRC_BUSY)) {
1195 hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_S_CTRL_ENA);
1196 dev->flags |= HIFN_FLAG_SRC_BUSY;
1197 }
1198
1199 return size;
1200}
1201
1202static void hifn_setup_res_desc(struct hifn_device *dev)
1203{
1204 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1205
1206 dma->resr[dma->resi].l = __cpu_to_le32(HIFN_USED_RESULT |
1207 HIFN_D_VALID | HIFN_D_LAST);
1208 /*
1209 * dma->resr[dma->resi].l = __cpu_to_le32(HIFN_MAX_RESULT | HIFN_D_VALID |
1210 * HIFN_D_LAST | HIFN_D_NOINVALID);
1211 */
1212
1213 if (++dma->resi == HIFN_D_RES_RSIZE) {
1214 dma->resr[HIFN_D_RES_RSIZE].l = __cpu_to_le32(HIFN_D_VALID |
1215 HIFN_D_JUMP | HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
1216 dma->resi = 0;
1217 }
1218
1219 dma->resu++;
1220
1221 if (!(dev->flags & HIFN_FLAG_RES_BUSY)) {
1222 hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_R_CTRL_ENA);
1223 dev->flags |= HIFN_FLAG_RES_BUSY;
1224 }
1225}
1226
1227static void hifn_setup_dst_desc(struct hifn_device *dev, struct page *page,
1228 unsigned offset, unsigned size)
1229{
1230 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1231 int idx;
1232 dma_addr_t addr;
1233
1234 addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_FROMDEVICE);
1235
1236 idx = dma->dsti;
1237 dma->dstr[idx].p = __cpu_to_le32(addr);
1238 dma->dstr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
1239 HIFN_D_MASKDONEIRQ | HIFN_D_NOINVALID | HIFN_D_LAST);
1240
1241 if (++idx == HIFN_D_DST_RSIZE) {
1242 dma->dstr[idx].l = __cpu_to_le32(HIFN_D_VALID |
1243 HIFN_D_JUMP | HIFN_D_MASKDONEIRQ |
1244 HIFN_D_LAST | HIFN_D_NOINVALID);
1245 idx = 0;
1246 }
1247 dma->dsti = idx;
1248 dma->dstu++;
1249
1250 if (!(dev->flags & HIFN_FLAG_DST_BUSY)) {
1251 hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_D_CTRL_ENA);
1252 dev->flags |= HIFN_FLAG_DST_BUSY;
1253 }
1254}
1255
1256static int hifn_setup_dma(struct hifn_device *dev, struct page *spage, unsigned int soff,
1257 struct page *dpage, unsigned int doff, unsigned int nbytes, void *priv,
1258 struct hifn_context *ctx)
1259{
1260 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1261 int cmd_len, sa_idx;
1262 u8 *buf, *buf_pos;
1263 u16 mask;
1264
1265 dprintk("%s: spage: %p, soffset: %u, dpage: %p, doffset: %u, nbytes: %u, priv: %p, ctx: %p.\n",
1266 dev->name, spage, soff, dpage, doff, nbytes, priv, ctx);
1267
1268 sa_idx = dma->resi;
1269
1270 hifn_setup_src_desc(dev, spage, soff, nbytes);
1271
1272 buf_pos = buf = dma->command_bufs[dma->cmdi];
1273
1274 mask = 0;
1275 switch (ctx->op) {
1276 case ACRYPTO_OP_DECRYPT:
1277 mask = HIFN_BASE_CMD_CRYPT | HIFN_BASE_CMD_DECODE;
1278 break;
1279 case ACRYPTO_OP_ENCRYPT:
1280 mask = HIFN_BASE_CMD_CRYPT;
1281 break;
1282 case ACRYPTO_OP_HMAC:
1283 mask = HIFN_BASE_CMD_MAC;
1284 break;
1285 default:
1286 goto err_out;
1287 }
1288
1289 buf_pos += hifn_setup_base_command(dev, buf_pos, nbytes,
1290 nbytes, mask, dev->snum);
1291
1292 if (ctx->op == ACRYPTO_OP_ENCRYPT || ctx->op == ACRYPTO_OP_DECRYPT) {
1293 u16 md = 0;
1294
1295 if (ctx->keysize)
1296 md |= HIFN_CRYPT_CMD_NEW_KEY;
1297 if (ctx->iv && ctx->mode != ACRYPTO_MODE_ECB)
1298 md |= HIFN_CRYPT_CMD_NEW_IV;
1299
1300 switch (ctx->mode) {
1301 case ACRYPTO_MODE_ECB:
1302 md |= HIFN_CRYPT_CMD_MODE_ECB;
1303 break;
1304 case ACRYPTO_MODE_CBC:
1305 md |= HIFN_CRYPT_CMD_MODE_CBC;
1306 break;
1307 case ACRYPTO_MODE_CFB:
1308 md |= HIFN_CRYPT_CMD_MODE_CFB;
1309 break;
1310 case ACRYPTO_MODE_OFB:
1311 md |= HIFN_CRYPT_CMD_MODE_OFB;
1312 break;
1313 default:
1314 goto err_out;
1315 }
1316
1317 switch (ctx->type) {
1318 case ACRYPTO_TYPE_AES_128:
1319 if (ctx->keysize != 16)
1320 goto err_out;
1321 md |= HIFN_CRYPT_CMD_KSZ_128 |
1322 HIFN_CRYPT_CMD_ALG_AES;
1323 break;
1324 case ACRYPTO_TYPE_AES_192:
1325 if (ctx->keysize != 24)
1326 goto err_out;
1327 md |= HIFN_CRYPT_CMD_KSZ_192 |
1328 HIFN_CRYPT_CMD_ALG_AES;
1329 break;
1330 case ACRYPTO_TYPE_AES_256:
1331 if (ctx->keysize != 32)
1332 goto err_out;
1333 md |= HIFN_CRYPT_CMD_KSZ_256 |
1334 HIFN_CRYPT_CMD_ALG_AES;
1335 break;
1336 case ACRYPTO_TYPE_3DES:
1337 if (ctx->keysize != 24)
1338 goto err_out;
1339 md |= HIFN_CRYPT_CMD_ALG_3DES;
1340 break;
1341 case ACRYPTO_TYPE_DES:
1342 if (ctx->keysize != 8)
1343 goto err_out;
1344 md |= HIFN_CRYPT_CMD_ALG_DES;
1345 break;
1346 default:
1347 goto err_out;
1348 }
1349
1350 buf_pos += hifn_setup_crypto_command(dev, buf_pos,
1351 nbytes, nbytes, ctx->key, ctx->keysize,
1352 ctx->iv, ctx->ivsize, md);
1353 }
1354
1355 dev->sa[sa_idx] = priv;
1356
1357 cmd_len = buf_pos - buf;
1358 dma->cmdr[dma->cmdi].l = __cpu_to_le32(cmd_len | HIFN_D_VALID |
1359 HIFN_D_LAST | HIFN_D_MASKDONEIRQ);
1360
1361 if (++dma->cmdi == HIFN_D_CMD_RSIZE) {
1362 dma->cmdr[dma->cmdi].l = __cpu_to_le32(HIFN_MAX_COMMAND |
1363 HIFN_D_VALID | HIFN_D_LAST |
1364 HIFN_D_MASKDONEIRQ | HIFN_D_JUMP);
1365 dma->cmdi = 0;
1366 } else
1367 dma->cmdr[dma->cmdi-1].l |= __cpu_to_le32(HIFN_D_VALID);
1368
1369 if (!(dev->flags & HIFN_FLAG_CMD_BUSY)) {
1370 hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_C_CTRL_ENA);
1371 dev->flags |= HIFN_FLAG_CMD_BUSY;
1372 }
1373
1374 hifn_setup_dst_desc(dev, dpage, doff, nbytes);
1375 hifn_setup_res_desc(dev);
1376
1377 return 0;
1378
1379err_out:
1380 return -EINVAL;
1381}
1382
1383static int ablkcipher_walk_init(struct ablkcipher_walk *w,
1384 int num, gfp_t gfp_flags)
1385{
1386 int i;
1387
1388 num = min(ASYNC_SCATTERLIST_CACHE, num);
1389 sg_init_table(w->cache, num);
1390
1391 w->num = 0;
1392 for (i=0; i<num; ++i) {
1393 struct page *page = alloc_page(gfp_flags);
1394 struct scatterlist *s;
1395
1396 if (!page)
1397 break;
1398
1399 s = &w->cache[i];
1400
1401 sg_set_page(s, page, PAGE_SIZE, 0);
1402 w->num++;
1403 }
1404
1405 return i;
1406}
1407
1408static void ablkcipher_walk_exit(struct ablkcipher_walk *w)
1409{
1410 int i;
1411
1412 for (i=0; i<w->num; ++i) {
1413 struct scatterlist *s = &w->cache[i];
1414
1415 __free_page(sg_page(s));
1416
1417 s->length = 0;
1418 }
1419
1420 w->num = 0;
1421}
1422
1423static int ablkcipher_add(void *daddr, unsigned int *drestp, struct scatterlist *src,
1424 unsigned int size, unsigned int *nbytesp)
1425{
1426 unsigned int copy, drest = *drestp, nbytes = *nbytesp;
1427 int idx = 0;
1428 void *saddr;
1429
1430 if (drest < size || size > nbytes)
1431 return -EINVAL;
1432
1433 while (size) {
1434 copy = min(drest, src->length);
1435
1436 saddr = kmap_atomic(sg_page(src), KM_SOFTIRQ1);
1437 memcpy(daddr, saddr + src->offset, copy);
1438 kunmap_atomic(saddr, KM_SOFTIRQ1);
1439
1440 size -= copy;
1441 drest -= copy;
1442 nbytes -= copy;
1443 daddr += copy;
1444
1445 dprintk("%s: copy: %u, size: %u, drest: %u, nbytes: %u.\n",
1446 __func__, copy, size, drest, nbytes);
1447
1448 src++;
1449 idx++;
1450 }
1451
1452 *nbytesp = nbytes;
1453 *drestp = drest;
1454
1455 return idx;
1456}
1457
1458static int ablkcipher_walk(struct ablkcipher_request *req,
1459 struct ablkcipher_walk *w)
1460{
1461 unsigned blocksize =
1462 crypto_ablkcipher_blocksize(crypto_ablkcipher_reqtfm(req));
1463 unsigned alignmask =
1464 crypto_ablkcipher_alignmask(crypto_ablkcipher_reqtfm(req));
1465 struct scatterlist *src, *dst, *t;
1466 void *daddr;
1467 unsigned int nbytes = req->nbytes, offset, copy, diff;
1468 int idx, tidx, err;
1469
1470 tidx = idx = 0;
1471 offset = 0;
1472 while (nbytes) {
1473 if (idx >= w->num && (w->flags & ASYNC_FLAGS_MISALIGNED))
1474 return -EINVAL;
1475
1476 src = &req->src[idx];
1477 dst = &req->dst[idx];
1478
1479 dprintk("\n%s: slen: %u, dlen: %u, soff: %u, doff: %u, offset: %u, "
1480 "blocksize: %u, nbytes: %u.\n",
1481 __func__, src->length, dst->length, src->offset,
1482 dst->offset, offset, blocksize, nbytes);
1483
1484 if (src->length & (blocksize - 1) ||
1485 src->offset & (alignmask - 1) ||
1486 dst->length & (blocksize - 1) ||
1487 dst->offset & (alignmask - 1) ||
1488 offset) {
1489 unsigned slen = src->length - offset;
1490 unsigned dlen = PAGE_SIZE;
1491
1492 t = &w->cache[idx];
1493
1494 daddr = kmap_atomic(sg_page(t), KM_SOFTIRQ0);
1495 err = ablkcipher_add(daddr, &dlen, src, slen, &nbytes);
1496 if (err < 0)
1497 goto err_out_unmap;
1498
1499 idx += err;
1500
1501 copy = slen & ~(blocksize - 1);
1502 diff = slen & (blocksize - 1);
1503
1504 if (dlen < nbytes) {
1505 /*
1506 * Destination page does not have enough space
1507 * to put there additional blocksized chunk,
1508 * so we mark that page as containing only
1509 * blocksize aligned chunks:
1510 * t->length = (slen & ~(blocksize - 1));
1511 * and increase number of bytes to be processed
1512 * in next chunk:
1513 * nbytes += diff;
1514 */
1515 nbytes += diff;
1516
1517 /*
1518 * Temporary of course...
1519 * Kick author if you will catch this one.
1520 */
1521 printk(KERN_ERR "%s: dlen: %u, nbytes: %u,"
1522 "slen: %u, offset: %u.\n",
1523 __func__, dlen, nbytes, slen, offset);
1524 printk(KERN_ERR "%s: please contact author to fix this "
1525 "issue, generally you should not catch "
1526 "this path under any condition but who "
1527 "knows how did you use crypto code.\n"
1528 "Thank you.\n", __func__);
1529 BUG();
1530 } else {
1531 copy += diff + nbytes;
1532
1533 src = &req->src[idx];
1534
1535 err = ablkcipher_add(daddr + slen, &dlen, src, nbytes, &nbytes);
1536 if (err < 0)
1537 goto err_out_unmap;
1538
1539 idx += err;
1540 }
1541
1542 t->length = copy;
1543 t->offset = offset;
1544
1545 kunmap_atomic(daddr, KM_SOFTIRQ0);
1546 } else {
1547 nbytes -= src->length;
1548 idx++;
1549 }
1550
1551 tidx++;
1552 }
1553
1554 return tidx;
1555
1556err_out_unmap:
1557 kunmap_atomic(daddr, KM_SOFTIRQ0);
1558 return err;
1559}
1560
1561static int hifn_setup_session(struct ablkcipher_request *req)
1562{
1563 struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
1564 struct hifn_device *dev = ctx->dev;
1565 struct page *spage, *dpage;
1566 unsigned long soff, doff, flags;
1567 unsigned int nbytes = req->nbytes, idx = 0, len;
1568 int err = -EINVAL, sg_num;
1569 struct scatterlist *src, *dst, *t;
1570 unsigned blocksize =
1571 crypto_ablkcipher_blocksize(crypto_ablkcipher_reqtfm(req));
1572 unsigned alignmask =
1573 crypto_ablkcipher_alignmask(crypto_ablkcipher_reqtfm(req));
1574
1575 if (ctx->iv && !ctx->ivsize && ctx->mode != ACRYPTO_MODE_ECB)
1576 goto err_out_exit;
1577
1578 ctx->walk.flags = 0;
1579
1580 while (nbytes) {
1581 src = &req->src[idx];
1582 dst = &req->dst[idx];
1583
1584 if (src->length & (blocksize - 1) ||
1585 src->offset & (alignmask - 1) ||
1586 dst->length & (blocksize - 1) ||
1587 dst->offset & (alignmask - 1)) {
1588 ctx->walk.flags |= ASYNC_FLAGS_MISALIGNED;
1589 }
1590
1591 nbytes -= src->length;
1592 idx++;
1593 }
1594
1595 if (ctx->walk.flags & ASYNC_FLAGS_MISALIGNED) {
1596 err = ablkcipher_walk_init(&ctx->walk, idx, GFP_ATOMIC);
1597 if (err < 0)
1598 return err;
1599 }
1600
1601 nbytes = req->nbytes;
1602 idx = 0;
1603
1604 sg_num = ablkcipher_walk(req, &ctx->walk);
Patrick McHardy94eaa1b2008-05-07 22:32:28 +08001605 if (sg_num < 0) {
1606 err = sg_num;
1607 goto err_out_exit;
1608 }
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001609 atomic_set(&ctx->sg_num, sg_num);
1610
1611 spin_lock_irqsave(&dev->lock, flags);
1612 if (dev->started + sg_num > HIFN_QUEUE_LENGTH) {
1613 err = -EAGAIN;
1614 goto err_out;
1615 }
1616
1617 dev->snum++;
1618 dev->started += sg_num;
1619
1620 while (nbytes) {
1621 src = &req->src[idx];
1622 dst = &req->dst[idx];
1623 t = &ctx->walk.cache[idx];
1624
1625 if (t->length) {
1626 spage = dpage = sg_page(t);
1627 soff = doff = 0;
1628 len = t->length;
1629 } else {
1630 spage = sg_page(src);
1631 soff = src->offset;
1632
1633 dpage = sg_page(dst);
1634 doff = dst->offset;
1635
1636 len = dst->length;
1637 }
1638
1639 idx++;
1640
1641 err = hifn_setup_dma(dev, spage, soff, dpage, doff, nbytes,
1642 req, ctx);
1643 if (err)
1644 goto err_out;
1645
1646 nbytes -= len;
1647 }
1648
1649 dev->active = HIFN_DEFAULT_ACTIVE_NUM;
1650 spin_unlock_irqrestore(&dev->lock, flags);
1651
1652 return 0;
1653
1654err_out:
1655 spin_unlock_irqrestore(&dev->lock, flags);
1656err_out_exit:
Patrick McHardy7808f072008-05-07 22:29:42 +08001657 if (err)
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001658 dprintk("%s: iv: %p [%d], key: %p [%d], mode: %u, op: %u, "
1659 "type: %u, err: %d.\n",
1660 dev->name, ctx->iv, ctx->ivsize,
1661 ctx->key, ctx->keysize,
1662 ctx->mode, ctx->op, ctx->type, err);
1663
1664 return err;
1665}
1666
1667static int hifn_test(struct hifn_device *dev, int encdec, u8 snum)
1668{
1669 int n, err;
1670 u8 src[16];
1671 struct hifn_context ctx;
1672 u8 fips_aes_ecb_from_zero[16] = {
1673 0x66, 0xE9, 0x4B, 0xD4,
1674 0xEF, 0x8A, 0x2C, 0x3B,
1675 0x88, 0x4C, 0xFA, 0x59,
1676 0xCA, 0x34, 0x2B, 0x2E};
1677
1678 memset(src, 0, sizeof(src));
1679 memset(ctx.key, 0, sizeof(ctx.key));
1680
1681 ctx.dev = dev;
1682 ctx.keysize = 16;
1683 ctx.ivsize = 0;
1684 ctx.iv = NULL;
1685 ctx.op = (encdec)?ACRYPTO_OP_ENCRYPT:ACRYPTO_OP_DECRYPT;
1686 ctx.mode = ACRYPTO_MODE_ECB;
1687 ctx.type = ACRYPTO_TYPE_AES_128;
1688 atomic_set(&ctx.sg_num, 1);
1689
1690 err = hifn_setup_dma(dev,
1691 virt_to_page(src), offset_in_page(src),
1692 virt_to_page(src), offset_in_page(src),
1693 sizeof(src), NULL, &ctx);
1694 if (err)
1695 goto err_out;
1696
1697 msleep(200);
1698
1699 dprintk("%s: decoded: ", dev->name);
1700 for (n=0; n<sizeof(src); ++n)
1701 dprintk("%02x ", src[n]);
1702 dprintk("\n");
1703 dprintk("%s: FIPS : ", dev->name);
1704 for (n=0; n<sizeof(fips_aes_ecb_from_zero); ++n)
1705 dprintk("%02x ", fips_aes_ecb_from_zero[n]);
1706 dprintk("\n");
1707
1708 if (!memcmp(src, fips_aes_ecb_from_zero, sizeof(fips_aes_ecb_from_zero))) {
1709 printk(KERN_INFO "%s: AES 128 ECB test has been successfully "
1710 "passed.\n", dev->name);
1711 return 0;
1712 }
1713
1714err_out:
1715 printk(KERN_INFO "%s: AES 128 ECB test has been failed.\n", dev->name);
1716 return -1;
1717}
1718
1719static int hifn_start_device(struct hifn_device *dev)
1720{
1721 int err;
1722
1723 hifn_reset_dma(dev, 1);
1724
1725 err = hifn_enable_crypto(dev);
1726 if (err)
1727 return err;
1728
1729 hifn_reset_puc(dev);
1730
1731 hifn_init_dma(dev);
1732
1733 hifn_init_registers(dev);
1734
1735 hifn_init_pubrng(dev);
1736
1737 return 0;
1738}
1739
1740static int ablkcipher_get(void *saddr, unsigned int *srestp, unsigned int offset,
1741 struct scatterlist *dst, unsigned int size, unsigned int *nbytesp)
1742{
1743 unsigned int srest = *srestp, nbytes = *nbytesp, copy;
1744 void *daddr;
1745 int idx = 0;
1746
1747 if (srest < size || size > nbytes)
1748 return -EINVAL;
1749
1750 while (size) {
1751
1752 copy = min(dst->length, srest);
1753
1754 daddr = kmap_atomic(sg_page(dst), KM_IRQ0);
1755 memcpy(daddr + dst->offset + offset, saddr, copy);
1756 kunmap_atomic(daddr, KM_IRQ0);
1757
1758 nbytes -= copy;
1759 size -= copy;
1760 srest -= copy;
1761 saddr += copy;
1762 offset = 0;
1763
1764 dprintk("%s: copy: %u, size: %u, srest: %u, nbytes: %u.\n",
1765 __func__, copy, size, srest, nbytes);
1766
1767 dst++;
1768 idx++;
1769 }
1770
1771 *nbytesp = nbytes;
1772 *srestp = srest;
1773
1774 return idx;
1775}
1776
1777static void hifn_process_ready(struct ablkcipher_request *req, int error)
1778{
1779 struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
1780 struct hifn_device *dev;
1781
1782 dprintk("%s: req: %p, ctx: %p.\n", __func__, req, ctx);
1783
1784 dev = ctx->dev;
1785 dprintk("%s: req: %p, started: %d, sg_num: %d.\n",
1786 __func__, req, dev->started, atomic_read(&ctx->sg_num));
1787
1788 if (--dev->started < 0)
1789 BUG();
1790
1791 if (atomic_dec_and_test(&ctx->sg_num)) {
1792 unsigned int nbytes = req->nbytes;
1793 int idx = 0, err;
1794 struct scatterlist *dst, *t;
1795 void *saddr;
1796
1797 if (ctx->walk.flags & ASYNC_FLAGS_MISALIGNED) {
1798 while (nbytes) {
1799 t = &ctx->walk.cache[idx];
1800 dst = &req->dst[idx];
1801
1802 dprintk("\n%s: sg_page(t): %p, t->length: %u, "
1803 "sg_page(dst): %p, dst->length: %u, "
1804 "nbytes: %u.\n",
1805 __func__, sg_page(t), t->length,
1806 sg_page(dst), dst->length, nbytes);
1807
1808 if (!t->length) {
1809 nbytes -= dst->length;
1810 idx++;
1811 continue;
1812 }
1813
1814 saddr = kmap_atomic(sg_page(t), KM_IRQ1);
1815
1816 err = ablkcipher_get(saddr, &t->length, t->offset,
1817 dst, nbytes, &nbytes);
1818 if (err < 0) {
1819 kunmap_atomic(saddr, KM_IRQ1);
1820 break;
1821 }
1822
1823 idx += err;
1824 kunmap_atomic(saddr, KM_IRQ1);
1825 }
1826
1827 ablkcipher_walk_exit(&ctx->walk);
1828 }
1829
1830 req->base.complete(&req->base, error);
1831 }
1832}
1833
1834static void hifn_check_for_completion(struct hifn_device *dev, int error)
1835{
1836 int i;
1837 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1838
1839 for (i=0; i<HIFN_D_RES_RSIZE; ++i) {
1840 struct hifn_desc *d = &dma->resr[i];
1841
1842 if (!(d->l & __cpu_to_le32(HIFN_D_VALID)) && dev->sa[i]) {
1843 dev->success++;
1844 dev->reset = 0;
1845 hifn_process_ready(dev->sa[i], error);
1846 dev->sa[i] = NULL;
1847 }
1848
1849 if (d->l & __cpu_to_le32(HIFN_D_DESTOVER | HIFN_D_OVER))
1850 if (printk_ratelimit())
1851 printk("%s: overflow detected [d: %u, o: %u] "
1852 "at %d resr: l: %08x, p: %08x.\n",
1853 dev->name,
1854 !!(d->l & __cpu_to_le32(HIFN_D_DESTOVER)),
1855 !!(d->l & __cpu_to_le32(HIFN_D_OVER)),
1856 i, d->l, d->p);
1857 }
1858}
1859
1860static void hifn_clear_rings(struct hifn_device *dev)
1861{
1862 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1863 int i, u;
1864
1865 dprintk("%s: ring cleanup 1: i: %d.%d.%d.%d, u: %d.%d.%d.%d, "
1866 "k: %d.%d.%d.%d.\n",
1867 dev->name,
1868 dma->cmdi, dma->srci, dma->dsti, dma->resi,
1869 dma->cmdu, dma->srcu, dma->dstu, dma->resu,
1870 dma->cmdk, dma->srck, dma->dstk, dma->resk);
1871
1872 i = dma->resk; u = dma->resu;
1873 while (u != 0) {
1874 if (dma->resr[i].l & __cpu_to_le32(HIFN_D_VALID))
1875 break;
1876
1877 if (i != HIFN_D_RES_RSIZE)
1878 u--;
1879
1880 if (++i == (HIFN_D_RES_RSIZE + 1))
1881 i = 0;
1882 }
1883 dma->resk = i; dma->resu = u;
1884
1885 i = dma->srck; u = dma->srcu;
1886 while (u != 0) {
1887 if (i == HIFN_D_SRC_RSIZE)
1888 i = 0;
1889 if (dma->srcr[i].l & __cpu_to_le32(HIFN_D_VALID))
1890 break;
1891 i++, u--;
1892 }
1893 dma->srck = i; dma->srcu = u;
1894
1895 i = dma->cmdk; u = dma->cmdu;
1896 while (u != 0) {
1897 if (dma->cmdr[i].l & __cpu_to_le32(HIFN_D_VALID))
1898 break;
1899 if (i != HIFN_D_CMD_RSIZE)
1900 u--;
1901 if (++i == (HIFN_D_CMD_RSIZE + 1))
1902 i = 0;
1903 }
1904 dma->cmdk = i; dma->cmdu = u;
1905
1906 i = dma->dstk; u = dma->dstu;
1907 while (u != 0) {
1908 if (i == HIFN_D_DST_RSIZE)
1909 i = 0;
1910 if (dma->dstr[i].l & __cpu_to_le32(HIFN_D_VALID))
1911 break;
1912 i++, u--;
1913 }
1914 dma->dstk = i; dma->dstu = u;
1915
1916 dprintk("%s: ring cleanup 2: i: %d.%d.%d.%d, u: %d.%d.%d.%d, "
1917 "k: %d.%d.%d.%d.\n",
1918 dev->name,
1919 dma->cmdi, dma->srci, dma->dsti, dma->resi,
1920 dma->cmdu, dma->srcu, dma->dstu, dma->resu,
1921 dma->cmdk, dma->srck, dma->dstk, dma->resk);
1922}
1923
1924static void hifn_work(struct work_struct *work)
1925{
1926 struct delayed_work *dw = container_of(work, struct delayed_work, work);
1927 struct hifn_device *dev = container_of(dw, struct hifn_device, work);
1928 unsigned long flags;
1929 int reset = 0;
1930 u32 r = 0;
1931
1932 spin_lock_irqsave(&dev->lock, flags);
1933 if (dev->active == 0) {
1934 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1935
1936 if (dma->cmdu == 0 && (dev->flags & HIFN_FLAG_CMD_BUSY)) {
1937 dev->flags &= ~HIFN_FLAG_CMD_BUSY;
1938 r |= HIFN_DMACSR_C_CTRL_DIS;
1939 }
1940 if (dma->srcu == 0 && (dev->flags & HIFN_FLAG_SRC_BUSY)) {
1941 dev->flags &= ~HIFN_FLAG_SRC_BUSY;
1942 r |= HIFN_DMACSR_S_CTRL_DIS;
1943 }
1944 if (dma->dstu == 0 && (dev->flags & HIFN_FLAG_DST_BUSY)) {
1945 dev->flags &= ~HIFN_FLAG_DST_BUSY;
1946 r |= HIFN_DMACSR_D_CTRL_DIS;
1947 }
1948 if (dma->resu == 0 && (dev->flags & HIFN_FLAG_RES_BUSY)) {
1949 dev->flags &= ~HIFN_FLAG_RES_BUSY;
1950 r |= HIFN_DMACSR_R_CTRL_DIS;
1951 }
1952 if (r)
1953 hifn_write_1(dev, HIFN_1_DMA_CSR, r);
1954 } else
1955 dev->active--;
1956
1957 if (dev->prev_success == dev->success && dev->started)
1958 reset = 1;
1959 dev->prev_success = dev->success;
1960 spin_unlock_irqrestore(&dev->lock, flags);
1961
1962 if (reset) {
1963 dprintk("%s: r: %08x, active: %d, started: %d, "
1964 "success: %lu: reset: %d.\n",
1965 dev->name, r, dev->active, dev->started,
1966 dev->success, reset);
1967
1968 if (++dev->reset >= 5) {
1969 dprintk("%s: really hard reset.\n", dev->name);
1970 hifn_reset_dma(dev, 1);
1971 hifn_stop_device(dev);
1972 hifn_start_device(dev);
1973 dev->reset = 0;
1974 }
1975
1976 spin_lock_irqsave(&dev->lock, flags);
1977 hifn_check_for_completion(dev, -EBUSY);
1978 hifn_clear_rings(dev);
1979 dev->started = 0;
1980 spin_unlock_irqrestore(&dev->lock, flags);
1981 }
1982
1983 schedule_delayed_work(&dev->work, HZ);
1984}
1985
1986static irqreturn_t hifn_interrupt(int irq, void *data)
1987{
1988 struct hifn_device *dev = (struct hifn_device *)data;
1989 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1990 u32 dmacsr, restart;
1991
1992 dmacsr = hifn_read_1(dev, HIFN_1_DMA_CSR);
1993
1994 dprintk("%s: 1 dmacsr: %08x, dmareg: %08x, res: %08x [%d], "
1995 "i: %d.%d.%d.%d, u: %d.%d.%d.%d.\n",
1996 dev->name, dmacsr, dev->dmareg, dmacsr & dev->dmareg, dma->cmdi,
1997 dma->cmdu, dma->srcu, dma->dstu, dma->resu,
1998 dma->cmdi, dma->srci, dma->dsti, dma->resi);
1999
2000 if ((dmacsr & dev->dmareg) == 0)
2001 return IRQ_NONE;
2002
2003 hifn_write_1(dev, HIFN_1_DMA_CSR, dmacsr & dev->dmareg);
2004
2005 if (dmacsr & HIFN_DMACSR_ENGINE)
2006 hifn_write_0(dev, HIFN_0_PUISR, hifn_read_0(dev, HIFN_0_PUISR));
2007 if (dmacsr & HIFN_DMACSR_PUBDONE)
2008 hifn_write_1(dev, HIFN_1_PUB_STATUS,
2009 hifn_read_1(dev, HIFN_1_PUB_STATUS) | HIFN_PUBSTS_DONE);
2010
2011 restart = dmacsr & (HIFN_DMACSR_R_OVER | HIFN_DMACSR_D_OVER);
2012 if (restart) {
2013 u32 puisr = hifn_read_0(dev, HIFN_0_PUISR);
2014
2015 if (printk_ratelimit())
2016 printk("%s: overflow: r: %d, d: %d, puisr: %08x, d: %u.\n",
2017 dev->name, !!(dmacsr & HIFN_DMACSR_R_OVER),
2018 !!(dmacsr & HIFN_DMACSR_D_OVER),
2019 puisr, !!(puisr & HIFN_PUISR_DSTOVER));
2020 if (!!(puisr & HIFN_PUISR_DSTOVER))
2021 hifn_write_0(dev, HIFN_0_PUISR, HIFN_PUISR_DSTOVER);
2022 hifn_write_1(dev, HIFN_1_DMA_CSR, dmacsr & (HIFN_DMACSR_R_OVER |
2023 HIFN_DMACSR_D_OVER));
2024 }
2025
2026 restart = dmacsr & (HIFN_DMACSR_C_ABORT | HIFN_DMACSR_S_ABORT |
2027 HIFN_DMACSR_D_ABORT | HIFN_DMACSR_R_ABORT);
2028 if (restart) {
2029 if (printk_ratelimit())
2030 printk("%s: abort: c: %d, s: %d, d: %d, r: %d.\n",
2031 dev->name, !!(dmacsr & HIFN_DMACSR_C_ABORT),
2032 !!(dmacsr & HIFN_DMACSR_S_ABORT),
2033 !!(dmacsr & HIFN_DMACSR_D_ABORT),
2034 !!(dmacsr & HIFN_DMACSR_R_ABORT));
2035 hifn_reset_dma(dev, 1);
2036 hifn_init_dma(dev);
2037 hifn_init_registers(dev);
2038 }
2039
2040 if ((dmacsr & HIFN_DMACSR_C_WAIT) && (dma->cmdu == 0)) {
2041 dprintk("%s: wait on command.\n", dev->name);
2042 dev->dmareg &= ~(HIFN_DMAIER_C_WAIT);
2043 hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
2044 }
2045
Evgeniy Polyakova1e6ef22007-11-10 20:24:18 +08002046 tasklet_schedule(&dev->tasklet);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002047 hifn_clear_rings(dev);
2048
2049 return IRQ_HANDLED;
2050}
2051
2052static void hifn_flush(struct hifn_device *dev)
2053{
2054 unsigned long flags;
2055 struct crypto_async_request *async_req;
2056 struct hifn_context *ctx;
2057 struct ablkcipher_request *req;
2058 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
2059 int i;
2060
2061 spin_lock_irqsave(&dev->lock, flags);
2062 for (i=0; i<HIFN_D_RES_RSIZE; ++i) {
2063 struct hifn_desc *d = &dma->resr[i];
2064
2065 if (dev->sa[i]) {
2066 hifn_process_ready(dev->sa[i],
2067 (d->l & __cpu_to_le32(HIFN_D_VALID))?-ENODEV:0);
2068 }
2069 }
2070
2071 while ((async_req = crypto_dequeue_request(&dev->queue))) {
2072 ctx = crypto_tfm_ctx(async_req->tfm);
2073 req = container_of(async_req, struct ablkcipher_request, base);
2074
2075 hifn_process_ready(req, -ENODEV);
2076 }
2077 spin_unlock_irqrestore(&dev->lock, flags);
2078}
2079
2080static int hifn_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
2081 unsigned int len)
2082{
2083 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
2084 struct hifn_context *ctx = crypto_tfm_ctx(tfm);
2085 struct hifn_device *dev = ctx->dev;
2086
2087 if (len > HIFN_MAX_CRYPT_KEY_LENGTH) {
2088 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
2089 return -1;
2090 }
2091
Evgeniy Polyakovc3041f92007-10-11 19:58:16 +08002092 if (len == HIFN_DES_KEY_LENGTH) {
2093 u32 tmp[DES_EXPKEY_WORDS];
2094 int ret = des_ekey(tmp, key);
2095
2096 if (unlikely(ret == 0) && (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
2097 tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
2098 return -EINVAL;
2099 }
2100 }
2101
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002102 dev->flags &= ~HIFN_FLAG_OLD_KEY;
2103
2104 memcpy(ctx->key, key, len);
2105 ctx->keysize = len;
2106
2107 return 0;
2108}
2109
2110static int hifn_handle_req(struct ablkcipher_request *req)
2111{
2112 struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
2113 struct hifn_device *dev = ctx->dev;
2114 int err = -EAGAIN;
2115
2116 if (dev->started + DIV_ROUND_UP(req->nbytes, PAGE_SIZE) <= HIFN_QUEUE_LENGTH)
2117 err = hifn_setup_session(req);
2118
2119 if (err == -EAGAIN) {
2120 unsigned long flags;
2121
2122 spin_lock_irqsave(&dev->lock, flags);
2123 err = ablkcipher_enqueue_request(&dev->queue, req);
2124 spin_unlock_irqrestore(&dev->lock, flags);
2125 }
2126
2127 return err;
2128}
2129
2130static int hifn_setup_crypto_req(struct ablkcipher_request *req, u8 op,
2131 u8 type, u8 mode)
2132{
2133 struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
2134 unsigned ivsize;
2135
2136 ivsize = crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(req));
2137
2138 if (req->info && mode != ACRYPTO_MODE_ECB) {
2139 if (type == ACRYPTO_TYPE_AES_128)
2140 ivsize = HIFN_AES_IV_LENGTH;
2141 else if (type == ACRYPTO_TYPE_DES)
2142 ivsize = HIFN_DES_KEY_LENGTH;
2143 else if (type == ACRYPTO_TYPE_3DES)
2144 ivsize = HIFN_3DES_KEY_LENGTH;
2145 }
2146
2147 if (ctx->keysize != 16 && type == ACRYPTO_TYPE_AES_128) {
2148 if (ctx->keysize == 24)
2149 type = ACRYPTO_TYPE_AES_192;
2150 else if (ctx->keysize == 32)
2151 type = ACRYPTO_TYPE_AES_256;
2152 }
2153
2154 ctx->op = op;
2155 ctx->mode = mode;
2156 ctx->type = type;
2157 ctx->iv = req->info;
2158 ctx->ivsize = ivsize;
2159
2160 /*
2161 * HEAVY TODO: needs to kick Herbert XU to write documentation.
2162 * HEAVY TODO: needs to kick Herbert XU to write documentation.
2163 * HEAVY TODO: needs to kick Herbert XU to write documentation.
2164 */
2165
2166 return hifn_handle_req(req);
2167}
2168
2169static int hifn_process_queue(struct hifn_device *dev)
2170{
2171 struct crypto_async_request *async_req;
2172 struct hifn_context *ctx;
2173 struct ablkcipher_request *req;
2174 unsigned long flags;
2175 int err = 0;
2176
2177 while (dev->started < HIFN_QUEUE_LENGTH) {
2178 spin_lock_irqsave(&dev->lock, flags);
2179 async_req = crypto_dequeue_request(&dev->queue);
2180 spin_unlock_irqrestore(&dev->lock, flags);
2181
2182 if (!async_req)
2183 break;
2184
2185 ctx = crypto_tfm_ctx(async_req->tfm);
2186 req = container_of(async_req, struct ablkcipher_request, base);
2187
2188 err = hifn_handle_req(req);
2189 if (err)
2190 break;
2191 }
2192
2193 return err;
2194}
2195
2196static int hifn_setup_crypto(struct ablkcipher_request *req, u8 op,
2197 u8 type, u8 mode)
2198{
2199 int err;
2200 struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
2201 struct hifn_device *dev = ctx->dev;
2202
2203 err = hifn_setup_crypto_req(req, op, type, mode);
2204 if (err)
2205 return err;
2206
2207 if (dev->started < HIFN_QUEUE_LENGTH && dev->queue.qlen)
Patrick McHardy9e70a402008-05-07 22:31:35 +08002208 hifn_process_queue(dev);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002209
Patrick McHardy9e70a402008-05-07 22:31:35 +08002210 return -EINPROGRESS;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002211}
2212
2213/*
2214 * AES ecryption functions.
2215 */
2216static inline int hifn_encrypt_aes_ecb(struct ablkcipher_request *req)
2217{
2218 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2219 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_ECB);
2220}
2221static inline int hifn_encrypt_aes_cbc(struct ablkcipher_request *req)
2222{
2223 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2224 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CBC);
2225}
2226static inline int hifn_encrypt_aes_cfb(struct ablkcipher_request *req)
2227{
2228 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2229 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CFB);
2230}
2231static inline int hifn_encrypt_aes_ofb(struct ablkcipher_request *req)
2232{
2233 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2234 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_OFB);
2235}
2236
2237/*
2238 * AES decryption functions.
2239 */
2240static inline int hifn_decrypt_aes_ecb(struct ablkcipher_request *req)
2241{
2242 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2243 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_ECB);
2244}
2245static inline int hifn_decrypt_aes_cbc(struct ablkcipher_request *req)
2246{
2247 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2248 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CBC);
2249}
2250static inline int hifn_decrypt_aes_cfb(struct ablkcipher_request *req)
2251{
2252 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2253 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CFB);
2254}
2255static inline int hifn_decrypt_aes_ofb(struct ablkcipher_request *req)
2256{
2257 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2258 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_OFB);
2259}
2260
2261/*
2262 * DES ecryption functions.
2263 */
2264static inline int hifn_encrypt_des_ecb(struct ablkcipher_request *req)
2265{
2266 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2267 ACRYPTO_TYPE_DES, ACRYPTO_MODE_ECB);
2268}
2269static inline int hifn_encrypt_des_cbc(struct ablkcipher_request *req)
2270{
2271 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2272 ACRYPTO_TYPE_DES, ACRYPTO_MODE_CBC);
2273}
2274static inline int hifn_encrypt_des_cfb(struct ablkcipher_request *req)
2275{
2276 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2277 ACRYPTO_TYPE_DES, ACRYPTO_MODE_CFB);
2278}
2279static inline int hifn_encrypt_des_ofb(struct ablkcipher_request *req)
2280{
2281 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2282 ACRYPTO_TYPE_DES, ACRYPTO_MODE_OFB);
2283}
2284
2285/*
2286 * DES decryption functions.
2287 */
2288static inline int hifn_decrypt_des_ecb(struct ablkcipher_request *req)
2289{
2290 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2291 ACRYPTO_TYPE_DES, ACRYPTO_MODE_ECB);
2292}
2293static inline int hifn_decrypt_des_cbc(struct ablkcipher_request *req)
2294{
2295 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2296 ACRYPTO_TYPE_DES, ACRYPTO_MODE_CBC);
2297}
2298static inline int hifn_decrypt_des_cfb(struct ablkcipher_request *req)
2299{
2300 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2301 ACRYPTO_TYPE_DES, ACRYPTO_MODE_CFB);
2302}
2303static inline int hifn_decrypt_des_ofb(struct ablkcipher_request *req)
2304{
2305 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2306 ACRYPTO_TYPE_DES, ACRYPTO_MODE_OFB);
2307}
2308
2309/*
2310 * 3DES ecryption functions.
2311 */
2312static inline int hifn_encrypt_3des_ecb(struct ablkcipher_request *req)
2313{
2314 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2315 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_ECB);
2316}
2317static inline int hifn_encrypt_3des_cbc(struct ablkcipher_request *req)
2318{
2319 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2320 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CBC);
2321}
2322static inline int hifn_encrypt_3des_cfb(struct ablkcipher_request *req)
2323{
2324 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2325 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CFB);
2326}
2327static inline int hifn_encrypt_3des_ofb(struct ablkcipher_request *req)
2328{
2329 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2330 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_OFB);
2331}
2332
2333/*
2334 * 3DES decryption functions.
2335 */
2336static inline int hifn_decrypt_3des_ecb(struct ablkcipher_request *req)
2337{
2338 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2339 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_ECB);
2340}
2341static inline int hifn_decrypt_3des_cbc(struct ablkcipher_request *req)
2342{
2343 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2344 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CBC);
2345}
2346static inline int hifn_decrypt_3des_cfb(struct ablkcipher_request *req)
2347{
2348 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2349 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CFB);
2350}
2351static inline int hifn_decrypt_3des_ofb(struct ablkcipher_request *req)
2352{
2353 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2354 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_OFB);
2355}
2356
2357struct hifn_alg_template
2358{
2359 char name[CRYPTO_MAX_ALG_NAME];
2360 char drv_name[CRYPTO_MAX_ALG_NAME];
2361 unsigned int bsize;
2362 struct ablkcipher_alg ablkcipher;
2363};
2364
2365static struct hifn_alg_template hifn_alg_templates[] = {
2366 /*
2367 * 3DES ECB, CBC, CFB and OFB modes.
2368 */
2369 {
2370 .name = "cfb(des3_ede)", .drv_name = "hifn-3des", .bsize = 8,
2371 .ablkcipher = {
2372 .min_keysize = HIFN_3DES_KEY_LENGTH,
2373 .max_keysize = HIFN_3DES_KEY_LENGTH,
2374 .setkey = hifn_setkey,
2375 .encrypt = hifn_encrypt_3des_cfb,
2376 .decrypt = hifn_decrypt_3des_cfb,
2377 },
2378 },
2379 {
2380 .name = "ofb(des3_ede)", .drv_name = "hifn-3des", .bsize = 8,
2381 .ablkcipher = {
2382 .min_keysize = HIFN_3DES_KEY_LENGTH,
2383 .max_keysize = HIFN_3DES_KEY_LENGTH,
2384 .setkey = hifn_setkey,
2385 .encrypt = hifn_encrypt_3des_ofb,
2386 .decrypt = hifn_decrypt_3des_ofb,
2387 },
2388 },
2389 {
2390 .name = "cbc(des3_ede)", .drv_name = "hifn-3des", .bsize = 8,
2391 .ablkcipher = {
2392 .min_keysize = HIFN_3DES_KEY_LENGTH,
2393 .max_keysize = HIFN_3DES_KEY_LENGTH,
2394 .setkey = hifn_setkey,
2395 .encrypt = hifn_encrypt_3des_cbc,
2396 .decrypt = hifn_decrypt_3des_cbc,
2397 },
2398 },
2399 {
2400 .name = "ecb(des3_ede)", .drv_name = "hifn-3des", .bsize = 8,
2401 .ablkcipher = {
2402 .min_keysize = HIFN_3DES_KEY_LENGTH,
2403 .max_keysize = HIFN_3DES_KEY_LENGTH,
2404 .setkey = hifn_setkey,
2405 .encrypt = hifn_encrypt_3des_ecb,
2406 .decrypt = hifn_decrypt_3des_ecb,
2407 },
2408 },
2409
2410 /*
2411 * DES ECB, CBC, CFB and OFB modes.
2412 */
2413 {
2414 .name = "cfb(des)", .drv_name = "hifn-des", .bsize = 8,
2415 .ablkcipher = {
2416 .min_keysize = HIFN_DES_KEY_LENGTH,
2417 .max_keysize = HIFN_DES_KEY_LENGTH,
2418 .setkey = hifn_setkey,
2419 .encrypt = hifn_encrypt_des_cfb,
2420 .decrypt = hifn_decrypt_des_cfb,
2421 },
2422 },
2423 {
2424 .name = "ofb(des)", .drv_name = "hifn-des", .bsize = 8,
2425 .ablkcipher = {
2426 .min_keysize = HIFN_DES_KEY_LENGTH,
2427 .max_keysize = HIFN_DES_KEY_LENGTH,
2428 .setkey = hifn_setkey,
2429 .encrypt = hifn_encrypt_des_ofb,
2430 .decrypt = hifn_decrypt_des_ofb,
2431 },
2432 },
2433 {
2434 .name = "cbc(des)", .drv_name = "hifn-des", .bsize = 8,
2435 .ablkcipher = {
2436 .min_keysize = HIFN_DES_KEY_LENGTH,
2437 .max_keysize = HIFN_DES_KEY_LENGTH,
2438 .setkey = hifn_setkey,
2439 .encrypt = hifn_encrypt_des_cbc,
2440 .decrypt = hifn_decrypt_des_cbc,
2441 },
2442 },
2443 {
2444 .name = "ecb(des)", .drv_name = "hifn-des", .bsize = 8,
2445 .ablkcipher = {
2446 .min_keysize = HIFN_DES_KEY_LENGTH,
2447 .max_keysize = HIFN_DES_KEY_LENGTH,
2448 .setkey = hifn_setkey,
2449 .encrypt = hifn_encrypt_des_ecb,
2450 .decrypt = hifn_decrypt_des_ecb,
2451 },
2452 },
2453
2454 /*
2455 * AES ECB, CBC, CFB and OFB modes.
2456 */
2457 {
2458 .name = "ecb(aes)", .drv_name = "hifn-aes", .bsize = 16,
2459 .ablkcipher = {
2460 .min_keysize = AES_MIN_KEY_SIZE,
2461 .max_keysize = AES_MAX_KEY_SIZE,
2462 .setkey = hifn_setkey,
2463 .encrypt = hifn_encrypt_aes_ecb,
2464 .decrypt = hifn_decrypt_aes_ecb,
2465 },
2466 },
2467 {
2468 .name = "cbc(aes)", .drv_name = "hifn-aes", .bsize = 16,
2469 .ablkcipher = {
2470 .min_keysize = AES_MIN_KEY_SIZE,
2471 .max_keysize = AES_MAX_KEY_SIZE,
2472 .setkey = hifn_setkey,
2473 .encrypt = hifn_encrypt_aes_cbc,
2474 .decrypt = hifn_decrypt_aes_cbc,
2475 },
2476 },
2477 {
2478 .name = "cfb(aes)", .drv_name = "hifn-aes", .bsize = 16,
2479 .ablkcipher = {
2480 .min_keysize = AES_MIN_KEY_SIZE,
2481 .max_keysize = AES_MAX_KEY_SIZE,
2482 .setkey = hifn_setkey,
2483 .encrypt = hifn_encrypt_aes_cfb,
2484 .decrypt = hifn_decrypt_aes_cfb,
2485 },
2486 },
2487 {
2488 .name = "ofb(aes)", .drv_name = "hifn-aes", .bsize = 16,
2489 .ablkcipher = {
2490 .min_keysize = AES_MIN_KEY_SIZE,
2491 .max_keysize = AES_MAX_KEY_SIZE,
2492 .setkey = hifn_setkey,
2493 .encrypt = hifn_encrypt_aes_ofb,
2494 .decrypt = hifn_decrypt_aes_ofb,
2495 },
2496 },
2497};
2498
2499static int hifn_cra_init(struct crypto_tfm *tfm)
2500{
2501 struct crypto_alg *alg = tfm->__crt_alg;
2502 struct hifn_crypto_alg *ha = crypto_alg_to_hifn(alg);
2503 struct hifn_context *ctx = crypto_tfm_ctx(tfm);
2504
2505 ctx->dev = ha->dev;
2506
2507 return 0;
2508}
2509
2510static int hifn_alg_alloc(struct hifn_device *dev, struct hifn_alg_template *t)
2511{
2512 struct hifn_crypto_alg *alg;
2513 int err;
2514
2515 alg = kzalloc(sizeof(struct hifn_crypto_alg), GFP_KERNEL);
2516 if (!alg)
2517 return -ENOMEM;
2518
2519 snprintf(alg->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s", t->name);
2520 snprintf(alg->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s", t->drv_name);
2521
2522 alg->alg.cra_priority = 300;
Herbert Xu332f88402007-11-15 22:36:07 +08002523 alg->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002524 alg->alg.cra_blocksize = t->bsize;
2525 alg->alg.cra_ctxsize = sizeof(struct hifn_context);
2526 alg->alg.cra_alignmask = 15;
2527 if (t->bsize == 8)
2528 alg->alg.cra_alignmask = 3;
2529 alg->alg.cra_type = &crypto_ablkcipher_type;
2530 alg->alg.cra_module = THIS_MODULE;
2531 alg->alg.cra_u.ablkcipher = t->ablkcipher;
2532 alg->alg.cra_init = hifn_cra_init;
2533
2534 alg->dev = dev;
2535
2536 list_add_tail(&alg->entry, &dev->alg_list);
2537
2538 err = crypto_register_alg(&alg->alg);
2539 if (err) {
2540 list_del(&alg->entry);
2541 kfree(alg);
2542 }
2543
2544 return err;
2545}
2546
2547static void hifn_unregister_alg(struct hifn_device *dev)
2548{
2549 struct hifn_crypto_alg *a, *n;
2550
2551 list_for_each_entry_safe(a, n, &dev->alg_list, entry) {
2552 list_del(&a->entry);
2553 crypto_unregister_alg(&a->alg);
2554 kfree(a);
2555 }
2556}
2557
2558static int hifn_register_alg(struct hifn_device *dev)
2559{
2560 int i, err;
2561
2562 for (i=0; i<ARRAY_SIZE(hifn_alg_templates); ++i) {
2563 err = hifn_alg_alloc(dev, &hifn_alg_templates[i]);
2564 if (err)
2565 goto err_out_exit;
2566 }
2567
2568 return 0;
2569
2570err_out_exit:
2571 hifn_unregister_alg(dev);
2572 return err;
2573}
2574
Evgeniy Polyakova1e6ef22007-11-10 20:24:18 +08002575static void hifn_tasklet_callback(unsigned long data)
2576{
2577 struct hifn_device *dev = (struct hifn_device *)data;
2578
2579 /*
2580 * This is ok to call this without lock being held,
2581 * althogh it modifies some parameters used in parallel,
2582 * (like dev->success), but they are used in process
2583 * context or update is atomic (like setting dev->sa[i] to NULL).
2584 */
2585 hifn_check_for_completion(dev, 0);
2586}
2587
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002588static int hifn_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2589{
2590 int err, i;
2591 struct hifn_device *dev;
2592 char name[8];
2593
2594 err = pci_enable_device(pdev);
2595 if (err)
2596 return err;
2597 pci_set_master(pdev);
2598
2599 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
2600 if (err)
2601 goto err_out_disable_pci_device;
2602
2603 snprintf(name, sizeof(name), "hifn%d",
2604 atomic_inc_return(&hifn_dev_number)-1);
2605
2606 err = pci_request_regions(pdev, name);
2607 if (err)
2608 goto err_out_disable_pci_device;
2609
2610 if (pci_resource_len(pdev, 0) < HIFN_BAR0_SIZE ||
2611 pci_resource_len(pdev, 1) < HIFN_BAR1_SIZE ||
2612 pci_resource_len(pdev, 2) < HIFN_BAR2_SIZE) {
2613 dprintk("%s: Broken hardware - I/O regions are too small.\n",
2614 pci_name(pdev));
2615 err = -ENODEV;
2616 goto err_out_free_regions;
2617 }
2618
2619 dev = kzalloc(sizeof(struct hifn_device) + sizeof(struct crypto_alg),
2620 GFP_KERNEL);
2621 if (!dev) {
2622 err = -ENOMEM;
2623 goto err_out_free_regions;
2624 }
2625
2626 INIT_LIST_HEAD(&dev->alg_list);
2627
2628 snprintf(dev->name, sizeof(dev->name), "%s", name);
2629 spin_lock_init(&dev->lock);
2630
2631 for (i=0; i<3; ++i) {
2632 unsigned long addr, size;
2633
2634 addr = pci_resource_start(pdev, i);
2635 size = pci_resource_len(pdev, i);
2636
2637 dev->bar[i] = ioremap_nocache(addr, size);
2638 if (!dev->bar[i])
2639 goto err_out_unmap_bars;
2640 }
2641
2642 dev->result_mem = __get_free_pages(GFP_KERNEL, HIFN_MAX_RESULT_ORDER);
2643 if (!dev->result_mem) {
2644 dprintk("Failed to allocate %d pages for result_mem.\n",
2645 HIFN_MAX_RESULT_ORDER);
2646 goto err_out_unmap_bars;
2647 }
2648 memset((void *)dev->result_mem, 0, PAGE_SIZE*(1<<HIFN_MAX_RESULT_ORDER));
2649
2650 dev->dst = pci_map_single(pdev, (void *)dev->result_mem,
2651 PAGE_SIZE << HIFN_MAX_RESULT_ORDER, PCI_DMA_FROMDEVICE);
2652
2653 dev->desc_virt = pci_alloc_consistent(pdev, sizeof(struct hifn_dma),
2654 &dev->desc_dma);
2655 if (!dev->desc_virt) {
2656 dprintk("Failed to allocate descriptor rings.\n");
2657 goto err_out_free_result_pages;
2658 }
2659 memset(dev->desc_virt, 0, sizeof(struct hifn_dma));
2660
2661 dev->pdev = pdev;
2662 dev->irq = pdev->irq;
2663
2664 for (i=0; i<HIFN_D_RES_RSIZE; ++i)
2665 dev->sa[i] = NULL;
2666
2667 pci_set_drvdata(pdev, dev);
2668
Evgeniy Polyakova1e6ef22007-11-10 20:24:18 +08002669 tasklet_init(&dev->tasklet, hifn_tasklet_callback, (unsigned long)dev);
2670
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002671 crypto_init_queue(&dev->queue, 1);
2672
2673 err = request_irq(dev->irq, hifn_interrupt, IRQF_SHARED, dev->name, dev);
2674 if (err) {
2675 dprintk("Failed to request IRQ%d: err: %d.\n", dev->irq, err);
2676 dev->irq = 0;
2677 goto err_out_free_desc;
2678 }
2679
2680 err = hifn_start_device(dev);
2681 if (err)
2682 goto err_out_free_irq;
2683
2684 err = hifn_test(dev, 1, 0);
2685 if (err)
2686 goto err_out_stop_device;
2687
Patrick McHardyfcd06752007-11-21 12:51:52 +08002688 err = hifn_register_rng(dev);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002689 if (err)
2690 goto err_out_stop_device;
2691
Patrick McHardyfcd06752007-11-21 12:51:52 +08002692 err = hifn_register_alg(dev);
2693 if (err)
2694 goto err_out_unregister_rng;
2695
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002696 INIT_DELAYED_WORK(&dev->work, hifn_work);
2697 schedule_delayed_work(&dev->work, HZ);
2698
2699 dprintk("HIFN crypto accelerator card at %s has been "
2700 "successfully registered as %s.\n",
2701 pci_name(pdev), dev->name);
2702
2703 return 0;
2704
Patrick McHardyfcd06752007-11-21 12:51:52 +08002705err_out_unregister_rng:
2706 hifn_unregister_rng(dev);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002707err_out_stop_device:
2708 hifn_reset_dma(dev, 1);
2709 hifn_stop_device(dev);
2710err_out_free_irq:
2711 free_irq(dev->irq, dev->name);
Evgeniy Polyakova1e6ef22007-11-10 20:24:18 +08002712 tasklet_kill(&dev->tasklet);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002713err_out_free_desc:
2714 pci_free_consistent(pdev, sizeof(struct hifn_dma),
2715 dev->desc_virt, dev->desc_dma);
2716
2717err_out_free_result_pages:
2718 pci_unmap_single(pdev, dev->dst, PAGE_SIZE << HIFN_MAX_RESULT_ORDER,
2719 PCI_DMA_FROMDEVICE);
2720 free_pages(dev->result_mem, HIFN_MAX_RESULT_ORDER);
2721
2722err_out_unmap_bars:
2723 for (i=0; i<3; ++i)
2724 if (dev->bar[i])
2725 iounmap(dev->bar[i]);
2726
2727err_out_free_regions:
2728 pci_release_regions(pdev);
2729
2730err_out_disable_pci_device:
2731 pci_disable_device(pdev);
2732
2733 return err;
2734}
2735
2736static void hifn_remove(struct pci_dev *pdev)
2737{
2738 int i;
2739 struct hifn_device *dev;
2740
2741 dev = pci_get_drvdata(pdev);
2742
2743 if (dev) {
2744 cancel_delayed_work(&dev->work);
2745 flush_scheduled_work();
2746
Patrick McHardyfcd06752007-11-21 12:51:52 +08002747 hifn_unregister_rng(dev);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002748 hifn_unregister_alg(dev);
2749 hifn_reset_dma(dev, 1);
2750 hifn_stop_device(dev);
2751
2752 free_irq(dev->irq, dev->name);
Evgeniy Polyakova1e6ef22007-11-10 20:24:18 +08002753 tasklet_kill(&dev->tasklet);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002754
2755 hifn_flush(dev);
2756
2757 pci_free_consistent(pdev, sizeof(struct hifn_dma),
2758 dev->desc_virt, dev->desc_dma);
2759 pci_unmap_single(pdev, dev->dst,
2760 PAGE_SIZE << HIFN_MAX_RESULT_ORDER,
2761 PCI_DMA_FROMDEVICE);
2762 free_pages(dev->result_mem, HIFN_MAX_RESULT_ORDER);
2763 for (i=0; i<3; ++i)
2764 if (dev->bar[i])
2765 iounmap(dev->bar[i]);
2766
2767 kfree(dev);
2768 }
2769
2770 pci_release_regions(pdev);
2771 pci_disable_device(pdev);
2772}
2773
2774static struct pci_device_id hifn_pci_tbl[] = {
2775 { PCI_DEVICE(PCI_VENDOR_ID_HIFN, PCI_DEVICE_ID_HIFN_7955) },
2776 { PCI_DEVICE(PCI_VENDOR_ID_HIFN, PCI_DEVICE_ID_HIFN_7956) },
2777 { 0 }
2778};
2779MODULE_DEVICE_TABLE(pci, hifn_pci_tbl);
2780
2781static struct pci_driver hifn_pci_driver = {
2782 .name = "hifn795x",
2783 .id_table = hifn_pci_tbl,
2784 .probe = hifn_probe,
2785 .remove = __devexit_p(hifn_remove),
2786};
2787
2788static int __devinit hifn_init(void)
2789{
Patrick McHardy37a80232007-11-21 12:47:13 +08002790 unsigned int freq;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002791 int err;
2792
Patrick McHardy37a80232007-11-21 12:47:13 +08002793 if (strncmp(hifn_pll_ref, "ext", 3) &&
2794 strncmp(hifn_pll_ref, "pci", 3)) {
2795 printk(KERN_ERR "hifn795x: invalid hifn_pll_ref clock, "
2796 "must be pci or ext");
2797 return -EINVAL;
2798 }
2799
2800 /*
2801 * For the 7955/7956 the reference clock frequency must be in the
2802 * range of 20MHz-100MHz. For the 7954 the upper bound is 66.67MHz,
2803 * but this chip is currently not supported.
2804 */
2805 if (hifn_pll_ref[3] != '\0') {
2806 freq = simple_strtoul(hifn_pll_ref + 3, NULL, 10);
2807 if (freq < 20 || freq > 100) {
2808 printk(KERN_ERR "hifn795x: invalid hifn_pll_ref "
2809 "frequency, must be in the range "
2810 "of 20-100");
2811 return -EINVAL;
2812 }
2813 }
2814
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002815 err = pci_register_driver(&hifn_pci_driver);
2816 if (err < 0) {
2817 dprintk("Failed to register PCI driver for %s device.\n",
2818 hifn_pci_driver.name);
2819 return -ENODEV;
2820 }
2821
2822 printk(KERN_INFO "Driver for HIFN 795x crypto accelerator chip "
2823 "has been successfully registered.\n");
2824
2825 return 0;
2826}
2827
2828static void __devexit hifn_fini(void)
2829{
2830 pci_unregister_driver(&hifn_pci_driver);
2831
2832 printk(KERN_INFO "Driver for HIFN 795x crypto accelerator chip "
2833 "has been successfully unregistered.\n");
2834}
2835
2836module_init(hifn_init);
2837module_exit(hifn_fini);
2838
2839MODULE_LICENSE("GPL");
2840MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
2841MODULE_DESCRIPTION("Driver for HIFN 795x crypto accelerator chip.");