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