xref: /linux/drivers/crypto/cavium/nitrox/nitrox_req.h (revision 76d09ea7c22f2cabf1f66ffc287c23b19b120be9)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NITROX_REQ_H
3 #define __NITROX_REQ_H
4 
5 #include <linux/dma-mapping.h>
6 #include <crypto/aes.h>
7 
8 #include "nitrox_dev.h"
9 
10 #define PENDING_SIG	0xFFFFFFFFFFFFFFFFUL
11 
12 /**
13  * struct gphdr - General purpose Header
14  * @param0: first parameter.
15  * @param1: second parameter.
16  * @param2: third parameter.
17  * @param3: fourth parameter.
18  *
19  * Params tell the iv and enc/dec data offsets.
20  */
21 struct gphdr {
22 	__be16 param0;
23 	__be16 param1;
24 	__be16 param2;
25 	__be16 param3;
26 };
27 
28 /**
29  * struct se_req_ctrl - SE request information.
30  * @arg: Minor number of the opcode
31  * @ctxc: Context control.
32  * @unca: Uncertainity enabled.
33  * @info: Additional information for SE cores.
34  * @ctxl: Context length in bytes.
35  * @uddl: User defined data length
36  */
37 union se_req_ctrl {
38 	u64 value;
39 	struct {
40 		u64 raz	: 22;
41 		u64 arg	: 8;
42 		u64 ctxc : 2;
43 		u64 unca : 1;
44 		u64 info : 3;
45 		u64 unc : 8;
46 		u64 ctxl : 12;
47 		u64 uddl : 8;
48 	} s;
49 };
50 
51 #define MAX_IV_LEN 16
52 
53 /**
54  * struct se_crypto_request - SE crypto request structure.
55  * @opcode: Request opcode (enc/dec)
56  * @flags: flags from crypto subsystem
57  * @ctx_handle: Crypto context handle.
58  * @gph: GP Header
59  * @ctrl: Request Information.
60  * @orh: ORH address
61  * @comp: completion address
62  * @src: Input sglist
63  * @dst: Output sglist
64  */
65 struct se_crypto_request {
66 	u8 opcode;
67 	gfp_t gfp;
68 	u32 flags;
69 	u64 ctx_handle;
70 
71 	struct gphdr gph;
72 	union se_req_ctrl ctrl;
73 	u64 *orh;
74 	u64 *comp;
75 
76 	struct scatterlist *src;
77 	struct scatterlist *dst;
78 };
79 
80 /* Crypto opcodes */
81 #define FLEXI_CRYPTO_ENCRYPT_HMAC	0x33
82 #define ENCRYPT	0
83 #define DECRYPT 1
84 
85 /* IV from context */
86 #define IV_FROM_CTX	0
87 /* IV from Input data */
88 #define IV_FROM_DPTR	1
89 
90 /**
91  * cipher opcodes for firmware
92  */
93 enum flexi_cipher {
94 	CIPHER_NULL = 0,
95 	CIPHER_3DES_CBC,
96 	CIPHER_3DES_ECB,
97 	CIPHER_AES_CBC,
98 	CIPHER_AES_ECB,
99 	CIPHER_AES_CFB,
100 	CIPHER_AES_CTR,
101 	CIPHER_AES_GCM,
102 	CIPHER_AES_XTS,
103 	CIPHER_AES_CCM,
104 	CIPHER_AES_CBC_CTS,
105 	CIPHER_AES_ECB_CTS,
106 	CIPHER_INVALID
107 };
108 
109 /**
110  * struct crypto_keys - Crypto keys
111  * @key: Encryption key or KEY1 for AES-XTS
112  * @iv: Encryption IV or Tweak for AES-XTS
113  */
114 struct crypto_keys {
115 	union {
116 		u8 key[AES_MAX_KEY_SIZE];
117 		u8 key1[AES_MAX_KEY_SIZE];
118 	} u;
119 	u8 iv[AES_BLOCK_SIZE];
120 };
121 
122 /**
123  * struct auth_keys - Authentication keys
124  * @ipad: IPAD or KEY2 for AES-XTS
125  * @opad: OPAD or AUTH KEY if auth_input_type = 1
126  */
127 struct auth_keys {
128 	union {
129 		u8 ipad[64];
130 		u8 key2[64];
131 	} u;
132 	u8 opad[64];
133 };
134 
135 /**
136  * struct flexi_crypto_context - Crypto context
137  * @cipher_type: Encryption cipher type
138  * @aes_keylen: AES key length
139  * @iv_source: Encryption IV source
140  * @hash_type: Authentication type
141  * @auth_input_type: Authentication input type
142  *   1 - Authentication IV and KEY, microcode calculates OPAD/IPAD
143  *   0 - Authentication OPAD/IPAD
144  * @mac_len: mac length
145  * @crypto: Crypto keys
146  * @auth: Authentication keys
147  */
148 struct flexi_crypto_context {
149 	union {
150 		__be64 flags;
151 		struct {
152 #if defined(__BIG_ENDIAN_BITFIELD)
153 			u64 cipher_type	: 4;
154 			u64 reserved_59	: 1;
155 			u64 aes_keylen : 2;
156 			u64 iv_source : 1;
157 			u64 hash_type : 4;
158 			u64 reserved_49_51 : 3;
159 			u64 auth_input_type: 1;
160 			u64 mac_len : 8;
161 			u64 reserved_0_39 : 40;
162 #else
163 			u64 reserved_0_39 : 40;
164 			u64 mac_len : 8;
165 			u64 auth_input_type: 1;
166 			u64 reserved_49_51 : 3;
167 			u64 hash_type : 4;
168 			u64 iv_source : 1;
169 			u64 aes_keylen : 2;
170 			u64 reserved_59	: 1;
171 			u64 cipher_type	: 4;
172 #endif
173 		} w0;
174 	};
175 
176 	struct crypto_keys crypto;
177 	struct auth_keys auth;
178 };
179 
180 struct crypto_ctx_hdr {
181 	struct dma_pool *pool;
182 	dma_addr_t dma;
183 	void *vaddr;
184 };
185 
186 struct nitrox_crypto_ctx {
187 	struct nitrox_device *ndev;
188 	union {
189 		u64 ctx_handle;
190 		struct flexi_crypto_context *fctx;
191 	} u;
192 	struct crypto_ctx_hdr *chdr;
193 };
194 
195 struct nitrox_kcrypt_request {
196 	struct se_crypto_request creq;
197 	struct nitrox_crypto_ctx *nctx;
198 	struct skcipher_request *skreq;
199 	u8 *src;
200 	u8 *dst;
201 };
202 
203 /**
204  * struct pkt_instr_hdr - Packet Instruction Header
205  * @g: Gather used
206  *   When [G] is set and [GSZ] != 0, the instruction is
207  *   indirect gather instruction.
208  *   When [G] is set and [GSZ] = 0, the instruction is
209  *   direct gather instruction.
210  * @gsz: Number of pointers in the indirect gather list
211  * @ihi: When set hardware duplicates the 1st 8 bytes of pkt_instr_hdr
212  *   and adds them to the packet after the pkt_instr_hdr but before any UDD
213  * @ssz: Not used by the input hardware. But can become slc_store_int[SSZ]
214  *   when [IHI] is set.
215  * @fsz: The number of front data bytes directly included in the
216  *   PCIe instruction.
217  * @tlen: The length of the input packet in bytes, include:
218  *   - 16B pkt_hdr
219  *   - Inline context bytes if any,
220  *   - UDD if any,
221  *   - packet payload bytes
222  */
223 union pkt_instr_hdr {
224 	u64 value;
225 	struct {
226 #if defined(__BIG_ENDIAN_BITFIELD)
227 		u64 raz_48_63 : 16;
228 		u64 g : 1;
229 		u64 gsz	: 7;
230 		u64 ihi	: 1;
231 		u64 ssz	: 7;
232 		u64 raz_30_31 : 2;
233 		u64 fsz	: 6;
234 		u64 raz_16_23 : 8;
235 		u64 tlen : 16;
236 #else
237 		u64 tlen : 16;
238 		u64 raz_16_23 : 8;
239 		u64 fsz	: 6;
240 		u64 raz_30_31 : 2;
241 		u64 ssz	: 7;
242 		u64 ihi	: 1;
243 		u64 gsz	: 7;
244 		u64 g : 1;
245 		u64 raz_48_63 : 16;
246 #endif
247 	} s;
248 };
249 
250 /**
251  * struct pkt_hdr - Packet Input Header
252  * @opcode: Request opcode (Major)
253  * @arg: Request opcode (Minor)
254  * @ctxc: Context control.
255  * @unca: When set [UNC] is the uncertainty count for an input packet.
256  *        The hardware uses uncertainty counts to predict
257  *        output buffer use and avoid deadlock.
258  * @info: Not used by input hardware. Available for use
259  *        during SE processing.
260  * @destport: The expected destination port/ring/channel for the packet.
261  * @unc: Uncertainty count for an input packet.
262  * @grp: SE group that will process the input packet.
263  * @ctxl: Context Length in 64-bit words.
264  * @uddl: User-defined data (UDD) length in bytes.
265  * @ctxp: Context pointer. CTXP<63,2:0> must be zero in all cases.
266  */
267 union pkt_hdr {
268 	u64 value[2];
269 	struct {
270 #if defined(__BIG_ENDIAN_BITFIELD)
271 		u64 opcode : 8;
272 		u64 arg	: 8;
273 		u64 ctxc : 2;
274 		u64 unca : 1;
275 		u64 raz_44 : 1;
276 		u64 info : 3;
277 		u64 destport : 9;
278 		u64 unc	: 8;
279 		u64 raz_19_23 : 5;
280 		u64 grp	: 3;
281 		u64 raz_15 : 1;
282 		u64 ctxl : 7;
283 		u64 uddl : 8;
284 #else
285 		u64 uddl : 8;
286 		u64 ctxl : 7;
287 		u64 raz_15 : 1;
288 		u64 grp	: 3;
289 		u64 raz_19_23 : 5;
290 		u64 unc	: 8;
291 		u64 destport : 9;
292 		u64 info : 3;
293 		u64 raz_44 : 1;
294 		u64 unca : 1;
295 		u64 ctxc : 2;
296 		u64 arg	: 8;
297 		u64 opcode : 8;
298 #endif
299 		__be64 ctxp;
300 	} s;
301 };
302 
303 /**
304  * struct slc_store_info - Solicited Paceket Output Store Information.
305  * @ssz: The number of scatterlist pointers for the solicited output port
306  *       packet.
307  * @rptr: The result pointer for the solicited output port packet.
308  *        If [SSZ]=0, [RPTR] must point directly to a buffer on the remote
309  *        host that is large enough to hold the entire output packet.
310  *        If [SSZ]!=0, [RPTR] must point to an array of ([SSZ]+3)/4
311  *        sglist components at [RPTR] on the remote host.
312  */
313 union slc_store_info {
314 	u64 value[2];
315 	struct {
316 #if defined(__BIG_ENDIAN_BITFIELD)
317 		u64 raz_39_63 : 25;
318 		u64 ssz	: 7;
319 		u64 raz_0_31 : 32;
320 #else
321 		u64 raz_0_31 : 32;
322 		u64 ssz	: 7;
323 		u64 raz_39_63 : 25;
324 #endif
325 		__be64 rptr;
326 	} s;
327 };
328 
329 /**
330  * struct nps_pkt_instr - NPS Packet Instruction of SE cores.
331  * @dptr0 : Input pointer points to buffer in remote host.
332  * @ih: Packet Instruction Header (8 bytes)
333  * @irh: Packet Input Header (16 bytes)
334  * @slc: Solicited Packet Output Store Information (16 bytes)
335  * @fdata: Front data
336  *
337  * 64-Byte Instruction Format
338  */
339 struct nps_pkt_instr {
340 	__be64 dptr0;
341 	union pkt_instr_hdr ih;
342 	union pkt_hdr irh;
343 	union slc_store_info slc;
344 	u64 fdata[2];
345 };
346 
347 /**
348  * struct ctx_hdr - Book keeping data about the crypto context
349  * @pool: Pool used to allocate crypto context
350  * @dma: Base DMA address of the cypto context
351  * @ctx_dma: Actual usable crypto context for NITROX
352  */
353 struct ctx_hdr {
354 	struct dma_pool *pool;
355 	dma_addr_t dma;
356 	dma_addr_t ctx_dma;
357 };
358 
359 /*
360  * struct sglist_component - SG list component format
361  * @len0: The number of bytes at [PTR0] on the remote host.
362  * @len1: The number of bytes at [PTR1] on the remote host.
363  * @len2: The number of bytes at [PTR2] on the remote host.
364  * @len3: The number of bytes at [PTR3] on the remote host.
365  * @dma0: First pointer point to buffer in remote host.
366  * @dma1: Second pointer point to buffer in remote host.
367  * @dma2: Third pointer point to buffer in remote host.
368  * @dma3: Fourth pointer point to buffer in remote host.
369  */
370 struct nitrox_sgcomp {
371 	__be16 len[4];
372 	__be64 dma[4];
373 };
374 
375 /*
376  * strutct nitrox_sgtable - SG list information
377  * @sgmap_cnt: Number of buffers mapped
378  * @total_bytes: Total bytes in sglist.
379  * @sgcomp_len: Total sglist components length.
380  * @sgcomp_dma: DMA address of sglist component.
381  * @sg: crypto request buffer.
382  * @sgcomp: sglist component for NITROX.
383  */
384 struct nitrox_sgtable {
385 	u8 sgmap_cnt;
386 	u16 total_bytes;
387 	u32 sgcomp_len;
388 	dma_addr_t sgcomp_dma;
389 	struct scatterlist *sg;
390 	struct nitrox_sgcomp *sgcomp;
391 };
392 
393 /* Response Header Length */
394 #define ORH_HLEN	8
395 /* Completion bytes Length */
396 #define COMP_HLEN	8
397 
398 struct resp_hdr {
399 	u64 *orh;
400 	u64 *completion;
401 };
402 
403 typedef void (*completion_t)(struct skcipher_request *skreq, int err);
404 
405 /**
406  * struct nitrox_softreq - Represents the NIROX Request.
407  * @response: response list entry
408  * @backlog: Backlog list entry
409  * @ndev: Device used to submit the request
410  * @cmdq: Command queue for submission
411  * @resp: Response headers
412  * @instr: 64B instruction
413  * @in: SG table for input
414  * @out SG table for output
415  * @tstamp: Request submitted time in jiffies
416  * @callback: callback after request completion/timeout
417  * @cb_arg: callback argument
418  */
419 struct nitrox_softreq {
420 	struct list_head response;
421 	struct list_head backlog;
422 
423 	u32 flags;
424 	gfp_t gfp;
425 	atomic_t status;
426 
427 	struct nitrox_device *ndev;
428 	struct nitrox_cmdq *cmdq;
429 
430 	struct nps_pkt_instr instr;
431 	struct resp_hdr resp;
432 	struct nitrox_sgtable in;
433 	struct nitrox_sgtable out;
434 
435 	unsigned long tstamp;
436 
437 	completion_t callback;
438 	struct skcipher_request *skreq;
439 };
440 
441 static inline void *alloc_req_buf(int nents, int extralen, gfp_t gfp)
442 {
443 	size_t size;
444 
445 	size = sizeof(struct scatterlist) * nents;
446 	size += extralen;
447 
448 	return kzalloc(size, gfp);
449 }
450 
451 static inline struct scatterlist *create_single_sg(struct scatterlist *sg,
452 						   void *buf, int buflen)
453 {
454 	sg_set_buf(sg, buf, buflen);
455 	sg++;
456 	return sg;
457 }
458 
459 static inline struct scatterlist *create_multi_sg(struct scatterlist *to_sg,
460 						  struct scatterlist *from_sg)
461 {
462 	struct scatterlist *sg;
463 	int i;
464 
465 	for_each_sg(from_sg, sg, sg_nents(from_sg), i) {
466 		sg_set_buf(to_sg, sg_virt(sg), sg->length);
467 		to_sg++;
468 	}
469 
470 	return to_sg;
471 }
472 
473 static inline void set_orh_value(u64 *orh)
474 {
475 	WRITE_ONCE(*orh, PENDING_SIG);
476 }
477 
478 static inline void set_comp_value(u64 *comp)
479 {
480 	WRITE_ONCE(*comp, PENDING_SIG);
481 }
482 
483 #endif /* __NITROX_REQ_H */
484