blob: 0bcc15eecda0c8619fcd339b59d10c077f80712b [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>
32#include <linux/crypto.h>
Patrick McHardyfcd06752007-11-21 12:51:52 +080033#include <linux/hw_random.h>
34#include <linux/ktime.h>
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +080035
36#include <crypto/algapi.h>
Evgeniy Polyakovc3041f92007-10-11 19:58:16 +080037#include <crypto/des.h>
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +080038
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +080039//#define HIFN_DEBUG
40
41#ifdef HIFN_DEBUG
42#define dprintk(f, a...) printk(f, ##a)
43#else
44#define dprintk(f, a...) do {} while (0)
45#endif
46
Patrick McHardy37a80232007-11-21 12:47:13 +080047static char hifn_pll_ref[sizeof("extNNN")] = "ext";
48module_param_string(hifn_pll_ref, hifn_pll_ref, sizeof(hifn_pll_ref), 0444);
49MODULE_PARM_DESC(hifn_pll_ref,
50 "PLL reference clock (pci[freq] or ext[freq], default ext)");
51
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +080052static atomic_t hifn_dev_number;
53
54#define ACRYPTO_OP_DECRYPT 0
55#define ACRYPTO_OP_ENCRYPT 1
56#define ACRYPTO_OP_HMAC 2
57#define ACRYPTO_OP_RNG 3
58
59#define ACRYPTO_MODE_ECB 0
60#define ACRYPTO_MODE_CBC 1
61#define ACRYPTO_MODE_CFB 2
62#define ACRYPTO_MODE_OFB 3
63
64#define ACRYPTO_TYPE_AES_128 0
65#define ACRYPTO_TYPE_AES_192 1
66#define ACRYPTO_TYPE_AES_256 2
67#define ACRYPTO_TYPE_3DES 3
68#define ACRYPTO_TYPE_DES 4
69
70#define PCI_VENDOR_ID_HIFN 0x13A3
71#define PCI_DEVICE_ID_HIFN_7955 0x0020
72#define PCI_DEVICE_ID_HIFN_7956 0x001d
73
74/* I/O region sizes */
75
76#define HIFN_BAR0_SIZE 0x1000
77#define HIFN_BAR1_SIZE 0x2000
78#define HIFN_BAR2_SIZE 0x8000
79
80/* DMA registres */
81
82#define HIFN_DMA_CRA 0x0C /* DMA Command Ring Address */
83#define HIFN_DMA_SDRA 0x1C /* DMA Source Data Ring Address */
84#define HIFN_DMA_RRA 0x2C /* DMA Result Ring Address */
85#define HIFN_DMA_DDRA 0x3C /* DMA Destination Data Ring Address */
86#define HIFN_DMA_STCTL 0x40 /* DMA Status and Control */
87#define HIFN_DMA_INTREN 0x44 /* DMA Interrupt Enable */
88#define HIFN_DMA_CFG1 0x48 /* DMA Configuration #1 */
89#define HIFN_DMA_CFG2 0x6C /* DMA Configuration #2 */
90#define HIFN_CHIP_ID 0x98 /* Chip ID */
91
92/*
93 * Processing Unit Registers (offset from BASEREG0)
94 */
95#define HIFN_0_PUDATA 0x00 /* Processing Unit Data */
96#define HIFN_0_PUCTRL 0x04 /* Processing Unit Control */
97#define HIFN_0_PUISR 0x08 /* Processing Unit Interrupt Status */
98#define HIFN_0_PUCNFG 0x0c /* Processing Unit Configuration */
99#define HIFN_0_PUIER 0x10 /* Processing Unit Interrupt Enable */
100#define HIFN_0_PUSTAT 0x14 /* Processing Unit Status/Chip ID */
101#define HIFN_0_FIFOSTAT 0x18 /* FIFO Status */
102#define HIFN_0_FIFOCNFG 0x1c /* FIFO Configuration */
103#define HIFN_0_SPACESIZE 0x20 /* Register space size */
104
105/* Processing Unit Control Register (HIFN_0_PUCTRL) */
106#define HIFN_PUCTRL_CLRSRCFIFO 0x0010 /* clear source fifo */
107#define HIFN_PUCTRL_STOP 0x0008 /* stop pu */
108#define HIFN_PUCTRL_LOCKRAM 0x0004 /* lock ram */
109#define HIFN_PUCTRL_DMAENA 0x0002 /* enable dma */
110#define HIFN_PUCTRL_RESET 0x0001 /* Reset processing unit */
111
112/* Processing Unit Interrupt Status Register (HIFN_0_PUISR) */
113#define HIFN_PUISR_CMDINVAL 0x8000 /* Invalid command interrupt */
114#define HIFN_PUISR_DATAERR 0x4000 /* Data error interrupt */
115#define HIFN_PUISR_SRCFIFO 0x2000 /* Source FIFO ready interrupt */
116#define HIFN_PUISR_DSTFIFO 0x1000 /* Destination FIFO ready interrupt */
117#define HIFN_PUISR_DSTOVER 0x0200 /* Destination overrun interrupt */
118#define HIFN_PUISR_SRCCMD 0x0080 /* Source command interrupt */
119#define HIFN_PUISR_SRCCTX 0x0040 /* Source context interrupt */
120#define HIFN_PUISR_SRCDATA 0x0020 /* Source data interrupt */
121#define HIFN_PUISR_DSTDATA 0x0010 /* Destination data interrupt */
122#define HIFN_PUISR_DSTRESULT 0x0004 /* Destination result interrupt */
123
124/* Processing Unit Configuration Register (HIFN_0_PUCNFG) */
125#define HIFN_PUCNFG_DRAMMASK 0xe000 /* DRAM size mask */
126#define HIFN_PUCNFG_DSZ_256K 0x0000 /* 256k dram */
127#define HIFN_PUCNFG_DSZ_512K 0x2000 /* 512k dram */
128#define HIFN_PUCNFG_DSZ_1M 0x4000 /* 1m dram */
129#define HIFN_PUCNFG_DSZ_2M 0x6000 /* 2m dram */
130#define HIFN_PUCNFG_DSZ_4M 0x8000 /* 4m dram */
131#define HIFN_PUCNFG_DSZ_8M 0xa000 /* 8m dram */
132#define HIFN_PUNCFG_DSZ_16M 0xc000 /* 16m dram */
133#define HIFN_PUCNFG_DSZ_32M 0xe000 /* 32m dram */
134#define HIFN_PUCNFG_DRAMREFRESH 0x1800 /* DRAM refresh rate mask */
135#define HIFN_PUCNFG_DRFR_512 0x0000 /* 512 divisor of ECLK */
136#define HIFN_PUCNFG_DRFR_256 0x0800 /* 256 divisor of ECLK */
137#define HIFN_PUCNFG_DRFR_128 0x1000 /* 128 divisor of ECLK */
138#define HIFN_PUCNFG_TCALLPHASES 0x0200 /* your guess is as good as mine... */
139#define HIFN_PUCNFG_TCDRVTOTEM 0x0100 /* your guess is as good as mine... */
140#define HIFN_PUCNFG_BIGENDIAN 0x0080 /* DMA big endian mode */
141#define HIFN_PUCNFG_BUS32 0x0040 /* Bus width 32bits */
142#define HIFN_PUCNFG_BUS16 0x0000 /* Bus width 16 bits */
143#define HIFN_PUCNFG_CHIPID 0x0020 /* Allow chipid from PUSTAT */
144#define HIFN_PUCNFG_DRAM 0x0010 /* Context RAM is DRAM */
145#define HIFN_PUCNFG_SRAM 0x0000 /* Context RAM is SRAM */
146#define HIFN_PUCNFG_COMPSING 0x0004 /* Enable single compression context */
147#define HIFN_PUCNFG_ENCCNFG 0x0002 /* Encryption configuration */
148
149/* Processing Unit Interrupt Enable Register (HIFN_0_PUIER) */
150#define HIFN_PUIER_CMDINVAL 0x8000 /* Invalid command interrupt */
151#define HIFN_PUIER_DATAERR 0x4000 /* Data error interrupt */
152#define HIFN_PUIER_SRCFIFO 0x2000 /* Source FIFO ready interrupt */
153#define HIFN_PUIER_DSTFIFO 0x1000 /* Destination FIFO ready interrupt */
154#define HIFN_PUIER_DSTOVER 0x0200 /* Destination overrun interrupt */
155#define HIFN_PUIER_SRCCMD 0x0080 /* Source command interrupt */
156#define HIFN_PUIER_SRCCTX 0x0040 /* Source context interrupt */
157#define HIFN_PUIER_SRCDATA 0x0020 /* Source data interrupt */
158#define HIFN_PUIER_DSTDATA 0x0010 /* Destination data interrupt */
159#define HIFN_PUIER_DSTRESULT 0x0004 /* Destination result interrupt */
160
161/* Processing Unit Status Register/Chip ID (HIFN_0_PUSTAT) */
162#define HIFN_PUSTAT_CMDINVAL 0x8000 /* Invalid command interrupt */
163#define HIFN_PUSTAT_DATAERR 0x4000 /* Data error interrupt */
164#define HIFN_PUSTAT_SRCFIFO 0x2000 /* Source FIFO ready interrupt */
165#define HIFN_PUSTAT_DSTFIFO 0x1000 /* Destination FIFO ready interrupt */
166#define HIFN_PUSTAT_DSTOVER 0x0200 /* Destination overrun interrupt */
167#define HIFN_PUSTAT_SRCCMD 0x0080 /* Source command interrupt */
168#define HIFN_PUSTAT_SRCCTX 0x0040 /* Source context interrupt */
169#define HIFN_PUSTAT_SRCDATA 0x0020 /* Source data interrupt */
170#define HIFN_PUSTAT_DSTDATA 0x0010 /* Destination data interrupt */
171#define HIFN_PUSTAT_DSTRESULT 0x0004 /* Destination result interrupt */
172#define HIFN_PUSTAT_CHIPREV 0x00ff /* Chip revision mask */
173#define HIFN_PUSTAT_CHIPENA 0xff00 /* Chip enabled mask */
174#define HIFN_PUSTAT_ENA_2 0x1100 /* Level 2 enabled */
175#define HIFN_PUSTAT_ENA_1 0x1000 /* Level 1 enabled */
176#define HIFN_PUSTAT_ENA_0 0x3000 /* Level 0 enabled */
177#define HIFN_PUSTAT_REV_2 0x0020 /* 7751 PT6/2 */
178#define HIFN_PUSTAT_REV_3 0x0030 /* 7751 PT6/3 */
179
180/* FIFO Status Register (HIFN_0_FIFOSTAT) */
181#define HIFN_FIFOSTAT_SRC 0x7f00 /* Source FIFO available */
182#define HIFN_FIFOSTAT_DST 0x007f /* Destination FIFO available */
183
184/* FIFO Configuration Register (HIFN_0_FIFOCNFG) */
185#define HIFN_FIFOCNFG_THRESHOLD 0x0400 /* must be written as 1 */
186
187/*
188 * DMA Interface Registers (offset from BASEREG1)
189 */
190#define HIFN_1_DMA_CRAR 0x0c /* DMA Command Ring Address */
191#define HIFN_1_DMA_SRAR 0x1c /* DMA Source Ring Address */
192#define HIFN_1_DMA_RRAR 0x2c /* DMA Result Ring Address */
193#define HIFN_1_DMA_DRAR 0x3c /* DMA Destination Ring Address */
194#define HIFN_1_DMA_CSR 0x40 /* DMA Status and Control */
195#define HIFN_1_DMA_IER 0x44 /* DMA Interrupt Enable */
196#define HIFN_1_DMA_CNFG 0x48 /* DMA Configuration */
197#define HIFN_1_PLL 0x4c /* 795x: PLL config */
198#define HIFN_1_7811_RNGENA 0x60 /* 7811: rng enable */
199#define HIFN_1_7811_RNGCFG 0x64 /* 7811: rng config */
200#define HIFN_1_7811_RNGDAT 0x68 /* 7811: rng data */
201#define HIFN_1_7811_RNGSTS 0x6c /* 7811: rng status */
202#define HIFN_1_7811_MIPSRST 0x94 /* 7811: MIPS reset */
203#define HIFN_1_REVID 0x98 /* Revision ID */
204#define HIFN_1_UNLOCK_SECRET1 0xf4
205#define HIFN_1_UNLOCK_SECRET2 0xfc
206#define HIFN_1_PUB_RESET 0x204 /* Public/RNG Reset */
207#define HIFN_1_PUB_BASE 0x300 /* Public Base Address */
208#define HIFN_1_PUB_OPLEN 0x304 /* Public Operand Length */
209#define HIFN_1_PUB_OP 0x308 /* Public Operand */
210#define HIFN_1_PUB_STATUS 0x30c /* Public Status */
211#define HIFN_1_PUB_IEN 0x310 /* Public Interrupt enable */
212#define HIFN_1_RNG_CONFIG 0x314 /* RNG config */
213#define HIFN_1_RNG_DATA 0x318 /* RNG data */
214#define HIFN_1_PUB_MEM 0x400 /* start of Public key memory */
215#define HIFN_1_PUB_MEMEND 0xbff /* end of Public key memory */
216
217/* DMA Status and Control Register (HIFN_1_DMA_CSR) */
218#define HIFN_DMACSR_D_CTRLMASK 0xc0000000 /* Destinition Ring Control */
219#define HIFN_DMACSR_D_CTRL_NOP 0x00000000 /* Dest. Control: no-op */
220#define HIFN_DMACSR_D_CTRL_DIS 0x40000000 /* Dest. Control: disable */
221#define HIFN_DMACSR_D_CTRL_ENA 0x80000000 /* Dest. Control: enable */
222#define HIFN_DMACSR_D_ABORT 0x20000000 /* Destinition Ring PCIAbort */
223#define HIFN_DMACSR_D_DONE 0x10000000 /* Destinition Ring Done */
224#define HIFN_DMACSR_D_LAST 0x08000000 /* Destinition Ring Last */
225#define HIFN_DMACSR_D_WAIT 0x04000000 /* Destinition Ring Waiting */
226#define HIFN_DMACSR_D_OVER 0x02000000 /* Destinition Ring Overflow */
227#define HIFN_DMACSR_R_CTRL 0x00c00000 /* Result Ring Control */
228#define HIFN_DMACSR_R_CTRL_NOP 0x00000000 /* Result Control: no-op */
229#define HIFN_DMACSR_R_CTRL_DIS 0x00400000 /* Result Control: disable */
230#define HIFN_DMACSR_R_CTRL_ENA 0x00800000 /* Result Control: enable */
231#define HIFN_DMACSR_R_ABORT 0x00200000 /* Result Ring PCI Abort */
232#define HIFN_DMACSR_R_DONE 0x00100000 /* Result Ring Done */
233#define HIFN_DMACSR_R_LAST 0x00080000 /* Result Ring Last */
234#define HIFN_DMACSR_R_WAIT 0x00040000 /* Result Ring Waiting */
235#define HIFN_DMACSR_R_OVER 0x00020000 /* Result Ring Overflow */
236#define HIFN_DMACSR_S_CTRL 0x0000c000 /* Source Ring Control */
237#define HIFN_DMACSR_S_CTRL_NOP 0x00000000 /* Source Control: no-op */
238#define HIFN_DMACSR_S_CTRL_DIS 0x00004000 /* Source Control: disable */
239#define HIFN_DMACSR_S_CTRL_ENA 0x00008000 /* Source Control: enable */
240#define HIFN_DMACSR_S_ABORT 0x00002000 /* Source Ring PCI Abort */
241#define HIFN_DMACSR_S_DONE 0x00001000 /* Source Ring Done */
242#define HIFN_DMACSR_S_LAST 0x00000800 /* Source Ring Last */
243#define HIFN_DMACSR_S_WAIT 0x00000400 /* Source Ring Waiting */
244#define HIFN_DMACSR_ILLW 0x00000200 /* Illegal write (7811 only) */
245#define HIFN_DMACSR_ILLR 0x00000100 /* Illegal read (7811 only) */
246#define HIFN_DMACSR_C_CTRL 0x000000c0 /* Command Ring Control */
247#define HIFN_DMACSR_C_CTRL_NOP 0x00000000 /* Command Control: no-op */
248#define HIFN_DMACSR_C_CTRL_DIS 0x00000040 /* Command Control: disable */
249#define HIFN_DMACSR_C_CTRL_ENA 0x00000080 /* Command Control: enable */
250#define HIFN_DMACSR_C_ABORT 0x00000020 /* Command Ring PCI Abort */
251#define HIFN_DMACSR_C_DONE 0x00000010 /* Command Ring Done */
252#define HIFN_DMACSR_C_LAST 0x00000008 /* Command Ring Last */
253#define HIFN_DMACSR_C_WAIT 0x00000004 /* Command Ring Waiting */
254#define HIFN_DMACSR_PUBDONE 0x00000002 /* Public op done (7951 only) */
255#define HIFN_DMACSR_ENGINE 0x00000001 /* Command Ring Engine IRQ */
256
257/* DMA Interrupt Enable Register (HIFN_1_DMA_IER) */
258#define HIFN_DMAIER_D_ABORT 0x20000000 /* Destination Ring PCIAbort */
259#define HIFN_DMAIER_D_DONE 0x10000000 /* Destination Ring Done */
260#define HIFN_DMAIER_D_LAST 0x08000000 /* Destination Ring Last */
261#define HIFN_DMAIER_D_WAIT 0x04000000 /* Destination Ring Waiting */
262#define HIFN_DMAIER_D_OVER 0x02000000 /* Destination Ring Overflow */
263#define HIFN_DMAIER_R_ABORT 0x00200000 /* Result Ring PCI Abort */
264#define HIFN_DMAIER_R_DONE 0x00100000 /* Result Ring Done */
265#define HIFN_DMAIER_R_LAST 0x00080000 /* Result Ring Last */
266#define HIFN_DMAIER_R_WAIT 0x00040000 /* Result Ring Waiting */
267#define HIFN_DMAIER_R_OVER 0x00020000 /* Result Ring Overflow */
268#define HIFN_DMAIER_S_ABORT 0x00002000 /* Source Ring PCI Abort */
269#define HIFN_DMAIER_S_DONE 0x00001000 /* Source Ring Done */
270#define HIFN_DMAIER_S_LAST 0x00000800 /* Source Ring Last */
271#define HIFN_DMAIER_S_WAIT 0x00000400 /* Source Ring Waiting */
272#define HIFN_DMAIER_ILLW 0x00000200 /* Illegal write (7811 only) */
273#define HIFN_DMAIER_ILLR 0x00000100 /* Illegal read (7811 only) */
274#define HIFN_DMAIER_C_ABORT 0x00000020 /* Command Ring PCI Abort */
275#define HIFN_DMAIER_C_DONE 0x00000010 /* Command Ring Done */
276#define HIFN_DMAIER_C_LAST 0x00000008 /* Command Ring Last */
277#define HIFN_DMAIER_C_WAIT 0x00000004 /* Command Ring Waiting */
278#define HIFN_DMAIER_PUBDONE 0x00000002 /* public op done (7951 only) */
279#define HIFN_DMAIER_ENGINE 0x00000001 /* Engine IRQ */
280
281/* DMA Configuration Register (HIFN_1_DMA_CNFG) */
282#define HIFN_DMACNFG_BIGENDIAN 0x10000000 /* big endian mode */
283#define HIFN_DMACNFG_POLLFREQ 0x00ff0000 /* Poll frequency mask */
284#define HIFN_DMACNFG_UNLOCK 0x00000800
285#define HIFN_DMACNFG_POLLINVAL 0x00000700 /* Invalid Poll Scalar */
286#define HIFN_DMACNFG_LAST 0x00000010 /* Host control LAST bit */
287#define HIFN_DMACNFG_MODE 0x00000004 /* DMA mode */
288#define HIFN_DMACNFG_DMARESET 0x00000002 /* DMA Reset # */
289#define HIFN_DMACNFG_MSTRESET 0x00000001 /* Master Reset # */
290
Patrick McHardy37a80232007-11-21 12:47:13 +0800291/* PLL configuration register */
292#define HIFN_PLL_REF_CLK_HBI 0x00000000 /* HBI reference clock */
293#define HIFN_PLL_REF_CLK_PLL 0x00000001 /* PLL reference clock */
294#define HIFN_PLL_BP 0x00000002 /* Reference clock bypass */
295#define HIFN_PLL_PK_CLK_HBI 0x00000000 /* PK engine HBI clock */
296#define HIFN_PLL_PK_CLK_PLL 0x00000008 /* PK engine PLL clock */
297#define HIFN_PLL_PE_CLK_HBI 0x00000000 /* PE engine HBI clock */
298#define HIFN_PLL_PE_CLK_PLL 0x00000010 /* PE engine PLL clock */
299#define HIFN_PLL_RESERVED_1 0x00000400 /* Reserved bit, must be 1 */
300#define HIFN_PLL_ND_SHIFT 11 /* Clock multiplier shift */
301#define HIFN_PLL_ND_MULT_2 0x00000000 /* PLL clock multiplier 2 */
302#define HIFN_PLL_ND_MULT_4 0x00000800 /* PLL clock multiplier 4 */
303#define HIFN_PLL_ND_MULT_6 0x00001000 /* PLL clock multiplier 6 */
304#define HIFN_PLL_ND_MULT_8 0x00001800 /* PLL clock multiplier 8 */
305#define HIFN_PLL_ND_MULT_10 0x00002000 /* PLL clock multiplier 10 */
306#define HIFN_PLL_ND_MULT_12 0x00002800 /* PLL clock multiplier 12 */
307#define HIFN_PLL_IS_1_8 0x00000000 /* charge pump (mult. 1-8) */
308#define HIFN_PLL_IS_9_12 0x00010000 /* charge pump (mult. 9-12) */
309
310#define HIFN_PLL_FCK_MAX 266 /* Maximum PLL frequency */
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800311
312/* Public key reset register (HIFN_1_PUB_RESET) */
313#define HIFN_PUBRST_RESET 0x00000001 /* reset public/rng unit */
314
315/* Public base address register (HIFN_1_PUB_BASE) */
316#define HIFN_PUBBASE_ADDR 0x00003fff /* base address */
317
318/* Public operand length register (HIFN_1_PUB_OPLEN) */
319#define HIFN_PUBOPLEN_MOD_M 0x0000007f /* modulus length mask */
320#define HIFN_PUBOPLEN_MOD_S 0 /* modulus length shift */
321#define HIFN_PUBOPLEN_EXP_M 0x0003ff80 /* exponent length mask */
Daniel Mack1537a362010-01-29 15:57:49 +0800322#define HIFN_PUBOPLEN_EXP_S 7 /* exponent length shift */
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800323#define HIFN_PUBOPLEN_RED_M 0x003c0000 /* reducend length mask */
324#define HIFN_PUBOPLEN_RED_S 18 /* reducend length shift */
325
326/* Public operation register (HIFN_1_PUB_OP) */
327#define HIFN_PUBOP_AOFFSET_M 0x0000007f /* A offset mask */
328#define HIFN_PUBOP_AOFFSET_S 0 /* A offset shift */
329#define HIFN_PUBOP_BOFFSET_M 0x00000f80 /* B offset mask */
330#define HIFN_PUBOP_BOFFSET_S 7 /* B offset shift */
331#define HIFN_PUBOP_MOFFSET_M 0x0003f000 /* M offset mask */
332#define HIFN_PUBOP_MOFFSET_S 12 /* M offset shift */
333#define HIFN_PUBOP_OP_MASK 0x003c0000 /* Opcode: */
334#define HIFN_PUBOP_OP_NOP 0x00000000 /* NOP */
335#define HIFN_PUBOP_OP_ADD 0x00040000 /* ADD */
336#define HIFN_PUBOP_OP_ADDC 0x00080000 /* ADD w/carry */
337#define HIFN_PUBOP_OP_SUB 0x000c0000 /* SUB */
338#define HIFN_PUBOP_OP_SUBC 0x00100000 /* SUB w/carry */
339#define HIFN_PUBOP_OP_MODADD 0x00140000 /* Modular ADD */
340#define HIFN_PUBOP_OP_MODSUB 0x00180000 /* Modular SUB */
341#define HIFN_PUBOP_OP_INCA 0x001c0000 /* INC A */
342#define HIFN_PUBOP_OP_DECA 0x00200000 /* DEC A */
343#define HIFN_PUBOP_OP_MULT 0x00240000 /* MULT */
344#define HIFN_PUBOP_OP_MODMULT 0x00280000 /* Modular MULT */
345#define HIFN_PUBOP_OP_MODRED 0x002c0000 /* Modular RED */
346#define HIFN_PUBOP_OP_MODEXP 0x00300000 /* Modular EXP */
347
348/* Public status register (HIFN_1_PUB_STATUS) */
349#define HIFN_PUBSTS_DONE 0x00000001 /* operation done */
350#define HIFN_PUBSTS_CARRY 0x00000002 /* carry */
351
352/* Public interrupt enable register (HIFN_1_PUB_IEN) */
353#define HIFN_PUBIEN_DONE 0x00000001 /* operation done interrupt */
354
355/* Random number generator config register (HIFN_1_RNG_CONFIG) */
356#define HIFN_RNGCFG_ENA 0x00000001 /* enable rng */
357
358#define HIFN_NAMESIZE 32
359#define HIFN_MAX_RESULT_ORDER 5
360
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +0800361#define HIFN_D_CMD_RSIZE 24*1
362#define HIFN_D_SRC_RSIZE 80*1
363#define HIFN_D_DST_RSIZE 80*1
364#define HIFN_D_RES_RSIZE 24*1
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800365
Patrick McHardyd0690332008-05-07 22:33:37 +0800366#define HIFN_D_DST_DALIGN 4
367
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +0800368#define HIFN_QUEUE_LENGTH (HIFN_D_CMD_RSIZE - 1)
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800369
370#define AES_MIN_KEY_SIZE 16
371#define AES_MAX_KEY_SIZE 32
372
373#define HIFN_DES_KEY_LENGTH 8
374#define HIFN_3DES_KEY_LENGTH 24
375#define HIFN_MAX_CRYPT_KEY_LENGTH AES_MAX_KEY_SIZE
376#define HIFN_IV_LENGTH 8
377#define HIFN_AES_IV_LENGTH 16
378#define HIFN_MAX_IV_LENGTH HIFN_AES_IV_LENGTH
379
380#define HIFN_MAC_KEY_LENGTH 64
381#define HIFN_MD5_LENGTH 16
382#define HIFN_SHA1_LENGTH 20
383#define HIFN_MAC_TRUNC_LENGTH 12
384
385#define HIFN_MAX_COMMAND (8 + 8 + 8 + 64 + 260)
386#define HIFN_MAX_RESULT (8 + 4 + 4 + 20 + 4)
387#define HIFN_USED_RESULT 12
388
389struct hifn_desc
390{
Al Viroe68970c2008-03-29 03:09:58 +0000391 volatile __le32 l;
392 volatile __le32 p;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800393};
394
395struct hifn_dma {
396 struct hifn_desc cmdr[HIFN_D_CMD_RSIZE+1];
397 struct hifn_desc srcr[HIFN_D_SRC_RSIZE+1];
398 struct hifn_desc dstr[HIFN_D_DST_RSIZE+1];
399 struct hifn_desc resr[HIFN_D_RES_RSIZE+1];
400
401 u8 command_bufs[HIFN_D_CMD_RSIZE][HIFN_MAX_COMMAND];
402 u8 result_bufs[HIFN_D_CMD_RSIZE][HIFN_MAX_RESULT];
403
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800404 /*
405 * Our current positions for insertion and removal from the descriptor
406 * rings.
407 */
408 volatile int cmdi, srci, dsti, resi;
409 volatile int cmdu, srcu, dstu, resu;
410 int cmdk, srck, dstk, resk;
411};
412
413#define HIFN_FLAG_CMD_BUSY (1<<0)
414#define HIFN_FLAG_SRC_BUSY (1<<1)
415#define HIFN_FLAG_DST_BUSY (1<<2)
416#define HIFN_FLAG_RES_BUSY (1<<3)
417#define HIFN_FLAG_OLD_KEY (1<<4)
418
419#define HIFN_DEFAULT_ACTIVE_NUM 5
420
421struct hifn_device
422{
423 char name[HIFN_NAMESIZE];
424
425 int irq;
426
427 struct pci_dev *pdev;
428 void __iomem *bar[3];
429
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800430 void *desc_virt;
431 dma_addr_t desc_dma;
432
433 u32 dmareg;
434
435 void *sa[HIFN_D_RES_RSIZE];
436
437 spinlock_t lock;
438
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800439 u32 flags;
440 int active, started;
441 struct delayed_work work;
442 unsigned long reset;
443 unsigned long success;
444 unsigned long prev_success;
445
446 u8 snum;
447
Evgeniy Polyakova1e6ef22007-11-10 20:24:18 +0800448 struct tasklet_struct tasklet;
449
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800450 struct crypto_queue queue;
451 struct list_head alg_list;
Patrick McHardyfcd06752007-11-21 12:51:52 +0800452
453 unsigned int pk_clk_freq;
454
Patrick McHardyf881d822008-02-15 19:15:05 +0800455#ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
Patrick McHardyfcd06752007-11-21 12:51:52 +0800456 unsigned int rng_wait_time;
457 ktime_t rngtime;
458 struct hwrng rng;
459#endif
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800460};
461
462#define HIFN_D_LENGTH 0x0000ffff
463#define HIFN_D_NOINVALID 0x01000000
464#define HIFN_D_MASKDONEIRQ 0x02000000
465#define HIFN_D_DESTOVER 0x04000000
466#define HIFN_D_OVER 0x08000000
467#define HIFN_D_LAST 0x20000000
468#define HIFN_D_JUMP 0x40000000
469#define HIFN_D_VALID 0x80000000
470
471struct hifn_base_command
472{
Al Viroe68970c2008-03-29 03:09:58 +0000473 volatile __le16 masks;
474 volatile __le16 session_num;
475 volatile __le16 total_source_count;
476 volatile __le16 total_dest_count;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800477};
478
479#define HIFN_BASE_CMD_COMP 0x0100 /* enable compression engine */
480#define HIFN_BASE_CMD_PAD 0x0200 /* enable padding engine */
481#define HIFN_BASE_CMD_MAC 0x0400 /* enable MAC engine */
482#define HIFN_BASE_CMD_CRYPT 0x0800 /* enable crypt engine */
483#define HIFN_BASE_CMD_DECODE 0x2000
484#define HIFN_BASE_CMD_SRCLEN_M 0xc000
485#define HIFN_BASE_CMD_SRCLEN_S 14
486#define HIFN_BASE_CMD_DSTLEN_M 0x3000
487#define HIFN_BASE_CMD_DSTLEN_S 12
488#define HIFN_BASE_CMD_LENMASK_HI 0x30000
489#define HIFN_BASE_CMD_LENMASK_LO 0x0ffff
490
491/*
492 * Structure to help build up the command data structure.
493 */
494struct hifn_crypt_command
495{
Al Viroe68970c2008-03-29 03:09:58 +0000496 volatile __le16 masks;
497 volatile __le16 header_skip;
498 volatile __le16 source_count;
499 volatile __le16 reserved;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800500};
501
502#define HIFN_CRYPT_CMD_ALG_MASK 0x0003 /* algorithm: */
503#define HIFN_CRYPT_CMD_ALG_DES 0x0000 /* DES */
504#define HIFN_CRYPT_CMD_ALG_3DES 0x0001 /* 3DES */
505#define HIFN_CRYPT_CMD_ALG_RC4 0x0002 /* RC4 */
506#define HIFN_CRYPT_CMD_ALG_AES 0x0003 /* AES */
507#define HIFN_CRYPT_CMD_MODE_MASK 0x0018 /* Encrypt mode: */
508#define HIFN_CRYPT_CMD_MODE_ECB 0x0000 /* ECB */
509#define HIFN_CRYPT_CMD_MODE_CBC 0x0008 /* CBC */
510#define HIFN_CRYPT_CMD_MODE_CFB 0x0010 /* CFB */
511#define HIFN_CRYPT_CMD_MODE_OFB 0x0018 /* OFB */
512#define HIFN_CRYPT_CMD_CLR_CTX 0x0040 /* clear context */
513#define HIFN_CRYPT_CMD_KSZ_MASK 0x0600 /* AES key size: */
514#define HIFN_CRYPT_CMD_KSZ_128 0x0000 /* 128 bit */
515#define HIFN_CRYPT_CMD_KSZ_192 0x0200 /* 192 bit */
516#define HIFN_CRYPT_CMD_KSZ_256 0x0400 /* 256 bit */
517#define HIFN_CRYPT_CMD_NEW_KEY 0x0800 /* expect new key */
518#define HIFN_CRYPT_CMD_NEW_IV 0x1000 /* expect new iv */
519#define HIFN_CRYPT_CMD_SRCLEN_M 0xc000
520#define HIFN_CRYPT_CMD_SRCLEN_S 14
521
522/*
523 * Structure to help build up the command data structure.
524 */
525struct hifn_mac_command
526{
Patrick McHardy3c42cbc2008-05-07 22:28:27 +0800527 volatile __le16 masks;
528 volatile __le16 header_skip;
529 volatile __le16 source_count;
530 volatile __le16 reserved;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800531};
532
533#define HIFN_MAC_CMD_ALG_MASK 0x0001
534#define HIFN_MAC_CMD_ALG_SHA1 0x0000
535#define HIFN_MAC_CMD_ALG_MD5 0x0001
536#define HIFN_MAC_CMD_MODE_MASK 0x000c
537#define HIFN_MAC_CMD_MODE_HMAC 0x0000
538#define HIFN_MAC_CMD_MODE_SSL_MAC 0x0004
539#define HIFN_MAC_CMD_MODE_HASH 0x0008
540#define HIFN_MAC_CMD_MODE_FULL 0x0004
541#define HIFN_MAC_CMD_TRUNC 0x0010
542#define HIFN_MAC_CMD_RESULT 0x0020
543#define HIFN_MAC_CMD_APPEND 0x0040
544#define HIFN_MAC_CMD_SRCLEN_M 0xc000
545#define HIFN_MAC_CMD_SRCLEN_S 14
546
547/*
548 * MAC POS IPsec initiates authentication after encryption on encodes
549 * and before decryption on decodes.
550 */
551#define HIFN_MAC_CMD_POS_IPSEC 0x0200
552#define HIFN_MAC_CMD_NEW_KEY 0x0800
553
554struct hifn_comp_command
555{
Patrick McHardy3c42cbc2008-05-07 22:28:27 +0800556 volatile __le16 masks;
557 volatile __le16 header_skip;
558 volatile __le16 source_count;
559 volatile __le16 reserved;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800560};
561
562#define HIFN_COMP_CMD_SRCLEN_M 0xc000
563#define HIFN_COMP_CMD_SRCLEN_S 14
564#define HIFN_COMP_CMD_ONE 0x0100 /* must be one */
565#define HIFN_COMP_CMD_CLEARHIST 0x0010 /* clear history */
566#define HIFN_COMP_CMD_UPDATEHIST 0x0008 /* update history */
567#define HIFN_COMP_CMD_LZS_STRIP0 0x0004 /* LZS: strip zero */
568#define HIFN_COMP_CMD_MPPC_RESTART 0x0004 /* MPPC: restart */
569#define HIFN_COMP_CMD_ALG_MASK 0x0001 /* compression mode: */
570#define HIFN_COMP_CMD_ALG_MPPC 0x0001 /* MPPC */
571#define HIFN_COMP_CMD_ALG_LZS 0x0000 /* LZS */
572
573struct hifn_base_result
574{
Patrick McHardy3c42cbc2008-05-07 22:28:27 +0800575 volatile __le16 flags;
576 volatile __le16 session;
577 volatile __le16 src_cnt; /* 15:0 of source count */
578 volatile __le16 dst_cnt; /* 15:0 of dest count */
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800579};
580
581#define HIFN_BASE_RES_DSTOVERRUN 0x0200 /* destination overrun */
582#define HIFN_BASE_RES_SRCLEN_M 0xc000 /* 17:16 of source count */
583#define HIFN_BASE_RES_SRCLEN_S 14
584#define HIFN_BASE_RES_DSTLEN_M 0x3000 /* 17:16 of dest count */
585#define HIFN_BASE_RES_DSTLEN_S 12
586
587struct hifn_comp_result
588{
Patrick McHardy3c42cbc2008-05-07 22:28:27 +0800589 volatile __le16 flags;
590 volatile __le16 crc;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800591};
592
593#define HIFN_COMP_RES_LCB_M 0xff00 /* longitudinal check byte */
594#define HIFN_COMP_RES_LCB_S 8
595#define HIFN_COMP_RES_RESTART 0x0004 /* MPPC: restart */
596#define HIFN_COMP_RES_ENDMARKER 0x0002 /* LZS: end marker seen */
597#define HIFN_COMP_RES_SRC_NOTZERO 0x0001 /* source expired */
598
599struct hifn_mac_result
600{
Patrick McHardy3c42cbc2008-05-07 22:28:27 +0800601 volatile __le16 flags;
602 volatile __le16 reserved;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800603 /* followed by 0, 6, 8, or 10 u16's of the MAC, then crypt */
604};
605
606#define HIFN_MAC_RES_MISCOMPARE 0x0002 /* compare failed */
607#define HIFN_MAC_RES_SRC_NOTZERO 0x0001 /* source expired */
608
609struct hifn_crypt_result
610{
Patrick McHardy3c42cbc2008-05-07 22:28:27 +0800611 volatile __le16 flags;
612 volatile __le16 reserved;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800613};
614
615#define HIFN_CRYPT_RES_SRC_NOTZERO 0x0001 /* source expired */
616
617#ifndef HIFN_POLL_FREQUENCY
618#define HIFN_POLL_FREQUENCY 0x1
619#endif
620
621#ifndef HIFN_POLL_SCALAR
622#define HIFN_POLL_SCALAR 0x0
623#endif
624
625#define HIFN_MAX_SEGLEN 0xffff /* maximum dma segment len */
626#define HIFN_MAX_DMALEN 0x3ffff /* maximum dma length */
627
628struct hifn_crypto_alg
629{
630 struct list_head entry;
631 struct crypto_alg alg;
632 struct hifn_device *dev;
633};
634
635#define ASYNC_SCATTERLIST_CACHE 16
636
637#define ASYNC_FLAGS_MISALIGNED (1<<0)
638
David S. Miller33853292010-05-19 13:56:37 +1000639struct hifn_cipher_walk
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800640{
641 struct scatterlist cache[ASYNC_SCATTERLIST_CACHE];
642 u32 flags;
643 int num;
644};
645
646struct hifn_context
647{
Patrick McHardy5df4c0c2008-11-24 22:01:42 +0800648 u8 key[HIFN_MAX_CRYPT_KEY_LENGTH];
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800649 struct hifn_device *dev;
Patrick McHardy5df4c0c2008-11-24 22:01:42 +0800650 unsigned int keysize;
651};
652
653struct hifn_request_context
654{
655 u8 *iv;
656 unsigned int ivsize;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800657 u8 op, type, mode, unused;
David S. Miller33853292010-05-19 13:56:37 +1000658 struct hifn_cipher_walk walk;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800659};
660
Alexey Dobriyanb966b542008-01-08 21:36:34 +1100661#define crypto_alg_to_hifn(a) container_of(a, struct hifn_crypto_alg, alg)
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800662
663static inline u32 hifn_read_0(struct hifn_device *dev, u32 reg)
664{
665 u32 ret;
666
Al Viroe68970c2008-03-29 03:09:58 +0000667 ret = readl(dev->bar[0] + reg);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800668
669 return ret;
670}
671
672static inline u32 hifn_read_1(struct hifn_device *dev, u32 reg)
673{
674 u32 ret;
675
Al Viroe68970c2008-03-29 03:09:58 +0000676 ret = readl(dev->bar[1] + reg);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800677
678 return ret;
679}
680
681static inline void hifn_write_0(struct hifn_device *dev, u32 reg, u32 val)
682{
Patrick McHardy3c42cbc2008-05-07 22:28:27 +0800683 writel((__force u32)cpu_to_le32(val), dev->bar[0] + reg);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800684}
685
686static inline void hifn_write_1(struct hifn_device *dev, u32 reg, u32 val)
687{
Patrick McHardy3c42cbc2008-05-07 22:28:27 +0800688 writel((__force u32)cpu_to_le32(val), dev->bar[1] + reg);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800689}
690
691static void hifn_wait_puc(struct hifn_device *dev)
692{
693 int i;
694 u32 ret;
695
696 for (i=10000; i > 0; --i) {
697 ret = hifn_read_0(dev, HIFN_0_PUCTRL);
698 if (!(ret & HIFN_PUCTRL_RESET))
699 break;
700
701 udelay(1);
702 }
703
704 if (!i)
705 dprintk("%s: Failed to reset PUC unit.\n", dev->name);
706}
707
708static void hifn_reset_puc(struct hifn_device *dev)
709{
710 hifn_write_0(dev, HIFN_0_PUCTRL, HIFN_PUCTRL_DMAENA);
711 hifn_wait_puc(dev);
712}
713
714static void hifn_stop_device(struct hifn_device *dev)
715{
716 hifn_write_1(dev, HIFN_1_DMA_CSR,
717 HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS |
718 HIFN_DMACSR_S_CTRL_DIS | HIFN_DMACSR_C_CTRL_DIS);
719 hifn_write_0(dev, HIFN_0_PUIER, 0);
720 hifn_write_1(dev, HIFN_1_DMA_IER, 0);
721}
722
723static void hifn_reset_dma(struct hifn_device *dev, int full)
724{
725 hifn_stop_device(dev);
726
727 /*
728 * Setting poll frequency and others to 0.
729 */
730 hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
731 HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
732 mdelay(1);
733
734 /*
735 * Reset DMA.
736 */
737 if (full) {
738 hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MODE);
739 mdelay(1);
740 } else {
741 hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MODE |
742 HIFN_DMACNFG_MSTRESET);
743 hifn_reset_puc(dev);
744 }
745
746 hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
747 HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
748
749 hifn_reset_puc(dev);
750}
751
752static u32 hifn_next_signature(u_int32_t a, u_int cnt)
753{
754 int i;
755 u32 v;
756
757 for (i = 0; i < cnt; i++) {
758
759 /* get the parity */
760 v = a & 0x80080125;
761 v ^= v >> 16;
762 v ^= v >> 8;
763 v ^= v >> 4;
764 v ^= v >> 2;
765 v ^= v >> 1;
766
767 a = (v & 1) ^ (a << 1);
768 }
769
770 return a;
771}
772
773static struct pci2id {
774 u_short pci_vendor;
775 u_short pci_prod;
776 char card_id[13];
777} pci2id[] = {
778 {
779 PCI_VENDOR_ID_HIFN,
780 PCI_DEVICE_ID_HIFN_7955,
781 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
782 0x00, 0x00, 0x00, 0x00, 0x00 }
783 },
784 {
785 PCI_VENDOR_ID_HIFN,
786 PCI_DEVICE_ID_HIFN_7956,
787 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
788 0x00, 0x00, 0x00, 0x00, 0x00 }
789 }
790};
791
Patrick McHardyf881d822008-02-15 19:15:05 +0800792#ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
Patrick McHardyfcd06752007-11-21 12:51:52 +0800793static int hifn_rng_data_present(struct hwrng *rng, int wait)
794{
795 struct hifn_device *dev = (struct hifn_device *)rng->priv;
796 s64 nsec;
797
798 nsec = ktime_to_ns(ktime_sub(ktime_get(), dev->rngtime));
799 nsec -= dev->rng_wait_time;
800 if (nsec <= 0)
801 return 1;
802 if (!wait)
803 return 0;
804 ndelay(nsec);
805 return 1;
806}
807
808static int hifn_rng_data_read(struct hwrng *rng, u32 *data)
809{
810 struct hifn_device *dev = (struct hifn_device *)rng->priv;
811
812 *data = hifn_read_1(dev, HIFN_1_RNG_DATA);
813 dev->rngtime = ktime_get();
814 return 4;
815}
816
817static int hifn_register_rng(struct hifn_device *dev)
818{
819 /*
820 * We must wait at least 256 Pk_clk cycles between two reads of the rng.
821 */
Jussi Kivilinna76f16f82012-07-30 15:42:36 +0800822 dev->rng_wait_time = DIV_ROUND_UP_ULL(NSEC_PER_SEC,
823 dev->pk_clk_freq) * 256;
Patrick McHardyfcd06752007-11-21 12:51:52 +0800824
825 dev->rng.name = dev->name;
826 dev->rng.data_present = hifn_rng_data_present,
827 dev->rng.data_read = hifn_rng_data_read,
828 dev->rng.priv = (unsigned long)dev;
829
830 return hwrng_register(&dev->rng);
831}
832
833static void hifn_unregister_rng(struct hifn_device *dev)
834{
835 hwrng_unregister(&dev->rng);
836}
837#else
838#define hifn_register_rng(dev) 0
839#define hifn_unregister_rng(dev)
840#endif
841
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800842static int hifn_init_pubrng(struct hifn_device *dev)
843{
844 int i;
845
846 hifn_write_1(dev, HIFN_1_PUB_RESET, hifn_read_1(dev, HIFN_1_PUB_RESET) |
847 HIFN_PUBRST_RESET);
848
849 for (i=100; i > 0; --i) {
850 mdelay(1);
851
852 if ((hifn_read_1(dev, HIFN_1_PUB_RESET) & HIFN_PUBRST_RESET) == 0)
853 break;
854 }
855
856 if (!i)
857 dprintk("Chip %s: Failed to initialise public key engine.\n",
858 dev->name);
859 else {
860 hifn_write_1(dev, HIFN_1_PUB_IEN, HIFN_PUBIEN_DONE);
861 dev->dmareg |= HIFN_DMAIER_PUBDONE;
862 hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
863
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200864 dprintk("Chip %s: Public key engine has been successfully "
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800865 "initialised.\n", dev->name);
866 }
867
868 /*
869 * Enable RNG engine.
870 */
871
872 hifn_write_1(dev, HIFN_1_RNG_CONFIG,
873 hifn_read_1(dev, HIFN_1_RNG_CONFIG) | HIFN_RNGCFG_ENA);
874 dprintk("Chip %s: RNG engine has been successfully initialised.\n",
875 dev->name);
876
Patrick McHardyf881d822008-02-15 19:15:05 +0800877#ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
Patrick McHardyfcd06752007-11-21 12:51:52 +0800878 /* First value must be discarded */
879 hifn_read_1(dev, HIFN_1_RNG_DATA);
880 dev->rngtime = ktime_get();
881#endif
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800882 return 0;
883}
884
885static int hifn_enable_crypto(struct hifn_device *dev)
886{
887 u32 dmacfg, addr;
888 char *offtbl = NULL;
889 int i;
890
Robert P. J. Day0936a942008-05-26 21:21:07 +1000891 for (i = 0; i < ARRAY_SIZE(pci2id); i++) {
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +0800892 if (pci2id[i].pci_vendor == dev->pdev->vendor &&
893 pci2id[i].pci_prod == dev->pdev->device) {
894 offtbl = pci2id[i].card_id;
895 break;
896 }
897 }
898
899 if (offtbl == NULL) {
900 dprintk("Chip %s: Unknown card!\n", dev->name);
901 return -ENODEV;
902 }
903
904 dmacfg = hifn_read_1(dev, HIFN_1_DMA_CNFG);
905
906 hifn_write_1(dev, HIFN_1_DMA_CNFG,
907 HIFN_DMACNFG_UNLOCK | HIFN_DMACNFG_MSTRESET |
908 HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
909 mdelay(1);
910 addr = hifn_read_1(dev, HIFN_1_UNLOCK_SECRET1);
911 mdelay(1);
912 hifn_write_1(dev, HIFN_1_UNLOCK_SECRET2, 0);
913 mdelay(1);
914
915 for (i=0; i<12; ++i) {
916 addr = hifn_next_signature(addr, offtbl[i] + 0x101);
917 hifn_write_1(dev, HIFN_1_UNLOCK_SECRET2, addr);
918
919 mdelay(1);
920 }
921 hifn_write_1(dev, HIFN_1_DMA_CNFG, dmacfg);
922
923 dprintk("Chip %s: %s.\n", dev->name, pci_name(dev->pdev));
924
925 return 0;
926}
927
928static void hifn_init_dma(struct hifn_device *dev)
929{
930 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
931 u32 dptr = dev->desc_dma;
932 int i;
933
934 for (i=0; i<HIFN_D_CMD_RSIZE; ++i)
935 dma->cmdr[i].p = __cpu_to_le32(dptr +
936 offsetof(struct hifn_dma, command_bufs[i][0]));
937 for (i=0; i<HIFN_D_RES_RSIZE; ++i)
938 dma->resr[i].p = __cpu_to_le32(dptr +
939 offsetof(struct hifn_dma, result_bufs[i][0]));
940
941 /*
942 * Setup LAST descriptors.
943 */
944 dma->cmdr[HIFN_D_CMD_RSIZE].p = __cpu_to_le32(dptr +
945 offsetof(struct hifn_dma, cmdr[0]));
946 dma->srcr[HIFN_D_SRC_RSIZE].p = __cpu_to_le32(dptr +
947 offsetof(struct hifn_dma, srcr[0]));
948 dma->dstr[HIFN_D_DST_RSIZE].p = __cpu_to_le32(dptr +
949 offsetof(struct hifn_dma, dstr[0]));
950 dma->resr[HIFN_D_RES_RSIZE].p = __cpu_to_le32(dptr +
951 offsetof(struct hifn_dma, resr[0]));
952
953 dma->cmdu = dma->srcu = dma->dstu = dma->resu = 0;
954 dma->cmdi = dma->srci = dma->dsti = dma->resi = 0;
955 dma->cmdk = dma->srck = dma->dstk = dma->resk = 0;
956}
957
Patrick McHardy37a80232007-11-21 12:47:13 +0800958/*
959 * Initialize the PLL. We need to know the frequency of the reference clock
960 * to calculate the optimal multiplier. For PCI we assume 66MHz, since that
961 * allows us to operate without the risk of overclocking the chip. If it
962 * actually uses 33MHz, the chip will operate at half the speed, this can be
963 * overriden by specifying the frequency as module parameter (pci33).
964 *
965 * Unfortunately the PCI clock is not very suitable since the HIFN needs a
966 * stable clock and the PCI clock frequency may vary, so the default is the
967 * external clock. There is no way to find out its frequency, we default to
968 * 66MHz since according to Mike Ham of HiFn, almost every board in existence
969 * has an external crystal populated at 66MHz.
970 */
971static void hifn_init_pll(struct hifn_device *dev)
972{
973 unsigned int freq, m;
974 u32 pllcfg;
975
976 pllcfg = HIFN_1_PLL | HIFN_PLL_RESERVED_1;
977
978 if (strncmp(hifn_pll_ref, "ext", 3) == 0)
979 pllcfg |= HIFN_PLL_REF_CLK_PLL;
980 else
981 pllcfg |= HIFN_PLL_REF_CLK_HBI;
982
983 if (hifn_pll_ref[3] != '\0')
984 freq = simple_strtoul(hifn_pll_ref + 3, NULL, 10);
985 else {
986 freq = 66;
987 printk(KERN_INFO "hifn795x: assuming %uMHz clock speed, "
988 "override with hifn_pll_ref=%.3s<frequency>\n",
989 freq, hifn_pll_ref);
990 }
991
992 m = HIFN_PLL_FCK_MAX / freq;
993
994 pllcfg |= (m / 2 - 1) << HIFN_PLL_ND_SHIFT;
995 if (m <= 8)
996 pllcfg |= HIFN_PLL_IS_1_8;
997 else
998 pllcfg |= HIFN_PLL_IS_9_12;
999
1000 /* Select clock source and enable clock bypass */
1001 hifn_write_1(dev, HIFN_1_PLL, pllcfg |
1002 HIFN_PLL_PK_CLK_HBI | HIFN_PLL_PE_CLK_HBI | HIFN_PLL_BP);
1003
1004 /* Let the chip lock to the input clock */
1005 mdelay(10);
1006
1007 /* Disable clock bypass */
1008 hifn_write_1(dev, HIFN_1_PLL, pllcfg |
1009 HIFN_PLL_PK_CLK_HBI | HIFN_PLL_PE_CLK_HBI);
1010
1011 /* Switch the engines to the PLL */
1012 hifn_write_1(dev, HIFN_1_PLL, pllcfg |
1013 HIFN_PLL_PK_CLK_PLL | HIFN_PLL_PE_CLK_PLL);
Patrick McHardyfcd06752007-11-21 12:51:52 +08001014
1015 /*
1016 * The Fpk_clk runs at half the total speed. Its frequency is needed to
1017 * calculate the minimum time between two reads of the rng. Since 33MHz
1018 * is actually 33.333... we overestimate the frequency here, resulting
1019 * in slightly larger intervals.
1020 */
1021 dev->pk_clk_freq = 1000000 * (freq + 1) * m / 2;
Patrick McHardy37a80232007-11-21 12:47:13 +08001022}
1023
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001024static void hifn_init_registers(struct hifn_device *dev)
1025{
1026 u32 dptr = dev->desc_dma;
1027
1028 /* Initialization magic... */
1029 hifn_write_0(dev, HIFN_0_PUCTRL, HIFN_PUCTRL_DMAENA);
1030 hifn_write_0(dev, HIFN_0_FIFOCNFG, HIFN_FIFOCNFG_THRESHOLD);
1031 hifn_write_0(dev, HIFN_0_PUIER, HIFN_PUIER_DSTOVER);
1032
1033 /* write all 4 ring address registers */
Patrick McHardy3c42cbc2008-05-07 22:28:27 +08001034 hifn_write_1(dev, HIFN_1_DMA_CRAR, dptr +
1035 offsetof(struct hifn_dma, cmdr[0]));
1036 hifn_write_1(dev, HIFN_1_DMA_SRAR, dptr +
1037 offsetof(struct hifn_dma, srcr[0]));
1038 hifn_write_1(dev, HIFN_1_DMA_DRAR, dptr +
1039 offsetof(struct hifn_dma, dstr[0]));
1040 hifn_write_1(dev, HIFN_1_DMA_RRAR, dptr +
1041 offsetof(struct hifn_dma, resr[0]));
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001042
1043 mdelay(2);
1044#if 0
1045 hifn_write_1(dev, HIFN_1_DMA_CSR,
1046 HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS |
1047 HIFN_DMACSR_S_CTRL_DIS | HIFN_DMACSR_C_CTRL_DIS |
1048 HIFN_DMACSR_D_ABORT | HIFN_DMACSR_D_DONE | HIFN_DMACSR_D_LAST |
1049 HIFN_DMACSR_D_WAIT | HIFN_DMACSR_D_OVER |
1050 HIFN_DMACSR_R_ABORT | HIFN_DMACSR_R_DONE | HIFN_DMACSR_R_LAST |
1051 HIFN_DMACSR_R_WAIT | HIFN_DMACSR_R_OVER |
1052 HIFN_DMACSR_S_ABORT | HIFN_DMACSR_S_DONE | HIFN_DMACSR_S_LAST |
1053 HIFN_DMACSR_S_WAIT |
1054 HIFN_DMACSR_C_ABORT | HIFN_DMACSR_C_DONE | HIFN_DMACSR_C_LAST |
1055 HIFN_DMACSR_C_WAIT |
1056 HIFN_DMACSR_ENGINE |
1057 HIFN_DMACSR_PUBDONE);
1058#else
1059 hifn_write_1(dev, HIFN_1_DMA_CSR,
1060 HIFN_DMACSR_C_CTRL_ENA | HIFN_DMACSR_S_CTRL_ENA |
1061 HIFN_DMACSR_D_CTRL_ENA | HIFN_DMACSR_R_CTRL_ENA |
1062 HIFN_DMACSR_D_ABORT | HIFN_DMACSR_D_DONE | HIFN_DMACSR_D_LAST |
1063 HIFN_DMACSR_D_WAIT | HIFN_DMACSR_D_OVER |
1064 HIFN_DMACSR_R_ABORT | HIFN_DMACSR_R_DONE | HIFN_DMACSR_R_LAST |
1065 HIFN_DMACSR_R_WAIT | HIFN_DMACSR_R_OVER |
1066 HIFN_DMACSR_S_ABORT | HIFN_DMACSR_S_DONE | HIFN_DMACSR_S_LAST |
1067 HIFN_DMACSR_S_WAIT |
1068 HIFN_DMACSR_C_ABORT | HIFN_DMACSR_C_DONE | HIFN_DMACSR_C_LAST |
1069 HIFN_DMACSR_C_WAIT |
1070 HIFN_DMACSR_ENGINE |
1071 HIFN_DMACSR_PUBDONE);
1072#endif
1073 hifn_read_1(dev, HIFN_1_DMA_CSR);
1074
1075 dev->dmareg |= HIFN_DMAIER_R_DONE | HIFN_DMAIER_C_ABORT |
1076 HIFN_DMAIER_D_OVER | HIFN_DMAIER_R_OVER |
1077 HIFN_DMAIER_S_ABORT | HIFN_DMAIER_D_ABORT | HIFN_DMAIER_R_ABORT |
1078 HIFN_DMAIER_ENGINE;
1079 dev->dmareg &= ~HIFN_DMAIER_C_WAIT;
1080
1081 hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
1082 hifn_read_1(dev, HIFN_1_DMA_IER);
1083#if 0
1084 hifn_write_0(dev, HIFN_0_PUCNFG, HIFN_PUCNFG_ENCCNFG |
1085 HIFN_PUCNFG_DRFR_128 | HIFN_PUCNFG_TCALLPHASES |
1086 HIFN_PUCNFG_TCDRVTOTEM | HIFN_PUCNFG_BUS32 |
1087 HIFN_PUCNFG_DRAM);
1088#else
1089 hifn_write_0(dev, HIFN_0_PUCNFG, 0x10342);
1090#endif
Patrick McHardy37a80232007-11-21 12:47:13 +08001091 hifn_init_pll(dev);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001092
1093 hifn_write_0(dev, HIFN_0_PUISR, HIFN_PUISR_DSTOVER);
1094 hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
1095 HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE | HIFN_DMACNFG_LAST |
1096 ((HIFN_POLL_FREQUENCY << 16 ) & HIFN_DMACNFG_POLLFREQ) |
1097 ((HIFN_POLL_SCALAR << 8) & HIFN_DMACNFG_POLLINVAL));
1098}
1099
1100static int hifn_setup_base_command(struct hifn_device *dev, u8 *buf,
1101 unsigned dlen, unsigned slen, u16 mask, u8 snum)
1102{
1103 struct hifn_base_command *base_cmd;
1104 u8 *buf_pos = buf;
1105
1106 base_cmd = (struct hifn_base_command *)buf_pos;
1107 base_cmd->masks = __cpu_to_le16(mask);
1108 base_cmd->total_source_count =
1109 __cpu_to_le16(slen & HIFN_BASE_CMD_LENMASK_LO);
1110 base_cmd->total_dest_count =
1111 __cpu_to_le16(dlen & HIFN_BASE_CMD_LENMASK_LO);
1112
1113 dlen >>= 16;
1114 slen >>= 16;
1115 base_cmd->session_num = __cpu_to_le16(snum |
1116 ((slen << HIFN_BASE_CMD_SRCLEN_S) & HIFN_BASE_CMD_SRCLEN_M) |
1117 ((dlen << HIFN_BASE_CMD_DSTLEN_S) & HIFN_BASE_CMD_DSTLEN_M));
1118
1119 return sizeof(struct hifn_base_command);
1120}
1121
1122static int hifn_setup_crypto_command(struct hifn_device *dev,
1123 u8 *buf, unsigned dlen, unsigned slen,
1124 u8 *key, int keylen, u8 *iv, int ivsize, u16 mode)
1125{
1126 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1127 struct hifn_crypt_command *cry_cmd;
1128 u8 *buf_pos = buf;
1129 u16 cmd_len;
1130
1131 cry_cmd = (struct hifn_crypt_command *)buf_pos;
1132
1133 cry_cmd->source_count = __cpu_to_le16(dlen & 0xffff);
1134 dlen >>= 16;
1135 cry_cmd->masks = __cpu_to_le16(mode |
1136 ((dlen << HIFN_CRYPT_CMD_SRCLEN_S) &
1137 HIFN_CRYPT_CMD_SRCLEN_M));
1138 cry_cmd->header_skip = 0;
1139 cry_cmd->reserved = 0;
1140
1141 buf_pos += sizeof(struct hifn_crypt_command);
1142
1143 dma->cmdu++;
1144 if (dma->cmdu > 1) {
1145 dev->dmareg |= HIFN_DMAIER_C_WAIT;
1146 hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
1147 }
1148
1149 if (keylen) {
1150 memcpy(buf_pos, key, keylen);
1151 buf_pos += keylen;
1152 }
1153 if (ivsize) {
1154 memcpy(buf_pos, iv, ivsize);
1155 buf_pos += ivsize;
1156 }
1157
1158 cmd_len = buf_pos - buf;
1159
1160 return cmd_len;
1161}
1162
Patrick McHardy85e7e602008-05-07 22:36:54 +08001163static int hifn_setup_cmd_desc(struct hifn_device *dev,
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001164 struct hifn_context *ctx, struct hifn_request_context *rctx,
1165 void *priv, unsigned int nbytes)
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001166{
1167 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1168 int cmd_len, sa_idx;
1169 u8 *buf, *buf_pos;
1170 u16 mask;
1171
Patrick McHardy85e7e602008-05-07 22:36:54 +08001172 sa_idx = dma->cmdi;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001173 buf_pos = buf = dma->command_bufs[dma->cmdi];
1174
1175 mask = 0;
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001176 switch (rctx->op) {
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001177 case ACRYPTO_OP_DECRYPT:
1178 mask = HIFN_BASE_CMD_CRYPT | HIFN_BASE_CMD_DECODE;
1179 break;
1180 case ACRYPTO_OP_ENCRYPT:
1181 mask = HIFN_BASE_CMD_CRYPT;
1182 break;
1183 case ACRYPTO_OP_HMAC:
1184 mask = HIFN_BASE_CMD_MAC;
1185 break;
1186 default:
1187 goto err_out;
1188 }
1189
1190 buf_pos += hifn_setup_base_command(dev, buf_pos, nbytes,
1191 nbytes, mask, dev->snum);
1192
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001193 if (rctx->op == ACRYPTO_OP_ENCRYPT || rctx->op == ACRYPTO_OP_DECRYPT) {
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001194 u16 md = 0;
1195
1196 if (ctx->keysize)
1197 md |= HIFN_CRYPT_CMD_NEW_KEY;
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001198 if (rctx->iv && rctx->mode != ACRYPTO_MODE_ECB)
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001199 md |= HIFN_CRYPT_CMD_NEW_IV;
1200
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001201 switch (rctx->mode) {
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001202 case ACRYPTO_MODE_ECB:
1203 md |= HIFN_CRYPT_CMD_MODE_ECB;
1204 break;
1205 case ACRYPTO_MODE_CBC:
1206 md |= HIFN_CRYPT_CMD_MODE_CBC;
1207 break;
1208 case ACRYPTO_MODE_CFB:
1209 md |= HIFN_CRYPT_CMD_MODE_CFB;
1210 break;
1211 case ACRYPTO_MODE_OFB:
1212 md |= HIFN_CRYPT_CMD_MODE_OFB;
1213 break;
1214 default:
1215 goto err_out;
1216 }
1217
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001218 switch (rctx->type) {
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001219 case ACRYPTO_TYPE_AES_128:
1220 if (ctx->keysize != 16)
1221 goto err_out;
1222 md |= HIFN_CRYPT_CMD_KSZ_128 |
1223 HIFN_CRYPT_CMD_ALG_AES;
1224 break;
1225 case ACRYPTO_TYPE_AES_192:
1226 if (ctx->keysize != 24)
1227 goto err_out;
1228 md |= HIFN_CRYPT_CMD_KSZ_192 |
1229 HIFN_CRYPT_CMD_ALG_AES;
1230 break;
1231 case ACRYPTO_TYPE_AES_256:
1232 if (ctx->keysize != 32)
1233 goto err_out;
1234 md |= HIFN_CRYPT_CMD_KSZ_256 |
1235 HIFN_CRYPT_CMD_ALG_AES;
1236 break;
1237 case ACRYPTO_TYPE_3DES:
1238 if (ctx->keysize != 24)
1239 goto err_out;
1240 md |= HIFN_CRYPT_CMD_ALG_3DES;
1241 break;
1242 case ACRYPTO_TYPE_DES:
1243 if (ctx->keysize != 8)
1244 goto err_out;
1245 md |= HIFN_CRYPT_CMD_ALG_DES;
1246 break;
1247 default:
1248 goto err_out;
1249 }
1250
1251 buf_pos += hifn_setup_crypto_command(dev, buf_pos,
1252 nbytes, nbytes, ctx->key, ctx->keysize,
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001253 rctx->iv, rctx->ivsize, md);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001254 }
1255
1256 dev->sa[sa_idx] = priv;
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001257 dev->started++;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001258
1259 cmd_len = buf_pos - buf;
1260 dma->cmdr[dma->cmdi].l = __cpu_to_le32(cmd_len | HIFN_D_VALID |
1261 HIFN_D_LAST | HIFN_D_MASKDONEIRQ);
1262
1263 if (++dma->cmdi == HIFN_D_CMD_RSIZE) {
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001264 dma->cmdr[dma->cmdi].l = __cpu_to_le32(
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001265 HIFN_D_VALID | HIFN_D_LAST |
1266 HIFN_D_MASKDONEIRQ | HIFN_D_JUMP);
1267 dma->cmdi = 0;
1268 } else
1269 dma->cmdr[dma->cmdi-1].l |= __cpu_to_le32(HIFN_D_VALID);
1270
1271 if (!(dev->flags & HIFN_FLAG_CMD_BUSY)) {
1272 hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_C_CTRL_ENA);
1273 dev->flags |= HIFN_FLAG_CMD_BUSY;
1274 }
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001275 return 0;
1276
1277err_out:
1278 return -EINVAL;
1279}
1280
Patrick McHardy85e7e602008-05-07 22:36:54 +08001281static int hifn_setup_src_desc(struct hifn_device *dev, struct page *page,
Patrick McHardy75741a02008-11-24 21:59:25 +08001282 unsigned int offset, unsigned int size, int last)
Patrick McHardy85e7e602008-05-07 22:36:54 +08001283{
1284 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1285 int idx;
1286 dma_addr_t addr;
1287
1288 addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_TODEVICE);
1289
1290 idx = dma->srci;
1291
1292 dma->srcr[idx].p = __cpu_to_le32(addr);
1293 dma->srcr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
Patrick McHardy75741a02008-11-24 21:59:25 +08001294 HIFN_D_MASKDONEIRQ | (last ? HIFN_D_LAST : 0));
Patrick McHardy85e7e602008-05-07 22:36:54 +08001295
1296 if (++idx == HIFN_D_SRC_RSIZE) {
1297 dma->srcr[idx].l = __cpu_to_le32(HIFN_D_VALID |
Patrick McHardy75741a02008-11-24 21:59:25 +08001298 HIFN_D_JUMP | HIFN_D_MASKDONEIRQ |
1299 (last ? HIFN_D_LAST : 0));
Patrick McHardy85e7e602008-05-07 22:36:54 +08001300 idx = 0;
1301 }
1302
1303 dma->srci = idx;
1304 dma->srcu++;
1305
1306 if (!(dev->flags & HIFN_FLAG_SRC_BUSY)) {
1307 hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_S_CTRL_ENA);
1308 dev->flags |= HIFN_FLAG_SRC_BUSY;
1309 }
1310
1311 return size;
1312}
1313
1314static void hifn_setup_res_desc(struct hifn_device *dev)
1315{
1316 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1317
1318 dma->resr[dma->resi].l = __cpu_to_le32(HIFN_USED_RESULT |
1319 HIFN_D_VALID | HIFN_D_LAST);
1320 /*
1321 * dma->resr[dma->resi].l = __cpu_to_le32(HIFN_MAX_RESULT | HIFN_D_VALID |
Patrick McHardy692af5d2008-05-07 22:37:29 +08001322 * HIFN_D_LAST);
Patrick McHardy85e7e602008-05-07 22:36:54 +08001323 */
1324
1325 if (++dma->resi == HIFN_D_RES_RSIZE) {
1326 dma->resr[HIFN_D_RES_RSIZE].l = __cpu_to_le32(HIFN_D_VALID |
1327 HIFN_D_JUMP | HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
1328 dma->resi = 0;
1329 }
1330
1331 dma->resu++;
1332
1333 if (!(dev->flags & HIFN_FLAG_RES_BUSY)) {
1334 hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_R_CTRL_ENA);
1335 dev->flags |= HIFN_FLAG_RES_BUSY;
1336 }
1337}
1338
1339static void hifn_setup_dst_desc(struct hifn_device *dev, struct page *page,
Patrick McHardy75741a02008-11-24 21:59:25 +08001340 unsigned offset, unsigned size, int last)
Patrick McHardy85e7e602008-05-07 22:36:54 +08001341{
1342 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1343 int idx;
1344 dma_addr_t addr;
1345
1346 addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_FROMDEVICE);
1347
1348 idx = dma->dsti;
1349 dma->dstr[idx].p = __cpu_to_le32(addr);
1350 dma->dstr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
Patrick McHardy75741a02008-11-24 21:59:25 +08001351 HIFN_D_MASKDONEIRQ | (last ? HIFN_D_LAST : 0));
Patrick McHardy85e7e602008-05-07 22:36:54 +08001352
1353 if (++idx == HIFN_D_DST_RSIZE) {
1354 dma->dstr[idx].l = __cpu_to_le32(HIFN_D_VALID |
1355 HIFN_D_JUMP | HIFN_D_MASKDONEIRQ |
Patrick McHardy75741a02008-11-24 21:59:25 +08001356 (last ? HIFN_D_LAST : 0));
Patrick McHardy85e7e602008-05-07 22:36:54 +08001357 idx = 0;
1358 }
1359 dma->dsti = idx;
1360 dma->dstu++;
1361
1362 if (!(dev->flags & HIFN_FLAG_DST_BUSY)) {
1363 hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_D_CTRL_ENA);
1364 dev->flags |= HIFN_FLAG_DST_BUSY;
1365 }
1366}
1367
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001368static int hifn_setup_dma(struct hifn_device *dev,
1369 struct hifn_context *ctx, struct hifn_request_context *rctx,
Patrick McHardy75741a02008-11-24 21:59:25 +08001370 struct scatterlist *src, struct scatterlist *dst,
1371 unsigned int nbytes, void *priv)
Patrick McHardy85e7e602008-05-07 22:36:54 +08001372{
Patrick McHardy75741a02008-11-24 21:59:25 +08001373 struct scatterlist *t;
1374 struct page *spage, *dpage;
1375 unsigned int soff, doff;
1376 unsigned int n, len;
1377
Patrick McHardy34161582008-11-24 22:00:49 +08001378 n = nbytes;
1379 while (n) {
1380 spage = sg_page(src);
1381 soff = src->offset;
1382 len = min(src->length, n);
1383
Patrick McHardy34161582008-11-24 22:00:49 +08001384 hifn_setup_src_desc(dev, spage, soff, len, n - len == 0);
1385
1386 src++;
1387 n -= len;
1388 }
1389
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001390 t = &rctx->walk.cache[0];
Patrick McHardy75741a02008-11-24 21:59:25 +08001391 n = nbytes;
1392 while (n) {
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001393 if (t->length && rctx->walk.flags & ASYNC_FLAGS_MISALIGNED) {
Evgeniy Polyakov5f459f02008-11-25 23:17:10 +08001394 BUG_ON(!sg_page(t));
Patrick McHardy34161582008-11-24 22:00:49 +08001395 dpage = sg_page(t);
1396 doff = 0;
Patrick McHardy75741a02008-11-24 21:59:25 +08001397 len = t->length;
1398 } else {
Evgeniy Polyakov5f459f02008-11-25 23:17:10 +08001399 BUG_ON(!sg_page(dst));
Patrick McHardy75741a02008-11-24 21:59:25 +08001400 dpage = sg_page(dst);
1401 doff = dst->offset;
Patrick McHardy75741a02008-11-24 21:59:25 +08001402 len = dst->length;
1403 }
1404 len = min(len, n);
1405
Patrick McHardy75741a02008-11-24 21:59:25 +08001406 hifn_setup_dst_desc(dev, dpage, doff, len, n - len == 0);
1407
Patrick McHardy75741a02008-11-24 21:59:25 +08001408 dst++;
1409 t++;
1410 n -= len;
1411 }
1412
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001413 hifn_setup_cmd_desc(dev, ctx, rctx, priv, nbytes);
Patrick McHardy85e7e602008-05-07 22:36:54 +08001414 hifn_setup_res_desc(dev);
1415 return 0;
1416}
1417
David S. Miller33853292010-05-19 13:56:37 +10001418static int hifn_cipher_walk_init(struct hifn_cipher_walk *w,
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001419 int num, gfp_t gfp_flags)
1420{
1421 int i;
1422
1423 num = min(ASYNC_SCATTERLIST_CACHE, num);
1424 sg_init_table(w->cache, num);
1425
1426 w->num = 0;
1427 for (i=0; i<num; ++i) {
1428 struct page *page = alloc_page(gfp_flags);
1429 struct scatterlist *s;
1430
1431 if (!page)
1432 break;
1433
1434 s = &w->cache[i];
1435
1436 sg_set_page(s, page, PAGE_SIZE, 0);
1437 w->num++;
1438 }
1439
1440 return i;
1441}
1442
David S. Miller33853292010-05-19 13:56:37 +10001443static void hifn_cipher_walk_exit(struct hifn_cipher_walk *w)
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001444{
1445 int i;
1446
1447 for (i=0; i<w->num; ++i) {
1448 struct scatterlist *s = &w->cache[i];
1449
1450 __free_page(sg_page(s));
1451
1452 s->length = 0;
1453 }
1454
1455 w->num = 0;
1456}
1457
Patrick McHardy34161582008-11-24 22:00:49 +08001458static int ablkcipher_add(unsigned int *drestp, struct scatterlist *dst,
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001459 unsigned int size, unsigned int *nbytesp)
1460{
1461 unsigned int copy, drest = *drestp, nbytes = *nbytesp;
1462 int idx = 0;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001463
1464 if (drest < size || size > nbytes)
1465 return -EINVAL;
1466
1467 while (size) {
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -07001468 copy = min3(drest, size, dst->length);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001469
1470 size -= copy;
1471 drest -= copy;
1472 nbytes -= copy;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001473
1474 dprintk("%s: copy: %u, size: %u, drest: %u, nbytes: %u.\n",
1475 __func__, copy, size, drest, nbytes);
1476
Patrick McHardy34161582008-11-24 22:00:49 +08001477 dst++;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001478 idx++;
1479 }
1480
1481 *nbytesp = nbytes;
1482 *drestp = drest;
1483
1484 return idx;
1485}
1486
David S. Miller33853292010-05-19 13:56:37 +10001487static int hifn_cipher_walk(struct ablkcipher_request *req,
1488 struct hifn_cipher_walk *w)
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001489{
Patrick McHardy34161582008-11-24 22:00:49 +08001490 struct scatterlist *dst, *t;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001491 unsigned int nbytes = req->nbytes, offset, copy, diff;
1492 int idx, tidx, err;
1493
1494 tidx = idx = 0;
1495 offset = 0;
1496 while (nbytes) {
1497 if (idx >= w->num && (w->flags & ASYNC_FLAGS_MISALIGNED))
1498 return -EINVAL;
1499
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001500 dst = &req->dst[idx];
1501
Patrick McHardy34161582008-11-24 22:00:49 +08001502 dprintk("\n%s: dlen: %u, doff: %u, offset: %u, nbytes: %u.\n",
1503 __func__, dst->length, dst->offset, offset, nbytes);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001504
Patrick McHardyd0690332008-05-07 22:33:37 +08001505 if (!IS_ALIGNED(dst->offset, HIFN_D_DST_DALIGN) ||
1506 !IS_ALIGNED(dst->length, HIFN_D_DST_DALIGN) ||
1507 offset) {
Patrick McHardy34161582008-11-24 22:00:49 +08001508 unsigned slen = min(dst->length - offset, nbytes);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001509 unsigned dlen = PAGE_SIZE;
1510
1511 t = &w->cache[idx];
1512
Patrick McHardy34161582008-11-24 22:00:49 +08001513 err = ablkcipher_add(&dlen, dst, slen, &nbytes);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001514 if (err < 0)
Patrick McHardy34161582008-11-24 22:00:49 +08001515 return err;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001516
1517 idx += err;
1518
Patrick McHardyd0690332008-05-07 22:33:37 +08001519 copy = slen & ~(HIFN_D_DST_DALIGN - 1);
1520 diff = slen & (HIFN_D_DST_DALIGN - 1);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001521
1522 if (dlen < nbytes) {
1523 /*
1524 * Destination page does not have enough space
1525 * to put there additional blocksized chunk,
1526 * so we mark that page as containing only
1527 * blocksize aligned chunks:
Patrick McHardyd0690332008-05-07 22:33:37 +08001528 * t->length = (slen & ~(HIFN_D_DST_DALIGN - 1));
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001529 * and increase number of bytes to be processed
1530 * in next chunk:
1531 * nbytes += diff;
1532 */
1533 nbytes += diff;
1534
1535 /*
1536 * Temporary of course...
1537 * Kick author if you will catch this one.
1538 */
1539 printk(KERN_ERR "%s: dlen: %u, nbytes: %u,"
1540 "slen: %u, offset: %u.\n",
1541 __func__, dlen, nbytes, slen, offset);
1542 printk(KERN_ERR "%s: please contact author to fix this "
1543 "issue, generally you should not catch "
1544 "this path under any condition but who "
1545 "knows how did you use crypto code.\n"
1546 "Thank you.\n", __func__);
1547 BUG();
1548 } else {
1549 copy += diff + nbytes;
1550
Patrick McHardy34161582008-11-24 22:00:49 +08001551 dst = &req->dst[idx];
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001552
Patrick McHardy34161582008-11-24 22:00:49 +08001553 err = ablkcipher_add(&dlen, dst, nbytes, &nbytes);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001554 if (err < 0)
Patrick McHardy34161582008-11-24 22:00:49 +08001555 return err;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001556
1557 idx += err;
1558 }
1559
1560 t->length = copy;
1561 t->offset = offset;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001562 } else {
Patrick McHardy34161582008-11-24 22:00:49 +08001563 nbytes -= min(dst->length, nbytes);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001564 idx++;
1565 }
1566
1567 tidx++;
1568 }
1569
1570 return tidx;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001571}
1572
1573static int hifn_setup_session(struct ablkcipher_request *req)
1574{
1575 struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001576 struct hifn_request_context *rctx = ablkcipher_request_ctx(req);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001577 struct hifn_device *dev = ctx->dev;
Patrick McHardy75741a02008-11-24 21:59:25 +08001578 unsigned long dlen, flags;
1579 unsigned int nbytes = req->nbytes, idx = 0;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001580 int err = -EINVAL, sg_num;
Patrick McHardy75741a02008-11-24 21:59:25 +08001581 struct scatterlist *dst;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001582
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001583 if (rctx->iv && !rctx->ivsize && rctx->mode != ACRYPTO_MODE_ECB)
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001584 goto err_out_exit;
1585
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001586 rctx->walk.flags = 0;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001587
1588 while (nbytes) {
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001589 dst = &req->dst[idx];
Patrick McHardy136f7022008-05-07 22:34:27 +08001590 dlen = min(dst->length, nbytes);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001591
Patrick McHardyd0690332008-05-07 22:33:37 +08001592 if (!IS_ALIGNED(dst->offset, HIFN_D_DST_DALIGN) ||
Patrick McHardy136f7022008-05-07 22:34:27 +08001593 !IS_ALIGNED(dlen, HIFN_D_DST_DALIGN))
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001594 rctx->walk.flags |= ASYNC_FLAGS_MISALIGNED;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001595
Patrick McHardy136f7022008-05-07 22:34:27 +08001596 nbytes -= dlen;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001597 idx++;
1598 }
1599
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001600 if (rctx->walk.flags & ASYNC_FLAGS_MISALIGNED) {
David S. Miller33853292010-05-19 13:56:37 +10001601 err = hifn_cipher_walk_init(&rctx->walk, idx, GFP_ATOMIC);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001602 if (err < 0)
1603 return err;
1604 }
1605
David S. Miller33853292010-05-19 13:56:37 +10001606 sg_num = hifn_cipher_walk(req, &rctx->walk);
Patrick McHardy94eaa1b2008-05-07 22:32:28 +08001607 if (sg_num < 0) {
1608 err = sg_num;
1609 goto err_out_exit;
1610 }
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001611
1612 spin_lock_irqsave(&dev->lock, flags);
1613 if (dev->started + sg_num > HIFN_QUEUE_LENGTH) {
1614 err = -EAGAIN;
1615 goto err_out;
1616 }
1617
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001618 err = hifn_setup_dma(dev, ctx, rctx, req->src, req->dst, req->nbytes, req);
Patrick McHardy75741a02008-11-24 21:59:25 +08001619 if (err)
1620 goto err_out;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001621
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001622 dev->snum++;
1623
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001624 dev->active = HIFN_DEFAULT_ACTIVE_NUM;
1625 spin_unlock_irqrestore(&dev->lock, flags);
1626
1627 return 0;
1628
1629err_out:
1630 spin_unlock_irqrestore(&dev->lock, flags);
1631err_out_exit:
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001632 if (err) {
1633 printk("%s: iv: %p [%d], key: %p [%d], mode: %u, op: %u, "
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001634 "type: %u, err: %d.\n",
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001635 dev->name, rctx->iv, rctx->ivsize,
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001636 ctx->key, ctx->keysize,
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001637 rctx->mode, rctx->op, rctx->type, err);
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001638 }
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001639
1640 return err;
1641}
1642
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001643static int hifn_start_device(struct hifn_device *dev)
1644{
1645 int err;
1646
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001647 dev->started = dev->active = 0;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001648 hifn_reset_dma(dev, 1);
1649
1650 err = hifn_enable_crypto(dev);
1651 if (err)
1652 return err;
1653
1654 hifn_reset_puc(dev);
1655
1656 hifn_init_dma(dev);
1657
1658 hifn_init_registers(dev);
1659
1660 hifn_init_pubrng(dev);
1661
1662 return 0;
1663}
1664
1665static int ablkcipher_get(void *saddr, unsigned int *srestp, unsigned int offset,
1666 struct scatterlist *dst, unsigned int size, unsigned int *nbytesp)
1667{
1668 unsigned int srest = *srestp, nbytes = *nbytesp, copy;
1669 void *daddr;
1670 int idx = 0;
1671
1672 if (srest < size || size > nbytes)
1673 return -EINVAL;
1674
1675 while (size) {
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -07001676 copy = min3(srest, dst->length, size);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001677
Cong Wang237f2592011-11-25 23:14:19 +08001678 daddr = kmap_atomic(sg_page(dst));
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001679 memcpy(daddr + dst->offset + offset, saddr, copy);
Cong Wang237f2592011-11-25 23:14:19 +08001680 kunmap_atomic(daddr);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001681
1682 nbytes -= copy;
1683 size -= copy;
1684 srest -= copy;
1685 saddr += copy;
1686 offset = 0;
1687
1688 dprintk("%s: copy: %u, size: %u, srest: %u, nbytes: %u.\n",
1689 __func__, copy, size, srest, nbytes);
1690
1691 dst++;
1692 idx++;
1693 }
1694
1695 *nbytesp = nbytes;
1696 *srestp = srest;
1697
1698 return idx;
1699}
1700
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001701static inline void hifn_complete_sa(struct hifn_device *dev, int i)
1702{
1703 unsigned long flags;
1704
1705 spin_lock_irqsave(&dev->lock, flags);
1706 dev->sa[i] = NULL;
1707 dev->started--;
1708 if (dev->started < 0)
1709 printk("%s: started: %d.\n", __func__, dev->started);
1710 spin_unlock_irqrestore(&dev->lock, flags);
1711 BUG_ON(dev->started < 0);
1712}
1713
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001714static void hifn_process_ready(struct ablkcipher_request *req, int error)
1715{
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001716 struct hifn_request_context *rctx = ablkcipher_request_ctx(req);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001717
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001718 if (rctx->walk.flags & ASYNC_FLAGS_MISALIGNED) {
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001719 unsigned int nbytes = req->nbytes;
1720 int idx = 0, err;
1721 struct scatterlist *dst, *t;
1722 void *saddr;
1723
Patrick McHardy75741a02008-11-24 21:59:25 +08001724 while (nbytes) {
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08001725 t = &rctx->walk.cache[idx];
Patrick McHardy75741a02008-11-24 21:59:25 +08001726 dst = &req->dst[idx];
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001727
Patrick McHardy75741a02008-11-24 21:59:25 +08001728 dprintk("\n%s: sg_page(t): %p, t->length: %u, "
1729 "sg_page(dst): %p, dst->length: %u, "
1730 "nbytes: %u.\n",
1731 __func__, sg_page(t), t->length,
1732 sg_page(dst), dst->length, nbytes);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001733
Patrick McHardy75741a02008-11-24 21:59:25 +08001734 if (!t->length) {
1735 nbytes -= min(dst->length, nbytes);
1736 idx++;
1737 continue;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001738 }
1739
Cong Wang237f2592011-11-25 23:14:19 +08001740 saddr = kmap_atomic(sg_page(t));
Patrick McHardy75741a02008-11-24 21:59:25 +08001741
1742 err = ablkcipher_get(saddr, &t->length, t->offset,
1743 dst, nbytes, &nbytes);
1744 if (err < 0) {
Cong Wang237f2592011-11-25 23:14:19 +08001745 kunmap_atomic(saddr);
Patrick McHardy75741a02008-11-24 21:59:25 +08001746 break;
1747 }
1748
1749 idx += err;
Cong Wang237f2592011-11-25 23:14:19 +08001750 kunmap_atomic(saddr);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001751 }
1752
David S. Miller33853292010-05-19 13:56:37 +10001753 hifn_cipher_walk_exit(&rctx->walk);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001754 }
Patrick McHardy75741a02008-11-24 21:59:25 +08001755
1756 req->base.complete(&req->base, error);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001757}
1758
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001759static void hifn_clear_rings(struct hifn_device *dev, int error)
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001760{
1761 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1762 int i, u;
1763
1764 dprintk("%s: ring cleanup 1: i: %d.%d.%d.%d, u: %d.%d.%d.%d, "
1765 "k: %d.%d.%d.%d.\n",
1766 dev->name,
1767 dma->cmdi, dma->srci, dma->dsti, dma->resi,
1768 dma->cmdu, dma->srcu, dma->dstu, dma->resu,
1769 dma->cmdk, dma->srck, dma->dstk, dma->resk);
1770
1771 i = dma->resk; u = dma->resu;
1772 while (u != 0) {
1773 if (dma->resr[i].l & __cpu_to_le32(HIFN_D_VALID))
1774 break;
1775
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001776 if (dev->sa[i]) {
1777 dev->success++;
1778 dev->reset = 0;
1779 hifn_process_ready(dev->sa[i], error);
1780 hifn_complete_sa(dev, i);
1781 }
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001782
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001783 if (++i == HIFN_D_RES_RSIZE)
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001784 i = 0;
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001785 u--;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001786 }
1787 dma->resk = i; dma->resu = u;
1788
1789 i = dma->srck; u = dma->srcu;
1790 while (u != 0) {
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001791 if (dma->srcr[i].l & __cpu_to_le32(HIFN_D_VALID))
1792 break;
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001793 if (++i == HIFN_D_SRC_RSIZE)
1794 i = 0;
1795 u--;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001796 }
1797 dma->srck = i; dma->srcu = u;
1798
1799 i = dma->cmdk; u = dma->cmdu;
1800 while (u != 0) {
1801 if (dma->cmdr[i].l & __cpu_to_le32(HIFN_D_VALID))
1802 break;
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001803 if (++i == HIFN_D_CMD_RSIZE)
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001804 i = 0;
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001805 u--;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001806 }
1807 dma->cmdk = i; dma->cmdu = u;
1808
1809 i = dma->dstk; u = dma->dstu;
1810 while (u != 0) {
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001811 if (dma->dstr[i].l & __cpu_to_le32(HIFN_D_VALID))
1812 break;
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001813 if (++i == HIFN_D_DST_RSIZE)
1814 i = 0;
1815 u--;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001816 }
1817 dma->dstk = i; dma->dstu = u;
1818
1819 dprintk("%s: ring cleanup 2: i: %d.%d.%d.%d, u: %d.%d.%d.%d, "
1820 "k: %d.%d.%d.%d.\n",
1821 dev->name,
1822 dma->cmdi, dma->srci, dma->dsti, dma->resi,
1823 dma->cmdu, dma->srcu, dma->dstu, dma->resu,
1824 dma->cmdk, dma->srck, dma->dstk, dma->resk);
1825}
1826
1827static void hifn_work(struct work_struct *work)
1828{
Jean Delvarebf6aede2009-04-02 16:56:54 -07001829 struct delayed_work *dw = to_delayed_work(work);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001830 struct hifn_device *dev = container_of(dw, struct hifn_device, work);
1831 unsigned long flags;
1832 int reset = 0;
1833 u32 r = 0;
1834
1835 spin_lock_irqsave(&dev->lock, flags);
1836 if (dev->active == 0) {
1837 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1838
1839 if (dma->cmdu == 0 && (dev->flags & HIFN_FLAG_CMD_BUSY)) {
1840 dev->flags &= ~HIFN_FLAG_CMD_BUSY;
1841 r |= HIFN_DMACSR_C_CTRL_DIS;
1842 }
1843 if (dma->srcu == 0 && (dev->flags & HIFN_FLAG_SRC_BUSY)) {
1844 dev->flags &= ~HIFN_FLAG_SRC_BUSY;
1845 r |= HIFN_DMACSR_S_CTRL_DIS;
1846 }
1847 if (dma->dstu == 0 && (dev->flags & HIFN_FLAG_DST_BUSY)) {
1848 dev->flags &= ~HIFN_FLAG_DST_BUSY;
1849 r |= HIFN_DMACSR_D_CTRL_DIS;
1850 }
1851 if (dma->resu == 0 && (dev->flags & HIFN_FLAG_RES_BUSY)) {
1852 dev->flags &= ~HIFN_FLAG_RES_BUSY;
1853 r |= HIFN_DMACSR_R_CTRL_DIS;
1854 }
1855 if (r)
1856 hifn_write_1(dev, HIFN_1_DMA_CSR, r);
1857 } else
1858 dev->active--;
1859
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001860 if ((dev->prev_success == dev->success) && dev->started)
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001861 reset = 1;
1862 dev->prev_success = dev->success;
1863 spin_unlock_irqrestore(&dev->lock, flags);
1864
1865 if (reset) {
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001866 if (++dev->reset >= 5) {
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001867 int i;
1868 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1869
1870 printk("%s: r: %08x, active: %d, started: %d, "
1871 "success: %lu: qlen: %u/%u, reset: %d.\n",
1872 dev->name, r, dev->active, dev->started,
1873 dev->success, dev->queue.qlen, dev->queue.max_qlen,
1874 reset);
1875
1876 printk("%s: res: ", __func__);
1877 for (i=0; i<HIFN_D_RES_RSIZE; ++i) {
1878 printk("%x.%p ", dma->resr[i].l, dev->sa[i]);
1879 if (dev->sa[i]) {
1880 hifn_process_ready(dev->sa[i], -ENODEV);
1881 hifn_complete_sa(dev, i);
1882 }
1883 }
1884 printk("\n");
1885
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001886 hifn_reset_dma(dev, 1);
1887 hifn_stop_device(dev);
1888 hifn_start_device(dev);
1889 dev->reset = 0;
1890 }
1891
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001892 tasklet_schedule(&dev->tasklet);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001893 }
1894
1895 schedule_delayed_work(&dev->work, HZ);
1896}
1897
1898static irqreturn_t hifn_interrupt(int irq, void *data)
1899{
1900 struct hifn_device *dev = (struct hifn_device *)data;
1901 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1902 u32 dmacsr, restart;
1903
1904 dmacsr = hifn_read_1(dev, HIFN_1_DMA_CSR);
1905
1906 dprintk("%s: 1 dmacsr: %08x, dmareg: %08x, res: %08x [%d], "
1907 "i: %d.%d.%d.%d, u: %d.%d.%d.%d.\n",
1908 dev->name, dmacsr, dev->dmareg, dmacsr & dev->dmareg, dma->cmdi,
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001909 dma->cmdi, dma->srci, dma->dsti, dma->resi,
1910 dma->cmdu, dma->srcu, dma->dstu, dma->resu);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001911
1912 if ((dmacsr & dev->dmareg) == 0)
1913 return IRQ_NONE;
1914
1915 hifn_write_1(dev, HIFN_1_DMA_CSR, dmacsr & dev->dmareg);
1916
1917 if (dmacsr & HIFN_DMACSR_ENGINE)
1918 hifn_write_0(dev, HIFN_0_PUISR, hifn_read_0(dev, HIFN_0_PUISR));
1919 if (dmacsr & HIFN_DMACSR_PUBDONE)
1920 hifn_write_1(dev, HIFN_1_PUB_STATUS,
1921 hifn_read_1(dev, HIFN_1_PUB_STATUS) | HIFN_PUBSTS_DONE);
1922
1923 restart = dmacsr & (HIFN_DMACSR_R_OVER | HIFN_DMACSR_D_OVER);
1924 if (restart) {
1925 u32 puisr = hifn_read_0(dev, HIFN_0_PUISR);
1926
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001927 printk(KERN_WARNING "%s: overflow: r: %d, d: %d, puisr: %08x, d: %u.\n",
1928 dev->name, !!(dmacsr & HIFN_DMACSR_R_OVER),
1929 !!(dmacsr & HIFN_DMACSR_D_OVER),
1930 puisr, !!(puisr & HIFN_PUISR_DSTOVER));
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001931 if (!!(puisr & HIFN_PUISR_DSTOVER))
1932 hifn_write_0(dev, HIFN_0_PUISR, HIFN_PUISR_DSTOVER);
1933 hifn_write_1(dev, HIFN_1_DMA_CSR, dmacsr & (HIFN_DMACSR_R_OVER |
1934 HIFN_DMACSR_D_OVER));
1935 }
1936
1937 restart = dmacsr & (HIFN_DMACSR_C_ABORT | HIFN_DMACSR_S_ABORT |
1938 HIFN_DMACSR_D_ABORT | HIFN_DMACSR_R_ABORT);
1939 if (restart) {
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001940 printk(KERN_WARNING "%s: abort: c: %d, s: %d, d: %d, r: %d.\n",
1941 dev->name, !!(dmacsr & HIFN_DMACSR_C_ABORT),
1942 !!(dmacsr & HIFN_DMACSR_S_ABORT),
1943 !!(dmacsr & HIFN_DMACSR_D_ABORT),
1944 !!(dmacsr & HIFN_DMACSR_R_ABORT));
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001945 hifn_reset_dma(dev, 1);
1946 hifn_init_dma(dev);
1947 hifn_init_registers(dev);
1948 }
1949
1950 if ((dmacsr & HIFN_DMACSR_C_WAIT) && (dma->cmdu == 0)) {
1951 dprintk("%s: wait on command.\n", dev->name);
1952 dev->dmareg &= ~(HIFN_DMAIER_C_WAIT);
1953 hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
1954 }
1955
Evgeniy Polyakova1e6ef22007-11-10 20:24:18 +08001956 tasklet_schedule(&dev->tasklet);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001957
1958 return IRQ_HANDLED;
1959}
1960
1961static void hifn_flush(struct hifn_device *dev)
1962{
1963 unsigned long flags;
1964 struct crypto_async_request *async_req;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001965 struct ablkcipher_request *req;
1966 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
1967 int i;
1968
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001969 for (i=0; i<HIFN_D_RES_RSIZE; ++i) {
1970 struct hifn_desc *d = &dma->resr[i];
1971
1972 if (dev->sa[i]) {
1973 hifn_process_ready(dev->sa[i],
1974 (d->l & __cpu_to_le32(HIFN_D_VALID))?-ENODEV:0);
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001975 hifn_complete_sa(dev, i);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001976 }
1977 }
1978
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001979 spin_lock_irqsave(&dev->lock, flags);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001980 while ((async_req = crypto_dequeue_request(&dev->queue))) {
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001981 req = container_of(async_req, struct ablkcipher_request, base);
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001982 spin_unlock_irqrestore(&dev->lock, flags);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001983
1984 hifn_process_ready(req, -ENODEV);
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08001985
1986 spin_lock_irqsave(&dev->lock, flags);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08001987 }
1988 spin_unlock_irqrestore(&dev->lock, flags);
1989}
1990
1991static int hifn_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
1992 unsigned int len)
1993{
1994 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
1995 struct hifn_context *ctx = crypto_tfm_ctx(tfm);
1996 struct hifn_device *dev = ctx->dev;
1997
1998 if (len > HIFN_MAX_CRYPT_KEY_LENGTH) {
1999 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
2000 return -1;
2001 }
2002
Evgeniy Polyakovc3041f92007-10-11 19:58:16 +08002003 if (len == HIFN_DES_KEY_LENGTH) {
2004 u32 tmp[DES_EXPKEY_WORDS];
2005 int ret = des_ekey(tmp, key);
2006
2007 if (unlikely(ret == 0) && (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
2008 tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
2009 return -EINVAL;
2010 }
2011 }
2012
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002013 dev->flags &= ~HIFN_FLAG_OLD_KEY;
2014
2015 memcpy(ctx->key, key, len);
2016 ctx->keysize = len;
2017
2018 return 0;
2019}
2020
2021static int hifn_handle_req(struct ablkcipher_request *req)
2022{
2023 struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
2024 struct hifn_device *dev = ctx->dev;
2025 int err = -EAGAIN;
2026
2027 if (dev->started + DIV_ROUND_UP(req->nbytes, PAGE_SIZE) <= HIFN_QUEUE_LENGTH)
2028 err = hifn_setup_session(req);
2029
2030 if (err == -EAGAIN) {
2031 unsigned long flags;
2032
2033 spin_lock_irqsave(&dev->lock, flags);
2034 err = ablkcipher_enqueue_request(&dev->queue, req);
2035 spin_unlock_irqrestore(&dev->lock, flags);
2036 }
2037
2038 return err;
2039}
2040
2041static int hifn_setup_crypto_req(struct ablkcipher_request *req, u8 op,
2042 u8 type, u8 mode)
2043{
2044 struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08002045 struct hifn_request_context *rctx = ablkcipher_request_ctx(req);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002046 unsigned ivsize;
2047
2048 ivsize = crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(req));
2049
2050 if (req->info && mode != ACRYPTO_MODE_ECB) {
2051 if (type == ACRYPTO_TYPE_AES_128)
2052 ivsize = HIFN_AES_IV_LENGTH;
2053 else if (type == ACRYPTO_TYPE_DES)
2054 ivsize = HIFN_DES_KEY_LENGTH;
2055 else if (type == ACRYPTO_TYPE_3DES)
2056 ivsize = HIFN_3DES_KEY_LENGTH;
2057 }
2058
2059 if (ctx->keysize != 16 && type == ACRYPTO_TYPE_AES_128) {
2060 if (ctx->keysize == 24)
2061 type = ACRYPTO_TYPE_AES_192;
2062 else if (ctx->keysize == 32)
2063 type = ACRYPTO_TYPE_AES_256;
2064 }
2065
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08002066 rctx->op = op;
2067 rctx->mode = mode;
2068 rctx->type = type;
2069 rctx->iv = req->info;
2070 rctx->ivsize = ivsize;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002071
2072 /*
2073 * HEAVY TODO: needs to kick Herbert XU to write documentation.
2074 * HEAVY TODO: needs to kick Herbert XU to write documentation.
2075 * HEAVY TODO: needs to kick Herbert XU to write documentation.
2076 */
2077
2078 return hifn_handle_req(req);
2079}
2080
2081static int hifn_process_queue(struct hifn_device *dev)
2082{
Patrick McHardyed4f92e2008-11-24 22:02:55 +08002083 struct crypto_async_request *async_req, *backlog;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002084 struct ablkcipher_request *req;
2085 unsigned long flags;
2086 int err = 0;
2087
2088 while (dev->started < HIFN_QUEUE_LENGTH) {
2089 spin_lock_irqsave(&dev->lock, flags);
Patrick McHardyed4f92e2008-11-24 22:02:55 +08002090 backlog = crypto_get_backlog(&dev->queue);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002091 async_req = crypto_dequeue_request(&dev->queue);
2092 spin_unlock_irqrestore(&dev->lock, flags);
2093
2094 if (!async_req)
2095 break;
2096
Patrick McHardyed4f92e2008-11-24 22:02:55 +08002097 if (backlog)
2098 backlog->complete(backlog, -EINPROGRESS);
2099
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002100 req = container_of(async_req, struct ablkcipher_request, base);
2101
2102 err = hifn_handle_req(req);
2103 if (err)
2104 break;
2105 }
2106
2107 return err;
2108}
2109
2110static int hifn_setup_crypto(struct ablkcipher_request *req, u8 op,
2111 u8 type, u8 mode)
2112{
2113 int err;
2114 struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
2115 struct hifn_device *dev = ctx->dev;
2116
2117 err = hifn_setup_crypto_req(req, op, type, mode);
2118 if (err)
2119 return err;
2120
2121 if (dev->started < HIFN_QUEUE_LENGTH && dev->queue.qlen)
Patrick McHardy9e70a402008-05-07 22:31:35 +08002122 hifn_process_queue(dev);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002123
Patrick McHardy9e70a402008-05-07 22:31:35 +08002124 return -EINPROGRESS;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002125}
2126
2127/*
2128 * AES ecryption functions.
2129 */
2130static inline int hifn_encrypt_aes_ecb(struct ablkcipher_request *req)
2131{
2132 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2133 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_ECB);
2134}
2135static inline int hifn_encrypt_aes_cbc(struct ablkcipher_request *req)
2136{
2137 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2138 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CBC);
2139}
2140static inline int hifn_encrypt_aes_cfb(struct ablkcipher_request *req)
2141{
2142 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2143 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CFB);
2144}
2145static inline int hifn_encrypt_aes_ofb(struct ablkcipher_request *req)
2146{
2147 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2148 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_OFB);
2149}
2150
2151/*
2152 * AES decryption functions.
2153 */
2154static inline int hifn_decrypt_aes_ecb(struct ablkcipher_request *req)
2155{
2156 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2157 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_ECB);
2158}
2159static inline int hifn_decrypt_aes_cbc(struct ablkcipher_request *req)
2160{
2161 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2162 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CBC);
2163}
2164static inline int hifn_decrypt_aes_cfb(struct ablkcipher_request *req)
2165{
2166 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2167 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CFB);
2168}
2169static inline int hifn_decrypt_aes_ofb(struct ablkcipher_request *req)
2170{
2171 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2172 ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_OFB);
2173}
2174
2175/*
2176 * DES ecryption functions.
2177 */
2178static inline int hifn_encrypt_des_ecb(struct ablkcipher_request *req)
2179{
2180 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2181 ACRYPTO_TYPE_DES, ACRYPTO_MODE_ECB);
2182}
2183static inline int hifn_encrypt_des_cbc(struct ablkcipher_request *req)
2184{
2185 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2186 ACRYPTO_TYPE_DES, ACRYPTO_MODE_CBC);
2187}
2188static inline int hifn_encrypt_des_cfb(struct ablkcipher_request *req)
2189{
2190 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2191 ACRYPTO_TYPE_DES, ACRYPTO_MODE_CFB);
2192}
2193static inline int hifn_encrypt_des_ofb(struct ablkcipher_request *req)
2194{
2195 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2196 ACRYPTO_TYPE_DES, ACRYPTO_MODE_OFB);
2197}
2198
2199/*
2200 * DES decryption functions.
2201 */
2202static inline int hifn_decrypt_des_ecb(struct ablkcipher_request *req)
2203{
2204 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2205 ACRYPTO_TYPE_DES, ACRYPTO_MODE_ECB);
2206}
2207static inline int hifn_decrypt_des_cbc(struct ablkcipher_request *req)
2208{
2209 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2210 ACRYPTO_TYPE_DES, ACRYPTO_MODE_CBC);
2211}
2212static inline int hifn_decrypt_des_cfb(struct ablkcipher_request *req)
2213{
2214 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2215 ACRYPTO_TYPE_DES, ACRYPTO_MODE_CFB);
2216}
2217static inline int hifn_decrypt_des_ofb(struct ablkcipher_request *req)
2218{
2219 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2220 ACRYPTO_TYPE_DES, ACRYPTO_MODE_OFB);
2221}
2222
2223/*
2224 * 3DES ecryption functions.
2225 */
2226static inline int hifn_encrypt_3des_ecb(struct ablkcipher_request *req)
2227{
2228 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2229 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_ECB);
2230}
2231static inline int hifn_encrypt_3des_cbc(struct ablkcipher_request *req)
2232{
2233 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2234 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CBC);
2235}
2236static inline int hifn_encrypt_3des_cfb(struct ablkcipher_request *req)
2237{
2238 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2239 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CFB);
2240}
2241static inline int hifn_encrypt_3des_ofb(struct ablkcipher_request *req)
2242{
2243 return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
2244 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_OFB);
2245}
2246
2247/*
2248 * 3DES decryption functions.
2249 */
2250static inline int hifn_decrypt_3des_ecb(struct ablkcipher_request *req)
2251{
2252 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2253 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_ECB);
2254}
2255static inline int hifn_decrypt_3des_cbc(struct ablkcipher_request *req)
2256{
2257 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2258 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CBC);
2259}
2260static inline int hifn_decrypt_3des_cfb(struct ablkcipher_request *req)
2261{
2262 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2263 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CFB);
2264}
2265static inline int hifn_decrypt_3des_ofb(struct ablkcipher_request *req)
2266{
2267 return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
2268 ACRYPTO_TYPE_3DES, ACRYPTO_MODE_OFB);
2269}
2270
2271struct hifn_alg_template
2272{
2273 char name[CRYPTO_MAX_ALG_NAME];
2274 char drv_name[CRYPTO_MAX_ALG_NAME];
2275 unsigned int bsize;
2276 struct ablkcipher_alg ablkcipher;
2277};
2278
2279static struct hifn_alg_template hifn_alg_templates[] = {
2280 /*
2281 * 3DES ECB, CBC, CFB and OFB modes.
2282 */
2283 {
Patrick McHardy281d6bd2008-05-07 22:35:07 +08002284 .name = "cfb(des3_ede)", .drv_name = "cfb-3des", .bsize = 8,
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002285 .ablkcipher = {
2286 .min_keysize = HIFN_3DES_KEY_LENGTH,
2287 .max_keysize = HIFN_3DES_KEY_LENGTH,
2288 .setkey = hifn_setkey,
2289 .encrypt = hifn_encrypt_3des_cfb,
2290 .decrypt = hifn_decrypt_3des_cfb,
2291 },
2292 },
2293 {
Patrick McHardy281d6bd2008-05-07 22:35:07 +08002294 .name = "ofb(des3_ede)", .drv_name = "ofb-3des", .bsize = 8,
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002295 .ablkcipher = {
2296 .min_keysize = HIFN_3DES_KEY_LENGTH,
2297 .max_keysize = HIFN_3DES_KEY_LENGTH,
2298 .setkey = hifn_setkey,
2299 .encrypt = hifn_encrypt_3des_ofb,
2300 .decrypt = hifn_decrypt_3des_ofb,
2301 },
2302 },
2303 {
Patrick McHardy281d6bd2008-05-07 22:35:07 +08002304 .name = "cbc(des3_ede)", .drv_name = "cbc-3des", .bsize = 8,
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002305 .ablkcipher = {
Patrick McHardy4b804b52008-05-07 22:35:47 +08002306 .ivsize = HIFN_IV_LENGTH,
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002307 .min_keysize = HIFN_3DES_KEY_LENGTH,
2308 .max_keysize = HIFN_3DES_KEY_LENGTH,
2309 .setkey = hifn_setkey,
2310 .encrypt = hifn_encrypt_3des_cbc,
2311 .decrypt = hifn_decrypt_3des_cbc,
2312 },
2313 },
2314 {
Patrick McHardy281d6bd2008-05-07 22:35:07 +08002315 .name = "ecb(des3_ede)", .drv_name = "ecb-3des", .bsize = 8,
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002316 .ablkcipher = {
2317 .min_keysize = HIFN_3DES_KEY_LENGTH,
2318 .max_keysize = HIFN_3DES_KEY_LENGTH,
2319 .setkey = hifn_setkey,
2320 .encrypt = hifn_encrypt_3des_ecb,
2321 .decrypt = hifn_decrypt_3des_ecb,
2322 },
2323 },
2324
2325 /*
2326 * DES ECB, CBC, CFB and OFB modes.
2327 */
2328 {
Patrick McHardy281d6bd2008-05-07 22:35:07 +08002329 .name = "cfb(des)", .drv_name = "cfb-des", .bsize = 8,
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002330 .ablkcipher = {
2331 .min_keysize = HIFN_DES_KEY_LENGTH,
2332 .max_keysize = HIFN_DES_KEY_LENGTH,
2333 .setkey = hifn_setkey,
2334 .encrypt = hifn_encrypt_des_cfb,
2335 .decrypt = hifn_decrypt_des_cfb,
2336 },
2337 },
2338 {
Patrick McHardy281d6bd2008-05-07 22:35:07 +08002339 .name = "ofb(des)", .drv_name = "ofb-des", .bsize = 8,
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002340 .ablkcipher = {
2341 .min_keysize = HIFN_DES_KEY_LENGTH,
2342 .max_keysize = HIFN_DES_KEY_LENGTH,
2343 .setkey = hifn_setkey,
2344 .encrypt = hifn_encrypt_des_ofb,
2345 .decrypt = hifn_decrypt_des_ofb,
2346 },
2347 },
2348 {
Patrick McHardy281d6bd2008-05-07 22:35:07 +08002349 .name = "cbc(des)", .drv_name = "cbc-des", .bsize = 8,
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002350 .ablkcipher = {
Patrick McHardy4b804b52008-05-07 22:35:47 +08002351 .ivsize = HIFN_IV_LENGTH,
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002352 .min_keysize = HIFN_DES_KEY_LENGTH,
2353 .max_keysize = HIFN_DES_KEY_LENGTH,
2354 .setkey = hifn_setkey,
2355 .encrypt = hifn_encrypt_des_cbc,
2356 .decrypt = hifn_decrypt_des_cbc,
2357 },
2358 },
2359 {
Patrick McHardy281d6bd2008-05-07 22:35:07 +08002360 .name = "ecb(des)", .drv_name = "ecb-des", .bsize = 8,
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002361 .ablkcipher = {
2362 .min_keysize = HIFN_DES_KEY_LENGTH,
2363 .max_keysize = HIFN_DES_KEY_LENGTH,
2364 .setkey = hifn_setkey,
2365 .encrypt = hifn_encrypt_des_ecb,
2366 .decrypt = hifn_decrypt_des_ecb,
2367 },
2368 },
2369
2370 /*
2371 * AES ECB, CBC, CFB and OFB modes.
2372 */
2373 {
Patrick McHardy281d6bd2008-05-07 22:35:07 +08002374 .name = "ecb(aes)", .drv_name = "ecb-aes", .bsize = 16,
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002375 .ablkcipher = {
2376 .min_keysize = AES_MIN_KEY_SIZE,
2377 .max_keysize = AES_MAX_KEY_SIZE,
2378 .setkey = hifn_setkey,
2379 .encrypt = hifn_encrypt_aes_ecb,
2380 .decrypt = hifn_decrypt_aes_ecb,
2381 },
2382 },
2383 {
Patrick McHardy281d6bd2008-05-07 22:35:07 +08002384 .name = "cbc(aes)", .drv_name = "cbc-aes", .bsize = 16,
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002385 .ablkcipher = {
Patrick McHardy4b804b52008-05-07 22:35:47 +08002386 .ivsize = HIFN_AES_IV_LENGTH,
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002387 .min_keysize = AES_MIN_KEY_SIZE,
2388 .max_keysize = AES_MAX_KEY_SIZE,
2389 .setkey = hifn_setkey,
2390 .encrypt = hifn_encrypt_aes_cbc,
2391 .decrypt = hifn_decrypt_aes_cbc,
2392 },
2393 },
2394 {
Patrick McHardy281d6bd2008-05-07 22:35:07 +08002395 .name = "cfb(aes)", .drv_name = "cfb-aes", .bsize = 16,
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002396 .ablkcipher = {
2397 .min_keysize = AES_MIN_KEY_SIZE,
2398 .max_keysize = AES_MAX_KEY_SIZE,
2399 .setkey = hifn_setkey,
2400 .encrypt = hifn_encrypt_aes_cfb,
2401 .decrypt = hifn_decrypt_aes_cfb,
2402 },
2403 },
2404 {
Patrick McHardy281d6bd2008-05-07 22:35:07 +08002405 .name = "ofb(aes)", .drv_name = "ofb-aes", .bsize = 16,
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002406 .ablkcipher = {
2407 .min_keysize = AES_MIN_KEY_SIZE,
2408 .max_keysize = AES_MAX_KEY_SIZE,
2409 .setkey = hifn_setkey,
2410 .encrypt = hifn_encrypt_aes_ofb,
2411 .decrypt = hifn_decrypt_aes_ofb,
2412 },
2413 },
2414};
2415
2416static int hifn_cra_init(struct crypto_tfm *tfm)
2417{
2418 struct crypto_alg *alg = tfm->__crt_alg;
2419 struct hifn_crypto_alg *ha = crypto_alg_to_hifn(alg);
2420 struct hifn_context *ctx = crypto_tfm_ctx(tfm);
2421
2422 ctx->dev = ha->dev;
Patrick McHardy5df4c0c2008-11-24 22:01:42 +08002423 tfm->crt_ablkcipher.reqsize = sizeof(struct hifn_request_context);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002424 return 0;
2425}
2426
2427static int hifn_alg_alloc(struct hifn_device *dev, struct hifn_alg_template *t)
2428{
2429 struct hifn_crypto_alg *alg;
2430 int err;
2431
2432 alg = kzalloc(sizeof(struct hifn_crypto_alg), GFP_KERNEL);
2433 if (!alg)
2434 return -ENOMEM;
2435
2436 snprintf(alg->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s", t->name);
Patrick McHardy281d6bd2008-05-07 22:35:07 +08002437 snprintf(alg->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s-%s",
2438 t->drv_name, dev->name);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002439
2440 alg->alg.cra_priority = 300;
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +01002441 alg->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2442 CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002443 alg->alg.cra_blocksize = t->bsize;
2444 alg->alg.cra_ctxsize = sizeof(struct hifn_context);
Patrick McHardyd0690332008-05-07 22:33:37 +08002445 alg->alg.cra_alignmask = 0;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002446 alg->alg.cra_type = &crypto_ablkcipher_type;
2447 alg->alg.cra_module = THIS_MODULE;
2448 alg->alg.cra_u.ablkcipher = t->ablkcipher;
2449 alg->alg.cra_init = hifn_cra_init;
2450
2451 alg->dev = dev;
2452
2453 list_add_tail(&alg->entry, &dev->alg_list);
2454
2455 err = crypto_register_alg(&alg->alg);
2456 if (err) {
2457 list_del(&alg->entry);
2458 kfree(alg);
2459 }
2460
2461 return err;
2462}
2463
2464static void hifn_unregister_alg(struct hifn_device *dev)
2465{
2466 struct hifn_crypto_alg *a, *n;
2467
2468 list_for_each_entry_safe(a, n, &dev->alg_list, entry) {
2469 list_del(&a->entry);
2470 crypto_unregister_alg(&a->alg);
2471 kfree(a);
2472 }
2473}
2474
2475static int hifn_register_alg(struct hifn_device *dev)
2476{
2477 int i, err;
2478
2479 for (i=0; i<ARRAY_SIZE(hifn_alg_templates); ++i) {
2480 err = hifn_alg_alloc(dev, &hifn_alg_templates[i]);
2481 if (err)
2482 goto err_out_exit;
2483 }
2484
2485 return 0;
2486
2487err_out_exit:
2488 hifn_unregister_alg(dev);
2489 return err;
2490}
2491
Evgeniy Polyakova1e6ef22007-11-10 20:24:18 +08002492static void hifn_tasklet_callback(unsigned long data)
2493{
2494 struct hifn_device *dev = (struct hifn_device *)data;
2495
2496 /*
2497 * This is ok to call this without lock being held,
2498 * althogh it modifies some parameters used in parallel,
2499 * (like dev->success), but they are used in process
2500 * context or update is atomic (like setting dev->sa[i] to NULL).
2501 */
Evgeniy Polyakovd6a10c82008-11-24 22:04:39 +08002502 hifn_clear_rings(dev, 0);
Patrick McHardyed4f92e2008-11-24 22:02:55 +08002503
2504 if (dev->started < HIFN_QUEUE_LENGTH && dev->queue.qlen)
2505 hifn_process_queue(dev);
Evgeniy Polyakova1e6ef22007-11-10 20:24:18 +08002506}
2507
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002508static int hifn_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002509{
2510 int err, i;
2511 struct hifn_device *dev;
2512 char name[8];
2513
2514 err = pci_enable_device(pdev);
2515 if (err)
2516 return err;
2517 pci_set_master(pdev);
2518
Yang Hongyang284901a2009-04-06 19:01:15 -07002519 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002520 if (err)
2521 goto err_out_disable_pci_device;
2522
2523 snprintf(name, sizeof(name), "hifn%d",
2524 atomic_inc_return(&hifn_dev_number)-1);
2525
2526 err = pci_request_regions(pdev, name);
2527 if (err)
2528 goto err_out_disable_pci_device;
2529
2530 if (pci_resource_len(pdev, 0) < HIFN_BAR0_SIZE ||
2531 pci_resource_len(pdev, 1) < HIFN_BAR1_SIZE ||
2532 pci_resource_len(pdev, 2) < HIFN_BAR2_SIZE) {
2533 dprintk("%s: Broken hardware - I/O regions are too small.\n",
2534 pci_name(pdev));
2535 err = -ENODEV;
2536 goto err_out_free_regions;
2537 }
2538
2539 dev = kzalloc(sizeof(struct hifn_device) + sizeof(struct crypto_alg),
2540 GFP_KERNEL);
2541 if (!dev) {
2542 err = -ENOMEM;
2543 goto err_out_free_regions;
2544 }
2545
2546 INIT_LIST_HEAD(&dev->alg_list);
2547
2548 snprintf(dev->name, sizeof(dev->name), "%s", name);
2549 spin_lock_init(&dev->lock);
2550
2551 for (i=0; i<3; ++i) {
2552 unsigned long addr, size;
2553
2554 addr = pci_resource_start(pdev, i);
2555 size = pci_resource_len(pdev, i);
2556
2557 dev->bar[i] = ioremap_nocache(addr, size);
Peter Senna Tschudinc2ff8612012-09-17 19:28:26 +02002558 if (!dev->bar[i]) {
2559 err = -ENOMEM;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002560 goto err_out_unmap_bars;
Peter Senna Tschudinc2ff8612012-09-17 19:28:26 +02002561 }
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002562 }
2563
Joe Perches7e835082014-08-08 14:24:14 -07002564 dev->desc_virt = pci_zalloc_consistent(pdev, sizeof(struct hifn_dma),
2565 &dev->desc_dma);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002566 if (!dev->desc_virt) {
2567 dprintk("Failed to allocate descriptor rings.\n");
Peter Senna Tschudinc2ff8612012-09-17 19:28:26 +02002568 err = -ENOMEM;
Patrick McHardy3ec858d2008-11-24 22:03:37 +08002569 goto err_out_unmap_bars;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002570 }
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002571
2572 dev->pdev = pdev;
2573 dev->irq = pdev->irq;
2574
2575 for (i=0; i<HIFN_D_RES_RSIZE; ++i)
2576 dev->sa[i] = NULL;
2577
2578 pci_set_drvdata(pdev, dev);
2579
Evgeniy Polyakova1e6ef22007-11-10 20:24:18 +08002580 tasklet_init(&dev->tasklet, hifn_tasklet_callback, (unsigned long)dev);
2581
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002582 crypto_init_queue(&dev->queue, 1);
2583
2584 err = request_irq(dev->irq, hifn_interrupt, IRQF_SHARED, dev->name, dev);
2585 if (err) {
2586 dprintk("Failed to request IRQ%d: err: %d.\n", dev->irq, err);
2587 dev->irq = 0;
2588 goto err_out_free_desc;
2589 }
2590
2591 err = hifn_start_device(dev);
2592 if (err)
2593 goto err_out_free_irq;
2594
Patrick McHardyfcd06752007-11-21 12:51:52 +08002595 err = hifn_register_rng(dev);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002596 if (err)
2597 goto err_out_stop_device;
2598
Patrick McHardyfcd06752007-11-21 12:51:52 +08002599 err = hifn_register_alg(dev);
2600 if (err)
2601 goto err_out_unregister_rng;
2602
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002603 INIT_DELAYED_WORK(&dev->work, hifn_work);
2604 schedule_delayed_work(&dev->work, HZ);
2605
2606 dprintk("HIFN crypto accelerator card at %s has been "
2607 "successfully registered as %s.\n",
2608 pci_name(pdev), dev->name);
2609
2610 return 0;
2611
Patrick McHardyfcd06752007-11-21 12:51:52 +08002612err_out_unregister_rng:
2613 hifn_unregister_rng(dev);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002614err_out_stop_device:
2615 hifn_reset_dma(dev, 1);
2616 hifn_stop_device(dev);
2617err_out_free_irq:
Lars-Peter Clausenb0226652013-05-20 19:14:50 +02002618 free_irq(dev->irq, dev);
Evgeniy Polyakova1e6ef22007-11-10 20:24:18 +08002619 tasklet_kill(&dev->tasklet);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002620err_out_free_desc:
2621 pci_free_consistent(pdev, sizeof(struct hifn_dma),
2622 dev->desc_virt, dev->desc_dma);
2623
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002624err_out_unmap_bars:
2625 for (i=0; i<3; ++i)
2626 if (dev->bar[i])
2627 iounmap(dev->bar[i]);
2628
2629err_out_free_regions:
2630 pci_release_regions(pdev);
2631
2632err_out_disable_pci_device:
2633 pci_disable_device(pdev);
2634
2635 return err;
2636}
2637
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002638static void hifn_remove(struct pci_dev *pdev)
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002639{
2640 int i;
2641 struct hifn_device *dev;
2642
2643 dev = pci_get_drvdata(pdev);
2644
2645 if (dev) {
Tejun Heof4e523f2010-10-19 20:50:23 +08002646 cancel_delayed_work_sync(&dev->work);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002647
Patrick McHardyfcd06752007-11-21 12:51:52 +08002648 hifn_unregister_rng(dev);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002649 hifn_unregister_alg(dev);
2650 hifn_reset_dma(dev, 1);
2651 hifn_stop_device(dev);
2652
Lars-Peter Clausenb0226652013-05-20 19:14:50 +02002653 free_irq(dev->irq, dev);
Evgeniy Polyakova1e6ef22007-11-10 20:24:18 +08002654 tasklet_kill(&dev->tasklet);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002655
2656 hifn_flush(dev);
2657
2658 pci_free_consistent(pdev, sizeof(struct hifn_dma),
2659 dev->desc_virt, dev->desc_dma);
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002660 for (i=0; i<3; ++i)
2661 if (dev->bar[i])
2662 iounmap(dev->bar[i]);
2663
2664 kfree(dev);
2665 }
2666
2667 pci_release_regions(pdev);
2668 pci_disable_device(pdev);
2669}
2670
2671static struct pci_device_id hifn_pci_tbl[] = {
2672 { PCI_DEVICE(PCI_VENDOR_ID_HIFN, PCI_DEVICE_ID_HIFN_7955) },
2673 { PCI_DEVICE(PCI_VENDOR_ID_HIFN, PCI_DEVICE_ID_HIFN_7956) },
2674 { 0 }
2675};
2676MODULE_DEVICE_TABLE(pci, hifn_pci_tbl);
2677
2678static struct pci_driver hifn_pci_driver = {
2679 .name = "hifn795x",
2680 .id_table = hifn_pci_tbl,
2681 .probe = hifn_probe,
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002682 .remove = hifn_remove,
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002683};
2684
Mike Frysingerf3d8fe42009-05-27 15:16:21 +10002685static int __init hifn_init(void)
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002686{
Patrick McHardy37a80232007-11-21 12:47:13 +08002687 unsigned int freq;
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002688 int err;
2689
Richard Weinberger75b76622011-10-10 12:55:41 +02002690 /* HIFN supports only 32-bit addresses */
2691 BUILD_BUG_ON(sizeof(dma_addr_t) != 4);
Evgeniy Polyakova44b56c2008-10-12 20:14:15 +08002692
Patrick McHardy37a80232007-11-21 12:47:13 +08002693 if (strncmp(hifn_pll_ref, "ext", 3) &&
2694 strncmp(hifn_pll_ref, "pci", 3)) {
2695 printk(KERN_ERR "hifn795x: invalid hifn_pll_ref clock, "
2696 "must be pci or ext");
2697 return -EINVAL;
2698 }
2699
2700 /*
2701 * For the 7955/7956 the reference clock frequency must be in the
2702 * range of 20MHz-100MHz. For the 7954 the upper bound is 66.67MHz,
2703 * but this chip is currently not supported.
2704 */
2705 if (hifn_pll_ref[3] != '\0') {
2706 freq = simple_strtoul(hifn_pll_ref + 3, NULL, 10);
2707 if (freq < 20 || freq > 100) {
2708 printk(KERN_ERR "hifn795x: invalid hifn_pll_ref "
2709 "frequency, must be in the range "
2710 "of 20-100");
2711 return -EINVAL;
2712 }
2713 }
2714
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002715 err = pci_register_driver(&hifn_pci_driver);
2716 if (err < 0) {
2717 dprintk("Failed to register PCI driver for %s device.\n",
2718 hifn_pci_driver.name);
2719 return -ENODEV;
2720 }
2721
2722 printk(KERN_INFO "Driver for HIFN 795x crypto accelerator chip "
2723 "has been successfully registered.\n");
2724
2725 return 0;
2726}
2727
Mike Frysingerf3d8fe42009-05-27 15:16:21 +10002728static void __exit hifn_fini(void)
Evgeniy Polyakovf7d05612007-10-26 21:31:14 +08002729{
2730 pci_unregister_driver(&hifn_pci_driver);
2731
2732 printk(KERN_INFO "Driver for HIFN 795x crypto accelerator chip "
2733 "has been successfully unregistered.\n");
2734}
2735
2736module_init(hifn_init);
2737module_exit(hifn_fini);
2738
2739MODULE_LICENSE("GPL");
2740MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
2741MODULE_DESCRIPTION("Driver for HIFN 795x crypto accelerator chip.");