ocf: switch MODULE_LICENSE from BSD to "Dual BSD/GPL" to make the kernel recognize...
[openwrt.git] / target / linux / generic / files / crypto / ocf / hifn / hifnHIPP.c
1 /*-
2  * Driver for Hifn HIPP-I/II chipset
3  * Copyright (c) 2006 Michael Richardson <mcr@xelerance.com>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in the
13  *   documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *   derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * Effort sponsored by Hifn Inc.
29  *
30  */
31
32 /*
33  * Driver for various Hifn encryption processors.
34  */
35 #include <linux/version.h>
36 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38) && !defined(AUTOCONF_INCLUDED)
37 #include <linux/config.h>
38 #endif
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/list.h>
42 #include <linux/slab.h>
43 #include <linux/wait.h>
44 #include <linux/sched.h>
45 #include <linux/pci.h>
46 #include <linux/delay.h>
47 #include <linux/interrupt.h>
48 #include <linux/spinlock.h>
49 #include <linux/random.h>
50 #include <linux/skbuff.h>
51 #include <linux/uio.h>
52 #include <linux/sysfs.h>
53 #include <linux/miscdevice.h>
54 #include <asm/io.h>
55
56 #include <cryptodev.h>
57
58 #include "hifnHIPPreg.h"
59 #include "hifnHIPPvar.h"
60
61 #if 1
62 #define DPRINTF(a...)   if (hipp_debug) { \
63                                                         printk("%s: ", sc ? \
64                                                                 device_get_nameunit(sc->sc_dev) : "hifn"); \
65                                                         printk(a); \
66                                                 } else
67 #else
68 #define DPRINTF(a...)
69 #endif
70
71 typedef int bus_size_t;
72
73 static inline int
74 pci_get_revid(struct pci_dev *dev)
75 {
76         u8 rid = 0;
77         pci_read_config_byte(dev, PCI_REVISION_ID, &rid);
78         return rid;
79 }
80
81 #define debug hipp_debug
82 int hipp_debug = 0;
83 module_param(hipp_debug, int, 0644);
84 MODULE_PARM_DESC(hipp_debug, "Enable debug");
85
86 int hipp_maxbatch = 1;
87 module_param(hipp_maxbatch, int, 0644);
88 MODULE_PARM_DESC(hipp_maxbatch, "max ops to batch w/o interrupt");
89
90 static  int  hipp_probe(struct pci_dev *dev, const struct pci_device_id *ent);
91 static  void hipp_remove(struct pci_dev *dev);
92 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
93 static irqreturn_t hipp_intr(int irq, void *arg);
94 #else
95 static irqreturn_t hipp_intr(int irq, void *arg, struct pt_regs *regs);
96 #endif
97
98 static int hipp_num_chips = 0;
99 static struct hipp_softc *hipp_chip_idx[HIPP_MAX_CHIPS];
100
101 static  int hipp_newsession(device_t, u_int32_t *, struct cryptoini *);
102 static  int hipp_freesession(device_t, u_int64_t);
103 static  int hipp_process(device_t, struct cryptop *, int);
104
105 static device_method_t hipp_methods = {
106         /* crypto device methods */
107         DEVMETHOD(cryptodev_newsession, hipp_newsession),
108         DEVMETHOD(cryptodev_freesession,hipp_freesession),
109         DEVMETHOD(cryptodev_process,    hipp_process),
110 };
111
112 static __inline u_int32_t
113 READ_REG(struct hipp_softc *sc, unsigned int barno, bus_size_t reg)
114 {
115         u_int32_t v = readl(sc->sc_bar[barno] + reg);
116         //sc->sc_bar0_lastreg = (bus_size_t) -1;
117         return (v);
118 }
119 static __inline void
120 WRITE_REG(struct hipp_softc *sc, unsigned int barno, bus_size_t reg, u_int32_t val)
121 {
122         writel(val, sc->sc_bar[barno] + reg);
123 }
124
125 #define READ_REG_0(sc, reg)         READ_REG(sc, 0, reg)
126 #define WRITE_REG_0(sc, reg, val)   WRITE_REG(sc,0, reg, val)
127 #define READ_REG_1(sc, reg)         READ_REG(sc, 1, reg)
128 #define WRITE_REG_1(sc, reg, val)   WRITE_REG(sc,1, reg, val)
129
130 static int
131 hipp_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri)
132 {
133         return EINVAL;
134 }
135
136 static int
137 hipp_freesession(device_t dev, u_int64_t tid)
138 {
139         return EINVAL;
140 }
141
142 static int
143 hipp_process(device_t dev, struct cryptop *crp, int hint)
144 {
145         return EINVAL;
146 }
147
148 static const char*
149 hipp_partname(struct hipp_softc *sc, char buf[128], size_t blen)
150 {
151         char *n = NULL;
152
153         switch (pci_get_vendor(sc->sc_pcidev)) {
154         case PCI_VENDOR_HIFN:
155                 switch (pci_get_device(sc->sc_pcidev)) {
156                 case PCI_PRODUCT_HIFN_7855:     n = "Hifn 7855";
157                 case PCI_PRODUCT_HIFN_8155:     n = "Hifn 8155";
158                 case PCI_PRODUCT_HIFN_6500:     n = "Hifn 6500";
159                 }
160         }
161
162         if(n==NULL) {
163                 snprintf(buf, blen, "VID=%02x,PID=%02x",
164                          pci_get_vendor(sc->sc_pcidev),
165                          pci_get_device(sc->sc_pcidev));
166         } else {
167                 buf[0]='\0';
168                 strncat(buf, n, blen);
169         }
170         return buf;
171 }
172
173 struct hipp_fs_entry {
174         struct attribute attr;
175         /* other stuff */
176 };
177
178
179 static ssize_t
180 cryptoid_show(struct device *dev,
181               struct device_attribute *attr,
182               char *buf)                                                
183 {                                                               
184         struct hipp_softc *sc;                                  
185
186         sc = pci_get_drvdata(to_pci_dev (dev));
187         return sprintf (buf, "%d\n", sc->sc_cid);
188 }
189
190 struct device_attribute hipp_dev_cryptoid = __ATTR_RO(cryptoid);
191
192 /*
193  * Attach an interface that successfully probed.
194  */
195 static int
196 hipp_probe(struct pci_dev *dev, const struct pci_device_id *ent)
197 {
198         struct hipp_softc *sc = NULL;
199         int i;
200         //char rbase;
201         //u_int16_t ena;
202         int rev;
203         //int rseg;
204         int rc;
205
206         DPRINTF("%s()\n", __FUNCTION__);
207
208         if (pci_enable_device(dev) < 0)
209                 return(-ENODEV);
210
211         if (pci_set_mwi(dev))
212                 return(-ENODEV);
213
214         if (!dev->irq) {
215                 printk("hifn: found device with no IRQ assigned. check BIOS settings!");
216                 pci_disable_device(dev);
217                 return(-ENODEV);
218         }
219
220         sc = (struct hipp_softc *) kmalloc(sizeof(*sc), GFP_KERNEL);
221         if (!sc)
222                 return(-ENOMEM);
223         memset(sc, 0, sizeof(*sc));
224
225         softc_device_init(sc, "hifn-hipp", hipp_num_chips, hipp_methods);
226
227         sc->sc_pcidev = dev;
228         sc->sc_irq = -1;
229         sc->sc_cid = -1;
230         sc->sc_num = hipp_num_chips++;
231
232         if (sc->sc_num < HIPP_MAX_CHIPS)
233                 hipp_chip_idx[sc->sc_num] = sc;
234
235         pci_set_drvdata(sc->sc_pcidev, sc);
236
237         spin_lock_init(&sc->sc_mtx);
238
239         /*
240          * Setup PCI resources.
241          * The READ_REG_0, WRITE_REG_0, READ_REG_1,
242          * and WRITE_REG_1 macros throughout the driver are used
243          * to permit better debugging.
244          */
245         for(i=0; i<4; i++) {
246                 unsigned long mem_start, mem_len;
247                 mem_start = pci_resource_start(sc->sc_pcidev, i);
248                 mem_len   = pci_resource_len(sc->sc_pcidev, i);
249                 sc->sc_barphy[i] = (caddr_t)mem_start;
250                 sc->sc_bar[i] = (ocf_iomem_t) ioremap(mem_start, mem_len);
251                 if (!sc->sc_bar[i]) {
252                         device_printf(sc->sc_dev, "cannot map bar%d register space\n", i);
253                         goto fail;
254                 }
255         }
256
257         //hipp_reset_board(sc, 0);
258         pci_set_master(sc->sc_pcidev);
259
260         /*
261          * Arrange the interrupt line.
262          */
263         rc = request_irq(dev->irq, hipp_intr, IRQF_SHARED, "hifn", sc);
264         if (rc) {
265                 device_printf(sc->sc_dev, "could not map interrupt: %d\n", rc);
266                 goto fail;
267         }
268         sc->sc_irq = dev->irq;
269
270         rev = READ_REG_1(sc, HIPP_1_REVID) & 0xffff;
271
272         {
273                 char b[32];
274                 device_printf(sc->sc_dev, "%s, rev %u",
275                               hipp_partname(sc, b, sizeof(b)), rev);
276         }
277
278 #if 0
279         if (sc->sc_flags & HIFN_IS_7956)
280                 printf(", pll=0x%x<%s clk, %ux mult>",
281                         sc->sc_pllconfig,
282                         sc->sc_pllconfig & HIFN_PLL_REF_SEL ? "ext" : "pci",
283                         2 + 2*((sc->sc_pllconfig & HIFN_PLL_ND) >> 11));
284 #endif
285         printf("\n");
286
287         sc->sc_cid = crypto_get_driverid(softc_get_device(sc),CRYPTOCAP_F_HARDWARE);
288         if (sc->sc_cid < 0) {
289                 device_printf(sc->sc_dev, "could not get crypto driver id\n");
290                 goto fail;
291         }
292
293 #if 0 /* cannot work with a non-GPL module */
294         /* make a sysfs entry to let the world know what entry we got */
295         sysfs_create_file(&sc->sc_pcidev->dev.kobj, &hipp_dev_cryptoid.attr);
296 #endif
297
298 #if 0
299         init_timer(&sc->sc_tickto);
300         sc->sc_tickto.function = hifn_tick;
301         sc->sc_tickto.data = (unsigned long) sc->sc_num;
302         mod_timer(&sc->sc_tickto, jiffies + HZ);
303 #endif
304
305 #if 0 /* no code here yet ?? */
306         crypto_register(sc->sc_cid, CRYPTO_3DES_CBC, 0, 0);
307 #endif
308
309         return (0);
310
311 fail:
312         if (sc->sc_cid >= 0)
313                 crypto_unregister_all(sc->sc_cid);
314         if (sc->sc_irq != -1)
315                 free_irq(sc->sc_irq, sc);
316         
317 #if 0
318         if (sc->sc_dma) {
319                 /* Turn off DMA polling */
320                 WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
321                             HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
322                 
323                 pci_free_consistent(sc->sc_pcidev,
324                                     sizeof(*sc->sc_dma),
325                                     sc->sc_dma, sc->sc_dma_physaddr);
326         }
327 #endif
328         kfree(sc);
329         return (-ENXIO);
330 }
331
332 /*
333  * Detach an interface that successfully probed.
334  */
335 static void
336 hipp_remove(struct pci_dev *dev)
337 {
338         struct hipp_softc *sc = pci_get_drvdata(dev);
339         unsigned long l_flags;
340
341         DPRINTF("%s()\n", __FUNCTION__);
342
343         /* disable interrupts */
344         HIPP_LOCK(sc);
345
346 #if 0
347         WRITE_REG_1(sc, HIFN_1_DMA_IER, 0);
348         HIFN_UNLOCK(sc);
349
350         /*XXX other resources */
351         del_timer_sync(&sc->sc_tickto);
352
353         /* Turn off DMA polling */
354         WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
355             HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
356 #endif
357
358         crypto_unregister_all(sc->sc_cid);
359
360         free_irq(sc->sc_irq, sc);
361
362 #if 0
363         pci_free_consistent(sc->sc_pcidev, sizeof(*sc->sc_dma),
364                 sc->sc_dma, sc->sc_dma_physaddr);
365 #endif
366 }
367
368 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
369 static irqreturn_t hipp_intr(int irq, void *arg)
370 #else
371 static irqreturn_t hipp_intr(int irq, void *arg, struct pt_regs *regs)
372 #endif
373 {
374         struct hipp_softc *sc = arg;
375
376         sc = sc; /* shut up compiler */
377
378         return IRQ_HANDLED;
379 }
380
381 static struct pci_device_id hipp_pci_tbl[] = {
382         { PCI_VENDOR_HIFN, PCI_PRODUCT_HIFN_7855,
383           PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
384         { PCI_VENDOR_HIFN, PCI_PRODUCT_HIFN_8155,
385           PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
386         { 0 }, /* terminating entry */
387 };
388 MODULE_DEVICE_TABLE(pci, hipp_pci_tbl);
389
390 static struct pci_driver hipp_driver = {
391         .name         = "hipp",
392         .id_table     = hipp_pci_tbl,
393         .probe        = hipp_probe,
394         .remove       = hipp_remove,
395         /* add PM stuff here one day */
396 };
397
398 static int __init hipp_init (void)
399 {
400         struct hipp_softc *sc = NULL;
401         int rc;
402
403         DPRINTF("%s(%p)\n", __FUNCTION__, hipp_init);
404
405         rc = pci_register_driver(&hipp_driver);
406         pci_register_driver_compat(&hipp_driver, rc);
407
408         return rc;
409 }
410
411 static void __exit hipp_exit (void)
412 {
413         pci_unregister_driver(&hipp_driver);
414 }
415
416 module_init(hipp_init);
417 module_exit(hipp_exit);
418
419 MODULE_LICENSE("Dual BSD/GPL");
420 MODULE_AUTHOR("Michael Richardson <mcr@xelerance.com>");
421 MODULE_DESCRIPTION("OCF driver for hifn HIPP-I/II PCI crypto devices");