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; 3886810ad6fSSam Leffler crypto_kregister(sc->sc_cid, CRK_MOD_EXP, 0); 3896810ad6fSSam Leffler crypto_kregister(sc->sc_cid, CRK_MOD_EXP_CRT, 0); 390b7e3f244SSam Leffler #endif 391b7e3f244SSam Leffler } 392c0341432SJohn Baldwin if (sc->sc_devinfo & SAFE_DEVINFO_DES) { 393b7e3f244SSam Leffler printf(" des/3des"); 394b7e3f244SSam Leffler } 395c0341432SJohn Baldwin if (sc->sc_devinfo & SAFE_DEVINFO_AES) { 396b7e3f244SSam Leffler printf(" aes"); 397b7e3f244SSam Leffler } 398c0341432SJohn Baldwin if (sc->sc_devinfo & SAFE_DEVINFO_MD5) { 399b7e3f244SSam Leffler printf(" md5"); 400b7e3f244SSam Leffler } 401c0341432SJohn Baldwin if (sc->sc_devinfo & SAFE_DEVINFO_SHA1) { 402b7e3f244SSam Leffler printf(" sha1"); 403b7e3f244SSam Leffler } 404b7e3f244SSam Leffler /* XXX other supported algorithms */ 405b7e3f244SSam Leffler printf("\n"); 406b7e3f244SSam Leffler 407b7e3f244SSam Leffler safe_reset_board(sc); /* reset h/w */ 408b7e3f244SSam Leffler safe_init_pciregs(dev); /* init pci settings */ 409b7e3f244SSam Leffler safe_init_board(sc); /* init h/w */ 410b7e3f244SSam Leffler 411b7e3f244SSam Leffler #ifndef SAFE_NO_RNG 412b7e3f244SSam Leffler if (sc->sc_flags & SAFE_FLAGS_RNG) { 413b7e3f244SSam Leffler #ifdef SAFE_RNDTEST 414b7e3f244SSam Leffler sc->sc_rndtest = rndtest_attach(dev); 415b7e3f244SSam Leffler if (sc->sc_rndtest) 416b7e3f244SSam Leffler sc->sc_harvest = rndtest_harvest; 417b7e3f244SSam Leffler else 418b7e3f244SSam Leffler sc->sc_harvest = default_harvest; 419b7e3f244SSam Leffler #else 420b7e3f244SSam Leffler sc->sc_harvest = default_harvest; 421b7e3f244SSam Leffler #endif 422b7e3f244SSam Leffler safe_rng_init(sc); 423b7e3f244SSam Leffler 424fd90e2edSJung-uk Kim callout_init(&sc->sc_rngto, 1); 425b7e3f244SSam Leffler callout_reset(&sc->sc_rngto, hz*safe_rnginterval, safe_rng, sc); 426b7e3f244SSam Leffler } 427b7e3f244SSam Leffler #endif /* SAFE_NO_RNG */ 428b7e3f244SSam Leffler #ifdef SAFE_DEBUG 429b7e3f244SSam Leffler safec = sc; /* for use by hw.safe.dump */ 430b7e3f244SSam Leffler #endif 431b7e3f244SSam Leffler return (0); 432b7e3f244SSam Leffler bad4: 433b7e3f244SSam Leffler crypto_unregister_all(sc->sc_cid); 434b7e3f244SSam Leffler bad3: 435b7e3f244SSam Leffler bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih); 436b7e3f244SSam Leffler bad2: 437b7e3f244SSam Leffler bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq); 438b7e3f244SSam Leffler bad1: 439b7e3f244SSam Leffler bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, sc->sc_sr); 440b7e3f244SSam Leffler bad: 441b7e3f244SSam Leffler return (ENXIO); 442b7e3f244SSam Leffler } 443b7e3f244SSam Leffler 444b7e3f244SSam Leffler /* 445b7e3f244SSam Leffler * Detach a device that successfully probed. 446b7e3f244SSam Leffler */ 447b7e3f244SSam Leffler static int 448b7e3f244SSam Leffler safe_detach(device_t dev) 449b7e3f244SSam Leffler { 450b7e3f244SSam Leffler struct safe_softc *sc = device_get_softc(dev); 451b7e3f244SSam Leffler 452b7e3f244SSam Leffler /* XXX wait/abort active ops */ 453b7e3f244SSam Leffler 454b7e3f244SSam Leffler WRITE_REG(sc, SAFE_HI_MASK, 0); /* disable interrupts */ 455b7e3f244SSam Leffler 456b7e3f244SSam Leffler callout_stop(&sc->sc_rngto); 457b7e3f244SSam Leffler 458b7e3f244SSam Leffler crypto_unregister_all(sc->sc_cid); 459b7e3f244SSam Leffler 460b7e3f244SSam Leffler #ifdef SAFE_RNDTEST 461b7e3f244SSam Leffler if (sc->sc_rndtest) 462b7e3f244SSam Leffler rndtest_detach(sc->sc_rndtest); 463b7e3f244SSam Leffler #endif 464b7e3f244SSam Leffler 465b7e3f244SSam Leffler safe_cleanchip(sc); 466b7e3f244SSam Leffler safe_dma_free(sc, &sc->sc_dpalloc); 467b7e3f244SSam Leffler safe_dma_free(sc, &sc->sc_spalloc); 468b7e3f244SSam Leffler mtx_destroy(&sc->sc_ringmtx); 469b7e3f244SSam Leffler safe_dma_free(sc, &sc->sc_ringalloc); 470b7e3f244SSam Leffler 471b7e3f244SSam Leffler bus_generic_detach(dev); 472b7e3f244SSam Leffler bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih); 473b7e3f244SSam Leffler bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq); 474b7e3f244SSam Leffler 475b7e3f244SSam Leffler bus_dma_tag_destroy(sc->sc_srcdmat); 476b7e3f244SSam Leffler bus_dma_tag_destroy(sc->sc_dstdmat); 477b7e3f244SSam Leffler bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, sc->sc_sr); 478b7e3f244SSam Leffler 479b7e3f244SSam Leffler return (0); 480b7e3f244SSam Leffler } 481b7e3f244SSam Leffler 482b7e3f244SSam Leffler /* 483b7e3f244SSam Leffler * Stop all chip i/o so that the kernel's probe routines don't 484b7e3f244SSam Leffler * get confused by errant DMAs when rebooting. 485b7e3f244SSam Leffler */ 486a6340ec8SWarner Losh static int 487b7e3f244SSam Leffler safe_shutdown(device_t dev) 488b7e3f244SSam Leffler { 489b7e3f244SSam Leffler #ifdef notyet 490b7e3f244SSam Leffler safe_stop(device_get_softc(dev)); 491b7e3f244SSam Leffler #endif 492a6340ec8SWarner Losh return (0); 493b7e3f244SSam Leffler } 494b7e3f244SSam Leffler 495b7e3f244SSam Leffler /* 496b7e3f244SSam Leffler * Device suspend routine. 497b7e3f244SSam Leffler */ 498b7e3f244SSam Leffler static int 499b7e3f244SSam Leffler safe_suspend(device_t dev) 500b7e3f244SSam Leffler { 501b7e3f244SSam Leffler struct safe_softc *sc = device_get_softc(dev); 502b7e3f244SSam Leffler 503b7e3f244SSam Leffler #ifdef notyet 504b7e3f244SSam Leffler /* XXX stop the device and save PCI settings */ 505b7e3f244SSam Leffler #endif 506b7e3f244SSam Leffler sc->sc_suspended = 1; 507b7e3f244SSam Leffler 508b7e3f244SSam Leffler return (0); 509b7e3f244SSam Leffler } 510b7e3f244SSam Leffler 511b7e3f244SSam Leffler static int 512b7e3f244SSam Leffler safe_resume(device_t dev) 513b7e3f244SSam Leffler { 514b7e3f244SSam Leffler struct safe_softc *sc = device_get_softc(dev); 515b7e3f244SSam Leffler 516b7e3f244SSam Leffler #ifdef notyet 517b7e3f244SSam Leffler /* XXX retore PCI settings and start the device */ 518b7e3f244SSam Leffler #endif 519b7e3f244SSam Leffler sc->sc_suspended = 0; 520b7e3f244SSam Leffler return (0); 521b7e3f244SSam Leffler } 522b7e3f244SSam Leffler 523b7e3f244SSam Leffler /* 524b7e3f244SSam Leffler * SafeXcel Interrupt routine 525b7e3f244SSam Leffler */ 526b7e3f244SSam Leffler static void 527b7e3f244SSam Leffler safe_intr(void *arg) 528b7e3f244SSam Leffler { 529b7e3f244SSam Leffler struct safe_softc *sc = arg; 530b7e3f244SSam Leffler volatile u_int32_t stat; 531b7e3f244SSam Leffler 532b7e3f244SSam Leffler stat = READ_REG(sc, SAFE_HM_STAT); 533b7e3f244SSam Leffler if (stat == 0) /* shared irq, not for us */ 534b7e3f244SSam Leffler return; 535b7e3f244SSam Leffler 536b7e3f244SSam Leffler WRITE_REG(sc, SAFE_HI_CLR, stat); /* IACK */ 537b7e3f244SSam Leffler 538b7e3f244SSam Leffler if ((stat & SAFE_INT_PE_DDONE)) { 539b7e3f244SSam Leffler /* 540b7e3f244SSam Leffler * Descriptor(s) done; scan the ring and 541b7e3f244SSam Leffler * process completed operations. 542b7e3f244SSam Leffler */ 543b7e3f244SSam Leffler mtx_lock(&sc->sc_ringmtx); 544b7e3f244SSam Leffler while (sc->sc_back != sc->sc_front) { 545b7e3f244SSam Leffler struct safe_ringentry *re = sc->sc_back; 546b7e3f244SSam Leffler #ifdef SAFE_DEBUG 547b7e3f244SSam Leffler if (safe_debug) { 548b7e3f244SSam Leffler safe_dump_ringstate(sc, __func__); 549b7e3f244SSam Leffler safe_dump_request(sc, __func__, re); 550b7e3f244SSam Leffler } 551b7e3f244SSam Leffler #endif 552b7e3f244SSam Leffler /* 553b7e3f244SSam Leffler * safe_process marks ring entries that were allocated 554b7e3f244SSam Leffler * but not used with a csr of zero. This insures the 555b7e3f244SSam Leffler * ring front pointer never needs to be set backwards 556b7e3f244SSam Leffler * in the event that an entry is allocated but not used 557b7e3f244SSam Leffler * because of a setup error. 558b7e3f244SSam Leffler */ 559b7e3f244SSam Leffler if (re->re_desc.d_csr != 0) { 560b7e3f244SSam Leffler if (!SAFE_PE_CSR_IS_DONE(re->re_desc.d_csr)) 561b7e3f244SSam Leffler break; 562b7e3f244SSam Leffler if (!SAFE_PE_LEN_IS_DONE(re->re_desc.d_len)) 563b7e3f244SSam Leffler break; 564b7e3f244SSam Leffler sc->sc_nqchip--; 565b7e3f244SSam Leffler safe_callback(sc, re); 566b7e3f244SSam Leffler } 567b7e3f244SSam Leffler if (++(sc->sc_back) == sc->sc_ringtop) 568b7e3f244SSam Leffler sc->sc_back = sc->sc_ring; 569b7e3f244SSam Leffler } 570b7e3f244SSam Leffler mtx_unlock(&sc->sc_ringmtx); 571b7e3f244SSam Leffler } 572b7e3f244SSam Leffler 573b7e3f244SSam Leffler /* 574b7e3f244SSam Leffler * Check to see if we got any DMA Error 575b7e3f244SSam Leffler */ 576b7e3f244SSam Leffler if (stat & SAFE_INT_PE_ERROR) { 577b7e3f244SSam Leffler DPRINTF(("dmaerr dmastat %08x\n", 578b7e3f244SSam Leffler READ_REG(sc, SAFE_PE_DMASTAT))); 579b7e3f244SSam Leffler safestats.st_dmaerr++; 580b7e3f244SSam Leffler safe_totalreset(sc); 581b7e3f244SSam Leffler #if 0 582b7e3f244SSam Leffler safe_feed(sc); 583b7e3f244SSam Leffler #endif 584b7e3f244SSam Leffler } 585b7e3f244SSam Leffler 586b7e3f244SSam Leffler if (sc->sc_needwakeup) { /* XXX check high watermark */ 587b7e3f244SSam Leffler int wakeup = sc->sc_needwakeup & (CRYPTO_SYMQ|CRYPTO_ASYMQ); 588b7e3f244SSam Leffler DPRINTF(("%s: wakeup crypto %x\n", __func__, 589b7e3f244SSam Leffler sc->sc_needwakeup)); 590b7e3f244SSam Leffler sc->sc_needwakeup &= ~wakeup; 591b7e3f244SSam Leffler crypto_unblock(sc->sc_cid, wakeup); 592b7e3f244SSam Leffler } 593b7e3f244SSam Leffler } 594b7e3f244SSam Leffler 595b7e3f244SSam Leffler /* 596b7e3f244SSam Leffler * safe_feed() - post a request to chip 597b7e3f244SSam Leffler */ 598b7e3f244SSam Leffler static void 599b7e3f244SSam Leffler safe_feed(struct safe_softc *sc, struct safe_ringentry *re) 600b7e3f244SSam Leffler { 601b7e3f244SSam Leffler bus_dmamap_sync(sc->sc_srcdmat, re->re_src_map, BUS_DMASYNC_PREWRITE); 602b7e3f244SSam Leffler if (re->re_dst_map != NULL) 603b7e3f244SSam Leffler bus_dmamap_sync(sc->sc_dstdmat, re->re_dst_map, 604b7e3f244SSam Leffler BUS_DMASYNC_PREREAD); 605b7e3f244SSam Leffler /* XXX have no smaller granularity */ 606b7e3f244SSam Leffler safe_dma_sync(&sc->sc_ringalloc, 607b7e3f244SSam Leffler BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 608b7e3f244SSam Leffler safe_dma_sync(&sc->sc_spalloc, BUS_DMASYNC_PREWRITE); 609b7e3f244SSam Leffler safe_dma_sync(&sc->sc_dpalloc, BUS_DMASYNC_PREWRITE); 610b7e3f244SSam Leffler 611b7e3f244SSam Leffler #ifdef SAFE_DEBUG 612b7e3f244SSam Leffler if (safe_debug) { 613b7e3f244SSam Leffler safe_dump_ringstate(sc, __func__); 614b7e3f244SSam Leffler safe_dump_request(sc, __func__, re); 615b7e3f244SSam Leffler } 616b7e3f244SSam Leffler #endif 617b7e3f244SSam Leffler sc->sc_nqchip++; 618b7e3f244SSam Leffler if (sc->sc_nqchip > safestats.st_maxqchip) 619b7e3f244SSam Leffler safestats.st_maxqchip = sc->sc_nqchip; 620b7e3f244SSam Leffler /* poke h/w to check descriptor ring, any value can be written */ 621b7e3f244SSam Leffler WRITE_REG(sc, SAFE_HI_RD_DESCR, 0); 622b7e3f244SSam Leffler } 623b7e3f244SSam Leffler 6249a2f6061SPawel Jakub Dawidek #define N(a) (sizeof(a) / sizeof (a[0])) 6259a2f6061SPawel Jakub Dawidek static void 626c0341432SJohn Baldwin safe_setup_enckey(struct safe_session *ses, const void *key) 6279a2f6061SPawel Jakub Dawidek { 6289a2f6061SPawel Jakub Dawidek int i; 6299a2f6061SPawel Jakub Dawidek 630c0341432SJohn Baldwin bcopy(key, ses->ses_key, ses->ses_klen); 6319a2f6061SPawel Jakub Dawidek 6329a2f6061SPawel Jakub Dawidek /* PE is little-endian, insure proper byte order */ 6339a2f6061SPawel Jakub Dawidek for (i = 0; i < N(ses->ses_key); i++) 6349a2f6061SPawel Jakub Dawidek ses->ses_key[i] = htole32(ses->ses_key[i]); 6359a2f6061SPawel Jakub Dawidek } 6369a2f6061SPawel Jakub Dawidek 6379a2f6061SPawel Jakub Dawidek static void 638c0341432SJohn Baldwin safe_setup_mackey(struct safe_session *ses, int algo, const uint8_t *key, 639c0341432SJohn Baldwin int klen) 6409a2f6061SPawel Jakub Dawidek { 6419a2f6061SPawel Jakub Dawidek SHA1_CTX sha1ctx; 6429a2f6061SPawel Jakub Dawidek int i; 6439a2f6061SPawel Jakub Dawidek 644c0341432SJohn Baldwin hmac_init_ipad(&auth_hash_hmac_sha1, key, klen, &sha1ctx); 645c0341432SJohn Baldwin bcopy(sha1ctx.h.b32, ses->ses_hminner, sizeof(sha1ctx.h.b32)); 646c0341432SJohn Baldwin 647c0341432SJohn Baldwin hmac_init_opad(&auth_hash_hmac_sha1, key, klen, &sha1ctx); 648c0341432SJohn Baldwin bcopy(sha1ctx.h.b32, ses->ses_hmouter, sizeof(sha1ctx.h.b32)); 649c0341432SJohn Baldwin 650c0341432SJohn Baldwin explicit_bzero(&sha1ctx, sizeof(sha1ctx)); 6519a2f6061SPawel Jakub Dawidek 6529a2f6061SPawel Jakub Dawidek /* PE is little-endian, insure proper byte order */ 6539a2f6061SPawel Jakub Dawidek for (i = 0; i < N(ses->ses_hminner); i++) { 6549a2f6061SPawel Jakub Dawidek ses->ses_hminner[i] = htole32(ses->ses_hminner[i]); 6559a2f6061SPawel Jakub Dawidek ses->ses_hmouter[i] = htole32(ses->ses_hmouter[i]); 6569a2f6061SPawel Jakub Dawidek } 6579a2f6061SPawel Jakub Dawidek } 6589a2f6061SPawel Jakub Dawidek #undef N 6599a2f6061SPawel Jakub Dawidek 660c0341432SJohn Baldwin static bool 661c0341432SJohn Baldwin safe_auth_supported(struct safe_softc *sc, 662c0341432SJohn Baldwin const struct crypto_session_params *csp) 663b7e3f244SSam Leffler { 664b7e3f244SSam Leffler 665c0341432SJohn Baldwin switch (csp->csp_auth_alg) { 666c0341432SJohn Baldwin case CRYPTO_SHA1_HMAC: 667c0341432SJohn Baldwin if ((sc->sc_devinfo & SAFE_DEVINFO_SHA1) == 0) 668c0341432SJohn Baldwin return (false); 669c0341432SJohn Baldwin break; 670c0341432SJohn Baldwin default: 671c0341432SJohn Baldwin return (false); 672c0341432SJohn Baldwin } 673c0341432SJohn Baldwin return (true); 674c0341432SJohn Baldwin } 675c0341432SJohn Baldwin 676c0341432SJohn Baldwin static bool 677c0341432SJohn Baldwin safe_cipher_supported(struct safe_softc *sc, 678c0341432SJohn Baldwin const struct crypto_session_params *csp) 679c0341432SJohn Baldwin { 680c0341432SJohn Baldwin 681c0341432SJohn Baldwin switch (csp->csp_cipher_alg) { 682b7e3f244SSam Leffler case CRYPTO_AES_CBC: 683c0341432SJohn Baldwin if ((sc->sc_devinfo & SAFE_DEVINFO_AES) == 0) 684c0341432SJohn Baldwin return (false); 685c0341432SJohn Baldwin if (csp->csp_ivlen != 16) 686c0341432SJohn Baldwin return (false); 687c0341432SJohn Baldwin if (csp->csp_cipher_klen != 16 && 688c0341432SJohn Baldwin csp->csp_cipher_klen != 24 && 689c0341432SJohn Baldwin csp->csp_cipher_klen != 32) 690c0341432SJohn Baldwin return (false); 691b7e3f244SSam Leffler break; 692b7e3f244SSam Leffler } 693c0341432SJohn Baldwin return (true); 694b7e3f244SSam Leffler } 695b7e3f244SSam Leffler 696c0341432SJohn Baldwin static int 697c0341432SJohn Baldwin safe_probesession(device_t dev, const struct crypto_session_params *csp) 698c0341432SJohn Baldwin { 699c0341432SJohn Baldwin struct safe_softc *sc = device_get_softc(dev); 700c0341432SJohn Baldwin 701c0341432SJohn Baldwin if (csp->csp_flags != 0) 702c0341432SJohn Baldwin return (EINVAL); 703c0341432SJohn Baldwin switch (csp->csp_mode) { 704c0341432SJohn Baldwin case CSP_MODE_DIGEST: 705c0341432SJohn Baldwin if (!safe_auth_supported(sc, csp)) 706c0341432SJohn Baldwin return (EINVAL); 707c0341432SJohn Baldwin break; 708c0341432SJohn Baldwin case CSP_MODE_CIPHER: 709c0341432SJohn Baldwin if (!safe_cipher_supported(sc, csp)) 710c0341432SJohn Baldwin return (EINVAL); 711c0341432SJohn Baldwin break; 712c0341432SJohn Baldwin case CSP_MODE_ETA: 713c0341432SJohn Baldwin if (!safe_auth_supported(sc, csp) || 714c0341432SJohn Baldwin !safe_cipher_supported(sc, csp)) 715c0341432SJohn Baldwin return (EINVAL); 716c0341432SJohn Baldwin break; 717c0341432SJohn Baldwin default: 718c0341432SJohn Baldwin return (EINVAL); 719c0341432SJohn Baldwin } 720c0341432SJohn Baldwin 721c0341432SJohn Baldwin return (CRYPTODEV_PROBE_HARDWARE); 722c0341432SJohn Baldwin } 723c0341432SJohn Baldwin 724c0341432SJohn Baldwin /* 725c0341432SJohn Baldwin * Allocate a new 'session'. 726c0341432SJohn Baldwin */ 727c0341432SJohn Baldwin static int 728c0341432SJohn Baldwin safe_newsession(device_t dev, crypto_session_t cses, 729c0341432SJohn Baldwin const struct crypto_session_params *csp) 730c0341432SJohn Baldwin { 731c0341432SJohn Baldwin struct safe_session *ses; 732c0341432SJohn Baldwin 7331b0909d5SConrad Meyer ses = crypto_get_driver_session(cses); 734c0341432SJohn Baldwin if (csp->csp_cipher_alg != 0) { 735c0341432SJohn Baldwin ses->ses_klen = csp->csp_cipher_klen; 736c0341432SJohn Baldwin if (csp->csp_cipher_key != NULL) 737c0341432SJohn Baldwin safe_setup_enckey(ses, csp->csp_cipher_key); 738b7e3f244SSam Leffler } 739b7e3f244SSam Leffler 740c0341432SJohn Baldwin if (csp->csp_auth_alg != 0) { 741c0341432SJohn Baldwin ses->ses_mlen = csp->csp_auth_mlen; 742af65c53aSPawel Jakub Dawidek if (ses->ses_mlen == 0) { 7431dc8d404SPawel Jakub Dawidek ses->ses_mlen = SHA1_HASH_LEN; 744af65c53aSPawel Jakub Dawidek } 745af65c53aSPawel Jakub Dawidek 746c0341432SJohn Baldwin if (csp->csp_auth_key != NULL) { 747c0341432SJohn Baldwin safe_setup_mackey(ses, csp->csp_auth_alg, 748c0341432SJohn Baldwin csp->csp_auth_key, csp->csp_auth_klen); 749b7e3f244SSam Leffler } 750b7e3f244SSam Leffler } 751b7e3f244SSam Leffler 752b7e3f244SSam Leffler return (0); 753b7e3f244SSam Leffler } 754b7e3f244SSam Leffler 755b7e3f244SSam Leffler static void 756c0341432SJohn Baldwin safe_op_cb(void *arg, bus_dma_segment_t *seg, int nsegs, int error) 757b7e3f244SSam Leffler { 758b7e3f244SSam Leffler struct safe_operand *op = arg; 759b7e3f244SSam Leffler 760c0341432SJohn Baldwin DPRINTF(("%s: nsegs %d error %d\n", __func__, 761c0341432SJohn Baldwin nsegs, error)); 762b7e3f244SSam Leffler if (error != 0) 763b7e3f244SSam Leffler return; 764b7e3f244SSam Leffler op->nsegs = nsegs; 765b7e3f244SSam Leffler bcopy(seg, op->segs, nsegs * sizeof (seg[0])); 766b7e3f244SSam Leffler } 767b7e3f244SSam Leffler 768b7e3f244SSam Leffler static int 7696810ad6fSSam Leffler safe_process(device_t dev, struct cryptop *crp, int hint) 770b7e3f244SSam Leffler { 7716810ad6fSSam Leffler struct safe_softc *sc = device_get_softc(dev); 772c0341432SJohn Baldwin const struct crypto_session_params *csp; 773b7e3f244SSam Leffler int err = 0, i, nicealign, uniform; 774c0341432SJohn Baldwin int bypass, oplen; 775b7e3f244SSam Leffler int16_t coffset; 776b7e3f244SSam Leffler struct safe_session *ses; 777b7e3f244SSam Leffler struct safe_ringentry *re; 778b7e3f244SSam Leffler struct safe_sarec *sa; 779b7e3f244SSam Leffler struct safe_pdesc *pd; 780b7e3f244SSam Leffler u_int32_t cmd0, cmd1, staterec; 781b7e3f244SSam Leffler 782b7e3f244SSam Leffler mtx_lock(&sc->sc_ringmtx); 783b7e3f244SSam Leffler if (sc->sc_front == sc->sc_back && sc->sc_nqchip != 0) { 784b7e3f244SSam Leffler safestats.st_ringfull++; 785b7e3f244SSam Leffler sc->sc_needwakeup |= CRYPTO_SYMQ; 786b7e3f244SSam Leffler mtx_unlock(&sc->sc_ringmtx); 787b7e3f244SSam Leffler return (ERESTART); 788b7e3f244SSam Leffler } 789b7e3f244SSam Leffler re = sc->sc_front; 790b7e3f244SSam Leffler 791b7e3f244SSam Leffler staterec = re->re_sa.sa_staterec; /* save */ 792b7e3f244SSam Leffler /* NB: zero everything but the PE descriptor */ 793b7e3f244SSam Leffler bzero(&re->re_sa, sizeof(struct safe_ringentry) - sizeof(re->re_desc)); 794b7e3f244SSam Leffler re->re_sa.sa_staterec = staterec; /* restore */ 795b7e3f244SSam Leffler 796b7e3f244SSam Leffler re->re_crp = crp; 797b7e3f244SSam Leffler 798b7e3f244SSam Leffler sa = &re->re_sa; 7991b0909d5SConrad Meyer ses = crypto_get_driver_session(crp->crp_session); 800c0341432SJohn Baldwin csp = crypto_get_params(crp->crp_session); 801b7e3f244SSam Leffler 802b7e3f244SSam Leffler cmd0 = SAFE_SA_CMD0_BASIC; /* basic group operation */ 803b7e3f244SSam Leffler cmd1 = 0; 804c0341432SJohn Baldwin switch (csp->csp_mode) { 805c0341432SJohn Baldwin case CSP_MODE_DIGEST: 806b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_OP_HASH; 807c0341432SJohn Baldwin break; 808c0341432SJohn Baldwin case CSP_MODE_CIPHER: 809b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_OP_CRYPT; 810c0341432SJohn Baldwin break; 811c0341432SJohn Baldwin case CSP_MODE_ETA: 812b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_OP_BOTH; 813c0341432SJohn Baldwin break; 814b7e3f244SSam Leffler } 815b7e3f244SSam Leffler 816c0341432SJohn Baldwin if (csp->csp_cipher_alg != 0) { 817c0341432SJohn Baldwin if (crp->crp_cipher_key != NULL) 818c0341432SJohn Baldwin safe_setup_enckey(ses, crp->crp_cipher_key); 8199a2f6061SPawel Jakub Dawidek 820c0341432SJohn Baldwin switch (csp->csp_cipher_alg) { 821c0341432SJohn Baldwin case CRYPTO_AES_CBC: 822b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_AES; 823b7e3f244SSam Leffler cmd1 |= SAFE_SA_CMD1_CBC; 824c0341432SJohn Baldwin if (ses->ses_klen * 8 == 128) 825b7e3f244SSam Leffler cmd1 |= SAFE_SA_CMD1_AES128; 826c0341432SJohn Baldwin else if (ses->ses_klen * 8 == 192) 827b7e3f244SSam Leffler cmd1 |= SAFE_SA_CMD1_AES192; 828b7e3f244SSam Leffler else 829b7e3f244SSam Leffler cmd1 |= SAFE_SA_CMD1_AES256; 830b7e3f244SSam Leffler } 831b7e3f244SSam Leffler 832b7e3f244SSam Leffler /* 833b7e3f244SSam Leffler * Setup encrypt/decrypt state. When using basic ops 834b7e3f244SSam Leffler * we can't use an inline IV because hash/crypt offset 835b7e3f244SSam Leffler * must be from the end of the IV to the start of the 836b7e3f244SSam Leffler * crypt data and this leaves out the preceding header 837b7e3f244SSam Leffler * from the hash calculation. Instead we place the IV 838b7e3f244SSam Leffler * in the state record and set the hash/crypt offset to 839b7e3f244SSam Leffler * copy both the header+IV. 840b7e3f244SSam Leffler */ 84129fe41ddSJohn Baldwin crypto_read_iv(crp, re->re_sastate.sa_saved_iv); 842c0341432SJohn Baldwin cmd0 |= SAFE_SA_CMD0_IVLD_STATE; 843c0341432SJohn Baldwin 844c0341432SJohn Baldwin if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) { 845b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_OUTBOUND; 846b7e3f244SSam Leffler 847c0341432SJohn Baldwin /* 848c0341432SJohn Baldwin * XXX: I suspect we don't need this since we 849c0341432SJohn Baldwin * don't save the returned IV. 850c0341432SJohn Baldwin */ 851c0341432SJohn Baldwin cmd0 |= SAFE_SA_CMD0_SAVEIV; 852b7e3f244SSam Leffler } else { 853b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_INBOUND; 854b7e3f244SSam Leffler } 855b7e3f244SSam Leffler /* 856b7e3f244SSam Leffler * For basic encryption use the zero pad algorithm. 857b7e3f244SSam Leffler * This pads results to an 8-byte boundary and 858b7e3f244SSam Leffler * suppresses padding verification for inbound (i.e. 859b7e3f244SSam Leffler * decrypt) operations. 860b7e3f244SSam Leffler * 861b7e3f244SSam Leffler * NB: Not sure if the 8-byte pad boundary is a problem. 862b7e3f244SSam Leffler */ 863b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_PAD_ZERO; 864b7e3f244SSam Leffler 865b7e3f244SSam Leffler /* XXX assert key bufs have the same size */ 866b7e3f244SSam Leffler bcopy(ses->ses_key, sa->sa_key, sizeof(sa->sa_key)); 867b7e3f244SSam Leffler } 868b7e3f244SSam Leffler 869c0341432SJohn Baldwin if (csp->csp_auth_alg != 0) { 870c0341432SJohn Baldwin if (crp->crp_auth_key != NULL) { 871c0341432SJohn Baldwin safe_setup_mackey(ses, csp->csp_auth_alg, 872c0341432SJohn Baldwin crp->crp_auth_key, csp->csp_auth_klen); 8739a2f6061SPawel Jakub Dawidek } 8749a2f6061SPawel Jakub Dawidek 875c0341432SJohn Baldwin switch (csp->csp_auth_alg) { 876c0341432SJohn Baldwin case CRYPTO_SHA1_HMAC: 877b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_SHA1; 878b7e3f244SSam Leffler cmd1 |= SAFE_SA_CMD1_HMAC; /* NB: enable HMAC */ 879c0341432SJohn Baldwin break; 880b7e3f244SSam Leffler } 881c0341432SJohn Baldwin 882b7e3f244SSam Leffler /* 883b7e3f244SSam Leffler * Digest data is loaded from the SA and the hash 884b7e3f244SSam Leffler * result is saved to the state block where we 885b7e3f244SSam Leffler * retrieve it for return to the caller. 886b7e3f244SSam Leffler */ 887b7e3f244SSam Leffler /* XXX assert digest bufs have the same size */ 888b7e3f244SSam Leffler bcopy(ses->ses_hminner, sa->sa_indigest, 889b7e3f244SSam Leffler sizeof(sa->sa_indigest)); 890b7e3f244SSam Leffler bcopy(ses->ses_hmouter, sa->sa_outdigest, 891b7e3f244SSam Leffler sizeof(sa->sa_outdigest)); 892b7e3f244SSam Leffler 893b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_HSLD_SA | SAFE_SA_CMD0_SAVEHASH; 894b7e3f244SSam Leffler re->re_flags |= SAFE_QFLAGS_COPYOUTICV; 895b7e3f244SSam Leffler } 896b7e3f244SSam Leffler 897c0341432SJohn Baldwin if (csp->csp_mode == CSP_MODE_ETA) { 898b7e3f244SSam Leffler /* 899c0341432SJohn Baldwin * The driver only supports ETA requests where there 900c0341432SJohn Baldwin * is no gap between the AAD and payload. 901b7e3f244SSam Leffler */ 902c0341432SJohn Baldwin if (crp->crp_aad_length != 0 && 903c0341432SJohn Baldwin crp->crp_aad_start + crp->crp_aad_length != 904c0341432SJohn Baldwin crp->crp_payload_start) { 905b7e3f244SSam Leffler safestats.st_lenmismatch++; 906b7e3f244SSam Leffler err = EINVAL; 907b7e3f244SSam Leffler goto errout; 908b7e3f244SSam Leffler } 909c0341432SJohn Baldwin if (crp->crp_aad_length != 0) 910c0341432SJohn Baldwin bypass = crp->crp_aad_start; 911c0341432SJohn Baldwin else 912c0341432SJohn Baldwin bypass = crp->crp_payload_start; 913c0341432SJohn Baldwin coffset = crp->crp_aad_length; 914c0341432SJohn Baldwin oplen = crp->crp_payload_start + crp->crp_payload_length; 915b7e3f244SSam Leffler #ifdef SAFE_DEBUG 916b7e3f244SSam Leffler if (safe_debug) { 917c0341432SJohn Baldwin printf("AAD: skip %d, len %d, digest %d\n", 918c0341432SJohn Baldwin crp->crp_aad_start, crp->crp_aad_length, 919c0341432SJohn Baldwin crp->crp_digest_start); 920c0341432SJohn Baldwin printf("payload: skip %d, len %d, IV %d\n", 921c0341432SJohn Baldwin crp->crp_payload_start, crp->crp_payload_length, 922c0341432SJohn Baldwin crp->crp_iv_start); 923b7e3f244SSam Leffler printf("bypass %d coffset %d oplen %d\n", 924b7e3f244SSam Leffler bypass, coffset, oplen); 925b7e3f244SSam Leffler } 926b7e3f244SSam Leffler #endif 927b7e3f244SSam Leffler if (coffset & 3) { /* offset must be 32-bit aligned */ 928b7e3f244SSam Leffler DPRINTF(("%s: coffset %u misaligned\n", 929b7e3f244SSam Leffler __func__, coffset)); 930b7e3f244SSam Leffler safestats.st_coffmisaligned++; 931b7e3f244SSam Leffler err = EINVAL; 932b7e3f244SSam Leffler goto errout; 933b7e3f244SSam Leffler } 934b7e3f244SSam Leffler coffset >>= 2; 935b7e3f244SSam Leffler if (coffset > 255) { /* offset must be <256 dwords */ 936b7e3f244SSam Leffler DPRINTF(("%s: coffset %u too big\n", 937b7e3f244SSam Leffler __func__, coffset)); 938b7e3f244SSam Leffler safestats.st_cofftoobig++; 939b7e3f244SSam Leffler err = EINVAL; 940b7e3f244SSam Leffler goto errout; 941b7e3f244SSam Leffler } 942b7e3f244SSam Leffler /* 943b7e3f244SSam Leffler * Tell the hardware to copy the header to the output. 944b7e3f244SSam Leffler * The header is defined as the data from the end of 945b7e3f244SSam Leffler * the bypass to the start of data to be encrypted. 946b7e3f244SSam Leffler * Typically this is the inline IV. Note that you need 947b7e3f244SSam Leffler * to do this even if src+dst are the same; it appears 948b7e3f244SSam Leffler * that w/o this bit the crypted data is written 949b7e3f244SSam Leffler * immediately after the bypass data. 950b7e3f244SSam Leffler */ 951b7e3f244SSam Leffler cmd1 |= SAFE_SA_CMD1_HDRCOPY; 952b7e3f244SSam Leffler /* 953b7e3f244SSam Leffler * Disable IP header mutable bit handling. This is 954b7e3f244SSam Leffler * needed to get correct HMAC calculations. 955b7e3f244SSam Leffler */ 956b7e3f244SSam Leffler cmd1 |= SAFE_SA_CMD1_MUTABLE; 957b7e3f244SSam Leffler } else { 958c0341432SJohn Baldwin bypass = crp->crp_payload_start; 959c0341432SJohn Baldwin oplen = bypass + crp->crp_payload_length; 960b7e3f244SSam Leffler coffset = 0; 961b7e3f244SSam Leffler } 962b7e3f244SSam Leffler /* XXX verify multiple of 4 when using s/g */ 963b7e3f244SSam Leffler if (bypass > 96) { /* bypass offset must be <= 96 bytes */ 964b7e3f244SSam Leffler DPRINTF(("%s: bypass %u too big\n", __func__, bypass)); 965b7e3f244SSam Leffler safestats.st_bypasstoobig++; 966b7e3f244SSam Leffler err = EINVAL; 967b7e3f244SSam Leffler goto errout; 968b7e3f244SSam Leffler } 969b7e3f244SSam Leffler 970b7e3f244SSam Leffler if (bus_dmamap_create(sc->sc_srcdmat, BUS_DMA_NOWAIT, &re->re_src_map)) { 971b7e3f244SSam Leffler safestats.st_nomap++; 972b7e3f244SSam Leffler err = ENOMEM; 973b7e3f244SSam Leffler goto errout; 974b7e3f244SSam Leffler } 975c0341432SJohn Baldwin if (bus_dmamap_load_crp(sc->sc_srcdmat, re->re_src_map, crp, safe_op_cb, 976b7e3f244SSam Leffler &re->re_src, BUS_DMA_NOWAIT) != 0) { 977b7e3f244SSam Leffler bus_dmamap_destroy(sc->sc_srcdmat, re->re_src_map); 978b7e3f244SSam Leffler re->re_src_map = NULL; 979b7e3f244SSam Leffler safestats.st_noload++; 980b7e3f244SSam Leffler err = ENOMEM; 981b7e3f244SSam Leffler goto errout; 982b7e3f244SSam Leffler } 983*9c0e3d3aSJohn Baldwin re->re_src_mapsize = crypto_buffer_len(&crp->crp_buf); 984b7e3f244SSam Leffler nicealign = safe_dmamap_aligned(&re->re_src); 985b7e3f244SSam Leffler uniform = safe_dmamap_uniform(&re->re_src); 986b7e3f244SSam Leffler 987b7e3f244SSam Leffler DPRINTF(("src nicealign %u uniform %u nsegs %u\n", 988b7e3f244SSam Leffler nicealign, uniform, re->re_src.nsegs)); 989b7e3f244SSam Leffler if (re->re_src.nsegs > 1) { 990b7e3f244SSam Leffler re->re_desc.d_src = sc->sc_spalloc.dma_paddr + 991b7e3f244SSam Leffler ((caddr_t) sc->sc_spfree - (caddr_t) sc->sc_spring); 992b7e3f244SSam Leffler for (i = 0; i < re->re_src_nsegs; i++) { 993b7e3f244SSam Leffler /* NB: no need to check if there's space */ 994b7e3f244SSam Leffler pd = sc->sc_spfree; 995b7e3f244SSam Leffler if (++(sc->sc_spfree) == sc->sc_springtop) 996b7e3f244SSam Leffler sc->sc_spfree = sc->sc_spring; 997b7e3f244SSam Leffler 998b7e3f244SSam Leffler KASSERT((pd->pd_flags&3) == 0 || 999b7e3f244SSam Leffler (pd->pd_flags&3) == SAFE_PD_DONE, 1000b7e3f244SSam Leffler ("bogus source particle descriptor; flags %x", 1001b7e3f244SSam Leffler pd->pd_flags)); 1002b7e3f244SSam Leffler pd->pd_addr = re->re_src_segs[i].ds_addr; 1003b7e3f244SSam Leffler pd->pd_size = re->re_src_segs[i].ds_len; 1004b7e3f244SSam Leffler pd->pd_flags = SAFE_PD_READY; 1005b7e3f244SSam Leffler } 1006b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_IGATHER; 1007b7e3f244SSam Leffler } else { 1008b7e3f244SSam Leffler /* 1009b7e3f244SSam Leffler * No need for gather, reference the operand directly. 1010b7e3f244SSam Leffler */ 1011b7e3f244SSam Leffler re->re_desc.d_src = re->re_src_segs[0].ds_addr; 1012b7e3f244SSam Leffler } 1013b7e3f244SSam Leffler 1014c0341432SJohn Baldwin if (csp->csp_mode == CSP_MODE_DIGEST) { 1015b7e3f244SSam Leffler /* 1016b7e3f244SSam Leffler * Hash op; no destination needed. 1017b7e3f244SSam Leffler */ 1018b7e3f244SSam Leffler } else { 1019b7e3f244SSam Leffler if (nicealign && uniform == 1) { 1020b7e3f244SSam Leffler /* 1021b7e3f244SSam Leffler * Source layout is suitable for direct 1022b7e3f244SSam Leffler * sharing of the DMA map and segment list. 1023b7e3f244SSam Leffler */ 1024b7e3f244SSam Leffler re->re_dst = re->re_src; 1025b7e3f244SSam Leffler } else if (nicealign && uniform == 2) { 1026b7e3f244SSam Leffler /* 1027b7e3f244SSam Leffler * The source is properly aligned but requires a 1028b7e3f244SSam Leffler * different particle list to handle DMA of the 1029b7e3f244SSam Leffler * result. Create a new map and do the load to 1030b7e3f244SSam Leffler * create the segment list. The particle 1031b7e3f244SSam Leffler * descriptor setup code below will handle the 1032b7e3f244SSam Leffler * rest. 1033b7e3f244SSam Leffler */ 1034c0341432SJohn Baldwin if (bus_dmamap_create(sc->sc_dstdmat, BUS_DMA_NOWAIT, 1035c0341432SJohn Baldwin &re->re_dst_map)) { 1036b7e3f244SSam Leffler safestats.st_nomap++; 1037b7e3f244SSam Leffler err = ENOMEM; 1038b7e3f244SSam Leffler goto errout; 1039b7e3f244SSam Leffler } 1040c0341432SJohn Baldwin if (bus_dmamap_load_crp(sc->sc_dstdmat, re->re_dst_map, 1041c0341432SJohn Baldwin crp, safe_op_cb, &re->re_dst, BUS_DMA_NOWAIT) != 1042c0341432SJohn Baldwin 0) { 1043b7e3f244SSam Leffler bus_dmamap_destroy(sc->sc_dstdmat, 1044b7e3f244SSam Leffler re->re_dst_map); 1045b7e3f244SSam Leffler re->re_dst_map = NULL; 1046b7e3f244SSam Leffler safestats.st_noload++; 1047b7e3f244SSam Leffler err = ENOMEM; 1048b7e3f244SSam Leffler goto errout; 1049b7e3f244SSam Leffler } 1050*9c0e3d3aSJohn Baldwin } else if (crp->crp_buf.cb_type == CRYPTO_BUF_MBUF) { 1051b7e3f244SSam Leffler int totlen, len; 1052b7e3f244SSam Leffler struct mbuf *m, *top, **mp; 1053b7e3f244SSam Leffler 1054b7e3f244SSam Leffler /* 1055b7e3f244SSam Leffler * DMA constraints require that we allocate a 1056b7e3f244SSam Leffler * new mbuf chain for the destination. We 1057b7e3f244SSam Leffler * allocate an entire new set of mbufs of 1058b7e3f244SSam Leffler * optimal/required size and then tell the 1059b7e3f244SSam Leffler * hardware to copy any bits that are not 1060b7e3f244SSam Leffler * created as a byproduct of the operation. 1061b7e3f244SSam Leffler */ 1062b7e3f244SSam Leffler if (!nicealign) 1063b7e3f244SSam Leffler safestats.st_unaligned++; 1064b7e3f244SSam Leffler if (!uniform) 1065b7e3f244SSam Leffler safestats.st_notuniform++; 1066b7e3f244SSam Leffler totlen = re->re_src_mapsize; 1067*9c0e3d3aSJohn Baldwin if (crp->crp_buf.cb_mbuf->m_flags & M_PKTHDR) { 1068b7e3f244SSam Leffler len = MHLEN; 1069c6499eccSGleb Smirnoff MGETHDR(m, M_NOWAIT, MT_DATA); 1070*9c0e3d3aSJohn Baldwin if (m && !m_dup_pkthdr(m, crp->crp_buf.cb_mbuf, 1071c6499eccSGleb Smirnoff M_NOWAIT)) { 1072b7e3f244SSam Leffler m_free(m); 1073b7e3f244SSam Leffler m = NULL; 1074b7e3f244SSam Leffler } 1075b7e3f244SSam Leffler } else { 1076b7e3f244SSam Leffler len = MLEN; 1077c6499eccSGleb Smirnoff MGET(m, M_NOWAIT, MT_DATA); 1078b7e3f244SSam Leffler } 1079b7e3f244SSam Leffler if (m == NULL) { 1080b7e3f244SSam Leffler safestats.st_nombuf++; 1081b7e3f244SSam Leffler err = sc->sc_nqchip ? ERESTART : ENOMEM; 1082b7e3f244SSam Leffler goto errout; 1083b7e3f244SSam Leffler } 1084b7e3f244SSam Leffler if (totlen >= MINCLSIZE) { 10852a8c860fSRobert Watson if (!(MCLGET(m, M_NOWAIT))) { 1086b7e3f244SSam Leffler m_free(m); 1087b7e3f244SSam Leffler safestats.st_nomcl++; 1088b7e3f244SSam Leffler err = sc->sc_nqchip ? 1089b7e3f244SSam Leffler ERESTART : ENOMEM; 1090b7e3f244SSam Leffler goto errout; 1091b7e3f244SSam Leffler } 1092b7e3f244SSam Leffler len = MCLBYTES; 1093b7e3f244SSam Leffler } 1094b7e3f244SSam Leffler m->m_len = len; 1095b7e3f244SSam Leffler top = NULL; 1096b7e3f244SSam Leffler mp = ⊤ 1097b7e3f244SSam Leffler 1098b7e3f244SSam Leffler while (totlen > 0) { 1099b7e3f244SSam Leffler if (top) { 1100c6499eccSGleb Smirnoff MGET(m, M_NOWAIT, MT_DATA); 1101b7e3f244SSam Leffler if (m == NULL) { 1102b7e3f244SSam Leffler m_freem(top); 1103b7e3f244SSam Leffler safestats.st_nombuf++; 1104b7e3f244SSam Leffler err = sc->sc_nqchip ? 1105b7e3f244SSam Leffler ERESTART : ENOMEM; 1106b7e3f244SSam Leffler goto errout; 1107b7e3f244SSam Leffler } 1108b7e3f244SSam Leffler len = MLEN; 1109b7e3f244SSam Leffler } 1110b7e3f244SSam Leffler if (top && totlen >= MINCLSIZE) { 11112a8c860fSRobert Watson if (!(MCLGET(m, M_NOWAIT))) { 1112b7e3f244SSam Leffler *mp = m; 1113b7e3f244SSam Leffler m_freem(top); 1114b7e3f244SSam Leffler safestats.st_nomcl++; 1115b7e3f244SSam Leffler err = sc->sc_nqchip ? 1116b7e3f244SSam Leffler ERESTART : ENOMEM; 1117b7e3f244SSam Leffler goto errout; 1118b7e3f244SSam Leffler } 1119b7e3f244SSam Leffler len = MCLBYTES; 1120b7e3f244SSam Leffler } 1121b7e3f244SSam Leffler m->m_len = len = min(totlen, len); 1122b7e3f244SSam Leffler totlen -= len; 1123b7e3f244SSam Leffler *mp = m; 1124b7e3f244SSam Leffler mp = &m->m_next; 1125b7e3f244SSam Leffler } 1126b7e3f244SSam Leffler re->re_dst_m = top; 1127b7e3f244SSam Leffler if (bus_dmamap_create(sc->sc_dstdmat, 1128b7e3f244SSam Leffler BUS_DMA_NOWAIT, &re->re_dst_map) != 0) { 1129b7e3f244SSam Leffler safestats.st_nomap++; 1130b7e3f244SSam Leffler err = ENOMEM; 1131b7e3f244SSam Leffler goto errout; 1132b7e3f244SSam Leffler } 1133c0341432SJohn Baldwin if (bus_dmamap_load_mbuf_sg(sc->sc_dstdmat, 1134c0341432SJohn Baldwin re->re_dst_map, top, re->re_dst_segs, 1135c0341432SJohn Baldwin &re->re_dst_nsegs, 0) != 0) { 1136b7e3f244SSam Leffler bus_dmamap_destroy(sc->sc_dstdmat, 1137b7e3f244SSam Leffler re->re_dst_map); 1138b7e3f244SSam Leffler re->re_dst_map = NULL; 1139b7e3f244SSam Leffler safestats.st_noload++; 1140b7e3f244SSam Leffler err = ENOMEM; 1141b7e3f244SSam Leffler goto errout; 1142b7e3f244SSam Leffler } 1143c0341432SJohn Baldwin re->re_dst_mapsize = re->re_src_mapsize; 1144b7e3f244SSam Leffler if (re->re_src.mapsize > oplen) { 1145b7e3f244SSam Leffler /* 1146b7e3f244SSam Leffler * There's data following what the 1147b7e3f244SSam Leffler * hardware will copy for us. If this 1148b7e3f244SSam Leffler * isn't just the ICV (that's going to 1149b7e3f244SSam Leffler * be written on completion), copy it 1150b7e3f244SSam Leffler * to the new mbufs 1151b7e3f244SSam Leffler */ 1152c0341432SJohn Baldwin if (!(csp->csp_mode == CSP_MODE_ETA && 1153c0341432SJohn Baldwin (re->re_src.mapsize-oplen) == ses->ses_mlen && 1154c0341432SJohn Baldwin crp->crp_digest_start == oplen)) 1155*9c0e3d3aSJohn Baldwin safe_mcopy(crp->crp_buf.cb_mbuf, 1156*9c0e3d3aSJohn Baldwin re->re_dst_m, oplen); 1157b7e3f244SSam Leffler else 1158b7e3f244SSam Leffler safestats.st_noicvcopy++; 1159b7e3f244SSam Leffler } 1160b7e3f244SSam Leffler } else { 1161c0341432SJohn Baldwin if (!nicealign) { 1162c0341432SJohn Baldwin safestats.st_iovmisaligned++; 1163b7e3f244SSam Leffler err = EINVAL; 1164b7e3f244SSam Leffler goto errout; 1165c0341432SJohn Baldwin } else { 1166c0341432SJohn Baldwin /* 1167c0341432SJohn Baldwin * There's no way to handle the DMA 1168c0341432SJohn Baldwin * requirements with this uio. We 1169c0341432SJohn Baldwin * could create a separate DMA area for 1170c0341432SJohn Baldwin * the result and then copy it back, 1171c0341432SJohn Baldwin * but for now we just bail and return 1172c0341432SJohn Baldwin * an error. Note that uio requests 1173c0341432SJohn Baldwin * > SAFE_MAX_DSIZE are handled because 1174c0341432SJohn Baldwin * the DMA map and segment list for the 1175c0341432SJohn Baldwin * destination wil result in a 1176c0341432SJohn Baldwin * destination particle list that does 1177c0341432SJohn Baldwin * the necessary scatter DMA. 1178c0341432SJohn Baldwin */ 1179c0341432SJohn Baldwin safestats.st_iovnotuniform++; 1180c0341432SJohn Baldwin err = EINVAL; 1181c0341432SJohn Baldwin goto errout; 1182c0341432SJohn Baldwin } 1183b7e3f244SSam Leffler } 1184b7e3f244SSam Leffler 1185b7e3f244SSam Leffler if (re->re_dst.nsegs > 1) { 1186b7e3f244SSam Leffler re->re_desc.d_dst = sc->sc_dpalloc.dma_paddr + 1187b7e3f244SSam Leffler ((caddr_t) sc->sc_dpfree - (caddr_t) sc->sc_dpring); 1188b7e3f244SSam Leffler for (i = 0; i < re->re_dst_nsegs; i++) { 1189b7e3f244SSam Leffler pd = sc->sc_dpfree; 1190b7e3f244SSam Leffler KASSERT((pd->pd_flags&3) == 0 || 1191b7e3f244SSam Leffler (pd->pd_flags&3) == SAFE_PD_DONE, 1192b7e3f244SSam Leffler ("bogus dest particle descriptor; flags %x", 1193b7e3f244SSam Leffler pd->pd_flags)); 1194b7e3f244SSam Leffler if (++(sc->sc_dpfree) == sc->sc_dpringtop) 1195b7e3f244SSam Leffler sc->sc_dpfree = sc->sc_dpring; 1196b7e3f244SSam Leffler pd->pd_addr = re->re_dst_segs[i].ds_addr; 1197b7e3f244SSam Leffler pd->pd_flags = SAFE_PD_READY; 1198b7e3f244SSam Leffler } 1199b7e3f244SSam Leffler cmd0 |= SAFE_SA_CMD0_OSCATTER; 1200b7e3f244SSam Leffler } else { 1201b7e3f244SSam Leffler /* 1202b7e3f244SSam Leffler * No need for scatter, reference the operand directly. 1203b7e3f244SSam Leffler */ 1204b7e3f244SSam Leffler re->re_desc.d_dst = re->re_dst_segs[0].ds_addr; 1205b7e3f244SSam Leffler } 1206b7e3f244SSam Leffler } 1207b7e3f244SSam Leffler 1208b7e3f244SSam Leffler /* 1209b7e3f244SSam Leffler * All done with setup; fillin the SA command words 1210b7e3f244SSam Leffler * and the packet engine descriptor. The operation 1211b7e3f244SSam Leffler * is now ready for submission to the hardware. 1212b7e3f244SSam Leffler */ 1213b7e3f244SSam Leffler sa->sa_cmd0 = cmd0 | SAFE_SA_CMD0_IPCI | SAFE_SA_CMD0_OPCI; 1214b7e3f244SSam Leffler sa->sa_cmd1 = cmd1 1215b7e3f244SSam Leffler | (coffset << SAFE_SA_CMD1_OFFSET_S) 1216b7e3f244SSam Leffler | SAFE_SA_CMD1_SAREV1 /* Rev 1 SA data structure */ 1217b7e3f244SSam Leffler | SAFE_SA_CMD1_SRPCI 1218b7e3f244SSam Leffler ; 1219b7e3f244SSam Leffler /* 1220b7e3f244SSam Leffler * NB: the order of writes is important here. In case the 1221b7e3f244SSam Leffler * chip is scanning the ring because of an outstanding request 1222b7e3f244SSam Leffler * it might nab this one too. In that case we need to make 1223b7e3f244SSam Leffler * sure the setup is complete before we write the length 1224b7e3f244SSam Leffler * field of the descriptor as it signals the descriptor is 1225b7e3f244SSam Leffler * ready for processing. 1226b7e3f244SSam Leffler */ 1227b7e3f244SSam Leffler re->re_desc.d_csr = SAFE_PE_CSR_READY | SAFE_PE_CSR_SAPCI; 1228c0341432SJohn Baldwin if (csp->csp_auth_alg != 0) 1229b7e3f244SSam Leffler re->re_desc.d_csr |= SAFE_PE_CSR_LOADSA | SAFE_PE_CSR_HASHFINAL; 1230b7e3f244SSam Leffler re->re_desc.d_len = oplen 1231b7e3f244SSam Leffler | SAFE_PE_LEN_READY 1232b7e3f244SSam Leffler | (bypass << SAFE_PE_LEN_BYPASS_S) 1233b7e3f244SSam Leffler ; 1234b7e3f244SSam Leffler 1235b7e3f244SSam Leffler safestats.st_ipackets++; 1236b7e3f244SSam Leffler safestats.st_ibytes += oplen; 1237b7e3f244SSam Leffler 1238b7e3f244SSam Leffler if (++(sc->sc_front) == sc->sc_ringtop) 1239b7e3f244SSam Leffler sc->sc_front = sc->sc_ring; 1240b7e3f244SSam Leffler 1241b7e3f244SSam Leffler /* XXX honor batching */ 1242b7e3f244SSam Leffler safe_feed(sc, re); 1243b7e3f244SSam Leffler mtx_unlock(&sc->sc_ringmtx); 1244b7e3f244SSam Leffler return (0); 1245b7e3f244SSam Leffler 1246b7e3f244SSam Leffler errout: 1247c0341432SJohn Baldwin if (re->re_dst_m != NULL) 1248b7e3f244SSam Leffler m_freem(re->re_dst_m); 1249b7e3f244SSam Leffler 1250b7e3f244SSam Leffler if (re->re_dst_map != NULL && re->re_dst_map != re->re_src_map) { 1251b7e3f244SSam Leffler bus_dmamap_unload(sc->sc_dstdmat, re->re_dst_map); 1252b7e3f244SSam Leffler bus_dmamap_destroy(sc->sc_dstdmat, re->re_dst_map); 1253b7e3f244SSam Leffler } 1254b7e3f244SSam Leffler if (re->re_src_map != NULL) { 1255b7e3f244SSam Leffler bus_dmamap_unload(sc->sc_srcdmat, re->re_src_map); 1256b7e3f244SSam Leffler bus_dmamap_destroy(sc->sc_srcdmat, re->re_src_map); 1257b7e3f244SSam Leffler } 1258b7e3f244SSam Leffler mtx_unlock(&sc->sc_ringmtx); 1259b7e3f244SSam Leffler if (err != ERESTART) { 1260b7e3f244SSam Leffler crp->crp_etype = err; 1261b7e3f244SSam Leffler crypto_done(crp); 1262b7e3f244SSam Leffler } else { 1263b7e3f244SSam Leffler sc->sc_needwakeup |= CRYPTO_SYMQ; 1264b7e3f244SSam Leffler } 1265b7e3f244SSam Leffler return (err); 1266b7e3f244SSam Leffler } 1267b7e3f244SSam Leffler 1268b7e3f244SSam Leffler static void 1269b7e3f244SSam Leffler safe_callback(struct safe_softc *sc, struct safe_ringentry *re) 1270b7e3f244SSam Leffler { 1271c0341432SJohn Baldwin const struct crypto_session_params *csp; 1272b7e3f244SSam Leffler struct cryptop *crp = (struct cryptop *)re->re_crp; 12731b0909d5SConrad Meyer struct safe_session *ses; 1274c0341432SJohn Baldwin uint8_t hash[HASH_MAX_LEN]; 1275b7e3f244SSam Leffler 12761b0909d5SConrad Meyer ses = crypto_get_driver_session(crp->crp_session); 1277c0341432SJohn Baldwin csp = crypto_get_params(crp->crp_session); 12781b0909d5SConrad Meyer 1279b7e3f244SSam Leffler safestats.st_opackets++; 1280b7e3f244SSam Leffler safestats.st_obytes += re->re_dst.mapsize; 1281b7e3f244SSam Leffler 1282b7e3f244SSam Leffler safe_dma_sync(&sc->sc_ringalloc, 1283b7e3f244SSam Leffler BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 1284b7e3f244SSam Leffler if (re->re_desc.d_csr & SAFE_PE_CSR_STATUS) { 1285b7e3f244SSam Leffler device_printf(sc->sc_dev, "csr 0x%x cmd0 0x%x cmd1 0x%x\n", 1286b7e3f244SSam Leffler re->re_desc.d_csr, 1287b7e3f244SSam Leffler re->re_sa.sa_cmd0, re->re_sa.sa_cmd1); 1288b7e3f244SSam Leffler safestats.st_peoperr++; 1289b7e3f244SSam Leffler crp->crp_etype = EIO; /* something more meaningful? */ 1290b7e3f244SSam Leffler } 1291c0341432SJohn Baldwin 1292*9c0e3d3aSJohn Baldwin /* 1293*9c0e3d3aSJohn Baldwin * XXX: Should crp_buf.cb_mbuf be updated to re->re_dst_m if 1294*9c0e3d3aSJohn Baldwin * it is non-NULL? 1295*9c0e3d3aSJohn Baldwin */ 1296c0341432SJohn Baldwin 1297b7e3f244SSam Leffler if (re->re_dst_map != NULL && re->re_dst_map != re->re_src_map) { 1298b7e3f244SSam Leffler bus_dmamap_sync(sc->sc_dstdmat, re->re_dst_map, 1299b7e3f244SSam Leffler BUS_DMASYNC_POSTREAD); 1300b7e3f244SSam Leffler bus_dmamap_unload(sc->sc_dstdmat, re->re_dst_map); 1301b7e3f244SSam Leffler bus_dmamap_destroy(sc->sc_dstdmat, re->re_dst_map); 1302b7e3f244SSam Leffler } 1303b7e3f244SSam Leffler bus_dmamap_sync(sc->sc_srcdmat, re->re_src_map, BUS_DMASYNC_POSTWRITE); 1304b7e3f244SSam Leffler bus_dmamap_unload(sc->sc_srcdmat, re->re_src_map); 1305b7e3f244SSam Leffler bus_dmamap_destroy(sc->sc_srcdmat, re->re_src_map); 1306b7e3f244SSam Leffler 1307b7e3f244SSam Leffler if (re->re_flags & SAFE_QFLAGS_COPYOUTICV) { 1308c0341432SJohn Baldwin if (csp->csp_auth_alg == CRYPTO_SHA1_HMAC) { 1309b7e3f244SSam Leffler /* 1310b7e3f244SSam Leffler * SHA-1 ICV's are byte-swapped; fix 'em up 1311c0341432SJohn Baldwin * before copying them to their destination. 1312b7e3f244SSam Leffler */ 1313357a26abSXin LI re->re_sastate.sa_saved_indigest[0] = 1314b7e3f244SSam Leffler bswap32(re->re_sastate.sa_saved_indigest[0]); 1315357a26abSXin LI re->re_sastate.sa_saved_indigest[1] = 1316b7e3f244SSam Leffler bswap32(re->re_sastate.sa_saved_indigest[1]); 1317357a26abSXin LI re->re_sastate.sa_saved_indigest[2] = 1318b7e3f244SSam Leffler bswap32(re->re_sastate.sa_saved_indigest[2]); 1319b7e3f244SSam Leffler } 1320c0341432SJohn Baldwin 1321c0341432SJohn Baldwin if (crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) { 1322c0341432SJohn Baldwin crypto_copydata(crp, crp->crp_digest_start, 1323c0341432SJohn Baldwin ses->ses_mlen, hash); 1324c0341432SJohn Baldwin if (timingsafe_bcmp(re->re_sastate.sa_saved_indigest, 1325c0341432SJohn Baldwin hash, ses->ses_mlen) != 0) 1326c0341432SJohn Baldwin crp->crp_etype = EBADMSG; 1327c0341432SJohn Baldwin } else 1328c0341432SJohn Baldwin crypto_copyback(crp, crp->crp_digest_start, 1329c0341432SJohn Baldwin ses->ses_mlen, re->re_sastate.sa_saved_indigest); 1330b7e3f244SSam Leffler } 1331b7e3f244SSam Leffler crypto_done(crp); 1332b7e3f244SSam Leffler } 1333b7e3f244SSam Leffler 1334b7e3f244SSam Leffler /* 1335b7e3f244SSam Leffler * Copy all data past offset from srcm to dstm. 1336b7e3f244SSam Leffler */ 1337b7e3f244SSam Leffler static void 1338b7e3f244SSam Leffler safe_mcopy(struct mbuf *srcm, struct mbuf *dstm, u_int offset) 1339b7e3f244SSam Leffler { 1340b7e3f244SSam Leffler u_int j, dlen, slen; 1341b7e3f244SSam Leffler caddr_t dptr, sptr; 1342b7e3f244SSam Leffler 1343b7e3f244SSam Leffler /* 1344b7e3f244SSam Leffler * Advance src and dst to offset. 1345b7e3f244SSam Leffler */ 1346b7e3f244SSam Leffler j = offset; 134719a9c3dfSRyan Libby while (j >= srcm->m_len) { 1348b7e3f244SSam Leffler j -= srcm->m_len; 1349b7e3f244SSam Leffler srcm = srcm->m_next; 1350b7e3f244SSam Leffler if (srcm == NULL) 1351b7e3f244SSam Leffler return; 1352b7e3f244SSam Leffler } 1353b7e3f244SSam Leffler sptr = mtod(srcm, caddr_t) + j; 1354b7e3f244SSam Leffler slen = srcm->m_len - j; 1355b7e3f244SSam Leffler 1356b7e3f244SSam Leffler j = offset; 135719a9c3dfSRyan Libby while (j >= dstm->m_len) { 1358b7e3f244SSam Leffler j -= dstm->m_len; 1359b7e3f244SSam Leffler dstm = dstm->m_next; 1360b7e3f244SSam Leffler if (dstm == NULL) 1361b7e3f244SSam Leffler return; 1362b7e3f244SSam Leffler } 1363b7e3f244SSam Leffler dptr = mtod(dstm, caddr_t) + j; 1364b7e3f244SSam Leffler dlen = dstm->m_len - j; 1365b7e3f244SSam Leffler 1366b7e3f244SSam Leffler /* 1367b7e3f244SSam Leffler * Copy everything that remains. 1368b7e3f244SSam Leffler */ 1369b7e3f244SSam Leffler for (;;) { 1370b7e3f244SSam Leffler j = min(slen, dlen); 1371b7e3f244SSam Leffler bcopy(sptr, dptr, j); 1372b7e3f244SSam Leffler if (slen == j) { 1373b7e3f244SSam Leffler srcm = srcm->m_next; 1374b7e3f244SSam Leffler if (srcm == NULL) 1375b7e3f244SSam Leffler return; 1376b7e3f244SSam Leffler sptr = srcm->m_data; 1377b7e3f244SSam Leffler slen = srcm->m_len; 1378b7e3f244SSam Leffler } else 1379b7e3f244SSam Leffler sptr += j, slen -= j; 1380b7e3f244SSam Leffler if (dlen == j) { 1381b7e3f244SSam Leffler dstm = dstm->m_next; 1382b7e3f244SSam Leffler if (dstm == NULL) 1383b7e3f244SSam Leffler return; 1384b7e3f244SSam Leffler dptr = dstm->m_data; 1385b7e3f244SSam Leffler dlen = dstm->m_len; 1386b7e3f244SSam Leffler } else 1387b7e3f244SSam Leffler dptr += j, dlen -= j; 1388b7e3f244SSam Leffler } 1389b7e3f244SSam Leffler } 1390b7e3f244SSam Leffler 1391b7e3f244SSam Leffler #ifndef SAFE_NO_RNG 1392b7e3f244SSam Leffler #define SAFE_RNG_MAXWAIT 1000 1393b7e3f244SSam Leffler 1394b7e3f244SSam Leffler static void 1395b7e3f244SSam Leffler safe_rng_init(struct safe_softc *sc) 1396b7e3f244SSam Leffler { 1397b7e3f244SSam Leffler u_int32_t w, v; 1398b7e3f244SSam Leffler int i; 1399b7e3f244SSam Leffler 1400b7e3f244SSam Leffler WRITE_REG(sc, SAFE_RNG_CTRL, 0); 1401b7e3f244SSam Leffler /* use default value according to the manual */ 1402b7e3f244SSam Leffler WRITE_REG(sc, SAFE_RNG_CNFG, 0x834); /* magic from SafeNet */ 1403b7e3f244SSam Leffler WRITE_REG(sc, SAFE_RNG_ALM_CNT, 0); 1404b7e3f244SSam Leffler 1405b7e3f244SSam Leffler /* 1406b7e3f244SSam Leffler * There is a bug in rev 1.0 of the 1140 that when the RNG 1407b7e3f244SSam Leffler * is brought out of reset the ready status flag does not 1408b7e3f244SSam Leffler * work until the RNG has finished its internal initialization. 1409b7e3f244SSam Leffler * 1410b7e3f244SSam Leffler * So in order to determine the device is through its 1411b7e3f244SSam Leffler * initialization we must read the data register, using the 1412b7e3f244SSam Leffler * status reg in the read in case it is initialized. Then read 1413b7e3f244SSam Leffler * the data register until it changes from the first read. 1414b7e3f244SSam Leffler * Once it changes read the data register until it changes 1415b7e3f244SSam Leffler * again. At this time the RNG is considered initialized. 1416b7e3f244SSam Leffler * This could take between 750ms - 1000ms in time. 1417b7e3f244SSam Leffler */ 1418b7e3f244SSam Leffler i = 0; 1419b7e3f244SSam Leffler w = READ_REG(sc, SAFE_RNG_OUT); 1420b7e3f244SSam Leffler do { 1421b7e3f244SSam Leffler v = READ_REG(sc, SAFE_RNG_OUT); 1422b7e3f244SSam Leffler if (v != w) { 1423b7e3f244SSam Leffler w = v; 1424b7e3f244SSam Leffler break; 1425b7e3f244SSam Leffler } 1426b7e3f244SSam Leffler DELAY(10); 1427b7e3f244SSam Leffler } while (++i < SAFE_RNG_MAXWAIT); 1428b7e3f244SSam Leffler 1429b7e3f244SSam Leffler /* Wait Until data changes again */ 1430b7e3f244SSam Leffler i = 0; 1431b7e3f244SSam Leffler do { 1432b7e3f244SSam Leffler v = READ_REG(sc, SAFE_RNG_OUT); 1433b7e3f244SSam Leffler if (v != w) 1434b7e3f244SSam Leffler break; 1435b7e3f244SSam Leffler DELAY(10); 1436b7e3f244SSam Leffler } while (++i < SAFE_RNG_MAXWAIT); 1437b7e3f244SSam Leffler } 1438b7e3f244SSam Leffler 1439b7e3f244SSam Leffler static __inline void 1440b7e3f244SSam Leffler safe_rng_disable_short_cycle(struct safe_softc *sc) 1441b7e3f244SSam Leffler { 1442b7e3f244SSam Leffler WRITE_REG(sc, SAFE_RNG_CTRL, 1443b7e3f244SSam Leffler READ_REG(sc, SAFE_RNG_CTRL) &~ SAFE_RNG_CTRL_SHORTEN); 1444b7e3f244SSam Leffler } 1445b7e3f244SSam Leffler 1446b7e3f244SSam Leffler static __inline void 1447b7e3f244SSam Leffler safe_rng_enable_short_cycle(struct safe_softc *sc) 1448b7e3f244SSam Leffler { 1449b7e3f244SSam Leffler WRITE_REG(sc, SAFE_RNG_CTRL, 1450b7e3f244SSam Leffler READ_REG(sc, SAFE_RNG_CTRL) | SAFE_RNG_CTRL_SHORTEN); 1451b7e3f244SSam Leffler } 1452b7e3f244SSam Leffler 1453b7e3f244SSam Leffler static __inline u_int32_t 1454b7e3f244SSam Leffler safe_rng_read(struct safe_softc *sc) 1455b7e3f244SSam Leffler { 1456b7e3f244SSam Leffler int i; 1457b7e3f244SSam Leffler 1458b7e3f244SSam Leffler i = 0; 1459b7e3f244SSam Leffler while (READ_REG(sc, SAFE_RNG_STAT) != 0 && ++i < SAFE_RNG_MAXWAIT) 1460b7e3f244SSam Leffler ; 1461b7e3f244SSam Leffler return READ_REG(sc, SAFE_RNG_OUT); 1462b7e3f244SSam Leffler } 1463b7e3f244SSam Leffler 1464b7e3f244SSam Leffler static void 1465b7e3f244SSam Leffler safe_rng(void *arg) 1466b7e3f244SSam Leffler { 1467b7e3f244SSam Leffler struct safe_softc *sc = arg; 1468b7e3f244SSam Leffler u_int32_t buf[SAFE_RNG_MAXBUFSIZ]; /* NB: maybe move to softc */ 1469b7e3f244SSam Leffler u_int maxwords; 1470b7e3f244SSam Leffler int i; 1471b7e3f244SSam Leffler 1472b7e3f244SSam Leffler safestats.st_rng++; 1473b7e3f244SSam Leffler /* 1474b7e3f244SSam Leffler * Fetch the next block of data. 1475b7e3f244SSam Leffler */ 1476b7e3f244SSam Leffler maxwords = safe_rngbufsize; 1477b7e3f244SSam Leffler if (maxwords > SAFE_RNG_MAXBUFSIZ) 1478b7e3f244SSam Leffler maxwords = SAFE_RNG_MAXBUFSIZ; 1479b7e3f244SSam Leffler retry: 1480b7e3f244SSam Leffler for (i = 0; i < maxwords; i++) 1481b7e3f244SSam Leffler buf[i] = safe_rng_read(sc); 1482b7e3f244SSam Leffler /* 1483b7e3f244SSam Leffler * Check the comparator alarm count and reset the h/w if 1484b7e3f244SSam Leffler * it exceeds our threshold. This guards against the 1485b7e3f244SSam Leffler * hardware oscillators resonating with external signals. 1486b7e3f244SSam Leffler */ 1487b7e3f244SSam Leffler if (READ_REG(sc, SAFE_RNG_ALM_CNT) > safe_rngmaxalarm) { 1488b7e3f244SSam Leffler u_int32_t freq_inc, w; 1489b7e3f244SSam Leffler 1490b7e3f244SSam Leffler DPRINTF(("%s: alarm count %u exceeds threshold %u\n", __func__, 1491b7e3f244SSam Leffler READ_REG(sc, SAFE_RNG_ALM_CNT), safe_rngmaxalarm)); 1492b7e3f244SSam Leffler safestats.st_rngalarm++; 1493b7e3f244SSam Leffler safe_rng_enable_short_cycle(sc); 1494b7e3f244SSam Leffler freq_inc = 18; 1495b7e3f244SSam Leffler for (i = 0; i < 64; i++) { 1496b7e3f244SSam Leffler w = READ_REG(sc, SAFE_RNG_CNFG); 1497b7e3f244SSam Leffler freq_inc = ((w + freq_inc) & 0x3fL); 1498b7e3f244SSam Leffler w = ((w & ~0x3fL) | freq_inc); 1499b7e3f244SSam Leffler WRITE_REG(sc, SAFE_RNG_CNFG, w); 1500b7e3f244SSam Leffler 1501b7e3f244SSam Leffler WRITE_REG(sc, SAFE_RNG_ALM_CNT, 0); 1502b7e3f244SSam Leffler 1503b7e3f244SSam Leffler (void) safe_rng_read(sc); 1504b7e3f244SSam Leffler DELAY(25); 1505b7e3f244SSam Leffler 1506b7e3f244SSam Leffler if (READ_REG(sc, SAFE_RNG_ALM_CNT) == 0) { 1507b7e3f244SSam Leffler safe_rng_disable_short_cycle(sc); 1508b7e3f244SSam Leffler goto retry; 1509b7e3f244SSam Leffler } 1510b7e3f244SSam Leffler freq_inc = 1; 1511b7e3f244SSam Leffler } 1512b7e3f244SSam Leffler safe_rng_disable_short_cycle(sc); 1513b7e3f244SSam Leffler } else 1514b7e3f244SSam Leffler WRITE_REG(sc, SAFE_RNG_ALM_CNT, 0); 1515b7e3f244SSam Leffler 1516b7e3f244SSam Leffler (*sc->sc_harvest)(sc->sc_rndtest, buf, maxwords*sizeof (u_int32_t)); 1517b7e3f244SSam Leffler callout_reset(&sc->sc_rngto, 1518b7e3f244SSam Leffler hz * (safe_rnginterval ? safe_rnginterval : 1), safe_rng, sc); 1519b7e3f244SSam Leffler } 1520b7e3f244SSam Leffler #endif /* SAFE_NO_RNG */ 1521b7e3f244SSam Leffler 1522b7e3f244SSam Leffler static void 1523b7e3f244SSam Leffler safe_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 1524b7e3f244SSam Leffler { 1525b7e3f244SSam Leffler bus_addr_t *paddr = (bus_addr_t*) arg; 1526b7e3f244SSam Leffler *paddr = segs->ds_addr; 1527b7e3f244SSam Leffler } 1528b7e3f244SSam Leffler 1529b7e3f244SSam Leffler static int 1530b7e3f244SSam Leffler safe_dma_malloc( 1531b7e3f244SSam Leffler struct safe_softc *sc, 1532b7e3f244SSam Leffler bus_size_t size, 1533b7e3f244SSam Leffler struct safe_dma_alloc *dma, 1534b7e3f244SSam Leffler int mapflags 1535b7e3f244SSam Leffler ) 1536b7e3f244SSam Leffler { 1537b7e3f244SSam Leffler int r; 1538b7e3f244SSam Leffler 153962ce43ccSScott Long r = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), /* parent */ 1540b7e3f244SSam Leffler sizeof(u_int32_t), 0, /* alignment, bounds */ 1541b7e3f244SSam Leffler BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 1542b7e3f244SSam Leffler BUS_SPACE_MAXADDR, /* highaddr */ 1543b7e3f244SSam Leffler NULL, NULL, /* filter, filterarg */ 1544b7e3f244SSam Leffler size, /* maxsize */ 1545b7e3f244SSam Leffler 1, /* nsegments */ 1546b7e3f244SSam Leffler size, /* maxsegsize */ 1547b7e3f244SSam Leffler BUS_DMA_ALLOCNOW, /* flags */ 1548b7e3f244SSam Leffler NULL, NULL, /* locking */ 1549b7e3f244SSam Leffler &dma->dma_tag); 1550b7e3f244SSam Leffler if (r != 0) { 1551b7e3f244SSam Leffler device_printf(sc->sc_dev, "safe_dma_malloc: " 1552b7e3f244SSam Leffler "bus_dma_tag_create failed; error %u\n", r); 1553b7e3f244SSam Leffler goto fail_0; 1554b7e3f244SSam Leffler } 1555b7e3f244SSam Leffler 1556b7e3f244SSam Leffler r = bus_dmamem_alloc(dma->dma_tag, (void**) &dma->dma_vaddr, 1557b7e3f244SSam Leffler BUS_DMA_NOWAIT, &dma->dma_map); 1558b7e3f244SSam Leffler if (r != 0) { 1559b7e3f244SSam Leffler device_printf(sc->sc_dev, "safe_dma_malloc: " 1560d14c4346SJohn-Mark Gurney "bus_dmammem_alloc failed; size %ju, error %u\n", 1561d14c4346SJohn-Mark Gurney (uintmax_t)size, r); 1562f07894dbSJohn Baldwin goto fail_1; 1563b7e3f244SSam Leffler } 1564b7e3f244SSam Leffler 1565b7e3f244SSam Leffler r = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr, 1566b7e3f244SSam Leffler size, 1567b7e3f244SSam Leffler safe_dmamap_cb, 1568b7e3f244SSam Leffler &dma->dma_paddr, 1569b7e3f244SSam Leffler mapflags | BUS_DMA_NOWAIT); 1570b7e3f244SSam Leffler if (r != 0) { 1571b7e3f244SSam Leffler device_printf(sc->sc_dev, "safe_dma_malloc: " 1572b7e3f244SSam Leffler "bus_dmamap_load failed; error %u\n", r); 1573f07894dbSJohn Baldwin goto fail_2; 1574b7e3f244SSam Leffler } 1575b7e3f244SSam Leffler 1576b7e3f244SSam Leffler dma->dma_size = size; 1577b7e3f244SSam Leffler return (0); 1578b7e3f244SSam Leffler 1579b7e3f244SSam Leffler bus_dmamap_unload(dma->dma_tag, dma->dma_map); 1580b7e3f244SSam Leffler fail_2: 1581b7e3f244SSam Leffler bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map); 1582b7e3f244SSam Leffler fail_1: 1583b7e3f244SSam Leffler bus_dma_tag_destroy(dma->dma_tag); 1584b7e3f244SSam Leffler fail_0: 1585b7e3f244SSam Leffler dma->dma_tag = NULL; 1586b7e3f244SSam Leffler return (r); 1587b7e3f244SSam Leffler } 1588b7e3f244SSam Leffler 1589b7e3f244SSam Leffler static void 1590b7e3f244SSam Leffler safe_dma_free(struct safe_softc *sc, struct safe_dma_alloc *dma) 1591b7e3f244SSam Leffler { 1592b7e3f244SSam Leffler bus_dmamap_unload(dma->dma_tag, dma->dma_map); 1593b7e3f244SSam Leffler bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map); 1594b7e3f244SSam Leffler bus_dma_tag_destroy(dma->dma_tag); 1595b7e3f244SSam Leffler } 1596b7e3f244SSam Leffler 1597b7e3f244SSam Leffler /* 1598b7e3f244SSam Leffler * Resets the board. Values in the regesters are left as is 1599b7e3f244SSam Leffler * from the reset (i.e. initial values are assigned elsewhere). 1600b7e3f244SSam Leffler */ 1601b7e3f244SSam Leffler static void 1602b7e3f244SSam Leffler safe_reset_board(struct safe_softc *sc) 1603b7e3f244SSam Leffler { 1604b7e3f244SSam Leffler u_int32_t v; 1605b7e3f244SSam Leffler /* 1606b7e3f244SSam Leffler * Reset the device. The manual says no delay 1607b7e3f244SSam Leffler * is needed between marking and clearing reset. 1608b7e3f244SSam Leffler */ 1609b7e3f244SSam Leffler v = READ_REG(sc, SAFE_PE_DMACFG) &~ 1610b7e3f244SSam Leffler (SAFE_PE_DMACFG_PERESET | SAFE_PE_DMACFG_PDRRESET | 1611b7e3f244SSam Leffler SAFE_PE_DMACFG_SGRESET); 1612b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_DMACFG, v 1613b7e3f244SSam Leffler | SAFE_PE_DMACFG_PERESET 1614b7e3f244SSam Leffler | SAFE_PE_DMACFG_PDRRESET 1615b7e3f244SSam Leffler | SAFE_PE_DMACFG_SGRESET); 1616b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_DMACFG, v); 1617b7e3f244SSam Leffler } 1618b7e3f244SSam Leffler 1619b7e3f244SSam Leffler /* 1620b7e3f244SSam Leffler * Initialize registers we need to touch only once. 1621b7e3f244SSam Leffler */ 1622b7e3f244SSam Leffler static void 1623b7e3f244SSam Leffler safe_init_board(struct safe_softc *sc) 1624b7e3f244SSam Leffler { 1625b7e3f244SSam Leffler u_int32_t v, dwords; 1626b7e3f244SSam Leffler 1627c2ede4b3SMartin Blapp v = READ_REG(sc, SAFE_PE_DMACFG); 1628b7e3f244SSam Leffler v &=~ SAFE_PE_DMACFG_PEMODE; 1629b7e3f244SSam Leffler v |= SAFE_PE_DMACFG_FSENA /* failsafe enable */ 1630b7e3f244SSam Leffler | SAFE_PE_DMACFG_GPRPCI /* gather ring on PCI */ 1631b7e3f244SSam Leffler | SAFE_PE_DMACFG_SPRPCI /* scatter ring on PCI */ 1632b7e3f244SSam Leffler | SAFE_PE_DMACFG_ESDESC /* endian-swap descriptors */ 1633b7e3f244SSam Leffler | SAFE_PE_DMACFG_ESSA /* endian-swap SA's */ 1634b7e3f244SSam Leffler | SAFE_PE_DMACFG_ESPDESC /* endian-swap part. desc's */ 1635b7e3f244SSam Leffler ; 1636b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_DMACFG, v); 1637b7e3f244SSam Leffler #if 0 1638b7e3f244SSam Leffler /* XXX select byte swap based on host byte order */ 1639b7e3f244SSam Leffler WRITE_REG(sc, SAFE_ENDIAN, 0x1b); 1640b7e3f244SSam Leffler #endif 1641b7e3f244SSam Leffler if (sc->sc_chiprev == SAFE_REV(1,0)) { 1642b7e3f244SSam Leffler /* 1643b7e3f244SSam Leffler * Avoid large PCI DMA transfers. Rev 1.0 has a bug where 1644b7e3f244SSam Leffler * "target mode transfers" done while the chip is DMA'ing 1645b7e3f244SSam Leffler * >1020 bytes cause the hardware to lockup. To avoid this 1646b7e3f244SSam Leffler * we reduce the max PCI transfer size and use small source 1647b7e3f244SSam Leffler * particle descriptors (<= 256 bytes). 1648b7e3f244SSam Leffler */ 1649b7e3f244SSam Leffler WRITE_REG(sc, SAFE_DMA_CFG, 256); 1650b7e3f244SSam Leffler device_printf(sc->sc_dev, 1651b7e3f244SSam Leffler "Reduce max DMA size to %u words for rev %u.%u WAR\n", 1652b7e3f244SSam Leffler (READ_REG(sc, SAFE_DMA_CFG)>>2) & 0xff, 1653b7e3f244SSam Leffler SAFE_REV_MAJ(sc->sc_chiprev), 1654b7e3f244SSam Leffler SAFE_REV_MIN(sc->sc_chiprev)); 1655b7e3f244SSam Leffler } 1656b7e3f244SSam Leffler 1657b7e3f244SSam Leffler /* NB: operands+results are overlaid */ 1658b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_PDRBASE, sc->sc_ringalloc.dma_paddr); 1659b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_RDRBASE, sc->sc_ringalloc.dma_paddr); 1660b7e3f244SSam Leffler /* 1661b7e3f244SSam Leffler * Configure ring entry size and number of items in the ring. 1662b7e3f244SSam Leffler */ 1663b7e3f244SSam Leffler KASSERT((sizeof(struct safe_ringentry) % sizeof(u_int32_t)) == 0, 1664b7e3f244SSam Leffler ("PE ring entry not 32-bit aligned!")); 1665b7e3f244SSam Leffler dwords = sizeof(struct safe_ringentry) / sizeof(u_int32_t); 1666b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_RINGCFG, 1667b7e3f244SSam Leffler (dwords << SAFE_PE_RINGCFG_OFFSET_S) | SAFE_MAX_NQUEUE); 1668b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_RINGPOLL, 0); /* disable polling */ 1669b7e3f244SSam Leffler 1670b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_GRNGBASE, sc->sc_spalloc.dma_paddr); 1671b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_SRNGBASE, sc->sc_dpalloc.dma_paddr); 1672b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_PARTSIZE, 1673b7e3f244SSam Leffler (SAFE_TOTAL_DPART<<16) | SAFE_TOTAL_SPART); 1674b7e3f244SSam Leffler /* 1675b7e3f244SSam Leffler * NB: destination particles are fixed size. We use 1676b7e3f244SSam Leffler * an mbuf cluster and require all results go to 1677b7e3f244SSam Leffler * clusters or smaller. 1678b7e3f244SSam Leffler */ 1679b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_PARTCFG, SAFE_MAX_DSIZE); 1680b7e3f244SSam Leffler 1681b7e3f244SSam Leffler /* it's now safe to enable PE mode, do it */ 1682b7e3f244SSam Leffler WRITE_REG(sc, SAFE_PE_DMACFG, v | SAFE_PE_DMACFG_PEMODE); 1683b7e3f244SSam Leffler 1684b7e3f244SSam Leffler /* 1685b7e3f244SSam Leffler * Configure hardware to use level-triggered interrupts and 1686b7e3f244SSam Leffler * to interrupt after each descriptor is processed. 1687b7e3f244SSam Leffler */ 1688b7e3f244SSam Leffler WRITE_REG(sc, SAFE_HI_CFG, SAFE_HI_CFG_LEVEL); 1689b7e3f244SSam Leffler WRITE_REG(sc, SAFE_HI_DESC_CNT, 1); 1690b7e3f244SSam Leffler WRITE_REG(sc, SAFE_HI_MASK, SAFE_INT_PE_DDONE | SAFE_INT_PE_ERROR); 1691b7e3f244SSam Leffler } 1692b7e3f244SSam Leffler 1693b7e3f244SSam Leffler /* 1694b7e3f244SSam Leffler * Init PCI registers 1695b7e3f244SSam Leffler */ 1696b7e3f244SSam Leffler static void 1697b7e3f244SSam Leffler safe_init_pciregs(device_t dev) 1698b7e3f244SSam Leffler { 1699b7e3f244SSam Leffler } 1700b7e3f244SSam Leffler 1701b7e3f244SSam Leffler /* 1702b7e3f244SSam Leffler * Clean up after a chip crash. 1703b7e3f244SSam Leffler * It is assumed that the caller in splimp() 1704b7e3f244SSam Leffler */ 1705b7e3f244SSam Leffler static void 1706b7e3f244SSam Leffler safe_cleanchip(struct safe_softc *sc) 1707b7e3f244SSam Leffler { 1708b7e3f244SSam Leffler 1709b7e3f244SSam Leffler if (sc->sc_nqchip != 0) { 1710b7e3f244SSam Leffler struct safe_ringentry *re = sc->sc_back; 1711b7e3f244SSam Leffler 1712b7e3f244SSam Leffler while (re != sc->sc_front) { 1713b7e3f244SSam Leffler if (re->re_desc.d_csr != 0) 1714b7e3f244SSam Leffler safe_free_entry(sc, re); 1715b7e3f244SSam Leffler if (++re == sc->sc_ringtop) 1716b7e3f244SSam Leffler re = sc->sc_ring; 1717b7e3f244SSam Leffler } 1718b7e3f244SSam Leffler sc->sc_back = re; 1719b7e3f244SSam Leffler sc->sc_nqchip = 0; 1720b7e3f244SSam Leffler } 1721b7e3f244SSam Leffler } 1722b7e3f244SSam Leffler 1723b7e3f244SSam Leffler /* 1724b7e3f244SSam Leffler * free a safe_q 1725b7e3f244SSam Leffler * It is assumed that the caller is within splimp(). 1726b7e3f244SSam Leffler */ 1727b7e3f244SSam Leffler static int 1728b7e3f244SSam Leffler safe_free_entry(struct safe_softc *sc, struct safe_ringentry *re) 1729b7e3f244SSam Leffler { 1730b7e3f244SSam Leffler struct cryptop *crp; 1731b7e3f244SSam Leffler 1732b7e3f244SSam Leffler /* 1733b7e3f244SSam Leffler * Free header MCR 1734b7e3f244SSam Leffler */ 1735c0341432SJohn Baldwin if (re->re_dst_m != NULL) 1736b7e3f244SSam Leffler m_freem(re->re_dst_m); 1737b7e3f244SSam Leffler 1738b7e3f244SSam Leffler crp = (struct cryptop *)re->re_crp; 1739b7e3f244SSam Leffler 1740b7e3f244SSam Leffler re->re_desc.d_csr = 0; 1741b7e3f244SSam Leffler 1742b7e3f244SSam Leffler crp->crp_etype = EFAULT; 1743b7e3f244SSam Leffler crypto_done(crp); 1744b7e3f244SSam Leffler return(0); 1745b7e3f244SSam Leffler } 1746b7e3f244SSam Leffler 1747b7e3f244SSam Leffler /* 1748b7e3f244SSam Leffler * Routine to reset the chip and clean up. 1749b7e3f244SSam Leffler * It is assumed that the caller is in splimp() 1750b7e3f244SSam Leffler */ 1751b7e3f244SSam Leffler static void 1752b7e3f244SSam Leffler safe_totalreset(struct safe_softc *sc) 1753b7e3f244SSam Leffler { 1754b7e3f244SSam Leffler safe_reset_board(sc); 1755b7e3f244SSam Leffler safe_init_board(sc); 1756b7e3f244SSam Leffler safe_cleanchip(sc); 1757b7e3f244SSam Leffler } 1758b7e3f244SSam Leffler 1759b7e3f244SSam Leffler /* 1760b7e3f244SSam Leffler * Is the operand suitable aligned for direct DMA. Each 1761b7e3f244SSam Leffler * segment must be aligned on a 32-bit boundary and all 1762b7e3f244SSam Leffler * but the last segment must be a multiple of 4 bytes. 1763b7e3f244SSam Leffler */ 1764b7e3f244SSam Leffler static int 1765b7e3f244SSam Leffler safe_dmamap_aligned(const struct safe_operand *op) 1766b7e3f244SSam Leffler { 1767b7e3f244SSam Leffler int i; 1768b7e3f244SSam Leffler 1769b7e3f244SSam Leffler for (i = 0; i < op->nsegs; i++) { 1770b7e3f244SSam Leffler if (op->segs[i].ds_addr & 3) 1771b7e3f244SSam Leffler return (0); 1772b7e3f244SSam Leffler if (i != (op->nsegs - 1) && (op->segs[i].ds_len & 3)) 1773b7e3f244SSam Leffler return (0); 1774b7e3f244SSam Leffler } 1775b7e3f244SSam Leffler return (1); 1776b7e3f244SSam Leffler } 1777b7e3f244SSam Leffler 1778b7e3f244SSam Leffler /* 1779b7e3f244SSam Leffler * Is the operand suitable for direct DMA as the destination 1780b7e3f244SSam Leffler * of an operation. The hardware requires that each ``particle'' 1781b7e3f244SSam Leffler * but the last in an operation result have the same size. We 1782b7e3f244SSam Leffler * fix that size at SAFE_MAX_DSIZE bytes. This routine returns 1783b7e3f244SSam Leffler * 0 if some segment is not a multiple of of this size, 1 if all 1784b7e3f244SSam Leffler * segments are exactly this size, or 2 if segments are at worst 1785b7e3f244SSam Leffler * a multple of this size. 1786b7e3f244SSam Leffler */ 1787b7e3f244SSam Leffler static int 1788b7e3f244SSam Leffler safe_dmamap_uniform(const struct safe_operand *op) 1789b7e3f244SSam Leffler { 1790b7e3f244SSam Leffler int result = 1; 1791b7e3f244SSam Leffler 1792b7e3f244SSam Leffler if (op->nsegs > 0) { 1793b7e3f244SSam Leffler int i; 1794b7e3f244SSam Leffler 1795900017e8SSam Leffler for (i = 0; i < op->nsegs-1; i++) { 1796b7e3f244SSam Leffler if (op->segs[i].ds_len % SAFE_MAX_DSIZE) 1797b7e3f244SSam Leffler return (0); 1798b7e3f244SSam Leffler if (op->segs[i].ds_len != SAFE_MAX_DSIZE) 1799b7e3f244SSam Leffler result = 2; 1800b7e3f244SSam Leffler } 1801900017e8SSam Leffler } 1802b7e3f244SSam Leffler return (result); 1803b7e3f244SSam Leffler } 1804b7e3f244SSam Leffler 1805b7e3f244SSam Leffler #ifdef SAFE_DEBUG 1806b7e3f244SSam Leffler static void 1807b7e3f244SSam Leffler safe_dump_dmastatus(struct safe_softc *sc, const char *tag) 1808b7e3f244SSam Leffler { 1809b7e3f244SSam Leffler printf("%s: ENDIAN 0x%x SRC 0x%x DST 0x%x STAT 0x%x\n" 1810b7e3f244SSam Leffler , tag 1811b7e3f244SSam Leffler , READ_REG(sc, SAFE_DMA_ENDIAN) 1812b7e3f244SSam Leffler , READ_REG(sc, SAFE_DMA_SRCADDR) 1813b7e3f244SSam Leffler , READ_REG(sc, SAFE_DMA_DSTADDR) 1814b7e3f244SSam Leffler , READ_REG(sc, SAFE_DMA_STAT) 1815b7e3f244SSam Leffler ); 1816b7e3f244SSam Leffler } 1817b7e3f244SSam Leffler 1818b7e3f244SSam Leffler static void 1819b7e3f244SSam Leffler safe_dump_intrstate(struct safe_softc *sc, const char *tag) 1820b7e3f244SSam Leffler { 1821b7e3f244SSam 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" 1822b7e3f244SSam Leffler , tag 1823b7e3f244SSam Leffler , READ_REG(sc, SAFE_HI_CFG) 1824b7e3f244SSam Leffler , READ_REG(sc, SAFE_HI_MASK) 1825b7e3f244SSam Leffler , READ_REG(sc, SAFE_HI_DESC_CNT) 1826b7e3f244SSam Leffler , READ_REG(sc, SAFE_HU_STAT) 1827b7e3f244SSam Leffler , READ_REG(sc, SAFE_HM_STAT) 1828b7e3f244SSam Leffler ); 1829b7e3f244SSam Leffler } 1830b7e3f244SSam Leffler 1831b7e3f244SSam Leffler static void 1832b7e3f244SSam Leffler safe_dump_ringstate(struct safe_softc *sc, const char *tag) 1833b7e3f244SSam Leffler { 1834b7e3f244SSam Leffler u_int32_t estat = READ_REG(sc, SAFE_PE_ERNGSTAT); 1835b7e3f244SSam Leffler 1836b7e3f244SSam Leffler /* NB: assume caller has lock on ring */ 1837668329e9SPeter Wemm printf("%s: ERNGSTAT %x (next %u) back %lu front %lu\n", 1838b7e3f244SSam Leffler tag, 1839b7e3f244SSam Leffler estat, (estat >> SAFE_PE_ERNGSTAT_NEXT_S), 1840668329e9SPeter Wemm (unsigned long)(sc->sc_back - sc->sc_ring), 1841668329e9SPeter Wemm (unsigned long)(sc->sc_front - sc->sc_ring)); 1842b7e3f244SSam Leffler } 1843b7e3f244SSam Leffler 1844b7e3f244SSam Leffler static void 1845b7e3f244SSam Leffler safe_dump_request(struct safe_softc *sc, const char* tag, struct safe_ringentry *re) 1846b7e3f244SSam Leffler { 1847b7e3f244SSam Leffler int ix, nsegs; 1848b7e3f244SSam Leffler 1849b7e3f244SSam Leffler ix = re - sc->sc_ring; 1850b7e3f244SSam Leffler printf("%s: %p (%u): csr %x src %x dst %x sa %x len %x\n" 1851b7e3f244SSam Leffler , tag 1852b7e3f244SSam Leffler , re, ix 1853b7e3f244SSam Leffler , re->re_desc.d_csr 1854b7e3f244SSam Leffler , re->re_desc.d_src 1855b7e3f244SSam Leffler , re->re_desc.d_dst 1856b7e3f244SSam Leffler , re->re_desc.d_sa 1857b7e3f244SSam Leffler , re->re_desc.d_len 1858b7e3f244SSam Leffler ); 1859b7e3f244SSam Leffler if (re->re_src.nsegs > 1) { 1860b7e3f244SSam Leffler ix = (re->re_desc.d_src - sc->sc_spalloc.dma_paddr) / 1861b7e3f244SSam Leffler sizeof(struct safe_pdesc); 1862b7e3f244SSam Leffler for (nsegs = re->re_src.nsegs; nsegs; nsegs--) { 1863b7e3f244SSam Leffler printf(" spd[%u] %p: %p size %u flags %x" 1864b7e3f244SSam Leffler , ix, &sc->sc_spring[ix] 1865668329e9SPeter Wemm , (caddr_t)(uintptr_t) sc->sc_spring[ix].pd_addr 1866b7e3f244SSam Leffler , sc->sc_spring[ix].pd_size 1867b7e3f244SSam Leffler , sc->sc_spring[ix].pd_flags 1868b7e3f244SSam Leffler ); 1869b7e3f244SSam Leffler if (sc->sc_spring[ix].pd_size == 0) 1870b7e3f244SSam Leffler printf(" (zero!)"); 1871b7e3f244SSam Leffler printf("\n"); 1872b7e3f244SSam Leffler if (++ix == SAFE_TOTAL_SPART) 1873b7e3f244SSam Leffler ix = 0; 1874b7e3f244SSam Leffler } 1875b7e3f244SSam Leffler } 1876b7e3f244SSam Leffler if (re->re_dst.nsegs > 1) { 1877b7e3f244SSam Leffler ix = (re->re_desc.d_dst - sc->sc_dpalloc.dma_paddr) / 1878b7e3f244SSam Leffler sizeof(struct safe_pdesc); 1879b7e3f244SSam Leffler for (nsegs = re->re_dst.nsegs; nsegs; nsegs--) { 1880b7e3f244SSam Leffler printf(" dpd[%u] %p: %p flags %x\n" 1881b7e3f244SSam Leffler , ix, &sc->sc_dpring[ix] 1882668329e9SPeter Wemm , (caddr_t)(uintptr_t) sc->sc_dpring[ix].pd_addr 1883b7e3f244SSam Leffler , sc->sc_dpring[ix].pd_flags 1884b7e3f244SSam Leffler ); 1885b7e3f244SSam Leffler if (++ix == SAFE_TOTAL_DPART) 1886b7e3f244SSam Leffler ix = 0; 1887b7e3f244SSam Leffler } 1888b7e3f244SSam Leffler } 1889b7e3f244SSam Leffler printf("sa: cmd0 %08x cmd1 %08x staterec %x\n", 1890b7e3f244SSam Leffler re->re_sa.sa_cmd0, re->re_sa.sa_cmd1, re->re_sa.sa_staterec); 1891b7e3f244SSam Leffler printf("sa: key %x %x %x %x %x %x %x %x\n" 1892b7e3f244SSam Leffler , re->re_sa.sa_key[0] 1893b7e3f244SSam Leffler , re->re_sa.sa_key[1] 1894b7e3f244SSam Leffler , re->re_sa.sa_key[2] 1895b7e3f244SSam Leffler , re->re_sa.sa_key[3] 1896b7e3f244SSam Leffler , re->re_sa.sa_key[4] 1897b7e3f244SSam Leffler , re->re_sa.sa_key[5] 1898b7e3f244SSam Leffler , re->re_sa.sa_key[6] 1899b7e3f244SSam Leffler , re->re_sa.sa_key[7] 1900b7e3f244SSam Leffler ); 1901b7e3f244SSam Leffler printf("sa: indigest %x %x %x %x %x\n" 1902b7e3f244SSam Leffler , re->re_sa.sa_indigest[0] 1903b7e3f244SSam Leffler , re->re_sa.sa_indigest[1] 1904b7e3f244SSam Leffler , re->re_sa.sa_indigest[2] 1905b7e3f244SSam Leffler , re->re_sa.sa_indigest[3] 1906b7e3f244SSam Leffler , re->re_sa.sa_indigest[4] 1907b7e3f244SSam Leffler ); 1908b7e3f244SSam Leffler printf("sa: outdigest %x %x %x %x %x\n" 1909b7e3f244SSam Leffler , re->re_sa.sa_outdigest[0] 1910b7e3f244SSam Leffler , re->re_sa.sa_outdigest[1] 1911b7e3f244SSam Leffler , re->re_sa.sa_outdigest[2] 1912b7e3f244SSam Leffler , re->re_sa.sa_outdigest[3] 1913b7e3f244SSam Leffler , re->re_sa.sa_outdigest[4] 1914b7e3f244SSam Leffler ); 1915b7e3f244SSam Leffler printf("sr: iv %x %x %x %x\n" 1916b7e3f244SSam Leffler , re->re_sastate.sa_saved_iv[0] 1917b7e3f244SSam Leffler , re->re_sastate.sa_saved_iv[1] 1918b7e3f244SSam Leffler , re->re_sastate.sa_saved_iv[2] 1919b7e3f244SSam Leffler , re->re_sastate.sa_saved_iv[3] 1920b7e3f244SSam Leffler ); 1921b7e3f244SSam Leffler printf("sr: hashbc %u indigest %x %x %x %x %x\n" 1922b7e3f244SSam Leffler , re->re_sastate.sa_saved_hashbc 1923b7e3f244SSam Leffler , re->re_sastate.sa_saved_indigest[0] 1924b7e3f244SSam Leffler , re->re_sastate.sa_saved_indigest[1] 1925b7e3f244SSam Leffler , re->re_sastate.sa_saved_indigest[2] 1926b7e3f244SSam Leffler , re->re_sastate.sa_saved_indigest[3] 1927b7e3f244SSam Leffler , re->re_sastate.sa_saved_indigest[4] 1928b7e3f244SSam Leffler ); 1929b7e3f244SSam Leffler } 1930b7e3f244SSam Leffler 1931b7e3f244SSam Leffler static void 1932b7e3f244SSam Leffler safe_dump_ring(struct safe_softc *sc, const char *tag) 1933b7e3f244SSam Leffler { 1934b7e3f244SSam Leffler mtx_lock(&sc->sc_ringmtx); 1935b7e3f244SSam Leffler printf("\nSafeNet Ring State:\n"); 1936b7e3f244SSam Leffler safe_dump_intrstate(sc, tag); 1937b7e3f244SSam Leffler safe_dump_dmastatus(sc, tag); 1938b7e3f244SSam Leffler safe_dump_ringstate(sc, tag); 1939b7e3f244SSam Leffler if (sc->sc_nqchip) { 1940b7e3f244SSam Leffler struct safe_ringentry *re = sc->sc_back; 1941b7e3f244SSam Leffler do { 1942b7e3f244SSam Leffler safe_dump_request(sc, tag, re); 1943b7e3f244SSam Leffler if (++re == sc->sc_ringtop) 1944b7e3f244SSam Leffler re = sc->sc_ring; 1945b7e3f244SSam Leffler } while (re != sc->sc_front); 1946b7e3f244SSam Leffler } 1947b7e3f244SSam Leffler mtx_unlock(&sc->sc_ringmtx); 1948b7e3f244SSam Leffler } 1949b7e3f244SSam Leffler 1950b7e3f244SSam Leffler static int 1951b7e3f244SSam Leffler sysctl_hw_safe_dump(SYSCTL_HANDLER_ARGS) 1952b7e3f244SSam Leffler { 1953b7e3f244SSam Leffler char dmode[64]; 1954b7e3f244SSam Leffler int error; 1955b7e3f244SSam Leffler 1956b7e3f244SSam Leffler strncpy(dmode, "", sizeof(dmode) - 1); 1957b7e3f244SSam Leffler dmode[sizeof(dmode) - 1] = '\0'; 1958b7e3f244SSam Leffler error = sysctl_handle_string(oidp, &dmode[0], sizeof(dmode), req); 1959b7e3f244SSam Leffler 1960b7e3f244SSam Leffler if (error == 0 && req->newptr != NULL) { 1961b7e3f244SSam Leffler struct safe_softc *sc = safec; 1962b7e3f244SSam Leffler 1963b7e3f244SSam Leffler if (!sc) 1964b7e3f244SSam Leffler return EINVAL; 1965b7e3f244SSam Leffler if (strncmp(dmode, "dma", 3) == 0) 1966b7e3f244SSam Leffler safe_dump_dmastatus(sc, "safe0"); 1967b7e3f244SSam Leffler else if (strncmp(dmode, "int", 3) == 0) 1968b7e3f244SSam Leffler safe_dump_intrstate(sc, "safe0"); 1969b7e3f244SSam Leffler else if (strncmp(dmode, "ring", 4) == 0) 1970b7e3f244SSam Leffler safe_dump_ring(sc, "safe0"); 1971b7e3f244SSam Leffler else 1972b7e3f244SSam Leffler return EINVAL; 1973b7e3f244SSam Leffler } 1974b7e3f244SSam Leffler return error; 1975b7e3f244SSam Leffler } 19767029da5cSPawel Biernacki SYSCTL_PROC(_hw_safe, OID_AUTO, dump, 19777029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, 0, 19787029da5cSPawel Biernacki sysctl_hw_safe_dump, "A", 19797029da5cSPawel Biernacki "Dump driver state"); 1980b7e3f244SSam Leffler #endif /* SAFE_DEBUG */ 1981