Home | History | Annotate | Line # | Download | only in pci
hifn7751.c revision 1.21.6.1
      1  1.21.6.1      yamt /*	$NetBSD: hifn7751.c,v 1.21.6.1 2005/03/19 08:35:10 yamt Exp $	*/
      2      1.20  jonathan /*	$FreeBSD: hifn7751.c,v 1.5.2.7 2003/10/08 23:52:00 sam Exp $ */
      3      1.17   thorpej /*	$OpenBSD: hifn7751.c,v 1.140 2003/08/01 17:55:54 deraadt Exp $	*/
      4       1.1    itojun 
      5       1.1    itojun /*
      6      1.15  jonathan  * Invertex AEON / Hifn 7751 driver
      7       1.1    itojun  * Copyright (c) 1999 Invertex Inc. All rights reserved.
      8       1.1    itojun  * Copyright (c) 1999 Theo de Raadt
      9      1.15  jonathan  * Copyright (c) 2000-2001 Network Security Technologies, Inc.
     10       1.1    itojun  *			http://www.netsec.net
     11      1.20  jonathan  * Copyright (c) 2003 Hifn Inc.
     12       1.1    itojun  *
     13       1.1    itojun  * This driver is based on a previous driver by Invertex, for which they
     14       1.1    itojun  * requested:  Please send any comments, feedback, bug-fixes, or feature
     15       1.1    itojun  * requests to software (at) invertex.com.
     16       1.1    itojun  *
     17       1.1    itojun  * Redistribution and use in source and binary forms, with or without
     18       1.1    itojun  * modification, are permitted provided that the following conditions
     19       1.1    itojun  * are met:
     20       1.1    itojun  *
     21       1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     22       1.1    itojun  *   notice, this list of conditions and the following disclaimer.
     23       1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     24       1.1    itojun  *   notice, this list of conditions and the following disclaimer in the
     25       1.1    itojun  *   documentation and/or other materials provided with the distribution.
     26       1.1    itojun  * 3. The name of the author may not be used to endorse or promote products
     27       1.1    itojun  *   derived from this software without specific prior written permission.
     28       1.1    itojun  *
     29       1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     30       1.1    itojun  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     31       1.1    itojun  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     32       1.1    itojun  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     33       1.1    itojun  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     34       1.1    itojun  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     35       1.1    itojun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     36       1.1    itojun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     37       1.1    itojun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     38       1.1    itojun  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     39      1.15  jonathan  *
     40      1.15  jonathan  * Effort sponsored in part by the Defense Advanced Research Projects
     41      1.15  jonathan  * Agency (DARPA) and Air Force Research Laboratory, Air Force
     42      1.15  jonathan  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
     43      1.15  jonathan  *
     44       1.1    itojun  */
     45       1.1    itojun 
     46       1.1    itojun /*
     47      1.20  jonathan  * Driver for various  Hifn pre-HIPP encryption processors.
     48       1.1    itojun  */
     49       1.6     lukem 
     50       1.6     lukem #include <sys/cdefs.h>
     51  1.21.6.1      yamt __KERNEL_RCSID(0, "$NetBSD: hifn7751.c,v 1.21.6.1 2005/03/19 08:35:10 yamt Exp $");
     52      1.15  jonathan 
     53      1.15  jonathan #include "rnd.h"
     54      1.15  jonathan #include "opencrypto.h"
     55      1.15  jonathan 
     56      1.15  jonathan #if NRND == 0 || NOPENCRYPTO == 0
     57      1.15  jonathan #error hifn7751 requires rnd and opencrypto pseudo-devices
     58      1.15  jonathan #endif
     59      1.15  jonathan 
     60       1.1    itojun 
     61       1.1    itojun #include <sys/param.h>
     62       1.1    itojun #include <sys/systm.h>
     63       1.1    itojun #include <sys/proc.h>
     64       1.1    itojun #include <sys/errno.h>
     65       1.1    itojun #include <sys/malloc.h>
     66       1.1    itojun #include <sys/kernel.h>
     67       1.1    itojun #include <sys/mbuf.h>
     68      1.15  jonathan #include <sys/device.h>
     69      1.15  jonathan 
     70       1.1    itojun #include <uvm/uvm_extern.h>
     71      1.15  jonathan 
     72       1.1    itojun 
     73       1.1    itojun #ifdef __OpenBSD__
     74       1.1    itojun #include <crypto/crypto.h>
     75       1.1    itojun #include <dev/rndvar.h>
     76      1.15  jonathan #else
     77      1.15  jonathan #include <opencrypto/cryptodev.h>
     78      1.15  jonathan #include <sys/rnd.h>
     79       1.1    itojun #endif
     80       1.1    itojun 
     81       1.1    itojun #include <dev/pci/pcireg.h>
     82       1.1    itojun #include <dev/pci/pcivar.h>
     83       1.1    itojun #include <dev/pci/pcidevs.h>
     84       1.1    itojun 
     85      1.15  jonathan #include <dev/pci/hifn7751reg.h>
     86       1.1    itojun #include <dev/pci/hifn7751var.h>
     87       1.1    itojun 
     88       1.1    itojun #undef HIFN_DEBUG
     89       1.1    itojun 
     90      1.15  jonathan #ifdef __NetBSD__
     91      1.15  jonathan #define	HIFN_NO_RNG			/* until statistically tested */
     92      1.15  jonathan #define M_DUP_PKTHDR M_COPY_PKTHDR	/* XXX */
     93      1.15  jonathan #endif
     94      1.15  jonathan 
     95      1.15  jonathan #ifdef HIFN_DEBUG
     96      1.15  jonathan extern int hifn_debug;		/* patchable */
     97      1.15  jonathan int hifn_debug = 1;
     98      1.15  jonathan #endif
     99      1.15  jonathan 
    100      1.15  jonathan #ifdef __OpenBSD__
    101      1.15  jonathan #define HAVE_CRYPTO_LZS		/* OpenBSD OCF supports CRYPTO_COMP_LZS */
    102      1.15  jonathan #endif
    103      1.15  jonathan 
    104       1.1    itojun /*
    105       1.1    itojun  * Prototypes and count for the pci_device structure
    106       1.1    itojun  */
    107       1.1    itojun #ifdef __OpenBSD__
    108      1.15  jonathan int hifn_probe((struct device *, void *, void *);
    109       1.1    itojun #else
    110      1.15  jonathan int hifn_probe(struct device *, struct cfdata *, void *);
    111       1.1    itojun #endif
    112      1.15  jonathan void hifn_attach(struct device *, struct device *, void *);
    113       1.1    itojun 
    114       1.9   thorpej CFATTACH_DECL(hifn, sizeof(struct hifn_softc),
    115      1.10   thorpej     hifn_probe, hifn_attach, NULL, NULL);
    116       1.1    itojun 
    117       1.1    itojun #ifdef __OpenBSD__
    118       1.1    itojun struct cfdriver hifn_cd = {
    119       1.1    itojun 	0, "hifn", DV_DULL
    120       1.1    itojun };
    121       1.1    itojun #endif
    122       1.1    itojun 
    123      1.15  jonathan void	hifn_reset_board(struct hifn_softc *, int);
    124      1.15  jonathan void	hifn_reset_puc(struct hifn_softc *);
    125      1.15  jonathan void	hifn_puc_wait(struct hifn_softc *);
    126      1.16   thorpej const char *hifn_enable_crypto(struct hifn_softc *, pcireg_t);
    127      1.15  jonathan void	hifn_set_retry(struct hifn_softc *);
    128      1.15  jonathan void	hifn_init_dma(struct hifn_softc *);
    129      1.15  jonathan void	hifn_init_pci_registers(struct hifn_softc *);
    130      1.15  jonathan int	hifn_sramsize(struct hifn_softc *);
    131      1.15  jonathan int	hifn_dramsize(struct hifn_softc *);
    132      1.15  jonathan int	hifn_ramtype(struct hifn_softc *);
    133      1.15  jonathan void	hifn_sessions(struct hifn_softc *);
    134      1.15  jonathan int	hifn_intr(void *);
    135      1.15  jonathan u_int	hifn_write_command(struct hifn_command *, u_int8_t *);
    136      1.15  jonathan u_int32_t hifn_next_signature(u_int32_t a, u_int cnt);
    137      1.15  jonathan int	hifn_newsession(void*, u_int32_t *, struct cryptoini *);
    138      1.15  jonathan int	hifn_freesession(void*, u_int64_t);
    139      1.15  jonathan int	hifn_process(void*, struct cryptop *, int);
    140      1.15  jonathan void	hifn_callback(struct hifn_softc *, struct hifn_command *, u_int8_t *);
    141      1.15  jonathan int	hifn_crypto(struct hifn_softc *, struct hifn_command *,
    142      1.15  jonathan 		    struct cryptop*, int);
    143      1.15  jonathan int	hifn_readramaddr(struct hifn_softc *, int, u_int8_t *);
    144      1.15  jonathan int	hifn_writeramaddr(struct hifn_softc *, int, u_int8_t *);
    145      1.15  jonathan int	hifn_dmamap_aligned(bus_dmamap_t);
    146      1.15  jonathan int	hifn_dmamap_load_src(struct hifn_softc *, struct hifn_command *);
    147      1.15  jonathan int	hifn_dmamap_load_dst(struct hifn_softc *, struct hifn_command *);
    148      1.15  jonathan int	hifn_init_pubrng(struct hifn_softc *);
    149      1.15  jonathan #ifndef HIFN_NO_RNG
    150      1.15  jonathan static	void hifn_rng(void *);
    151      1.15  jonathan #endif
    152      1.15  jonathan void	hifn_tick(void *);
    153      1.15  jonathan void	hifn_abort(struct hifn_softc *);
    154      1.15  jonathan void	hifn_alloc_slot(struct hifn_softc *, int *, int *, int *, int *);
    155      1.15  jonathan void	hifn_write_4(struct hifn_softc *, int, bus_size_t, u_int32_t);
    156      1.15  jonathan u_int32_t hifn_read_4(struct hifn_softc *, int, bus_size_t);
    157      1.15  jonathan #ifdef	HAVE_CRYPTO_LZS
    158      1.15  jonathan int	hifn_compression(struct hifn_softc *, struct cryptop *,
    159      1.15  jonathan 			 struct hifn_command *);
    160      1.15  jonathan struct mbuf *hifn_mkmbuf_chain(int, struct mbuf *);
    161      1.15  jonathan int	hifn_compress_enter(struct hifn_softc *, struct hifn_command *);
    162      1.15  jonathan void	hifn_callback_comp(struct hifn_softc *, struct hifn_command *,
    163      1.15  jonathan 			   u_int8_t *);
    164      1.15  jonathan #endif	/* HAVE_CRYPTO_LZS */
    165      1.15  jonathan 
    166      1.15  jonathan 
    167      1.15  jonathan #ifdef	notyet
    168      1.15  jonathan int	hifn_compression(struct hifn_softc *, struct cryptop *,
    169      1.15  jonathan     struct hifn_command *);
    170      1.15  jonathan struct mbuf *hifn_mkmbuf_chain(int, struct mbuf *);
    171      1.15  jonathan int	hifn_compress_enter(struct hifn_softc *, struct hifn_command *);
    172      1.15  jonathan void	hifn_callback_comp(struct hifn_softc *, struct hifn_command *,
    173      1.15  jonathan     u_int8_t *);
    174      1.15  jonathan #endif
    175      1.15  jonathan 
    176      1.15  jonathan struct hifn_stats hifnstats;
    177       1.1    itojun 
    178      1.16   thorpej static const struct hifn_product {
    179      1.16   thorpej 	pci_vendor_id_t		hifn_vendor;
    180      1.16   thorpej 	pci_product_id_t	hifn_product;
    181      1.16   thorpej 	int			hifn_flags;
    182      1.16   thorpej 	const char		*hifn_name;
    183      1.16   thorpej } hifn_products[] = {
    184      1.16   thorpej 	{ PCI_VENDOR_INVERTEX,	PCI_PRODUCT_INVERTEX_AEON,
    185      1.16   thorpej 	  0,
    186      1.16   thorpej 	  "Invertex AEON",
    187      1.16   thorpej 	},
    188      1.16   thorpej 
    189      1.16   thorpej 	{ PCI_VENDOR_HIFN,	PCI_PRODUCT_HIFN_7751,
    190      1.16   thorpej 	  0,
    191      1.18   thorpej 	  "Hifn 7751",
    192      1.16   thorpej 	},
    193      1.16   thorpej 	{ PCI_VENDOR_NETSEC,	PCI_PRODUCT_NETSEC_7751,
    194      1.16   thorpej 	  0,
    195      1.18   thorpej 	  "Hifn 7751 (NetSec)"
    196      1.16   thorpej 	},
    197      1.16   thorpej 
    198      1.16   thorpej 	{ PCI_VENDOR_HIFN,	PCI_PRODUCT_HIFN_7811,
    199      1.16   thorpej 	  HIFN_IS_7811 | HIFN_HAS_RNG | HIFN_HAS_LEDS | HIFN_NO_BURSTWRITE,
    200      1.18   thorpej 	  "Hifn 7811",
    201      1.16   thorpej 	},
    202      1.16   thorpej 
    203      1.16   thorpej 	{ PCI_VENDOR_HIFN,	PCI_PRODUCT_HIFN_7951,
    204      1.16   thorpej 	  HIFN_HAS_RNG | HIFN_HAS_PUBLIC,
    205      1.18   thorpej 	  "Hifn 7951",
    206      1.16   thorpej 	},
    207      1.16   thorpej 
    208      1.20  jonathan 	{ PCI_VENDOR_HIFN,	PCI_PRODUCT_HIFN_7955,
    209      1.20  jonathan 	  HIFN_HAS_RNG | HIFN_HAS_PUBLIC | HIFN_IS_7956 | HIFN_HAS_AES,
    210      1.20  jonathan 	  "Hifn 7955",
    211      1.20  jonathan 	},
    212      1.20  jonathan 
    213      1.20  jonathan 	{ PCI_VENDOR_HIFN,	PCI_PRODUCT_HIFN_7956,
    214      1.20  jonathan 	  HIFN_HAS_RNG | HIFN_HAS_PUBLIC | HIFN_IS_7956 | HIFN_HAS_AES,
    215      1.20  jonathan 	  "Hifn 7956",
    216      1.20  jonathan 	},
    217      1.20  jonathan 
    218      1.16   thorpej 
    219      1.16   thorpej 	{ 0,			0,
    220      1.16   thorpej 	  0,
    221      1.16   thorpej 	  NULL
    222      1.16   thorpej 	}
    223      1.16   thorpej };
    224      1.16   thorpej 
    225      1.16   thorpej static const struct hifn_product *
    226      1.16   thorpej hifn_lookup(const struct pci_attach_args *pa)
    227      1.16   thorpej {
    228      1.16   thorpej 	const struct hifn_product *hp;
    229      1.16   thorpej 
    230      1.16   thorpej 	for (hp = hifn_products; hp->hifn_name != NULL; hp++) {
    231      1.16   thorpej 		if (PCI_VENDOR(pa->pa_id) == hp->hifn_vendor &&
    232      1.16   thorpej 		    PCI_PRODUCT(pa->pa_id) == hp->hifn_product)
    233      1.16   thorpej 			return (hp);
    234      1.16   thorpej 	}
    235      1.16   thorpej 	return (NULL);
    236      1.16   thorpej }
    237      1.16   thorpej 
    238       1.1    itojun int
    239      1.17   thorpej hifn_probe(struct device *parent, struct cfdata *match, void *aux)
    240       1.1    itojun {
    241       1.1    itojun 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
    242       1.1    itojun 
    243      1.16   thorpej 	if (hifn_lookup(pa) != NULL)
    244       1.1    itojun 		return (1);
    245      1.16   thorpej 
    246       1.1    itojun 	return (0);
    247       1.1    itojun }
    248       1.1    itojun 
    249  1.21.6.1      yamt void
    250      1.17   thorpej hifn_attach(struct device *parent, struct device *self, void *aux)
    251       1.1    itojun {
    252       1.1    itojun 	struct hifn_softc *sc = (struct hifn_softc *)self;
    253       1.1    itojun 	struct pci_attach_args *pa = aux;
    254      1.16   thorpej 	const struct hifn_product *hp;
    255       1.1    itojun 	pci_chipset_tag_t pc = pa->pa_pc;
    256       1.1    itojun 	pci_intr_handle_t ih;
    257       1.1    itojun 	const char *intrstr = NULL;
    258      1.16   thorpej 	const char *hifncap;
    259       1.1    itojun 	char rbase;
    260       1.1    itojun 	bus_size_t iosize0, iosize1;
    261       1.1    itojun 	u_int32_t cmd;
    262       1.1    itojun 	u_int16_t ena;
    263       1.1    itojun 	bus_dma_segment_t seg;
    264       1.1    itojun 	bus_dmamap_t dmamap;
    265       1.1    itojun 	int rseg;
    266       1.1    itojun 	caddr_t kva;
    267       1.1    itojun 
    268      1.16   thorpej 	hp = hifn_lookup(pa);
    269      1.16   thorpej 	if (hp == NULL) {
    270      1.16   thorpej 		printf("\n");
    271      1.16   thorpej 		panic("hifn_attach: impossible");
    272      1.16   thorpej 	}
    273      1.16   thorpej 
    274      1.13   thorpej 	aprint_naive(": Crypto processor\n");
    275      1.16   thorpej 	aprint_normal(": %s, rev. %d\n", hp->hifn_name,
    276      1.16   thorpej 	    PCI_REVISION(pa->pa_class));
    277      1.13   thorpej 
    278      1.15  jonathan 	sc->sc_pci_pc = pa->pa_pc;
    279      1.15  jonathan 	sc->sc_pci_tag = pa->pa_tag;
    280      1.15  jonathan 
    281      1.16   thorpej 	sc->sc_flags = hp->hifn_flags;
    282      1.15  jonathan 
    283       1.1    itojun 	cmd = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    284      1.16   thorpej 	cmd |= PCI_COMMAND_MASTER_ENABLE;
    285       1.1    itojun 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, cmd);
    286       1.1    itojun 
    287       1.1    itojun 	if (pci_mapreg_map(pa, HIFN_BAR0, PCI_MAPREG_TYPE_MEM, 0,
    288       1.1    itojun 	    &sc->sc_st0, &sc->sc_sh0, NULL, &iosize0)) {
    289      1.16   thorpej 		aprint_error("%s: can't map mem space %d\n",
    290      1.16   thorpej 		    sc->sc_dv.dv_xname, 0);
    291       1.1    itojun 		return;
    292       1.1    itojun 	}
    293       1.1    itojun 
    294       1.1    itojun 	if (pci_mapreg_map(pa, HIFN_BAR1, PCI_MAPREG_TYPE_MEM, 0,
    295       1.1    itojun 	    &sc->sc_st1, &sc->sc_sh1, NULL, &iosize1)) {
    296      1.16   thorpej 		aprint_error("%s: can't find mem space %d\n",
    297      1.16   thorpej 		    sc->sc_dv.dv_xname, 1);
    298       1.1    itojun 		goto fail_io0;
    299       1.1    itojun 	}
    300       1.1    itojun 
    301      1.15  jonathan 	hifn_set_retry(sc);
    302      1.15  jonathan 
    303      1.15  jonathan 	if (sc->sc_flags & HIFN_NO_BURSTWRITE) {
    304      1.15  jonathan 		sc->sc_waw_lastgroup = -1;
    305      1.15  jonathan 		sc->sc_waw_lastreg = 1;
    306      1.15  jonathan 	}
    307      1.15  jonathan 
    308       1.1    itojun 	sc->sc_dmat = pa->pa_dmat;
    309       1.1    itojun 	if (bus_dmamem_alloc(sc->sc_dmat, sizeof(*sc->sc_dma), PAGE_SIZE, 0,
    310       1.1    itojun 	    &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
    311      1.16   thorpej 		aprint_error("%s: can't alloc DMA buffer\n",
    312      1.16   thorpej 		    sc->sc_dv.dv_xname);
    313       1.1    itojun 		goto fail_io1;
    314       1.1    itojun         }
    315       1.1    itojun 	if (bus_dmamem_map(sc->sc_dmat, &seg, rseg, sizeof(*sc->sc_dma), &kva,
    316       1.1    itojun 	    BUS_DMA_NOWAIT)) {
    317      1.16   thorpej 		aprint_error("%s: can't map DMA buffers (%lu bytes)\n",
    318      1.16   thorpej 		    sc->sc_dv.dv_xname, (u_long)sizeof(*sc->sc_dma));
    319       1.1    itojun 		bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    320       1.1    itojun 		goto fail_io1;
    321       1.1    itojun 	}
    322       1.1    itojun 	if (bus_dmamap_create(sc->sc_dmat, sizeof(*sc->sc_dma), 1,
    323       1.1    itojun 	    sizeof(*sc->sc_dma), 0, BUS_DMA_NOWAIT, &dmamap)) {
    324      1.16   thorpej 		aprint_error("%s: can't create DMA map\n",
    325      1.16   thorpej 		    sc->sc_dv.dv_xname);
    326       1.1    itojun 		bus_dmamem_unmap(sc->sc_dmat, kva, sizeof(*sc->sc_dma));
    327       1.1    itojun 		bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    328       1.1    itojun 		goto fail_io1;
    329       1.1    itojun 	}
    330       1.1    itojun 	if (bus_dmamap_load(sc->sc_dmat, dmamap, kva, sizeof(*sc->sc_dma),
    331       1.1    itojun 	    NULL, BUS_DMA_NOWAIT)) {
    332      1.16   thorpej 		aprint_error("%s: can't load DMA map\n",
    333      1.16   thorpej 		    sc->sc_dv.dv_xname);
    334       1.1    itojun 		bus_dmamap_destroy(sc->sc_dmat, dmamap);
    335       1.1    itojun 		bus_dmamem_unmap(sc->sc_dmat, kva, sizeof(*sc->sc_dma));
    336       1.1    itojun 		bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    337       1.1    itojun 		goto fail_io1;
    338       1.1    itojun 	}
    339      1.15  jonathan 	sc->sc_dmamap = dmamap;
    340       1.1    itojun 	sc->sc_dma = (struct hifn_dma *)kva;
    341      1.15  jonathan 	bzero(sc->sc_dma, sizeof(*sc->sc_dma));
    342       1.1    itojun 
    343      1.15  jonathan 	hifn_reset_board(sc, 0);
    344       1.1    itojun 
    345      1.16   thorpej 	if ((hifncap = hifn_enable_crypto(sc, pa->pa_id)) == NULL) {
    346      1.13   thorpej 		aprint_error("%s: crypto enabling failed\n",
    347      1.13   thorpej 		    sc->sc_dv.dv_xname);
    348       1.1    itojun 		goto fail_mem;
    349       1.1    itojun 	}
    350      1.15  jonathan 	hifn_reset_puc(sc);
    351       1.1    itojun 
    352       1.1    itojun 	hifn_init_dma(sc);
    353       1.1    itojun 	hifn_init_pci_registers(sc);
    354       1.1    itojun 
    355      1.20  jonathan 	/* XXX can't dynamically determine ram type for 795x; force dram */
    356      1.20  jonathan 	if (sc->sc_flags & HIFN_IS_7956)
    357      1.20  jonathan 		sc->sc_drammodel = 1;
    358      1.20  jonathan 	else if (hifn_ramtype(sc))
    359      1.15  jonathan 		goto fail_mem;
    360       1.1    itojun 
    361       1.1    itojun 	if (sc->sc_drammodel == 0)
    362       1.1    itojun 		hifn_sramsize(sc);
    363       1.1    itojun 	else
    364       1.1    itojun 		hifn_dramsize(sc);
    365       1.1    itojun 
    366      1.15  jonathan 	/*
    367      1.15  jonathan 	 * Workaround for NetSec 7751 rev A: half ram size because two
    368      1.15  jonathan 	 * of the address lines were left floating
    369      1.15  jonathan 	 */
    370       1.1    itojun 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NETSEC &&
    371       1.1    itojun 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NETSEC_7751 &&
    372       1.1    itojun 	    PCI_REVISION(pa->pa_class) == 0x61)
    373       1.1    itojun 		sc->sc_ramsize >>= 1;
    374       1.1    itojun 
    375       1.2  sommerfe 	if (pci_intr_map(pa, &ih)) {
    376      1.16   thorpej 		aprint_error("%s: couldn't map interrupt\n",
    377      1.16   thorpej 		    sc->sc_dv.dv_xname);
    378       1.1    itojun 		goto fail_mem;
    379       1.1    itojun 	}
    380       1.1    itojun 	intrstr = pci_intr_string(pc, ih);
    381      1.15  jonathan #ifdef	__OpenBSD__
    382       1.1    itojun 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, hifn_intr, sc,
    383       1.1    itojun 	    self->dv_xname);
    384       1.1    itojun #else
    385       1.1    itojun 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, hifn_intr, sc);
    386       1.1    itojun #endif
    387       1.1    itojun 	if (sc->sc_ih == NULL) {
    388      1.16   thorpej 		aprint_error("%s: couldn't establish interrupt\n",
    389      1.16   thorpej 		    sc->sc_dv.dv_xname);
    390       1.1    itojun 		if (intrstr != NULL)
    391      1.13   thorpej 			aprint_normal(" at %s", intrstr);
    392      1.13   thorpej 		aprint_normal("\n");
    393       1.1    itojun 		goto fail_mem;
    394       1.1    itojun 	}
    395       1.1    itojun 
    396       1.1    itojun 	hifn_sessions(sc);
    397       1.1    itojun 
    398       1.1    itojun 	rseg = sc->sc_ramsize / 1024;
    399       1.1    itojun 	rbase = 'K';
    400       1.1    itojun 	if (sc->sc_ramsize >= (1024 * 1024)) {
    401       1.1    itojun 		rbase = 'M';
    402       1.1    itojun 		rseg /= 1024;
    403       1.1    itojun 	}
    404      1.16   thorpej 	aprint_normal("%s: %s, %d%cB %cram, interrupting at %s\n",
    405      1.16   thorpej 	    sc->sc_dv.dv_xname, hifncap, rseg, rbase,
    406       1.1    itojun 	    sc->sc_drammodel ? 'd' : 's', intrstr);
    407       1.1    itojun 
    408      1.15  jonathan 	sc->sc_cid = crypto_get_driverid(0);
    409      1.15  jonathan 	if (sc->sc_cid < 0) {
    410      1.16   thorpej 		aprint_error("%s: couldn't get crypto driver id\n",
    411      1.16   thorpej 		    sc->sc_dv.dv_xname);
    412       1.1    itojun 		goto fail_intr;
    413      1.15  jonathan 	}
    414       1.1    itojun 
    415       1.1    itojun 	WRITE_REG_0(sc, HIFN_0_PUCNFG,
    416       1.1    itojun 	    READ_REG_0(sc, HIFN_0_PUCNFG) | HIFN_PUCNFG_CHIPID);
    417       1.1    itojun 	ena = READ_REG_0(sc, HIFN_0_PUSTAT) & HIFN_PUSTAT_CHIPENA;
    418       1.1    itojun 
    419       1.1    itojun 	switch (ena) {
    420       1.1    itojun 	case HIFN_PUSTAT_ENA_2:
    421      1.15  jonathan 		crypto_register(sc->sc_cid, CRYPTO_3DES_CBC, 0, 0,
    422      1.15  jonathan 		    hifn_newsession, hifn_freesession, hifn_process, sc);
    423      1.15  jonathan 		crypto_register(sc->sc_cid, CRYPTO_ARC4, 0, 0,
    424      1.15  jonathan 		    hifn_newsession, hifn_freesession, hifn_process, sc);
    425      1.20  jonathan 		if (sc->sc_flags & HIFN_HAS_AES)
    426      1.20  jonathan 			crypto_register(sc->sc_cid, CRYPTO_AES_CBC,  0, 0,
    427      1.20  jonathan 				hifn_newsession, hifn_freesession,
    428      1.20  jonathan 				hifn_process, sc);
    429       1.1    itojun 		/*FALLTHROUGH*/
    430       1.1    itojun 	case HIFN_PUSTAT_ENA_1:
    431      1.15  jonathan 		crypto_register(sc->sc_cid, CRYPTO_MD5, 0, 0,
    432      1.15  jonathan 		    hifn_newsession, hifn_freesession, hifn_process, sc);
    433      1.15  jonathan 		crypto_register(sc->sc_cid, CRYPTO_SHA1, 0, 0,
    434      1.15  jonathan 		    hifn_newsession, hifn_freesession, hifn_process, sc);
    435      1.15  jonathan 		crypto_register(sc->sc_cid, CRYPTO_MD5_HMAC, 0, 0,
    436      1.15  jonathan 		    hifn_newsession, hifn_freesession, hifn_process, sc);
    437      1.15  jonathan 		crypto_register(sc->sc_cid, CRYPTO_SHA1_HMAC, 0, 0,
    438      1.15  jonathan 		    hifn_newsession, hifn_freesession, hifn_process, sc);
    439      1.15  jonathan 		crypto_register(sc->sc_cid, CRYPTO_DES_CBC, 0, 0,
    440      1.15  jonathan 		    hifn_newsession, hifn_freesession, hifn_process, sc);
    441      1.15  jonathan 		break;
    442       1.1    itojun 	}
    443      1.15  jonathan 
    444      1.15  jonathan 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, 0,
    445      1.15  jonathan 	    sc->sc_dmamap->dm_mapsize,
    446      1.15  jonathan 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    447      1.15  jonathan 
    448      1.15  jonathan 	if (sc->sc_flags & (HIFN_HAS_PUBLIC | HIFN_HAS_RNG))
    449      1.15  jonathan 		hifn_init_pubrng(sc);
    450      1.15  jonathan 
    451      1.15  jonathan #ifdef	__OpenBSD__
    452      1.15  jonathan 	timeout_set(&sc->sc_tickto, hifn_tick, sc);
    453      1.15  jonathan 	timeout_add(&sc->sc_tickto, hz);
    454      1.15  jonathan #else
    455      1.15  jonathan 	callout_init(&sc->sc_tickto);
    456      1.15  jonathan 	callout_reset(&sc->sc_tickto, hz, hifn_tick, sc);
    457       1.1    itojun #endif
    458       1.1    itojun 	return;
    459       1.1    itojun 
    460       1.1    itojun fail_intr:
    461       1.1    itojun 	pci_intr_disestablish(pc, sc->sc_ih);
    462       1.1    itojun fail_mem:
    463       1.1    itojun 	bus_dmamap_unload(sc->sc_dmat, dmamap);
    464       1.1    itojun 	bus_dmamap_destroy(sc->sc_dmat, dmamap);
    465       1.1    itojun 	bus_dmamem_unmap(sc->sc_dmat, kva, sizeof(*sc->sc_dma));
    466       1.1    itojun 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    467      1.15  jonathan 
    468      1.15  jonathan 	/* Turn off DMA polling */
    469      1.15  jonathan 	WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
    470      1.15  jonathan 	    HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
    471      1.15  jonathan 
    472       1.1    itojun fail_io1:
    473       1.1    itojun 	bus_space_unmap(sc->sc_st1, sc->sc_sh1, iosize1);
    474       1.1    itojun fail_io0:
    475       1.1    itojun 	bus_space_unmap(sc->sc_st0, sc->sc_sh0, iosize0);
    476       1.1    itojun }
    477       1.1    itojun 
    478      1.15  jonathan int
    479      1.17   thorpej hifn_init_pubrng(struct hifn_softc *sc)
    480      1.15  jonathan {
    481      1.15  jonathan 	u_int32_t r;
    482      1.15  jonathan 	int i;
    483      1.15  jonathan 
    484      1.15  jonathan 	if ((sc->sc_flags & HIFN_IS_7811) == 0) {
    485      1.15  jonathan 		/* Reset 7951 public key/rng engine */
    486      1.15  jonathan 		WRITE_REG_1(sc, HIFN_1_PUB_RESET,
    487      1.15  jonathan 		    READ_REG_1(sc, HIFN_1_PUB_RESET) | HIFN_PUBRST_RESET);
    488      1.15  jonathan 
    489      1.15  jonathan 		for (i = 0; i < 100; i++) {
    490      1.15  jonathan 			DELAY(1000);
    491      1.15  jonathan 			if ((READ_REG_1(sc, HIFN_1_PUB_RESET) &
    492      1.15  jonathan 			    HIFN_PUBRST_RESET) == 0)
    493      1.15  jonathan 				break;
    494      1.15  jonathan 		}
    495      1.15  jonathan 
    496      1.15  jonathan 		if (i == 100) {
    497      1.15  jonathan 			printf("%s: public key init failed\n",
    498      1.15  jonathan 			    sc->sc_dv.dv_xname);
    499      1.15  jonathan 			return (1);
    500      1.15  jonathan 		}
    501      1.15  jonathan 	}
    502      1.15  jonathan 
    503      1.15  jonathan 	/* Enable the rng, if available */
    504      1.15  jonathan 	if (sc->sc_flags & HIFN_HAS_RNG) {
    505      1.15  jonathan 		if (sc->sc_flags & HIFN_IS_7811) {
    506      1.15  jonathan 			r = READ_REG_1(sc, HIFN_1_7811_RNGENA);
    507      1.15  jonathan 			if (r & HIFN_7811_RNGENA_ENA) {
    508      1.15  jonathan 				r &= ~HIFN_7811_RNGENA_ENA;
    509      1.15  jonathan 				WRITE_REG_1(sc, HIFN_1_7811_RNGENA, r);
    510      1.15  jonathan 			}
    511      1.15  jonathan 			WRITE_REG_1(sc, HIFN_1_7811_RNGCFG,
    512      1.15  jonathan 			    HIFN_7811_RNGCFG_DEFL);
    513      1.15  jonathan 			r |= HIFN_7811_RNGENA_ENA;
    514      1.15  jonathan 			WRITE_REG_1(sc, HIFN_1_7811_RNGENA, r);
    515      1.15  jonathan 		} else
    516      1.15  jonathan 			WRITE_REG_1(sc, HIFN_1_RNG_CONFIG,
    517      1.15  jonathan 			    READ_REG_1(sc, HIFN_1_RNG_CONFIG) |
    518      1.15  jonathan 			    HIFN_RNGCFG_ENA);
    519      1.15  jonathan 
    520      1.15  jonathan 		sc->sc_rngfirst = 1;
    521      1.15  jonathan 		if (hz >= 100)
    522      1.15  jonathan 			sc->sc_rnghz = hz / 100;
    523      1.15  jonathan 		else
    524      1.15  jonathan 			sc->sc_rnghz = 1;
    525      1.15  jonathan #ifndef	HIFN_NO_RNG
    526      1.15  jonathan #ifdef	__OpenBSD__
    527      1.15  jonathan 		timeout_set(&sc->sc_rngto, hifn_rng, sc);
    528      1.15  jonathan 		timeout_add(&sc->sc_rngto, sc->sc_rnghz);
    529      1.15  jonathan #else	/* !__OpenBSD__ */
    530      1.15  jonathan 		callout_init(&sc->sc_rngto);
    531      1.15  jonathan 		callout_reset(&sc->sc_rngto, sc->sc_rnghz, hifn_rng, sc);
    532      1.15  jonathan #endif	/* !__OpenBSD__ */
    533      1.15  jonathan #endif	/* HIFN_NO_RNG */
    534      1.15  jonathan 	}
    535      1.15  jonathan 
    536      1.15  jonathan 	/* Enable public key engine, if available */
    537      1.15  jonathan 	if (sc->sc_flags & HIFN_HAS_PUBLIC) {
    538      1.15  jonathan 		WRITE_REG_1(sc, HIFN_1_PUB_IEN, HIFN_PUBIEN_DONE);
    539      1.15  jonathan 		sc->sc_dmaier |= HIFN_DMAIER_PUBDONE;
    540      1.15  jonathan 		WRITE_REG_1(sc, HIFN_1_DMA_IER, sc->sc_dmaier);
    541      1.15  jonathan 	}
    542      1.15  jonathan 
    543      1.15  jonathan 	return (0);
    544      1.15  jonathan }
    545      1.15  jonathan 
    546      1.15  jonathan #ifndef HIFN_NO_RNG
    547      1.15  jonathan static void
    548      1.17   thorpej hifn_rng(void *vsc)
    549      1.15  jonathan {
    550      1.15  jonathan #ifndef	__NetBSD__
    551      1.15  jonathan 	struct hifn_softc *sc = vsc;
    552      1.15  jonathan 	u_int32_t num1, sts, num2;
    553      1.15  jonathan 	int i;
    554      1.15  jonathan 
    555      1.15  jonathan 	if (sc->sc_flags & HIFN_IS_7811) {
    556      1.15  jonathan 		for (i = 0; i < 5; i++) {
    557      1.15  jonathan 			sts = READ_REG_1(sc, HIFN_1_7811_RNGSTS);
    558      1.15  jonathan 			if (sts & HIFN_7811_RNGSTS_UFL) {
    559      1.15  jonathan 				printf("%s: RNG underflow: disabling\n",
    560      1.15  jonathan 				    sc->sc_dv.dv_xname);
    561      1.15  jonathan 				return;
    562      1.15  jonathan 			}
    563      1.15  jonathan 			if ((sts & HIFN_7811_RNGSTS_RDY) == 0)
    564      1.15  jonathan 				break;
    565      1.15  jonathan 
    566      1.15  jonathan 			/*
    567      1.15  jonathan 			 * There are at least two words in the RNG FIFO
    568      1.15  jonathan 			 * at this point.
    569      1.15  jonathan 			 */
    570      1.15  jonathan 			num1 = READ_REG_1(sc, HIFN_1_7811_RNGDAT);
    571      1.15  jonathan 			num2 = READ_REG_1(sc, HIFN_1_7811_RNGDAT);
    572      1.15  jonathan 			if (sc->sc_rngfirst)
    573      1.15  jonathan 				sc->sc_rngfirst = 0;
    574      1.15  jonathan 			else {
    575      1.15  jonathan 				add_true_randomness(num1);
    576      1.15  jonathan 				add_true_randomness(num2);
    577      1.15  jonathan 			}
    578      1.15  jonathan 		}
    579      1.15  jonathan 	} else {
    580      1.15  jonathan 		num1 = READ_REG_1(sc, HIFN_1_RNG_DATA);
    581      1.15  jonathan 
    582      1.15  jonathan 		if (sc->sc_rngfirst)
    583      1.15  jonathan 			sc->sc_rngfirst = 0;
    584      1.15  jonathan 		else
    585      1.15  jonathan 			add_true_randomness(num1);
    586      1.15  jonathan 	}
    587      1.15  jonathan 
    588      1.15  jonathan #ifdef	__OpenBSD__
    589      1.15  jonathan 	timeout_add(&sc->sc_rngto, sc->sc_rnghz);
    590      1.15  jonathan #else
    591      1.15  jonathan 	callout_reset(&sc->sc_rngto, sc->sc_rnghz, hifn_rng, sc);
    592      1.15  jonathan #endif
    593      1.15  jonathan #endif	/*!__NetBSD__*/
    594      1.15  jonathan }
    595      1.15  jonathan #endif
    596      1.15  jonathan 
    597      1.15  jonathan void
    598      1.17   thorpej hifn_puc_wait(struct hifn_softc *sc)
    599      1.15  jonathan {
    600      1.15  jonathan 	int i;
    601      1.15  jonathan 
    602      1.15  jonathan 	for (i = 5000; i > 0; i--) {
    603      1.15  jonathan 		DELAY(1);
    604      1.15  jonathan 		if (!(READ_REG_0(sc, HIFN_0_PUCTRL) & HIFN_PUCTRL_RESET))
    605      1.15  jonathan 			break;
    606      1.15  jonathan 	}
    607      1.15  jonathan 	if (!i)
    608      1.15  jonathan 		printf("%s: proc unit did not reset\n", sc->sc_dv.dv_xname);
    609      1.15  jonathan }
    610      1.15  jonathan 
    611      1.15  jonathan /*
    612      1.15  jonathan  * Reset the processing unit.
    613      1.15  jonathan  */
    614      1.15  jonathan void
    615      1.17   thorpej hifn_reset_puc(struct hifn_softc *sc)
    616      1.15  jonathan {
    617      1.15  jonathan 	/* Reset processing unit */
    618      1.15  jonathan 	WRITE_REG_0(sc, HIFN_0_PUCTRL, HIFN_PUCTRL_DMAENA);
    619      1.15  jonathan 	hifn_puc_wait(sc);
    620      1.15  jonathan }
    621      1.15  jonathan 
    622      1.15  jonathan void
    623      1.17   thorpej hifn_set_retry(struct hifn_softc *sc)
    624      1.15  jonathan {
    625      1.15  jonathan 	u_int32_t r;
    626      1.15  jonathan 
    627      1.15  jonathan 	r = pci_conf_read(sc->sc_pci_pc, sc->sc_pci_tag, HIFN_TRDY_TIMEOUT);
    628      1.15  jonathan 	r &= 0xffff0000;
    629      1.15  jonathan 	pci_conf_write(sc->sc_pci_pc, sc->sc_pci_tag, HIFN_TRDY_TIMEOUT, r);
    630      1.15  jonathan }
    631      1.15  jonathan 
    632       1.1    itojun /*
    633       1.1    itojun  * Resets the board.  Values in the regesters are left as is
    634       1.1    itojun  * from the reset (i.e. initial values are assigned elsewhere).
    635       1.1    itojun  */
    636       1.1    itojun void
    637      1.15  jonathan hifn_reset_board(struct hifn_softc *sc, int full)
    638       1.1    itojun {
    639      1.15  jonathan 	u_int32_t reg;
    640      1.15  jonathan 
    641       1.1    itojun 	/*
    642       1.1    itojun 	 * Set polling in the DMA configuration register to zero.  0x7 avoids
    643       1.1    itojun 	 * resetting the board and zeros out the other fields.
    644       1.1    itojun 	 */
    645       1.1    itojun 	WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
    646       1.1    itojun 	    HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
    647       1.1    itojun 
    648       1.1    itojun 	/*
    649       1.1    itojun 	 * Now that polling has been disabled, we have to wait 1 ms
    650       1.1    itojun 	 * before resetting the board.
    651       1.1    itojun 	 */
    652       1.1    itojun 	DELAY(1000);
    653       1.1    itojun 
    654      1.15  jonathan 	/* Reset the DMA unit */
    655      1.15  jonathan 	if (full) {
    656      1.15  jonathan 		WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MODE);
    657      1.15  jonathan 		DELAY(1000);
    658      1.15  jonathan 	} else {
    659      1.15  jonathan 		WRITE_REG_1(sc, HIFN_1_DMA_CNFG,
    660      1.15  jonathan 		    HIFN_DMACNFG_MODE | HIFN_DMACNFG_MSTRESET);
    661      1.15  jonathan 		hifn_reset_puc(sc);
    662      1.15  jonathan 	}
    663       1.1    itojun 
    664      1.15  jonathan 	bzero(sc->sc_dma, sizeof(*sc->sc_dma));
    665       1.1    itojun 
    666      1.15  jonathan 	/* Bring dma unit out of reset */
    667       1.1    itojun 	WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
    668       1.1    itojun 	    HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
    669      1.15  jonathan 
    670      1.15  jonathan 	hifn_puc_wait(sc);
    671      1.15  jonathan 
    672      1.15  jonathan 	hifn_set_retry(sc);
    673      1.15  jonathan 
    674      1.15  jonathan 	if (sc->sc_flags & HIFN_IS_7811) {
    675      1.15  jonathan 		for (reg = 0; reg < 1000; reg++) {
    676      1.15  jonathan 			if (READ_REG_1(sc, HIFN_1_7811_MIPSRST) &
    677      1.15  jonathan 			    HIFN_MIPSRST_CRAMINIT)
    678      1.15  jonathan 				break;
    679      1.15  jonathan 			DELAY(1000);
    680      1.15  jonathan 		}
    681      1.15  jonathan 		if (reg == 1000)
    682      1.15  jonathan 			printf(": cram init timeout\n");
    683      1.15  jonathan 	}
    684       1.1    itojun }
    685       1.1    itojun 
    686       1.1    itojun u_int32_t
    687      1.17   thorpej hifn_next_signature(u_int32_t a, u_int cnt)
    688       1.1    itojun {
    689       1.1    itojun 	int i;
    690       1.1    itojun 	u_int32_t v;
    691       1.1    itojun 
    692       1.1    itojun 	for (i = 0; i < cnt; i++) {
    693       1.1    itojun 
    694       1.1    itojun 		/* get the parity */
    695       1.1    itojun 		v = a & 0x80080125;
    696       1.1    itojun 		v ^= v >> 16;
    697       1.1    itojun 		v ^= v >> 8;
    698       1.1    itojun 		v ^= v >> 4;
    699       1.1    itojun 		v ^= v >> 2;
    700       1.1    itojun 		v ^= v >> 1;
    701       1.1    itojun 
    702       1.1    itojun 		a = (v & 1) ^ (a << 1);
    703       1.1    itojun 	}
    704       1.1    itojun 
    705       1.1    itojun 	return a;
    706       1.1    itojun }
    707       1.1    itojun 
    708       1.1    itojun struct pci2id {
    709       1.1    itojun 	u_short		pci_vendor;
    710       1.1    itojun 	u_short		pci_prod;
    711       1.1    itojun 	char		card_id[13];
    712       1.1    itojun } pci2id[] = {
    713       1.1    itojun 	{
    714      1.15  jonathan 		PCI_VENDOR_HIFN,
    715      1.15  jonathan 		PCI_PRODUCT_HIFN_7951,
    716      1.15  jonathan 		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    717      1.15  jonathan 		  0x00, 0x00, 0x00, 0x00, 0x00 }
    718      1.15  jonathan 	}, {
    719      1.20  jonathan 		PCI_VENDOR_HIFN,
    720      1.20  jonathan 		PCI_PRODUCT_HIFN_7955,
    721      1.20  jonathan 		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    722      1.20  jonathan 		  0x00, 0x00, 0x00, 0x00, 0x00 }
    723      1.20  jonathan 	}, {
    724      1.20  jonathan 		PCI_VENDOR_HIFN,
    725      1.20  jonathan 		PCI_PRODUCT_HIFN_7956,
    726      1.20  jonathan 		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    727      1.20  jonathan 		  0x00, 0x00, 0x00, 0x00, 0x00 }
    728      1.20  jonathan 	}, {
    729       1.1    itojun 		PCI_VENDOR_NETSEC,
    730       1.1    itojun 		PCI_PRODUCT_NETSEC_7751,
    731       1.1    itojun 		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    732       1.1    itojun 		  0x00, 0x00, 0x00, 0x00, 0x00 }
    733       1.1    itojun 	}, {
    734       1.1    itojun 		PCI_VENDOR_INVERTEX,
    735       1.1    itojun 		PCI_PRODUCT_INVERTEX_AEON,
    736       1.1    itojun 		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    737       1.1    itojun 		  0x00, 0x00, 0x00, 0x00, 0x00 }
    738       1.1    itojun 	}, {
    739      1.15  jonathan 		PCI_VENDOR_HIFN,
    740      1.15  jonathan 		PCI_PRODUCT_HIFN_7811,
    741      1.15  jonathan 		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    742      1.15  jonathan 		  0x00, 0x00, 0x00, 0x00, 0x00 }
    743      1.15  jonathan 	}, {
    744       1.1    itojun 		/*
    745       1.1    itojun 		 * Other vendors share this PCI ID as well, such as
    746       1.1    itojun 		 * http://www.powercrypt.com, and obviously they also
    747       1.1    itojun 		 * use the same key.
    748       1.1    itojun 		 */
    749       1.1    itojun 		PCI_VENDOR_HIFN,
    750       1.1    itojun 		PCI_PRODUCT_HIFN_7751,
    751       1.1    itojun 		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    752       1.1    itojun 		  0x00, 0x00, 0x00, 0x00, 0x00 }
    753       1.1    itojun 	},
    754       1.1    itojun };
    755       1.1    itojun 
    756       1.1    itojun /*
    757       1.1    itojun  * Checks to see if crypto is already enabled.  If crypto isn't enable,
    758       1.1    itojun  * "hifn_enable_crypto" is called to enable it.  The check is important,
    759       1.1    itojun  * as enabling crypto twice will lock the board.
    760       1.1    itojun  */
    761      1.16   thorpej const char *
    762      1.17   thorpej hifn_enable_crypto(struct hifn_softc *sc, pcireg_t pciid)
    763       1.1    itojun {
    764       1.1    itojun 	u_int32_t dmacfg, ramcfg, encl, addr, i;
    765       1.1    itojun 	char *offtbl = NULL;
    766       1.1    itojun 
    767       1.1    itojun 	for (i = 0; i < sizeof(pci2id)/sizeof(pci2id[0]); i++) {
    768       1.1    itojun 		if (pci2id[i].pci_vendor == PCI_VENDOR(pciid) &&
    769       1.1    itojun 		    pci2id[i].pci_prod == PCI_PRODUCT(pciid)) {
    770       1.1    itojun 			offtbl = pci2id[i].card_id;
    771       1.1    itojun 			break;
    772       1.1    itojun 		}
    773       1.1    itojun 	}
    774       1.1    itojun 
    775       1.1    itojun 	if (offtbl == NULL) {
    776       1.1    itojun #ifdef HIFN_DEBUG
    777      1.13   thorpej 		aprint_debug("%s: Unknown card!\n", sc->sc_dv.dv_xname);
    778       1.1    itojun #endif
    779      1.16   thorpej 		return (NULL);
    780       1.1    itojun 	}
    781       1.1    itojun 
    782       1.1    itojun 	ramcfg = READ_REG_0(sc, HIFN_0_PUCNFG);
    783       1.1    itojun 	dmacfg = READ_REG_1(sc, HIFN_1_DMA_CNFG);
    784       1.1    itojun 
    785       1.1    itojun 	/*
    786       1.1    itojun 	 * The RAM config register's encrypt level bit needs to be set before
    787       1.1    itojun 	 * every read performed on the encryption level register.
    788       1.1    itojun 	 */
    789       1.1    itojun 	WRITE_REG_0(sc, HIFN_0_PUCNFG, ramcfg | HIFN_PUCNFG_CHIPID);
    790       1.1    itojun 
    791       1.1    itojun 	encl = READ_REG_0(sc, HIFN_0_PUSTAT) & HIFN_PUSTAT_CHIPENA;
    792       1.1    itojun 
    793       1.1    itojun 	/*
    794       1.1    itojun 	 * Make sure we don't re-unlock.  Two unlocks kills chip until the
    795       1.1    itojun 	 * next reboot.
    796       1.1    itojun 	 */
    797       1.1    itojun 	if (encl == HIFN_PUSTAT_ENA_1 || encl == HIFN_PUSTAT_ENA_2) {
    798       1.1    itojun #ifdef HIFN_DEBUG
    799      1.13   thorpej 		aprint_debug("%s: Strong Crypto already enabled!\n",
    800       1.1    itojun 		    sc->sc_dv.dv_xname);
    801       1.1    itojun #endif
    802      1.15  jonathan 		goto report;
    803       1.1    itojun 	}
    804       1.1    itojun 
    805       1.1    itojun 	if (encl != 0 && encl != HIFN_PUSTAT_ENA_0) {
    806       1.1    itojun #ifdef HIFN_DEBUG
    807      1.13   thorpej 		aprint_debug("%s: Unknown encryption level\n",
    808      1.13   thorpej 		    sc->sc_dv.dv_xname);
    809       1.1    itojun #endif
    810      1.16   thorpej 		return (NULL);
    811       1.1    itojun 	}
    812       1.1    itojun 
    813       1.1    itojun 	WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_UNLOCK |
    814       1.1    itojun 	    HIFN_DMACNFG_MSTRESET | HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
    815       1.1    itojun 	DELAY(1000);
    816      1.15  jonathan 	addr = READ_REG_1(sc, HIFN_1_UNLOCK_SECRET1);
    817       1.1    itojun 	DELAY(1000);
    818      1.15  jonathan 	WRITE_REG_1(sc, HIFN_1_UNLOCK_SECRET2, 0);
    819       1.1    itojun 	DELAY(1000);
    820       1.1    itojun 
    821       1.1    itojun 	for (i = 0; i <= 12; i++) {
    822       1.1    itojun 		addr = hifn_next_signature(addr, offtbl[i] + 0x101);
    823      1.15  jonathan 		WRITE_REG_1(sc, HIFN_1_UNLOCK_SECRET2, addr);
    824       1.1    itojun 
    825       1.1    itojun 		DELAY(1000);
    826       1.1    itojun 	}
    827       1.1    itojun 
    828       1.1    itojun 	WRITE_REG_0(sc, HIFN_0_PUCNFG, ramcfg | HIFN_PUCNFG_CHIPID);
    829       1.1    itojun 	encl = READ_REG_0(sc, HIFN_0_PUSTAT) & HIFN_PUSTAT_CHIPENA;
    830       1.1    itojun 
    831       1.1    itojun #ifdef HIFN_DEBUG
    832       1.1    itojun 	if (encl != HIFN_PUSTAT_ENA_1 && encl != HIFN_PUSTAT_ENA_2)
    833      1.13   thorpej 		aprint_debug("Encryption engine is permanently locked until next system reset.");
    834       1.1    itojun 	else
    835      1.13   thorpej 		aprint_debug("Encryption engine enabled successfully!");
    836       1.1    itojun #endif
    837       1.1    itojun 
    838      1.15  jonathan report:
    839       1.1    itojun 	WRITE_REG_0(sc, HIFN_0_PUCNFG, ramcfg);
    840       1.1    itojun 	WRITE_REG_1(sc, HIFN_1_DMA_CNFG, dmacfg);
    841       1.1    itojun 
    842       1.1    itojun 	switch (encl) {
    843       1.1    itojun 	case HIFN_PUSTAT_ENA_0:
    844      1.16   thorpej 		return ("LZS-only (no encr/auth)");
    845      1.16   thorpej 
    846       1.1    itojun 	case HIFN_PUSTAT_ENA_1:
    847      1.16   thorpej 		return ("DES");
    848      1.16   thorpej 
    849       1.1    itojun 	case HIFN_PUSTAT_ENA_2:
    850      1.21  jonathan 		if (sc->sc_flags & HIFN_HAS_AES)
    851      1.21  jonathan 		    return ("3DES/AES");
    852      1.21  jonathan 		else
    853      1.21  jonathan 		    return ("3DES");
    854      1.16   thorpej 
    855       1.1    itojun 	default:
    856      1.16   thorpej 		return ("disabled");
    857       1.1    itojun 	}
    858      1.16   thorpej 	/* NOTREACHED */
    859       1.1    itojun }
    860       1.1    itojun 
    861       1.1    itojun /*
    862       1.1    itojun  * Give initial values to the registers listed in the "Register Space"
    863       1.1    itojun  * section of the HIFN Software Development reference manual.
    864       1.1    itojun  */
    865  1.21.6.1      yamt void
    866      1.17   thorpej hifn_init_pci_registers(struct hifn_softc *sc)
    867       1.1    itojun {
    868       1.1    itojun 	/* write fixed values needed by the Initialization registers */
    869       1.1    itojun 	WRITE_REG_0(sc, HIFN_0_PUCTRL, HIFN_PUCTRL_DMAENA);
    870       1.1    itojun 	WRITE_REG_0(sc, HIFN_0_FIFOCNFG, HIFN_FIFOCNFG_THRESHOLD);
    871       1.1    itojun 	WRITE_REG_0(sc, HIFN_0_PUIER, HIFN_PUIER_DSTOVER);
    872       1.1    itojun 
    873       1.1    itojun 	/* write all 4 ring address registers */
    874      1.15  jonathan 	WRITE_REG_1(sc, HIFN_1_DMA_CRAR, sc->sc_dmamap->dm_segs[0].ds_addr +
    875      1.15  jonathan 	    offsetof(struct hifn_dma, cmdr[0]));
    876      1.15  jonathan 	WRITE_REG_1(sc, HIFN_1_DMA_SRAR, sc->sc_dmamap->dm_segs[0].ds_addr +
    877      1.15  jonathan 	    offsetof(struct hifn_dma, srcr[0]));
    878      1.15  jonathan 	WRITE_REG_1(sc, HIFN_1_DMA_DRAR, sc->sc_dmamap->dm_segs[0].ds_addr +
    879      1.15  jonathan 	    offsetof(struct hifn_dma, dstr[0]));
    880      1.15  jonathan 	WRITE_REG_1(sc, HIFN_1_DMA_RRAR, sc->sc_dmamap->dm_segs[0].ds_addr +
    881      1.15  jonathan 	    offsetof(struct hifn_dma, resr[0]));
    882      1.15  jonathan 
    883      1.15  jonathan 	DELAY(2000);
    884       1.1    itojun 
    885       1.1    itojun 	/* write status register */
    886      1.15  jonathan 	WRITE_REG_1(sc, HIFN_1_DMA_CSR,
    887      1.15  jonathan 	    HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS |
    888      1.15  jonathan 	    HIFN_DMACSR_S_CTRL_DIS | HIFN_DMACSR_C_CTRL_DIS |
    889      1.15  jonathan 	    HIFN_DMACSR_D_ABORT | HIFN_DMACSR_D_DONE | HIFN_DMACSR_D_LAST |
    890      1.15  jonathan 	    HIFN_DMACSR_D_WAIT | HIFN_DMACSR_D_OVER |
    891      1.15  jonathan 	    HIFN_DMACSR_R_ABORT | HIFN_DMACSR_R_DONE | HIFN_DMACSR_R_LAST |
    892      1.15  jonathan 	    HIFN_DMACSR_R_WAIT | HIFN_DMACSR_R_OVER |
    893      1.15  jonathan 	    HIFN_DMACSR_S_ABORT | HIFN_DMACSR_S_DONE | HIFN_DMACSR_S_LAST |
    894      1.15  jonathan 	    HIFN_DMACSR_S_WAIT |
    895      1.15  jonathan 	    HIFN_DMACSR_C_ABORT | HIFN_DMACSR_C_DONE | HIFN_DMACSR_C_LAST |
    896      1.15  jonathan 	    HIFN_DMACSR_C_WAIT |
    897      1.15  jonathan 	    HIFN_DMACSR_ENGINE |
    898      1.15  jonathan 	    ((sc->sc_flags & HIFN_HAS_PUBLIC) ?
    899      1.15  jonathan 		HIFN_DMACSR_PUBDONE : 0) |
    900      1.15  jonathan 	    ((sc->sc_flags & HIFN_IS_7811) ?
    901      1.15  jonathan 		HIFN_DMACSR_ILLW | HIFN_DMACSR_ILLR : 0));
    902      1.15  jonathan 
    903      1.15  jonathan 	sc->sc_d_busy = sc->sc_r_busy = sc->sc_s_busy = sc->sc_c_busy = 0;
    904      1.15  jonathan 	sc->sc_dmaier |= HIFN_DMAIER_R_DONE | HIFN_DMAIER_C_ABORT |
    905      1.15  jonathan 	    HIFN_DMAIER_D_OVER | HIFN_DMAIER_R_OVER |
    906      1.15  jonathan 	    HIFN_DMAIER_S_ABORT | HIFN_DMAIER_D_ABORT | HIFN_DMAIER_R_ABORT |
    907      1.15  jonathan 	    HIFN_DMAIER_ENGINE |
    908      1.15  jonathan 	    ((sc->sc_flags & HIFN_IS_7811) ?
    909      1.15  jonathan 		HIFN_DMAIER_ILLW | HIFN_DMAIER_ILLR : 0);
    910      1.15  jonathan 	sc->sc_dmaier &= ~HIFN_DMAIER_C_WAIT;
    911      1.15  jonathan 	WRITE_REG_1(sc, HIFN_1_DMA_IER, sc->sc_dmaier);
    912      1.15  jonathan 	CLR_LED(sc, HIFN_MIPSRST_LED0 | HIFN_MIPSRST_LED1 | HIFN_MIPSRST_LED2);
    913      1.15  jonathan 
    914      1.20  jonathan 	if (sc->sc_flags & HIFN_IS_7956) {
    915      1.20  jonathan 		WRITE_REG_0(sc, HIFN_0_PUCNFG, HIFN_PUCNFG_COMPSING |
    916      1.20  jonathan 		    HIFN_PUCNFG_TCALLPHASES |
    917      1.20  jonathan 		    HIFN_PUCNFG_TCDRVTOTEM | HIFN_PUCNFG_BUS32);
    918      1.20  jonathan 		WRITE_REG_1(sc, HIFN_1_PLL, HIFN_PLL_7956);
    919      1.20  jonathan 	} else {
    920      1.20  jonathan 		WRITE_REG_0(sc, HIFN_0_PUCNFG, HIFN_PUCNFG_COMPSING |
    921      1.20  jonathan 		    HIFN_PUCNFG_DRFR_128 | HIFN_PUCNFG_TCALLPHASES |
    922      1.20  jonathan 		    HIFN_PUCNFG_TCDRVTOTEM | HIFN_PUCNFG_BUS32 |
    923      1.20  jonathan 		    (sc->sc_drammodel ? HIFN_PUCNFG_DRAM : HIFN_PUCNFG_SRAM));
    924      1.20  jonathan 	}
    925       1.1    itojun 
    926       1.1    itojun 	WRITE_REG_0(sc, HIFN_0_PUISR, HIFN_PUISR_DSTOVER);
    927       1.1    itojun 	WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
    928       1.1    itojun 	    HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE | HIFN_DMACNFG_LAST |
    929       1.1    itojun 	    ((HIFN_POLL_FREQUENCY << 16 ) & HIFN_DMACNFG_POLLFREQ) |
    930       1.1    itojun 	    ((HIFN_POLL_SCALAR << 8) & HIFN_DMACNFG_POLLINVAL));
    931       1.1    itojun }
    932       1.1    itojun 
    933       1.1    itojun /*
    934       1.1    itojun  * The maximum number of sessions supported by the card
    935       1.1    itojun  * is dependent on the amount of context ram, which
    936       1.1    itojun  * encryption algorithms are enabled, and how compression
    937       1.1    itojun  * is configured.  This should be configured before this
    938       1.1    itojun  * routine is called.
    939       1.1    itojun  */
    940       1.1    itojun void
    941      1.17   thorpej hifn_sessions(struct hifn_softc *sc)
    942       1.1    itojun {
    943       1.1    itojun 	u_int32_t pucnfg;
    944       1.1    itojun 	int ctxsize;
    945       1.1    itojun 
    946       1.1    itojun 	pucnfg = READ_REG_0(sc, HIFN_0_PUCNFG);
    947       1.1    itojun 
    948       1.1    itojun 	if (pucnfg & HIFN_PUCNFG_COMPSING) {
    949       1.1    itojun 		if (pucnfg & HIFN_PUCNFG_ENCCNFG)
    950       1.1    itojun 			ctxsize = 128;
    951       1.1    itojun 		else
    952       1.1    itojun 			ctxsize = 512;
    953      1.20  jonathan 		/*
    954      1.20  jonathan 		 * 7955/7956 has internal context memory of 32K
    955      1.20  jonathan 		 */
    956      1.20  jonathan 		if (sc->sc_flags & HIFN_IS_7956)
    957      1.20  jonathan 			sc->sc_maxses = 32768 / ctxsize;
    958      1.20  jonathan 		else
    959      1.20  jonathan 			sc->sc_maxses = 1 +
    960      1.20  jonathan 			    ((sc->sc_ramsize - 32768) / ctxsize);
    961       1.1    itojun 	}
    962       1.1    itojun 	else
    963       1.1    itojun 		sc->sc_maxses = sc->sc_ramsize / 16384;
    964       1.1    itojun 
    965       1.1    itojun 	if (sc->sc_maxses > 2048)
    966       1.1    itojun 		sc->sc_maxses = 2048;
    967       1.1    itojun }
    968       1.1    itojun 
    969      1.15  jonathan /*
    970      1.15  jonathan  * Determine ram type (sram or dram).  Board should be just out of a reset
    971      1.15  jonathan  * state when this is called.
    972      1.15  jonathan  */
    973      1.15  jonathan int
    974      1.17   thorpej hifn_ramtype(struct hifn_softc *sc)
    975       1.1    itojun {
    976       1.1    itojun 	u_int8_t data[8], dataexpect[8];
    977       1.1    itojun 	int i;
    978       1.1    itojun 
    979       1.1    itojun 	for (i = 0; i < sizeof(data); i++)
    980       1.1    itojun 		data[i] = dataexpect[i] = 0x55;
    981      1.15  jonathan 	if (hifn_writeramaddr(sc, 0, data))
    982      1.15  jonathan 		return (-1);
    983      1.15  jonathan 	if (hifn_readramaddr(sc, 0, data))
    984      1.15  jonathan 		return (-1);
    985      1.15  jonathan 	if (bcmp(data, dataexpect, sizeof(data)) != 0) {
    986       1.1    itojun 		sc->sc_drammodel = 1;
    987      1.15  jonathan 		return (0);
    988       1.1    itojun 	}
    989       1.1    itojun 
    990       1.1    itojun 	for (i = 0; i < sizeof(data); i++)
    991       1.1    itojun 		data[i] = dataexpect[i] = 0xaa;
    992      1.15  jonathan 	if (hifn_writeramaddr(sc, 0, data))
    993      1.15  jonathan 		return (-1);
    994      1.15  jonathan 	if (hifn_readramaddr(sc, 0, data))
    995      1.15  jonathan 		return (-1);
    996      1.15  jonathan 	if (bcmp(data, dataexpect, sizeof(data)) != 0) {
    997       1.1    itojun 		sc->sc_drammodel = 1;
    998      1.15  jonathan 		return (0);
    999      1.15  jonathan 	}
   1000      1.15  jonathan 
   1001      1.15  jonathan 	return (0);
   1002       1.1    itojun }
   1003       1.1    itojun 
   1004      1.15  jonathan #define	HIFN_SRAM_MAX		(32 << 20)
   1005      1.15  jonathan #define	HIFN_SRAM_STEP_SIZE	16384
   1006      1.15  jonathan #define	HIFN_SRAM_GRANULARITY	(HIFN_SRAM_MAX / HIFN_SRAM_STEP_SIZE)
   1007      1.15  jonathan 
   1008       1.1    itojun int
   1009      1.17   thorpej hifn_sramsize(struct hifn_softc *sc)
   1010       1.1    itojun {
   1011      1.15  jonathan 	u_int32_t a;
   1012      1.15  jonathan 	u_int8_t data[8];
   1013      1.15  jonathan 	u_int8_t dataexpect[sizeof(data)];
   1014      1.15  jonathan 	int32_t i;
   1015       1.1    itojun 
   1016      1.15  jonathan 	for (i = 0; i < sizeof(data); i++)
   1017      1.15  jonathan 		data[i] = dataexpect[i] = i ^ 0x5a;
   1018       1.1    itojun 
   1019      1.15  jonathan 	for (i = HIFN_SRAM_GRANULARITY - 1; i >= 0; i--) {
   1020      1.15  jonathan 		a = i * HIFN_SRAM_STEP_SIZE;
   1021      1.15  jonathan 		bcopy(&i, data, sizeof(i));
   1022      1.15  jonathan 		hifn_writeramaddr(sc, a, data);
   1023       1.1    itojun 	}
   1024       1.1    itojun 
   1025      1.15  jonathan 	for (i = 0; i < HIFN_SRAM_GRANULARITY; i++) {
   1026      1.15  jonathan 		a = i * HIFN_SRAM_STEP_SIZE;
   1027      1.15  jonathan 		bcopy(&i, dataexpect, sizeof(i));
   1028      1.15  jonathan 		if (hifn_readramaddr(sc, a, data) < 0)
   1029       1.1    itojun 			return (0);
   1030      1.15  jonathan 		if (bcmp(data, dataexpect, sizeof(data)) != 0)
   1031       1.1    itojun 			return (0);
   1032      1.15  jonathan 		sc->sc_ramsize = a + HIFN_SRAM_STEP_SIZE;
   1033       1.1    itojun 	}
   1034       1.1    itojun 
   1035       1.1    itojun 	return (0);
   1036       1.1    itojun }
   1037       1.1    itojun 
   1038       1.1    itojun /*
   1039       1.1    itojun  * XXX For dram boards, one should really try all of the
   1040       1.1    itojun  * HIFN_PUCNFG_DSZ_*'s.  This just assumes that PUCNFG
   1041       1.1    itojun  * is already set up correctly.
   1042       1.1    itojun  */
   1043       1.1    itojun int
   1044      1.17   thorpej hifn_dramsize(struct hifn_softc *sc)
   1045       1.1    itojun {
   1046       1.1    itojun 	u_int32_t cnfg;
   1047       1.1    itojun 
   1048      1.20  jonathan 	if (sc->sc_flags & HIFN_IS_7956) {
   1049      1.20  jonathan 		/*
   1050      1.20  jonathan 		 * 7955/7956 have a fixed internal ram of only 32K.
   1051      1.20  jonathan 		 */
   1052      1.20  jonathan 		sc->sc_ramsize = 32768;
   1053      1.20  jonathan 	} else {
   1054      1.20  jonathan 		cnfg = READ_REG_0(sc, HIFN_0_PUCNFG) &
   1055      1.20  jonathan 		    HIFN_PUCNFG_DRAMMASK;
   1056      1.20  jonathan 		sc->sc_ramsize = 1 << ((cnfg >> 13) + 18);
   1057      1.20  jonathan 	}
   1058       1.1    itojun 	return (0);
   1059       1.1    itojun }
   1060       1.1    itojun 
   1061      1.15  jonathan void
   1062      1.17   thorpej hifn_alloc_slot(struct hifn_softc *sc, int *cmdp, int *srcp, int *dstp,
   1063      1.17   thorpej     int *resp)
   1064      1.15  jonathan {
   1065      1.15  jonathan 	struct hifn_dma *dma = sc->sc_dma;
   1066      1.15  jonathan 
   1067      1.15  jonathan 	if (dma->cmdi == HIFN_D_CMD_RSIZE) {
   1068      1.15  jonathan 		dma->cmdi = 0;
   1069      1.15  jonathan 		dma->cmdr[HIFN_D_CMD_RSIZE].l = htole32(HIFN_D_VALID |
   1070      1.15  jonathan 		    HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
   1071      1.15  jonathan 		HIFN_CMDR_SYNC(sc, HIFN_D_CMD_RSIZE,
   1072      1.15  jonathan 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1073      1.15  jonathan 	}
   1074      1.15  jonathan 	*cmdp = dma->cmdi++;
   1075      1.15  jonathan 	dma->cmdk = dma->cmdi;
   1076      1.15  jonathan 
   1077      1.15  jonathan 	if (dma->srci == HIFN_D_SRC_RSIZE) {
   1078      1.15  jonathan 		dma->srci = 0;
   1079      1.15  jonathan 		dma->srcr[HIFN_D_SRC_RSIZE].l = htole32(HIFN_D_VALID |
   1080      1.15  jonathan 		    HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
   1081      1.15  jonathan 		HIFN_SRCR_SYNC(sc, HIFN_D_SRC_RSIZE,
   1082      1.15  jonathan 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1083      1.15  jonathan 	}
   1084      1.15  jonathan 	*srcp = dma->srci++;
   1085      1.15  jonathan 	dma->srck = dma->srci;
   1086      1.15  jonathan 
   1087      1.15  jonathan 	if (dma->dsti == HIFN_D_DST_RSIZE) {
   1088      1.15  jonathan 		dma->dsti = 0;
   1089      1.15  jonathan 		dma->dstr[HIFN_D_DST_RSIZE].l = htole32(HIFN_D_VALID |
   1090      1.15  jonathan 		    HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
   1091      1.15  jonathan 		HIFN_DSTR_SYNC(sc, HIFN_D_DST_RSIZE,
   1092      1.15  jonathan 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1093      1.15  jonathan 	}
   1094      1.15  jonathan 	*dstp = dma->dsti++;
   1095      1.15  jonathan 	dma->dstk = dma->dsti;
   1096      1.15  jonathan 
   1097      1.15  jonathan 	if (dma->resi == HIFN_D_RES_RSIZE) {
   1098      1.15  jonathan 		dma->resi = 0;
   1099      1.15  jonathan 		dma->resr[HIFN_D_RES_RSIZE].l = htole32(HIFN_D_VALID |
   1100      1.15  jonathan 		    HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
   1101      1.15  jonathan 		HIFN_RESR_SYNC(sc, HIFN_D_RES_RSIZE,
   1102      1.15  jonathan 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1103      1.15  jonathan 	}
   1104      1.15  jonathan 	*resp = dma->resi++;
   1105      1.15  jonathan 	dma->resk = dma->resi;
   1106      1.15  jonathan }
   1107      1.15  jonathan 
   1108       1.1    itojun int
   1109      1.17   thorpej hifn_writeramaddr(struct hifn_softc *sc, int addr, u_int8_t *data)
   1110       1.1    itojun {
   1111       1.1    itojun 	struct hifn_dma *dma = sc->sc_dma;
   1112      1.15  jonathan 	struct hifn_base_command wc;
   1113       1.1    itojun 	const u_int32_t masks = HIFN_D_VALID | HIFN_D_LAST | HIFN_D_MASKDONEIRQ;
   1114      1.15  jonathan 	int r, cmdi, resi, srci, dsti;
   1115       1.1    itojun 
   1116      1.15  jonathan 	wc.masks = htole16(3 << 13);
   1117      1.15  jonathan 	wc.session_num = htole16(addr >> 14);
   1118      1.15  jonathan 	wc.total_source_count = htole16(8);
   1119      1.15  jonathan 	wc.total_dest_count = htole16(addr & 0x3fff);
   1120      1.15  jonathan 
   1121      1.15  jonathan 	hifn_alloc_slot(sc, &cmdi, &srci, &dsti, &resi);
   1122      1.15  jonathan 
   1123      1.15  jonathan 	WRITE_REG_1(sc, HIFN_1_DMA_CSR,
   1124      1.15  jonathan 	    HIFN_DMACSR_C_CTRL_ENA | HIFN_DMACSR_S_CTRL_ENA |
   1125      1.15  jonathan 	    HIFN_DMACSR_D_CTRL_ENA | HIFN_DMACSR_R_CTRL_ENA);
   1126       1.1    itojun 
   1127       1.1    itojun 	/* build write command */
   1128      1.15  jonathan 	bzero(dma->command_bufs[cmdi], HIFN_MAX_COMMAND);
   1129      1.15  jonathan 	*(struct hifn_base_command *)dma->command_bufs[cmdi] = wc;
   1130      1.15  jonathan 	bcopy(data, &dma->test_src, sizeof(dma->test_src));
   1131      1.15  jonathan 
   1132      1.15  jonathan 	dma->srcr[srci].p = htole32(sc->sc_dmamap->dm_segs[0].ds_addr
   1133      1.15  jonathan 	    + offsetof(struct hifn_dma, test_src));
   1134      1.15  jonathan 	dma->dstr[dsti].p = htole32(sc->sc_dmamap->dm_segs[0].ds_addr
   1135      1.15  jonathan 	    + offsetof(struct hifn_dma, test_dst));
   1136      1.15  jonathan 
   1137      1.15  jonathan 	dma->cmdr[cmdi].l = htole32(16 | masks);
   1138      1.15  jonathan 	dma->srcr[srci].l = htole32(8 | masks);
   1139      1.15  jonathan 	dma->dstr[dsti].l = htole32(4 | masks);
   1140      1.15  jonathan 	dma->resr[resi].l = htole32(4 | masks);
   1141      1.15  jonathan 
   1142      1.15  jonathan 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   1143      1.15  jonathan 	    0, sc->sc_dmamap->dm_mapsize,
   1144      1.15  jonathan 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1145      1.15  jonathan 
   1146      1.15  jonathan 	for (r = 10000; r >= 0; r--) {
   1147      1.15  jonathan 		DELAY(10);
   1148      1.15  jonathan 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   1149      1.15  jonathan 		    0, sc->sc_dmamap->dm_mapsize,
   1150      1.15  jonathan 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1151      1.15  jonathan 		if ((dma->resr[resi].l & htole32(HIFN_D_VALID)) == 0)
   1152      1.15  jonathan 			break;
   1153      1.15  jonathan 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   1154      1.15  jonathan 		    0, sc->sc_dmamap->dm_mapsize,
   1155      1.15  jonathan 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1156      1.15  jonathan 	}
   1157      1.15  jonathan 	if (r == 0) {
   1158      1.15  jonathan 		printf("%s: writeramaddr -- "
   1159      1.15  jonathan 		    "result[%d](addr %d) still valid\n",
   1160      1.15  jonathan 		    sc->sc_dv.dv_xname, resi, addr);
   1161      1.15  jonathan 		r = -1;
   1162      1.15  jonathan 		return (-1);
   1163      1.15  jonathan 	} else
   1164      1.15  jonathan 		r = 0;
   1165       1.1    itojun 
   1166      1.15  jonathan 	WRITE_REG_1(sc, HIFN_1_DMA_CSR,
   1167      1.15  jonathan 	    HIFN_DMACSR_C_CTRL_DIS | HIFN_DMACSR_S_CTRL_DIS |
   1168      1.15  jonathan 	    HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS);
   1169       1.1    itojun 
   1170      1.15  jonathan 	return (r);
   1171       1.1    itojun }
   1172       1.1    itojun 
   1173       1.1    itojun int
   1174      1.17   thorpej hifn_readramaddr(struct hifn_softc *sc, int addr, u_int8_t *data)
   1175       1.1    itojun {
   1176       1.1    itojun 	struct hifn_dma *dma = sc->sc_dma;
   1177      1.15  jonathan 	struct hifn_base_command rc;
   1178       1.1    itojun 	const u_int32_t masks = HIFN_D_VALID | HIFN_D_LAST | HIFN_D_MASKDONEIRQ;
   1179      1.15  jonathan 	int r, cmdi, srci, dsti, resi;
   1180       1.1    itojun 
   1181      1.15  jonathan 	rc.masks = htole16(2 << 13);
   1182      1.15  jonathan 	rc.session_num = htole16(addr >> 14);
   1183      1.15  jonathan 	rc.total_source_count = htole16(addr & 0x3fff);
   1184      1.15  jonathan 	rc.total_dest_count = htole16(8);
   1185      1.15  jonathan 
   1186      1.15  jonathan 	hifn_alloc_slot(sc, &cmdi, &srci, &dsti, &resi);
   1187      1.15  jonathan 
   1188      1.15  jonathan 	WRITE_REG_1(sc, HIFN_1_DMA_CSR,
   1189      1.15  jonathan 	    HIFN_DMACSR_C_CTRL_ENA | HIFN_DMACSR_S_CTRL_ENA |
   1190      1.15  jonathan 	    HIFN_DMACSR_D_CTRL_ENA | HIFN_DMACSR_R_CTRL_ENA);
   1191      1.15  jonathan 
   1192      1.15  jonathan 	bzero(dma->command_bufs[cmdi], HIFN_MAX_COMMAND);
   1193      1.15  jonathan 	*(struct hifn_base_command *)dma->command_bufs[cmdi] = rc;
   1194      1.15  jonathan 
   1195      1.15  jonathan 	dma->srcr[srci].p = htole32(sc->sc_dmamap->dm_segs[0].ds_addr +
   1196      1.15  jonathan 	    offsetof(struct hifn_dma, test_src));
   1197      1.15  jonathan 	dma->test_src = 0;
   1198      1.15  jonathan 	dma->dstr[dsti].p =  htole32(sc->sc_dmamap->dm_segs[0].ds_addr +
   1199      1.15  jonathan 	    offsetof(struct hifn_dma, test_dst));
   1200      1.15  jonathan 	dma->test_dst = 0;
   1201      1.15  jonathan 	dma->cmdr[cmdi].l = htole32(8 | masks);
   1202      1.15  jonathan 	dma->srcr[srci].l = htole32(8 | masks);
   1203      1.15  jonathan 	dma->dstr[dsti].l = htole32(8 | masks);
   1204      1.15  jonathan 	dma->resr[resi].l = htole32(HIFN_MAX_RESULT | masks);
   1205      1.15  jonathan 
   1206      1.15  jonathan 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   1207      1.15  jonathan 	    0, sc->sc_dmamap->dm_mapsize,
   1208      1.15  jonathan 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1209      1.15  jonathan 
   1210      1.15  jonathan 	for (r = 10000; r >= 0; r--) {
   1211      1.15  jonathan 		DELAY(10);
   1212      1.15  jonathan 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   1213      1.15  jonathan 		    0, sc->sc_dmamap->dm_mapsize,
   1214      1.15  jonathan 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1215      1.15  jonathan 		if ((dma->resr[resi].l & htole32(HIFN_D_VALID)) == 0)
   1216      1.15  jonathan 			break;
   1217      1.15  jonathan 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   1218      1.15  jonathan 		    0, sc->sc_dmamap->dm_mapsize,
   1219      1.15  jonathan 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1220      1.15  jonathan 	}
   1221      1.15  jonathan 	if (r == 0) {
   1222      1.15  jonathan 		printf("%s: readramaddr -- "
   1223      1.15  jonathan 		    "result[%d](addr %d) still valid\n",
   1224      1.15  jonathan 		    sc->sc_dv.dv_xname, resi, addr);
   1225      1.15  jonathan 		r = -1;
   1226      1.15  jonathan 	} else {
   1227      1.15  jonathan 		r = 0;
   1228      1.15  jonathan 		bcopy(&dma->test_dst, data, sizeof(dma->test_dst));
   1229       1.1    itojun 	}
   1230      1.15  jonathan 
   1231      1.15  jonathan 	WRITE_REG_1(sc, HIFN_1_DMA_CSR,
   1232      1.15  jonathan 	    HIFN_DMACSR_C_CTRL_DIS | HIFN_DMACSR_S_CTRL_DIS |
   1233      1.15  jonathan 	    HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS);
   1234      1.15  jonathan 
   1235      1.15  jonathan 	return (r);
   1236       1.1    itojun }
   1237       1.1    itojun 
   1238       1.1    itojun /*
   1239       1.1    itojun  * Initialize the descriptor rings.
   1240       1.1    itojun  */
   1241  1.21.6.1      yamt void
   1242      1.17   thorpej hifn_init_dma(struct hifn_softc *sc)
   1243       1.1    itojun {
   1244       1.1    itojun 	struct hifn_dma *dma = sc->sc_dma;
   1245       1.1    itojun 	int i;
   1246       1.1    itojun 
   1247      1.15  jonathan 	hifn_set_retry(sc);
   1248      1.15  jonathan 
   1249       1.1    itojun 	/* initialize static pointer values */
   1250       1.1    itojun 	for (i = 0; i < HIFN_D_CMD_RSIZE; i++)
   1251      1.15  jonathan 		dma->cmdr[i].p = htole32(sc->sc_dmamap->dm_segs[0].ds_addr +
   1252      1.15  jonathan 		    offsetof(struct hifn_dma, command_bufs[i][0]));
   1253       1.1    itojun 	for (i = 0; i < HIFN_D_RES_RSIZE; i++)
   1254      1.15  jonathan 		dma->resr[i].p = htole32(sc->sc_dmamap->dm_segs[0].ds_addr +
   1255      1.15  jonathan 		    offsetof(struct hifn_dma, result_bufs[i][0]));
   1256      1.15  jonathan 
   1257      1.15  jonathan 	dma->cmdr[HIFN_D_CMD_RSIZE].p =
   1258      1.15  jonathan 	    htole32(sc->sc_dmamap->dm_segs[0].ds_addr +
   1259      1.15  jonathan 		offsetof(struct hifn_dma, cmdr[0]));
   1260      1.15  jonathan 	dma->srcr[HIFN_D_SRC_RSIZE].p =
   1261      1.15  jonathan 	    htole32(sc->sc_dmamap->dm_segs[0].ds_addr +
   1262      1.15  jonathan 		offsetof(struct hifn_dma, srcr[0]));
   1263      1.15  jonathan 	dma->dstr[HIFN_D_DST_RSIZE].p =
   1264      1.15  jonathan 	    htole32(sc->sc_dmamap->dm_segs[0].ds_addr +
   1265      1.15  jonathan 		offsetof(struct hifn_dma, dstr[0]));
   1266      1.15  jonathan 	dma->resr[HIFN_D_RES_RSIZE].p =
   1267      1.15  jonathan 	    htole32(sc->sc_dmamap->dm_segs[0].ds_addr +
   1268      1.15  jonathan 		offsetof(struct hifn_dma, resr[0]));
   1269       1.1    itojun 
   1270       1.1    itojun 	dma->cmdu = dma->srcu = dma->dstu = dma->resu = 0;
   1271       1.1    itojun 	dma->cmdi = dma->srci = dma->dsti = dma->resi = 0;
   1272       1.1    itojun 	dma->cmdk = dma->srck = dma->dstk = dma->resk = 0;
   1273       1.1    itojun }
   1274       1.1    itojun 
   1275       1.1    itojun /*
   1276       1.1    itojun  * Writes out the raw command buffer space.  Returns the
   1277       1.1    itojun  * command buffer size.
   1278       1.1    itojun  */
   1279       1.1    itojun u_int
   1280      1.17   thorpej hifn_write_command(struct hifn_command *cmd, u_int8_t *buf)
   1281       1.1    itojun {
   1282       1.1    itojun 	u_int8_t *buf_pos;
   1283      1.15  jonathan 	struct hifn_base_command *base_cmd;
   1284      1.15  jonathan 	struct hifn_mac_command *mac_cmd;
   1285      1.15  jonathan 	struct hifn_crypt_command *cry_cmd;
   1286      1.15  jonathan 	struct hifn_comp_command *comp_cmd;
   1287      1.20  jonathan 	int using_mac, using_crypt, using_comp, len, ivlen;
   1288      1.15  jonathan 	u_int32_t dlen, slen;
   1289       1.1    itojun 
   1290       1.1    itojun 	buf_pos = buf;
   1291       1.1    itojun 	using_mac = cmd->base_masks & HIFN_BASE_CMD_MAC;
   1292       1.1    itojun 	using_crypt = cmd->base_masks & HIFN_BASE_CMD_CRYPT;
   1293      1.15  jonathan 	using_comp = cmd->base_masks & HIFN_BASE_CMD_COMP;
   1294       1.1    itojun 
   1295      1.15  jonathan 	base_cmd = (struct hifn_base_command *)buf_pos;
   1296      1.15  jonathan 	base_cmd->masks = htole16(cmd->base_masks);
   1297      1.15  jonathan 	slen = cmd->src_map->dm_mapsize;
   1298      1.15  jonathan 	if (cmd->sloplen)
   1299      1.15  jonathan 		dlen = cmd->dst_map->dm_mapsize - cmd->sloplen +
   1300      1.15  jonathan 		    sizeof(u_int32_t);
   1301      1.15  jonathan 	else
   1302      1.15  jonathan 		dlen = cmd->dst_map->dm_mapsize;
   1303      1.15  jonathan 	base_cmd->total_source_count = htole16(slen & HIFN_BASE_CMD_LENMASK_LO);
   1304      1.15  jonathan 	base_cmd->total_dest_count = htole16(dlen & HIFN_BASE_CMD_LENMASK_LO);
   1305      1.15  jonathan 	dlen >>= 16;
   1306      1.15  jonathan 	slen >>= 16;
   1307      1.15  jonathan 	base_cmd->session_num = htole16(cmd->session_num |
   1308      1.15  jonathan 	    ((slen << HIFN_BASE_CMD_SRCLEN_S) & HIFN_BASE_CMD_SRCLEN_M) |
   1309      1.15  jonathan 	    ((dlen << HIFN_BASE_CMD_DSTLEN_S) & HIFN_BASE_CMD_DSTLEN_M));
   1310      1.15  jonathan 	buf_pos += sizeof(struct hifn_base_command);
   1311      1.15  jonathan 
   1312      1.15  jonathan 	if (using_comp) {
   1313      1.15  jonathan 		comp_cmd = (struct hifn_comp_command *)buf_pos;
   1314      1.15  jonathan 		dlen = cmd->compcrd->crd_len;
   1315      1.15  jonathan 		comp_cmd->source_count = htole16(dlen & 0xffff);
   1316      1.15  jonathan 		dlen >>= 16;
   1317      1.15  jonathan 		comp_cmd->masks = htole16(cmd->comp_masks |
   1318      1.15  jonathan 		    ((dlen << HIFN_COMP_CMD_SRCLEN_S) & HIFN_COMP_CMD_SRCLEN_M));
   1319      1.15  jonathan 		comp_cmd->header_skip = htole16(cmd->compcrd->crd_skip);
   1320      1.15  jonathan 		comp_cmd->reserved = 0;
   1321      1.15  jonathan 		buf_pos += sizeof(struct hifn_comp_command);
   1322      1.15  jonathan 	}
   1323       1.1    itojun 
   1324       1.1    itojun 	if (using_mac) {
   1325      1.15  jonathan 		mac_cmd = (struct hifn_mac_command *)buf_pos;
   1326      1.15  jonathan 		dlen = cmd->maccrd->crd_len;
   1327      1.15  jonathan 		mac_cmd->source_count = htole16(dlen & 0xffff);
   1328      1.15  jonathan 		dlen >>= 16;
   1329      1.15  jonathan 		mac_cmd->masks = htole16(cmd->mac_masks |
   1330      1.15  jonathan 		    ((dlen << HIFN_MAC_CMD_SRCLEN_S) & HIFN_MAC_CMD_SRCLEN_M));
   1331      1.15  jonathan 		mac_cmd->header_skip = htole16(cmd->maccrd->crd_skip);
   1332      1.15  jonathan 		mac_cmd->reserved = 0;
   1333      1.15  jonathan 		buf_pos += sizeof(struct hifn_mac_command);
   1334       1.1    itojun 	}
   1335       1.1    itojun 
   1336       1.1    itojun 	if (using_crypt) {
   1337      1.15  jonathan 		cry_cmd = (struct hifn_crypt_command *)buf_pos;
   1338      1.15  jonathan 		dlen = cmd->enccrd->crd_len;
   1339      1.15  jonathan 		cry_cmd->source_count = htole16(dlen & 0xffff);
   1340      1.15  jonathan 		dlen >>= 16;
   1341      1.15  jonathan 		cry_cmd->masks = htole16(cmd->cry_masks |
   1342      1.15  jonathan 		    ((dlen << HIFN_CRYPT_CMD_SRCLEN_S) & HIFN_CRYPT_CMD_SRCLEN_M));
   1343      1.15  jonathan 		cry_cmd->header_skip = htole16(cmd->enccrd->crd_skip);
   1344      1.15  jonathan 		cry_cmd->reserved = 0;
   1345      1.15  jonathan 		buf_pos += sizeof(struct hifn_crypt_command);
   1346       1.1    itojun 	}
   1347       1.1    itojun 
   1348      1.15  jonathan 	if (using_mac && cmd->mac_masks & HIFN_MAC_CMD_NEW_KEY) {
   1349      1.15  jonathan 		bcopy(cmd->mac, buf_pos, HIFN_MAC_KEY_LENGTH);
   1350       1.1    itojun 		buf_pos += HIFN_MAC_KEY_LENGTH;
   1351       1.1    itojun 	}
   1352       1.1    itojun 
   1353      1.15  jonathan 	if (using_crypt && cmd->cry_masks & HIFN_CRYPT_CMD_NEW_KEY) {
   1354      1.15  jonathan 		switch (cmd->cry_masks & HIFN_CRYPT_CMD_ALG_MASK) {
   1355      1.15  jonathan 		case HIFN_CRYPT_CMD_ALG_3DES:
   1356      1.15  jonathan 			bcopy(cmd->ck, buf_pos, HIFN_3DES_KEY_LENGTH);
   1357      1.15  jonathan 			buf_pos += HIFN_3DES_KEY_LENGTH;
   1358      1.15  jonathan 			break;
   1359      1.15  jonathan 		case HIFN_CRYPT_CMD_ALG_DES:
   1360      1.15  jonathan 			bcopy(cmd->ck, buf_pos, HIFN_DES_KEY_LENGTH);
   1361      1.20  jonathan 			buf_pos += HIFN_DES_KEY_LENGTH;
   1362      1.15  jonathan 			break;
   1363      1.15  jonathan 		case HIFN_CRYPT_CMD_ALG_RC4:
   1364      1.15  jonathan 			len = 256;
   1365      1.15  jonathan 			do {
   1366      1.15  jonathan 				int clen;
   1367      1.15  jonathan 
   1368      1.15  jonathan 				clen = MIN(cmd->cklen, len);
   1369      1.15  jonathan 				bcopy(cmd->ck, buf_pos, clen);
   1370      1.15  jonathan 				len -= clen;
   1371      1.15  jonathan 				buf_pos += clen;
   1372      1.15  jonathan 			} while (len > 0);
   1373      1.15  jonathan 			bzero(buf_pos, 4);
   1374      1.15  jonathan 			buf_pos += 4;
   1375      1.15  jonathan 			break;
   1376      1.20  jonathan 		case HIFN_CRYPT_CMD_ALG_AES:
   1377      1.20  jonathan 			/*
   1378      1.20  jonathan 			 * AES keys are variable 128, 192 and
   1379      1.20  jonathan 			 * 256 bits (16, 24 and 32 bytes).
   1380      1.20  jonathan 			 */
   1381      1.20  jonathan 			bcopy(cmd->ck, buf_pos, cmd->cklen);
   1382      1.20  jonathan 			buf_pos += cmd->cklen;
   1383      1.20  jonathan 			break;
   1384      1.15  jonathan 		}
   1385       1.1    itojun 	}
   1386       1.1    itojun 
   1387      1.15  jonathan 	if (using_crypt && cmd->cry_masks & HIFN_CRYPT_CMD_NEW_IV) {
   1388      1.20  jonathan 		switch (cmd->cry_masks & HIFN_CRYPT_CMD_ALG_MASK) {
   1389      1.20  jonathan 		case HIFN_CRYPT_CMD_ALG_AES:
   1390      1.20  jonathan 			ivlen = HIFN_AES_IV_LENGTH;
   1391      1.20  jonathan 			break;
   1392      1.20  jonathan 		default:
   1393      1.20  jonathan 			ivlen = HIFN_IV_LENGTH;
   1394      1.20  jonathan 			break;
   1395      1.20  jonathan 		}
   1396      1.20  jonathan 		bcopy(cmd->iv, buf_pos, ivlen);
   1397      1.20  jonathan 		buf_pos += ivlen;
   1398       1.1    itojun 	}
   1399       1.1    itojun 
   1400      1.15  jonathan 	if ((cmd->base_masks & (HIFN_BASE_CMD_MAC | HIFN_BASE_CMD_CRYPT |
   1401      1.15  jonathan 	    HIFN_BASE_CMD_COMP)) == 0) {
   1402      1.15  jonathan 		bzero(buf_pos, 8);
   1403       1.1    itojun 		buf_pos += 8;
   1404       1.1    itojun 	}
   1405       1.1    itojun 
   1406       1.1    itojun 	return (buf_pos - buf);
   1407       1.1    itojun }
   1408       1.1    itojun 
   1409      1.15  jonathan int
   1410      1.17   thorpej hifn_dmamap_aligned(bus_dmamap_t map)
   1411      1.15  jonathan {
   1412      1.15  jonathan 	int i;
   1413      1.15  jonathan 
   1414      1.15  jonathan 	for (i = 0; i < map->dm_nsegs; i++) {
   1415      1.15  jonathan 		if (map->dm_segs[i].ds_addr & 3)
   1416      1.15  jonathan 			return (0);
   1417      1.15  jonathan 		if ((i != (map->dm_nsegs - 1)) &&
   1418      1.15  jonathan 		    (map->dm_segs[i].ds_len & 3))
   1419      1.15  jonathan 			return (0);
   1420      1.15  jonathan 	}
   1421      1.15  jonathan 	return (1);
   1422      1.15  jonathan }
   1423      1.15  jonathan 
   1424      1.15  jonathan int
   1425      1.17   thorpej hifn_dmamap_load_dst(struct hifn_softc *sc, struct hifn_command *cmd)
   1426       1.1    itojun {
   1427      1.15  jonathan 	struct hifn_dma *dma = sc->sc_dma;
   1428      1.15  jonathan 	bus_dmamap_t map = cmd->dst_map;
   1429      1.15  jonathan 	u_int32_t p, l;
   1430      1.15  jonathan 	int idx, used = 0, i;
   1431      1.15  jonathan 
   1432      1.15  jonathan 	idx = dma->dsti;
   1433      1.15  jonathan 	for (i = 0; i < map->dm_nsegs - 1; i++) {
   1434      1.15  jonathan 		dma->dstr[idx].p = htole32(map->dm_segs[i].ds_addr);
   1435      1.15  jonathan 		dma->dstr[idx].l = htole32(HIFN_D_VALID |
   1436      1.15  jonathan 		    HIFN_D_MASKDONEIRQ | map->dm_segs[i].ds_len);
   1437      1.15  jonathan 		HIFN_DSTR_SYNC(sc, idx,
   1438      1.15  jonathan 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1439      1.15  jonathan 		used++;
   1440      1.15  jonathan 
   1441      1.15  jonathan 		if (++idx == HIFN_D_DST_RSIZE) {
   1442      1.15  jonathan 			dma->dstr[idx].l = htole32(HIFN_D_VALID |
   1443      1.15  jonathan 			    HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
   1444      1.15  jonathan 			HIFN_DSTR_SYNC(sc, idx,
   1445      1.15  jonathan 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1446      1.15  jonathan 			idx = 0;
   1447       1.1    itojun 		}
   1448      1.15  jonathan 	}
   1449       1.1    itojun 
   1450      1.15  jonathan 	if (cmd->sloplen == 0) {
   1451      1.15  jonathan 		p = map->dm_segs[i].ds_addr;
   1452      1.15  jonathan 		l = HIFN_D_VALID | HIFN_D_MASKDONEIRQ | HIFN_D_LAST |
   1453      1.15  jonathan 		    map->dm_segs[i].ds_len;
   1454      1.15  jonathan 	} else {
   1455      1.15  jonathan 		p = sc->sc_dmamap->dm_segs[0].ds_addr +
   1456      1.15  jonathan 		    offsetof(struct hifn_dma, slop[cmd->slopidx]);
   1457      1.15  jonathan 		l = HIFN_D_VALID | HIFN_D_MASKDONEIRQ | HIFN_D_LAST |
   1458      1.15  jonathan 		    sizeof(u_int32_t);
   1459      1.15  jonathan 
   1460      1.15  jonathan 		if ((map->dm_segs[i].ds_len - cmd->sloplen) != 0) {
   1461      1.15  jonathan 			dma->dstr[idx].p = htole32(map->dm_segs[i].ds_addr);
   1462      1.15  jonathan 			dma->dstr[idx].l = htole32(HIFN_D_VALID |
   1463      1.15  jonathan 			    HIFN_D_MASKDONEIRQ |
   1464      1.15  jonathan 			    (map->dm_segs[i].ds_len - cmd->sloplen));
   1465      1.15  jonathan 			HIFN_DSTR_SYNC(sc, idx,
   1466      1.15  jonathan 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1467      1.15  jonathan 			used++;
   1468      1.15  jonathan 
   1469      1.15  jonathan 			if (++idx == HIFN_D_DST_RSIZE) {
   1470      1.15  jonathan 				dma->dstr[idx].l = htole32(HIFN_D_VALID |
   1471      1.15  jonathan 				    HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
   1472      1.15  jonathan 				HIFN_DSTR_SYNC(sc, idx,
   1473      1.15  jonathan 				    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1474      1.15  jonathan 				idx = 0;
   1475       1.1    itojun 			}
   1476       1.1    itojun 		}
   1477       1.1    itojun 	}
   1478      1.15  jonathan 	dma->dstr[idx].p = htole32(p);
   1479      1.15  jonathan 	dma->dstr[idx].l = htole32(l);
   1480      1.15  jonathan 	HIFN_DSTR_SYNC(sc, idx, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1481      1.15  jonathan 	used++;
   1482      1.15  jonathan 
   1483      1.15  jonathan 	if (++idx == HIFN_D_DST_RSIZE) {
   1484      1.15  jonathan 		dma->dstr[idx].l = htole32(HIFN_D_VALID | HIFN_D_JUMP |
   1485      1.15  jonathan 		    HIFN_D_MASKDONEIRQ);
   1486      1.15  jonathan 		HIFN_DSTR_SYNC(sc, idx,
   1487      1.15  jonathan 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1488      1.15  jonathan 		idx = 0;
   1489      1.15  jonathan 	}
   1490      1.15  jonathan 
   1491      1.15  jonathan 	dma->dsti = idx;
   1492      1.15  jonathan 	dma->dstu += used;
   1493      1.15  jonathan 	return (idx);
   1494      1.15  jonathan }
   1495      1.15  jonathan 
   1496      1.15  jonathan int
   1497      1.17   thorpej hifn_dmamap_load_src(struct hifn_softc *sc, struct hifn_command *cmd)
   1498      1.15  jonathan {
   1499      1.15  jonathan 	struct hifn_dma *dma = sc->sc_dma;
   1500      1.15  jonathan 	bus_dmamap_t map = cmd->src_map;
   1501      1.15  jonathan 	int idx, i;
   1502      1.15  jonathan 	u_int32_t last = 0;
   1503      1.15  jonathan 
   1504      1.15  jonathan 	idx = dma->srci;
   1505      1.15  jonathan 	for (i = 0; i < map->dm_nsegs; i++) {
   1506      1.15  jonathan 		if (i == map->dm_nsegs - 1)
   1507      1.15  jonathan 			last = HIFN_D_LAST;
   1508      1.15  jonathan 
   1509      1.15  jonathan 		dma->srcr[idx].p = htole32(map->dm_segs[i].ds_addr);
   1510      1.15  jonathan 		dma->srcr[idx].l = htole32(map->dm_segs[i].ds_len |
   1511      1.15  jonathan 		    HIFN_D_VALID | HIFN_D_MASKDONEIRQ | last);
   1512      1.15  jonathan 		HIFN_SRCR_SYNC(sc, idx,
   1513      1.15  jonathan 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1514      1.15  jonathan 
   1515      1.15  jonathan 		if (++idx == HIFN_D_SRC_RSIZE) {
   1516      1.15  jonathan 			dma->srcr[idx].l = htole32(HIFN_D_VALID |
   1517      1.15  jonathan 			    HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
   1518      1.15  jonathan 			HIFN_SRCR_SYNC(sc, HIFN_D_SRC_RSIZE,
   1519      1.15  jonathan 			    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1520      1.15  jonathan 			idx = 0;
   1521      1.15  jonathan 		}
   1522      1.15  jonathan 	}
   1523      1.15  jonathan 	dma->srci = idx;
   1524      1.15  jonathan 	dma->srcu += map->dm_nsegs;
   1525      1.15  jonathan 	return (idx);
   1526      1.15  jonathan }
   1527      1.15  jonathan 
   1528  1.21.6.1      yamt int
   1529      1.17   thorpej hifn_crypto(struct hifn_softc *sc, struct hifn_command *cmd,
   1530      1.17   thorpej     struct cryptop *crp, int hint)
   1531      1.15  jonathan {
   1532      1.15  jonathan 	struct	hifn_dma *dma = sc->sc_dma;
   1533      1.15  jonathan 	u_int32_t cmdlen;
   1534      1.15  jonathan 	int	cmdi, resi, s, err = 0;
   1535      1.15  jonathan 
   1536      1.15  jonathan 	if (bus_dmamap_create(sc->sc_dmat, HIFN_MAX_DMALEN, MAX_SCATTER,
   1537      1.15  jonathan 	    HIFN_MAX_SEGLEN, 0, BUS_DMA_NOWAIT, &cmd->src_map))
   1538      1.15  jonathan 		return (ENOMEM);
   1539      1.15  jonathan 
   1540      1.15  jonathan 	if (crp->crp_flags & CRYPTO_F_IMBUF) {
   1541      1.15  jonathan 		if (bus_dmamap_load_mbuf(sc->sc_dmat, cmd->src_map,
   1542      1.15  jonathan 		    cmd->srcu.src_m, BUS_DMA_NOWAIT)) {
   1543      1.15  jonathan 			err = ENOMEM;
   1544      1.15  jonathan 			goto err_srcmap1;
   1545      1.15  jonathan 		}
   1546      1.15  jonathan 	} else if (crp->crp_flags & CRYPTO_F_IOV) {
   1547      1.15  jonathan 		if (bus_dmamap_load_uio(sc->sc_dmat, cmd->src_map,
   1548      1.15  jonathan 		    cmd->srcu.src_io, BUS_DMA_NOWAIT)) {
   1549      1.15  jonathan 			err = ENOMEM;
   1550      1.15  jonathan 			goto err_srcmap1;
   1551      1.15  jonathan 		}
   1552      1.15  jonathan 	} else {
   1553      1.15  jonathan 		err = EINVAL;
   1554      1.15  jonathan 		goto err_srcmap1;
   1555      1.15  jonathan 	}
   1556      1.15  jonathan 
   1557      1.15  jonathan 	if (hifn_dmamap_aligned(cmd->src_map)) {
   1558      1.15  jonathan 		cmd->sloplen = cmd->src_map->dm_mapsize & 3;
   1559      1.15  jonathan 		if (crp->crp_flags & CRYPTO_F_IOV)
   1560      1.15  jonathan 			cmd->dstu.dst_io = cmd->srcu.src_io;
   1561      1.15  jonathan 		else if (crp->crp_flags & CRYPTO_F_IMBUF)
   1562      1.15  jonathan 			cmd->dstu.dst_m = cmd->srcu.src_m;
   1563      1.15  jonathan 		cmd->dst_map = cmd->src_map;
   1564      1.15  jonathan 	} else {
   1565      1.15  jonathan 		if (crp->crp_flags & CRYPTO_F_IOV) {
   1566      1.15  jonathan 			err = EINVAL;
   1567      1.15  jonathan 			goto err_srcmap;
   1568      1.15  jonathan 		} else if (crp->crp_flags & CRYPTO_F_IMBUF) {
   1569      1.15  jonathan 			int totlen, len;
   1570      1.15  jonathan 			struct mbuf *m, *m0, *mlast;
   1571      1.15  jonathan 
   1572      1.15  jonathan 			totlen = cmd->src_map->dm_mapsize;
   1573      1.15  jonathan 			if (cmd->srcu.src_m->m_flags & M_PKTHDR) {
   1574      1.15  jonathan 				len = MHLEN;
   1575      1.15  jonathan 				MGETHDR(m0, M_DONTWAIT, MT_DATA);
   1576      1.15  jonathan 			} else {
   1577      1.15  jonathan 				len = MLEN;
   1578      1.15  jonathan 				MGET(m0, M_DONTWAIT, MT_DATA);
   1579      1.15  jonathan 			}
   1580      1.15  jonathan 			if (m0 == NULL) {
   1581      1.15  jonathan 				err = ENOMEM;
   1582      1.15  jonathan 				goto err_srcmap;
   1583      1.15  jonathan 			}
   1584      1.15  jonathan 			if (len == MHLEN)
   1585      1.15  jonathan 				M_DUP_PKTHDR(m0, cmd->srcu.src_m);
   1586      1.15  jonathan 			if (totlen >= MINCLSIZE) {
   1587      1.15  jonathan 				MCLGET(m0, M_DONTWAIT);
   1588      1.15  jonathan 				if (m0->m_flags & M_EXT)
   1589      1.15  jonathan 					len = MCLBYTES;
   1590      1.15  jonathan 			}
   1591      1.15  jonathan 			totlen -= len;
   1592      1.15  jonathan 			m0->m_pkthdr.len = m0->m_len = len;
   1593      1.15  jonathan 			mlast = m0;
   1594      1.15  jonathan 
   1595      1.15  jonathan 			while (totlen > 0) {
   1596      1.15  jonathan 				MGET(m, M_DONTWAIT, MT_DATA);
   1597      1.15  jonathan 				if (m == NULL) {
   1598      1.15  jonathan 					err = ENOMEM;
   1599      1.15  jonathan 					m_freem(m0);
   1600      1.15  jonathan 					goto err_srcmap;
   1601      1.15  jonathan 				}
   1602      1.15  jonathan 				len = MLEN;
   1603      1.15  jonathan 				if (totlen >= MINCLSIZE) {
   1604      1.15  jonathan 					MCLGET(m, M_DONTWAIT);
   1605      1.15  jonathan 					if (m->m_flags & M_EXT)
   1606      1.15  jonathan 						len = MCLBYTES;
   1607      1.15  jonathan 				}
   1608      1.15  jonathan 
   1609      1.15  jonathan 				m->m_len = len;
   1610      1.15  jonathan 				if (m0->m_flags & M_PKTHDR)
   1611      1.15  jonathan 					m0->m_pkthdr.len += len;
   1612      1.15  jonathan 				totlen -= len;
   1613      1.15  jonathan 
   1614      1.15  jonathan 				mlast->m_next = m;
   1615      1.15  jonathan 				mlast = m;
   1616      1.15  jonathan 			}
   1617      1.15  jonathan 			cmd->dstu.dst_m = m0;
   1618      1.15  jonathan 		}
   1619      1.15  jonathan 	}
   1620       1.1    itojun 
   1621      1.15  jonathan 	if (cmd->dst_map == NULL) {
   1622      1.15  jonathan 		if (bus_dmamap_create(sc->sc_dmat,
   1623      1.15  jonathan 		    HIFN_MAX_SEGLEN * MAX_SCATTER, MAX_SCATTER,
   1624      1.15  jonathan 		    HIFN_MAX_SEGLEN, 0, BUS_DMA_NOWAIT, &cmd->dst_map)) {
   1625      1.15  jonathan 			err = ENOMEM;
   1626      1.15  jonathan 			goto err_srcmap;
   1627      1.15  jonathan 		}
   1628      1.15  jonathan 		if (crp->crp_flags & CRYPTO_F_IMBUF) {
   1629      1.15  jonathan 			if (bus_dmamap_load_mbuf(sc->sc_dmat, cmd->dst_map,
   1630      1.15  jonathan 			    cmd->dstu.dst_m, BUS_DMA_NOWAIT)) {
   1631      1.15  jonathan 				err = ENOMEM;
   1632      1.15  jonathan 				goto err_dstmap1;
   1633      1.15  jonathan 			}
   1634      1.15  jonathan 		} else if (crp->crp_flags & CRYPTO_F_IOV) {
   1635      1.15  jonathan 			if (bus_dmamap_load_uio(sc->sc_dmat, cmd->dst_map,
   1636      1.15  jonathan 			    cmd->dstu.dst_io, BUS_DMA_NOWAIT)) {
   1637      1.15  jonathan 				err = ENOMEM;
   1638      1.15  jonathan 				goto err_dstmap1;
   1639      1.15  jonathan 			}
   1640      1.15  jonathan 		}
   1641      1.15  jonathan 	}
   1642       1.1    itojun 
   1643       1.1    itojun #ifdef HIFN_DEBUG
   1644      1.15  jonathan 	if (hifn_debug)
   1645      1.15  jonathan 		printf("%s: Entering cmd: stat %8x ien %8x u %d/%d/%d/%d n %d/%d\n",
   1646      1.15  jonathan 		    sc->sc_dv.dv_xname,
   1647      1.15  jonathan 		    READ_REG_1(sc, HIFN_1_DMA_CSR),
   1648      1.15  jonathan 		    READ_REG_1(sc, HIFN_1_DMA_IER),
   1649      1.15  jonathan 		    dma->cmdu, dma->srcu, dma->dstu, dma->resu,
   1650      1.15  jonathan 		    cmd->src_map->dm_nsegs, cmd->dst_map->dm_nsegs);
   1651      1.15  jonathan #endif
   1652      1.15  jonathan 
   1653      1.15  jonathan 	if (cmd->src_map == cmd->dst_map)
   1654      1.15  jonathan 		bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
   1655      1.15  jonathan 		    0, cmd->src_map->dm_mapsize,
   1656      1.15  jonathan 		    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
   1657      1.15  jonathan 	else {
   1658      1.15  jonathan 		bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
   1659      1.15  jonathan 		    0, cmd->src_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1660      1.15  jonathan 		bus_dmamap_sync(sc->sc_dmat, cmd->dst_map,
   1661      1.15  jonathan 		    0, cmd->dst_map->dm_mapsize, BUS_DMASYNC_PREREAD);
   1662      1.15  jonathan 	}
   1663       1.1    itojun 
   1664       1.1    itojun 	s = splnet();
   1665       1.1    itojun 
   1666       1.1    itojun 	/*
   1667       1.1    itojun 	 * need 1 cmd, and 1 res
   1668       1.1    itojun 	 * need N src, and N dst
   1669       1.1    itojun 	 */
   1670      1.15  jonathan 	if ((dma->cmdu + 1) > HIFN_D_CMD_RSIZE ||
   1671      1.15  jonathan 	    (dma->resu + 1) > HIFN_D_RES_RSIZE) {
   1672       1.1    itojun 		splx(s);
   1673      1.15  jonathan 		err = ENOMEM;
   1674      1.15  jonathan 		goto err_dstmap;
   1675      1.15  jonathan 	}
   1676      1.15  jonathan 	if ((dma->srcu + cmd->src_map->dm_nsegs) > HIFN_D_SRC_RSIZE ||
   1677      1.15  jonathan 	    (dma->dstu + cmd->dst_map->dm_nsegs + 1) > HIFN_D_DST_RSIZE) {
   1678      1.15  jonathan 		splx(s);
   1679      1.15  jonathan 		err = ENOMEM;
   1680      1.15  jonathan 		goto err_dstmap;
   1681       1.1    itojun 	}
   1682       1.1    itojun 
   1683       1.1    itojun 	if (dma->cmdi == HIFN_D_CMD_RSIZE) {
   1684       1.1    itojun 		dma->cmdi = 0;
   1685      1.15  jonathan 		dma->cmdr[HIFN_D_CMD_RSIZE].l = htole32(HIFN_D_VALID |
   1686      1.15  jonathan 		    HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
   1687      1.15  jonathan 		HIFN_CMDR_SYNC(sc, HIFN_D_CMD_RSIZE,
   1688      1.15  jonathan 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1689       1.1    itojun 	}
   1690       1.1    itojun 	cmdi = dma->cmdi++;
   1691      1.15  jonathan 	cmdlen = hifn_write_command(cmd, dma->command_bufs[cmdi]);
   1692      1.15  jonathan 	HIFN_CMD_SYNC(sc, cmdi, BUS_DMASYNC_PREWRITE);
   1693       1.1    itojun 
   1694       1.1    itojun 	/* .p for command/result already set */
   1695      1.15  jonathan 	dma->cmdr[cmdi].l = htole32(cmdlen | HIFN_D_VALID | HIFN_D_LAST |
   1696      1.15  jonathan 	    HIFN_D_MASKDONEIRQ);
   1697      1.15  jonathan 	HIFN_CMDR_SYNC(sc, cmdi,
   1698      1.15  jonathan 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1699       1.1    itojun 	dma->cmdu++;
   1700      1.15  jonathan 	if (sc->sc_c_busy == 0) {
   1701      1.15  jonathan 		WRITE_REG_1(sc, HIFN_1_DMA_CSR, HIFN_DMACSR_C_CTRL_ENA);
   1702      1.15  jonathan 		sc->sc_c_busy = 1;
   1703      1.15  jonathan 		SET_LED(sc, HIFN_MIPSRST_LED0);
   1704      1.15  jonathan 	}
   1705       1.1    itojun 
   1706       1.1    itojun 	/*
   1707       1.1    itojun 	 * We don't worry about missing an interrupt (which a "command wait"
   1708       1.1    itojun 	 * interrupt salvages us from), unless there is more than one command
   1709       1.1    itojun 	 * in the queue.
   1710       1.1    itojun 	 */
   1711      1.15  jonathan 	if (dma->cmdu > 1) {
   1712      1.15  jonathan 		sc->sc_dmaier |= HIFN_DMAIER_C_WAIT;
   1713      1.15  jonathan 		WRITE_REG_1(sc, HIFN_1_DMA_IER, sc->sc_dmaier);
   1714      1.15  jonathan 	}
   1715       1.1    itojun 
   1716       1.1    itojun 	hifnstats.hst_ipackets++;
   1717      1.15  jonathan 	hifnstats.hst_ibytes += cmd->src_map->dm_mapsize;
   1718       1.1    itojun 
   1719      1.15  jonathan 	hifn_dmamap_load_src(sc, cmd);
   1720      1.15  jonathan 	if (sc->sc_s_busy == 0) {
   1721      1.15  jonathan 		WRITE_REG_1(sc, HIFN_1_DMA_CSR, HIFN_DMACSR_S_CTRL_ENA);
   1722      1.15  jonathan 		sc->sc_s_busy = 1;
   1723      1.15  jonathan 		SET_LED(sc, HIFN_MIPSRST_LED1);
   1724       1.1    itojun 	}
   1725       1.1    itojun 
   1726       1.1    itojun 	/*
   1727       1.1    itojun 	 * Unlike other descriptors, we don't mask done interrupt from
   1728       1.1    itojun 	 * result descriptor.
   1729       1.1    itojun 	 */
   1730       1.1    itojun #ifdef HIFN_DEBUG
   1731      1.15  jonathan 	if (hifn_debug)
   1732      1.15  jonathan 		printf("load res\n");
   1733       1.1    itojun #endif
   1734      1.15  jonathan 	if (dma->resi == HIFN_D_RES_RSIZE) {
   1735      1.15  jonathan 		dma->resi = 0;
   1736      1.15  jonathan 		dma->resr[HIFN_D_RES_RSIZE].l = htole32(HIFN_D_VALID |
   1737      1.15  jonathan 		    HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
   1738      1.15  jonathan 		HIFN_RESR_SYNC(sc, HIFN_D_RES_RSIZE,
   1739      1.15  jonathan 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1740      1.15  jonathan 	}
   1741      1.15  jonathan 	resi = dma->resi++;
   1742       1.1    itojun 	dma->hifn_commands[resi] = cmd;
   1743      1.15  jonathan 	HIFN_RES_SYNC(sc, resi, BUS_DMASYNC_PREREAD);
   1744  1.21.6.1      yamt 	dma->resr[resi].l = htole32(HIFN_MAX_RESULT |
   1745      1.15  jonathan 	    HIFN_D_VALID | HIFN_D_LAST);
   1746      1.15  jonathan 	HIFN_RESR_SYNC(sc, resi,
   1747      1.15  jonathan 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1748       1.1    itojun 	dma->resu++;
   1749      1.15  jonathan 	if (sc->sc_r_busy == 0) {
   1750      1.15  jonathan 		WRITE_REG_1(sc, HIFN_1_DMA_CSR, HIFN_DMACSR_R_CTRL_ENA);
   1751      1.15  jonathan 		sc->sc_r_busy = 1;
   1752      1.15  jonathan 		SET_LED(sc, HIFN_MIPSRST_LED2);
   1753      1.15  jonathan 	}
   1754      1.15  jonathan 
   1755      1.15  jonathan 	if (cmd->sloplen)
   1756      1.15  jonathan 		cmd->slopidx = resi;
   1757      1.15  jonathan 
   1758      1.15  jonathan 	hifn_dmamap_load_dst(sc, cmd);
   1759      1.15  jonathan 
   1760      1.15  jonathan 	if (sc->sc_d_busy == 0) {
   1761      1.15  jonathan 		WRITE_REG_1(sc, HIFN_1_DMA_CSR, HIFN_DMACSR_D_CTRL_ENA);
   1762      1.15  jonathan 		sc->sc_d_busy = 1;
   1763      1.15  jonathan 	}
   1764       1.1    itojun 
   1765       1.1    itojun #ifdef HIFN_DEBUG
   1766      1.15  jonathan 	if (hifn_debug)
   1767      1.15  jonathan 		printf("%s: command: stat %8x ier %8x\n",
   1768      1.15  jonathan 		    sc->sc_dv.dv_xname,
   1769      1.15  jonathan 		    READ_REG_1(sc, HIFN_1_DMA_CSR), READ_REG_1(sc, HIFN_1_DMA_IER));
   1770       1.1    itojun #endif
   1771       1.1    itojun 
   1772      1.15  jonathan 	sc->sc_active = 5;
   1773      1.15  jonathan 	splx(s);
   1774      1.15  jonathan 	return (err);		/* success */
   1775      1.15  jonathan 
   1776      1.15  jonathan err_dstmap:
   1777      1.15  jonathan 	if (cmd->src_map != cmd->dst_map)
   1778      1.15  jonathan 		bus_dmamap_unload(sc->sc_dmat, cmd->dst_map);
   1779      1.15  jonathan err_dstmap1:
   1780      1.15  jonathan 	if (cmd->src_map != cmd->dst_map)
   1781      1.15  jonathan 		bus_dmamap_destroy(sc->sc_dmat, cmd->dst_map);
   1782      1.15  jonathan err_srcmap:
   1783      1.15  jonathan 	if (crp->crp_flags & CRYPTO_F_IMBUF &&
   1784      1.15  jonathan 	    cmd->srcu.src_m != cmd->dstu.dst_m)
   1785      1.15  jonathan 		m_freem(cmd->dstu.dst_m);
   1786      1.15  jonathan 	bus_dmamap_unload(sc->sc_dmat, cmd->src_map);
   1787      1.15  jonathan err_srcmap1:
   1788      1.15  jonathan 	bus_dmamap_destroy(sc->sc_dmat, cmd->src_map);
   1789      1.15  jonathan 	return (err);
   1790      1.15  jonathan }
   1791      1.15  jonathan 
   1792      1.15  jonathan void
   1793      1.17   thorpej hifn_tick(void *vsc)
   1794      1.15  jonathan {
   1795      1.15  jonathan 	struct hifn_softc *sc = vsc;
   1796      1.15  jonathan 	int s;
   1797      1.15  jonathan 
   1798      1.15  jonathan 	s = splnet();
   1799      1.15  jonathan 	if (sc->sc_active == 0) {
   1800      1.15  jonathan 		struct hifn_dma *dma = sc->sc_dma;
   1801      1.15  jonathan 		u_int32_t r = 0;
   1802      1.15  jonathan 
   1803      1.15  jonathan 		if (dma->cmdu == 0 && sc->sc_c_busy) {
   1804      1.15  jonathan 			sc->sc_c_busy = 0;
   1805      1.15  jonathan 			r |= HIFN_DMACSR_C_CTRL_DIS;
   1806      1.15  jonathan 			CLR_LED(sc, HIFN_MIPSRST_LED0);
   1807      1.15  jonathan 		}
   1808      1.15  jonathan 		if (dma->srcu == 0 && sc->sc_s_busy) {
   1809      1.15  jonathan 			sc->sc_s_busy = 0;
   1810      1.15  jonathan 			r |= HIFN_DMACSR_S_CTRL_DIS;
   1811      1.15  jonathan 			CLR_LED(sc, HIFN_MIPSRST_LED1);
   1812      1.15  jonathan 		}
   1813      1.15  jonathan 		if (dma->dstu == 0 && sc->sc_d_busy) {
   1814      1.15  jonathan 			sc->sc_d_busy = 0;
   1815      1.15  jonathan 			r |= HIFN_DMACSR_D_CTRL_DIS;
   1816      1.15  jonathan 		}
   1817      1.15  jonathan 		if (dma->resu == 0 && sc->sc_r_busy) {
   1818      1.15  jonathan 			sc->sc_r_busy = 0;
   1819      1.15  jonathan 			r |= HIFN_DMACSR_R_CTRL_DIS;
   1820      1.15  jonathan 			CLR_LED(sc, HIFN_MIPSRST_LED2);
   1821      1.15  jonathan 		}
   1822      1.15  jonathan 		if (r)
   1823      1.15  jonathan 			WRITE_REG_1(sc, HIFN_1_DMA_CSR, r);
   1824      1.15  jonathan 	}
   1825      1.15  jonathan 	else
   1826      1.15  jonathan 		sc->sc_active--;
   1827       1.1    itojun 	splx(s);
   1828      1.15  jonathan #ifdef	__OpenBSD__
   1829      1.15  jonathan 	timeout_add(&sc->sc_tickto, hz);
   1830      1.15  jonathan #else
   1831      1.15  jonathan 	callout_reset(&sc->sc_tickto, hz, hifn_tick, sc);
   1832       1.1    itojun #endif
   1833       1.1    itojun }
   1834       1.1    itojun 
   1835  1.21.6.1      yamt int
   1836      1.15  jonathan hifn_intr(void *arg)
   1837       1.1    itojun {
   1838       1.1    itojun 	struct hifn_softc *sc = arg;
   1839       1.1    itojun 	struct hifn_dma *dma = sc->sc_dma;
   1840      1.15  jonathan 	u_int32_t dmacsr, restart;
   1841       1.1    itojun 	int i, u;
   1842       1.1    itojun 
   1843       1.1    itojun 	dmacsr = READ_REG_1(sc, HIFN_1_DMA_CSR);
   1844       1.1    itojun 
   1845       1.1    itojun #ifdef HIFN_DEBUG
   1846      1.15  jonathan 	if (hifn_debug)
   1847      1.15  jonathan 		printf("%s: irq: stat %08x ien %08x u %d/%d/%d/%d\n",
   1848      1.15  jonathan 		       sc->sc_dv.dv_xname,
   1849      1.15  jonathan 		       dmacsr, READ_REG_1(sc, HIFN_1_DMA_IER),
   1850      1.15  jonathan 		       dma->cmdu, dma->srcu, dma->dstu, dma->resu);
   1851       1.1    itojun #endif
   1852       1.1    itojun 
   1853      1.15  jonathan 	/* Nothing in the DMA unit interrupted */
   1854      1.15  jonathan 	if ((dmacsr & sc->sc_dmaier) == 0)
   1855       1.1    itojun 		return (0);
   1856       1.1    itojun 
   1857      1.15  jonathan 	WRITE_REG_1(sc, HIFN_1_DMA_CSR, dmacsr & sc->sc_dmaier);
   1858      1.15  jonathan 
   1859      1.15  jonathan 	if (dmacsr & HIFN_DMACSR_ENGINE)
   1860      1.15  jonathan 		WRITE_REG_0(sc, HIFN_0_PUISR, READ_REG_0(sc, HIFN_0_PUISR));
   1861      1.15  jonathan 
   1862      1.15  jonathan 	if ((sc->sc_flags & HIFN_HAS_PUBLIC) &&
   1863      1.15  jonathan 	    (dmacsr & HIFN_DMACSR_PUBDONE))
   1864      1.15  jonathan 		WRITE_REG_1(sc, HIFN_1_PUB_STATUS,
   1865      1.15  jonathan 		    READ_REG_1(sc, HIFN_1_PUB_STATUS) | HIFN_PUBSTS_DONE);
   1866      1.15  jonathan 
   1867      1.15  jonathan 	restart = dmacsr & (HIFN_DMACSR_R_OVER | HIFN_DMACSR_D_OVER);
   1868      1.15  jonathan 	if (restart)
   1869      1.15  jonathan 		printf("%s: overrun %x\n", sc->sc_dv.dv_xname, dmacsr);
   1870      1.15  jonathan 
   1871      1.15  jonathan 	if (sc->sc_flags & HIFN_IS_7811) {
   1872      1.15  jonathan 		if (dmacsr & HIFN_DMACSR_ILLR)
   1873      1.15  jonathan 			printf("%s: illegal read\n", sc->sc_dv.dv_xname);
   1874      1.15  jonathan 		if (dmacsr & HIFN_DMACSR_ILLW)
   1875      1.15  jonathan 			printf("%s: illegal write\n", sc->sc_dv.dv_xname);
   1876      1.15  jonathan 	}
   1877      1.15  jonathan 
   1878      1.15  jonathan 	restart = dmacsr & (HIFN_DMACSR_C_ABORT | HIFN_DMACSR_S_ABORT |
   1879      1.15  jonathan 	    HIFN_DMACSR_D_ABORT | HIFN_DMACSR_R_ABORT);
   1880      1.15  jonathan 	if (restart) {
   1881      1.15  jonathan 		printf("%s: abort, resetting.\n", sc->sc_dv.dv_xname);
   1882      1.15  jonathan 		hifnstats.hst_abort++;
   1883      1.15  jonathan 		hifn_abort(sc);
   1884      1.15  jonathan 		return (1);
   1885      1.15  jonathan 	}
   1886       1.1    itojun 
   1887      1.15  jonathan 	if ((dmacsr & HIFN_DMACSR_C_WAIT) && (dma->resu == 0)) {
   1888       1.1    itojun 		/*
   1889       1.1    itojun 		 * If no slots to process and we receive a "waiting on
   1890       1.1    itojun 		 * command" interrupt, we disable the "waiting on command"
   1891       1.1    itojun 		 * (by clearing it).
   1892       1.1    itojun 		 */
   1893      1.15  jonathan 		sc->sc_dmaier &= ~HIFN_DMAIER_C_WAIT;
   1894      1.15  jonathan 		WRITE_REG_1(sc, HIFN_1_DMA_IER, sc->sc_dmaier);
   1895       1.1    itojun 	}
   1896       1.1    itojun 
   1897      1.15  jonathan 	/* clear the rings */
   1898      1.15  jonathan 	i = dma->resk;
   1899      1.15  jonathan 	while (dma->resu != 0) {
   1900      1.15  jonathan 		HIFN_RESR_SYNC(sc, i,
   1901      1.15  jonathan 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1902      1.15  jonathan 		if (dma->resr[i].l & htole32(HIFN_D_VALID)) {
   1903      1.15  jonathan 			HIFN_RESR_SYNC(sc, i,
   1904      1.15  jonathan 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1905      1.15  jonathan 			break;
   1906      1.15  jonathan 		}
   1907       1.1    itojun 
   1908      1.15  jonathan 		if (i != HIFN_D_RES_RSIZE) {
   1909      1.15  jonathan 			struct hifn_command *cmd;
   1910      1.15  jonathan 			u_int8_t *macbuf = NULL;
   1911      1.15  jonathan 
   1912      1.15  jonathan 			HIFN_RES_SYNC(sc, i, BUS_DMASYNC_POSTREAD);
   1913      1.15  jonathan 			cmd = dma->hifn_commands[i];
   1914  1.21.6.1      yamt 			KASSERT(cmd != NULL
   1915      1.15  jonathan 				/*("hifn_intr: null command slot %u", i)*/);
   1916      1.15  jonathan 			dma->hifn_commands[i] = NULL;
   1917      1.15  jonathan 
   1918      1.15  jonathan 			if (cmd->base_masks & HIFN_BASE_CMD_MAC) {
   1919      1.15  jonathan 				macbuf = dma->result_bufs[i];
   1920      1.15  jonathan 				macbuf += 12;
   1921      1.15  jonathan 			}
   1922       1.1    itojun 
   1923      1.15  jonathan 			hifn_callback(sc, cmd, macbuf);
   1924      1.15  jonathan 			hifnstats.hst_opackets++;
   1925       1.1    itojun 		}
   1926       1.1    itojun 
   1927      1.15  jonathan 		if (++i == (HIFN_D_RES_RSIZE + 1))
   1928      1.15  jonathan 			i = 0;
   1929      1.15  jonathan 		else
   1930      1.15  jonathan 			dma->resu--;
   1931       1.1    itojun 	}
   1932      1.15  jonathan 	dma->resk = i;
   1933       1.1    itojun 
   1934       1.1    itojun 	i = dma->srck; u = dma->srcu;
   1935      1.15  jonathan 	while (u != 0) {
   1936      1.15  jonathan 		HIFN_SRCR_SYNC(sc, i,
   1937      1.15  jonathan 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1938      1.15  jonathan 		if (dma->srcr[i].l & htole32(HIFN_D_VALID)) {
   1939      1.15  jonathan 			HIFN_SRCR_SYNC(sc, i,
   1940      1.15  jonathan 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1941      1.15  jonathan 			break;
   1942      1.15  jonathan 		}
   1943      1.15  jonathan 		if (++i == (HIFN_D_SRC_RSIZE + 1))
   1944       1.1    itojun 			i = 0;
   1945      1.15  jonathan 		else
   1946      1.15  jonathan 			u--;
   1947       1.1    itojun 	}
   1948       1.1    itojun 	dma->srck = i; dma->srcu = u;
   1949       1.1    itojun 
   1950       1.1    itojun 	i = dma->cmdk; u = dma->cmdu;
   1951      1.15  jonathan 	while (u != 0) {
   1952      1.15  jonathan 		HIFN_CMDR_SYNC(sc, i,
   1953      1.15  jonathan 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1954      1.15  jonathan 		if (dma->cmdr[i].l & htole32(HIFN_D_VALID)) {
   1955      1.15  jonathan 			HIFN_CMDR_SYNC(sc, i,
   1956      1.15  jonathan 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1957      1.15  jonathan 			break;
   1958      1.15  jonathan 		}
   1959      1.15  jonathan 		if (i != HIFN_D_CMD_RSIZE) {
   1960      1.15  jonathan 			u--;
   1961      1.15  jonathan 			HIFN_CMD_SYNC(sc, i, BUS_DMASYNC_POSTWRITE);
   1962      1.15  jonathan 		}
   1963      1.15  jonathan 		if (++i == (HIFN_D_CMD_RSIZE + 1))
   1964       1.1    itojun 			i = 0;
   1965       1.1    itojun 	}
   1966       1.1    itojun 	dma->cmdk = i; dma->cmdu = u;
   1967       1.1    itojun 
   1968       1.1    itojun 	return (1);
   1969       1.1    itojun }
   1970       1.1    itojun 
   1971       1.1    itojun /*
   1972       1.1    itojun  * Allocate a new 'session' and return an encoded session id.  'sidp'
   1973       1.1    itojun  * contains our registration id, and should contain an encoded session
   1974       1.1    itojun  * id on successful allocation.
   1975       1.1    itojun  */
   1976       1.1    itojun int
   1977      1.15  jonathan hifn_newsession(void *arg, u_int32_t *sidp, struct cryptoini *cri)
   1978       1.1    itojun {
   1979       1.1    itojun 	struct cryptoini *c;
   1980      1.15  jonathan 	struct hifn_softc *sc = arg;
   1981      1.15  jonathan 	int i, mac = 0, cry = 0, comp = 0;
   1982       1.1    itojun 
   1983      1.15  jonathan 	KASSERT(sc != NULL /*, ("hifn_newsession: null softc")*/);
   1984      1.15  jonathan 	if (sidp == NULL || cri == NULL || sc == NULL)
   1985       1.1    itojun 		return (EINVAL);
   1986       1.1    itojun 
   1987       1.1    itojun 	for (i = 0; i < sc->sc_maxses; i++)
   1988      1.15  jonathan 		if (sc->sc_sessions[i].hs_state == HS_STATE_FREE)
   1989       1.1    itojun 			break;
   1990       1.1    itojun 	if (i == sc->sc_maxses)
   1991       1.1    itojun 		return (ENOMEM);
   1992       1.1    itojun 
   1993       1.1    itojun 	for (c = cri; c != NULL; c = c->cri_next) {
   1994      1.15  jonathan 		switch (c->cri_alg) {
   1995      1.15  jonathan 		case CRYPTO_MD5:
   1996      1.15  jonathan 		case CRYPTO_SHA1:
   1997      1.15  jonathan 		case CRYPTO_MD5_HMAC:
   1998      1.15  jonathan 		case CRYPTO_SHA1_HMAC:
   1999       1.1    itojun 			if (mac)
   2000       1.1    itojun 				return (EINVAL);
   2001       1.1    itojun 			mac = 1;
   2002      1.15  jonathan 			break;
   2003      1.15  jonathan 		case CRYPTO_DES_CBC:
   2004      1.15  jonathan 		case CRYPTO_3DES_CBC:
   2005      1.20  jonathan 		case CRYPTO_AES_CBC:
   2006      1.15  jonathan #ifdef __NetBSD__
   2007      1.15  jonathan 			rnd_extract_data(sc->sc_sessions[i].hs_iv,
   2008      1.20  jonathan 			    c->cri_alg == CRYPTO_AES_CBC ?
   2009      1.20  jonathan 				HIFN_AES_IV_LENGTH : HIFN_IV_LENGTH,
   2010      1.20  jonathan 			    RND_EXTRACT_ANY);
   2011      1.15  jonathan #else	/* FreeBSD and OpenBSD have get_random_bytes */
   2012      1.15  jonathan 			/* XXX this may read fewer, does it matter? */
   2013  1.21.6.1      yamt  			get_random_bytes(sc->sc_sessions[i].hs_iv,
   2014      1.20  jonathan 				c->cri_alg == CRYPTO_AES_CBC ?
   2015      1.20  jonathan 					HIFN_AES_IV_LENGTH : HIFN_IV_LENGTH);
   2016      1.15  jonathan #endif
   2017      1.15  jonathan 			/*FALLTHROUGH*/
   2018      1.15  jonathan 		case CRYPTO_ARC4:
   2019       1.1    itojun 			if (cry)
   2020       1.1    itojun 				return (EINVAL);
   2021       1.1    itojun 			cry = 1;
   2022      1.15  jonathan 			break;
   2023      1.15  jonathan #ifdef HAVE_CRYPTO_LSZ
   2024      1.15  jonathan 		case CRYPTO_LZS_COMP:
   2025      1.15  jonathan 			if (comp)
   2026      1.15  jonathan 				return (EINVAL);
   2027      1.15  jonathan 			comp = 1;
   2028      1.15  jonathan 			break;
   2029      1.15  jonathan #endif
   2030      1.15  jonathan 		default:
   2031      1.15  jonathan 			return (EINVAL);
   2032       1.1    itojun 		}
   2033       1.1    itojun 	}
   2034      1.15  jonathan 	if (mac == 0 && cry == 0 && comp == 0)
   2035      1.15  jonathan 		return (EINVAL);
   2036      1.15  jonathan 
   2037      1.15  jonathan 	/*
   2038      1.15  jonathan 	 * XXX only want to support compression without chaining to
   2039      1.15  jonathan 	 * MAC/crypt engine right now
   2040      1.15  jonathan 	 */
   2041      1.15  jonathan 	if ((comp && mac) || (comp && cry))
   2042       1.1    itojun 		return (EINVAL);
   2043       1.1    itojun 
   2044       1.1    itojun 	*sidp = HIFN_SID(sc->sc_dv.dv_unit, i);
   2045      1.15  jonathan 	sc->sc_sessions[i].hs_state = HS_STATE_USED;
   2046       1.1    itojun 
   2047       1.1    itojun 	return (0);
   2048       1.1    itojun }
   2049       1.1    itojun 
   2050       1.1    itojun /*
   2051       1.1    itojun  * Deallocate a session.
   2052       1.1    itojun  * XXX this routine should run a zero'd mac/encrypt key into context ram.
   2053       1.1    itojun  * XXX to blow away any keys already stored there.
   2054       1.1    itojun  */
   2055       1.1    itojun int
   2056      1.15  jonathan hifn_freesession(void *arg, u_int64_t tid)
   2057       1.1    itojun {
   2058      1.15  jonathan 	struct hifn_softc *sc = arg;
   2059      1.15  jonathan 	int session;
   2060       1.1    itojun 	u_int32_t sid = ((u_int32_t) tid) & 0xffffffff;
   2061       1.1    itojun 
   2062      1.15  jonathan 	KASSERT(sc != NULL /*, ("hifn_freesession: null softc")*/);
   2063      1.15  jonathan 	if (sc == NULL)
   2064       1.1    itojun 		return (EINVAL);
   2065       1.1    itojun 
   2066       1.1    itojun 	session = HIFN_SESSION(sid);
   2067       1.1    itojun 	if (session >= sc->sc_maxses)
   2068       1.1    itojun 		return (EINVAL);
   2069       1.1    itojun 
   2070      1.15  jonathan 	bzero(&sc->sc_sessions[session], sizeof(sc->sc_sessions[session]));
   2071       1.1    itojun 	return (0);
   2072       1.1    itojun }
   2073       1.1    itojun 
   2074       1.1    itojun int
   2075      1.15  jonathan hifn_process(void *arg, struct cryptop *crp, int hint)
   2076       1.1    itojun {
   2077      1.15  jonathan 	struct hifn_softc *sc = arg;
   2078       1.1    itojun 	struct hifn_command *cmd = NULL;
   2079      1.20  jonathan 	int session, err, ivlen;
   2080       1.1    itojun 	struct cryptodesc *crd1, *crd2, *maccrd, *enccrd;
   2081       1.1    itojun 
   2082       1.1    itojun 	if (crp == NULL || crp->crp_callback == NULL) {
   2083       1.1    itojun 		hifnstats.hst_invalid++;
   2084       1.1    itojun 		return (EINVAL);
   2085       1.1    itojun 	}
   2086      1.15  jonathan 	session = HIFN_SESSION(crp->crp_sid);
   2087       1.1    itojun 
   2088      1.15  jonathan 	if (sc == NULL || session >= sc->sc_maxses) {
   2089       1.1    itojun 		err = EINVAL;
   2090       1.1    itojun 		goto errout;
   2091       1.1    itojun 	}
   2092       1.1    itojun 
   2093       1.1    itojun 	cmd = (struct hifn_command *)malloc(sizeof(struct hifn_command),
   2094       1.7   tsutsui 	    M_DEVBUF, M_NOWAIT|M_ZERO);
   2095       1.1    itojun 	if (cmd == NULL) {
   2096      1.15  jonathan 		hifnstats.hst_nomem++;
   2097       1.1    itojun 		err = ENOMEM;
   2098       1.1    itojun 		goto errout;
   2099       1.1    itojun 	}
   2100       1.1    itojun 
   2101       1.1    itojun 	if (crp->crp_flags & CRYPTO_F_IMBUF) {
   2102      1.15  jonathan 		cmd->srcu.src_m = (struct mbuf *)crp->crp_buf;
   2103      1.15  jonathan 		cmd->dstu.dst_m = (struct mbuf *)crp->crp_buf;
   2104      1.15  jonathan 	} else if (crp->crp_flags & CRYPTO_F_IOV) {
   2105      1.15  jonathan 		cmd->srcu.src_io = (struct uio *)crp->crp_buf;
   2106      1.15  jonathan 		cmd->dstu.dst_io = (struct uio *)crp->crp_buf;
   2107       1.1    itojun 	} else {
   2108       1.1    itojun 		err = EINVAL;
   2109      1.15  jonathan 		goto errout;	/* XXX we don't handle contiguous buffers! */
   2110       1.1    itojun 	}
   2111       1.1    itojun 
   2112       1.1    itojun 	crd1 = crp->crp_desc;
   2113       1.1    itojun 	if (crd1 == NULL) {
   2114       1.1    itojun 		err = EINVAL;
   2115       1.1    itojun 		goto errout;
   2116       1.1    itojun 	}
   2117       1.1    itojun 	crd2 = crd1->crd_next;
   2118       1.1    itojun 
   2119       1.1    itojun 	if (crd2 == NULL) {
   2120      1.15  jonathan 		if (crd1->crd_alg == CRYPTO_MD5_HMAC ||
   2121      1.15  jonathan 		    crd1->crd_alg == CRYPTO_SHA1_HMAC ||
   2122      1.15  jonathan 		    crd1->crd_alg == CRYPTO_SHA1 ||
   2123      1.15  jonathan 		    crd1->crd_alg == CRYPTO_MD5) {
   2124       1.1    itojun 			maccrd = crd1;
   2125       1.1    itojun 			enccrd = NULL;
   2126       1.1    itojun 		} else if (crd1->crd_alg == CRYPTO_DES_CBC ||
   2127      1.15  jonathan 			   crd1->crd_alg == CRYPTO_3DES_CBC ||
   2128      1.20  jonathan 			   crd1->crd_alg == CRYPTO_AES_CBC ||
   2129      1.15  jonathan 			   crd1->crd_alg == CRYPTO_ARC4) {
   2130       1.1    itojun 			if ((crd1->crd_flags & CRD_F_ENCRYPT) == 0)
   2131       1.1    itojun 				cmd->base_masks |= HIFN_BASE_CMD_DECODE;
   2132       1.1    itojun 			maccrd = NULL;
   2133       1.1    itojun 			enccrd = crd1;
   2134      1.15  jonathan #ifdef	HAVE_CRYPTO_LSZ
   2135      1.15  jonathan 		} else if (crd1->crd_alg == CRYPTO_LZS_COMP) {
   2136      1.15  jonathan 		  return (hifn_compression(sc, crp, cmd));
   2137      1.15  jonathan #endif
   2138       1.1    itojun 		} else {
   2139       1.1    itojun 			err = EINVAL;
   2140       1.1    itojun 			goto errout;
   2141       1.1    itojun 		}
   2142       1.1    itojun 	} else {
   2143      1.15  jonathan 		if ((crd1->crd_alg == CRYPTO_MD5_HMAC ||
   2144      1.15  jonathan 		     crd1->crd_alg == CRYPTO_SHA1_HMAC ||
   2145      1.15  jonathan 		     crd1->crd_alg == CRYPTO_MD5 ||
   2146      1.15  jonathan 		     crd1->crd_alg == CRYPTO_SHA1) &&
   2147       1.1    itojun 		    (crd2->crd_alg == CRYPTO_DES_CBC ||
   2148      1.15  jonathan 		     crd2->crd_alg == CRYPTO_3DES_CBC ||
   2149      1.20  jonathan 		     crd2->crd_alg == CRYPTO_AES_CBC ||
   2150      1.15  jonathan 		     crd2->crd_alg == CRYPTO_ARC4) &&
   2151       1.1    itojun 		    ((crd2->crd_flags & CRD_F_ENCRYPT) == 0)) {
   2152       1.1    itojun 			cmd->base_masks = HIFN_BASE_CMD_DECODE;
   2153       1.1    itojun 			maccrd = crd1;
   2154       1.1    itojun 			enccrd = crd2;
   2155       1.1    itojun 		} else if ((crd1->crd_alg == CRYPTO_DES_CBC ||
   2156      1.15  jonathan 			    crd1->crd_alg == CRYPTO_ARC4 ||
   2157      1.20  jonathan 			    crd1->crd_alg == CRYPTO_3DES_CBC ||
   2158      1.20  jonathan 			    crd1->crd_alg == CRYPTO_AES_CBC) &&
   2159      1.15  jonathan 			   (crd2->crd_alg == CRYPTO_MD5_HMAC ||
   2160      1.15  jonathan 			    crd2->crd_alg == CRYPTO_SHA1_HMAC ||
   2161      1.15  jonathan 			    crd2->crd_alg == CRYPTO_MD5 ||
   2162      1.15  jonathan 			    crd2->crd_alg == CRYPTO_SHA1) &&
   2163      1.15  jonathan 			   (crd1->crd_flags & CRD_F_ENCRYPT)) {
   2164       1.1    itojun 			enccrd = crd1;
   2165       1.1    itojun 			maccrd = crd2;
   2166       1.1    itojun 		} else {
   2167       1.1    itojun 			/*
   2168       1.1    itojun 			 * We cannot order the 7751 as requested
   2169       1.1    itojun 			 */
   2170       1.1    itojun 			err = EINVAL;
   2171       1.1    itojun 			goto errout;
   2172       1.1    itojun 		}
   2173       1.1    itojun 	}
   2174       1.1    itojun 
   2175       1.1    itojun 	if (enccrd) {
   2176      1.15  jonathan 		cmd->enccrd = enccrd;
   2177       1.1    itojun 		cmd->base_masks |= HIFN_BASE_CMD_CRYPT;
   2178      1.15  jonathan 		switch (enccrd->crd_alg) {
   2179      1.15  jonathan 		case CRYPTO_ARC4:
   2180      1.15  jonathan 			cmd->cry_masks |= HIFN_CRYPT_CMD_ALG_RC4;
   2181      1.15  jonathan 			if ((enccrd->crd_flags & CRD_F_ENCRYPT)
   2182      1.15  jonathan 			    != sc->sc_sessions[session].hs_prev_op)
   2183      1.15  jonathan 				sc->sc_sessions[session].hs_state =
   2184      1.15  jonathan 				    HS_STATE_USED;
   2185      1.15  jonathan 			break;
   2186      1.15  jonathan 		case CRYPTO_DES_CBC:
   2187      1.15  jonathan 			cmd->cry_masks |= HIFN_CRYPT_CMD_ALG_DES |
   2188      1.15  jonathan 			    HIFN_CRYPT_CMD_MODE_CBC |
   2189      1.15  jonathan 			    HIFN_CRYPT_CMD_NEW_IV;
   2190      1.15  jonathan 			break;
   2191      1.15  jonathan 		case CRYPTO_3DES_CBC:
   2192      1.15  jonathan 			cmd->cry_masks |= HIFN_CRYPT_CMD_ALG_3DES |
   2193      1.15  jonathan 			    HIFN_CRYPT_CMD_MODE_CBC |
   2194      1.15  jonathan 			    HIFN_CRYPT_CMD_NEW_IV;
   2195      1.15  jonathan 			break;
   2196      1.20  jonathan 		case CRYPTO_AES_CBC:
   2197      1.20  jonathan 			cmd->cry_masks |= HIFN_CRYPT_CMD_ALG_AES |
   2198      1.20  jonathan 			    HIFN_CRYPT_CMD_MODE_CBC |
   2199      1.20  jonathan 			    HIFN_CRYPT_CMD_NEW_IV;
   2200      1.20  jonathan 			break;
   2201      1.15  jonathan 		default:
   2202      1.15  jonathan 			err = EINVAL;
   2203      1.15  jonathan 			goto errout;
   2204      1.15  jonathan 		}
   2205      1.15  jonathan 		if (enccrd->crd_alg != CRYPTO_ARC4) {
   2206      1.20  jonathan 			ivlen = ((enccrd->crd_alg == CRYPTO_AES_CBC) ?
   2207      1.20  jonathan 				HIFN_AES_IV_LENGTH : HIFN_IV_LENGTH);
   2208      1.15  jonathan 			if (enccrd->crd_flags & CRD_F_ENCRYPT) {
   2209      1.15  jonathan 				if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
   2210      1.20  jonathan 					bcopy(enccrd->crd_iv, cmd->iv, ivlen);
   2211      1.15  jonathan 				else
   2212      1.15  jonathan 					bcopy(sc->sc_sessions[session].hs_iv,
   2213      1.20  jonathan 					    cmd->iv, ivlen);
   2214      1.15  jonathan 
   2215      1.15  jonathan 				if ((enccrd->crd_flags & CRD_F_IV_PRESENT)
   2216      1.15  jonathan 				    == 0) {
   2217      1.15  jonathan 					if (crp->crp_flags & CRYPTO_F_IMBUF)
   2218      1.15  jonathan 						m_copyback(cmd->srcu.src_m,
   2219      1.15  jonathan 						    enccrd->crd_inject,
   2220      1.20  jonathan 						    ivlen, cmd->iv);
   2221      1.15  jonathan 					else if (crp->crp_flags & CRYPTO_F_IOV)
   2222      1.15  jonathan 						cuio_copyback(cmd->srcu.src_io,
   2223      1.15  jonathan 						    enccrd->crd_inject,
   2224      1.20  jonathan 						    ivlen, cmd->iv);
   2225      1.15  jonathan 				}
   2226      1.15  jonathan 			} else {
   2227      1.15  jonathan 				if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
   2228      1.20  jonathan 					bcopy(enccrd->crd_iv, cmd->iv, ivlen);
   2229      1.15  jonathan 				else if (crp->crp_flags & CRYPTO_F_IMBUF)
   2230      1.15  jonathan 					m_copydata(cmd->srcu.src_m,
   2231      1.20  jonathan 					    enccrd->crd_inject, ivlen, cmd->iv);
   2232      1.15  jonathan 				else if (crp->crp_flags & CRYPTO_F_IOV)
   2233      1.15  jonathan 					cuio_copydata(cmd->srcu.src_io,
   2234      1.20  jonathan 					    enccrd->crd_inject, ivlen, cmd->iv);
   2235      1.15  jonathan 			}
   2236       1.1    itojun 		}
   2237       1.1    itojun 
   2238       1.1    itojun 		cmd->ck = enccrd->crd_key;
   2239      1.15  jonathan 		cmd->cklen = enccrd->crd_klen >> 3;
   2240       1.1    itojun 
   2241  1.21.6.1      yamt 		/*
   2242      1.20  jonathan 		 * Need to specify the size for the AES key in the masks.
   2243      1.20  jonathan 		 */
   2244      1.20  jonathan 		if ((cmd->cry_masks & HIFN_CRYPT_CMD_ALG_MASK) ==
   2245      1.20  jonathan 		    HIFN_CRYPT_CMD_ALG_AES) {
   2246      1.20  jonathan 			switch (cmd->cklen) {
   2247      1.20  jonathan 			case 16:
   2248      1.20  jonathan 				cmd->cry_masks |= HIFN_CRYPT_CMD_KSZ_128;
   2249      1.20  jonathan 				break;
   2250      1.20  jonathan 			case 24:
   2251      1.20  jonathan 				cmd->cry_masks |= HIFN_CRYPT_CMD_KSZ_192;
   2252      1.20  jonathan 				break;
   2253      1.20  jonathan 			case 32:
   2254      1.20  jonathan 				cmd->cry_masks |= HIFN_CRYPT_CMD_KSZ_256;
   2255      1.20  jonathan 				break;
   2256      1.20  jonathan 			default:
   2257      1.20  jonathan 				err = EINVAL;
   2258      1.20  jonathan 				goto errout;
   2259      1.20  jonathan 			}
   2260      1.20  jonathan 		}
   2261      1.20  jonathan 
   2262      1.15  jonathan 		if (sc->sc_sessions[session].hs_state == HS_STATE_USED)
   2263       1.1    itojun 			cmd->cry_masks |= HIFN_CRYPT_CMD_NEW_KEY;
   2264       1.1    itojun 	}
   2265       1.1    itojun 
   2266       1.1    itojun 	if (maccrd) {
   2267      1.15  jonathan 		cmd->maccrd = maccrd;
   2268       1.1    itojun 		cmd->base_masks |= HIFN_BASE_CMD_MAC;
   2269       1.1    itojun 
   2270      1.15  jonathan 		switch (maccrd->crd_alg) {
   2271      1.15  jonathan 		case CRYPTO_MD5:
   2272      1.15  jonathan 			cmd->mac_masks |= HIFN_MAC_CMD_ALG_MD5 |
   2273      1.15  jonathan 			    HIFN_MAC_CMD_RESULT | HIFN_MAC_CMD_MODE_HASH |
   2274      1.15  jonathan 			    HIFN_MAC_CMD_POS_IPSEC;
   2275      1.15  jonathan 			break;
   2276      1.15  jonathan 		case CRYPTO_MD5_HMAC:
   2277      1.15  jonathan 			cmd->mac_masks |= HIFN_MAC_CMD_ALG_MD5 |
   2278      1.15  jonathan 			    HIFN_MAC_CMD_RESULT | HIFN_MAC_CMD_MODE_HMAC |
   2279      1.15  jonathan 			    HIFN_MAC_CMD_POS_IPSEC | HIFN_MAC_CMD_TRUNC;
   2280      1.15  jonathan 			break;
   2281      1.15  jonathan 		case CRYPTO_SHA1:
   2282      1.15  jonathan 			cmd->mac_masks |= HIFN_MAC_CMD_ALG_SHA1 |
   2283      1.15  jonathan 			    HIFN_MAC_CMD_RESULT | HIFN_MAC_CMD_MODE_HASH |
   2284      1.15  jonathan 			    HIFN_MAC_CMD_POS_IPSEC;
   2285      1.15  jonathan 			break;
   2286      1.15  jonathan 		case CRYPTO_SHA1_HMAC:
   2287      1.15  jonathan 			cmd->mac_masks |= HIFN_MAC_CMD_ALG_SHA1 |
   2288      1.15  jonathan 			    HIFN_MAC_CMD_RESULT | HIFN_MAC_CMD_MODE_HMAC |
   2289      1.15  jonathan 			    HIFN_MAC_CMD_POS_IPSEC | HIFN_MAC_CMD_TRUNC;
   2290      1.15  jonathan 			break;
   2291      1.15  jonathan 		}
   2292       1.1    itojun 
   2293      1.15  jonathan 		if ((maccrd->crd_alg == CRYPTO_SHA1_HMAC ||
   2294      1.15  jonathan 		     maccrd->crd_alg == CRYPTO_MD5_HMAC) &&
   2295      1.15  jonathan 		    sc->sc_sessions[session].hs_state == HS_STATE_USED) {
   2296       1.1    itojun 			cmd->mac_masks |= HIFN_MAC_CMD_NEW_KEY;
   2297      1.15  jonathan 			bcopy(maccrd->crd_key, cmd->mac, maccrd->crd_klen >> 3);
   2298      1.15  jonathan 			bzero(cmd->mac + (maccrd->crd_klen >> 3),
   2299       1.1    itojun 			    HIFN_MAC_KEY_LENGTH - (maccrd->crd_klen >> 3));
   2300       1.1    itojun 		}
   2301       1.1    itojun 	}
   2302       1.1    itojun 
   2303      1.15  jonathan 	cmd->crp = crp;
   2304       1.1    itojun 	cmd->session_num = session;
   2305       1.1    itojun 	cmd->softc = sc;
   2306       1.1    itojun 
   2307      1.15  jonathan 	err = hifn_crypto(sc, cmd, crp, hint);
   2308      1.15  jonathan 	if (err == 0) {
   2309      1.15  jonathan 		if (enccrd)
   2310      1.15  jonathan 			sc->sc_sessions[session].hs_prev_op =
   2311      1.15  jonathan 				enccrd->crd_flags & CRD_F_ENCRYPT;
   2312      1.15  jonathan 		if (sc->sc_sessions[session].hs_state == HS_STATE_USED)
   2313      1.15  jonathan 			sc->sc_sessions[session].hs_state = HS_STATE_KEY;
   2314      1.15  jonathan 		return 0;
   2315      1.15  jonathan 	} else if (err == ERESTART) {
   2316      1.15  jonathan 		/*
   2317      1.15  jonathan 		 * There weren't enough resources to dispatch the request
   2318      1.15  jonathan 		 * to the part.  Notify the caller so they'll requeue this
   2319      1.15  jonathan 		 * request and resubmit it again soon.
   2320      1.15  jonathan 		 */
   2321      1.15  jonathan #ifdef HIFN_DEBUG
   2322      1.15  jonathan 		if (hifn_debug)
   2323      1.15  jonathan 			printf(sc->sc_dv.dv_xname, "requeue request\n");
   2324      1.15  jonathan #endif
   2325      1.15  jonathan 		free(cmd, M_DEVBUF);
   2326      1.15  jonathan 		sc->sc_needwakeup |= CRYPTO_SYMQ;
   2327      1.15  jonathan 		return (err);
   2328      1.15  jonathan 	}
   2329       1.1    itojun 
   2330       1.1    itojun errout:
   2331       1.1    itojun 	if (cmd != NULL)
   2332       1.1    itojun 		free(cmd, M_DEVBUF);
   2333       1.1    itojun 	if (err == EINVAL)
   2334       1.1    itojun 		hifnstats.hst_invalid++;
   2335       1.1    itojun 	else
   2336       1.1    itojun 		hifnstats.hst_nomem++;
   2337       1.1    itojun 	crp->crp_etype = err;
   2338      1.15  jonathan 	crypto_done(crp);
   2339       1.1    itojun 	return (0);
   2340       1.1    itojun }
   2341       1.1    itojun 
   2342       1.1    itojun void
   2343      1.15  jonathan hifn_abort(struct hifn_softc *sc)
   2344      1.15  jonathan {
   2345      1.15  jonathan 	struct hifn_dma *dma = sc->sc_dma;
   2346      1.15  jonathan 	struct hifn_command *cmd;
   2347      1.15  jonathan 	struct cryptop *crp;
   2348      1.15  jonathan 	int i, u;
   2349      1.15  jonathan 
   2350      1.15  jonathan 	i = dma->resk; u = dma->resu;
   2351      1.15  jonathan 	while (u != 0) {
   2352      1.15  jonathan 		cmd = dma->hifn_commands[i];
   2353      1.15  jonathan 		KASSERT(cmd != NULL /*, ("hifn_abort: null cmd slot %u", i)*/);
   2354      1.15  jonathan 		dma->hifn_commands[i] = NULL;
   2355      1.15  jonathan 		crp = cmd->crp;
   2356      1.15  jonathan 
   2357      1.15  jonathan 		if ((dma->resr[i].l & htole32(HIFN_D_VALID)) == 0) {
   2358      1.15  jonathan 			/* Salvage what we can. */
   2359      1.15  jonathan 			u_int8_t *macbuf;
   2360      1.15  jonathan 
   2361      1.15  jonathan 			if (cmd->base_masks & HIFN_BASE_CMD_MAC) {
   2362      1.15  jonathan 				macbuf = dma->result_bufs[i];
   2363      1.15  jonathan 				macbuf += 12;
   2364      1.15  jonathan 			} else
   2365      1.15  jonathan 				macbuf = NULL;
   2366      1.15  jonathan 			hifnstats.hst_opackets++;
   2367      1.15  jonathan 			hifn_callback(sc, cmd, macbuf);
   2368      1.15  jonathan 		} else {
   2369      1.15  jonathan 			if (cmd->src_map == cmd->dst_map) {
   2370      1.15  jonathan 				bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
   2371      1.15  jonathan 						0, cmd->src_map->dm_mapsize,
   2372      1.15  jonathan 				    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   2373      1.15  jonathan 			} else {
   2374      1.15  jonathan 				bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
   2375      1.15  jonathan 				    0, cmd->src_map->dm_mapsize,
   2376      1.15  jonathan 				    BUS_DMASYNC_POSTWRITE);
   2377      1.15  jonathan 				bus_dmamap_sync(sc->sc_dmat, cmd->dst_map,
   2378      1.15  jonathan 				    0, cmd->dst_map->dm_mapsize,
   2379      1.15  jonathan 				    BUS_DMASYNC_POSTREAD);
   2380      1.15  jonathan 			}
   2381      1.15  jonathan 
   2382      1.15  jonathan 			if (cmd->srcu.src_m != cmd->dstu.dst_m) {
   2383      1.15  jonathan 				m_freem(cmd->srcu.src_m);
   2384      1.15  jonathan 				crp->crp_buf = (caddr_t)cmd->dstu.dst_m;
   2385      1.15  jonathan 			}
   2386      1.15  jonathan 
   2387      1.15  jonathan 			/* non-shared buffers cannot be restarted */
   2388      1.15  jonathan 			if (cmd->src_map != cmd->dst_map) {
   2389      1.15  jonathan 				/*
   2390      1.15  jonathan 				 * XXX should be EAGAIN, delayed until
   2391      1.15  jonathan 				 * after the reset.
   2392      1.15  jonathan 				 */
   2393      1.15  jonathan 				crp->crp_etype = ENOMEM;
   2394      1.15  jonathan 				bus_dmamap_unload(sc->sc_dmat, cmd->dst_map);
   2395      1.15  jonathan 				bus_dmamap_destroy(sc->sc_dmat, cmd->dst_map);
   2396      1.15  jonathan 			} else
   2397      1.15  jonathan 				crp->crp_etype = ENOMEM;
   2398      1.15  jonathan 
   2399      1.15  jonathan 			bus_dmamap_unload(sc->sc_dmat, cmd->src_map);
   2400      1.15  jonathan 			bus_dmamap_destroy(sc->sc_dmat, cmd->src_map);
   2401      1.15  jonathan 
   2402      1.15  jonathan 			free(cmd, M_DEVBUF);
   2403      1.15  jonathan 			if (crp->crp_etype != EAGAIN)
   2404      1.15  jonathan 				crypto_done(crp);
   2405      1.15  jonathan 		}
   2406      1.15  jonathan 
   2407      1.15  jonathan 		if (++i == HIFN_D_RES_RSIZE)
   2408      1.15  jonathan 			i = 0;
   2409      1.15  jonathan 		u--;
   2410      1.15  jonathan 	}
   2411      1.15  jonathan 	dma->resk = i; dma->resu = u;
   2412      1.15  jonathan 
   2413      1.15  jonathan 	/* Force upload of key next time */
   2414      1.15  jonathan 	for (i = 0; i < sc->sc_maxses; i++)
   2415      1.15  jonathan 		if (sc->sc_sessions[i].hs_state == HS_STATE_KEY)
   2416      1.15  jonathan 			sc->sc_sessions[i].hs_state = HS_STATE_USED;
   2417  1.21.6.1      yamt 
   2418      1.15  jonathan 	hifn_reset_board(sc, 1);
   2419      1.15  jonathan 	hifn_init_dma(sc);
   2420      1.15  jonathan 	hifn_init_pci_registers(sc);
   2421      1.15  jonathan }
   2422      1.15  jonathan 
   2423      1.15  jonathan void
   2424      1.17   thorpej hifn_callback(struct hifn_softc *sc, struct hifn_command *cmd, u_int8_t *resbuf)
   2425       1.1    itojun {
   2426       1.1    itojun 	struct hifn_dma *dma = sc->sc_dma;
   2427      1.15  jonathan 	struct cryptop *crp = cmd->crp;
   2428       1.1    itojun 	struct cryptodesc *crd;
   2429       1.1    itojun 	struct mbuf *m;
   2430      1.20  jonathan 	int totlen, i, u, ivlen;
   2431       1.1    itojun 
   2432      1.15  jonathan 	if (cmd->src_map == cmd->dst_map)
   2433      1.15  jonathan 		bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
   2434      1.15  jonathan 		    0, cmd->src_map->dm_mapsize,
   2435      1.15  jonathan 		    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   2436      1.15  jonathan 	else {
   2437      1.15  jonathan 		bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
   2438      1.15  jonathan 		    0, cmd->src_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   2439      1.15  jonathan 		bus_dmamap_sync(sc->sc_dmat, cmd->dst_map,
   2440      1.15  jonathan 		    0, cmd->dst_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
   2441       1.1    itojun 	}
   2442       1.1    itojun 
   2443      1.15  jonathan 	if (crp->crp_flags & CRYPTO_F_IMBUF) {
   2444      1.15  jonathan 		if (cmd->srcu.src_m != cmd->dstu.dst_m) {
   2445      1.15  jonathan 			crp->crp_buf = (caddr_t)cmd->dstu.dst_m;
   2446      1.15  jonathan 			totlen = cmd->src_map->dm_mapsize;
   2447      1.15  jonathan 			for (m = cmd->dstu.dst_m; m != NULL; m = m->m_next) {
   2448      1.15  jonathan 				if (totlen < m->m_len) {
   2449      1.15  jonathan 					m->m_len = totlen;
   2450      1.15  jonathan 					totlen = 0;
   2451      1.15  jonathan 				} else
   2452      1.15  jonathan 					totlen -= m->m_len;
   2453      1.15  jonathan 			}
   2454      1.15  jonathan 			cmd->dstu.dst_m->m_pkthdr.len =
   2455      1.15  jonathan 			    cmd->srcu.src_m->m_pkthdr.len;
   2456      1.15  jonathan 			m_freem(cmd->srcu.src_m);
   2457      1.15  jonathan 		}
   2458      1.15  jonathan 	}
   2459      1.15  jonathan 
   2460      1.15  jonathan 	if (cmd->sloplen != 0) {
   2461      1.15  jonathan 		if (crp->crp_flags & CRYPTO_F_IMBUF)
   2462      1.15  jonathan 			m_copyback((struct mbuf *)crp->crp_buf,
   2463      1.15  jonathan 			    cmd->src_map->dm_mapsize - cmd->sloplen,
   2464      1.15  jonathan 			    cmd->sloplen, (caddr_t)&dma->slop[cmd->slopidx]);
   2465      1.15  jonathan 		else if (crp->crp_flags & CRYPTO_F_IOV)
   2466      1.15  jonathan 			cuio_copyback((struct uio *)crp->crp_buf,
   2467      1.15  jonathan 			    cmd->src_map->dm_mapsize - cmd->sloplen,
   2468      1.15  jonathan 			    cmd->sloplen, (caddr_t)&dma->slop[cmd->slopidx]);
   2469      1.15  jonathan 	}
   2470      1.15  jonathan 
   2471      1.15  jonathan 	i = dma->dstk; u = dma->dstu;
   2472      1.15  jonathan 	while (u != 0) {
   2473      1.15  jonathan 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   2474      1.15  jonathan 		    offsetof(struct hifn_dma, dstr[i]), sizeof(struct hifn_desc),
   2475      1.15  jonathan 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   2476      1.15  jonathan 		if (dma->dstr[i].l & htole32(HIFN_D_VALID)) {
   2477      1.15  jonathan 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   2478      1.15  jonathan 			    offsetof(struct hifn_dma, dstr[i]),
   2479      1.15  jonathan 			    sizeof(struct hifn_desc),
   2480      1.15  jonathan 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   2481      1.15  jonathan 			break;
   2482       1.1    itojun 		}
   2483      1.15  jonathan 		if (++i == (HIFN_D_DST_RSIZE + 1))
   2484      1.15  jonathan 			i = 0;
   2485      1.15  jonathan 		else
   2486      1.15  jonathan 			u--;
   2487       1.1    itojun 	}
   2488      1.15  jonathan 	dma->dstk = i; dma->dstu = u;
   2489      1.15  jonathan 
   2490      1.15  jonathan 	hifnstats.hst_obytes += cmd->dst_map->dm_mapsize;
   2491       1.1    itojun 
   2492       1.1    itojun 	if ((cmd->base_masks & (HIFN_BASE_CMD_CRYPT | HIFN_BASE_CMD_DECODE)) ==
   2493       1.1    itojun 	    HIFN_BASE_CMD_CRYPT) {
   2494       1.1    itojun 		for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
   2495       1.1    itojun 			if (crd->crd_alg != CRYPTO_DES_CBC &&
   2496      1.20  jonathan 			    crd->crd_alg != CRYPTO_3DES_CBC &&
   2497      1.20  jonathan 			    crd->crd_alg != CRYPTO_AES_CBC)
   2498       1.1    itojun 				continue;
   2499      1.20  jonathan 			ivlen = ((crd->crd_alg == CRYPTO_AES_CBC) ?
   2500      1.20  jonathan 				HIFN_AES_IV_LENGTH : HIFN_IV_LENGTH);
   2501      1.15  jonathan 			if (crp->crp_flags & CRYPTO_F_IMBUF)
   2502      1.15  jonathan 				m_copydata((struct mbuf *)crp->crp_buf,
   2503      1.20  jonathan 				    crd->crd_skip + crd->crd_len - ivlen,
   2504      1.20  jonathan 				    ivlen,
   2505      1.15  jonathan 				    cmd->softc->sc_sessions[cmd->session_num].hs_iv);
   2506      1.15  jonathan 			else if (crp->crp_flags & CRYPTO_F_IOV) {
   2507      1.15  jonathan 				cuio_copydata((struct uio *)crp->crp_buf,
   2508      1.20  jonathan 				    crd->crd_skip + crd->crd_len - ivlen,
   2509      1.20  jonathan 				    ivlen,
   2510      1.15  jonathan 				    cmd->softc->sc_sessions[cmd->session_num].hs_iv);
   2511      1.15  jonathan 			}
   2512      1.15  jonathan 			/* XXX We do not handle contig data */
   2513       1.1    itojun 			break;
   2514       1.1    itojun 		}
   2515       1.1    itojun 	}
   2516       1.1    itojun 
   2517      1.15  jonathan 	if (cmd->base_masks & HIFN_BASE_CMD_MAC) {
   2518      1.15  jonathan 		u_int8_t *macbuf;
   2519      1.15  jonathan 
   2520      1.15  jonathan 		macbuf = resbuf + sizeof(struct hifn_base_result);
   2521      1.15  jonathan 		if (cmd->base_masks & HIFN_BASE_CMD_COMP)
   2522      1.15  jonathan 			macbuf += sizeof(struct hifn_comp_result);
   2523      1.15  jonathan 		macbuf += sizeof(struct hifn_mac_result);
   2524      1.15  jonathan 
   2525       1.1    itojun 		for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
   2526      1.15  jonathan 			int len;
   2527      1.15  jonathan 
   2528      1.15  jonathan 			if (crd->crd_alg == CRYPTO_MD5)
   2529      1.15  jonathan 				len = 16;
   2530      1.15  jonathan 			else if (crd->crd_alg == CRYPTO_SHA1)
   2531      1.15  jonathan 				len = 20;
   2532      1.15  jonathan 			else if (crd->crd_alg == CRYPTO_MD5_HMAC ||
   2533      1.15  jonathan 			    crd->crd_alg == CRYPTO_SHA1_HMAC)
   2534      1.15  jonathan 				len = 12;
   2535      1.15  jonathan 			else
   2536       1.1    itojun 				continue;
   2537      1.15  jonathan 
   2538      1.15  jonathan 			if (crp->crp_flags & CRYPTO_F_IMBUF)
   2539      1.15  jonathan 				m_copyback((struct mbuf *)crp->crp_buf,
   2540      1.15  jonathan 				    crd->crd_inject, len, macbuf);
   2541      1.15  jonathan 			else if ((crp->crp_flags & CRYPTO_F_IOV) && crp->crp_mac)
   2542      1.15  jonathan 				bcopy((caddr_t)macbuf, crp->crp_mac, len);
   2543      1.15  jonathan 			break;
   2544      1.15  jonathan 		}
   2545      1.15  jonathan 	}
   2546      1.15  jonathan 
   2547      1.15  jonathan 	if (cmd->src_map != cmd->dst_map) {
   2548      1.15  jonathan 		bus_dmamap_unload(sc->sc_dmat, cmd->dst_map);
   2549      1.15  jonathan 		bus_dmamap_destroy(sc->sc_dmat, cmd->dst_map);
   2550      1.15  jonathan 	}
   2551      1.15  jonathan 	bus_dmamap_unload(sc->sc_dmat, cmd->src_map);
   2552      1.15  jonathan 	bus_dmamap_destroy(sc->sc_dmat, cmd->src_map);
   2553      1.15  jonathan 	free(cmd, M_DEVBUF);
   2554      1.15  jonathan 	crypto_done(crp);
   2555      1.15  jonathan }
   2556      1.15  jonathan 
   2557      1.15  jonathan #ifdef HAVE_CRYPTO_LSZ
   2558      1.15  jonathan 
   2559      1.15  jonathan int
   2560      1.15  jonathan hifn_compression(struct hifn_softc *sc, struct cryptop *crp,
   2561      1.15  jonathan     struct hifn_command *cmd)
   2562      1.15  jonathan {
   2563      1.15  jonathan 	struct cryptodesc *crd = crp->crp_desc;
   2564      1.15  jonathan 	int s, err = 0;
   2565      1.15  jonathan 
   2566      1.15  jonathan 	cmd->compcrd = crd;
   2567      1.15  jonathan 	cmd->base_masks |= HIFN_BASE_CMD_COMP;
   2568      1.15  jonathan 
   2569      1.15  jonathan 	if ((crp->crp_flags & CRYPTO_F_IMBUF) == 0) {
   2570      1.15  jonathan 		/*
   2571      1.15  jonathan 		 * XXX can only handle mbufs right now since we can
   2572      1.15  jonathan 		 * XXX dynamically resize them.
   2573      1.15  jonathan 		 */
   2574      1.15  jonathan 		err = EINVAL;
   2575      1.15  jonathan 		return (ENOMEM);
   2576      1.15  jonathan 	}
   2577      1.15  jonathan 
   2578      1.15  jonathan 	if ((crd->crd_flags & CRD_F_COMP) == 0)
   2579      1.15  jonathan 		cmd->base_masks |= HIFN_BASE_CMD_DECODE;
   2580      1.15  jonathan 	if (crd->crd_alg == CRYPTO_LZS_COMP)
   2581      1.15  jonathan 		cmd->comp_masks |= HIFN_COMP_CMD_ALG_LZS |
   2582      1.15  jonathan 		    HIFN_COMP_CMD_CLEARHIST;
   2583      1.15  jonathan 
   2584      1.15  jonathan 	if (bus_dmamap_create(sc->sc_dmat, HIFN_MAX_DMALEN, MAX_SCATTER,
   2585      1.15  jonathan 	    HIFN_MAX_SEGLEN, 0, BUS_DMA_NOWAIT, &cmd->src_map)) {
   2586      1.15  jonathan 		err = ENOMEM;
   2587      1.15  jonathan 		goto fail;
   2588      1.15  jonathan 	}
   2589      1.15  jonathan 
   2590      1.15  jonathan 	if (bus_dmamap_create(sc->sc_dmat, HIFN_MAX_DMALEN, MAX_SCATTER,
   2591      1.15  jonathan 	    HIFN_MAX_SEGLEN, 0, BUS_DMA_NOWAIT, &cmd->dst_map)) {
   2592      1.15  jonathan 		err = ENOMEM;
   2593      1.15  jonathan 		goto fail;
   2594      1.15  jonathan 	}
   2595      1.15  jonathan 
   2596      1.15  jonathan 	if (crp->crp_flags & CRYPTO_F_IMBUF) {
   2597      1.15  jonathan 		int len;
   2598      1.15  jonathan 
   2599      1.15  jonathan 		if (bus_dmamap_load_mbuf(sc->sc_dmat, cmd->src_map,
   2600      1.15  jonathan 		    cmd->srcu.src_m, BUS_DMA_NOWAIT)) {
   2601      1.15  jonathan 			err = ENOMEM;
   2602      1.15  jonathan 			goto fail;
   2603      1.15  jonathan 		}
   2604      1.15  jonathan 
   2605      1.15  jonathan 		len = cmd->src_map->dm_mapsize / MCLBYTES;
   2606      1.15  jonathan 		if ((cmd->src_map->dm_mapsize % MCLBYTES) != 0)
   2607      1.15  jonathan 			len++;
   2608      1.15  jonathan 		len *= MCLBYTES;
   2609      1.15  jonathan 
   2610      1.15  jonathan 		if ((crd->crd_flags & CRD_F_COMP) == 0)
   2611      1.15  jonathan 			len *= 4;
   2612      1.15  jonathan 
   2613      1.15  jonathan 		if (len > HIFN_MAX_DMALEN)
   2614      1.15  jonathan 			len = HIFN_MAX_DMALEN;
   2615      1.15  jonathan 
   2616      1.15  jonathan 		cmd->dstu.dst_m = hifn_mkmbuf_chain(len, cmd->srcu.src_m);
   2617      1.15  jonathan 		if (cmd->dstu.dst_m == NULL) {
   2618      1.15  jonathan 			err = ENOMEM;
   2619      1.15  jonathan 			goto fail;
   2620      1.15  jonathan 		}
   2621      1.15  jonathan 
   2622      1.15  jonathan 		if (bus_dmamap_load_mbuf(sc->sc_dmat, cmd->dst_map,
   2623      1.15  jonathan 		    cmd->dstu.dst_m, BUS_DMA_NOWAIT)) {
   2624      1.15  jonathan 			err = ENOMEM;
   2625      1.15  jonathan 			goto fail;
   2626      1.15  jonathan 		}
   2627      1.15  jonathan 	} else if (crp->crp_flags & CRYPTO_F_IOV) {
   2628      1.15  jonathan 		if (bus_dmamap_load_uio(sc->sc_dmat, cmd->src_map,
   2629      1.15  jonathan 		    cmd->srcu.src_io, BUS_DMA_NOWAIT)) {
   2630      1.15  jonathan 			err = ENOMEM;
   2631      1.15  jonathan 			goto fail;
   2632      1.15  jonathan 		}
   2633      1.15  jonathan 		if (bus_dmamap_load_uio(sc->sc_dmat, cmd->dst_map,
   2634      1.15  jonathan 		    cmd->dstu.dst_io, BUS_DMA_NOWAIT)) {
   2635      1.15  jonathan 			err = ENOMEM;
   2636      1.15  jonathan 			goto fail;
   2637      1.15  jonathan 		}
   2638      1.15  jonathan 	}
   2639      1.15  jonathan 
   2640      1.15  jonathan 	if (cmd->src_map == cmd->dst_map)
   2641      1.15  jonathan 		bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
   2642      1.15  jonathan 		    0, cmd->src_map->dm_mapsize,
   2643      1.15  jonathan 		    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
   2644      1.15  jonathan 	else {
   2645      1.15  jonathan 		bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
   2646      1.15  jonathan 		    0, cmd->src_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   2647      1.15  jonathan 		bus_dmamap_sync(sc->sc_dmat, cmd->dst_map,
   2648      1.15  jonathan 		    0, cmd->dst_map->dm_mapsize, BUS_DMASYNC_PREREAD);
   2649      1.15  jonathan 	}
   2650      1.15  jonathan 
   2651      1.15  jonathan 	cmd->crp = crp;
   2652      1.15  jonathan 	/*
   2653      1.15  jonathan 	 * Always use session 0.  The modes of compression we use are
   2654      1.15  jonathan 	 * stateless and there is always at least one compression
   2655      1.15  jonathan 	 * context, zero.
   2656      1.15  jonathan 	 */
   2657      1.15  jonathan 	cmd->session_num = 0;
   2658      1.15  jonathan 	cmd->softc = sc;
   2659      1.15  jonathan 
   2660      1.15  jonathan 	s = splnet();
   2661      1.15  jonathan 	err = hifn_compress_enter(sc, cmd);
   2662      1.15  jonathan 	splx(s);
   2663      1.15  jonathan 
   2664      1.15  jonathan 	if (err != 0)
   2665      1.15  jonathan 		goto fail;
   2666      1.15  jonathan 	return (0);
   2667      1.15  jonathan 
   2668      1.15  jonathan fail:
   2669      1.15  jonathan 	if (cmd->dst_map != NULL) {
   2670      1.15  jonathan 		if (cmd->dst_map->dm_nsegs > 0)
   2671      1.15  jonathan 			bus_dmamap_unload(sc->sc_dmat, cmd->dst_map);
   2672      1.15  jonathan 		bus_dmamap_destroy(sc->sc_dmat, cmd->dst_map);
   2673      1.15  jonathan 	}
   2674      1.15  jonathan 	if (cmd->src_map != NULL) {
   2675      1.15  jonathan 		if (cmd->src_map->dm_nsegs > 0)
   2676      1.15  jonathan 			bus_dmamap_unload(sc->sc_dmat, cmd->src_map);
   2677      1.15  jonathan 		bus_dmamap_destroy(sc->sc_dmat, cmd->src_map);
   2678      1.15  jonathan 	}
   2679      1.15  jonathan 	free(cmd, M_DEVBUF);
   2680      1.15  jonathan 	if (err == EINVAL)
   2681      1.15  jonathan 		hifnstats.hst_invalid++;
   2682      1.15  jonathan 	else
   2683      1.15  jonathan 		hifnstats.hst_nomem++;
   2684      1.15  jonathan 	crp->crp_etype = err;
   2685      1.15  jonathan 	crypto_done(crp);
   2686      1.15  jonathan 	return (0);
   2687      1.15  jonathan }
   2688      1.15  jonathan 
   2689      1.15  jonathan /*
   2690      1.15  jonathan  * must be called at splnet()
   2691      1.15  jonathan  */
   2692      1.15  jonathan int
   2693      1.15  jonathan hifn_compress_enter(struct hifn_softc *sc, struct hifn_command *cmd)
   2694      1.15  jonathan {
   2695      1.15  jonathan 	struct hifn_dma *dma = sc->sc_dma;
   2696      1.15  jonathan 	int cmdi, resi;
   2697      1.15  jonathan 	u_int32_t cmdlen;
   2698      1.15  jonathan 
   2699      1.15  jonathan 	if ((dma->cmdu + 1) > HIFN_D_CMD_RSIZE ||
   2700      1.15  jonathan 	    (dma->resu + 1) > HIFN_D_CMD_RSIZE)
   2701      1.15  jonathan 		return (ENOMEM);
   2702      1.15  jonathan 
   2703      1.15  jonathan 	if ((dma->srcu + cmd->src_map->dm_nsegs) > HIFN_D_SRC_RSIZE ||
   2704      1.15  jonathan 	    (dma->dstu + cmd->dst_map->dm_nsegs) > HIFN_D_DST_RSIZE)
   2705      1.15  jonathan 		return (ENOMEM);
   2706      1.15  jonathan 
   2707      1.15  jonathan 	if (dma->cmdi == HIFN_D_CMD_RSIZE) {
   2708      1.15  jonathan 		dma->cmdi = 0;
   2709      1.15  jonathan 		dma->cmdr[HIFN_D_CMD_RSIZE].l = htole32(HIFN_D_VALID |
   2710      1.15  jonathan 		    HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
   2711      1.15  jonathan 		HIFN_CMDR_SYNC(sc, HIFN_D_CMD_RSIZE,
   2712      1.15  jonathan 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2713      1.15  jonathan 	}
   2714      1.15  jonathan 	cmdi = dma->cmdi++;
   2715      1.15  jonathan 	cmdlen = hifn_write_command(cmd, dma->command_bufs[cmdi]);
   2716      1.15  jonathan 	HIFN_CMD_SYNC(sc, cmdi, BUS_DMASYNC_PREWRITE);
   2717      1.15  jonathan 
   2718      1.15  jonathan 	/* .p for command/result already set */
   2719      1.15  jonathan 	dma->cmdr[cmdi].l = htole32(cmdlen | HIFN_D_VALID | HIFN_D_LAST |
   2720      1.15  jonathan 	    HIFN_D_MASKDONEIRQ);
   2721      1.15  jonathan 	HIFN_CMDR_SYNC(sc, cmdi,
   2722      1.15  jonathan 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2723      1.15  jonathan 	dma->cmdu++;
   2724      1.15  jonathan 	if (sc->sc_c_busy == 0) {
   2725      1.15  jonathan 		WRITE_REG_1(sc, HIFN_1_DMA_CSR, HIFN_DMACSR_C_CTRL_ENA);
   2726      1.15  jonathan 		sc->sc_c_busy = 1;
   2727      1.15  jonathan 		SET_LED(sc, HIFN_MIPSRST_LED0);
   2728      1.15  jonathan 	}
   2729      1.15  jonathan 
   2730      1.15  jonathan 	/*
   2731      1.15  jonathan 	 * We don't worry about missing an interrupt (which a "command wait"
   2732      1.15  jonathan 	 * interrupt salvages us from), unless there is more than one command
   2733      1.15  jonathan 	 * in the queue.
   2734      1.15  jonathan 	 */
   2735      1.15  jonathan 	if (dma->cmdu > 1) {
   2736      1.15  jonathan 		sc->sc_dmaier |= HIFN_DMAIER_C_WAIT;
   2737      1.15  jonathan 		WRITE_REG_1(sc, HIFN_1_DMA_IER, sc->sc_dmaier);
   2738      1.15  jonathan 	}
   2739      1.15  jonathan 
   2740      1.15  jonathan 	hifnstats.hst_ipackets++;
   2741      1.15  jonathan 	hifnstats.hst_ibytes += cmd->src_map->dm_mapsize;
   2742      1.15  jonathan 
   2743      1.15  jonathan 	hifn_dmamap_load_src(sc, cmd);
   2744      1.15  jonathan 	if (sc->sc_s_busy == 0) {
   2745      1.15  jonathan 		WRITE_REG_1(sc, HIFN_1_DMA_CSR, HIFN_DMACSR_S_CTRL_ENA);
   2746      1.15  jonathan 		sc->sc_s_busy = 1;
   2747      1.15  jonathan 		SET_LED(sc, HIFN_MIPSRST_LED1);
   2748      1.15  jonathan 	}
   2749      1.15  jonathan 
   2750      1.15  jonathan 	/*
   2751      1.15  jonathan 	 * Unlike other descriptors, we don't mask done interrupt from
   2752      1.15  jonathan 	 * result descriptor.
   2753      1.15  jonathan 	 */
   2754      1.15  jonathan 	if (dma->resi == HIFN_D_RES_RSIZE) {
   2755      1.15  jonathan 		dma->resi = 0;
   2756      1.15  jonathan 		dma->resr[HIFN_D_RES_RSIZE].l = htole32(HIFN_D_VALID |
   2757      1.15  jonathan 		    HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
   2758      1.15  jonathan 		HIFN_RESR_SYNC(sc, HIFN_D_RES_RSIZE,
   2759      1.15  jonathan 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   2760      1.15  jonathan 	}
   2761      1.15  jonathan 	resi = dma->resi++;
   2762      1.15  jonathan 	dma->hifn_commands[resi] = cmd;
   2763      1.15  jonathan 	HIFN_RES_SYNC(sc, resi, BUS_DMASYNC_PREREAD);
   2764      1.15  jonathan 	dma->resr[resi].l = htole32(HIFN_MAX_RESULT |
   2765      1.15  jonathan 	    HIFN_D_VALID | HIFN_D_LAST);
   2766      1.15  jonathan 	HIFN_RESR_SYNC(sc, resi,
   2767      1.15  jonathan 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   2768      1.15  jonathan 	dma->resu++;
   2769      1.15  jonathan 	if (sc->sc_r_busy == 0) {
   2770      1.15  jonathan 		WRITE_REG_1(sc, HIFN_1_DMA_CSR, HIFN_DMACSR_R_CTRL_ENA);
   2771      1.15  jonathan 		sc->sc_r_busy = 1;
   2772      1.15  jonathan 		SET_LED(sc, HIFN_MIPSRST_LED2);
   2773      1.15  jonathan 	}
   2774      1.15  jonathan 
   2775      1.15  jonathan 	if (cmd->sloplen)
   2776      1.15  jonathan 		cmd->slopidx = resi;
   2777      1.15  jonathan 
   2778      1.15  jonathan 	hifn_dmamap_load_dst(sc, cmd);
   2779      1.15  jonathan 
   2780      1.15  jonathan 	if (sc->sc_d_busy == 0) {
   2781      1.15  jonathan 		WRITE_REG_1(sc, HIFN_1_DMA_CSR, HIFN_DMACSR_D_CTRL_ENA);
   2782      1.15  jonathan 		sc->sc_d_busy = 1;
   2783      1.15  jonathan 	}
   2784      1.15  jonathan 	sc->sc_active = 5;
   2785      1.15  jonathan 	cmd->cmd_callback = hifn_callback_comp;
   2786      1.15  jonathan 	return (0);
   2787      1.15  jonathan }
   2788      1.15  jonathan 
   2789      1.15  jonathan void
   2790      1.15  jonathan hifn_callback_comp(struct hifn_softc *sc, struct hifn_command *cmd,
   2791      1.15  jonathan     u_int8_t *resbuf)
   2792      1.15  jonathan {
   2793      1.15  jonathan 	struct hifn_base_result baseres;
   2794      1.15  jonathan 	struct cryptop *crp = cmd->crp;
   2795      1.15  jonathan 	struct hifn_dma *dma = sc->sc_dma;
   2796      1.15  jonathan 	struct mbuf *m;
   2797      1.15  jonathan 	int err = 0, i, u;
   2798      1.15  jonathan 	u_int32_t olen;
   2799      1.15  jonathan 	bus_size_t dstsize;
   2800      1.15  jonathan 
   2801      1.15  jonathan 	bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
   2802      1.15  jonathan 	    0, cmd->src_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   2803      1.15  jonathan 	bus_dmamap_sync(sc->sc_dmat, cmd->dst_map,
   2804      1.15  jonathan 	    0, cmd->dst_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
   2805      1.15  jonathan 
   2806      1.15  jonathan 	dstsize = cmd->dst_map->dm_mapsize;
   2807      1.15  jonathan 	bus_dmamap_unload(sc->sc_dmat, cmd->dst_map);
   2808      1.15  jonathan 
   2809      1.15  jonathan 	bcopy(resbuf, &baseres, sizeof(struct hifn_base_result));
   2810      1.15  jonathan 
   2811      1.15  jonathan 	i = dma->dstk; u = dma->dstu;
   2812      1.15  jonathan 	while (u != 0) {
   2813      1.15  jonathan 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   2814      1.15  jonathan 		    offsetof(struct hifn_dma, dstr[i]), sizeof(struct hifn_desc),
   2815      1.15  jonathan 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   2816      1.15  jonathan 		if (dma->dstr[i].l & htole32(HIFN_D_VALID)) {
   2817      1.15  jonathan 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   2818      1.15  jonathan 			    offsetof(struct hifn_dma, dstr[i]),
   2819      1.15  jonathan 			    sizeof(struct hifn_desc),
   2820      1.15  jonathan 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   2821       1.1    itojun 			break;
   2822       1.1    itojun 		}
   2823      1.15  jonathan 		if (++i == (HIFN_D_DST_RSIZE + 1))
   2824      1.15  jonathan 			i = 0;
   2825      1.15  jonathan 		else
   2826      1.15  jonathan 			u--;
   2827       1.1    itojun 	}
   2828      1.15  jonathan 	dma->dstk = i; dma->dstu = u;
   2829       1.1    itojun 
   2830      1.15  jonathan 	if (baseres.flags & htole16(HIFN_BASE_RES_DSTOVERRUN)) {
   2831      1.15  jonathan 		bus_size_t xlen;
   2832      1.15  jonathan 
   2833      1.15  jonathan 		xlen = dstsize;
   2834      1.15  jonathan 
   2835      1.15  jonathan 		m_freem(cmd->dstu.dst_m);
   2836      1.15  jonathan 
   2837      1.15  jonathan 		if (xlen == HIFN_MAX_DMALEN) {
   2838      1.15  jonathan 			/* We've done all we can. */
   2839      1.15  jonathan 			err = E2BIG;
   2840      1.15  jonathan 			goto out;
   2841      1.15  jonathan 		}
   2842      1.15  jonathan 
   2843      1.15  jonathan 		xlen += MCLBYTES;
   2844      1.15  jonathan 
   2845      1.15  jonathan 		if (xlen > HIFN_MAX_DMALEN)
   2846      1.15  jonathan 			xlen = HIFN_MAX_DMALEN;
   2847      1.15  jonathan 
   2848      1.15  jonathan 		cmd->dstu.dst_m = hifn_mkmbuf_chain(xlen,
   2849      1.15  jonathan 		    cmd->srcu.src_m);
   2850      1.15  jonathan 		if (cmd->dstu.dst_m == NULL) {
   2851      1.15  jonathan 			err = ENOMEM;
   2852      1.15  jonathan 			goto out;
   2853      1.15  jonathan 		}
   2854      1.15  jonathan 		if (bus_dmamap_load_mbuf(sc->sc_dmat, cmd->dst_map,
   2855      1.15  jonathan 		    cmd->dstu.dst_m, BUS_DMA_NOWAIT)) {
   2856      1.15  jonathan 			err = ENOMEM;
   2857      1.15  jonathan 			goto out;
   2858      1.15  jonathan 		}
   2859      1.15  jonathan 
   2860      1.15  jonathan 		bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
   2861      1.15  jonathan 		    0, cmd->src_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   2862      1.15  jonathan 		bus_dmamap_sync(sc->sc_dmat, cmd->dst_map,
   2863      1.15  jonathan 		    0, cmd->dst_map->dm_mapsize, BUS_DMASYNC_PREREAD);
   2864      1.15  jonathan 
   2865      1.15  jonathan 		/* already at splnet... */
   2866      1.15  jonathan 		err = hifn_compress_enter(sc, cmd);
   2867      1.15  jonathan 		if (err != 0)
   2868      1.15  jonathan 			goto out;
   2869      1.15  jonathan 		return;
   2870      1.15  jonathan 	}
   2871      1.15  jonathan 
   2872      1.15  jonathan 	olen = dstsize - (letoh16(baseres.dst_cnt) |
   2873      1.15  jonathan 	    (((letoh16(baseres.session) & HIFN_BASE_RES_DSTLEN_M) >>
   2874      1.15  jonathan 	    HIFN_BASE_RES_DSTLEN_S) << 16));
   2875      1.15  jonathan 
   2876      1.15  jonathan 	crp->crp_olen = olen - cmd->compcrd->crd_skip;
   2877      1.15  jonathan 
   2878      1.15  jonathan 	bus_dmamap_unload(sc->sc_dmat, cmd->src_map);
   2879      1.15  jonathan 	bus_dmamap_destroy(sc->sc_dmat, cmd->src_map);
   2880      1.15  jonathan 	bus_dmamap_destroy(sc->sc_dmat, cmd->dst_map);
   2881      1.15  jonathan 
   2882      1.15  jonathan 	m = cmd->dstu.dst_m;
   2883      1.15  jonathan 	if (m->m_flags & M_PKTHDR)
   2884      1.15  jonathan 		m->m_pkthdr.len = olen;
   2885      1.15  jonathan 	crp->crp_buf = (caddr_t)m;
   2886      1.15  jonathan 	for (; m != NULL; m = m->m_next) {
   2887      1.15  jonathan 		if (olen >= m->m_len)
   2888      1.15  jonathan 			olen -= m->m_len;
   2889      1.15  jonathan 		else {
   2890      1.15  jonathan 			m->m_len = olen;
   2891      1.15  jonathan 			olen = 0;
   2892      1.15  jonathan 		}
   2893      1.15  jonathan 	}
   2894      1.15  jonathan 
   2895      1.15  jonathan 	m_freem(cmd->srcu.src_m);
   2896       1.1    itojun 	free(cmd, M_DEVBUF);
   2897      1.15  jonathan 	crp->crp_etype = 0;
   2898      1.15  jonathan 	crypto_done(crp);
   2899      1.15  jonathan 	return;
   2900      1.15  jonathan 
   2901      1.15  jonathan out:
   2902      1.15  jonathan 	if (cmd->dst_map != NULL) {
   2903      1.15  jonathan 		if (cmd->src_map->dm_nsegs != 0)
   2904      1.15  jonathan 			bus_dmamap_unload(sc->sc_dmat, cmd->src_map);
   2905      1.15  jonathan 		bus_dmamap_destroy(sc->sc_dmat, cmd->dst_map);
   2906      1.15  jonathan 	}
   2907      1.15  jonathan 	if (cmd->src_map != NULL) {
   2908      1.15  jonathan 		if (cmd->src_map->dm_nsegs != 0)
   2909      1.15  jonathan 			bus_dmamap_unload(sc->sc_dmat, cmd->src_map);
   2910      1.15  jonathan 		bus_dmamap_destroy(sc->sc_dmat, cmd->src_map);
   2911      1.15  jonathan 	}
   2912      1.15  jonathan 	if (cmd->dstu.dst_m != NULL)
   2913      1.15  jonathan 		m_freem(cmd->dstu.dst_m);
   2914      1.15  jonathan 	free(cmd, M_DEVBUF);
   2915      1.15  jonathan 	crp->crp_etype = err;
   2916       1.1    itojun 	crypto_done(crp);
   2917       1.1    itojun }
   2918      1.15  jonathan 
   2919      1.15  jonathan struct mbuf *
   2920      1.15  jonathan hifn_mkmbuf_chain(int totlen, struct mbuf *mtemplate)
   2921      1.15  jonathan {
   2922      1.15  jonathan 	int len;
   2923      1.15  jonathan 	struct mbuf *m, *m0, *mlast;
   2924      1.15  jonathan 
   2925      1.15  jonathan 	if (mtemplate->m_flags & M_PKTHDR) {
   2926      1.15  jonathan 		len = MHLEN;
   2927      1.15  jonathan 		MGETHDR(m0, M_DONTWAIT, MT_DATA);
   2928      1.15  jonathan 	} else {
   2929      1.15  jonathan 		len = MLEN;
   2930      1.15  jonathan 		MGET(m0, M_DONTWAIT, MT_DATA);
   2931      1.15  jonathan 	}
   2932      1.15  jonathan 	if (m0 == NULL)
   2933      1.15  jonathan 		return (NULL);
   2934      1.15  jonathan 	if (len == MHLEN)
   2935      1.15  jonathan 		M_DUP_PKTHDR(m0, mtemplate);
   2936      1.15  jonathan 	MCLGET(m0, M_DONTWAIT);
   2937      1.15  jonathan 	if (!(m0->m_flags & M_EXT))
   2938      1.15  jonathan 		m_freem(m0);
   2939      1.15  jonathan 	len = MCLBYTES;
   2940      1.15  jonathan 
   2941      1.15  jonathan 	totlen -= len;
   2942      1.15  jonathan 	m0->m_pkthdr.len = m0->m_len = len;
   2943      1.15  jonathan 	mlast = m0;
   2944      1.15  jonathan 
   2945      1.15  jonathan 	while (totlen > 0) {
   2946      1.15  jonathan 		MGET(m, M_DONTWAIT, MT_DATA);
   2947      1.15  jonathan 		if (m == NULL) {
   2948      1.15  jonathan 			m_freem(m0);
   2949      1.15  jonathan 			return (NULL);
   2950      1.15  jonathan 		}
   2951      1.15  jonathan 		MCLGET(m, M_DONTWAIT);
   2952      1.15  jonathan 		if (!(m->m_flags & M_EXT)) {
   2953      1.15  jonathan 			m_freem(m0);
   2954      1.15  jonathan 			return (NULL);
   2955      1.15  jonathan 		}
   2956      1.15  jonathan 		len = MCLBYTES;
   2957      1.15  jonathan 		m->m_len = len;
   2958      1.15  jonathan 		if (m0->m_flags & M_PKTHDR)
   2959      1.15  jonathan 			m0->m_pkthdr.len += len;
   2960      1.15  jonathan 		totlen -= len;
   2961      1.15  jonathan 
   2962      1.15  jonathan 		mlast->m_next = m;
   2963      1.15  jonathan 		mlast = m;
   2964      1.15  jonathan 	}
   2965      1.15  jonathan 
   2966      1.15  jonathan 	return (m0);
   2967      1.15  jonathan }
   2968      1.15  jonathan #endif	/* HAVE_CRYPTO_LSZ */
   2969      1.15  jonathan 
   2970      1.15  jonathan void
   2971      1.17   thorpej hifn_write_4(struct hifn_softc *sc, int reggrp, bus_size_t reg, u_int32_t val)
   2972      1.15  jonathan {
   2973      1.15  jonathan 	/*
   2974      1.15  jonathan 	 * 7811 PB3 rev/2 parts lock-up on burst writes to Group 0
   2975      1.15  jonathan 	 * and Group 1 registers; avoid conditions that could create
   2976      1.15  jonathan 	 * burst writes by doing a read in between the writes.
   2977      1.15  jonathan 	 */
   2978      1.15  jonathan 	if (sc->sc_flags & HIFN_NO_BURSTWRITE) {
   2979      1.15  jonathan 		if (sc->sc_waw_lastgroup == reggrp &&
   2980      1.15  jonathan 		    sc->sc_waw_lastreg == reg - 4) {
   2981      1.15  jonathan 			bus_space_read_4(sc->sc_st1, sc->sc_sh1, HIFN_1_REVID);
   2982      1.15  jonathan 		}
   2983      1.15  jonathan 		sc->sc_waw_lastgroup = reggrp;
   2984      1.15  jonathan 		sc->sc_waw_lastreg = reg;
   2985      1.15  jonathan 	}
   2986      1.15  jonathan 	if (reggrp == 0)
   2987      1.15  jonathan 		bus_space_write_4(sc->sc_st0, sc->sc_sh0, reg, val);
   2988      1.15  jonathan 	else
   2989      1.15  jonathan 		bus_space_write_4(sc->sc_st1, sc->sc_sh1, reg, val);
   2990      1.15  jonathan 
   2991      1.15  jonathan }
   2992      1.15  jonathan 
   2993      1.15  jonathan u_int32_t
   2994      1.17   thorpej hifn_read_4(struct hifn_softc *sc, int reggrp, bus_size_t reg)
   2995      1.15  jonathan {
   2996      1.15  jonathan 	if (sc->sc_flags & HIFN_NO_BURSTWRITE) {
   2997      1.15  jonathan 		sc->sc_waw_lastgroup = -1;
   2998      1.15  jonathan 		sc->sc_waw_lastreg = 1;
   2999      1.15  jonathan 	}
   3000      1.15  jonathan 	if (reggrp == 0)
   3001      1.15  jonathan 		return (bus_space_read_4(sc->sc_st0, sc->sc_sh0, reg));
   3002      1.15  jonathan 	return (bus_space_read_4(sc->sc_st1, sc->sc_sh1, reg));
   3003      1.15  jonathan }
   3004