18817e1bfSRafal Jaworowski /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
48817e1bfSRafal Jaworowski * Copyright (C) 2009-2011 Semihalf.
58817e1bfSRafal Jaworowski * All rights reserved.
68817e1bfSRafal Jaworowski *
78817e1bfSRafal Jaworowski * Redistribution and use in source and binary forms, with or without
88817e1bfSRafal Jaworowski * modification, are permitted provided that the following conditions
98817e1bfSRafal Jaworowski * are met:
108817e1bfSRafal Jaworowski * 1. Redistributions of source code must retain the above copyright
118817e1bfSRafal Jaworowski * notice, this list of conditions and the following disclaimer.
128817e1bfSRafal Jaworowski * 2. Redistributions in binary form must reproduce the above copyright
138817e1bfSRafal Jaworowski * notice, this list of conditions and the following disclaimer in the
148817e1bfSRafal Jaworowski * documentation and/or other materials provided with the distribution.
158817e1bfSRafal Jaworowski *
168817e1bfSRafal Jaworowski * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
178817e1bfSRafal Jaworowski * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188817e1bfSRafal Jaworowski * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198817e1bfSRafal Jaworowski * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
208817e1bfSRafal Jaworowski * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218817e1bfSRafal Jaworowski * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228817e1bfSRafal Jaworowski * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238817e1bfSRafal Jaworowski * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248817e1bfSRafal Jaworowski * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258817e1bfSRafal Jaworowski * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268817e1bfSRafal Jaworowski * SUCH DAMAGE.
278817e1bfSRafal Jaworowski */
288817e1bfSRafal Jaworowski
298817e1bfSRafal Jaworowski /*
308817e1bfSRafal Jaworowski * CESA SRAM Memory Map:
318817e1bfSRafal Jaworowski *
325188e3ccSZbigniew Bodek * +------------------------+ <= sc->sc_sram_base_va + CESA_SRAM_SIZE
338817e1bfSRafal Jaworowski * | |
348817e1bfSRafal Jaworowski * | DATA |
358817e1bfSRafal Jaworowski * | |
365188e3ccSZbigniew Bodek * +------------------------+ <= sc->sc_sram_base_va + CESA_DATA(0)
378817e1bfSRafal Jaworowski * | struct cesa_sa_data |
388817e1bfSRafal Jaworowski * +------------------------+
398817e1bfSRafal Jaworowski * | struct cesa_sa_hdesc |
405188e3ccSZbigniew Bodek * +------------------------+ <= sc->sc_sram_base_va
418817e1bfSRafal Jaworowski */
428817e1bfSRafal Jaworowski
438817e1bfSRafal Jaworowski #include <sys/param.h>
448817e1bfSRafal Jaworowski #include <sys/systm.h>
458817e1bfSRafal Jaworowski #include <sys/bus.h>
468817e1bfSRafal Jaworowski #include <sys/endian.h>
478817e1bfSRafal Jaworowski #include <sys/kernel.h>
488817e1bfSRafal Jaworowski #include <sys/lock.h>
498817e1bfSRafal Jaworowski #include <sys/mbuf.h>
508817e1bfSRafal Jaworowski #include <sys/module.h>
518817e1bfSRafal Jaworowski #include <sys/mutex.h>
528817e1bfSRafal Jaworowski #include <sys/rman.h>
538817e1bfSRafal Jaworowski
548817e1bfSRafal Jaworowski #include <machine/bus.h>
558817e1bfSRafal Jaworowski #include <machine/intr.h>
568817e1bfSRafal Jaworowski #include <machine/resource.h>
575188e3ccSZbigniew Bodek #include <machine/fdt.h>
588817e1bfSRafal Jaworowski
59e9e2a7c1SMarcin Wojtas #include <dev/fdt/simplebus.h>
608817e1bfSRafal Jaworowski #include <dev/fdt/fdt_common.h>
618817e1bfSRafal Jaworowski #include <dev/ofw/ofw_bus.h>
628817e1bfSRafal Jaworowski #include <dev/ofw/ofw_bus_subr.h>
638817e1bfSRafal Jaworowski
648817e1bfSRafal Jaworowski #include <crypto/sha1.h>
65b07df6e5SZbigniew Bodek #include <crypto/sha2/sha256.h>
668817e1bfSRafal Jaworowski #include <crypto/rijndael/rijndael.h>
678817e1bfSRafal Jaworowski #include <opencrypto/cryptodev.h>
68c0341432SJohn Baldwin #include <opencrypto/xform.h>
698817e1bfSRafal Jaworowski #include "cryptodev_if.h"
708817e1bfSRafal Jaworowski
718817e1bfSRafal Jaworowski #include <arm/mv/mvreg.h>
728817e1bfSRafal Jaworowski #include <arm/mv/mvvar.h>
738817e1bfSRafal Jaworowski #include "cesa.h"
748817e1bfSRafal Jaworowski
758817e1bfSRafal Jaworowski static int cesa_probe(device_t);
768817e1bfSRafal Jaworowski static int cesa_attach(device_t);
77e9e2a7c1SMarcin Wojtas static int cesa_attach_late(device_t);
788817e1bfSRafal Jaworowski static int cesa_detach(device_t);
798817e1bfSRafal Jaworowski static void cesa_intr(void *);
80c0341432SJohn Baldwin static int cesa_probesession(device_t,
81c0341432SJohn Baldwin const struct crypto_session_params *);
82c0341432SJohn Baldwin static int cesa_newsession(device_t, crypto_session_t,
83c0341432SJohn Baldwin const struct crypto_session_params *);
848817e1bfSRafal Jaworowski static int cesa_process(device_t, struct cryptop *, int);
858817e1bfSRafal Jaworowski
868817e1bfSRafal Jaworowski static struct resource_spec cesa_res_spec[] = {
878817e1bfSRafal Jaworowski { SYS_RES_MEMORY, 0, RF_ACTIVE },
8822c7a031SZbigniew Bodek { SYS_RES_MEMORY, 1, RF_ACTIVE },
898817e1bfSRafal Jaworowski { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE },
908817e1bfSRafal Jaworowski { -1, 0 }
918817e1bfSRafal Jaworowski };
928817e1bfSRafal Jaworowski
938817e1bfSRafal Jaworowski static device_method_t cesa_methods[] = {
948817e1bfSRafal Jaworowski /* Device interface */
958817e1bfSRafal Jaworowski DEVMETHOD(device_probe, cesa_probe),
968817e1bfSRafal Jaworowski DEVMETHOD(device_attach, cesa_attach),
978817e1bfSRafal Jaworowski DEVMETHOD(device_detach, cesa_detach),
988817e1bfSRafal Jaworowski
998817e1bfSRafal Jaworowski /* Crypto device methods */
100c0341432SJohn Baldwin DEVMETHOD(cryptodev_probesession, cesa_probesession),
1018817e1bfSRafal Jaworowski DEVMETHOD(cryptodev_newsession, cesa_newsession),
1028817e1bfSRafal Jaworowski DEVMETHOD(cryptodev_process, cesa_process),
1038817e1bfSRafal Jaworowski
1044b7ec270SMarius Strobl DEVMETHOD_END
1058817e1bfSRafal Jaworowski };
1068817e1bfSRafal Jaworowski
1078817e1bfSRafal Jaworowski static driver_t cesa_driver = {
1088817e1bfSRafal Jaworowski "cesa",
1098817e1bfSRafal Jaworowski cesa_methods,
1108817e1bfSRafal Jaworowski sizeof (struct cesa_softc)
1118817e1bfSRafal Jaworowski };
1128817e1bfSRafal Jaworowski
1133c77ca68SJohn Baldwin DRIVER_MODULE(cesa, simplebus, cesa_driver, 0, 0);
1148817e1bfSRafal Jaworowski MODULE_DEPEND(cesa, crypto, 1, 1, 1);
1158817e1bfSRafal Jaworowski
1168817e1bfSRafal Jaworowski static void
cesa_dump_cshd(struct cesa_softc * sc,struct cesa_sa_hdesc * cshd)1178817e1bfSRafal Jaworowski cesa_dump_cshd(struct cesa_softc *sc, struct cesa_sa_hdesc *cshd)
1188817e1bfSRafal Jaworowski {
1198817e1bfSRafal Jaworowski #ifdef DEBUG
1208817e1bfSRafal Jaworowski device_t dev;
1218817e1bfSRafal Jaworowski
1228817e1bfSRafal Jaworowski dev = sc->sc_dev;
1238817e1bfSRafal Jaworowski device_printf(dev, "CESA SA Hardware Descriptor:\n");
1248817e1bfSRafal Jaworowski device_printf(dev, "\t\tconfig: 0x%08X\n", cshd->cshd_config);
1258817e1bfSRafal Jaworowski device_printf(dev, "\t\te_src: 0x%08X\n", cshd->cshd_enc_src);
1268817e1bfSRafal Jaworowski device_printf(dev, "\t\te_dst: 0x%08X\n", cshd->cshd_enc_dst);
1278817e1bfSRafal Jaworowski device_printf(dev, "\t\te_dlen: 0x%08X\n", cshd->cshd_enc_dlen);
1288817e1bfSRafal Jaworowski device_printf(dev, "\t\te_key: 0x%08X\n", cshd->cshd_enc_key);
1298817e1bfSRafal Jaworowski device_printf(dev, "\t\te_iv_1: 0x%08X\n", cshd->cshd_enc_iv);
1308817e1bfSRafal Jaworowski device_printf(dev, "\t\te_iv_2: 0x%08X\n", cshd->cshd_enc_iv_buf);
1318817e1bfSRafal Jaworowski device_printf(dev, "\t\tm_src: 0x%08X\n", cshd->cshd_mac_src);
1328817e1bfSRafal Jaworowski device_printf(dev, "\t\tm_dst: 0x%08X\n", cshd->cshd_mac_dst);
1338817e1bfSRafal Jaworowski device_printf(dev, "\t\tm_dlen: 0x%08X\n", cshd->cshd_mac_dlen);
1348817e1bfSRafal Jaworowski device_printf(dev, "\t\tm_tlen: 0x%08X\n", cshd->cshd_mac_total_dlen);
1358817e1bfSRafal Jaworowski device_printf(dev, "\t\tm_iv_i: 0x%08X\n", cshd->cshd_mac_iv_in);
1368817e1bfSRafal Jaworowski device_printf(dev, "\t\tm_iv_o: 0x%08X\n", cshd->cshd_mac_iv_out);
1378817e1bfSRafal Jaworowski #endif
1388817e1bfSRafal Jaworowski }
1398817e1bfSRafal Jaworowski
1408817e1bfSRafal Jaworowski static void
cesa_alloc_dma_mem_cb(void * arg,bus_dma_segment_t * segs,int nseg,int error)1418817e1bfSRafal Jaworowski cesa_alloc_dma_mem_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1428817e1bfSRafal Jaworowski {
1438817e1bfSRafal Jaworowski struct cesa_dma_mem *cdm;
1448817e1bfSRafal Jaworowski
1458817e1bfSRafal Jaworowski if (error)
1468817e1bfSRafal Jaworowski return;
1478817e1bfSRafal Jaworowski
1488817e1bfSRafal Jaworowski KASSERT(nseg == 1, ("Got wrong number of DMA segments, should be 1."));
1498817e1bfSRafal Jaworowski cdm = arg;
1508817e1bfSRafal Jaworowski cdm->cdm_paddr = segs->ds_addr;
1518817e1bfSRafal Jaworowski }
1528817e1bfSRafal Jaworowski
1538817e1bfSRafal Jaworowski static int
cesa_alloc_dma_mem(struct cesa_softc * sc,struct cesa_dma_mem * cdm,bus_size_t size)1548817e1bfSRafal Jaworowski cesa_alloc_dma_mem(struct cesa_softc *sc, struct cesa_dma_mem *cdm,
1558817e1bfSRafal Jaworowski bus_size_t size)
1568817e1bfSRafal Jaworowski {
1578817e1bfSRafal Jaworowski int error;
1588817e1bfSRafal Jaworowski
1598817e1bfSRafal Jaworowski KASSERT(cdm->cdm_vaddr == NULL,
1608817e1bfSRafal Jaworowski ("%s(): DMA memory descriptor in use.", __func__));
1618817e1bfSRafal Jaworowski
1626ac6f295SScott Long error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), /* parent */
1638817e1bfSRafal Jaworowski PAGE_SIZE, 0, /* alignment, boundary */
1648817e1bfSRafal Jaworowski BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1658817e1bfSRafal Jaworowski BUS_SPACE_MAXADDR, /* highaddr */
1668817e1bfSRafal Jaworowski NULL, NULL, /* filtfunc, filtfuncarg */
1678817e1bfSRafal Jaworowski size, 1, /* maxsize, nsegments */
1688817e1bfSRafal Jaworowski size, 0, /* maxsegsz, flags */
1698817e1bfSRafal Jaworowski NULL, NULL, /* lockfunc, lockfuncarg */
1708817e1bfSRafal Jaworowski &cdm->cdm_tag); /* dmat */
1718817e1bfSRafal Jaworowski if (error) {
1728817e1bfSRafal Jaworowski device_printf(sc->sc_dev, "failed to allocate busdma tag, error"
1738817e1bfSRafal Jaworowski " %i!\n", error);
1748817e1bfSRafal Jaworowski
1758817e1bfSRafal Jaworowski goto err1;
1768817e1bfSRafal Jaworowski }
1778817e1bfSRafal Jaworowski
1788817e1bfSRafal Jaworowski error = bus_dmamem_alloc(cdm->cdm_tag, &cdm->cdm_vaddr,
1798817e1bfSRafal Jaworowski BUS_DMA_NOWAIT | BUS_DMA_ZERO, &cdm->cdm_map);
1808817e1bfSRafal Jaworowski if (error) {
1818817e1bfSRafal Jaworowski device_printf(sc->sc_dev, "failed to allocate DMA safe"
1828817e1bfSRafal Jaworowski " memory, error %i!\n", error);
1838817e1bfSRafal Jaworowski
1848817e1bfSRafal Jaworowski goto err2;
1858817e1bfSRafal Jaworowski }
1868817e1bfSRafal Jaworowski
1878817e1bfSRafal Jaworowski error = bus_dmamap_load(cdm->cdm_tag, cdm->cdm_map, cdm->cdm_vaddr,
1888817e1bfSRafal Jaworowski size, cesa_alloc_dma_mem_cb, cdm, BUS_DMA_NOWAIT);
1898817e1bfSRafal Jaworowski if (error) {
1908817e1bfSRafal Jaworowski device_printf(sc->sc_dev, "cannot get address of the DMA"
1918817e1bfSRafal Jaworowski " memory, error %i\n", error);
1928817e1bfSRafal Jaworowski
1938817e1bfSRafal Jaworowski goto err3;
1948817e1bfSRafal Jaworowski }
1958817e1bfSRafal Jaworowski
1968817e1bfSRafal Jaworowski return (0);
1978817e1bfSRafal Jaworowski err3:
1988817e1bfSRafal Jaworowski bus_dmamem_free(cdm->cdm_tag, cdm->cdm_vaddr, cdm->cdm_map);
1998817e1bfSRafal Jaworowski err2:
2008817e1bfSRafal Jaworowski bus_dma_tag_destroy(cdm->cdm_tag);
2018817e1bfSRafal Jaworowski err1:
2028817e1bfSRafal Jaworowski cdm->cdm_vaddr = NULL;
2038817e1bfSRafal Jaworowski return (error);
2048817e1bfSRafal Jaworowski }
2058817e1bfSRafal Jaworowski
2068817e1bfSRafal Jaworowski static void
cesa_free_dma_mem(struct cesa_dma_mem * cdm)2078817e1bfSRafal Jaworowski cesa_free_dma_mem(struct cesa_dma_mem *cdm)
2088817e1bfSRafal Jaworowski {
2098817e1bfSRafal Jaworowski
2108817e1bfSRafal Jaworowski bus_dmamap_unload(cdm->cdm_tag, cdm->cdm_map);
2118817e1bfSRafal Jaworowski bus_dmamem_free(cdm->cdm_tag, cdm->cdm_vaddr, cdm->cdm_map);
2128817e1bfSRafal Jaworowski bus_dma_tag_destroy(cdm->cdm_tag);
2138817e1bfSRafal Jaworowski cdm->cdm_vaddr = NULL;
2148817e1bfSRafal Jaworowski }
2158817e1bfSRafal Jaworowski
2168817e1bfSRafal Jaworowski static void
cesa_sync_dma_mem(struct cesa_dma_mem * cdm,bus_dmasync_op_t op)2178817e1bfSRafal Jaworowski cesa_sync_dma_mem(struct cesa_dma_mem *cdm, bus_dmasync_op_t op)
2188817e1bfSRafal Jaworowski {
2198817e1bfSRafal Jaworowski
2208817e1bfSRafal Jaworowski /* Sync only if dma memory is valid */
2218817e1bfSRafal Jaworowski if (cdm->cdm_vaddr != NULL)
2228817e1bfSRafal Jaworowski bus_dmamap_sync(cdm->cdm_tag, cdm->cdm_map, op);
2238817e1bfSRafal Jaworowski }
2248817e1bfSRafal Jaworowski
2258817e1bfSRafal Jaworowski static void
cesa_sync_desc(struct cesa_softc * sc,bus_dmasync_op_t op)2268817e1bfSRafal Jaworowski cesa_sync_desc(struct cesa_softc *sc, bus_dmasync_op_t op)
2278817e1bfSRafal Jaworowski {
2288817e1bfSRafal Jaworowski
2298817e1bfSRafal Jaworowski cesa_sync_dma_mem(&sc->sc_tdesc_cdm, op);
2308817e1bfSRafal Jaworowski cesa_sync_dma_mem(&sc->sc_sdesc_cdm, op);
2318817e1bfSRafal Jaworowski cesa_sync_dma_mem(&sc->sc_requests_cdm, op);
2328817e1bfSRafal Jaworowski }
2338817e1bfSRafal Jaworowski
2348817e1bfSRafal Jaworowski static struct cesa_request *
cesa_alloc_request(struct cesa_softc * sc)2358817e1bfSRafal Jaworowski cesa_alloc_request(struct cesa_softc *sc)
2368817e1bfSRafal Jaworowski {
2378817e1bfSRafal Jaworowski struct cesa_request *cr;
2388817e1bfSRafal Jaworowski
2398817e1bfSRafal Jaworowski CESA_GENERIC_ALLOC_LOCKED(sc, cr, requests);
2408817e1bfSRafal Jaworowski if (!cr)
2418817e1bfSRafal Jaworowski return (NULL);
2428817e1bfSRafal Jaworowski
2438817e1bfSRafal Jaworowski STAILQ_INIT(&cr->cr_tdesc);
2448817e1bfSRafal Jaworowski STAILQ_INIT(&cr->cr_sdesc);
2458817e1bfSRafal Jaworowski
2468817e1bfSRafal Jaworowski return (cr);
2478817e1bfSRafal Jaworowski }
2488817e1bfSRafal Jaworowski
2498817e1bfSRafal Jaworowski static void
cesa_free_request(struct cesa_softc * sc,struct cesa_request * cr)2508817e1bfSRafal Jaworowski cesa_free_request(struct cesa_softc *sc, struct cesa_request *cr)
2518817e1bfSRafal Jaworowski {
2528817e1bfSRafal Jaworowski
2538817e1bfSRafal Jaworowski /* Free TDMA descriptors assigned to this request */
2548817e1bfSRafal Jaworowski CESA_LOCK(sc, tdesc);
2558817e1bfSRafal Jaworowski STAILQ_CONCAT(&sc->sc_free_tdesc, &cr->cr_tdesc);
2568817e1bfSRafal Jaworowski CESA_UNLOCK(sc, tdesc);
2578817e1bfSRafal Jaworowski
2588817e1bfSRafal Jaworowski /* Free SA descriptors assigned to this request */
2598817e1bfSRafal Jaworowski CESA_LOCK(sc, sdesc);
2608817e1bfSRafal Jaworowski STAILQ_CONCAT(&sc->sc_free_sdesc, &cr->cr_sdesc);
2618817e1bfSRafal Jaworowski CESA_UNLOCK(sc, sdesc);
2628817e1bfSRafal Jaworowski
263453130d9SPedro F. Giffuni /* Unload DMA memory associated with request */
2648817e1bfSRafal Jaworowski if (cr->cr_dmap_loaded) {
2658817e1bfSRafal Jaworowski bus_dmamap_unload(sc->sc_data_dtag, cr->cr_dmap);
2668817e1bfSRafal Jaworowski cr->cr_dmap_loaded = 0;
2678817e1bfSRafal Jaworowski }
2688817e1bfSRafal Jaworowski
2698817e1bfSRafal Jaworowski CESA_GENERIC_FREE_LOCKED(sc, cr, requests);
2708817e1bfSRafal Jaworowski }
2718817e1bfSRafal Jaworowski
2728817e1bfSRafal Jaworowski static void
cesa_enqueue_request(struct cesa_softc * sc,struct cesa_request * cr)2738817e1bfSRafal Jaworowski cesa_enqueue_request(struct cesa_softc *sc, struct cesa_request *cr)
2748817e1bfSRafal Jaworowski {
2758817e1bfSRafal Jaworowski
2768817e1bfSRafal Jaworowski CESA_LOCK(sc, requests);
2778817e1bfSRafal Jaworowski STAILQ_INSERT_TAIL(&sc->sc_ready_requests, cr, cr_stq);
2788817e1bfSRafal Jaworowski CESA_UNLOCK(sc, requests);
2798817e1bfSRafal Jaworowski }
2808817e1bfSRafal Jaworowski
2818817e1bfSRafal Jaworowski static struct cesa_tdma_desc *
cesa_alloc_tdesc(struct cesa_softc * sc)2828817e1bfSRafal Jaworowski cesa_alloc_tdesc(struct cesa_softc *sc)
2838817e1bfSRafal Jaworowski {
2848817e1bfSRafal Jaworowski struct cesa_tdma_desc *ctd;
2858817e1bfSRafal Jaworowski
2868817e1bfSRafal Jaworowski CESA_GENERIC_ALLOC_LOCKED(sc, ctd, tdesc);
2878817e1bfSRafal Jaworowski
2888817e1bfSRafal Jaworowski if (!ctd)
2898817e1bfSRafal Jaworowski device_printf(sc->sc_dev, "TDMA descriptors pool exhaused. "
2908817e1bfSRafal Jaworowski "Consider increasing CESA_TDMA_DESCRIPTORS.\n");
2918817e1bfSRafal Jaworowski
2928817e1bfSRafal Jaworowski return (ctd);
2938817e1bfSRafal Jaworowski }
2948817e1bfSRafal Jaworowski
2958817e1bfSRafal Jaworowski static struct cesa_sa_desc *
cesa_alloc_sdesc(struct cesa_softc * sc,struct cesa_request * cr)2968817e1bfSRafal Jaworowski cesa_alloc_sdesc(struct cesa_softc *sc, struct cesa_request *cr)
2978817e1bfSRafal Jaworowski {
2988817e1bfSRafal Jaworowski struct cesa_sa_desc *csd;
2998817e1bfSRafal Jaworowski
3008817e1bfSRafal Jaworowski CESA_GENERIC_ALLOC_LOCKED(sc, csd, sdesc);
3018817e1bfSRafal Jaworowski if (!csd) {
3028817e1bfSRafal Jaworowski device_printf(sc->sc_dev, "SA descriptors pool exhaused. "
3038817e1bfSRafal Jaworowski "Consider increasing CESA_SA_DESCRIPTORS.\n");
3048817e1bfSRafal Jaworowski return (NULL);
3058817e1bfSRafal Jaworowski }
3068817e1bfSRafal Jaworowski
3078817e1bfSRafal Jaworowski STAILQ_INSERT_TAIL(&cr->cr_sdesc, csd, csd_stq);
3088817e1bfSRafal Jaworowski
3098817e1bfSRafal Jaworowski /* Fill-in SA descriptor with default values */
3108817e1bfSRafal Jaworowski csd->csd_cshd->cshd_enc_key = CESA_SA_DATA(csd_key);
3118817e1bfSRafal Jaworowski csd->csd_cshd->cshd_enc_iv = CESA_SA_DATA(csd_iv);
3128817e1bfSRafal Jaworowski csd->csd_cshd->cshd_enc_iv_buf = CESA_SA_DATA(csd_iv);
3138817e1bfSRafal Jaworowski csd->csd_cshd->cshd_enc_src = 0;
3148817e1bfSRafal Jaworowski csd->csd_cshd->cshd_enc_dst = 0;
3158817e1bfSRafal Jaworowski csd->csd_cshd->cshd_enc_dlen = 0;
3168817e1bfSRafal Jaworowski csd->csd_cshd->cshd_mac_dst = CESA_SA_DATA(csd_hash);
3178817e1bfSRafal Jaworowski csd->csd_cshd->cshd_mac_iv_in = CESA_SA_DATA(csd_hiv_in);
3188817e1bfSRafal Jaworowski csd->csd_cshd->cshd_mac_iv_out = CESA_SA_DATA(csd_hiv_out);
3198817e1bfSRafal Jaworowski csd->csd_cshd->cshd_mac_src = 0;
3208817e1bfSRafal Jaworowski csd->csd_cshd->cshd_mac_dlen = 0;
3218817e1bfSRafal Jaworowski
3228817e1bfSRafal Jaworowski return (csd);
3238817e1bfSRafal Jaworowski }
3248817e1bfSRafal Jaworowski
3258817e1bfSRafal Jaworowski static struct cesa_tdma_desc *
cesa_tdma_copy(struct cesa_softc * sc,bus_addr_t dst,bus_addr_t src,bus_size_t size)3268817e1bfSRafal Jaworowski cesa_tdma_copy(struct cesa_softc *sc, bus_addr_t dst, bus_addr_t src,
3278817e1bfSRafal Jaworowski bus_size_t size)
3288817e1bfSRafal Jaworowski {
3298817e1bfSRafal Jaworowski struct cesa_tdma_desc *ctd;
3308817e1bfSRafal Jaworowski
3318817e1bfSRafal Jaworowski ctd = cesa_alloc_tdesc(sc);
3328817e1bfSRafal Jaworowski if (!ctd)
3338817e1bfSRafal Jaworowski return (NULL);
3348817e1bfSRafal Jaworowski
3358817e1bfSRafal Jaworowski ctd->ctd_cthd->cthd_dst = dst;
3368817e1bfSRafal Jaworowski ctd->ctd_cthd->cthd_src = src;
3378817e1bfSRafal Jaworowski ctd->ctd_cthd->cthd_byte_count = size;
3388817e1bfSRafal Jaworowski
3398817e1bfSRafal Jaworowski /* Handle special control packet */
3408817e1bfSRafal Jaworowski if (size != 0)
3418817e1bfSRafal Jaworowski ctd->ctd_cthd->cthd_flags = CESA_CTHD_OWNED;
3428817e1bfSRafal Jaworowski else
3438817e1bfSRafal Jaworowski ctd->ctd_cthd->cthd_flags = 0;
3448817e1bfSRafal Jaworowski
3458817e1bfSRafal Jaworowski return (ctd);
3468817e1bfSRafal Jaworowski }
3478817e1bfSRafal Jaworowski
3488817e1bfSRafal Jaworowski static struct cesa_tdma_desc *
cesa_tdma_copyin_sa_data(struct cesa_softc * sc,struct cesa_request * cr)3498817e1bfSRafal Jaworowski cesa_tdma_copyin_sa_data(struct cesa_softc *sc, struct cesa_request *cr)
3508817e1bfSRafal Jaworowski {
3518817e1bfSRafal Jaworowski
3525188e3ccSZbigniew Bodek return (cesa_tdma_copy(sc, sc->sc_sram_base_pa +
3538817e1bfSRafal Jaworowski sizeof(struct cesa_sa_hdesc), cr->cr_csd_paddr,
3548817e1bfSRafal Jaworowski sizeof(struct cesa_sa_data)));
3558817e1bfSRafal Jaworowski }
3568817e1bfSRafal Jaworowski
3578817e1bfSRafal Jaworowski static struct cesa_tdma_desc *
cesa_tdma_copyout_sa_data(struct cesa_softc * sc,struct cesa_request * cr)3588817e1bfSRafal Jaworowski cesa_tdma_copyout_sa_data(struct cesa_softc *sc, struct cesa_request *cr)
3598817e1bfSRafal Jaworowski {
3608817e1bfSRafal Jaworowski
3615188e3ccSZbigniew Bodek return (cesa_tdma_copy(sc, cr->cr_csd_paddr, sc->sc_sram_base_pa +
3628817e1bfSRafal Jaworowski sizeof(struct cesa_sa_hdesc), sizeof(struct cesa_sa_data)));
3638817e1bfSRafal Jaworowski }
3648817e1bfSRafal Jaworowski
3658817e1bfSRafal Jaworowski static struct cesa_tdma_desc *
cesa_tdma_copy_sdesc(struct cesa_softc * sc,struct cesa_sa_desc * csd)3668817e1bfSRafal Jaworowski cesa_tdma_copy_sdesc(struct cesa_softc *sc, struct cesa_sa_desc *csd)
3678817e1bfSRafal Jaworowski {
3688817e1bfSRafal Jaworowski
3695188e3ccSZbigniew Bodek return (cesa_tdma_copy(sc, sc->sc_sram_base_pa, csd->csd_cshd_paddr,
3708817e1bfSRafal Jaworowski sizeof(struct cesa_sa_hdesc)));
3718817e1bfSRafal Jaworowski }
3728817e1bfSRafal Jaworowski
3738817e1bfSRafal Jaworowski static void
cesa_append_tdesc(struct cesa_request * cr,struct cesa_tdma_desc * ctd)3748817e1bfSRafal Jaworowski cesa_append_tdesc(struct cesa_request *cr, struct cesa_tdma_desc *ctd)
3758817e1bfSRafal Jaworowski {
3768817e1bfSRafal Jaworowski struct cesa_tdma_desc *ctd_prev;
3778817e1bfSRafal Jaworowski
3788817e1bfSRafal Jaworowski if (!STAILQ_EMPTY(&cr->cr_tdesc)) {
3798817e1bfSRafal Jaworowski ctd_prev = STAILQ_LAST(&cr->cr_tdesc, cesa_tdma_desc, ctd_stq);
3808817e1bfSRafal Jaworowski ctd_prev->ctd_cthd->cthd_next = ctd->ctd_cthd_paddr;
3818817e1bfSRafal Jaworowski }
3828817e1bfSRafal Jaworowski
3838817e1bfSRafal Jaworowski ctd->ctd_cthd->cthd_next = 0;
3848817e1bfSRafal Jaworowski STAILQ_INSERT_TAIL(&cr->cr_tdesc, ctd, ctd_stq);
3858817e1bfSRafal Jaworowski }
3868817e1bfSRafal Jaworowski
3878817e1bfSRafal Jaworowski static int
cesa_append_packet(struct cesa_softc * sc,struct cesa_request * cr,struct cesa_packet * cp,struct cesa_sa_desc * csd)3888817e1bfSRafal Jaworowski cesa_append_packet(struct cesa_softc *sc, struct cesa_request *cr,
3898817e1bfSRafal Jaworowski struct cesa_packet *cp, struct cesa_sa_desc *csd)
3908817e1bfSRafal Jaworowski {
3918817e1bfSRafal Jaworowski struct cesa_tdma_desc *ctd, *tmp;
3928817e1bfSRafal Jaworowski
3938817e1bfSRafal Jaworowski /* Copy SA descriptor for this packet */
3948817e1bfSRafal Jaworowski ctd = cesa_tdma_copy_sdesc(sc, csd);
3958817e1bfSRafal Jaworowski if (!ctd)
3968817e1bfSRafal Jaworowski return (ENOMEM);
3978817e1bfSRafal Jaworowski
3988817e1bfSRafal Jaworowski cesa_append_tdesc(cr, ctd);
3998817e1bfSRafal Jaworowski
4008817e1bfSRafal Jaworowski /* Copy data to be processed */
4018817e1bfSRafal Jaworowski STAILQ_FOREACH_SAFE(ctd, &cp->cp_copyin, ctd_stq, tmp)
4028817e1bfSRafal Jaworowski cesa_append_tdesc(cr, ctd);
4038817e1bfSRafal Jaworowski STAILQ_INIT(&cp->cp_copyin);
4048817e1bfSRafal Jaworowski
4058817e1bfSRafal Jaworowski /* Insert control descriptor */
4068817e1bfSRafal Jaworowski ctd = cesa_tdma_copy(sc, 0, 0, 0);
4078817e1bfSRafal Jaworowski if (!ctd)
4088817e1bfSRafal Jaworowski return (ENOMEM);
4098817e1bfSRafal Jaworowski
4108817e1bfSRafal Jaworowski cesa_append_tdesc(cr, ctd);
4118817e1bfSRafal Jaworowski
4128817e1bfSRafal Jaworowski /* Copy back results */
4138817e1bfSRafal Jaworowski STAILQ_FOREACH_SAFE(ctd, &cp->cp_copyout, ctd_stq, tmp)
4148817e1bfSRafal Jaworowski cesa_append_tdesc(cr, ctd);
4158817e1bfSRafal Jaworowski STAILQ_INIT(&cp->cp_copyout);
4168817e1bfSRafal Jaworowski
4178817e1bfSRafal Jaworowski return (0);
4188817e1bfSRafal Jaworowski }
4198817e1bfSRafal Jaworowski
420c0341432SJohn Baldwin static void
cesa_set_mkey(struct cesa_session * cs,int alg,const uint8_t * mkey,int mklen)4218817e1bfSRafal Jaworowski cesa_set_mkey(struct cesa_session *cs, int alg, const uint8_t *mkey, int mklen)
4228817e1bfSRafal Jaworowski {
423c0341432SJohn Baldwin union authctx auth_ctx;
4248817e1bfSRafal Jaworowski uint32_t *hout;
4258817e1bfSRafal Jaworowski uint32_t *hin;
4268817e1bfSRafal Jaworowski int i;
4278817e1bfSRafal Jaworowski
4288817e1bfSRafal Jaworowski hin = (uint32_t *)cs->cs_hiv_in;
4298817e1bfSRafal Jaworowski hout = (uint32_t *)cs->cs_hiv_out;
4308817e1bfSRafal Jaworowski
4318817e1bfSRafal Jaworowski switch (alg) {
4328817e1bfSRafal Jaworowski case CRYPTO_SHA1_HMAC:
433c0341432SJohn Baldwin hmac_init_ipad(&auth_hash_hmac_sha1, mkey, mklen, &auth_ctx);
434c0341432SJohn Baldwin memcpy(hin, auth_ctx.sha1ctx.h.b32,
435c0341432SJohn Baldwin sizeof(auth_ctx.sha1ctx.h.b32));
436c0341432SJohn Baldwin hmac_init_opad(&auth_hash_hmac_sha1, mkey, mklen, &auth_ctx);
437c0341432SJohn Baldwin memcpy(hout, auth_ctx.sha1ctx.h.b32,
438c0341432SJohn Baldwin sizeof(auth_ctx.sha1ctx.h.b32));
4398817e1bfSRafal Jaworowski break;
440b07df6e5SZbigniew Bodek case CRYPTO_SHA2_256_HMAC:
441c0341432SJohn Baldwin hmac_init_ipad(&auth_hash_hmac_sha2_256, mkey, mklen,
442c0341432SJohn Baldwin &auth_ctx);
443c0341432SJohn Baldwin memcpy(hin, auth_ctx.sha256ctx.state,
444c0341432SJohn Baldwin sizeof(auth_ctx.sha256ctx.state));
445c0341432SJohn Baldwin hmac_init_opad(&auth_hash_hmac_sha2_256, mkey, mklen,
446c0341432SJohn Baldwin &auth_ctx);
447c0341432SJohn Baldwin memcpy(hout, auth_ctx.sha256ctx.state,
448c0341432SJohn Baldwin sizeof(auth_ctx.sha256ctx.state));
449b07df6e5SZbigniew Bodek break;
4508817e1bfSRafal Jaworowski default:
451c0341432SJohn Baldwin panic("shouldn't get here");
4528817e1bfSRafal Jaworowski }
4538817e1bfSRafal Jaworowski
4548817e1bfSRafal Jaworowski for (i = 0; i < CESA_MAX_HASH_LEN / sizeof(uint32_t); i++) {
4558817e1bfSRafal Jaworowski hin[i] = htobe32(hin[i]);
4568817e1bfSRafal Jaworowski hout[i] = htobe32(hout[i]);
4578817e1bfSRafal Jaworowski }
4589b6dc281SJohn Baldwin explicit_bzero(&auth_ctx, sizeof(auth_ctx));
4598817e1bfSRafal Jaworowski }
4608817e1bfSRafal Jaworowski
4618817e1bfSRafal Jaworowski static int
cesa_prep_aes_key(struct cesa_session * cs,const struct crypto_session_params * csp)462c0341432SJohn Baldwin cesa_prep_aes_key(struct cesa_session *cs,
463c0341432SJohn Baldwin const struct crypto_session_params *csp)
4648817e1bfSRafal Jaworowski {
4658817e1bfSRafal Jaworowski uint32_t ek[4 * (RIJNDAEL_MAXNR + 1)];
4668817e1bfSRafal Jaworowski uint32_t *dkey;
4678817e1bfSRafal Jaworowski int i;
4688817e1bfSRafal Jaworowski
469c0341432SJohn Baldwin rijndaelKeySetupEnc(ek, cs->cs_key, csp->csp_cipher_klen * 8);
4708817e1bfSRafal Jaworowski
4718817e1bfSRafal Jaworowski cs->cs_config &= ~CESA_CSH_AES_KLEN_MASK;
4728817e1bfSRafal Jaworowski dkey = (uint32_t *)cs->cs_aes_dkey;
4738817e1bfSRafal Jaworowski
474c0341432SJohn Baldwin switch (csp->csp_cipher_klen) {
4758817e1bfSRafal Jaworowski case 16:
4768817e1bfSRafal Jaworowski cs->cs_config |= CESA_CSH_AES_KLEN_128;
4778817e1bfSRafal Jaworowski for (i = 0; i < 4; i++)
4788817e1bfSRafal Jaworowski *dkey++ = htobe32(ek[4 * 10 + i]);
4798817e1bfSRafal Jaworowski break;
4808817e1bfSRafal Jaworowski case 24:
4818817e1bfSRafal Jaworowski cs->cs_config |= CESA_CSH_AES_KLEN_192;
4828817e1bfSRafal Jaworowski for (i = 0; i < 4; i++)
4838817e1bfSRafal Jaworowski *dkey++ = htobe32(ek[4 * 12 + i]);
4848817e1bfSRafal Jaworowski for (i = 0; i < 2; i++)
4858817e1bfSRafal Jaworowski *dkey++ = htobe32(ek[4 * 11 + 2 + i]);
4868817e1bfSRafal Jaworowski break;
4878817e1bfSRafal Jaworowski case 32:
4888817e1bfSRafal Jaworowski cs->cs_config |= CESA_CSH_AES_KLEN_256;
4898817e1bfSRafal Jaworowski for (i = 0; i < 4; i++)
4908817e1bfSRafal Jaworowski *dkey++ = htobe32(ek[4 * 14 + i]);
4918817e1bfSRafal Jaworowski for (i = 0; i < 4; i++)
4928817e1bfSRafal Jaworowski *dkey++ = htobe32(ek[4 * 13 + i]);
4938817e1bfSRafal Jaworowski break;
4948817e1bfSRafal Jaworowski default:
4958817e1bfSRafal Jaworowski return (EINVAL);
4968817e1bfSRafal Jaworowski }
4978817e1bfSRafal Jaworowski
4988817e1bfSRafal Jaworowski return (0);
4998817e1bfSRafal Jaworowski }
5008817e1bfSRafal Jaworowski
5018817e1bfSRafal Jaworowski static void
cesa_start_packet(struct cesa_packet * cp,unsigned int size)5028817e1bfSRafal Jaworowski cesa_start_packet(struct cesa_packet *cp, unsigned int size)
5038817e1bfSRafal Jaworowski {
5048817e1bfSRafal Jaworowski
5058817e1bfSRafal Jaworowski cp->cp_size = size;
5068817e1bfSRafal Jaworowski cp->cp_offset = 0;
5078817e1bfSRafal Jaworowski STAILQ_INIT(&cp->cp_copyin);
5088817e1bfSRafal Jaworowski STAILQ_INIT(&cp->cp_copyout);
5098817e1bfSRafal Jaworowski }
5108817e1bfSRafal Jaworowski
5118817e1bfSRafal Jaworowski static int
cesa_fill_packet(struct cesa_softc * sc,struct cesa_packet * cp,bus_dma_segment_t * seg)5128817e1bfSRafal Jaworowski cesa_fill_packet(struct cesa_softc *sc, struct cesa_packet *cp,
5138817e1bfSRafal Jaworowski bus_dma_segment_t *seg)
5148817e1bfSRafal Jaworowski {
5158817e1bfSRafal Jaworowski struct cesa_tdma_desc *ctd;
5168817e1bfSRafal Jaworowski unsigned int bsize;
5178817e1bfSRafal Jaworowski
5188817e1bfSRafal Jaworowski /* Calculate size of block copy */
5198817e1bfSRafal Jaworowski bsize = MIN(seg->ds_len, cp->cp_size - cp->cp_offset);
5208817e1bfSRafal Jaworowski
5218817e1bfSRafal Jaworowski if (bsize > 0) {
5225188e3ccSZbigniew Bodek ctd = cesa_tdma_copy(sc, sc->sc_sram_base_pa +
5238817e1bfSRafal Jaworowski CESA_DATA(cp->cp_offset), seg->ds_addr, bsize);
5248817e1bfSRafal Jaworowski if (!ctd)
5258817e1bfSRafal Jaworowski return (-ENOMEM);
5268817e1bfSRafal Jaworowski
5278817e1bfSRafal Jaworowski STAILQ_INSERT_TAIL(&cp->cp_copyin, ctd, ctd_stq);
5288817e1bfSRafal Jaworowski
5295188e3ccSZbigniew Bodek ctd = cesa_tdma_copy(sc, seg->ds_addr, sc->sc_sram_base_pa +
5308817e1bfSRafal Jaworowski CESA_DATA(cp->cp_offset), bsize);
5318817e1bfSRafal Jaworowski if (!ctd)
5328817e1bfSRafal Jaworowski return (-ENOMEM);
5338817e1bfSRafal Jaworowski
5348817e1bfSRafal Jaworowski STAILQ_INSERT_TAIL(&cp->cp_copyout, ctd, ctd_stq);
5358817e1bfSRafal Jaworowski
5368817e1bfSRafal Jaworowski seg->ds_len -= bsize;
5378817e1bfSRafal Jaworowski seg->ds_addr += bsize;
5388817e1bfSRafal Jaworowski cp->cp_offset += bsize;
5398817e1bfSRafal Jaworowski }
5408817e1bfSRafal Jaworowski
5418817e1bfSRafal Jaworowski return (bsize);
5428817e1bfSRafal Jaworowski }
5438817e1bfSRafal Jaworowski
5448817e1bfSRafal Jaworowski static void
cesa_create_chain_cb(void * arg,bus_dma_segment_t * segs,int nseg,int error)5458817e1bfSRafal Jaworowski cesa_create_chain_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
5468817e1bfSRafal Jaworowski {
5478817e1bfSRafal Jaworowski unsigned int mpsize, fragmented;
5488817e1bfSRafal Jaworowski unsigned int mlen, mskip, tmlen;
5498817e1bfSRafal Jaworowski struct cesa_chain_info *cci;
5508817e1bfSRafal Jaworowski unsigned int elen, eskip;
5518817e1bfSRafal Jaworowski unsigned int skip, len;
5528817e1bfSRafal Jaworowski struct cesa_sa_desc *csd;
5538817e1bfSRafal Jaworowski struct cesa_request *cr;
554c0341432SJohn Baldwin struct cryptop *crp;
5558817e1bfSRafal Jaworowski struct cesa_softc *sc;
5568817e1bfSRafal Jaworowski struct cesa_packet cp;
5578817e1bfSRafal Jaworowski bus_dma_segment_t seg;
5588817e1bfSRafal Jaworowski uint32_t config;
5598817e1bfSRafal Jaworowski int size;
5608817e1bfSRafal Jaworowski
5618817e1bfSRafal Jaworowski cci = arg;
5628817e1bfSRafal Jaworowski sc = cci->cci_sc;
5638817e1bfSRafal Jaworowski cr = cci->cci_cr;
564c0341432SJohn Baldwin crp = cr->cr_crp;
5658817e1bfSRafal Jaworowski
5668817e1bfSRafal Jaworowski if (error) {
5678817e1bfSRafal Jaworowski cci->cci_error = error;
5688817e1bfSRafal Jaworowski return;
5698817e1bfSRafal Jaworowski }
5708817e1bfSRafal Jaworowski
571c0341432SJohn Baldwin /*
572c0341432SJohn Baldwin * Only do a combined op if the AAD is adjacent to the payload
573c0341432SJohn Baldwin * and the AAD length is a multiple of the IV length. The
574c0341432SJohn Baldwin * checks against 'config' are to avoid recursing when the
575c0341432SJohn Baldwin * logic below invokes separate operations.
576c0341432SJohn Baldwin */
577c0341432SJohn Baldwin config = cci->cci_config;
578c0341432SJohn Baldwin if (((config & CESA_CSHD_OP_MASK) == CESA_CSHD_MAC_AND_ENC ||
579c0341432SJohn Baldwin (config & CESA_CSHD_OP_MASK) == CESA_CSHD_ENC_AND_MAC) &&
580c0341432SJohn Baldwin crp->crp_aad_length != 0 &&
581c0341432SJohn Baldwin (crp->crp_aad_length & (cr->cr_cs->cs_ivlen - 1)) != 0) {
5828817e1bfSRafal Jaworowski /*
5838817e1bfSRafal Jaworowski * Data alignment in the request does not meet CESA requiremnts
5848817e1bfSRafal Jaworowski * for combined encryption/decryption and hashing. We have to
5858817e1bfSRafal Jaworowski * split the request to separate operations and process them
5868817e1bfSRafal Jaworowski * one by one.
5878817e1bfSRafal Jaworowski */
5888817e1bfSRafal Jaworowski if ((config & CESA_CSHD_OP_MASK) == CESA_CSHD_MAC_AND_ENC) {
5898817e1bfSRafal Jaworowski config &= ~CESA_CSHD_OP_MASK;
5908817e1bfSRafal Jaworowski
5918817e1bfSRafal Jaworowski cci->cci_config = config | CESA_CSHD_MAC;
592c0341432SJohn Baldwin cesa_create_chain_cb(cci, segs, nseg, 0);
5938817e1bfSRafal Jaworowski
5948817e1bfSRafal Jaworowski cci->cci_config = config | CESA_CSHD_ENC;
595c0341432SJohn Baldwin cesa_create_chain_cb(cci, segs, nseg, 0);
5968817e1bfSRafal Jaworowski } else {
5978817e1bfSRafal Jaworowski config &= ~CESA_CSHD_OP_MASK;
5988817e1bfSRafal Jaworowski
5998817e1bfSRafal Jaworowski cci->cci_config = config | CESA_CSHD_ENC;
600c0341432SJohn Baldwin cesa_create_chain_cb(cci, segs, nseg, 0);
6018817e1bfSRafal Jaworowski
6028817e1bfSRafal Jaworowski cci->cci_config = config | CESA_CSHD_MAC;
603c0341432SJohn Baldwin cesa_create_chain_cb(cci, segs, nseg, 0);
6048817e1bfSRafal Jaworowski }
6058817e1bfSRafal Jaworowski
6068817e1bfSRafal Jaworowski return;
6078817e1bfSRafal Jaworowski }
6088817e1bfSRafal Jaworowski
609c0341432SJohn Baldwin mskip = mlen = eskip = elen = 0;
610c0341432SJohn Baldwin
611c0341432SJohn Baldwin if (crp->crp_aad_length == 0) {
612c0341432SJohn Baldwin skip = crp->crp_payload_start;
613c0341432SJohn Baldwin len = crp->crp_payload_length;
614c0341432SJohn Baldwin switch (config & CESA_CSHD_OP_MASK) {
615c0341432SJohn Baldwin case CESA_CSHD_ENC:
616c0341432SJohn Baldwin eskip = skip;
617c0341432SJohn Baldwin elen = len;
618c0341432SJohn Baldwin break;
619c0341432SJohn Baldwin case CESA_CSHD_MAC:
620c0341432SJohn Baldwin mskip = skip;
621c0341432SJohn Baldwin mlen = len;
622c0341432SJohn Baldwin break;
623c0341432SJohn Baldwin default:
624c0341432SJohn Baldwin eskip = skip;
625c0341432SJohn Baldwin elen = len;
626c0341432SJohn Baldwin mskip = skip;
627c0341432SJohn Baldwin mlen = len;
628c0341432SJohn Baldwin break;
629c0341432SJohn Baldwin }
630c0341432SJohn Baldwin } else {
631c0341432SJohn Baldwin /*
632c0341432SJohn Baldwin * For an encryption-only separate request, only
633c0341432SJohn Baldwin * process the payload. For combined requests and
634c0341432SJohn Baldwin * hash-only requests, process the entire region.
635c0341432SJohn Baldwin */
636c0341432SJohn Baldwin switch (config & CESA_CSHD_OP_MASK) {
637c0341432SJohn Baldwin case CESA_CSHD_ENC:
638c0341432SJohn Baldwin skip = crp->crp_payload_start;
639c0341432SJohn Baldwin len = crp->crp_payload_length;
640c0341432SJohn Baldwin eskip = skip;
641c0341432SJohn Baldwin elen = len;
642c0341432SJohn Baldwin break;
643c0341432SJohn Baldwin case CESA_CSHD_MAC:
644c0341432SJohn Baldwin skip = crp->crp_aad_start;
645c0341432SJohn Baldwin len = crp->crp_aad_length + crp->crp_payload_length;
646c0341432SJohn Baldwin mskip = skip;
647c0341432SJohn Baldwin mlen = len;
648c0341432SJohn Baldwin break;
649c0341432SJohn Baldwin default:
650c0341432SJohn Baldwin skip = crp->crp_aad_start;
651c0341432SJohn Baldwin len = crp->crp_aad_length + crp->crp_payload_length;
652c0341432SJohn Baldwin mskip = skip;
653c0341432SJohn Baldwin mlen = len;
654c0341432SJohn Baldwin eskip = crp->crp_payload_start;
655c0341432SJohn Baldwin elen = crp->crp_payload_length;
656c0341432SJohn Baldwin break;
657c0341432SJohn Baldwin }
658c0341432SJohn Baldwin }
659c0341432SJohn Baldwin
6608817e1bfSRafal Jaworowski tmlen = mlen;
6618817e1bfSRafal Jaworowski fragmented = 0;
6628817e1bfSRafal Jaworowski mpsize = CESA_MAX_PACKET_SIZE;
6638817e1bfSRafal Jaworowski mpsize &= ~((cr->cr_cs->cs_ivlen - 1) | (cr->cr_cs->cs_mblen - 1));
6648817e1bfSRafal Jaworowski
6658817e1bfSRafal Jaworowski /* Start first packet in chain */
6668817e1bfSRafal Jaworowski cesa_start_packet(&cp, MIN(mpsize, len));
6678817e1bfSRafal Jaworowski
6688817e1bfSRafal Jaworowski while (nseg-- && len > 0) {
6698817e1bfSRafal Jaworowski seg = *(segs++);
6708817e1bfSRafal Jaworowski
6718817e1bfSRafal Jaworowski /*
6728817e1bfSRafal Jaworowski * Skip data in buffer on which neither ENC nor MAC operation
6738817e1bfSRafal Jaworowski * is requested.
6748817e1bfSRafal Jaworowski */
6758817e1bfSRafal Jaworowski if (skip > 0) {
6768817e1bfSRafal Jaworowski size = MIN(skip, seg.ds_len);
6778817e1bfSRafal Jaworowski skip -= size;
6788817e1bfSRafal Jaworowski
6798817e1bfSRafal Jaworowski seg.ds_addr += size;
6808817e1bfSRafal Jaworowski seg.ds_len -= size;
6818817e1bfSRafal Jaworowski
6828817e1bfSRafal Jaworowski if (eskip > 0)
6838817e1bfSRafal Jaworowski eskip -= size;
6848817e1bfSRafal Jaworowski
6858817e1bfSRafal Jaworowski if (mskip > 0)
6868817e1bfSRafal Jaworowski mskip -= size;
6878817e1bfSRafal Jaworowski
6888817e1bfSRafal Jaworowski if (seg.ds_len == 0)
6898817e1bfSRafal Jaworowski continue;
6908817e1bfSRafal Jaworowski }
6918817e1bfSRafal Jaworowski
6928817e1bfSRafal Jaworowski while (1) {
6938817e1bfSRafal Jaworowski /*
6948817e1bfSRafal Jaworowski * Fill in current packet with data. Break if there is
6958817e1bfSRafal Jaworowski * no more data in current DMA segment or an error
696453130d9SPedro F. Giffuni * occurred.
6978817e1bfSRafal Jaworowski */
6988817e1bfSRafal Jaworowski size = cesa_fill_packet(sc, &cp, &seg);
6998817e1bfSRafal Jaworowski if (size <= 0) {
7008817e1bfSRafal Jaworowski error = -size;
7018817e1bfSRafal Jaworowski break;
7028817e1bfSRafal Jaworowski }
7038817e1bfSRafal Jaworowski
7048817e1bfSRafal Jaworowski len -= size;
7058817e1bfSRafal Jaworowski
7068817e1bfSRafal Jaworowski /* If packet is full, append it to the chain */
7078817e1bfSRafal Jaworowski if (cp.cp_size == cp.cp_offset) {
7088817e1bfSRafal Jaworowski csd = cesa_alloc_sdesc(sc, cr);
7098817e1bfSRafal Jaworowski if (!csd) {
7108817e1bfSRafal Jaworowski error = ENOMEM;
7118817e1bfSRafal Jaworowski break;
7128817e1bfSRafal Jaworowski }
7138817e1bfSRafal Jaworowski
7148817e1bfSRafal Jaworowski /* Create SA descriptor for this packet */
7158817e1bfSRafal Jaworowski csd->csd_cshd->cshd_config = cci->cci_config;
7168817e1bfSRafal Jaworowski csd->csd_cshd->cshd_mac_total_dlen = tmlen;
7178817e1bfSRafal Jaworowski
7188817e1bfSRafal Jaworowski /*
7198817e1bfSRafal Jaworowski * Enable fragmentation if request will not fit
7208817e1bfSRafal Jaworowski * into one packet.
7218817e1bfSRafal Jaworowski */
7228817e1bfSRafal Jaworowski if (len > 0) {
7238817e1bfSRafal Jaworowski if (!fragmented) {
7248817e1bfSRafal Jaworowski fragmented = 1;
7258817e1bfSRafal Jaworowski csd->csd_cshd->cshd_config |=
7268817e1bfSRafal Jaworowski CESA_CSHD_FRAG_FIRST;
7278817e1bfSRafal Jaworowski } else
7288817e1bfSRafal Jaworowski csd->csd_cshd->cshd_config |=
7298817e1bfSRafal Jaworowski CESA_CSHD_FRAG_MIDDLE;
7308817e1bfSRafal Jaworowski } else if (fragmented)
7318817e1bfSRafal Jaworowski csd->csd_cshd->cshd_config |=
7328817e1bfSRafal Jaworowski CESA_CSHD_FRAG_LAST;
7338817e1bfSRafal Jaworowski
7348817e1bfSRafal Jaworowski if (eskip < cp.cp_size && elen > 0) {
7358817e1bfSRafal Jaworowski csd->csd_cshd->cshd_enc_src =
7368817e1bfSRafal Jaworowski CESA_DATA(eskip);
7378817e1bfSRafal Jaworowski csd->csd_cshd->cshd_enc_dst =
7388817e1bfSRafal Jaworowski CESA_DATA(eskip);
7398817e1bfSRafal Jaworowski csd->csd_cshd->cshd_enc_dlen =
7408817e1bfSRafal Jaworowski MIN(elen, cp.cp_size - eskip);
7418817e1bfSRafal Jaworowski }
7428817e1bfSRafal Jaworowski
7438817e1bfSRafal Jaworowski if (mskip < cp.cp_size && mlen > 0) {
7448817e1bfSRafal Jaworowski csd->csd_cshd->cshd_mac_src =
7458817e1bfSRafal Jaworowski CESA_DATA(mskip);
7468817e1bfSRafal Jaworowski csd->csd_cshd->cshd_mac_dlen =
7478817e1bfSRafal Jaworowski MIN(mlen, cp.cp_size - mskip);
7488817e1bfSRafal Jaworowski }
7498817e1bfSRafal Jaworowski
7508817e1bfSRafal Jaworowski elen -= csd->csd_cshd->cshd_enc_dlen;
7518817e1bfSRafal Jaworowski eskip -= MIN(eskip, cp.cp_size);
7528817e1bfSRafal Jaworowski mlen -= csd->csd_cshd->cshd_mac_dlen;
7538817e1bfSRafal Jaworowski mskip -= MIN(mskip, cp.cp_size);
7548817e1bfSRafal Jaworowski
7558817e1bfSRafal Jaworowski cesa_dump_cshd(sc, csd->csd_cshd);
7568817e1bfSRafal Jaworowski
7578817e1bfSRafal Jaworowski /* Append packet to the request */
7588817e1bfSRafal Jaworowski error = cesa_append_packet(sc, cr, &cp, csd);
7598817e1bfSRafal Jaworowski if (error)
7608817e1bfSRafal Jaworowski break;
7618817e1bfSRafal Jaworowski
7628817e1bfSRafal Jaworowski /* Start a new packet, as current is full */
7638817e1bfSRafal Jaworowski cesa_start_packet(&cp, MIN(mpsize, len));
7648817e1bfSRafal Jaworowski }
7658817e1bfSRafal Jaworowski }
7668817e1bfSRafal Jaworowski
7678817e1bfSRafal Jaworowski if (error)
7688817e1bfSRafal Jaworowski break;
7698817e1bfSRafal Jaworowski }
7708817e1bfSRafal Jaworowski
7718817e1bfSRafal Jaworowski if (error) {
7728817e1bfSRafal Jaworowski /*
7738817e1bfSRafal Jaworowski * Move all allocated resources to the request. They will be
7748817e1bfSRafal Jaworowski * freed later.
7758817e1bfSRafal Jaworowski */
7768817e1bfSRafal Jaworowski STAILQ_CONCAT(&cr->cr_tdesc, &cp.cp_copyin);
7778817e1bfSRafal Jaworowski STAILQ_CONCAT(&cr->cr_tdesc, &cp.cp_copyout);
7788817e1bfSRafal Jaworowski cci->cci_error = error;
7798817e1bfSRafal Jaworowski }
7808817e1bfSRafal Jaworowski }
7818817e1bfSRafal Jaworowski
7828817e1bfSRafal Jaworowski static int
cesa_create_chain(struct cesa_softc * sc,const struct crypto_session_params * csp,struct cesa_request * cr)783c0341432SJohn Baldwin cesa_create_chain(struct cesa_softc *sc,
784c0341432SJohn Baldwin const struct crypto_session_params *csp, struct cesa_request *cr)
7858817e1bfSRafal Jaworowski {
7868817e1bfSRafal Jaworowski struct cesa_chain_info cci;
7878817e1bfSRafal Jaworowski struct cesa_tdma_desc *ctd;
7888817e1bfSRafal Jaworowski uint32_t config;
7898817e1bfSRafal Jaworowski int error;
7908817e1bfSRafal Jaworowski
7918817e1bfSRafal Jaworowski error = 0;
7928817e1bfSRafal Jaworowski CESA_LOCK_ASSERT(sc, sessions);
7938817e1bfSRafal Jaworowski
7948817e1bfSRafal Jaworowski /* Create request metadata */
795c0341432SJohn Baldwin if (csp->csp_cipher_klen != 0) {
796c0341432SJohn Baldwin if (csp->csp_cipher_alg == CRYPTO_AES_CBC &&
797c0341432SJohn Baldwin !CRYPTO_OP_IS_ENCRYPT(cr->cr_crp->crp_op))
7988817e1bfSRafal Jaworowski memcpy(cr->cr_csd->csd_key, cr->cr_cs->cs_aes_dkey,
799c0341432SJohn Baldwin csp->csp_cipher_klen);
8008817e1bfSRafal Jaworowski else
8018817e1bfSRafal Jaworowski memcpy(cr->cr_csd->csd_key, cr->cr_cs->cs_key,
802c0341432SJohn Baldwin csp->csp_cipher_klen);
8038817e1bfSRafal Jaworowski }
8048817e1bfSRafal Jaworowski
805c0341432SJohn Baldwin if (csp->csp_auth_klen != 0) {
8068817e1bfSRafal Jaworowski memcpy(cr->cr_csd->csd_hiv_in, cr->cr_cs->cs_hiv_in,
8078817e1bfSRafal Jaworowski CESA_MAX_HASH_LEN);
8088817e1bfSRafal Jaworowski memcpy(cr->cr_csd->csd_hiv_out, cr->cr_cs->cs_hiv_out,
8098817e1bfSRafal Jaworowski CESA_MAX_HASH_LEN);
8108817e1bfSRafal Jaworowski }
8118817e1bfSRafal Jaworowski
8128817e1bfSRafal Jaworowski ctd = cesa_tdma_copyin_sa_data(sc, cr);
8138817e1bfSRafal Jaworowski if (!ctd)
8148817e1bfSRafal Jaworowski return (ENOMEM);
8158817e1bfSRafal Jaworowski
8168817e1bfSRafal Jaworowski cesa_append_tdesc(cr, ctd);
8178817e1bfSRafal Jaworowski
8188817e1bfSRafal Jaworowski /* Prepare SA configuration */
8198817e1bfSRafal Jaworowski config = cr->cr_cs->cs_config;
8208817e1bfSRafal Jaworowski
821c0341432SJohn Baldwin if (csp->csp_cipher_alg != 0 &&
822c0341432SJohn Baldwin !CRYPTO_OP_IS_ENCRYPT(cr->cr_crp->crp_op))
8238817e1bfSRafal Jaworowski config |= CESA_CSHD_DECRYPT;
824c0341432SJohn Baldwin switch (csp->csp_mode) {
825c0341432SJohn Baldwin case CSP_MODE_CIPHER:
8268817e1bfSRafal Jaworowski config |= CESA_CSHD_ENC;
827c0341432SJohn Baldwin break;
828c0341432SJohn Baldwin case CSP_MODE_DIGEST:
8298817e1bfSRafal Jaworowski config |= CESA_CSHD_MAC;
830c0341432SJohn Baldwin break;
831c0341432SJohn Baldwin case CSP_MODE_ETA:
8328817e1bfSRafal Jaworowski config |= (config & CESA_CSHD_DECRYPT) ? CESA_CSHD_MAC_AND_ENC :
8338817e1bfSRafal Jaworowski CESA_CSHD_ENC_AND_MAC;
834c0341432SJohn Baldwin break;
835c0341432SJohn Baldwin }
8368817e1bfSRafal Jaworowski
8378817e1bfSRafal Jaworowski /* Create data packets */
8388817e1bfSRafal Jaworowski cci.cci_sc = sc;
8398817e1bfSRafal Jaworowski cci.cci_cr = cr;
8408817e1bfSRafal Jaworowski cci.cci_config = config;
8418817e1bfSRafal Jaworowski cci.cci_error = 0;
8428817e1bfSRafal Jaworowski
843c0341432SJohn Baldwin error = bus_dmamap_load_crp(sc->sc_data_dtag, cr->cr_dmap, cr->cr_crp,
844c0341432SJohn Baldwin cesa_create_chain_cb, &cci, BUS_DMA_NOWAIT);
8458817e1bfSRafal Jaworowski
8468817e1bfSRafal Jaworowski if (!error)
8478817e1bfSRafal Jaworowski cr->cr_dmap_loaded = 1;
8488817e1bfSRafal Jaworowski
8498817e1bfSRafal Jaworowski if (cci.cci_error)
8508817e1bfSRafal Jaworowski error = cci.cci_error;
8518817e1bfSRafal Jaworowski
8528817e1bfSRafal Jaworowski if (error)
8538817e1bfSRafal Jaworowski return (error);
8548817e1bfSRafal Jaworowski
8558817e1bfSRafal Jaworowski /* Read back request metadata */
8568817e1bfSRafal Jaworowski ctd = cesa_tdma_copyout_sa_data(sc, cr);
8578817e1bfSRafal Jaworowski if (!ctd)
8588817e1bfSRafal Jaworowski return (ENOMEM);
8598817e1bfSRafal Jaworowski
8608817e1bfSRafal Jaworowski cesa_append_tdesc(cr, ctd);
8618817e1bfSRafal Jaworowski
8628817e1bfSRafal Jaworowski return (0);
8638817e1bfSRafal Jaworowski }
8648817e1bfSRafal Jaworowski
8658817e1bfSRafal Jaworowski static void
cesa_execute(struct cesa_softc * sc)8668817e1bfSRafal Jaworowski cesa_execute(struct cesa_softc *sc)
8678817e1bfSRafal Jaworowski {
8688817e1bfSRafal Jaworowski struct cesa_tdma_desc *prev_ctd, *ctd;
8698817e1bfSRafal Jaworowski struct cesa_request *prev_cr, *cr;
8708817e1bfSRafal Jaworowski
8718817e1bfSRafal Jaworowski CESA_LOCK(sc, requests);
8728817e1bfSRafal Jaworowski
8738817e1bfSRafal Jaworowski /*
8748817e1bfSRafal Jaworowski * If ready list is empty, there is nothing to execute. If queued list
8758817e1bfSRafal Jaworowski * is not empty, the hardware is busy and we cannot start another
8768817e1bfSRafal Jaworowski * execution.
8778817e1bfSRafal Jaworowski */
8788817e1bfSRafal Jaworowski if (STAILQ_EMPTY(&sc->sc_ready_requests) ||
8798817e1bfSRafal Jaworowski !STAILQ_EMPTY(&sc->sc_queued_requests)) {
8808817e1bfSRafal Jaworowski CESA_UNLOCK(sc, requests);
8818817e1bfSRafal Jaworowski return;
8828817e1bfSRafal Jaworowski }
8838817e1bfSRafal Jaworowski
8848817e1bfSRafal Jaworowski /* Move all ready requests to queued list */
8858817e1bfSRafal Jaworowski STAILQ_CONCAT(&sc->sc_queued_requests, &sc->sc_ready_requests);
8868817e1bfSRafal Jaworowski STAILQ_INIT(&sc->sc_ready_requests);
8878817e1bfSRafal Jaworowski
8888817e1bfSRafal Jaworowski /* Create one execution chain from all requests on the list */
8898817e1bfSRafal Jaworowski if (STAILQ_FIRST(&sc->sc_queued_requests) !=
8908817e1bfSRafal Jaworowski STAILQ_LAST(&sc->sc_queued_requests, cesa_request, cr_stq)) {
8918817e1bfSRafal Jaworowski prev_cr = NULL;
8928817e1bfSRafal Jaworowski cesa_sync_dma_mem(&sc->sc_tdesc_cdm, BUS_DMASYNC_POSTREAD |
8938817e1bfSRafal Jaworowski BUS_DMASYNC_POSTWRITE);
8948817e1bfSRafal Jaworowski
8958817e1bfSRafal Jaworowski STAILQ_FOREACH(cr, &sc->sc_queued_requests, cr_stq) {
8968817e1bfSRafal Jaworowski if (prev_cr) {
8978817e1bfSRafal Jaworowski ctd = STAILQ_FIRST(&cr->cr_tdesc);
8988817e1bfSRafal Jaworowski prev_ctd = STAILQ_LAST(&prev_cr->cr_tdesc,
8998817e1bfSRafal Jaworowski cesa_tdma_desc, ctd_stq);
9008817e1bfSRafal Jaworowski
9018817e1bfSRafal Jaworowski prev_ctd->ctd_cthd->cthd_next =
9028817e1bfSRafal Jaworowski ctd->ctd_cthd_paddr;
9038817e1bfSRafal Jaworowski }
9048817e1bfSRafal Jaworowski
9058817e1bfSRafal Jaworowski prev_cr = cr;
9068817e1bfSRafal Jaworowski }
9078817e1bfSRafal Jaworowski
9088817e1bfSRafal Jaworowski cesa_sync_dma_mem(&sc->sc_tdesc_cdm, BUS_DMASYNC_PREREAD |
9098817e1bfSRafal Jaworowski BUS_DMASYNC_PREWRITE);
9108817e1bfSRafal Jaworowski }
9118817e1bfSRafal Jaworowski
9128817e1bfSRafal Jaworowski /* Start chain execution in hardware */
9138817e1bfSRafal Jaworowski cr = STAILQ_FIRST(&sc->sc_queued_requests);
9148817e1bfSRafal Jaworowski ctd = STAILQ_FIRST(&cr->cr_tdesc);
9158817e1bfSRafal Jaworowski
91622c7a031SZbigniew Bodek CESA_TDMA_WRITE(sc, CESA_TDMA_ND, ctd->ctd_cthd_paddr);
91781526b83SWojciech Macek
91881526b83SWojciech Macek if (sc->sc_soc_id == MV_DEV_88F6828 ||
91981526b83SWojciech Macek sc->sc_soc_id == MV_DEV_88F6820 ||
92081526b83SWojciech Macek sc->sc_soc_id == MV_DEV_88F6810)
921b07df6e5SZbigniew Bodek CESA_REG_WRITE(sc, CESA_SA_CMD, CESA_SA_CMD_ACTVATE | CESA_SA_CMD_SHA2);
92281526b83SWojciech Macek else
92322c7a031SZbigniew Bodek CESA_REG_WRITE(sc, CESA_SA_CMD, CESA_SA_CMD_ACTVATE);
9248817e1bfSRafal Jaworowski
9258817e1bfSRafal Jaworowski CESA_UNLOCK(sc, requests);
9268817e1bfSRafal Jaworowski }
9278817e1bfSRafal Jaworowski
9288817e1bfSRafal Jaworowski static int
cesa_setup_sram(struct cesa_softc * sc)9298817e1bfSRafal Jaworowski cesa_setup_sram(struct cesa_softc *sc)
9308817e1bfSRafal Jaworowski {
9318817e1bfSRafal Jaworowski phandle_t sram_node;
9328817e1bfSRafal Jaworowski ihandle_t sram_ihandle;
9335188e3ccSZbigniew Bodek pcell_t sram_handle, sram_reg[2];
93481526b83SWojciech Macek void *sram_va;
9355188e3ccSZbigniew Bodek int rv;
9368817e1bfSRafal Jaworowski
9372cf4c169SZbigniew Bodek rv = OF_getencprop(ofw_bus_get_node(sc->sc_dev), "sram-handle",
9385188e3ccSZbigniew Bodek (void *)&sram_handle, sizeof(sram_handle));
9395188e3ccSZbigniew Bodek if (rv <= 0)
9405188e3ccSZbigniew Bodek return (rv);
9418817e1bfSRafal Jaworowski
9428817e1bfSRafal Jaworowski sram_ihandle = (ihandle_t)sram_handle;
9438817e1bfSRafal Jaworowski sram_node = OF_instance_to_package(sram_ihandle);
9448817e1bfSRafal Jaworowski
9452cf4c169SZbigniew Bodek rv = OF_getencprop(sram_node, "reg", (void *)sram_reg, sizeof(sram_reg));
9465188e3ccSZbigniew Bodek if (rv <= 0)
9475188e3ccSZbigniew Bodek return (rv);
9488817e1bfSRafal Jaworowski
9492cf4c169SZbigniew Bodek sc->sc_sram_base_pa = sram_reg[0];
9505188e3ccSZbigniew Bodek /* Store SRAM size to be able to unmap in detach() */
9512cf4c169SZbigniew Bodek sc->sc_sram_size = sram_reg[1];
9528817e1bfSRafal Jaworowski
95381526b83SWojciech Macek if (sc->sc_soc_id != MV_DEV_88F6828 &&
95481526b83SWojciech Macek sc->sc_soc_id != MV_DEV_88F6820 &&
95581526b83SWojciech Macek sc->sc_soc_id != MV_DEV_88F6810)
95681526b83SWojciech Macek return (0);
9572cf4c169SZbigniew Bodek
9585188e3ccSZbigniew Bodek /* SRAM memory was not mapped in platform_sram_devmap(), map it now */
9592cf4c169SZbigniew Bodek sram_va = pmap_mapdev(sc->sc_sram_base_pa, sc->sc_sram_size);
9602cf4c169SZbigniew Bodek if (sram_va == NULL)
9612cf4c169SZbigniew Bodek return (ENOMEM);
9627ae99f80SJohn Baldwin sc->sc_sram_base_va = sram_va;
96381526b83SWojciech Macek
9648817e1bfSRafal Jaworowski return (0);
9658817e1bfSRafal Jaworowski }
9668817e1bfSRafal Jaworowski
967e9e2a7c1SMarcin Wojtas /*
968e9e2a7c1SMarcin Wojtas * Function: device_from_node
969e9e2a7c1SMarcin Wojtas * This function returns appropriate device_t to phandle_t
970e9e2a7c1SMarcin Wojtas * Parameters:
971e9e2a7c1SMarcin Wojtas * root - device where you want to start search
972e9e2a7c1SMarcin Wojtas * if you provide NULL here, function will take
973e9e2a7c1SMarcin Wojtas * "root0" device as root.
974e9e2a7c1SMarcin Wojtas * node - we are checking every device_t to be
975e9e2a7c1SMarcin Wojtas * appropriate with this.
976e9e2a7c1SMarcin Wojtas */
977e9e2a7c1SMarcin Wojtas static device_t
device_from_node(device_t root,phandle_t node)978e9e2a7c1SMarcin Wojtas device_from_node(device_t root, phandle_t node)
979e9e2a7c1SMarcin Wojtas {
980e9e2a7c1SMarcin Wojtas device_t *children, retval;
981e9e2a7c1SMarcin Wojtas int nkid, i;
982e9e2a7c1SMarcin Wojtas
983e9e2a7c1SMarcin Wojtas /* Nothing matches no node */
984e9e2a7c1SMarcin Wojtas if (node == -1)
985e9e2a7c1SMarcin Wojtas return (NULL);
986e9e2a7c1SMarcin Wojtas
987e9e2a7c1SMarcin Wojtas if (root == NULL)
988e9e2a7c1SMarcin Wojtas /* Get root of device tree */
989e9e2a7c1SMarcin Wojtas if ((root = device_lookup_by_name("root0")) == NULL)
990e9e2a7c1SMarcin Wojtas return (NULL);
991e9e2a7c1SMarcin Wojtas
992e9e2a7c1SMarcin Wojtas if (device_get_children(root, &children, &nkid) != 0)
993e9e2a7c1SMarcin Wojtas return (NULL);
994e9e2a7c1SMarcin Wojtas
995e9e2a7c1SMarcin Wojtas retval = NULL;
996e9e2a7c1SMarcin Wojtas for (i = 0; i < nkid; i++) {
997e9e2a7c1SMarcin Wojtas /* Check if device and node matches */
998e9e2a7c1SMarcin Wojtas if (OFW_BUS_GET_NODE(root, children[i]) == node) {
999e9e2a7c1SMarcin Wojtas retval = children[i];
1000e9e2a7c1SMarcin Wojtas break;
1001e9e2a7c1SMarcin Wojtas }
1002e9e2a7c1SMarcin Wojtas /* or go deeper */
1003e9e2a7c1SMarcin Wojtas if ((retval = device_from_node(children[i], node)) != NULL)
1004e9e2a7c1SMarcin Wojtas break;
1005e9e2a7c1SMarcin Wojtas }
1006e9e2a7c1SMarcin Wojtas free(children, M_TEMP);
1007e9e2a7c1SMarcin Wojtas
1008e9e2a7c1SMarcin Wojtas return (retval);
1009e9e2a7c1SMarcin Wojtas }
1010e9e2a7c1SMarcin Wojtas
1011e9e2a7c1SMarcin Wojtas static int
cesa_setup_sram_armada(struct cesa_softc * sc)1012e9e2a7c1SMarcin Wojtas cesa_setup_sram_armada(struct cesa_softc *sc)
1013e9e2a7c1SMarcin Wojtas {
1014e9e2a7c1SMarcin Wojtas phandle_t sram_node;
1015e9e2a7c1SMarcin Wojtas ihandle_t sram_ihandle;
1016e9e2a7c1SMarcin Wojtas pcell_t sram_handle[2];
1017e9e2a7c1SMarcin Wojtas void *sram_va;
1018e9e2a7c1SMarcin Wojtas int rv, j;
1019e9e2a7c1SMarcin Wojtas struct resource_list rl;
1020e9e2a7c1SMarcin Wojtas struct resource_list_entry *rle;
1021e9e2a7c1SMarcin Wojtas struct simplebus_softc *ssc;
1022e9e2a7c1SMarcin Wojtas device_t sdev;
1023e9e2a7c1SMarcin Wojtas
1024e9e2a7c1SMarcin Wojtas /* Get refs to SRAMS from CESA node */
1025e9e2a7c1SMarcin Wojtas rv = OF_getencprop(ofw_bus_get_node(sc->sc_dev), "marvell,crypto-srams",
1026e9e2a7c1SMarcin Wojtas (void *)sram_handle, sizeof(sram_handle));
1027e9e2a7c1SMarcin Wojtas if (rv <= 0)
1028e9e2a7c1SMarcin Wojtas return (rv);
1029e9e2a7c1SMarcin Wojtas
1030e9e2a7c1SMarcin Wojtas if (sc->sc_cesa_engine_id >= 2)
1031e9e2a7c1SMarcin Wojtas return (ENXIO);
1032e9e2a7c1SMarcin Wojtas
1033e9e2a7c1SMarcin Wojtas /* Get SRAM node on the basis of sc_cesa_engine_id */
1034e9e2a7c1SMarcin Wojtas sram_ihandle = (ihandle_t)sram_handle[sc->sc_cesa_engine_id];
1035e9e2a7c1SMarcin Wojtas sram_node = OF_instance_to_package(sram_ihandle);
1036e9e2a7c1SMarcin Wojtas
1037e9e2a7c1SMarcin Wojtas /* Get device_t of simplebus (sram_node parent) */
1038e9e2a7c1SMarcin Wojtas sdev = device_from_node(NULL, OF_parent(sram_node));
1039e9e2a7c1SMarcin Wojtas if (!sdev)
1040e9e2a7c1SMarcin Wojtas return (ENXIO);
1041e9e2a7c1SMarcin Wojtas
1042e9e2a7c1SMarcin Wojtas ssc = device_get_softc(sdev);
1043e9e2a7c1SMarcin Wojtas
1044e9e2a7c1SMarcin Wojtas resource_list_init(&rl);
1045e9e2a7c1SMarcin Wojtas /* Parse reg property to resource list */
1046e9e2a7c1SMarcin Wojtas ofw_bus_reg_to_rl(sdev, sram_node, ssc->acells,
1047e9e2a7c1SMarcin Wojtas ssc->scells, &rl);
1048e9e2a7c1SMarcin Wojtas
1049e9e2a7c1SMarcin Wojtas /* We expect only one resource */
1050e9e2a7c1SMarcin Wojtas rle = resource_list_find(&rl, SYS_RES_MEMORY, 0);
1051e9e2a7c1SMarcin Wojtas if (rle == NULL)
1052e9e2a7c1SMarcin Wojtas return (ENXIO);
1053e9e2a7c1SMarcin Wojtas
1054e9e2a7c1SMarcin Wojtas /* Remap through ranges property */
1055e9e2a7c1SMarcin Wojtas for (j = 0; j < ssc->nranges; j++) {
1056e9e2a7c1SMarcin Wojtas if (rle->start >= ssc->ranges[j].bus &&
1057e9e2a7c1SMarcin Wojtas rle->end < ssc->ranges[j].bus + ssc->ranges[j].size) {
1058e9e2a7c1SMarcin Wojtas rle->start -= ssc->ranges[j].bus;
1059e9e2a7c1SMarcin Wojtas rle->start += ssc->ranges[j].host;
1060e9e2a7c1SMarcin Wojtas rle->end -= ssc->ranges[j].bus;
1061e9e2a7c1SMarcin Wojtas rle->end += ssc->ranges[j].host;
1062e9e2a7c1SMarcin Wojtas }
1063e9e2a7c1SMarcin Wojtas }
1064e9e2a7c1SMarcin Wojtas
1065e9e2a7c1SMarcin Wojtas sc->sc_sram_base_pa = rle->start;
1066e9e2a7c1SMarcin Wojtas sc->sc_sram_size = rle->count;
1067e9e2a7c1SMarcin Wojtas
1068e9e2a7c1SMarcin Wojtas /* SRAM memory was not mapped in platform_sram_devmap(), map it now */
1069e9e2a7c1SMarcin Wojtas sram_va = pmap_mapdev(sc->sc_sram_base_pa, sc->sc_sram_size);
1070e9e2a7c1SMarcin Wojtas if (sram_va == NULL)
1071e9e2a7c1SMarcin Wojtas return (ENOMEM);
10727ae99f80SJohn Baldwin sc->sc_sram_base_va = sram_va;
1073e9e2a7c1SMarcin Wojtas
1074e9e2a7c1SMarcin Wojtas return (0);
1075e9e2a7c1SMarcin Wojtas }
1076e9e2a7c1SMarcin Wojtas
1077e9e2a7c1SMarcin Wojtas struct ofw_compat_data cesa_devices[] = {
1078e9e2a7c1SMarcin Wojtas { "mrvl,cesa", (uintptr_t)true },
1079e9e2a7c1SMarcin Wojtas { "marvell,armada-38x-crypto", (uintptr_t)true },
1080e9e2a7c1SMarcin Wojtas { NULL, 0 }
1081e9e2a7c1SMarcin Wojtas };
1082e9e2a7c1SMarcin Wojtas
10838817e1bfSRafal Jaworowski static int
cesa_probe(device_t dev)10848817e1bfSRafal Jaworowski cesa_probe(device_t dev)
10858817e1bfSRafal Jaworowski {
1086add35ed5SIan Lepore
1087add35ed5SIan Lepore if (!ofw_bus_status_okay(dev))
1088add35ed5SIan Lepore return (ENXIO);
1089add35ed5SIan Lepore
1090e9e2a7c1SMarcin Wojtas if (!ofw_bus_search_compatible(dev, cesa_devices)->ocd_data)
10918817e1bfSRafal Jaworowski return (ENXIO);
10928817e1bfSRafal Jaworowski
10938817e1bfSRafal Jaworowski device_set_desc(dev, "Marvell Cryptographic Engine and Security "
10948817e1bfSRafal Jaworowski "Accelerator");
10958817e1bfSRafal Jaworowski
10968817e1bfSRafal Jaworowski return (BUS_PROBE_DEFAULT);
10978817e1bfSRafal Jaworowski }
10988817e1bfSRafal Jaworowski
10998817e1bfSRafal Jaworowski static int
cesa_attach(device_t dev)11008817e1bfSRafal Jaworowski cesa_attach(device_t dev)
11018817e1bfSRafal Jaworowski {
1102e9e2a7c1SMarcin Wojtas static int engine_idx = 0;
1103e9e2a7c1SMarcin Wojtas struct simplebus_devinfo *ndi;
1104e9e2a7c1SMarcin Wojtas struct resource_list *rl;
1105e9e2a7c1SMarcin Wojtas struct cesa_softc *sc;
1106e9e2a7c1SMarcin Wojtas
1107e9e2a7c1SMarcin Wojtas if (!ofw_bus_is_compatible(dev, "marvell,armada-38x-crypto"))
1108e9e2a7c1SMarcin Wojtas return (cesa_attach_late(dev));
1109e9e2a7c1SMarcin Wojtas
1110e9e2a7c1SMarcin Wojtas /*
1111e9e2a7c1SMarcin Wojtas * Get simplebus_devinfo which contains
1112e9e2a7c1SMarcin Wojtas * resource list filled with adresses and
1113e9e2a7c1SMarcin Wojtas * interrupts read form FDT.
1114e9e2a7c1SMarcin Wojtas * Let's correct it by splitting resources
1115e9e2a7c1SMarcin Wojtas * for each engine.
1116e9e2a7c1SMarcin Wojtas */
1117e9e2a7c1SMarcin Wojtas if ((ndi = device_get_ivars(dev)) == NULL)
1118e9e2a7c1SMarcin Wojtas return (ENXIO);
1119e9e2a7c1SMarcin Wojtas
1120e9e2a7c1SMarcin Wojtas rl = &ndi->rl;
1121e9e2a7c1SMarcin Wojtas
1122e9e2a7c1SMarcin Wojtas switch (engine_idx) {
1123e9e2a7c1SMarcin Wojtas case 0:
1124e9e2a7c1SMarcin Wojtas /* Update regs values */
1125e9e2a7c1SMarcin Wojtas resource_list_add(rl, SYS_RES_MEMORY, 0, CESA0_TDMA_ADDR,
1126e9e2a7c1SMarcin Wojtas CESA0_TDMA_ADDR + CESA_TDMA_SIZE - 1, CESA_TDMA_SIZE);
1127e9e2a7c1SMarcin Wojtas resource_list_add(rl, SYS_RES_MEMORY, 1, CESA0_CESA_ADDR,
1128e9e2a7c1SMarcin Wojtas CESA0_CESA_ADDR + CESA_CESA_SIZE - 1, CESA_CESA_SIZE);
1129e9e2a7c1SMarcin Wojtas
1130e9e2a7c1SMarcin Wojtas /* Remove unused interrupt */
1131e9e2a7c1SMarcin Wojtas resource_list_delete(rl, SYS_RES_IRQ, 1);
1132e9e2a7c1SMarcin Wojtas break;
1133e9e2a7c1SMarcin Wojtas
1134e9e2a7c1SMarcin Wojtas case 1:
1135e9e2a7c1SMarcin Wojtas /* Update regs values */
1136e9e2a7c1SMarcin Wojtas resource_list_add(rl, SYS_RES_MEMORY, 0, CESA1_TDMA_ADDR,
1137e9e2a7c1SMarcin Wojtas CESA1_TDMA_ADDR + CESA_TDMA_SIZE - 1, CESA_TDMA_SIZE);
1138e9e2a7c1SMarcin Wojtas resource_list_add(rl, SYS_RES_MEMORY, 1, CESA1_CESA_ADDR,
1139e9e2a7c1SMarcin Wojtas CESA1_CESA_ADDR + CESA_CESA_SIZE - 1, CESA_CESA_SIZE);
1140e9e2a7c1SMarcin Wojtas
1141e9e2a7c1SMarcin Wojtas /* Remove unused interrupt */
1142e9e2a7c1SMarcin Wojtas resource_list_delete(rl, SYS_RES_IRQ, 0);
1143e9e2a7c1SMarcin Wojtas resource_list_find(rl, SYS_RES_IRQ, 1)->rid = 0;
1144e9e2a7c1SMarcin Wojtas break;
1145e9e2a7c1SMarcin Wojtas
1146e9e2a7c1SMarcin Wojtas default:
1147e9e2a7c1SMarcin Wojtas device_printf(dev, "Bad cesa engine_idx\n");
1148e9e2a7c1SMarcin Wojtas return (ENXIO);
1149e9e2a7c1SMarcin Wojtas }
1150e9e2a7c1SMarcin Wojtas
1151e9e2a7c1SMarcin Wojtas sc = device_get_softc(dev);
1152e9e2a7c1SMarcin Wojtas sc->sc_cesa_engine_id = engine_idx;
1153e9e2a7c1SMarcin Wojtas
1154e9e2a7c1SMarcin Wojtas /*
1155e9e2a7c1SMarcin Wojtas * Call simplebus_add_device only once.
1156e9e2a7c1SMarcin Wojtas * It will create second cesa driver instance
1157e9e2a7c1SMarcin Wojtas * with the same FDT node as first instance.
1158e9e2a7c1SMarcin Wojtas * When second driver reach this function,
1159e9e2a7c1SMarcin Wojtas * it will be configured to use second cesa engine
1160e9e2a7c1SMarcin Wojtas */
1161e9e2a7c1SMarcin Wojtas if (engine_idx == 0)
1162e9e2a7c1SMarcin Wojtas simplebus_add_device(device_get_parent(dev), ofw_bus_get_node(dev),
1163e9e2a7c1SMarcin Wojtas 0, "cesa", 1, NULL);
1164e9e2a7c1SMarcin Wojtas
1165e9e2a7c1SMarcin Wojtas engine_idx++;
1166e9e2a7c1SMarcin Wojtas
1167e9e2a7c1SMarcin Wojtas return (cesa_attach_late(dev));
1168e9e2a7c1SMarcin Wojtas }
1169e9e2a7c1SMarcin Wojtas
1170e9e2a7c1SMarcin Wojtas static int
cesa_attach_late(device_t dev)1171e9e2a7c1SMarcin Wojtas cesa_attach_late(device_t dev)
1172e9e2a7c1SMarcin Wojtas {
11738817e1bfSRafal Jaworowski struct cesa_softc *sc;
117481526b83SWojciech Macek uint32_t d, r, val;
11758817e1bfSRafal Jaworowski int error;
11768817e1bfSRafal Jaworowski int i;
11778817e1bfSRafal Jaworowski
11788817e1bfSRafal Jaworowski sc = device_get_softc(dev);
11798817e1bfSRafal Jaworowski sc->sc_blocked = 0;
11808817e1bfSRafal Jaworowski sc->sc_error = 0;
11818817e1bfSRafal Jaworowski sc->sc_dev = dev;
11828817e1bfSRafal Jaworowski
11838817e1bfSRafal Jaworowski soc_id(&d, &r);
11848817e1bfSRafal Jaworowski
11858817e1bfSRafal Jaworowski switch (d) {
1186c247b3caSZbigniew Bodek case MV_DEV_88F6828:
118776b73da7SWojciech Macek case MV_DEV_88F6820:
118876b73da7SWojciech Macek case MV_DEV_88F6810:
11898817e1bfSRafal Jaworowski sc->sc_tperr = 0;
11908817e1bfSRafal Jaworowski break;
11918817e1bfSRafal Jaworowski default:
11928817e1bfSRafal Jaworowski return (ENXIO);
11938817e1bfSRafal Jaworowski }
11948817e1bfSRafal Jaworowski
119581526b83SWojciech Macek sc->sc_soc_id = d;
119681526b83SWojciech Macek
11978817e1bfSRafal Jaworowski /* Initialize mutexes */
11988817e1bfSRafal Jaworowski mtx_init(&sc->sc_sc_lock, device_get_nameunit(dev),
11998817e1bfSRafal Jaworowski "CESA Shared Data", MTX_DEF);
12008817e1bfSRafal Jaworowski mtx_init(&sc->sc_tdesc_lock, device_get_nameunit(dev),
12018817e1bfSRafal Jaworowski "CESA TDMA Descriptors Pool", MTX_DEF);
12028817e1bfSRafal Jaworowski mtx_init(&sc->sc_sdesc_lock, device_get_nameunit(dev),
12038817e1bfSRafal Jaworowski "CESA SA Descriptors Pool", MTX_DEF);
12048817e1bfSRafal Jaworowski mtx_init(&sc->sc_requests_lock, device_get_nameunit(dev),
12058817e1bfSRafal Jaworowski "CESA Requests Pool", MTX_DEF);
12068817e1bfSRafal Jaworowski mtx_init(&sc->sc_sessions_lock, device_get_nameunit(dev),
12078817e1bfSRafal Jaworowski "CESA Sessions Pool", MTX_DEF);
12088817e1bfSRafal Jaworowski
12098817e1bfSRafal Jaworowski /* Allocate I/O and IRQ resources */
12108817e1bfSRafal Jaworowski error = bus_alloc_resources(dev, cesa_res_spec, sc->sc_res);
12118817e1bfSRafal Jaworowski if (error) {
12128817e1bfSRafal Jaworowski device_printf(dev, "could not allocate resources\n");
12138817e1bfSRafal Jaworowski goto err0;
12148817e1bfSRafal Jaworowski }
12158817e1bfSRafal Jaworowski
121699eef682SGrzegorz Bernacki /* Acquire SRAM base address */
1217e9e2a7c1SMarcin Wojtas if (!ofw_bus_is_compatible(dev, "marvell,armada-38x-crypto"))
121899eef682SGrzegorz Bernacki error = cesa_setup_sram(sc);
1219e9e2a7c1SMarcin Wojtas else
1220e9e2a7c1SMarcin Wojtas error = cesa_setup_sram_armada(sc);
1221e9e2a7c1SMarcin Wojtas
122299eef682SGrzegorz Bernacki if (error) {
122399eef682SGrzegorz Bernacki device_printf(dev, "could not setup SRAM\n");
122499eef682SGrzegorz Bernacki goto err1;
122599eef682SGrzegorz Bernacki }
122699eef682SGrzegorz Bernacki
12278817e1bfSRafal Jaworowski /* Setup interrupt handler */
122822c7a031SZbigniew Bodek error = bus_setup_intr(dev, sc->sc_res[RES_CESA_IRQ], INTR_TYPE_NET |
122922c7a031SZbigniew Bodek INTR_MPSAFE, NULL, cesa_intr, sc, &(sc->sc_icookie));
12308817e1bfSRafal Jaworowski if (error) {
12318817e1bfSRafal Jaworowski device_printf(dev, "could not setup engine completion irq\n");
12325188e3ccSZbigniew Bodek goto err2;
12338817e1bfSRafal Jaworowski }
12348817e1bfSRafal Jaworowski
12358817e1bfSRafal Jaworowski /* Create DMA tag for processed data */
12366ac6f295SScott Long error = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
12378817e1bfSRafal Jaworowski 1, 0, /* alignment, boundary */
12388817e1bfSRafal Jaworowski BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
12398817e1bfSRafal Jaworowski BUS_SPACE_MAXADDR, /* highaddr */
12408817e1bfSRafal Jaworowski NULL, NULL, /* filtfunc, filtfuncarg */
12418817e1bfSRafal Jaworowski CESA_MAX_REQUEST_SIZE, /* maxsize */
12428817e1bfSRafal Jaworowski CESA_MAX_FRAGMENTS, /* nsegments */
12438817e1bfSRafal Jaworowski CESA_MAX_REQUEST_SIZE, 0, /* maxsegsz, flags */
12448817e1bfSRafal Jaworowski NULL, NULL, /* lockfunc, lockfuncarg */
12458817e1bfSRafal Jaworowski &sc->sc_data_dtag); /* dmat */
12468817e1bfSRafal Jaworowski if (error)
12475188e3ccSZbigniew Bodek goto err3;
12488817e1bfSRafal Jaworowski
12498817e1bfSRafal Jaworowski /* Initialize data structures: TDMA Descriptors Pool */
12508817e1bfSRafal Jaworowski error = cesa_alloc_dma_mem(sc, &sc->sc_tdesc_cdm,
12518817e1bfSRafal Jaworowski CESA_TDMA_DESCRIPTORS * sizeof(struct cesa_tdma_hdesc));
12528817e1bfSRafal Jaworowski if (error)
12535188e3ccSZbigniew Bodek goto err4;
12548817e1bfSRafal Jaworowski
12558817e1bfSRafal Jaworowski STAILQ_INIT(&sc->sc_free_tdesc);
12568817e1bfSRafal Jaworowski for (i = 0; i < CESA_TDMA_DESCRIPTORS; i++) {
12578817e1bfSRafal Jaworowski sc->sc_tdesc[i].ctd_cthd =
12588817e1bfSRafal Jaworowski (struct cesa_tdma_hdesc *)(sc->sc_tdesc_cdm.cdm_vaddr) + i;
12598817e1bfSRafal Jaworowski sc->sc_tdesc[i].ctd_cthd_paddr = sc->sc_tdesc_cdm.cdm_paddr +
12608817e1bfSRafal Jaworowski (i * sizeof(struct cesa_tdma_hdesc));
12618817e1bfSRafal Jaworowski STAILQ_INSERT_TAIL(&sc->sc_free_tdesc, &sc->sc_tdesc[i],
12628817e1bfSRafal Jaworowski ctd_stq);
12638817e1bfSRafal Jaworowski }
12648817e1bfSRafal Jaworowski
12658817e1bfSRafal Jaworowski /* Initialize data structures: SA Descriptors Pool */
12668817e1bfSRafal Jaworowski error = cesa_alloc_dma_mem(sc, &sc->sc_sdesc_cdm,
12678817e1bfSRafal Jaworowski CESA_SA_DESCRIPTORS * sizeof(struct cesa_sa_hdesc));
12688817e1bfSRafal Jaworowski if (error)
12695188e3ccSZbigniew Bodek goto err5;
12708817e1bfSRafal Jaworowski
12718817e1bfSRafal Jaworowski STAILQ_INIT(&sc->sc_free_sdesc);
12728817e1bfSRafal Jaworowski for (i = 0; i < CESA_SA_DESCRIPTORS; i++) {
12738817e1bfSRafal Jaworowski sc->sc_sdesc[i].csd_cshd =
12748817e1bfSRafal Jaworowski (struct cesa_sa_hdesc *)(sc->sc_sdesc_cdm.cdm_vaddr) + i;
12758817e1bfSRafal Jaworowski sc->sc_sdesc[i].csd_cshd_paddr = sc->sc_sdesc_cdm.cdm_paddr +
12768817e1bfSRafal Jaworowski (i * sizeof(struct cesa_sa_hdesc));
12778817e1bfSRafal Jaworowski STAILQ_INSERT_TAIL(&sc->sc_free_sdesc, &sc->sc_sdesc[i],
12788817e1bfSRafal Jaworowski csd_stq);
12798817e1bfSRafal Jaworowski }
12808817e1bfSRafal Jaworowski
12818817e1bfSRafal Jaworowski /* Initialize data structures: Requests Pool */
12828817e1bfSRafal Jaworowski error = cesa_alloc_dma_mem(sc, &sc->sc_requests_cdm,
12838817e1bfSRafal Jaworowski CESA_REQUESTS * sizeof(struct cesa_sa_data));
12848817e1bfSRafal Jaworowski if (error)
12855188e3ccSZbigniew Bodek goto err6;
12868817e1bfSRafal Jaworowski
12878817e1bfSRafal Jaworowski STAILQ_INIT(&sc->sc_free_requests);
12888817e1bfSRafal Jaworowski STAILQ_INIT(&sc->sc_ready_requests);
12898817e1bfSRafal Jaworowski STAILQ_INIT(&sc->sc_queued_requests);
12908817e1bfSRafal Jaworowski for (i = 0; i < CESA_REQUESTS; i++) {
12918817e1bfSRafal Jaworowski sc->sc_requests[i].cr_csd =
12928817e1bfSRafal Jaworowski (struct cesa_sa_data *)(sc->sc_requests_cdm.cdm_vaddr) + i;
12938817e1bfSRafal Jaworowski sc->sc_requests[i].cr_csd_paddr =
12948817e1bfSRafal Jaworowski sc->sc_requests_cdm.cdm_paddr +
12958817e1bfSRafal Jaworowski (i * sizeof(struct cesa_sa_data));
12968817e1bfSRafal Jaworowski
12978817e1bfSRafal Jaworowski /* Preallocate DMA maps */
12988817e1bfSRafal Jaworowski error = bus_dmamap_create(sc->sc_data_dtag, 0,
12998817e1bfSRafal Jaworowski &sc->sc_requests[i].cr_dmap);
13008817e1bfSRafal Jaworowski if (error && i > 0) {
13018817e1bfSRafal Jaworowski i--;
13028817e1bfSRafal Jaworowski do {
13038817e1bfSRafal Jaworowski bus_dmamap_destroy(sc->sc_data_dtag,
13048817e1bfSRafal Jaworowski sc->sc_requests[i].cr_dmap);
13058817e1bfSRafal Jaworowski } while (i--);
13068817e1bfSRafal Jaworowski
13075188e3ccSZbigniew Bodek goto err7;
13088817e1bfSRafal Jaworowski }
13098817e1bfSRafal Jaworowski
13108817e1bfSRafal Jaworowski STAILQ_INSERT_TAIL(&sc->sc_free_requests, &sc->sc_requests[i],
13118817e1bfSRafal Jaworowski cr_stq);
13128817e1bfSRafal Jaworowski }
13138817e1bfSRafal Jaworowski
13148817e1bfSRafal Jaworowski /*
13158817e1bfSRafal Jaworowski * Initialize TDMA:
13168817e1bfSRafal Jaworowski * - Burst limit: 128 bytes,
13178817e1bfSRafal Jaworowski * - Outstanding reads enabled,
13188817e1bfSRafal Jaworowski * - No byte-swap.
13198817e1bfSRafal Jaworowski */
132081526b83SWojciech Macek val = CESA_TDMA_CR_DBL128 | CESA_TDMA_CR_SBL128 |
132181526b83SWojciech Macek CESA_TDMA_CR_ORDEN | CESA_TDMA_CR_NBS | CESA_TDMA_CR_ENABLE;
132281526b83SWojciech Macek
132381526b83SWojciech Macek if (sc->sc_soc_id == MV_DEV_88F6828 ||
132481526b83SWojciech Macek sc->sc_soc_id == MV_DEV_88F6820 ||
132581526b83SWojciech Macek sc->sc_soc_id == MV_DEV_88F6810)
132681526b83SWojciech Macek val |= CESA_TDMA_NUM_OUTSTAND;
132781526b83SWojciech Macek
132881526b83SWojciech Macek CESA_TDMA_WRITE(sc, CESA_TDMA_CR, val);
13298817e1bfSRafal Jaworowski
13308817e1bfSRafal Jaworowski /*
13318817e1bfSRafal Jaworowski * Initialize SA:
13328817e1bfSRafal Jaworowski * - SA descriptor is present at beginning of CESA SRAM,
13338817e1bfSRafal Jaworowski * - Multi-packet chain mode,
13348817e1bfSRafal Jaworowski * - Cooperation with TDMA enabled.
13358817e1bfSRafal Jaworowski */
133622c7a031SZbigniew Bodek CESA_REG_WRITE(sc, CESA_SA_DPR, 0);
133722c7a031SZbigniew Bodek CESA_REG_WRITE(sc, CESA_SA_CR, CESA_SA_CR_ACTIVATE_TDMA |
13388817e1bfSRafal Jaworowski CESA_SA_CR_WAIT_FOR_TDMA | CESA_SA_CR_MULTI_MODE);
13398817e1bfSRafal Jaworowski
13408817e1bfSRafal Jaworowski /* Unmask interrupts */
134122c7a031SZbigniew Bodek CESA_REG_WRITE(sc, CESA_ICR, 0);
134222c7a031SZbigniew Bodek CESA_REG_WRITE(sc, CESA_ICM, CESA_ICM_ACCTDMA | sc->sc_tperr);
134322c7a031SZbigniew Bodek CESA_TDMA_WRITE(sc, CESA_TDMA_ECR, 0);
134422c7a031SZbigniew Bodek CESA_TDMA_WRITE(sc, CESA_TDMA_EMR, CESA_TDMA_EMR_MISS |
13458817e1bfSRafal Jaworowski CESA_TDMA_EMR_DOUBLE_HIT | CESA_TDMA_EMR_BOTH_HIT |
13468817e1bfSRafal Jaworowski CESA_TDMA_EMR_DATA_ERROR);
13478817e1bfSRafal Jaworowski
13488817e1bfSRafal Jaworowski /* Register in OCF */
13491b0909d5SConrad Meyer sc->sc_cid = crypto_get_driverid(dev, sizeof(struct cesa_session),
13501b0909d5SConrad Meyer CRYPTOCAP_F_HARDWARE);
1351c247b3caSZbigniew Bodek if (sc->sc_cid < 0) {
13528817e1bfSRafal Jaworowski device_printf(dev, "could not get crypto driver id\n");
13535188e3ccSZbigniew Bodek goto err8;
13548817e1bfSRafal Jaworowski }
13558817e1bfSRafal Jaworowski
13568817e1bfSRafal Jaworowski return (0);
13575188e3ccSZbigniew Bodek err8:
13588817e1bfSRafal Jaworowski for (i = 0; i < CESA_REQUESTS; i++)
13598817e1bfSRafal Jaworowski bus_dmamap_destroy(sc->sc_data_dtag,
13608817e1bfSRafal Jaworowski sc->sc_requests[i].cr_dmap);
13615188e3ccSZbigniew Bodek err7:
13628817e1bfSRafal Jaworowski cesa_free_dma_mem(&sc->sc_requests_cdm);
13635188e3ccSZbigniew Bodek err6:
13648817e1bfSRafal Jaworowski cesa_free_dma_mem(&sc->sc_sdesc_cdm);
13655188e3ccSZbigniew Bodek err5:
13668817e1bfSRafal Jaworowski cesa_free_dma_mem(&sc->sc_tdesc_cdm);
13675188e3ccSZbigniew Bodek err4:
13688817e1bfSRafal Jaworowski bus_dma_tag_destroy(sc->sc_data_dtag);
13695188e3ccSZbigniew Bodek err3:
137022c7a031SZbigniew Bodek bus_teardown_intr(dev, sc->sc_res[RES_CESA_IRQ], sc->sc_icookie);
13715188e3ccSZbigniew Bodek err2:
137281526b83SWojciech Macek if (sc->sc_soc_id == MV_DEV_88F6828 ||
137381526b83SWojciech Macek sc->sc_soc_id == MV_DEV_88F6820 ||
137481526b83SWojciech Macek sc->sc_soc_id == MV_DEV_88F6810)
13752cf4c169SZbigniew Bodek pmap_unmapdev(sc->sc_sram_base_va, sc->sc_sram_size);
13768817e1bfSRafal Jaworowski err1:
13778817e1bfSRafal Jaworowski bus_release_resources(dev, cesa_res_spec, sc->sc_res);
13788817e1bfSRafal Jaworowski err0:
13798817e1bfSRafal Jaworowski mtx_destroy(&sc->sc_sessions_lock);
13808817e1bfSRafal Jaworowski mtx_destroy(&sc->sc_requests_lock);
13818817e1bfSRafal Jaworowski mtx_destroy(&sc->sc_sdesc_lock);
13828817e1bfSRafal Jaworowski mtx_destroy(&sc->sc_tdesc_lock);
13838817e1bfSRafal Jaworowski mtx_destroy(&sc->sc_sc_lock);
13848817e1bfSRafal Jaworowski return (ENXIO);
13858817e1bfSRafal Jaworowski }
13868817e1bfSRafal Jaworowski
13878817e1bfSRafal Jaworowski static int
cesa_detach(device_t dev)13888817e1bfSRafal Jaworowski cesa_detach(device_t dev)
13898817e1bfSRafal Jaworowski {
13908817e1bfSRafal Jaworowski struct cesa_softc *sc;
13918817e1bfSRafal Jaworowski int i;
13928817e1bfSRafal Jaworowski
13938817e1bfSRafal Jaworowski sc = device_get_softc(dev);
13948817e1bfSRafal Jaworowski
13958817e1bfSRafal Jaworowski /* TODO: Wait for queued requests completion before shutdown. */
13968817e1bfSRafal Jaworowski
13978817e1bfSRafal Jaworowski /* Mask interrupts */
139822c7a031SZbigniew Bodek CESA_REG_WRITE(sc, CESA_ICM, 0);
139922c7a031SZbigniew Bodek CESA_TDMA_WRITE(sc, CESA_TDMA_EMR, 0);
14008817e1bfSRafal Jaworowski
14018817e1bfSRafal Jaworowski /* Unregister from OCF */
14028817e1bfSRafal Jaworowski crypto_unregister_all(sc->sc_cid);
14038817e1bfSRafal Jaworowski
14048817e1bfSRafal Jaworowski /* Free DMA Maps */
14058817e1bfSRafal Jaworowski for (i = 0; i < CESA_REQUESTS; i++)
14068817e1bfSRafal Jaworowski bus_dmamap_destroy(sc->sc_data_dtag,
14078817e1bfSRafal Jaworowski sc->sc_requests[i].cr_dmap);
14088817e1bfSRafal Jaworowski
14098817e1bfSRafal Jaworowski /* Free DMA Memory */
14108817e1bfSRafal Jaworowski cesa_free_dma_mem(&sc->sc_requests_cdm);
14118817e1bfSRafal Jaworowski cesa_free_dma_mem(&sc->sc_sdesc_cdm);
14128817e1bfSRafal Jaworowski cesa_free_dma_mem(&sc->sc_tdesc_cdm);
14138817e1bfSRafal Jaworowski
14148817e1bfSRafal Jaworowski /* Free DMA Tag */
14158817e1bfSRafal Jaworowski bus_dma_tag_destroy(sc->sc_data_dtag);
14168817e1bfSRafal Jaworowski
14178817e1bfSRafal Jaworowski /* Stop interrupt */
141822c7a031SZbigniew Bodek bus_teardown_intr(dev, sc->sc_res[RES_CESA_IRQ], sc->sc_icookie);
14198817e1bfSRafal Jaworowski
14208817e1bfSRafal Jaworowski /* Relase I/O and IRQ resources */
14218817e1bfSRafal Jaworowski bus_release_resources(dev, cesa_res_spec, sc->sc_res);
14228817e1bfSRafal Jaworowski
14235188e3ccSZbigniew Bodek /* Unmap SRAM memory */
142481526b83SWojciech Macek if (sc->sc_soc_id == MV_DEV_88F6828 ||
142581526b83SWojciech Macek sc->sc_soc_id == MV_DEV_88F6820 ||
142681526b83SWojciech Macek sc->sc_soc_id == MV_DEV_88F6810)
14272cf4c169SZbigniew Bodek pmap_unmapdev(sc->sc_sram_base_va, sc->sc_sram_size);
142881526b83SWojciech Macek
1429453130d9SPedro F. Giffuni /* Destroy mutexes */
14308817e1bfSRafal Jaworowski mtx_destroy(&sc->sc_sessions_lock);
14318817e1bfSRafal Jaworowski mtx_destroy(&sc->sc_requests_lock);
14328817e1bfSRafal Jaworowski mtx_destroy(&sc->sc_sdesc_lock);
14338817e1bfSRafal Jaworowski mtx_destroy(&sc->sc_tdesc_lock);
14348817e1bfSRafal Jaworowski mtx_destroy(&sc->sc_sc_lock);
14358817e1bfSRafal Jaworowski
14368817e1bfSRafal Jaworowski return (0);
14378817e1bfSRafal Jaworowski }
14388817e1bfSRafal Jaworowski
14398817e1bfSRafal Jaworowski static void
cesa_intr(void * arg)14408817e1bfSRafal Jaworowski cesa_intr(void *arg)
14418817e1bfSRafal Jaworowski {
14428817e1bfSRafal Jaworowski STAILQ_HEAD(, cesa_request) requests;
14438817e1bfSRafal Jaworowski struct cesa_request *cr, *tmp;
14448817e1bfSRafal Jaworowski struct cesa_softc *sc;
14458817e1bfSRafal Jaworowski uint32_t ecr, icr;
1446c0341432SJohn Baldwin uint8_t hash[HASH_MAX_LEN];
14478817e1bfSRafal Jaworowski int blocked;
14488817e1bfSRafal Jaworowski
14498817e1bfSRafal Jaworowski sc = arg;
14508817e1bfSRafal Jaworowski
14518817e1bfSRafal Jaworowski /* Ack interrupt */
145222c7a031SZbigniew Bodek ecr = CESA_TDMA_READ(sc, CESA_TDMA_ECR);
145322c7a031SZbigniew Bodek CESA_TDMA_WRITE(sc, CESA_TDMA_ECR, 0);
145422c7a031SZbigniew Bodek icr = CESA_REG_READ(sc, CESA_ICR);
145522c7a031SZbigniew Bodek CESA_REG_WRITE(sc, CESA_ICR, 0);
14568817e1bfSRafal Jaworowski
14578817e1bfSRafal Jaworowski /* Check for TDMA errors */
14588817e1bfSRafal Jaworowski if (ecr & CESA_TDMA_ECR_MISS) {
14598817e1bfSRafal Jaworowski device_printf(sc->sc_dev, "TDMA Miss error detected!\n");
14608817e1bfSRafal Jaworowski sc->sc_error = EIO;
14618817e1bfSRafal Jaworowski }
14628817e1bfSRafal Jaworowski
14638817e1bfSRafal Jaworowski if (ecr & CESA_TDMA_ECR_DOUBLE_HIT) {
14648817e1bfSRafal Jaworowski device_printf(sc->sc_dev, "TDMA Double Hit error detected!\n");
14658817e1bfSRafal Jaworowski sc->sc_error = EIO;
14668817e1bfSRafal Jaworowski }
14678817e1bfSRafal Jaworowski
14688817e1bfSRafal Jaworowski if (ecr & CESA_TDMA_ECR_BOTH_HIT) {
14698817e1bfSRafal Jaworowski device_printf(sc->sc_dev, "TDMA Both Hit error detected!\n");
14708817e1bfSRafal Jaworowski sc->sc_error = EIO;
14718817e1bfSRafal Jaworowski }
14728817e1bfSRafal Jaworowski
14738817e1bfSRafal Jaworowski if (ecr & CESA_TDMA_ECR_DATA_ERROR) {
14748817e1bfSRafal Jaworowski device_printf(sc->sc_dev, "TDMA Data error detected!\n");
14758817e1bfSRafal Jaworowski sc->sc_error = EIO;
14768817e1bfSRafal Jaworowski }
14778817e1bfSRafal Jaworowski
14788817e1bfSRafal Jaworowski /* Check for CESA errors */
14798817e1bfSRafal Jaworowski if (icr & sc->sc_tperr) {
14808817e1bfSRafal Jaworowski device_printf(sc->sc_dev, "CESA SRAM Parity error detected!\n");
14818817e1bfSRafal Jaworowski sc->sc_error = EIO;
14828817e1bfSRafal Jaworowski }
14838817e1bfSRafal Jaworowski
14848817e1bfSRafal Jaworowski /* If there is nothing more to do, return */
14858817e1bfSRafal Jaworowski if ((icr & CESA_ICR_ACCTDMA) == 0)
14868817e1bfSRafal Jaworowski return;
14878817e1bfSRafal Jaworowski
14888817e1bfSRafal Jaworowski /* Get all finished requests */
14898817e1bfSRafal Jaworowski CESA_LOCK(sc, requests);
14908817e1bfSRafal Jaworowski STAILQ_INIT(&requests);
14918817e1bfSRafal Jaworowski STAILQ_CONCAT(&requests, &sc->sc_queued_requests);
14928817e1bfSRafal Jaworowski STAILQ_INIT(&sc->sc_queued_requests);
14938817e1bfSRafal Jaworowski CESA_UNLOCK(sc, requests);
14948817e1bfSRafal Jaworowski
14958817e1bfSRafal Jaworowski /* Execute all ready requests */
14968817e1bfSRafal Jaworowski cesa_execute(sc);
14978817e1bfSRafal Jaworowski
14988817e1bfSRafal Jaworowski /* Process completed requests */
14998817e1bfSRafal Jaworowski cesa_sync_dma_mem(&sc->sc_requests_cdm, BUS_DMASYNC_POSTREAD |
15008817e1bfSRafal Jaworowski BUS_DMASYNC_POSTWRITE);
15018817e1bfSRafal Jaworowski
15028817e1bfSRafal Jaworowski STAILQ_FOREACH_SAFE(cr, &requests, cr_stq, tmp) {
15038817e1bfSRafal Jaworowski bus_dmamap_sync(sc->sc_data_dtag, cr->cr_dmap,
15048817e1bfSRafal Jaworowski BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
15058817e1bfSRafal Jaworowski
15068817e1bfSRafal Jaworowski cr->cr_crp->crp_etype = sc->sc_error;
1507c0341432SJohn Baldwin if (cr->cr_cs->cs_hlen != 0 && cr->cr_crp->crp_etype == 0) {
1508c0341432SJohn Baldwin if (cr->cr_crp->crp_op & CRYPTO_OP_VERIFY_DIGEST) {
1509c0341432SJohn Baldwin crypto_copydata(cr->cr_crp,
1510c0341432SJohn Baldwin cr->cr_crp->crp_digest_start,
1511c0341432SJohn Baldwin cr->cr_cs->cs_hlen, hash);
1512c0341432SJohn Baldwin if (timingsafe_bcmp(hash, cr->cr_csd->csd_hash,
1513c0341432SJohn Baldwin cr->cr_cs->cs_hlen) != 0)
1514c0341432SJohn Baldwin cr->cr_crp->crp_etype = EBADMSG;
1515c0341432SJohn Baldwin } else
1516c0341432SJohn Baldwin crypto_copyback(cr->cr_crp,
1517c0341432SJohn Baldwin cr->cr_crp->crp_digest_start,
15188817e1bfSRafal Jaworowski cr->cr_cs->cs_hlen, cr->cr_csd->csd_hash);
1519c0341432SJohn Baldwin }
15208817e1bfSRafal Jaworowski crypto_done(cr->cr_crp);
15218817e1bfSRafal Jaworowski cesa_free_request(sc, cr);
15228817e1bfSRafal Jaworowski }
15238817e1bfSRafal Jaworowski
15248817e1bfSRafal Jaworowski cesa_sync_dma_mem(&sc->sc_requests_cdm, BUS_DMASYNC_PREREAD |
15258817e1bfSRafal Jaworowski BUS_DMASYNC_PREWRITE);
15268817e1bfSRafal Jaworowski
15278817e1bfSRafal Jaworowski sc->sc_error = 0;
15288817e1bfSRafal Jaworowski
15298817e1bfSRafal Jaworowski /* Unblock driver if it ran out of resources */
15308817e1bfSRafal Jaworowski CESA_LOCK(sc, sc);
15318817e1bfSRafal Jaworowski blocked = sc->sc_blocked;
15328817e1bfSRafal Jaworowski sc->sc_blocked = 0;
15338817e1bfSRafal Jaworowski CESA_UNLOCK(sc, sc);
15348817e1bfSRafal Jaworowski
15358817e1bfSRafal Jaworowski if (blocked)
15368817e1bfSRafal Jaworowski crypto_unblock(sc->sc_cid, blocked);
15378817e1bfSRafal Jaworowski }
15388817e1bfSRafal Jaworowski
1539c0341432SJohn Baldwin static bool
cesa_cipher_supported(const struct crypto_session_params * csp)1540c0341432SJohn Baldwin cesa_cipher_supported(const struct crypto_session_params *csp)
1541c0341432SJohn Baldwin {
1542c0341432SJohn Baldwin
1543c0341432SJohn Baldwin switch (csp->csp_cipher_alg) {
1544c0341432SJohn Baldwin case CRYPTO_AES_CBC:
1545c0341432SJohn Baldwin if (csp->csp_ivlen != AES_BLOCK_LEN)
1546c0341432SJohn Baldwin return (false);
1547c0341432SJohn Baldwin break;
1548c0341432SJohn Baldwin default:
1549c0341432SJohn Baldwin return (false);
1550c0341432SJohn Baldwin }
1551c0341432SJohn Baldwin
1552c0341432SJohn Baldwin if (csp->csp_cipher_klen > CESA_MAX_KEY_LEN)
1553c0341432SJohn Baldwin return (false);
1554c0341432SJohn Baldwin
1555c0341432SJohn Baldwin return (true);
1556c0341432SJohn Baldwin }
1557c0341432SJohn Baldwin
1558c0341432SJohn Baldwin static bool
cesa_auth_supported(struct cesa_softc * sc,const struct crypto_session_params * csp)1559c0341432SJohn Baldwin cesa_auth_supported(struct cesa_softc *sc,
1560c0341432SJohn Baldwin const struct crypto_session_params *csp)
1561c0341432SJohn Baldwin {
1562c0341432SJohn Baldwin
1563c0341432SJohn Baldwin switch (csp->csp_auth_alg) {
1564c0341432SJohn Baldwin case CRYPTO_SHA2_256_HMAC:
1565c0341432SJohn Baldwin if (!(sc->sc_soc_id == MV_DEV_88F6828 ||
1566c0341432SJohn Baldwin sc->sc_soc_id == MV_DEV_88F6820 ||
1567c0341432SJohn Baldwin sc->sc_soc_id == MV_DEV_88F6810))
1568c0341432SJohn Baldwin return (false);
1569c0341432SJohn Baldwin /* FALLTHROUGH */
1570c0341432SJohn Baldwin case CRYPTO_SHA1:
1571c0341432SJohn Baldwin case CRYPTO_SHA1_HMAC:
1572c0341432SJohn Baldwin break;
1573c0341432SJohn Baldwin default:
1574c0341432SJohn Baldwin return (false);
1575c0341432SJohn Baldwin }
1576c0341432SJohn Baldwin
1577c0341432SJohn Baldwin if (csp->csp_auth_klen > CESA_MAX_MKEY_LEN)
1578c0341432SJohn Baldwin return (false);
1579c0341432SJohn Baldwin
1580c0341432SJohn Baldwin return (true);
1581c0341432SJohn Baldwin }
1582c0341432SJohn Baldwin
15838817e1bfSRafal Jaworowski static int
cesa_probesession(device_t dev,const struct crypto_session_params * csp)1584c0341432SJohn Baldwin cesa_probesession(device_t dev, const struct crypto_session_params *csp)
1585c0341432SJohn Baldwin {
1586c0341432SJohn Baldwin struct cesa_softc *sc;
1587c0341432SJohn Baldwin
1588c0341432SJohn Baldwin sc = device_get_softc(dev);
1589c0341432SJohn Baldwin if (csp->csp_flags != 0)
1590c0341432SJohn Baldwin return (EINVAL);
1591c0341432SJohn Baldwin switch (csp->csp_mode) {
1592c0341432SJohn Baldwin case CSP_MODE_DIGEST:
1593c0341432SJohn Baldwin if (!cesa_auth_supported(sc, csp))
1594c0341432SJohn Baldwin return (EINVAL);
1595c0341432SJohn Baldwin break;
1596c0341432SJohn Baldwin case CSP_MODE_CIPHER:
1597c0341432SJohn Baldwin if (!cesa_cipher_supported(csp))
1598c0341432SJohn Baldwin return (EINVAL);
1599c0341432SJohn Baldwin break;
1600c0341432SJohn Baldwin case CSP_MODE_ETA:
1601c0341432SJohn Baldwin if (!cesa_auth_supported(sc, csp) ||
1602c0341432SJohn Baldwin !cesa_cipher_supported(csp))
1603c0341432SJohn Baldwin return (EINVAL);
1604c0341432SJohn Baldwin break;
1605c0341432SJohn Baldwin default:
1606c0341432SJohn Baldwin return (EINVAL);
1607c0341432SJohn Baldwin }
1608c0341432SJohn Baldwin return (CRYPTODEV_PROBE_HARDWARE);
1609c0341432SJohn Baldwin }
1610c0341432SJohn Baldwin
1611c0341432SJohn Baldwin static int
cesa_newsession(device_t dev,crypto_session_t cses,const struct crypto_session_params * csp)1612c0341432SJohn Baldwin cesa_newsession(device_t dev, crypto_session_t cses,
1613c0341432SJohn Baldwin const struct crypto_session_params *csp)
16148817e1bfSRafal Jaworowski {
16158817e1bfSRafal Jaworowski struct cesa_session *cs;
16168817e1bfSRafal Jaworowski int error;
16178817e1bfSRafal Jaworowski
16188817e1bfSRafal Jaworowski error = 0;
16198817e1bfSRafal Jaworowski
16208817e1bfSRafal Jaworowski /* Allocate session */
16211b0909d5SConrad Meyer cs = crypto_get_driver_session(cses);
16228817e1bfSRafal Jaworowski
16238817e1bfSRafal Jaworowski /* Prepare CESA configuration */
16248817e1bfSRafal Jaworowski cs->cs_config = 0;
16258817e1bfSRafal Jaworowski cs->cs_ivlen = 1;
16268817e1bfSRafal Jaworowski cs->cs_mblen = 1;
16278817e1bfSRafal Jaworowski
1628c0341432SJohn Baldwin switch (csp->csp_cipher_alg) {
16298817e1bfSRafal Jaworowski case CRYPTO_AES_CBC:
16308817e1bfSRafal Jaworowski cs->cs_config |= CESA_CSHD_AES | CESA_CSHD_CBC;
16318817e1bfSRafal Jaworowski cs->cs_ivlen = AES_BLOCK_LEN;
16328817e1bfSRafal Jaworowski break;
16338817e1bfSRafal Jaworowski }
16348817e1bfSRafal Jaworowski
1635c0341432SJohn Baldwin switch (csp->csp_auth_alg) {
16368817e1bfSRafal Jaworowski case CRYPTO_SHA1:
16378817e1bfSRafal Jaworowski cs->cs_mblen = 1;
1638c0341432SJohn Baldwin cs->cs_hlen = (csp->csp_auth_mlen == 0) ? SHA1_HASH_LEN :
1639c0341432SJohn Baldwin csp->csp_auth_mlen;
16401e964f8aSZbigniew Bodek cs->cs_config |= CESA_CSHD_SHA1;
16418817e1bfSRafal Jaworowski break;
16428817e1bfSRafal Jaworowski case CRYPTO_SHA1_HMAC:
1643590adc1bSConrad Meyer cs->cs_mblen = SHA1_BLOCK_LEN;
1644c0341432SJohn Baldwin cs->cs_hlen = (csp->csp_auth_mlen == 0) ? SHA1_HASH_LEN :
1645c0341432SJohn Baldwin csp->csp_auth_mlen;
16461e964f8aSZbigniew Bodek cs->cs_config |= CESA_CSHD_SHA1_HMAC;
16471e964f8aSZbigniew Bodek if (cs->cs_hlen == CESA_HMAC_TRUNC_LEN)
16481e964f8aSZbigniew Bodek cs->cs_config |= CESA_CSHD_96_BIT_HMAC;
16498817e1bfSRafal Jaworowski break;
1650b07df6e5SZbigniew Bodek case CRYPTO_SHA2_256_HMAC:
1651590adc1bSConrad Meyer cs->cs_mblen = SHA2_256_BLOCK_LEN;
1652c0341432SJohn Baldwin cs->cs_hlen = (csp->csp_auth_mlen == 0) ? SHA2_256_HASH_LEN :
1653c0341432SJohn Baldwin csp->csp_auth_mlen;
1654b07df6e5SZbigniew Bodek cs->cs_config |= CESA_CSHD_SHA2_256_HMAC;
1655b07df6e5SZbigniew Bodek break;
16568817e1bfSRafal Jaworowski }
16578817e1bfSRafal Jaworowski
16588817e1bfSRafal Jaworowski /* Save cipher key */
1659c0341432SJohn Baldwin if (csp->csp_cipher_key != NULL) {
1660c0341432SJohn Baldwin memcpy(cs->cs_key, csp->csp_cipher_key,
1661c0341432SJohn Baldwin csp->csp_cipher_klen);
1662c0341432SJohn Baldwin if (csp->csp_cipher_alg == CRYPTO_AES_CBC)
1663c0341432SJohn Baldwin error = cesa_prep_aes_key(cs, csp);
16648817e1bfSRafal Jaworowski }
16658817e1bfSRafal Jaworowski
16668817e1bfSRafal Jaworowski /* Save digest key */
1667c0341432SJohn Baldwin if (csp->csp_auth_key != NULL)
1668c0341432SJohn Baldwin cesa_set_mkey(cs, csp->csp_auth_alg, csp->csp_auth_key,
1669c0341432SJohn Baldwin csp->csp_auth_klen);
16708817e1bfSRafal Jaworowski
16711b0909d5SConrad Meyer return (error);
16728817e1bfSRafal Jaworowski }
16738817e1bfSRafal Jaworowski
16748817e1bfSRafal Jaworowski static int
cesa_process(device_t dev,struct cryptop * crp,int hint)16758817e1bfSRafal Jaworowski cesa_process(device_t dev, struct cryptop *crp, int hint)
16768817e1bfSRafal Jaworowski {
1677c0341432SJohn Baldwin const struct crypto_session_params *csp;
16788817e1bfSRafal Jaworowski struct cesa_request *cr;
16798817e1bfSRafal Jaworowski struct cesa_session *cs;
16808817e1bfSRafal Jaworowski struct cesa_softc *sc;
16818817e1bfSRafal Jaworowski int error;
16828817e1bfSRafal Jaworowski
16838817e1bfSRafal Jaworowski sc = device_get_softc(dev);
16848817e1bfSRafal Jaworowski error = 0;
16858817e1bfSRafal Jaworowski
16861b0909d5SConrad Meyer cs = crypto_get_driver_session(crp->crp_session);
1687c0341432SJohn Baldwin csp = crypto_get_params(crp->crp_session);
16888817e1bfSRafal Jaworowski
16898817e1bfSRafal Jaworowski /* Check and parse input */
169072d874faSJohn Baldwin if (crypto_buffer_len(&crp->crp_buf) > CESA_MAX_REQUEST_SIZE) {
16918817e1bfSRafal Jaworowski crp->crp_etype = E2BIG;
16928817e1bfSRafal Jaworowski crypto_done(crp);
16938817e1bfSRafal Jaworowski return (0);
16948817e1bfSRafal Jaworowski }
16958817e1bfSRafal Jaworowski
1696c0341432SJohn Baldwin /*
1697c0341432SJohn Baldwin * For requests with AAD, only requests where the AAD is
1698c0341432SJohn Baldwin * immediately adjacent to the payload are supported.
1699c0341432SJohn Baldwin */
1700c0341432SJohn Baldwin if (crp->crp_aad_length != 0 &&
1701c0341432SJohn Baldwin (crp->crp_aad_start + crp->crp_aad_length) !=
1702c0341432SJohn Baldwin crp->crp_payload_start) {
17038817e1bfSRafal Jaworowski crp->crp_etype = EINVAL;
17048817e1bfSRafal Jaworowski crypto_done(crp);
17058817e1bfSRafal Jaworowski return (0);
17068817e1bfSRafal Jaworowski }
17078817e1bfSRafal Jaworowski
17088817e1bfSRafal Jaworowski /*
17098817e1bfSRafal Jaworowski * Get request descriptor. Block driver if there is no free
17108817e1bfSRafal Jaworowski * descriptors in pool.
17118817e1bfSRafal Jaworowski */
17128817e1bfSRafal Jaworowski cr = cesa_alloc_request(sc);
17138817e1bfSRafal Jaworowski if (!cr) {
17148817e1bfSRafal Jaworowski CESA_LOCK(sc, sc);
17158817e1bfSRafal Jaworowski sc->sc_blocked = CRYPTO_SYMQ;
17168817e1bfSRafal Jaworowski CESA_UNLOCK(sc, sc);
17178817e1bfSRafal Jaworowski return (ERESTART);
17188817e1bfSRafal Jaworowski }
17198817e1bfSRafal Jaworowski
17208817e1bfSRafal Jaworowski /* Prepare request */
17218817e1bfSRafal Jaworowski cr->cr_crp = crp;
17228817e1bfSRafal Jaworowski cr->cr_cs = cs;
17238817e1bfSRafal Jaworowski
17248817e1bfSRafal Jaworowski CESA_LOCK(sc, sessions);
17258817e1bfSRafal Jaworowski cesa_sync_desc(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
17268817e1bfSRafal Jaworowski
172729fe41ddSJohn Baldwin if (csp->csp_cipher_alg != 0)
172829fe41ddSJohn Baldwin crypto_read_iv(crp, cr->cr_csd->csd_iv);
17298817e1bfSRafal Jaworowski
1730c0341432SJohn Baldwin if (crp->crp_cipher_key != NULL) {
1731c0341432SJohn Baldwin memcpy(cs->cs_key, crp->crp_cipher_key,
1732c0341432SJohn Baldwin csp->csp_cipher_klen);
1733c0341432SJohn Baldwin if (csp->csp_cipher_alg == CRYPTO_AES_CBC)
1734c0341432SJohn Baldwin error = cesa_prep_aes_key(cs, csp);
17358817e1bfSRafal Jaworowski }
17368817e1bfSRafal Jaworowski
1737c0341432SJohn Baldwin if (!error && crp->crp_auth_key != NULL)
1738c0341432SJohn Baldwin cesa_set_mkey(cs, csp->csp_auth_alg, crp->crp_auth_key,
1739c0341432SJohn Baldwin csp->csp_auth_klen);
17408817e1bfSRafal Jaworowski
17418817e1bfSRafal Jaworowski /* Convert request to chain of TDMA and SA descriptors */
17428817e1bfSRafal Jaworowski if (!error)
1743c0341432SJohn Baldwin error = cesa_create_chain(sc, csp, cr);
17448817e1bfSRafal Jaworowski
17458817e1bfSRafal Jaworowski cesa_sync_desc(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
17468817e1bfSRafal Jaworowski CESA_UNLOCK(sc, sessions);
17478817e1bfSRafal Jaworowski
17488817e1bfSRafal Jaworowski if (error) {
17498817e1bfSRafal Jaworowski cesa_free_request(sc, cr);
17508817e1bfSRafal Jaworowski crp->crp_etype = error;
17518817e1bfSRafal Jaworowski crypto_done(crp);
17528817e1bfSRafal Jaworowski return (0);
17538817e1bfSRafal Jaworowski }
17548817e1bfSRafal Jaworowski
17558817e1bfSRafal Jaworowski bus_dmamap_sync(sc->sc_data_dtag, cr->cr_dmap, BUS_DMASYNC_PREREAD |
17568817e1bfSRafal Jaworowski BUS_DMASYNC_PREWRITE);
17578817e1bfSRafal Jaworowski
17588817e1bfSRafal Jaworowski /* Enqueue request to execution */
17598817e1bfSRafal Jaworowski cesa_enqueue_request(sc, cr);
17608817e1bfSRafal Jaworowski
17618817e1bfSRafal Jaworowski /* Start execution, if we have no more requests in queue */
17628817e1bfSRafal Jaworowski if ((hint & CRYPTO_HINT_MORE) == 0)
17638817e1bfSRafal Jaworowski cesa_execute(sc);
17648817e1bfSRafal Jaworowski
17658817e1bfSRafal Jaworowski return (0);
17668817e1bfSRafal Jaworowski }
1767