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
13 typedef void (*sereq_completion_t
)(void *req
, int err
);
16 * struct gphdr - General purpose Header
17 * @param0: first parameter.
18 * @param1: second parameter.
19 * @param2: third parameter.
20 * @param3: fourth parameter.
22 * Params tell the iv and enc/dec data offsets.
32 * struct se_req_ctrl - SE request information.
33 * @arg: Minor number of the opcode
34 * @ctxc: Context control.
35 * @unca: Uncertainity enabled.
36 * @info: Additional information for SE cores.
37 * @ctxl: Context length in bytes.
38 * @uddl: User defined data length
57 * struct se_crypto_request - SE crypto request structure.
58 * @opcode: Request opcode (enc/dec)
59 * @flags: flags from crypto subsystem
60 * @ctx_handle: Crypto context handle.
62 * @ctrl: Request Information.
64 * @comp: completion address
68 struct se_crypto_request
{
75 union se_req_ctrl ctrl
;
79 struct scatterlist
*src
;
80 struct scatterlist
*dst
;
84 #define FLEXI_CRYPTO_ENCRYPT_HMAC 0x33
90 /* IV from Input data */
91 #define IV_FROM_DPTR 1
94 * cipher opcodes for firmware
125 * struct crypto_keys - Crypto keys
126 * @key: Encryption key or KEY1 for AES-XTS
127 * @iv: Encryption IV or Tweak for AES-XTS
131 u8 key
[AES_MAX_KEY_SIZE
];
132 u8 key1
[AES_MAX_KEY_SIZE
];
134 u8 iv
[AES_BLOCK_SIZE
];
138 * struct auth_keys - Authentication keys
139 * @ipad: IPAD or KEY2 for AES-XTS
140 * @opad: OPAD or AUTH KEY if auth_input_type = 1
153 #if defined(__BIG_ENDIAN_BITFIELD)
159 u64 reserved_49_51
: 3;
160 u64 auth_input_type
: 1;
162 u64 reserved_0_39
: 40;
164 u64 reserved_0_39
: 40;
166 u64 auth_input_type
: 1;
167 u64 reserved_49_51
: 3;
177 * struct flexi_crypto_context - Crypto context
178 * @cipher_type: Encryption cipher type
179 * @aes_keylen: AES key length
180 * @iv_source: Encryption IV source
181 * @hash_type: Authentication type
182 * @auth_input_type: Authentication input type
183 * 1 - Authentication IV and KEY, microcode calculates OPAD/IPAD
184 * 0 - Authentication OPAD/IPAD
185 * @mac_len: mac length
186 * @crypto: Crypto keys
187 * @auth: Authentication keys
189 struct flexi_crypto_context
{
190 union fc_ctx_flags flags
;
191 struct crypto_keys crypto
;
192 struct auth_keys auth
;
195 struct crypto_ctx_hdr
{
196 struct dma_pool
*pool
;
201 struct nitrox_crypto_ctx
{
202 struct nitrox_device
*ndev
;
205 struct flexi_crypto_context
*fctx
;
207 struct crypto_ctx_hdr
*chdr
;
208 sereq_completion_t callback
;
211 struct nitrox_kcrypt_request
{
212 struct se_crypto_request creq
;
219 * struct nitrox_aead_rctx - AEAD request context
220 * @nkreq: Base request context
221 * @cryptlen: Encryption/Decryption data length
222 * @assoclen: AAD length
223 * @srclen: Input buffer length
224 * @dstlen: Output buffer length
226 * @ivsize: IV data length
227 * @flags: AEAD req flags
228 * @ctx_handle: Device context handle
229 * @src: Source sglist
230 * @dst: Destination sglist
231 * @ctrl_arg: Identifies the request type (ENCRYPT/DECRYPT)
233 struct nitrox_aead_rctx
{
234 struct nitrox_kcrypt_request nkreq
;
235 unsigned int cryptlen
;
236 unsigned int assoclen
;
243 struct scatterlist
*src
;
244 struct scatterlist
*dst
;
249 * struct nitrox_rfc4106_rctx - rfc4106 cipher request context
250 * @base: AEAD request context
251 * @src: Source sglist
252 * @dst: Destination sglist
255 struct nitrox_rfc4106_rctx
{
256 struct nitrox_aead_rctx base
;
257 struct scatterlist src
[3];
258 struct scatterlist dst
[3];
263 * struct pkt_instr_hdr - Packet Instruction Header
265 * When [G] is set and [GSZ] != 0, the instruction is
266 * indirect gather instruction.
267 * When [G] is set and [GSZ] = 0, the instruction is
268 * direct gather instruction.
269 * @gsz: Number of pointers in the indirect gather list
270 * @ihi: When set hardware duplicates the 1st 8 bytes of pkt_instr_hdr
271 * and adds them to the packet after the pkt_instr_hdr but before any UDD
272 * @ssz: Not used by the input hardware. But can become slc_store_int[SSZ]
274 * @fsz: The number of front data bytes directly included in the
276 * @tlen: The length of the input packet in bytes, include:
278 * - Inline context bytes if any,
280 * - packet payload bytes
282 union pkt_instr_hdr
{
285 #if defined(__BIG_ENDIAN_BITFIELD)
310 * struct pkt_hdr - Packet Input Header
311 * @opcode: Request opcode (Major)
312 * @arg: Request opcode (Minor)
313 * @ctxc: Context control.
314 * @unca: When set [UNC] is the uncertainty count for an input packet.
315 * The hardware uses uncertainty counts to predict
316 * output buffer use and avoid deadlock.
317 * @info: Not used by input hardware. Available for use
318 * during SE processing.
319 * @destport: The expected destination port/ring/channel for the packet.
320 * @unc: Uncertainty count for an input packet.
321 * @grp: SE group that will process the input packet.
322 * @ctxl: Context Length in 64-bit words.
323 * @uddl: User-defined data (UDD) length in bytes.
324 * @ctxp: Context pointer. CTXP<63,2:0> must be zero in all cases.
329 #if defined(__BIG_ENDIAN_BITFIELD)
363 * struct slc_store_info - Solicited Paceket Output Store Information.
364 * @ssz: The number of scatterlist pointers for the solicited output port
366 * @rptr: The result pointer for the solicited output port packet.
367 * If [SSZ]=0, [RPTR] must point directly to a buffer on the remote
368 * host that is large enough to hold the entire output packet.
369 * If [SSZ]!=0, [RPTR] must point to an array of ([SSZ]+3)/4
370 * sglist components at [RPTR] on the remote host.
372 union slc_store_info
{
375 #if defined(__BIG_ENDIAN_BITFIELD)
389 * struct nps_pkt_instr - NPS Packet Instruction of SE cores.
390 * @dptr0 : Input pointer points to buffer in remote host.
391 * @ih: Packet Instruction Header (8 bytes)
392 * @irh: Packet Input Header (16 bytes)
393 * @slc: Solicited Packet Output Store Information (16 bytes)
396 * 64-Byte Instruction Format
398 struct nps_pkt_instr
{
400 union pkt_instr_hdr ih
;
402 union slc_store_info slc
;
407 * struct aqmq_command_s - The 32 byte command for AE processing.
408 * @opcode: Request opcode
409 * @param1: Request control parameter 1
410 * @param2: Request control parameter 2
411 * @dlen: Input length
412 * @dptr: Input pointer points to buffer in remote host
413 * @rptr: Result pointer points to buffer in remote host
414 * @grp: AQM Group (0..7)
415 * @cptr: Context pointer
417 struct aqmq_command_s
{
426 #if defined(__BIG_ENDIAN_BITFIELD)
437 * struct ctx_hdr - Book keeping data about the crypto context
438 * @pool: Pool used to allocate crypto context
439 * @dma: Base DMA address of the cypto context
440 * @ctx_dma: Actual usable crypto context for NITROX
443 struct dma_pool
*pool
;
449 * struct sglist_component - SG list component format
450 * @len0: The number of bytes at [PTR0] on the remote host.
451 * @len1: The number of bytes at [PTR1] on the remote host.
452 * @len2: The number of bytes at [PTR2] on the remote host.
453 * @len3: The number of bytes at [PTR3] on the remote host.
454 * @dma0: First pointer point to buffer in remote host.
455 * @dma1: Second pointer point to buffer in remote host.
456 * @dma2: Third pointer point to buffer in remote host.
457 * @dma3: Fourth pointer point to buffer in remote host.
459 struct nitrox_sgcomp
{
465 * strutct nitrox_sgtable - SG list information
466 * @sgmap_cnt: Number of buffers mapped
467 * @total_bytes: Total bytes in sglist.
468 * @sgcomp_len: Total sglist components length.
469 * @sgcomp_dma: DMA address of sglist component.
470 * @sg: crypto request buffer.
471 * @sgcomp: sglist component for NITROX.
473 struct nitrox_sgtable
{
477 dma_addr_t sgcomp_dma
;
478 struct scatterlist
*sg
;
479 struct nitrox_sgcomp
*sgcomp
;
482 /* Response Header Length */
484 /* Completion bytes Length */
492 typedef void (*completion_t
)(void *arg
, int err
);
495 * struct nitrox_softreq - Represents the NIROX Request.
496 * @response: response list entry
497 * @backlog: Backlog list entry
498 * @ndev: Device used to submit the request
499 * @cmdq: Command queue for submission
500 * @resp: Response headers
501 * @instr: 64B instruction
502 * @in: SG table for input
503 * @out SG table for output
504 * @tstamp: Request submitted time in jiffies
505 * @callback: callback after request completion/timeout
506 * @cb_arg: callback argument
508 struct nitrox_softreq
{
509 struct list_head response
;
510 struct list_head backlog
;
516 struct nitrox_device
*ndev
;
517 struct nitrox_cmdq
*cmdq
;
519 struct nps_pkt_instr instr
;
520 struct resp_hdr resp
;
521 struct nitrox_sgtable in
;
522 struct nitrox_sgtable out
;
524 unsigned long tstamp
;
526 completion_t callback
;
530 static inline int flexi_aes_keylen(int keylen
)
535 case AES_KEYSIZE_128
:
538 case AES_KEYSIZE_192
:
541 case AES_KEYSIZE_256
:
545 aes_keylen
= -EINVAL
;
551 static inline void *alloc_req_buf(int nents
, int extralen
, gfp_t gfp
)
555 size
= sizeof(struct scatterlist
) * nents
;
558 return kzalloc(size
, gfp
);
562 * create_single_sg - Point SG entry to the data
563 * @sg: Destination SG list
565 * @buflen: Data length
567 * Returns next free entry in the destination SG list
569 static inline struct scatterlist
*create_single_sg(struct scatterlist
*sg
,
570 void *buf
, int buflen
)
572 sg_set_buf(sg
, buf
, buflen
);
578 * create_multi_sg - Create multiple sg entries with buflen data length from
580 * @to_sg: Destination SG list
581 * @from_sg: Source SG list
582 * @buflen: Data length
584 * Returns next free entry in the destination SG list
586 static inline struct scatterlist
*create_multi_sg(struct scatterlist
*to_sg
,
587 struct scatterlist
*from_sg
,
590 struct scatterlist
*sg
= to_sg
;
593 for (; buflen
&& from_sg
; buflen
-= sglen
) {
594 sglen
= from_sg
->length
;
598 sg_set_buf(sg
, sg_virt(from_sg
), sglen
);
599 from_sg
= sg_next(from_sg
);
606 static inline void set_orh_value(u64
*orh
)
608 WRITE_ONCE(*orh
, PENDING_SIG
);
611 static inline void set_comp_value(u64
*comp
)
613 WRITE_ONCE(*comp
, PENDING_SIG
);
616 static inline int alloc_src_req_buf(struct nitrox_kcrypt_request
*nkreq
,
617 int nents
, int ivsize
)
619 struct se_crypto_request
*creq
= &nkreq
->creq
;
621 nkreq
->src
= alloc_req_buf(nents
, ivsize
, creq
->gfp
);
628 static inline void nitrox_creq_copy_iv(char *dst
, char *src
, int size
)
630 memcpy(dst
, src
, size
);
633 static inline struct scatterlist
*nitrox_creq_src_sg(char *iv
, int ivsize
)
635 return (struct scatterlist
*)(iv
+ ivsize
);
638 static inline void nitrox_creq_set_src_sg(struct nitrox_kcrypt_request
*nkreq
,
639 int nents
, int ivsize
,
640 struct scatterlist
*src
, int buflen
)
642 char *iv
= nkreq
->src
;
643 struct scatterlist
*sg
;
644 struct se_crypto_request
*creq
= &nkreq
->creq
;
646 creq
->src
= nitrox_creq_src_sg(iv
, ivsize
);
648 sg_init_table(sg
, nents
);
651 * +----+----------------+
652 * | IV | SRC sg entries |
653 * +----+----------------+
657 sg
= create_single_sg(sg
, iv
, ivsize
);
659 create_multi_sg(sg
, src
, buflen
);
662 static inline int alloc_dst_req_buf(struct nitrox_kcrypt_request
*nkreq
,
665 int extralen
= ORH_HLEN
+ COMP_HLEN
;
666 struct se_crypto_request
*creq
= &nkreq
->creq
;
668 nkreq
->dst
= alloc_req_buf(nents
, extralen
, creq
->gfp
);
675 static inline void nitrox_creq_set_orh(struct nitrox_kcrypt_request
*nkreq
)
677 struct se_crypto_request
*creq
= &nkreq
->creq
;
679 creq
->orh
= (u64
*)(nkreq
->dst
);
680 set_orh_value(creq
->orh
);
683 static inline void nitrox_creq_set_comp(struct nitrox_kcrypt_request
*nkreq
)
685 struct se_crypto_request
*creq
= &nkreq
->creq
;
687 creq
->comp
= (u64
*)(nkreq
->dst
+ ORH_HLEN
);
688 set_comp_value(creq
->comp
);
691 static inline struct scatterlist
*nitrox_creq_dst_sg(char *dst
)
693 return (struct scatterlist
*)(dst
+ ORH_HLEN
+ COMP_HLEN
);
696 static inline void nitrox_creq_set_dst_sg(struct nitrox_kcrypt_request
*nkreq
,
697 int nents
, int ivsize
,
698 struct scatterlist
*dst
, int buflen
)
700 struct se_crypto_request
*creq
= &nkreq
->creq
;
701 struct scatterlist
*sg
;
702 char *iv
= nkreq
->src
;
704 creq
->dst
= nitrox_creq_dst_sg(nkreq
->dst
);
706 sg_init_table(sg
, nents
);
709 * +-----+----+----------------+-----------------+
710 * | ORH | IV | DST sg entries | COMPLETION Bytes|
711 * +-----+----+----------------+-----------------+
715 sg
= create_single_sg(sg
, creq
->orh
, ORH_HLEN
);
717 sg
= create_single_sg(sg
, iv
, ivsize
);
719 sg
= create_multi_sg(sg
, dst
, buflen
);
720 /* COMPLETION Bytes */
721 create_single_sg(sg
, creq
->comp
, COMP_HLEN
);
724 #endif /* __NITROX_REQ_H */