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