From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from EUR03-DB5-obe.outbound.protection.outlook.com (mail-eopbgr40089.outbound.protection.outlook.com [40.107.4.89]) by dpdk.org (Postfix) with ESMTP id 984951B0F7 for ; Mon, 22 Oct 2018 16:17:46 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=BCM3FDEezI9+1058fvI6nRnDubU04tmktpZLFJvLh4s=; b=KEmajM11aMYwYOCs/S4/N6/ClQg1cmxX1fZ9bXSRLXw5P+LPIBU4GsBGTibg5HNhZLq159RwZdJkUPviz50Z6PufBDHn0mwa24xWMyaLJD6TjCUICt0XB9S0gnJFU/1j+Yfsd0anLtkmSC4uPndXDNvx+shBDaKBalWMZMaw9hg= Received: from HE1PR04MB1530.eurprd04.prod.outlook.com (10.164.48.20) by HE1PR04MB1051.eurprd04.prod.outlook.com (10.162.26.148) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1250.30; Mon, 22 Oct 2018 14:17:43 +0000 Received: from HE1PR04MB1530.eurprd04.prod.outlook.com ([fe80::95cd:198:d5c5:59a4]) by HE1PR04MB1530.eurprd04.prod.outlook.com ([fe80::95cd:198:d5c5:59a4%5]) with mapi id 15.20.1250.028; Mon, 22 Oct 2018 14:17:43 +0000 From: Gagandeep Singh To: "dev@dpdk.org" , Akhil Goyal CC: Gagandeep Singh , Hemant Agrawal Thread-Topic: [PATCH v4 05/15] crypto/caam_jr: add basic job ring routines Thread-Index: AQHUahH/PpaLtG7UaEes0sFuAVy5Uw== Date: Mon, 22 Oct 2018 14:17:43 +0000 Message-ID: <20181022141657.4727-6-g.singh@nxp.com> References: <20181022133021.11264-1-g.singh@nxp.com> <20181022141657.4727-1-g.singh@nxp.com> In-Reply-To: <20181022141657.4727-1-g.singh@nxp.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: BM1PR0101CA0025.INDPRD01.PROD.OUTLOOK.COM (2603:1096:b00:1a::11) To HE1PR04MB1530.eurprd04.prod.outlook.com (2a01:111:e400:59a8::20) authentication-results: spf=none (sender IP is ) smtp.mailfrom=G.Singh@nxp.com; x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [14.142.187.166] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; HE1PR04MB1051; 6:xVBiIFVi0pHs57gNIUmAElVIRWMYBHyv9o0lF2T4iMXa1fb+mhaLCXZ9Vq/8U0gM5XuAyzxJ9CkwpQB1xIyIE2B+s1Oqqe0lObm6alJ/RYDgOrCYSXJeR1rWfDQgMbw3jeel4bPwgkttTn/lE15VHJ7xuVBjkUrpW4bV5p8dQQULkYV9Uk4Lk9KiokkepRmFBqZNzWoPzTQat6zWFjZZjUUooosPWyh8NFjILPDQu9HOz8ZZl9NrmdVqaOFdV9Upr6+48pONkYqHKsGOvVU8QEGAh5WgoKZrmB3DQ8irN2mRqvt/7Dm+F0+mGj4tmjRXYEuskBcTmYQrysme32jVFX1DJcP7X0n19apUWbRpUBr3rF+5HzxphsGrBNH6ZicsNMDhT22RQbN66beuVCs6aXsOyQA/FiTeRZuqRb9vNCnlnyc0hvflVDoTfK9qMhARcoLPnWdcSa/WfCqmclJG+w==; 5:W0jDgUmh+1HTdXXYFd6lPK39ij7rS5yT1jTpO2VgwxeMhvflLeo8wlCmqQw0ITRVC7kn0hsXPC6Me/9Qlh6o/WlRM//FaVzPSN2r+JCBgeOWa0dui9wmogYFaZx/FL8MfcPABQsMmvi+gfORMxYjRaz6Fvqsq3PJ2Hrkv/yrY8Q=; 7:pgAJnNA1zQ8xt7jwxkuxuxi2ZAx0s9eljiGlK6n1t6edPnW92Rvt5heYOidp+MEJ+7YLzk2AhAYP5F25lTvhcMu1nG32Y3gThYXvjd8ui1TzMUzMCuHHIdOyyHR8M9+fQtiLDMUJNDTJw2WYl2Fok+zgPixy4J/U3vS+7shB0ugJ8z8D8fz5mF1fCnvRg9zmZ4dUSbNj1EqUhPEblNWq15HC5llTJ3PduGamJicmY9GZm/ZmMw7Uj6uvHDjASEB3 x-ms-office365-filtering-correlation-id: 117a0c15-cab8-47a4-89c2-08d6382921cc x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0; PCL:0; RULEID:(7020095)(4652040)(8989299)(5600074)(711020)(4618075)(4534185)(7168020)(4627221)(201703031133081)(201702281549075)(8990200)(2017052603328)(7153060)(7193020); SRVR:HE1PR04MB1051; x-ms-traffictypediagnostic: HE1PR04MB1051: x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(192374486261705)(185117386973197); x-ms-exchange-senderadcheck: 1 x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(8211001083)(6040522)(2401047)(8121501046)(5005006)(10201501046)(3231355)(944501410)(52105095)(3002001)(93006095)(93001095)(6055026)(148016)(149066)(150057)(6041310)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123558120)(20161123562045)(20161123564045)(20161123560045)(201708071742011)(7699051)(76991095); SRVR:HE1PR04MB1051; BCL:0; PCL:0; RULEID:; SRVR:HE1PR04MB1051; x-forefront-prvs: 08331F819E x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(366004)(396003)(39860400002)(136003)(346002)(376002)(199004)(189003)(14454004)(486006)(256004)(14444005)(3846002)(7736002)(4326008)(2616005)(2501003)(11346002)(6116002)(54906003)(2900100001)(110136005)(476003)(78486010)(72206003)(6512007)(8936002)(316002)(36756003)(6436002)(81166006)(71200400001)(71190400001)(4744004)(478600001)(68736007)(86362001)(52116002)(53936002)(186003)(76176011)(446003)(81156014)(6486002)(8676002)(102836004)(26005)(55236004)(66066001)(6636002)(105586002)(5660300001)(305945005)(106356001)(25786009)(2906002)(97736004)(386003)(6506007)(99286004)(1076002)(5250100002); DIR:OUT; SFP:1101; SCL:1; SRVR:HE1PR04MB1051; H:HE1PR04MB1530.eurprd04.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; received-spf: None (protection.outlook.com: nxp.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: NcFFYqImotqewuOE96tBeFTUcDSGmYC1hmQUr6/TEblsDt9lHIcQq0FlUmLY5eQ0iFvN7Q+nbTsZdEZ3H/9WBFZjnavhoJP0clC/eOulKshkvwdCt99uz3YbDqBm76qwOl0UrTx80507+6i3ixaBQVzSO594zF4cSLyeAo7OBHbs62iKIgphRn0wmutHfqaCTdbUFj3Z40Qla0JQAxORL+fRyivmNsblh1eFnAiwJPKXXaH+o1QUWz9aFEM0TAGyAG8OSzEPlFnI7N4pADV3Fo5rlqAX2mLugmlFKB1zaM7yd6cjYau+z3nw6CX8mpvZbbtuEjNAnWl3+E8Fbjq4hzcopMF94rD09rDLz7yIfD0= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 117a0c15-cab8-47a4-89c2-08d6382921cc X-MS-Exchange-CrossTenant-originalarrivaltime: 22 Oct 2018 14:17:43.7526 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR04MB1051 Subject: [dpdk-dev] [PATCH v4 05/15] crypto/caam_jr: add basic job ring routines X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 22 Oct 2018 14:17:47 -0000 This patch adds following job ring routines - init_job_ring (configure hw/sw resources) - shutdown_job_ring (releases hw/sw resources) - close_job_ring (flush job ring) Signed-off-by: Gagandeep Singh Signed-off-by: Hemant Agrawal Acked-by: Akhil Goyal --- drivers/crypto/caam_jr/caam_jr.c | 329 ++++++++++++++++++++++++++++++- 1 file changed, 327 insertions(+), 2 deletions(-) diff --git a/drivers/crypto/caam_jr/caam_jr.c b/drivers/crypto/caam_jr/caam= _jr.c index 6d25967a1..1a04a3ce6 100644 --- a/drivers/crypto/caam_jr/caam_jr.c +++ b/drivers/crypto/caam_jr/caam_jr.c @@ -16,12 +16,142 @@ #include #include =20 +#include +#include +#include #include =20 +/* RTA header files */ +#include +#include + #define CRYPTODEV_NAME_CAAM_JR_PMD crypto_caam_jr static uint8_t cryptodev_driver_id; int caam_jr_logtype; =20 +enum rta_sec_era rta_sec_era; + +/* Lists the states possible for the SEC user space driver. */ +enum sec_driver_state_e { + SEC_DRIVER_STATE_IDLE, /* Driver not initialized */ + SEC_DRIVER_STATE_STARTED, /* Driver initialized and can be used*/ + SEC_DRIVER_STATE_RELEASE, /* Driver release is in progress */ +}; + +/* Job rings used for communication with SEC HW */ +static struct sec_job_ring_t g_job_rings[MAX_SEC_JOB_RINGS]; + +/* The current state of SEC user space driver */ +static enum sec_driver_state_e g_driver_state =3D SEC_DRIVER_STATE_IDLE; + +/* The number of job rings used by SEC user space driver */ +static int g_job_rings_no; +static int g_job_rings_max; + +/* @brief Poll the HW for already processed jobs in the JR + * and silently discard the available jobs or notify them to UA + * with indicated error code. + * + * @param [in,out] job_ring The job ring to poll. + * @param [in] do_notify Can be #TRUE or #FALSE. Indicates if + * descriptors are to be discarded + * or notified to UA with given error_cod= e. + * @param [out] notified_descs Number of notified descriptors. Can be N= ULL + * if do_notify is #FALSE + */ +static void +hw_flush_job_ring(struct sec_job_ring_t *job_ring, + uint32_t do_notify, + uint32_t *notified_descs) +{ + int32_t jobs_no_to_discard =3D 0; + int32_t discarded_descs_no =3D 0; + + PMD_INIT_FUNC_TRACE(); + CAAM_JR_DEBUG("Jr[%p] pi[%d] ci[%d].Flushing jr notify desc=3D[%d]", + job_ring, job_ring->pidx, job_ring->cidx, do_notify); + + jobs_no_to_discard =3D hw_get_no_finished_jobs(job_ring); + + /* Discard all jobs */ + CAAM_JR_DEBUG("Jr[%p] pi[%d] ci[%d].Discarding %d descs", + job_ring, job_ring->pidx, job_ring->cidx, + jobs_no_to_discard); + + while (jobs_no_to_discard > discarded_descs_no) { + discarded_descs_no++; + /* Now increment the consumer index for the current job ring, + * AFTER saving job in temporary location! + * Increment the consumer index for the current job ring + */ + job_ring->cidx =3D SEC_CIRCULAR_COUNTER(job_ring->cidx, + SEC_JOB_RING_SIZE); + + hw_remove_entries(job_ring, 1); + } + + if (do_notify =3D=3D true) { + ASSERT(notified_descs !=3D NULL); + *notified_descs =3D discarded_descs_no; + } +} + + +/* @brief Flush job rings of any processed descs. + * The processed descs are silently dropped, + * WITHOUT being notified to UA. + */ +static void +close_job_ring(struct sec_job_ring_t *job_ring) +{ + PMD_INIT_FUNC_TRACE(); + if (job_ring->irq_fd) { + /* Producer index is frozen. If consumer index is not equal + * with producer index, then we have descs to flush. + */ + while (job_ring->pidx !=3D job_ring->cidx) + hw_flush_job_ring(job_ring, false, NULL); + + /* free the uio job ring */ + free_job_ring(job_ring->irq_fd); + job_ring->irq_fd =3D 0; + caam_jr_dma_free(job_ring->input_ring); + caam_jr_dma_free(job_ring->output_ring); + g_job_rings_no--; + } +} + +/** @brief Release the software and hardware resources tied to a job ring. + * @param [in] job_ring The job ring + * + * @retval 0 for success + * @retval -1 for error + */ +static int +shutdown_job_ring(struct sec_job_ring_t *job_ring) +{ + int ret =3D 0; + + PMD_INIT_FUNC_TRACE(); + ASSERT(job_ring !=3D NULL); + ret =3D hw_shutdown_job_ring(job_ring); + SEC_ASSERT(ret =3D=3D 0, ret, + "Failed to shutdown hardware job ring %p", + job_ring); + + if (job_ring->coalescing_en) + hw_job_ring_disable_coalescing(job_ring); + + if (job_ring->jr_mode !=3D SEC_NOTIFICATION_TYPE_POLL) { + ret =3D caam_jr_disable_irqs(job_ring->irq_fd); + SEC_ASSERT(ret =3D=3D 0, ret, + "Failed to disable irqs for job ring %p", + job_ring); + } + + return ret; +} + /* * @brief Release the resources used by the SEC user space driver. * @@ -39,28 +169,195 @@ int caam_jr_logtype; static int caam_jr_dev_uninit(struct rte_cryptodev *dev) { + struct sec_job_ring_t *internals; + + PMD_INIT_FUNC_TRACE(); if (dev =3D=3D NULL) return -ENODEV; =20 + internals =3D dev->data->dev_private; + rte_free(dev->security_ctx); + + /* If any descriptors in flight , poll and wait + * until all descriptors are received and silently discarded. + */ + if (internals) { + shutdown_job_ring(internals); + close_job_ring(internals); + rte_mempool_free(internals->ctx_pool); + } + CAAM_JR_INFO("Closing crypto device %s", dev->data->name); =20 - return 0; + /* last caam jr instance) */ + if (g_job_rings_no =3D=3D 0) + g_driver_state =3D SEC_DRIVER_STATE_IDLE; + + return SEC_SUCCESS; +} + +/* @brief Initialize the software and hardware resources tied to a job rin= g. + * @param [in] jr_mode; Model to be used by SEC Driver to receive + * notifications from SEC. Can be either + * of the three: #SEC_NOTIFICATION_TYPE_NAPI + * #SEC_NOTIFICATION_TYPE_IRQ or + * #SEC_NOTIFICATION_TYPE_POLL + * @param [in] NAPI_mode The NAPI work mode to configure a job ring at + * startup. Used only when #SEC_NOTIFICATION_TYPE + * is set to #SEC_NOTIFICATION_TYPE_NAPI. + * @param [in] irq_coalescing_timer This value determines the maximum + * amount of time after processing a + * descriptor before raising an interrupt. + * @param [in] irq_coalescing_count This value determines how many + * descriptors are completed before + * raising an interrupt. + * @param [in] reg_base_addr, The job ring base address register + * @param [in] irq_id The job ring interrupt identification number. + * @retval job_ring_handle for successful job ring configuration + * @retval NULL on error + * + */ +static void * +init_job_ring(void *reg_base_addr, uint32_t irq_id) +{ + struct sec_job_ring_t *job_ring =3D NULL; + int i, ret =3D 0; + int jr_mode =3D SEC_NOTIFICATION_TYPE_POLL; + int napi_mode =3D 0; + int irq_coalescing_timer =3D 0; + int irq_coalescing_count =3D 0; + + for (i =3D 0; i < MAX_SEC_JOB_RINGS; i++) { + if (g_job_rings[i].irq_fd =3D=3D 0) { + job_ring =3D &g_job_rings[i]; + g_job_rings_no++; + break; + } + } + if (job_ring =3D=3D NULL) { + CAAM_JR_ERR("No free job ring\n"); + return NULL; + } + + job_ring->register_base_addr =3D reg_base_addr; + job_ring->jr_mode =3D jr_mode; + job_ring->napi_mode =3D 0; + job_ring->irq_fd =3D irq_id; + + /* Allocate mem for input and output ring */ + + /* Allocate memory for input ring */ + job_ring->input_ring =3D caam_jr_dma_mem_alloc(L1_CACHE_BYTES, + SEC_DMA_MEM_INPUT_RING_SIZE); + memset(job_ring->input_ring, 0, SEC_DMA_MEM_INPUT_RING_SIZE); + + /* Allocate memory for output ring */ + job_ring->output_ring =3D caam_jr_dma_mem_alloc(L1_CACHE_BYTES, + SEC_DMA_MEM_OUTPUT_RING_SIZE); + memset(job_ring->output_ring, 0, SEC_DMA_MEM_OUTPUT_RING_SIZE); + + /* Reset job ring in SEC hw and configure job ring registers */ + ret =3D hw_reset_job_ring(job_ring); + if (ret !=3D 0) { + CAAM_JR_ERR("Failed to reset hardware job ring"); + goto cleanup; + } + + if (jr_mode =3D=3D SEC_NOTIFICATION_TYPE_NAPI) { + /* When SEC US driver works in NAPI mode, the UA can select + * if the driver starts with IRQs on or off. + */ + if (napi_mode =3D=3D SEC_STARTUP_INTERRUPT_MODE) { + CAAM_JR_INFO("Enabling DONE IRQ generationon job ring - %p", + job_ring); + ret =3D caam_jr_enable_irqs(job_ring->irq_fd); + if (ret !=3D 0) { + CAAM_JR_ERR("Failed to enable irqs for job ring"); + goto cleanup; + } + } + } else if (jr_mode =3D=3D SEC_NOTIFICATION_TYPE_IRQ) { + /* When SEC US driver works in pure interrupt mode, + * IRQ's are always enabled. + */ + CAAM_JR_INFO("Enabling DONE IRQ generation on job ring - %p", + job_ring); + ret =3D caam_jr_enable_irqs(job_ring->irq_fd); + if (ret !=3D 0) { + CAAM_JR_ERR("Failed to enable irqs for job ring"); + goto cleanup; + } + } + if (irq_coalescing_timer || irq_coalescing_count) { + hw_job_ring_set_coalescing_param(job_ring, + irq_coalescing_timer, + irq_coalescing_count); + + hw_job_ring_enable_coalescing(job_ring); + job_ring->coalescing_en =3D 1; + } + + job_ring->jr_state =3D SEC_JOB_RING_STATE_STARTED; + job_ring->max_nb_queue_pairs =3D RTE_CAAM_MAX_NB_SEC_QPS; + job_ring->max_nb_sessions =3D RTE_CAAM_JR_PMD_MAX_NB_SESSIONS; + + return job_ring; +cleanup: + caam_jr_dma_free(job_ring->output_ring); + caam_jr_dma_free(job_ring->input_ring); + return NULL; } =20 + static int caam_jr_dev_init(const char *name, struct rte_vdev_device *vdev, struct rte_cryptodev_pmd_init_params *init_params) { struct rte_cryptodev *dev; + struct uio_job_ring *job_ring; + char str[RTE_CRYPTODEV_NAME_MAX_LEN]; =20 PMD_INIT_FUNC_TRACE(); =20 + /* Validate driver state */ + if (g_driver_state =3D=3D SEC_DRIVER_STATE_IDLE) { + g_job_rings_max =3D sec_configure(); + if (!g_job_rings_max) { + CAAM_JR_ERR("No job ring detected on UIO !!!!"); + return -1; + } + /* Update driver state */ + g_driver_state =3D SEC_DRIVER_STATE_STARTED; + } + + if (g_job_rings_no >=3D g_job_rings_max) { + CAAM_JR_ERR("No more job rings available max=3D%d!!!!", + g_job_rings_max); + return -1; + } + + job_ring =3D config_job_ring(); + if (job_ring =3D=3D NULL) { + CAAM_JR_ERR("failed to create job ring"); + goto init_error; + } + + snprintf(str, sizeof(str), "caam_jr%d", job_ring->jr_id); + dev =3D rte_cryptodev_pmd_create(name, &vdev->device, init_params); if (dev =3D=3D NULL) { CAAM_JR_ERR("failed to create cryptodev vdev"); goto cleanup; } + /*TODO free it during teardown*/ + dev->data->dev_private =3D init_job_ring(job_ring->register_base_addr, + job_ring->uio_fd); + + if (!dev->data->dev_private) { + CAAM_JR_ERR("Ring memory allocation failed\n"); + goto cleanup2; + } =20 dev->driver_id =3D cryptodev_driver_id; dev->dev_ops =3D NULL; @@ -78,7 +375,12 @@ caam_jr_dev_init(const char *name, =20 return 0; =20 +cleanup2: + caam_jr_dev_uninit(dev); + rte_cryptodev_pmd_release_device(dev); cleanup: + free_job_ring(job_ring->uio_fd); +init_error: CAAM_JR_ERR("driver %s: cryptodev_caam_jr_create failed", init_params->name); =20 @@ -91,7 +393,7 @@ cryptodev_caam_jr_probe(struct rte_vdev_device *vdev) { struct rte_cryptodev_pmd_init_params init_params =3D { "", - 128, + sizeof(struct sec_job_ring_t), rte_socket_id(), RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS }; @@ -105,6 +407,29 @@ cryptodev_caam_jr_probe(struct rte_vdev_device *vdev) input_args =3D rte_vdev_device_args(vdev); rte_cryptodev_pmd_parse_input_args(&init_params, input_args); =20 + /* if sec device version is not configured */ + if (!rta_get_sec_era()) { + const struct device_node *caam_node; + + for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") { + const uint32_t *prop =3D of_get_property(caam_node, + "fsl,sec-era", + NULL); + if (prop) { + rta_set_sec_era( + INTL_SEC_ERA(cpu_to_caam32(*prop))); + break; + } + } + } +#ifdef RTE_LIBRTE_PMD_CAAM_JR_BE + if (rta_get_sec_era() > RTA_SEC_ERA_8) { + RTE_LOG(ERR, PMD, + "CAAM is compiled in BE mode for device with sec era > 8???\n"); + return -EINVAL; + } +#endif + return caam_jr_dev_init(name, vdev, &init_params); } =20 --=20 2.17.1