1b7e3f244SSam Leffler /*- 2718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3718cf2ccSPedro F. Giffuni * 4b7e3f244SSam Leffler * Copyright (c) 2003 Sam Leffler, Errno Consulting 5b7e3f244SSam Leffler * Copyright (c) 2003 Global Technology Associates, Inc. 6b7e3f244SSam Leffler * All rights reserved. 7b7e3f244SSam Leffler * 8b7e3f244SSam Leffler * Redistribution and use in source and binary forms, with or without 9b7e3f244SSam Leffler * modification, are permitted provided that the following conditions 10b7e3f244SSam Leffler * are met: 11b7e3f244SSam Leffler * 1. Redistributions of source code must retain the above copyright 12b7e3f244SSam Leffler * notice, this list of conditions and the following disclaimer. 13b7e3f244SSam Leffler * 2. Redistributions in binary form must reproduce the above copyright 14b7e3f244SSam Leffler * notice, this list of conditions and the following disclaimer in the 15b7e3f244SSam Leffler * documentation and/or other materials provided with the distribution. 16b7e3f244SSam Leffler * 17b7e3f244SSam Leffler * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18b7e3f244SSam Leffler * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19b7e3f244SSam Leffler * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20b7e3f244SSam Leffler * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21b7e3f244SSam Leffler * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22b7e3f244SSam Leffler * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23b7e3f244SSam Leffler * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24b7e3f244SSam Leffler * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25b7e3f244SSam Leffler * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26b7e3f244SSam Leffler * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27b7e3f244SSam Leffler * SUCH DAMAGE. 28b7e3f244SSam Leffler */ 29b7e3f244SSam Leffler 30b7e3f244SSam Leffler #include <sys/cdefs.h> 31b7e3f244SSam Leffler __FBSDID("$FreeBSD$"); 32b7e3f244SSam Leffler 33b7e3f244SSam Leffler /* 34b7e3f244SSam Leffler * SafeNet SafeXcel-1141 hardware crypto accelerator 35b7e3f244SSam Leffler */ 36b7e3f244SSam Leffler #include "opt_safe.h" 37b7e3f244SSam Leffler 38b7e3f244SSam Leffler #include <sys/param.h> 39b7e3f244SSam Leffler #include <sys/systm.h> 40b7e3f244SSam Leffler #include <sys/proc.h> 41b7e3f244SSam Leffler #include <sys/errno.h> 42b7e3f244SSam Leffler #include <sys/malloc.h> 43b7e3f244SSam Leffler #include <sys/kernel.h> 44b7e3f244SSam Leffler #include <sys/mbuf.h> 45fe12f24bSPoul-Henning Kamp #include <sys/module.h> 46b7e3f244SSam Leffler #include <sys/lock.h> 47b7e3f244SSam Leffler #include <sys/mutex.h> 48b7e3f244SSam Leffler #include <sys/sysctl.h> 49b7e3f244SSam Leffler #include <sys/endian.h> 50c0341432SJohn Baldwin #include <sys/uio.h> 51b7e3f244SSam Leffler 52b7e3f244SSam Leffler #include <vm/vm.h> 53b7e3f244SSam Leffler #include <vm/pmap.h> 54b7e3f244SSam Leffler 55b7e3f244SSam Leffler #include <machine/bus.h> 56b7e3f244SSam Leffler #include <machine/resource.h> 57b7e3f244SSam Leffler #include <sys/bus.h> 58b7e3f244SSam Leffler #include <sys/rman.h> 59b7e3f244SSam Leffler 60b7e3f244SSam Leffler #include <opencrypto/cryptodev.h> 61c0341432SJohn Baldwin #include <opencrypto/xform_auth.h> 62b7e3f244SSam Leffler #include <sys/random.h> 636810ad6fSSam Leffler #include <sys/kobj.h> 646810ad6fSSam Leffler 656810ad6fSSam Leffler #include "cryptodev_if.h" 66b7e3f244SSam Leffler 6790cf0136SWarner Losh #include <dev/pci/pcivar.h> 6890cf0136SWarner Losh #include <dev/pci/pcireg.h> 69b7e3f244SSam Leffler 70b7e3f244SSam Leffler #ifdef SAFE_RNDTEST 71b7e3f244SSam Leffler #include <dev/rndtest/rndtest.h> 72b7e3f244SSam Leffler #endif 73b7e3f244SSam Leffler #include <dev/safe/safereg.h> 74b7e3f244SSam Leffler #include <dev/safe/safevar.h> 75b7e3f244SSam Leffler 76b7e3f244SSam Leffler #ifndef bswap32 77b7e3f244SSam Leffler #define bswap32 NTOHL 78b7e3f244SSam Leffler #endif 79b7e3f244SSam Leffler 80b7e3f244SSam Leffler /* 81b7e3f244SSam Leffler * Prototypes and count for the pci_device structure 82b7e3f244SSam Leffler */ 83b7e3f244SSam Leffler static int safe_probe(device_t); 84b7e3f244SSam Leffler static int safe_attach(device_t); 85b7e3f244SSam Leffler static int safe_detach(device_t); 86b7e3f244SSam Leffler static int safe_suspend(device_t); 87b7e3f244SSam Leffler static int safe_resume(device_t); 88a6340ec8SWarner Losh static int safe_shutdown(device_t); 89b7e3f244SSam Leffler 90c0341432SJohn Baldwin static int safe_probesession(device_t, const struct crypto_session_params *); 91c0341432SJohn Baldwin static int safe_newsession(device_t, crypto_session_t, 92c0341432SJohn Baldwin const struct crypto_session_params *); 936810ad6fSSam Leffler static int safe_process(device_t, struct cryptop *, int); 946810ad6fSSam Leffler 95b7e3f244SSam Leffler static device_method_t safe_methods[] = { 96b7e3f244SSam Leffler /* Device interface */ 97b7e3f244SSam Leffler DEVMETHOD(device_probe, safe_probe), 98b7e3f244SSam Leffler DEVMETHOD(device_attach, safe_attach), 99b7e3f244SSam Leffler DEVMETHOD(device_detach, safe_detach), 100b7e3f244SSam Leffler DEVMETHOD(device_suspend, safe_suspend), 101b7e3f244SSam Leffler DEVMETHOD(device_resume, safe_resume), 102b7e3f244SSam Leffler DEVMETHOD(device_shutdown, safe_shutdown), 103b7e3f244SSam Leffler 1046810ad6fSSam Leffler /* crypto device methods */ 105c0341432SJohn Baldwin DEVMETHOD(cryptodev_probesession, safe_probesession), 1066810ad6fSSam Leffler DEVMETHOD(cryptodev_newsession, safe_newsession), 1076810ad6fSSam Leffler DEVMETHOD(cryptodev_process, safe_process), 1086810ad6fSSam Leffler 1094b7ec270SMarius Strobl DEVMETHOD_END 110b7e3f244SSam Leffler }; 111b7e3f244SSam Leffler static driver_t safe_driver = { 112b7e3f244SSam Leffler "safe", 113b7e3f244SSam Leffler safe_methods, 114b7e3f244SSam Leffler sizeof (struct safe_softc) 115b7e3f244SSam Leffler }; 116b7e3f244SSam Leffler static devclass_t safe_devclass; 117b7e3f244SSam Leffler 118b7e3f244SSam Leffler DRIVER_MODULE(safe, pci, safe_driver, safe_devclass, 0, 0); 119b7e3f244SSam Leffler MODULE_DEPEND(safe, crypto, 1, 1, 1); 120b7e3f244SSam Leffler #ifdef SAFE_RNDTEST 121b7e3f244SSam Leffler MODULE_DEPEND(safe, rndtest, 1, 1, 1); 122b7e3f244SSam Leffler #endif 123b7e3f244SSam Leffler 124b7e3f244SSam Leffler static void safe_intr(void *); 125b7e3f244SSam Leffler static void safe_callback(struct safe_softc *, struct safe_ringentry *); 126b7e3f244SSam Leffler static void safe_feed(struct safe_softc *, struct safe_ringentry *); 127b7e3f244SSam Leffler static void safe_mcopy(struct mbuf *, struct mbuf *, u_int); 128b7e3f244SSam Leffler #ifndef SAFE_NO_RNG 129b7e3f244SSam Leffler static void safe_rng_init(struct safe_softc *); 130b7e3f244SSam Leffler static void safe_rng(void *); 131b7e3f244SSam Leffler #endif /* SAFE_NO_RNG */ 132b7e3f244SSam Leffler static int safe_dma_malloc(struct safe_softc *, bus_size_t, 133b7e3f244SSam Leffler struct safe_dma_alloc *, int); 134b7e3f244SSam Leffler #define safe_dma_sync(_dma, _flags) \ 135b7e3f244SSam Leffler bus_dmamap_sync((_dma)->dma_tag, (_dma)->dma_map, (_flags)) 136b7e3f244SSam Leffler static void safe_dma_free(struct safe_softc *, struct safe_dma_alloc *); 137b7e3f244SSam Leffler static int safe_dmamap_aligned(const struct safe_operand *); 138b7e3f244SSam Leffler static int safe_dmamap_uniform(const struct safe_operand *); 139b7e3f244SSam Leffler 140b7e3f244SSam Leffler static void safe_reset_board(struct safe_softc *); 141b7e3f244SSam Leffler static void safe_init_board(struct safe_softc *); 142b7e3f244SSam Leffler static void safe_init_pciregs(device_t dev); 143b7e3f244SSam Leffler static void safe_cleanchip(struct safe_softc *); 144b7e3f244SSam Leffler static void safe_totalreset(struct safe_softc *); 145b7e3f244SSam Leffler 146b7e3f244SSam Leffler static int safe_free_entry(struct safe_softc *, struct safe_ringentry *); 147b7e3f244SSam Leffler 1487029da5cSPawel Biernacki static SYSCTL_NODE(_hw, OID_AUTO, safe, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 1496472ac3dSEd Schouten "SafeNet driver parameters"); 150b7e3f244SSam Leffler 151b7e3f244SSam Leffler #ifdef SAFE_DEBUG 152b7e3f244SSam Leffler static void safe_dump_dmastatus(struct safe_softc *, const char *); 153b7e3f244SSam Leffler static void safe_dump_ringstate(struct safe_softc *, const char *); 154b7e3f244SSam Leffler static void safe_dump_intrstate(struct safe_softc *, const char *); 155b7e3f244SSam Leffler static void safe_dump_request(struct safe_softc *, const char *, 156b7e3f244SSam Leffler struct safe_ringentry *); 157b7e3f244SSam Leffler 158b7e3f244SSam Leffler static struct safe_softc *safec; /* for use by hw.safe.dump */ 159b7e3f244SSam Leffler 160b7e3f244SSam Leffler static int safe_debug = 0; 161b7e3f244SSam Leffler SYSCTL_INT(_hw_safe, OID_AUTO, debug, CTLFLAG_RW, &safe_debug, 162b7e3f244SSam Leffler 0, "control debugging msgs"); 163b7e3f244SSam Leffler #define DPRINTF(_x) if (safe_debug) printf _x 164b7e3f244SSam Leffler #else 165b7e3f244SSam Leffler #define DPRINTF(_x) 166b7e3f244SSam Leffler #endif 167b7e3f244SSam Leffler 168b7e3f244SSam Leffler #define READ_REG(sc,r) \ 169b7e3f244SSam Leffler bus_space_read_4((sc)->sc_st, (sc)->sc_sh, (r)) 170b7e3f244SSam Leffler 171b7e3f244SSam Leffler #define WRITE_REG(sc,reg,val) \ 172b7e3f244SSam Leffler bus_space_write_4((sc)->sc_st, (sc)->sc_sh, reg, val) 173b7e3f244SSam Leffler 174b7e3f244SSam Leffler struct safe_stats safestats; 175b7e3f244SSam Leffler SYSCTL_STRUCT(_hw_safe, OID_AUTO, stats, CTLFLAG_RD, &safestats, 176b7e3f244SSam Leffler safe_stats, "driver statistics"); 177b7e3f244SSam Leffler #ifndef SAFE_NO_RNG 178b7e3f244SSam Leffler static int safe_rnginterval = 1; /* poll once a second */ 179b7e3f244SSam Leffler SYSCTL_INT(_hw_safe, OID_AUTO, rnginterval, CTLFLAG_RW, &safe_rnginterval, 180b7e3f244SSam Leffler 0, "RNG polling interval (secs)"); 181b7e3f244SSam Leffler static int safe_rngbufsize = 16; /* 64 bytes each poll */ 182b7e3f244SSam Leffler SYSCTL_INT(_hw_safe, OID_AUTO, rngbufsize, CTLFLAG_RW, &safe_rngbufsize, 183b7e3f244SSam Leffler 0, "RNG polling buffer size (32-bit words)"); 184b7e3f244SSam Leffler static int safe_rngmaxalarm = 8; /* max alarms before reset */ 185b7e3f244SSam Leffler SYSCTL_INT(_hw_safe, OID_AUTO, rngmaxalarm, CTLFLAG_RW, &safe_rngmaxalarm, 186b7e3f244SSam Leffler 0, "RNG max alarms before reset"); 187b7e3f244SSam Leffler #endif /* SAFE_NO_RNG */ 188b7e3f244SSam Leffler 189b7e3f244SSam Leffler static int 190b7e3f244SSam Leffler safe_probe(device_t dev) 191b7e3f244SSam Leffler { 192b7e3f244SSam Leffler if (pci_get_vendor(dev) == PCI_VENDOR_SAFENET && 193b7e3f244SSam Leffler pci_get_device(dev) == PCI_PRODUCT_SAFEXCEL) 194d2b677bbSWarner Losh return (BUS_PROBE_DEFAULT); 195b7e3f244SSam Leffler return (ENXIO); 196b7e3f244SSam Leffler } 197b7e3f244SSam Leffler 198b7e3f244SSam Leffler static const char* 199b7e3f244SSam Leffler safe_partname(struct safe_softc *sc) 200b7e3f244SSam Leffler { 201b7e3f244SSam Leffler /* XXX sprintf numbers when not decoded */ 202b7e3f244SSam Leffler switch (pci_get_vendor(sc->sc_dev)) { 203b7e3f244SSam Leffler case PCI_VENDOR_SAFENET: 204b7e3f244SSam Leffler switch (pci_get_device(sc->sc_dev)) { 205b7e3f244SSam Leffler case PCI_PRODUCT_SAFEXCEL: return "SafeNet SafeXcel-1141"; 206b7e3f244SSam Leffler } 207b7e3f244SSam Leffler return "SafeNet unknown-part"; 208b7e3f244SSam Leffler } 209b7e3f244SSam Leffler return "Unknown-vendor unknown-part"; 210b7e3f244SSam Leffler } 211b7e3f244SSam Leffler 212b7e3f244SSam Leffler #ifndef SAFE_NO_RNG 213b7e3f244SSam Leffler static void 214b7e3f244SSam Leffler default_harvest(struct rndtest_state *rsp, void *buf, u_int count) 215b7e3f244SSam Leffler { 216d1b06863SMark Murray /* MarkM: FIX!! Check that this does not swamp the harvester! */ 21719fa89e9SMark Murray random_harvest_queue(buf, count, RANDOM_PURE_SAFE); 218b7e3f244SSam Leffler } 219b7e3f244SSam Leffler #endif /* SAFE_NO_RNG */ 220b7e3f244SSam Leffler 221b7e3f244SSam Leffler static int 222b7e3f244SSam Leffler safe_attach(device_t dev) 223b7e3f244SSam Leffler { 224b7e3f244SSam Leffler struct safe_softc *sc = device_get_softc(dev); 225b7e3f244SSam Leffler u_int32_t raddr; 226c0341432SJohn Baldwin u_int32_t i; 227b7e3f244SSam Leffler int rid; 228b7e3f244SSam Leffler 229b7e3f244SSam Leffler bzero(sc, sizeof (*sc)); 230b7e3f244SSam Leffler sc->sc_dev = dev; 231b7e3f244SSam Leffler 232b7e3f244SSam Leffler /* XXX handle power management */ 233b7e3f244SSam Leffler 234c68534f1SScott Long pci_enable_busmaster(dev); 235b7e3f244SSam Leffler 236b7e3f244SSam Leffler /* 237b7e3f244SSam Leffler * Setup memory-mapping of PCI registers. 238b7e3f244SSam Leffler */ 239b7e3f244SSam Leffler rid = BS_BAR; 2405f96beb9SNate Lawson sc->sc_sr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 2415f96beb9SNate Lawson RF_ACTIVE); 242b7e3f244SSam Leffler if (sc->sc_sr == NULL) { 243b7e3f244SSam Leffler device_printf(dev, "cannot map register space\n"); 244b7e3f244SSam Leffler goto bad; 245b7e3f244SSam Leffler } 246b7e3f244SSam Leffler sc->sc_st = rman_get_bustag(sc->sc_sr); 247b7e3f244SSam Leffler sc->sc_sh = rman_get_bushandle(sc->sc_sr); 248b7e3f244SSam Leffler 249b7e3f244SSam Leffler /* 250b7e3f244SSam Leffler * Arrange interrupt line. 251b7e3f244SSam Leffler */ 252b7e3f244SSam Leffler rid = 0; 2535f96beb9SNate Lawson sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 2545f96beb9SNate Lawson RF_SHAREABLE|RF_ACTIVE); 255b7e3f244SSam Leffler if (sc->sc_irq == NULL) { 256b7e3f244SSam Leffler device_printf(dev, "could not map interrupt\n"); 257b7e3f244SSam Leffler goto bad1; 258b7e3f244SSam Leffler } 259b7e3f244SSam Leffler /* 260b7e3f244SSam Leffler * NB: Network code assumes we are blocked with splimp() 261b7e3f244SSam Leffler * so make sure the IRQ is mapped appropriately. 262b7e3f244SSam Leffler */ 263b7e3f244SSam Leffler if (bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_NET | INTR_MPSAFE, 264ef544f63SPaolo Pisati NULL, safe_intr, sc, &sc->sc_ih)) { 265b7e3f244SSam Leffler device_printf(dev, "could not establish interrupt\n"); 266b7e3f244SSam Leffler goto bad2; 267b7e3f244SSam Leffler } 268b7e3f244SSam Leffler 2691b0909d5SConrad Meyer sc->sc_cid = crypto_get_driverid(dev, sizeof(struct safe_session), 2701b0909d5SConrad Meyer CRYPTOCAP_F_HARDWARE); 271b7e3f244SSam Leffler if (sc->sc_cid < 0) { 272b7e3f244SSam Leffler device_printf(dev, "could not get crypto driver id\n"); 273b7e3f244SSam Leffler goto bad3; 274b7e3f244SSam Leffler } 275b7e3f244SSam Leffler 276b7e3f244SSam Leffler sc->sc_chiprev = READ_REG(sc, SAFE_DEVINFO) & 277b7e3f244SSam Leffler (SAFE_DEVINFO_REV_MAJ | SAFE_DEVINFO_REV_MIN); 278b7e3f244SSam Leffler 279b7e3f244SSam Leffler /* 280b7e3f244SSam Leffler * Setup DMA descriptor area. 281b7e3f244SSam Leffler */ 28262ce43ccSScott Long if (bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */ 283b7e3f244SSam Leffler 1, /* alignment */ 284b7e3f244SSam Leffler SAFE_DMA_BOUNDARY, /* boundary */ 285b7e3f244SSam Leffler BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 286b7e3f244SSam Leffler BUS_SPACE_MAXADDR, /* highaddr */ 287b7e3f244SSam Leffler NULL, NULL, /* filter, filterarg */ 288b7e3f244SSam Leffler SAFE_MAX_DMA, /* maxsize */ 289b7e3f244SSam Leffler SAFE_MAX_PART, /* nsegments */ 290b7e3f244SSam Leffler SAFE_MAX_SSIZE, /* maxsegsize */ 291b7e3f244SSam Leffler BUS_DMA_ALLOCNOW, /* flags */ 292b7e3f244SSam Leffler NULL, NULL, /* locking */ 293b7e3f244SSam Leffler &sc->sc_srcdmat)) { 294b7e3f244SSam Leffler device_printf(dev, "cannot allocate DMA tag\n"); 295b7e3f244SSam Leffler goto bad4; 296b7e3f244SSam Leffler } 29762ce43ccSScott Long if (bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */ 29888bba874SSam Leffler 1, /* alignment */ 299b7e3f244SSam Leffler SAFE_MAX_DSIZE, /* boundary */ 300b7e3f244SSam Leffler BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 301b7e3f244SSam Leffler BUS_SPACE_MAXADDR, /* highaddr */ 302b7e3f244SSam Leffler NULL, NULL, /* filter, filterarg */ 303b7e3f244SSam Leffler SAFE_MAX_DMA, /* maxsize */ 304b7e3f244SSam Leffler SAFE_MAX_PART, /* nsegments */ 305b7e3f244SSam Leffler SAFE_MAX_DSIZE, /* maxsegsize */ 306b7e3f244SSam Leffler BUS_DMA_ALLOCNOW, /* flags */ 307b7e3f244SSam Leffler NULL, NULL, /* locking */ 308b7e3f244SSam Leffler &sc->sc_dstdmat)) { 309b7e3f244SSam Leffler device_printf(dev, "cannot allocate DMA tag\n"); 310b7e3f244SSam Leffler goto bad4; 311b7e3f244SSam Leffler } 312b7e3f244SSam Leffler 313b7e3f244SSam Leffler /* 314b7e3f244SSam Leffler * Allocate packet engine descriptors. 315b7e3f244SSam Leffler */ 316b7e3f244SSam Leffler if (safe_dma_malloc(sc, 317b7e3f244SSam Leffler SAFE_MAX_NQUEUE * sizeof (struct safe_ringentry), 318b7e3f244SSam Leffler &sc->sc_ringalloc, 0)) { 319b7e3f244SSam Leffler device_printf(dev, "cannot allocate PE descriptor ring\n"); 320b7e3f244SSam Leffler bus_dma_tag_destroy(sc->sc_srcdmat); 321b7e3f244SSam Leffler goto bad4; 322b7e3f244SSam Leffler } 323b7e3f244SSam Leffler /* 324b7e3f244SSam Leffler * Hookup the static portion of all our data structures. 325b7e3f244SSam Leffler */ 326b7e3f244SSam Leffler sc->sc_ring = (struct safe_ringentry *) sc->sc_ringalloc.dma_vaddr; 327b7e3f244SSam Leffler sc->sc_ringtop = sc->sc_ring + SAFE_MAX_NQUEUE; 328b7e3f244SSam Leffler sc->sc_front = sc->sc_ring; 329b7e3f244SSam Leffler sc->sc_back = sc->sc_ring; 330b7e3f244SSam Leffler raddr = sc->sc_ringalloc.dma_paddr; 331b7e3f244SSam Leffler bzero(sc->sc_ring, SAFE_MAX_NQUEUE * sizeof(struct safe_ringentry)); 332b7e3f244SSam Leffler for (i = 0; i < SAFE_MAX_NQUEUE; i++) { 333b7e3f244SSam Leffler struct safe_ringentry *re = &sc->sc_ring[i]; 334b7e3f244SSam Leffler 335b7e3f244SSam Leffler re->re_desc.d_sa = raddr + 336b7e3f244SSam Leffler offsetof(struct safe_ringentry, re_sa); 337b7e3f244SSam Leffler re->re_sa.sa_staterec = raddr + 338b7e3f244SSam Leffler offsetof(struct safe_ringentry, re_sastate); 339b7e3f244SSam Leffler 340b7e3f244SSam Leffler raddr += sizeof (struct safe_ringentry); 341b7e3f244SSam Leffler } 342b7e3f244SSam Leffler mtx_init(&sc->sc_ringmtx, device_get_nameunit(dev), 343b7e3f244SSam Leffler "packet engine ring", MTX_DEF); 344b7e3f244SSam Leffler 345b7e3f244SSam Leffler /* 346b7e3f244SSam Leffler * Allocate scatter and gather particle descriptors. 347b7e3f244SSam Leffler */ 348b7e3f244SSam Leffler if (safe_dma_malloc(sc, SAFE_TOTAL_SPART * sizeof (struct safe_pdesc), 349b7e3f244SSam Leffler &sc->sc_spalloc, 0)) { 350b7e3f244SSam Leffler device_printf(dev, "cannot allocate source particle " 351b7e3f244SSam Leffler "descriptor ring\n"); 352b7e3f244SSam Leffler mtx_destroy(&sc->sc_ringmtx); 353b7e3f244SSam Leffler safe_dma_free(sc, &sc->sc_ringalloc); 354b7e3f244SSam Leffler bus_dma_tag_destroy(sc->sc_srcdmat); 355b7e3f244SSam Leffler goto bad4; 356b7e3f244SSam Leffler } 357b7e3f244SSam Leffler sc->sc_spring = (struct safe_pdesc *) sc->sc_spalloc.dma_vaddr; 358b7e3f244SSam Leffler sc->sc_springtop = sc->sc_spring + SAFE_TOTAL_SPART; 359b7e3f244SSam Leffler sc->sc_spfree = sc->sc_spring; 360b7e3f244SSam Leffler bzero(sc->sc_spring, SAFE_TOTAL_SPART * sizeof(struct safe_pdesc)); 361b7e3f244SSam Leffler 362b7e3f244SSam Leffler if (safe_dma_malloc(sc, SAFE_TOTAL_DPART * sizeof (struct safe_pdesc), 363b7e3f244SSam Leffler &sc->sc_dpalloc, 0)) { 364b7e3f244SSam Leffler device_printf(dev, "cannot allocate destination particle " 365b7e3f244SSam Leffler "descriptor ring\n"); 366b7e3f244SSam Leffler mtx_destroy(&sc->sc_ringmtx); 367b7e3f244SSam Leffler safe_dma_free(sc, &sc->sc_spalloc); 368b7e3f244SSam Leffler safe_dma_free(sc, &sc->sc_ringalloc); 369b7e3f244SSam Leffler bus_dma_tag_destroy(sc->sc_dstdmat); 370b7e3f244SSam Leffler goto bad4; 371b7e3f244SSam Leffler } 372b7e3f244SSam Leffler sc->sc_dpring = (struct safe_pdesc *) sc->sc_dpalloc.dma_vaddr; 373b7e3f244SSam Leffler sc->sc_dpringtop = sc->sc_dpring + SAFE_TOTAL_DPART; 374b7e3f244SSam Leffler sc->sc_dpfree = sc->sc_dpring; 375b7e3f244SSam Leffler bzero(sc->sc_dpring, SAFE_TOTAL_DPART * sizeof(struct safe_pdesc)); 376b7e3f244SSam Leffler 377b7e3f244SSam Leffler device_printf(sc->sc_dev, "%s", safe_partname(sc)); 378b7e3f244SSam Leffler 379c0341432SJohn Baldwin sc->sc_devinfo = READ_REG(sc, SAFE_DEVINFO); 380c0341432SJohn Baldwin if (sc->sc_devinfo & SAFE_DEVINFO_RNG) { 381b7e3f244SSam Leffler sc->sc_flags |= SAFE_FLAGS_RNG; 382b7e3f244SSam Leffler printf(" rng"); 383b7e3f244SSam Leffler } 384c0341432SJohn Baldwin if (sc->sc_devinfo & SAFE_DEVINFO_PKEY) { 385b7e3f244SSam Leffler #if 0 386b7e3f244SSam Leffler printf(" key"); 387b7e3f244SSam Leffler sc->sc_flags |= SAFE_FLAGS_KEY; 388b7e3f244SSam Leffler #endif 389b7e3f244SSam Leffler } 390c0341432SJohn Baldwin if (sc->sc_devinfo & SAFE_DEVINFO_DES) { 391b7e3f244SSam Leffler printf(" des/3des"); 392b7e3f244SSam Leffler } 393c0341432SJohn Baldwin if (sc->sc_devinfo & SAFE_DEVINFO_AES) { 394b7e3f244SSam Leffler printf(" aes"); 395b7e3f244SSam Leffler } 396c0341432SJohn Baldwin if (sc->sc_devinfo & SAFE_DEVINFO_MD5) { 397b7e3f244SSam Leffler printf(" md5"); 398b7e3f244SSam Leffler } 399c0341432SJohn Baldwin if (sc->sc_devinfo & SAFE_DEVINFO_SHA1) { 400b7e3f244SSam Leffler printf(" sha1"); 401b7e3f244SSam Leffler } 402b7e3f244SSam Leffler /* XXX other supported algorithms */ 403b7e3f244SSam Leffler printf("\n"); 404b7e3f244SSam Leffler 405b7e3f244SSam Leffler safe_reset_board(sc); /* reset h/w */ 406b7e3f244SSam Leffler safe_init_pciregs(dev); /* init pci settings */ 407b7e3f244SSam Leffler safe_init_board(sc); /* init h/w */ 408b7e3f244SSam Leffler 409b7e3f244SSam Leffler #ifndef SAFE_NO_RNG 410b7e3f244SSam Leffler if (sc->sc_flags & SAFE_FLAGS_RNG) { 411b7e3f244SSam Leffler #ifdef SAFE_RNDTEST 412b7e3f244SSam Leffler sc->sc_rndtest = rndtest_attach(dev); 413b7e3f244SSam Leffler if (sc->sc_rndtest) 414b7e3f244SSam Leffler sc->sc_harvest = rndtest_harvest; 415b7e3f244SSam Leffler else 416b7e3f244SSam Leffler sc->sc_harvest = default_harvest; 417b7e3f244SSam Leffler #else 418b7e3f244SSam Leffler sc->sc_harvest = default_harvest; 419b7e3f244SSam Leffler #endif 420b7e3f244SSam Leffler safe_rng_init(sc); 421b7e3f244SSam Leffler 422fd90e2edSJung-uk Kim callout_init(&sc->sc_rngto, 1); 423b7e3f244SSam Leffler callout_reset(&sc->sc_rngto, hz*safe_rnginterval, safe_rng, sc); 424b7e3f244SSam Leffler } 425b7e3f244SSam Leffler #endif /* SAFE_NO_RNG */ 426b7e3f244SSam Leffler #ifdef SAFE_DEBUG 427b7e3f244SSam Leffler safec = sc; /* for use by hw.safe.dump */ 428b7e3f244SSam Leffler #endif 429b7e3f244SSam Leffler return (0); 430b7e3f244SSam Leffler bad4: 431b7e3f244SSam Leffler crypto_unregister_all(sc->sc_cid); 432b7e3f244SSam Leffler bad3: 433b7e3f244SSam Leffler bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih); 434b7e3f244SSam Leffler bad2: 435b7e3f244SSam Leffler bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq); 436b7e3f244SSam Leffler bad1: 437b7e3f244SSam Leffler bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, sc->sc_sr); 438b7e3f244SSam Leffler bad: 439b7e3f244SSam Leffler return (ENXIO); 440b7e3f244SSam Leffler } 441b7e3f244SSam Leffler 442b7e3f244SSam Leffler /* 443b7e3f244SSam Leffler * Detach a device that successfully probed. 444b7e3f244SSam Leffler */ 445b7e3f244SSam Leffler static int 446b7e3f244SSam Leffler safe_detach(device_t dev) 447b7e3f244SSam Leffler { 448b7e3f244SSam Leffler struct safe_softc *sc = device_get_softc(dev); 449b7e3f244SSam Leffler 450b7e3f244SSam Leffler /* XXX wait/abort active ops */ 451b7e3f244SSam Leffler 452b7e3f244SSam Leffler WRITE_REG(sc, SAFE_HI_MASK, 0); /* disable interrupts */ 453b7e3f244SSam Leffler 454b7e3f244SSam Leffler callout_stop(&sc->sc_rngto); 455b7e3f244SSam Leffler 456b7e3f244SSam Leffler crypto_unregister_all(sc->sc_cid); 457b7e3f244SSam Leffler 458b7e3f244SSam Leffler #ifdef SAFE_RNDTEST 459b7e3f244SSam Leffler if (sc->sc_rndtest) 460b7e3f244SSam Leffler rndtest_detach(sc->sc_rndtest); 461b7e3f244SSam Leffler #endif 462b7e3f244SSam Leffler 463b7e3f244SSam Leffler safe_cleanchip(sc); 464b7e3f244SSam Leffler safe_dma_free(sc, &sc->sc_dpalloc); 465b7e3f244SSam Leffler safe_dma_free(sc, &sc->sc_spalloc); 466b7e3f244SSam Leffler mtx_destroy(&sc->sc_ringmtx); 467b7e3f244SSam Leffler safe_dma_free(sc, &sc->sc_ringalloc); 468b7e3f244SSam Leffler 469b7e3f244SSam Leffler bus_generic_detach(dev); 470b7e3f244SSam Leffler bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih); 471b7e3f244SSam Leffler bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq); 472b7e3f244SSam Leffler 473b7e3f244SSam Leffler bus_dma_tag_destroy(sc->sc_srcdmat); 474b7e3f244SSam Leffler bus_dma_tag_destroy(sc->sc_dstdmat); 475b7e3f244SSam Leffler bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, sc->sc_sr); 476b7e3f244SSam Leffler 477b7e3f244SSam Leffler return (0); 478b7e3f244SSam Leffler } 479b7e3f244SSam Leffler 480b7e3f244SSam Leffler /* 481b7e3f244SSam Leffler * Stop all chip i/o so that the kernel's probe routines don't 482b7e3f244SSam Leffler * get confused by errant DMAs when rebooting. 483b7e3f244SSam Leffler */ 484a6340ec8SWarner Losh static int 485b7e3f244SSam Leffler safe_shutdown(device_t dev) 486b7e3f244SSam Leffler { 487b7e3f244SSam Leffler #ifdef notyet 488b7e3f244SSam Leffler safe_stop(device_get_softc(dev)); 489b7e3f244SSam Leffler #endif 490a6340ec8SWarner Losh return (0); 491b7e3f244SSam Leffler } 492b7e3f244SSam Leffler 493b7e3f244SSam Leffler /* 494b7e3f244SSam Leffler * Device suspend routine. 495b7e3f244SSam Leffler */ 496b7e3f244SSam Leffler static int 497b7e3f244SSam Leffler safe_suspend(device_t dev) 498b7e3f244SSam Leffler { 499b7e3f244SSam Leffler struct safe_softc *sc = device_get_softc(dev); 500b7e3f244SSam Leffler 501b7e3f244SSam Leffler #ifdef notyet 502b7e3f244SSam Leffler /* XXX stop the device and save PCI settings */ 503b7e3f244SSam Leffler #endif 504b7e3f244SSam Leffler sc->sc_suspended = 1; 505b7e3f244SSam Leffler 506b7e3f244SSam Leffler return (0); 507b7e3f244SSam Leffler } 508b7e3f244SSam Leffler 509b7e3f244SSam Leffler static int 510b7e3f244SSam Leffler safe_resume(device_t dev) 511b7e3f244SSam Leffler { 512b7e3f244SSam Leffler struct safe_softc *sc = device_get_softc(dev); 513b7e3f244SSam Leffler 514b7e3f244SSam Leffler #ifdef notyet 515b7e3f244SSam Leffler /* XXX retore PCI settings and start the device */ 516b7e3f244SSam Leffler #endif 517b7e3f244SSam Leffler sc->sc_suspended = 0; 518b7e3f244SSam Leffler return (0); 519b7e3f244SSam Leffler } 520b7e3f244SSam Leffler 521b7e3f244SSam Leffler /* 522b7e3f244SSam Leffler * SafeXcel Interrupt routine 523b7e3f244SSam Leffler */ 524b7e3f244SSam Leffler static void 525b7e3f244SSam Leffler safe_intr(void *arg) 526b7e3f244SSam Leffler { 527b7e3f244SSam Leffler struct safe_softc *sc = arg; 528b7e3f244SSam Leffler volatile u_int32_t stat; 529b7e3f244SSam Leffler 530b7e3f244SSam Leffler stat = READ_REG(sc, SAFE_HM_STAT); 531b7e3f244SSam Leffler if (stat == 0) /* shared irq, not for us */ 532b7e3f244SSam Leffler return; 533b7e3f244SSam Leffler 534b7e3f244SSam Leffler WRITE_REG(sc, SAFE_HI_CLR, stat); /* IACK */ 535b7e3f244SSam Leffler 536b7e3f244SSam Leffler if ((stat & SAFE_INT_PE_DDONE)) { 537b7e3f244SSam Leffler /* 538b7e3f244SSam Leffler * Descriptor(s) done; scan the ring and 539b7e3f244SSam Leffler * process completed operations. 540b7e3f244SSam Leffler */ 541b7e3f244SSam Leffler mtx_lock(&sc->sc_ringmtx); 542b7e3f244SSam Leffler while (sc->sc_back != sc->sc_front) { 543b7e3f244SSam Leffler struct safe_ringentry *re = sc->sc_back; 544b7e3f244SSam Leffler #ifdef SAFE_DEBUG 545b7e3f244SSam Leffler if (safe_debug) { 546b7e3f244SSam Leffler safe_dump_ringstate(sc, __func__); 547b7e3f244SSam Leffler safe_dump_request(sc, __func__, re); 548b7e3f244SSam Leffler } 549b7e3f244SSam Leffler #endif 550b7e3f244SSam Leffler /* 551b7e3f244SSam Leffler * safe_process marks ring entries that were allocated 552b7e3f244SSam Leffler * but not used with a csr of zero. This insures the 553b7e3f244SSam Leffler * ring front pointer never needs to be set backwards 554b7e3f244SSam Leffler * in the event that an entry is allocated but not used 555b7e3f244SSam Leffler * because of a setup error. 556b7e3f244SSam Leffler */ 557b7e3f244SSam Leffler if (re->re_desc.d_csr != 0) { 558b7e3f244SSam Leffler if (!SAFE_PE_CSR_IS_DONE(re->re_desc.d_csr)) 559b7e3f244SSam Leffler break; 560b7e3f244SSam Leffler if (!SAFE_PE_LEN_IS_DONE(re->re_desc.d_len)) 561b7e3f244SSam Leffler break; 562b7e3f244SSam Leffler sc->sc_nqchip--; 563b7e3f244SSam Leffler safe_callback(sc, re); 564b7e3f244SSam Leffler } 565b7e3f244SSam Leffler if (++(sc->sc_back) == sc->sc_ringtop) 566b7e3f244SSam Leffler sc->sc_back = sc->sc_ring; 567b7e3f244SSam Leffler } 568b7e3f244SSam Leffler mtx_unlock(&sc->sc_ringmtx); 569b7e3f244SSam Leffler } 570b7e3f244SSam Leffler 571b7e3f244SSam Leffler /* 572b7e3f244SSam Leffler * Check to see if we got any DMA Error 573b7e3f244SSam Leffler */ 574b7e3f244SSam Leffler if (stat & SAFE_INT_PE_ERROR) { 575b7e3f244SSam Leffler DPRINTF(("dmaerr dmastat %08x\n", 576b7e3f244SSam Leffler READ_REG(sc, SAFE_PE_DMASTAT))); 577b7e3f244SSam Leffler safestats.st_dmaerr++; 578b7e3f244SSam Leffler safe_totalreset(sc); 579b7e3f244SSam Leffler #if 0 580b7e3f244SSam Leffler safe_feed(sc); 581b7e3f244SSam Leffler #endif 582b7e3f244SSam Leffler } 583b7e3f244SSam Leffler 584b7e3f244SSam Leffler if (sc->sc_needwakeup) { /* XXX check high watermark */ 58576681661SJohn Baldwin int wakeup = sc->sc_needwakeup & CRYPTO_SYMQ; 586b7e3f244SSam Leffler DPRINTF(("%s: wakeup crypto %x\n", __func__, 587b7e3f244SSam Leffler sc->sc_needwakeup)); 588b7e3f244SSam Leffler sc->sc_needwakeup &= ~wakeup; 589b7e3f244SSam Leffler crypto_unblock(sc->sc_cid, wakeup); 590b7e3f244SSam Leffler } 591b7e3f244SSam Leffler } 592b7e3f244SSam Leffler 593b7e3f244SSam Leffler /* 594b7e3f244SSam Leffler * safe_feed() - post a request to chip 595b7e3f244SSam Leffler */ 596b7e3f244SSam Leffler static void 597b7e3f244SSam Leffler safe_feed(struct safe_softc *sc, struct safe_ringentry *re) 598b7e3f244SSam Leffler { 599b7e3f244SSam Leffler bus_dmamap_sync(sc->sc_srcdmat, re->re_src_map, BUS_DMASYNC_PREWRITE); 600b7e3f244SSam Leffler if (re->re_dst_map != NULL) 601b7e3f244SSam Leffler bus_dmamap_sync(sc->sc_dstdmat, re->re_dst_map, 602b7e3f244SSam Leffler BUS_DMASYNC_PREREAD); 603b7e3f244SSam Leffler /* XXX have no smaller granularity */ 604b7e3f244SSam Leffler safe_dma_sync(&sc->sc_ringalloc, 605b7e3f244SSam Leffler BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 606b7e3f244SSam Leffler safe_dma_sync(&sc->sc_spalloc, BUS_DMASYNC_PREWRITE); 607b7e3f244SSam Leffler safe_dma_sync(&sc->sc_dpalloc, BUS_DMASYNC_PREWRITE); 608b7e3f244SSam Leffler 609b7e3f244SSam Leffler #ifdef SAFE_DEBUG 610b7e3f244SSam Leffler if (safe_debug) { 611b7e3f244SSam Leffler safe_dump_ringstate(sc, __func__); 612b7e3f244SSam Leffler safe_dump_request(sc, __func__, re); 613b7e3f244SSam Leffler } 614b7e3f244SSam Leffler #endif 615b7e3f244SSam Leffler sc->sc_nqchip++; 616b7e3f244SSam Leffler if (sc->sc_nqchip > safestats.st_maxqchip) 617b7e3f244SSam Leffler safestats.st_maxqchip = sc->sc_nqchip; 618b7e3f244SSam Leffler /* poke h/w to check descriptor ring, any value can be written */ 619b7e3f244SSam Leffler WRITE_REG(sc, SAFE_HI_RD_DESCR, 0); 620b7e3f244SSam Leffler } 621b7e3f244SSam Leffler 6229a2f6061SPawel Jakub Dawidek #define N(a) (sizeof(a) / sizeof (a[0])) 6239a2f6061SPawel Jakub Dawidek static void 624c0341432SJohn Baldwin safe_setup_enckey(struct safe_session *ses, const void *key) 6259a2f6061SPawel Jakub Dawidek { 6269a2f6061SPawel Jakub Dawidek int i; 6279a2f6061SPawel Jakub Dawidek 628c0341432SJohn Baldwin bcopy(key, ses->ses_key, ses->ses_klen); 6299a2f6061SPawel Jakub Dawidek 6309a2f6061SPawel Jakub Dawidek /* PE is little-endian, insure proper byte order */ 6319a2f6061SPawel Jakub Dawidek for (i = 0; i < N(ses->ses_key); i++) 6329a2f6061SPawel Jakub Dawidek ses->ses_key[i] = htole32(ses->ses_key[i]); 6339a2f6061SPawel Jakub Dawidek } 6349a2f6061SPawel Jakub Dawidek 6359a2f6061SPawel Jakub Dawidek static void 636c0341432SJohn Baldwin safe_setup_mackey(struct safe_session *ses, int algo, const uint8_t *key, 637c0341432SJohn Baldwin int klen) 6389a2f6061SPawel Jakub Dawidek { 6399a2f6061SPawel Jakub Dawidek SHA1_CTX sha1ctx; 6409a2f6061SPawel Jakub Dawidek int i; 6419a2f6061SPawel Jakub Dawidek 642c0341432SJohn Baldwin hmac_init_ipad(&auth_hash_hmac_sha1, key, klen, &sha1ctx); 643c0341432SJohn Baldwin bcopy(sha1ctx.h.b32, ses->ses_hminner, sizeof(sha1ctx.h.b32)); 644c0341432SJohn Baldwin 645c0341432SJohn Baldwin hmac_init_opad(&auth_hash_hmac_sha1, key, klen, &sha1ctx); 646c0341432SJohn Baldwin bcopy(sha1ctx.h.b32, ses->ses_hmouter, sizeof(sha1ctx.h.b32)); 647c0341432SJohn Baldwin 648c0341432SJohn Baldwin explicit_bzero(&sha1ctx, sizeof(sha1ctx)); 6499a2f6061SPawel Jakub Dawidek 6509a2f6061SPawel Jakub Dawidek /* PE is little-endian, insure proper byte order */ 6519a2f6061SPawel Jakub Dawidek for (i = 0; i < N(ses->ses_hminner); i++) { 6529a2f6061SPawel Jakub Dawidek ses->ses_hminner[i] = htole32(ses->ses_hminner[i]); 6539a2f6061SPawel Jakub Dawidek ses->ses_hmouter[i] = htole32(ses->ses_hmouter[i]); 6549a2f6061SPawel Jakub Dawidek } 6559a2f6061SPawel Jakub Dawidek } 6569a2f6061SPawel Jakub Dawidek #undef N 6579a2f6061SPawel Jakub Dawidek 658c0341432SJohn Baldwin static bool 659c0341432SJohn Baldwin safe_auth_supported(struct safe_softc *sc, 660c0341432SJohn Baldwin const struct crypto_session_params *csp) 661b7e3f244SSam Leffler { 662b7e3f244SSam Leffler 663c0341432SJohn Baldwin switch (csp->csp_auth_alg) { 664c0341432SJohn Baldwin case CRYPTO_SHA1_HMAC: 665c0341432SJohn Baldwin if ((sc->sc_devinfo & SAFE_DEVINFO_SHA1) == 0) 666c0341432SJohn Baldwin return (false); 667c0341432SJohn Baldwin break; 668c0341432SJohn Baldwin default: 669c0341432SJohn Baldwin return (false); 670c0341432SJohn Baldwin } 671c0341432SJohn Baldwin return (true); 672c0341432SJohn Baldwin } 673c0341432SJohn Baldwin 674c0341432SJohn Baldwin static bool 675c0341432SJohn Baldwin safe_cipher_supported(struct safe_softc *sc, 676c0341432SJohn Baldwin const struct crypto_session_params *csp) 677c0341432SJohn Baldwin { 678c0341432SJohn Baldwin 679c0341432SJohn Baldwin switch (csp->csp_cipher_alg) { 680b7e3f244SSam Leffler case CRYPTO_AES_CBC: 681c0341432SJohn Baldwin if ((sc->sc_devinfo & SAFE_DEVINFO_AES) == 0) 682c0341432SJohn Baldwin return (false); 683c0341432SJohn Baldwin if (csp->csp_ivlen != 16) 684c0341432SJohn Baldwin return (false); 685c0341432SJohn Baldwin if (csp->csp_cipher_klen != 16 && 686c0341432SJohn Baldwin csp->csp_cipher_klen != 24 && 687c0341432SJohn Baldwin csp->csp_cipher_klen != 32) 688c0341432SJohn Baldwin return (false); 689b7e3f244SSam Leffler break; 690b7e3f244SSam Leffler } 691c0341432SJohn Baldwin return (true); 692b7e3f244SSam Leffler } 693b7e3f244SSam Leffler 694c0341432SJohn Baldwin static int 695c0341432SJohn Baldwin safe_probesession(device_t dev, const struct crypto_session_params *csp) 696c0341432SJohn Baldwin { 697c0341432SJohn Baldwin struct safe_softc *sc = device_get_softc(dev); 698c0341432SJohn Baldwin 699c0341432SJohn Baldwin if (csp->csp_flags != 0) 700c0341432SJohn Baldwin return (EINVAL); 701c0341432SJohn Baldwin switch (csp->csp_mode) { 702c0341432SJohn Baldwin case CSP_MODE_DIGEST: 703c0341432SJohn Baldwin if (!safe_auth_supported(sc, csp)) 704c0341432SJohn Baldwin return (EINVAL); 705c0341432SJohn Baldwin break; 706c0341432SJohn Baldwin case CSP_MODE_CIPHER: 707c0341432SJohn Baldwin if (!safe_cipher_supported(sc, csp)) 708c0341432SJohn Baldwin return (EINVAL); 709c0341432SJohn Baldwin break; 710c0341432SJohn Baldwin case CSP_MODE_ETA: 711c0341432SJohn Baldwin if (!safe_auth_supported(sc, csp) || 712c0341432SJohn Baldwin !safe_cipher_supported(sc, csp)) 713c0341432SJohn Baldwin return (EINVAL); 714c0341432SJohn Baldwin break; 715c0341432SJohn Baldwin default: 716c0341432SJohn Baldwin return (EINVAL); 717c0341432SJohn Baldwin } 718c0341432SJohn Baldwin 719c0341432SJohn Baldwin return (CRYPTODEV_PROBE_HARDWARE); 720c0341432SJohn Baldwin } 721c0341432SJohn Baldwin 722c0341432SJohn Baldwin /* 723c0341432SJohn Baldwin * Allocate a new 'session'. 724c0341432SJohn Baldwin */ 725c0341432SJohn Baldwin static int 726c0341432SJohn Baldwin safe_newsession(device_t dev, crypto_session_t cses, 727c0341432SJohn Baldwin const struct crypto_session_params *csp) 728c0341432SJohn Baldwin { 729c0341432SJohn Baldwin struct safe_session *ses; 730c0341432SJohn Baldwin 7311b0909d5SConrad Meyer ses = crypto_get_driver_session(cses); 732c0341432SJohn Baldwin if (csp->csp_cipher_alg != 0) { 733c0341432SJohn Baldwin ses->ses_klen = csp->csp_cipher_klen; 734c0341432SJohn Baldwin if (csp->csp_cipher_key != NULL) 735c0341432SJohn Baldwin safe_setup_enckey(ses, csp->csp_cipher_key); 736b7e3f244SSam Leffler } 737b7e3f244SSam Leffler 738c0341432SJohn Baldwin if (csp->csp_auth_alg != 0) { 739c0341432SJohn Baldwin ses->ses_mlen = csp->csp_auth_mlen; 740af65c53aSPawel Jakub Dawidek if (ses->ses_mlen == 0) { 7411dc8d404SPawel Jakub Dawidek ses->ses_mlen = SHA1_HASH_LEN; 742af65c53aSPawel Jakub Dawidek } 743af65c53aSPawel Jakub Dawidek 744c0341432SJohn Baldwin if (csp->csp_auth_key != NULL) { 745c0341432SJohn Baldwin safe_setup_mackey(ses, csp->csp_auth_alg, 746c0341432SJohn Baldwin csp->csp_auth_key, csp->csp_auth_klen); 747b7e3f244SSam Leffler } 748b7e3f244SSam Leffler } 749b7e3f244SSam Leffler 750b7e3f244SSam Leffler return (0); 751b7e3f244SSam Leffler } 752b7e3f244SSam Leffler 753b7e3f244SSam Leffler static void 754c0341432SJohn Baldwin safe_op_cb(void *arg, bus_dma_segment_t *seg, int nsegs, int error) 755b7e3f244SSam Leffler { 756b7e3f244SSam Leffler struct safe_operand *op = arg; 757b7e3f244SSam Leffler 758c0341432SJohn Baldwin DPRINTF(("%s: nsegs %d error %d\n", __func__, 759c0341432SJohn Baldwin nsegs, error)); 760b7e3f244SSam Leffler if (error != 0) 761b7e3f244SSam Leffler return; 762b7e3f244SSam Leffler op->nsegs = nsegs; 763b7e3f244SSam Leffler bcopy(seg, op->segs, nsegs * sizeof (seg[0])); 764b7e3f244SSam Leffler } 765b7e3f244SSam Leffler 766b7e3f244SSam Leffler static int 7676810ad6fSSam Leffler safe_process(device_t dev, struct cryptop *crp, int hint) 768b7e3f244SSam Leffler { 7696810ad6fSSam Leffler struct safe_softc *sc = device_get_softc(dev); 770c0341432SJohn Baldwin const struct crypto_session_params *csp; 771b7e3f244SSam Leffler int err = 0, i, nicealign, uniform; 772c0341432SJohn Baldwin int bypass, oplen; 773b7e3f244SSam Leffler int16_t coffset; 774b7e3f244SSam Leffler struct safe_session *ses; 775b7e3f244SSam Leffler struct safe_ringentry *re; 776b7e3f244SSam Leffler struct safe_sarec *sa; 777b7e3f244SSam Leffler struct safe_pdesc *pd; 778b7e3f244SSam Leffler u_int32_t cmd0, cmd1, staterec; 779b7e3f244SSam Leffler 780b7e3f244SSam Leffler mtx_lock(&sc->sc_ringmtx); 781b7e3f244SSam Leffler if (sc->sc_front == sc->sc_back && sc->sc_nqchip != 0) { 782b7e3f244SSam Leffler safestats.st_ringfull++; 783b7e3f244SSam Leffler sc->sc_needwakeup |= CRYPTO_SYMQ; 784b7e3f244SSam Leffler mtx_unlock(&sc->sc_ringmtx); 785b7e3f244SSam Leffler return (ERESTART); 786b7e3f244SSam Leffler } 787b7e3f244SSam Leffler re = sc->sc_front; 788b7e3f244SSam Leffler 789b7e3f244SSam Leffler staterec = re->re_sa.sa_staterec; /* save */ 790b7e3f244SSam Leffler /* NB: zero everything but the PE descriptor */ 791b7e3f244SSam Leffler bzero(&re->re_sa, sizeof(struct safe_ringentry) - sizeof(re->re_desc)); 792b7e3f244SSam Leffler re->re_sa.sa_staterec = staterec; /* restore */ 793b7e3f244SSam Leffler 794b7e3f244SSam Leffler re->re_crp = crp; 795b7e3f244SSam Leffler 796b7e3f244SSam Leffler sa = &re->re_sa; 7971b0909d5SConrad Meyer ses = crypto_get_driver_session(crp->crp_session); 798c0341432SJohn Baldwin csp = crypto_get_params(crp->crp_session); 799b7e3f244SSam Leffler 800b7e3f244SSam Leffler cmd0 = SAFE_SA_CMD0_BASIC; /* basic group operation */ 801b7e3f244SSam Leffler cmd1 = 0; 802c0341432SJohn Baldwin switch (csp->csp_mode) { 803c0341432SJohn Baldwin case CSP_MODE_DIGEST: 804b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_OP_HASH; 805c0341432SJohn Baldwin break; 806c0341432SJohn Baldwin case CSP_MODE_CIPHER: 807b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_OP_CRYPT; 808c0341432SJohn Baldwin break; 809c0341432SJohn Baldwin case CSP_MODE_ETA: 810b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_OP_BOTH; 811c0341432SJohn Baldwin break; 812b7e3f244SSam Leffler } 813b7e3f244SSam Leffler 814c0341432SJohn Baldwin if (csp->csp_cipher_alg != 0) { 815c0341432SJohn Baldwin if (crp->crp_cipher_key != NULL) 816c0341432SJohn Baldwin safe_setup_enckey(ses, crp->crp_cipher_key); 8179a2f6061SPawel Jakub Dawidek 818c0341432SJohn Baldwin switch (csp->csp_cipher_alg) { 819c0341432SJohn Baldwin case CRYPTO_AES_CBC: 820b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_AES; 821b7e3f244SSam Leffler cmd1 |= SAFE_SA_CMD1_CBC; 822c0341432SJohn Baldwin if (ses->ses_klen * 8 == 128) 823b7e3f244SSam Leffler cmd1 |= SAFE_SA_CMD1_AES128; 824c0341432SJohn Baldwin else if (ses->ses_klen * 8 == 192) 825b7e3f244SSam Leffler cmd1 |= SAFE_SA_CMD1_AES192; 826b7e3f244SSam Leffler else 827b7e3f244SSam Leffler cmd1 |= SAFE_SA_CMD1_AES256; 828b7e3f244SSam Leffler } 829b7e3f244SSam Leffler 830b7e3f244SSam Leffler /* 831b7e3f244SSam Leffler * Setup encrypt/decrypt state. When using basic ops 832b7e3f244SSam Leffler * we can't use an inline IV because hash/crypt offset 833b7e3f244SSam Leffler * must be from the end of the IV to the start of the 834b7e3f244SSam Leffler * crypt data and this leaves out the preceding header 835b7e3f244SSam Leffler * from the hash calculation. Instead we place the IV 836b7e3f244SSam Leffler * in the state record and set the hash/crypt offset to 837b7e3f244SSam Leffler * copy both the header+IV. 838b7e3f244SSam Leffler */ 83929fe41ddSJohn Baldwin crypto_read_iv(crp, re->re_sastate.sa_saved_iv); 840c0341432SJohn Baldwin cmd0 |= SAFE_SA_CMD0_IVLD_STATE; 841c0341432SJohn Baldwin 842c0341432SJohn Baldwin if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) { 843b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_OUTBOUND; 844b7e3f244SSam Leffler 845c0341432SJohn Baldwin /* 846c0341432SJohn Baldwin * XXX: I suspect we don't need this since we 847c0341432SJohn Baldwin * don't save the returned IV. 848c0341432SJohn Baldwin */ 849c0341432SJohn Baldwin cmd0 |= SAFE_SA_CMD0_SAVEIV; 850b7e3f244SSam Leffler } else { 851b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_INBOUND; 852b7e3f244SSam Leffler } 853b7e3f244SSam Leffler /* 854b7e3f244SSam Leffler * For basic encryption use the zero pad algorithm. 855b7e3f244SSam Leffler * This pads results to an 8-byte boundary and 856b7e3f244SSam Leffler * suppresses padding verification for inbound (i.e. 857b7e3f244SSam Leffler * decrypt) operations. 858b7e3f244SSam Leffler * 859b7e3f244SSam Leffler * NB: Not sure if the 8-byte pad boundary is a problem. 860b7e3f244SSam Leffler */ 861b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_PAD_ZERO; 862b7e3f244SSam Leffler 863b7e3f244SSam Leffler /* XXX assert key bufs have the same size */ 864b7e3f244SSam Leffler bcopy(ses->ses_key, sa->sa_key, sizeof(sa->sa_key)); 865b7e3f244SSam Leffler } 866b7e3f244SSam Leffler 867c0341432SJohn Baldwin if (csp->csp_auth_alg != 0) { 868c0341432SJohn Baldwin if (crp->crp_auth_key != NULL) { 869c0341432SJohn Baldwin safe_setup_mackey(ses, csp->csp_auth_alg, 870c0341432SJohn Baldwin crp->crp_auth_key, csp->csp_auth_klen); 8719a2f6061SPawel Jakub Dawidek } 8729a2f6061SPawel Jakub Dawidek 873c0341432SJohn Baldwin switch (csp->csp_auth_alg) { 874c0341432SJohn Baldwin case CRYPTO_SHA1_HMAC: 875b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_SHA1; 876b7e3f244SSam Leffler cmd1 |= SAFE_SA_CMD1_HMAC; /* NB: enable HMAC */ 877c0341432SJohn Baldwin break; 878b7e3f244SSam Leffler } 879c0341432SJohn Baldwin 880b7e3f244SSam Leffler /* 881b7e3f244SSam Leffler * Digest data is loaded from the SA and the hash 882b7e3f244SSam Leffler * result is saved to the state block where we 883b7e3f244SSam Leffler * retrieve it for return to the caller. 884b7e3f244SSam Leffler */ 885b7e3f244SSam Leffler /* XXX assert digest bufs have the same size */ 886b7e3f244SSam Leffler bcopy(ses->ses_hminner, sa->sa_indigest, 887b7e3f244SSam Leffler sizeof(sa->sa_indigest)); 888b7e3f244SSam Leffler bcopy(ses->ses_hmouter, sa->sa_outdigest, 889b7e3f244SSam Leffler sizeof(sa->sa_outdigest)); 890b7e3f244SSam Leffler 891b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_HSLD_SA | SAFE_SA_CMD0_SAVEHASH; 892b7e3f244SSam Leffler re->re_flags |= SAFE_QFLAGS_COPYOUTICV; 893b7e3f244SSam Leffler } 894b7e3f244SSam Leffler 895c0341432SJohn Baldwin if (csp->csp_mode == CSP_MODE_ETA) { 896b7e3f244SSam Leffler /* 897c0341432SJohn Baldwin * The driver only supports ETA requests where there 898c0341432SJohn Baldwin * is no gap between the AAD and payload. 899b7e3f244SSam Leffler */ 900c0341432SJohn Baldwin if (crp->crp_aad_length != 0 && 901c0341432SJohn Baldwin crp->crp_aad_start + crp->crp_aad_length != 902c0341432SJohn Baldwin crp->crp_payload_start) { 903b7e3f244SSam Leffler safestats.st_lenmismatch++; 904b7e3f244SSam Leffler err = EINVAL; 905b7e3f244SSam Leffler goto errout; 906b7e3f244SSam Leffler } 907c0341432SJohn Baldwin if (crp->crp_aad_length != 0) 908c0341432SJohn Baldwin bypass = crp->crp_aad_start; 909c0341432SJohn Baldwin else 910c0341432SJohn Baldwin bypass = crp->crp_payload_start; 911c0341432SJohn Baldwin coffset = crp->crp_aad_length; 912c0341432SJohn Baldwin oplen = crp->crp_payload_start + crp->crp_payload_length; 913b7e3f244SSam Leffler #ifdef SAFE_DEBUG 914b7e3f244SSam Leffler if (safe_debug) { 915c0341432SJohn Baldwin printf("AAD: skip %d, len %d, digest %d\n", 916c0341432SJohn Baldwin crp->crp_aad_start, crp->crp_aad_length, 917c0341432SJohn Baldwin crp->crp_digest_start); 918c0341432SJohn Baldwin printf("payload: skip %d, len %d, IV %d\n", 919c0341432SJohn Baldwin crp->crp_payload_start, crp->crp_payload_length, 920c0341432SJohn Baldwin crp->crp_iv_start); 921b7e3f244SSam Leffler printf("bypass %d coffset %d oplen %d\n", 922b7e3f244SSam Leffler bypass, coffset, oplen); 923b7e3f244SSam Leffler } 924b7e3f244SSam Leffler #endif 925b7e3f244SSam Leffler if (coffset & 3) { /* offset must be 32-bit aligned */ 926b7e3f244SSam Leffler DPRINTF(("%s: coffset %u misaligned\n", 927b7e3f244SSam Leffler __func__, coffset)); 928b7e3f244SSam Leffler safestats.st_coffmisaligned++; 929b7e3f244SSam Leffler err = EINVAL; 930b7e3f244SSam Leffler goto errout; 931b7e3f244SSam Leffler } 932b7e3f244SSam Leffler coffset >>= 2; 933b7e3f244SSam Leffler if (coffset > 255) { /* offset must be <256 dwords */ 934b7e3f244SSam Leffler DPRINTF(("%s: coffset %u too big\n", 935b7e3f244SSam Leffler __func__, coffset)); 936b7e3f244SSam Leffler safestats.st_cofftoobig++; 937b7e3f244SSam Leffler err = EINVAL; 938b7e3f244SSam Leffler goto errout; 939b7e3f244SSam Leffler } 940b7e3f244SSam Leffler /* 941b7e3f244SSam Leffler * Tell the hardware to copy the header to the output. 942b7e3f244SSam Leffler * The header is defined as the data from the end of 943b7e3f244SSam Leffler * the bypass to the start of data to be encrypted. 944b7e3f244SSam Leffler * Typically this is the inline IV. Note that you need 945b7e3f244SSam Leffler * to do this even if src+dst are the same; it appears 946b7e3f244SSam Leffler * that w/o this bit the crypted data is written 947b7e3f244SSam Leffler * immediately after the bypass data. 948b7e3f244SSam Leffler */ 949b7e3f244SSam Leffler cmd1 |= SAFE_SA_CMD1_HDRCOPY; 950b7e3f244SSam Leffler /* 951b7e3f244SSam Leffler * Disable IP header mutable bit handling. This is 952b7e3f244SSam Leffler * needed to get correct HMAC calculations. 953b7e3f244SSam Leffler */ 954b7e3f244SSam Leffler cmd1 |= SAFE_SA_CMD1_MUTABLE; 955b7e3f244SSam Leffler } else { 956c0341432SJohn Baldwin bypass = crp->crp_payload_start; 957c0341432SJohn Baldwin oplen = bypass + crp->crp_payload_length; 958b7e3f244SSam Leffler coffset = 0; 959b7e3f244SSam Leffler } 960b7e3f244SSam Leffler /* XXX verify multiple of 4 when using s/g */ 961b7e3f244SSam Leffler if (bypass > 96) { /* bypass offset must be <= 96 bytes */ 962b7e3f244SSam Leffler DPRINTF(("%s: bypass %u too big\n", __func__, bypass)); 963b7e3f244SSam Leffler safestats.st_bypasstoobig++; 964b7e3f244SSam Leffler err = EINVAL; 965b7e3f244SSam Leffler goto errout; 966b7e3f244SSam Leffler } 967b7e3f244SSam Leffler 968b7e3f244SSam Leffler if (bus_dmamap_create(sc->sc_srcdmat, BUS_DMA_NOWAIT, &re->re_src_map)) { 969b7e3f244SSam Leffler safestats.st_nomap++; 970b7e3f244SSam Leffler err = ENOMEM; 971b7e3f244SSam Leffler goto errout; 972b7e3f244SSam Leffler } 973c0341432SJohn Baldwin if (bus_dmamap_load_crp(sc->sc_srcdmat, re->re_src_map, crp, safe_op_cb, 974b7e3f244SSam Leffler &re->re_src, BUS_DMA_NOWAIT) != 0) { 975b7e3f244SSam Leffler bus_dmamap_destroy(sc->sc_srcdmat, re->re_src_map); 976b7e3f244SSam Leffler re->re_src_map = NULL; 977b7e3f244SSam Leffler safestats.st_noload++; 978b7e3f244SSam Leffler err = ENOMEM; 979b7e3f244SSam Leffler goto errout; 980b7e3f244SSam Leffler } 9819c0e3d3aSJohn Baldwin re->re_src_mapsize = crypto_buffer_len(&crp->crp_buf); 982b7e3f244SSam Leffler nicealign = safe_dmamap_aligned(&re->re_src); 983b7e3f244SSam Leffler uniform = safe_dmamap_uniform(&re->re_src); 984b7e3f244SSam Leffler 985b7e3f244SSam Leffler DPRINTF(("src nicealign %u uniform %u nsegs %u\n", 986b7e3f244SSam Leffler nicealign, uniform, re->re_src.nsegs)); 987b7e3f244SSam Leffler if (re->re_src.nsegs > 1) { 988b7e3f244SSam Leffler re->re_desc.d_src = sc->sc_spalloc.dma_paddr + 989b7e3f244SSam Leffler ((caddr_t) sc->sc_spfree - (caddr_t) sc->sc_spring); 990b7e3f244SSam Leffler for (i = 0; i < re->re_src_nsegs; i++) { 991b7e3f244SSam Leffler /* NB: no need to check if there's space */ 992b7e3f244SSam Leffler pd = sc->sc_spfree; 993b7e3f244SSam Leffler if (++(sc->sc_spfree) == sc->sc_springtop) 994b7e3f244SSam Leffler sc->sc_spfree = sc->sc_spring; 995b7e3f244SSam Leffler 996b7e3f244SSam Leffler KASSERT((pd->pd_flags&3) == 0 || 997b7e3f244SSam Leffler (pd->pd_flags&3) == SAFE_PD_DONE, 998b7e3f244SSam Leffler ("bogus source particle descriptor; flags %x", 999b7e3f244SSam Leffler pd->pd_flags)); 1000b7e3f244SSam Leffler pd->pd_addr = re->re_src_segs[i].ds_addr; 1001b7e3f244SSam Leffler pd->pd_size = re->re_src_segs[i].ds_len; 1002b7e3f244SSam Leffler pd->pd_flags = SAFE_PD_READY; 1003b7e3f244SSam Leffler } 1004b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_IGATHER; 1005b7e3f244SSam Leffler } else { 1006b7e3f244SSam Leffler /* 1007b7e3f244SSam Leffler * No need for gather, reference the operand directly. 1008b7e3f244SSam Leffler */ 1009b7e3f244SSam Leffler re->re_desc.d_src = re->re_src_segs[0].ds_addr; 1010b7e3f244SSam Leffler } 1011b7e3f244SSam Leffler 1012c0341432SJohn Baldwin if (csp->csp_mode == CSP_MODE_DIGEST) { 1013b7e3f244SSam Leffler /* 1014b7e3f244SSam Leffler * Hash op; no destination needed. 1015b7e3f244SSam Leffler */ 1016b7e3f244SSam Leffler } else { 1017b7e3f244SSam Leffler if (nicealign && uniform == 1) { 1018b7e3f244SSam Leffler /* 1019b7e3f244SSam Leffler * Source layout is suitable for direct 1020b7e3f244SSam Leffler * sharing of the DMA map and segment list. 1021b7e3f244SSam Leffler */ 1022b7e3f244SSam Leffler re->re_dst = re->re_src; 1023b7e3f244SSam Leffler } else if (nicealign && uniform == 2) { 1024b7e3f244SSam Leffler /* 1025b7e3f244SSam Leffler * The source is properly aligned but requires a 1026b7e3f244SSam Leffler * different particle list to handle DMA of the 1027b7e3f244SSam Leffler * result. Create a new map and do the load to 1028b7e3f244SSam Leffler * create the segment list. The particle 1029b7e3f244SSam Leffler * descriptor setup code below will handle the 1030b7e3f244SSam Leffler * rest. 1031b7e3f244SSam Leffler */ 1032c0341432SJohn Baldwin if (bus_dmamap_create(sc->sc_dstdmat, BUS_DMA_NOWAIT, 1033c0341432SJohn Baldwin &re->re_dst_map)) { 1034b7e3f244SSam Leffler safestats.st_nomap++; 1035b7e3f244SSam Leffler err = ENOMEM; 1036b7e3f244SSam Leffler goto errout; 1037b7e3f244SSam Leffler } 1038c0341432SJohn Baldwin if (bus_dmamap_load_crp(sc->sc_dstdmat, re->re_dst_map, 1039c0341432SJohn Baldwin crp, safe_op_cb, &re->re_dst, BUS_DMA_NOWAIT) != 1040c0341432SJohn Baldwin 0) { 1041b7e3f244SSam Leffler bus_dmamap_destroy(sc->sc_dstdmat, 1042b7e3f244SSam Leffler re->re_dst_map); 1043b7e3f244SSam Leffler re->re_dst_map = NULL; 1044b7e3f244SSam Leffler safestats.st_noload++; 1045b7e3f244SSam Leffler err = ENOMEM; 1046b7e3f244SSam Leffler goto errout; 1047b7e3f244SSam Leffler } 10489c0e3d3aSJohn Baldwin } else if (crp->crp_buf.cb_type == CRYPTO_BUF_MBUF) { 1049b7e3f244SSam Leffler int totlen, len; 1050b7e3f244SSam Leffler struct mbuf *m, *top, **mp; 1051b7e3f244SSam Leffler 1052b7e3f244SSam Leffler /* 1053b7e3f244SSam Leffler * DMA constraints require that we allocate a 1054b7e3f244SSam Leffler * new mbuf chain for the destination. We 1055b7e3f244SSam Leffler * allocate an entire new set of mbufs of 1056b7e3f244SSam Leffler * optimal/required size and then tell the 1057b7e3f244SSam Leffler * hardware to copy any bits that are not 1058b7e3f244SSam Leffler * created as a byproduct of the operation. 1059b7e3f244SSam Leffler */ 1060b7e3f244SSam Leffler if (!nicealign) 1061b7e3f244SSam Leffler safestats.st_unaligned++; 1062b7e3f244SSam Leffler if (!uniform) 1063b7e3f244SSam Leffler safestats.st_notuniform++; 1064b7e3f244SSam Leffler totlen = re->re_src_mapsize; 10659c0e3d3aSJohn Baldwin if (crp->crp_buf.cb_mbuf->m_flags & M_PKTHDR) { 1066b7e3f244SSam Leffler len = MHLEN; 1067c6499eccSGleb Smirnoff MGETHDR(m, M_NOWAIT, MT_DATA); 10689c0e3d3aSJohn Baldwin if (m && !m_dup_pkthdr(m, crp->crp_buf.cb_mbuf, 1069c6499eccSGleb Smirnoff M_NOWAIT)) { 1070b7e3f244SSam Leffler m_free(m); 1071b7e3f244SSam Leffler m = NULL; 1072b7e3f244SSam Leffler } 1073b7e3f244SSam Leffler } else { 1074b7e3f244SSam Leffler len = MLEN; 1075c6499eccSGleb Smirnoff MGET(m, M_NOWAIT, MT_DATA); 1076b7e3f244SSam Leffler } 1077b7e3f244SSam Leffler if (m == NULL) { 1078b7e3f244SSam Leffler safestats.st_nombuf++; 1079b7e3f244SSam Leffler err = sc->sc_nqchip ? ERESTART : ENOMEM; 1080b7e3f244SSam Leffler goto errout; 1081b7e3f244SSam Leffler } 1082b7e3f244SSam Leffler if (totlen >= MINCLSIZE) { 10832a8c860fSRobert Watson if (!(MCLGET(m, M_NOWAIT))) { 1084b7e3f244SSam Leffler m_free(m); 1085b7e3f244SSam Leffler safestats.st_nomcl++; 1086b7e3f244SSam Leffler err = sc->sc_nqchip ? 1087b7e3f244SSam Leffler ERESTART : ENOMEM; 1088b7e3f244SSam Leffler goto errout; 1089b7e3f244SSam Leffler } 1090b7e3f244SSam Leffler len = MCLBYTES; 1091b7e3f244SSam Leffler } 1092b7e3f244SSam Leffler m->m_len = len; 1093b7e3f244SSam Leffler top = NULL; 1094b7e3f244SSam Leffler mp = ⊤ 1095b7e3f244SSam Leffler 1096b7e3f244SSam Leffler while (totlen > 0) { 1097b7e3f244SSam Leffler if (top) { 1098c6499eccSGleb Smirnoff MGET(m, M_NOWAIT, MT_DATA); 1099b7e3f244SSam Leffler if (m == NULL) { 1100b7e3f244SSam Leffler m_freem(top); 1101b7e3f244SSam Leffler safestats.st_nombuf++; 1102b7e3f244SSam Leffler err = sc->sc_nqchip ? 1103b7e3f244SSam Leffler ERESTART : ENOMEM; 1104b7e3f244SSam Leffler goto errout; 1105b7e3f244SSam Leffler } 1106b7e3f244SSam Leffler len = MLEN; 1107b7e3f244SSam Leffler } 1108b7e3f244SSam Leffler if (top && totlen >= MINCLSIZE) { 11092a8c860fSRobert Watson if (!(MCLGET(m, M_NOWAIT))) { 1110b7e3f244SSam Leffler *mp = m; 1111b7e3f244SSam Leffler m_freem(top); 1112b7e3f244SSam Leffler safestats.st_nomcl++; 1113b7e3f244SSam Leffler err = sc->sc_nqchip ? 1114b7e3f244SSam Leffler ERESTART : ENOMEM; 1115b7e3f244SSam Leffler goto errout; 1116b7e3f244SSam Leffler } 1117b7e3f244SSam Leffler len = MCLBYTES; 1118b7e3f244SSam Leffler } 1119b7e3f244SSam Leffler m->m_len = len = min(totlen, len); 1120b7e3f244SSam Leffler totlen -= len; 1121b7e3f244SSam Leffler *mp = m; 1122b7e3f244SSam Leffler mp = &m->m_next; 1123b7e3f244SSam Leffler } 1124b7e3f244SSam Leffler re->re_dst_m = top; 1125b7e3f244SSam Leffler if (bus_dmamap_create(sc->sc_dstdmat, 1126b7e3f244SSam Leffler BUS_DMA_NOWAIT, &re->re_dst_map) != 0) { 1127b7e3f244SSam Leffler safestats.st_nomap++; 1128b7e3f244SSam Leffler err = ENOMEM; 1129b7e3f244SSam Leffler goto errout; 1130b7e3f244SSam Leffler } 1131c0341432SJohn Baldwin if (bus_dmamap_load_mbuf_sg(sc->sc_dstdmat, 1132c0341432SJohn Baldwin re->re_dst_map, top, re->re_dst_segs, 1133c0341432SJohn Baldwin &re->re_dst_nsegs, 0) != 0) { 1134b7e3f244SSam Leffler bus_dmamap_destroy(sc->sc_dstdmat, 1135b7e3f244SSam Leffler re->re_dst_map); 1136b7e3f244SSam Leffler re->re_dst_map = NULL; 1137b7e3f244SSam Leffler safestats.st_noload++; 1138b7e3f244SSam Leffler err = ENOMEM; 1139b7e3f244SSam Leffler goto errout; 1140b7e3f244SSam Leffler } 1141c0341432SJohn Baldwin re->re_dst_mapsize = re->re_src_mapsize; 1142b7e3f244SSam Leffler if (re->re_src.mapsize > oplen) { 1143b7e3f244SSam Leffler /* 1144b7e3f244SSam Leffler * There's data following what the 1145b7e3f244SSam Leffler * hardware will copy for us. If this 1146b7e3f244SSam Leffler * isn't just the ICV (that's going to 1147b7e3f244SSam Leffler * be written on completion), copy it 1148b7e3f244SSam Leffler * to the new mbufs 1149b7e3f244SSam Leffler */ 1150c0341432SJohn Baldwin if (!(csp->csp_mode == CSP_MODE_ETA && 1151c0341432SJohn Baldwin (re->re_src.mapsize-oplen) == ses->ses_mlen && 1152c0341432SJohn Baldwin crp->crp_digest_start == oplen)) 11539c0e3d3aSJohn Baldwin safe_mcopy(crp->crp_buf.cb_mbuf, 11549c0e3d3aSJohn Baldwin re->re_dst_m, oplen); 1155b7e3f244SSam Leffler else 1156b7e3f244SSam Leffler safestats.st_noicvcopy++; 1157b7e3f244SSam Leffler } 1158b7e3f244SSam Leffler } else { 1159c0341432SJohn Baldwin if (!nicealign) { 1160c0341432SJohn Baldwin safestats.st_iovmisaligned++; 1161b7e3f244SSam Leffler err = EINVAL; 1162b7e3f244SSam Leffler goto errout; 1163c0341432SJohn Baldwin } else { 1164c0341432SJohn Baldwin /* 1165c0341432SJohn Baldwin * There's no way to handle the DMA 1166c0341432SJohn Baldwin * requirements with this uio. We 1167c0341432SJohn Baldwin * could create a separate DMA area for 1168c0341432SJohn Baldwin * the result and then copy it back, 1169c0341432SJohn Baldwin * but for now we just bail and return 1170c0341432SJohn Baldwin * an error. Note that uio requests 1171c0341432SJohn Baldwin * > SAFE_MAX_DSIZE are handled because 1172c0341432SJohn Baldwin * the DMA map and segment list for the 1173c0341432SJohn Baldwin * destination wil result in a 1174c0341432SJohn Baldwin * destination particle list that does 1175c0341432SJohn Baldwin * the necessary scatter DMA. 1176c0341432SJohn Baldwin */ 1177c0341432SJohn Baldwin safestats.st_iovnotuniform++; 1178c0341432SJohn Baldwin err = EINVAL; 1179c0341432SJohn Baldwin goto errout; 1180c0341432SJohn Baldwin } 1181b7e3f244SSam Leffler } 1182b7e3f244SSam Leffler 1183b7e3f244SSam Leffler if (re->re_dst.nsegs > 1) { 1184b7e3f244SSam Leffler re->re_desc.d_dst = sc->sc_dpalloc.dma_paddr + 1185b7e3f244SSam Leffler ((caddr_t) sc->sc_dpfree - (caddr_t) sc->sc_dpring); 1186b7e3f244SSam Leffler for (i = 0; i < re->re_dst_nsegs; i++) { 1187b7e3f244SSam Leffler pd = sc->sc_dpfree; 1188b7e3f244SSam Leffler KASSERT((pd->pd_flags&3) == 0 || 1189b7e3f244SSam Leffler (pd->pd_flags&3) == SAFE_PD_DONE, 1190b7e3f244SSam Leffler ("bogus dest particle descriptor; flags %x", 1191b7e3f244SSam Leffler pd->pd_flags)); 1192b7e3f244SSam Leffler if (++(sc->sc_dpfree) == sc->sc_dpringtop) 1193b7e3f244SSam Leffler sc->sc_dpfree = sc->sc_dpring; 1194b7e3f244SSam Leffler pd->pd_addr = re->re_dst_segs[i].ds_addr; 1195b7e3f244SSam Leffler pd->pd_flags = SAFE_PD_READY; 1196b7e3f244SSam Leffler } 1197b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_OSCATTER; 1198b7e3f244SSam Leffler } else { 1199b7e3f244SSam Leffler /* 1200b7e3f244SSam Leffler * No need for scatter, reference the operand directly. 1201b7e3f244SSam Leffler */ 1202b7e3f244SSam Leffler re->re_desc.d_dst = re->re_dst_segs[0].ds_addr; 1203b7e3f244SSam Leffler } 1204b7e3f244SSam Leffler } 1205b7e3f244SSam Leffler 1206b7e3f244SSam Leffler /* 1207b7e3f244SSam Leffler * All done with setup; fillin the SA command words 1208b7e3f244SSam Leffler * and the packet engine descriptor. The operation 1209b7e3f244SSam Leffler * is now ready for submission to the hardware. 1210b7e3f244SSam Leffler */ 1211b7e3f244SSam Leffler sa->sa_cmd0 = cmd0 | SAFE_SA_CMD0_IPCI | SAFE_SA_CMD0_OPCI; 1212b7e3f244SSam Leffler sa->sa_cmd1 = cmd1 1213b7e3f244SSam Leffler | (coffset << SAFE_SA_CMD1_OFFSET_S) 1214b7e3f244SSam Leffler | SAFE_SA_CMD1_SAREV1 /* Rev 1 SA data structure */ 1215b7e3f244SSam Leffler | SAFE_SA_CMD1_SRPCI 1216b7e3f244SSam Leffler ; 1217b7e3f244SSam Leffler /* 1218b7e3f244SSam Leffler * NB: the order of writes is important here. In case the 1219b7e3f244SSam Leffler * chip is scanning the ring because of an outstanding request 1220b7e3f244SSam Leffler * it might nab this one too. In that case we need to make 1221b7e3f244SSam Leffler * sure the setup is complete before we write the length 1222b7e3f244SSam Leffler * field of the descriptor as it signals the descriptor is 1223b7e3f244SSam Leffler * ready for processing. 1224b7e3f244SSam Leffler */ 1225b7e3f244SSam Leffler re->re_desc.d_csr = SAFE_PE_CSR_READY | SAFE_PE_CSR_SAPCI; 1226c0341432SJohn Baldwin if (csp->csp_auth_alg != 0) 1227b7e3f244SSam Leffler re->re_desc.d_csr |= SAFE_PE_CSR_LOADSA | SAFE_PE_CSR_HASHFINAL; 1228b7e3f244SSam Leffler re->re_desc.d_len = oplen 1229b7e3f244SSam Leffler | SAFE_PE_LEN_READY 1230b7e3f244SSam Leffler | (bypass << SAFE_PE_LEN_BYPASS_S) 1231b7e3f244SSam Leffler ; 1232b7e3f244SSam Leffler 1233b7e3f244SSam Leffler safestats.st_ipackets++; 1234b7e3f244SSam Leffler safestats.st_ibytes += oplen; 1235b7e3f244SSam Leffler 1236b7e3f244SSam Leffler if (++(sc->sc_front) == sc->sc_ringtop) 1237b7e3f244SSam Leffler sc->sc_front = sc->sc_ring; 1238b7e3f244SSam Leffler 1239b7e3f244SSam Leffler /* XXX honor batching */ 1240b7e3f244SSam Leffler safe_feed(sc, re); 1241b7e3f244SSam Leffler mtx_unlock(&sc->sc_ringmtx); 1242b7e3f244SSam Leffler return (0); 1243b7e3f244SSam Leffler 1244b7e3f244SSam Leffler errout: 1245c0341432SJohn Baldwin if (re->re_dst_m != NULL) 1246b7e3f244SSam Leffler m_freem(re->re_dst_m); 1247b7e3f244SSam Leffler 1248b7e3f244SSam Leffler if (re->re_dst_map != NULL && re->re_dst_map != re->re_src_map) { 1249b7e3f244SSam Leffler bus_dmamap_unload(sc->sc_dstdmat, re->re_dst_map); 1250b7e3f244SSam Leffler bus_dmamap_destroy(sc->sc_dstdmat, re->re_dst_map); 1251b7e3f244SSam Leffler } 1252b7e3f244SSam Leffler if (re->re_src_map != NULL) { 1253b7e3f244SSam Leffler bus_dmamap_unload(sc->sc_srcdmat, re->re_src_map); 1254b7e3f244SSam Leffler bus_dmamap_destroy(sc->sc_srcdmat, re->re_src_map); 1255b7e3f244SSam Leffler } 1256b7e3f244SSam Leffler mtx_unlock(&sc->sc_ringmtx); 1257b7e3f244SSam Leffler if (err != ERESTART) { 1258b7e3f244SSam Leffler crp->crp_etype = err; 1259b7e3f244SSam Leffler crypto_done(crp); 1260895c98ccSJohn Baldwin err = 0; 1261b7e3f244SSam Leffler } else { 1262b7e3f244SSam Leffler sc->sc_needwakeup |= CRYPTO_SYMQ; 1263b7e3f244SSam Leffler } 1264b7e3f244SSam Leffler return (err); 1265b7e3f244SSam Leffler } 1266b7e3f244SSam Leffler 1267b7e3f244SSam Leffler static void 1268b7e3f244SSam Leffler safe_callback(struct safe_softc *sc, struct safe_ringentry *re) 1269b7e3f244SSam Leffler { 1270c0341432SJohn Baldwin const struct crypto_session_params *csp; 1271b7e3f244SSam Leffler struct cryptop *crp = (struct cryptop *)re->re_crp; 12721b0909d5SConrad Meyer struct safe_session *ses; 1273c0341432SJohn Baldwin uint8_t hash[HASH_MAX_LEN]; 1274b7e3f244SSam Leffler 12751b0909d5SConrad Meyer ses = crypto_get_driver_session(crp->crp_session); 1276c0341432SJohn Baldwin csp = crypto_get_params(crp->crp_session); 12771b0909d5SConrad Meyer 1278b7e3f244SSam Leffler safestats.st_opackets++; 1279b7e3f244SSam Leffler safestats.st_obytes += re->re_dst.mapsize; 1280b7e3f244SSam Leffler 1281b7e3f244SSam Leffler safe_dma_sync(&sc->sc_ringalloc, 1282b7e3f244SSam Leffler BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 1283b7e3f244SSam Leffler if (re->re_desc.d_csr & SAFE_PE_CSR_STATUS) { 1284b7e3f244SSam Leffler device_printf(sc->sc_dev, "csr 0x%x cmd0 0x%x cmd1 0x%x\n", 1285b7e3f244SSam Leffler re->re_desc.d_csr, 1286b7e3f244SSam Leffler re->re_sa.sa_cmd0, re->re_sa.sa_cmd1); 1287b7e3f244SSam Leffler safestats.st_peoperr++; 1288b7e3f244SSam Leffler crp->crp_etype = EIO; /* something more meaningful? */ 1289b7e3f244SSam Leffler } 1290c0341432SJohn Baldwin 12919c0e3d3aSJohn Baldwin /* 12929c0e3d3aSJohn Baldwin * XXX: Should crp_buf.cb_mbuf be updated to re->re_dst_m if 12939c0e3d3aSJohn Baldwin * it is non-NULL? 12949c0e3d3aSJohn Baldwin */ 1295c0341432SJohn Baldwin 1296b7e3f244SSam Leffler if (re->re_dst_map != NULL && re->re_dst_map != re->re_src_map) { 1297b7e3f244SSam Leffler bus_dmamap_sync(sc->sc_dstdmat, re->re_dst_map, 1298b7e3f244SSam Leffler BUS_DMASYNC_POSTREAD); 1299b7e3f244SSam Leffler bus_dmamap_unload(sc->sc_dstdmat, re->re_dst_map); 1300b7e3f244SSam Leffler bus_dmamap_destroy(sc->sc_dstdmat, re->re_dst_map); 1301b7e3f244SSam Leffler } 1302b7e3f244SSam Leffler bus_dmamap_sync(sc->sc_srcdmat, re->re_src_map, BUS_DMASYNC_POSTWRITE); 1303b7e3f244SSam Leffler bus_dmamap_unload(sc->sc_srcdmat, re->re_src_map); 1304b7e3f244SSam Leffler bus_dmamap_destroy(sc->sc_srcdmat, re->re_src_map); 1305b7e3f244SSam Leffler 1306b7e3f244SSam Leffler if (re->re_flags & SAFE_QFLAGS_COPYOUTICV) { 1307c0341432SJohn Baldwin if (csp->csp_auth_alg == CRYPTO_SHA1_HMAC) { 1308b7e3f244SSam Leffler /* 1309b7e3f244SSam Leffler * SHA-1 ICV's are byte-swapped; fix 'em up 1310c0341432SJohn Baldwin * before copying them to their destination. 1311b7e3f244SSam Leffler */ 1312357a26abSXin LI re->re_sastate.sa_saved_indigest[0] = 1313b7e3f244SSam Leffler bswap32(re->re_sastate.sa_saved_indigest[0]); 1314357a26abSXin LI re->re_sastate.sa_saved_indigest[1] = 1315b7e3f244SSam Leffler bswap32(re->re_sastate.sa_saved_indigest[1]); 1316357a26abSXin LI re->re_sastate.sa_saved_indigest[2] = 1317b7e3f244SSam Leffler bswap32(re->re_sastate.sa_saved_indigest[2]); 1318b7e3f244SSam Leffler } 1319c0341432SJohn Baldwin 1320c0341432SJohn Baldwin if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) { 1321c0341432SJohn Baldwin crypto_copydata(crp, crp->crp_digest_start, 1322c0341432SJohn Baldwin ses->ses_mlen, hash); 1323c0341432SJohn Baldwin if (timingsafe_bcmp(re->re_sastate.sa_saved_indigest, 1324c0341432SJohn Baldwin hash, ses->ses_mlen) != 0) 1325c0341432SJohn Baldwin crp->crp_etype = EBADMSG; 1326c0341432SJohn Baldwin } else 1327c0341432SJohn Baldwin crypto_copyback(crp, crp->crp_digest_start, 1328c0341432SJohn Baldwin ses->ses_mlen, re->re_sastate.sa_saved_indigest); 1329b7e3f244SSam Leffler } 1330b7e3f244SSam Leffler crypto_done(crp); 1331b7e3f244SSam Leffler } 1332b7e3f244SSam Leffler 1333b7e3f244SSam Leffler /* 1334b7e3f244SSam Leffler * Copy all data past offset from srcm to dstm. 1335b7e3f244SSam Leffler */ 1336b7e3f244SSam Leffler static void 1337b7e3f244SSam Leffler safe_mcopy(struct mbuf *srcm, struct mbuf *dstm, u_int offset) 1338b7e3f244SSam Leffler { 1339b7e3f244SSam Leffler u_int j, dlen, slen; 1340b7e3f244SSam Leffler caddr_t dptr, sptr; 1341b7e3f244SSam Leffler 1342b7e3f244SSam Leffler /* 1343b7e3f244SSam Leffler * Advance src and dst to offset. 1344b7e3f244SSam Leffler */ 1345b7e3f244SSam Leffler j = offset; 134619a9c3dfSRyan Libby while (j >= srcm->m_len) { 1347b7e3f244SSam Leffler j -= srcm->m_len; 1348b7e3f244SSam Leffler srcm = srcm->m_next; 1349b7e3f244SSam Leffler if (srcm == NULL) 1350b7e3f244SSam Leffler return; 1351b7e3f244SSam Leffler } 1352b7e3f244SSam Leffler sptr = mtod(srcm, caddr_t) + j; 1353b7e3f244SSam Leffler slen = srcm->m_len - j; 1354b7e3f244SSam Leffler 1355b7e3f244SSam Leffler j = offset; 135619a9c3dfSRyan Libby while (j >= dstm->m_len) { 1357b7e3f244SSam Leffler j -= dstm->m_len; 1358b7e3f244SSam Leffler dstm = dstm->m_next; 1359b7e3f244SSam Leffler if (dstm == NULL) 1360b7e3f244SSam Leffler return; 1361b7e3f244SSam Leffler } 1362b7e3f244SSam Leffler dptr = mtod(dstm, caddr_t) + j; 1363b7e3f244SSam Leffler dlen = dstm->m_len - j; 1364b7e3f244SSam Leffler 1365b7e3f244SSam Leffler /* 1366b7e3f244SSam Leffler * Copy everything that remains. 1367b7e3f244SSam Leffler */ 1368b7e3f244SSam Leffler for (;;) { 1369b7e3f244SSam Leffler j = min(slen, dlen); 1370b7e3f244SSam Leffler bcopy(sptr, dptr, j); 1371b7e3f244SSam Leffler if (slen == j) { 1372b7e3f244SSam Leffler srcm = srcm->m_next; 1373b7e3f244SSam Leffler if (srcm == NULL) 1374b7e3f244SSam Leffler return; 1375b7e3f244SSam Leffler sptr = srcm->m_data; 1376b7e3f244SSam Leffler slen = srcm->m_len; 1377b7e3f244SSam Leffler } else 1378b7e3f244SSam Leffler sptr += j, slen -= j; 1379b7e3f244SSam Leffler if (dlen == j) { 1380b7e3f244SSam Leffler dstm = dstm->m_next; 1381b7e3f244SSam Leffler if (dstm == NULL) 1382b7e3f244SSam Leffler return; 1383b7e3f244SSam Leffler dptr = dstm->m_data; 1384b7e3f244SSam Leffler dlen = dstm->m_len; 1385b7e3f244SSam Leffler } else 1386b7e3f244SSam Leffler dptr += j, dlen -= j; 1387b7e3f244SSam Leffler } 1388b7e3f244SSam Leffler } 1389b7e3f244SSam Leffler 1390b7e3f244SSam Leffler #ifndef SAFE_NO_RNG 1391b7e3f244SSam Leffler #define SAFE_RNG_MAXWAIT 1000 1392b7e3f244SSam Leffler 1393b7e3f244SSam Leffler static void 1394b7e3f244SSam Leffler safe_rng_init(struct safe_softc *sc) 1395b7e3f244SSam Leffler { 1396b7e3f244SSam Leffler u_int32_t w, v; 1397b7e3f244SSam Leffler int i; 1398b7e3f244SSam Leffler 1399b7e3f244SSam Leffler WRITE_REG(sc, SAFE_RNG_CTRL, 0); 1400b7e3f244SSam Leffler /* use default value according to the manual */ 1401b7e3f244SSam Leffler WRITE_REG(sc, SAFE_RNG_CNFG, 0x834); /* magic from SafeNet */ 1402b7e3f244SSam Leffler WRITE_REG(sc, SAFE_RNG_ALM_CNT, 0); 1403b7e3f244SSam Leffler 1404b7e3f244SSam Leffler /* 1405b7e3f244SSam Leffler * There is a bug in rev 1.0 of the 1140 that when the RNG 1406b7e3f244SSam Leffler * is brought out of reset the ready status flag does not 1407b7e3f244SSam Leffler * work until the RNG has finished its internal initialization. 1408b7e3f244SSam Leffler * 1409b7e3f244SSam Leffler * So in order to determine the device is through its 1410b7e3f244SSam Leffler * initialization we must read the data register, using the 1411b7e3f244SSam Leffler * status reg in the read in case it is initialized. Then read 1412b7e3f244SSam Leffler * the data register until it changes from the first read. 1413b7e3f244SSam Leffler * Once it changes read the data register until it changes 1414b7e3f244SSam Leffler * again. At this time the RNG is considered initialized. 1415b7e3f244SSam Leffler * This could take between 750ms - 1000ms in time. 1416b7e3f244SSam Leffler */ 1417b7e3f244SSam Leffler i = 0; 1418b7e3f244SSam Leffler w = READ_REG(sc, SAFE_RNG_OUT); 1419b7e3f244SSam Leffler do { 1420b7e3f244SSam Leffler v = READ_REG(sc, SAFE_RNG_OUT); 1421b7e3f244SSam Leffler if (v != w) { 1422b7e3f244SSam Leffler w = v; 1423b7e3f244SSam Leffler break; 1424b7e3f244SSam Leffler } 1425b7e3f244SSam Leffler DELAY(10); 1426b7e3f244SSam Leffler } while (++i < SAFE_RNG_MAXWAIT); 1427b7e3f244SSam Leffler 1428b7e3f244SSam Leffler /* Wait Until data changes again */ 1429b7e3f244SSam Leffler i = 0; 1430b7e3f244SSam Leffler do { 1431b7e3f244SSam Leffler v = READ_REG(sc, SAFE_RNG_OUT); 1432b7e3f244SSam Leffler if (v != w) 1433b7e3f244SSam Leffler break; 1434b7e3f244SSam Leffler DELAY(10); 1435b7e3f244SSam Leffler } while (++i < SAFE_RNG_MAXWAIT); 1436b7e3f244SSam Leffler } 1437b7e3f244SSam Leffler 1438b7e3f244SSam Leffler static __inline void 1439b7e3f244SSam Leffler safe_rng_disable_short_cycle(struct safe_softc *sc) 1440b7e3f244SSam Leffler { 1441b7e3f244SSam Leffler WRITE_REG(sc, SAFE_RNG_CTRL, 1442b7e3f244SSam Leffler READ_REG(sc, SAFE_RNG_CTRL) &~ SAFE_RNG_CTRL_SHORTEN); 1443b7e3f244SSam Leffler } 1444b7e3f244SSam Leffler 1445b7e3f244SSam Leffler static __inline void 1446b7e3f244SSam Leffler safe_rng_enable_short_cycle(struct safe_softc *sc) 1447b7e3f244SSam Leffler { 1448b7e3f244SSam Leffler WRITE_REG(sc, SAFE_RNG_CTRL, 1449b7e3f244SSam Leffler READ_REG(sc, SAFE_RNG_CTRL) | SAFE_RNG_CTRL_SHORTEN); 1450b7e3f244SSam Leffler } 1451b7e3f244SSam Leffler 1452b7e3f244SSam Leffler static __inline u_int32_t 1453b7e3f244SSam Leffler safe_rng_read(struct safe_softc *sc) 1454b7e3f244SSam Leffler { 1455b7e3f244SSam Leffler int i; 1456b7e3f244SSam Leffler 1457b7e3f244SSam Leffler i = 0; 1458b7e3f244SSam Leffler while (READ_REG(sc, SAFE_RNG_STAT) != 0 && ++i < SAFE_RNG_MAXWAIT) 1459b7e3f244SSam Leffler ; 1460b7e3f244SSam Leffler return READ_REG(sc, SAFE_RNG_OUT); 1461b7e3f244SSam Leffler } 1462b7e3f244SSam Leffler 1463b7e3f244SSam Leffler static void 1464b7e3f244SSam Leffler safe_rng(void *arg) 1465b7e3f244SSam Leffler { 1466b7e3f244SSam Leffler struct safe_softc *sc = arg; 1467b7e3f244SSam Leffler u_int32_t buf[SAFE_RNG_MAXBUFSIZ]; /* NB: maybe move to softc */ 1468b7e3f244SSam Leffler u_int maxwords; 1469b7e3f244SSam Leffler int i; 1470b7e3f244SSam Leffler 1471b7e3f244SSam Leffler safestats.st_rng++; 1472b7e3f244SSam Leffler /* 1473b7e3f244SSam Leffler * Fetch the next block of data. 1474b7e3f244SSam Leffler */ 1475b7e3f244SSam Leffler maxwords = safe_rngbufsize; 1476b7e3f244SSam Leffler if (maxwords > SAFE_RNG_MAXBUFSIZ) 1477b7e3f244SSam Leffler maxwords = SAFE_RNG_MAXBUFSIZ; 1478b7e3f244SSam Leffler retry: 1479b7e3f244SSam Leffler for (i = 0; i < maxwords; i++) 1480b7e3f244SSam Leffler buf[i] = safe_rng_read(sc); 1481b7e3f244SSam Leffler /* 1482b7e3f244SSam Leffler * Check the comparator alarm count and reset the h/w if 1483b7e3f244SSam Leffler * it exceeds our threshold. This guards against the 1484b7e3f244SSam Leffler * hardware oscillators resonating with external signals. 1485b7e3f244SSam Leffler */ 1486b7e3f244SSam Leffler if (READ_REG(sc, SAFE_RNG_ALM_CNT) > safe_rngmaxalarm) { 1487b7e3f244SSam Leffler u_int32_t freq_inc, w; 1488b7e3f244SSam Leffler 1489b7e3f244SSam Leffler DPRINTF(("%s: alarm count %u exceeds threshold %u\n", __func__, 1490b7e3f244SSam Leffler READ_REG(sc, SAFE_RNG_ALM_CNT), safe_rngmaxalarm)); 1491b7e3f244SSam Leffler safestats.st_rngalarm++; 1492b7e3f244SSam Leffler safe_rng_enable_short_cycle(sc); 1493b7e3f244SSam Leffler freq_inc = 18; 1494b7e3f244SSam Leffler for (i = 0; i < 64; i++) { 1495b7e3f244SSam Leffler w = READ_REG(sc, SAFE_RNG_CNFG); 1496b7e3f244SSam Leffler freq_inc = ((w + freq_inc) & 0x3fL); 1497b7e3f244SSam Leffler w = ((w & ~0x3fL) | freq_inc); 1498b7e3f244SSam Leffler WRITE_REG(sc, SAFE_RNG_CNFG, w); 1499b7e3f244SSam Leffler 1500b7e3f244SSam Leffler WRITE_REG(sc, SAFE_RNG_ALM_CNT, 0); 1501b7e3f244SSam Leffler 1502b7e3f244SSam Leffler (void) safe_rng_read(sc); 1503b7e3f244SSam Leffler DELAY(25); 1504b7e3f244SSam Leffler 1505b7e3f244SSam Leffler if (READ_REG(sc, SAFE_RNG_ALM_CNT) == 0) { 1506b7e3f244SSam Leffler safe_rng_disable_short_cycle(sc); 1507b7e3f244SSam Leffler goto retry; 1508b7e3f244SSam Leffler } 1509b7e3f244SSam Leffler freq_inc = 1; 1510b7e3f244SSam Leffler } 1511b7e3f244SSam Leffler safe_rng_disable_short_cycle(sc); 1512b7e3f244SSam Leffler } else 1513b7e3f244SSam Leffler WRITE_REG(sc, SAFE_RNG_ALM_CNT, 0); 1514b7e3f244SSam Leffler 1515b7e3f244SSam Leffler (*sc->sc_harvest)(sc->sc_rndtest, buf, maxwords*sizeof (u_int32_t)); 1516b7e3f244SSam Leffler callout_reset(&sc->sc_rngto, 1517b7e3f244SSam Leffler hz * (safe_rnginterval ? safe_rnginterval : 1), safe_rng, sc); 1518b7e3f244SSam Leffler } 1519b7e3f244SSam Leffler #endif /* SAFE_NO_RNG */ 1520b7e3f244SSam Leffler 1521b7e3f244SSam Leffler static void 1522b7e3f244SSam Leffler safe_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 1523b7e3f244SSam Leffler { 1524b7e3f244SSam Leffler bus_addr_t *paddr = (bus_addr_t*) arg; 1525b7e3f244SSam Leffler *paddr = segs->ds_addr; 1526b7e3f244SSam Leffler } 1527b7e3f244SSam Leffler 1528b7e3f244SSam Leffler static int 1529b7e3f244SSam Leffler safe_dma_malloc( 1530b7e3f244SSam Leffler struct safe_softc *sc, 1531b7e3f244SSam Leffler bus_size_t size, 1532b7e3f244SSam Leffler struct safe_dma_alloc *dma, 1533b7e3f244SSam Leffler int mapflags 1534b7e3f244SSam Leffler ) 1535b7e3f244SSam Leffler { 1536b7e3f244SSam Leffler int r; 1537b7e3f244SSam Leffler 153862ce43ccSScott Long r = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), /* parent */ 1539b7e3f244SSam Leffler sizeof(u_int32_t), 0, /* alignment, bounds */ 1540b7e3f244SSam Leffler BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 1541b7e3f244SSam Leffler BUS_SPACE_MAXADDR, /* highaddr */ 1542b7e3f244SSam Leffler NULL, NULL, /* filter, filterarg */ 1543b7e3f244SSam Leffler size, /* maxsize */ 1544b7e3f244SSam Leffler 1, /* nsegments */ 1545b7e3f244SSam Leffler size, /* maxsegsize */ 1546b7e3f244SSam Leffler BUS_DMA_ALLOCNOW, /* flags */ 1547b7e3f244SSam Leffler NULL, NULL, /* locking */ 1548b7e3f244SSam Leffler &dma->dma_tag); 1549b7e3f244SSam Leffler if (r != 0) { 1550b7e3f244SSam Leffler device_printf(sc->sc_dev, "safe_dma_malloc: " 1551b7e3f244SSam Leffler "bus_dma_tag_create failed; error %u\n", r); 1552b7e3f244SSam Leffler goto fail_0; 1553b7e3f244SSam Leffler } 1554b7e3f244SSam Leffler 1555b7e3f244SSam Leffler r = bus_dmamem_alloc(dma->dma_tag, (void**) &dma->dma_vaddr, 1556b7e3f244SSam Leffler BUS_DMA_NOWAIT, &dma->dma_map); 1557b7e3f244SSam Leffler if (r != 0) { 1558b7e3f244SSam Leffler device_printf(sc->sc_dev, "safe_dma_malloc: " 1559d14c4346SJohn-Mark Gurney "bus_dmammem_alloc failed; size %ju, error %u\n", 1560d14c4346SJohn-Mark Gurney (uintmax_t)size, r); 1561f07894dbSJohn Baldwin goto fail_1; 1562b7e3f244SSam Leffler } 1563b7e3f244SSam Leffler 1564b7e3f244SSam Leffler r = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr, 1565b7e3f244SSam Leffler size, 1566b7e3f244SSam Leffler safe_dmamap_cb, 1567b7e3f244SSam Leffler &dma->dma_paddr, 1568b7e3f244SSam Leffler mapflags | BUS_DMA_NOWAIT); 1569b7e3f244SSam Leffler if (r != 0) { 1570b7e3f244SSam Leffler device_printf(sc->sc_dev, "safe_dma_malloc: " 1571b7e3f244SSam Leffler "bus_dmamap_load failed; error %u\n", r); 1572f07894dbSJohn Baldwin goto fail_2; 1573b7e3f244SSam Leffler } 1574b7e3f244SSam Leffler 1575b7e3f244SSam Leffler dma->dma_size = size; 1576b7e3f244SSam Leffler return (0); 1577b7e3f244SSam Leffler 1578b7e3f244SSam Leffler bus_dmamap_unload(dma->dma_tag, dma->dma_map); 1579b7e3f244SSam Leffler fail_2: 1580b7e3f244SSam Leffler bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map); 1581b7e3f244SSam Leffler fail_1: 1582b7e3f244SSam Leffler bus_dma_tag_destroy(dma->dma_tag); 1583b7e3f244SSam Leffler fail_0: 1584b7e3f244SSam Leffler dma->dma_tag = NULL; 1585b7e3f244SSam Leffler return (r); 1586b7e3f244SSam Leffler } 1587b7e3f244SSam Leffler 1588b7e3f244SSam Leffler static void 1589b7e3f244SSam Leffler safe_dma_free(struct safe_softc *sc, struct safe_dma_alloc *dma) 1590b7e3f244SSam Leffler { 1591b7e3f244SSam Leffler bus_dmamap_unload(dma->dma_tag, dma->dma_map); 1592b7e3f244SSam Leffler bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map); 1593b7e3f244SSam Leffler bus_dma_tag_destroy(dma->dma_tag); 1594b7e3f244SSam Leffler } 1595b7e3f244SSam Leffler 1596b7e3f244SSam Leffler /* 1597b7e3f244SSam Leffler * Resets the board. Values in the regesters are left as is 1598b7e3f244SSam Leffler * from the reset (i.e. initial values are assigned elsewhere). 1599b7e3f244SSam Leffler */ 1600b7e3f244SSam Leffler static void 1601b7e3f244SSam Leffler safe_reset_board(struct safe_softc *sc) 1602b7e3f244SSam Leffler { 1603b7e3f244SSam Leffler u_int32_t v; 1604b7e3f244SSam Leffler /* 1605b7e3f244SSam Leffler * Reset the device. The manual says no delay 1606b7e3f244SSam Leffler * is needed between marking and clearing reset. 1607b7e3f244SSam Leffler */ 1608b7e3f244SSam Leffler v = READ_REG(sc, SAFE_PE_DMACFG) &~ 1609b7e3f244SSam Leffler (SAFE_PE_DMACFG_PERESET | SAFE_PE_DMACFG_PDRRESET | 1610b7e3f244SSam Leffler SAFE_PE_DMACFG_SGRESET); 1611b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_DMACFG, v 1612b7e3f244SSam Leffler | SAFE_PE_DMACFG_PERESET 1613b7e3f244SSam Leffler | SAFE_PE_DMACFG_PDRRESET 1614b7e3f244SSam Leffler | SAFE_PE_DMACFG_SGRESET); 1615b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_DMACFG, v); 1616b7e3f244SSam Leffler } 1617b7e3f244SSam Leffler 1618b7e3f244SSam Leffler /* 1619b7e3f244SSam Leffler * Initialize registers we need to touch only once. 1620b7e3f244SSam Leffler */ 1621b7e3f244SSam Leffler static void 1622b7e3f244SSam Leffler safe_init_board(struct safe_softc *sc) 1623b7e3f244SSam Leffler { 1624b7e3f244SSam Leffler u_int32_t v, dwords; 1625b7e3f244SSam Leffler 1626c2ede4b3SMartin Blapp v = READ_REG(sc, SAFE_PE_DMACFG); 1627b7e3f244SSam Leffler v &=~ SAFE_PE_DMACFG_PEMODE; 1628b7e3f244SSam Leffler v |= SAFE_PE_DMACFG_FSENA /* failsafe enable */ 1629b7e3f244SSam Leffler | SAFE_PE_DMACFG_GPRPCI /* gather ring on PCI */ 1630b7e3f244SSam Leffler | SAFE_PE_DMACFG_SPRPCI /* scatter ring on PCI */ 1631b7e3f244SSam Leffler | SAFE_PE_DMACFG_ESDESC /* endian-swap descriptors */ 1632b7e3f244SSam Leffler | SAFE_PE_DMACFG_ESSA /* endian-swap SA's */ 1633b7e3f244SSam Leffler | SAFE_PE_DMACFG_ESPDESC /* endian-swap part. desc's */ 1634b7e3f244SSam Leffler ; 1635b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_DMACFG, v); 1636b7e3f244SSam Leffler #if 0 1637b7e3f244SSam Leffler /* XXX select byte swap based on host byte order */ 1638b7e3f244SSam Leffler WRITE_REG(sc, SAFE_ENDIAN, 0x1b); 1639b7e3f244SSam Leffler #endif 1640b7e3f244SSam Leffler if (sc->sc_chiprev == SAFE_REV(1,0)) { 1641b7e3f244SSam Leffler /* 1642b7e3f244SSam Leffler * Avoid large PCI DMA transfers. Rev 1.0 has a bug where 1643b7e3f244SSam Leffler * "target mode transfers" done while the chip is DMA'ing 1644b7e3f244SSam Leffler * >1020 bytes cause the hardware to lockup. To avoid this 1645b7e3f244SSam Leffler * we reduce the max PCI transfer size and use small source 1646b7e3f244SSam Leffler * particle descriptors (<= 256 bytes). 1647b7e3f244SSam Leffler */ 1648b7e3f244SSam Leffler WRITE_REG(sc, SAFE_DMA_CFG, 256); 1649b7e3f244SSam Leffler device_printf(sc->sc_dev, 1650b7e3f244SSam Leffler "Reduce max DMA size to %u words for rev %u.%u WAR\n", 1651b7e3f244SSam Leffler (READ_REG(sc, SAFE_DMA_CFG)>>2) & 0xff, 1652b7e3f244SSam Leffler SAFE_REV_MAJ(sc->sc_chiprev), 1653b7e3f244SSam Leffler SAFE_REV_MIN(sc->sc_chiprev)); 1654b7e3f244SSam Leffler } 1655b7e3f244SSam Leffler 1656b7e3f244SSam Leffler /* NB: operands+results are overlaid */ 1657b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_PDRBASE, sc->sc_ringalloc.dma_paddr); 1658b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_RDRBASE, sc->sc_ringalloc.dma_paddr); 1659b7e3f244SSam Leffler /* 1660b7e3f244SSam Leffler * Configure ring entry size and number of items in the ring. 1661b7e3f244SSam Leffler */ 1662b7e3f244SSam Leffler KASSERT((sizeof(struct safe_ringentry) % sizeof(u_int32_t)) == 0, 1663b7e3f244SSam Leffler ("PE ring entry not 32-bit aligned!")); 1664b7e3f244SSam Leffler dwords = sizeof(struct safe_ringentry) / sizeof(u_int32_t); 1665b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_RINGCFG, 1666b7e3f244SSam Leffler (dwords << SAFE_PE_RINGCFG_OFFSET_S) | SAFE_MAX_NQUEUE); 1667b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_RINGPOLL, 0); /* disable polling */ 1668b7e3f244SSam Leffler 1669b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_GRNGBASE, sc->sc_spalloc.dma_paddr); 1670b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_SRNGBASE, sc->sc_dpalloc.dma_paddr); 1671b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_PARTSIZE, 1672b7e3f244SSam Leffler (SAFE_TOTAL_DPART<<16) | SAFE_TOTAL_SPART); 1673b7e3f244SSam Leffler /* 1674b7e3f244SSam Leffler * NB: destination particles are fixed size. We use 1675b7e3f244SSam Leffler * an mbuf cluster and require all results go to 1676b7e3f244SSam Leffler * clusters or smaller. 1677b7e3f244SSam Leffler */ 1678b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_PARTCFG, SAFE_MAX_DSIZE); 1679b7e3f244SSam Leffler 1680b7e3f244SSam Leffler /* it's now safe to enable PE mode, do it */ 1681b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_DMACFG, v | SAFE_PE_DMACFG_PEMODE); 1682b7e3f244SSam Leffler 1683b7e3f244SSam Leffler /* 1684b7e3f244SSam Leffler * Configure hardware to use level-triggered interrupts and 1685b7e3f244SSam Leffler * to interrupt after each descriptor is processed. 1686b7e3f244SSam Leffler */ 1687b7e3f244SSam Leffler WRITE_REG(sc, SAFE_HI_CFG, SAFE_HI_CFG_LEVEL); 1688b7e3f244SSam Leffler WRITE_REG(sc, SAFE_HI_DESC_CNT, 1); 1689b7e3f244SSam Leffler WRITE_REG(sc, SAFE_HI_MASK, SAFE_INT_PE_DDONE | SAFE_INT_PE_ERROR); 1690b7e3f244SSam Leffler } 1691b7e3f244SSam Leffler 1692b7e3f244SSam Leffler /* 1693b7e3f244SSam Leffler * Init PCI registers 1694b7e3f244SSam Leffler */ 1695b7e3f244SSam Leffler static void 1696b7e3f244SSam Leffler safe_init_pciregs(device_t dev) 1697b7e3f244SSam Leffler { 1698b7e3f244SSam Leffler } 1699b7e3f244SSam Leffler 1700b7e3f244SSam Leffler /* 1701b7e3f244SSam Leffler * Clean up after a chip crash. 1702b7e3f244SSam Leffler * It is assumed that the caller in splimp() 1703b7e3f244SSam Leffler */ 1704b7e3f244SSam Leffler static void 1705b7e3f244SSam Leffler safe_cleanchip(struct safe_softc *sc) 1706b7e3f244SSam Leffler { 1707b7e3f244SSam Leffler 1708b7e3f244SSam Leffler if (sc->sc_nqchip != 0) { 1709b7e3f244SSam Leffler struct safe_ringentry *re = sc->sc_back; 1710b7e3f244SSam Leffler 1711b7e3f244SSam Leffler while (re != sc->sc_front) { 1712b7e3f244SSam Leffler if (re->re_desc.d_csr != 0) 1713b7e3f244SSam Leffler safe_free_entry(sc, re); 1714b7e3f244SSam Leffler if (++re == sc->sc_ringtop) 1715b7e3f244SSam Leffler re = sc->sc_ring; 1716b7e3f244SSam Leffler } 1717b7e3f244SSam Leffler sc->sc_back = re; 1718b7e3f244SSam Leffler sc->sc_nqchip = 0; 1719b7e3f244SSam Leffler } 1720b7e3f244SSam Leffler } 1721b7e3f244SSam Leffler 1722b7e3f244SSam Leffler /* 1723b7e3f244SSam Leffler * free a safe_q 1724b7e3f244SSam Leffler * It is assumed that the caller is within splimp(). 1725b7e3f244SSam Leffler */ 1726b7e3f244SSam Leffler static int 1727b7e3f244SSam Leffler safe_free_entry(struct safe_softc *sc, struct safe_ringentry *re) 1728b7e3f244SSam Leffler { 1729b7e3f244SSam Leffler struct cryptop *crp; 1730b7e3f244SSam Leffler 1731b7e3f244SSam Leffler /* 1732b7e3f244SSam Leffler * Free header MCR 1733b7e3f244SSam Leffler */ 1734c0341432SJohn Baldwin if (re->re_dst_m != NULL) 1735b7e3f244SSam Leffler m_freem(re->re_dst_m); 1736b7e3f244SSam Leffler 1737b7e3f244SSam Leffler crp = (struct cryptop *)re->re_crp; 1738b7e3f244SSam Leffler 1739b7e3f244SSam Leffler re->re_desc.d_csr = 0; 1740b7e3f244SSam Leffler 1741b7e3f244SSam Leffler crp->crp_etype = EFAULT; 1742b7e3f244SSam Leffler crypto_done(crp); 1743b7e3f244SSam Leffler return(0); 1744b7e3f244SSam Leffler } 1745b7e3f244SSam Leffler 1746b7e3f244SSam Leffler /* 1747b7e3f244SSam Leffler * Routine to reset the chip and clean up. 1748b7e3f244SSam Leffler * It is assumed that the caller is in splimp() 1749b7e3f244SSam Leffler */ 1750b7e3f244SSam Leffler static void 1751b7e3f244SSam Leffler safe_totalreset(struct safe_softc *sc) 1752b7e3f244SSam Leffler { 1753b7e3f244SSam Leffler safe_reset_board(sc); 1754b7e3f244SSam Leffler safe_init_board(sc); 1755b7e3f244SSam Leffler safe_cleanchip(sc); 1756b7e3f244SSam Leffler } 1757b7e3f244SSam Leffler 1758b7e3f244SSam Leffler /* 1759b7e3f244SSam Leffler * Is the operand suitable aligned for direct DMA. Each 1760b7e3f244SSam Leffler * segment must be aligned on a 32-bit boundary and all 1761b7e3f244SSam Leffler * but the last segment must be a multiple of 4 bytes. 1762b7e3f244SSam Leffler */ 1763b7e3f244SSam Leffler static int 1764b7e3f244SSam Leffler safe_dmamap_aligned(const struct safe_operand *op) 1765b7e3f244SSam Leffler { 1766b7e3f244SSam Leffler int i; 1767b7e3f244SSam Leffler 1768b7e3f244SSam Leffler for (i = 0; i < op->nsegs; i++) { 1769b7e3f244SSam Leffler if (op->segs[i].ds_addr & 3) 1770b7e3f244SSam Leffler return (0); 1771b7e3f244SSam Leffler if (i != (op->nsegs - 1) && (op->segs[i].ds_len & 3)) 1772b7e3f244SSam Leffler return (0); 1773b7e3f244SSam Leffler } 1774b7e3f244SSam Leffler return (1); 1775b7e3f244SSam Leffler } 1776b7e3f244SSam Leffler 1777b7e3f244SSam Leffler /* 1778b7e3f244SSam Leffler * Is the operand suitable for direct DMA as the destination 1779b7e3f244SSam Leffler * of an operation. The hardware requires that each ``particle'' 1780b7e3f244SSam Leffler * but the last in an operation result have the same size. We 1781b7e3f244SSam Leffler * fix that size at SAFE_MAX_DSIZE bytes. This routine returns 1782b7e3f244SSam Leffler * 0 if some segment is not a multiple of of this size, 1 if all 1783b7e3f244SSam Leffler * segments are exactly this size, or 2 if segments are at worst 1784*81d4309fSGordon Bergling * a multiple of this size. 1785b7e3f244SSam Leffler */ 1786b7e3f244SSam Leffler static int 1787b7e3f244SSam Leffler safe_dmamap_uniform(const struct safe_operand *op) 1788b7e3f244SSam Leffler { 1789b7e3f244SSam Leffler int result = 1; 1790b7e3f244SSam Leffler 1791b7e3f244SSam Leffler if (op->nsegs > 0) { 1792b7e3f244SSam Leffler int i; 1793b7e3f244SSam Leffler 1794900017e8SSam Leffler for (i = 0; i < op->nsegs-1; i++) { 1795b7e3f244SSam Leffler if (op->segs[i].ds_len % SAFE_MAX_DSIZE) 1796b7e3f244SSam Leffler return (0); 1797b7e3f244SSam Leffler if (op->segs[i].ds_len != SAFE_MAX_DSIZE) 1798b7e3f244SSam Leffler result = 2; 1799b7e3f244SSam Leffler } 1800900017e8SSam Leffler } 1801b7e3f244SSam Leffler return (result); 1802b7e3f244SSam Leffler } 1803b7e3f244SSam Leffler 1804b7e3f244SSam Leffler #ifdef SAFE_DEBUG 1805b7e3f244SSam Leffler static void 1806b7e3f244SSam Leffler safe_dump_dmastatus(struct safe_softc *sc, const char *tag) 1807b7e3f244SSam Leffler { 1808b7e3f244SSam Leffler printf("%s: ENDIAN 0x%x SRC 0x%x DST 0x%x STAT 0x%x\n" 1809b7e3f244SSam Leffler , tag 1810b7e3f244SSam Leffler , READ_REG(sc, SAFE_DMA_ENDIAN) 1811b7e3f244SSam Leffler , READ_REG(sc, SAFE_DMA_SRCADDR) 1812b7e3f244SSam Leffler , READ_REG(sc, SAFE_DMA_DSTADDR) 1813b7e3f244SSam Leffler , READ_REG(sc, SAFE_DMA_STAT) 1814b7e3f244SSam Leffler ); 1815b7e3f244SSam Leffler } 1816b7e3f244SSam Leffler 1817b7e3f244SSam Leffler static void 1818b7e3f244SSam Leffler safe_dump_intrstate(struct safe_softc *sc, const char *tag) 1819b7e3f244SSam Leffler { 1820b7e3f244SSam Leffler printf("%s: HI_CFG 0x%x HI_MASK 0x%x HI_DESC_CNT 0x%x HU_STAT 0x%x HM_STAT 0x%x\n" 1821b7e3f244SSam Leffler , tag 1822b7e3f244SSam Leffler , READ_REG(sc, SAFE_HI_CFG) 1823b7e3f244SSam Leffler , READ_REG(sc, SAFE_HI_MASK) 1824b7e3f244SSam Leffler , READ_REG(sc, SAFE_HI_DESC_CNT) 1825b7e3f244SSam Leffler , READ_REG(sc, SAFE_HU_STAT) 1826b7e3f244SSam Leffler , READ_REG(sc, SAFE_HM_STAT) 1827b7e3f244SSam Leffler ); 1828b7e3f244SSam Leffler } 1829b7e3f244SSam Leffler 1830b7e3f244SSam Leffler static void 1831b7e3f244SSam Leffler safe_dump_ringstate(struct safe_softc *sc, const char *tag) 1832b7e3f244SSam Leffler { 1833b7e3f244SSam Leffler u_int32_t estat = READ_REG(sc, SAFE_PE_ERNGSTAT); 1834b7e3f244SSam Leffler 1835b7e3f244SSam Leffler /* NB: assume caller has lock on ring */ 1836668329e9SPeter Wemm printf("%s: ERNGSTAT %x (next %u) back %lu front %lu\n", 1837b7e3f244SSam Leffler tag, 1838b7e3f244SSam Leffler estat, (estat >> SAFE_PE_ERNGSTAT_NEXT_S), 1839668329e9SPeter Wemm (unsigned long)(sc->sc_back - sc->sc_ring), 1840668329e9SPeter Wemm (unsigned long)(sc->sc_front - sc->sc_ring)); 1841b7e3f244SSam Leffler } 1842b7e3f244SSam Leffler 1843b7e3f244SSam Leffler static void 1844b7e3f244SSam Leffler safe_dump_request(struct safe_softc *sc, const char* tag, struct safe_ringentry *re) 1845b7e3f244SSam Leffler { 1846b7e3f244SSam Leffler int ix, nsegs; 1847b7e3f244SSam Leffler 1848b7e3f244SSam Leffler ix = re - sc->sc_ring; 1849b7e3f244SSam Leffler printf("%s: %p (%u): csr %x src %x dst %x sa %x len %x\n" 1850b7e3f244SSam Leffler , tag 1851b7e3f244SSam Leffler , re, ix 1852b7e3f244SSam Leffler , re->re_desc.d_csr 1853b7e3f244SSam Leffler , re->re_desc.d_src 1854b7e3f244SSam Leffler , re->re_desc.d_dst 1855b7e3f244SSam Leffler , re->re_desc.d_sa 1856b7e3f244SSam Leffler , re->re_desc.d_len 1857b7e3f244SSam Leffler ); 1858b7e3f244SSam Leffler if (re->re_src.nsegs > 1) { 1859b7e3f244SSam Leffler ix = (re->re_desc.d_src - sc->sc_spalloc.dma_paddr) / 1860b7e3f244SSam Leffler sizeof(struct safe_pdesc); 1861b7e3f244SSam Leffler for (nsegs = re->re_src.nsegs; nsegs; nsegs--) { 1862b7e3f244SSam Leffler printf(" spd[%u] %p: %p size %u flags %x" 1863b7e3f244SSam Leffler , ix, &sc->sc_spring[ix] 1864668329e9SPeter Wemm , (caddr_t)(uintptr_t) sc->sc_spring[ix].pd_addr 1865b7e3f244SSam Leffler , sc->sc_spring[ix].pd_size 1866b7e3f244SSam Leffler , sc->sc_spring[ix].pd_flags 1867b7e3f244SSam Leffler ); 1868b7e3f244SSam Leffler if (sc->sc_spring[ix].pd_size == 0) 1869b7e3f244SSam Leffler printf(" (zero!)"); 1870b7e3f244SSam Leffler printf("\n"); 1871b7e3f244SSam Leffler if (++ix == SAFE_TOTAL_SPART) 1872b7e3f244SSam Leffler ix = 0; 1873b7e3f244SSam Leffler } 1874b7e3f244SSam Leffler } 1875b7e3f244SSam Leffler if (re->re_dst.nsegs > 1) { 1876b7e3f244SSam Leffler ix = (re->re_desc.d_dst - sc->sc_dpalloc.dma_paddr) / 1877b7e3f244SSam Leffler sizeof(struct safe_pdesc); 1878b7e3f244SSam Leffler for (nsegs = re->re_dst.nsegs; nsegs; nsegs--) { 1879b7e3f244SSam Leffler printf(" dpd[%u] %p: %p flags %x\n" 1880b7e3f244SSam Leffler , ix, &sc->sc_dpring[ix] 1881668329e9SPeter Wemm , (caddr_t)(uintptr_t) sc->sc_dpring[ix].pd_addr 1882b7e3f244SSam Leffler , sc->sc_dpring[ix].pd_flags 1883b7e3f244SSam Leffler ); 1884b7e3f244SSam Leffler if (++ix == SAFE_TOTAL_DPART) 1885b7e3f244SSam Leffler ix = 0; 1886b7e3f244SSam Leffler } 1887b7e3f244SSam Leffler } 1888b7e3f244SSam Leffler printf("sa: cmd0 %08x cmd1 %08x staterec %x\n", 1889b7e3f244SSam Leffler re->re_sa.sa_cmd0, re->re_sa.sa_cmd1, re->re_sa.sa_staterec); 1890b7e3f244SSam Leffler printf("sa: key %x %x %x %x %x %x %x %x\n" 1891b7e3f244SSam Leffler , re->re_sa.sa_key[0] 1892b7e3f244SSam Leffler , re->re_sa.sa_key[1] 1893b7e3f244SSam Leffler , re->re_sa.sa_key[2] 1894b7e3f244SSam Leffler , re->re_sa.sa_key[3] 1895b7e3f244SSam Leffler , re->re_sa.sa_key[4] 1896b7e3f244SSam Leffler , re->re_sa.sa_key[5] 1897b7e3f244SSam Leffler , re->re_sa.sa_key[6] 1898b7e3f244SSam Leffler , re->re_sa.sa_key[7] 1899b7e3f244SSam Leffler ); 1900b7e3f244SSam Leffler printf("sa: indigest %x %x %x %x %x\n" 1901b7e3f244SSam Leffler , re->re_sa.sa_indigest[0] 1902b7e3f244SSam Leffler , re->re_sa.sa_indigest[1] 1903b7e3f244SSam Leffler , re->re_sa.sa_indigest[2] 1904b7e3f244SSam Leffler , re->re_sa.sa_indigest[3] 1905b7e3f244SSam Leffler , re->re_sa.sa_indigest[4] 1906b7e3f244SSam Leffler ); 1907b7e3f244SSam Leffler printf("sa: outdigest %x %x %x %x %x\n" 1908b7e3f244SSam Leffler , re->re_sa.sa_outdigest[0] 1909b7e3f244SSam Leffler , re->re_sa.sa_outdigest[1] 1910b7e3f244SSam Leffler , re->re_sa.sa_outdigest[2] 1911b7e3f244SSam Leffler , re->re_sa.sa_outdigest[3] 1912b7e3f244SSam Leffler , re->re_sa.sa_outdigest[4] 1913b7e3f244SSam Leffler ); 1914b7e3f244SSam Leffler printf("sr: iv %x %x %x %x\n" 1915b7e3f244SSam Leffler , re->re_sastate.sa_saved_iv[0] 1916b7e3f244SSam Leffler , re->re_sastate.sa_saved_iv[1] 1917b7e3f244SSam Leffler , re->re_sastate.sa_saved_iv[2] 1918b7e3f244SSam Leffler , re->re_sastate.sa_saved_iv[3] 1919b7e3f244SSam Leffler ); 1920b7e3f244SSam Leffler printf("sr: hashbc %u indigest %x %x %x %x %x\n" 1921b7e3f244SSam Leffler , re->re_sastate.sa_saved_hashbc 1922b7e3f244SSam Leffler , re->re_sastate.sa_saved_indigest[0] 1923b7e3f244SSam Leffler , re->re_sastate.sa_saved_indigest[1] 1924b7e3f244SSam Leffler , re->re_sastate.sa_saved_indigest[2] 1925b7e3f244SSam Leffler , re->re_sastate.sa_saved_indigest[3] 1926b7e3f244SSam Leffler , re->re_sastate.sa_saved_indigest[4] 1927b7e3f244SSam Leffler ); 1928b7e3f244SSam Leffler } 1929b7e3f244SSam Leffler 1930b7e3f244SSam Leffler static void 1931b7e3f244SSam Leffler safe_dump_ring(struct safe_softc *sc, const char *tag) 1932b7e3f244SSam Leffler { 1933b7e3f244SSam Leffler mtx_lock(&sc->sc_ringmtx); 1934b7e3f244SSam Leffler printf("\nSafeNet Ring State:\n"); 1935b7e3f244SSam Leffler safe_dump_intrstate(sc, tag); 1936b7e3f244SSam Leffler safe_dump_dmastatus(sc, tag); 1937b7e3f244SSam Leffler safe_dump_ringstate(sc, tag); 1938b7e3f244SSam Leffler if (sc->sc_nqchip) { 1939b7e3f244SSam Leffler struct safe_ringentry *re = sc->sc_back; 1940b7e3f244SSam Leffler do { 1941b7e3f244SSam Leffler safe_dump_request(sc, tag, re); 1942b7e3f244SSam Leffler if (++re == sc->sc_ringtop) 1943b7e3f244SSam Leffler re = sc->sc_ring; 1944b7e3f244SSam Leffler } while (re != sc->sc_front); 1945b7e3f244SSam Leffler } 1946b7e3f244SSam Leffler mtx_unlock(&sc->sc_ringmtx); 1947b7e3f244SSam Leffler } 1948b7e3f244SSam Leffler 1949b7e3f244SSam Leffler static int 1950b7e3f244SSam Leffler sysctl_hw_safe_dump(SYSCTL_HANDLER_ARGS) 1951b7e3f244SSam Leffler { 1952b7e3f244SSam Leffler char dmode[64]; 1953b7e3f244SSam Leffler int error; 1954b7e3f244SSam Leffler 1955b7e3f244SSam Leffler strncpy(dmode, "", sizeof(dmode) - 1); 1956b7e3f244SSam Leffler dmode[sizeof(dmode) - 1] = '\0'; 1957b7e3f244SSam Leffler error = sysctl_handle_string(oidp, &dmode[0], sizeof(dmode), req); 1958b7e3f244SSam Leffler 1959b7e3f244SSam Leffler if (error == 0 && req->newptr != NULL) { 1960b7e3f244SSam Leffler struct safe_softc *sc = safec; 1961b7e3f244SSam Leffler 1962b7e3f244SSam Leffler if (!sc) 1963b7e3f244SSam Leffler return EINVAL; 1964b7e3f244SSam Leffler if (strncmp(dmode, "dma", 3) == 0) 1965b7e3f244SSam Leffler safe_dump_dmastatus(sc, "safe0"); 1966b7e3f244SSam Leffler else if (strncmp(dmode, "int", 3) == 0) 1967b7e3f244SSam Leffler safe_dump_intrstate(sc, "safe0"); 1968b7e3f244SSam Leffler else if (strncmp(dmode, "ring", 4) == 0) 1969b7e3f244SSam Leffler safe_dump_ring(sc, "safe0"); 1970b7e3f244SSam Leffler else 1971b7e3f244SSam Leffler return EINVAL; 1972b7e3f244SSam Leffler } 1973b7e3f244SSam Leffler return error; 1974b7e3f244SSam Leffler } 19757029da5cSPawel Biernacki SYSCTL_PROC(_hw_safe, OID_AUTO, dump, 19767029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, 0, 19777029da5cSPawel Biernacki sysctl_hw_safe_dump, "A", 19787029da5cSPawel Biernacki "Dump driver state"); 1979b7e3f244SSam Leffler #endif /* SAFE_DEBUG */ 1980