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 #define PRIO 4001 12 13 /** 14 * struct gphdr - General purpose Header 15 * @param0: first parameter. 16 * @param1: second parameter. 17 * @param2: third parameter. 18 * @param3: fourth parameter. 19 * 20 * Params tell the iv and enc/dec data offsets. 21 */ 22 struct gphdr { 23 __be16 param0; 24 __be16 param1; 25 __be16 param2; 26 __be16 param3; 27 }; 28 29 /** 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 37 */ 38 union se_req_ctrl { 39 u64 value; 40 struct { 41 u64 raz : 22; 42 u64 arg : 8; 43 u64 ctxc : 2; 44 u64 unca : 1; 45 u64 info : 3; 46 u64 unc : 8; 47 u64 ctxl : 12; 48 u64 uddl : 8; 49 } s; 50 }; 51 52 #define MAX_IV_LEN 16 53 54 /** 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. 59 * @gph: GP Header 60 * @ctrl: Request Information. 61 * @orh: ORH address 62 * @comp: completion address 63 * @src: Input sglist 64 * @dst: Output sglist 65 */ 66 struct se_crypto_request { 67 u8 opcode; 68 gfp_t gfp; 69 u32 flags; 70 u64 ctx_handle; 71 72 struct gphdr gph; 73 union se_req_ctrl ctrl; 74 u64 *orh; 75 u64 *comp; 76 77 struct scatterlist *src; 78 struct scatterlist *dst; 79 }; 80 81 /* Crypto opcodes */ 82 #define FLEXI_CRYPTO_ENCRYPT_HMAC 0x33 83 #define ENCRYPT 0 84 #define DECRYPT 1 85 86 /* IV from context */ 87 #define IV_FROM_CTX 0 88 /* IV from Input data */ 89 #define IV_FROM_DPTR 1 90 91 /** 92 * cipher opcodes for firmware 93 */ 94 enum flexi_cipher { 95 CIPHER_NULL = 0, 96 CIPHER_3DES_CBC, 97 CIPHER_3DES_ECB, 98 CIPHER_AES_CBC, 99 CIPHER_AES_ECB, 100 CIPHER_AES_CFB, 101 CIPHER_AES_CTR, 102 CIPHER_AES_GCM, 103 CIPHER_AES_XTS, 104 CIPHER_AES_CCM, 105 CIPHER_AES_CBC_CTS, 106 CIPHER_AES_ECB_CTS, 107 CIPHER_INVALID 108 }; 109 110 enum flexi_auth { 111 AUTH_NULL = 0, 112 AUTH_MD5, 113 AUTH_SHA1, 114 AUTH_SHA2_SHA224, 115 AUTH_SHA2_SHA256, 116 AUTH_SHA2_SHA384, 117 AUTH_SHA2_SHA512, 118 AUTH_GMAC, 119 AUTH_INVALID 120 }; 121 122 /** 123 * struct crypto_keys - Crypto keys 124 * @key: Encryption key or KEY1 for AES-XTS 125 * @iv: Encryption IV or Tweak for AES-XTS 126 */ 127 struct crypto_keys { 128 union { 129 u8 key[AES_MAX_KEY_SIZE]; 130 u8 key1[AES_MAX_KEY_SIZE]; 131 } u; 132 u8 iv[AES_BLOCK_SIZE]; 133 }; 134 135 /** 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 139 */ 140 struct auth_keys { 141 union { 142 u8 ipad[64]; 143 u8 key2[64]; 144 } u; 145 u8 opad[64]; 146 }; 147 148 union fc_ctx_flags { 149 __be64 f; 150 struct { 151 #if defined(__BIG_ENDIAN_BITFIELD) 152 u64 cipher_type : 4; 153 u64 reserved_59 : 1; 154 u64 aes_keylen : 2; 155 u64 iv_source : 1; 156 u64 hash_type : 4; 157 u64 reserved_49_51 : 3; 158 u64 auth_input_type: 1; 159 u64 mac_len : 8; 160 u64 reserved_0_39 : 40; 161 #else 162 u64 reserved_0_39 : 40; 163 u64 mac_len : 8; 164 u64 auth_input_type: 1; 165 u64 reserved_49_51 : 3; 166 u64 hash_type : 4; 167 u64 iv_source : 1; 168 u64 aes_keylen : 2; 169 u64 reserved_59 : 1; 170 u64 cipher_type : 4; 171 #endif 172 } w0; 173 }; 174 /** 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 186 */ 187 struct flexi_crypto_context { 188 union fc_ctx_flags flags; 189 struct crypto_keys crypto; 190 struct auth_keys auth; 191 }; 192 193 struct crypto_ctx_hdr { 194 struct dma_pool *pool; 195 dma_addr_t dma; 196 void *vaddr; 197 }; 198 199 struct nitrox_crypto_ctx { 200 struct nitrox_device *ndev; 201 union { 202 u64 ctx_handle; 203 struct flexi_crypto_context *fctx; 204 } u; 205 struct crypto_ctx_hdr *chdr; 206 }; 207 208 struct nitrox_kcrypt_request { 209 struct se_crypto_request creq; 210 u8 *src; 211 u8 *dst; 212 }; 213 214 /** 215 * struct pkt_instr_hdr - Packet Instruction Header 216 * @g: Gather used 217 * When [G] is set and [GSZ] != 0, the instruction is 218 * indirect gather instruction. 219 * When [G] is set and [GSZ] = 0, the instruction is 220 * direct gather instruction. 221 * @gsz: Number of pointers in the indirect gather list 222 * @ihi: When set hardware duplicates the 1st 8 bytes of pkt_instr_hdr 223 * and adds them to the packet after the pkt_instr_hdr but before any UDD 224 * @ssz: Not used by the input hardware. But can become slc_store_int[SSZ] 225 * when [IHI] is set. 226 * @fsz: The number of front data bytes directly included in the 227 * PCIe instruction. 228 * @tlen: The length of the input packet in bytes, include: 229 * - 16B pkt_hdr 230 * - Inline context bytes if any, 231 * - UDD if any, 232 * - packet payload bytes 233 */ 234 union pkt_instr_hdr { 235 u64 value; 236 struct { 237 #if defined(__BIG_ENDIAN_BITFIELD) 238 u64 raz_48_63 : 16; 239 u64 g : 1; 240 u64 gsz : 7; 241 u64 ihi : 1; 242 u64 ssz : 7; 243 u64 raz_30_31 : 2; 244 u64 fsz : 6; 245 u64 raz_16_23 : 8; 246 u64 tlen : 16; 247 #else 248 u64 tlen : 16; 249 u64 raz_16_23 : 8; 250 u64 fsz : 6; 251 u64 raz_30_31 : 2; 252 u64 ssz : 7; 253 u64 ihi : 1; 254 u64 gsz : 7; 255 u64 g : 1; 256 u64 raz_48_63 : 16; 257 #endif 258 } s; 259 }; 260 261 /** 262 * struct pkt_hdr - Packet Input Header 263 * @opcode: Request opcode (Major) 264 * @arg: Request opcode (Minor) 265 * @ctxc: Context control. 266 * @unca: When set [UNC] is the uncertainty count for an input packet. 267 * The hardware uses uncertainty counts to predict 268 * output buffer use and avoid deadlock. 269 * @info: Not used by input hardware. Available for use 270 * during SE processing. 271 * @destport: The expected destination port/ring/channel for the packet. 272 * @unc: Uncertainty count for an input packet. 273 * @grp: SE group that will process the input packet. 274 * @ctxl: Context Length in 64-bit words. 275 * @uddl: User-defined data (UDD) length in bytes. 276 * @ctxp: Context pointer. CTXP<63,2:0> must be zero in all cases. 277 */ 278 union pkt_hdr { 279 u64 value[2]; 280 struct { 281 #if defined(__BIG_ENDIAN_BITFIELD) 282 u64 opcode : 8; 283 u64 arg : 8; 284 u64 ctxc : 2; 285 u64 unca : 1; 286 u64 raz_44 : 1; 287 u64 info : 3; 288 u64 destport : 9; 289 u64 unc : 8; 290 u64 raz_19_23 : 5; 291 u64 grp : 3; 292 u64 raz_15 : 1; 293 u64 ctxl : 7; 294 u64 uddl : 8; 295 #else 296 u64 uddl : 8; 297 u64 ctxl : 7; 298 u64 raz_15 : 1; 299 u64 grp : 3; 300 u64 raz_19_23 : 5; 301 u64 unc : 8; 302 u64 destport : 9; 303 u64 info : 3; 304 u64 raz_44 : 1; 305 u64 unca : 1; 306 u64 ctxc : 2; 307 u64 arg : 8; 308 u64 opcode : 8; 309 #endif 310 __be64 ctxp; 311 } s; 312 }; 313 314 /** 315 * struct slc_store_info - Solicited Paceket Output Store Information. 316 * @ssz: The number of scatterlist pointers for the solicited output port 317 * packet. 318 * @rptr: The result pointer for the solicited output port packet. 319 * If [SSZ]=0, [RPTR] must point directly to a buffer on the remote 320 * host that is large enough to hold the entire output packet. 321 * If [SSZ]!=0, [RPTR] must point to an array of ([SSZ]+3)/4 322 * sglist components at [RPTR] on the remote host. 323 */ 324 union slc_store_info { 325 u64 value[2]; 326 struct { 327 #if defined(__BIG_ENDIAN_BITFIELD) 328 u64 raz_39_63 : 25; 329 u64 ssz : 7; 330 u64 raz_0_31 : 32; 331 #else 332 u64 raz_0_31 : 32; 333 u64 ssz : 7; 334 u64 raz_39_63 : 25; 335 #endif 336 __be64 rptr; 337 } s; 338 }; 339 340 /** 341 * struct nps_pkt_instr - NPS Packet Instruction of SE cores. 342 * @dptr0 : Input pointer points to buffer in remote host. 343 * @ih: Packet Instruction Header (8 bytes) 344 * @irh: Packet Input Header (16 bytes) 345 * @slc: Solicited Packet Output Store Information (16 bytes) 346 * @fdata: Front data 347 * 348 * 64-Byte Instruction Format 349 */ 350 struct nps_pkt_instr { 351 __be64 dptr0; 352 union pkt_instr_hdr ih; 353 union pkt_hdr irh; 354 union slc_store_info slc; 355 u64 fdata[2]; 356 }; 357 358 /** 359 * struct ctx_hdr - Book keeping data about the crypto context 360 * @pool: Pool used to allocate crypto context 361 * @dma: Base DMA address of the cypto context 362 * @ctx_dma: Actual usable crypto context for NITROX 363 */ 364 struct ctx_hdr { 365 struct dma_pool *pool; 366 dma_addr_t dma; 367 dma_addr_t ctx_dma; 368 }; 369 370 /* 371 * struct sglist_component - SG list component format 372 * @len0: The number of bytes at [PTR0] on the remote host. 373 * @len1: The number of bytes at [PTR1] on the remote host. 374 * @len2: The number of bytes at [PTR2] on the remote host. 375 * @len3: The number of bytes at [PTR3] on the remote host. 376 * @dma0: First pointer point to buffer in remote host. 377 * @dma1: Second pointer point to buffer in remote host. 378 * @dma2: Third pointer point to buffer in remote host. 379 * @dma3: Fourth pointer point to buffer in remote host. 380 */ 381 struct nitrox_sgcomp { 382 __be16 len[4]; 383 __be64 dma[4]; 384 }; 385 386 /* 387 * strutct nitrox_sgtable - SG list information 388 * @sgmap_cnt: Number of buffers mapped 389 * @total_bytes: Total bytes in sglist. 390 * @sgcomp_len: Total sglist components length. 391 * @sgcomp_dma: DMA address of sglist component. 392 * @sg: crypto request buffer. 393 * @sgcomp: sglist component for NITROX. 394 */ 395 struct nitrox_sgtable { 396 u8 sgmap_cnt; 397 u16 total_bytes; 398 u32 sgcomp_len; 399 dma_addr_t sgcomp_dma; 400 struct scatterlist *sg; 401 struct nitrox_sgcomp *sgcomp; 402 }; 403 404 /* Response Header Length */ 405 #define ORH_HLEN 8 406 /* Completion bytes Length */ 407 #define COMP_HLEN 8 408 409 struct resp_hdr { 410 u64 *orh; 411 u64 *completion; 412 }; 413 414 typedef void (*completion_t)(void *arg, int err); 415 416 /** 417 * struct nitrox_softreq - Represents the NIROX Request. 418 * @response: response list entry 419 * @backlog: Backlog list entry 420 * @ndev: Device used to submit the request 421 * @cmdq: Command queue for submission 422 * @resp: Response headers 423 * @instr: 64B instruction 424 * @in: SG table for input 425 * @out SG table for output 426 * @tstamp: Request submitted time in jiffies 427 * @callback: callback after request completion/timeout 428 * @cb_arg: callback argument 429 */ 430 struct nitrox_softreq { 431 struct list_head response; 432 struct list_head backlog; 433 434 u32 flags; 435 gfp_t gfp; 436 atomic_t status; 437 438 struct nitrox_device *ndev; 439 struct nitrox_cmdq *cmdq; 440 441 struct nps_pkt_instr instr; 442 struct resp_hdr resp; 443 struct nitrox_sgtable in; 444 struct nitrox_sgtable out; 445 446 unsigned long tstamp; 447 448 completion_t callback; 449 void *cb_arg; 450 }; 451 452 static inline int flexi_aes_keylen(int keylen) 453 { 454 int aes_keylen; 455 456 switch (keylen) { 457 case AES_KEYSIZE_128: 458 aes_keylen = 1; 459 break; 460 case AES_KEYSIZE_192: 461 aes_keylen = 2; 462 break; 463 case AES_KEYSIZE_256: 464 aes_keylen = 3; 465 break; 466 default: 467 aes_keylen = -EINVAL; 468 break; 469 } 470 return aes_keylen; 471 } 472 473 static inline void *alloc_req_buf(int nents, int extralen, gfp_t gfp) 474 { 475 size_t size; 476 477 size = sizeof(struct scatterlist) * nents; 478 size += extralen; 479 480 return kzalloc(size, gfp); 481 } 482 483 /** 484 * create_single_sg - Point SG entry to the data 485 * @sg: Destination SG list 486 * @buf: Data 487 * @buflen: Data length 488 * 489 * Returns next free entry in the destination SG list 490 **/ 491 static inline struct scatterlist *create_single_sg(struct scatterlist *sg, 492 void *buf, int buflen) 493 { 494 sg_set_buf(sg, buf, buflen); 495 sg++; 496 return sg; 497 } 498 499 /** 500 * create_multi_sg - Create multiple sg entries with buflen data length from 501 * source sglist 502 * @to_sg: Destination SG list 503 * @from_sg: Source SG list 504 * @buflen: Data length 505 * 506 * Returns next free entry in the destination SG list 507 **/ 508 static inline struct scatterlist *create_multi_sg(struct scatterlist *to_sg, 509 struct scatterlist *from_sg, 510 int buflen) 511 { 512 struct scatterlist *sg = to_sg; 513 unsigned int sglen; 514 515 for (; buflen; buflen -= sglen) { 516 sglen = from_sg->length; 517 if (sglen > buflen) 518 sglen = buflen; 519 520 sg_set_buf(sg, sg_virt(from_sg), sglen); 521 from_sg = sg_next(from_sg); 522 sg++; 523 } 524 525 return sg; 526 } 527 528 static inline void set_orh_value(u64 *orh) 529 { 530 WRITE_ONCE(*orh, PENDING_SIG); 531 } 532 533 static inline void set_comp_value(u64 *comp) 534 { 535 WRITE_ONCE(*comp, PENDING_SIG); 536 } 537 538 static inline int alloc_src_req_buf(struct nitrox_kcrypt_request *nkreq, 539 int nents, int ivsize) 540 { 541 struct se_crypto_request *creq = &nkreq->creq; 542 543 nkreq->src = alloc_req_buf(nents, ivsize, creq->gfp); 544 if (!nkreq->src) 545 return -ENOMEM; 546 547 return 0; 548 } 549 550 static inline void nitrox_creq_copy_iv(char *dst, char *src, int size) 551 { 552 memcpy(dst, src, size); 553 } 554 555 static inline struct scatterlist *nitrox_creq_src_sg(char *iv, int ivsize) 556 { 557 return (struct scatterlist *)(iv + ivsize); 558 } 559 560 static inline void nitrox_creq_set_src_sg(struct nitrox_kcrypt_request *nkreq, 561 int nents, int ivsize, 562 struct scatterlist *src, int buflen) 563 { 564 char *iv = nkreq->src; 565 struct scatterlist *sg; 566 struct se_crypto_request *creq = &nkreq->creq; 567 568 creq->src = nitrox_creq_src_sg(iv, ivsize); 569 sg = creq->src; 570 sg_init_table(sg, nents); 571 572 /* Input format: 573 * +----+----------------+ 574 * | IV | SRC sg entries | 575 * +----+----------------+ 576 */ 577 578 /* IV */ 579 sg = create_single_sg(sg, iv, ivsize); 580 /* SRC entries */ 581 create_multi_sg(sg, src, buflen); 582 } 583 584 static inline int alloc_dst_req_buf(struct nitrox_kcrypt_request *nkreq, 585 int nents) 586 { 587 int extralen = ORH_HLEN + COMP_HLEN; 588 struct se_crypto_request *creq = &nkreq->creq; 589 590 nkreq->dst = alloc_req_buf(nents, extralen, creq->gfp); 591 if (!nkreq->dst) 592 return -ENOMEM; 593 594 return 0; 595 } 596 597 static inline void nitrox_creq_set_orh(struct nitrox_kcrypt_request *nkreq) 598 { 599 struct se_crypto_request *creq = &nkreq->creq; 600 601 creq->orh = (u64 *)(nkreq->dst); 602 set_orh_value(creq->orh); 603 } 604 605 static inline void nitrox_creq_set_comp(struct nitrox_kcrypt_request *nkreq) 606 { 607 struct se_crypto_request *creq = &nkreq->creq; 608 609 creq->comp = (u64 *)(nkreq->dst + ORH_HLEN); 610 set_comp_value(creq->comp); 611 } 612 613 static inline struct scatterlist *nitrox_creq_dst_sg(char *dst) 614 { 615 return (struct scatterlist *)(dst + ORH_HLEN + COMP_HLEN); 616 } 617 618 static inline void nitrox_creq_set_dst_sg(struct nitrox_kcrypt_request *nkreq, 619 int nents, int ivsize, 620 struct scatterlist *dst, int buflen) 621 { 622 struct se_crypto_request *creq = &nkreq->creq; 623 struct scatterlist *sg; 624 char *iv = nkreq->src; 625 626 creq->dst = nitrox_creq_dst_sg(nkreq->dst); 627 sg = creq->dst; 628 sg_init_table(sg, nents); 629 630 /* Output format: 631 * +-----+----+----------------+-----------------+ 632 * | ORH | IV | DST sg entries | COMPLETION Bytes| 633 * +-----+----+----------------+-----------------+ 634 */ 635 636 /* ORH */ 637 sg = create_single_sg(sg, creq->orh, ORH_HLEN); 638 /* IV */ 639 sg = create_single_sg(sg, iv, ivsize); 640 /* DST entries */ 641 sg = create_multi_sg(sg, dst, buflen); 642 /* COMPLETION Bytes */ 643 create_single_sg(sg, creq->comp, COMP_HLEN); 644 } 645 646 #endif /* __NITROX_REQ_H */ 647