Path: blob/master/drivers/crypto/intel/keembay/keembay-ocs-hcu-core.c
29268 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Intel Keem Bay OCS HCU Crypto Driver.3*4* Copyright (C) 2018-2020 Intel Corporation5*/67#include <crypto/engine.h>8#include <crypto/hmac.h>9#include <crypto/internal/hash.h>10#include <crypto/scatterwalk.h>11#include <crypto/sha2.h>12#include <crypto/sm3.h>13#include <linux/completion.h>14#include <linux/dma-mapping.h>15#include <linux/err.h>16#include <linux/interrupt.h>17#include <linux/kernel.h>18#include <linux/mod_devicetable.h>19#include <linux/module.h>20#include <linux/platform_device.h>21#include <linux/string.h>2223#include "ocs-hcu.h"2425#define DRV_NAME "keembay-ocs-hcu"2627/* Flag marking a final request. */28#define REQ_FINAL BIT(0)29/* Flag marking a HMAC request. */30#define REQ_FLAGS_HMAC BIT(1)31/* Flag set when HW HMAC is being used. */32#define REQ_FLAGS_HMAC_HW BIT(2)33/* Flag set when SW HMAC is being used. */34#define REQ_FLAGS_HMAC_SW BIT(3)3536/**37* struct ocs_hcu_ctx: OCS HCU Transform context.38* @hcu_dev: The OCS HCU device used by the transformation.39* @key: The key (used only for HMAC transformations).40* @key_len: The length of the key.41* @is_sm3_tfm: Whether or not this is an SM3 transformation.42* @is_hmac_tfm: Whether or not this is a HMAC transformation.43*/44struct ocs_hcu_ctx {45struct ocs_hcu_dev *hcu_dev;46u8 key[SHA512_BLOCK_SIZE];47size_t key_len;48bool is_sm3_tfm;49bool is_hmac_tfm;50};5152/**53* struct ocs_hcu_rctx - Context for the request.54* @hcu_dev: OCS HCU device to be used to service the request.55* @flags: Flags tracking request status.56* @algo: Algorithm to use for the request.57* @blk_sz: Block size of the transformation / request.58* @dig_sz: Digest size of the transformation / request.59* @dma_list: OCS DMA linked list.60* @hash_ctx: OCS HCU hashing context.61* @buffer: Buffer to store: partial block of data and SW HMAC62* artifacts (ipad, opad, etc.).63* @buf_cnt: Number of bytes currently stored in the buffer.64* @buf_dma_addr: The DMA address of @buffer (when mapped).65* @buf_dma_count: The number of bytes in @buffer currently DMA-mapped.66* @sg: Head of the scatterlist entries containing data.67* @sg_data_total: Total data in the SG list at any time.68* @sg_data_offset: Offset into the data of the current individual SG node.69* @sg_dma_nents: Number of sg entries mapped in dma_list.70* @nents: Number of entries in the scatterlist.71*/72struct ocs_hcu_rctx {73struct ocs_hcu_dev *hcu_dev;74u32 flags;75enum ocs_hcu_algo algo;76size_t blk_sz;77size_t dig_sz;78struct ocs_hcu_dma_list *dma_list;79struct ocs_hcu_hash_ctx hash_ctx;80/*81* Buffer is double the block size because we need space for SW HMAC82* artifacts, i.e:83* - ipad (1 block) + a possible partial block of data.84* - opad (1 block) + digest of H(k ^ ipad || m)85*/86u8 buffer[2 * SHA512_BLOCK_SIZE];87size_t buf_cnt;88dma_addr_t buf_dma_addr;89size_t buf_dma_count;90struct scatterlist *sg;91unsigned int sg_data_total;92unsigned int sg_data_offset;93unsigned int sg_dma_nents;94unsigned int nents;95};9697/**98* struct ocs_hcu_drv - Driver data99* @dev_list: The list of HCU devices.100* @lock: The lock protecting dev_list.101*/102struct ocs_hcu_drv {103struct list_head dev_list;104spinlock_t lock; /* Protects dev_list. */105};106107static struct ocs_hcu_drv ocs_hcu = {108.dev_list = LIST_HEAD_INIT(ocs_hcu.dev_list),109.lock = __SPIN_LOCK_UNLOCKED(ocs_hcu.lock),110};111112/*113* Return the total amount of data in the request; that is: the data in the114* request buffer + the data in the sg list.115*/116static inline unsigned int kmb_get_total_data(struct ocs_hcu_rctx *rctx)117{118return rctx->sg_data_total + rctx->buf_cnt;119}120121/* Move remaining content of scatter-gather list to context buffer. */122static int flush_sg_to_ocs_buffer(struct ocs_hcu_rctx *rctx)123{124size_t count;125126if (rctx->sg_data_total > (sizeof(rctx->buffer) - rctx->buf_cnt)) {127WARN(1, "%s: sg data does not fit in buffer\n", __func__);128return -EINVAL;129}130131while (rctx->sg_data_total) {132if (!rctx->sg) {133WARN(1, "%s: unexpected NULL sg\n", __func__);134return -EINVAL;135}136/*137* If current sg has been fully processed, skip to the next138* one.139*/140if (rctx->sg_data_offset == rctx->sg->length) {141rctx->sg = sg_next(rctx->sg);142rctx->sg_data_offset = 0;143continue;144}145/*146* Determine the maximum data available to copy from the node.147* Minimum of the length left in the sg node, or the total data148* in the request.149*/150count = min(rctx->sg->length - rctx->sg_data_offset,151rctx->sg_data_total);152/* Copy from scatter-list entry to context buffer. */153scatterwalk_map_and_copy(&rctx->buffer[rctx->buf_cnt],154rctx->sg, rctx->sg_data_offset,155count, 0);156157rctx->sg_data_offset += count;158rctx->sg_data_total -= count;159rctx->buf_cnt += count;160}161162return 0;163}164165static struct ocs_hcu_dev *kmb_ocs_hcu_find_dev(struct ahash_request *req)166{167struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);168struct ocs_hcu_ctx *tctx = crypto_ahash_ctx(tfm);169170/* If the HCU device for the request was previously set, return it. */171if (tctx->hcu_dev)172return tctx->hcu_dev;173174/*175* Otherwise, get the first HCU device available (there should be one176* and only one device).177*/178spin_lock_bh(&ocs_hcu.lock);179tctx->hcu_dev = list_first_entry_or_null(&ocs_hcu.dev_list,180struct ocs_hcu_dev,181list);182spin_unlock_bh(&ocs_hcu.lock);183184return tctx->hcu_dev;185}186187/* Free OCS DMA linked list and DMA-able context buffer. */188static void kmb_ocs_hcu_dma_cleanup(struct ahash_request *req,189struct ocs_hcu_rctx *rctx)190{191struct ocs_hcu_dev *hcu_dev = rctx->hcu_dev;192struct device *dev = hcu_dev->dev;193194/* Unmap rctx->buffer (if mapped). */195if (rctx->buf_dma_count) {196dma_unmap_single(dev, rctx->buf_dma_addr, rctx->buf_dma_count,197DMA_TO_DEVICE);198rctx->buf_dma_count = 0;199}200201/* Unmap req->src (if mapped). */202if (rctx->sg_dma_nents) {203dma_unmap_sg(dev, req->src, rctx->nents, DMA_TO_DEVICE);204rctx->sg_dma_nents = 0;205}206207/* Free dma_list (if allocated). */208if (rctx->dma_list) {209ocs_hcu_dma_list_free(hcu_dev, rctx->dma_list);210rctx->dma_list = NULL;211}212}213214/*215* Prepare for DMA operation:216* - DMA-map request context buffer (if needed)217* - DMA-map SG list (only the entries to be processed, see note below)218* - Allocate OCS HCU DMA linked list (number of elements = SG entries to219* process + context buffer (if not empty)).220* - Add DMA-mapped request context buffer to OCS HCU DMA list.221* - Add SG entries to DMA list.222*223* Note: if this is a final request, we process all the data in the SG list,224* otherwise we can only process up to the maximum amount of block-aligned data225* (the remainder will be put into the context buffer and processed in the next226* request).227*/228static int kmb_ocs_dma_prepare(struct ahash_request *req)229{230struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);231struct device *dev = rctx->hcu_dev->dev;232unsigned int remainder = 0;233unsigned int total;234size_t nents;235size_t count;236int rc;237int i;238239/* This function should be called only when there is data to process. */240total = kmb_get_total_data(rctx);241if (!total)242return -EINVAL;243244/*245* If this is not a final DMA (terminated DMA), the data passed to the246* HCU must be aligned to the block size; compute the remainder data to247* be processed in the next request.248*/249if (!(rctx->flags & REQ_FINAL))250remainder = total % rctx->blk_sz;251252/* Determine the number of scatter gather list entries to process. */253nents = sg_nents_for_len(req->src, rctx->sg_data_total - remainder);254255/* If there are entries to process, map them. */256if (nents) {257rctx->sg_dma_nents = dma_map_sg(dev, req->src, nents,258DMA_TO_DEVICE);259if (!rctx->sg_dma_nents) {260dev_err(dev, "Failed to MAP SG\n");261rc = -ENOMEM;262goto cleanup;263}264265/* Save the value of nents to pass to dma_unmap_sg. */266rctx->nents = nents;267268/*269* The value returned by dma_map_sg() can be < nents; so update270* nents accordingly.271*/272nents = rctx->sg_dma_nents;273}274275/*276* If context buffer is not empty, map it and add extra DMA entry for277* it.278*/279if (rctx->buf_cnt) {280rctx->buf_dma_addr = dma_map_single(dev, rctx->buffer,281rctx->buf_cnt,282DMA_TO_DEVICE);283if (dma_mapping_error(dev, rctx->buf_dma_addr)) {284dev_err(dev, "Failed to map request context buffer\n");285rc = -ENOMEM;286goto cleanup;287}288rctx->buf_dma_count = rctx->buf_cnt;289/* Increase number of dma entries. */290nents++;291}292293/* Allocate OCS HCU DMA list. */294rctx->dma_list = ocs_hcu_dma_list_alloc(rctx->hcu_dev, nents);295if (!rctx->dma_list) {296rc = -ENOMEM;297goto cleanup;298}299300/* Add request context buffer (if previously DMA-mapped) */301if (rctx->buf_dma_count) {302rc = ocs_hcu_dma_list_add_tail(rctx->hcu_dev, rctx->dma_list,303rctx->buf_dma_addr,304rctx->buf_dma_count);305if (rc)306goto cleanup;307}308309/* Add the SG nodes to be processed to the DMA linked list. */310for_each_sg(req->src, rctx->sg, rctx->sg_dma_nents, i) {311/*312* The number of bytes to add to the list entry is the minimum313* between:314* - The DMA length of the SG entry.315* - The data left to be processed.316*/317count = min(rctx->sg_data_total - remainder,318sg_dma_len(rctx->sg) - rctx->sg_data_offset);319/*320* Do not create a zero length DMA descriptor. Check in case of321* zero length SG node.322*/323if (count == 0)324continue;325/* Add sg to HCU DMA list. */326rc = ocs_hcu_dma_list_add_tail(rctx->hcu_dev,327rctx->dma_list,328rctx->sg->dma_address,329count);330if (rc)331goto cleanup;332333/* Update amount of data remaining in SG list. */334rctx->sg_data_total -= count;335336/*337* If remaining data is equal to remainder (note: 'less than'338* case should never happen in practice), we are done: update339* offset and exit the loop.340*/341if (rctx->sg_data_total <= remainder) {342WARN_ON(rctx->sg_data_total < remainder);343rctx->sg_data_offset += count;344break;345}346347/*348* If we get here is because we need to process the next sg in349* the list; set offset within the sg to 0.350*/351rctx->sg_data_offset = 0;352}353354return 0;355cleanup:356dev_err(dev, "Failed to prepare DMA.\n");357kmb_ocs_hcu_dma_cleanup(req, rctx);358359return rc;360}361362static void kmb_ocs_hcu_secure_cleanup(struct ahash_request *req)363{364struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);365366/* Clear buffer of any data. */367memzero_explicit(rctx->buffer, sizeof(rctx->buffer));368}369370static int kmb_ocs_hcu_handle_queue(struct ahash_request *req)371{372struct ocs_hcu_dev *hcu_dev = kmb_ocs_hcu_find_dev(req);373374if (!hcu_dev)375return -ENOENT;376377return crypto_transfer_hash_request_to_engine(hcu_dev->engine, req);378}379380static int prepare_ipad(struct ahash_request *req)381{382struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);383struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);384struct ocs_hcu_ctx *ctx = crypto_ahash_ctx(tfm);385int i;386387WARN(rctx->buf_cnt, "%s: Context buffer is not empty\n", __func__);388WARN(!(rctx->flags & REQ_FLAGS_HMAC_SW),389"%s: HMAC_SW flag is not set\n", __func__);390/*391* Key length must be equal to block size. If key is shorter,392* we pad it with zero (note: key cannot be longer, since393* longer keys are hashed by kmb_ocs_hcu_setkey()).394*/395if (ctx->key_len > rctx->blk_sz) {396WARN(1, "%s: Invalid key length in tfm context\n", __func__);397return -EINVAL;398}399memzero_explicit(&ctx->key[ctx->key_len],400rctx->blk_sz - ctx->key_len);401ctx->key_len = rctx->blk_sz;402/*403* Prepare IPAD for HMAC. Only done for first block.404* HMAC(k,m) = H(k ^ opad || H(k ^ ipad || m))405* k ^ ipad will be first hashed block.406* k ^ opad will be calculated in the final request.407* Only needed if not using HW HMAC.408*/409for (i = 0; i < rctx->blk_sz; i++)410rctx->buffer[i] = ctx->key[i] ^ HMAC_IPAD_VALUE;411rctx->buf_cnt = rctx->blk_sz;412413return 0;414}415416static int kmb_ocs_hcu_do_one_request(struct crypto_engine *engine, void *areq)417{418struct ahash_request *req = container_of(areq, struct ahash_request,419base);420struct ocs_hcu_dev *hcu_dev = kmb_ocs_hcu_find_dev(req);421struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);422struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);423struct ocs_hcu_ctx *tctx = crypto_ahash_ctx(tfm);424int rc;425int i;426427if (!hcu_dev) {428rc = -ENOENT;429goto error;430}431432/*433* If hardware HMAC flag is set, perform HMAC in hardware.434*435* NOTE: this flag implies REQ_FINAL && kmb_get_total_data(rctx)436*/437if (rctx->flags & REQ_FLAGS_HMAC_HW) {438/* Map input data into the HCU DMA linked list. */439rc = kmb_ocs_dma_prepare(req);440if (rc)441goto error;442443rc = ocs_hcu_hmac(hcu_dev, rctx->algo, tctx->key, tctx->key_len,444rctx->dma_list, req->result, rctx->dig_sz);445446/* Unmap data and free DMA list regardless of return code. */447kmb_ocs_hcu_dma_cleanup(req, rctx);448449/* Process previous return code. */450if (rc)451goto error;452453goto done;454}455456/* Handle update request case. */457if (!(rctx->flags & REQ_FINAL)) {458/* Update should always have input data. */459if (!kmb_get_total_data(rctx))460return -EINVAL;461462/* Map input data into the HCU DMA linked list. */463rc = kmb_ocs_dma_prepare(req);464if (rc)465goto error;466467/* Do hashing step. */468rc = ocs_hcu_hash_update(hcu_dev, &rctx->hash_ctx,469rctx->dma_list);470471/* Unmap data and free DMA list regardless of return code. */472kmb_ocs_hcu_dma_cleanup(req, rctx);473474/* Process previous return code. */475if (rc)476goto error;477478/*479* Reset request buffer count (data in the buffer was just480* processed).481*/482rctx->buf_cnt = 0;483/*484* Move remaining sg data into the request buffer, so that it485* will be processed during the next request.486*487* NOTE: we have remaining data if kmb_get_total_data() was not488* a multiple of block size.489*/490rc = flush_sg_to_ocs_buffer(rctx);491if (rc)492goto error;493494goto done;495}496497/* If we get here, this is a final request. */498499/* If there is data to process, use finup. */500if (kmb_get_total_data(rctx)) {501/* Map input data into the HCU DMA linked list. */502rc = kmb_ocs_dma_prepare(req);503if (rc)504goto error;505506/* Do hashing step. */507rc = ocs_hcu_hash_finup(hcu_dev, &rctx->hash_ctx,508rctx->dma_list,509req->result, rctx->dig_sz);510/* Free DMA list regardless of return code. */511kmb_ocs_hcu_dma_cleanup(req, rctx);512513/* Process previous return code. */514if (rc)515goto error;516517} else { /* Otherwise (if we have no data), use final. */518rc = ocs_hcu_hash_final(hcu_dev, &rctx->hash_ctx, req->result,519rctx->dig_sz);520if (rc)521goto error;522}523524/*525* If we are finalizing a SW HMAC request, we just computed the result526* of: H(k ^ ipad || m).527*528* We now need to complete the HMAC calculation with the OPAD step,529* that is, we need to compute H(k ^ opad || digest), where digest is530* the digest we just obtained, i.e., H(k ^ ipad || m).531*/532if (rctx->flags & REQ_FLAGS_HMAC_SW) {533/*534* Compute k ^ opad and store it in the request buffer (which535* is not used anymore at this point).536* Note: key has been padded / hashed already (so keylen ==537* blksz) .538*/539WARN_ON(tctx->key_len != rctx->blk_sz);540for (i = 0; i < rctx->blk_sz; i++)541rctx->buffer[i] = tctx->key[i] ^ HMAC_OPAD_VALUE;542/* Now append the digest to the rest of the buffer. */543for (i = 0; (i < rctx->dig_sz); i++)544rctx->buffer[rctx->blk_sz + i] = req->result[i];545546/* Now hash the buffer to obtain the final HMAC. */547rc = ocs_hcu_digest(hcu_dev, rctx->algo, rctx->buffer,548rctx->blk_sz + rctx->dig_sz, req->result,549rctx->dig_sz);550if (rc)551goto error;552}553554/* Perform secure clean-up. */555kmb_ocs_hcu_secure_cleanup(req);556done:557crypto_finalize_hash_request(hcu_dev->engine, req, 0);558559return 0;560561error:562kmb_ocs_hcu_secure_cleanup(req);563return rc;564}565566static int kmb_ocs_hcu_init(struct ahash_request *req)567{568struct ocs_hcu_dev *hcu_dev = kmb_ocs_hcu_find_dev(req);569struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);570struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);571struct ocs_hcu_ctx *ctx = crypto_ahash_ctx(tfm);572573if (!hcu_dev)574return -ENOENT;575576/* Initialize entire request context to zero. */577memset(rctx, 0, sizeof(*rctx));578579rctx->hcu_dev = hcu_dev;580rctx->dig_sz = crypto_ahash_digestsize(tfm);581582switch (rctx->dig_sz) {583#ifdef CONFIG_CRYPTO_DEV_KEEMBAY_OCS_HCU_HMAC_SHA224584case SHA224_DIGEST_SIZE:585rctx->blk_sz = SHA224_BLOCK_SIZE;586rctx->algo = OCS_HCU_ALGO_SHA224;587break;588#endif /* CONFIG_CRYPTO_DEV_KEEMBAY_OCS_HCU_HMAC_SHA224 */589case SHA256_DIGEST_SIZE:590rctx->blk_sz = SHA256_BLOCK_SIZE;591/*592* SHA256 and SM3 have the same digest size: use info from tfm593* context to find out which one we should use.594*/595rctx->algo = ctx->is_sm3_tfm ? OCS_HCU_ALGO_SM3 :596OCS_HCU_ALGO_SHA256;597break;598case SHA384_DIGEST_SIZE:599rctx->blk_sz = SHA384_BLOCK_SIZE;600rctx->algo = OCS_HCU_ALGO_SHA384;601break;602case SHA512_DIGEST_SIZE:603rctx->blk_sz = SHA512_BLOCK_SIZE;604rctx->algo = OCS_HCU_ALGO_SHA512;605break;606default:607return -EINVAL;608}609610/* Initialize intermediate data. */611ocs_hcu_hash_init(&rctx->hash_ctx, rctx->algo);612613/* If this a HMAC request, set HMAC flag. */614if (ctx->is_hmac_tfm)615rctx->flags |= REQ_FLAGS_HMAC;616617return 0;618}619620static int kmb_ocs_hcu_update(struct ahash_request *req)621{622struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);623int rc;624625if (!req->nbytes)626return 0;627628rctx->sg_data_total = req->nbytes;629rctx->sg_data_offset = 0;630rctx->sg = req->src;631632/*633* If we are doing HMAC, then we must use SW-assisted HMAC, since HW634* HMAC does not support context switching (there it can only be used635* with finup() or digest()).636*/637if (rctx->flags & REQ_FLAGS_HMAC &&638!(rctx->flags & REQ_FLAGS_HMAC_SW)) {639rctx->flags |= REQ_FLAGS_HMAC_SW;640rc = prepare_ipad(req);641if (rc)642return rc;643}644645/*646* If remaining sg_data fits into ctx buffer, just copy it there; we'll647* process it at the next update() or final().648*/649if (rctx->sg_data_total <= (sizeof(rctx->buffer) - rctx->buf_cnt))650return flush_sg_to_ocs_buffer(rctx);651652return kmb_ocs_hcu_handle_queue(req);653}654655/* Common logic for kmb_ocs_hcu_final() and kmb_ocs_hcu_finup(). */656static int kmb_ocs_hcu_fin_common(struct ahash_request *req)657{658struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);659struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);660struct ocs_hcu_ctx *ctx = crypto_ahash_ctx(tfm);661int rc;662663rctx->flags |= REQ_FINAL;664665/*666* If this is a HMAC request and, so far, we didn't have to switch to667* SW HMAC, check if we can use HW HMAC.668*/669if (rctx->flags & REQ_FLAGS_HMAC &&670!(rctx->flags & REQ_FLAGS_HMAC_SW)) {671/*672* If we are here, it means we never processed any data so far,673* so we can use HW HMAC, but only if there is some data to674* process (since OCS HW MAC does not support zero-length675* messages) and the key length is supported by the hardware676* (OCS HCU HW only supports length <= 64); if HW HMAC cannot677* be used, fall back to SW-assisted HMAC.678*/679if (kmb_get_total_data(rctx) &&680ctx->key_len <= OCS_HCU_HW_KEY_LEN) {681rctx->flags |= REQ_FLAGS_HMAC_HW;682} else {683rctx->flags |= REQ_FLAGS_HMAC_SW;684rc = prepare_ipad(req);685if (rc)686return rc;687}688}689690return kmb_ocs_hcu_handle_queue(req);691}692693static int kmb_ocs_hcu_final(struct ahash_request *req)694{695struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);696697rctx->sg_data_total = 0;698rctx->sg_data_offset = 0;699rctx->sg = NULL;700701return kmb_ocs_hcu_fin_common(req);702}703704static int kmb_ocs_hcu_finup(struct ahash_request *req)705{706struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);707708rctx->sg_data_total = req->nbytes;709rctx->sg_data_offset = 0;710rctx->sg = req->src;711712return kmb_ocs_hcu_fin_common(req);713}714715static int kmb_ocs_hcu_digest(struct ahash_request *req)716{717int rc = 0;718struct ocs_hcu_dev *hcu_dev = kmb_ocs_hcu_find_dev(req);719720if (!hcu_dev)721return -ENOENT;722723rc = kmb_ocs_hcu_init(req);724if (rc)725return rc;726727rc = kmb_ocs_hcu_finup(req);728729return rc;730}731732static int kmb_ocs_hcu_export(struct ahash_request *req, void *out)733{734struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);735736/* Intermediate data is always stored and applied per request. */737memcpy(out, rctx, sizeof(*rctx));738739return 0;740}741742static int kmb_ocs_hcu_import(struct ahash_request *req, const void *in)743{744struct ocs_hcu_rctx *rctx = ahash_request_ctx_dma(req);745746/* Intermediate data is always stored and applied per request. */747memcpy(rctx, in, sizeof(*rctx));748749return 0;750}751752static int kmb_ocs_hcu_setkey(struct crypto_ahash *tfm, const u8 *key,753unsigned int keylen)754{755unsigned int digestsize = crypto_ahash_digestsize(tfm);756struct ocs_hcu_ctx *ctx = crypto_ahash_ctx(tfm);757size_t blk_sz = crypto_ahash_blocksize(tfm);758struct crypto_ahash *ahash_tfm;759struct ahash_request *req;760struct crypto_wait wait;761struct scatterlist sg;762const char *alg_name;763int rc;764765/*766* Key length must be equal to block size:767* - If key is shorter, we are done for now (the key will be padded768* later on); this is to maximize the use of HW HMAC (which works769* only for keys <= 64 bytes).770* - If key is longer, we hash it.771*/772if (keylen <= blk_sz) {773memcpy(ctx->key, key, keylen);774ctx->key_len = keylen;775return 0;776}777778switch (digestsize) {779#ifdef CONFIG_CRYPTO_DEV_KEEMBAY_OCS_HCU_HMAC_SHA224780case SHA224_DIGEST_SIZE:781alg_name = "sha224-keembay-ocs";782break;783#endif /* CONFIG_CRYPTO_DEV_KEEMBAY_OCS_HCU_HMAC_SHA224 */784case SHA256_DIGEST_SIZE:785alg_name = ctx->is_sm3_tfm ? "sm3-keembay-ocs" :786"sha256-keembay-ocs";787break;788case SHA384_DIGEST_SIZE:789alg_name = "sha384-keembay-ocs";790break;791case SHA512_DIGEST_SIZE:792alg_name = "sha512-keembay-ocs";793break;794default:795return -EINVAL;796}797798ahash_tfm = crypto_alloc_ahash(alg_name, 0, 0);799if (IS_ERR(ahash_tfm))800return PTR_ERR(ahash_tfm);801802req = ahash_request_alloc(ahash_tfm, GFP_KERNEL);803if (!req) {804rc = -ENOMEM;805goto err_free_ahash;806}807808crypto_init_wait(&wait);809ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,810crypto_req_done, &wait);811crypto_ahash_clear_flags(ahash_tfm, ~0);812813sg_init_one(&sg, key, keylen);814ahash_request_set_crypt(req, &sg, ctx->key, keylen);815816rc = crypto_wait_req(crypto_ahash_digest(req), &wait);817if (rc == 0)818ctx->key_len = digestsize;819820ahash_request_free(req);821err_free_ahash:822crypto_free_ahash(ahash_tfm);823824return rc;825}826827/* Set request size and initialize tfm context. */828static void __cra_init(struct crypto_tfm *tfm, struct ocs_hcu_ctx *ctx)829{830crypto_ahash_set_reqsize_dma(__crypto_ahash_cast(tfm),831sizeof(struct ocs_hcu_rctx));832}833834static int kmb_ocs_hcu_sha_cra_init(struct crypto_tfm *tfm)835{836struct ocs_hcu_ctx *ctx = crypto_tfm_ctx(tfm);837838__cra_init(tfm, ctx);839840return 0;841}842843static int kmb_ocs_hcu_sm3_cra_init(struct crypto_tfm *tfm)844{845struct ocs_hcu_ctx *ctx = crypto_tfm_ctx(tfm);846847__cra_init(tfm, ctx);848849ctx->is_sm3_tfm = true;850851return 0;852}853854static int kmb_ocs_hcu_hmac_sm3_cra_init(struct crypto_tfm *tfm)855{856struct ocs_hcu_ctx *ctx = crypto_tfm_ctx(tfm);857858__cra_init(tfm, ctx);859860ctx->is_sm3_tfm = true;861ctx->is_hmac_tfm = true;862863return 0;864}865866static int kmb_ocs_hcu_hmac_cra_init(struct crypto_tfm *tfm)867{868struct ocs_hcu_ctx *ctx = crypto_tfm_ctx(tfm);869870__cra_init(tfm, ctx);871872ctx->is_hmac_tfm = true;873874return 0;875}876877/* Function called when 'tfm' is de-initialized. */878static void kmb_ocs_hcu_hmac_cra_exit(struct crypto_tfm *tfm)879{880struct ocs_hcu_ctx *ctx = crypto_tfm_ctx(tfm);881882/* Clear the key. */883memzero_explicit(ctx->key, sizeof(ctx->key));884}885886static struct ahash_engine_alg ocs_hcu_algs[] = {887#ifdef CONFIG_CRYPTO_DEV_KEEMBAY_OCS_HCU_HMAC_SHA224888{889.base.init = kmb_ocs_hcu_init,890.base.update = kmb_ocs_hcu_update,891.base.final = kmb_ocs_hcu_final,892.base.finup = kmb_ocs_hcu_finup,893.base.digest = kmb_ocs_hcu_digest,894.base.export = kmb_ocs_hcu_export,895.base.import = kmb_ocs_hcu_import,896.base.halg = {897.digestsize = SHA224_DIGEST_SIZE,898.statesize = sizeof(struct ocs_hcu_rctx),899.base = {900.cra_name = "sha224",901.cra_driver_name = "sha224-keembay-ocs",902.cra_priority = 255,903.cra_flags = CRYPTO_ALG_ASYNC,904.cra_blocksize = SHA224_BLOCK_SIZE,905.cra_ctxsize = sizeof(struct ocs_hcu_ctx),906.cra_alignmask = 0,907.cra_module = THIS_MODULE,908.cra_init = kmb_ocs_hcu_sha_cra_init,909}910},911.op.do_one_request = kmb_ocs_hcu_do_one_request,912},913{914.base.init = kmb_ocs_hcu_init,915.base.update = kmb_ocs_hcu_update,916.base.final = kmb_ocs_hcu_final,917.base.finup = kmb_ocs_hcu_finup,918.base.digest = kmb_ocs_hcu_digest,919.base.export = kmb_ocs_hcu_export,920.base.import = kmb_ocs_hcu_import,921.base.setkey = kmb_ocs_hcu_setkey,922.base.halg = {923.digestsize = SHA224_DIGEST_SIZE,924.statesize = sizeof(struct ocs_hcu_rctx),925.base = {926.cra_name = "hmac(sha224)",927.cra_driver_name = "hmac-sha224-keembay-ocs",928.cra_priority = 255,929.cra_flags = CRYPTO_ALG_ASYNC,930.cra_blocksize = SHA224_BLOCK_SIZE,931.cra_ctxsize = sizeof(struct ocs_hcu_ctx),932.cra_alignmask = 0,933.cra_module = THIS_MODULE,934.cra_init = kmb_ocs_hcu_hmac_cra_init,935.cra_exit = kmb_ocs_hcu_hmac_cra_exit,936}937},938.op.do_one_request = kmb_ocs_hcu_do_one_request,939},940#endif /* CONFIG_CRYPTO_DEV_KEEMBAY_OCS_HCU_HMAC_SHA224 */941{942.base.init = kmb_ocs_hcu_init,943.base.update = kmb_ocs_hcu_update,944.base.final = kmb_ocs_hcu_final,945.base.finup = kmb_ocs_hcu_finup,946.base.digest = kmb_ocs_hcu_digest,947.base.export = kmb_ocs_hcu_export,948.base.import = kmb_ocs_hcu_import,949.base.halg = {950.digestsize = SHA256_DIGEST_SIZE,951.statesize = sizeof(struct ocs_hcu_rctx),952.base = {953.cra_name = "sha256",954.cra_driver_name = "sha256-keembay-ocs",955.cra_priority = 255,956.cra_flags = CRYPTO_ALG_ASYNC,957.cra_blocksize = SHA256_BLOCK_SIZE,958.cra_ctxsize = sizeof(struct ocs_hcu_ctx),959.cra_alignmask = 0,960.cra_module = THIS_MODULE,961.cra_init = kmb_ocs_hcu_sha_cra_init,962}963},964.op.do_one_request = kmb_ocs_hcu_do_one_request,965},966{967.base.init = kmb_ocs_hcu_init,968.base.update = kmb_ocs_hcu_update,969.base.final = kmb_ocs_hcu_final,970.base.finup = kmb_ocs_hcu_finup,971.base.digest = kmb_ocs_hcu_digest,972.base.export = kmb_ocs_hcu_export,973.base.import = kmb_ocs_hcu_import,974.base.setkey = kmb_ocs_hcu_setkey,975.base.halg = {976.digestsize = SHA256_DIGEST_SIZE,977.statesize = sizeof(struct ocs_hcu_rctx),978.base = {979.cra_name = "hmac(sha256)",980.cra_driver_name = "hmac-sha256-keembay-ocs",981.cra_priority = 255,982.cra_flags = CRYPTO_ALG_ASYNC,983.cra_blocksize = SHA256_BLOCK_SIZE,984.cra_ctxsize = sizeof(struct ocs_hcu_ctx),985.cra_alignmask = 0,986.cra_module = THIS_MODULE,987.cra_init = kmb_ocs_hcu_hmac_cra_init,988.cra_exit = kmb_ocs_hcu_hmac_cra_exit,989}990},991.op.do_one_request = kmb_ocs_hcu_do_one_request,992},993{994.base.init = kmb_ocs_hcu_init,995.base.update = kmb_ocs_hcu_update,996.base.final = kmb_ocs_hcu_final,997.base.finup = kmb_ocs_hcu_finup,998.base.digest = kmb_ocs_hcu_digest,999.base.export = kmb_ocs_hcu_export,1000.base.import = kmb_ocs_hcu_import,1001.base.halg = {1002.digestsize = SM3_DIGEST_SIZE,1003.statesize = sizeof(struct ocs_hcu_rctx),1004.base = {1005.cra_name = "sm3",1006.cra_driver_name = "sm3-keembay-ocs",1007.cra_priority = 255,1008.cra_flags = CRYPTO_ALG_ASYNC,1009.cra_blocksize = SM3_BLOCK_SIZE,1010.cra_ctxsize = sizeof(struct ocs_hcu_ctx),1011.cra_alignmask = 0,1012.cra_module = THIS_MODULE,1013.cra_init = kmb_ocs_hcu_sm3_cra_init,1014}1015},1016.op.do_one_request = kmb_ocs_hcu_do_one_request,1017},1018{1019.base.init = kmb_ocs_hcu_init,1020.base.update = kmb_ocs_hcu_update,1021.base.final = kmb_ocs_hcu_final,1022.base.finup = kmb_ocs_hcu_finup,1023.base.digest = kmb_ocs_hcu_digest,1024.base.export = kmb_ocs_hcu_export,1025.base.import = kmb_ocs_hcu_import,1026.base.setkey = kmb_ocs_hcu_setkey,1027.base.halg = {1028.digestsize = SM3_DIGEST_SIZE,1029.statesize = sizeof(struct ocs_hcu_rctx),1030.base = {1031.cra_name = "hmac(sm3)",1032.cra_driver_name = "hmac-sm3-keembay-ocs",1033.cra_priority = 255,1034.cra_flags = CRYPTO_ALG_ASYNC,1035.cra_blocksize = SM3_BLOCK_SIZE,1036.cra_ctxsize = sizeof(struct ocs_hcu_ctx),1037.cra_alignmask = 0,1038.cra_module = THIS_MODULE,1039.cra_init = kmb_ocs_hcu_hmac_sm3_cra_init,1040.cra_exit = kmb_ocs_hcu_hmac_cra_exit,1041}1042},1043.op.do_one_request = kmb_ocs_hcu_do_one_request,1044},1045{1046.base.init = kmb_ocs_hcu_init,1047.base.update = kmb_ocs_hcu_update,1048.base.final = kmb_ocs_hcu_final,1049.base.finup = kmb_ocs_hcu_finup,1050.base.digest = kmb_ocs_hcu_digest,1051.base.export = kmb_ocs_hcu_export,1052.base.import = kmb_ocs_hcu_import,1053.base.halg = {1054.digestsize = SHA384_DIGEST_SIZE,1055.statesize = sizeof(struct ocs_hcu_rctx),1056.base = {1057.cra_name = "sha384",1058.cra_driver_name = "sha384-keembay-ocs",1059.cra_priority = 255,1060.cra_flags = CRYPTO_ALG_ASYNC,1061.cra_blocksize = SHA384_BLOCK_SIZE,1062.cra_ctxsize = sizeof(struct ocs_hcu_ctx),1063.cra_alignmask = 0,1064.cra_module = THIS_MODULE,1065.cra_init = kmb_ocs_hcu_sha_cra_init,1066}1067},1068.op.do_one_request = kmb_ocs_hcu_do_one_request,1069},1070{1071.base.init = kmb_ocs_hcu_init,1072.base.update = kmb_ocs_hcu_update,1073.base.final = kmb_ocs_hcu_final,1074.base.finup = kmb_ocs_hcu_finup,1075.base.digest = kmb_ocs_hcu_digest,1076.base.export = kmb_ocs_hcu_export,1077.base.import = kmb_ocs_hcu_import,1078.base.setkey = kmb_ocs_hcu_setkey,1079.base.halg = {1080.digestsize = SHA384_DIGEST_SIZE,1081.statesize = sizeof(struct ocs_hcu_rctx),1082.base = {1083.cra_name = "hmac(sha384)",1084.cra_driver_name = "hmac-sha384-keembay-ocs",1085.cra_priority = 255,1086.cra_flags = CRYPTO_ALG_ASYNC,1087.cra_blocksize = SHA384_BLOCK_SIZE,1088.cra_ctxsize = sizeof(struct ocs_hcu_ctx),1089.cra_alignmask = 0,1090.cra_module = THIS_MODULE,1091.cra_init = kmb_ocs_hcu_hmac_cra_init,1092.cra_exit = kmb_ocs_hcu_hmac_cra_exit,1093}1094},1095.op.do_one_request = kmb_ocs_hcu_do_one_request,1096},1097{1098.base.init = kmb_ocs_hcu_init,1099.base.update = kmb_ocs_hcu_update,1100.base.final = kmb_ocs_hcu_final,1101.base.finup = kmb_ocs_hcu_finup,1102.base.digest = kmb_ocs_hcu_digest,1103.base.export = kmb_ocs_hcu_export,1104.base.import = kmb_ocs_hcu_import,1105.base.halg = {1106.digestsize = SHA512_DIGEST_SIZE,1107.statesize = sizeof(struct ocs_hcu_rctx),1108.base = {1109.cra_name = "sha512",1110.cra_driver_name = "sha512-keembay-ocs",1111.cra_priority = 255,1112.cra_flags = CRYPTO_ALG_ASYNC,1113.cra_blocksize = SHA512_BLOCK_SIZE,1114.cra_ctxsize = sizeof(struct ocs_hcu_ctx),1115.cra_alignmask = 0,1116.cra_module = THIS_MODULE,1117.cra_init = kmb_ocs_hcu_sha_cra_init,1118}1119},1120.op.do_one_request = kmb_ocs_hcu_do_one_request,1121},1122{1123.base.init = kmb_ocs_hcu_init,1124.base.update = kmb_ocs_hcu_update,1125.base.final = kmb_ocs_hcu_final,1126.base.finup = kmb_ocs_hcu_finup,1127.base.digest = kmb_ocs_hcu_digest,1128.base.export = kmb_ocs_hcu_export,1129.base.import = kmb_ocs_hcu_import,1130.base.setkey = kmb_ocs_hcu_setkey,1131.base.halg = {1132.digestsize = SHA512_DIGEST_SIZE,1133.statesize = sizeof(struct ocs_hcu_rctx),1134.base = {1135.cra_name = "hmac(sha512)",1136.cra_driver_name = "hmac-sha512-keembay-ocs",1137.cra_priority = 255,1138.cra_flags = CRYPTO_ALG_ASYNC,1139.cra_blocksize = SHA512_BLOCK_SIZE,1140.cra_ctxsize = sizeof(struct ocs_hcu_ctx),1141.cra_alignmask = 0,1142.cra_module = THIS_MODULE,1143.cra_init = kmb_ocs_hcu_hmac_cra_init,1144.cra_exit = kmb_ocs_hcu_hmac_cra_exit,1145}1146},1147.op.do_one_request = kmb_ocs_hcu_do_one_request,1148},1149};11501151/* Device tree driver match. */1152static const struct of_device_id kmb_ocs_hcu_of_match[] = {1153{1154.compatible = "intel,keembay-ocs-hcu",1155},1156{}1157};1158MODULE_DEVICE_TABLE(of, kmb_ocs_hcu_of_match);11591160static void kmb_ocs_hcu_remove(struct platform_device *pdev)1161{1162struct ocs_hcu_dev *hcu_dev = platform_get_drvdata(pdev);11631164crypto_engine_unregister_ahashes(ocs_hcu_algs, ARRAY_SIZE(ocs_hcu_algs));11651166crypto_engine_exit(hcu_dev->engine);11671168spin_lock_bh(&ocs_hcu.lock);1169list_del(&hcu_dev->list);1170spin_unlock_bh(&ocs_hcu.lock);1171}11721173static int kmb_ocs_hcu_probe(struct platform_device *pdev)1174{1175struct device *dev = &pdev->dev;1176struct ocs_hcu_dev *hcu_dev;1177int rc;11781179hcu_dev = devm_kzalloc(dev, sizeof(*hcu_dev), GFP_KERNEL);1180if (!hcu_dev)1181return -ENOMEM;11821183hcu_dev->dev = dev;11841185platform_set_drvdata(pdev, hcu_dev);1186rc = dma_set_mask_and_coherent(&pdev->dev, OCS_HCU_DMA_BIT_MASK);1187if (rc)1188return rc;11891190hcu_dev->io_base = devm_platform_ioremap_resource(pdev, 0);1191if (IS_ERR(hcu_dev->io_base))1192return PTR_ERR(hcu_dev->io_base);11931194init_completion(&hcu_dev->irq_done);11951196/* Get and request IRQ. */1197hcu_dev->irq = platform_get_irq(pdev, 0);1198if (hcu_dev->irq < 0)1199return hcu_dev->irq;12001201rc = devm_request_threaded_irq(&pdev->dev, hcu_dev->irq,1202ocs_hcu_irq_handler, NULL, 0,1203"keembay-ocs-hcu", hcu_dev);1204if (rc < 0) {1205dev_err(dev, "Could not request IRQ.\n");1206return rc;1207}12081209INIT_LIST_HEAD(&hcu_dev->list);12101211spin_lock_bh(&ocs_hcu.lock);1212list_add_tail(&hcu_dev->list, &ocs_hcu.dev_list);1213spin_unlock_bh(&ocs_hcu.lock);12141215/* Initialize crypto engine */1216hcu_dev->engine = crypto_engine_alloc_init(dev, 1);1217if (!hcu_dev->engine) {1218rc = -ENOMEM;1219goto list_del;1220}12211222rc = crypto_engine_start(hcu_dev->engine);1223if (rc) {1224dev_err(dev, "Could not start engine.\n");1225goto cleanup;1226}12271228/* Security infrastructure guarantees OCS clock is enabled. */12291230rc = crypto_engine_register_ahashes(ocs_hcu_algs, ARRAY_SIZE(ocs_hcu_algs));1231if (rc) {1232dev_err(dev, "Could not register algorithms.\n");1233goto cleanup;1234}12351236return 0;12371238cleanup:1239crypto_engine_exit(hcu_dev->engine);1240list_del:1241spin_lock_bh(&ocs_hcu.lock);1242list_del(&hcu_dev->list);1243spin_unlock_bh(&ocs_hcu.lock);12441245return rc;1246}12471248/* The OCS driver is a platform device. */1249static struct platform_driver kmb_ocs_hcu_driver = {1250.probe = kmb_ocs_hcu_probe,1251.remove = kmb_ocs_hcu_remove,1252.driver = {1253.name = DRV_NAME,1254.of_match_table = kmb_ocs_hcu_of_match,1255},1256};12571258module_platform_driver(kmb_ocs_hcu_driver);12591260MODULE_LICENSE("GPL");126112621263