Path: blob/master/drivers/crypto/hisilicon/sec/sec_drv.h
26292 views
/* SPDX-License-Identifier: GPL-2.0 */1/* Copyright (c) 2016-2017 HiSilicon Limited. */23#ifndef _SEC_DRV_H_4#define _SEC_DRV_H_56#include <crypto/algapi.h>7#include <linux/kfifo.h>89#define SEC_MAX_SGE_NUM 6410#define SEC_HW_RING_NUM 31112#define SEC_CMD_RING 013#define SEC_OUTORDER_RING 114#define SEC_DBG_RING 21516/* A reasonable length to balance memory use against flexibility */17#define SEC_QUEUE_LEN 5121819#define SEC_MAX_SGE_NUM 642021struct sec_bd_info {22#define SEC_BD_W0_T_LEN_M GENMASK(4, 0)23#define SEC_BD_W0_T_LEN_S 02425#define SEC_BD_W0_C_WIDTH_M GENMASK(6, 5)26#define SEC_BD_W0_C_WIDTH_S 527#define SEC_C_WIDTH_AES_128BIT 028#define SEC_C_WIDTH_AES_8BIT 129#define SEC_C_WIDTH_AES_1BIT 230#define SEC_C_WIDTH_DES_64BIT 031#define SEC_C_WIDTH_DES_8BIT 132#define SEC_C_WIDTH_DES_1BIT 23334#define SEC_BD_W0_C_MODE_M GENMASK(9, 7)35#define SEC_BD_W0_C_MODE_S 736#define SEC_C_MODE_ECB 037#define SEC_C_MODE_CBC 138#define SEC_C_MODE_CTR 439#define SEC_C_MODE_CCM 540#define SEC_C_MODE_GCM 641#define SEC_C_MODE_XTS 74243#define SEC_BD_W0_SEQ BIT(10)44#define SEC_BD_W0_DE BIT(11)45#define SEC_BD_W0_DAT_SKIP_M GENMASK(13, 12)46#define SEC_BD_W0_DAT_SKIP_S 1247#define SEC_BD_W0_C_GRAN_SIZE_19_16_M GENMASK(17, 14)48#define SEC_BD_W0_C_GRAN_SIZE_19_16_S 144950#define SEC_BD_W0_CIPHER_M GENMASK(19, 18)51#define SEC_BD_W0_CIPHER_S 1852#define SEC_CIPHER_NULL 053#define SEC_CIPHER_ENCRYPT 154#define SEC_CIPHER_DECRYPT 25556#define SEC_BD_W0_AUTH_M GENMASK(21, 20)57#define SEC_BD_W0_AUTH_S 2058#define SEC_AUTH_NULL 059#define SEC_AUTH_MAC 160#define SEC_AUTH_VERIF 26162#define SEC_BD_W0_AI_GEN BIT(22)63#define SEC_BD_W0_CI_GEN BIT(23)64#define SEC_BD_W0_NO_HPAD BIT(24)65#define SEC_BD_W0_HM_M GENMASK(26, 25)66#define SEC_BD_W0_HM_S 2567#define SEC_BD_W0_ICV_OR_SKEY_EN_M GENMASK(28, 27)68#define SEC_BD_W0_ICV_OR_SKEY_EN_S 276970/* Multi purpose field - gran size bits for send, flag for recv */71#define SEC_BD_W0_FLAG_M GENMASK(30, 29)72#define SEC_BD_W0_C_GRAN_SIZE_21_20_M GENMASK(30, 29)73#define SEC_BD_W0_FLAG_S 2974#define SEC_BD_W0_C_GRAN_SIZE_21_20_S 297576#define SEC_BD_W0_DONE BIT(31)77u32 w0;7879#define SEC_BD_W1_AUTH_GRAN_SIZE_M GENMASK(21, 0)80#define SEC_BD_W1_AUTH_GRAN_SIZE_S 081#define SEC_BD_W1_M_KEY_EN BIT(22)82#define SEC_BD_W1_BD_INVALID BIT(23)83#define SEC_BD_W1_ADDR_TYPE BIT(24)8485#define SEC_BD_W1_A_ALG_M GENMASK(28, 25)86#define SEC_BD_W1_A_ALG_S 2587#define SEC_A_ALG_SHA1 088#define SEC_A_ALG_SHA256 189#define SEC_A_ALG_MD5 290#define SEC_A_ALG_SHA224 391#define SEC_A_ALG_HMAC_SHA1 892#define SEC_A_ALG_HMAC_SHA224 1093#define SEC_A_ALG_HMAC_SHA256 1194#define SEC_A_ALG_HMAC_MD5 1295#define SEC_A_ALG_AES_XCBC 1396#define SEC_A_ALG_AES_CMAC 149798#define SEC_BD_W1_C_ALG_M GENMASK(31, 29)99#define SEC_BD_W1_C_ALG_S 29100#define SEC_C_ALG_DES 0101#define SEC_C_ALG_3DES 1102#define SEC_C_ALG_AES 2103104u32 w1;105106#define SEC_BD_W2_C_GRAN_SIZE_15_0_M GENMASK(15, 0)107#define SEC_BD_W2_C_GRAN_SIZE_15_0_S 0108#define SEC_BD_W2_GRAN_NUM_M GENMASK(31, 16)109#define SEC_BD_W2_GRAN_NUM_S 16110u32 w2;111112#define SEC_BD_W3_AUTH_LEN_OFFSET_M GENMASK(9, 0)113#define SEC_BD_W3_AUTH_LEN_OFFSET_S 0114#define SEC_BD_W3_CIPHER_LEN_OFFSET_M GENMASK(19, 10)115#define SEC_BD_W3_CIPHER_LEN_OFFSET_S 10116#define SEC_BD_W3_MAC_LEN_M GENMASK(24, 20)117#define SEC_BD_W3_MAC_LEN_S 20118#define SEC_BD_W3_A_KEY_LEN_M GENMASK(29, 25)119#define SEC_BD_W3_A_KEY_LEN_S 25120#define SEC_BD_W3_C_KEY_LEN_M GENMASK(31, 30)121#define SEC_BD_W3_C_KEY_LEN_S 30122#define SEC_KEY_LEN_AES_128 0123#define SEC_KEY_LEN_AES_192 1124#define SEC_KEY_LEN_AES_256 2125#define SEC_KEY_LEN_DES 1126#define SEC_KEY_LEN_3DES_3_KEY 1127#define SEC_KEY_LEN_3DES_2_KEY 3128u32 w3;129130/* W4,5 */131union {132u32 authkey_addr_lo;133u32 authiv_addr_lo;134};135union {136u32 authkey_addr_hi;137u32 authiv_addr_hi;138};139140/* W6,7 */141u32 cipher_key_addr_lo;142u32 cipher_key_addr_hi;143144/* W8,9 */145u32 cipher_iv_addr_lo;146u32 cipher_iv_addr_hi;147148/* W10,11 */149u32 data_addr_lo;150u32 data_addr_hi;151152/* W12,13 */153u32 mac_addr_lo;154u32 mac_addr_hi;155156/* W14,15 */157u32 cipher_destin_addr_lo;158u32 cipher_destin_addr_hi;159};160161enum sec_mem_region {162SEC_COMMON = 0,163SEC_SAA,164SEC_NUM_ADDR_REGIONS165};166167#define SEC_NAME_SIZE 64168#define SEC_Q_NUM 16169170171/**172* struct sec_queue_ring_cmd - store information about a SEC HW cmd ring173* @used: Local counter used to cheaply establish if the ring is empty.174* @lock: Protect against simultaneous adjusting of the read and write pointers.175* @vaddr: Virtual address for the ram pages used for the ring.176* @paddr: Physical address of the dma mapped region of ram used for the ring.177* @callback: Callback function called on a ring element completing.178*/179struct sec_queue_ring_cmd {180atomic_t used;181struct mutex lock;182struct sec_bd_info *vaddr;183dma_addr_t paddr;184void (*callback)(struct sec_bd_info *resp, void *ctx);185};186187struct sec_debug_bd_info;188struct sec_queue_ring_db {189struct sec_debug_bd_info *vaddr;190dma_addr_t paddr;191};192193struct sec_out_bd_info;194struct sec_queue_ring_cq {195struct sec_out_bd_info *vaddr;196dma_addr_t paddr;197};198199struct sec_dev_info;200201enum sec_cipher_alg {202SEC_C_DES_ECB_64,203SEC_C_DES_CBC_64,204205SEC_C_3DES_ECB_192_3KEY,206SEC_C_3DES_ECB_192_2KEY,207208SEC_C_3DES_CBC_192_3KEY,209SEC_C_3DES_CBC_192_2KEY,210211SEC_C_AES_ECB_128,212SEC_C_AES_ECB_192,213SEC_C_AES_ECB_256,214215SEC_C_AES_CBC_128,216SEC_C_AES_CBC_192,217SEC_C_AES_CBC_256,218219SEC_C_AES_CTR_128,220SEC_C_AES_CTR_192,221SEC_C_AES_CTR_256,222223SEC_C_AES_XTS_128,224SEC_C_AES_XTS_256,225226SEC_C_NULL,227};228229/**230* struct sec_alg_tfm_ctx - hardware specific tranformation context231* @cipher_alg: Cipher algorithm enabled include encryption mode.232* @key: Key storage if required.233* @pkey: DMA address for the key storage.234* @req_template: Request template to save time on setup.235* @queue: The hardware queue associated with this tfm context.236* @lock: Protect key and pkey to ensure they are consistent237* @auth_buf: Current context buffer for auth operations.238* @backlog: The backlog queue used for cases where our buffers aren't239* large enough.240*/241struct sec_alg_tfm_ctx {242enum sec_cipher_alg cipher_alg;243u8 *key;244dma_addr_t pkey;245struct sec_bd_info req_template;246struct sec_queue *queue;247struct mutex lock;248u8 *auth_buf;249struct list_head backlog;250};251252/**253* struct sec_request - data associate with a single crypto request254* @elements: List of subparts of this request (hardware size restriction)255* @num_elements: The number of subparts (used as an optimization)256* @lock: Protect elements of this structure against concurrent change.257* @tfm_ctx: hardware specific context.258* @len_in: length of in sgl from upper layers259* @len_out: length of out sgl from upper layers260* @dma_iv: initialization vector - phsyical address261* @err: store used to track errors across subelements of this request.262* @req_base: pointer to base element of associate crypto context.263* This is needed to allow shared handling skcipher, ahash etc.264* @cb: completion callback.265* @backlog_head: list head to allow backlog maintenance.266*267* The hardware is limited in the maximum size of data that it can268* process from a single BD. Typically this is fairly large (32MB)269* but still requires the complexity of splitting the incoming270* skreq up into a number of elements complete with appropriate271* iv chaining.272*/273struct sec_request {274struct list_head elements;275int num_elements;276struct mutex lock;277struct sec_alg_tfm_ctx *tfm_ctx;278int len_in;279int len_out;280dma_addr_t dma_iv;281int err;282struct crypto_async_request *req_base;283void (*cb)(struct sec_bd_info *resp, struct crypto_async_request *req);284struct list_head backlog_head;285};286287/**288* struct sec_request_el - A subpart of a request.289* @head: allow us to attach this to the list in the sec_request290* @req: hardware block descriptor corresponding to this request subpart291* @in: hardware sgl for input - virtual address292* @dma_in: hardware sgl for input - physical address293* @sgl_in: scatterlist for this request subpart294* @out: hardware sgl for output - virtual address295* @dma_out: hardware sgl for output - physical address296* @sgl_out: scatterlist for this request subpart297* @sec_req: The request which this subpart forms a part of298* @el_length: Number of bytes in this subpart. Needed to locate299* last ivsize chunk for iv chaining.300*/301struct sec_request_el {302struct list_head head;303struct sec_bd_info req;304struct sec_hw_sgl *in;305dma_addr_t dma_in;306struct scatterlist *sgl_in;307struct sec_hw_sgl *out;308dma_addr_t dma_out;309struct scatterlist *sgl_out;310struct sec_request *sec_req;311size_t el_length;312};313314/**315* struct sec_queue - All the information about a HW queue316* @dev_info: The parent SEC device to which this queue belongs.317* @task_irq: Completion interrupt for the queue.318* @name: Human readable queue description also used as irq name.319* @ring: The several HW rings associated with one queue.320* @regs: The iomapped device registers321* @queue_id: Index of the queue used for naming and resource selection.322* @in_use: Flag to say if the queue is in use.323* @expected: The next expected element to finish assuming we were in order.324* @uprocessed: A bitmap to track which OoO elements are done but not handled.325* @softqueue: A software queue used when chaining requirements prevent direct326* use of the hardware queues.327* @havesoftqueue: A flag to say we have a queues - as we may need one for the328* current mode.329* @queuelock: Protect the soft queue from concurrent changes to avoid some330* potential loss of data races.331* @shadow: Pointers back to the shadow copy of the hardware ring element332* need because we can't store any context reference in the bd element.333*/334struct sec_queue {335struct sec_dev_info *dev_info;336int task_irq;337char name[SEC_NAME_SIZE];338struct sec_queue_ring_cmd ring_cmd;339struct sec_queue_ring_cq ring_cq;340struct sec_queue_ring_db ring_db;341void __iomem *regs;342u32 queue_id;343bool in_use;344int expected;345346DECLARE_BITMAP(unprocessed, SEC_QUEUE_LEN);347DECLARE_KFIFO_PTR(softqueue, typeof(struct sec_request_el *));348bool havesoftqueue;349spinlock_t queuelock;350void *shadow[SEC_QUEUE_LEN];351};352353/**354* struct sec_hw_sge: Track each of the 64 element SEC HW SGL entries355* @buf: The IOV dma address for this entry.356* @len: Length of this IOV.357* @pad: Reserved space.358*/359struct sec_hw_sge {360dma_addr_t buf;361unsigned int len;362unsigned int pad;363};364365/**366* struct sec_hw_sgl: One hardware SGL entry.367* @next_sgl: The next entry if we need to chain dma address. Null if last.368* @entry_sum_in_chain: The full count of SGEs - only matters for first SGL.369* @entry_sum_in_sgl: The number of SGEs in this SGL element.370* @flag: Unused in skciphers.371* @serial_num: Unsued in skciphers.372* @cpuid: Currently unused.373* @data_bytes_in_sgl: Count of bytes from all SGEs in this SGL.374* @next: Virtual address used to stash the next sgl - useful in completion.375* @reserved: A reserved field not currently used.376* @sge_entries: The (up to) 64 Scatter Gather Entries, representing IOVs.377* @node: Currently unused.378*/379struct sec_hw_sgl {380dma_addr_t next_sgl;381u16 entry_sum_in_chain;382u16 entry_sum_in_sgl;383u32 flag;384u64 serial_num;385u32 cpuid;386u32 data_bytes_in_sgl;387struct sec_hw_sgl *next;388u64 reserved;389struct sec_hw_sge sge_entries[SEC_MAX_SGE_NUM];390u8 node[16];391};392393struct dma_pool;394395/**396* struct sec_dev_info: The full SEC unit comprising queues and processors.397* @sec_id: Index used to track which SEC this is when more than one is present.398* @num_saas: The number of backed processors enabled.399* @regs: iomapped register regions shared by whole SEC unit.400* @dev_lock: Protects concurrent queue allocation / freeing for the SEC.401* @queues: The 16 queues that this SEC instance provides.402* @dev: Device pointer.403* @hw_sgl_pool: DMA pool used to mimise mapping for the scatter gather lists.404*/405struct sec_dev_info {406int sec_id;407int num_saas;408void __iomem *regs[SEC_NUM_ADDR_REGIONS];409struct mutex dev_lock;410int queues_in_use;411struct sec_queue queues[SEC_Q_NUM];412struct device *dev;413struct dma_pool *hw_sgl_pool;414};415416int sec_queue_send(struct sec_queue *queue, struct sec_bd_info *msg, void *ctx);417bool sec_queue_can_enqueue(struct sec_queue *queue, int num);418int sec_queue_stop_release(struct sec_queue *queue);419struct sec_queue *sec_queue_alloc_start_safe(void);420bool sec_queue_empty(struct sec_queue *queue);421422/* Algorithm specific elements from sec_algs.c */423void sec_alg_callback(struct sec_bd_info *resp, void *ctx);424int sec_algs_register(void);425void sec_algs_unregister(void);426427#endif /* _SEC_DRV_H_ */428429430