Home | History | Annotate | Line # | Download | only in pci
hifn7751.c revision 1.1
      1  1.1  itojun /*	$NetBSD: hifn7751.c,v 1.1 2000/10/12 02:59:59 itojun Exp $	*/
      2  1.1  itojun /*	$OpenBSD: hifn7751.c,v 1.47 2000/10/11 13:15:41 itojun Exp $	*/
      3  1.1  itojun 
      4  1.1  itojun /*
      5  1.1  itojun  * Invertex AEON / Hi/fn 7751 driver
      6  1.1  itojun  * Copyright (c) 1999 Invertex Inc. All rights reserved.
      7  1.1  itojun  * Copyright (c) 1999 Theo de Raadt
      8  1.1  itojun  * Copyright (c) 2000 Network Security Technologies, Inc.
      9  1.1  itojun  *			http://www.netsec.net
     10  1.1  itojun  *
     11  1.1  itojun  * This driver is based on a previous driver by Invertex, for which they
     12  1.1  itojun  * requested:  Please send any comments, feedback, bug-fixes, or feature
     13  1.1  itojun  * requests to software (at) invertex.com.
     14  1.1  itojun  *
     15  1.1  itojun  * Redistribution and use in source and binary forms, with or without
     16  1.1  itojun  * modification, are permitted provided that the following conditions
     17  1.1  itojun  * are met:
     18  1.1  itojun  *
     19  1.1  itojun  * 1. Redistributions of source code must retain the above copyright
     20  1.1  itojun  *   notice, this list of conditions and the following disclaimer.
     21  1.1  itojun  * 2. Redistributions in binary form must reproduce the above copyright
     22  1.1  itojun  *   notice, this list of conditions and the following disclaimer in the
     23  1.1  itojun  *   documentation and/or other materials provided with the distribution.
     24  1.1  itojun  * 3. The name of the author may not be used to endorse or promote products
     25  1.1  itojun  *   derived from this software without specific prior written permission.
     26  1.1  itojun  *
     27  1.1  itojun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     28  1.1  itojun  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     29  1.1  itojun  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     30  1.1  itojun  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     31  1.1  itojun  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     32  1.1  itojun  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     33  1.1  itojun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     34  1.1  itojun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     35  1.1  itojun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     36  1.1  itojun  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     37  1.1  itojun  */
     38  1.1  itojun 
     39  1.1  itojun /*
     40  1.1  itojun  * Driver for the Hi/Fn 7751 encryption processor.
     41  1.1  itojun  */
     42  1.1  itojun 
     43  1.1  itojun #include <sys/param.h>
     44  1.1  itojun #include <sys/systm.h>
     45  1.1  itojun #include <sys/proc.h>
     46  1.1  itojun #include <sys/errno.h>
     47  1.1  itojun #include <sys/malloc.h>
     48  1.1  itojun #include <sys/kernel.h>
     49  1.1  itojun #include <sys/mbuf.h>
     50  1.1  itojun #ifdef __OpenBSD__
     51  1.1  itojun #include <vm/vm.h>
     52  1.1  itojun #include <vm/vm_extern.h>
     53  1.1  itojun #include <vm/pmap.h>
     54  1.1  itojun #else
     55  1.1  itojun #include <uvm/uvm.h>
     56  1.1  itojun #include <uvm/uvm_extern.h>
     57  1.1  itojun #include <uvm/uvm_pmap.h>
     58  1.1  itojun #endif
     59  1.1  itojun #include <machine/pmap.h>
     60  1.1  itojun #include <sys/device.h>
     61  1.1  itojun 
     62  1.1  itojun #ifdef __OpenBSD__
     63  1.1  itojun #include <crypto/crypto.h>
     64  1.1  itojun #include <dev/rndvar.h>
     65  1.1  itojun #endif
     66  1.1  itojun 
     67  1.1  itojun #include <dev/pci/pcireg.h>
     68  1.1  itojun #include <dev/pci/pcivar.h>
     69  1.1  itojun #include <dev/pci/pcidevs.h>
     70  1.1  itojun 
     71  1.1  itojun #include <dev/pci/hifn7751var.h>
     72  1.1  itojun #include <dev/pci/hifn7751reg.h>
     73  1.1  itojun 
     74  1.1  itojun #undef HIFN_DEBUG
     75  1.1  itojun 
     76  1.1  itojun /*
     77  1.1  itojun  * Prototypes and count for the pci_device structure
     78  1.1  itojun  */
     79  1.1  itojun #ifdef __OpenBSD__
     80  1.1  itojun int hifn_probe		__P((struct device *, void *, void *));
     81  1.1  itojun #else
     82  1.1  itojun int hifn_probe		__P((struct device *, struct cfdata *, void *));
     83  1.1  itojun #endif
     84  1.1  itojun void hifn_attach	__P((struct device *, struct device *, void *));
     85  1.1  itojun 
     86  1.1  itojun struct cfattach hifn_ca = {
     87  1.1  itojun 	sizeof(struct hifn_softc), hifn_probe, hifn_attach,
     88  1.1  itojun };
     89  1.1  itojun 
     90  1.1  itojun #ifdef __OpenBSD__
     91  1.1  itojun struct cfdriver hifn_cd = {
     92  1.1  itojun 	0, "hifn", DV_DULL
     93  1.1  itojun };
     94  1.1  itojun #endif
     95  1.1  itojun 
     96  1.1  itojun void	hifn_reset_board __P((struct hifn_softc *));
     97  1.1  itojun int	hifn_enable_crypto __P((struct hifn_softc *, pcireg_t));
     98  1.1  itojun void	hifn_init_dma __P((struct hifn_softc *));
     99  1.1  itojun void	hifn_init_pci_registers __P((struct hifn_softc *));
    100  1.1  itojun int	hifn_sramsize __P((struct hifn_softc *));
    101  1.1  itojun int	hifn_dramsize __P((struct hifn_softc *));
    102  1.1  itojun void	hifn_ramtype __P((struct hifn_softc *));
    103  1.1  itojun void	hifn_sessions __P((struct hifn_softc *));
    104  1.1  itojun int	hifn_intr __P((void *));
    105  1.1  itojun u_int	hifn_write_command __P((struct hifn_command *, u_int8_t *));
    106  1.1  itojun u_int32_t hifn_next_signature __P((u_int32_t a, u_int cnt));
    107  1.1  itojun #ifdef __OpenBSD__
    108  1.1  itojun int	hifn_newsession __P((u_int32_t *, struct cryptoini *));
    109  1.1  itojun int	hifn_freesession __P((u_int64_t));
    110  1.1  itojun int	hifn_process __P((struct cryptop *));
    111  1.1  itojun void	hifn_callback __P((struct hifn_softc *, struct hifn_command *, u_int8_t *));
    112  1.1  itojun #endif
    113  1.1  itojun int	hifn_crypto __P((struct hifn_softc *, hifn_command_t *));
    114  1.1  itojun int	hifn_readramaddr __P((struct hifn_softc *, int, u_int8_t *, int));
    115  1.1  itojun int	hifn_writeramaddr __P((struct hifn_softc *, int, u_int8_t *, int));
    116  1.1  itojun 
    117  1.1  itojun struct hifn_stats {
    118  1.1  itojun 	u_int64_t hst_ibytes;
    119  1.1  itojun 	u_int64_t hst_obytes;
    120  1.1  itojun 	u_int32_t hst_ipackets;
    121  1.1  itojun 	u_int32_t hst_opackets;
    122  1.1  itojun 	u_int32_t hst_invalid;
    123  1.1  itojun 	u_int32_t hst_nomem;
    124  1.1  itojun } hifnstats;
    125  1.1  itojun 
    126  1.1  itojun int
    127  1.1  itojun hifn_probe(parent, match, aux)
    128  1.1  itojun 	struct device *parent;
    129  1.1  itojun #ifdef __OpenBSD__
    130  1.1  itojun 	void *match;
    131  1.1  itojun #else
    132  1.1  itojun 	struct cfdata *match;
    133  1.1  itojun #endif
    134  1.1  itojun 	void *aux;
    135  1.1  itojun {
    136  1.1  itojun 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
    137  1.1  itojun 
    138  1.1  itojun 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INVERTEX &&
    139  1.1  itojun 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INVERTEX_AEON)
    140  1.1  itojun 		return (1);
    141  1.1  itojun 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_HIFN &&
    142  1.1  itojun 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_HIFN_7751)
    143  1.1  itojun 		return (1);
    144  1.1  itojun 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NETSEC &&
    145  1.1  itojun 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NETSEC_7751)
    146  1.1  itojun 		return (1);
    147  1.1  itojun 	return (0);
    148  1.1  itojun }
    149  1.1  itojun 
    150  1.1  itojun void
    151  1.1  itojun hifn_attach(parent, self, aux)
    152  1.1  itojun 	struct device *parent, *self;
    153  1.1  itojun 	void *aux;
    154  1.1  itojun {
    155  1.1  itojun 	struct hifn_softc *sc = (struct hifn_softc *)self;
    156  1.1  itojun 	struct pci_attach_args *pa = aux;
    157  1.1  itojun 	pci_chipset_tag_t pc = pa->pa_pc;
    158  1.1  itojun 	pci_intr_handle_t ih;
    159  1.1  itojun 	const char *intrstr = NULL;
    160  1.1  itojun 	char rbase;
    161  1.1  itojun 	bus_size_t iosize0, iosize1;
    162  1.1  itojun 	u_int32_t cmd;
    163  1.1  itojun 	u_int16_t ena;
    164  1.1  itojun 	bus_dma_segment_t seg;
    165  1.1  itojun 	bus_dmamap_t dmamap;
    166  1.1  itojun 	int rseg;
    167  1.1  itojun 	caddr_t kva;
    168  1.1  itojun 
    169  1.1  itojun 	cmd = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    170  1.1  itojun 	cmd |= PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE;
    171  1.1  itojun 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, cmd);
    172  1.1  itojun 	cmd = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    173  1.1  itojun 
    174  1.1  itojun 	if (!(cmd & PCI_COMMAND_MEM_ENABLE)) {
    175  1.1  itojun 		printf(": failed to enable memory mapping\n");
    176  1.1  itojun 		return;
    177  1.1  itojun 	}
    178  1.1  itojun 
    179  1.1  itojun 	if (pci_mapreg_map(pa, HIFN_BAR0, PCI_MAPREG_TYPE_MEM, 0,
    180  1.1  itojun 	    &sc->sc_st0, &sc->sc_sh0, NULL, &iosize0)) {
    181  1.1  itojun 		printf(": can't find mem space %d\n", 0);
    182  1.1  itojun 		return;
    183  1.1  itojun 	}
    184  1.1  itojun 
    185  1.1  itojun 	if (pci_mapreg_map(pa, HIFN_BAR1, PCI_MAPREG_TYPE_MEM, 0,
    186  1.1  itojun 	    &sc->sc_st1, &sc->sc_sh1, NULL, &iosize1)) {
    187  1.1  itojun 		printf(": can't find mem space %d\n", 1);
    188  1.1  itojun 		goto fail_io0;
    189  1.1  itojun 	}
    190  1.1  itojun 
    191  1.1  itojun 	sc->sc_dmat = pa->pa_dmat;
    192  1.1  itojun 	if (bus_dmamem_alloc(sc->sc_dmat, sizeof(*sc->sc_dma), PAGE_SIZE, 0,
    193  1.1  itojun 	    &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
    194  1.1  itojun 		printf(": can't alloc dma buffer\n");
    195  1.1  itojun 		goto fail_io1;
    196  1.1  itojun         }
    197  1.1  itojun 	if (bus_dmamem_map(sc->sc_dmat, &seg, rseg, sizeof(*sc->sc_dma), &kva,
    198  1.1  itojun 	    BUS_DMA_NOWAIT)) {
    199  1.1  itojun 		printf(": can't map dma buffers (%lu bytes)\n",
    200  1.1  itojun 		    (u_long)sizeof(*sc->sc_dma));
    201  1.1  itojun 		bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    202  1.1  itojun 		goto fail_io1;
    203  1.1  itojun 	}
    204  1.1  itojun 	if (bus_dmamap_create(sc->sc_dmat, sizeof(*sc->sc_dma), 1,
    205  1.1  itojun 	    sizeof(*sc->sc_dma), 0, BUS_DMA_NOWAIT, &dmamap)) {
    206  1.1  itojun 		printf(": can't create dma map\n");
    207  1.1  itojun 		bus_dmamem_unmap(sc->sc_dmat, kva, sizeof(*sc->sc_dma));
    208  1.1  itojun 		bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    209  1.1  itojun 		goto fail_io1;
    210  1.1  itojun 	}
    211  1.1  itojun 	if (bus_dmamap_load(sc->sc_dmat, dmamap, kva, sizeof(*sc->sc_dma),
    212  1.1  itojun 	    NULL, BUS_DMA_NOWAIT)) {
    213  1.1  itojun 		printf(": can't load dma map\n");
    214  1.1  itojun 		bus_dmamap_destroy(sc->sc_dmat, dmamap);
    215  1.1  itojun 		bus_dmamem_unmap(sc->sc_dmat, kva, sizeof(*sc->sc_dma));
    216  1.1  itojun 		bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    217  1.1  itojun 		goto fail_io1;
    218  1.1  itojun 	}
    219  1.1  itojun 	sc->sc_dma = (struct hifn_dma *)kva;
    220  1.1  itojun 	bzero(sc->sc_dma, sizeof(*sc->sc_dma));
    221  1.1  itojun 
    222  1.1  itojun 	hifn_reset_board(sc);
    223  1.1  itojun 
    224  1.1  itojun 	if (hifn_enable_crypto(sc, pa->pa_id) != 0) {
    225  1.1  itojun 		printf("%s: crypto enabling failed\n", sc->sc_dv.dv_xname);
    226  1.1  itojun 		goto fail_mem;
    227  1.1  itojun 	}
    228  1.1  itojun 
    229  1.1  itojun 	hifn_init_dma(sc);
    230  1.1  itojun 	hifn_init_pci_registers(sc);
    231  1.1  itojun 
    232  1.1  itojun 	hifn_ramtype(sc);
    233  1.1  itojun 
    234  1.1  itojun 	if (sc->sc_drammodel == 0)
    235  1.1  itojun 		hifn_sramsize(sc);
    236  1.1  itojun 	else
    237  1.1  itojun 		hifn_dramsize(sc);
    238  1.1  itojun 
    239  1.1  itojun 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NETSEC &&
    240  1.1  itojun 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NETSEC_7751 &&
    241  1.1  itojun 	    PCI_REVISION(pa->pa_class) == 0x61)
    242  1.1  itojun 		sc->sc_ramsize >>= 1;
    243  1.1  itojun 
    244  1.1  itojun 	/*
    245  1.1  itojun 	 * Reinitialize again, since the DRAM/SRAM detection shifted our ring
    246  1.1  itojun 	 * pointers and may have changed the value we send to the RAM Config
    247  1.1  itojun 	 * Register.
    248  1.1  itojun 	 */
    249  1.1  itojun 	hifn_reset_board(sc);
    250  1.1  itojun 	hifn_init_dma(sc);
    251  1.1  itojun 	hifn_init_pci_registers(sc);
    252  1.1  itojun 
    253  1.1  itojun 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
    254  1.1  itojun 	    pa->pa_intrline, &ih)) {
    255  1.1  itojun 		printf(": couldn't map interrupt\n");
    256  1.1  itojun 		goto fail_mem;
    257  1.1  itojun 	}
    258  1.1  itojun 	intrstr = pci_intr_string(pc, ih);
    259  1.1  itojun #ifdef __OpenBSD__
    260  1.1  itojun 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, hifn_intr, sc,
    261  1.1  itojun 	    self->dv_xname);
    262  1.1  itojun #else
    263  1.1  itojun 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, hifn_intr, sc);
    264  1.1  itojun #endif
    265  1.1  itojun 	if (sc->sc_ih == NULL) {
    266  1.1  itojun 		printf(": couldn't establish interrupt\n");
    267  1.1  itojun 		if (intrstr != NULL)
    268  1.1  itojun 			printf(" at %s", intrstr);
    269  1.1  itojun 		printf("\n");
    270  1.1  itojun 		goto fail_mem;
    271  1.1  itojun 	}
    272  1.1  itojun 
    273  1.1  itojun 	hifn_sessions(sc);
    274  1.1  itojun 
    275  1.1  itojun 	rseg = sc->sc_ramsize / 1024;
    276  1.1  itojun 	rbase = 'K';
    277  1.1  itojun 	if (sc->sc_ramsize >= (1024 * 1024)) {
    278  1.1  itojun 		rbase = 'M';
    279  1.1  itojun 		rseg /= 1024;
    280  1.1  itojun 	}
    281  1.1  itojun 	printf(", %d%cB %cram, %s\n", rseg, rbase,
    282  1.1  itojun 	    sc->sc_drammodel ? 'd' : 's', intrstr);
    283  1.1  itojun 
    284  1.1  itojun #ifdef __OpenBSD__
    285  1.1  itojun 	sc->sc_cid = crypto_get_driverid();
    286  1.1  itojun 	if (sc->sc_cid < 0)
    287  1.1  itojun 		goto fail_intr;
    288  1.1  itojun #endif
    289  1.1  itojun 
    290  1.1  itojun 	WRITE_REG_0(sc, HIFN_0_PUCNFG,
    291  1.1  itojun 	    READ_REG_0(sc, HIFN_0_PUCNFG) | HIFN_PUCNFG_CHIPID);
    292  1.1  itojun 	ena = READ_REG_0(sc, HIFN_0_PUSTAT) & HIFN_PUSTAT_CHIPENA;
    293  1.1  itojun 
    294  1.1  itojun #ifdef __OpenBSD__
    295  1.1  itojun 	switch (ena) {
    296  1.1  itojun 	case HIFN_PUSTAT_ENA_2:
    297  1.1  itojun 		crypto_register(sc->sc_cid, CRYPTO_3DES_CBC,
    298  1.1  itojun 		    hifn_newsession, hifn_freesession, hifn_process);
    299  1.1  itojun 		/*FALLTHROUGH*/
    300  1.1  itojun 	case HIFN_PUSTAT_ENA_1:
    301  1.1  itojun 		crypto_register(sc->sc_cid, CRYPTO_MD5_HMAC96,
    302  1.1  itojun 		    hifn_newsession, hifn_freesession, hifn_process);
    303  1.1  itojun 		crypto_register(sc->sc_cid, CRYPTO_SHA1_HMAC96,
    304  1.1  itojun 		    NULL, NULL, NULL);
    305  1.1  itojun 		crypto_register(sc->sc_cid, CRYPTO_DES_CBC,
    306  1.1  itojun 		    NULL, NULL, NULL);
    307  1.1  itojun 	}
    308  1.1  itojun #endif
    309  1.1  itojun 
    310  1.1  itojun 	return;
    311  1.1  itojun 
    312  1.1  itojun #ifdef __OpenBSD__
    313  1.1  itojun fail_intr:
    314  1.1  itojun #endif
    315  1.1  itojun 	pci_intr_disestablish(pc, sc->sc_ih);
    316  1.1  itojun fail_mem:
    317  1.1  itojun 	bus_dmamap_unload(sc->sc_dmat, dmamap);
    318  1.1  itojun 	bus_dmamap_destroy(sc->sc_dmat, dmamap);
    319  1.1  itojun 	bus_dmamem_unmap(sc->sc_dmat, kva, sizeof(*sc->sc_dma));
    320  1.1  itojun 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    321  1.1  itojun fail_io1:
    322  1.1  itojun 	bus_space_unmap(sc->sc_st1, sc->sc_sh1, iosize1);
    323  1.1  itojun fail_io0:
    324  1.1  itojun 	bus_space_unmap(sc->sc_st0, sc->sc_sh0, iosize0);
    325  1.1  itojun }
    326  1.1  itojun 
    327  1.1  itojun /*
    328  1.1  itojun  * Resets the board.  Values in the regesters are left as is
    329  1.1  itojun  * from the reset (i.e. initial values are assigned elsewhere).
    330  1.1  itojun  */
    331  1.1  itojun void
    332  1.1  itojun hifn_reset_board(sc)
    333  1.1  itojun 	struct hifn_softc *sc;
    334  1.1  itojun {
    335  1.1  itojun 	/*
    336  1.1  itojun 	 * Set polling in the DMA configuration register to zero.  0x7 avoids
    337  1.1  itojun 	 * resetting the board and zeros out the other fields.
    338  1.1  itojun 	 */
    339  1.1  itojun 	WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
    340  1.1  itojun 	    HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
    341  1.1  itojun 
    342  1.1  itojun 	/*
    343  1.1  itojun 	 * Now that polling has been disabled, we have to wait 1 ms
    344  1.1  itojun 	 * before resetting the board.
    345  1.1  itojun 	 */
    346  1.1  itojun 	DELAY(1000);
    347  1.1  itojun 
    348  1.1  itojun 	/* Reset the board.  We do this by writing zeros to the DMA reset
    349  1.1  itojun 	 * field, the BRD reset field, and the manditory 1 at position 2.
    350  1.1  itojun 	 * Every other field is set to zero.
    351  1.1  itojun 	 */
    352  1.1  itojun 	WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MODE);
    353  1.1  itojun 
    354  1.1  itojun 	/*
    355  1.1  itojun 	 * Wait another millisecond for the board to reset.
    356  1.1  itojun 	 */
    357  1.1  itojun 	DELAY(1000);
    358  1.1  itojun 
    359  1.1  itojun 	/*
    360  1.1  itojun 	 * Turn off the reset!  (No joke.)
    361  1.1  itojun 	 */
    362  1.1  itojun 	WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
    363  1.1  itojun 	    HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
    364  1.1  itojun }
    365  1.1  itojun 
    366  1.1  itojun u_int32_t
    367  1.1  itojun hifn_next_signature(a, cnt)
    368  1.1  itojun 	u_int32_t a;
    369  1.1  itojun 	u_int cnt;
    370  1.1  itojun {
    371  1.1  itojun 	int i;
    372  1.1  itojun 	u_int32_t v;
    373  1.1  itojun 
    374  1.1  itojun 	for (i = 0; i < cnt; i++) {
    375  1.1  itojun 
    376  1.1  itojun 		/* get the parity */
    377  1.1  itojun 		v = a & 0x80080125;
    378  1.1  itojun 		v ^= v >> 16;
    379  1.1  itojun 		v ^= v >> 8;
    380  1.1  itojun 		v ^= v >> 4;
    381  1.1  itojun 		v ^= v >> 2;
    382  1.1  itojun 		v ^= v >> 1;
    383  1.1  itojun 
    384  1.1  itojun 		a = (v & 1) ^ (a << 1);
    385  1.1  itojun 	}
    386  1.1  itojun 
    387  1.1  itojun 	return a;
    388  1.1  itojun }
    389  1.1  itojun 
    390  1.1  itojun struct pci2id {
    391  1.1  itojun 	u_short		pci_vendor;
    392  1.1  itojun 	u_short		pci_prod;
    393  1.1  itojun 	char		card_id[13];
    394  1.1  itojun } pci2id[] = {
    395  1.1  itojun 	{
    396  1.1  itojun 		PCI_VENDOR_NETSEC,
    397  1.1  itojun 		PCI_PRODUCT_NETSEC_7751,
    398  1.1  itojun 		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    399  1.1  itojun 		  0x00, 0x00, 0x00, 0x00, 0x00 }
    400  1.1  itojun 	}, {
    401  1.1  itojun 		PCI_VENDOR_INVERTEX,
    402  1.1  itojun 		PCI_PRODUCT_INVERTEX_AEON,
    403  1.1  itojun 		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    404  1.1  itojun 		  0x00, 0x00, 0x00, 0x00, 0x00 }
    405  1.1  itojun 	}, {
    406  1.1  itojun 		/*
    407  1.1  itojun 		 * Other vendors share this PCI ID as well, such as
    408  1.1  itojun 		 * http://www.powercrypt.com, and obviously they also
    409  1.1  itojun 		 * use the same key.
    410  1.1  itojun 		 */
    411  1.1  itojun 		PCI_VENDOR_HIFN,
    412  1.1  itojun 		PCI_PRODUCT_HIFN_7751,
    413  1.1  itojun 		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    414  1.1  itojun 		  0x00, 0x00, 0x00, 0x00, 0x00 }
    415  1.1  itojun 	},
    416  1.1  itojun };
    417  1.1  itojun 
    418  1.1  itojun /*
    419  1.1  itojun  * Checks to see if crypto is already enabled.  If crypto isn't enable,
    420  1.1  itojun  * "hifn_enable_crypto" is called to enable it.  The check is important,
    421  1.1  itojun  * as enabling crypto twice will lock the board.
    422  1.1  itojun  */
    423  1.1  itojun int
    424  1.1  itojun hifn_enable_crypto(sc, pciid)
    425  1.1  itojun 	struct hifn_softc *sc;
    426  1.1  itojun 	pcireg_t pciid;
    427  1.1  itojun {
    428  1.1  itojun 	u_int32_t dmacfg, ramcfg, encl, addr, i;
    429  1.1  itojun 	char *offtbl = NULL;
    430  1.1  itojun 
    431  1.1  itojun 	for (i = 0; i < sizeof(pci2id)/sizeof(pci2id[0]); i++) {
    432  1.1  itojun 		if (pci2id[i].pci_vendor == PCI_VENDOR(pciid) &&
    433  1.1  itojun 		    pci2id[i].pci_prod == PCI_PRODUCT(pciid)) {
    434  1.1  itojun 			offtbl = pci2id[i].card_id;
    435  1.1  itojun 			break;
    436  1.1  itojun 		}
    437  1.1  itojun 	}
    438  1.1  itojun 
    439  1.1  itojun 	if (offtbl == NULL) {
    440  1.1  itojun #ifdef HIFN_DEBUG
    441  1.1  itojun 		printf("%s: Unknown card!\n", sc->sc_dv.dv_xname);
    442  1.1  itojun #endif
    443  1.1  itojun 		return (1);
    444  1.1  itojun 	}
    445  1.1  itojun 
    446  1.1  itojun 	ramcfg = READ_REG_0(sc, HIFN_0_PUCNFG);
    447  1.1  itojun 	dmacfg = READ_REG_1(sc, HIFN_1_DMA_CNFG);
    448  1.1  itojun 
    449  1.1  itojun 	/*
    450  1.1  itojun 	 * The RAM config register's encrypt level bit needs to be set before
    451  1.1  itojun 	 * every read performed on the encryption level register.
    452  1.1  itojun 	 */
    453  1.1  itojun 	WRITE_REG_0(sc, HIFN_0_PUCNFG, ramcfg | HIFN_PUCNFG_CHIPID);
    454  1.1  itojun 
    455  1.1  itojun 	encl = READ_REG_0(sc, HIFN_0_PUSTAT) & HIFN_PUSTAT_CHIPENA;
    456  1.1  itojun 
    457  1.1  itojun 	/*
    458  1.1  itojun 	 * Make sure we don't re-unlock.  Two unlocks kills chip until the
    459  1.1  itojun 	 * next reboot.
    460  1.1  itojun 	 */
    461  1.1  itojun 	if (encl == HIFN_PUSTAT_ENA_1 || encl == HIFN_PUSTAT_ENA_2) {
    462  1.1  itojun #ifdef HIFN_DEBUG
    463  1.1  itojun 		printf("%s: Strong Crypto already enabled!\n",
    464  1.1  itojun 		    sc->sc_dv.dv_xname);
    465  1.1  itojun #endif
    466  1.1  itojun 		WRITE_REG_0(sc, HIFN_0_PUCNFG, ramcfg);
    467  1.1  itojun 		WRITE_REG_1(sc, HIFN_1_DMA_CNFG, dmacfg);
    468  1.1  itojun 		return 0;	/* success */
    469  1.1  itojun 	}
    470  1.1  itojun 
    471  1.1  itojun 	if (encl != 0 && encl != HIFN_PUSTAT_ENA_0) {
    472  1.1  itojun #ifdef HIFN_DEBUG
    473  1.1  itojun 		printf("%s: Unknown encryption level\n", sc->sc_dv.dv_xname);
    474  1.1  itojun #endif
    475  1.1  itojun 		return 1;
    476  1.1  itojun 	}
    477  1.1  itojun 
    478  1.1  itojun 	WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_UNLOCK |
    479  1.1  itojun 	    HIFN_DMACNFG_MSTRESET | HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
    480  1.1  itojun 	DELAY(1000);
    481  1.1  itojun 	addr = READ_REG_1(sc, HIFN_UNLOCK_SECRET1);
    482  1.1  itojun 	DELAY(1000);
    483  1.1  itojun 	WRITE_REG_1(sc, HIFN_UNLOCK_SECRET2, 0);
    484  1.1  itojun 	DELAY(1000);
    485  1.1  itojun 
    486  1.1  itojun 	for (i = 0; i <= 12; i++) {
    487  1.1  itojun 		addr = hifn_next_signature(addr, offtbl[i] + 0x101);
    488  1.1  itojun 		WRITE_REG_1(sc, HIFN_UNLOCK_SECRET2, addr);
    489  1.1  itojun 
    490  1.1  itojun 		DELAY(1000);
    491  1.1  itojun 	}
    492  1.1  itojun 
    493  1.1  itojun 	WRITE_REG_0(sc, HIFN_0_PUCNFG, ramcfg | HIFN_PUCNFG_CHIPID);
    494  1.1  itojun 	encl = READ_REG_0(sc, HIFN_0_PUSTAT) & HIFN_PUSTAT_CHIPENA;
    495  1.1  itojun 
    496  1.1  itojun #ifdef HIFN_DEBUG
    497  1.1  itojun 	if (encl != HIFN_PUSTAT_ENA_1 && encl != HIFN_PUSTAT_ENA_2)
    498  1.1  itojun 		printf("Encryption engine is permanently locked until next system reset.");
    499  1.1  itojun 	else
    500  1.1  itojun 		printf("Encryption engine enabled successfully!");
    501  1.1  itojun #endif
    502  1.1  itojun 
    503  1.1  itojun 	WRITE_REG_0(sc, HIFN_0_PUCNFG, ramcfg);
    504  1.1  itojun 	WRITE_REG_1(sc, HIFN_1_DMA_CNFG, dmacfg);
    505  1.1  itojun 
    506  1.1  itojun 	switch (encl) {
    507  1.1  itojun 	case HIFN_PUSTAT_ENA_0:
    508  1.1  itojun 		printf(": no encr/auth");
    509  1.1  itojun 		break;
    510  1.1  itojun 	case HIFN_PUSTAT_ENA_1:
    511  1.1  itojun 		printf(": DES enabled");
    512  1.1  itojun 		break;
    513  1.1  itojun 	case HIFN_PUSTAT_ENA_2:
    514  1.1  itojun 		printf(": fully enabled");
    515  1.1  itojun 		break;
    516  1.1  itojun 	default:
    517  1.1  itojun 		printf(": disabled");
    518  1.1  itojun 		break;
    519  1.1  itojun 	}
    520  1.1  itojun 
    521  1.1  itojun 	return 0;
    522  1.1  itojun }
    523  1.1  itojun 
    524  1.1  itojun /*
    525  1.1  itojun  * Give initial values to the registers listed in the "Register Space"
    526  1.1  itojun  * section of the HIFN Software Development reference manual.
    527  1.1  itojun  */
    528  1.1  itojun void
    529  1.1  itojun hifn_init_pci_registers(sc)
    530  1.1  itojun 	struct hifn_softc *sc;
    531  1.1  itojun {
    532  1.1  itojun 	/* write fixed values needed by the Initialization registers */
    533  1.1  itojun 	WRITE_REG_0(sc, HIFN_0_PUCTRL, HIFN_PUCTRL_DMAENA);
    534  1.1  itojun 	WRITE_REG_0(sc, HIFN_0_FIFOCNFG, HIFN_FIFOCNFG_THRESHOLD);
    535  1.1  itojun 	WRITE_REG_0(sc, HIFN_0_PUIER, HIFN_PUIER_DSTOVER);
    536  1.1  itojun 
    537  1.1  itojun 	/* write all 4 ring address registers */
    538  1.1  itojun 	WRITE_REG_1(sc, HIFN_1_DMA_CRAR, vtophys((vaddr_t)sc->sc_dma->cmdr));
    539  1.1  itojun 	WRITE_REG_1(sc, HIFN_1_DMA_SRAR, vtophys((vaddr_t)sc->sc_dma->srcr));
    540  1.1  itojun 	WRITE_REG_1(sc, HIFN_1_DMA_DRAR, vtophys((vaddr_t)sc->sc_dma->dstr));
    541  1.1  itojun 	WRITE_REG_1(sc, HIFN_1_DMA_RRAR, vtophys((vaddr_t)sc->sc_dma->resr));
    542  1.1  itojun 
    543  1.1  itojun 	/* write status register */
    544  1.1  itojun 	WRITE_REG_1(sc, HIFN_1_DMA_CSR, HIFN_DMACSR_D_CTRL_ENA |
    545  1.1  itojun 	    HIFN_DMACSR_R_CTRL_ENA | HIFN_DMACSR_S_CTRL_ENA |
    546  1.1  itojun 	    HIFN_DMACSR_C_CTRL_ENA);
    547  1.1  itojun 	WRITE_REG_1(sc, HIFN_1_DMA_IER, HIFN_DMAIER_R_DONE);
    548  1.1  itojun 
    549  1.1  itojun #if 0
    550  1.1  itojun #if BYTE_ORDER == BIG_ENDIAN
    551  1.1  itojun 	    (0x1 << 7) |
    552  1.1  itojun #endif
    553  1.1  itojun #endif
    554  1.1  itojun 	WRITE_REG_0(sc, HIFN_0_PUCNFG, HIFN_PUCNFG_COMPSING |
    555  1.1  itojun 	    HIFN_PUCNFG_DRFR_128 | HIFN_PUCNFG_TCALLPHASES |
    556  1.1  itojun 	    HIFN_PUCNFG_TCDRVTOTEM | HIFN_PUCNFG_BUS32 |
    557  1.1  itojun 	    (sc->sc_drammodel ? HIFN_PUCNFG_DRAM : HIFN_PUCNFG_SRAM));
    558  1.1  itojun 
    559  1.1  itojun 	WRITE_REG_0(sc, HIFN_0_PUISR, HIFN_PUISR_DSTOVER);
    560  1.1  itojun 	WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
    561  1.1  itojun 	    HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE | HIFN_DMACNFG_LAST |
    562  1.1  itojun 	    ((HIFN_POLL_FREQUENCY << 16 ) & HIFN_DMACNFG_POLLFREQ) |
    563  1.1  itojun 	    ((HIFN_POLL_SCALAR << 8) & HIFN_DMACNFG_POLLINVAL));
    564  1.1  itojun }
    565  1.1  itojun 
    566  1.1  itojun /*
    567  1.1  itojun  * The maximum number of sessions supported by the card
    568  1.1  itojun  * is dependent on the amount of context ram, which
    569  1.1  itojun  * encryption algorithms are enabled, and how compression
    570  1.1  itojun  * is configured.  This should be configured before this
    571  1.1  itojun  * routine is called.
    572  1.1  itojun  */
    573  1.1  itojun void
    574  1.1  itojun hifn_sessions(sc)
    575  1.1  itojun 	struct hifn_softc *sc;
    576  1.1  itojun {
    577  1.1  itojun 	u_int32_t pucnfg;
    578  1.1  itojun 	int ctxsize;
    579  1.1  itojun 
    580  1.1  itojun 	pucnfg = READ_REG_0(sc, HIFN_0_PUCNFG);
    581  1.1  itojun 
    582  1.1  itojun 	if (pucnfg & HIFN_PUCNFG_COMPSING) {
    583  1.1  itojun 		if (pucnfg & HIFN_PUCNFG_ENCCNFG)
    584  1.1  itojun 			ctxsize = 128;
    585  1.1  itojun 		else
    586  1.1  itojun 			ctxsize = 512;
    587  1.1  itojun 		sc->sc_maxses = 1 +
    588  1.1  itojun 		    ((sc->sc_ramsize - 32768) / ctxsize);
    589  1.1  itojun 	}
    590  1.1  itojun 	else
    591  1.1  itojun 		sc->sc_maxses = sc->sc_ramsize / 16384;
    592  1.1  itojun 
    593  1.1  itojun 	if (sc->sc_maxses > 2048)
    594  1.1  itojun 		sc->sc_maxses = 2048;
    595  1.1  itojun }
    596  1.1  itojun 
    597  1.1  itojun void
    598  1.1  itojun hifn_ramtype(sc)
    599  1.1  itojun 	struct hifn_softc *sc;
    600  1.1  itojun {
    601  1.1  itojun 	u_int8_t data[8], dataexpect[8];
    602  1.1  itojun 	int i;
    603  1.1  itojun 
    604  1.1  itojun 	hifn_reset_board(sc);
    605  1.1  itojun 	hifn_init_dma(sc);
    606  1.1  itojun 	hifn_init_pci_registers(sc);
    607  1.1  itojun 
    608  1.1  itojun 	for (i = 0; i < sizeof(data); i++)
    609  1.1  itojun 		data[i] = dataexpect[i] = 0x55;
    610  1.1  itojun 	if (hifn_writeramaddr(sc, 0, data, 0) < 0)
    611  1.1  itojun 		return;
    612  1.1  itojun 	if (hifn_readramaddr(sc, 0, data, 1) < 0)
    613  1.1  itojun 		return;
    614  1.1  itojun 	if (bcmp(data, dataexpect, sizeof(data)) != 0) {
    615  1.1  itojun 		sc->sc_drammodel = 1;
    616  1.1  itojun 		return;
    617  1.1  itojun 	}
    618  1.1  itojun 
    619  1.1  itojun 	hifn_reset_board(sc);
    620  1.1  itojun 	hifn_init_dma(sc);
    621  1.1  itojun 	hifn_init_pci_registers(sc);
    622  1.1  itojun 
    623  1.1  itojun 	for (i = 0; i < sizeof(data); i++)
    624  1.1  itojun 		data[i] = dataexpect[i] = 0xaa;
    625  1.1  itojun 	if (hifn_writeramaddr(sc, 0, data, 0) < 0)
    626  1.1  itojun 		return;
    627  1.1  itojun 	if (hifn_readramaddr(sc, 0, data, 1) < 0)
    628  1.1  itojun 		return;
    629  1.1  itojun 	if (bcmp(data, dataexpect, sizeof(data)) != 0)
    630  1.1  itojun 		sc->sc_drammodel = 1;
    631  1.1  itojun }
    632  1.1  itojun 
    633  1.1  itojun /*
    634  1.1  itojun  * For sram boards, just write/read memory until it fails, also check for
    635  1.1  itojun  * banking.
    636  1.1  itojun  */
    637  1.1  itojun int
    638  1.1  itojun hifn_sramsize(sc)
    639  1.1  itojun 	struct hifn_softc *sc;
    640  1.1  itojun {
    641  1.1  itojun 	u_int32_t a = 0, end;
    642  1.1  itojun 	u_int8_t data[8], dataexpect[8];
    643  1.1  itojun 
    644  1.1  itojun 	for (a = 0; a < sizeof(data); a++)
    645  1.1  itojun 		data[a] = dataexpect[a] = 0x5a;
    646  1.1  itojun 
    647  1.1  itojun 	hifn_reset_board(sc);
    648  1.1  itojun 	hifn_init_dma(sc);
    649  1.1  itojun 	hifn_init_pci_registers(sc);
    650  1.1  itojun 	end = 1 << 20;	/* 1MB */
    651  1.1  itojun 	for (a = 0; a < end; a += 16384) {
    652  1.1  itojun 		if (hifn_writeramaddr(sc, a, data, 0) < 0)
    653  1.1  itojun 			return (0);
    654  1.1  itojun 		if (hifn_readramaddr(sc, a, data, 1) < 0)
    655  1.1  itojun 			return (0);
    656  1.1  itojun 		if (bcmp(data, dataexpect, sizeof(data)) != 0)
    657  1.1  itojun 			return (0);
    658  1.1  itojun 		hifn_reset_board(sc);
    659  1.1  itojun 		hifn_init_dma(sc);
    660  1.1  itojun 		hifn_init_pci_registers(sc);
    661  1.1  itojun 		sc->sc_ramsize = a + 16384;
    662  1.1  itojun 	}
    663  1.1  itojun 
    664  1.1  itojun 	for (a = 0; a < sizeof(data); a++)
    665  1.1  itojun 		data[a] = dataexpect[a] = 0xa5;
    666  1.1  itojun 	if (hifn_writeramaddr(sc, 0, data, 0) < 0)
    667  1.1  itojun 		return (0);
    668  1.1  itojun 
    669  1.1  itojun 	end = sc->sc_ramsize;
    670  1.1  itojun 	for (a = 0; a < end; a += 16384) {
    671  1.1  itojun 		hifn_reset_board(sc);
    672  1.1  itojun 		hifn_init_dma(sc);
    673  1.1  itojun 		hifn_init_pci_registers(sc);
    674  1.1  itojun 		if (hifn_readramaddr(sc, a, data, 0) < 0)
    675  1.1  itojun 			return (0);
    676  1.1  itojun 		if (a != 0 && bcmp(data, dataexpect, sizeof(data)) == 0)
    677  1.1  itojun 			return (0);
    678  1.1  itojun 		sc->sc_ramsize = a + 16384;
    679  1.1  itojun 	}
    680  1.1  itojun 
    681  1.1  itojun 	hifn_reset_board(sc);
    682  1.1  itojun 	hifn_init_dma(sc);
    683  1.1  itojun 	hifn_init_pci_registers(sc);
    684  1.1  itojun 
    685  1.1  itojun 	return (0);
    686  1.1  itojun }
    687  1.1  itojun 
    688  1.1  itojun /*
    689  1.1  itojun  * XXX For dram boards, one should really try all of the
    690  1.1  itojun  * HIFN_PUCNFG_DSZ_*'s.  This just assumes that PUCNFG
    691  1.1  itojun  * is already set up correctly.
    692  1.1  itojun  */
    693  1.1  itojun int
    694  1.1  itojun hifn_dramsize(sc)
    695  1.1  itojun 	struct hifn_softc *sc;
    696  1.1  itojun {
    697  1.1  itojun 	u_int32_t cnfg;
    698  1.1  itojun 
    699  1.1  itojun 	cnfg = READ_REG_0(sc, HIFN_0_PUCNFG) &
    700  1.1  itojun 	    HIFN_PUCNFG_DRAMMASK;
    701  1.1  itojun 	sc->sc_ramsize = 1 << ((cnfg >> 13) + 18);
    702  1.1  itojun 	return (0);
    703  1.1  itojun }
    704  1.1  itojun 
    705  1.1  itojun int
    706  1.1  itojun hifn_writeramaddr(sc, addr, data, slot)
    707  1.1  itojun 	struct hifn_softc *sc;
    708  1.1  itojun 	int addr, slot;
    709  1.1  itojun 	u_int8_t *data;
    710  1.1  itojun {
    711  1.1  itojun 	struct hifn_dma *dma = sc->sc_dma;
    712  1.1  itojun 	hifn_base_command_t wc;
    713  1.1  itojun 	const u_int32_t masks = HIFN_D_VALID | HIFN_D_LAST | HIFN_D_MASKDONEIRQ;
    714  1.1  itojun 	u_int64_t src, dst;
    715  1.1  itojun 
    716  1.1  itojun 	wc.masks = 3 << 13;
    717  1.1  itojun 	wc.session_num = addr >> 14;
    718  1.1  itojun 	wc.total_source_count = 8;
    719  1.1  itojun 	wc.total_dest_count = addr & 0x3fff;;
    720  1.1  itojun 
    721  1.1  itojun 	/* build write command */
    722  1.1  itojun 	*(hifn_base_command_t *) sc->sc_dma->command_bufs[slot] = wc;
    723  1.1  itojun 	bcopy(data, &src, sizeof(src));
    724  1.1  itojun 
    725  1.1  itojun 	dma->srcr[slot].p = vtophys((vaddr_t)&src);
    726  1.1  itojun 	dma->dstr[slot].p = vtophys((vaddr_t)&dst);
    727  1.1  itojun 
    728  1.1  itojun 	dma->cmdr[slot].l = 16 | masks;
    729  1.1  itojun 	dma->srcr[slot].l = 8 | masks;
    730  1.1  itojun 	dma->dstr[slot].l = 8 | masks;
    731  1.1  itojun 	dma->resr[slot].l = HIFN_MAX_RESULT | masks;
    732  1.1  itojun 
    733  1.1  itojun 	DELAY(1000);	/* let write command execute */
    734  1.1  itojun 	if (dma->resr[slot].l & HIFN_D_VALID) {
    735  1.1  itojun 		printf("%s: SRAM/DRAM detection error -- "
    736  1.1  itojun 		    "result[%d] valid still set\n", sc->sc_dv.dv_xname, slot);
    737  1.1  itojun 		return (-1);
    738  1.1  itojun 	}
    739  1.1  itojun 	return (0);
    740  1.1  itojun }
    741  1.1  itojun 
    742  1.1  itojun int
    743  1.1  itojun hifn_readramaddr(sc, addr, data, slot)
    744  1.1  itojun 	struct hifn_softc *sc;
    745  1.1  itojun 	int addr, slot;
    746  1.1  itojun 	u_int8_t *data;
    747  1.1  itojun {
    748  1.1  itojun 	struct hifn_dma *dma = sc->sc_dma;
    749  1.1  itojun 	hifn_base_command_t rc;
    750  1.1  itojun 	const u_int32_t masks = HIFN_D_VALID | HIFN_D_LAST | HIFN_D_MASKDONEIRQ;
    751  1.1  itojun 	u_int64_t src, dst;
    752  1.1  itojun 
    753  1.1  itojun 	rc.masks = 2 << 13;
    754  1.1  itojun 	rc.session_num = addr >> 14;
    755  1.1  itojun 	rc.total_source_count = addr & 0x3fff;
    756  1.1  itojun 	rc.total_dest_count = 8;
    757  1.1  itojun 
    758  1.1  itojun 	*(hifn_base_command_t *) sc->sc_dma->command_bufs[slot] = rc;
    759  1.1  itojun 
    760  1.1  itojun 	dma->srcr[slot].p = vtophys((vaddr_t)&src);
    761  1.1  itojun 	dma->dstr[slot].p = vtophys((vaddr_t)&dst);
    762  1.1  itojun 	dma->cmdr[slot].l = 16 | masks;
    763  1.1  itojun 	dma->srcr[slot].l = 8 | masks;
    764  1.1  itojun 	dma->dstr[slot].l = 8 | masks;
    765  1.1  itojun 	dma->resr[slot].l = HIFN_MAX_RESULT | masks;
    766  1.1  itojun 
    767  1.1  itojun 	DELAY(1000);	/* let read command execute */
    768  1.1  itojun 	if (dma->resr[slot].l & HIFN_D_VALID) {
    769  1.1  itojun 		printf("%s: SRAM/DRAM detection error -- "
    770  1.1  itojun 		    "result[%d] valid still set\n", sc->sc_dv.dv_xname, slot);
    771  1.1  itojun 		return (-1);
    772  1.1  itojun 	}
    773  1.1  itojun 	bcopy(&dst, data, sizeof(dst));
    774  1.1  itojun 	return (0);
    775  1.1  itojun }
    776  1.1  itojun 
    777  1.1  itojun /*
    778  1.1  itojun  * Initialize the descriptor rings.
    779  1.1  itojun  */
    780  1.1  itojun void
    781  1.1  itojun hifn_init_dma(sc)
    782  1.1  itojun 	struct hifn_softc *sc;
    783  1.1  itojun {
    784  1.1  itojun 	struct hifn_dma *dma = sc->sc_dma;
    785  1.1  itojun 	int i;
    786  1.1  itojun 
    787  1.1  itojun 	/* initialize static pointer values */
    788  1.1  itojun 	for (i = 0; i < HIFN_D_CMD_RSIZE; i++)
    789  1.1  itojun 		dma->cmdr[i].p = vtophys((vaddr_t)dma->command_bufs[i]);
    790  1.1  itojun 	for (i = 0; i < HIFN_D_RES_RSIZE; i++)
    791  1.1  itojun 		dma->resr[i].p = vtophys((vaddr_t)dma->result_bufs[i]);
    792  1.1  itojun 
    793  1.1  itojun 	dma->cmdr[HIFN_D_CMD_RSIZE].p = vtophys((vaddr_t)dma->cmdr);
    794  1.1  itojun 	dma->srcr[HIFN_D_SRC_RSIZE].p = vtophys((vaddr_t)dma->srcr);
    795  1.1  itojun 	dma->dstr[HIFN_D_DST_RSIZE].p = vtophys((vaddr_t)dma->dstr);
    796  1.1  itojun 	dma->resr[HIFN_D_RES_RSIZE].p = vtophys((vaddr_t)dma->resr);
    797  1.1  itojun 	dma->cmdu = dma->srcu = dma->dstu = dma->resu = 0;
    798  1.1  itojun 	dma->cmdi = dma->srci = dma->dsti = dma->resi = 0;
    799  1.1  itojun 	dma->cmdk = dma->srck = dma->dstk = dma->resk = 0;
    800  1.1  itojun }
    801  1.1  itojun 
    802  1.1  itojun /*
    803  1.1  itojun  * Writes out the raw command buffer space.  Returns the
    804  1.1  itojun  * command buffer size.
    805  1.1  itojun  */
    806  1.1  itojun u_int
    807  1.1  itojun hifn_write_command(cmd, buf)
    808  1.1  itojun 	struct hifn_command *cmd;
    809  1.1  itojun 	u_int8_t *buf;
    810  1.1  itojun {
    811  1.1  itojun 	u_int8_t *buf_pos;
    812  1.1  itojun 	hifn_base_command_t *base_cmd;
    813  1.1  itojun 	hifn_mac_command_t *mac_cmd;
    814  1.1  itojun 	hifn_crypt_command_t *cry_cmd;
    815  1.1  itojun 	int using_mac, using_crypt, len;
    816  1.1  itojun 
    817  1.1  itojun 	buf_pos = buf;
    818  1.1  itojun 	using_mac = cmd->base_masks & HIFN_BASE_CMD_MAC;
    819  1.1  itojun 	using_crypt = cmd->base_masks & HIFN_BASE_CMD_CRYPT;
    820  1.1  itojun 
    821  1.1  itojun 	base_cmd = (hifn_base_command_t *)buf_pos;
    822  1.1  itojun 	base_cmd->masks = cmd->base_masks;
    823  1.1  itojun 	base_cmd->total_source_count = cmd->src_l;
    824  1.1  itojun 	base_cmd->total_dest_count = cmd->dst_l;
    825  1.1  itojun 	base_cmd->session_num = cmd->session_num;
    826  1.1  itojun 	buf_pos += sizeof(hifn_base_command_t);
    827  1.1  itojun 
    828  1.1  itojun 	if (using_mac) {
    829  1.1  itojun 		mac_cmd = (hifn_mac_command_t *)buf_pos;
    830  1.1  itojun 		mac_cmd->masks = cmd->mac_masks;
    831  1.1  itojun 		mac_cmd->header_skip = cmd->mac_header_skip;
    832  1.1  itojun 		mac_cmd->source_count = cmd->mac_process_len;
    833  1.1  itojun 		buf_pos += sizeof(hifn_mac_command_t);
    834  1.1  itojun 	}
    835  1.1  itojun 
    836  1.1  itojun 	if (using_crypt) {
    837  1.1  itojun 		cry_cmd = (hifn_crypt_command_t *)buf_pos;
    838  1.1  itojun 		cry_cmd->masks = cmd->cry_masks;
    839  1.1  itojun 		cry_cmd->header_skip = cmd->crypt_header_skip;
    840  1.1  itojun 		cry_cmd->source_count = cmd->crypt_process_len;
    841  1.1  itojun 		buf_pos += sizeof(hifn_crypt_command_t);
    842  1.1  itojun 	}
    843  1.1  itojun 
    844  1.1  itojun 	if (using_mac && mac_cmd->masks & HIFN_MAC_CMD_NEW_KEY) {
    845  1.1  itojun 		bcopy(cmd->mac, buf_pos, HIFN_MAC_KEY_LENGTH);
    846  1.1  itojun 		buf_pos += HIFN_MAC_KEY_LENGTH;
    847  1.1  itojun 	}
    848  1.1  itojun 
    849  1.1  itojun 	if (using_crypt && cry_cmd->masks & HIFN_CRYPT_CMD_NEW_KEY) {
    850  1.1  itojun 		len = (cry_cmd->masks & HIFN_CRYPT_CMD_ALG_3DES) ?
    851  1.1  itojun 		    HIFN_3DES_KEY_LENGTH : HIFN_DES_KEY_LENGTH;
    852  1.1  itojun 		bcopy(cmd->ck, buf_pos, len);
    853  1.1  itojun 		buf_pos += len;
    854  1.1  itojun 	}
    855  1.1  itojun 
    856  1.1  itojun 	if (using_crypt && cry_cmd->masks & HIFN_CRYPT_CMD_NEW_IV) {
    857  1.1  itojun 		bcopy(cmd->iv, buf_pos, HIFN_IV_LENGTH);
    858  1.1  itojun 		buf_pos += HIFN_IV_LENGTH;
    859  1.1  itojun 	}
    860  1.1  itojun 
    861  1.1  itojun 	if ((base_cmd->masks & (HIFN_BASE_CMD_MAC | HIFN_BASE_CMD_CRYPT)) == 0) {
    862  1.1  itojun 		bzero(buf_pos, 8);
    863  1.1  itojun 		buf_pos += 8;
    864  1.1  itojun 	}
    865  1.1  itojun 
    866  1.1  itojun 	return (buf_pos - buf);
    867  1.1  itojun }
    868  1.1  itojun 
    869  1.1  itojun int
    870  1.1  itojun hifn_crypto(sc, cmd)
    871  1.1  itojun 	struct hifn_softc *sc;
    872  1.1  itojun 	struct hifn_command *cmd;
    873  1.1  itojun {
    874  1.1  itojun #ifndef __OpenBSD__
    875  1.1  itojun 	return -1;
    876  1.1  itojun #else
    877  1.1  itojun 	u_int32_t cmdlen;
    878  1.1  itojun 	struct	hifn_dma *dma = sc->sc_dma;
    879  1.1  itojun 	int	cmdi, srci, dsti, resi, nicealign = 0;
    880  1.1  itojun 	int     s, i;
    881  1.1  itojun 
    882  1.1  itojun 	if (cmd->src_npa == 0 && cmd->src_m)
    883  1.1  itojun 		cmd->src_l = mbuf2pages(cmd->src_m, &cmd->src_npa,
    884  1.1  itojun 		    cmd->src_packp, cmd->src_packl, MAX_SCATTER, &nicealign);
    885  1.1  itojun 	if (cmd->src_l == 0)
    886  1.1  itojun 		return (-1);
    887  1.1  itojun 
    888  1.1  itojun 	if (nicealign == 0) {
    889  1.1  itojun 		int totlen, len;
    890  1.1  itojun 		struct mbuf *m, *top, **mp;
    891  1.1  itojun 
    892  1.1  itojun 		totlen = cmd->dst_l = cmd->src_l;
    893  1.1  itojun 		if (cmd->src_m->m_flags & M_PKTHDR) {
    894  1.1  itojun 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    895  1.1  itojun 			M_COPY_PKTHDR(m, cmd->src_m);
    896  1.1  itojun 			len = MHLEN;
    897  1.1  itojun 		} else {
    898  1.1  itojun 			MGET(m, M_DONTWAIT, MT_DATA);
    899  1.1  itojun 			len = MLEN;
    900  1.1  itojun 		}
    901  1.1  itojun 		if (m == NULL)
    902  1.1  itojun 			return (-1);
    903  1.1  itojun 		if (totlen >= MINCLSIZE) {
    904  1.1  itojun 			MCLGET(m, M_DONTWAIT);
    905  1.1  itojun 			if (m->m_flags & M_EXT)
    906  1.1  itojun 				len = MCLBYTES;
    907  1.1  itojun 		}
    908  1.1  itojun 		m->m_len = len;
    909  1.1  itojun 		top = NULL;
    910  1.1  itojun 		mp = &top;
    911  1.1  itojun 
    912  1.1  itojun 		while (totlen > 0) {
    913  1.1  itojun 			if (top) {
    914  1.1  itojun 				MGET(m, M_DONTWAIT, MT_DATA);
    915  1.1  itojun 				if (m == NULL) {
    916  1.1  itojun 					m_freem(top);
    917  1.1  itojun 					return (-1);
    918  1.1  itojun 				}
    919  1.1  itojun 				len = MLEN;
    920  1.1  itojun 			}
    921  1.1  itojun 			if (top && totlen >= MINCLSIZE) {
    922  1.1  itojun 				MCLGET(m, M_DONTWAIT);
    923  1.1  itojun 				if (m->m_flags & M_EXT)
    924  1.1  itojun 					len = MCLBYTES;
    925  1.1  itojun 			}
    926  1.1  itojun 			m->m_len = len;
    927  1.1  itojun 			totlen -= len;
    928  1.1  itojun 			*mp = m;
    929  1.1  itojun 			mp = &m->m_next;
    930  1.1  itojun 		}
    931  1.1  itojun 		cmd->dst_m = top;
    932  1.1  itojun 	}
    933  1.1  itojun 	else
    934  1.1  itojun 		cmd->dst_m = cmd->src_m;
    935  1.1  itojun 
    936  1.1  itojun 	cmd->dst_l = mbuf2pages(cmd->dst_m, &cmd->dst_npa,
    937  1.1  itojun 	    cmd->dst_packp, cmd->dst_packl, MAX_SCATTER, NULL);
    938  1.1  itojun 	if (cmd->dst_l == 0)
    939  1.1  itojun 		return (-1);
    940  1.1  itojun 
    941  1.1  itojun #ifdef HIFN_DEBUG
    942  1.1  itojun 	printf("%s: Entering cmd: stat %8x ien %8x u %d/%d/%d/%d n %d/%d\n",
    943  1.1  itojun 	    sc->sc_dv.dv_xname,
    944  1.1  itojun 	    READ_REG_1(sc, HIFN_1_DMA_CSR), READ_REG_1(sc, HIFN_1_DMA_IER),
    945  1.1  itojun 	    dma->cmdu, dma->srcu, dma->dstu, dma->resu, cmd->src_npa,
    946  1.1  itojun 	    cmd->dst_npa);
    947  1.1  itojun #endif
    948  1.1  itojun 
    949  1.1  itojun 	s = splnet();
    950  1.1  itojun 
    951  1.1  itojun 	/*
    952  1.1  itojun 	 * need 1 cmd, and 1 res
    953  1.1  itojun 	 * need N src, and N dst
    954  1.1  itojun 	 */
    955  1.1  itojun 	if (dma->cmdu+1 > HIFN_D_CMD_RSIZE ||
    956  1.1  itojun 	    dma->srcu+cmd->src_npa > HIFN_D_SRC_RSIZE ||
    957  1.1  itojun 	    dma->dstu+cmd->dst_npa > HIFN_D_DST_RSIZE ||
    958  1.1  itojun 	    dma->resu+1 > HIFN_D_RES_RSIZE) {
    959  1.1  itojun 		splx(s);
    960  1.1  itojun 		return (HIFN_CRYPTO_RINGS_FULL);
    961  1.1  itojun 	}
    962  1.1  itojun 
    963  1.1  itojun 	if (dma->cmdi == HIFN_D_CMD_RSIZE) {
    964  1.1  itojun 		dma->cmdi = 0;
    965  1.1  itojun 		dma->cmdr[HIFN_D_CMD_RSIZE].l = HIFN_D_VALID | HIFN_D_LAST |
    966  1.1  itojun 		    HIFN_D_MASKDONEIRQ | HIFN_D_JUMP;
    967  1.1  itojun 	}
    968  1.1  itojun 	cmdi = dma->cmdi++;
    969  1.1  itojun 
    970  1.1  itojun 	if (dma->resi == HIFN_D_RES_RSIZE) {
    971  1.1  itojun 		dma->resi = 0;
    972  1.1  itojun 		dma->resr[HIFN_D_RES_RSIZE].l = HIFN_D_VALID | HIFN_D_LAST |
    973  1.1  itojun 		    HIFN_D_MASKDONEIRQ | HIFN_D_JUMP;
    974  1.1  itojun 	}
    975  1.1  itojun 	resi = dma->resi++;
    976  1.1  itojun 
    977  1.1  itojun 	cmdlen = hifn_write_command(cmd, dma->command_bufs[cmdi]);
    978  1.1  itojun #ifdef HIFN_DEBUG
    979  1.1  itojun 	printf("write_command %d (nice %d)\n", cmdlen, nicealign);
    980  1.1  itojun #endif
    981  1.1  itojun 	/* .p for command/result already set */
    982  1.1  itojun 	dma->cmdr[cmdi].l = cmdlen | HIFN_D_VALID | HIFN_D_LAST |
    983  1.1  itojun 	    HIFN_D_MASKDONEIRQ;
    984  1.1  itojun 	dma->cmdu++;
    985  1.1  itojun 
    986  1.1  itojun 	/*
    987  1.1  itojun 	 * We don't worry about missing an interrupt (which a "command wait"
    988  1.1  itojun 	 * interrupt salvages us from), unless there is more than one command
    989  1.1  itojun 	 * in the queue.
    990  1.1  itojun 	 */
    991  1.1  itojun 	if (dma->cmdu > 1)
    992  1.1  itojun 		WRITE_REG_1(sc, HIFN_1_DMA_IER,
    993  1.1  itojun 		    HIFN_DMAIER_C_WAIT | HIFN_DMAIER_R_DONE);
    994  1.1  itojun 
    995  1.1  itojun 	hifnstats.hst_ipackets++;
    996  1.1  itojun 
    997  1.1  itojun 	for (i = 0; i < cmd->src_npa; i++) {
    998  1.1  itojun 		int last = 0;
    999  1.1  itojun 
   1000  1.1  itojun 		if (i == cmd->src_npa-1)
   1001  1.1  itojun 			last = HIFN_D_LAST;
   1002  1.1  itojun 
   1003  1.1  itojun 		if (dma->srci == HIFN_D_SRC_RSIZE) {
   1004  1.1  itojun 			srci = 0, dma->srci = 1;
   1005  1.1  itojun 			dma->srcr[HIFN_D_SRC_RSIZE].l = HIFN_D_VALID |
   1006  1.1  itojun 			    HIFN_D_MASKDONEIRQ | HIFN_D_JUMP | HIFN_D_LAST;
   1007  1.1  itojun 		} else
   1008  1.1  itojun 			srci = dma->srci++;
   1009  1.1  itojun 		dma->srcr[srci].p = cmd->src_packp[i];
   1010  1.1  itojun 		dma->srcr[srci].l = cmd->src_packl[i] | HIFN_D_VALID |
   1011  1.1  itojun 		    HIFN_D_MASKDONEIRQ | last;
   1012  1.1  itojun 		hifnstats.hst_ibytes += cmd->src_packl[i];
   1013  1.1  itojun 	}
   1014  1.1  itojun 	dma->srcu += cmd->src_npa;
   1015  1.1  itojun 
   1016  1.1  itojun 	for (i = 0; i < cmd->dst_npa; i++) {
   1017  1.1  itojun 		int last = 0;
   1018  1.1  itojun 
   1019  1.1  itojun 		if (i == cmd->dst_npa-1)
   1020  1.1  itojun 			last = HIFN_D_LAST;
   1021  1.1  itojun 
   1022  1.1  itojun 		if (dma->dsti == HIFN_D_DST_RSIZE) {
   1023  1.1  itojun 			dsti = 0, dma->dsti = 1;
   1024  1.1  itojun 			dma->dstr[HIFN_D_DST_RSIZE].l = HIFN_D_VALID |
   1025  1.1  itojun 			    HIFN_D_MASKDONEIRQ | HIFN_D_JUMP | HIFN_D_LAST;
   1026  1.1  itojun 		} else
   1027  1.1  itojun 			dsti = dma->dsti++;
   1028  1.1  itojun 		dma->dstr[dsti].p = cmd->dst_packp[i];
   1029  1.1  itojun 		dma->dstr[dsti].l = cmd->dst_packl[i] | HIFN_D_VALID |
   1030  1.1  itojun 		    HIFN_D_MASKDONEIRQ | last;
   1031  1.1  itojun 	}
   1032  1.1  itojun 	dma->dstu += cmd->dst_npa;
   1033  1.1  itojun 
   1034  1.1  itojun 	/*
   1035  1.1  itojun 	 * Unlike other descriptors, we don't mask done interrupt from
   1036  1.1  itojun 	 * result descriptor.
   1037  1.1  itojun 	 */
   1038  1.1  itojun #ifdef HIFN_DEBUG
   1039  1.1  itojun 	printf("load res\n");
   1040  1.1  itojun #endif
   1041  1.1  itojun 	dma->hifn_commands[resi] = cmd;
   1042  1.1  itojun 	dma->resr[resi].l = HIFN_MAX_RESULT | HIFN_D_VALID | HIFN_D_LAST;
   1043  1.1  itojun 	dma->resu++;
   1044  1.1  itojun 
   1045  1.1  itojun #ifdef HIFN_DEBUG
   1046  1.1  itojun 	printf("%s: command: stat %8x ier %8x\n",
   1047  1.1  itojun 	    sc->sc_dv.dv_xname,
   1048  1.1  itojun 	    READ_REG_1(sc, HIFN_1_DMA_CSR), READ_REG_1(sc, HIFN_1_DMA_IER));
   1049  1.1  itojun #endif
   1050  1.1  itojun 
   1051  1.1  itojun 	splx(s);
   1052  1.1  itojun 	return 0;		/* success */
   1053  1.1  itojun #endif
   1054  1.1  itojun }
   1055  1.1  itojun 
   1056  1.1  itojun int
   1057  1.1  itojun hifn_intr(arg)
   1058  1.1  itojun 	void *arg;
   1059  1.1  itojun {
   1060  1.1  itojun 	struct hifn_softc *sc = arg;
   1061  1.1  itojun 	struct hifn_dma *dma = sc->sc_dma;
   1062  1.1  itojun 	u_int32_t dmacsr;
   1063  1.1  itojun 	int i, u;
   1064  1.1  itojun 
   1065  1.1  itojun 	dmacsr = READ_REG_1(sc, HIFN_1_DMA_CSR);
   1066  1.1  itojun 
   1067  1.1  itojun #ifdef HIFN_DEBUG
   1068  1.1  itojun 	printf("%s: irq: stat %08x ien %08x u %d/%d/%d/%d\n",
   1069  1.1  itojun 	    sc->sc_dv.dv_xname,
   1070  1.1  itojun 	    dmacsr, READ_REG_1(sc, HIFN_1_DMA_IER),
   1071  1.1  itojun 	    dma->cmdu, dma->srcu, dma->dstu, dma->resu);
   1072  1.1  itojun #endif
   1073  1.1  itojun 
   1074  1.1  itojun 	if ((dmacsr & (HIFN_DMACSR_R_DONE | HIFN_DMACSR_C_WAIT)) == 0)
   1075  1.1  itojun 		return (0);
   1076  1.1  itojun 
   1077  1.1  itojun 	if (dma->resu > HIFN_D_RES_RSIZE)
   1078  1.1  itojun 		printf("%s: Internal Error -- ring overflow\n",
   1079  1.1  itojun 		    sc->sc_dv.dv_xname);
   1080  1.1  itojun 
   1081  1.1  itojun 	if ((dmacsr & HIFN_DMACSR_C_WAIT) && (dma->cmdu == 0)) {
   1082  1.1  itojun 		/*
   1083  1.1  itojun 		 * If no slots to process and we receive a "waiting on
   1084  1.1  itojun 		 * command" interrupt, we disable the "waiting on command"
   1085  1.1  itojun 		 * (by clearing it).
   1086  1.1  itojun 		 */
   1087  1.1  itojun 		WRITE_REG_1(sc, HIFN_1_DMA_IER, HIFN_DMAIER_R_DONE);
   1088  1.1  itojun 	}
   1089  1.1  itojun 
   1090  1.1  itojun 	while (dma->resu > 0) {
   1091  1.1  itojun 		struct hifn_command *cmd;
   1092  1.1  itojun 		u_int8_t *macbuf = NULL;
   1093  1.1  itojun 
   1094  1.1  itojun 		cmd = dma->hifn_commands[dma->resk];
   1095  1.1  itojun 
   1096  1.1  itojun 		/* if still valid, stop processing */
   1097  1.1  itojun 		if (dma->resr[dma->resk].l & HIFN_D_VALID)
   1098  1.1  itojun 			break;
   1099  1.1  itojun 
   1100  1.1  itojun 		if (cmd->base_masks & HIFN_BASE_CMD_MAC) {
   1101  1.1  itojun 			macbuf = dma->result_bufs[dma->resk];
   1102  1.1  itojun 			macbuf += 12;
   1103  1.1  itojun 		}
   1104  1.1  itojun 
   1105  1.1  itojun #ifdef __OpenBSD__
   1106  1.1  itojun 		hifn_callback(sc, cmd, macbuf);
   1107  1.1  itojun #endif
   1108  1.1  itojun 
   1109  1.1  itojun 		if (++dma->resk == HIFN_D_RES_RSIZE)
   1110  1.1  itojun 			dma->resk = 0;
   1111  1.1  itojun 		dma->resu--;
   1112  1.1  itojun 		hifnstats.hst_opackets++;
   1113  1.1  itojun 	}
   1114  1.1  itojun 
   1115  1.1  itojun 	/* clear the rings */
   1116  1.1  itojun 
   1117  1.1  itojun 	i = dma->srck; u = dma->srcu;
   1118  1.1  itojun 	while (u != 0 && (dma->srcr[i].l & HIFN_D_VALID) == 0) {
   1119  1.1  itojun 		if (++i == HIFN_D_SRC_RSIZE)
   1120  1.1  itojun 			i = 0;
   1121  1.1  itojun 		u--;
   1122  1.1  itojun 	}
   1123  1.1  itojun 	dma->srck = i; dma->srcu = u;
   1124  1.1  itojun 
   1125  1.1  itojun 	i = dma->cmdk; u = dma->cmdu;
   1126  1.1  itojun 	while (u != 0 && (dma->cmdr[i].l & HIFN_D_VALID) == 0) {
   1127  1.1  itojun 		if (++i == HIFN_D_CMD_RSIZE)
   1128  1.1  itojun 			i = 0;
   1129  1.1  itojun 		u--;
   1130  1.1  itojun 	}
   1131  1.1  itojun 	dma->cmdk = i; dma->cmdu = u;
   1132  1.1  itojun 
   1133  1.1  itojun 	/*
   1134  1.1  itojun 	 * Clear "result done" and "command wait" flags in status register.
   1135  1.1  itojun 	 * If we still have slots to process and we received a "command wait"
   1136  1.1  itojun 	 * interrupt, this will interupt us again.
   1137  1.1  itojun 	 */
   1138  1.1  itojun 	WRITE_REG_1(sc, HIFN_1_DMA_CSR, HIFN_DMACSR_R_DONE|HIFN_DMACSR_C_WAIT);
   1139  1.1  itojun 	return (1);
   1140  1.1  itojun }
   1141  1.1  itojun 
   1142  1.1  itojun #ifdef __OpenBSD__
   1143  1.1  itojun /*
   1144  1.1  itojun  * Allocate a new 'session' and return an encoded session id.  'sidp'
   1145  1.1  itojun  * contains our registration id, and should contain an encoded session
   1146  1.1  itojun  * id on successful allocation.
   1147  1.1  itojun  */
   1148  1.1  itojun int
   1149  1.1  itojun hifn_newsession(sidp, cri)
   1150  1.1  itojun 	u_int32_t *sidp;
   1151  1.1  itojun 	struct cryptoini *cri;
   1152  1.1  itojun {
   1153  1.1  itojun 	struct cryptoini *c;
   1154  1.1  itojun 	struct hifn_softc *sc = NULL;
   1155  1.1  itojun 	int i, mac = 0, cry = 0;
   1156  1.1  itojun 
   1157  1.1  itojun 	if (sidp == NULL || cri == NULL)
   1158  1.1  itojun 		return (EINVAL);
   1159  1.1  itojun 
   1160  1.1  itojun 	for (i = 0; i < hifn_cd.cd_ndevs; i++) {
   1161  1.1  itojun 		sc = hifn_cd.cd_devs[i];
   1162  1.1  itojun 		if (sc == NULL)
   1163  1.1  itojun 			break;
   1164  1.1  itojun 		if (sc->sc_cid == (*sidp))
   1165  1.1  itojun 			break;
   1166  1.1  itojun 	}
   1167  1.1  itojun 	if (sc == NULL)
   1168  1.1  itojun 		return (EINVAL);
   1169  1.1  itojun 
   1170  1.1  itojun 	for (i = 0; i < sc->sc_maxses; i++)
   1171  1.1  itojun 		if (sc->sc_sessions[i].hs_flags == 0)
   1172  1.1  itojun 			break;
   1173  1.1  itojun 	if (i == sc->sc_maxses)
   1174  1.1  itojun 		return (ENOMEM);
   1175  1.1  itojun 
   1176  1.1  itojun 	for (c = cri; c != NULL; c = c->cri_next) {
   1177  1.1  itojun 		if (c->cri_alg == CRYPTO_MD5_HMAC96 ||
   1178  1.1  itojun 		    c->cri_alg == CRYPTO_SHA1_HMAC96) {
   1179  1.1  itojun 			if (mac)
   1180  1.1  itojun 				return (EINVAL);
   1181  1.1  itojun 			mac = 1;
   1182  1.1  itojun 		} else if (c->cri_alg == CRYPTO_DES_CBC ||
   1183  1.1  itojun 		    c->cri_alg == CRYPTO_3DES_CBC) {
   1184  1.1  itojun 			if (cry)
   1185  1.1  itojun 				return (EINVAL);
   1186  1.1  itojun 			cry = 1;
   1187  1.1  itojun 		}
   1188  1.1  itojun 		else
   1189  1.1  itojun 			return (EINVAL);
   1190  1.1  itojun 	}
   1191  1.1  itojun 	if (mac == 0 && cry == 0)
   1192  1.1  itojun 		return (EINVAL);
   1193  1.1  itojun 
   1194  1.1  itojun 	*sidp = HIFN_SID(sc->sc_dv.dv_unit, i);
   1195  1.1  itojun 	sc->sc_sessions[i].hs_flags = 1;
   1196  1.1  itojun 	get_random_bytes(sc->sc_sessions[i].hs_iv, HIFN_IV_LENGTH);
   1197  1.1  itojun 
   1198  1.1  itojun 	return (0);
   1199  1.1  itojun }
   1200  1.1  itojun 
   1201  1.1  itojun /*
   1202  1.1  itojun  * Deallocate a session.
   1203  1.1  itojun  * XXX this routine should run a zero'd mac/encrypt key into context ram.
   1204  1.1  itojun  * XXX to blow away any keys already stored there.
   1205  1.1  itojun  */
   1206  1.1  itojun int
   1207  1.1  itojun hifn_freesession(tid)
   1208  1.1  itojun 	u_int64_t tid;
   1209  1.1  itojun {
   1210  1.1  itojun 	struct hifn_softc *sc;
   1211  1.1  itojun 	int card, session;
   1212  1.1  itojun 	u_int32_t sid = ((u_int32_t) tid) & 0xffffffff;
   1213  1.1  itojun 
   1214  1.1  itojun 	card = HIFN_CARD(sid);
   1215  1.1  itojun 	if (card >= hifn_cd.cd_ndevs || hifn_cd.cd_devs[card] == NULL)
   1216  1.1  itojun 		return (EINVAL);
   1217  1.1  itojun 
   1218  1.1  itojun 	sc = hifn_cd.cd_devs[card];
   1219  1.1  itojun 	session = HIFN_SESSION(sid);
   1220  1.1  itojun 	if (session >= sc->sc_maxses)
   1221  1.1  itojun 		return (EINVAL);
   1222  1.1  itojun 
   1223  1.1  itojun 	bzero(&sc->sc_sessions[session], sizeof(sc->sc_sessions[session]));
   1224  1.1  itojun 	return (0);
   1225  1.1  itojun }
   1226  1.1  itojun 
   1227  1.1  itojun int
   1228  1.1  itojun hifn_process(crp)
   1229  1.1  itojun 	struct cryptop *crp;
   1230  1.1  itojun {
   1231  1.1  itojun 	struct hifn_command *cmd = NULL;
   1232  1.1  itojun 	int card, session, err;
   1233  1.1  itojun 	struct hifn_softc *sc;
   1234  1.1  itojun 	struct cryptodesc *crd1, *crd2, *maccrd, *enccrd;
   1235  1.1  itojun 
   1236  1.1  itojun 	if (crp == NULL || crp->crp_callback == NULL) {
   1237  1.1  itojun 		hifnstats.hst_invalid++;
   1238  1.1  itojun 		return (EINVAL);
   1239  1.1  itojun 	}
   1240  1.1  itojun 
   1241  1.1  itojun 	card = HIFN_CARD(crp->crp_sid);
   1242  1.1  itojun 	if (card >= hifn_cd.cd_ndevs || hifn_cd.cd_devs[card] == NULL) {
   1243  1.1  itojun 		err = EINVAL;
   1244  1.1  itojun 		goto errout;
   1245  1.1  itojun 	}
   1246  1.1  itojun 
   1247  1.1  itojun 	sc = hifn_cd.cd_devs[card];
   1248  1.1  itojun 	session = HIFN_SESSION(crp->crp_sid);
   1249  1.1  itojun 	if (session >= sc->sc_maxses) {
   1250  1.1  itojun 		err = EINVAL;
   1251  1.1  itojun 		goto errout;
   1252  1.1  itojun 	}
   1253  1.1  itojun 
   1254  1.1  itojun 	cmd = (struct hifn_command *)malloc(sizeof(struct hifn_command),
   1255  1.1  itojun 	    M_DEVBUF, M_NOWAIT);
   1256  1.1  itojun 	if (cmd == NULL) {
   1257  1.1  itojun 		err = ENOMEM;
   1258  1.1  itojun 		goto errout;
   1259  1.1  itojun 	}
   1260  1.1  itojun 	bzero(cmd, sizeof(struct hifn_command));
   1261  1.1  itojun 
   1262  1.1  itojun 	if (crp->crp_flags & CRYPTO_F_IMBUF) {
   1263  1.1  itojun 		cmd->src_m = (struct mbuf *)crp->crp_buf;
   1264  1.1  itojun 		cmd->dst_m = (struct mbuf *)crp->crp_buf;
   1265  1.1  itojun 	} else {
   1266  1.1  itojun 		err = EINVAL;
   1267  1.1  itojun 		goto errout;	/* XXX only handle mbufs right now */
   1268  1.1  itojun 	}
   1269  1.1  itojun 
   1270  1.1  itojun 	crd1 = crp->crp_desc;
   1271  1.1  itojun 	if (crd1 == NULL) {
   1272  1.1  itojun 		err = EINVAL;
   1273  1.1  itojun 		goto errout;
   1274  1.1  itojun 	}
   1275  1.1  itojun 	crd2 = crd1->crd_next;
   1276  1.1  itojun 
   1277  1.1  itojun 	if (crd2 == NULL) {
   1278  1.1  itojun 		if (crd1->crd_alg == CRYPTO_MD5_HMAC96 ||
   1279  1.1  itojun 		    crd1->crd_alg == CRYPTO_SHA1_HMAC96) {
   1280  1.1  itojun 			maccrd = crd1;
   1281  1.1  itojun 			enccrd = NULL;
   1282  1.1  itojun 		} else if (crd1->crd_alg == CRYPTO_DES_CBC ||
   1283  1.1  itojun 			 crd1->crd_alg == CRYPTO_3DES_CBC) {
   1284  1.1  itojun 			if ((crd1->crd_flags & CRD_F_ENCRYPT) == 0)
   1285  1.1  itojun 				cmd->base_masks |= HIFN_BASE_CMD_DECODE;
   1286  1.1  itojun 			maccrd = NULL;
   1287  1.1  itojun 			enccrd = crd1;
   1288  1.1  itojun 		} else {
   1289  1.1  itojun 			err = EINVAL;
   1290  1.1  itojun 			goto errout;
   1291  1.1  itojun 		}
   1292  1.1  itojun 	} else {
   1293  1.1  itojun 		if ((crd1->crd_alg == CRYPTO_MD5_HMAC96 ||
   1294  1.1  itojun 		    crd1->crd_alg == CRYPTO_SHA1_HMAC96) &&
   1295  1.1  itojun 		    (crd2->crd_alg == CRYPTO_DES_CBC ||
   1296  1.1  itojun 			crd2->crd_alg == CRYPTO_3DES_CBC) &&
   1297  1.1  itojun 		    ((crd2->crd_flags & CRD_F_ENCRYPT) == 0)) {
   1298  1.1  itojun 			cmd->base_masks = HIFN_BASE_CMD_DECODE;
   1299  1.1  itojun 			maccrd = crd1;
   1300  1.1  itojun 			enccrd = crd2;
   1301  1.1  itojun 		} else if ((crd1->crd_alg == CRYPTO_DES_CBC ||
   1302  1.1  itojun 		    crd1->crd_alg == CRYPTO_3DES_CBC) &&
   1303  1.1  itojun 		    (crd2->crd_alg == CRYPTO_MD5_HMAC96 ||
   1304  1.1  itojun 			crd2->crd_alg == CRYPTO_SHA1_HMAC96) &&
   1305  1.1  itojun 		    (crd1->crd_flags & CRD_F_ENCRYPT)) {
   1306  1.1  itojun 			enccrd = crd1;
   1307  1.1  itojun 			maccrd = crd2;
   1308  1.1  itojun 		} else {
   1309  1.1  itojun 			/*
   1310  1.1  itojun 			 * We cannot order the 7751 as requested
   1311  1.1  itojun 			 */
   1312  1.1  itojun 			err = EINVAL;
   1313  1.1  itojun 			goto errout;
   1314  1.1  itojun 		}
   1315  1.1  itojun 	}
   1316  1.1  itojun 
   1317  1.1  itojun 	if (enccrd) {
   1318  1.1  itojun 		cmd->base_masks |= HIFN_BASE_CMD_CRYPT;
   1319  1.1  itojun 		cmd->cry_masks |= HIFN_CRYPT_CMD_MODE_CBC |
   1320  1.1  itojun 		    HIFN_CRYPT_CMD_NEW_IV;
   1321  1.1  itojun 		if (enccrd->crd_flags & CRD_F_ENCRYPT) {
   1322  1.1  itojun 			if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
   1323  1.1  itojun 				bcopy(enccrd->crd_iv, cmd->iv, HIFN_IV_LENGTH);
   1324  1.1  itojun 			else
   1325  1.1  itojun 				bcopy(sc->sc_sessions[session].hs_iv,
   1326  1.1  itojun 				    cmd->iv, HIFN_IV_LENGTH);
   1327  1.1  itojun 
   1328  1.1  itojun 			if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0)
   1329  1.1  itojun 				m_copyback(cmd->src_m, enccrd->crd_inject,
   1330  1.1  itojun 				    HIFN_IV_LENGTH, cmd->iv);
   1331  1.1  itojun 		} else {
   1332  1.1  itojun 			if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
   1333  1.1  itojun 				bcopy(enccrd->crd_iv, cmd->iv, HIFN_IV_LENGTH);
   1334  1.1  itojun 			else
   1335  1.1  itojun 				m_copydata(cmd->src_m, enccrd->crd_inject,
   1336  1.1  itojun 				    HIFN_IV_LENGTH, cmd->iv);
   1337  1.1  itojun 		}
   1338  1.1  itojun 
   1339  1.1  itojun 		if (enccrd->crd_alg == CRYPTO_DES_CBC)
   1340  1.1  itojun 			cmd->cry_masks |= HIFN_CRYPT_CMD_ALG_DES;
   1341  1.1  itojun 		else
   1342  1.1  itojun 			cmd->cry_masks |= HIFN_CRYPT_CMD_ALG_3DES;
   1343  1.1  itojun 
   1344  1.1  itojun 		cmd->crypt_header_skip = enccrd->crd_skip;
   1345  1.1  itojun 		cmd->crypt_process_len = enccrd->crd_len;
   1346  1.1  itojun 		cmd->ck = enccrd->crd_key;
   1347  1.1  itojun 
   1348  1.1  itojun 		if (sc->sc_sessions[session].hs_flags == 1)
   1349  1.1  itojun 			cmd->cry_masks |= HIFN_CRYPT_CMD_NEW_KEY;
   1350  1.1  itojun 	}
   1351  1.1  itojun 
   1352  1.1  itojun 	if (maccrd) {
   1353  1.1  itojun 		cmd->base_masks |= HIFN_BASE_CMD_MAC;
   1354  1.1  itojun 		cmd->mac_masks |= HIFN_MAC_CMD_RESULT |
   1355  1.1  itojun 		    HIFN_MAC_CMD_MODE_HMAC | HIFN_MAC_CMD_RESULT |
   1356  1.1  itojun 		    HIFN_MAC_CMD_POS_IPSEC | HIFN_MAC_CMD_TRUNC;
   1357  1.1  itojun 
   1358  1.1  itojun 		if (maccrd->crd_alg == CRYPTO_MD5_HMAC96)
   1359  1.1  itojun 			cmd->mac_masks |= HIFN_MAC_CMD_ALG_MD5;
   1360  1.1  itojun 		else
   1361  1.1  itojun 			cmd->mac_masks |= HIFN_MAC_CMD_ALG_SHA1;
   1362  1.1  itojun 
   1363  1.1  itojun 		if (sc->sc_sessions[session].hs_flags == 1) {
   1364  1.1  itojun 			cmd->mac_masks |= HIFN_MAC_CMD_NEW_KEY;
   1365  1.1  itojun 			bcopy(maccrd->crd_key, cmd->mac, maccrd->crd_klen >> 3);
   1366  1.1  itojun 			bzero(cmd->mac + (maccrd->crd_klen >> 3),
   1367  1.1  itojun 			    HIFN_MAC_KEY_LENGTH - (maccrd->crd_klen >> 3));
   1368  1.1  itojun 		}
   1369  1.1  itojun 
   1370  1.1  itojun 		cmd->mac_header_skip = maccrd->crd_skip;
   1371  1.1  itojun 		cmd->mac_process_len = maccrd->crd_len;
   1372  1.1  itojun 	}
   1373  1.1  itojun 
   1374  1.1  itojun 	if (sc->sc_sessions[session].hs_flags == 1)
   1375  1.1  itojun 		sc->sc_sessions[session].hs_flags = 2;
   1376  1.1  itojun 
   1377  1.1  itojun 	cmd->private_data = (u_long)crp;
   1378  1.1  itojun 	cmd->session_num = session;
   1379  1.1  itojun 	cmd->softc = sc;
   1380  1.1  itojun 
   1381  1.1  itojun 	if (hifn_crypto(sc, cmd) == 0)
   1382  1.1  itojun 		return (0);
   1383  1.1  itojun 
   1384  1.1  itojun 	err = ENOMEM;
   1385  1.1  itojun 
   1386  1.1  itojun errout:
   1387  1.1  itojun 	if (cmd != NULL)
   1388  1.1  itojun 		free(cmd, M_DEVBUF);
   1389  1.1  itojun 	if (err == EINVAL)
   1390  1.1  itojun 		hifnstats.hst_invalid++;
   1391  1.1  itojun 	else
   1392  1.1  itojun 		hifnstats.hst_nomem++;
   1393  1.1  itojun 	crp->crp_etype = err;
   1394  1.1  itojun 	crp->crp_callback(crp);
   1395  1.1  itojun 	return (0);
   1396  1.1  itojun }
   1397  1.1  itojun 
   1398  1.1  itojun void
   1399  1.1  itojun hifn_callback(sc, cmd, macbuf)
   1400  1.1  itojun 	struct hifn_softc *sc;
   1401  1.1  itojun 	struct hifn_command *cmd;
   1402  1.1  itojun 	u_int8_t *macbuf;
   1403  1.1  itojun {
   1404  1.1  itojun 	struct hifn_dma *dma = sc->sc_dma;
   1405  1.1  itojun 	struct cryptop *crp = (struct cryptop *)cmd->private_data;
   1406  1.1  itojun 	struct cryptodesc *crd;
   1407  1.1  itojun 	struct mbuf *m;
   1408  1.1  itojun 	int totlen;
   1409  1.1  itojun 
   1410  1.1  itojun 	if ((crp->crp_flags & CRYPTO_F_IMBUF) && (cmd->src_m != cmd->dst_m)) {
   1411  1.1  itojun 		m_freem(cmd->src_m);
   1412  1.1  itojun 		crp->crp_buf = (caddr_t)cmd->dst_m;
   1413  1.1  itojun 	}
   1414  1.1  itojun 
   1415  1.1  itojun 	if ((m = cmd->dst_m) != NULL) {
   1416  1.1  itojun 		totlen = cmd->src_l;
   1417  1.1  itojun 		hifnstats.hst_obytes += totlen;
   1418  1.1  itojun 		while (m) {
   1419  1.1  itojun 			if (totlen < m->m_len) {
   1420  1.1  itojun 				m->m_len = totlen;
   1421  1.1  itojun 				totlen = 0;
   1422  1.1  itojun 			} else
   1423  1.1  itojun 				totlen -= m->m_len;
   1424  1.1  itojun 			m = m->m_next;
   1425  1.1  itojun 			if (++dma->dstk == HIFN_D_DST_RSIZE)
   1426  1.1  itojun 				dma->dstk = 0;
   1427  1.1  itojun 			dma->dstu--;
   1428  1.1  itojun 		}
   1429  1.1  itojun 	} else {
   1430  1.1  itojun 		hifnstats.hst_obytes += dma->dstr[dma->dstk].l & HIFN_D_LENGTH;
   1431  1.1  itojun 		if (++dma->dstk == HIFN_D_DST_RSIZE)
   1432  1.1  itojun 			dma->dstk = 0;
   1433  1.1  itojun 		dma->dstu--;
   1434  1.1  itojun 	}
   1435  1.1  itojun 
   1436  1.1  itojun 	if ((cmd->base_masks & (HIFN_BASE_CMD_CRYPT | HIFN_BASE_CMD_DECODE)) ==
   1437  1.1  itojun 	    HIFN_BASE_CMD_CRYPT) {
   1438  1.1  itojun 		for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
   1439  1.1  itojun 			if (crd->crd_alg != CRYPTO_DES_CBC &&
   1440  1.1  itojun 			    crd->crd_alg != CRYPTO_3DES_CBC)
   1441  1.1  itojun 				continue;
   1442  1.1  itojun 			m_copydata((struct mbuf *)crp->crp_buf,
   1443  1.1  itojun 			    crd->crd_skip + crd->crd_len - HIFN_IV_LENGTH,
   1444  1.1  itojun 			    HIFN_IV_LENGTH,
   1445  1.1  itojun 			    cmd->softc->sc_sessions[cmd->session_num].hs_iv);
   1446  1.1  itojun 			break;
   1447  1.1  itojun 		}
   1448  1.1  itojun 	}
   1449  1.1  itojun 
   1450  1.1  itojun 	if (macbuf != NULL) {
   1451  1.1  itojun 		for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
   1452  1.1  itojun 			if (crd->crd_alg != CRYPTO_MD5_HMAC96 &&
   1453  1.1  itojun 			    crd->crd_alg != CRYPTO_SHA1_HMAC96)
   1454  1.1  itojun 				continue;
   1455  1.1  itojun 			m_copyback((struct mbuf *)crp->crp_buf,
   1456  1.1  itojun 			    crd->crd_inject, 12, macbuf);
   1457  1.1  itojun 			break;
   1458  1.1  itojun 		}
   1459  1.1  itojun 	}
   1460  1.1  itojun 
   1461  1.1  itojun 	free(cmd, M_DEVBUF);
   1462  1.1  itojun 	crypto_done(crp);
   1463  1.1  itojun }
   1464  1.1  itojun #endif
   1465