1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/dma-mapping.h>
6 #include <crypto/aes.h>
8 #include "nitrox_dev.h"
10 #define PENDING_SIG 0xFFFFFFFFFFFFFFFFUL
14 * struct gphdr - General purpose Header
15 * @param0: first parameter.
16 * @param1: second parameter.
17 * @param2: third parameter.
18 * @param3: fourth parameter.
20 * Params tell the iv and enc/dec data offsets.
30 * struct se_req_ctrl - SE request information.
31 * @arg: Minor number of the opcode
32 * @ctxc: Context control.
33 * @unca: Uncertainity enabled.
34 * @info: Additional information for SE cores.
35 * @ctxl: Context length in bytes.
36 * @uddl: User defined data length
55 * struct se_crypto_request - SE crypto request structure.
56 * @opcode: Request opcode (enc/dec)
57 * @flags: flags from crypto subsystem
58 * @ctx_handle: Crypto context handle.
60 * @ctrl: Request Information.
62 * @comp: completion address
66 struct se_crypto_request
{
73 union se_req_ctrl ctrl
;
77 struct scatterlist
*src
;
78 struct scatterlist
*dst
;
82 #define FLEXI_CRYPTO_ENCRYPT_HMAC 0x33
88 /* IV from Input data */
89 #define IV_FROM_DPTR 1
92 * cipher opcodes for firmware
123 * struct crypto_keys - Crypto keys
124 * @key: Encryption key or KEY1 for AES-XTS
125 * @iv: Encryption IV or Tweak for AES-XTS
129 u8 key
[AES_MAX_KEY_SIZE
];
130 u8 key1
[AES_MAX_KEY_SIZE
];
132 u8 iv
[AES_BLOCK_SIZE
];
136 * struct auth_keys - Authentication keys
137 * @ipad: IPAD or KEY2 for AES-XTS
138 * @opad: OPAD or AUTH KEY if auth_input_type = 1
151 #if defined(__BIG_ENDIAN_BITFIELD)
157 u64 reserved_49_51
: 3;
158 u64 auth_input_type
: 1;
160 u64 reserved_0_39
: 40;
162 u64 reserved_0_39
: 40;
164 u64 auth_input_type
: 1;
165 u64 reserved_49_51
: 3;
175 * struct flexi_crypto_context - Crypto context
176 * @cipher_type: Encryption cipher type
177 * @aes_keylen: AES key length
178 * @iv_source: Encryption IV source
179 * @hash_type: Authentication type
180 * @auth_input_type: Authentication input type
181 * 1 - Authentication IV and KEY, microcode calculates OPAD/IPAD
182 * 0 - Authentication OPAD/IPAD
183 * @mac_len: mac length
184 * @crypto: Crypto keys
185 * @auth: Authentication keys
187 struct flexi_crypto_context
{
188 union fc_ctx_flags flags
;
189 struct crypto_keys crypto
;
190 struct auth_keys auth
;
193 struct crypto_ctx_hdr
{
194 struct dma_pool
*pool
;
199 struct nitrox_crypto_ctx
{
200 struct nitrox_device
*ndev
;
203 struct flexi_crypto_context
*fctx
;
205 struct crypto_ctx_hdr
*chdr
;
208 struct nitrox_kcrypt_request
{
209 struct se_crypto_request creq
;
215 * struct nitrox_aead_rctx - AEAD request context
216 * @nkreq: Base request context
217 * @cryptlen: Encryption/Decryption data length
218 * @assoclen: AAD length
219 * @srclen: Input buffer length
220 * @dstlen: Output buffer length
222 * @ivsize: IV data length
223 * @flags: AEAD req flags
224 * @ctx_handle: Device context handle
225 * @src: Source sglist
226 * @dst: Destination sglist
227 * @ctrl_arg: Identifies the request type (ENCRYPT/DECRYPT)
229 struct nitrox_aead_rctx
{
230 struct nitrox_kcrypt_request nkreq
;
231 unsigned int cryptlen
;
232 unsigned int assoclen
;
239 struct scatterlist
*src
;
240 struct scatterlist
*dst
;
245 * struct nitrox_rfc4106_rctx - rfc4106 cipher request context
246 * @base: AEAD request context
247 * @src: Source sglist
248 * @dst: Destination sglist
251 struct nitrox_rfc4106_rctx
{
252 struct nitrox_aead_rctx base
;
253 struct scatterlist src
[3];
254 struct scatterlist dst
[3];
259 * struct pkt_instr_hdr - Packet Instruction Header
261 * When [G] is set and [GSZ] != 0, the instruction is
262 * indirect gather instruction.
263 * When [G] is set and [GSZ] = 0, the instruction is
264 * direct gather instruction.
265 * @gsz: Number of pointers in the indirect gather list
266 * @ihi: When set hardware duplicates the 1st 8 bytes of pkt_instr_hdr
267 * and adds them to the packet after the pkt_instr_hdr but before any UDD
268 * @ssz: Not used by the input hardware. But can become slc_store_int[SSZ]
270 * @fsz: The number of front data bytes directly included in the
272 * @tlen: The length of the input packet in bytes, include:
274 * - Inline context bytes if any,
276 * - packet payload bytes
278 union pkt_instr_hdr
{
281 #if defined(__BIG_ENDIAN_BITFIELD)
306 * struct pkt_hdr - Packet Input Header
307 * @opcode: Request opcode (Major)
308 * @arg: Request opcode (Minor)
309 * @ctxc: Context control.
310 * @unca: When set [UNC] is the uncertainty count for an input packet.
311 * The hardware uses uncertainty counts to predict
312 * output buffer use and avoid deadlock.
313 * @info: Not used by input hardware. Available for use
314 * during SE processing.
315 * @destport: The expected destination port/ring/channel for the packet.
316 * @unc: Uncertainty count for an input packet.
317 * @grp: SE group that will process the input packet.
318 * @ctxl: Context Length in 64-bit words.
319 * @uddl: User-defined data (UDD) length in bytes.
320 * @ctxp: Context pointer. CTXP<63,2:0> must be zero in all cases.
325 #if defined(__BIG_ENDIAN_BITFIELD)
359 * struct slc_store_info - Solicited Paceket Output Store Information.
360 * @ssz: The number of scatterlist pointers for the solicited output port
362 * @rptr: The result pointer for the solicited output port packet.
363 * If [SSZ]=0, [RPTR] must point directly to a buffer on the remote
364 * host that is large enough to hold the entire output packet.
365 * If [SSZ]!=0, [RPTR] must point to an array of ([SSZ]+3)/4
366 * sglist components at [RPTR] on the remote host.
368 union slc_store_info
{
371 #if defined(__BIG_ENDIAN_BITFIELD)
385 * struct nps_pkt_instr - NPS Packet Instruction of SE cores.
386 * @dptr0 : Input pointer points to buffer in remote host.
387 * @ih: Packet Instruction Header (8 bytes)
388 * @irh: Packet Input Header (16 bytes)
389 * @slc: Solicited Packet Output Store Information (16 bytes)
392 * 64-Byte Instruction Format
394 struct nps_pkt_instr
{
396 union pkt_instr_hdr ih
;
398 union slc_store_info slc
;
403 * struct aqmq_command_s - The 32 byte command for AE processing.
404 * @opcode: Request opcode
405 * @param1: Request control parameter 1
406 * @param2: Request control parameter 2
407 * @dlen: Input length
408 * @dptr: Input pointer points to buffer in remote host
409 * @rptr: Result pointer points to buffer in remote host
410 * @grp: AQM Group (0..7)
411 * @cptr: Context pointer
413 struct aqmq_command_s
{
422 #if defined(__BIG_ENDIAN_BITFIELD)
433 * struct ctx_hdr - Book keeping data about the crypto context
434 * @pool: Pool used to allocate crypto context
435 * @dma: Base DMA address of the cypto context
436 * @ctx_dma: Actual usable crypto context for NITROX
439 struct dma_pool
*pool
;
445 * struct sglist_component - SG list component format
446 * @len0: The number of bytes at [PTR0] on the remote host.
447 * @len1: The number of bytes at [PTR1] on the remote host.
448 * @len2: The number of bytes at [PTR2] on the remote host.
449 * @len3: The number of bytes at [PTR3] on the remote host.
450 * @dma0: First pointer point to buffer in remote host.
451 * @dma1: Second pointer point to buffer in remote host.
452 * @dma2: Third pointer point to buffer in remote host.
453 * @dma3: Fourth pointer point to buffer in remote host.
455 struct nitrox_sgcomp
{
461 * strutct nitrox_sgtable - SG list information
462 * @sgmap_cnt: Number of buffers mapped
463 * @total_bytes: Total bytes in sglist.
464 * @sgcomp_len: Total sglist components length.
465 * @sgcomp_dma: DMA address of sglist component.
466 * @sg: crypto request buffer.
467 * @sgcomp: sglist component for NITROX.
469 struct nitrox_sgtable
{
473 dma_addr_t sgcomp_dma
;
474 struct scatterlist
*sg
;
475 struct nitrox_sgcomp
*sgcomp
;
478 /* Response Header Length */
480 /* Completion bytes Length */
488 typedef void (*completion_t
)(void *arg
, int err
);
491 * struct nitrox_softreq - Represents the NIROX Request.
492 * @response: response list entry
493 * @backlog: Backlog list entry
494 * @ndev: Device used to submit the request
495 * @cmdq: Command queue for submission
496 * @resp: Response headers
497 * @instr: 64B instruction
498 * @in: SG table for input
499 * @out SG table for output
500 * @tstamp: Request submitted time in jiffies
501 * @callback: callback after request completion/timeout
502 * @cb_arg: callback argument
504 struct nitrox_softreq
{
505 struct list_head response
;
506 struct list_head backlog
;
512 struct nitrox_device
*ndev
;
513 struct nitrox_cmdq
*cmdq
;
515 struct nps_pkt_instr instr
;
516 struct resp_hdr resp
;
517 struct nitrox_sgtable in
;
518 struct nitrox_sgtable out
;
520 unsigned long tstamp
;
522 completion_t callback
;
526 static inline int flexi_aes_keylen(int keylen
)
531 case AES_KEYSIZE_128
:
534 case AES_KEYSIZE_192
:
537 case AES_KEYSIZE_256
:
541 aes_keylen
= -EINVAL
;
547 static inline void *alloc_req_buf(int nents
, int extralen
, gfp_t gfp
)
551 size
= sizeof(struct scatterlist
) * nents
;
554 return kzalloc(size
, gfp
);
558 * create_single_sg - Point SG entry to the data
559 * @sg: Destination SG list
561 * @buflen: Data length
563 * Returns next free entry in the destination SG list
565 static inline struct scatterlist
*create_single_sg(struct scatterlist
*sg
,
566 void *buf
, int buflen
)
568 sg_set_buf(sg
, buf
, buflen
);
574 * create_multi_sg - Create multiple sg entries with buflen data length from
576 * @to_sg: Destination SG list
577 * @from_sg: Source SG list
578 * @buflen: Data length
580 * Returns next free entry in the destination SG list
582 static inline struct scatterlist
*create_multi_sg(struct scatterlist
*to_sg
,
583 struct scatterlist
*from_sg
,
586 struct scatterlist
*sg
= to_sg
;
589 for (; buflen
&& from_sg
; buflen
-= sglen
) {
590 sglen
= from_sg
->length
;
594 sg_set_buf(sg
, sg_virt(from_sg
), sglen
);
595 from_sg
= sg_next(from_sg
);
602 static inline void set_orh_value(u64
*orh
)
604 WRITE_ONCE(*orh
, PENDING_SIG
);
607 static inline void set_comp_value(u64
*comp
)
609 WRITE_ONCE(*comp
, PENDING_SIG
);
612 static inline int alloc_src_req_buf(struct nitrox_kcrypt_request
*nkreq
,
613 int nents
, int ivsize
)
615 struct se_crypto_request
*creq
= &nkreq
->creq
;
617 nkreq
->src
= alloc_req_buf(nents
, ivsize
, creq
->gfp
);
624 static inline void nitrox_creq_copy_iv(char *dst
, char *src
, int size
)
626 memcpy(dst
, src
, size
);
629 static inline struct scatterlist
*nitrox_creq_src_sg(char *iv
, int ivsize
)
631 return (struct scatterlist
*)(iv
+ ivsize
);
634 static inline void nitrox_creq_set_src_sg(struct nitrox_kcrypt_request
*nkreq
,
635 int nents
, int ivsize
,
636 struct scatterlist
*src
, int buflen
)
638 char *iv
= nkreq
->src
;
639 struct scatterlist
*sg
;
640 struct se_crypto_request
*creq
= &nkreq
->creq
;
642 creq
->src
= nitrox_creq_src_sg(iv
, ivsize
);
644 sg_init_table(sg
, nents
);
647 * +----+----------------+
648 * | IV | SRC sg entries |
649 * +----+----------------+
653 sg
= create_single_sg(sg
, iv
, ivsize
);
655 create_multi_sg(sg
, src
, buflen
);
658 static inline int alloc_dst_req_buf(struct nitrox_kcrypt_request
*nkreq
,
661 int extralen
= ORH_HLEN
+ COMP_HLEN
;
662 struct se_crypto_request
*creq
= &nkreq
->creq
;
664 nkreq
->dst
= alloc_req_buf(nents
, extralen
, creq
->gfp
);
671 static inline void nitrox_creq_set_orh(struct nitrox_kcrypt_request
*nkreq
)
673 struct se_crypto_request
*creq
= &nkreq
->creq
;
675 creq
->orh
= (u64
*)(nkreq
->dst
);
676 set_orh_value(creq
->orh
);
679 static inline void nitrox_creq_set_comp(struct nitrox_kcrypt_request
*nkreq
)
681 struct se_crypto_request
*creq
= &nkreq
->creq
;
683 creq
->comp
= (u64
*)(nkreq
->dst
+ ORH_HLEN
);
684 set_comp_value(creq
->comp
);
687 static inline struct scatterlist
*nitrox_creq_dst_sg(char *dst
)
689 return (struct scatterlist
*)(dst
+ ORH_HLEN
+ COMP_HLEN
);
692 static inline void nitrox_creq_set_dst_sg(struct nitrox_kcrypt_request
*nkreq
,
693 int nents
, int ivsize
,
694 struct scatterlist
*dst
, int buflen
)
696 struct se_crypto_request
*creq
= &nkreq
->creq
;
697 struct scatterlist
*sg
;
698 char *iv
= nkreq
->src
;
700 creq
->dst
= nitrox_creq_dst_sg(nkreq
->dst
);
702 sg_init_table(sg
, nents
);
705 * +-----+----+----------------+-----------------+
706 * | ORH | IV | DST sg entries | COMPLETION Bytes|
707 * +-----+----+----------------+-----------------+
711 sg
= create_single_sg(sg
, creq
->orh
, ORH_HLEN
);
713 sg
= create_single_sg(sg
, iv
, ivsize
);
715 sg
= create_multi_sg(sg
, dst
, buflen
);
716 /* COMPLETION Bytes */
717 create_single_sg(sg
, creq
->comp
, COMP_HLEN
);
720 #endif /* __NITROX_REQ_H */