4 * Copyright (c) 2008 Daniel Mueller (daniel@danm.de)
5 * Copyright (c) 2007 David McCullough (david_mccullough@securecomputing.com)
6 * Copyright (c) 2000 Jason L. Wright (jason@thought.net)
7 * Copyright (c) 2000 Theo de Raadt (deraadt@openbsd.org)
8 * Copyright (c) 2001 Patrik Lindergren (patrik@ipunplugged.com)
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 * Effort sponsored in part by the Defense Advanced Research Projects
32 * Agency (DARPA) and Air Force Research Laboratory, Air Force
33 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
37 #undef UBSEC_VERBOSE_DEBUG
39 #ifdef UBSEC_VERBOSE_DEBUG
44 * uBsec BCM5365 hardware crypto accelerator
47 #include <linux/kernel.h>
48 #include <linux/module.h>
49 #include <linux/moduleparam.h>
50 #include <linux/proc_fs.h>
51 #include <linux/types.h>
52 #include <linux/init.h>
53 #include <linux/delay.h>
54 #include <linux/interrupt.h>
56 #include <linux/random.h>
57 #include <linux/skbuff.h>
58 #include <linux/stat.h>
61 #include <linux/ssb/ssb.h>
71 #include "cryptodev.h"
87 #define DRV_MODULE_NAME "ubsec_ssb"
88 #define PFX DRV_MODULE_NAME ": "
89 #define DRV_MODULE_VERSION "0.02"
90 #define DRV_MODULE_RELDATE "Feb 21, 2009"
93 #define DPRINTF(a...) \
96 printk(DRV_MODULE_NAME ": " a); \
105 static irqreturn_t
ubsec_ssb_isr(int, void *, struct pt_regs
*);
106 static int __devinit
ubsec_ssb_probe(struct ssb_device
*sdev
,
107 const struct ssb_device_id
*ent
);
108 static void __devexit
ubsec_ssb_remove(struct ssb_device
*sdev
);
109 int ubsec_attach(struct ssb_device
*sdev
, const struct ssb_device_id
*ent
,
110 struct device
*self
);
111 static void ubsec_setup_mackey(struct ubsec_session
*ses
, int algo
,
112 caddr_t key
, int klen
);
113 static int dma_map_skb(struct ubsec_softc
*sc
,
114 struct ubsec_dma_alloc
* q_map
, struct sk_buff
*skb
, int *mlen
);
115 static int dma_map_uio(struct ubsec_softc
*sc
,
116 struct ubsec_dma_alloc
*q_map
, struct uio
*uio
, int *mlen
);
117 static void dma_unmap(struct ubsec_softc
*sc
,
118 struct ubsec_dma_alloc
*q_map
, int mlen
);
119 static int ubsec_dmamap_aligned(struct ubsec_softc
*sc
,
120 const struct ubsec_dma_alloc
*q_map
, int mlen
);
123 static int proc_read(char *buf
, char **start
, off_t offset
,
124 int size
, int *peof
, void *data
);
127 void ubsec_reset_board(struct ubsec_softc
*);
128 void ubsec_init_board(struct ubsec_softc
*);
129 void ubsec_cleanchip(struct ubsec_softc
*);
130 void ubsec_totalreset(struct ubsec_softc
*);
131 int ubsec_free_q(struct ubsec_softc
*, struct ubsec_q
*);
133 static int ubsec_newsession(device_t
, u_int32_t
*, struct cryptoini
*);
134 static int ubsec_freesession(device_t
, u_int64_t
);
135 static int ubsec_process(device_t
, struct cryptop
*, int);
137 void ubsec_callback(struct ubsec_softc
*, struct ubsec_q
*);
138 void ubsec_feed(struct ubsec_softc
*);
139 void ubsec_mcopy(struct sk_buff
*, struct sk_buff
*, int, int);
140 void ubsec_dma_free(struct ubsec_softc
*, struct ubsec_dma_alloc
*);
141 int ubsec_dma_malloc(struct ubsec_softc
*, struct ubsec_dma_alloc
*,
145 void ubsec_dump_pb(struct ubsec_pktbuf
*);
146 void ubsec_dump_mcr(struct ubsec_mcr
*);
148 #define READ_REG(sc,r) \
149 ssb_read32((sc)->sdev, (r));
150 #define WRITE_REG(sc,r,val) \
151 ssb_write32((sc)->sdev, (r), (val));
152 #define READ_REG_SDEV(sdev,r) \
153 ssb_read32((sdev), (r));
154 #define WRITE_REG_SDEV(sdev,r,val) \
155 ssb_write32((sdev), (r), (val));
157 #define SWAP32(x) (x) = htole32(ntohl((x)))
158 #define HTOLE32(x) (x) = htole32(x)
160 #ifdef __LITTLE_ENDIAN
161 #define letoh16(x) (x)
162 #define letoh32(x) (x)
166 module_param(debug
, int, 0644);
167 MODULE_PARM_DESC(debug
, "Enable debug output");
169 #define UBSEC_SSB_MAX_CHIPS 1
170 static struct ubsec_softc
*ubsec_chip_idx
[UBSEC_SSB_MAX_CHIPS
];
171 static struct ubsec_stats ubsecstats
;
174 static struct proc_dir_entry
*procdebug
;
177 static struct ssb_device_id ubsec_ssb_tbl
[] = {
178 /* Broadcom BCM5365P IPSec Core */
179 SSB_DEVICE(SSB_VENDOR_BROADCOM
, SSB_DEV_IPSEC
, SSB_ANY_REV
),
183 static struct ssb_driver ubsec_ssb_driver
= {
184 .name
= DRV_MODULE_NAME
,
185 .id_table
= ubsec_ssb_tbl
,
186 .probe
= ubsec_ssb_probe
,
187 .remove
= __devexit_p(ubsec_ssb_remove
),
189 .suspend = ubsec_ssb_suspend,
190 .resume = ubsec_ssb_resume
194 static device_method_t ubsec_ssb_methods
= {
195 /* crypto device methods */
196 DEVMETHOD(cryptodev_newsession
, ubsec_newsession
),
197 DEVMETHOD(cryptodev_freesession
,ubsec_freesession
),
198 DEVMETHOD(cryptodev_process
, ubsec_process
),
203 proc_read(char *buf
, char **start
, off_t offset
,
204 int size
, int *peof
, void *data
)
206 int i
= 0, byteswritten
= 0, ret
;
207 unsigned int stat
, ctrl
;
208 #ifdef UBSEC_VERBOSE_DEBUG
210 struct ubsec_dma
*dmap
;
213 while ((i
< UBSEC_SSB_MAX_CHIPS
) && (ubsec_chip_idx
[i
] != NULL
))
215 struct ubsec_softc
*sc
= ubsec_chip_idx
[i
];
217 stat
= READ_REG(sc
, BS_STAT
);
218 ctrl
= READ_REG(sc
, BS_CTRL
);
219 ret
= snprintf((buf
+ byteswritten
),
220 (size
- byteswritten
) ,
221 "DEV %d, DMASTAT %08x, DMACTRL %08x\n", i
, stat
, ctrl
);
225 #ifdef UBSEC_VERBOSE_DEBUG
226 printf("DEV %d, DMASTAT %08x, DMACTRL %08x\n", i
, stat
, ctrl
);
228 /* Dump all queues MCRs */
229 if (!BSD_SIMPLEQ_EMPTY(&sc
->sc_qchip
)) {
230 BSD_SIMPLEQ_FOREACH(q
, &sc
->sc_qchip
, q_next
)
233 ubsec_dump_mcr(&dmap
->d_dma
->d_mcr
);
248 * map in a given sk_buff
251 dma_map_skb(struct ubsec_softc
*sc
, struct ubsec_dma_alloc
* q_map
, struct sk_buff
*skb
, int *mlen
)
257 DPRINTF("%s()\n", __FUNCTION__
);
261 * We support only a limited number of fragments.
263 if (unlikely((skb_shinfo(skb
)->nr_frags
+ 1) >= UBS_MAX_SCATTER
))
265 printk(KERN_ERR
"Only %d scatter fragments are supported.\n", UBS_MAX_SCATTER
);
269 #ifdef UBSEC_VERBOSE_DEBUG
270 DPRINTF("%s - map %d 0x%x %d\n", __FUNCTION__
, 0, (unsigned int)skb
->data
, skb_headlen(skb
));
273 /* first data package */
274 tmp
= dma_map_single(sc
->sc_dv
,
279 q_map
[i
].dma_paddr
= tmp
;
280 q_map
[i
].dma_vaddr
= skb
->data
;
281 q_map
[i
].dma_size
= skb_headlen(skb
);
283 if (unlikely(tmp
== 0))
285 printk(KERN_ERR
"Could not map memory region for dma.\n");
289 #ifdef UBSEC_VERBOSE_DEBUG
290 DPRINTF("%s - map %d done physical addr 0x%x\n", __FUNCTION__
, 0, (unsigned int)tmp
);
294 /* all other data packages */
295 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
297 #ifdef UBSEC_VERBOSE_DEBUG
298 DPRINTF("%s - map %d 0x%x %d\n", __FUNCTION__
, i
+ 1,
299 (unsigned int)page_address(skb_shinfo(skb
)->frags
[i
].page
) +
300 skb_shinfo(skb
)->frags
[i
].page_offset
, skb_shinfo(skb
)->frags
[i
].size
);
303 tmp
= dma_map_single(sc
->sc_dv
,
304 page_address(skb_shinfo(skb
)->frags
[i
].page
) +
305 skb_shinfo(skb
)->frags
[i
].page_offset
,
306 skb_shinfo(skb
)->frags
[i
].size
,
309 q_map
[i
+ 1].dma_paddr
= tmp
;
310 q_map
[i
+ 1].dma_vaddr
= (void*)(page_address(skb_shinfo(skb
)->frags
[i
].page
) +
311 skb_shinfo(skb
)->frags
[i
].page_offset
);
312 q_map
[i
+ 1].dma_size
= skb_shinfo(skb
)->frags
[i
].size
;
314 if (unlikely(tmp
== 0))
316 printk(KERN_ERR
"Could not map memory region for dma.\n");
320 #ifdef UBSEC_VERBOSE_DEBUG
321 DPRINTF("%s - map %d done physical addr 0x%x\n", __FUNCTION__
, i
+ 1, (unsigned int)tmp
);
331 * map in a given uio buffer
335 dma_map_uio(struct ubsec_softc
*sc
, struct ubsec_dma_alloc
*q_map
, struct uio
*uio
, int *mlen
)
337 struct iovec
*iov
= uio
->uio_iov
;
342 DPRINTF("%s()\n", __FUNCTION__
);
346 * We support only a limited number of fragments.
348 if (unlikely(uio
->uio_iovcnt
>= UBS_MAX_SCATTER
))
350 printk(KERN_ERR
"Only %d scatter fragments are supported.\n", UBS_MAX_SCATTER
);
354 for (n
= 0; n
< uio
->uio_iovcnt
; n
++) {
355 #ifdef UBSEC_VERBOSE_DEBUG
356 DPRINTF("%s - map %d 0x%x %d\n", __FUNCTION__
, n
, (unsigned int)iov
->iov_base
, iov
->iov_len
);
358 tmp
= dma_map_single(sc
->sc_dv
,
363 q_map
[n
].dma_paddr
= tmp
;
364 q_map
[n
].dma_vaddr
= iov
->iov_base
;
365 q_map
[n
].dma_size
= iov
->iov_len
;
367 if (unlikely(tmp
== 0))
369 printk(KERN_ERR
"Could not map memory region for dma.\n");
373 #ifdef UBSEC_VERBOSE_DEBUG
374 DPRINTF("%s - map %d done physical addr 0x%x\n", __FUNCTION__
, n
, (unsigned int)tmp
);
385 dma_unmap(struct ubsec_softc
*sc
, struct ubsec_dma_alloc
*q_map
, int mlen
)
390 DPRINTF("%s()\n", __FUNCTION__
);
393 for(i
= 0; i
< mlen
; i
++)
395 #ifdef UBSEC_VERBOSE_DEBUG
396 DPRINTF("%s - unmap %d 0x%x %d\n", __FUNCTION__
, i
, (unsigned int)q_map
[i
].dma_paddr
, q_map
[i
].dma_size
);
398 dma_unmap_single(sc
->sc_dv
,
407 * Is the operand suitable aligned for direct DMA. Each
408 * segment must be aligned on a 32-bit boundary and all
409 * but the last segment must be a multiple of 4 bytes.
412 ubsec_dmamap_aligned(struct ubsec_softc
*sc
, const struct ubsec_dma_alloc
*q_map
, int mlen
)
417 DPRINTF("%s()\n", __FUNCTION__
);
420 for (i
= 0; i
< mlen
; i
++) {
421 if (q_map
[i
].dma_paddr
& 3)
423 if (i
!= (mlen
- 1) && (q_map
[i
].dma_size
& 3))
430 #define N(a) (sizeof(a) / sizeof (a[0]))
432 ubsec_setup_mackey(struct ubsec_session
*ses
, int algo
, caddr_t key
, int klen
)
440 DPRINTF("%s()\n", __FUNCTION__
);
443 for (i
= 0; i
< klen
; i
++)
444 key
[i
] ^= HMAC_IPAD_VAL
;
446 if (algo
== CRYPTO_MD5_HMAC
) {
448 MD5Update(&md5ctx
, key
, klen
);
449 MD5Update(&md5ctx
, hmac_ipad_buffer
, MD5_HMAC_BLOCK_LEN
- klen
);
450 bcopy(md5ctx
.md5_st8
, ses
->ses_hminner
, sizeof(md5ctx
.md5_st8
));
453 SHA1Update(&sha1ctx
, key
, klen
);
454 SHA1Update(&sha1ctx
, hmac_ipad_buffer
,
455 SHA1_HMAC_BLOCK_LEN
- klen
);
456 bcopy(sha1ctx
.h
.b32
, ses
->ses_hminner
, sizeof(sha1ctx
.h
.b32
));
459 for (i
= 0; i
< klen
; i
++)
460 key
[i
] ^= (HMAC_IPAD_VAL
^ HMAC_OPAD_VAL
);
462 if (algo
== CRYPTO_MD5_HMAC
) {
464 MD5Update(&md5ctx
, key
, klen
);
465 MD5Update(&md5ctx
, hmac_opad_buffer
, MD5_HMAC_BLOCK_LEN
- klen
);
466 bcopy(md5ctx
.md5_st8
, ses
->ses_hmouter
, sizeof(md5ctx
.md5_st8
));
469 SHA1Update(&sha1ctx
, key
, klen
);
470 SHA1Update(&sha1ctx
, hmac_opad_buffer
,
471 SHA1_HMAC_BLOCK_LEN
- klen
);
472 bcopy(sha1ctx
.h
.b32
, ses
->ses_hmouter
, sizeof(sha1ctx
.h
.b32
));
475 for (i
= 0; i
< klen
; i
++)
476 key
[i
] ^= HMAC_OPAD_VAL
;
478 #else /* HMAC_HACK */
479 DPRINTF("md5/sha not implemented\n");
480 #endif /* HMAC_HACK */
485 __devinit
ubsec_ssb_probe(struct ssb_device
*sdev
,
486 const struct ssb_device_id
*ent
)
491 DPRINTF("%s()\n", __FUNCTION__
);
494 err
= ssb_bus_powerup(sdev
->bus
, 0);
496 dev_err(sdev
->dev
, "Failed to powerup the bus\n");
500 err
= request_irq(sdev
->irq
, (irq_handler_t
)ubsec_ssb_isr
,
501 IRQF_DISABLED
| IRQF_SHARED
, DRV_MODULE_NAME
, sdev
);
503 dev_err(sdev
->dev
, "Could not request irq\n");
504 goto err_out_powerdown
;
507 err
= ssb_dma_set_mask(sdev
, DMA_32BIT_MASK
);
510 "Required 32BIT DMA mask unsupported by the system.\n");
511 goto err_out_powerdown
;
514 printk(KERN_INFO
"Sentry5(tm) ROBOGateway(tm) IPSec Core at IRQ %u\n",
517 DPRINTF("Vendor: %x, core id: %x, revision: %x\n",
518 sdev
->id
.vendor
, sdev
->id
.coreid
, sdev
->id
.revision
);
520 ssb_device_enable(sdev
, 0);
522 if (ubsec_attach(sdev
, ent
, sdev
->dev
) != 0)
523 goto err_disable_interrupt
;
526 procdebug
= create_proc_entry(DRV_MODULE_NAME
, S_IRUSR
, NULL
);
529 procdebug
->read_proc
= proc_read
;
530 procdebug
->data
= NULL
;
532 DPRINTF("Unable to create proc file.\n");
537 err_disable_interrupt
:
538 free_irq(sdev
->irq
, sdev
);
541 ssb_bus_may_powerdown(sdev
->bus
);
544 ssb_device_disable(sdev
, 0);
548 static void __devexit
ubsec_ssb_remove(struct ssb_device
*sdev
) {
550 struct ubsec_softc
*sc
;
551 unsigned int ctrlflgs
;
552 struct ubsec_dma
*dmap
;
556 DPRINTF("%s()\n", __FUNCTION__
);
559 ctrlflgs
= READ_REG_SDEV(sdev
, BS_CTRL
);
560 /* disable all IPSec Core interrupts globally */
561 ctrlflgs
^= (BS_CTRL_MCR1INT
| BS_CTRL_MCR2INT
|
563 WRITE_REG_SDEV(sdev
, BS_CTRL
, ctrlflgs
);
565 free_irq(sdev
->irq
, sdev
);
567 sc
= (struct ubsec_softc
*)ssb_get_drvdata(sdev
);
569 /* unregister all crypto algorithms */
570 crypto_unregister_all(sc
->sc_cid
);
572 /* Free queue / dma memory */
573 for (i
= 0; i
< UBS_MAX_NQUEUE
; i
++) {
576 q
= sc
->sc_queuea
[i
];
582 ubsec_dma_free(sc
, &dmap
->d_alloc
);
587 sc
->sc_queuea
[i
] = NULL
;
590 ssb_bus_may_powerdown(sdev
->bus
);
591 ssb_device_disable(sdev
, 0);
592 ssb_set_drvdata(sdev
, NULL
);
596 remove_proc_entry(DRV_MODULE_NAME
, NULL
);
603 ubsec_attach(struct ssb_device
*sdev
, const struct ssb_device_id
*ent
,
606 struct ubsec_softc
*sc
= NULL
;
607 struct ubsec_dma
*dmap
;
609 static int num_chips
= 0;
612 DPRINTF("%s()\n", __FUNCTION__
);
615 sc
= (struct ubsec_softc
*) kmalloc(sizeof(*sc
), GFP_KERNEL
);
618 memset(sc
, 0, sizeof(*sc
));
620 sc
->sc_dv
= sdev
->dev
;
623 spin_lock_init(&sc
->sc_ringmtx
);
625 softc_device_init(sc
, "ubsec_ssb", num_chips
, ubsec_ssb_methods
);
627 /* Maybe someday there are boards with more than one chip available */
628 if (num_chips
< UBSEC_SSB_MAX_CHIPS
) {
629 ubsec_chip_idx
[device_get_unit(sc
->sc_dev
)] = sc
;
633 ssb_set_drvdata(sdev
, sc
);
635 BSD_SIMPLEQ_INIT(&sc
->sc_queue
);
636 BSD_SIMPLEQ_INIT(&sc
->sc_qchip
);
637 BSD_SIMPLEQ_INIT(&sc
->sc_queue2
);
638 BSD_SIMPLEQ_INIT(&sc
->sc_qchip2
);
639 BSD_SIMPLEQ_INIT(&sc
->sc_q2free
);
641 sc
->sc_statmask
= BS_STAT_MCR1_DONE
| BS_STAT_DMAERR
;
643 sc
->sc_cid
= crypto_get_driverid(softc_get_device(sc
), CRYPTOCAP_F_HARDWARE
);
644 if (sc
->sc_cid
< 0) {
645 device_printf(sc
->sc_dev
, "could not get crypto driver id\n");
649 BSD_SIMPLEQ_INIT(&sc
->sc_freequeue
);
651 for (i
= 0; i
< UBS_MAX_NQUEUE
; i
++, dmap
++) {
654 q
= (struct ubsec_q
*)kmalloc(sizeof(struct ubsec_q
), GFP_KERNEL
);
656 printf(": can't allocate queue buffers\n");
660 if (ubsec_dma_malloc(sc
, &dmap
->d_alloc
, sizeof(struct ubsec_dmachunk
),0)) {
661 printf(": can't allocate dma buffers\n");
665 dmap
->d_dma
= (struct ubsec_dmachunk
*)dmap
->d_alloc
.dma_vaddr
;
668 sc
->sc_queuea
[i
] = q
;
670 BSD_SIMPLEQ_INSERT_TAIL(&sc
->sc_freequeue
, q
, q_next
);
674 * Reset Broadcom chip
676 ubsec_reset_board(sc
);
681 ubsec_init_board(sc
);
683 /* supported crypto algorithms */
684 crypto_register(sc
->sc_cid
, CRYPTO_3DES_CBC
, 0, 0);
685 crypto_register(sc
->sc_cid
, CRYPTO_DES_CBC
, 0, 0);
687 if (sc
->sc_flags
& UBS_FLAGS_AES
) {
688 crypto_register(sc
->sc_cid
, CRYPTO_AES_CBC
, 0, 0);
689 printf(KERN_INFO DRV_MODULE_NAME
": DES 3DES AES128 AES192 AES256 MD5_HMAC SHA1_HMAC\n");
692 printf(KERN_INFO DRV_MODULE_NAME
": DES 3DES MD5_HMAC SHA1_HMAC\n");
694 crypto_register(sc
->sc_cid
, CRYPTO_MD5_HMAC
, 0, 0);
695 crypto_register(sc
->sc_cid
, CRYPTO_SHA1_HMAC
, 0, 0);
701 * UBSEC Interrupt routine
704 ubsec_ssb_isr(int irq
, void *arg
, struct pt_regs
*regs
)
706 struct ubsec_softc
*sc
= NULL
;
707 volatile u_int32_t stat
;
709 struct ubsec_dma
*dmap
;
712 #ifdef UBSEC_VERBOSE_DEBUG
713 DPRINTF("%s()\n", __FUNCTION__
);
716 sc
= (struct ubsec_softc
*)ssb_get_drvdata(arg
);
718 stat
= READ_REG(sc
, BS_STAT
);
720 stat
&= sc
->sc_statmask
;
724 WRITE_REG(sc
, BS_STAT
, stat
); /* IACK */
727 * Check to see if we have any packets waiting for us
729 if ((stat
& BS_STAT_MCR1_DONE
)) {
730 while (!BSD_SIMPLEQ_EMPTY(&sc
->sc_qchip
)) {
731 q
= BSD_SIMPLEQ_FIRST(&sc
->sc_qchip
);
734 if ((dmap
->d_dma
->d_mcr
.mcr_flags
& htole16(UBS_MCR_DONE
)) == 0)
736 DPRINTF("error while processing MCR. Flags = %x\n", dmap
->d_dma
->d_mcr
.mcr_flags
);
740 BSD_SIMPLEQ_REMOVE_HEAD(&sc
->sc_qchip
, q_next
);
742 npkts
= q
->q_nstacked_mcrs
;
744 * search for further sc_qchip ubsec_q's that share
745 * the same MCR, and complete them too, they must be
748 for (i
= 0; i
< npkts
; i
++) {
749 if(q
->q_stacked_mcr
[i
])
750 ubsec_callback(sc
, q
->q_stacked_mcr
[i
]);
754 ubsec_callback(sc
, q
);
758 * Don't send any more packet to chip if there has been
761 if (likely(!(stat
& BS_STAT_DMAERR
)))
764 DPRINTF("DMA error occurred. Stop feeding crypto chip.\n");
768 * Check to see if we got any DMA Error
770 if (stat
& BS_STAT_DMAERR
) {
771 volatile u_int32_t a
= READ_REG(sc
, BS_ERR
);
773 printf(KERN_ERR
"%s: dmaerr %s@%08x\n", DRV_MODULE_NAME
,
774 (a
& BS_ERR_READ
) ? "read" : "write", a
& BS_ERR_ADDR
);
776 ubsecstats
.hst_dmaerr
++;
777 ubsec_totalreset(sc
);
785 * ubsec_feed() - aggregate and post requests to chip
786 * It is assumed that the caller set splnet()
789 ubsec_feed(struct ubsec_softc
*sc
)
791 #ifdef UBSEC_VERBOSE_DEBUG
794 struct ubsec_q
*q
, *q2
;
799 npkts
= sc
->sc_nqueue
;
800 if (npkts
> UBS_MAX_AGGR
)
801 npkts
= UBS_MAX_AGGR
;
805 stat
= READ_REG(sc
, BS_STAT
);
807 if (stat
& (BS_STAT_MCR1_FULL
| BS_STAT_DMAERR
)) {
808 if(stat
& BS_STAT_DMAERR
) {
809 ubsec_totalreset(sc
);
810 ubsecstats
.hst_dmaerr
++;
815 #ifdef UBSEC_VERBOSE_DEBUG
816 DPRINTF("merging %d records\n", npkts
);
818 /* XXX temporary aggregation statistics reporting code */
821 DPRINTF("%s: new max aggregate %d\n", DRV_MODULE_NAME
, max
);
823 #endif /* UBSEC_VERBOSE_DEBUG */
825 q
= BSD_SIMPLEQ_FIRST(&sc
->sc_queue
);
826 BSD_SIMPLEQ_REMOVE_HEAD(&sc
->sc_queue
, q_next
);
832 * We use dma_map_single() - no sync required!
835 bus_dmamap_sync(sc
->sc_dmat
, q
->q_src_map
,
836 0, q
->q_src_map
->dm_mapsize
, BUS_DMASYNC_PREWRITE
);
837 if (q
->q_dst_map
!= NULL
)
838 bus_dmamap_sync(sc
->sc_dmat
, q
->q_dst_map
,
839 0, q
->q_dst_map
->dm_mapsize
, BUS_DMASYNC_PREREAD
);
842 q
->q_nstacked_mcrs
= npkts
- 1; /* Number of packets stacked */
844 for (i
= 0; i
< q
->q_nstacked_mcrs
; i
++) {
845 q2
= BSD_SIMPLEQ_FIRST(&sc
->sc_queue
);
848 bus_dmamap_sync(sc
->sc_dmat
, q2
->q_src_map
,
849 0, q2
->q_src_map
->dm_mapsize
, BUS_DMASYNC_PREWRITE
);
850 if (q2
->q_dst_map
!= NULL
)
851 bus_dmamap_sync(sc
->sc_dmat
, q2
->q_dst_map
,
852 0, q2
->q_dst_map
->dm_mapsize
, BUS_DMASYNC_PREREAD
);
854 BSD_SIMPLEQ_REMOVE_HEAD(&sc
->sc_queue
, q_next
);
857 v
= ((char *)&q2
->q_dma
->d_dma
->d_mcr
) + sizeof(struct ubsec_mcr
) -
858 sizeof(struct ubsec_mcr_add
);
859 bcopy(v
, &q
->q_dma
->d_dma
->d_mcradd
[i
], sizeof(struct ubsec_mcr_add
));
860 q
->q_stacked_mcr
[i
] = q2
;
862 q
->q_dma
->d_dma
->d_mcr
.mcr_pkts
= htole16(npkts
);
863 BSD_SIMPLEQ_INSERT_TAIL(&sc
->sc_qchip
, q
, q_next
);
865 bus_dmamap_sync(sc
->sc_dmat
, q
->q_dma
->d_alloc
.dma_map
,
866 0, q
->q_dma
->d_alloc
.dma_map
->dm_mapsize
,
867 BUS_DMASYNC_PREREAD
| BUS_DMASYNC_PREWRITE
);
869 WRITE_REG(sc
, BS_MCR1
, q
->q_dma
->d_alloc
.dma_paddr
+
870 offsetof(struct ubsec_dmachunk
, d_mcr
));
871 #ifdef UBSEC_VERBOSE_DEBUG
872 DPRINTF("feed (1): q->chip %p %08x %08x\n", q
,
873 (u_int32_t
)q
->q_dma
->d_alloc
.dma_paddr
,
874 (u_int32_t
)(q
->q_dma
->d_alloc
.dma_paddr
+
875 offsetof(struct ubsec_dmachunk
, d_mcr
)));
876 #endif /* UBSEC_DEBUG */
880 while (!BSD_SIMPLEQ_EMPTY(&sc
->sc_queue
)) {
881 stat
= READ_REG(sc
, BS_STAT
);
883 if (stat
& (BS_STAT_MCR1_FULL
| BS_STAT_DMAERR
)) {
884 if(stat
& BS_STAT_DMAERR
) {
885 ubsec_totalreset(sc
);
886 ubsecstats
.hst_dmaerr
++;
891 q
= BSD_SIMPLEQ_FIRST(&sc
->sc_queue
);
894 bus_dmamap_sync(sc
->sc_dmat
, q
->q_src_map
,
895 0, q
->q_src_map
->dm_mapsize
, BUS_DMASYNC_PREWRITE
);
896 if (q
->q_dst_map
!= NULL
)
897 bus_dmamap_sync(sc
->sc_dmat
, q
->q_dst_map
,
898 0, q
->q_dst_map
->dm_mapsize
, BUS_DMASYNC_PREREAD
);
899 bus_dmamap_sync(sc
->sc_dmat
, q
->q_dma
->d_alloc
.dma_map
,
900 0, q
->q_dma
->d_alloc
.dma_map
->dm_mapsize
,
901 BUS_DMASYNC_PREREAD
| BUS_DMASYNC_PREWRITE
);
904 WRITE_REG(sc
, BS_MCR1
, q
->q_dma
->d_alloc
.dma_paddr
+
905 offsetof(struct ubsec_dmachunk
, d_mcr
));
906 #ifdef UBSEC_VERBOSE_DEBUG
907 DPRINTF("feed (2): q->chip %p %08x %08x\n", q
,
908 (u_int32_t
)q
->q_dma
->d_alloc
.dma_paddr
,
909 (u_int32_t
)(q
->q_dma
->d_alloc
.dma_paddr
+
910 offsetof(struct ubsec_dmachunk
, d_mcr
)));
911 #endif /* UBSEC_DEBUG */
912 BSD_SIMPLEQ_REMOVE_HEAD(&sc
->sc_queue
, q_next
);
914 BSD_SIMPLEQ_INSERT_TAIL(&sc
->sc_qchip
, q
, q_next
);
919 * Allocate a new 'session' and return an encoded session id. 'sidp'
920 * contains our registration id, and should contain an encoded session
921 * id on successful allocation.
924 ubsec_newsession(device_t dev
, u_int32_t
*sidp
, struct cryptoini
*cri
)
926 struct cryptoini
*c
, *encini
= NULL
, *macini
= NULL
;
927 struct ubsec_softc
*sc
= NULL
;
928 struct ubsec_session
*ses
= NULL
;
932 DPRINTF("%s()\n", __FUNCTION__
);
935 if (sidp
== NULL
|| cri
== NULL
)
938 sc
= device_get_softc(dev
);
943 for (c
= cri
; c
!= NULL
; c
= c
->cri_next
) {
944 if (c
->cri_alg
== CRYPTO_MD5_HMAC
||
945 c
->cri_alg
== CRYPTO_SHA1_HMAC
) {
949 } else if (c
->cri_alg
== CRYPTO_DES_CBC
||
950 c
->cri_alg
== CRYPTO_3DES_CBC
||
951 c
->cri_alg
== CRYPTO_AES_CBC
) {
958 if (encini
== NULL
&& macini
== NULL
)
961 if (sc
->sc_sessions
== NULL
) {
962 ses
= sc
->sc_sessions
= (struct ubsec_session
*)kmalloc(
963 sizeof(struct ubsec_session
), SLAB_ATOMIC
);
966 memset(ses
, 0, sizeof(struct ubsec_session
));
968 sc
->sc_nsessions
= 1;
970 for (sesn
= 0; sesn
< sc
->sc_nsessions
; sesn
++) {
971 if (sc
->sc_sessions
[sesn
].ses_used
== 0) {
972 ses
= &sc
->sc_sessions
[sesn
];
978 sesn
= sc
->sc_nsessions
;
979 ses
= (struct ubsec_session
*)kmalloc((sesn
+ 1) *
980 sizeof(struct ubsec_session
), SLAB_ATOMIC
);
983 memset(ses
, 0, (sesn
+ 1) * sizeof(struct ubsec_session
));
984 bcopy(sc
->sc_sessions
, ses
, sesn
*
985 sizeof(struct ubsec_session
));
986 bzero(sc
->sc_sessions
, sesn
*
987 sizeof(struct ubsec_session
));
988 kfree(sc
->sc_sessions
);
989 sc
->sc_sessions
= ses
;
990 ses
= &sc
->sc_sessions
[sesn
];
995 bzero(ses
, sizeof(struct ubsec_session
));
999 /* XXX may read fewer than requested */
1000 read_random(ses
->ses_iv
, sizeof(ses
->ses_iv
));
1002 /* Go ahead and compute key in ubsec's byte order */
1003 if (encini
->cri_alg
== CRYPTO_DES_CBC
) {
1004 /* DES uses the same key three times:
1005 * 1st encrypt -> 2nd decrypt -> 3nd encrypt */
1006 bcopy(encini
->cri_key
, &ses
->ses_key
[0], 8);
1007 bcopy(encini
->cri_key
, &ses
->ses_key
[2], 8);
1008 bcopy(encini
->cri_key
, &ses
->ses_key
[4], 8);
1009 ses
->ses_keysize
= 192; /* Fake! Actually its only 64bits ..
1010 oh no it is even less: 54bits. */
1011 } else if(encini
->cri_alg
== CRYPTO_3DES_CBC
) {
1012 bcopy(encini
->cri_key
, ses
->ses_key
, 24);
1013 ses
->ses_keysize
= 192;
1014 } else if(encini
->cri_alg
== CRYPTO_AES_CBC
) {
1015 ses
->ses_keysize
= encini
->cri_klen
;
1017 if (ses
->ses_keysize
!= 128 &&
1018 ses
->ses_keysize
!= 192 &&
1019 ses
->ses_keysize
!= 256)
1021 DPRINTF("unsupported AES key size: %d\n", ses
->ses_keysize
);
1024 bcopy(encini
->cri_key
, ses
->ses_key
, (ses
->ses_keysize
/ 8));
1027 /* Hardware requires the keys in little endian byte order */
1028 for (i
=0; i
< (ses
->ses_keysize
/ 32); i
++)
1029 SWAP32(ses
->ses_key
[i
]);
1033 ses
->ses_mlen
= macini
->cri_mlen
;
1035 if (ses
->ses_mlen
== 0 ||
1036 ses
->ses_mlen
> SHA1_HASH_LEN
) {
1038 if (macini
->cri_alg
== CRYPTO_MD5_HMAC
||
1039 macini
->cri_alg
== CRYPTO_SHA1_HMAC
)
1041 ses
->ses_mlen
= DEFAULT_HMAC_LEN
;
1045 * Reserved for future usage. MD5/SHA1 calculations have
1046 * different hash sizes.
1048 printk(KERN_ERR DRV_MODULE_NAME
": unsupported hash operation with mac/hash len: %d\n", ses
->ses_mlen
);
1054 if (macini
->cri_key
!= NULL
) {
1055 ubsec_setup_mackey(ses
, macini
->cri_alg
, macini
->cri_key
,
1056 macini
->cri_klen
/ 8);
1060 *sidp
= UBSEC_SID(device_get_unit(sc
->sc_dev
), sesn
);
1065 * Deallocate a session.
1068 ubsec_freesession(device_t dev
, u_int64_t tid
)
1070 struct ubsec_softc
*sc
= device_get_softc(dev
);
1072 u_int32_t sid
= ((u_int32_t
)tid
) & 0xffffffff;
1075 DPRINTF("%s()\n", __FUNCTION__
);
1081 session
= UBSEC_SESSION(sid
);
1082 if (session
< sc
->sc_nsessions
) {
1083 bzero(&sc
->sc_sessions
[session
], sizeof(sc
->sc_sessions
[session
]));
1090 ubsec_process(device_t dev
, struct cryptop
*crp
, int hint
)
1092 struct ubsec_q
*q
= NULL
;
1093 int err
= 0, i
, j
, nicealign
;
1094 struct ubsec_softc
*sc
= device_get_softc(dev
);
1095 struct cryptodesc
*crd1
, *crd2
, *maccrd
, *enccrd
;
1096 int encoffset
= 0, macoffset
= 0, cpskip
, cpoffset
;
1097 int sskip
, dskip
, stheend
, dtheend
, ivsize
= 8;
1099 struct ubsec_session
*ses
;
1100 struct ubsec_generic_ctx ctx
;
1101 struct ubsec_dma
*dmap
= NULL
;
1102 unsigned long flags
;
1105 DPRINTF("%s()\n", __FUNCTION__
);
1108 if (unlikely(crp
== NULL
|| crp
->crp_callback
== NULL
)) {
1109 ubsecstats
.hst_invalid
++;
1113 if (unlikely(sc
== NULL
))
1116 #ifdef UBSEC_VERBOSE_DEBUG
1117 DPRINTF("spin_lock_irqsave\n");
1119 spin_lock_irqsave(&sc
->sc_ringmtx
, flags
);
1120 //spin_lock_irq(&sc->sc_ringmtx);
1122 if (BSD_SIMPLEQ_EMPTY(&sc
->sc_freequeue
)) {
1123 ubsecstats
.hst_queuefull
++;
1124 #ifdef UBSEC_VERBOSE_DEBUG
1125 DPRINTF("spin_unlock_irqrestore\n");
1127 spin_unlock_irqrestore(&sc
->sc_ringmtx
, flags
);
1128 //spin_unlock_irq(&sc->sc_ringmtx);
1133 q
= BSD_SIMPLEQ_FIRST(&sc
->sc_freequeue
);
1134 BSD_SIMPLEQ_REMOVE_HEAD(&sc
->sc_freequeue
, q_next
);
1135 #ifdef UBSEC_VERBOSE_DEBUG
1136 DPRINTF("spin_unlock_irqrestore\n");
1138 spin_unlock_irqrestore(&sc
->sc_ringmtx
, flags
);
1139 //spin_unlock_irq(&sc->sc_ringmtx);
1141 dmap
= q
->q_dma
; /* Save dma pointer */
1142 bzero(q
, sizeof(struct ubsec_q
));
1143 bzero(&ctx
, sizeof(ctx
));
1145 q
->q_sesn
= UBSEC_SESSION(crp
->crp_sid
);
1147 ses
= &sc
->sc_sessions
[q
->q_sesn
];
1149 if (crp
->crp_flags
& CRYPTO_F_SKBUF
) {
1150 q
->q_src_m
= (struct sk_buff
*)crp
->crp_buf
;
1151 q
->q_dst_m
= (struct sk_buff
*)crp
->crp_buf
;
1152 } else if (crp
->crp_flags
& CRYPTO_F_IOV
) {
1153 q
->q_src_io
= (struct uio
*)crp
->crp_buf
;
1154 q
->q_dst_io
= (struct uio
*)crp
->crp_buf
;
1157 goto errout
; /* XXX we don't handle contiguous blocks! */
1160 bzero(&dmap
->d_dma
->d_mcr
, sizeof(struct ubsec_mcr
));
1162 dmap
->d_dma
->d_mcr
.mcr_pkts
= htole16(1);
1163 dmap
->d_dma
->d_mcr
.mcr_flags
= 0;
1166 crd1
= crp
->crp_desc
;
1171 crd2
= crd1
->crd_next
;
1174 if (crd1
->crd_alg
== CRYPTO_MD5_HMAC
||
1175 crd1
->crd_alg
== CRYPTO_SHA1_HMAC
) {
1178 } else if (crd1
->crd_alg
== CRYPTO_DES_CBC
||
1179 crd1
->crd_alg
== CRYPTO_3DES_CBC
||
1180 crd1
->crd_alg
== CRYPTO_AES_CBC
) {
1188 if ((crd1
->crd_alg
== CRYPTO_MD5_HMAC
||
1189 crd1
->crd_alg
== CRYPTO_SHA1_HMAC
) &&
1190 (crd2
->crd_alg
== CRYPTO_DES_CBC
||
1191 crd2
->crd_alg
== CRYPTO_3DES_CBC
||
1192 crd2
->crd_alg
== CRYPTO_AES_CBC
) &&
1193 ((crd2
->crd_flags
& CRD_F_ENCRYPT
) == 0)) {
1196 } else if ((crd1
->crd_alg
== CRYPTO_DES_CBC
||
1197 crd1
->crd_alg
== CRYPTO_3DES_CBC
||
1198 crd1
->crd_alg
== CRYPTO_AES_CBC
) &&
1199 (crd2
->crd_alg
== CRYPTO_MD5_HMAC
||
1200 crd2
->crd_alg
== CRYPTO_SHA1_HMAC
) &&
1201 (crd1
->crd_flags
& CRD_F_ENCRYPT
)) {
1206 * We cannot order the ubsec as requested
1208 printk(KERN_ERR DRV_MODULE_NAME
": got wrong algorithm/signature order.\n");
1214 /* Encryption/Decryption requested */
1216 encoffset
= enccrd
->crd_skip
;
1218 if (enccrd
->crd_alg
== CRYPTO_DES_CBC
||
1219 enccrd
->crd_alg
== CRYPTO_3DES_CBC
)
1221 ctx
.pc_flags
|= htole16(UBS_PKTCTX_ENC_3DES
);
1222 ctx
.pc_type
= htole16(UBS_PKTCTX_TYPE_IPSEC_DES
);
1223 ivsize
= 8; /* [3]DES uses 64bit IVs */
1225 ctx
.pc_flags
|= htole16(UBS_PKTCTX_ENC_AES
);
1226 ctx
.pc_type
= htole16(UBS_PKTCTX_TYPE_IPSEC_AES
);
1227 ivsize
= 16; /* AES uses 128bit IVs / [3]DES 64bit IVs */
1229 switch(ses
->ses_keysize
)
1232 ctx
.pc_flags
|= htole16(UBS_PKTCTX_AES128
);
1235 ctx
.pc_flags
|= htole16(UBS_PKTCTX_AES192
);
1238 ctx
.pc_flags
|= htole16(UBS_PKTCTX_AES256
);
1241 DPRINTF("invalid AES key size: %d\n", ses
->ses_keysize
);
1247 if (enccrd
->crd_flags
& CRD_F_ENCRYPT
) {
1248 /* Direction: Outbound */
1250 q
->q_flags
|= UBSEC_QFLAGS_COPYOUTIV
;
1252 if (enccrd
->crd_flags
& CRD_F_IV_EXPLICIT
) {
1253 bcopy(enccrd
->crd_iv
, ctx
.pc_iv
, ivsize
);
1255 for(i
=0; i
< (ivsize
/ 4); i
++)
1256 ctx
.pc_iv
[i
] = ses
->ses_iv
[i
];
1259 /* If there is no IV in the buffer -> copy it here */
1260 if ((enccrd
->crd_flags
& CRD_F_IV_PRESENT
) == 0) {
1261 if (crp
->crp_flags
& CRYPTO_F_SKBUF
)
1263 m_copyback(q->q_src_m,
1267 crypto_copyback(crp
->crp_flags
, (caddr_t
)q
->q_src_m
,
1268 enccrd
->crd_inject
, ivsize
, (caddr_t
)ctx
.pc_iv
);
1269 else if (crp
->crp_flags
& CRYPTO_F_IOV
)
1271 cuio_copyback(q->q_src_io,
1275 crypto_copyback(crp
->crp_flags
, (caddr_t
)q
->q_src_io
,
1276 enccrd
->crd_inject
, ivsize
, (caddr_t
)ctx
.pc_iv
);
1279 /* Direction: Inbound */
1281 ctx
.pc_flags
|= htole16(UBS_PKTCTX_INBOUND
);
1283 if (enccrd
->crd_flags
& CRD_F_IV_EXPLICIT
)
1284 bcopy(enccrd
->crd_iv
, ctx
.pc_iv
, ivsize
);
1285 else if (crp
->crp_flags
& CRYPTO_F_SKBUF
)
1287 m_copydata(q->q_src_m, enccrd->crd_inject,
1288 8, (caddr_t)ctx.pc_iv);
1290 crypto_copydata(crp
->crp_flags
, (caddr_t
)q
->q_src_m
,
1291 enccrd
->crd_inject
, ivsize
,
1292 (caddr_t
)ctx
.pc_iv
);
1293 else if (crp
->crp_flags
& CRYPTO_F_IOV
)
1295 cuio_copydata(q->q_src_io,
1296 enccrd->crd_inject, 8,
1297 (caddr_t)ctx.pc_iv);
1299 crypto_copydata(crp
->crp_flags
, (caddr_t
)q
->q_src_io
,
1300 enccrd
->crd_inject
, ivsize
,
1301 (caddr_t
)ctx
.pc_iv
);
1305 /* Even though key & IV sizes differ from cipher to cipher
1306 * copy / swap the full array lengths. Let the compiler unroll
1307 * the loop to increase the cpu pipeline performance... */
1308 for(i
=0; i
< 8; i
++)
1309 ctx
.pc_key
[i
] = ses
->ses_key
[i
];
1310 for(i
=0; i
< 4; i
++)
1311 SWAP32(ctx
.pc_iv
[i
]);
1314 /* Authentication requested */
1316 macoffset
= maccrd
->crd_skip
;
1318 if (maccrd
->crd_alg
== CRYPTO_MD5_HMAC
)
1319 ctx
.pc_flags
|= htole16(UBS_PKTCTX_AUTH_MD5
);
1321 ctx
.pc_flags
|= htole16(UBS_PKTCTX_AUTH_SHA1
);
1323 for (i
= 0; i
< 5; i
++) {
1324 ctx
.pc_hminner
[i
] = ses
->ses_hminner
[i
];
1325 ctx
.pc_hmouter
[i
] = ses
->ses_hmouter
[i
];
1327 HTOLE32(ctx
.pc_hminner
[i
]);
1328 HTOLE32(ctx
.pc_hmouter
[i
]);
1332 if (enccrd
&& maccrd
) {
1334 * ubsec cannot handle packets where the end of encryption
1335 * and authentication are not the same, or where the
1336 * encrypted part begins before the authenticated part.
1338 if (((encoffset
+ enccrd
->crd_len
) !=
1339 (macoffset
+ maccrd
->crd_len
)) ||
1340 (enccrd
->crd_skip
< maccrd
->crd_skip
)) {
1344 sskip
= maccrd
->crd_skip
;
1345 cpskip
= dskip
= enccrd
->crd_skip
;
1346 stheend
= maccrd
->crd_len
;
1347 dtheend
= enccrd
->crd_len
;
1348 coffset
= enccrd
->crd_skip
- maccrd
->crd_skip
;
1349 cpoffset
= cpskip
+ dtheend
;
1351 DPRINTF("mac: skip %d, len %d, inject %d\n",
1352 maccrd
->crd_skip
, maccrd
->crd_len
, maccrd
->crd_inject
);
1353 DPRINTF("enc: skip %d, len %d, inject %d\n",
1354 enccrd
->crd_skip
, enccrd
->crd_len
, enccrd
->crd_inject
);
1355 DPRINTF("src: skip %d, len %d\n", sskip
, stheend
);
1356 DPRINTF("dst: skip %d, len %d\n", dskip
, dtheend
);
1357 DPRINTF("ubs: coffset %d, pktlen %d, cpskip %d, cpoffset %d\n",
1358 coffset
, stheend
, cpskip
, cpoffset
);
1361 cpskip
= dskip
= sskip
= macoffset
+ encoffset
;
1362 dtheend
= stheend
= (enccrd
)?enccrd
->crd_len
:maccrd
->crd_len
;
1363 cpoffset
= cpskip
+ dtheend
;
1366 ctx
.pc_offset
= htole16(coffset
>> 2);
1369 if (bus_dmamap_create(sc
->sc_dmat
, 0xfff0, UBS_MAX_SCATTER
,
1370 0xfff0, 0, BUS_DMA_NOWAIT
, &q
->q_src_map
) != 0) {
1376 if (crp
->crp_flags
& CRYPTO_F_SKBUF
) {
1378 if (bus_dmamap_load_mbuf(sc
->sc_dmat
, q
->q_src_map
,
1379 q
->q_src_m
, BUS_DMA_NOWAIT
) != 0) {
1380 bus_dmamap_destroy(sc
->sc_dmat
, q
->q_src_map
);
1381 q
->q_src_map
= NULL
;
1386 err
= dma_map_skb(sc
, q
->q_src_map
, q
->q_src_m
, &q
->q_src_len
);
1387 if (unlikely(err
!= 0))
1390 } else if (crp
->crp_flags
& CRYPTO_F_IOV
) {
1392 if (bus_dmamap_load_uio(sc
->sc_dmat
, q
->q_src_map
,
1393 q
->q_src_io
, BUS_DMA_NOWAIT
) != 0) {
1394 bus_dmamap_destroy(sc
->sc_dmat
, q
->q_src_map
);
1395 q
->q_src_map
= NULL
;
1400 err
= dma_map_uio(sc
, q
->q_src_map
, q
->q_src_io
, &q
->q_src_len
);
1401 if (unlikely(err
!= 0))
1408 nicealign
= ubsec_dmamap_aligned(sc
, q
->q_src_map
, q
->q_src_len
);
1410 dmap
->d_dma
->d_mcr
.mcr_pktlen
= htole16(stheend
);
1413 DPRINTF("src skip: %d\n", sskip
);
1415 for (i
= j
= 0; i
< q
->q_src_len
; i
++) {
1416 struct ubsec_pktbuf
*pb
;
1417 size_t packl
= q
->q_src_map
[i
].dma_size
;
1418 dma_addr_t packp
= q
->q_src_map
[i
].dma_paddr
;
1420 if (sskip
>= packl
) {
1429 /* maximum fragment size is 0xfffc */
1430 if (packl
> 0xfffc) {
1431 DPRINTF("Error: fragment size is bigger than 0xfffc.\n");
1437 pb
= &dmap
->d_dma
->d_mcr
.mcr_ipktbuf
;
1439 pb
= &dmap
->d_dma
->d_sbuf
[j
- 1];
1441 pb
->pb_addr
= htole32(packp
);
1444 if (packl
> stheend
) {
1445 pb
->pb_len
= htole32(stheend
);
1448 pb
->pb_len
= htole32(packl
);
1452 pb
->pb_len
= htole32(packl
);
1454 if ((i
+ 1) == q
->q_src_len
)
1457 pb
->pb_next
= htole32(dmap
->d_alloc
.dma_paddr
+
1458 offsetof(struct ubsec_dmachunk
, d_sbuf
[j
]));
1462 if (enccrd
== NULL
&& maccrd
!= NULL
) {
1463 /* Authentication only */
1464 dmap
->d_dma
->d_mcr
.mcr_opktbuf
.pb_addr
= 0;
1465 dmap
->d_dma
->d_mcr
.mcr_opktbuf
.pb_len
= 0;
1466 dmap
->d_dma
->d_mcr
.mcr_opktbuf
.pb_next
=
1467 htole32(dmap
->d_alloc
.dma_paddr
+
1468 offsetof(struct ubsec_dmachunk
, d_macbuf
[0]));
1470 DPRINTF("opkt: %x %x %x\n",
1471 dmap
->d_dma
->d_mcr
.mcr_opktbuf
.pb_addr
,
1472 dmap
->d_dma
->d_mcr
.mcr_opktbuf
.pb_len
,
1473 dmap
->d_dma
->d_mcr
.mcr_opktbuf
.pb_next
);
1476 if (crp
->crp_flags
& CRYPTO_F_IOV
) {
1482 if (bus_dmamap_create(sc
->sc_dmat
, 0xfff0,
1483 UBS_MAX_SCATTER
, 0xfff0, 0, BUS_DMA_NOWAIT
,
1484 &q
->q_dst_map
) != 0) {
1488 if (bus_dmamap_load_uio(sc
->sc_dmat
, q
->q_dst_map
,
1489 q
->q_dst_io
, BUS_DMA_NOWAIT
) != 0) {
1490 bus_dmamap_destroy(sc
->sc_dmat
, q
->q_dst_map
);
1491 q
->q_dst_map
= NULL
;
1496 /* HW shall copy the result into the source memory */
1497 for(i
= 0; i
< q
->q_src_len
; i
++)
1498 q
->q_dst_map
[i
] = q
->q_src_map
[i
];
1500 q
->q_dst_len
= q
->q_src_len
;
1503 } else if (crp
->crp_flags
& CRYPTO_F_SKBUF
) {
1506 /* HW shall copy the result into the source memory */
1507 q
->q_dst_m
= q
->q_src_m
;
1508 for(i
= 0; i
< q
->q_src_len
; i
++)
1509 q
->q_dst_map
[i
] = q
->q_src_map
[i
];
1511 q
->q_dst_len
= q
->q_src_len
;
1517 struct sk_buff
*m
, *top
, **mp
;
1519 totlen
= q
->q_src_map
->dm_mapsize
;
1520 if (q
->q_src_m
->m_flags
& M_PKTHDR
) {
1522 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
1525 MGET(m
, M_DONTWAIT
, MT_DATA
);
1532 M_DUP_PKTHDR(m
, q
->q_src_m
);
1533 if (totlen
>= MINCLSIZE
) {
1534 MCLGET(m
, M_DONTWAIT
);
1535 if (m
->m_flags
& M_EXT
)
1542 while (totlen
> 0) {
1544 MGET(m
, M_DONTWAIT
, MT_DATA
);
1552 if (top
&& totlen
>= MINCLSIZE
) {
1553 MCLGET(m
, M_DONTWAIT
);
1554 if (m
->m_flags
& M_EXT
)
1557 m
->m_len
= len
= min(totlen
, len
);
1563 ubsec_mcopy(q
->q_src_m
, q
->q_dst_m
,
1565 if (bus_dmamap_create(sc
->sc_dmat
, 0xfff0,
1566 UBS_MAX_SCATTER
, 0xfff0, 0, BUS_DMA_NOWAIT
,
1567 &q
->q_dst_map
) != 0) {
1571 if (bus_dmamap_load_mbuf(sc
->sc_dmat
,
1572 q
->q_dst_map
, q
->q_dst_m
,
1573 BUS_DMA_NOWAIT
) != 0) {
1574 bus_dmamap_destroy(sc
->sc_dmat
,
1576 q
->q_dst_map
= NULL
;
1581 device_printf(sc
->sc_dev
,
1582 "%s,%d: CRYPTO_F_SKBUF unaligned not implemented\n",
1583 __FILE__
, __LINE__
);
1594 DPRINTF("dst skip: %d\n", dskip
);
1596 for (i
= j
= 0; i
< q
->q_dst_len
; i
++) {
1597 struct ubsec_pktbuf
*pb
;
1598 size_t packl
= q
->q_dst_map
[i
].dma_size
;
1599 dma_addr_t packp
= q
->q_dst_map
[i
].dma_paddr
;
1601 if (dskip
>= packl
) {
1610 if (packl
> 0xfffc) {
1611 DPRINTF("Error: fragment size is bigger than 0xfffc.\n");
1617 pb
= &dmap
->d_dma
->d_mcr
.mcr_opktbuf
;
1619 pb
= &dmap
->d_dma
->d_dbuf
[j
- 1];
1621 pb
->pb_addr
= htole32(packp
);
1624 if (packl
> dtheend
) {
1625 pb
->pb_len
= htole32(dtheend
);
1628 pb
->pb_len
= htole32(packl
);
1632 pb
->pb_len
= htole32(packl
);
1634 if ((i
+ 1) == q
->q_dst_len
) {
1637 * The last fragment of the output buffer
1638 * contains the HMAC. */
1639 pb
->pb_next
= htole32(dmap
->d_alloc
.dma_paddr
+
1640 offsetof(struct ubsec_dmachunk
, d_macbuf
[0]));
1644 pb
->pb_next
= htole32(dmap
->d_alloc
.dma_paddr
+
1645 offsetof(struct ubsec_dmachunk
, d_dbuf
[j
]));
1650 dmap
->d_dma
->d_mcr
.mcr_cmdctxp
= htole32(dmap
->d_alloc
.dma_paddr
+
1651 offsetof(struct ubsec_dmachunk
, d_ctx
));
1653 if (sc
->sc_flags
& UBS_FLAGS_LONGCTX
) {
1654 /* new Broadcom cards with dynamic long command context structure */
1656 if (enccrd
!= NULL
&&
1657 enccrd
->crd_alg
== CRYPTO_AES_CBC
)
1659 struct ubsec_pktctx_aes128
*ctxaes128
;
1660 struct ubsec_pktctx_aes192
*ctxaes192
;
1661 struct ubsec_pktctx_aes256
*ctxaes256
;
1663 switch(ses
->ses_keysize
)
1667 ctxaes128
= (struct ubsec_pktctx_aes128
*)
1668 (dmap
->d_alloc
.dma_vaddr
+
1669 offsetof(struct ubsec_dmachunk
, d_ctx
));
1671 ctxaes128
->pc_len
= htole16(sizeof(struct ubsec_pktctx_aes128
));
1672 ctxaes128
->pc_type
= ctx
.pc_type
;
1673 ctxaes128
->pc_flags
= ctx
.pc_flags
;
1674 ctxaes128
->pc_offset
= ctx
.pc_offset
;
1675 for (i
= 0; i
< 4; i
++)
1676 ctxaes128
->pc_aeskey
[i
] = ctx
.pc_key
[i
];
1677 for (i
= 0; i
< 5; i
++)
1678 ctxaes128
->pc_hminner
[i
] = ctx
.pc_hminner
[i
];
1679 for (i
= 0; i
< 5; i
++)
1680 ctxaes128
->pc_hmouter
[i
] = ctx
.pc_hmouter
[i
];
1681 for (i
= 0; i
< 4; i
++)
1682 ctxaes128
->pc_iv
[i
] = ctx
.pc_iv
[i
];
1687 ctxaes192
= (struct ubsec_pktctx_aes192
*)
1688 (dmap
->d_alloc
.dma_vaddr
+
1689 offsetof(struct ubsec_dmachunk
, d_ctx
));
1691 ctxaes192
->pc_len
= htole16(sizeof(struct ubsec_pktctx_aes192
));
1692 ctxaes192
->pc_type
= ctx
.pc_type
;
1693 ctxaes192
->pc_flags
= ctx
.pc_flags
;
1694 ctxaes192
->pc_offset
= ctx
.pc_offset
;
1695 for (i
= 0; i
< 6; i
++)
1696 ctxaes192
->pc_aeskey
[i
] = ctx
.pc_key
[i
];
1697 for (i
= 0; i
< 5; i
++)
1698 ctxaes192
->pc_hminner
[i
] = ctx
.pc_hminner
[i
];
1699 for (i
= 0; i
< 5; i
++)
1700 ctxaes192
->pc_hmouter
[i
] = ctx
.pc_hmouter
[i
];
1701 for (i
= 0; i
< 4; i
++)
1702 ctxaes192
->pc_iv
[i
] = ctx
.pc_iv
[i
];
1707 ctxaes256
= (struct ubsec_pktctx_aes256
*)
1708 (dmap
->d_alloc
.dma_vaddr
+
1709 offsetof(struct ubsec_dmachunk
, d_ctx
));
1711 ctxaes256
->pc_len
= htole16(sizeof(struct ubsec_pktctx_aes256
));
1712 ctxaes256
->pc_type
= ctx
.pc_type
;
1713 ctxaes256
->pc_flags
= ctx
.pc_flags
;
1714 ctxaes256
->pc_offset
= ctx
.pc_offset
;
1715 for (i
= 0; i
< 8; i
++)
1716 ctxaes256
->pc_aeskey
[i
] = ctx
.pc_key
[i
];
1717 for (i
= 0; i
< 5; i
++)
1718 ctxaes256
->pc_hminner
[i
] = ctx
.pc_hminner
[i
];
1719 for (i
= 0; i
< 5; i
++)
1720 ctxaes256
->pc_hmouter
[i
] = ctx
.pc_hmouter
[i
];
1721 for (i
= 0; i
< 4; i
++)
1722 ctxaes256
->pc_iv
[i
] = ctx
.pc_iv
[i
];
1728 * [3]DES / MD5_HMAC / SHA1_HMAC
1730 * MD5_HMAC / SHA1_HMAC can use the IPSEC 3DES operation without
1733 struct ubsec_pktctx_des
*ctxdes
;
1735 ctxdes
= (struct ubsec_pktctx_des
*)(dmap
->d_alloc
.dma_vaddr
+
1736 offsetof(struct ubsec_dmachunk
, d_ctx
));
1738 ctxdes
->pc_len
= htole16(sizeof(struct ubsec_pktctx_des
));
1739 ctxdes
->pc_type
= ctx
.pc_type
;
1740 ctxdes
->pc_flags
= ctx
.pc_flags
;
1741 ctxdes
->pc_offset
= ctx
.pc_offset
;
1742 for (i
= 0; i
< 6; i
++)
1743 ctxdes
->pc_deskey
[i
] = ctx
.pc_key
[i
];
1744 for (i
= 0; i
< 5; i
++)
1745 ctxdes
->pc_hminner
[i
] = ctx
.pc_hminner
[i
];
1746 for (i
= 0; i
< 5; i
++)
1747 ctxdes
->pc_hmouter
[i
] = ctx
.pc_hmouter
[i
];
1748 ctxdes
->pc_iv
[0] = ctx
.pc_iv
[0];
1749 ctxdes
->pc_iv
[1] = ctx
.pc_iv
[1];
1753 /* old Broadcom card with fixed small command context structure */
1756 * [3]DES / MD5_HMAC / SHA1_HMAC
1758 struct ubsec_pktctx
*ctxs
;
1760 ctxs
= (struct ubsec_pktctx
*)(dmap
->d_alloc
.dma_vaddr
+
1761 offsetof(struct ubsec_dmachunk
, d_ctx
));
1763 /* transform generic context into small context */
1764 for (i
= 0; i
< 6; i
++)
1765 ctxs
->pc_deskey
[i
] = ctx
.pc_key
[i
];
1766 for (i
= 0; i
< 5; i
++)
1767 ctxs
->pc_hminner
[i
] = ctx
.pc_hminner
[i
];
1768 for (i
= 0; i
< 5; i
++)
1769 ctxs
->pc_hmouter
[i
] = ctx
.pc_hmouter
[i
];
1770 ctxs
->pc_iv
[0] = ctx
.pc_iv
[0];
1771 ctxs
->pc_iv
[1] = ctx
.pc_iv
[1];
1772 ctxs
->pc_flags
= ctx
.pc_flags
;
1773 ctxs
->pc_offset
= ctx
.pc_offset
;
1776 #ifdef UBSEC_VERBOSE_DEBUG
1777 DPRINTF("spin_lock_irqsave\n");
1779 spin_lock_irqsave(&sc
->sc_ringmtx
, flags
);
1780 //spin_lock_irq(&sc->sc_ringmtx);
1782 BSD_SIMPLEQ_INSERT_TAIL(&sc
->sc_queue
, q
, q_next
);
1784 ubsecstats
.hst_ipackets
++;
1785 ubsecstats
.hst_ibytes
+= stheend
;
1788 #ifdef UBSEC_VERBOSE_DEBUG
1789 DPRINTF("spin_unlock_irqrestore\n");
1791 spin_unlock_irqrestore(&sc
->sc_ringmtx
, flags
);
1792 //spin_unlock_irq(&sc->sc_ringmtx);
1799 if ((q
->q_dst_m
!= NULL
) && (q
->q_src_m
!= q
->q_dst_m
))
1800 m_freem(q
->q_dst_m
);
1803 if ((q
->q_has_dst
== 1) && q
->q_dst_len
> 0) {
1805 bus_dmamap_unload(sc
->sc_dmat
, q
->q_dst_map
);
1806 bus_dmamap_destroy(sc
->sc_dmat
, q
->q_dst_map
);
1808 dma_unmap(sc
, q
->q_dst_map
, q
->q_dst_len
);
1810 if (q
->q_src_len
> 0) {
1812 bus_dmamap_unload(sc
->sc_dmat
, q
->q_src_map
);
1813 bus_dmamap_destroy(sc
->sc_dmat
, q
->q_src_map
);
1815 dma_unmap(sc
, q
->q_src_map
, q
->q_src_len
);
1818 #ifdef UBSEC_VERBOSE_DEBUG
1819 DPRINTF("spin_lock_irqsave\n");
1821 spin_lock_irqsave(&sc
->sc_ringmtx
, flags
);
1822 //spin_lock_irq(&sc->sc_ringmtx);
1824 BSD_SIMPLEQ_INSERT_TAIL(&sc
->sc_freequeue
, q
, q_next
);
1826 #ifdef UBSEC_VERBOSE_DEBUG
1827 DPRINTF("spin_unlock_irqrestore\n");
1829 spin_unlock_irqrestore(&sc
->sc_ringmtx
, flags
);
1830 //spin_unlock_irq(&sc->sc_ringmtx);
1834 ubsecstats
.hst_invalid
++;
1836 ubsecstats
.hst_nomem
++;
1838 crp
->crp_etype
= err
;
1842 DPRINTF("%s() err = %x\n", __FUNCTION__
, err
);
1849 ubsec_callback(struct ubsec_softc
*sc
, struct ubsec_q
*q
)
1851 struct cryptop
*crp
= (struct cryptop
*)q
->q_crp
;
1852 struct cryptodesc
*crd
;
1853 struct ubsec_dma
*dmap
= q
->q_dma
;
1857 DPRINTF("%s()\n", __FUNCTION__
);
1860 ubsecstats
.hst_opackets
++;
1861 ubsecstats
.hst_obytes
+= dmap
->d_alloc
.dma_size
;
1864 bus_dmamap_sync(sc
->sc_dmat
, dmap
->d_alloc
.dma_map
, 0,
1865 dmap
->d_alloc
.dma_map
->dm_mapsize
,
1866 BUS_DMASYNC_POSTREAD
|BUS_DMASYNC_POSTWRITE
);
1867 if (q
->q_dst_map
!= NULL
&& q
->q_dst_map
!= q
->q_src_map
) {
1868 bus_dmamap_sync(sc
->sc_dmat
, q
->q_dst_map
,
1869 0, q
->q_dst_map
->dm_mapsize
, BUS_DMASYNC_POSTREAD
);
1870 bus_dmamap_unload(sc
->sc_dmat
, q
->q_dst_map
);
1871 bus_dmamap_destroy(sc
->sc_dmat
, q
->q_dst_map
);
1873 bus_dmamap_sync(sc
->sc_dmat
, q
->q_src_map
,
1874 0, q
->q_src_map
->dm_mapsize
, BUS_DMASYNC_POSTWRITE
);
1875 bus_dmamap_unload(sc
->sc_dmat
, q
->q_src_map
);
1876 bus_dmamap_destroy(sc
->sc_dmat
, q
->q_src_map
);
1879 if ((q
->q_has_dst
== 1) && q
->q_dst_len
> 0)
1880 dma_unmap(sc
, q
->q_dst_map
, q
->q_dst_len
);
1882 dma_unmap(sc
, q
->q_src_map
, q
->q_src_len
);
1885 if ((crp
->crp_flags
& CRYPTO_F_SKBUF
) && (q
->q_src_m
!= q
->q_dst_m
)) {
1886 m_freem(q
->q_src_m
);
1887 crp
->crp_buf
= (caddr_t
)q
->q_dst_m
;
1891 /* copy out IV for future use */
1892 if (q
->q_flags
& UBSEC_QFLAGS_COPYOUTIV
) {
1893 for (crd
= crp
->crp_desc
; crd
; crd
= crd
->crd_next
) {
1894 if (crd
->crd_alg
!= CRYPTO_DES_CBC
&&
1895 crd
->crd_alg
!= CRYPTO_3DES_CBC
&&
1896 crd
->crd_alg
!= CRYPTO_AES_CBC
)
1899 if (crd
->crd_alg
== CRYPTO_AES_CBC
)
1904 if (crp
->crp_flags
& CRYPTO_F_SKBUF
)
1906 m_copydata((struct sk_buff
*)crp
->crp_buf
,
1907 crd
->crd_skip
+ crd
->crd_len
- 8, 8,
1908 (caddr_t
)sc
->sc_sessions
[q
->q_sesn
].ses_iv
);
1910 crypto_copydata(crp
->crp_flags
, (caddr_t
)crp
->crp_buf
,
1911 crd
->crd_skip
+ crd
->crd_len
- ivsize
, ivsize
,
1912 (caddr_t
)sc
->sc_sessions
[q
->q_sesn
].ses_iv
);
1914 else if (crp
->crp_flags
& CRYPTO_F_IOV
) {
1916 cuio_copydata((struct uio
*)crp
->crp_buf
,
1917 crd
->crd_skip
+ crd
->crd_len
- 8, 8,
1918 (caddr_t
)sc
->sc_sessions
[q
->q_sesn
].ses_iv
);
1920 crypto_copydata(crp
->crp_flags
, (caddr_t
)crp
->crp_buf
,
1921 crd
->crd_skip
+ crd
->crd_len
- ivsize
, ivsize
,
1922 (caddr_t
)sc
->sc_sessions
[q
->q_sesn
].ses_iv
);
1929 for (crd
= crp
->crp_desc
; crd
; crd
= crd
->crd_next
) {
1930 if (crd
->crd_alg
!= CRYPTO_MD5_HMAC
&&
1931 crd
->crd_alg
!= CRYPTO_SHA1_HMAC
)
1934 if (crp
->crp_flags
& CRYPTO_F_SKBUF
)
1935 m_copyback((struct sk_buff
*)crp
->crp_buf
,
1936 crd
->crd_inject
, 12,
1937 dmap
->d_dma
->d_macbuf
);
1940 /* BUG? it does not honor the mac len.. */
1941 crypto_copyback(crp
->crp_flags
, crp
->crp_buf
,
1942 crd
->crd_inject
, 12,
1943 (caddr_t
)dmap
->d_dma
->d_macbuf
);
1945 crypto_copyback(crp
->crp_flags
, crp
->crp_buf
,
1947 sc
->sc_sessions
[q
->q_sesn
].ses_mlen
,
1948 (caddr_t
)dmap
->d_dma
->d_macbuf
);
1950 else if (crp
->crp_flags
& CRYPTO_F_IOV
&& crp
->crp_mac
)
1951 bcopy((caddr_t
)dmap
->d_dma
->d_macbuf
,
1956 BSD_SIMPLEQ_INSERT_TAIL(&sc
->sc_freequeue
, q
, q_next
);
1961 ubsec_mcopy(struct sk_buff
*srcm
, struct sk_buff
*dstm
, int hoffset
, int toffset
)
1963 int i
, j
, dlen
, slen
;
1973 for (i
= 0; i
< min(slen
, dlen
); i
++) {
1974 if (j
< hoffset
|| j
>= toffset
)
1998 ubsec_dma_malloc(struct ubsec_softc
*sc
, struct ubsec_dma_alloc
*dma
,
1999 size_t size
, int mapflags
)
2001 dma
->dma_vaddr
= dma_alloc_coherent(sc
->sc_dv
,
2002 size
, &dma
->dma_paddr
, GFP_KERNEL
);
2004 if (likely(dma
->dma_vaddr
))
2006 dma
->dma_size
= size
;
2010 DPRINTF("could not allocate %d bytes of coherent memory.\n", size
);
2016 ubsec_dma_free(struct ubsec_softc
*sc
, struct ubsec_dma_alloc
*dma
)
2018 dma_free_coherent(sc
->sc_dv
, dma
->dma_size
, dma
->dma_vaddr
,
2023 * Resets the board. Values in the regesters are left as is
2024 * from the reset (i.e. initial values are assigned elsewhere).
2027 ubsec_reset_board(struct ubsec_softc
*sc
)
2029 volatile u_int32_t ctrl
;
2032 DPRINTF("%s()\n", __FUNCTION__
);
2034 DPRINTF("Send reset signal to chip.\n");
2036 ctrl
= READ_REG(sc
, BS_CTRL
);
2037 ctrl
|= BS_CTRL_RESET
;
2038 WRITE_REG(sc
, BS_CTRL
, ctrl
);
2041 * Wait aprox. 30 PCI clocks = 900 ns = 0.9 us
2047 * Init Broadcom registers
2050 ubsec_init_board(struct ubsec_softc
*sc
)
2055 DPRINTF("%s()\n", __FUNCTION__
);
2057 DPRINTF("Initialize chip.\n");
2059 ctrl
= READ_REG(sc
, BS_CTRL
);
2060 ctrl
&= ~(BS_CTRL_BE32
| BS_CTRL_BE64
);
2061 ctrl
|= BS_CTRL_LITTLE_ENDIAN
| BS_CTRL_MCR1INT
| BS_CTRL_DMAERR
;
2063 WRITE_REG(sc
, BS_CTRL
, ctrl
);
2065 /* Set chip capabilities (BCM5365P) */
2066 sc
->sc_flags
|= UBS_FLAGS_LONGCTX
| UBS_FLAGS_AES
;
2070 * Clean up after a chip crash.
2071 * It is assumed that the caller has spin_lock_irq(sc_ringmtx).
2074 ubsec_cleanchip(struct ubsec_softc
*sc
)
2079 DPRINTF("%s()\n", __FUNCTION__
);
2081 DPRINTF("Clean up queues after chip crash.\n");
2083 while (!BSD_SIMPLEQ_EMPTY(&sc
->sc_qchip
)) {
2084 q
= BSD_SIMPLEQ_FIRST(&sc
->sc_qchip
);
2085 BSD_SIMPLEQ_REMOVE_HEAD(&sc
->sc_qchip
, q_next
);
2086 ubsec_free_q(sc
, q
);
2092 * It is assumed that the caller has spin_lock_irq(sc_ringmtx).
2095 ubsec_free_q(struct ubsec_softc
*sc
, struct ubsec_q
*q
)
2098 struct cryptop
*crp
;
2103 DPRINTF("%s()\n", __FUNCTION__
);
2106 npkts
= q
->q_nstacked_mcrs
;
2108 for (i
= 0; i
< npkts
; i
++) {
2109 if(q
->q_stacked_mcr
[i
]) {
2110 q2
= q
->q_stacked_mcr
[i
];
2112 if ((q2
->q_dst_m
!= NULL
) && (q2
->q_src_m
!= q2
->q_dst_m
))
2114 m_freem(q2
->q_dst_m
);
2116 printk(KERN_ERR
"%s,%d: SKB not supported\n", __FILE__
, __LINE__
);
2119 crp
= (struct cryptop
*)q2
->q_crp
;
2121 BSD_SIMPLEQ_INSERT_TAIL(&sc
->sc_freequeue
, q2
, q_next
);
2123 crp
->crp_etype
= EFAULT
;
2133 if ((q
->q_dst_m
!= NULL
) && (q
->q_src_m
!= q
->q_dst_m
))
2135 m_freem(q
->q_dst_m
);
2137 printk(KERN_ERR
"%s,%d: SKB not supported\n", __FILE__
, __LINE__
);
2140 crp
= (struct cryptop
*)q
->q_crp
;
2142 BSD_SIMPLEQ_INSERT_TAIL(&sc
->sc_freequeue
, q
, q_next
);
2144 crp
->crp_etype
= EFAULT
;
2150 * Routine to reset the chip and clean up.
2151 * It is assumed that the caller has spin_lock_irq(sc_ringmtx).
2154 ubsec_totalreset(struct ubsec_softc
*sc
)
2158 DPRINTF("%s()\n", __FUNCTION__
);
2160 DPRINTF("initiate total chip reset.. \n");
2161 ubsec_reset_board(sc
);
2162 ubsec_init_board(sc
);
2163 ubsec_cleanchip(sc
);
2167 ubsec_dump_pb(struct ubsec_pktbuf
*pb
)
2169 printf("addr 0x%x (0x%x) next 0x%x\n",
2170 pb
->pb_addr
, pb
->pb_len
, pb
->pb_next
);
2174 ubsec_dump_mcr(struct ubsec_mcr
*mcr
)
2176 struct ubsec_mcr_add
*ma
;
2180 printf(" pkts: %u, flags 0x%x\n",
2181 letoh16(mcr
->mcr_pkts
), letoh16(mcr
->mcr_flags
));
2182 ma
= (struct ubsec_mcr_add
*)&mcr
->mcr_cmdctxp
;
2183 for (i
= 0; i
< letoh16(mcr
->mcr_pkts
); i
++) {
2184 printf(" %d: ctx 0x%x len 0x%x rsvd 0x%x\n", i
,
2185 letoh32(ma
->mcr_cmdctxp
), letoh16(ma
->mcr_pktlen
),
2186 letoh16(ma
->mcr_reserved
));
2187 printf(" %d: ipkt ", i
);
2188 ubsec_dump_pb(&ma
->mcr_ipktbuf
);
2189 printf(" %d: opkt ", i
);
2190 ubsec_dump_pb(&ma
->mcr_opktbuf
);
2193 printf("END MCR\n");
2196 static int __init
mod_init(void) {
2197 return ssb_driver_register(&ubsec_ssb_driver
);
2200 static void __exit
mod_exit(void) {
2201 ssb_driver_unregister(&ubsec_ssb_driver
);
2204 module_init(mod_init
);
2205 module_exit(mod_exit
);
2208 MODULE_AUTHOR("Daniel Mueller <daniel@danm.de>");
2209 MODULE_LICENSE("BSD");
2210 MODULE_DESCRIPTION("OCF driver for BCM5365P IPSec Core");
2211 MODULE_VERSION(DRV_MODULE_VERSION
);