3 * Copyright (c) 2008 Daniel Mueller (daniel@danm.de)
4 * Copyright (c) 2007 David McCullough (david_mccullough@securecomputing.com)
5 * Copyright (c) 2000 Jason L. Wright (jason@thought.net)
6 * Copyright (c) 2000 Theo de Raadt (deraadt@openbsd.org)
7 * Copyright (c) 2001 Patrik Lindergren (patrik@ipunplugged.com)
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * Effort sponsored in part by the Defense Advanced Research Projects
31 * Agency (DARPA) and Air Force Research Laboratory, Air Force
32 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
36 #undef UBSEC_VERBOSE_DEBUG
38 #ifdef UBSEC_VERBOSE_DEBUG
43 * uBsec BCM5365 hardware crypto accelerator
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/moduleparam.h>
49 #include <linux/proc_fs.h>
50 #include <linux/types.h>
51 #include <linux/init.h>
52 #include <linux/delay.h>
53 #include <linux/interrupt.h>
55 #include <linux/random.h>
56 #include <linux/skbuff.h>
57 #include <linux/stat.h>
60 #include <linux/ssb/ssb.h>
70 #include "cryptodev.h"
86 #define DRV_MODULE_NAME "ubsec_ssb"
87 #define PFX DRV_MODULE_NAME ": "
88 #define DRV_MODULE_VERSION "0.02"
89 #define DRV_MODULE_RELDATE "Feb 21, 2009"
92 #define DPRINTF(a...) \
95 printk(DRV_MODULE_NAME ": " a); \
104 static irqreturn_t
ubsec_ssb_isr(int, void *, struct pt_regs
*);
105 static int __devinit
ubsec_ssb_probe(struct ssb_device
*sdev
,
106 const struct ssb_device_id
*ent
);
107 static void __devexit
ubsec_ssb_remove(struct ssb_device
*sdev
);
108 int ubsec_attach(struct ssb_device
*sdev
, const struct ssb_device_id
*ent
,
109 struct device
*self
);
110 static void ubsec_setup_mackey(struct ubsec_session
*ses
, int algo
,
111 caddr_t key
, int klen
);
112 static int dma_map_skb(struct ubsec_softc
*sc
,
113 struct ubsec_dma_alloc
* q_map
, struct sk_buff
*skb
, int *mlen
);
114 static int dma_map_uio(struct ubsec_softc
*sc
,
115 struct ubsec_dma_alloc
*q_map
, struct uio
*uio
, int *mlen
);
116 static void dma_unmap(struct ubsec_softc
*sc
,
117 struct ubsec_dma_alloc
*q_map
, int mlen
);
118 static int ubsec_dmamap_aligned(struct ubsec_softc
*sc
,
119 const struct ubsec_dma_alloc
*q_map
, int mlen
);
122 static int proc_read(char *buf
, char **start
, off_t offset
,
123 int size
, int *peof
, void *data
);
126 void ubsec_reset_board(struct ubsec_softc
*);
127 void ubsec_init_board(struct ubsec_softc
*);
128 void ubsec_cleanchip(struct ubsec_softc
*);
129 void ubsec_totalreset(struct ubsec_softc
*);
130 int ubsec_free_q(struct ubsec_softc
*, struct ubsec_q
*);
132 static int ubsec_newsession(device_t
, u_int32_t
*, struct cryptoini
*);
133 static int ubsec_freesession(device_t
, u_int64_t
);
134 static int ubsec_process(device_t
, struct cryptop
*, int);
136 void ubsec_callback(struct ubsec_softc
*, struct ubsec_q
*);
137 void ubsec_feed(struct ubsec_softc
*);
138 void ubsec_mcopy(struct sk_buff
*, struct sk_buff
*, int, int);
139 void ubsec_dma_free(struct ubsec_softc
*, struct ubsec_dma_alloc
*);
140 int ubsec_dma_malloc(struct ubsec_softc
*, struct ubsec_dma_alloc
*,
144 void ubsec_dump_pb(struct ubsec_pktbuf
*);
145 void ubsec_dump_mcr(struct ubsec_mcr
*);
147 #define READ_REG(sc,r) \
148 ssb_read32((sc)->sdev, (r));
149 #define WRITE_REG(sc,r,val) \
150 ssb_write32((sc)->sdev, (r), (val));
151 #define READ_REG_SDEV(sdev,r) \
152 ssb_read32((sdev), (r));
153 #define WRITE_REG_SDEV(sdev,r,val) \
154 ssb_write32((sdev), (r), (val));
156 #define SWAP32(x) (x) = htole32(ntohl((x)))
157 #define HTOLE32(x) (x) = htole32(x)
159 #ifdef __LITTLE_ENDIAN
160 #define letoh16(x) (x)
161 #define letoh32(x) (x)
165 module_param(debug
, int, 0644);
166 MODULE_PARM_DESC(debug
, "Enable debug output");
168 #define UBSEC_SSB_MAX_CHIPS 1
169 static struct ubsec_softc
*ubsec_chip_idx
[UBSEC_SSB_MAX_CHIPS
];
170 static struct ubsec_stats ubsecstats
;
173 static struct proc_dir_entry
*procdebug
;
176 static struct ssb_device_id ubsec_ssb_tbl
[] = {
177 /* Broadcom BCM5365P IPSec Core */
178 SSB_DEVICE(SSB_VENDOR_BROADCOM
, SSB_DEV_IPSEC
, SSB_ANY_REV
),
182 static struct ssb_driver ubsec_ssb_driver
= {
183 .name
= DRV_MODULE_NAME
,
184 .id_table
= ubsec_ssb_tbl
,
185 .probe
= ubsec_ssb_probe
,
186 .remove
= __devexit_p(ubsec_ssb_remove
),
188 .suspend = ubsec_ssb_suspend,
189 .resume = ubsec_ssb_resume
193 static device_method_t ubsec_ssb_methods
= {
194 /* crypto device methods */
195 DEVMETHOD(cryptodev_newsession
, ubsec_newsession
),
196 DEVMETHOD(cryptodev_freesession
,ubsec_freesession
),
197 DEVMETHOD(cryptodev_process
, ubsec_process
),
202 proc_read(char *buf
, char **start
, off_t offset
,
203 int size
, int *peof
, void *data
)
205 int i
= 0, byteswritten
= 0, ret
;
206 unsigned int stat
, ctrl
;
207 #ifdef UBSEC_VERBOSE_DEBUG
209 struct ubsec_dma
*dmap
;
212 while ((i
< UBSEC_SSB_MAX_CHIPS
) && (ubsec_chip_idx
[i
] != NULL
))
214 struct ubsec_softc
*sc
= ubsec_chip_idx
[i
];
216 stat
= READ_REG(sc
, BS_STAT
);
217 ctrl
= READ_REG(sc
, BS_CTRL
);
218 ret
= snprintf((buf
+ byteswritten
),
219 (size
- byteswritten
) ,
220 "DEV %d, DMASTAT %08x, DMACTRL %08x\n", i
, stat
, ctrl
);
224 #ifdef UBSEC_VERBOSE_DEBUG
225 printf("DEV %d, DMASTAT %08x, DMACTRL %08x\n", i
, stat
, ctrl
);
227 /* Dump all queues MCRs */
228 if (!BSD_SIMPLEQ_EMPTY(&sc
->sc_qchip
)) {
229 BSD_SIMPLEQ_FOREACH(q
, &sc
->sc_qchip
, q_next
)
232 ubsec_dump_mcr(&dmap
->d_dma
->d_mcr
);
247 * map in a given sk_buff
250 dma_map_skb(struct ubsec_softc
*sc
, struct ubsec_dma_alloc
* q_map
, struct sk_buff
*skb
, int *mlen
)
256 DPRINTF("%s()\n", __FUNCTION__
);
260 * We support only a limited number of fragments.
262 if (unlikely((skb_shinfo(skb
)->nr_frags
+ 1) >= UBS_MAX_SCATTER
))
264 printk(KERN_ERR
"Only %d scatter fragments are supported.\n", UBS_MAX_SCATTER
);
268 #ifdef UBSEC_VERBOSE_DEBUG
269 DPRINTF("%s - map %d 0x%x %d\n", __FUNCTION__
, 0, (unsigned int)skb
->data
, skb_headlen(skb
));
272 /* first data package */
273 tmp
= dma_map_single(sc
->sc_dv
,
278 q_map
[i
].dma_paddr
= tmp
;
279 q_map
[i
].dma_vaddr
= skb
->data
;
280 q_map
[i
].dma_size
= skb_headlen(skb
);
282 if (unlikely(tmp
== 0))
284 printk(KERN_ERR
"Could not map memory region for dma.\n");
288 #ifdef UBSEC_VERBOSE_DEBUG
289 DPRINTF("%s - map %d done physical addr 0x%x\n", __FUNCTION__
, 0, (unsigned int)tmp
);
293 /* all other data packages */
294 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
296 #ifdef UBSEC_VERBOSE_DEBUG
297 DPRINTF("%s - map %d 0x%x %d\n", __FUNCTION__
, i
+ 1,
298 (unsigned int)page_address(skb_shinfo(skb
)->frags
[i
].page
) +
299 skb_shinfo(skb
)->frags
[i
].page_offset
, skb_shinfo(skb
)->frags
[i
].size
);
302 tmp
= dma_map_single(sc
->sc_dv
,
303 page_address(skb_shinfo(skb
)->frags
[i
].page
) +
304 skb_shinfo(skb
)->frags
[i
].page_offset
,
305 skb_shinfo(skb
)->frags
[i
].size
,
308 q_map
[i
+ 1].dma_paddr
= tmp
;
309 q_map
[i
+ 1].dma_vaddr
= (void*)(page_address(skb_shinfo(skb
)->frags
[i
].page
) +
310 skb_shinfo(skb
)->frags
[i
].page_offset
);
311 q_map
[i
+ 1].dma_size
= skb_shinfo(skb
)->frags
[i
].size
;
313 if (unlikely(tmp
== 0))
315 printk(KERN_ERR
"Could not map memory region for dma.\n");
319 #ifdef UBSEC_VERBOSE_DEBUG
320 DPRINTF("%s - map %d done physical addr 0x%x\n", __FUNCTION__
, i
+ 1, (unsigned int)tmp
);
330 * map in a given uio buffer
334 dma_map_uio(struct ubsec_softc
*sc
, struct ubsec_dma_alloc
*q_map
, struct uio
*uio
, int *mlen
)
336 struct iovec
*iov
= uio
->uio_iov
;
341 DPRINTF("%s()\n", __FUNCTION__
);
345 * We support only a limited number of fragments.
347 if (unlikely(uio
->uio_iovcnt
>= UBS_MAX_SCATTER
))
349 printk(KERN_ERR
"Only %d scatter fragments are supported.\n", UBS_MAX_SCATTER
);
353 for (n
= 0; n
< uio
->uio_iovcnt
; n
++) {
354 #ifdef UBSEC_VERBOSE_DEBUG
355 DPRINTF("%s - map %d 0x%x %d\n", __FUNCTION__
, n
, (unsigned int)iov
->iov_base
, iov
->iov_len
);
357 tmp
= dma_map_single(sc
->sc_dv
,
362 q_map
[n
].dma_paddr
= tmp
;
363 q_map
[n
].dma_vaddr
= iov
->iov_base
;
364 q_map
[n
].dma_size
= iov
->iov_len
;
366 if (unlikely(tmp
== 0))
368 printk(KERN_ERR
"Could not map memory region for dma.\n");
372 #ifdef UBSEC_VERBOSE_DEBUG
373 DPRINTF("%s - map %d done physical addr 0x%x\n", __FUNCTION__
, n
, (unsigned int)tmp
);
384 dma_unmap(struct ubsec_softc
*sc
, struct ubsec_dma_alloc
*q_map
, int mlen
)
389 DPRINTF("%s()\n", __FUNCTION__
);
392 for(i
= 0; i
< mlen
; i
++)
394 #ifdef UBSEC_VERBOSE_DEBUG
395 DPRINTF("%s - unmap %d 0x%x %d\n", __FUNCTION__
, i
, (unsigned int)q_map
[i
].dma_paddr
, q_map
[i
].dma_size
);
397 dma_unmap_single(sc
->sc_dv
,
406 * Is the operand suitable aligned for direct DMA. Each
407 * segment must be aligned on a 32-bit boundary and all
408 * but the last segment must be a multiple of 4 bytes.
411 ubsec_dmamap_aligned(struct ubsec_softc
*sc
, const struct ubsec_dma_alloc
*q_map
, int mlen
)
416 DPRINTF("%s()\n", __FUNCTION__
);
419 for (i
= 0; i
< mlen
; i
++) {
420 if (q_map
[i
].dma_paddr
& 3)
422 if (i
!= (mlen
- 1) && (q_map
[i
].dma_size
& 3))
429 #define N(a) (sizeof(a) / sizeof (a[0]))
431 ubsec_setup_mackey(struct ubsec_session
*ses
, int algo
, caddr_t key
, int klen
)
439 DPRINTF("%s()\n", __FUNCTION__
);
442 for (i
= 0; i
< klen
; i
++)
443 key
[i
] ^= HMAC_IPAD_VAL
;
445 if (algo
== CRYPTO_MD5_HMAC
) {
447 MD5Update(&md5ctx
, key
, klen
);
448 MD5Update(&md5ctx
, hmac_ipad_buffer
, MD5_HMAC_BLOCK_LEN
- klen
);
449 bcopy(md5ctx
.md5_st8
, ses
->ses_hminner
, sizeof(md5ctx
.md5_st8
));
452 SHA1Update(&sha1ctx
, key
, klen
);
453 SHA1Update(&sha1ctx
, hmac_ipad_buffer
,
454 SHA1_HMAC_BLOCK_LEN
- klen
);
455 bcopy(sha1ctx
.h
.b32
, ses
->ses_hminner
, sizeof(sha1ctx
.h
.b32
));
458 for (i
= 0; i
< klen
; i
++)
459 key
[i
] ^= (HMAC_IPAD_VAL
^ HMAC_OPAD_VAL
);
461 if (algo
== CRYPTO_MD5_HMAC
) {
463 MD5Update(&md5ctx
, key
, klen
);
464 MD5Update(&md5ctx
, hmac_opad_buffer
, MD5_HMAC_BLOCK_LEN
- klen
);
465 bcopy(md5ctx
.md5_st8
, ses
->ses_hmouter
, sizeof(md5ctx
.md5_st8
));
468 SHA1Update(&sha1ctx
, key
, klen
);
469 SHA1Update(&sha1ctx
, hmac_opad_buffer
,
470 SHA1_HMAC_BLOCK_LEN
- klen
);
471 bcopy(sha1ctx
.h
.b32
, ses
->ses_hmouter
, sizeof(sha1ctx
.h
.b32
));
474 for (i
= 0; i
< klen
; i
++)
475 key
[i
] ^= HMAC_OPAD_VAL
;
477 #else /* HMAC_HACK */
478 DPRINTF("md5/sha not implemented\n");
479 #endif /* HMAC_HACK */
484 __devinit
ubsec_ssb_probe(struct ssb_device
*sdev
,
485 const struct ssb_device_id
*ent
)
490 DPRINTF("%s()\n", __FUNCTION__
);
493 err
= ssb_bus_powerup(sdev
->bus
, 0);
495 dev_err(sdev
->dev
, "Failed to powerup the bus\n");
499 err
= request_irq(sdev
->irq
, (irq_handler_t
)ubsec_ssb_isr
,
500 IRQF_DISABLED
| IRQF_SHARED
, DRV_MODULE_NAME
, sdev
);
502 dev_err(sdev
->dev
, "Could not request irq\n");
503 goto err_out_powerdown
;
506 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,34))
507 err
= dma_set_mask(sdev
->dma_dev
, DMA_BIT_MASK(32)) ||
508 dma_set_coherent_mask(sdev
->dma_dev
, DMA_BIT_MASK(32));
510 err
= ssb_dma_set_mask(sdev
, DMA_32BIT_MASK
);
514 "Required 32BIT DMA mask unsupported by the system.\n");
515 goto err_out_free_irq
;
518 printk(KERN_INFO
"Sentry5(tm) ROBOGateway(tm) IPSec Core at IRQ %u\n",
521 DPRINTF("Vendor: %x, core id: %x, revision: %x\n",
522 sdev
->id
.vendor
, sdev
->id
.coreid
, sdev
->id
.revision
);
524 ssb_device_enable(sdev
, 0);
526 if (ubsec_attach(sdev
, ent
, sdev
->dev
) != 0)
527 goto err_out_disable
;
530 procdebug
= create_proc_entry(DRV_MODULE_NAME
, S_IRUSR
, NULL
);
533 procdebug
->read_proc
= proc_read
;
534 procdebug
->data
= NULL
;
536 DPRINTF("Unable to create proc file.\n");
542 ssb_device_disable(sdev
, 0);
545 free_irq(sdev
->irq
, sdev
);
548 ssb_bus_may_powerdown(sdev
->bus
);
554 static void __devexit
ubsec_ssb_remove(struct ssb_device
*sdev
) {
556 struct ubsec_softc
*sc
;
557 unsigned int ctrlflgs
;
558 struct ubsec_dma
*dmap
;
562 DPRINTF("%s()\n", __FUNCTION__
);
565 ctrlflgs
= READ_REG_SDEV(sdev
, BS_CTRL
);
566 /* disable all IPSec Core interrupts globally */
567 ctrlflgs
^= (BS_CTRL_MCR1INT
| BS_CTRL_MCR2INT
|
569 WRITE_REG_SDEV(sdev
, BS_CTRL
, ctrlflgs
);
571 free_irq(sdev
->irq
, sdev
);
573 sc
= (struct ubsec_softc
*)ssb_get_drvdata(sdev
);
575 /* unregister all crypto algorithms */
576 crypto_unregister_all(sc
->sc_cid
);
578 /* Free queue / dma memory */
579 for (i
= 0; i
< UBS_MAX_NQUEUE
; i
++) {
582 q
= sc
->sc_queuea
[i
];
588 ubsec_dma_free(sc
, &dmap
->d_alloc
);
593 sc
->sc_queuea
[i
] = NULL
;
596 ssb_device_disable(sdev
, 0);
597 ssb_bus_may_powerdown(sdev
->bus
);
598 ssb_set_drvdata(sdev
, NULL
);
602 remove_proc_entry(DRV_MODULE_NAME
, NULL
);
609 ubsec_attach(struct ssb_device
*sdev
, const struct ssb_device_id
*ent
,
612 struct ubsec_softc
*sc
= NULL
;
613 struct ubsec_dma
*dmap
;
615 static int num_chips
= 0;
618 DPRINTF("%s()\n", __FUNCTION__
);
621 sc
= (struct ubsec_softc
*) kmalloc(sizeof(*sc
), GFP_KERNEL
);
624 memset(sc
, 0, sizeof(*sc
));
626 sc
->sc_dv
= sdev
->dev
;
629 spin_lock_init(&sc
->sc_ringmtx
);
631 softc_device_init(sc
, "ubsec_ssb", num_chips
, ubsec_ssb_methods
);
633 /* Maybe someday there are boards with more than one chip available */
634 if (num_chips
< UBSEC_SSB_MAX_CHIPS
) {
635 ubsec_chip_idx
[device_get_unit(sc
->sc_dev
)] = sc
;
639 ssb_set_drvdata(sdev
, sc
);
641 BSD_SIMPLEQ_INIT(&sc
->sc_queue
);
642 BSD_SIMPLEQ_INIT(&sc
->sc_qchip
);
643 BSD_SIMPLEQ_INIT(&sc
->sc_queue2
);
644 BSD_SIMPLEQ_INIT(&sc
->sc_qchip2
);
645 BSD_SIMPLEQ_INIT(&sc
->sc_q2free
);
647 sc
->sc_statmask
= BS_STAT_MCR1_DONE
| BS_STAT_DMAERR
;
649 sc
->sc_cid
= crypto_get_driverid(softc_get_device(sc
), CRYPTOCAP_F_HARDWARE
);
650 if (sc
->sc_cid
< 0) {
651 device_printf(sc
->sc_dev
, "could not get crypto driver id\n");
655 BSD_SIMPLEQ_INIT(&sc
->sc_freequeue
);
657 for (i
= 0; i
< UBS_MAX_NQUEUE
; i
++, dmap
++) {
660 q
= (struct ubsec_q
*)kmalloc(sizeof(struct ubsec_q
), GFP_KERNEL
);
662 printf(": can't allocate queue buffers\n");
666 if (ubsec_dma_malloc(sc
, &dmap
->d_alloc
, sizeof(struct ubsec_dmachunk
),0)) {
667 printf(": can't allocate dma buffers\n");
671 dmap
->d_dma
= (struct ubsec_dmachunk
*)dmap
->d_alloc
.dma_vaddr
;
674 sc
->sc_queuea
[i
] = q
;
676 BSD_SIMPLEQ_INSERT_TAIL(&sc
->sc_freequeue
, q
, q_next
);
680 * Reset Broadcom chip
682 ubsec_reset_board(sc
);
687 ubsec_init_board(sc
);
689 /* supported crypto algorithms */
690 crypto_register(sc
->sc_cid
, CRYPTO_3DES_CBC
, 0, 0);
691 crypto_register(sc
->sc_cid
, CRYPTO_DES_CBC
, 0, 0);
693 if (sc
->sc_flags
& UBS_FLAGS_AES
) {
694 crypto_register(sc
->sc_cid
, CRYPTO_AES_CBC
, 0, 0);
695 printf(KERN_INFO DRV_MODULE_NAME
": DES 3DES AES128 AES192 AES256 MD5_HMAC SHA1_HMAC\n");
698 printf(KERN_INFO DRV_MODULE_NAME
": DES 3DES MD5_HMAC SHA1_HMAC\n");
700 crypto_register(sc
->sc_cid
, CRYPTO_MD5_HMAC
, 0, 0);
701 crypto_register(sc
->sc_cid
, CRYPTO_SHA1_HMAC
, 0, 0);
707 * UBSEC Interrupt routine
710 ubsec_ssb_isr(int irq
, void *arg
, struct pt_regs
*regs
)
712 struct ubsec_softc
*sc
= NULL
;
713 volatile u_int32_t stat
;
715 struct ubsec_dma
*dmap
;
718 #ifdef UBSEC_VERBOSE_DEBUG
719 DPRINTF("%s()\n", __FUNCTION__
);
722 sc
= (struct ubsec_softc
*)ssb_get_drvdata(arg
);
724 stat
= READ_REG(sc
, BS_STAT
);
726 stat
&= sc
->sc_statmask
;
730 WRITE_REG(sc
, BS_STAT
, stat
); /* IACK */
733 * Check to see if we have any packets waiting for us
735 if ((stat
& BS_STAT_MCR1_DONE
)) {
736 while (!BSD_SIMPLEQ_EMPTY(&sc
->sc_qchip
)) {
737 q
= BSD_SIMPLEQ_FIRST(&sc
->sc_qchip
);
740 if ((dmap
->d_dma
->d_mcr
.mcr_flags
& htole16(UBS_MCR_DONE
)) == 0)
742 DPRINTF("error while processing MCR. Flags = %x\n", dmap
->d_dma
->d_mcr
.mcr_flags
);
746 BSD_SIMPLEQ_REMOVE_HEAD(&sc
->sc_qchip
, q_next
);
748 npkts
= q
->q_nstacked_mcrs
;
750 * search for further sc_qchip ubsec_q's that share
751 * the same MCR, and complete them too, they must be
754 for (i
= 0; i
< npkts
; i
++) {
755 if(q
->q_stacked_mcr
[i
])
756 ubsec_callback(sc
, q
->q_stacked_mcr
[i
]);
760 ubsec_callback(sc
, q
);
764 * Don't send any more packet to chip if there has been
767 if (likely(!(stat
& BS_STAT_DMAERR
)))
770 DPRINTF("DMA error occurred. Stop feeding crypto chip.\n");
774 * Check to see if we got any DMA Error
776 if (stat
& BS_STAT_DMAERR
) {
777 volatile u_int32_t a
= READ_REG(sc
, BS_ERR
);
779 printf(KERN_ERR
"%s: dmaerr %s@%08x\n", DRV_MODULE_NAME
,
780 (a
& BS_ERR_READ
) ? "read" : "write", a
& BS_ERR_ADDR
);
782 ubsecstats
.hst_dmaerr
++;
783 ubsec_totalreset(sc
);
791 * ubsec_feed() - aggregate and post requests to chip
792 * It is assumed that the caller set splnet()
795 ubsec_feed(struct ubsec_softc
*sc
)
797 #ifdef UBSEC_VERBOSE_DEBUG
800 struct ubsec_q
*q
, *q2
;
805 npkts
= sc
->sc_nqueue
;
806 if (npkts
> UBS_MAX_AGGR
)
807 npkts
= UBS_MAX_AGGR
;
811 stat
= READ_REG(sc
, BS_STAT
);
813 if (stat
& (BS_STAT_MCR1_FULL
| BS_STAT_DMAERR
)) {
814 if(stat
& BS_STAT_DMAERR
) {
815 ubsec_totalreset(sc
);
816 ubsecstats
.hst_dmaerr
++;
821 #ifdef UBSEC_VERBOSE_DEBUG
822 DPRINTF("merging %d records\n", npkts
);
824 /* XXX temporary aggregation statistics reporting code */
827 DPRINTF("%s: new max aggregate %d\n", DRV_MODULE_NAME
, max
);
829 #endif /* UBSEC_VERBOSE_DEBUG */
831 q
= BSD_SIMPLEQ_FIRST(&sc
->sc_queue
);
832 BSD_SIMPLEQ_REMOVE_HEAD(&sc
->sc_queue
, q_next
);
838 * We use dma_map_single() - no sync required!
841 bus_dmamap_sync(sc
->sc_dmat
, q
->q_src_map
,
842 0, q
->q_src_map
->dm_mapsize
, BUS_DMASYNC_PREWRITE
);
843 if (q
->q_dst_map
!= NULL
)
844 bus_dmamap_sync(sc
->sc_dmat
, q
->q_dst_map
,
845 0, q
->q_dst_map
->dm_mapsize
, BUS_DMASYNC_PREREAD
);
848 q
->q_nstacked_mcrs
= npkts
- 1; /* Number of packets stacked */
850 for (i
= 0; i
< q
->q_nstacked_mcrs
; i
++) {
851 q2
= BSD_SIMPLEQ_FIRST(&sc
->sc_queue
);
854 bus_dmamap_sync(sc
->sc_dmat
, q2
->q_src_map
,
855 0, q2
->q_src_map
->dm_mapsize
, BUS_DMASYNC_PREWRITE
);
856 if (q2
->q_dst_map
!= NULL
)
857 bus_dmamap_sync(sc
->sc_dmat
, q2
->q_dst_map
,
858 0, q2
->q_dst_map
->dm_mapsize
, BUS_DMASYNC_PREREAD
);
860 BSD_SIMPLEQ_REMOVE_HEAD(&sc
->sc_queue
, q_next
);
863 v
= ((char *)&q2
->q_dma
->d_dma
->d_mcr
) + sizeof(struct ubsec_mcr
) -
864 sizeof(struct ubsec_mcr_add
);
865 bcopy(v
, &q
->q_dma
->d_dma
->d_mcradd
[i
], sizeof(struct ubsec_mcr_add
));
866 q
->q_stacked_mcr
[i
] = q2
;
868 q
->q_dma
->d_dma
->d_mcr
.mcr_pkts
= htole16(npkts
);
869 BSD_SIMPLEQ_INSERT_TAIL(&sc
->sc_qchip
, q
, q_next
);
871 bus_dmamap_sync(sc
->sc_dmat
, q
->q_dma
->d_alloc
.dma_map
,
872 0, q
->q_dma
->d_alloc
.dma_map
->dm_mapsize
,
873 BUS_DMASYNC_PREREAD
| BUS_DMASYNC_PREWRITE
);
875 WRITE_REG(sc
, BS_MCR1
, q
->q_dma
->d_alloc
.dma_paddr
+
876 offsetof(struct ubsec_dmachunk
, d_mcr
));
877 #ifdef UBSEC_VERBOSE_DEBUG
878 DPRINTF("feed (1): q->chip %p %08x %08x\n", q
,
879 (u_int32_t
)q
->q_dma
->d_alloc
.dma_paddr
,
880 (u_int32_t
)(q
->q_dma
->d_alloc
.dma_paddr
+
881 offsetof(struct ubsec_dmachunk
, d_mcr
)));
882 #endif /* UBSEC_DEBUG */
886 while (!BSD_SIMPLEQ_EMPTY(&sc
->sc_queue
)) {
887 stat
= READ_REG(sc
, BS_STAT
);
889 if (stat
& (BS_STAT_MCR1_FULL
| BS_STAT_DMAERR
)) {
890 if(stat
& BS_STAT_DMAERR
) {
891 ubsec_totalreset(sc
);
892 ubsecstats
.hst_dmaerr
++;
897 q
= BSD_SIMPLEQ_FIRST(&sc
->sc_queue
);
900 bus_dmamap_sync(sc
->sc_dmat
, q
->q_src_map
,
901 0, q
->q_src_map
->dm_mapsize
, BUS_DMASYNC_PREWRITE
);
902 if (q
->q_dst_map
!= NULL
)
903 bus_dmamap_sync(sc
->sc_dmat
, q
->q_dst_map
,
904 0, q
->q_dst_map
->dm_mapsize
, BUS_DMASYNC_PREREAD
);
905 bus_dmamap_sync(sc
->sc_dmat
, q
->q_dma
->d_alloc
.dma_map
,
906 0, q
->q_dma
->d_alloc
.dma_map
->dm_mapsize
,
907 BUS_DMASYNC_PREREAD
| BUS_DMASYNC_PREWRITE
);
910 WRITE_REG(sc
, BS_MCR1
, q
->q_dma
->d_alloc
.dma_paddr
+
911 offsetof(struct ubsec_dmachunk
, d_mcr
));
912 #ifdef UBSEC_VERBOSE_DEBUG
913 DPRINTF("feed (2): q->chip %p %08x %08x\n", q
,
914 (u_int32_t
)q
->q_dma
->d_alloc
.dma_paddr
,
915 (u_int32_t
)(q
->q_dma
->d_alloc
.dma_paddr
+
916 offsetof(struct ubsec_dmachunk
, d_mcr
)));
917 #endif /* UBSEC_DEBUG */
918 BSD_SIMPLEQ_REMOVE_HEAD(&sc
->sc_queue
, q_next
);
920 BSD_SIMPLEQ_INSERT_TAIL(&sc
->sc_qchip
, q
, q_next
);
925 * Allocate a new 'session' and return an encoded session id. 'sidp'
926 * contains our registration id, and should contain an encoded session
927 * id on successful allocation.
930 ubsec_newsession(device_t dev
, u_int32_t
*sidp
, struct cryptoini
*cri
)
932 struct cryptoini
*c
, *encini
= NULL
, *macini
= NULL
;
933 struct ubsec_softc
*sc
= NULL
;
934 struct ubsec_session
*ses
= NULL
;
938 DPRINTF("%s()\n", __FUNCTION__
);
941 if (sidp
== NULL
|| cri
== NULL
)
944 sc
= device_get_softc(dev
);
949 for (c
= cri
; c
!= NULL
; c
= c
->cri_next
) {
950 if (c
->cri_alg
== CRYPTO_MD5_HMAC
||
951 c
->cri_alg
== CRYPTO_SHA1_HMAC
) {
955 } else if (c
->cri_alg
== CRYPTO_DES_CBC
||
956 c
->cri_alg
== CRYPTO_3DES_CBC
||
957 c
->cri_alg
== CRYPTO_AES_CBC
) {
964 if (encini
== NULL
&& macini
== NULL
)
967 if (sc
->sc_sessions
== NULL
) {
968 ses
= sc
->sc_sessions
= (struct ubsec_session
*)kmalloc(
969 sizeof(struct ubsec_session
), SLAB_ATOMIC
);
972 memset(ses
, 0, sizeof(struct ubsec_session
));
974 sc
->sc_nsessions
= 1;
976 for (sesn
= 0; sesn
< sc
->sc_nsessions
; sesn
++) {
977 if (sc
->sc_sessions
[sesn
].ses_used
== 0) {
978 ses
= &sc
->sc_sessions
[sesn
];
984 sesn
= sc
->sc_nsessions
;
985 ses
= (struct ubsec_session
*)kmalloc((sesn
+ 1) *
986 sizeof(struct ubsec_session
), SLAB_ATOMIC
);
989 memset(ses
, 0, (sesn
+ 1) * sizeof(struct ubsec_session
));
990 bcopy(sc
->sc_sessions
, ses
, sesn
*
991 sizeof(struct ubsec_session
));
992 bzero(sc
->sc_sessions
, sesn
*
993 sizeof(struct ubsec_session
));
994 kfree(sc
->sc_sessions
);
995 sc
->sc_sessions
= ses
;
996 ses
= &sc
->sc_sessions
[sesn
];
1001 bzero(ses
, sizeof(struct ubsec_session
));
1005 /* XXX may read fewer than requested */
1006 read_random(ses
->ses_iv
, sizeof(ses
->ses_iv
));
1008 /* Go ahead and compute key in ubsec's byte order */
1009 if (encini
->cri_alg
== CRYPTO_DES_CBC
) {
1010 /* DES uses the same key three times:
1011 * 1st encrypt -> 2nd decrypt -> 3nd encrypt */
1012 bcopy(encini
->cri_key
, &ses
->ses_key
[0], 8);
1013 bcopy(encini
->cri_key
, &ses
->ses_key
[2], 8);
1014 bcopy(encini
->cri_key
, &ses
->ses_key
[4], 8);
1015 ses
->ses_keysize
= 192; /* Fake! Actually its only 64bits ..
1016 oh no it is even less: 54bits. */
1017 } else if(encini
->cri_alg
== CRYPTO_3DES_CBC
) {
1018 bcopy(encini
->cri_key
, ses
->ses_key
, 24);
1019 ses
->ses_keysize
= 192;
1020 } else if(encini
->cri_alg
== CRYPTO_AES_CBC
) {
1021 ses
->ses_keysize
= encini
->cri_klen
;
1023 if (ses
->ses_keysize
!= 128 &&
1024 ses
->ses_keysize
!= 192 &&
1025 ses
->ses_keysize
!= 256)
1027 DPRINTF("unsupported AES key size: %d\n", ses
->ses_keysize
);
1030 bcopy(encini
->cri_key
, ses
->ses_key
, (ses
->ses_keysize
/ 8));
1033 /* Hardware requires the keys in little endian byte order */
1034 for (i
=0; i
< (ses
->ses_keysize
/ 32); i
++)
1035 SWAP32(ses
->ses_key
[i
]);
1039 ses
->ses_mlen
= macini
->cri_mlen
;
1041 if (ses
->ses_mlen
== 0 ||
1042 ses
->ses_mlen
> SHA1_HASH_LEN
) {
1044 if (macini
->cri_alg
== CRYPTO_MD5_HMAC
||
1045 macini
->cri_alg
== CRYPTO_SHA1_HMAC
)
1047 ses
->ses_mlen
= DEFAULT_HMAC_LEN
;
1051 * Reserved for future usage. MD5/SHA1 calculations have
1052 * different hash sizes.
1054 printk(KERN_ERR DRV_MODULE_NAME
": unsupported hash operation with mac/hash len: %d\n", ses
->ses_mlen
);
1060 if (macini
->cri_key
!= NULL
) {
1061 ubsec_setup_mackey(ses
, macini
->cri_alg
, macini
->cri_key
,
1062 macini
->cri_klen
/ 8);
1066 *sidp
= UBSEC_SID(device_get_unit(sc
->sc_dev
), sesn
);
1071 * Deallocate a session.
1074 ubsec_freesession(device_t dev
, u_int64_t tid
)
1076 struct ubsec_softc
*sc
= device_get_softc(dev
);
1078 u_int32_t sid
= ((u_int32_t
)tid
) & 0xffffffff;
1081 DPRINTF("%s()\n", __FUNCTION__
);
1087 session
= UBSEC_SESSION(sid
);
1088 if (session
< sc
->sc_nsessions
) {
1089 bzero(&sc
->sc_sessions
[session
], sizeof(sc
->sc_sessions
[session
]));
1096 ubsec_process(device_t dev
, struct cryptop
*crp
, int hint
)
1098 struct ubsec_q
*q
= NULL
;
1099 int err
= 0, i
, j
, nicealign
;
1100 struct ubsec_softc
*sc
= device_get_softc(dev
);
1101 struct cryptodesc
*crd1
, *crd2
, *maccrd
, *enccrd
;
1102 int encoffset
= 0, macoffset
= 0, cpskip
, cpoffset
;
1103 int sskip
, dskip
, stheend
, dtheend
, ivsize
= 8;
1105 struct ubsec_session
*ses
;
1106 struct ubsec_generic_ctx ctx
;
1107 struct ubsec_dma
*dmap
= NULL
;
1108 unsigned long flags
;
1111 DPRINTF("%s()\n", __FUNCTION__
);
1114 if (unlikely(crp
== NULL
|| crp
->crp_callback
== NULL
)) {
1115 ubsecstats
.hst_invalid
++;
1119 if (unlikely(sc
== NULL
))
1122 #ifdef UBSEC_VERBOSE_DEBUG
1123 DPRINTF("spin_lock_irqsave\n");
1125 spin_lock_irqsave(&sc
->sc_ringmtx
, flags
);
1126 //spin_lock_irq(&sc->sc_ringmtx);
1128 if (BSD_SIMPLEQ_EMPTY(&sc
->sc_freequeue
)) {
1129 ubsecstats
.hst_queuefull
++;
1130 #ifdef UBSEC_VERBOSE_DEBUG
1131 DPRINTF("spin_unlock_irqrestore\n");
1133 spin_unlock_irqrestore(&sc
->sc_ringmtx
, flags
);
1134 //spin_unlock_irq(&sc->sc_ringmtx);
1139 q
= BSD_SIMPLEQ_FIRST(&sc
->sc_freequeue
);
1140 BSD_SIMPLEQ_REMOVE_HEAD(&sc
->sc_freequeue
, q_next
);
1141 #ifdef UBSEC_VERBOSE_DEBUG
1142 DPRINTF("spin_unlock_irqrestore\n");
1144 spin_unlock_irqrestore(&sc
->sc_ringmtx
, flags
);
1145 //spin_unlock_irq(&sc->sc_ringmtx);
1147 dmap
= q
->q_dma
; /* Save dma pointer */
1148 bzero(q
, sizeof(struct ubsec_q
));
1149 bzero(&ctx
, sizeof(ctx
));
1151 q
->q_sesn
= UBSEC_SESSION(crp
->crp_sid
);
1153 ses
= &sc
->sc_sessions
[q
->q_sesn
];
1155 if (crp
->crp_flags
& CRYPTO_F_SKBUF
) {
1156 q
->q_src_m
= (struct sk_buff
*)crp
->crp_buf
;
1157 q
->q_dst_m
= (struct sk_buff
*)crp
->crp_buf
;
1158 } else if (crp
->crp_flags
& CRYPTO_F_IOV
) {
1159 q
->q_src_io
= (struct uio
*)crp
->crp_buf
;
1160 q
->q_dst_io
= (struct uio
*)crp
->crp_buf
;
1163 goto errout
; /* XXX we don't handle contiguous blocks! */
1166 bzero(&dmap
->d_dma
->d_mcr
, sizeof(struct ubsec_mcr
));
1168 dmap
->d_dma
->d_mcr
.mcr_pkts
= htole16(1);
1169 dmap
->d_dma
->d_mcr
.mcr_flags
= 0;
1172 crd1
= crp
->crp_desc
;
1177 crd2
= crd1
->crd_next
;
1180 if (crd1
->crd_alg
== CRYPTO_MD5_HMAC
||
1181 crd1
->crd_alg
== CRYPTO_SHA1_HMAC
) {
1184 } else if (crd1
->crd_alg
== CRYPTO_DES_CBC
||
1185 crd1
->crd_alg
== CRYPTO_3DES_CBC
||
1186 crd1
->crd_alg
== CRYPTO_AES_CBC
) {
1194 if ((crd1
->crd_alg
== CRYPTO_MD5_HMAC
||
1195 crd1
->crd_alg
== CRYPTO_SHA1_HMAC
) &&
1196 (crd2
->crd_alg
== CRYPTO_DES_CBC
||
1197 crd2
->crd_alg
== CRYPTO_3DES_CBC
||
1198 crd2
->crd_alg
== CRYPTO_AES_CBC
) &&
1199 ((crd2
->crd_flags
& CRD_F_ENCRYPT
) == 0)) {
1202 } else if ((crd1
->crd_alg
== CRYPTO_DES_CBC
||
1203 crd1
->crd_alg
== CRYPTO_3DES_CBC
||
1204 crd1
->crd_alg
== CRYPTO_AES_CBC
) &&
1205 (crd2
->crd_alg
== CRYPTO_MD5_HMAC
||
1206 crd2
->crd_alg
== CRYPTO_SHA1_HMAC
) &&
1207 (crd1
->crd_flags
& CRD_F_ENCRYPT
)) {
1212 * We cannot order the ubsec as requested
1214 printk(KERN_ERR DRV_MODULE_NAME
": got wrong algorithm/signature order.\n");
1220 /* Encryption/Decryption requested */
1222 encoffset
= enccrd
->crd_skip
;
1224 if (enccrd
->crd_alg
== CRYPTO_DES_CBC
||
1225 enccrd
->crd_alg
== CRYPTO_3DES_CBC
)
1227 ctx
.pc_flags
|= htole16(UBS_PKTCTX_ENC_3DES
);
1228 ctx
.pc_type
= htole16(UBS_PKTCTX_TYPE_IPSEC_DES
);
1229 ivsize
= 8; /* [3]DES uses 64bit IVs */
1231 ctx
.pc_flags
|= htole16(UBS_PKTCTX_ENC_AES
);
1232 ctx
.pc_type
= htole16(UBS_PKTCTX_TYPE_IPSEC_AES
);
1233 ivsize
= 16; /* AES uses 128bit IVs / [3]DES 64bit IVs */
1235 switch(ses
->ses_keysize
)
1238 ctx
.pc_flags
|= htole16(UBS_PKTCTX_AES128
);
1241 ctx
.pc_flags
|= htole16(UBS_PKTCTX_AES192
);
1244 ctx
.pc_flags
|= htole16(UBS_PKTCTX_AES256
);
1247 DPRINTF("invalid AES key size: %d\n", ses
->ses_keysize
);
1253 if (enccrd
->crd_flags
& CRD_F_ENCRYPT
) {
1254 /* Direction: Outbound */
1256 q
->q_flags
|= UBSEC_QFLAGS_COPYOUTIV
;
1258 if (enccrd
->crd_flags
& CRD_F_IV_EXPLICIT
) {
1259 bcopy(enccrd
->crd_iv
, ctx
.pc_iv
, ivsize
);
1261 for(i
=0; i
< (ivsize
/ 4); i
++)
1262 ctx
.pc_iv
[i
] = ses
->ses_iv
[i
];
1265 /* If there is no IV in the buffer -> copy it here */
1266 if ((enccrd
->crd_flags
& CRD_F_IV_PRESENT
) == 0) {
1267 if (crp
->crp_flags
& CRYPTO_F_SKBUF
)
1269 m_copyback(q->q_src_m,
1273 crypto_copyback(crp
->crp_flags
, (caddr_t
)q
->q_src_m
,
1274 enccrd
->crd_inject
, ivsize
, (caddr_t
)ctx
.pc_iv
);
1275 else if (crp
->crp_flags
& CRYPTO_F_IOV
)
1277 cuio_copyback(q->q_src_io,
1281 crypto_copyback(crp
->crp_flags
, (caddr_t
)q
->q_src_io
,
1282 enccrd
->crd_inject
, ivsize
, (caddr_t
)ctx
.pc_iv
);
1285 /* Direction: Inbound */
1287 ctx
.pc_flags
|= htole16(UBS_PKTCTX_INBOUND
);
1289 if (enccrd
->crd_flags
& CRD_F_IV_EXPLICIT
)
1290 bcopy(enccrd
->crd_iv
, ctx
.pc_iv
, ivsize
);
1291 else if (crp
->crp_flags
& CRYPTO_F_SKBUF
)
1293 m_copydata(q->q_src_m, enccrd->crd_inject,
1294 8, (caddr_t)ctx.pc_iv);
1296 crypto_copydata(crp
->crp_flags
, (caddr_t
)q
->q_src_m
,
1297 enccrd
->crd_inject
, ivsize
,
1298 (caddr_t
)ctx
.pc_iv
);
1299 else if (crp
->crp_flags
& CRYPTO_F_IOV
)
1301 cuio_copydata(q->q_src_io,
1302 enccrd->crd_inject, 8,
1303 (caddr_t)ctx.pc_iv);
1305 crypto_copydata(crp
->crp_flags
, (caddr_t
)q
->q_src_io
,
1306 enccrd
->crd_inject
, ivsize
,
1307 (caddr_t
)ctx
.pc_iv
);
1311 /* Even though key & IV sizes differ from cipher to cipher
1312 * copy / swap the full array lengths. Let the compiler unroll
1313 * the loop to increase the cpu pipeline performance... */
1314 for(i
=0; i
< 8; i
++)
1315 ctx
.pc_key
[i
] = ses
->ses_key
[i
];
1316 for(i
=0; i
< 4; i
++)
1317 SWAP32(ctx
.pc_iv
[i
]);
1320 /* Authentication requested */
1322 macoffset
= maccrd
->crd_skip
;
1324 if (maccrd
->crd_alg
== CRYPTO_MD5_HMAC
)
1325 ctx
.pc_flags
|= htole16(UBS_PKTCTX_AUTH_MD5
);
1327 ctx
.pc_flags
|= htole16(UBS_PKTCTX_AUTH_SHA1
);
1329 for (i
= 0; i
< 5; i
++) {
1330 ctx
.pc_hminner
[i
] = ses
->ses_hminner
[i
];
1331 ctx
.pc_hmouter
[i
] = ses
->ses_hmouter
[i
];
1333 HTOLE32(ctx
.pc_hminner
[i
]);
1334 HTOLE32(ctx
.pc_hmouter
[i
]);
1338 if (enccrd
&& maccrd
) {
1340 * ubsec cannot handle packets where the end of encryption
1341 * and authentication are not the same, or where the
1342 * encrypted part begins before the authenticated part.
1344 if (((encoffset
+ enccrd
->crd_len
) !=
1345 (macoffset
+ maccrd
->crd_len
)) ||
1346 (enccrd
->crd_skip
< maccrd
->crd_skip
)) {
1350 sskip
= maccrd
->crd_skip
;
1351 cpskip
= dskip
= enccrd
->crd_skip
;
1352 stheend
= maccrd
->crd_len
;
1353 dtheend
= enccrd
->crd_len
;
1354 coffset
= enccrd
->crd_skip
- maccrd
->crd_skip
;
1355 cpoffset
= cpskip
+ dtheend
;
1357 DPRINTF("mac: skip %d, len %d, inject %d\n",
1358 maccrd
->crd_skip
, maccrd
->crd_len
, maccrd
->crd_inject
);
1359 DPRINTF("enc: skip %d, len %d, inject %d\n",
1360 enccrd
->crd_skip
, enccrd
->crd_len
, enccrd
->crd_inject
);
1361 DPRINTF("src: skip %d, len %d\n", sskip
, stheend
);
1362 DPRINTF("dst: skip %d, len %d\n", dskip
, dtheend
);
1363 DPRINTF("ubs: coffset %d, pktlen %d, cpskip %d, cpoffset %d\n",
1364 coffset
, stheend
, cpskip
, cpoffset
);
1367 cpskip
= dskip
= sskip
= macoffset
+ encoffset
;
1368 dtheend
= stheend
= (enccrd
)?enccrd
->crd_len
:maccrd
->crd_len
;
1369 cpoffset
= cpskip
+ dtheend
;
1372 ctx
.pc_offset
= htole16(coffset
>> 2);
1375 if (bus_dmamap_create(sc
->sc_dmat
, 0xfff0, UBS_MAX_SCATTER
,
1376 0xfff0, 0, BUS_DMA_NOWAIT
, &q
->q_src_map
) != 0) {
1382 if (crp
->crp_flags
& CRYPTO_F_SKBUF
) {
1384 if (bus_dmamap_load_mbuf(sc
->sc_dmat
, q
->q_src_map
,
1385 q
->q_src_m
, BUS_DMA_NOWAIT
) != 0) {
1386 bus_dmamap_destroy(sc
->sc_dmat
, q
->q_src_map
);
1387 q
->q_src_map
= NULL
;
1392 err
= dma_map_skb(sc
, q
->q_src_map
, q
->q_src_m
, &q
->q_src_len
);
1393 if (unlikely(err
!= 0))
1396 } else if (crp
->crp_flags
& CRYPTO_F_IOV
) {
1398 if (bus_dmamap_load_uio(sc
->sc_dmat
, q
->q_src_map
,
1399 q
->q_src_io
, BUS_DMA_NOWAIT
) != 0) {
1400 bus_dmamap_destroy(sc
->sc_dmat
, q
->q_src_map
);
1401 q
->q_src_map
= NULL
;
1406 err
= dma_map_uio(sc
, q
->q_src_map
, q
->q_src_io
, &q
->q_src_len
);
1407 if (unlikely(err
!= 0))
1414 nicealign
= ubsec_dmamap_aligned(sc
, q
->q_src_map
, q
->q_src_len
);
1416 dmap
->d_dma
->d_mcr
.mcr_pktlen
= htole16(stheend
);
1419 DPRINTF("src skip: %d\n", sskip
);
1421 for (i
= j
= 0; i
< q
->q_src_len
; i
++) {
1422 struct ubsec_pktbuf
*pb
;
1423 size_t packl
= q
->q_src_map
[i
].dma_size
;
1424 dma_addr_t packp
= q
->q_src_map
[i
].dma_paddr
;
1426 if (sskip
>= packl
) {
1435 /* maximum fragment size is 0xfffc */
1436 if (packl
> 0xfffc) {
1437 DPRINTF("Error: fragment size is bigger than 0xfffc.\n");
1443 pb
= &dmap
->d_dma
->d_mcr
.mcr_ipktbuf
;
1445 pb
= &dmap
->d_dma
->d_sbuf
[j
- 1];
1447 pb
->pb_addr
= htole32(packp
);
1450 if (packl
> stheend
) {
1451 pb
->pb_len
= htole32(stheend
);
1454 pb
->pb_len
= htole32(packl
);
1458 pb
->pb_len
= htole32(packl
);
1460 if ((i
+ 1) == q
->q_src_len
)
1463 pb
->pb_next
= htole32(dmap
->d_alloc
.dma_paddr
+
1464 offsetof(struct ubsec_dmachunk
, d_sbuf
[j
]));
1468 if (enccrd
== NULL
&& maccrd
!= NULL
) {
1469 /* Authentication only */
1470 dmap
->d_dma
->d_mcr
.mcr_opktbuf
.pb_addr
= 0;
1471 dmap
->d_dma
->d_mcr
.mcr_opktbuf
.pb_len
= 0;
1472 dmap
->d_dma
->d_mcr
.mcr_opktbuf
.pb_next
=
1473 htole32(dmap
->d_alloc
.dma_paddr
+
1474 offsetof(struct ubsec_dmachunk
, d_macbuf
[0]));
1476 DPRINTF("opkt: %x %x %x\n",
1477 dmap
->d_dma
->d_mcr
.mcr_opktbuf
.pb_addr
,
1478 dmap
->d_dma
->d_mcr
.mcr_opktbuf
.pb_len
,
1479 dmap
->d_dma
->d_mcr
.mcr_opktbuf
.pb_next
);
1482 if (crp
->crp_flags
& CRYPTO_F_IOV
) {
1488 if (bus_dmamap_create(sc
->sc_dmat
, 0xfff0,
1489 UBS_MAX_SCATTER
, 0xfff0, 0, BUS_DMA_NOWAIT
,
1490 &q
->q_dst_map
) != 0) {
1494 if (bus_dmamap_load_uio(sc
->sc_dmat
, q
->q_dst_map
,
1495 q
->q_dst_io
, BUS_DMA_NOWAIT
) != 0) {
1496 bus_dmamap_destroy(sc
->sc_dmat
, q
->q_dst_map
);
1497 q
->q_dst_map
= NULL
;
1502 /* HW shall copy the result into the source memory */
1503 for(i
= 0; i
< q
->q_src_len
; i
++)
1504 q
->q_dst_map
[i
] = q
->q_src_map
[i
];
1506 q
->q_dst_len
= q
->q_src_len
;
1509 } else if (crp
->crp_flags
& CRYPTO_F_SKBUF
) {
1512 /* HW shall copy the result into the source memory */
1513 q
->q_dst_m
= q
->q_src_m
;
1514 for(i
= 0; i
< q
->q_src_len
; i
++)
1515 q
->q_dst_map
[i
] = q
->q_src_map
[i
];
1517 q
->q_dst_len
= q
->q_src_len
;
1523 struct sk_buff
*m
, *top
, **mp
;
1525 totlen
= q
->q_src_map
->dm_mapsize
;
1526 if (q
->q_src_m
->m_flags
& M_PKTHDR
) {
1528 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
1531 MGET(m
, M_DONTWAIT
, MT_DATA
);
1538 M_DUP_PKTHDR(m
, q
->q_src_m
);
1539 if (totlen
>= MINCLSIZE
) {
1540 MCLGET(m
, M_DONTWAIT
);
1541 if (m
->m_flags
& M_EXT
)
1548 while (totlen
> 0) {
1550 MGET(m
, M_DONTWAIT
, MT_DATA
);
1558 if (top
&& totlen
>= MINCLSIZE
) {
1559 MCLGET(m
, M_DONTWAIT
);
1560 if (m
->m_flags
& M_EXT
)
1563 m
->m_len
= len
= min(totlen
, len
);
1569 ubsec_mcopy(q
->q_src_m
, q
->q_dst_m
,
1571 if (bus_dmamap_create(sc
->sc_dmat
, 0xfff0,
1572 UBS_MAX_SCATTER
, 0xfff0, 0, BUS_DMA_NOWAIT
,
1573 &q
->q_dst_map
) != 0) {
1577 if (bus_dmamap_load_mbuf(sc
->sc_dmat
,
1578 q
->q_dst_map
, q
->q_dst_m
,
1579 BUS_DMA_NOWAIT
) != 0) {
1580 bus_dmamap_destroy(sc
->sc_dmat
,
1582 q
->q_dst_map
= NULL
;
1587 device_printf(sc
->sc_dev
,
1588 "%s,%d: CRYPTO_F_SKBUF unaligned not implemented\n",
1589 __FILE__
, __LINE__
);
1600 DPRINTF("dst skip: %d\n", dskip
);
1602 for (i
= j
= 0; i
< q
->q_dst_len
; i
++) {
1603 struct ubsec_pktbuf
*pb
;
1604 size_t packl
= q
->q_dst_map
[i
].dma_size
;
1605 dma_addr_t packp
= q
->q_dst_map
[i
].dma_paddr
;
1607 if (dskip
>= packl
) {
1616 if (packl
> 0xfffc) {
1617 DPRINTF("Error: fragment size is bigger than 0xfffc.\n");
1623 pb
= &dmap
->d_dma
->d_mcr
.mcr_opktbuf
;
1625 pb
= &dmap
->d_dma
->d_dbuf
[j
- 1];
1627 pb
->pb_addr
= htole32(packp
);
1630 if (packl
> dtheend
) {
1631 pb
->pb_len
= htole32(dtheend
);
1634 pb
->pb_len
= htole32(packl
);
1638 pb
->pb_len
= htole32(packl
);
1640 if ((i
+ 1) == q
->q_dst_len
) {
1643 * The last fragment of the output buffer
1644 * contains the HMAC. */
1645 pb
->pb_next
= htole32(dmap
->d_alloc
.dma_paddr
+
1646 offsetof(struct ubsec_dmachunk
, d_macbuf
[0]));
1650 pb
->pb_next
= htole32(dmap
->d_alloc
.dma_paddr
+
1651 offsetof(struct ubsec_dmachunk
, d_dbuf
[j
]));
1656 dmap
->d_dma
->d_mcr
.mcr_cmdctxp
= htole32(dmap
->d_alloc
.dma_paddr
+
1657 offsetof(struct ubsec_dmachunk
, d_ctx
));
1659 if (sc
->sc_flags
& UBS_FLAGS_LONGCTX
) {
1660 /* new Broadcom cards with dynamic long command context structure */
1662 if (enccrd
!= NULL
&&
1663 enccrd
->crd_alg
== CRYPTO_AES_CBC
)
1665 struct ubsec_pktctx_aes128
*ctxaes128
;
1666 struct ubsec_pktctx_aes192
*ctxaes192
;
1667 struct ubsec_pktctx_aes256
*ctxaes256
;
1669 switch(ses
->ses_keysize
)
1673 ctxaes128
= (struct ubsec_pktctx_aes128
*)
1674 (dmap
->d_alloc
.dma_vaddr
+
1675 offsetof(struct ubsec_dmachunk
, d_ctx
));
1677 ctxaes128
->pc_len
= htole16(sizeof(struct ubsec_pktctx_aes128
));
1678 ctxaes128
->pc_type
= ctx
.pc_type
;
1679 ctxaes128
->pc_flags
= ctx
.pc_flags
;
1680 ctxaes128
->pc_offset
= ctx
.pc_offset
;
1681 for (i
= 0; i
< 4; i
++)
1682 ctxaes128
->pc_aeskey
[i
] = ctx
.pc_key
[i
];
1683 for (i
= 0; i
< 5; i
++)
1684 ctxaes128
->pc_hminner
[i
] = ctx
.pc_hminner
[i
];
1685 for (i
= 0; i
< 5; i
++)
1686 ctxaes128
->pc_hmouter
[i
] = ctx
.pc_hmouter
[i
];
1687 for (i
= 0; i
< 4; i
++)
1688 ctxaes128
->pc_iv
[i
] = ctx
.pc_iv
[i
];
1693 ctxaes192
= (struct ubsec_pktctx_aes192
*)
1694 (dmap
->d_alloc
.dma_vaddr
+
1695 offsetof(struct ubsec_dmachunk
, d_ctx
));
1697 ctxaes192
->pc_len
= htole16(sizeof(struct ubsec_pktctx_aes192
));
1698 ctxaes192
->pc_type
= ctx
.pc_type
;
1699 ctxaes192
->pc_flags
= ctx
.pc_flags
;
1700 ctxaes192
->pc_offset
= ctx
.pc_offset
;
1701 for (i
= 0; i
< 6; i
++)
1702 ctxaes192
->pc_aeskey
[i
] = ctx
.pc_key
[i
];
1703 for (i
= 0; i
< 5; i
++)
1704 ctxaes192
->pc_hminner
[i
] = ctx
.pc_hminner
[i
];
1705 for (i
= 0; i
< 5; i
++)
1706 ctxaes192
->pc_hmouter
[i
] = ctx
.pc_hmouter
[i
];
1707 for (i
= 0; i
< 4; i
++)
1708 ctxaes192
->pc_iv
[i
] = ctx
.pc_iv
[i
];
1713 ctxaes256
= (struct ubsec_pktctx_aes256
*)
1714 (dmap
->d_alloc
.dma_vaddr
+
1715 offsetof(struct ubsec_dmachunk
, d_ctx
));
1717 ctxaes256
->pc_len
= htole16(sizeof(struct ubsec_pktctx_aes256
));
1718 ctxaes256
->pc_type
= ctx
.pc_type
;
1719 ctxaes256
->pc_flags
= ctx
.pc_flags
;
1720 ctxaes256
->pc_offset
= ctx
.pc_offset
;
1721 for (i
= 0; i
< 8; i
++)
1722 ctxaes256
->pc_aeskey
[i
] = ctx
.pc_key
[i
];
1723 for (i
= 0; i
< 5; i
++)
1724 ctxaes256
->pc_hminner
[i
] = ctx
.pc_hminner
[i
];
1725 for (i
= 0; i
< 5; i
++)
1726 ctxaes256
->pc_hmouter
[i
] = ctx
.pc_hmouter
[i
];
1727 for (i
= 0; i
< 4; i
++)
1728 ctxaes256
->pc_iv
[i
] = ctx
.pc_iv
[i
];
1734 * [3]DES / MD5_HMAC / SHA1_HMAC
1736 * MD5_HMAC / SHA1_HMAC can use the IPSEC 3DES operation without
1739 struct ubsec_pktctx_des
*ctxdes
;
1741 ctxdes
= (struct ubsec_pktctx_des
*)(dmap
->d_alloc
.dma_vaddr
+
1742 offsetof(struct ubsec_dmachunk
, d_ctx
));
1744 ctxdes
->pc_len
= htole16(sizeof(struct ubsec_pktctx_des
));
1745 ctxdes
->pc_type
= ctx
.pc_type
;
1746 ctxdes
->pc_flags
= ctx
.pc_flags
;
1747 ctxdes
->pc_offset
= ctx
.pc_offset
;
1748 for (i
= 0; i
< 6; i
++)
1749 ctxdes
->pc_deskey
[i
] = ctx
.pc_key
[i
];
1750 for (i
= 0; i
< 5; i
++)
1751 ctxdes
->pc_hminner
[i
] = ctx
.pc_hminner
[i
];
1752 for (i
= 0; i
< 5; i
++)
1753 ctxdes
->pc_hmouter
[i
] = ctx
.pc_hmouter
[i
];
1754 ctxdes
->pc_iv
[0] = ctx
.pc_iv
[0];
1755 ctxdes
->pc_iv
[1] = ctx
.pc_iv
[1];
1759 /* old Broadcom card with fixed small command context structure */
1762 * [3]DES / MD5_HMAC / SHA1_HMAC
1764 struct ubsec_pktctx
*ctxs
;
1766 ctxs
= (struct ubsec_pktctx
*)(dmap
->d_alloc
.dma_vaddr
+
1767 offsetof(struct ubsec_dmachunk
, d_ctx
));
1769 /* transform generic context into small context */
1770 for (i
= 0; i
< 6; i
++)
1771 ctxs
->pc_deskey
[i
] = ctx
.pc_key
[i
];
1772 for (i
= 0; i
< 5; i
++)
1773 ctxs
->pc_hminner
[i
] = ctx
.pc_hminner
[i
];
1774 for (i
= 0; i
< 5; i
++)
1775 ctxs
->pc_hmouter
[i
] = ctx
.pc_hmouter
[i
];
1776 ctxs
->pc_iv
[0] = ctx
.pc_iv
[0];
1777 ctxs
->pc_iv
[1] = ctx
.pc_iv
[1];
1778 ctxs
->pc_flags
= ctx
.pc_flags
;
1779 ctxs
->pc_offset
= ctx
.pc_offset
;
1782 #ifdef UBSEC_VERBOSE_DEBUG
1783 DPRINTF("spin_lock_irqsave\n");
1785 spin_lock_irqsave(&sc
->sc_ringmtx
, flags
);
1786 //spin_lock_irq(&sc->sc_ringmtx);
1788 BSD_SIMPLEQ_INSERT_TAIL(&sc
->sc_queue
, q
, q_next
);
1790 ubsecstats
.hst_ipackets
++;
1791 ubsecstats
.hst_ibytes
+= stheend
;
1794 #ifdef UBSEC_VERBOSE_DEBUG
1795 DPRINTF("spin_unlock_irqrestore\n");
1797 spin_unlock_irqrestore(&sc
->sc_ringmtx
, flags
);
1798 //spin_unlock_irq(&sc->sc_ringmtx);
1805 if ((q
->q_dst_m
!= NULL
) && (q
->q_src_m
!= q
->q_dst_m
))
1806 m_freem(q
->q_dst_m
);
1809 if ((q
->q_has_dst
== 1) && q
->q_dst_len
> 0) {
1811 bus_dmamap_unload(sc
->sc_dmat
, q
->q_dst_map
);
1812 bus_dmamap_destroy(sc
->sc_dmat
, q
->q_dst_map
);
1814 dma_unmap(sc
, q
->q_dst_map
, q
->q_dst_len
);
1816 if (q
->q_src_len
> 0) {
1818 bus_dmamap_unload(sc
->sc_dmat
, q
->q_src_map
);
1819 bus_dmamap_destroy(sc
->sc_dmat
, q
->q_src_map
);
1821 dma_unmap(sc
, q
->q_src_map
, q
->q_src_len
);
1824 #ifdef UBSEC_VERBOSE_DEBUG
1825 DPRINTF("spin_lock_irqsave\n");
1827 spin_lock_irqsave(&sc
->sc_ringmtx
, flags
);
1828 //spin_lock_irq(&sc->sc_ringmtx);
1830 BSD_SIMPLEQ_INSERT_TAIL(&sc
->sc_freequeue
, q
, q_next
);
1832 #ifdef UBSEC_VERBOSE_DEBUG
1833 DPRINTF("spin_unlock_irqrestore\n");
1835 spin_unlock_irqrestore(&sc
->sc_ringmtx
, flags
);
1836 //spin_unlock_irq(&sc->sc_ringmtx);
1840 ubsecstats
.hst_invalid
++;
1842 ubsecstats
.hst_nomem
++;
1844 crp
->crp_etype
= err
;
1848 DPRINTF("%s() err = %x\n", __FUNCTION__
, err
);
1855 ubsec_callback(struct ubsec_softc
*sc
, struct ubsec_q
*q
)
1857 struct cryptop
*crp
= (struct cryptop
*)q
->q_crp
;
1858 struct cryptodesc
*crd
;
1859 struct ubsec_dma
*dmap
= q
->q_dma
;
1863 DPRINTF("%s()\n", __FUNCTION__
);
1866 ubsecstats
.hst_opackets
++;
1867 ubsecstats
.hst_obytes
+= dmap
->d_alloc
.dma_size
;
1870 bus_dmamap_sync(sc
->sc_dmat
, dmap
->d_alloc
.dma_map
, 0,
1871 dmap
->d_alloc
.dma_map
->dm_mapsize
,
1872 BUS_DMASYNC_POSTREAD
|BUS_DMASYNC_POSTWRITE
);
1873 if (q
->q_dst_map
!= NULL
&& q
->q_dst_map
!= q
->q_src_map
) {
1874 bus_dmamap_sync(sc
->sc_dmat
, q
->q_dst_map
,
1875 0, q
->q_dst_map
->dm_mapsize
, BUS_DMASYNC_POSTREAD
);
1876 bus_dmamap_unload(sc
->sc_dmat
, q
->q_dst_map
);
1877 bus_dmamap_destroy(sc
->sc_dmat
, q
->q_dst_map
);
1879 bus_dmamap_sync(sc
->sc_dmat
, q
->q_src_map
,
1880 0, q
->q_src_map
->dm_mapsize
, BUS_DMASYNC_POSTWRITE
);
1881 bus_dmamap_unload(sc
->sc_dmat
, q
->q_src_map
);
1882 bus_dmamap_destroy(sc
->sc_dmat
, q
->q_src_map
);
1885 if ((q
->q_has_dst
== 1) && q
->q_dst_len
> 0)
1886 dma_unmap(sc
, q
->q_dst_map
, q
->q_dst_len
);
1888 dma_unmap(sc
, q
->q_src_map
, q
->q_src_len
);
1891 if ((crp
->crp_flags
& CRYPTO_F_SKBUF
) && (q
->q_src_m
!= q
->q_dst_m
)) {
1892 m_freem(q
->q_src_m
);
1893 crp
->crp_buf
= (caddr_t
)q
->q_dst_m
;
1897 /* copy out IV for future use */
1898 if (q
->q_flags
& UBSEC_QFLAGS_COPYOUTIV
) {
1899 for (crd
= crp
->crp_desc
; crd
; crd
= crd
->crd_next
) {
1900 if (crd
->crd_alg
!= CRYPTO_DES_CBC
&&
1901 crd
->crd_alg
!= CRYPTO_3DES_CBC
&&
1902 crd
->crd_alg
!= CRYPTO_AES_CBC
)
1905 if (crd
->crd_alg
== CRYPTO_AES_CBC
)
1910 if (crp
->crp_flags
& CRYPTO_F_SKBUF
)
1912 m_copydata((struct sk_buff
*)crp
->crp_buf
,
1913 crd
->crd_skip
+ crd
->crd_len
- 8, 8,
1914 (caddr_t
)sc
->sc_sessions
[q
->q_sesn
].ses_iv
);
1916 crypto_copydata(crp
->crp_flags
, (caddr_t
)crp
->crp_buf
,
1917 crd
->crd_skip
+ crd
->crd_len
- ivsize
, ivsize
,
1918 (caddr_t
)sc
->sc_sessions
[q
->q_sesn
].ses_iv
);
1920 else if (crp
->crp_flags
& CRYPTO_F_IOV
) {
1922 cuio_copydata((struct uio
*)crp
->crp_buf
,
1923 crd
->crd_skip
+ crd
->crd_len
- 8, 8,
1924 (caddr_t
)sc
->sc_sessions
[q
->q_sesn
].ses_iv
);
1926 crypto_copydata(crp
->crp_flags
, (caddr_t
)crp
->crp_buf
,
1927 crd
->crd_skip
+ crd
->crd_len
- ivsize
, ivsize
,
1928 (caddr_t
)sc
->sc_sessions
[q
->q_sesn
].ses_iv
);
1935 for (crd
= crp
->crp_desc
; crd
; crd
= crd
->crd_next
) {
1936 if (crd
->crd_alg
!= CRYPTO_MD5_HMAC
&&
1937 crd
->crd_alg
!= CRYPTO_SHA1_HMAC
)
1940 if (crp
->crp_flags
& CRYPTO_F_SKBUF
)
1941 m_copyback((struct sk_buff
*)crp
->crp_buf
,
1942 crd
->crd_inject
, 12,
1943 dmap
->d_dma
->d_macbuf
);
1946 /* BUG? it does not honor the mac len.. */
1947 crypto_copyback(crp
->crp_flags
, crp
->crp_buf
,
1948 crd
->crd_inject
, 12,
1949 (caddr_t
)dmap
->d_dma
->d_macbuf
);
1951 crypto_copyback(crp
->crp_flags
, crp
->crp_buf
,
1953 sc
->sc_sessions
[q
->q_sesn
].ses_mlen
,
1954 (caddr_t
)dmap
->d_dma
->d_macbuf
);
1956 else if (crp
->crp_flags
& CRYPTO_F_IOV
&& crp
->crp_mac
)
1957 bcopy((caddr_t
)dmap
->d_dma
->d_macbuf
,
1962 BSD_SIMPLEQ_INSERT_TAIL(&sc
->sc_freequeue
, q
, q_next
);
1967 ubsec_mcopy(struct sk_buff
*srcm
, struct sk_buff
*dstm
, int hoffset
, int toffset
)
1969 int i
, j
, dlen
, slen
;
1979 for (i
= 0; i
< min(slen
, dlen
); i
++) {
1980 if (j
< hoffset
|| j
>= toffset
)
2004 ubsec_dma_malloc(struct ubsec_softc
*sc
, struct ubsec_dma_alloc
*dma
,
2005 size_t size
, int mapflags
)
2007 dma
->dma_vaddr
= dma_alloc_coherent(sc
->sc_dv
,
2008 size
, &dma
->dma_paddr
, GFP_KERNEL
);
2010 if (likely(dma
->dma_vaddr
))
2012 dma
->dma_size
= size
;
2016 DPRINTF("could not allocate %d bytes of coherent memory.\n", size
);
2022 ubsec_dma_free(struct ubsec_softc
*sc
, struct ubsec_dma_alloc
*dma
)
2024 dma_free_coherent(sc
->sc_dv
, dma
->dma_size
, dma
->dma_vaddr
,
2029 * Resets the board. Values in the regesters are left as is
2030 * from the reset (i.e. initial values are assigned elsewhere).
2033 ubsec_reset_board(struct ubsec_softc
*sc
)
2035 volatile u_int32_t ctrl
;
2038 DPRINTF("%s()\n", __FUNCTION__
);
2040 DPRINTF("Send reset signal to chip.\n");
2042 ctrl
= READ_REG(sc
, BS_CTRL
);
2043 ctrl
|= BS_CTRL_RESET
;
2044 WRITE_REG(sc
, BS_CTRL
, ctrl
);
2047 * Wait aprox. 30 PCI clocks = 900 ns = 0.9 us
2053 * Init Broadcom registers
2056 ubsec_init_board(struct ubsec_softc
*sc
)
2061 DPRINTF("%s()\n", __FUNCTION__
);
2063 DPRINTF("Initialize chip.\n");
2065 ctrl
= READ_REG(sc
, BS_CTRL
);
2066 ctrl
&= ~(BS_CTRL_BE32
| BS_CTRL_BE64
);
2067 ctrl
|= BS_CTRL_LITTLE_ENDIAN
| BS_CTRL_MCR1INT
| BS_CTRL_DMAERR
;
2069 WRITE_REG(sc
, BS_CTRL
, ctrl
);
2071 /* Set chip capabilities (BCM5365P) */
2072 sc
->sc_flags
|= UBS_FLAGS_LONGCTX
| UBS_FLAGS_AES
;
2076 * Clean up after a chip crash.
2077 * It is assumed that the caller has spin_lock_irq(sc_ringmtx).
2080 ubsec_cleanchip(struct ubsec_softc
*sc
)
2085 DPRINTF("%s()\n", __FUNCTION__
);
2087 DPRINTF("Clean up queues after chip crash.\n");
2089 while (!BSD_SIMPLEQ_EMPTY(&sc
->sc_qchip
)) {
2090 q
= BSD_SIMPLEQ_FIRST(&sc
->sc_qchip
);
2091 BSD_SIMPLEQ_REMOVE_HEAD(&sc
->sc_qchip
, q_next
);
2092 ubsec_free_q(sc
, q
);
2098 * It is assumed that the caller has spin_lock_irq(sc_ringmtx).
2101 ubsec_free_q(struct ubsec_softc
*sc
, struct ubsec_q
*q
)
2104 struct cryptop
*crp
;
2109 DPRINTF("%s()\n", __FUNCTION__
);
2112 npkts
= q
->q_nstacked_mcrs
;
2114 for (i
= 0; i
< npkts
; i
++) {
2115 if(q
->q_stacked_mcr
[i
]) {
2116 q2
= q
->q_stacked_mcr
[i
];
2118 if ((q2
->q_dst_m
!= NULL
) && (q2
->q_src_m
!= q2
->q_dst_m
))
2120 m_freem(q2
->q_dst_m
);
2122 printk(KERN_ERR
"%s,%d: SKB not supported\n", __FILE__
, __LINE__
);
2125 crp
= (struct cryptop
*)q2
->q_crp
;
2127 BSD_SIMPLEQ_INSERT_TAIL(&sc
->sc_freequeue
, q2
, q_next
);
2129 crp
->crp_etype
= EFAULT
;
2139 if ((q
->q_dst_m
!= NULL
) && (q
->q_src_m
!= q
->q_dst_m
))
2141 m_freem(q
->q_dst_m
);
2143 printk(KERN_ERR
"%s,%d: SKB not supported\n", __FILE__
, __LINE__
);
2146 crp
= (struct cryptop
*)q
->q_crp
;
2148 BSD_SIMPLEQ_INSERT_TAIL(&sc
->sc_freequeue
, q
, q_next
);
2150 crp
->crp_etype
= EFAULT
;
2156 * Routine to reset the chip and clean up.
2157 * It is assumed that the caller has spin_lock_irq(sc_ringmtx).
2160 ubsec_totalreset(struct ubsec_softc
*sc
)
2164 DPRINTF("%s()\n", __FUNCTION__
);
2166 DPRINTF("initiate total chip reset.. \n");
2167 ubsec_reset_board(sc
);
2168 ubsec_init_board(sc
);
2169 ubsec_cleanchip(sc
);
2173 ubsec_dump_pb(struct ubsec_pktbuf
*pb
)
2175 printf("addr 0x%x (0x%x) next 0x%x\n",
2176 pb
->pb_addr
, pb
->pb_len
, pb
->pb_next
);
2180 ubsec_dump_mcr(struct ubsec_mcr
*mcr
)
2182 struct ubsec_mcr_add
*ma
;
2186 printf(" pkts: %u, flags 0x%x\n",
2187 letoh16(mcr
->mcr_pkts
), letoh16(mcr
->mcr_flags
));
2188 ma
= (struct ubsec_mcr_add
*)&mcr
->mcr_cmdctxp
;
2189 for (i
= 0; i
< letoh16(mcr
->mcr_pkts
); i
++) {
2190 printf(" %d: ctx 0x%x len 0x%x rsvd 0x%x\n", i
,
2191 letoh32(ma
->mcr_cmdctxp
), letoh16(ma
->mcr_pktlen
),
2192 letoh16(ma
->mcr_reserved
));
2193 printf(" %d: ipkt ", i
);
2194 ubsec_dump_pb(&ma
->mcr_ipktbuf
);
2195 printf(" %d: opkt ", i
);
2196 ubsec_dump_pb(&ma
->mcr_opktbuf
);
2199 printf("END MCR\n");
2202 static int __init
mod_init(void) {
2203 return ssb_driver_register(&ubsec_ssb_driver
);
2206 static void __exit
mod_exit(void) {
2207 ssb_driver_unregister(&ubsec_ssb_driver
);
2210 module_init(mod_init
);
2211 module_exit(mod_exit
);
2214 MODULE_AUTHOR("Daniel Mueller <daniel@danm.de>");
2215 MODULE_LICENSE("BSD");
2216 MODULE_DESCRIPTION("OCF driver for BCM5365P IPSec Core");
2217 MODULE_VERSION(DRV_MODULE_VERSION
);