Path: blob/master/drivers/crypto/cavium/nitrox/nitrox_req.h
26285 views
/* SPDX-License-Identifier: GPL-2.0 */1#ifndef __NITROX_REQ_H2#define __NITROX_REQ_H34#include <linux/dma-mapping.h>5#include <crypto/aes.h>67#include "nitrox_dev.h"89#define PENDING_SIG 0xFFFFFFFFFFFFFFFFUL10#define PRIO 40011112typedef void (*sereq_completion_t)(void *req, int err);1314/**15* struct gphdr - General purpose Header16* @param0: first parameter.17* @param1: second parameter.18* @param2: third parameter.19* @param3: fourth parameter.20*21* Params tell the iv and enc/dec data offsets.22*/23struct gphdr {24__be16 param0;25__be16 param1;26__be16 param2;27__be16 param3;28};2930/**31* struct se_req_ctrl - SE request information.32* @arg: Minor number of the opcode33* @ctxc: Context control.34* @unca: Uncertainity enabled.35* @info: Additional information for SE cores.36* @ctxl: Context length in bytes.37* @uddl: User defined data length38*/39union se_req_ctrl {40u64 value;41struct {42u64 raz : 22;43u64 arg : 8;44u64 ctxc : 2;45u64 unca : 1;46u64 info : 3;47u64 unc : 8;48u64 ctxl : 12;49u64 uddl : 8;50} s;51};5253#define MAX_IV_LEN 165455/**56* struct se_crypto_request - SE crypto request structure.57* @opcode: Request opcode (enc/dec)58* @flags: flags from crypto subsystem59* @ctx_handle: Crypto context handle.60* @gph: GP Header61* @ctrl: Request Information.62* @orh: ORH address63* @comp: completion address64* @src: Input sglist65* @dst: Output sglist66*/67struct se_crypto_request {68u8 opcode;69gfp_t gfp;70u32 flags;71u64 ctx_handle;7273struct gphdr gph;74union se_req_ctrl ctrl;75u64 *orh;76u64 *comp;7778struct scatterlist *src;79struct scatterlist *dst;80};8182/* Crypto opcodes */83#define FLEXI_CRYPTO_ENCRYPT_HMAC 0x3384#define ENCRYPT 085#define DECRYPT 18687/* IV from context */88#define IV_FROM_CTX 089/* IV from Input data */90#define IV_FROM_DPTR 19192/**93* cipher opcodes for firmware94*/95enum flexi_cipher {96CIPHER_NULL = 0,97CIPHER_3DES_CBC,98CIPHER_3DES_ECB,99CIPHER_AES_CBC,100CIPHER_AES_ECB,101CIPHER_AES_CFB,102CIPHER_AES_CTR,103CIPHER_AES_GCM,104CIPHER_AES_XTS,105CIPHER_AES_CCM,106CIPHER_AES_CBC_CTS,107CIPHER_AES_ECB_CTS,108CIPHER_INVALID109};110111enum flexi_auth {112AUTH_NULL = 0,113AUTH_MD5,114AUTH_SHA1,115AUTH_SHA2_SHA224,116AUTH_SHA2_SHA256,117AUTH_SHA2_SHA384,118AUTH_SHA2_SHA512,119AUTH_GMAC,120AUTH_INVALID121};122123/**124* struct crypto_keys - Crypto keys125* @key: Encryption key or KEY1 for AES-XTS126* @iv: Encryption IV or Tweak for AES-XTS127*/128struct crypto_keys {129union {130u8 key[AES_MAX_KEY_SIZE];131u8 key1[AES_MAX_KEY_SIZE];132} u;133u8 iv[AES_BLOCK_SIZE];134};135136/**137* struct auth_keys - Authentication keys138* @ipad: IPAD or KEY2 for AES-XTS139* @opad: OPAD or AUTH KEY if auth_input_type = 1140*/141struct auth_keys {142union {143u8 ipad[64];144u8 key2[64];145} u;146u8 opad[64];147};148149union fc_ctx_flags {150__be64 f;151u64 fu;152struct {153#if defined(__BIG_ENDIAN_BITFIELD)154u64 cipher_type : 4;155u64 reserved_59 : 1;156u64 aes_keylen : 2;157u64 iv_source : 1;158u64 hash_type : 4;159u64 reserved_49_51 : 3;160u64 auth_input_type: 1;161u64 mac_len : 8;162u64 reserved_0_39 : 40;163#else164u64 reserved_0_39 : 40;165u64 mac_len : 8;166u64 auth_input_type: 1;167u64 reserved_49_51 : 3;168u64 hash_type : 4;169u64 iv_source : 1;170u64 aes_keylen : 2;171u64 reserved_59 : 1;172u64 cipher_type : 4;173#endif174} w0;175};176/**177* struct flexi_crypto_context - Crypto context178* @cipher_type: Encryption cipher type179* @aes_keylen: AES key length180* @iv_source: Encryption IV source181* @hash_type: Authentication type182* @auth_input_type: Authentication input type183* 1 - Authentication IV and KEY, microcode calculates OPAD/IPAD184* 0 - Authentication OPAD/IPAD185* @mac_len: mac length186* @crypto: Crypto keys187* @auth: Authentication keys188*/189struct flexi_crypto_context {190union fc_ctx_flags flags;191struct crypto_keys crypto;192struct auth_keys auth;193};194195struct crypto_ctx_hdr {196struct dma_pool *pool;197dma_addr_t dma;198void *vaddr;199};200201struct nitrox_crypto_ctx {202struct nitrox_device *ndev;203union {204u64 ctx_handle;205struct flexi_crypto_context *fctx;206} u;207struct crypto_ctx_hdr *chdr;208sereq_completion_t callback;209};210211struct nitrox_kcrypt_request {212struct se_crypto_request creq;213u8 *src;214u8 *dst;215u8 *iv_out;216};217218/**219* struct nitrox_aead_rctx - AEAD request context220* @nkreq: Base request context221* @cryptlen: Encryption/Decryption data length222* @assoclen: AAD length223* @srclen: Input buffer length224* @dstlen: Output buffer length225* @iv: IV data226* @ivsize: IV data length227* @flags: AEAD req flags228* @ctx_handle: Device context handle229* @src: Source sglist230* @dst: Destination sglist231* @ctrl_arg: Identifies the request type (ENCRYPT/DECRYPT)232*/233struct nitrox_aead_rctx {234struct nitrox_kcrypt_request nkreq;235unsigned int cryptlen;236unsigned int assoclen;237unsigned int srclen;238unsigned int dstlen;239u8 *iv;240int ivsize;241u32 flags;242u64 ctx_handle;243struct scatterlist *src;244struct scatterlist *dst;245u8 ctrl_arg;246};247248/**249* struct nitrox_rfc4106_rctx - rfc4106 cipher request context250* @base: AEAD request context251* @src: Source sglist252* @dst: Destination sglist253* @assoc: AAD254*/255struct nitrox_rfc4106_rctx {256struct nitrox_aead_rctx base;257struct scatterlist src[3];258struct scatterlist dst[3];259u8 assoc[20];260};261262/**263* struct pkt_instr_hdr - Packet Instruction Header264* @g: Gather used265* When [G] is set and [GSZ] != 0, the instruction is266* indirect gather instruction.267* When [G] is set and [GSZ] = 0, the instruction is268* direct gather instruction.269* @gsz: Number of pointers in the indirect gather list270* @ihi: When set hardware duplicates the 1st 8 bytes of pkt_instr_hdr271* and adds them to the packet after the pkt_instr_hdr but before any UDD272* @ssz: Not used by the input hardware. But can become slc_store_int[SSZ]273* when [IHI] is set.274* @fsz: The number of front data bytes directly included in the275* PCIe instruction.276* @tlen: The length of the input packet in bytes, include:277* - 16B pkt_hdr278* - Inline context bytes if any,279* - UDD if any,280* - packet payload bytes281*/282union pkt_instr_hdr {283__be64 bev;284u64 value;285struct {286#if defined(__BIG_ENDIAN_BITFIELD)287u64 raz_48_63 : 16;288u64 g : 1;289u64 gsz : 7;290u64 ihi : 1;291u64 ssz : 7;292u64 raz_30_31 : 2;293u64 fsz : 6;294u64 raz_16_23 : 8;295u64 tlen : 16;296#else297u64 tlen : 16;298u64 raz_16_23 : 8;299u64 fsz : 6;300u64 raz_30_31 : 2;301u64 ssz : 7;302u64 ihi : 1;303u64 gsz : 7;304u64 g : 1;305u64 raz_48_63 : 16;306#endif307} s;308};309310/**311* struct pkt_hdr - Packet Input Header312* @opcode: Request opcode (Major)313* @arg: Request opcode (Minor)314* @ctxc: Context control.315* @unca: When set [UNC] is the uncertainty count for an input packet.316* The hardware uses uncertainty counts to predict317* output buffer use and avoid deadlock.318* @info: Not used by input hardware. Available for use319* during SE processing.320* @destport: The expected destination port/ring/channel for the packet.321* @unc: Uncertainty count for an input packet.322* @grp: SE group that will process the input packet.323* @ctxl: Context Length in 64-bit words.324* @uddl: User-defined data (UDD) length in bytes.325* @ctxp: Context pointer. CTXP<63,2:0> must be zero in all cases.326*/327union pkt_hdr {328__be64 bev[2];329u64 value[2];330struct {331#if defined(__BIG_ENDIAN_BITFIELD)332u64 opcode : 8;333u64 arg : 8;334u64 ctxc : 2;335u64 unca : 1;336u64 raz_44 : 1;337u64 info : 3;338u64 destport : 9;339u64 unc : 8;340u64 raz_19_23 : 5;341u64 grp : 3;342u64 raz_15 : 1;343u64 ctxl : 7;344u64 uddl : 8;345#else346u64 uddl : 8;347u64 ctxl : 7;348u64 raz_15 : 1;349u64 grp : 3;350u64 raz_19_23 : 5;351u64 unc : 8;352u64 destport : 9;353u64 info : 3;354u64 raz_44 : 1;355u64 unca : 1;356u64 ctxc : 2;357u64 arg : 8;358u64 opcode : 8;359#endif360__be64 ctxp;361} s;362};363364/**365* struct slc_store_info - Solicited Paceket Output Store Information.366* @ssz: The number of scatterlist pointers for the solicited output port367* packet.368* @rptr: The result pointer for the solicited output port packet.369* If [SSZ]=0, [RPTR] must point directly to a buffer on the remote370* host that is large enough to hold the entire output packet.371* If [SSZ]!=0, [RPTR] must point to an array of ([SSZ]+3)/4372* sglist components at [RPTR] on the remote host.373*/374union slc_store_info {375__be64 bev[2];376u64 value[2];377struct {378#if defined(__BIG_ENDIAN_BITFIELD)379u64 raz_39_63 : 25;380u64 ssz : 7;381u64 raz_0_31 : 32;382#else383u64 raz_0_31 : 32;384u64 ssz : 7;385u64 raz_39_63 : 25;386#endif387__be64 rptr;388} s;389};390391/**392* struct nps_pkt_instr - NPS Packet Instruction of SE cores.393* @dptr0 : Input pointer points to buffer in remote host.394* @ih: Packet Instruction Header (8 bytes)395* @irh: Packet Input Header (16 bytes)396* @slc: Solicited Packet Output Store Information (16 bytes)397* @fdata: Front data398*399* 64-Byte Instruction Format400*/401struct nps_pkt_instr {402__be64 dptr0;403union pkt_instr_hdr ih;404union pkt_hdr irh;405union slc_store_info slc;406u64 fdata[2];407};408409/**410* struct aqmq_command_s - The 32 byte command for AE processing.411* @opcode: Request opcode412* @param1: Request control parameter 1413* @param2: Request control parameter 2414* @dlen: Input length415* @dptr: Input pointer points to buffer in remote host416* @rptr: Result pointer points to buffer in remote host417* @grp: AQM Group (0..7)418* @cptr: Context pointer419*/420struct aqmq_command_s {421__be16 opcode;422__be16 param1;423__be16 param2;424__be16 dlen;425__be64 dptr;426__be64 rptr;427union {428__be64 word3;429#if defined(__BIG_ENDIAN_BITFIELD)430u64 grp : 3;431u64 cptr : 61;432#else433u64 cptr : 61;434u64 grp : 3;435#endif436};437};438439/**440* struct ctx_hdr - Book keeping data about the crypto context441* @pool: Pool used to allocate crypto context442* @dma: Base DMA address of the crypto context443* @ctx_dma: Actual usable crypto context for NITROX444*/445struct ctx_hdr {446struct dma_pool *pool;447dma_addr_t dma;448dma_addr_t ctx_dma;449};450451/*452* struct sglist_component - SG list component format453* @len0: The number of bytes at [PTR0] on the remote host.454* @len1: The number of bytes at [PTR1] on the remote host.455* @len2: The number of bytes at [PTR2] on the remote host.456* @len3: The number of bytes at [PTR3] on the remote host.457* @dma0: First pointer point to buffer in remote host.458* @dma1: Second pointer point to buffer in remote host.459* @dma2: Third pointer point to buffer in remote host.460* @dma3: Fourth pointer point to buffer in remote host.461*/462struct nitrox_sgcomp {463__be16 len[4];464__be64 dma[4];465};466467/*468* strutct nitrox_sgtable - SG list information469* @sgmap_cnt: Number of buffers mapped470* @total_bytes: Total bytes in sglist.471* @sgcomp_len: Total sglist components length.472* @sgcomp_dma: DMA address of sglist component.473* @sg: crypto request buffer.474* @sgcomp: sglist component for NITROX.475*/476struct nitrox_sgtable {477u8 sgmap_cnt;478u16 total_bytes;479u32 sgcomp_len;480dma_addr_t sgcomp_dma;481struct scatterlist *sg;482struct nitrox_sgcomp *sgcomp;483};484485/* Response Header Length */486#define ORH_HLEN 8487/* Completion bytes Length */488#define COMP_HLEN 8489490struct resp_hdr {491u64 *orh;492u64 *completion;493};494495typedef void (*completion_t)(void *arg, int err);496497/**498* struct nitrox_softreq - Represents the NIROX Request.499* @response: response list entry500* @backlog: Backlog list entry501* @ndev: Device used to submit the request502* @cmdq: Command queue for submission503* @resp: Response headers504* @instr: 64B instruction505* @in: SG table for input506* @out SG table for output507* @tstamp: Request submitted time in jiffies508* @callback: callback after request completion/timeout509* @cb_arg: callback argument510*/511struct nitrox_softreq {512struct list_head response;513struct list_head backlog;514515u32 flags;516gfp_t gfp;517atomic_t status;518519struct nitrox_device *ndev;520struct nitrox_cmdq *cmdq;521522struct nps_pkt_instr instr;523struct resp_hdr resp;524struct nitrox_sgtable in;525struct nitrox_sgtable out;526527unsigned long tstamp;528529completion_t callback;530void *cb_arg;531};532533static inline int flexi_aes_keylen(int keylen)534{535int aes_keylen;536537switch (keylen) {538case AES_KEYSIZE_128:539aes_keylen = 1;540break;541case AES_KEYSIZE_192:542aes_keylen = 2;543break;544case AES_KEYSIZE_256:545aes_keylen = 3;546break;547default:548aes_keylen = -EINVAL;549break;550}551return aes_keylen;552}553554static inline void *alloc_req_buf(int nents, int extralen, gfp_t gfp)555{556size_t size;557558size = sizeof(struct scatterlist) * nents;559size += extralen;560561return kzalloc(size, gfp);562}563564/**565* create_single_sg - Point SG entry to the data566* @sg: Destination SG list567* @buf: Data568* @buflen: Data length569*570* Returns next free entry in the destination SG list571**/572static inline struct scatterlist *create_single_sg(struct scatterlist *sg,573void *buf, int buflen)574{575sg_set_buf(sg, buf, buflen);576sg++;577return sg;578}579580/**581* create_multi_sg - Create multiple sg entries with buflen data length from582* source sglist583* @to_sg: Destination SG list584* @from_sg: Source SG list585* @buflen: Data length586*587* Returns next free entry in the destination SG list588**/589static inline struct scatterlist *create_multi_sg(struct scatterlist *to_sg,590struct scatterlist *from_sg,591int buflen)592{593struct scatterlist *sg = to_sg;594unsigned int sglen;595596for (; buflen && from_sg; buflen -= sglen) {597sglen = from_sg->length;598if (sglen > buflen)599sglen = buflen;600601sg_set_buf(sg, sg_virt(from_sg), sglen);602from_sg = sg_next(from_sg);603sg++;604}605606return sg;607}608609static inline void set_orh_value(u64 *orh)610{611WRITE_ONCE(*orh, PENDING_SIG);612}613614static inline void set_comp_value(u64 *comp)615{616WRITE_ONCE(*comp, PENDING_SIG);617}618619static inline int alloc_src_req_buf(struct nitrox_kcrypt_request *nkreq,620int nents, int ivsize)621{622struct se_crypto_request *creq = &nkreq->creq;623624nkreq->src = alloc_req_buf(nents, ivsize, creq->gfp);625if (!nkreq->src)626return -ENOMEM;627628return 0;629}630631static inline void nitrox_creq_copy_iv(char *dst, char *src, int size)632{633memcpy(dst, src, size);634}635636static inline struct scatterlist *nitrox_creq_src_sg(char *iv, int ivsize)637{638return (struct scatterlist *)(iv + ivsize);639}640641static inline void nitrox_creq_set_src_sg(struct nitrox_kcrypt_request *nkreq,642int nents, int ivsize,643struct scatterlist *src, int buflen)644{645char *iv = nkreq->src;646struct scatterlist *sg;647struct se_crypto_request *creq = &nkreq->creq;648649creq->src = nitrox_creq_src_sg(iv, ivsize);650sg = creq->src;651sg_init_table(sg, nents);652653/* Input format:654* +----+----------------+655* | IV | SRC sg entries |656* +----+----------------+657*/658659/* IV */660sg = create_single_sg(sg, iv, ivsize);661/* SRC entries */662create_multi_sg(sg, src, buflen);663}664665static inline int alloc_dst_req_buf(struct nitrox_kcrypt_request *nkreq,666int nents)667{668int extralen = ORH_HLEN + COMP_HLEN;669struct se_crypto_request *creq = &nkreq->creq;670671nkreq->dst = alloc_req_buf(nents, extralen, creq->gfp);672if (!nkreq->dst)673return -ENOMEM;674675return 0;676}677678static inline void nitrox_creq_set_orh(struct nitrox_kcrypt_request *nkreq)679{680struct se_crypto_request *creq = &nkreq->creq;681682creq->orh = (u64 *)(nkreq->dst);683set_orh_value(creq->orh);684}685686static inline void nitrox_creq_set_comp(struct nitrox_kcrypt_request *nkreq)687{688struct se_crypto_request *creq = &nkreq->creq;689690creq->comp = (u64 *)(nkreq->dst + ORH_HLEN);691set_comp_value(creq->comp);692}693694static inline struct scatterlist *nitrox_creq_dst_sg(char *dst)695{696return (struct scatterlist *)(dst + ORH_HLEN + COMP_HLEN);697}698699static inline void nitrox_creq_set_dst_sg(struct nitrox_kcrypt_request *nkreq,700int nents, int ivsize,701struct scatterlist *dst, int buflen)702{703struct se_crypto_request *creq = &nkreq->creq;704struct scatterlist *sg;705char *iv = nkreq->src;706707creq->dst = nitrox_creq_dst_sg(nkreq->dst);708sg = creq->dst;709sg_init_table(sg, nents);710711/* Output format:712* +-----+----+----------------+-----------------+713* | ORH | IV | DST sg entries | COMPLETION Bytes|714* +-----+----+----------------+-----------------+715*/716717/* ORH */718sg = create_single_sg(sg, creq->orh, ORH_HLEN);719/* IV */720sg = create_single_sg(sg, iv, ivsize);721/* DST entries */722sg = create_multi_sg(sg, dst, buflen);723/* COMPLETION Bytes */724create_single_sg(sg, creq->comp, COMP_HLEN);725}726727#endif /* __NITROX_REQ_H */728729730