Home | History | Annotate | Line # | Download | only in pci
ubsec.c revision 1.22
      1  1.22    cegger /*	$NetBSD: ubsec.c,v 1.22 2009/05/06 10:34:33 cegger Exp $	*/
      2   1.1  jonathan /* $FreeBSD: src/sys/dev/ubsec/ubsec.c,v 1.6.2.6 2003/01/23 21:06:43 sam Exp $ */
      3   1.1  jonathan /*	$OpenBSD: ubsec.c,v 1.127 2003/06/04 14:04:58 jason Exp $	*/
      4   1.1  jonathan 
      5   1.1  jonathan /*
      6   1.1  jonathan  * Copyright (c) 2000 Jason L. Wright (jason (at) thought.net)
      7   1.1  jonathan  * Copyright (c) 2000 Theo de Raadt (deraadt (at) openbsd.org)
      8   1.1  jonathan  * Copyright (c) 2001 Patrik Lindergren (patrik (at) ipunplugged.com)
      9   1.5     perry  *
     10   1.1  jonathan  * Redistribution and use in source and binary forms, with or without
     11   1.1  jonathan  * modification, are permitted provided that the following conditions
     12   1.1  jonathan  * are met:
     13   1.1  jonathan  * 1. Redistributions of source code must retain the above copyright
     14   1.1  jonathan  *    notice, this list of conditions and the following disclaimer.
     15   1.1  jonathan  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  jonathan  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  jonathan  *    documentation and/or other materials provided with the distribution.
     18   1.1  jonathan  *
     19   1.1  jonathan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20   1.1  jonathan  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21   1.1  jonathan  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22   1.1  jonathan  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     23   1.1  jonathan  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     24   1.1  jonathan  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25   1.1  jonathan  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1  jonathan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     27   1.1  jonathan  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     28   1.1  jonathan  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  jonathan  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  jonathan  *
     31   1.1  jonathan  * Effort sponsored in part by the Defense Advanced Research Projects
     32   1.1  jonathan  * Agency (DARPA) and Air Force Research Laboratory, Air Force
     33   1.1  jonathan  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
     34   1.1  jonathan  *
     35   1.1  jonathan  */
     36   1.1  jonathan 
     37  1.14     lukem #include <sys/cdefs.h>
     38  1.22    cegger __KERNEL_RCSID(0, "$NetBSD: ubsec.c,v 1.22 2009/05/06 10:34:33 cegger Exp $");
     39  1.14     lukem 
     40   1.1  jonathan #undef UBSEC_DEBUG
     41   1.1  jonathan 
     42   1.1  jonathan /*
     43   1.1  jonathan  * uBsec 5[56]01, bcm580xx, bcm582x hardware crypto accelerator
     44   1.1  jonathan  */
     45   1.1  jonathan 
     46   1.1  jonathan #include <sys/param.h>
     47   1.1  jonathan #include <sys/systm.h>
     48   1.1  jonathan #include <sys/proc.h>
     49   1.1  jonathan #include <sys/endian.h>
     50   1.1  jonathan #ifdef __NetBSD__
     51   1.1  jonathan   #define letoh16 htole16
     52   1.1  jonathan   #define letoh32 htole32
     53   1.1  jonathan #define UBSEC_NO_RNG		/* until statistically tested */
     54   1.1  jonathan #endif
     55   1.1  jonathan #include <sys/errno.h>
     56   1.1  jonathan #include <sys/malloc.h>
     57   1.1  jonathan #include <sys/kernel.h>
     58   1.1  jonathan #include <sys/mbuf.h>
     59   1.1  jonathan #include <sys/device.h>
     60   1.1  jonathan #include <sys/queue.h>
     61   1.1  jonathan 
     62   1.1  jonathan #include <uvm/uvm_extern.h>
     63   1.1  jonathan 
     64   1.1  jonathan #include <opencrypto/cryptodev.h>
     65   1.8   thorpej #include <opencrypto/xform.h>
     66   1.1  jonathan #ifdef __OpenBSD__
     67   1.1  jonathan  #include <dev/rndvar.h>
     68   1.1  jonathan  #include <sys/md5k.h>
     69   1.1  jonathan #else
     70   1.1  jonathan  #include <sys/rnd.h>
     71   1.1  jonathan  #include <sys/md5.h>
     72   1.1  jonathan #endif
     73   1.1  jonathan #include <sys/sha1.h>
     74   1.1  jonathan 
     75   1.1  jonathan #include <dev/pci/pcireg.h>
     76   1.1  jonathan #include <dev/pci/pcivar.h>
     77   1.1  jonathan #include <dev/pci/pcidevs.h>
     78   1.1  jonathan 
     79   1.1  jonathan #include <dev/pci/ubsecreg.h>
     80   1.1  jonathan #include <dev/pci/ubsecvar.h>
     81   1.1  jonathan 
     82   1.1  jonathan /*
     83   1.1  jonathan  * Prototypes and count for the pci_device structure
     84   1.1  jonathan  */
     85  1.22    cegger static	int ubsec_probe(device_t, cfdata_t, void *);
     86  1.22    cegger static	void ubsec_attach(device_t, device_t, void *);
     87   1.1  jonathan static	void ubsec_reset_board(struct ubsec_softc *);
     88   1.1  jonathan static	void ubsec_init_board(struct ubsec_softc *);
     89   1.1  jonathan static	void ubsec_init_pciregs(struct pci_attach_args *pa);
     90   1.1  jonathan static	void ubsec_cleanchip(struct ubsec_softc *);
     91   1.1  jonathan static	void ubsec_totalreset(struct ubsec_softc *);
     92   1.1  jonathan static	int  ubsec_free_q(struct ubsec_softc*, struct ubsec_q *);
     93   1.1  jonathan 
     94   1.1  jonathan #ifdef __OpenBSD__
     95   1.1  jonathan struct cfattach ubsec_ca = {
     96   1.1  jonathan 	sizeof(struct ubsec_softc), ubsec_probe, ubsec_attach,
     97   1.1  jonathan };
     98   1.1  jonathan 
     99   1.1  jonathan struct cfdriver ubsec_cd = {
    100   1.1  jonathan 	0, "ubsec", DV_DULL
    101   1.1  jonathan };
    102   1.1  jonathan #else
    103   1.1  jonathan CFATTACH_DECL(ubsec, sizeof(struct ubsec_softc), ubsec_probe, ubsec_attach,
    104   1.1  jonathan 	      NULL, NULL);
    105   1.1  jonathan extern struct cfdriver ubsec_cd;
    106   1.1  jonathan #endif
    107   1.1  jonathan 
    108   1.1  jonathan /* patchable */
    109   1.1  jonathan #ifdef	UBSEC_DEBUG
    110   1.1  jonathan extern int ubsec_debug;
    111   1.1  jonathan int ubsec_debug=1;
    112   1.1  jonathan #endif
    113   1.1  jonathan 
    114   1.1  jonathan static	int	ubsec_intr(void *);
    115   1.1  jonathan static	int	ubsec_newsession(void*, u_int32_t *, struct cryptoini *);
    116   1.1  jonathan static	int	ubsec_freesession(void*, u_int64_t);
    117   1.1  jonathan static	int	ubsec_process(void*, struct cryptop *, int hint);
    118   1.1  jonathan static	void	ubsec_callback(struct ubsec_softc *, struct ubsec_q *);
    119   1.1  jonathan static	void	ubsec_feed(struct ubsec_softc *);
    120   1.1  jonathan static	void	ubsec_mcopy(struct mbuf *, struct mbuf *, int, int);
    121   1.1  jonathan static	void	ubsec_callback2(struct ubsec_softc *, struct ubsec_q2 *);
    122   1.1  jonathan static	void	ubsec_feed2(struct ubsec_softc *);
    123   1.1  jonathan #ifndef UBSEC_NO_RNG
    124   1.1  jonathan static	void	ubsec_rng(void *);
    125   1.1  jonathan #endif /* UBSEC_NO_RNG */
    126   1.1  jonathan static	int 	ubsec_dma_malloc(struct ubsec_softc *, bus_size_t,
    127   1.1  jonathan 				 struct ubsec_dma_alloc *, int);
    128   1.1  jonathan static	void	ubsec_dma_free(struct ubsec_softc *, struct ubsec_dma_alloc *);
    129   1.1  jonathan static	int	ubsec_dmamap_aligned(bus_dmamap_t);
    130   1.1  jonathan 
    131   1.1  jonathan static	int	ubsec_kprocess(void*, struct cryptkop *, int);
    132   1.1  jonathan static	int	ubsec_kprocess_modexp_sw(struct ubsec_softc *,
    133   1.1  jonathan 					 struct cryptkop *, int);
    134   1.1  jonathan static	int	ubsec_kprocess_modexp_hw(struct ubsec_softc *,
    135   1.1  jonathan 					 struct cryptkop *, int);
    136   1.1  jonathan static	int	ubsec_kprocess_rsapriv(struct ubsec_softc *,
    137   1.1  jonathan 				       struct cryptkop *, int);
    138   1.1  jonathan static	void	ubsec_kfree(struct ubsec_softc *, struct ubsec_q2 *);
    139   1.1  jonathan static	int	ubsec_ksigbits(struct crparam *);
    140   1.1  jonathan static	void	ubsec_kshift_r(u_int, u_int8_t *, u_int, u_int8_t *, u_int);
    141   1.1  jonathan static	void	ubsec_kshift_l(u_int, u_int8_t *, u_int, u_int8_t *, u_int);
    142   1.1  jonathan 
    143   1.1  jonathan #ifdef UBSEC_DEBUG
    144   1.1  jonathan static void	ubsec_dump_pb(volatile struct ubsec_pktbuf *);
    145   1.1  jonathan static void	ubsec_dump_mcr(struct ubsec_mcr *);
    146   1.1  jonathan static	void	ubsec_dump_ctx2(volatile struct ubsec_ctx_keyop *);
    147   1.1  jonathan #endif
    148   1.1  jonathan 
    149   1.1  jonathan #define	READ_REG(sc,r) \
    150   1.1  jonathan 	bus_space_read_4((sc)->sc_st, (sc)->sc_sh, (r))
    151   1.1  jonathan 
    152   1.1  jonathan #define WRITE_REG(sc,reg,val) \
    153   1.1  jonathan 	bus_space_write_4((sc)->sc_st, (sc)->sc_sh, reg, val)
    154   1.1  jonathan 
    155   1.1  jonathan #define	SWAP32(x) (x) = htole32(ntohl((x)))
    156   1.1  jonathan #ifndef HTOLE32
    157   1.1  jonathan  #define	HTOLE32(x) (x) = htole32(x)
    158   1.1  jonathan #endif
    159   1.1  jonathan 
    160   1.1  jonathan struct ubsec_stats ubsecstats;
    161   1.1  jonathan 
    162   1.1  jonathan /*
    163   1.5     perry  * ubsec_maxbatch controls the number of crypto ops to voluntarily
    164   1.1  jonathan  * collect into one submission to the hardware.  This batching happens
    165   1.1  jonathan  * when ops are dispatched from the crypto subsystem with a hint that
    166   1.1  jonathan  * more are to follow immediately.  These ops must also not be marked
    167   1.1  jonathan  * with a ``no delay'' flag.
    168   1.1  jonathan  */
    169   1.1  jonathan static	int ubsec_maxbatch = 1;
    170   1.1  jonathan #ifdef SYSCTL_INT
    171   1.1  jonathan SYSCTL_INT(_kern, OID_AUTO, ubsec_maxbatch, CTLFLAG_RW, &ubsec_maxbatch,
    172   1.1  jonathan 	    0, "Broadcom driver: max ops to batch w/o interrupt");
    173   1.1  jonathan #endif
    174   1.1  jonathan 
    175   1.1  jonathan /*
    176   1.1  jonathan  * ubsec_maxaggr controls the number of crypto ops to submit to the
    177   1.1  jonathan  * hardware as a unit.  This aggregation reduces the number of interrupts
    178   1.1  jonathan  * to the host at the expense of increased latency (for all but the last
    179   1.1  jonathan  * operation).  For network traffic setting this to one yields the highest
    180   1.1  jonathan  * performance but at the expense of more interrupt processing.
    181   1.1  jonathan  */
    182   1.1  jonathan static	int ubsec_maxaggr = 1;
    183   1.1  jonathan #ifdef SYSCTL_INT
    184   1.1  jonathan SYSCTL_INT(_kern, OID_AUTO, ubsec_maxaggr, CTLFLAG_RW, &ubsec_maxaggr,
    185   1.1  jonathan 	    0, "Broadcom driver: max ops to aggregate under one interrupt");
    186   1.1  jonathan #endif
    187   1.1  jonathan 
    188   1.4   thorpej static const struct ubsec_product {
    189   1.4   thorpej 	pci_vendor_id_t		ubsec_vendor;
    190   1.4   thorpej 	pci_product_id_t	ubsec_product;
    191   1.4   thorpej 	int			ubsec_flags;
    192   1.4   thorpej 	int			ubsec_statmask;
    193   1.4   thorpej 	const char		*ubsec_name;
    194   1.4   thorpej } ubsec_products[] = {
    195   1.4   thorpej 	{ PCI_VENDOR_BLUESTEEL,	PCI_PRODUCT_BLUESTEEL_5501,
    196   1.4   thorpej 	  0,
    197   1.4   thorpej 	  BS_STAT_MCR1_DONE | BS_STAT_DMAERR,
    198   1.4   thorpej 	  "Bluesteel 5501"
    199   1.4   thorpej 	},
    200   1.4   thorpej 	{ PCI_VENDOR_BLUESTEEL,	PCI_PRODUCT_BLUESTEEL_5601,
    201   1.4   thorpej 	  UBS_FLAGS_KEY | UBS_FLAGS_RNG,
    202   1.4   thorpej 	  BS_STAT_MCR1_DONE | BS_STAT_DMAERR,
    203   1.4   thorpej 	  "Bluesteel 5601"
    204   1.4   thorpej 	},
    205   1.4   thorpej 
    206   1.4   thorpej 	{ PCI_VENDOR_BROADCOM,	PCI_PRODUCT_BROADCOM_5801,
    207   1.4   thorpej 	  0,
    208   1.4   thorpej 	  BS_STAT_MCR1_DONE | BS_STAT_DMAERR,
    209   1.4   thorpej 	  "Broadcom BCM5801"
    210   1.4   thorpej 	},
    211   1.4   thorpej 
    212   1.4   thorpej 	{ PCI_VENDOR_BROADCOM,	PCI_PRODUCT_BROADCOM_5802,
    213   1.4   thorpej 	  UBS_FLAGS_KEY | UBS_FLAGS_RNG,
    214   1.4   thorpej 	  BS_STAT_MCR1_DONE | BS_STAT_DMAERR,
    215   1.4   thorpej 	  "Broadcom BCM5802"
    216   1.4   thorpej 	},
    217   1.4   thorpej 
    218   1.4   thorpej 	{ PCI_VENDOR_BROADCOM,	PCI_PRODUCT_BROADCOM_5805,
    219   1.4   thorpej 	  UBS_FLAGS_KEY | UBS_FLAGS_RNG,
    220   1.4   thorpej 	  BS_STAT_MCR1_DONE | BS_STAT_DMAERR,
    221   1.4   thorpej 	  "Broadcom BCM5805"
    222   1.4   thorpej 	},
    223   1.4   thorpej 
    224   1.4   thorpej 	{ PCI_VENDOR_BROADCOM,	PCI_PRODUCT_BROADCOM_5820,
    225   1.4   thorpej 	  UBS_FLAGS_KEY | UBS_FLAGS_RNG | UBS_FLAGS_LONGCTX |
    226   1.4   thorpej 	      UBS_FLAGS_HWNORM | UBS_FLAGS_BIGKEY,
    227   1.4   thorpej 	  BS_STAT_MCR1_DONE | BS_STAT_DMAERR,
    228   1.4   thorpej 	  "Broadcom BCM5820"
    229   1.4   thorpej 	},
    230   1.4   thorpej 
    231   1.4   thorpej 	{ PCI_VENDOR_BROADCOM,	PCI_PRODUCT_BROADCOM_5821,
    232   1.4   thorpej 	  UBS_FLAGS_KEY | UBS_FLAGS_RNG | UBS_FLAGS_LONGCTX |
    233   1.4   thorpej 	      UBS_FLAGS_HWNORM | UBS_FLAGS_BIGKEY,
    234   1.4   thorpej 	  BS_STAT_MCR1_DONE | BS_STAT_DMAERR |
    235   1.4   thorpej 	      BS_STAT_MCR1_ALLEMPTY | BS_STAT_MCR2_ALLEMPTY,
    236   1.4   thorpej 	  "Broadcom BCM5821"
    237   1.4   thorpej 	},
    238   1.4   thorpej 	{ PCI_VENDOR_SUN,	PCI_PRODUCT_SUN_SCA1K,
    239   1.4   thorpej 	  UBS_FLAGS_KEY | UBS_FLAGS_RNG | UBS_FLAGS_LONGCTX |
    240   1.4   thorpej 	      UBS_FLAGS_HWNORM | UBS_FLAGS_BIGKEY,
    241   1.4   thorpej 	  BS_STAT_MCR1_DONE | BS_STAT_DMAERR |
    242   1.4   thorpej 	      BS_STAT_MCR1_ALLEMPTY | BS_STAT_MCR2_ALLEMPTY,
    243   1.4   thorpej 	  "Sun Crypto Accelerator 1000"
    244   1.4   thorpej 	},
    245   1.4   thorpej 	{ PCI_VENDOR_SUN,	PCI_PRODUCT_SUN_5821,
    246   1.4   thorpej 	  UBS_FLAGS_KEY | UBS_FLAGS_RNG | UBS_FLAGS_LONGCTX |
    247   1.4   thorpej 	      UBS_FLAGS_HWNORM | UBS_FLAGS_BIGKEY,
    248   1.4   thorpej 	  BS_STAT_MCR1_DONE | BS_STAT_DMAERR |
    249   1.4   thorpej 	      BS_STAT_MCR1_ALLEMPTY | BS_STAT_MCR2_ALLEMPTY,
    250   1.4   thorpej 	  "Broadcom BCM5821 (Sun)"
    251   1.4   thorpej 	},
    252   1.4   thorpej 
    253   1.4   thorpej 	{ PCI_VENDOR_BROADCOM,	PCI_PRODUCT_BROADCOM_5822,
    254   1.4   thorpej 	  UBS_FLAGS_KEY | UBS_FLAGS_RNG | UBS_FLAGS_LONGCTX |
    255   1.4   thorpej 	      UBS_FLAGS_HWNORM | UBS_FLAGS_BIGKEY,
    256   1.4   thorpej 	  BS_STAT_MCR1_DONE | BS_STAT_DMAERR |
    257   1.4   thorpej 	      BS_STAT_MCR1_ALLEMPTY | BS_STAT_MCR2_ALLEMPTY,
    258   1.4   thorpej 	  "Broadcom BCM5822"
    259   1.4   thorpej 	},
    260   1.4   thorpej 
    261   1.4   thorpej 	{ PCI_VENDOR_BROADCOM,	PCI_PRODUCT_BROADCOM_5823,
    262   1.4   thorpej 	  UBS_FLAGS_KEY | UBS_FLAGS_RNG | UBS_FLAGS_LONGCTX |
    263   1.4   thorpej 	      UBS_FLAGS_HWNORM | UBS_FLAGS_BIGKEY,
    264   1.4   thorpej 	  BS_STAT_MCR1_DONE | BS_STAT_DMAERR |
    265   1.4   thorpej 	      BS_STAT_MCR1_ALLEMPTY | BS_STAT_MCR2_ALLEMPTY,
    266   1.4   thorpej 	  "Broadcom BCM5823"
    267   1.4   thorpej 	},
    268   1.4   thorpej 
    269   1.4   thorpej 	{ 0,			0,
    270   1.4   thorpej 	  0,
    271   1.4   thorpej 	  0,
    272   1.4   thorpej 	  NULL
    273   1.4   thorpej 	}
    274   1.4   thorpej };
    275   1.4   thorpej 
    276   1.4   thorpej static const struct ubsec_product *
    277   1.4   thorpej ubsec_lookup(const struct pci_attach_args *pa)
    278   1.4   thorpej {
    279   1.4   thorpej 	const struct ubsec_product *up;
    280   1.4   thorpej 
    281   1.4   thorpej 	for (up = ubsec_products; up->ubsec_name != NULL; up++) {
    282   1.4   thorpej 		if (PCI_VENDOR(pa->pa_id) == up->ubsec_vendor &&
    283   1.4   thorpej 		    PCI_PRODUCT(pa->pa_id) == up->ubsec_product)
    284   1.4   thorpej 			return (up);
    285   1.4   thorpej 	}
    286   1.4   thorpej 	return (NULL);
    287   1.4   thorpej }
    288   1.4   thorpej 
    289   1.1  jonathan static int
    290  1.22    cegger ubsec_probe(device_t parent, cfdata_t match, void *aux)
    291   1.1  jonathan {
    292   1.1  jonathan 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    293   1.1  jonathan 
    294   1.4   thorpej 	if (ubsec_lookup(pa) != NULL)
    295   1.1  jonathan 		return (1);
    296   1.1  jonathan 
    297   1.1  jonathan 	return (0);
    298   1.1  jonathan }
    299   1.1  jonathan 
    300   1.7   thorpej static void
    301  1.22    cegger ubsec_attach(device_t parent, device_t self, void *aux)
    302   1.1  jonathan {
    303   1.1  jonathan 	struct ubsec_softc *sc = (struct ubsec_softc *)self;
    304   1.1  jonathan 	struct pci_attach_args *pa = aux;
    305   1.4   thorpej 	const struct ubsec_product *up;
    306   1.1  jonathan 	pci_chipset_tag_t pc = pa->pa_pc;
    307   1.1  jonathan 	pci_intr_handle_t ih;
    308   1.1  jonathan 	const char *intrstr = NULL;
    309   1.1  jonathan 	struct ubsec_dma *dmap;
    310   1.1  jonathan 	u_int32_t cmd, i;
    311   1.1  jonathan 
    312   1.4   thorpej 	up = ubsec_lookup(pa);
    313   1.4   thorpej 	if (up == NULL) {
    314   1.4   thorpej 		printf("\n");
    315   1.4   thorpej 		panic("ubsec_attach: impossible");
    316   1.4   thorpej 	}
    317   1.4   thorpej 
    318   1.4   thorpej 	aprint_naive(": Crypto processor\n");
    319   1.4   thorpej 	aprint_normal(": %s, rev. %d\n", up->ubsec_name,
    320   1.4   thorpej 	    PCI_REVISION(pa->pa_class));
    321   1.4   thorpej 
    322   1.1  jonathan 	SIMPLEQ_INIT(&sc->sc_queue);
    323   1.1  jonathan 	SIMPLEQ_INIT(&sc->sc_qchip);
    324   1.1  jonathan 	SIMPLEQ_INIT(&sc->sc_queue2);
    325   1.1  jonathan 	SIMPLEQ_INIT(&sc->sc_qchip2);
    326   1.1  jonathan 	SIMPLEQ_INIT(&sc->sc_q2free);
    327   1.1  jonathan 
    328   1.4   thorpej 	sc->sc_flags = up->ubsec_flags;
    329   1.4   thorpej 	sc->sc_statmask = up->ubsec_statmask;
    330   1.1  jonathan 
    331   1.1  jonathan 	cmd = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    332   1.4   thorpej 	cmd |= PCI_COMMAND_MASTER_ENABLE;
    333   1.1  jonathan 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, cmd);
    334   1.1  jonathan 
    335   1.4   thorpej 	if (pci_mapreg_map(pa, BS_BAR, PCI_MAPREG_TYPE_MEM, 0,
    336   1.4   thorpej 	    &sc->sc_st, &sc->sc_sh, NULL, NULL)) {
    337  1.16    cegger 		aprint_error_dev(&sc->sc_dv, "can't find mem space");
    338   1.1  jonathan 		return;
    339   1.1  jonathan 	}
    340   1.1  jonathan 
    341   1.1  jonathan 	sc->sc_dmat = pa->pa_dmat;
    342   1.1  jonathan 
    343   1.1  jonathan 	if (pci_intr_map(pa, &ih)) {
    344  1.16    cegger 		aprint_error_dev(&sc->sc_dv, "couldn't map interrupt\n");
    345   1.1  jonathan 		return;
    346   1.1  jonathan 	}
    347   1.1  jonathan 	intrstr = pci_intr_string(pc, ih);
    348   1.4   thorpej 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, ubsec_intr, sc);
    349   1.1  jonathan 	if (sc->sc_ih == NULL) {
    350  1.16    cegger 		aprint_error_dev(&sc->sc_dv, "couldn't establish interrupt");
    351   1.1  jonathan 		if (intrstr != NULL)
    352   1.4   thorpej 			aprint_normal(" at %s", intrstr);
    353   1.4   thorpej 		aprint_normal("\n");
    354   1.1  jonathan 		return;
    355   1.1  jonathan 	}
    356  1.16    cegger 	aprint_normal_dev(&sc->sc_dv, "interrupting at %s\n", intrstr);
    357   1.1  jonathan 
    358   1.1  jonathan 	sc->sc_cid = crypto_get_driverid(0);
    359   1.1  jonathan 	if (sc->sc_cid < 0) {
    360  1.16    cegger 		aprint_error_dev(&sc->sc_dv, "couldn't get crypto driver id\n");
    361   1.1  jonathan 		pci_intr_disestablish(pc, sc->sc_ih);
    362   1.1  jonathan 		return;
    363   1.1  jonathan 	}
    364   1.1  jonathan 
    365   1.1  jonathan 	SIMPLEQ_INIT(&sc->sc_freequeue);
    366   1.1  jonathan 	dmap = sc->sc_dmaa;
    367   1.1  jonathan 	for (i = 0; i < UBS_MAX_NQUEUE; i++, dmap++) {
    368   1.1  jonathan 		struct ubsec_q *q;
    369   1.1  jonathan 
    370   1.1  jonathan 		q = (struct ubsec_q *)malloc(sizeof(struct ubsec_q),
    371   1.1  jonathan 		    M_DEVBUF, M_NOWAIT);
    372   1.1  jonathan 		if (q == NULL) {
    373  1.16    cegger 			aprint_error_dev(&sc->sc_dv, "can't allocate queue buffers\n");
    374   1.1  jonathan 			break;
    375   1.1  jonathan 		}
    376   1.1  jonathan 
    377   1.1  jonathan 		if (ubsec_dma_malloc(sc, sizeof(struct ubsec_dmachunk),
    378   1.1  jonathan 		    &dmap->d_alloc, 0)) {
    379  1.16    cegger 			aprint_error_dev(&sc->sc_dv, "can't allocate dma buffers\n");
    380   1.1  jonathan 			free(q, M_DEVBUF);
    381   1.1  jonathan 			break;
    382   1.1  jonathan 		}
    383   1.1  jonathan 		dmap->d_dma = (struct ubsec_dmachunk *)dmap->d_alloc.dma_vaddr;
    384   1.1  jonathan 
    385   1.1  jonathan 		q->q_dma = dmap;
    386   1.1  jonathan 		sc->sc_queuea[i] = q;
    387   1.1  jonathan 
    388   1.1  jonathan 		SIMPLEQ_INSERT_TAIL(&sc->sc_freequeue, q, q_next);
    389   1.1  jonathan 	}
    390   1.1  jonathan 
    391   1.1  jonathan 	crypto_register(sc->sc_cid, CRYPTO_3DES_CBC, 0, 0,
    392   1.1  jonathan 	    ubsec_newsession, ubsec_freesession, ubsec_process, sc);
    393   1.1  jonathan 	crypto_register(sc->sc_cid, CRYPTO_DES_CBC, 0, 0,
    394   1.1  jonathan 	    ubsec_newsession, ubsec_freesession, ubsec_process, sc);
    395  1.15       tls 	crypto_register(sc->sc_cid, CRYPTO_MD5_HMAC_96, 0, 0,
    396   1.1  jonathan 	    ubsec_newsession, ubsec_freesession, ubsec_process, sc);
    397  1.15       tls 	crypto_register(sc->sc_cid, CRYPTO_SHA1_HMAC_96, 0, 0,
    398   1.1  jonathan 	    ubsec_newsession, ubsec_freesession, ubsec_process, sc);
    399   1.1  jonathan 
    400   1.1  jonathan 	/*
    401   1.1  jonathan 	 * Reset Broadcom chip
    402   1.1  jonathan 	 */
    403   1.1  jonathan 	ubsec_reset_board(sc);
    404   1.1  jonathan 
    405   1.1  jonathan 	/*
    406   1.1  jonathan 	 * Init Broadcom specific PCI settings
    407   1.1  jonathan 	 */
    408   1.1  jonathan 	ubsec_init_pciregs(pa);
    409   1.1  jonathan 
    410   1.1  jonathan 	/*
    411   1.1  jonathan 	 * Init Broadcom chip
    412   1.1  jonathan 	 */
    413   1.1  jonathan 	ubsec_init_board(sc);
    414   1.1  jonathan 
    415   1.1  jonathan #ifndef UBSEC_NO_RNG
    416   1.1  jonathan 	if (sc->sc_flags & UBS_FLAGS_RNG) {
    417   1.1  jonathan 		sc->sc_statmask |= BS_STAT_MCR2_DONE;
    418   1.1  jonathan 
    419   1.1  jonathan 		if (ubsec_dma_malloc(sc, sizeof(struct ubsec_mcr),
    420   1.1  jonathan 		    &sc->sc_rng.rng_q.q_mcr, 0))
    421   1.1  jonathan 			goto skip_rng;
    422   1.1  jonathan 
    423   1.1  jonathan 		if (ubsec_dma_malloc(sc, sizeof(struct ubsec_ctx_rngbypass),
    424   1.1  jonathan 		    &sc->sc_rng.rng_q.q_ctx, 0)) {
    425   1.1  jonathan 			ubsec_dma_free(sc, &sc->sc_rng.rng_q.q_mcr);
    426   1.1  jonathan 			goto skip_rng;
    427   1.1  jonathan 		}
    428   1.1  jonathan 
    429   1.1  jonathan 		if (ubsec_dma_malloc(sc, sizeof(u_int32_t) *
    430   1.1  jonathan 		    UBSEC_RNG_BUFSIZ, &sc->sc_rng.rng_buf, 0)) {
    431   1.1  jonathan 			ubsec_dma_free(sc, &sc->sc_rng.rng_q.q_ctx);
    432   1.1  jonathan 			ubsec_dma_free(sc, &sc->sc_rng.rng_q.q_mcr);
    433   1.1  jonathan 			goto skip_rng;
    434   1.1  jonathan 		}
    435   1.1  jonathan 
    436   1.1  jonathan 		if (hz >= 100)
    437   1.1  jonathan 			sc->sc_rnghz = hz / 100;
    438   1.1  jonathan 		else
    439   1.1  jonathan 			sc->sc_rnghz = 1;
    440   1.1  jonathan #ifdef __OpenBSD__
    441   1.1  jonathan 		timeout_set(&sc->sc_rngto, ubsec_rng, sc);
    442   1.1  jonathan 		timeout_add(&sc->sc_rngto, sc->sc_rnghz);
    443   1.1  jonathan #else
    444  1.13        ad 		callout_init(&sc->sc_rngto, 0);
    445   1.1  jonathan 		callout_reset(&sc->sc_rngto, sc->sc_rnghz, ubsec_rng, sc);
    446   1.5     perry #endif
    447   1.4   thorpej  skip_rng:
    448   1.4   thorpej 		if (sc->sc_rnghz)
    449  1.16    cegger 			aprint_normal_dev(&sc->sc_dv, "random number generator enabled\n");
    450   1.4   thorpej 		else
    451  1.16    cegger 			aprint_error_dev(&sc->sc_dv, "WARNING: random number generator "
    452  1.16    cegger 			    "disabled\n");
    453   1.1  jonathan 	}
    454   1.1  jonathan #endif /* UBSEC_NO_RNG */
    455   1.1  jonathan 
    456   1.1  jonathan 	if (sc->sc_flags & UBS_FLAGS_KEY) {
    457   1.1  jonathan 		sc->sc_statmask |= BS_STAT_MCR2_DONE;
    458   1.1  jonathan 
    459   1.1  jonathan 		crypto_kregister(sc->sc_cid, CRK_MOD_EXP, 0,
    460   1.1  jonathan 				 ubsec_kprocess, sc);
    461   1.1  jonathan #if 0
    462   1.1  jonathan 		crypto_kregister(sc->sc_cid, CRK_MOD_EXP_CRT, 0,
    463   1.1  jonathan 				 ubsec_kprocess, sc);
    464   1.1  jonathan #endif
    465   1.1  jonathan 	}
    466   1.1  jonathan }
    467   1.1  jonathan 
    468   1.1  jonathan /*
    469   1.1  jonathan  * UBSEC Interrupt routine
    470   1.1  jonathan  */
    471   1.7   thorpej static int
    472   1.1  jonathan ubsec_intr(void *arg)
    473   1.1  jonathan {
    474   1.1  jonathan 	struct ubsec_softc *sc = arg;
    475   1.1  jonathan 	volatile u_int32_t stat;
    476   1.1  jonathan 	struct ubsec_q *q;
    477   1.1  jonathan 	struct ubsec_dma *dmap;
    478   1.1  jonathan 	int npkts = 0, i;
    479   1.1  jonathan 
    480   1.1  jonathan 	stat = READ_REG(sc, BS_STAT);
    481   1.1  jonathan 	stat &= sc->sc_statmask;
    482   1.1  jonathan 	if (stat == 0) {
    483   1.1  jonathan 		return (0);
    484   1.1  jonathan 	}
    485   1.1  jonathan 
    486   1.1  jonathan 	WRITE_REG(sc, BS_STAT, stat);		/* IACK */
    487   1.1  jonathan 
    488   1.1  jonathan 	/*
    489   1.1  jonathan 	 * Check to see if we have any packets waiting for us
    490   1.1  jonathan 	 */
    491   1.1  jonathan 	if ((stat & BS_STAT_MCR1_DONE)) {
    492   1.1  jonathan 		while (!SIMPLEQ_EMPTY(&sc->sc_qchip)) {
    493   1.1  jonathan 			q = SIMPLEQ_FIRST(&sc->sc_qchip);
    494   1.1  jonathan 			dmap = q->q_dma;
    495   1.1  jonathan 
    496   1.1  jonathan 			if ((dmap->d_dma->d_mcr.mcr_flags & htole16(UBS_MCR_DONE)) == 0)
    497   1.1  jonathan 				break;
    498   1.1  jonathan 
    499   1.1  jonathan 			q = SIMPLEQ_FIRST(&sc->sc_qchip);
    500   1.1  jonathan 			SIMPLEQ_REMOVE_HEAD(&sc->sc_qchip, /*q,*/ q_next);
    501   1.1  jonathan 
    502   1.1  jonathan 			npkts = q->q_nstacked_mcrs;
    503   1.1  jonathan 			sc->sc_nqchip -= 1+npkts;
    504   1.1  jonathan 			/*
    505   1.1  jonathan 			 * search for further sc_qchip ubsec_q's that share
    506   1.1  jonathan 			 * the same MCR, and complete them too, they must be
    507   1.1  jonathan 			 * at the top.
    508   1.1  jonathan 			 */
    509   1.1  jonathan 			for (i = 0; i < npkts; i++) {
    510   1.1  jonathan 				if(q->q_stacked_mcr[i])
    511   1.1  jonathan 					ubsec_callback(sc, q->q_stacked_mcr[i]);
    512   1.1  jonathan 				else
    513   1.1  jonathan 					break;
    514   1.1  jonathan 			}
    515   1.1  jonathan 			ubsec_callback(sc, q);
    516   1.1  jonathan 		}
    517   1.1  jonathan 
    518   1.1  jonathan 		/*
    519   1.1  jonathan 		 * Don't send any more packet to chip if there has been
    520   1.1  jonathan 		 * a DMAERR.
    521   1.1  jonathan 		 */
    522   1.1  jonathan 		if (!(stat & BS_STAT_DMAERR))
    523   1.1  jonathan 			ubsec_feed(sc);
    524   1.1  jonathan 	}
    525   1.1  jonathan 
    526   1.1  jonathan 	/*
    527   1.1  jonathan 	 * Check to see if we have any key setups/rng's waiting for us
    528   1.1  jonathan 	 */
    529   1.1  jonathan 	if ((sc->sc_flags & (UBS_FLAGS_KEY|UBS_FLAGS_RNG)) &&
    530   1.1  jonathan 	    (stat & BS_STAT_MCR2_DONE)) {
    531   1.1  jonathan 		struct ubsec_q2 *q2;
    532   1.1  jonathan 		struct ubsec_mcr *mcr;
    533   1.1  jonathan 
    534   1.1  jonathan 		while (!SIMPLEQ_EMPTY(&sc->sc_qchip2)) {
    535   1.1  jonathan 			q2 = SIMPLEQ_FIRST(&sc->sc_qchip2);
    536   1.1  jonathan 
    537   1.1  jonathan 			bus_dmamap_sync(sc->sc_dmat, q2->q_mcr.dma_map,
    538   1.1  jonathan 			    0, q2->q_mcr.dma_map->dm_mapsize,
    539   1.1  jonathan 			    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    540   1.1  jonathan 
    541   1.1  jonathan 			mcr = (struct ubsec_mcr *)q2->q_mcr.dma_vaddr;
    542   1.1  jonathan 			if ((mcr->mcr_flags & htole16(UBS_MCR_DONE)) == 0) {
    543   1.1  jonathan 				bus_dmamap_sync(sc->sc_dmat,
    544   1.1  jonathan 				    q2->q_mcr.dma_map, 0,
    545   1.1  jonathan 				    q2->q_mcr.dma_map->dm_mapsize,
    546   1.1  jonathan 				    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    547   1.1  jonathan 				break;
    548   1.1  jonathan 			}
    549   1.1  jonathan 			q2 = SIMPLEQ_FIRST(&sc->sc_qchip2);
    550   1.1  jonathan 			SIMPLEQ_REMOVE_HEAD(&sc->sc_qchip2, /*q2,*/ q_next);
    551   1.1  jonathan 			ubsec_callback2(sc, q2);
    552   1.1  jonathan 			/*
    553   1.1  jonathan 			 * Don't send any more packet to chip if there has been
    554   1.1  jonathan 			 * a DMAERR.
    555   1.1  jonathan 			 */
    556   1.1  jonathan 			if (!(stat & BS_STAT_DMAERR))
    557   1.1  jonathan 				ubsec_feed2(sc);
    558   1.1  jonathan 		}
    559   1.1  jonathan 	}
    560   1.1  jonathan 
    561   1.1  jonathan 	/*
    562   1.1  jonathan 	 * Check to see if we got any DMA Error
    563   1.1  jonathan 	 */
    564   1.1  jonathan 	if (stat & BS_STAT_DMAERR) {
    565   1.1  jonathan #ifdef UBSEC_DEBUG
    566   1.1  jonathan 		if (ubsec_debug) {
    567   1.1  jonathan 			volatile u_int32_t a = READ_REG(sc, BS_ERR);
    568   1.1  jonathan 
    569  1.16    cegger 			printf("%s: dmaerr %s@%08x\n", device_xname(&sc->sc_dv),
    570   1.1  jonathan 			    (a & BS_ERR_READ) ? "read" : "write",
    571   1.1  jonathan 			       a & BS_ERR_ADDR);
    572   1.1  jonathan 		}
    573   1.1  jonathan #endif /* UBSEC_DEBUG */
    574   1.1  jonathan 		ubsecstats.hst_dmaerr++;
    575   1.1  jonathan 		ubsec_totalreset(sc);
    576   1.1  jonathan 		ubsec_feed(sc);
    577   1.1  jonathan 	}
    578   1.1  jonathan 
    579   1.1  jonathan 	if (sc->sc_needwakeup) {		/* XXX check high watermark */
    580   1.6  christos 		int wkeup = sc->sc_needwakeup & (CRYPTO_SYMQ|CRYPTO_ASYMQ);
    581   1.1  jonathan #ifdef UBSEC_DEBUG
    582   1.1  jonathan 		if (ubsec_debug)
    583  1.16    cegger 			printf("%s: wakeup crypto (%x)\n", device_xname(&sc->sc_dv),
    584   1.1  jonathan 				sc->sc_needwakeup);
    585   1.1  jonathan #endif /* UBSEC_DEBUG */
    586   1.6  christos 		sc->sc_needwakeup &= ~wkeup;
    587   1.6  christos 		crypto_unblock(sc->sc_cid, wkeup);
    588   1.1  jonathan 	}
    589   1.1  jonathan 	return (1);
    590   1.1  jonathan }
    591   1.1  jonathan 
    592   1.1  jonathan /*
    593   1.1  jonathan  * ubsec_feed() - aggregate and post requests to chip
    594   1.1  jonathan  * OpenBSD comments:
    595   1.1  jonathan  *		  It is assumed that the caller set splnet()
    596   1.1  jonathan  */
    597   1.1  jonathan static void
    598   1.1  jonathan ubsec_feed(struct ubsec_softc *sc)
    599   1.1  jonathan {
    600   1.1  jonathan 	struct ubsec_q *q, *q2;
    601   1.1  jonathan 	int npkts, i;
    602   1.1  jonathan 	void *v;
    603   1.1  jonathan 	u_int32_t stat;
    604   1.1  jonathan #ifdef UBSEC_DEBUG
    605   1.1  jonathan 	static int max;
    606   1.1  jonathan #endif /* UBSEC_DEBUG */
    607   1.1  jonathan 
    608   1.1  jonathan 	npkts = sc->sc_nqueue;
    609   1.1  jonathan 	if (npkts > ubsecstats.hst_maxqueue)
    610   1.1  jonathan 		ubsecstats.hst_maxqueue = npkts;
    611   1.1  jonathan 	if (npkts < 2)
    612   1.1  jonathan 		goto feed1;
    613   1.1  jonathan 
    614   1.1  jonathan 	/*
    615   1.1  jonathan 	 * Decide how many ops to combine in a single MCR.  We cannot
    616   1.1  jonathan 	 * aggregate more than UBS_MAX_AGGR because this is the number
    617   1.1  jonathan 	 * of slots defined in the data structure.  Otherwise we clamp
    618   1.1  jonathan 	 * based on the tunable parameter ubsec_maxaggr.  Note that
    619   1.1  jonathan 	 * aggregation can happen in two ways: either by batching ops
    620   1.5     perry 	 * from above or because the h/w backs up and throttles us.
    621   1.1  jonathan 	 * Aggregating ops reduces the number of interrupts to the host
    622   1.1  jonathan 	 * but also (potentially) increases the latency for processing
    623   1.1  jonathan 	 * completed ops as we only get an interrupt when all aggregated
    624   1.1  jonathan 	 * ops have completed.
    625   1.1  jonathan 	 */
    626   1.1  jonathan 	if (npkts > UBS_MAX_AGGR)
    627   1.1  jonathan 		npkts = UBS_MAX_AGGR;
    628   1.1  jonathan 	if (npkts > ubsec_maxaggr)
    629   1.1  jonathan 		npkts = ubsec_maxaggr;
    630   1.1  jonathan 	if (npkts > ubsecstats.hst_maxbatch)
    631   1.1  jonathan 		ubsecstats.hst_maxbatch = npkts;
    632   1.1  jonathan 	if (npkts < 2)
    633   1.1  jonathan 		goto feed1;
    634   1.1  jonathan 	ubsecstats.hst_totbatch += npkts-1;
    635   1.1  jonathan 
    636   1.1  jonathan 	if ((stat = READ_REG(sc, BS_STAT)) & (BS_STAT_MCR1_FULL | BS_STAT_DMAERR)) {
    637   1.1  jonathan 		if (stat & BS_STAT_DMAERR) {
    638   1.1  jonathan 			ubsec_totalreset(sc);
    639   1.1  jonathan 			ubsecstats.hst_dmaerr++;
    640   1.1  jonathan 		} else {
    641   1.1  jonathan 			ubsecstats.hst_mcr1full++;
    642   1.1  jonathan 		}
    643   1.1  jonathan 		return;
    644   1.1  jonathan 	}
    645   1.1  jonathan 
    646   1.1  jonathan #ifdef UBSEC_DEBUG
    647   1.1  jonathan 	if (ubsec_debug)
    648   1.1  jonathan 	    printf("merging %d records\n", npkts);
    649   1.1  jonathan 	/* XXX temporary aggregation statistics reporting code */
    650   1.1  jonathan 	if (max < npkts) {
    651   1.1  jonathan 		max = npkts;
    652  1.16    cegger 		printf("%s: new max aggregate %d\n", device_xname(&sc->sc_dv), max);
    653   1.1  jonathan 	}
    654   1.1  jonathan #endif /* UBSEC_DEBUG */
    655   1.1  jonathan 
    656   1.1  jonathan 	q = SIMPLEQ_FIRST(&sc->sc_queue);
    657   1.1  jonathan 	SIMPLEQ_REMOVE_HEAD(&sc->sc_queue, /*q,*/ q_next);
    658   1.1  jonathan 	--sc->sc_nqueue;
    659   1.1  jonathan 
    660   1.1  jonathan 	bus_dmamap_sync(sc->sc_dmat, q->q_src_map,
    661   1.1  jonathan 	    0, q->q_src_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
    662   1.1  jonathan 	if (q->q_dst_map != NULL)
    663   1.1  jonathan 		bus_dmamap_sync(sc->sc_dmat, q->q_dst_map,
    664   1.1  jonathan 		    0, q->q_dst_map->dm_mapsize, BUS_DMASYNC_PREREAD);
    665   1.1  jonathan 
    666   1.1  jonathan 	q->q_nstacked_mcrs = npkts - 1;		/* Number of packets stacked */
    667   1.1  jonathan 
    668   1.1  jonathan 	for (i = 0; i < q->q_nstacked_mcrs; i++) {
    669   1.1  jonathan 		q2 = SIMPLEQ_FIRST(&sc->sc_queue);
    670   1.1  jonathan 		bus_dmamap_sync(sc->sc_dmat, q2->q_src_map,
    671   1.1  jonathan 		    0, q2->q_src_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
    672   1.1  jonathan 		if (q2->q_dst_map != NULL)
    673   1.1  jonathan 			bus_dmamap_sync(sc->sc_dmat, q2->q_dst_map,
    674   1.1  jonathan 			    0, q2->q_dst_map->dm_mapsize, BUS_DMASYNC_PREREAD);
    675   1.1  jonathan 		q2= SIMPLEQ_FIRST(&sc->sc_queue);
    676   1.1  jonathan 		SIMPLEQ_REMOVE_HEAD(&sc->sc_queue, /*q2,*/ q_next);
    677   1.1  jonathan 		--sc->sc_nqueue;
    678   1.1  jonathan 
    679   1.1  jonathan 		v = ((void *)&q2->q_dma->d_dma->d_mcr);
    680   1.1  jonathan 		v = (char*)v + (sizeof(struct ubsec_mcr) -
    681   1.1  jonathan 				 sizeof(struct ubsec_mcr_add));
    682  1.20   tsutsui 		memcpy(&q->q_dma->d_dma->d_mcradd[i], v, sizeof(struct ubsec_mcr_add));
    683   1.1  jonathan 		q->q_stacked_mcr[i] = q2;
    684   1.1  jonathan 	}
    685   1.1  jonathan 	q->q_dma->d_dma->d_mcr.mcr_pkts = htole16(npkts);
    686   1.1  jonathan 	SIMPLEQ_INSERT_TAIL(&sc->sc_qchip, q, q_next);
    687   1.1  jonathan 	sc->sc_nqchip += npkts;
    688   1.1  jonathan 	if (sc->sc_nqchip > ubsecstats.hst_maxqchip)
    689   1.1  jonathan 		ubsecstats.hst_maxqchip = sc->sc_nqchip;
    690   1.1  jonathan 	bus_dmamap_sync(sc->sc_dmat, q->q_dma->d_alloc.dma_map,
    691   1.1  jonathan 	    0, q->q_dma->d_alloc.dma_map->dm_mapsize,
    692   1.1  jonathan 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    693   1.1  jonathan 	WRITE_REG(sc, BS_MCR1, q->q_dma->d_alloc.dma_paddr +
    694   1.1  jonathan 	    offsetof(struct ubsec_dmachunk, d_mcr));
    695   1.1  jonathan 	return;
    696   1.1  jonathan 
    697   1.1  jonathan feed1:
    698   1.1  jonathan 	while (!SIMPLEQ_EMPTY(&sc->sc_queue)) {
    699   1.1  jonathan 		if ((stat = READ_REG(sc, BS_STAT)) & (BS_STAT_MCR1_FULL | BS_STAT_DMAERR)) {
    700   1.1  jonathan 			if (stat & BS_STAT_DMAERR) {
    701   1.1  jonathan 				ubsec_totalreset(sc);
    702   1.1  jonathan 				ubsecstats.hst_dmaerr++;
    703   1.1  jonathan 			} else {
    704   1.1  jonathan 				ubsecstats.hst_mcr1full++;
    705   1.1  jonathan 			}
    706   1.1  jonathan 			break;
    707   1.1  jonathan 		}
    708   1.1  jonathan 
    709   1.1  jonathan 		q = SIMPLEQ_FIRST(&sc->sc_queue);
    710   1.1  jonathan 
    711   1.1  jonathan 		bus_dmamap_sync(sc->sc_dmat, q->q_src_map,
    712   1.1  jonathan 		    0, q->q_src_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
    713   1.1  jonathan 		if (q->q_dst_map != NULL)
    714   1.1  jonathan 			bus_dmamap_sync(sc->sc_dmat, q->q_dst_map,
    715   1.1  jonathan 			    0, q->q_dst_map->dm_mapsize, BUS_DMASYNC_PREREAD);
    716   1.1  jonathan 		bus_dmamap_sync(sc->sc_dmat, q->q_dma->d_alloc.dma_map,
    717   1.1  jonathan 		    0, q->q_dma->d_alloc.dma_map->dm_mapsize,
    718   1.1  jonathan 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    719   1.1  jonathan 
    720   1.1  jonathan 		WRITE_REG(sc, BS_MCR1, q->q_dma->d_alloc.dma_paddr +
    721   1.1  jonathan 		    offsetof(struct ubsec_dmachunk, d_mcr));
    722   1.1  jonathan #ifdef UBSEC_DEBUG
    723   1.1  jonathan 		if (ubsec_debug)
    724   1.1  jonathan 			printf("feed: q->chip %p %08x stat %08x\n",
    725   1.1  jonathan  		    	       q, (u_int32_t)q->q_dma->d_alloc.dma_paddr,
    726   1.1  jonathan 			       stat);
    727   1.1  jonathan #endif /* UBSEC_DEBUG */
    728   1.1  jonathan 		q = SIMPLEQ_FIRST(&sc->sc_queue);
    729   1.1  jonathan 		SIMPLEQ_REMOVE_HEAD(&sc->sc_queue, /*q,*/ q_next);
    730   1.1  jonathan 		--sc->sc_nqueue;
    731   1.1  jonathan 		SIMPLEQ_INSERT_TAIL(&sc->sc_qchip, q, q_next);
    732   1.1  jonathan 		sc->sc_nqchip++;
    733   1.1  jonathan 	}
    734   1.1  jonathan 	if (sc->sc_nqchip > ubsecstats.hst_maxqchip)
    735   1.1  jonathan 		ubsecstats.hst_maxqchip = sc->sc_nqchip;
    736   1.1  jonathan }
    737   1.1  jonathan 
    738   1.1  jonathan /*
    739   1.1  jonathan  * Allocate a new 'session' and return an encoded session id.  'sidp'
    740   1.1  jonathan  * contains our registration id, and should contain an encoded session
    741   1.1  jonathan  * id on successful allocation.
    742   1.1  jonathan  */
    743   1.1  jonathan static int
    744   1.1  jonathan ubsec_newsession(void *arg, u_int32_t *sidp, struct cryptoini *cri)
    745   1.1  jonathan {
    746   1.1  jonathan 	struct cryptoini *c, *encini = NULL, *macini = NULL;
    747   1.1  jonathan 	struct ubsec_softc *sc;
    748   1.1  jonathan 	struct ubsec_session *ses = NULL;
    749   1.1  jonathan 	MD5_CTX md5ctx;
    750   1.1  jonathan 	SHA1_CTX sha1ctx;
    751   1.1  jonathan 	int i, sesn;
    752   1.1  jonathan 
    753   1.2  jonathan 	sc = arg;
    754   1.2  jonathan 	KASSERT(sc != NULL /*, ("ubsec_newsession: null softc")*/);
    755   1.2  jonathan 
    756   1.1  jonathan 	if (sidp == NULL || cri == NULL || sc == NULL)
    757   1.1  jonathan 		return (EINVAL);
    758   1.1  jonathan 
    759   1.1  jonathan 	for (c = cri; c != NULL; c = c->cri_next) {
    760  1.15       tls 		if (c->cri_alg == CRYPTO_MD5_HMAC_96 ||
    761  1.15       tls 		    c->cri_alg == CRYPTO_SHA1_HMAC_96) {
    762   1.1  jonathan 			if (macini)
    763   1.1  jonathan 				return (EINVAL);
    764   1.1  jonathan 			macini = c;
    765   1.1  jonathan 		} else if (c->cri_alg == CRYPTO_DES_CBC ||
    766   1.1  jonathan 		    c->cri_alg == CRYPTO_3DES_CBC) {
    767   1.1  jonathan 			if (encini)
    768   1.1  jonathan 				return (EINVAL);
    769   1.1  jonathan 			encini = c;
    770   1.1  jonathan 		} else
    771   1.1  jonathan 			return (EINVAL);
    772   1.1  jonathan 	}
    773   1.1  jonathan 	if (encini == NULL && macini == NULL)
    774   1.1  jonathan 		return (EINVAL);
    775   1.1  jonathan 
    776   1.1  jonathan 	if (sc->sc_sessions == NULL) {
    777   1.1  jonathan 		ses = sc->sc_sessions = (struct ubsec_session *)malloc(
    778   1.1  jonathan 		    sizeof(struct ubsec_session), M_DEVBUF, M_NOWAIT);
    779   1.1  jonathan 		if (ses == NULL)
    780   1.1  jonathan 			return (ENOMEM);
    781   1.1  jonathan 		sesn = 0;
    782   1.1  jonathan 		sc->sc_nsessions = 1;
    783   1.1  jonathan 	} else {
    784   1.1  jonathan 		for (sesn = 0; sesn < sc->sc_nsessions; sesn++) {
    785   1.1  jonathan 			if (sc->sc_sessions[sesn].ses_used == 0) {
    786   1.1  jonathan 				ses = &sc->sc_sessions[sesn];
    787   1.1  jonathan 				break;
    788   1.1  jonathan 			}
    789   1.1  jonathan 		}
    790   1.1  jonathan 
    791   1.1  jonathan 		if (ses == NULL) {
    792   1.1  jonathan 			sesn = sc->sc_nsessions;
    793   1.1  jonathan 			ses = (struct ubsec_session *)malloc((sesn + 1) *
    794   1.1  jonathan 			    sizeof(struct ubsec_session), M_DEVBUF, M_NOWAIT);
    795   1.1  jonathan 			if (ses == NULL)
    796   1.1  jonathan 				return (ENOMEM);
    797  1.20   tsutsui 			memcpy(ses, sc->sc_sessions, sesn *
    798   1.1  jonathan 			    sizeof(struct ubsec_session));
    799  1.18    cegger 			memset(sc->sc_sessions, 0, sesn *
    800   1.1  jonathan 			    sizeof(struct ubsec_session));
    801   1.1  jonathan 			free(sc->sc_sessions, M_DEVBUF);
    802   1.1  jonathan 			sc->sc_sessions = ses;
    803   1.1  jonathan 			ses = &sc->sc_sessions[sesn];
    804   1.1  jonathan 			sc->sc_nsessions++;
    805   1.1  jonathan 		}
    806   1.1  jonathan 	}
    807   1.1  jonathan 
    808  1.18    cegger 	memset(ses, 0, sizeof(struct ubsec_session));
    809   1.1  jonathan 	ses->ses_used = 1;
    810   1.1  jonathan 	if (encini) {
    811   1.1  jonathan 		/* get an IV, network byte order */
    812   1.1  jonathan #ifdef __NetBSD__
    813   1.1  jonathan 		rnd_extract_data(ses->ses_iv,
    814   1.1  jonathan 		    sizeof(ses->ses_iv), RND_EXTRACT_ANY);
    815   1.1  jonathan #else
    816   1.1  jonathan 		get_random_bytes(ses->ses_iv, sizeof(ses->ses_iv));
    817   1.1  jonathan #endif
    818   1.1  jonathan 
    819   1.1  jonathan 		/* Go ahead and compute key in ubsec's byte order */
    820   1.1  jonathan 		if (encini->cri_alg == CRYPTO_DES_CBC) {
    821  1.20   tsutsui 			memcpy(&ses->ses_deskey[0], encini->cri_key, 8);
    822  1.20   tsutsui 			memcpy(&ses->ses_deskey[2], encini->cri_key, 8);
    823  1.20   tsutsui 			memcpy(&ses->ses_deskey[4], encini->cri_key, 8);
    824   1.1  jonathan 		} else
    825  1.20   tsutsui 			memcpy(ses->ses_deskey, encini->cri_key, 24);
    826   1.1  jonathan 
    827   1.1  jonathan 		SWAP32(ses->ses_deskey[0]);
    828   1.1  jonathan 		SWAP32(ses->ses_deskey[1]);
    829   1.1  jonathan 		SWAP32(ses->ses_deskey[2]);
    830   1.1  jonathan 		SWAP32(ses->ses_deskey[3]);
    831   1.1  jonathan 		SWAP32(ses->ses_deskey[4]);
    832   1.1  jonathan 		SWAP32(ses->ses_deskey[5]);
    833   1.1  jonathan 	}
    834   1.1  jonathan 
    835   1.1  jonathan 	if (macini) {
    836   1.1  jonathan 		for (i = 0; i < macini->cri_klen / 8; i++)
    837   1.1  jonathan 			macini->cri_key[i] ^= HMAC_IPAD_VAL;
    838   1.1  jonathan 
    839  1.15       tls 		if (macini->cri_alg == CRYPTO_MD5_HMAC_96) {
    840   1.1  jonathan 			MD5Init(&md5ctx);
    841   1.1  jonathan 			MD5Update(&md5ctx, macini->cri_key,
    842   1.1  jonathan 			    macini->cri_klen / 8);
    843   1.1  jonathan 			MD5Update(&md5ctx, hmac_ipad_buffer,
    844   1.1  jonathan 			    HMAC_BLOCK_LEN - (macini->cri_klen / 8));
    845  1.20   tsutsui 			memcpy(ses->ses_hminner, md5ctx.state,
    846   1.1  jonathan 			    sizeof(md5ctx.state));
    847   1.1  jonathan 		} else {
    848   1.1  jonathan 			SHA1Init(&sha1ctx);
    849   1.1  jonathan 			SHA1Update(&sha1ctx, macini->cri_key,
    850   1.1  jonathan 			    macini->cri_klen / 8);
    851   1.1  jonathan 			SHA1Update(&sha1ctx, hmac_ipad_buffer,
    852   1.1  jonathan 			    HMAC_BLOCK_LEN - (macini->cri_klen / 8));
    853  1.20   tsutsui 			memcpy(ses->ses_hminner, sha1ctx.state,
    854   1.1  jonathan 			    sizeof(sha1ctx.state));
    855   1.1  jonathan 		}
    856   1.1  jonathan 
    857   1.1  jonathan 		for (i = 0; i < macini->cri_klen / 8; i++)
    858   1.1  jonathan 			macini->cri_key[i] ^= (HMAC_IPAD_VAL ^ HMAC_OPAD_VAL);
    859   1.1  jonathan 
    860  1.15       tls 		if (macini->cri_alg == CRYPTO_MD5_HMAC_96) {
    861   1.1  jonathan 			MD5Init(&md5ctx);
    862   1.1  jonathan 			MD5Update(&md5ctx, macini->cri_key,
    863   1.1  jonathan 			    macini->cri_klen / 8);
    864   1.1  jonathan 			MD5Update(&md5ctx, hmac_opad_buffer,
    865   1.1  jonathan 			    HMAC_BLOCK_LEN - (macini->cri_klen / 8));
    866  1.20   tsutsui 			memcpy(ses->ses_hmouter, md5ctx.state,
    867   1.1  jonathan 			    sizeof(md5ctx.state));
    868   1.1  jonathan 		} else {
    869   1.1  jonathan 			SHA1Init(&sha1ctx);
    870   1.1  jonathan 			SHA1Update(&sha1ctx, macini->cri_key,
    871   1.1  jonathan 			    macini->cri_klen / 8);
    872   1.1  jonathan 			SHA1Update(&sha1ctx, hmac_opad_buffer,
    873   1.1  jonathan 			    HMAC_BLOCK_LEN - (macini->cri_klen / 8));
    874  1.20   tsutsui 			memcpy(ses->ses_hmouter, sha1ctx.state,
    875   1.1  jonathan 			    sizeof(sha1ctx.state));
    876   1.1  jonathan 		}
    877   1.1  jonathan 
    878   1.1  jonathan 		for (i = 0; i < macini->cri_klen / 8; i++)
    879   1.1  jonathan 			macini->cri_key[i] ^= HMAC_OPAD_VAL;
    880   1.1  jonathan 	}
    881   1.1  jonathan 
    882   1.9   thorpej 	*sidp = UBSEC_SID(device_unit(&sc->sc_dv), sesn);
    883   1.1  jonathan 	return (0);
    884   1.1  jonathan }
    885   1.1  jonathan 
    886   1.1  jonathan /*
    887   1.1  jonathan  * Deallocate a session.
    888   1.1  jonathan  */
    889   1.1  jonathan static int
    890   1.1  jonathan ubsec_freesession(void *arg, u_int64_t tid)
    891   1.1  jonathan {
    892   1.1  jonathan 	struct ubsec_softc *sc;
    893   1.1  jonathan 	int session;
    894   1.1  jonathan 	u_int32_t sid = ((u_int32_t) tid) & 0xffffffff;
    895   1.1  jonathan 
    896   1.1  jonathan 	sc = arg;
    897   1.1  jonathan 	KASSERT(sc != NULL /*, ("ubsec_freesession: null softc")*/);
    898   1.1  jonathan 
    899   1.1  jonathan 	session = UBSEC_SESSION(sid);
    900   1.1  jonathan 	if (session >= sc->sc_nsessions)
    901   1.1  jonathan 		return (EINVAL);
    902   1.1  jonathan 
    903  1.18    cegger 	memset(&sc->sc_sessions[session], 0, sizeof(sc->sc_sessions[session]));
    904   1.1  jonathan 	return (0);
    905   1.1  jonathan }
    906   1.1  jonathan 
    907   1.1  jonathan #ifdef __FreeBSD__ /* Ugly gratuitous changes to bus_dma */
    908   1.1  jonathan static void
    909   1.1  jonathan ubsec_op_cb(void *arg, bus_dma_segment_t *seg, int nsegs, bus_size_t mapsize, int error)
    910   1.1  jonathan {
    911   1.1  jonathan 	struct ubsec_operand *op = arg;
    912   1.1  jonathan 
    913   1.5     perry 	KASSERT(nsegs <= UBS_MAX_SCATTER
    914   1.1  jonathan 		/*, ("Too many DMA segments returned when mapping operand")*/);
    915   1.1  jonathan #ifdef UBSEC_DEBUG
    916   1.1  jonathan 	if (ubsec_debug)
    917   1.1  jonathan 		printf("ubsec_op_cb: mapsize %u nsegs %d\n",
    918   1.1  jonathan 			(u_int) mapsize, nsegs);
    919   1.1  jonathan #endif
    920   1.1  jonathan 	op->mapsize = mapsize;
    921   1.1  jonathan 	op->nsegs = nsegs;
    922  1.20   tsutsui 	memcpy(op->segs, seg, nsegs * sizeof (seg[0]));
    923   1.1  jonathan }
    924   1.1  jonathan #endif
    925   1.1  jonathan 
    926   1.1  jonathan static int
    927   1.1  jonathan ubsec_process(void *arg, struct cryptop *crp, int hint)
    928   1.1  jonathan {
    929   1.1  jonathan 	struct ubsec_q *q = NULL;
    930   1.1  jonathan #ifdef	__OpenBSD__
    931   1.1  jonathan 	int card;
    932   1.1  jonathan #endif
    933   1.1  jonathan 	int err = 0, i, j, s, nicealign;
    934   1.1  jonathan 	struct ubsec_softc *sc;
    935   1.1  jonathan 	struct cryptodesc *crd1, *crd2, *maccrd, *enccrd;
    936   1.1  jonathan 	int encoffset = 0, macoffset = 0, cpskip, cpoffset;
    937   1.1  jonathan 	int sskip, dskip, stheend, dtheend;
    938   1.1  jonathan 	int16_t coffset;
    939   1.1  jonathan 	struct ubsec_session *ses;
    940   1.1  jonathan 	struct ubsec_pktctx ctx;
    941   1.1  jonathan 	struct ubsec_dma *dmap = NULL;
    942   1.1  jonathan 
    943   1.2  jonathan 	sc = arg;
    944   1.2  jonathan 	KASSERT(sc != NULL /*, ("ubsec_process: null softc")*/);
    945   1.2  jonathan 
    946   1.1  jonathan 	if (crp == NULL || crp->crp_callback == NULL || sc == NULL) {
    947   1.1  jonathan 		ubsecstats.hst_invalid++;
    948   1.1  jonathan 		return (EINVAL);
    949   1.1  jonathan 	}
    950   1.1  jonathan 	if (UBSEC_SESSION(crp->crp_sid) >= sc->sc_nsessions) {
    951   1.1  jonathan 		ubsecstats.hst_badsession++;
    952   1.1  jonathan 		return (EINVAL);
    953   1.1  jonathan 	}
    954   1.1  jonathan 
    955   1.1  jonathan 	s = splnet();
    956   1.1  jonathan 
    957   1.1  jonathan 	if (SIMPLEQ_EMPTY(&sc->sc_freequeue)) {
    958   1.1  jonathan 		ubsecstats.hst_queuefull++;
    959   1.1  jonathan 		sc->sc_needwakeup |= CRYPTO_SYMQ;
    960   1.1  jonathan 		splx(s);
    961   1.1  jonathan 		return(ERESTART);
    962   1.1  jonathan 	}
    963   1.1  jonathan 
    964   1.1  jonathan 	q = SIMPLEQ_FIRST(&sc->sc_freequeue);
    965   1.1  jonathan 	SIMPLEQ_REMOVE_HEAD(&sc->sc_freequeue, /*q,*/ q_next);
    966   1.1  jonathan 	splx(s);
    967   1.1  jonathan 
    968   1.1  jonathan 	dmap = q->q_dma; /* Save dma pointer */
    969  1.18    cegger 	memset(q, 0, sizeof(struct ubsec_q));
    970  1.18    cegger 	memset(&ctx, 0, sizeof(ctx));
    971   1.1  jonathan 
    972   1.1  jonathan 	q->q_sesn = UBSEC_SESSION(crp->crp_sid);
    973   1.1  jonathan 	q->q_dma = dmap;
    974   1.1  jonathan 	ses = &sc->sc_sessions[q->q_sesn];
    975   1.1  jonathan 
    976   1.1  jonathan 	if (crp->crp_flags & CRYPTO_F_IMBUF) {
    977   1.1  jonathan 		q->q_src_m = (struct mbuf *)crp->crp_buf;
    978   1.1  jonathan 		q->q_dst_m = (struct mbuf *)crp->crp_buf;
    979   1.1  jonathan 	} else if (crp->crp_flags & CRYPTO_F_IOV) {
    980   1.1  jonathan 		q->q_src_io = (struct uio *)crp->crp_buf;
    981   1.1  jonathan 		q->q_dst_io = (struct uio *)crp->crp_buf;
    982   1.1  jonathan 	} else {
    983   1.1  jonathan 		ubsecstats.hst_badflags++;
    984   1.1  jonathan 		err = EINVAL;
    985   1.1  jonathan 		goto errout;	/* XXX we don't handle contiguous blocks! */
    986   1.1  jonathan 	}
    987   1.1  jonathan 
    988  1.18    cegger 	memset(&dmap->d_dma->d_mcr, 0, sizeof(struct ubsec_mcr));
    989   1.1  jonathan 
    990   1.1  jonathan 	dmap->d_dma->d_mcr.mcr_pkts = htole16(1);
    991   1.1  jonathan 	dmap->d_dma->d_mcr.mcr_flags = 0;
    992   1.1  jonathan 	q->q_crp = crp;
    993   1.1  jonathan 
    994   1.1  jonathan 	crd1 = crp->crp_desc;
    995   1.1  jonathan 	if (crd1 == NULL) {
    996   1.1  jonathan 		ubsecstats.hst_nodesc++;
    997   1.1  jonathan 		err = EINVAL;
    998   1.1  jonathan 		goto errout;
    999   1.1  jonathan 	}
   1000   1.1  jonathan 	crd2 = crd1->crd_next;
   1001   1.1  jonathan 
   1002   1.1  jonathan 	if (crd2 == NULL) {
   1003  1.15       tls 		if (crd1->crd_alg == CRYPTO_MD5_HMAC_96 ||
   1004  1.15       tls 		    crd1->crd_alg == CRYPTO_SHA1_HMAC_96) {
   1005   1.1  jonathan 			maccrd = crd1;
   1006   1.1  jonathan 			enccrd = NULL;
   1007   1.1  jonathan 		} else if (crd1->crd_alg == CRYPTO_DES_CBC ||
   1008   1.1  jonathan 		    crd1->crd_alg == CRYPTO_3DES_CBC) {
   1009   1.1  jonathan 			maccrd = NULL;
   1010   1.1  jonathan 			enccrd = crd1;
   1011   1.1  jonathan 		} else {
   1012   1.1  jonathan 			ubsecstats.hst_badalg++;
   1013   1.1  jonathan 			err = EINVAL;
   1014   1.1  jonathan 			goto errout;
   1015   1.1  jonathan 		}
   1016   1.1  jonathan 	} else {
   1017  1.15       tls 		if ((crd1->crd_alg == CRYPTO_MD5_HMAC_96 ||
   1018  1.15       tls 		    crd1->crd_alg == CRYPTO_SHA1_HMAC_96) &&
   1019   1.1  jonathan 		    (crd2->crd_alg == CRYPTO_DES_CBC ||
   1020   1.1  jonathan 			crd2->crd_alg == CRYPTO_3DES_CBC) &&
   1021   1.1  jonathan 		    ((crd2->crd_flags & CRD_F_ENCRYPT) == 0)) {
   1022   1.1  jonathan 			maccrd = crd1;
   1023   1.1  jonathan 			enccrd = crd2;
   1024   1.1  jonathan 		} else if ((crd1->crd_alg == CRYPTO_DES_CBC ||
   1025   1.1  jonathan 		    crd1->crd_alg == CRYPTO_3DES_CBC) &&
   1026  1.15       tls 		    (crd2->crd_alg == CRYPTO_MD5_HMAC_96 ||
   1027  1.15       tls 			crd2->crd_alg == CRYPTO_SHA1_HMAC_96) &&
   1028   1.1  jonathan 		    (crd1->crd_flags & CRD_F_ENCRYPT)) {
   1029   1.1  jonathan 			enccrd = crd1;
   1030   1.1  jonathan 			maccrd = crd2;
   1031   1.1  jonathan 		} else {
   1032   1.1  jonathan 			/*
   1033   1.1  jonathan 			 * We cannot order the ubsec as requested
   1034   1.1  jonathan 			 */
   1035   1.1  jonathan 			ubsecstats.hst_badalg++;
   1036   1.1  jonathan 			err = EINVAL;
   1037   1.1  jonathan 			goto errout;
   1038   1.1  jonathan 		}
   1039   1.1  jonathan 	}
   1040   1.1  jonathan 
   1041   1.1  jonathan 	if (enccrd) {
   1042   1.1  jonathan 		encoffset = enccrd->crd_skip;
   1043   1.1  jonathan 		ctx.pc_flags |= htole16(UBS_PKTCTX_ENC_3DES);
   1044   1.1  jonathan 
   1045   1.1  jonathan 		if (enccrd->crd_flags & CRD_F_ENCRYPT) {
   1046   1.1  jonathan 			q->q_flags |= UBSEC_QFLAGS_COPYOUTIV;
   1047   1.1  jonathan 
   1048   1.1  jonathan 			if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
   1049  1.20   tsutsui 				memcpy(ctx.pc_iv, enccrd->crd_iv, 8);
   1050   1.1  jonathan 			else {
   1051   1.1  jonathan 				ctx.pc_iv[0] = ses->ses_iv[0];
   1052   1.1  jonathan 				ctx.pc_iv[1] = ses->ses_iv[1];
   1053   1.1  jonathan 			}
   1054   1.1  jonathan 
   1055   1.1  jonathan 			if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0) {
   1056   1.1  jonathan 				if (crp->crp_flags & CRYPTO_F_IMBUF)
   1057   1.1  jonathan 					m_copyback(q->q_src_m,
   1058   1.1  jonathan 					    enccrd->crd_inject,
   1059  1.12  christos 					    8, (void *)ctx.pc_iv);
   1060   1.1  jonathan 				else if (crp->crp_flags & CRYPTO_F_IOV)
   1061   1.1  jonathan 					cuio_copyback(q->q_src_io,
   1062   1.1  jonathan 					    enccrd->crd_inject,
   1063  1.12  christos 					    8, (void *)ctx.pc_iv);
   1064   1.1  jonathan 			}
   1065   1.1  jonathan 		} else {
   1066   1.1  jonathan 			ctx.pc_flags |= htole16(UBS_PKTCTX_INBOUND);
   1067   1.1  jonathan 
   1068   1.1  jonathan 			if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
   1069  1.20   tsutsui 				memcpy(ctx.pc_iv, enccrd->crd_iv, 8);
   1070   1.1  jonathan 			else if (crp->crp_flags & CRYPTO_F_IMBUF)
   1071   1.1  jonathan 				m_copydata(q->q_src_m, enccrd->crd_inject,
   1072  1.12  christos 				    8, (void *)ctx.pc_iv);
   1073   1.1  jonathan 			else if (crp->crp_flags & CRYPTO_F_IOV)
   1074   1.1  jonathan 				cuio_copydata(q->q_src_io,
   1075   1.1  jonathan 				    enccrd->crd_inject, 8,
   1076  1.12  christos 				    (void *)ctx.pc_iv);
   1077   1.1  jonathan 		}
   1078   1.1  jonathan 
   1079   1.1  jonathan 		ctx.pc_deskey[0] = ses->ses_deskey[0];
   1080   1.1  jonathan 		ctx.pc_deskey[1] = ses->ses_deskey[1];
   1081   1.1  jonathan 		ctx.pc_deskey[2] = ses->ses_deskey[2];
   1082   1.1  jonathan 		ctx.pc_deskey[3] = ses->ses_deskey[3];
   1083   1.1  jonathan 		ctx.pc_deskey[4] = ses->ses_deskey[4];
   1084   1.1  jonathan 		ctx.pc_deskey[5] = ses->ses_deskey[5];
   1085   1.1  jonathan 		SWAP32(ctx.pc_iv[0]);
   1086   1.1  jonathan 		SWAP32(ctx.pc_iv[1]);
   1087   1.1  jonathan 	}
   1088   1.1  jonathan 
   1089   1.1  jonathan 	if (maccrd) {
   1090   1.1  jonathan 		macoffset = maccrd->crd_skip;
   1091   1.1  jonathan 
   1092  1.15       tls 		if (maccrd->crd_alg == CRYPTO_MD5_HMAC_96)
   1093   1.1  jonathan 			ctx.pc_flags |= htole16(UBS_PKTCTX_AUTH_MD5);
   1094   1.1  jonathan 		else
   1095   1.1  jonathan 			ctx.pc_flags |= htole16(UBS_PKTCTX_AUTH_SHA1);
   1096   1.1  jonathan 
   1097   1.1  jonathan 		for (i = 0; i < 5; i++) {
   1098   1.1  jonathan 			ctx.pc_hminner[i] = ses->ses_hminner[i];
   1099   1.1  jonathan 			ctx.pc_hmouter[i] = ses->ses_hmouter[i];
   1100   1.1  jonathan 
   1101   1.1  jonathan 			HTOLE32(ctx.pc_hminner[i]);
   1102   1.1  jonathan 			HTOLE32(ctx.pc_hmouter[i]);
   1103   1.1  jonathan 		}
   1104   1.1  jonathan 	}
   1105   1.1  jonathan 
   1106   1.1  jonathan 	if (enccrd && maccrd) {
   1107   1.1  jonathan 		/*
   1108   1.1  jonathan 		 * ubsec cannot handle packets where the end of encryption
   1109   1.1  jonathan 		 * and authentication are not the same, or where the
   1110   1.1  jonathan 		 * encrypted part begins before the authenticated part.
   1111   1.1  jonathan 		 */
   1112   1.1  jonathan 		if ((encoffset + enccrd->crd_len) !=
   1113   1.1  jonathan 		    (macoffset + maccrd->crd_len)) {
   1114   1.1  jonathan 			ubsecstats.hst_lenmismatch++;
   1115   1.1  jonathan 			err = EINVAL;
   1116   1.1  jonathan 			goto errout;
   1117   1.1  jonathan 		}
   1118   1.1  jonathan 		if (enccrd->crd_skip < maccrd->crd_skip) {
   1119   1.1  jonathan 			ubsecstats.hst_skipmismatch++;
   1120   1.1  jonathan 			err = EINVAL;
   1121   1.1  jonathan 			goto errout;
   1122   1.1  jonathan 		}
   1123   1.1  jonathan 		sskip = maccrd->crd_skip;
   1124   1.1  jonathan 		cpskip = dskip = enccrd->crd_skip;
   1125   1.1  jonathan 		stheend = maccrd->crd_len;
   1126   1.1  jonathan 		dtheend = enccrd->crd_len;
   1127   1.1  jonathan 		coffset = enccrd->crd_skip - maccrd->crd_skip;
   1128   1.1  jonathan 		cpoffset = cpskip + dtheend;
   1129   1.1  jonathan #ifdef UBSEC_DEBUG
   1130   1.1  jonathan 		if (ubsec_debug) {
   1131   1.1  jonathan 			printf("mac: skip %d, len %d, inject %d\n",
   1132   1.1  jonathan 			       maccrd->crd_skip, maccrd->crd_len, maccrd->crd_inject);
   1133   1.1  jonathan 			printf("enc: skip %d, len %d, inject %d\n",
   1134   1.1  jonathan 			       enccrd->crd_skip, enccrd->crd_len, enccrd->crd_inject);
   1135   1.1  jonathan 			printf("src: skip %d, len %d\n", sskip, stheend);
   1136   1.1  jonathan 			printf("dst: skip %d, len %d\n", dskip, dtheend);
   1137   1.1  jonathan 			printf("ubs: coffset %d, pktlen %d, cpskip %d, cpoffset %d\n",
   1138   1.1  jonathan 			       coffset, stheend, cpskip, cpoffset);
   1139   1.1  jonathan 		}
   1140   1.1  jonathan #endif
   1141   1.1  jonathan 	} else {
   1142   1.1  jonathan 		cpskip = dskip = sskip = macoffset + encoffset;
   1143   1.1  jonathan 		dtheend = stheend = (enccrd)?enccrd->crd_len:maccrd->crd_len;
   1144   1.1  jonathan 		cpoffset = cpskip + dtheend;
   1145   1.1  jonathan 		coffset = 0;
   1146   1.1  jonathan 	}
   1147   1.1  jonathan 	ctx.pc_offset = htole16(coffset >> 2);
   1148   1.1  jonathan 
   1149   1.1  jonathan 	/* XXX FIXME: jonathan asks, what the heck's that 0xfff0?  */
   1150   1.1  jonathan 	if (bus_dmamap_create(sc->sc_dmat, 0xfff0, UBS_MAX_SCATTER,
   1151   1.1  jonathan 		0xfff0, 0, BUS_DMA_NOWAIT, &q->q_src_map) != 0) {
   1152   1.1  jonathan 		err = ENOMEM;
   1153   1.1  jonathan 		goto errout;
   1154   1.1  jonathan 	}
   1155   1.1  jonathan 	if (crp->crp_flags & CRYPTO_F_IMBUF) {
   1156   1.1  jonathan 		if (bus_dmamap_load_mbuf(sc->sc_dmat, q->q_src_map,
   1157   1.1  jonathan 		    q->q_src_m, BUS_DMA_NOWAIT) != 0) {
   1158   1.1  jonathan 			bus_dmamap_destroy(sc->sc_dmat, q->q_src_map);
   1159   1.1  jonathan 			q->q_src_map = NULL;
   1160   1.1  jonathan 			ubsecstats.hst_noload++;
   1161   1.1  jonathan 			err = ENOMEM;
   1162   1.1  jonathan 			goto errout;
   1163   1.1  jonathan 		}
   1164   1.1  jonathan 	} else if (crp->crp_flags & CRYPTO_F_IOV) {
   1165   1.1  jonathan 		if (bus_dmamap_load_uio(sc->sc_dmat, q->q_src_map,
   1166   1.1  jonathan 		    q->q_src_io, BUS_DMA_NOWAIT) != 0) {
   1167   1.1  jonathan 			bus_dmamap_destroy(sc->sc_dmat, q->q_src_map);
   1168   1.1  jonathan 			q->q_src_map = NULL;
   1169   1.1  jonathan 			ubsecstats.hst_noload++;
   1170   1.1  jonathan 			err = ENOMEM;
   1171   1.1  jonathan 			goto errout;
   1172   1.1  jonathan 		}
   1173   1.1  jonathan 	}
   1174   1.1  jonathan 	nicealign = ubsec_dmamap_aligned(q->q_src_map);
   1175   1.1  jonathan 
   1176   1.1  jonathan 	dmap->d_dma->d_mcr.mcr_pktlen = htole16(stheend);
   1177   1.1  jonathan 
   1178   1.1  jonathan #ifdef UBSEC_DEBUG
   1179   1.1  jonathan 	if (ubsec_debug)
   1180   1.1  jonathan 		printf("src skip: %d nicealign: %u\n", sskip, nicealign);
   1181   1.1  jonathan #endif
   1182   1.1  jonathan 	for (i = j = 0; i < q->q_src_map->dm_nsegs; i++) {
   1183   1.1  jonathan 		struct ubsec_pktbuf *pb;
   1184   1.1  jonathan 		bus_size_t packl = q->q_src_map->dm_segs[i].ds_len;
   1185   1.1  jonathan 		bus_addr_t packp = q->q_src_map->dm_segs[i].ds_addr;
   1186   1.1  jonathan 
   1187   1.1  jonathan 		if (sskip >= packl) {
   1188   1.1  jonathan 			sskip -= packl;
   1189   1.1  jonathan 			continue;
   1190   1.1  jonathan 		}
   1191   1.1  jonathan 
   1192   1.1  jonathan 		packl -= sskip;
   1193   1.1  jonathan 		packp += sskip;
   1194   1.1  jonathan 		sskip = 0;
   1195   1.1  jonathan 
   1196   1.1  jonathan 		if (packl > 0xfffc) {
   1197   1.1  jonathan 			err = EIO;
   1198   1.1  jonathan 			goto errout;
   1199   1.1  jonathan 		}
   1200   1.1  jonathan 
   1201   1.1  jonathan 		if (j == 0)
   1202   1.1  jonathan 			pb = &dmap->d_dma->d_mcr.mcr_ipktbuf;
   1203   1.1  jonathan 		else
   1204   1.1  jonathan 			pb = &dmap->d_dma->d_sbuf[j - 1];
   1205   1.1  jonathan 
   1206   1.1  jonathan 		pb->pb_addr = htole32(packp);
   1207   1.1  jonathan 
   1208   1.1  jonathan 		if (stheend) {
   1209   1.1  jonathan 			if (packl > stheend) {
   1210   1.1  jonathan 				pb->pb_len = htole32(stheend);
   1211   1.1  jonathan 				stheend = 0;
   1212   1.1  jonathan 			} else {
   1213   1.1  jonathan 				pb->pb_len = htole32(packl);
   1214   1.1  jonathan 				stheend -= packl;
   1215   1.1  jonathan 			}
   1216   1.1  jonathan 		} else
   1217   1.1  jonathan 			pb->pb_len = htole32(packl);
   1218   1.1  jonathan 
   1219   1.1  jonathan 		if ((i + 1) == q->q_src_map->dm_nsegs)
   1220   1.1  jonathan 			pb->pb_next = 0;
   1221   1.1  jonathan 		else
   1222   1.1  jonathan 			pb->pb_next = htole32(dmap->d_alloc.dma_paddr +
   1223   1.1  jonathan 			    offsetof(struct ubsec_dmachunk, d_sbuf[j]));
   1224   1.1  jonathan 		j++;
   1225   1.1  jonathan 	}
   1226   1.1  jonathan 
   1227   1.1  jonathan 	if (enccrd == NULL && maccrd != NULL) {
   1228   1.1  jonathan 		dmap->d_dma->d_mcr.mcr_opktbuf.pb_addr = 0;
   1229   1.1  jonathan 		dmap->d_dma->d_mcr.mcr_opktbuf.pb_len = 0;
   1230   1.1  jonathan 		dmap->d_dma->d_mcr.mcr_opktbuf.pb_next = htole32(dmap->d_alloc.dma_paddr +
   1231   1.1  jonathan 		    offsetof(struct ubsec_dmachunk, d_macbuf[0]));
   1232   1.1  jonathan #ifdef UBSEC_DEBUG
   1233   1.1  jonathan 		if (ubsec_debug)
   1234   1.1  jonathan 			printf("opkt: %x %x %x\n",
   1235   1.1  jonathan 	 		    dmap->d_dma->d_mcr.mcr_opktbuf.pb_addr,
   1236   1.1  jonathan 	 		    dmap->d_dma->d_mcr.mcr_opktbuf.pb_len,
   1237   1.1  jonathan 	 		    dmap->d_dma->d_mcr.mcr_opktbuf.pb_next);
   1238   1.1  jonathan 
   1239   1.1  jonathan #endif
   1240   1.1  jonathan 	} else {
   1241   1.1  jonathan 		if (crp->crp_flags & CRYPTO_F_IOV) {
   1242   1.1  jonathan 			if (!nicealign) {
   1243   1.1  jonathan 				ubsecstats.hst_iovmisaligned++;
   1244   1.1  jonathan 				err = EINVAL;
   1245   1.1  jonathan 				goto errout;
   1246   1.1  jonathan 			}
   1247   1.1  jonathan 			/* XXX: ``what the heck's that'' 0xfff0? */
   1248   1.1  jonathan 			if (bus_dmamap_create(sc->sc_dmat, 0xfff0,
   1249   1.1  jonathan 			    UBS_MAX_SCATTER, 0xfff0, 0, BUS_DMA_NOWAIT,
   1250   1.1  jonathan 			    &q->q_dst_map) != 0) {
   1251   1.1  jonathan 				ubsecstats.hst_nomap++;
   1252   1.1  jonathan 				err = ENOMEM;
   1253   1.1  jonathan 				goto errout;
   1254   1.1  jonathan 			}
   1255   1.1  jonathan 			if (bus_dmamap_load_uio(sc->sc_dmat, q->q_dst_map,
   1256   1.1  jonathan 			    q->q_dst_io, BUS_DMA_NOWAIT) != 0) {
   1257   1.1  jonathan 				bus_dmamap_destroy(sc->sc_dmat, q->q_dst_map);
   1258   1.1  jonathan 				q->q_dst_map = NULL;
   1259   1.1  jonathan 				ubsecstats.hst_noload++;
   1260   1.1  jonathan 				err = ENOMEM;
   1261   1.1  jonathan 				goto errout;
   1262   1.1  jonathan 			}
   1263   1.1  jonathan 		} else if (crp->crp_flags & CRYPTO_F_IMBUF) {
   1264   1.1  jonathan 			if (nicealign) {
   1265   1.1  jonathan 				q->q_dst_m = q->q_src_m;
   1266   1.1  jonathan 				q->q_dst_map = q->q_src_map;
   1267   1.1  jonathan 			} else {
   1268   1.1  jonathan 				int totlen, len;
   1269   1.1  jonathan 				struct mbuf *m, *top, **mp;
   1270   1.1  jonathan 
   1271   1.1  jonathan 				ubsecstats.hst_unaligned++;
   1272   1.1  jonathan 				totlen = q->q_src_map->dm_mapsize;
   1273   1.1  jonathan 				if (q->q_src_m->m_flags & M_PKTHDR) {
   1274   1.1  jonathan 					len = MHLEN;
   1275   1.1  jonathan 					MGETHDR(m, M_DONTWAIT, MT_DATA);
   1276   1.1  jonathan 					/*XXX FIXME: m_dup_pkthdr */
   1277   1.1  jonathan 					if (m && 1 /*!m_dup_pkthdr(m, q->q_src_m, M_DONTWAIT)*/) {
   1278   1.1  jonathan 						m_free(m);
   1279   1.1  jonathan 						m = NULL;
   1280   1.1  jonathan 					}
   1281   1.1  jonathan 				} else {
   1282   1.1  jonathan 					len = MLEN;
   1283   1.1  jonathan 					MGET(m, M_DONTWAIT, MT_DATA);
   1284   1.1  jonathan 				}
   1285   1.1  jonathan 				if (m == NULL) {
   1286   1.1  jonathan 					ubsecstats.hst_nombuf++;
   1287   1.1  jonathan 					err = sc->sc_nqueue ? ERESTART : ENOMEM;
   1288   1.1  jonathan 					goto errout;
   1289   1.1  jonathan 				}
   1290   1.1  jonathan 				if (len == MHLEN)
   1291   1.1  jonathan 				  /*XXX was M_DUP_PKTHDR*/
   1292   1.1  jonathan 				  M_COPY_PKTHDR(m, q->q_src_m);
   1293   1.1  jonathan 				if (totlen >= MINCLSIZE) {
   1294   1.1  jonathan 					MCLGET(m, M_DONTWAIT);
   1295   1.1  jonathan 					if ((m->m_flags & M_EXT) == 0) {
   1296   1.1  jonathan 						m_free(m);
   1297   1.1  jonathan 						ubsecstats.hst_nomcl++;
   1298   1.1  jonathan 						err = sc->sc_nqueue ? ERESTART : ENOMEM;
   1299   1.1  jonathan 						goto errout;
   1300   1.1  jonathan 					}
   1301   1.1  jonathan 					len = MCLBYTES;
   1302   1.1  jonathan 				}
   1303   1.1  jonathan 				m->m_len = len;
   1304   1.1  jonathan 				top = NULL;
   1305   1.1  jonathan 				mp = &top;
   1306   1.1  jonathan 
   1307   1.1  jonathan 				while (totlen > 0) {
   1308   1.1  jonathan 					if (top) {
   1309   1.1  jonathan 						MGET(m, M_DONTWAIT, MT_DATA);
   1310   1.1  jonathan 						if (m == NULL) {
   1311   1.1  jonathan 							m_freem(top);
   1312   1.1  jonathan 							ubsecstats.hst_nombuf++;
   1313   1.1  jonathan 							err = sc->sc_nqueue ? ERESTART : ENOMEM;
   1314   1.1  jonathan 							goto errout;
   1315   1.1  jonathan 						}
   1316   1.1  jonathan 						len = MLEN;
   1317   1.1  jonathan 					}
   1318   1.1  jonathan 					if (top && totlen >= MINCLSIZE) {
   1319   1.1  jonathan 						MCLGET(m, M_DONTWAIT);
   1320   1.1  jonathan 						if ((m->m_flags & M_EXT) == 0) {
   1321   1.1  jonathan 							*mp = m;
   1322   1.1  jonathan 							m_freem(top);
   1323   1.1  jonathan 							ubsecstats.hst_nomcl++;
   1324   1.1  jonathan 							err = sc->sc_nqueue ? ERESTART : ENOMEM;
   1325   1.1  jonathan 							goto errout;
   1326   1.1  jonathan 						}
   1327   1.1  jonathan 						len = MCLBYTES;
   1328   1.1  jonathan 					}
   1329   1.1  jonathan 					m->m_len = len = min(totlen, len);
   1330   1.1  jonathan 					totlen -= len;
   1331   1.1  jonathan 					*mp = m;
   1332   1.1  jonathan 					mp = &m->m_next;
   1333   1.1  jonathan 				}
   1334   1.1  jonathan 				q->q_dst_m = top;
   1335   1.1  jonathan 				ubsec_mcopy(q->q_src_m, q->q_dst_m,
   1336   1.1  jonathan 				    cpskip, cpoffset);
   1337   1.1  jonathan 				/* XXX again, what the heck is that 0xfff0? */
   1338   1.1  jonathan 				if (bus_dmamap_create(sc->sc_dmat, 0xfff0,
   1339   1.1  jonathan 				    UBS_MAX_SCATTER, 0xfff0, 0, BUS_DMA_NOWAIT,
   1340   1.1  jonathan 				    &q->q_dst_map) != 0) {
   1341   1.1  jonathan 					ubsecstats.hst_nomap++;
   1342   1.1  jonathan 					err = ENOMEM;
   1343   1.1  jonathan 					goto errout;
   1344   1.1  jonathan 				}
   1345   1.1  jonathan 				if (bus_dmamap_load_mbuf(sc->sc_dmat,
   1346   1.1  jonathan 				    q->q_dst_map, q->q_dst_m,
   1347   1.1  jonathan 				    BUS_DMA_NOWAIT) != 0) {
   1348   1.1  jonathan 					bus_dmamap_destroy(sc->sc_dmat,
   1349   1.1  jonathan 					q->q_dst_map);
   1350   1.1  jonathan 					q->q_dst_map = NULL;
   1351   1.1  jonathan 					ubsecstats.hst_noload++;
   1352   1.1  jonathan 					err = ENOMEM;
   1353   1.1  jonathan 					goto errout;
   1354   1.1  jonathan 				}
   1355   1.1  jonathan 			}
   1356   1.1  jonathan 		} else {
   1357   1.1  jonathan 			ubsecstats.hst_badflags++;
   1358   1.1  jonathan 			err = EINVAL;
   1359   1.1  jonathan 			goto errout;
   1360   1.1  jonathan 		}
   1361   1.1  jonathan 
   1362   1.1  jonathan #ifdef UBSEC_DEBUG
   1363   1.1  jonathan 		if (ubsec_debug)
   1364   1.1  jonathan 			printf("dst skip: %d\n", dskip);
   1365   1.1  jonathan #endif
   1366   1.1  jonathan 		for (i = j = 0; i < q->q_dst_map->dm_nsegs; i++) {
   1367   1.1  jonathan 			struct ubsec_pktbuf *pb;
   1368   1.1  jonathan 			bus_size_t packl = q->q_dst_map->dm_segs[i].ds_len;
   1369   1.1  jonathan 			bus_addr_t packp = q->q_dst_map->dm_segs[i].ds_addr;
   1370   1.1  jonathan 
   1371   1.1  jonathan 			if (dskip >= packl) {
   1372   1.1  jonathan 				dskip -= packl;
   1373   1.1  jonathan 				continue;
   1374   1.1  jonathan 			}
   1375   1.1  jonathan 
   1376   1.1  jonathan 			packl -= dskip;
   1377   1.1  jonathan 			packp += dskip;
   1378   1.1  jonathan 			dskip = 0;
   1379   1.1  jonathan 
   1380   1.1  jonathan 			if (packl > 0xfffc) {
   1381   1.1  jonathan 				err = EIO;
   1382   1.1  jonathan 				goto errout;
   1383   1.1  jonathan 			}
   1384   1.1  jonathan 
   1385   1.1  jonathan 			if (j == 0)
   1386   1.1  jonathan 				pb = &dmap->d_dma->d_mcr.mcr_opktbuf;
   1387   1.1  jonathan 			else
   1388   1.1  jonathan 				pb = &dmap->d_dma->d_dbuf[j - 1];
   1389   1.1  jonathan 
   1390   1.1  jonathan 			pb->pb_addr = htole32(packp);
   1391   1.1  jonathan 
   1392   1.1  jonathan 			if (dtheend) {
   1393   1.1  jonathan 				if (packl > dtheend) {
   1394   1.1  jonathan 					pb->pb_len = htole32(dtheend);
   1395   1.1  jonathan 					dtheend = 0;
   1396   1.1  jonathan 				} else {
   1397   1.1  jonathan 					pb->pb_len = htole32(packl);
   1398   1.1  jonathan 					dtheend -= packl;
   1399   1.1  jonathan 				}
   1400   1.1  jonathan 			} else
   1401   1.1  jonathan 				pb->pb_len = htole32(packl);
   1402   1.1  jonathan 
   1403   1.1  jonathan 			if ((i + 1) == q->q_dst_map->dm_nsegs) {
   1404   1.1  jonathan 				if (maccrd)
   1405   1.1  jonathan 					pb->pb_next = htole32(dmap->d_alloc.dma_paddr +
   1406   1.1  jonathan 					    offsetof(struct ubsec_dmachunk, d_macbuf[0]));
   1407   1.1  jonathan 				else
   1408   1.1  jonathan 					pb->pb_next = 0;
   1409   1.1  jonathan 			} else
   1410   1.1  jonathan 				pb->pb_next = htole32(dmap->d_alloc.dma_paddr +
   1411   1.1  jonathan 				    offsetof(struct ubsec_dmachunk, d_dbuf[j]));
   1412   1.1  jonathan 			j++;
   1413   1.1  jonathan 		}
   1414   1.1  jonathan 	}
   1415   1.1  jonathan 
   1416   1.1  jonathan 	dmap->d_dma->d_mcr.mcr_cmdctxp = htole32(dmap->d_alloc.dma_paddr +
   1417   1.1  jonathan 	    offsetof(struct ubsec_dmachunk, d_ctx));
   1418   1.1  jonathan 
   1419   1.1  jonathan 	if (sc->sc_flags & UBS_FLAGS_LONGCTX) {
   1420   1.1  jonathan 		struct ubsec_pktctx_long *ctxl;
   1421   1.1  jonathan 
   1422  1.12  christos 		ctxl = (struct ubsec_pktctx_long *)((char *)dmap->d_alloc.dma_vaddr +
   1423   1.1  jonathan 		    offsetof(struct ubsec_dmachunk, d_ctx));
   1424   1.5     perry 
   1425   1.1  jonathan 		/* transform small context into long context */
   1426   1.1  jonathan 		ctxl->pc_len = htole16(sizeof(struct ubsec_pktctx_long));
   1427   1.1  jonathan 		ctxl->pc_type = htole16(UBS_PKTCTX_TYPE_IPSEC);
   1428   1.1  jonathan 		ctxl->pc_flags = ctx.pc_flags;
   1429   1.1  jonathan 		ctxl->pc_offset = ctx.pc_offset;
   1430   1.1  jonathan 		for (i = 0; i < 6; i++)
   1431   1.1  jonathan 			ctxl->pc_deskey[i] = ctx.pc_deskey[i];
   1432   1.1  jonathan 		for (i = 0; i < 5; i++)
   1433   1.1  jonathan 			ctxl->pc_hminner[i] = ctx.pc_hminner[i];
   1434   1.1  jonathan 		for (i = 0; i < 5; i++)
   1435   1.5     perry 			ctxl->pc_hmouter[i] = ctx.pc_hmouter[i];
   1436   1.1  jonathan 		ctxl->pc_iv[0] = ctx.pc_iv[0];
   1437   1.1  jonathan 		ctxl->pc_iv[1] = ctx.pc_iv[1];
   1438   1.1  jonathan 	} else
   1439  1.12  christos 		memcpy((char *)dmap->d_alloc.dma_vaddr +
   1440  1.12  christos 		    offsetof(struct ubsec_dmachunk, d_ctx), &ctx,
   1441   1.1  jonathan 		    sizeof(struct ubsec_pktctx));
   1442   1.1  jonathan 
   1443   1.1  jonathan 	s = splnet();
   1444   1.1  jonathan 	SIMPLEQ_INSERT_TAIL(&sc->sc_queue, q, q_next);
   1445   1.1  jonathan 	sc->sc_nqueue++;
   1446   1.1  jonathan 	ubsecstats.hst_ipackets++;
   1447   1.1  jonathan 	ubsecstats.hst_ibytes += dmap->d_alloc.dma_map->dm_mapsize;
   1448   1.1  jonathan 	if ((hint & CRYPTO_HINT_MORE) == 0 || sc->sc_nqueue >= ubsec_maxbatch)
   1449   1.1  jonathan 		ubsec_feed(sc);
   1450   1.1  jonathan 	splx(s);
   1451   1.1  jonathan 	return (0);
   1452   1.1  jonathan 
   1453   1.1  jonathan errout:
   1454   1.1  jonathan 	if (q != NULL) {
   1455   1.1  jonathan 		if ((q->q_dst_m != NULL) && (q->q_src_m != q->q_dst_m))
   1456   1.1  jonathan 			m_freem(q->q_dst_m);
   1457   1.1  jonathan 
   1458   1.1  jonathan 		if (q->q_dst_map != NULL && q->q_dst_map != q->q_src_map) {
   1459   1.1  jonathan 			bus_dmamap_unload(sc->sc_dmat, q->q_dst_map);
   1460   1.1  jonathan 			bus_dmamap_destroy(sc->sc_dmat, q->q_dst_map);
   1461   1.1  jonathan 		}
   1462   1.1  jonathan 		if (q->q_src_map != NULL) {
   1463   1.1  jonathan 			bus_dmamap_unload(sc->sc_dmat, q->q_src_map);
   1464   1.1  jonathan 			bus_dmamap_destroy(sc->sc_dmat, q->q_src_map);
   1465   1.1  jonathan 		}
   1466   1.1  jonathan 
   1467   1.1  jonathan 		s = splnet();
   1468   1.1  jonathan 		SIMPLEQ_INSERT_TAIL(&sc->sc_freequeue, q, q_next);
   1469   1.1  jonathan 		splx(s);
   1470   1.1  jonathan 	}
   1471   1.1  jonathan #if 0 /* jonathan says: this openbsd code seems to be subsumed elsewhere */
   1472   1.1  jonathan 	if (err == EINVAL)
   1473   1.1  jonathan 		ubsecstats.hst_invalid++;
   1474   1.1  jonathan 	else
   1475   1.1  jonathan 		ubsecstats.hst_nomem++;
   1476   1.5     perry #endif
   1477   1.1  jonathan 	if (err != ERESTART) {
   1478   1.1  jonathan 		crp->crp_etype = err;
   1479   1.1  jonathan 		crypto_done(crp);
   1480   1.1  jonathan 	} else {
   1481   1.1  jonathan 		sc->sc_needwakeup |= CRYPTO_SYMQ;
   1482   1.1  jonathan 	}
   1483   1.1  jonathan 	return (err);
   1484   1.1  jonathan }
   1485   1.1  jonathan 
   1486   1.7   thorpej static void
   1487   1.7   thorpej ubsec_callback(struct ubsec_softc *sc, struct ubsec_q *q)
   1488   1.1  jonathan {
   1489   1.1  jonathan 	struct cryptop *crp = (struct cryptop *)q->q_crp;
   1490   1.1  jonathan 	struct cryptodesc *crd;
   1491   1.1  jonathan 	struct ubsec_dma *dmap = q->q_dma;
   1492   1.1  jonathan 
   1493   1.1  jonathan 	ubsecstats.hst_opackets++;
   1494   1.1  jonathan 	ubsecstats.hst_obytes += dmap->d_alloc.dma_size;
   1495   1.1  jonathan 
   1496   1.1  jonathan 	bus_dmamap_sync(sc->sc_dmat, dmap->d_alloc.dma_map, 0,
   1497   1.1  jonathan 	    dmap->d_alloc.dma_map->dm_mapsize,
   1498   1.1  jonathan 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1499   1.1  jonathan 	if (q->q_dst_map != NULL && q->q_dst_map != q->q_src_map) {
   1500   1.1  jonathan 		bus_dmamap_sync(sc->sc_dmat, q->q_dst_map,
   1501   1.1  jonathan 		    0, q->q_dst_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1502   1.1  jonathan 		bus_dmamap_unload(sc->sc_dmat, q->q_dst_map);
   1503   1.1  jonathan 		bus_dmamap_destroy(sc->sc_dmat, q->q_dst_map);
   1504   1.1  jonathan 	}
   1505   1.1  jonathan 	bus_dmamap_sync(sc->sc_dmat, q->q_src_map,
   1506   1.1  jonathan 	    0, q->q_src_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1507   1.1  jonathan 	bus_dmamap_unload(sc->sc_dmat, q->q_src_map);
   1508   1.1  jonathan 	bus_dmamap_destroy(sc->sc_dmat, q->q_src_map);
   1509   1.1  jonathan 
   1510   1.1  jonathan 	if ((crp->crp_flags & CRYPTO_F_IMBUF) && (q->q_src_m != q->q_dst_m)) {
   1511   1.1  jonathan 		m_freem(q->q_src_m);
   1512  1.12  christos 		crp->crp_buf = (void *)q->q_dst_m;
   1513   1.1  jonathan 	}
   1514   1.1  jonathan 
   1515   1.1  jonathan 	/* copy out IV for future use */
   1516   1.1  jonathan 	if (q->q_flags & UBSEC_QFLAGS_COPYOUTIV) {
   1517   1.1  jonathan 		for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
   1518   1.1  jonathan 			if (crd->crd_alg != CRYPTO_DES_CBC &&
   1519   1.1  jonathan 			    crd->crd_alg != CRYPTO_3DES_CBC)
   1520   1.1  jonathan 				continue;
   1521   1.1  jonathan 			if (crp->crp_flags & CRYPTO_F_IMBUF)
   1522   1.1  jonathan 				m_copydata((struct mbuf *)crp->crp_buf,
   1523   1.1  jonathan 				    crd->crd_skip + crd->crd_len - 8, 8,
   1524  1.12  christos 				    (void *)sc->sc_sessions[q->q_sesn].ses_iv);
   1525   1.1  jonathan 			else if (crp->crp_flags & CRYPTO_F_IOV) {
   1526   1.1  jonathan 				cuio_copydata((struct uio *)crp->crp_buf,
   1527   1.1  jonathan 				    crd->crd_skip + crd->crd_len - 8, 8,
   1528  1.12  christos 				    (void *)sc->sc_sessions[q->q_sesn].ses_iv);
   1529   1.1  jonathan 			}
   1530   1.1  jonathan 			break;
   1531   1.1  jonathan 		}
   1532   1.1  jonathan 	}
   1533   1.1  jonathan 
   1534   1.1  jonathan 	for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
   1535  1.15       tls 		if (crd->crd_alg != CRYPTO_MD5_HMAC_96 &&
   1536  1.15       tls 		    crd->crd_alg != CRYPTO_SHA1_HMAC_96)
   1537   1.1  jonathan 			continue;
   1538   1.1  jonathan 		if (crp->crp_flags & CRYPTO_F_IMBUF)
   1539   1.1  jonathan 			m_copyback((struct mbuf *)crp->crp_buf,
   1540   1.1  jonathan 			    crd->crd_inject, 12,
   1541  1.12  christos 			    (void *)dmap->d_dma->d_macbuf);
   1542   1.1  jonathan 		else if (crp->crp_flags & CRYPTO_F_IOV && crp->crp_mac)
   1543  1.12  christos 			bcopy((void *)dmap->d_dma->d_macbuf,
   1544   1.1  jonathan 			    crp->crp_mac, 12);
   1545   1.1  jonathan 		break;
   1546   1.1  jonathan 	}
   1547   1.1  jonathan 	SIMPLEQ_INSERT_TAIL(&sc->sc_freequeue, q, q_next);
   1548   1.1  jonathan 	crypto_done(crp);
   1549   1.1  jonathan }
   1550   1.1  jonathan 
   1551   1.1  jonathan static void
   1552   1.1  jonathan ubsec_mcopy(struct mbuf *srcm, struct mbuf *dstm, int hoffset, int toffset)
   1553   1.1  jonathan {
   1554   1.1  jonathan 	int i, j, dlen, slen;
   1555  1.12  christos 	char *dptr, *sptr;
   1556   1.1  jonathan 
   1557   1.1  jonathan 	j = 0;
   1558   1.1  jonathan 	sptr = srcm->m_data;
   1559   1.1  jonathan 	slen = srcm->m_len;
   1560   1.1  jonathan 	dptr = dstm->m_data;
   1561   1.1  jonathan 	dlen = dstm->m_len;
   1562   1.1  jonathan 
   1563   1.1  jonathan 	while (1) {
   1564   1.1  jonathan 		for (i = 0; i < min(slen, dlen); i++) {
   1565   1.1  jonathan 			if (j < hoffset || j >= toffset)
   1566   1.1  jonathan 				*dptr++ = *sptr++;
   1567   1.1  jonathan 			slen--;
   1568   1.1  jonathan 			dlen--;
   1569   1.1  jonathan 			j++;
   1570   1.1  jonathan 		}
   1571   1.1  jonathan 		if (slen == 0) {
   1572   1.1  jonathan 			srcm = srcm->m_next;
   1573   1.1  jonathan 			if (srcm == NULL)
   1574   1.1  jonathan 				return;
   1575   1.1  jonathan 			sptr = srcm->m_data;
   1576   1.1  jonathan 			slen = srcm->m_len;
   1577   1.1  jonathan 		}
   1578   1.1  jonathan 		if (dlen == 0) {
   1579   1.1  jonathan 			dstm = dstm->m_next;
   1580   1.1  jonathan 			if (dstm == NULL)
   1581   1.1  jonathan 				return;
   1582   1.1  jonathan 			dptr = dstm->m_data;
   1583   1.1  jonathan 			dlen = dstm->m_len;
   1584   1.1  jonathan 		}
   1585   1.1  jonathan 	}
   1586   1.1  jonathan }
   1587   1.1  jonathan 
   1588   1.1  jonathan /*
   1589   1.1  jonathan  * feed the key generator, must be called at splnet() or higher.
   1590   1.1  jonathan  */
   1591   1.1  jonathan static void
   1592   1.1  jonathan ubsec_feed2(struct ubsec_softc *sc)
   1593   1.1  jonathan {
   1594   1.1  jonathan 	struct ubsec_q2 *q;
   1595   1.1  jonathan 
   1596   1.1  jonathan 	while (!SIMPLEQ_EMPTY(&sc->sc_queue2)) {
   1597   1.1  jonathan 		if (READ_REG(sc, BS_STAT) & BS_STAT_MCR2_FULL)
   1598   1.1  jonathan 			break;
   1599   1.1  jonathan 		q = SIMPLEQ_FIRST(&sc->sc_queue2);
   1600   1.1  jonathan 
   1601   1.1  jonathan 		bus_dmamap_sync(sc->sc_dmat, q->q_mcr.dma_map, 0,
   1602   1.1  jonathan 		    q->q_mcr.dma_map->dm_mapsize,
   1603   1.1  jonathan 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1604   1.1  jonathan 		bus_dmamap_sync(sc->sc_dmat, q->q_ctx.dma_map, 0,
   1605   1.1  jonathan 		    q->q_ctx.dma_map->dm_mapsize,
   1606   1.1  jonathan 		    BUS_DMASYNC_PREWRITE);
   1607   1.1  jonathan 
   1608   1.1  jonathan 		WRITE_REG(sc, BS_MCR2, q->q_mcr.dma_paddr);
   1609   1.1  jonathan 		q = SIMPLEQ_FIRST(&sc->sc_queue2);
   1610   1.1  jonathan 		SIMPLEQ_REMOVE_HEAD(&sc->sc_queue2, /*q,*/ q_next);
   1611   1.1  jonathan 		--sc->sc_nqueue2;
   1612   1.1  jonathan 		SIMPLEQ_INSERT_TAIL(&sc->sc_qchip2, q, q_next);
   1613   1.1  jonathan 	}
   1614   1.1  jonathan }
   1615   1.1  jonathan 
   1616   1.1  jonathan /*
   1617   1.1  jonathan  * Callback for handling random numbers
   1618   1.1  jonathan  */
   1619   1.1  jonathan static void
   1620   1.1  jonathan ubsec_callback2(struct ubsec_softc *sc, struct ubsec_q2 *q)
   1621   1.1  jonathan {
   1622   1.1  jonathan 	struct cryptkop *krp;
   1623   1.1  jonathan 	struct ubsec_ctx_keyop *ctx;
   1624   1.1  jonathan 
   1625   1.1  jonathan 	ctx = (struct ubsec_ctx_keyop *)q->q_ctx.dma_vaddr;
   1626   1.1  jonathan 	bus_dmamap_sync(sc->sc_dmat, q->q_ctx.dma_map, 0,
   1627   1.1  jonathan 	    q->q_ctx.dma_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1628   1.1  jonathan 
   1629   1.1  jonathan 	switch (q->q_type) {
   1630   1.1  jonathan #ifndef UBSEC_NO_RNG
   1631   1.1  jonathan 	case UBS_CTXOP_RNGSHA1:
   1632   1.1  jonathan 	case UBS_CTXOP_RNGBYPASS: {
   1633   1.1  jonathan 		struct ubsec_q2_rng *rng = (struct ubsec_q2_rng *)q;
   1634   1.1  jonathan 		u_int32_t *p;
   1635   1.1  jonathan 		int i;
   1636   1.1  jonathan 
   1637   1.1  jonathan 		bus_dmamap_sync(sc->sc_dmat, rng->rng_buf.dma_map, 0,
   1638   1.1  jonathan 		    rng->rng_buf.dma_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1639   1.1  jonathan 		p = (u_int32_t *)rng->rng_buf.dma_vaddr;
   1640   1.1  jonathan #ifndef __NetBSD__
   1641   1.1  jonathan 		for (i = 0; i < UBSEC_RNG_BUFSIZ; p++, i++)
   1642   1.1  jonathan 			add_true_randomness(letoh32(*p));
   1643   1.1  jonathan 		rng->rng_used = 0;
   1644   1.1  jonathan #else
   1645   1.1  jonathan 		/* XXX NetBSD rnd subsystem too weak */
   1646   1.1  jonathan 		i = 0; (void)i;	/* shut off gcc warnings */
   1647   1.1  jonathan #endif
   1648   1.1  jonathan #ifdef __OpenBSD__
   1649   1.1  jonathan 		timeout_add(&sc->sc_rngto, sc->sc_rnghz);
   1650   1.1  jonathan #else
   1651   1.1  jonathan 		callout_reset(&sc->sc_rngto, sc->sc_rnghz, ubsec_rng, sc);
   1652   1.1  jonathan #endif
   1653   1.1  jonathan 		break;
   1654   1.1  jonathan 	}
   1655   1.1  jonathan #endif
   1656   1.1  jonathan 	case UBS_CTXOP_MODEXP: {
   1657   1.1  jonathan 		struct ubsec_q2_modexp *me = (struct ubsec_q2_modexp *)q;
   1658   1.1  jonathan 		u_int rlen, clen;
   1659   1.1  jonathan 
   1660   1.1  jonathan 		krp = me->me_krp;
   1661   1.1  jonathan 		rlen = (me->me_modbits + 7) / 8;
   1662   1.1  jonathan 		clen = (krp->krp_param[krp->krp_iparams].crp_nbits + 7) / 8;
   1663   1.1  jonathan 
   1664   1.1  jonathan 		bus_dmamap_sync(sc->sc_dmat, me->me_M.dma_map,
   1665   1.1  jonathan 		    0, me->me_M.dma_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1666   1.1  jonathan 		bus_dmamap_sync(sc->sc_dmat, me->me_E.dma_map,
   1667   1.1  jonathan 		    0, me->me_E.dma_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1668   1.1  jonathan 		bus_dmamap_sync(sc->sc_dmat, me->me_C.dma_map,
   1669   1.1  jonathan 		    0, me->me_C.dma_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1670   1.1  jonathan 		bus_dmamap_sync(sc->sc_dmat, me->me_epb.dma_map,
   1671   1.1  jonathan 		    0, me->me_epb.dma_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1672   1.1  jonathan 
   1673   1.1  jonathan 		if (clen < rlen)
   1674   1.1  jonathan 			krp->krp_status = E2BIG;
   1675   1.1  jonathan 		else {
   1676   1.1  jonathan 			if (sc->sc_flags & UBS_FLAGS_HWNORM) {
   1677  1.18    cegger 				memset(krp->krp_param[krp->krp_iparams].crp_p, 0,
   1678   1.1  jonathan 				    (krp->krp_param[krp->krp_iparams].crp_nbits
   1679   1.1  jonathan 					+ 7) / 8);
   1680   1.1  jonathan 				bcopy(me->me_C.dma_vaddr,
   1681   1.1  jonathan 				    krp->krp_param[krp->krp_iparams].crp_p,
   1682   1.1  jonathan 				    (me->me_modbits + 7) / 8);
   1683   1.1  jonathan 			} else
   1684   1.1  jonathan 				ubsec_kshift_l(me->me_shiftbits,
   1685   1.1  jonathan 				    me->me_C.dma_vaddr, me->me_normbits,
   1686   1.1  jonathan 				    krp->krp_param[krp->krp_iparams].crp_p,
   1687   1.1  jonathan 				    krp->krp_param[krp->krp_iparams].crp_nbits);
   1688   1.1  jonathan 		}
   1689   1.1  jonathan 
   1690   1.1  jonathan 		crypto_kdone(krp);
   1691   1.1  jonathan 
   1692   1.1  jonathan 		/* bzero all potentially sensitive data */
   1693  1.18    cegger 		memset(me->me_E.dma_vaddr, 0, me->me_E.dma_size);
   1694  1.18    cegger 		memset(me->me_M.dma_vaddr, 0, me->me_M.dma_size);
   1695  1.18    cegger 		memset(me->me_C.dma_vaddr, 0, me->me_C.dma_size);
   1696  1.18    cegger 		memset(me->me_q.q_ctx.dma_vaddr, 0, me->me_q.q_ctx.dma_size);
   1697   1.1  jonathan 
   1698   1.1  jonathan 		/* Can't free here, so put us on the free list. */
   1699   1.1  jonathan 		SIMPLEQ_INSERT_TAIL(&sc->sc_q2free, &me->me_q, q_next);
   1700   1.1  jonathan 		break;
   1701   1.1  jonathan 	}
   1702   1.1  jonathan 	case UBS_CTXOP_RSAPRIV: {
   1703   1.1  jonathan 		struct ubsec_q2_rsapriv *rp = (struct ubsec_q2_rsapriv *)q;
   1704   1.1  jonathan 		u_int len;
   1705   1.1  jonathan 
   1706   1.1  jonathan 		krp = rp->rpr_krp;
   1707   1.1  jonathan 		bus_dmamap_sync(sc->sc_dmat, rp->rpr_msgin.dma_map, 0,
   1708   1.1  jonathan 		    rp->rpr_msgin.dma_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1709   1.1  jonathan 		bus_dmamap_sync(sc->sc_dmat, rp->rpr_msgout.dma_map, 0,
   1710   1.1  jonathan 		    rp->rpr_msgout.dma_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1711   1.1  jonathan 
   1712   1.1  jonathan 		len = (krp->krp_param[UBS_RSAPRIV_PAR_MSGOUT].crp_nbits + 7) / 8;
   1713   1.1  jonathan 		bcopy(rp->rpr_msgout.dma_vaddr,
   1714   1.1  jonathan 		    krp->krp_param[UBS_RSAPRIV_PAR_MSGOUT].crp_p, len);
   1715   1.1  jonathan 
   1716   1.1  jonathan 		crypto_kdone(krp);
   1717   1.1  jonathan 
   1718  1.18    cegger 		memset(rp->rpr_msgin.dma_vaddr, 0, rp->rpr_msgin.dma_size);
   1719  1.18    cegger 		memset(rp->rpr_msgout.dma_vaddr, 0, rp->rpr_msgout.dma_size);
   1720  1.18    cegger 		memset(rp->rpr_q.q_ctx.dma_vaddr, 0, rp->rpr_q.q_ctx.dma_size);
   1721   1.1  jonathan 
   1722   1.1  jonathan 		/* Can't free here, so put us on the free list. */
   1723   1.1  jonathan 		SIMPLEQ_INSERT_TAIL(&sc->sc_q2free, &rp->rpr_q, q_next);
   1724   1.1  jonathan 		break;
   1725   1.1  jonathan 	}
   1726   1.1  jonathan 	default:
   1727  1.16    cegger 		printf("%s: unknown ctx op: %x\n", device_xname(&sc->sc_dv),
   1728   1.1  jonathan 		    letoh16(ctx->ctx_op));
   1729   1.1  jonathan 		break;
   1730   1.1  jonathan 	}
   1731   1.1  jonathan }
   1732   1.1  jonathan 
   1733   1.1  jonathan #ifndef UBSEC_NO_RNG
   1734   1.1  jonathan static void
   1735   1.1  jonathan ubsec_rng(void *vsc)
   1736   1.1  jonathan {
   1737   1.1  jonathan 	struct ubsec_softc *sc = vsc;
   1738   1.1  jonathan 	struct ubsec_q2_rng *rng = &sc->sc_rng;
   1739   1.1  jonathan 	struct ubsec_mcr *mcr;
   1740   1.1  jonathan 	struct ubsec_ctx_rngbypass *ctx;
   1741   1.1  jonathan 	int s;
   1742   1.1  jonathan 
   1743   1.1  jonathan 	s = splnet();
   1744   1.1  jonathan 	if (rng->rng_used) {
   1745   1.1  jonathan 		splx(s);
   1746   1.1  jonathan 		return;
   1747   1.1  jonathan 	}
   1748   1.1  jonathan 	sc->sc_nqueue2++;
   1749   1.1  jonathan 	if (sc->sc_nqueue2 >= UBS_MAX_NQUEUE)
   1750   1.1  jonathan 		goto out;
   1751   1.1  jonathan 
   1752   1.1  jonathan 	mcr = (struct ubsec_mcr *)rng->rng_q.q_mcr.dma_vaddr;
   1753   1.1  jonathan 	ctx = (struct ubsec_ctx_rngbypass *)rng->rng_q.q_ctx.dma_vaddr;
   1754   1.1  jonathan 
   1755   1.1  jonathan 	mcr->mcr_pkts = htole16(1);
   1756   1.1  jonathan 	mcr->mcr_flags = 0;
   1757   1.1  jonathan 	mcr->mcr_cmdctxp = htole32(rng->rng_q.q_ctx.dma_paddr);
   1758   1.1  jonathan 	mcr->mcr_ipktbuf.pb_addr = mcr->mcr_ipktbuf.pb_next = 0;
   1759   1.1  jonathan 	mcr->mcr_ipktbuf.pb_len = 0;
   1760   1.1  jonathan 	mcr->mcr_reserved = mcr->mcr_pktlen = 0;
   1761   1.1  jonathan 	mcr->mcr_opktbuf.pb_addr = htole32(rng->rng_buf.dma_paddr);
   1762   1.1  jonathan 	mcr->mcr_opktbuf.pb_len = htole32(((sizeof(u_int32_t) * UBSEC_RNG_BUFSIZ)) &
   1763   1.1  jonathan 	    UBS_PKTBUF_LEN);
   1764   1.1  jonathan 	mcr->mcr_opktbuf.pb_next = 0;
   1765   1.1  jonathan 
   1766   1.1  jonathan 	ctx->rbp_len = htole16(sizeof(struct ubsec_ctx_rngbypass));
   1767   1.1  jonathan 	ctx->rbp_op = htole16(UBS_CTXOP_RNGSHA1);
   1768   1.1  jonathan 	rng->rng_q.q_type = UBS_CTXOP_RNGSHA1;
   1769   1.1  jonathan 
   1770   1.1  jonathan 	bus_dmamap_sync(sc->sc_dmat, rng->rng_buf.dma_map, 0,
   1771   1.1  jonathan 	    rng->rng_buf.dma_map->dm_mapsize, BUS_DMASYNC_PREREAD);
   1772   1.1  jonathan 
   1773   1.1  jonathan 	SIMPLEQ_INSERT_TAIL(&sc->sc_queue2, &rng->rng_q, q_next);
   1774   1.1  jonathan 	rng->rng_used = 1;
   1775   1.1  jonathan 	ubsec_feed2(sc);
   1776   1.1  jonathan 	ubsecstats.hst_rng++;
   1777   1.1  jonathan 	splx(s);
   1778   1.1  jonathan 
   1779   1.1  jonathan 	return;
   1780   1.1  jonathan 
   1781   1.1  jonathan out:
   1782   1.1  jonathan 	/*
   1783   1.1  jonathan 	 * Something weird happened, generate our own call back.
   1784   1.1  jonathan 	 */
   1785   1.1  jonathan 	sc->sc_nqueue2--;
   1786   1.1  jonathan 	splx(s);
   1787   1.1  jonathan #ifdef __OpenBSD__
   1788   1.1  jonathan 	timeout_add(&sc->sc_rngto, sc->sc_rnghz);
   1789   1.1  jonathan #else
   1790   1.1  jonathan 	callout_reset(&sc->sc_rngto, sc->sc_rnghz, ubsec_rng, sc);
   1791   1.1  jonathan #endif
   1792   1.1  jonathan }
   1793   1.1  jonathan #endif /* UBSEC_NO_RNG */
   1794   1.1  jonathan 
   1795   1.1  jonathan static int
   1796   1.1  jonathan ubsec_dma_malloc(struct ubsec_softc *sc, bus_size_t size,
   1797   1.1  jonathan 		 struct ubsec_dma_alloc *dma,int mapflags)
   1798   1.1  jonathan {
   1799   1.1  jonathan 	int r;
   1800   1.1  jonathan 
   1801   1.1  jonathan 	if ((r = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0,
   1802   1.1  jonathan 	    &dma->dma_seg, 1, &dma->dma_nseg, BUS_DMA_NOWAIT)) != 0)
   1803   1.1  jonathan 		goto fail_0;
   1804   1.1  jonathan 
   1805   1.1  jonathan 	if ((r = bus_dmamem_map(sc->sc_dmat, &dma->dma_seg, dma->dma_nseg,
   1806   1.1  jonathan 	    size, &dma->dma_vaddr, mapflags | BUS_DMA_NOWAIT)) != 0)
   1807   1.1  jonathan 		goto fail_1;
   1808   1.1  jonathan 
   1809   1.1  jonathan 	if ((r = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
   1810   1.1  jonathan 	    BUS_DMA_NOWAIT, &dma->dma_map)) != 0)
   1811   1.1  jonathan 		goto fail_2;
   1812   1.1  jonathan 
   1813   1.1  jonathan 	if ((r = bus_dmamap_load(sc->sc_dmat, dma->dma_map, dma->dma_vaddr,
   1814   1.1  jonathan 	    size, NULL, BUS_DMA_NOWAIT)) != 0)
   1815   1.1  jonathan 		goto fail_3;
   1816   1.1  jonathan 
   1817   1.1  jonathan 	dma->dma_paddr = dma->dma_map->dm_segs[0].ds_addr;
   1818   1.1  jonathan 	dma->dma_size = size;
   1819   1.1  jonathan 	return (0);
   1820   1.1  jonathan 
   1821   1.1  jonathan fail_3:
   1822   1.1  jonathan 	bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
   1823   1.1  jonathan fail_2:
   1824   1.1  jonathan 	bus_dmamem_unmap(sc->sc_dmat, dma->dma_vaddr, size);
   1825   1.1  jonathan fail_1:
   1826   1.1  jonathan 	bus_dmamem_free(sc->sc_dmat, &dma->dma_seg, dma->dma_nseg);
   1827   1.1  jonathan fail_0:
   1828   1.1  jonathan 	dma->dma_map = NULL;
   1829   1.1  jonathan 	return (r);
   1830   1.1  jonathan }
   1831   1.1  jonathan 
   1832   1.1  jonathan static void
   1833   1.1  jonathan ubsec_dma_free(struct ubsec_softc *sc, struct ubsec_dma_alloc *dma)
   1834   1.1  jonathan {
   1835   1.1  jonathan 	bus_dmamap_unload(sc->sc_dmat, dma->dma_map);
   1836   1.1  jonathan 	bus_dmamem_unmap(sc->sc_dmat, dma->dma_vaddr, dma->dma_size);
   1837   1.1  jonathan 	bus_dmamem_free(sc->sc_dmat, &dma->dma_seg, dma->dma_nseg);
   1838   1.1  jonathan 	bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
   1839   1.1  jonathan }
   1840   1.1  jonathan 
   1841   1.1  jonathan /*
   1842   1.1  jonathan  * Resets the board.  Values in the regesters are left as is
   1843   1.1  jonathan  * from the reset (i.e. initial values are assigned elsewhere).
   1844   1.1  jonathan  */
   1845   1.1  jonathan static void
   1846   1.1  jonathan ubsec_reset_board(struct ubsec_softc *sc)
   1847   1.1  jonathan {
   1848   1.1  jonathan     volatile u_int32_t ctrl;
   1849   1.1  jonathan 
   1850   1.1  jonathan     ctrl = READ_REG(sc, BS_CTRL);
   1851   1.1  jonathan     ctrl |= BS_CTRL_RESET;
   1852   1.1  jonathan     WRITE_REG(sc, BS_CTRL, ctrl);
   1853   1.1  jonathan 
   1854   1.1  jonathan     /*
   1855   1.1  jonathan      * Wait aprox. 30 PCI clocks = 900 ns = 0.9 us
   1856   1.1  jonathan      */
   1857   1.1  jonathan     DELAY(10);
   1858   1.1  jonathan }
   1859   1.1  jonathan 
   1860   1.1  jonathan /*
   1861   1.1  jonathan  * Init Broadcom registers
   1862   1.1  jonathan  */
   1863   1.1  jonathan static void
   1864   1.1  jonathan ubsec_init_board(struct ubsec_softc *sc)
   1865   1.1  jonathan {
   1866   1.1  jonathan 	u_int32_t ctrl;
   1867   1.1  jonathan 
   1868   1.1  jonathan 	ctrl = READ_REG(sc, BS_CTRL);
   1869   1.1  jonathan 	ctrl &= ~(BS_CTRL_BE32 | BS_CTRL_BE64);
   1870   1.1  jonathan 	ctrl |= BS_CTRL_LITTLE_ENDIAN | BS_CTRL_MCR1INT;
   1871   1.1  jonathan 
   1872   1.1  jonathan 	/*
   1873   1.1  jonathan 	 * XXX: Sam Leffler's code has (UBS_FLAGS_KEY|UBS_FLAGS_RNG)).
   1874   1.1  jonathan 	 * anyone got hw docs?
   1875   1.1  jonathan 	 */
   1876   1.1  jonathan 	if (sc->sc_flags & UBS_FLAGS_KEY)
   1877   1.1  jonathan 		ctrl |= BS_CTRL_MCR2INT;
   1878   1.1  jonathan 	else
   1879   1.1  jonathan 		ctrl &= ~BS_CTRL_MCR2INT;
   1880   1.1  jonathan 
   1881   1.1  jonathan 	if (sc->sc_flags & UBS_FLAGS_HWNORM)
   1882   1.1  jonathan 		ctrl &= ~BS_CTRL_SWNORM;
   1883   1.1  jonathan 
   1884   1.1  jonathan 	WRITE_REG(sc, BS_CTRL, ctrl);
   1885   1.1  jonathan }
   1886   1.1  jonathan 
   1887   1.1  jonathan /*
   1888   1.1  jonathan  * Init Broadcom PCI registers
   1889   1.1  jonathan  */
   1890   1.1  jonathan static void
   1891   1.7   thorpej ubsec_init_pciregs(struct pci_attach_args *pa)
   1892   1.1  jonathan {
   1893   1.1  jonathan 	pci_chipset_tag_t pc = pa->pa_pc;
   1894   1.1  jonathan 	u_int32_t misc;
   1895   1.1  jonathan 
   1896   1.1  jonathan 	/*
   1897   1.1  jonathan 	 * This will set the cache line size to 1, this will
   1898   1.1  jonathan 	 * force the BCM58xx chip just to do burst read/writes.
   1899   1.1  jonathan 	 * Cache line read/writes are to slow
   1900   1.1  jonathan 	 */
   1901   1.1  jonathan 	misc = pci_conf_read(pc, pa->pa_tag, PCI_BHLC_REG);
   1902   1.1  jonathan 	misc = (misc & ~(PCI_CACHELINE_MASK << PCI_CACHELINE_SHIFT))
   1903   1.1  jonathan 	    | ((UBS_DEF_CACHELINE & 0xff) << PCI_CACHELINE_SHIFT);
   1904   1.1  jonathan 	pci_conf_write(pc, pa->pa_tag, PCI_BHLC_REG, misc);
   1905   1.1  jonathan }
   1906   1.1  jonathan 
   1907   1.1  jonathan /*
   1908   1.1  jonathan  * Clean up after a chip crash.
   1909   1.1  jonathan  * It is assumed that the caller in splnet()
   1910   1.1  jonathan  */
   1911   1.1  jonathan static void
   1912   1.1  jonathan ubsec_cleanchip(struct ubsec_softc *sc)
   1913   1.1  jonathan {
   1914   1.1  jonathan 	struct ubsec_q *q;
   1915   1.1  jonathan 
   1916   1.1  jonathan 	while (!SIMPLEQ_EMPTY(&sc->sc_qchip)) {
   1917   1.1  jonathan 		q = SIMPLEQ_FIRST(&sc->sc_qchip);
   1918   1.1  jonathan 		SIMPLEQ_REMOVE_HEAD(&sc->sc_qchip, /*q,*/ q_next);
   1919   1.1  jonathan 		ubsec_free_q(sc, q);
   1920   1.1  jonathan 	}
   1921   1.1  jonathan 	sc->sc_nqchip = 0;
   1922   1.1  jonathan }
   1923   1.1  jonathan 
   1924   1.1  jonathan /*
   1925   1.1  jonathan  * free a ubsec_q
   1926   1.1  jonathan  * It is assumed that the caller is within splnet()
   1927   1.1  jonathan  */
   1928   1.1  jonathan static int
   1929   1.1  jonathan ubsec_free_q(struct ubsec_softc *sc, struct ubsec_q *q)
   1930   1.1  jonathan {
   1931   1.1  jonathan 	struct ubsec_q *q2;
   1932   1.1  jonathan 	struct cryptop *crp;
   1933   1.1  jonathan 	int npkts;
   1934   1.1  jonathan 	int i;
   1935   1.1  jonathan 
   1936   1.1  jonathan 	npkts = q->q_nstacked_mcrs;
   1937   1.1  jonathan 
   1938   1.1  jonathan 	for (i = 0; i < npkts; i++) {
   1939   1.1  jonathan 		if(q->q_stacked_mcr[i]) {
   1940   1.1  jonathan 			q2 = q->q_stacked_mcr[i];
   1941   1.1  jonathan 
   1942   1.5     perry 			if ((q2->q_dst_m != NULL) && (q2->q_src_m != q2->q_dst_m))
   1943   1.1  jonathan 				m_freem(q2->q_dst_m);
   1944   1.1  jonathan 
   1945   1.1  jonathan 			crp = (struct cryptop *)q2->q_crp;
   1946   1.5     perry 
   1947   1.1  jonathan 			SIMPLEQ_INSERT_TAIL(&sc->sc_freequeue, q2, q_next);
   1948   1.5     perry 
   1949   1.1  jonathan 			crp->crp_etype = EFAULT;
   1950   1.1  jonathan 			crypto_done(crp);
   1951   1.1  jonathan 		} else {
   1952   1.1  jonathan 			break;
   1953   1.1  jonathan 		}
   1954   1.1  jonathan 	}
   1955   1.1  jonathan 
   1956   1.1  jonathan 	/*
   1957   1.1  jonathan 	 * Free header MCR
   1958   1.1  jonathan 	 */
   1959   1.1  jonathan 	if ((q->q_dst_m != NULL) && (q->q_src_m != q->q_dst_m))
   1960   1.1  jonathan 		m_freem(q->q_dst_m);
   1961   1.1  jonathan 
   1962   1.1  jonathan 	crp = (struct cryptop *)q->q_crp;
   1963   1.5     perry 
   1964   1.1  jonathan 	SIMPLEQ_INSERT_TAIL(&sc->sc_freequeue, q, q_next);
   1965   1.5     perry 
   1966   1.1  jonathan 	crp->crp_etype = EFAULT;
   1967   1.1  jonathan 	crypto_done(crp);
   1968   1.1  jonathan 	return(0);
   1969   1.1  jonathan }
   1970   1.1  jonathan 
   1971   1.1  jonathan /*
   1972   1.1  jonathan  * Routine to reset the chip and clean up.
   1973   1.1  jonathan  * It is assumed that the caller is in splnet()
   1974   1.1  jonathan  */
   1975   1.1  jonathan static void
   1976   1.1  jonathan ubsec_totalreset(struct ubsec_softc *sc)
   1977   1.1  jonathan {
   1978   1.1  jonathan 	ubsec_reset_board(sc);
   1979   1.1  jonathan 	ubsec_init_board(sc);
   1980   1.1  jonathan 	ubsec_cleanchip(sc);
   1981   1.1  jonathan }
   1982   1.1  jonathan 
   1983   1.1  jonathan static int
   1984   1.1  jonathan ubsec_dmamap_aligned(bus_dmamap_t map)
   1985   1.1  jonathan {
   1986   1.1  jonathan 	int i;
   1987   1.1  jonathan 
   1988   1.1  jonathan 	for (i = 0; i < map->dm_nsegs; i++) {
   1989   1.1  jonathan 		if (map->dm_segs[i].ds_addr & 3)
   1990   1.1  jonathan 			return (0);
   1991   1.1  jonathan 		if ((i != (map->dm_nsegs - 1)) &&
   1992   1.1  jonathan 		    (map->dm_segs[i].ds_len & 3))
   1993   1.1  jonathan 			return (0);
   1994   1.1  jonathan 	}
   1995   1.1  jonathan 	return (1);
   1996   1.1  jonathan }
   1997   1.1  jonathan 
   1998   1.1  jonathan #ifdef __OpenBSD__
   1999   1.1  jonathan struct ubsec_softc *
   2000   1.7   thorpej ubsec_kfind(struct cryptkop *krp)
   2001   1.1  jonathan {
   2002   1.1  jonathan 	struct ubsec_softc *sc;
   2003   1.1  jonathan 	int i;
   2004   1.1  jonathan 
   2005   1.1  jonathan 	for (i = 0; i < ubsec_cd.cd_ndevs; i++) {
   2006   1.1  jonathan 		sc = ubsec_cd.cd_devs[i];
   2007   1.1  jonathan 		if (sc == NULL)
   2008   1.1  jonathan 			continue;
   2009   1.1  jonathan 		if (sc->sc_cid == krp->krp_hid)
   2010   1.1  jonathan 			return (sc);
   2011   1.1  jonathan 	}
   2012   1.1  jonathan 	return (NULL);
   2013   1.1  jonathan }
   2014   1.1  jonathan #endif
   2015   1.1  jonathan 
   2016   1.1  jonathan static void
   2017   1.1  jonathan ubsec_kfree(struct ubsec_softc *sc, struct ubsec_q2 *q)
   2018   1.1  jonathan {
   2019   1.1  jonathan 	switch (q->q_type) {
   2020   1.1  jonathan 	case UBS_CTXOP_MODEXP: {
   2021   1.1  jonathan 		struct ubsec_q2_modexp *me = (struct ubsec_q2_modexp *)q;
   2022   1.1  jonathan 
   2023   1.1  jonathan 		ubsec_dma_free(sc, &me->me_q.q_mcr);
   2024   1.1  jonathan 		ubsec_dma_free(sc, &me->me_q.q_ctx);
   2025   1.1  jonathan 		ubsec_dma_free(sc, &me->me_M);
   2026   1.1  jonathan 		ubsec_dma_free(sc, &me->me_E);
   2027   1.1  jonathan 		ubsec_dma_free(sc, &me->me_C);
   2028   1.1  jonathan 		ubsec_dma_free(sc, &me->me_epb);
   2029   1.1  jonathan 		free(me, M_DEVBUF);
   2030   1.1  jonathan 		break;
   2031   1.1  jonathan 	}
   2032   1.1  jonathan 	case UBS_CTXOP_RSAPRIV: {
   2033   1.1  jonathan 		struct ubsec_q2_rsapriv *rp = (struct ubsec_q2_rsapriv *)q;
   2034   1.1  jonathan 
   2035   1.1  jonathan 		ubsec_dma_free(sc, &rp->rpr_q.q_mcr);
   2036   1.1  jonathan 		ubsec_dma_free(sc, &rp->rpr_q.q_ctx);
   2037   1.1  jonathan 		ubsec_dma_free(sc, &rp->rpr_msgin);
   2038   1.1  jonathan 		ubsec_dma_free(sc, &rp->rpr_msgout);
   2039   1.1  jonathan 		free(rp, M_DEVBUF);
   2040   1.1  jonathan 		break;
   2041   1.1  jonathan 	}
   2042   1.1  jonathan 	default:
   2043  1.16    cegger 		printf("%s: invalid kfree 0x%x\n", device_xname(&sc->sc_dv),
   2044   1.1  jonathan 		    q->q_type);
   2045   1.1  jonathan 		break;
   2046   1.1  jonathan 	}
   2047   1.1  jonathan }
   2048   1.1  jonathan 
   2049   1.1  jonathan static int
   2050   1.1  jonathan ubsec_kprocess(void *arg, struct cryptkop *krp, int hint)
   2051   1.1  jonathan {
   2052   1.1  jonathan 	struct ubsec_softc *sc;
   2053   1.1  jonathan 	int r;
   2054   1.1  jonathan 
   2055   1.1  jonathan 	if (krp == NULL || krp->krp_callback == NULL)
   2056   1.1  jonathan 		return (EINVAL);
   2057   1.1  jonathan #ifdef __OpenBSD__
   2058   1.1  jonathan 	if ((sc = ubsec_kfind(krp)) == NULL)
   2059   1.1  jonathan 		return (EINVAL);
   2060   1.1  jonathan #else
   2061   1.1  jonathan 	sc = arg;
   2062   1.1  jonathan 	KASSERT(sc != NULL /*, ("ubsec_kprocess: null softc")*/);
   2063   1.1  jonathan #endif
   2064   1.1  jonathan 
   2065   1.1  jonathan 	while (!SIMPLEQ_EMPTY(&sc->sc_q2free)) {
   2066   1.1  jonathan 		struct ubsec_q2 *q;
   2067   1.1  jonathan 
   2068   1.1  jonathan 		q = SIMPLEQ_FIRST(&sc->sc_q2free);
   2069   1.1  jonathan 		SIMPLEQ_REMOVE_HEAD(&sc->sc_q2free, /*q,*/ q_next);
   2070   1.1  jonathan 		ubsec_kfree(sc, q);
   2071   1.1  jonathan 	}
   2072   1.1  jonathan 
   2073   1.1  jonathan 	switch (krp->krp_op) {
   2074   1.1  jonathan 	case CRK_MOD_EXP:
   2075   1.1  jonathan 		if (sc->sc_flags & UBS_FLAGS_HWNORM)
   2076   1.1  jonathan 			r = ubsec_kprocess_modexp_hw(sc, krp, hint);
   2077   1.1  jonathan 		else
   2078   1.1  jonathan 			r = ubsec_kprocess_modexp_sw(sc, krp, hint);
   2079   1.1  jonathan 		break;
   2080   1.1  jonathan 	case CRK_MOD_EXP_CRT:
   2081   1.1  jonathan 		r = ubsec_kprocess_rsapriv(sc, krp, hint);
   2082   1.1  jonathan 		break;
   2083   1.1  jonathan 	default:
   2084   1.1  jonathan 		printf("%s: kprocess: invalid op 0x%x\n",
   2085  1.16    cegger 		    device_xname(&sc->sc_dv), krp->krp_op);
   2086   1.1  jonathan 		krp->krp_status = EOPNOTSUPP;
   2087   1.1  jonathan 		crypto_kdone(krp);
   2088   1.1  jonathan 		r = 0;
   2089   1.1  jonathan 	}
   2090   1.1  jonathan 	return (r);
   2091   1.1  jonathan }
   2092   1.1  jonathan 
   2093   1.1  jonathan /*
   2094   1.1  jonathan  * Start computation of cr[C] = (cr[M] ^ cr[E]) mod cr[N] (sw normalization)
   2095   1.1  jonathan  */
   2096   1.1  jonathan static int
   2097   1.1  jonathan ubsec_kprocess_modexp_sw(struct ubsec_softc *sc, struct cryptkop *krp,
   2098  1.11  christos 			 int hint)
   2099   1.1  jonathan {
   2100   1.1  jonathan 	struct ubsec_q2_modexp *me;
   2101   1.1  jonathan 	struct ubsec_mcr *mcr;
   2102   1.1  jonathan 	struct ubsec_ctx_modexp *ctx;
   2103   1.1  jonathan 	struct ubsec_pktbuf *epb;
   2104   1.1  jonathan 	int s, err = 0;
   2105   1.1  jonathan 	u_int nbits, normbits, mbits, shiftbits, ebits;
   2106   1.1  jonathan 
   2107   1.1  jonathan 	me = (struct ubsec_q2_modexp *)malloc(sizeof *me, M_DEVBUF, M_NOWAIT);
   2108   1.1  jonathan 	if (me == NULL) {
   2109   1.1  jonathan 		err = ENOMEM;
   2110   1.1  jonathan 		goto errout;
   2111   1.1  jonathan 	}
   2112  1.18    cegger 	memset(me, 0, sizeof *me);
   2113   1.1  jonathan 	me->me_krp = krp;
   2114   1.1  jonathan 	me->me_q.q_type = UBS_CTXOP_MODEXP;
   2115   1.1  jonathan 
   2116   1.1  jonathan 	nbits = ubsec_ksigbits(&krp->krp_param[UBS_MODEXP_PAR_N]);
   2117   1.1  jonathan 	if (nbits <= 512)
   2118   1.1  jonathan 		normbits = 512;
   2119   1.1  jonathan 	else if (nbits <= 768)
   2120   1.1  jonathan 		normbits = 768;
   2121   1.1  jonathan 	else if (nbits <= 1024)
   2122   1.1  jonathan 		normbits = 1024;
   2123   1.1  jonathan 	else if (sc->sc_flags & UBS_FLAGS_BIGKEY && nbits <= 1536)
   2124   1.1  jonathan 		normbits = 1536;
   2125   1.1  jonathan 	else if (sc->sc_flags & UBS_FLAGS_BIGKEY && nbits <= 2048)
   2126   1.1  jonathan 		normbits = 2048;
   2127   1.1  jonathan 	else {
   2128   1.1  jonathan 		err = E2BIG;
   2129   1.1  jonathan 		goto errout;
   2130   1.1  jonathan 	}
   2131   1.1  jonathan 
   2132   1.1  jonathan 	shiftbits = normbits - nbits;
   2133   1.1  jonathan 
   2134   1.1  jonathan 	me->me_modbits = nbits;
   2135   1.1  jonathan 	me->me_shiftbits = shiftbits;
   2136   1.1  jonathan 	me->me_normbits = normbits;
   2137   1.1  jonathan 
   2138   1.1  jonathan 	/* Sanity check: result bits must be >= true modulus bits. */
   2139   1.1  jonathan 	if (krp->krp_param[krp->krp_iparams].crp_nbits < nbits) {
   2140   1.1  jonathan 		err = ERANGE;
   2141   1.1  jonathan 		goto errout;
   2142   1.1  jonathan 	}
   2143   1.1  jonathan 
   2144   1.1  jonathan 	if (ubsec_dma_malloc(sc, sizeof(struct ubsec_mcr),
   2145   1.1  jonathan 	    &me->me_q.q_mcr, 0)) {
   2146   1.1  jonathan 		err = ENOMEM;
   2147   1.1  jonathan 		goto errout;
   2148   1.1  jonathan 	}
   2149   1.1  jonathan 	mcr = (struct ubsec_mcr *)me->me_q.q_mcr.dma_vaddr;
   2150   1.1  jonathan 
   2151   1.1  jonathan 	if (ubsec_dma_malloc(sc, sizeof(struct ubsec_ctx_modexp),
   2152   1.1  jonathan 	    &me->me_q.q_ctx, 0)) {
   2153   1.1  jonathan 		err = ENOMEM;
   2154   1.1  jonathan 		goto errout;
   2155   1.1  jonathan 	}
   2156   1.1  jonathan 
   2157   1.1  jonathan 	mbits = ubsec_ksigbits(&krp->krp_param[UBS_MODEXP_PAR_M]);
   2158   1.1  jonathan 	if (mbits > nbits) {
   2159   1.1  jonathan 		err = E2BIG;
   2160   1.1  jonathan 		goto errout;
   2161   1.1  jonathan 	}
   2162   1.1  jonathan 	if (ubsec_dma_malloc(sc, normbits / 8, &me->me_M, 0)) {
   2163   1.1  jonathan 		err = ENOMEM;
   2164   1.1  jonathan 		goto errout;
   2165   1.1  jonathan 	}
   2166   1.1  jonathan 	ubsec_kshift_r(shiftbits,
   2167   1.1  jonathan 	    krp->krp_param[UBS_MODEXP_PAR_M].crp_p, mbits,
   2168   1.1  jonathan 	    me->me_M.dma_vaddr, normbits);
   2169   1.1  jonathan 
   2170   1.1  jonathan 	if (ubsec_dma_malloc(sc, normbits / 8, &me->me_C, 0)) {
   2171   1.1  jonathan 		err = ENOMEM;
   2172   1.1  jonathan 		goto errout;
   2173   1.1  jonathan 	}
   2174  1.18    cegger 	memset(me->me_C.dma_vaddr, 0, me->me_C.dma_size);
   2175   1.1  jonathan 
   2176   1.1  jonathan 	ebits = ubsec_ksigbits(&krp->krp_param[UBS_MODEXP_PAR_E]);
   2177   1.1  jonathan 	if (ebits > nbits) {
   2178   1.1  jonathan 		err = E2BIG;
   2179   1.1  jonathan 		goto errout;
   2180   1.1  jonathan 	}
   2181   1.1  jonathan 	if (ubsec_dma_malloc(sc, normbits / 8, &me->me_E, 0)) {
   2182   1.1  jonathan 		err = ENOMEM;
   2183   1.1  jonathan 		goto errout;
   2184   1.1  jonathan 	}
   2185   1.1  jonathan 	ubsec_kshift_r(shiftbits,
   2186   1.1  jonathan 	    krp->krp_param[UBS_MODEXP_PAR_E].crp_p, ebits,
   2187   1.1  jonathan 	    me->me_E.dma_vaddr, normbits);
   2188   1.1  jonathan 
   2189   1.1  jonathan 	if (ubsec_dma_malloc(sc, sizeof(struct ubsec_pktbuf),
   2190   1.1  jonathan 	    &me->me_epb, 0)) {
   2191   1.1  jonathan 		err = ENOMEM;
   2192   1.1  jonathan 		goto errout;
   2193   1.1  jonathan 	}
   2194   1.1  jonathan 	epb = (struct ubsec_pktbuf *)me->me_epb.dma_vaddr;
   2195   1.1  jonathan 	epb->pb_addr = htole32(me->me_E.dma_paddr);
   2196   1.1  jonathan 	epb->pb_next = 0;
   2197   1.1  jonathan 	epb->pb_len = htole32(normbits / 8);
   2198   1.1  jonathan 
   2199   1.1  jonathan #ifdef UBSEC_DEBUG
   2200   1.1  jonathan 	if (ubsec_debug) {
   2201   1.1  jonathan 		printf("Epb ");
   2202   1.1  jonathan 		ubsec_dump_pb(epb);
   2203   1.1  jonathan 	}
   2204   1.1  jonathan #endif
   2205   1.1  jonathan 
   2206   1.1  jonathan 	mcr->mcr_pkts = htole16(1);
   2207   1.1  jonathan 	mcr->mcr_flags = 0;
   2208   1.1  jonathan 	mcr->mcr_cmdctxp = htole32(me->me_q.q_ctx.dma_paddr);
   2209   1.1  jonathan 	mcr->mcr_reserved = 0;
   2210   1.1  jonathan 	mcr->mcr_pktlen = 0;
   2211   1.1  jonathan 
   2212   1.1  jonathan 	mcr->mcr_ipktbuf.pb_addr = htole32(me->me_M.dma_paddr);
   2213   1.1  jonathan 	mcr->mcr_ipktbuf.pb_len = htole32(normbits / 8);
   2214   1.1  jonathan 	mcr->mcr_ipktbuf.pb_next = htole32(me->me_epb.dma_paddr);
   2215   1.1  jonathan 
   2216   1.1  jonathan 	mcr->mcr_opktbuf.pb_addr = htole32(me->me_C.dma_paddr);
   2217   1.1  jonathan 	mcr->mcr_opktbuf.pb_next = 0;
   2218   1.1  jonathan 	mcr->mcr_opktbuf.pb_len = htole32(normbits / 8);
   2219   1.1  jonathan 
   2220   1.1  jonathan #ifdef DIAGNOSTIC
   2221   1.1  jonathan 	/* Misaligned output buffer will hang the chip. */
   2222   1.1  jonathan 	if ((letoh32(mcr->mcr_opktbuf.pb_addr) & 3) != 0)
   2223   1.1  jonathan 		panic("%s: modexp invalid addr 0x%x",
   2224  1.16    cegger 		    device_xname(&sc->sc_dv), letoh32(mcr->mcr_opktbuf.pb_addr));
   2225   1.1  jonathan 	if ((letoh32(mcr->mcr_opktbuf.pb_len) & 3) != 0)
   2226   1.1  jonathan 		panic("%s: modexp invalid len 0x%x",
   2227  1.16    cegger 		    device_xname(&sc->sc_dv), letoh32(mcr->mcr_opktbuf.pb_len));
   2228   1.1  jonathan #endif
   2229   1.1  jonathan 
   2230   1.1  jonathan 	ctx = (struct ubsec_ctx_modexp *)me->me_q.q_ctx.dma_vaddr;
   2231  1.18    cegger 	memset(ctx, 0, sizeof(*ctx));
   2232   1.1  jonathan 	ubsec_kshift_r(shiftbits,
   2233   1.1  jonathan 	    krp->krp_param[UBS_MODEXP_PAR_N].crp_p, nbits,
   2234   1.1  jonathan 	    ctx->me_N, normbits);
   2235   1.1  jonathan 	ctx->me_len = htole16((normbits / 8) + (4 * sizeof(u_int16_t)));
   2236   1.1  jonathan 	ctx->me_op = htole16(UBS_CTXOP_MODEXP);
   2237   1.1  jonathan 	ctx->me_E_len = htole16(nbits);
   2238   1.1  jonathan 	ctx->me_N_len = htole16(nbits);
   2239   1.1  jonathan 
   2240   1.1  jonathan #ifdef UBSEC_DEBUG
   2241   1.1  jonathan 	if (ubsec_debug) {
   2242   1.1  jonathan 		ubsec_dump_mcr(mcr);
   2243   1.1  jonathan 		ubsec_dump_ctx2((struct ubsec_ctx_keyop *)ctx);
   2244   1.1  jonathan 	}
   2245   1.1  jonathan #endif
   2246   1.1  jonathan 
   2247   1.1  jonathan 	/*
   2248   1.1  jonathan 	 * ubsec_feed2 will sync mcr and ctx, we just need to sync
   2249   1.1  jonathan 	 * everything else.
   2250   1.1  jonathan 	 */
   2251   1.1  jonathan 	bus_dmamap_sync(sc->sc_dmat, me->me_M.dma_map,
   2252   1.1  jonathan 	    0, me->me_M.dma_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   2253   1.1  jonathan 	bus_dmamap_sync(sc->sc_dmat, me->me_E.dma_map,
   2254   1.1  jonathan 	    0, me->me_E.dma_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   2255   1.1  jonathan 	bus_dmamap_sync(sc->sc_dmat, me->me_C.dma_map,
   2256   1.1  jonathan 	    0, me->me_C.dma_map->dm_mapsize, BUS_DMASYNC_PREREAD);
   2257   1.1  jonathan 	bus_dmamap_sync(sc->sc_dmat, me->me_epb.dma_map,
   2258   1.1  jonathan 	    0, me->me_epb.dma_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   2259   1.1  jonathan 
   2260   1.1  jonathan 	/* Enqueue and we're done... */
   2261   1.1  jonathan 	s = splnet();
   2262   1.1  jonathan 	SIMPLEQ_INSERT_TAIL(&sc->sc_queue2, &me->me_q, q_next);
   2263   1.1  jonathan 	ubsec_feed2(sc);
   2264   1.1  jonathan 	ubsecstats.hst_modexp++;
   2265   1.1  jonathan 	splx(s);
   2266   1.1  jonathan 
   2267   1.1  jonathan 	return (0);
   2268   1.1  jonathan 
   2269   1.1  jonathan errout:
   2270   1.1  jonathan 	if (me != NULL) {
   2271   1.1  jonathan 		if (me->me_q.q_mcr.dma_map != NULL)
   2272   1.1  jonathan 			ubsec_dma_free(sc, &me->me_q.q_mcr);
   2273   1.1  jonathan 		if (me->me_q.q_ctx.dma_map != NULL) {
   2274  1.18    cegger 			memset(me->me_q.q_ctx.dma_vaddr, 0, me->me_q.q_ctx.dma_size);
   2275   1.1  jonathan 			ubsec_dma_free(sc, &me->me_q.q_ctx);
   2276   1.1  jonathan 		}
   2277   1.1  jonathan 		if (me->me_M.dma_map != NULL) {
   2278  1.18    cegger 			memset(me->me_M.dma_vaddr, 0, me->me_M.dma_size);
   2279   1.1  jonathan 			ubsec_dma_free(sc, &me->me_M);
   2280   1.1  jonathan 		}
   2281   1.1  jonathan 		if (me->me_E.dma_map != NULL) {
   2282  1.18    cegger 			memset(me->me_E.dma_vaddr, 0, me->me_E.dma_size);
   2283   1.1  jonathan 			ubsec_dma_free(sc, &me->me_E);
   2284   1.1  jonathan 		}
   2285   1.1  jonathan 		if (me->me_C.dma_map != NULL) {
   2286  1.18    cegger 			memset(me->me_C.dma_vaddr, 0, me->me_C.dma_size);
   2287   1.1  jonathan 			ubsec_dma_free(sc, &me->me_C);
   2288   1.1  jonathan 		}
   2289   1.1  jonathan 		if (me->me_epb.dma_map != NULL)
   2290   1.1  jonathan 			ubsec_dma_free(sc, &me->me_epb);
   2291   1.1  jonathan 		free(me, M_DEVBUF);
   2292   1.1  jonathan 	}
   2293   1.1  jonathan 	krp->krp_status = err;
   2294   1.1  jonathan 	crypto_kdone(krp);
   2295   1.1  jonathan 	return (0);
   2296   1.1  jonathan }
   2297   1.1  jonathan 
   2298   1.1  jonathan /*
   2299   1.1  jonathan  * Start computation of cr[C] = (cr[M] ^ cr[E]) mod cr[N] (hw normalization)
   2300   1.1  jonathan  */
   2301   1.1  jonathan static int
   2302   1.1  jonathan ubsec_kprocess_modexp_hw(struct ubsec_softc *sc, struct cryptkop *krp,
   2303  1.11  christos 			 int hint)
   2304   1.1  jonathan {
   2305   1.1  jonathan 	struct ubsec_q2_modexp *me;
   2306   1.1  jonathan 	struct ubsec_mcr *mcr;
   2307   1.1  jonathan 	struct ubsec_ctx_modexp *ctx;
   2308   1.1  jonathan 	struct ubsec_pktbuf *epb;
   2309   1.1  jonathan 	int s, err = 0;
   2310   1.1  jonathan 	u_int nbits, normbits, mbits, shiftbits, ebits;
   2311   1.1  jonathan 
   2312   1.1  jonathan 	me = (struct ubsec_q2_modexp *)malloc(sizeof *me, M_DEVBUF, M_NOWAIT);
   2313   1.1  jonathan 	if (me == NULL) {
   2314   1.1  jonathan 		err = ENOMEM;
   2315   1.1  jonathan 		goto errout;
   2316   1.1  jonathan 	}
   2317  1.18    cegger 	memset(me, 0, sizeof *me);
   2318   1.1  jonathan 	me->me_krp = krp;
   2319   1.1  jonathan 	me->me_q.q_type = UBS_CTXOP_MODEXP;
   2320   1.1  jonathan 
   2321   1.1  jonathan 	nbits = ubsec_ksigbits(&krp->krp_param[UBS_MODEXP_PAR_N]);
   2322   1.1  jonathan 	if (nbits <= 512)
   2323   1.1  jonathan 		normbits = 512;
   2324   1.1  jonathan 	else if (nbits <= 768)
   2325   1.1  jonathan 		normbits = 768;
   2326   1.1  jonathan 	else if (nbits <= 1024)
   2327   1.1  jonathan 		normbits = 1024;
   2328   1.1  jonathan 	else if (sc->sc_flags & UBS_FLAGS_BIGKEY && nbits <= 1536)
   2329   1.1  jonathan 		normbits = 1536;
   2330   1.1  jonathan 	else if (sc->sc_flags & UBS_FLAGS_BIGKEY && nbits <= 2048)
   2331   1.1  jonathan 		normbits = 2048;
   2332   1.1  jonathan 	else {
   2333   1.1  jonathan 		err = E2BIG;
   2334   1.1  jonathan 		goto errout;
   2335   1.1  jonathan 	}
   2336   1.1  jonathan 
   2337   1.1  jonathan 	shiftbits = normbits - nbits;
   2338   1.1  jonathan 
   2339   1.1  jonathan 	/* XXX ??? */
   2340   1.1  jonathan 	me->me_modbits = nbits;
   2341   1.1  jonathan 	me->me_shiftbits = shiftbits;
   2342   1.1  jonathan 	me->me_normbits = normbits;
   2343   1.1  jonathan 
   2344   1.1  jonathan 	/* Sanity check: result bits must be >= true modulus bits. */
   2345   1.1  jonathan 	if (krp->krp_param[krp->krp_iparams].crp_nbits < nbits) {
   2346   1.1  jonathan 		err = ERANGE;
   2347   1.1  jonathan 		goto errout;
   2348   1.1  jonathan 	}
   2349   1.1  jonathan 
   2350   1.1  jonathan 	if (ubsec_dma_malloc(sc, sizeof(struct ubsec_mcr),
   2351   1.1  jonathan 	    &me->me_q.q_mcr, 0)) {
   2352   1.1  jonathan 		err = ENOMEM;
   2353   1.1  jonathan 		goto errout;
   2354   1.1  jonathan 	}
   2355   1.1  jonathan 	mcr = (struct ubsec_mcr *)me->me_q.q_mcr.dma_vaddr;
   2356   1.1  jonathan 
   2357   1.1  jonathan 	if (ubsec_dma_malloc(sc, sizeof(struct ubsec_ctx_modexp),
   2358   1.1  jonathan 	    &me->me_q.q_ctx, 0)) {
   2359   1.1  jonathan 		err = ENOMEM;
   2360   1.1  jonathan 		goto errout;
   2361   1.1  jonathan 	}
   2362   1.1  jonathan 
   2363   1.1  jonathan 	mbits = ubsec_ksigbits(&krp->krp_param[UBS_MODEXP_PAR_M]);
   2364   1.1  jonathan 	if (mbits > nbits) {
   2365   1.1  jonathan 		err = E2BIG;
   2366   1.1  jonathan 		goto errout;
   2367   1.1  jonathan 	}
   2368   1.1  jonathan 	if (ubsec_dma_malloc(sc, normbits / 8, &me->me_M, 0)) {
   2369   1.1  jonathan 		err = ENOMEM;
   2370   1.1  jonathan 		goto errout;
   2371   1.1  jonathan 	}
   2372  1.18    cegger 	memset(me->me_M.dma_vaddr, 0, normbits / 8);
   2373   1.1  jonathan 	bcopy(krp->krp_param[UBS_MODEXP_PAR_M].crp_p,
   2374   1.1  jonathan 	    me->me_M.dma_vaddr, (mbits + 7) / 8);
   2375   1.1  jonathan 
   2376   1.1  jonathan 	if (ubsec_dma_malloc(sc, normbits / 8, &me->me_C, 0)) {
   2377   1.1  jonathan 		err = ENOMEM;
   2378   1.1  jonathan 		goto errout;
   2379   1.1  jonathan 	}
   2380  1.18    cegger 	memset(me->me_C.dma_vaddr, 0, me->me_C.dma_size);
   2381   1.1  jonathan 
   2382   1.1  jonathan 	ebits = ubsec_ksigbits(&krp->krp_param[UBS_MODEXP_PAR_E]);
   2383   1.1  jonathan 	if (ebits > nbits) {
   2384   1.1  jonathan 		err = E2BIG;
   2385   1.1  jonathan 		goto errout;
   2386   1.1  jonathan 	}
   2387   1.1  jonathan 	if (ubsec_dma_malloc(sc, normbits / 8, &me->me_E, 0)) {
   2388   1.1  jonathan 		err = ENOMEM;
   2389   1.1  jonathan 		goto errout;
   2390   1.1  jonathan 	}
   2391  1.18    cegger 	memset(me->me_E.dma_vaddr, 0, normbits / 8);
   2392   1.1  jonathan 	bcopy(krp->krp_param[UBS_MODEXP_PAR_E].crp_p,
   2393   1.1  jonathan 	    me->me_E.dma_vaddr, (ebits + 7) / 8);
   2394   1.1  jonathan 
   2395   1.1  jonathan 	if (ubsec_dma_malloc(sc, sizeof(struct ubsec_pktbuf),
   2396   1.1  jonathan 	    &me->me_epb, 0)) {
   2397   1.1  jonathan 		err = ENOMEM;
   2398   1.1  jonathan 		goto errout;
   2399   1.1  jonathan 	}
   2400   1.1  jonathan 	epb = (struct ubsec_pktbuf *)me->me_epb.dma_vaddr;
   2401   1.1  jonathan 	epb->pb_addr = htole32(me->me_E.dma_paddr);
   2402   1.1  jonathan 	epb->pb_next = 0;
   2403   1.1  jonathan 	epb->pb_len = htole32((ebits + 7) / 8);
   2404   1.1  jonathan 
   2405   1.1  jonathan #ifdef UBSEC_DEBUG
   2406   1.1  jonathan 	if (ubsec_debug) {
   2407   1.1  jonathan 		printf("Epb ");
   2408   1.1  jonathan 		ubsec_dump_pb(epb);
   2409   1.1  jonathan 	}
   2410   1.1  jonathan #endif
   2411   1.1  jonathan 
   2412   1.1  jonathan 	mcr->mcr_pkts = htole16(1);
   2413   1.1  jonathan 	mcr->mcr_flags = 0;
   2414   1.1  jonathan 	mcr->mcr_cmdctxp = htole32(me->me_q.q_ctx.dma_paddr);
   2415   1.1  jonathan 	mcr->mcr_reserved = 0;
   2416   1.1  jonathan 	mcr->mcr_pktlen = 0;
   2417   1.1  jonathan 
   2418   1.1  jonathan 	mcr->mcr_ipktbuf.pb_addr = htole32(me->me_M.dma_paddr);
   2419   1.1  jonathan 	mcr->mcr_ipktbuf.pb_len = htole32(normbits / 8);
   2420   1.1  jonathan 	mcr->mcr_ipktbuf.pb_next = htole32(me->me_epb.dma_paddr);
   2421   1.1  jonathan 
   2422   1.1  jonathan 	mcr->mcr_opktbuf.pb_addr = htole32(me->me_C.dma_paddr);
   2423   1.1  jonathan 	mcr->mcr_opktbuf.pb_next = 0;
   2424   1.1  jonathan 	mcr->mcr_opktbuf.pb_len = htole32(normbits / 8);
   2425   1.1  jonathan 
   2426   1.1  jonathan #ifdef DIAGNOSTIC
   2427   1.1  jonathan 	/* Misaligned output buffer will hang the chip. */
   2428   1.1  jonathan 	if ((letoh32(mcr->mcr_opktbuf.pb_addr) & 3) != 0)
   2429   1.1  jonathan 		panic("%s: modexp invalid addr 0x%x",
   2430  1.16    cegger 		    device_xname(&sc->sc_dv), letoh32(mcr->mcr_opktbuf.pb_addr));
   2431   1.1  jonathan 	if ((letoh32(mcr->mcr_opktbuf.pb_len) & 3) != 0)
   2432   1.1  jonathan 		panic("%s: modexp invalid len 0x%x",
   2433  1.16    cegger 		    device_xname(&sc->sc_dv), letoh32(mcr->mcr_opktbuf.pb_len));
   2434   1.1  jonathan #endif
   2435   1.1  jonathan 
   2436   1.1  jonathan 	ctx = (struct ubsec_ctx_modexp *)me->me_q.q_ctx.dma_vaddr;
   2437  1.18    cegger 	memset(ctx, 0, sizeof(*ctx));
   2438  1.20   tsutsui 	memcpy(ctx->me_N, krp->krp_param[UBS_MODEXP_PAR_N].crp_p,
   2439   1.1  jonathan 	    (nbits + 7) / 8);
   2440   1.1  jonathan 	ctx->me_len = htole16((normbits / 8) + (4 * sizeof(u_int16_t)));
   2441   1.1  jonathan 	ctx->me_op = htole16(UBS_CTXOP_MODEXP);
   2442   1.1  jonathan 	ctx->me_E_len = htole16(ebits);
   2443   1.1  jonathan 	ctx->me_N_len = htole16(nbits);
   2444   1.1  jonathan 
   2445   1.1  jonathan #ifdef UBSEC_DEBUG
   2446   1.1  jonathan 	if (ubsec_debug) {
   2447   1.1  jonathan 		ubsec_dump_mcr(mcr);
   2448   1.1  jonathan 		ubsec_dump_ctx2((struct ubsec_ctx_keyop *)ctx);
   2449   1.1  jonathan 	}
   2450   1.1  jonathan #endif
   2451   1.1  jonathan 
   2452   1.1  jonathan 	/*
   2453   1.1  jonathan 	 * ubsec_feed2 will sync mcr and ctx, we just need to sync
   2454   1.1  jonathan 	 * everything else.
   2455   1.1  jonathan 	 */
   2456   1.1  jonathan 	bus_dmamap_sync(sc->sc_dmat, me->me_M.dma_map,
   2457   1.1  jonathan 	    0, me->me_M.dma_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   2458   1.1  jonathan 	bus_dmamap_sync(sc->sc_dmat, me->me_E.dma_map,
   2459   1.1  jonathan 	    0, me->me_E.dma_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   2460   1.1  jonathan 	bus_dmamap_sync(sc->sc_dmat, me->me_C.dma_map,
   2461   1.1  jonathan 	    0, me->me_C.dma_map->dm_mapsize, BUS_DMASYNC_PREREAD);
   2462   1.1  jonathan 	bus_dmamap_sync(sc->sc_dmat, me->me_epb.dma_map,
   2463   1.1  jonathan 	    0, me->me_epb.dma_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   2464   1.1  jonathan 
   2465   1.1  jonathan 	/* Enqueue and we're done... */
   2466   1.1  jonathan 	s = splnet();
   2467   1.1  jonathan 	SIMPLEQ_INSERT_TAIL(&sc->sc_queue2, &me->me_q, q_next);
   2468   1.1  jonathan 	ubsec_feed2(sc);
   2469   1.1  jonathan 	splx(s);
   2470   1.1  jonathan 
   2471   1.1  jonathan 	return (0);
   2472   1.1  jonathan 
   2473   1.1  jonathan errout:
   2474   1.1  jonathan 	if (me != NULL) {
   2475   1.1  jonathan 		if (me->me_q.q_mcr.dma_map != NULL)
   2476   1.1  jonathan 			ubsec_dma_free(sc, &me->me_q.q_mcr);
   2477   1.1  jonathan 		if (me->me_q.q_ctx.dma_map != NULL) {
   2478  1.18    cegger 			memset(me->me_q.q_ctx.dma_vaddr, 0, me->me_q.q_ctx.dma_size);
   2479   1.1  jonathan 			ubsec_dma_free(sc, &me->me_q.q_ctx);
   2480   1.1  jonathan 		}
   2481   1.1  jonathan 		if (me->me_M.dma_map != NULL) {
   2482  1.18    cegger 			memset(me->me_M.dma_vaddr, 0, me->me_M.dma_size);
   2483   1.1  jonathan 			ubsec_dma_free(sc, &me->me_M);
   2484   1.1  jonathan 		}
   2485   1.1  jonathan 		if (me->me_E.dma_map != NULL) {
   2486  1.18    cegger 			memset(me->me_E.dma_vaddr, 0, me->me_E.dma_size);
   2487   1.1  jonathan 			ubsec_dma_free(sc, &me->me_E);
   2488   1.1  jonathan 		}
   2489   1.1  jonathan 		if (me->me_C.dma_map != NULL) {
   2490  1.18    cegger 			memset(me->me_C.dma_vaddr, 0, me->me_C.dma_size);
   2491   1.1  jonathan 			ubsec_dma_free(sc, &me->me_C);
   2492   1.1  jonathan 		}
   2493   1.1  jonathan 		if (me->me_epb.dma_map != NULL)
   2494   1.1  jonathan 			ubsec_dma_free(sc, &me->me_epb);
   2495   1.1  jonathan 		free(me, M_DEVBUF);
   2496   1.1  jonathan 	}
   2497   1.1  jonathan 	krp->krp_status = err;
   2498   1.1  jonathan 	crypto_kdone(krp);
   2499   1.1  jonathan 	return (0);
   2500   1.1  jonathan }
   2501   1.1  jonathan 
   2502   1.1  jonathan static int
   2503   1.1  jonathan ubsec_kprocess_rsapriv(struct ubsec_softc *sc, struct cryptkop *krp,
   2504  1.11  christos 		       int hint)
   2505   1.1  jonathan {
   2506   1.1  jonathan 	struct ubsec_q2_rsapriv *rp = NULL;
   2507   1.1  jonathan 	struct ubsec_mcr *mcr;
   2508   1.1  jonathan 	struct ubsec_ctx_rsapriv *ctx;
   2509   1.1  jonathan 	int s, err = 0;
   2510   1.1  jonathan 	u_int padlen, msglen;
   2511   1.1  jonathan 
   2512   1.1  jonathan 	msglen = ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_P]);
   2513   1.1  jonathan 	padlen = ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_Q]);
   2514   1.1  jonathan 	if (msglen > padlen)
   2515   1.1  jonathan 		padlen = msglen;
   2516   1.1  jonathan 
   2517   1.1  jonathan 	if (padlen <= 256)
   2518   1.1  jonathan 		padlen = 256;
   2519   1.1  jonathan 	else if (padlen <= 384)
   2520   1.1  jonathan 		padlen = 384;
   2521   1.1  jonathan 	else if (padlen <= 512)
   2522   1.1  jonathan 		padlen = 512;
   2523   1.1  jonathan 	else if (sc->sc_flags & UBS_FLAGS_BIGKEY && padlen <= 768)
   2524   1.1  jonathan 		padlen = 768;
   2525   1.1  jonathan 	else if (sc->sc_flags & UBS_FLAGS_BIGKEY && padlen <= 1024)
   2526   1.1  jonathan 		padlen = 1024;
   2527   1.1  jonathan 	else {
   2528   1.1  jonathan 		err = E2BIG;
   2529   1.1  jonathan 		goto errout;
   2530   1.1  jonathan 	}
   2531   1.1  jonathan 
   2532   1.1  jonathan 	if (ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_DP]) > padlen) {
   2533   1.1  jonathan 		err = E2BIG;
   2534   1.1  jonathan 		goto errout;
   2535   1.1  jonathan 	}
   2536   1.1  jonathan 
   2537   1.1  jonathan 	if (ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_DQ]) > padlen) {
   2538   1.1  jonathan 		err = E2BIG;
   2539   1.1  jonathan 		goto errout;
   2540   1.1  jonathan 	}
   2541   1.1  jonathan 
   2542   1.1  jonathan 	if (ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_PINV]) > padlen) {
   2543   1.1  jonathan 		err = E2BIG;
   2544   1.1  jonathan 		goto errout;
   2545   1.1  jonathan 	}
   2546   1.1  jonathan 
   2547  1.17    cegger 	rp = malloc(sizeof *rp, M_DEVBUF, M_NOWAIT|M_ZERO);
   2548   1.1  jonathan 	if (rp == NULL)
   2549   1.1  jonathan 		return (ENOMEM);
   2550   1.1  jonathan 	rp->rpr_krp = krp;
   2551   1.1  jonathan 	rp->rpr_q.q_type = UBS_CTXOP_RSAPRIV;
   2552   1.1  jonathan 
   2553   1.1  jonathan 	if (ubsec_dma_malloc(sc, sizeof(struct ubsec_mcr),
   2554   1.1  jonathan 	    &rp->rpr_q.q_mcr, 0)) {
   2555   1.1  jonathan 		err = ENOMEM;
   2556   1.1  jonathan 		goto errout;
   2557   1.1  jonathan 	}
   2558   1.1  jonathan 	mcr = (struct ubsec_mcr *)rp->rpr_q.q_mcr.dma_vaddr;
   2559   1.1  jonathan 
   2560   1.1  jonathan 	if (ubsec_dma_malloc(sc, sizeof(struct ubsec_ctx_rsapriv),
   2561   1.1  jonathan 	    &rp->rpr_q.q_ctx, 0)) {
   2562   1.1  jonathan 		err = ENOMEM;
   2563   1.1  jonathan 		goto errout;
   2564   1.1  jonathan 	}
   2565   1.1  jonathan 	ctx = (struct ubsec_ctx_rsapriv *)rp->rpr_q.q_ctx.dma_vaddr;
   2566  1.18    cegger 	memset(ctx, 0, sizeof *ctx);
   2567   1.1  jonathan 
   2568   1.1  jonathan 	/* Copy in p */
   2569   1.1  jonathan 	bcopy(krp->krp_param[UBS_RSAPRIV_PAR_P].crp_p,
   2570   1.1  jonathan 	    &ctx->rpr_buf[0 * (padlen / 8)],
   2571   1.1  jonathan 	    (krp->krp_param[UBS_RSAPRIV_PAR_P].crp_nbits + 7) / 8);
   2572   1.1  jonathan 
   2573   1.1  jonathan 	/* Copy in q */
   2574   1.1  jonathan 	bcopy(krp->krp_param[UBS_RSAPRIV_PAR_Q].crp_p,
   2575   1.1  jonathan 	    &ctx->rpr_buf[1 * (padlen / 8)],
   2576   1.1  jonathan 	    (krp->krp_param[UBS_RSAPRIV_PAR_Q].crp_nbits + 7) / 8);
   2577   1.1  jonathan 
   2578   1.1  jonathan 	/* Copy in dp */
   2579   1.1  jonathan 	bcopy(krp->krp_param[UBS_RSAPRIV_PAR_DP].crp_p,
   2580   1.1  jonathan 	    &ctx->rpr_buf[2 * (padlen / 8)],
   2581   1.1  jonathan 	    (krp->krp_param[UBS_RSAPRIV_PAR_DP].crp_nbits + 7) / 8);
   2582   1.1  jonathan 
   2583   1.1  jonathan 	/* Copy in dq */
   2584   1.1  jonathan 	bcopy(krp->krp_param[UBS_RSAPRIV_PAR_DQ].crp_p,
   2585   1.1  jonathan 	    &ctx->rpr_buf[3 * (padlen / 8)],
   2586   1.1  jonathan 	    (krp->krp_param[UBS_RSAPRIV_PAR_DQ].crp_nbits + 7) / 8);
   2587   1.1  jonathan 
   2588   1.1  jonathan 	/* Copy in pinv */
   2589   1.1  jonathan 	bcopy(krp->krp_param[UBS_RSAPRIV_PAR_PINV].crp_p,
   2590   1.1  jonathan 	    &ctx->rpr_buf[4 * (padlen / 8)],
   2591   1.1  jonathan 	    (krp->krp_param[UBS_RSAPRIV_PAR_PINV].crp_nbits + 7) / 8);
   2592   1.1  jonathan 
   2593   1.1  jonathan 	msglen = padlen * 2;
   2594   1.1  jonathan 
   2595   1.1  jonathan 	/* Copy in input message (aligned buffer/length). */
   2596   1.1  jonathan 	if (ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_MSGIN]) > msglen) {
   2597   1.1  jonathan 		/* Is this likely? */
   2598   1.1  jonathan 		err = E2BIG;
   2599   1.1  jonathan 		goto errout;
   2600   1.1  jonathan 	}
   2601   1.1  jonathan 	if (ubsec_dma_malloc(sc, (msglen + 7) / 8, &rp->rpr_msgin, 0)) {
   2602   1.1  jonathan 		err = ENOMEM;
   2603   1.1  jonathan 		goto errout;
   2604   1.1  jonathan 	}
   2605  1.18    cegger 	memset(rp->rpr_msgin.dma_vaddr, 0, (msglen + 7) / 8);
   2606   1.1  jonathan 	bcopy(krp->krp_param[UBS_RSAPRIV_PAR_MSGIN].crp_p,
   2607   1.1  jonathan 	    rp->rpr_msgin.dma_vaddr,
   2608   1.1  jonathan 	    (krp->krp_param[UBS_RSAPRIV_PAR_MSGIN].crp_nbits + 7) / 8);
   2609   1.1  jonathan 
   2610   1.1  jonathan 	/* Prepare space for output message (aligned buffer/length). */
   2611   1.1  jonathan 	if (ubsec_ksigbits(&krp->krp_param[UBS_RSAPRIV_PAR_MSGOUT]) < msglen) {
   2612   1.1  jonathan 		/* Is this likely? */
   2613   1.1  jonathan 		err = E2BIG;
   2614   1.1  jonathan 		goto errout;
   2615   1.1  jonathan 	}
   2616   1.1  jonathan 	if (ubsec_dma_malloc(sc, (msglen + 7) / 8, &rp->rpr_msgout, 0)) {
   2617   1.1  jonathan 		err = ENOMEM;
   2618   1.1  jonathan 		goto errout;
   2619   1.1  jonathan 	}
   2620  1.18    cegger 	memset(rp->rpr_msgout.dma_vaddr, 0, (msglen + 7) / 8);
   2621   1.1  jonathan 
   2622   1.1  jonathan 	mcr->mcr_pkts = htole16(1);
   2623   1.1  jonathan 	mcr->mcr_flags = 0;
   2624   1.1  jonathan 	mcr->mcr_cmdctxp = htole32(rp->rpr_q.q_ctx.dma_paddr);
   2625   1.1  jonathan 	mcr->mcr_ipktbuf.pb_addr = htole32(rp->rpr_msgin.dma_paddr);
   2626   1.1  jonathan 	mcr->mcr_ipktbuf.pb_next = 0;
   2627   1.1  jonathan 	mcr->mcr_ipktbuf.pb_len = htole32(rp->rpr_msgin.dma_size);
   2628   1.1  jonathan 	mcr->mcr_reserved = 0;
   2629   1.1  jonathan 	mcr->mcr_pktlen = htole16(msglen);
   2630   1.1  jonathan 	mcr->mcr_opktbuf.pb_addr = htole32(rp->rpr_msgout.dma_paddr);
   2631   1.1  jonathan 	mcr->mcr_opktbuf.pb_next = 0;
   2632   1.1  jonathan 	mcr->mcr_opktbuf.pb_len = htole32(rp->rpr_msgout.dma_size);
   2633   1.1  jonathan 
   2634   1.1  jonathan #ifdef DIAGNOSTIC
   2635   1.1  jonathan 	if (rp->rpr_msgin.dma_paddr & 3 || rp->rpr_msgin.dma_size & 3) {
   2636   1.3   thorpej 		panic("%s: rsapriv: invalid msgin 0x%lx(0x%lx)",
   2637  1.16    cegger 		    device_xname(&sc->sc_dv), (u_long) rp->rpr_msgin.dma_paddr,
   2638   1.3   thorpej 		    (u_long) rp->rpr_msgin.dma_size);
   2639   1.1  jonathan 	}
   2640   1.1  jonathan 	if (rp->rpr_msgout.dma_paddr & 3 || rp->rpr_msgout.dma_size & 3) {
   2641   1.3   thorpej 		panic("%s: rsapriv: invalid msgout 0x%lx(0x%lx)",
   2642  1.16    cegger 		    device_xname(&sc->sc_dv), (u_long) rp->rpr_msgout.dma_paddr,
   2643   1.3   thorpej 		    (u_long) rp->rpr_msgout.dma_size);
   2644   1.1  jonathan 	}
   2645   1.1  jonathan #endif
   2646   1.1  jonathan 
   2647   1.1  jonathan 	ctx->rpr_len = (sizeof(u_int16_t) * 4) + (5 * (padlen / 8));
   2648   1.1  jonathan 	ctx->rpr_op = htole16(UBS_CTXOP_RSAPRIV);
   2649   1.1  jonathan 	ctx->rpr_q_len = htole16(padlen);
   2650   1.1  jonathan 	ctx->rpr_p_len = htole16(padlen);
   2651   1.1  jonathan 
   2652   1.1  jonathan 	/*
   2653   1.1  jonathan 	 * ubsec_feed2 will sync mcr and ctx, we just need to sync
   2654   1.1  jonathan 	 * everything else.
   2655   1.1  jonathan 	 */
   2656   1.1  jonathan 	bus_dmamap_sync(sc->sc_dmat, rp->rpr_msgin.dma_map,
   2657   1.1  jonathan 	    0, rp->rpr_msgin.dma_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   2658   1.1  jonathan 	bus_dmamap_sync(sc->sc_dmat, rp->rpr_msgout.dma_map,
   2659   1.1  jonathan 	    0, rp->rpr_msgout.dma_map->dm_mapsize, BUS_DMASYNC_PREREAD);
   2660   1.1  jonathan 
   2661   1.1  jonathan 	/* Enqueue and we're done... */
   2662   1.1  jonathan 	s = splnet();
   2663   1.1  jonathan 	SIMPLEQ_INSERT_TAIL(&sc->sc_queue2, &rp->rpr_q, q_next);
   2664   1.1  jonathan 	ubsec_feed2(sc);
   2665   1.1  jonathan 	ubsecstats.hst_modexpcrt++;
   2666   1.1  jonathan 	splx(s);
   2667   1.1  jonathan 	return (0);
   2668   1.1  jonathan 
   2669   1.1  jonathan errout:
   2670   1.1  jonathan 	if (rp != NULL) {
   2671   1.1  jonathan 		if (rp->rpr_q.q_mcr.dma_map != NULL)
   2672   1.1  jonathan 			ubsec_dma_free(sc, &rp->rpr_q.q_mcr);
   2673   1.1  jonathan 		if (rp->rpr_msgin.dma_map != NULL) {
   2674  1.18    cegger 			memset(rp->rpr_msgin.dma_vaddr, 0, rp->rpr_msgin.dma_size);
   2675   1.1  jonathan 			ubsec_dma_free(sc, &rp->rpr_msgin);
   2676   1.1  jonathan 		}
   2677   1.1  jonathan 		if (rp->rpr_msgout.dma_map != NULL) {
   2678  1.18    cegger 			memset(rp->rpr_msgout.dma_vaddr, 0, rp->rpr_msgout.dma_size);
   2679   1.1  jonathan 			ubsec_dma_free(sc, &rp->rpr_msgout);
   2680   1.1  jonathan 		}
   2681   1.1  jonathan 		free(rp, M_DEVBUF);
   2682   1.1  jonathan 	}
   2683   1.1  jonathan 	krp->krp_status = err;
   2684   1.1  jonathan 	crypto_kdone(krp);
   2685   1.1  jonathan 	return (0);
   2686   1.1  jonathan }
   2687   1.1  jonathan 
   2688   1.1  jonathan #ifdef UBSEC_DEBUG
   2689   1.1  jonathan static void
   2690   1.1  jonathan ubsec_dump_pb(volatile struct ubsec_pktbuf *pb)
   2691   1.1  jonathan {
   2692   1.1  jonathan 	printf("addr 0x%x (0x%x) next 0x%x\n",
   2693   1.1  jonathan 	    pb->pb_addr, pb->pb_len, pb->pb_next);
   2694   1.1  jonathan }
   2695   1.1  jonathan 
   2696   1.1  jonathan static void
   2697   1.1  jonathan ubsec_dump_ctx2(volatile struct ubsec_ctx_keyop *c)
   2698   1.1  jonathan {
   2699   1.1  jonathan 	printf("CTX (0x%x):\n", c->ctx_len);
   2700   1.1  jonathan 	switch (letoh16(c->ctx_op)) {
   2701   1.1  jonathan 	case UBS_CTXOP_RNGBYPASS:
   2702   1.1  jonathan 	case UBS_CTXOP_RNGSHA1:
   2703   1.1  jonathan 		break;
   2704   1.1  jonathan 	case UBS_CTXOP_MODEXP:
   2705   1.1  jonathan 	{
   2706   1.1  jonathan 		struct ubsec_ctx_modexp *cx = (void *)c;
   2707   1.1  jonathan 		int i, len;
   2708   1.1  jonathan 
   2709   1.1  jonathan 		printf(" Elen %u, Nlen %u\n",
   2710   1.1  jonathan 		    letoh16(cx->me_E_len), letoh16(cx->me_N_len));
   2711   1.1  jonathan 		len = (cx->me_N_len + 7)/8;
   2712   1.1  jonathan 		for (i = 0; i < len; i++)
   2713   1.1  jonathan 			printf("%s%02x", (i == 0) ? " N: " : ":", cx->me_N[i]);
   2714   1.1  jonathan 		printf("\n");
   2715   1.1  jonathan 		break;
   2716   1.1  jonathan 	}
   2717   1.1  jonathan 	default:
   2718   1.1  jonathan 		printf("unknown context: %x\n", c->ctx_op);
   2719   1.1  jonathan 	}
   2720   1.1  jonathan 	printf("END CTX\n");
   2721   1.1  jonathan }
   2722   1.1  jonathan 
   2723   1.1  jonathan static void
   2724   1.1  jonathan ubsec_dump_mcr(struct ubsec_mcr *mcr)
   2725   1.1  jonathan {
   2726   1.1  jonathan 	volatile struct ubsec_mcr_add *ma;
   2727   1.1  jonathan 	int i;
   2728   1.1  jonathan 
   2729   1.1  jonathan 	printf("MCR:\n");
   2730   1.1  jonathan 	printf(" pkts: %u, flags 0x%x\n",
   2731   1.1  jonathan 	    letoh16(mcr->mcr_pkts), letoh16(mcr->mcr_flags));
   2732   1.1  jonathan 	ma = (volatile struct ubsec_mcr_add *)&mcr->mcr_cmdctxp;
   2733   1.1  jonathan 	for (i = 0; i < letoh16(mcr->mcr_pkts); i++) {
   2734   1.1  jonathan 		printf(" %d: ctx 0x%x len 0x%x rsvd 0x%x\n", i,
   2735   1.1  jonathan 		    letoh32(ma->mcr_cmdctxp), letoh16(ma->mcr_pktlen),
   2736   1.1  jonathan 		    letoh16(ma->mcr_reserved));
   2737   1.1  jonathan 		printf(" %d: ipkt ", i);
   2738   1.1  jonathan 		ubsec_dump_pb(&ma->mcr_ipktbuf);
   2739   1.1  jonathan 		printf(" %d: opkt ", i);
   2740   1.1  jonathan 		ubsec_dump_pb(&ma->mcr_opktbuf);
   2741   1.1  jonathan 		ma++;
   2742   1.1  jonathan 	}
   2743   1.1  jonathan 	printf("END MCR\n");
   2744   1.1  jonathan }
   2745   1.1  jonathan #endif /* UBSEC_DEBUG */
   2746   1.1  jonathan 
   2747   1.1  jonathan /*
   2748   1.1  jonathan  * Return the number of significant bits of a big number.
   2749   1.1  jonathan  */
   2750   1.1  jonathan static int
   2751   1.1  jonathan ubsec_ksigbits(struct crparam *cr)
   2752   1.1  jonathan {
   2753   1.1  jonathan 	u_int plen = (cr->crp_nbits + 7) / 8;
   2754   1.1  jonathan 	int i, sig = plen * 8;
   2755   1.1  jonathan 	u_int8_t c, *p = cr->crp_p;
   2756   1.1  jonathan 
   2757   1.1  jonathan 	for (i = plen - 1; i >= 0; i--) {
   2758   1.1  jonathan 		c = p[i];
   2759   1.1  jonathan 		if (c != 0) {
   2760   1.1  jonathan 			while ((c & 0x80) == 0) {
   2761   1.1  jonathan 				sig--;
   2762   1.1  jonathan 				c <<= 1;
   2763   1.1  jonathan 			}
   2764   1.1  jonathan 			break;
   2765   1.1  jonathan 		}
   2766   1.1  jonathan 		sig -= 8;
   2767   1.1  jonathan 	}
   2768   1.1  jonathan 	return (sig);
   2769   1.1  jonathan }
   2770   1.1  jonathan 
   2771   1.1  jonathan static void
   2772   1.7   thorpej ubsec_kshift_r(u_int shiftbits, u_int8_t *src, u_int srcbits,
   2773   1.7   thorpej     u_int8_t *dst, u_int dstbits)
   2774   1.1  jonathan {
   2775   1.1  jonathan 	u_int slen, dlen;
   2776   1.1  jonathan 	int i, si, di, n;
   2777   1.1  jonathan 
   2778   1.1  jonathan 	slen = (srcbits + 7) / 8;
   2779   1.1  jonathan 	dlen = (dstbits + 7) / 8;
   2780   1.1  jonathan 
   2781   1.1  jonathan 	for (i = 0; i < slen; i++)
   2782   1.1  jonathan 		dst[i] = src[i];
   2783   1.1  jonathan 	for (i = 0; i < dlen - slen; i++)
   2784   1.1  jonathan 		dst[slen + i] = 0;
   2785   1.1  jonathan 
   2786   1.1  jonathan 	n = shiftbits / 8;
   2787   1.1  jonathan 	if (n != 0) {
   2788   1.1  jonathan 		si = dlen - n - 1;
   2789   1.1  jonathan 		di = dlen - 1;
   2790   1.1  jonathan 		while (si >= 0)
   2791   1.1  jonathan 			dst[di--] = dst[si--];
   2792   1.1  jonathan 		while (di >= 0)
   2793   1.1  jonathan 			dst[di--] = 0;
   2794   1.1  jonathan 	}
   2795   1.1  jonathan 
   2796   1.1  jonathan 	n = shiftbits % 8;
   2797   1.1  jonathan 	if (n != 0) {
   2798   1.1  jonathan 		for (i = dlen - 1; i > 0; i--)
   2799   1.1  jonathan 			dst[i] = (dst[i] << n) |
   2800   1.1  jonathan 			    (dst[i - 1] >> (8 - n));
   2801   1.1  jonathan 		dst[0] = dst[0] << n;
   2802   1.1  jonathan 	}
   2803   1.1  jonathan }
   2804   1.1  jonathan 
   2805   1.1  jonathan static void
   2806   1.7   thorpej ubsec_kshift_l(u_int shiftbits, u_int8_t *src, u_int srcbits,
   2807   1.7   thorpej     u_int8_t *dst, u_int dstbits)
   2808   1.1  jonathan {
   2809   1.1  jonathan 	int slen, dlen, i, n;
   2810   1.1  jonathan 
   2811   1.1  jonathan 	slen = (srcbits + 7) / 8;
   2812   1.1  jonathan 	dlen = (dstbits + 7) / 8;
   2813   1.1  jonathan 
   2814   1.1  jonathan 	n = shiftbits / 8;
   2815   1.1  jonathan 	for (i = 0; i < slen; i++)
   2816   1.1  jonathan 		dst[i] = src[i + n];
   2817   1.1  jonathan 	for (i = 0; i < dlen - slen; i++)
   2818   1.1  jonathan 		dst[slen + i] = 0;
   2819   1.1  jonathan 
   2820   1.1  jonathan 	n = shiftbits % 8;
   2821   1.1  jonathan 	if (n != 0) {
   2822   1.1  jonathan 		for (i = 0; i < (dlen - 1); i++)
   2823   1.1  jonathan 			dst[i] = (dst[i] >> n) | (dst[i + 1] << (8 - n));
   2824   1.1  jonathan 		dst[dlen - 1] = dst[dlen - 1] >> n;
   2825   1.1  jonathan 	}
   2826   1.1  jonathan }
   2827