Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1 | /* |
| 2 | * caam - Freescale FSL CAAM support for crypto API |
| 3 | * |
| 4 | * Copyright 2008-2011 Freescale Semiconductor, Inc. |
| 5 | * |
| 6 | * Based on talitos crypto API driver. |
| 7 | * |
| 8 | * relationship of job descriptors to shared descriptors (SteveC Dec 10 2008): |
| 9 | * |
| 10 | * --------------- --------------- |
| 11 | * | JobDesc #1 |-------------------->| ShareDesc | |
| 12 | * | *(packet 1) | | (PDB) | |
| 13 | * --------------- |------------->| (hashKey) | |
| 14 | * . | | (cipherKey) | |
| 15 | * . | |-------->| (operation) | |
| 16 | * --------------- | | --------------- |
| 17 | * | JobDesc #2 |------| | |
| 18 | * | *(packet 2) | | |
| 19 | * --------------- | |
| 20 | * . | |
| 21 | * . | |
| 22 | * --------------- | |
| 23 | * | JobDesc #3 |------------ |
| 24 | * | *(packet 3) | |
| 25 | * --------------- |
| 26 | * |
| 27 | * The SharedDesc never changes for a connection unless rekeyed, but |
| 28 | * each packet will likely be in a different place. So all we need |
| 29 | * to know to process the packet is where the input is, where the |
| 30 | * output goes, and what context we want to process with. Context is |
| 31 | * in the SharedDesc, packet references in the JobDesc. |
| 32 | * |
| 33 | * So, a job desc looks like: |
| 34 | * |
| 35 | * --------------------- |
| 36 | * | Header | |
| 37 | * | ShareDesc Pointer | |
| 38 | * | SEQ_OUT_PTR | |
| 39 | * | (output buffer) | |
| 40 | * | SEQ_IN_PTR | |
| 41 | * | (input buffer) | |
| 42 | * | LOAD (to DECO) | |
| 43 | * --------------------- |
| 44 | */ |
| 45 | |
| 46 | #include "compat.h" |
| 47 | |
| 48 | #include "regs.h" |
| 49 | #include "intern.h" |
| 50 | #include "desc_constr.h" |
| 51 | #include "jr.h" |
| 52 | #include "error.h" |
| 53 | |
| 54 | /* |
| 55 | * crypto alg |
| 56 | */ |
| 57 | #define CAAM_CRA_PRIORITY 3000 |
| 58 | /* max key is sum of AES_MAX_KEY_SIZE, max split key size */ |
| 59 | #define CAAM_MAX_KEY_SIZE (AES_MAX_KEY_SIZE + \ |
| 60 | SHA512_DIGEST_SIZE * 2) |
| 61 | /* max IV is max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */ |
| 62 | #define CAAM_MAX_IV_LENGTH 16 |
| 63 | |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 64 | /* length of descriptors text */ |
| 65 | #define DESC_AEAD_SHARED_TEXT_LEN 4 |
| 66 | #define DESC_AEAD_ENCRYPT_TEXT_LEN 21 |
| 67 | #define DESC_AEAD_DECRYPT_TEXT_LEN 24 |
| 68 | #define DESC_AEAD_GIVENCRYPT_TEXT_LEN 27 |
| 69 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 70 | #ifdef DEBUG |
| 71 | /* for print_hex_dumps with line references */ |
| 72 | #define xstr(s) str(s) |
| 73 | #define str(s) #s |
| 74 | #define debug(format, arg...) printk(format, arg) |
| 75 | #else |
| 76 | #define debug(format, arg...) |
| 77 | #endif |
| 78 | |
| 79 | /* |
| 80 | * per-session context |
| 81 | */ |
| 82 | struct caam_ctx { |
| 83 | struct device *jrdev; |
| 84 | u32 *sh_desc; |
| 85 | dma_addr_t shared_desc_phys; |
| 86 | u32 class1_alg_type; |
| 87 | u32 class2_alg_type; |
| 88 | u32 alg_op; |
| 89 | u8 *key; |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame^] | 90 | dma_addr_t key_dma; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 91 | unsigned int enckeylen; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 92 | unsigned int split_key_len; |
| 93 | unsigned int split_key_pad_len; |
| 94 | unsigned int authsize; |
| 95 | }; |
| 96 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 97 | static int aead_setauthsize(struct crypto_aead *authenc, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 98 | unsigned int authsize) |
| 99 | { |
| 100 | struct caam_ctx *ctx = crypto_aead_ctx(authenc); |
| 101 | |
| 102 | ctx->authsize = authsize; |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | struct split_key_result { |
| 108 | struct completion completion; |
| 109 | int err; |
| 110 | }; |
| 111 | |
| 112 | static void split_key_done(struct device *dev, u32 *desc, u32 err, |
| 113 | void *context) |
| 114 | { |
| 115 | struct split_key_result *res = context; |
| 116 | |
| 117 | #ifdef DEBUG |
| 118 | dev_err(dev, "%s %d: err 0x%x\n", __func__, __LINE__, err); |
| 119 | #endif |
| 120 | if (err) { |
Kim Phillips | de2954d | 2011-05-02 18:29:17 -0500 | [diff] [blame] | 121 | char tmp[CAAM_ERROR_STR_MAX]; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 122 | |
| 123 | dev_err(dev, "%08x: %s\n", err, caam_jr_strstatus(tmp, err)); |
| 124 | } |
| 125 | |
| 126 | res->err = err; |
| 127 | |
| 128 | complete(&res->completion); |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | get a split ipad/opad key |
| 133 | |
| 134 | Split key generation----------------------------------------------- |
| 135 | |
| 136 | [00] 0xb0810008 jobdesc: stidx=1 share=never len=8 |
| 137 | [01] 0x04000014 key: class2->keyreg len=20 |
| 138 | @0xffe01000 |
| 139 | [03] 0x84410014 operation: cls2-op sha1 hmac init dec |
| 140 | [04] 0x24940000 fifold: class2 msgdata-last2 len=0 imm |
| 141 | [05] 0xa4000001 jump: class2 local all ->1 [06] |
| 142 | [06] 0x64260028 fifostr: class2 mdsplit-jdk len=40 |
| 143 | @0xffe04000 |
| 144 | */ |
| 145 | static u32 gen_split_key(struct caam_ctx *ctx, const u8 *key_in, u32 authkeylen) |
| 146 | { |
| 147 | struct device *jrdev = ctx->jrdev; |
| 148 | u32 *desc; |
| 149 | struct split_key_result result; |
| 150 | dma_addr_t dma_addr_in, dma_addr_out; |
| 151 | int ret = 0; |
| 152 | |
| 153 | desc = kmalloc(CAAM_CMD_SZ * 6 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA); |
| 154 | |
| 155 | init_job_desc(desc, 0); |
| 156 | |
| 157 | dma_addr_in = dma_map_single(jrdev, (void *)key_in, authkeylen, |
| 158 | DMA_TO_DEVICE); |
| 159 | if (dma_mapping_error(jrdev, dma_addr_in)) { |
| 160 | dev_err(jrdev, "unable to map key input memory\n"); |
| 161 | kfree(desc); |
| 162 | return -ENOMEM; |
| 163 | } |
| 164 | append_key(desc, dma_addr_in, authkeylen, CLASS_2 | |
| 165 | KEY_DEST_CLASS_REG); |
| 166 | |
| 167 | /* Sets MDHA up into an HMAC-INIT */ |
| 168 | append_operation(desc, ctx->alg_op | OP_ALG_DECRYPT | |
| 169 | OP_ALG_AS_INIT); |
| 170 | |
| 171 | /* |
| 172 | * do a FIFO_LOAD of zero, this will trigger the internal key expansion |
| 173 | into both pads inside MDHA |
| 174 | */ |
| 175 | append_fifo_load_as_imm(desc, NULL, 0, LDST_CLASS_2_CCB | |
| 176 | FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST2); |
| 177 | |
| 178 | /* |
| 179 | * FIFO_STORE with the explicit split-key content store |
| 180 | * (0x26 output type) |
| 181 | */ |
| 182 | dma_addr_out = dma_map_single(jrdev, ctx->key, ctx->split_key_pad_len, |
| 183 | DMA_FROM_DEVICE); |
| 184 | if (dma_mapping_error(jrdev, dma_addr_out)) { |
| 185 | dev_err(jrdev, "unable to map key output memory\n"); |
| 186 | kfree(desc); |
| 187 | return -ENOMEM; |
| 188 | } |
| 189 | append_fifo_store(desc, dma_addr_out, ctx->split_key_len, |
| 190 | LDST_CLASS_2_CCB | FIFOST_TYPE_SPLIT_KEK); |
| 191 | |
| 192 | #ifdef DEBUG |
| 193 | print_hex_dump(KERN_ERR, "ctx.key@"xstr(__LINE__)": ", |
| 194 | DUMP_PREFIX_ADDRESS, 16, 4, key_in, authkeylen, 1); |
| 195 | print_hex_dump(KERN_ERR, "jobdesc@"xstr(__LINE__)": ", |
| 196 | DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1); |
| 197 | #endif |
| 198 | |
| 199 | result.err = 0; |
| 200 | init_completion(&result.completion); |
| 201 | |
| 202 | ret = caam_jr_enqueue(jrdev, desc, split_key_done, &result); |
| 203 | if (!ret) { |
| 204 | /* in progress */ |
| 205 | wait_for_completion_interruptible(&result.completion); |
| 206 | ret = result.err; |
| 207 | #ifdef DEBUG |
| 208 | print_hex_dump(KERN_ERR, "ctx.key@"xstr(__LINE__)": ", |
| 209 | DUMP_PREFIX_ADDRESS, 16, 4, ctx->key, |
| 210 | ctx->split_key_pad_len, 1); |
| 211 | #endif |
| 212 | } |
| 213 | |
| 214 | dma_unmap_single(jrdev, dma_addr_out, ctx->split_key_pad_len, |
| 215 | DMA_FROM_DEVICE); |
| 216 | dma_unmap_single(jrdev, dma_addr_in, authkeylen, DMA_TO_DEVICE); |
| 217 | |
| 218 | kfree(desc); |
| 219 | |
| 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | static int build_sh_desc_ipsec(struct caam_ctx *ctx) |
| 224 | { |
| 225 | struct device *jrdev = ctx->jrdev; |
| 226 | u32 *sh_desc; |
| 227 | u32 *jump_cmd; |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 228 | bool keys_fit_inline = 0; |
| 229 | |
| 230 | /* |
| 231 | * largest Job Descriptor and its Shared Descriptor |
| 232 | * must both fit into the 64-word Descriptor h/w Buffer |
| 233 | */ |
| 234 | if ((DESC_AEAD_GIVENCRYPT_TEXT_LEN + |
| 235 | DESC_AEAD_SHARED_TEXT_LEN) * CAAM_CMD_SZ + |
| 236 | ctx->split_key_pad_len + ctx->enckeylen <= CAAM_DESC_BYTES_MAX) |
| 237 | keys_fit_inline = 1; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 238 | |
| 239 | /* build shared descriptor for this session */ |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 240 | sh_desc = kmalloc(CAAM_CMD_SZ * DESC_AEAD_SHARED_TEXT_LEN + |
Kim Phillips | a18b989 | 2011-05-26 13:30:34 +1000 | [diff] [blame] | 241 | (keys_fit_inline ? |
| 242 | ctx->split_key_pad_len + ctx->enckeylen : |
| 243 | CAAM_PTR_SZ * 2), GFP_DMA | GFP_KERNEL); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 244 | if (!sh_desc) { |
| 245 | dev_err(jrdev, "could not allocate shared descriptor\n"); |
| 246 | return -ENOMEM; |
| 247 | } |
| 248 | |
| 249 | init_sh_desc(sh_desc, HDR_SAVECTX | HDR_SHARE_SERIAL); |
| 250 | |
| 251 | jump_cmd = append_jump(sh_desc, CLASS_BOTH | JUMP_TEST_ALL | |
| 252 | JUMP_COND_SHRD | JUMP_COND_SELF); |
| 253 | |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 254 | /* |
| 255 | * process keys, starting with class 2/authentication. |
| 256 | */ |
| 257 | if (keys_fit_inline) { |
| 258 | append_key_as_imm(sh_desc, ctx->key, ctx->split_key_pad_len, |
| 259 | ctx->split_key_len, |
| 260 | CLASS_2 | KEY_DEST_MDHA_SPLIT | KEY_ENC); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 261 | |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 262 | append_key_as_imm(sh_desc, (void *)ctx->key + |
| 263 | ctx->split_key_pad_len, ctx->enckeylen, |
| 264 | ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG); |
| 265 | } else { |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame^] | 266 | append_key(sh_desc, ctx->key_dma, ctx->split_key_len, CLASS_2 | |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 267 | KEY_DEST_MDHA_SPLIT | KEY_ENC); |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame^] | 268 | append_key(sh_desc, ctx->key_dma + ctx->split_key_pad_len, |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 269 | ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG); |
| 270 | } |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 271 | |
| 272 | /* update jump cmd now that we are at the jump target */ |
| 273 | set_jump_tgt_here(sh_desc, jump_cmd); |
| 274 | |
| 275 | ctx->shared_desc_phys = dma_map_single(jrdev, sh_desc, |
| 276 | desc_bytes(sh_desc), |
| 277 | DMA_TO_DEVICE); |
| 278 | if (dma_mapping_error(jrdev, ctx->shared_desc_phys)) { |
| 279 | dev_err(jrdev, "unable to map shared descriptor\n"); |
| 280 | kfree(sh_desc); |
| 281 | return -ENOMEM; |
| 282 | } |
| 283 | |
| 284 | ctx->sh_desc = sh_desc; |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 289 | static int aead_setkey(struct crypto_aead *aead, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 290 | const u8 *key, unsigned int keylen) |
| 291 | { |
| 292 | /* Sizes for MDHA pads (*not* keys): MD5, SHA1, 224, 256, 384, 512 */ |
| 293 | static const u8 mdpadlen[] = { 16, 20, 32, 32, 64, 64 }; |
| 294 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
| 295 | struct device *jrdev = ctx->jrdev; |
| 296 | struct rtattr *rta = (void *)key; |
| 297 | struct crypto_authenc_key_param *param; |
| 298 | unsigned int authkeylen; |
| 299 | unsigned int enckeylen; |
| 300 | int ret = 0; |
| 301 | |
| 302 | param = RTA_DATA(rta); |
| 303 | enckeylen = be32_to_cpu(param->enckeylen); |
| 304 | |
| 305 | key += RTA_ALIGN(rta->rta_len); |
| 306 | keylen -= RTA_ALIGN(rta->rta_len); |
| 307 | |
| 308 | if (keylen < enckeylen) |
| 309 | goto badkey; |
| 310 | |
| 311 | authkeylen = keylen - enckeylen; |
| 312 | |
| 313 | if (keylen > CAAM_MAX_KEY_SIZE) |
| 314 | goto badkey; |
| 315 | |
| 316 | /* Pick class 2 key length from algorithm submask */ |
| 317 | ctx->split_key_len = mdpadlen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >> |
| 318 | OP_ALG_ALGSEL_SHIFT] * 2; |
| 319 | ctx->split_key_pad_len = ALIGN(ctx->split_key_len, 16); |
| 320 | |
| 321 | #ifdef DEBUG |
| 322 | printk(KERN_ERR "keylen %d enckeylen %d authkeylen %d\n", |
| 323 | keylen, enckeylen, authkeylen); |
| 324 | printk(KERN_ERR "split_key_len %d split_key_pad_len %d\n", |
| 325 | ctx->split_key_len, ctx->split_key_pad_len); |
| 326 | print_hex_dump(KERN_ERR, "key in @"xstr(__LINE__)": ", |
| 327 | DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1); |
| 328 | #endif |
| 329 | ctx->key = kmalloc(ctx->split_key_pad_len + enckeylen, |
| 330 | GFP_KERNEL | GFP_DMA); |
| 331 | if (!ctx->key) { |
| 332 | dev_err(jrdev, "could not allocate key output memory\n"); |
| 333 | return -ENOMEM; |
| 334 | } |
| 335 | |
| 336 | ret = gen_split_key(ctx, key, authkeylen); |
| 337 | if (ret) { |
| 338 | kfree(ctx->key); |
| 339 | goto badkey; |
| 340 | } |
| 341 | |
| 342 | /* postpend encryption key to auth split key */ |
| 343 | memcpy(ctx->key + ctx->split_key_pad_len, key + authkeylen, enckeylen); |
| 344 | |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame^] | 345 | ctx->key_dma = dma_map_single(jrdev, ctx->key, ctx->split_key_pad_len + |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 346 | enckeylen, DMA_TO_DEVICE); |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame^] | 347 | if (dma_mapping_error(jrdev, ctx->key_dma)) { |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 348 | dev_err(jrdev, "unable to map key i/o memory\n"); |
| 349 | kfree(ctx->key); |
| 350 | return -ENOMEM; |
| 351 | } |
| 352 | #ifdef DEBUG |
| 353 | print_hex_dump(KERN_ERR, "ctx.key@"xstr(__LINE__)": ", |
| 354 | DUMP_PREFIX_ADDRESS, 16, 4, ctx->key, |
| 355 | ctx->split_key_pad_len + enckeylen, 1); |
| 356 | #endif |
| 357 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 358 | ctx->enckeylen = enckeylen; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 359 | |
| 360 | ret = build_sh_desc_ipsec(ctx); |
| 361 | if (ret) { |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame^] | 362 | dma_unmap_single(jrdev, ctx->key_dma, ctx->split_key_pad_len + |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 363 | enckeylen, DMA_TO_DEVICE); |
| 364 | kfree(ctx->key); |
| 365 | } |
| 366 | |
| 367 | return ret; |
| 368 | badkey: |
| 369 | crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 370 | return -EINVAL; |
| 371 | } |
| 372 | |
| 373 | struct link_tbl_entry { |
| 374 | u64 ptr; |
| 375 | u32 len; |
| 376 | u8 reserved; |
| 377 | u8 buf_pool_id; |
| 378 | u16 offset; |
| 379 | }; |
| 380 | |
| 381 | /* |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 382 | * aead_edesc - s/w-extended ipsec_esp descriptor |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 383 | * @src_nents: number of segments in input scatterlist |
| 384 | * @dst_nents: number of segments in output scatterlist |
| 385 | * @assoc_nents: number of segments in associated data (SPI+Seq) scatterlist |
| 386 | * @desc: h/w descriptor (variable length; must not exceed MAX_CAAM_DESCSIZE) |
| 387 | * @link_tbl_bytes: length of dma mapped link_tbl space |
| 388 | * @link_tbl_dma: bus physical mapped address of h/w link table |
| 389 | * @hw_desc: the h/w job descriptor followed by any referenced link tables |
| 390 | */ |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 391 | struct aead_edesc { |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 392 | int assoc_nents; |
| 393 | int src_nents; |
| 394 | int dst_nents; |
| 395 | int link_tbl_bytes; |
| 396 | dma_addr_t link_tbl_dma; |
| 397 | struct link_tbl_entry *link_tbl; |
| 398 | u32 hw_desc[0]; |
| 399 | }; |
| 400 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 401 | static void aead_unmap(struct device *dev, |
| 402 | struct aead_edesc *edesc, |
| 403 | struct aead_request *req) |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 404 | { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 405 | dma_unmap_sg(dev, req->assoc, edesc->assoc_nents, DMA_TO_DEVICE); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 406 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 407 | if (unlikely(req->dst != req->src)) { |
| 408 | dma_unmap_sg(dev, req->src, edesc->src_nents, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 409 | DMA_TO_DEVICE); |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 410 | dma_unmap_sg(dev, req->dst, edesc->dst_nents, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 411 | DMA_FROM_DEVICE); |
| 412 | } else { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 413 | dma_unmap_sg(dev, req->src, edesc->src_nents, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 414 | DMA_BIDIRECTIONAL); |
| 415 | } |
| 416 | |
| 417 | if (edesc->link_tbl_bytes) |
| 418 | dma_unmap_single(dev, edesc->link_tbl_dma, |
| 419 | edesc->link_tbl_bytes, |
| 420 | DMA_TO_DEVICE); |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * ipsec_esp descriptor callbacks |
| 425 | */ |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 426 | static void aead_encrypt_done(struct device *jrdev, u32 *desc, u32 err, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 427 | void *context) |
| 428 | { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 429 | struct aead_request *req = context; |
| 430 | struct aead_edesc *edesc; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 431 | #ifdef DEBUG |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 432 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 433 | int ivsize = crypto_aead_ivsize(aead); |
| 434 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
| 435 | |
| 436 | dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err); |
| 437 | #endif |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 438 | edesc = (struct aead_edesc *)((char *)desc - |
| 439 | offsetof(struct aead_edesc, hw_desc)); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 440 | |
| 441 | if (err) { |
Kim Phillips | de2954d | 2011-05-02 18:29:17 -0500 | [diff] [blame] | 442 | char tmp[CAAM_ERROR_STR_MAX]; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 443 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 444 | dev_err(jrdev, "%08x: %s\n", err, caam_jr_strstatus(tmp, err)); |
| 445 | } |
| 446 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 447 | aead_unmap(jrdev, edesc, req); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 448 | |
| 449 | #ifdef DEBUG |
| 450 | print_hex_dump(KERN_ERR, "assoc @"xstr(__LINE__)": ", |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 451 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->assoc), |
| 452 | req->assoclen , 1); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 453 | print_hex_dump(KERN_ERR, "dstiv @"xstr(__LINE__)": ", |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 454 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src) - ivsize, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 455 | edesc->src_nents ? 100 : ivsize, 1); |
| 456 | print_hex_dump(KERN_ERR, "dst @"xstr(__LINE__)": ", |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 457 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src), |
| 458 | edesc->src_nents ? 100 : req->cryptlen + |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 459 | ctx->authsize + 4, 1); |
| 460 | #endif |
| 461 | |
| 462 | kfree(edesc); |
| 463 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 464 | aead_request_complete(req, err); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 465 | } |
| 466 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 467 | static void aead_decrypt_done(struct device *jrdev, u32 *desc, u32 err, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 468 | void *context) |
| 469 | { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 470 | struct aead_request *req = context; |
| 471 | struct aead_edesc *edesc; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 472 | #ifdef DEBUG |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 473 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 474 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
| 475 | |
| 476 | dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err); |
| 477 | #endif |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 478 | edesc = (struct aead_edesc *)((char *)desc - |
| 479 | offsetof(struct aead_edesc, hw_desc)); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 480 | |
| 481 | if (err) { |
Kim Phillips | de2954d | 2011-05-02 18:29:17 -0500 | [diff] [blame] | 482 | char tmp[CAAM_ERROR_STR_MAX]; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 483 | |
| 484 | dev_err(jrdev, "%08x: %s\n", err, caam_jr_strstatus(tmp, err)); |
| 485 | } |
| 486 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 487 | aead_unmap(jrdev, edesc, req); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 488 | |
| 489 | /* |
| 490 | * verify hw auth check passed else return -EBADMSG |
| 491 | */ |
| 492 | if ((err & JRSTA_CCBERR_ERRID_MASK) == JRSTA_CCBERR_ERRID_ICVCHK) |
| 493 | err = -EBADMSG; |
| 494 | |
| 495 | #ifdef DEBUG |
| 496 | print_hex_dump(KERN_ERR, "iphdrout@"xstr(__LINE__)": ", |
| 497 | DUMP_PREFIX_ADDRESS, 16, 4, |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 498 | ((char *)sg_virt(req->assoc) - sizeof(struct iphdr)), |
| 499 | sizeof(struct iphdr) + req->assoclen + |
| 500 | ((req->cryptlen > 1500) ? 1500 : req->cryptlen) + |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 501 | ctx->authsize + 36, 1); |
| 502 | if (!err && edesc->link_tbl_bytes) { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 503 | struct scatterlist *sg = sg_last(req->src, edesc->src_nents); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 504 | print_hex_dump(KERN_ERR, "sglastout@"xstr(__LINE__)": ", |
| 505 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(sg), |
| 506 | sg->length + ctx->authsize + 16, 1); |
| 507 | } |
| 508 | #endif |
| 509 | kfree(edesc); |
| 510 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 511 | aead_request_complete(req, err); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /* |
| 515 | * convert scatterlist to h/w link table format |
| 516 | * scatterlist must have been previously dma mapped |
| 517 | */ |
| 518 | static void sg_to_link_tbl(struct scatterlist *sg, int sg_count, |
| 519 | struct link_tbl_entry *link_tbl_ptr, u32 offset) |
| 520 | { |
| 521 | while (sg_count) { |
| 522 | link_tbl_ptr->ptr = sg_dma_address(sg); |
| 523 | link_tbl_ptr->len = sg_dma_len(sg); |
| 524 | link_tbl_ptr->reserved = 0; |
| 525 | link_tbl_ptr->buf_pool_id = 0; |
| 526 | link_tbl_ptr->offset = offset; |
| 527 | link_tbl_ptr++; |
| 528 | sg = sg_next(sg); |
| 529 | sg_count--; |
| 530 | } |
| 531 | |
| 532 | /* set Final bit (marks end of link table) */ |
| 533 | link_tbl_ptr--; |
| 534 | link_tbl_ptr->len |= 0x40000000; |
| 535 | } |
| 536 | |
| 537 | /* |
| 538 | * fill in and submit ipsec_esp job descriptor |
| 539 | */ |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 540 | static int init_aead_job(struct aead_edesc *edesc, struct aead_request *req, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 541 | u32 encrypt, |
| 542 | void (*callback) (struct device *dev, u32 *desc, |
| 543 | u32 err, void *context)) |
| 544 | { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 545 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 546 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
| 547 | struct device *jrdev = ctx->jrdev; |
| 548 | u32 *desc = edesc->hw_desc, options; |
| 549 | int ret, sg_count, assoc_sg_count; |
| 550 | int ivsize = crypto_aead_ivsize(aead); |
| 551 | int authsize = ctx->authsize; |
| 552 | dma_addr_t ptr, dst_dma, src_dma; |
| 553 | #ifdef DEBUG |
| 554 | u32 *sh_desc = ctx->sh_desc; |
| 555 | |
| 556 | debug("assoclen %d cryptlen %d authsize %d\n", |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 557 | req->assoclen, req->cryptlen, authsize); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 558 | print_hex_dump(KERN_ERR, "assoc @"xstr(__LINE__)": ", |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 559 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->assoc), |
| 560 | req->assoclen , 1); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 561 | print_hex_dump(KERN_ERR, "presciv@"xstr(__LINE__)": ", |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 562 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src) - ivsize, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 563 | edesc->src_nents ? 100 : ivsize, 1); |
| 564 | print_hex_dump(KERN_ERR, "src @"xstr(__LINE__)": ", |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 565 | DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src), |
| 566 | edesc->src_nents ? 100 : req->cryptlen + authsize, 1); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 567 | print_hex_dump(KERN_ERR, "shrdesc@"xstr(__LINE__)": ", |
| 568 | DUMP_PREFIX_ADDRESS, 16, 4, sh_desc, |
| 569 | desc_bytes(sh_desc), 1); |
| 570 | #endif |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 571 | assoc_sg_count = dma_map_sg(jrdev, req->assoc, edesc->assoc_nents ?: 1, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 572 | DMA_TO_DEVICE); |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 573 | if (req->src == req->dst) |
| 574 | sg_count = dma_map_sg(jrdev, req->src, edesc->src_nents ? : 1, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 575 | DMA_BIDIRECTIONAL); |
| 576 | else |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 577 | sg_count = dma_map_sg(jrdev, req->src, edesc->src_nents ? : 1, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 578 | DMA_TO_DEVICE); |
| 579 | |
| 580 | /* start auth operation */ |
| 581 | append_operation(desc, ctx->class2_alg_type | OP_ALG_AS_INITFINAL | |
| 582 | (encrypt ? : OP_ALG_ICV_ON)); |
| 583 | |
| 584 | /* Load FIFO with data for Class 2 CHA */ |
| 585 | options = FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG; |
| 586 | if (!edesc->assoc_nents) { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 587 | ptr = sg_dma_address(req->assoc); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 588 | } else { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 589 | sg_to_link_tbl(req->assoc, edesc->assoc_nents, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 590 | edesc->link_tbl, 0); |
| 591 | ptr = edesc->link_tbl_dma; |
| 592 | options |= LDST_SGF; |
| 593 | } |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 594 | append_fifo_load(desc, ptr, req->assoclen, options); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 595 | |
| 596 | /* copy iv from cipher/class1 input context to class2 infifo */ |
| 597 | append_move(desc, MOVE_SRC_CLASS1CTX | MOVE_DEST_CLASS2INFIFO | ivsize); |
| 598 | |
Kim Phillips | ddbb808 | 2011-05-14 22:08:02 -0500 | [diff] [blame] | 599 | if (!encrypt) { |
| 600 | u32 *jump_cmd, *uncond_jump_cmd; |
| 601 | |
| 602 | /* JUMP if shared */ |
| 603 | jump_cmd = append_jump(desc, JUMP_TEST_ALL | JUMP_COND_SHRD); |
| 604 | |
| 605 | /* start class 1 (cipher) operation, non-shared version */ |
| 606 | append_operation(desc, ctx->class1_alg_type | |
| 607 | OP_ALG_AS_INITFINAL); |
| 608 | |
| 609 | uncond_jump_cmd = append_jump(desc, 0); |
| 610 | |
| 611 | set_jump_tgt_here(desc, jump_cmd); |
| 612 | |
| 613 | /* start class 1 (cipher) operation, shared version */ |
| 614 | append_operation(desc, ctx->class1_alg_type | |
| 615 | OP_ALG_AS_INITFINAL | OP_ALG_AAI_DK); |
| 616 | set_jump_tgt_here(desc, uncond_jump_cmd); |
| 617 | } else |
| 618 | append_operation(desc, ctx->class1_alg_type | |
| 619 | OP_ALG_AS_INITFINAL | encrypt); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 620 | |
| 621 | /* load payload & instruct to class2 to snoop class 1 if encrypting */ |
| 622 | options = 0; |
| 623 | if (!edesc->src_nents) { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 624 | src_dma = sg_dma_address(req->src); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 625 | } else { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 626 | sg_to_link_tbl(req->src, edesc->src_nents, edesc->link_tbl + |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 627 | edesc->assoc_nents, 0); |
| 628 | src_dma = edesc->link_tbl_dma + edesc->assoc_nents * |
| 629 | sizeof(struct link_tbl_entry); |
| 630 | options |= LDST_SGF; |
| 631 | } |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 632 | append_seq_in_ptr(desc, src_dma, req->cryptlen + authsize, options); |
| 633 | append_seq_fifo_load(desc, req->cryptlen, FIFOLD_CLASS_BOTH | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 634 | FIFOLD_TYPE_LASTBOTH | |
| 635 | (encrypt ? FIFOLD_TYPE_MSG1OUT2 |
| 636 | : FIFOLD_TYPE_MSG)); |
| 637 | |
| 638 | /* specify destination */ |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 639 | if (req->src == req->dst) { |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 640 | dst_dma = src_dma; |
| 641 | } else { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 642 | sg_count = dma_map_sg(jrdev, req->dst, edesc->dst_nents ? : 1, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 643 | DMA_FROM_DEVICE); |
| 644 | if (!edesc->dst_nents) { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 645 | dst_dma = sg_dma_address(req->dst); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 646 | options = 0; |
| 647 | } else { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 648 | sg_to_link_tbl(req->dst, edesc->dst_nents, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 649 | edesc->link_tbl + edesc->assoc_nents + |
| 650 | edesc->src_nents, 0); |
| 651 | dst_dma = edesc->link_tbl_dma + (edesc->assoc_nents + |
| 652 | edesc->src_nents) * |
| 653 | sizeof(struct link_tbl_entry); |
| 654 | options = LDST_SGF; |
| 655 | } |
| 656 | } |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 657 | append_seq_out_ptr(desc, dst_dma, req->cryptlen + authsize, options); |
| 658 | append_seq_fifo_store(desc, req->cryptlen, FIFOST_TYPE_MESSAGE_DATA); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 659 | |
| 660 | /* ICV */ |
| 661 | if (encrypt) |
| 662 | append_seq_store(desc, authsize, LDST_CLASS_2_CCB | |
| 663 | LDST_SRCDST_BYTE_CONTEXT); |
| 664 | else |
| 665 | append_seq_fifo_load(desc, authsize, FIFOLD_CLASS_CLASS2 | |
| 666 | FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_ICV); |
| 667 | |
| 668 | #ifdef DEBUG |
| 669 | debug("job_desc_len %d\n", desc_len(desc)); |
| 670 | print_hex_dump(KERN_ERR, "jobdesc@"xstr(__LINE__)": ", |
| 671 | DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc) , 1); |
| 672 | print_hex_dump(KERN_ERR, "jdlinkt@"xstr(__LINE__)": ", |
| 673 | DUMP_PREFIX_ADDRESS, 16, 4, edesc->link_tbl, |
| 674 | edesc->link_tbl_bytes, 1); |
| 675 | #endif |
| 676 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 677 | ret = caam_jr_enqueue(jrdev, desc, callback, req); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 678 | if (!ret) |
| 679 | ret = -EINPROGRESS; |
| 680 | else { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 681 | aead_unmap(jrdev, edesc, req); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 682 | kfree(edesc); |
| 683 | } |
| 684 | |
| 685 | return ret; |
| 686 | } |
| 687 | |
| 688 | /* |
| 689 | * derive number of elements in scatterlist |
| 690 | */ |
| 691 | static int sg_count(struct scatterlist *sg_list, int nbytes, int *chained) |
| 692 | { |
| 693 | struct scatterlist *sg = sg_list; |
| 694 | int sg_nents = 0; |
| 695 | |
| 696 | *chained = 0; |
| 697 | while (nbytes > 0) { |
| 698 | sg_nents++; |
| 699 | nbytes -= sg->length; |
| 700 | if (!sg_is_last(sg) && (sg + 1)->length == 0) |
| 701 | *chained = 1; |
| 702 | sg = scatterwalk_sg_next(sg); |
| 703 | } |
| 704 | |
| 705 | return sg_nents; |
| 706 | } |
| 707 | |
| 708 | /* |
| 709 | * allocate and map the ipsec_esp extended descriptor |
| 710 | */ |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 711 | static struct aead_edesc *aead_edesc_alloc(struct aead_request *req, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 712 | int desc_bytes) |
| 713 | { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 714 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 715 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
| 716 | struct device *jrdev = ctx->jrdev; |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 717 | gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 718 | GFP_ATOMIC; |
| 719 | int assoc_nents, src_nents, dst_nents = 0, chained, link_tbl_bytes; |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 720 | struct aead_edesc *edesc; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 721 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 722 | assoc_nents = sg_count(req->assoc, req->assoclen, &chained); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 723 | BUG_ON(chained); |
| 724 | if (likely(assoc_nents == 1)) |
| 725 | assoc_nents = 0; |
| 726 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 727 | src_nents = sg_count(req->src, req->cryptlen + ctx->authsize, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 728 | &chained); |
| 729 | BUG_ON(chained); |
| 730 | if (src_nents == 1) |
| 731 | src_nents = 0; |
| 732 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 733 | if (unlikely(req->dst != req->src)) { |
| 734 | dst_nents = sg_count(req->dst, req->cryptlen + ctx->authsize, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 735 | &chained); |
| 736 | BUG_ON(chained); |
| 737 | if (dst_nents == 1) |
| 738 | dst_nents = 0; |
| 739 | } |
| 740 | |
| 741 | link_tbl_bytes = (assoc_nents + src_nents + dst_nents) * |
| 742 | sizeof(struct link_tbl_entry); |
| 743 | debug("link_tbl_bytes %d\n", link_tbl_bytes); |
| 744 | |
| 745 | /* allocate space for base edesc and hw desc commands, link tables */ |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 746 | edesc = kmalloc(sizeof(struct aead_edesc) + desc_bytes + |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 747 | link_tbl_bytes, GFP_DMA | flags); |
| 748 | if (!edesc) { |
| 749 | dev_err(jrdev, "could not allocate extended descriptor\n"); |
| 750 | return ERR_PTR(-ENOMEM); |
| 751 | } |
| 752 | |
| 753 | edesc->assoc_nents = assoc_nents; |
| 754 | edesc->src_nents = src_nents; |
| 755 | edesc->dst_nents = dst_nents; |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 756 | edesc->link_tbl = (void *)edesc + sizeof(struct aead_edesc) + |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 757 | desc_bytes; |
| 758 | edesc->link_tbl_dma = dma_map_single(jrdev, edesc->link_tbl, |
| 759 | link_tbl_bytes, DMA_TO_DEVICE); |
| 760 | edesc->link_tbl_bytes = link_tbl_bytes; |
| 761 | |
| 762 | return edesc; |
| 763 | } |
| 764 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 765 | static int aead_encrypt(struct aead_request *req) |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 766 | { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 767 | struct aead_edesc *edesc; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 768 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 769 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
| 770 | struct device *jrdev = ctx->jrdev; |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 771 | int ivsize = crypto_aead_ivsize(aead); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 772 | u32 *desc; |
| 773 | dma_addr_t iv_dma; |
| 774 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 775 | /* allocate extended descriptor */ |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 776 | edesc = aead_edesc_alloc(req, DESC_AEAD_ENCRYPT_TEXT_LEN * |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 777 | CAAM_CMD_SZ); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 778 | if (IS_ERR(edesc)) |
| 779 | return PTR_ERR(edesc); |
| 780 | |
| 781 | desc = edesc->hw_desc; |
| 782 | |
| 783 | /* insert shared descriptor pointer */ |
| 784 | init_job_desc_shared(desc, ctx->shared_desc_phys, |
| 785 | desc_len(ctx->sh_desc), HDR_SHARE_DEFER); |
| 786 | |
| 787 | iv_dma = dma_map_single(jrdev, req->iv, ivsize, DMA_TO_DEVICE); |
| 788 | /* check dma error */ |
| 789 | |
| 790 | append_load(desc, iv_dma, ivsize, |
| 791 | LDST_CLASS_1_CCB | LDST_SRCDST_BYTE_CONTEXT); |
| 792 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 793 | return init_aead_job(edesc, req, OP_ALG_ENCRYPT, aead_encrypt_done); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 794 | } |
| 795 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 796 | static int aead_decrypt(struct aead_request *req) |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 797 | { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 798 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 799 | int ivsize = crypto_aead_ivsize(aead); |
| 800 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
| 801 | struct device *jrdev = ctx->jrdev; |
| 802 | struct aead_edesc *edesc; |
| 803 | u32 *desc; |
| 804 | dma_addr_t iv_dma; |
| 805 | |
| 806 | req->cryptlen -= ctx->authsize; |
| 807 | |
| 808 | /* allocate extended descriptor */ |
| 809 | edesc = aead_edesc_alloc(req, DESC_AEAD_DECRYPT_TEXT_LEN * |
| 810 | CAAM_CMD_SZ); |
| 811 | if (IS_ERR(edesc)) |
| 812 | return PTR_ERR(edesc); |
| 813 | |
| 814 | desc = edesc->hw_desc; |
| 815 | |
| 816 | /* insert shared descriptor pointer */ |
| 817 | init_job_desc_shared(desc, ctx->shared_desc_phys, |
| 818 | desc_len(ctx->sh_desc), HDR_SHARE_DEFER); |
| 819 | |
| 820 | iv_dma = dma_map_single(jrdev, req->iv, ivsize, DMA_TO_DEVICE); |
| 821 | /* check dma error */ |
| 822 | |
| 823 | append_load(desc, iv_dma, ivsize, |
| 824 | LDST_CLASS_1_CCB | LDST_SRCDST_BYTE_CONTEXT); |
| 825 | |
| 826 | return init_aead_job(edesc, req, !OP_ALG_ENCRYPT, aead_decrypt_done); |
| 827 | } |
| 828 | |
| 829 | static int aead_givencrypt(struct aead_givcrypt_request *areq) |
| 830 | { |
| 831 | struct aead_request *req = &areq->areq; |
| 832 | struct aead_edesc *edesc; |
| 833 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 834 | struct caam_ctx *ctx = crypto_aead_ctx(aead); |
| 835 | struct device *jrdev = ctx->jrdev; |
| 836 | int ivsize = crypto_aead_ivsize(aead); |
| 837 | dma_addr_t iv_dma; |
| 838 | u32 *desc; |
| 839 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 840 | iv_dma = dma_map_single(jrdev, areq->giv, ivsize, DMA_FROM_DEVICE); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 841 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 842 | debug("%s: giv %p\n", __func__, areq->giv); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 843 | |
| 844 | /* allocate extended descriptor */ |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 845 | edesc = aead_edesc_alloc(req, DESC_AEAD_GIVENCRYPT_TEXT_LEN * |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 846 | CAAM_CMD_SZ); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 847 | if (IS_ERR(edesc)) |
| 848 | return PTR_ERR(edesc); |
| 849 | |
| 850 | desc = edesc->hw_desc; |
| 851 | |
| 852 | /* insert shared descriptor pointer */ |
| 853 | init_job_desc_shared(desc, ctx->shared_desc_phys, |
| 854 | desc_len(ctx->sh_desc), HDR_SHARE_DEFER); |
| 855 | |
| 856 | /* |
| 857 | * LOAD IMM Info FIFO |
| 858 | * to DECO, Last, Padding, Random, Message, 16 bytes |
| 859 | */ |
| 860 | append_load_imm_u32(desc, NFIFOENTRY_DEST_DECO | NFIFOENTRY_LC1 | |
| 861 | NFIFOENTRY_STYPE_PAD | NFIFOENTRY_DTYPE_MSG | |
| 862 | NFIFOENTRY_PTYPE_RND | ivsize, |
| 863 | LDST_SRCDST_WORD_INFO_FIFO); |
| 864 | |
| 865 | /* |
| 866 | * disable info fifo entries since the above serves as the entry |
| 867 | * this way, the MOVE command won't generate an entry. |
| 868 | * Note that this isn't required in more recent versions of |
| 869 | * SEC as a MOVE that doesn't do info FIFO entries is available. |
| 870 | */ |
| 871 | append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO); |
| 872 | |
| 873 | /* MOVE DECO Alignment -> C1 Context 16 bytes */ |
Kim Phillips | d37d36e | 2011-04-11 19:15:24 -0500 | [diff] [blame] | 874 | append_move(desc, MOVE_SRC_INFIFO | MOVE_DEST_CLASS1CTX | ivsize); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 875 | |
| 876 | /* re-enable info fifo entries */ |
| 877 | append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO); |
| 878 | |
| 879 | /* MOVE C1 Context -> OFIFO 16 bytes */ |
Kim Phillips | d37d36e | 2011-04-11 19:15:24 -0500 | [diff] [blame] | 880 | append_move(desc, MOVE_SRC_CLASS1CTX | MOVE_DEST_OUTFIFO | ivsize); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 881 | |
| 882 | append_fifo_store(desc, iv_dma, ivsize, FIFOST_TYPE_MESSAGE_DATA); |
| 883 | |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 884 | return init_aead_job(edesc, req, OP_ALG_ENCRYPT, aead_encrypt_done); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 885 | } |
| 886 | |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame^] | 887 | #define template_aead template_u.aead |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 888 | struct caam_alg_template { |
| 889 | char name[CRYPTO_MAX_ALG_NAME]; |
| 890 | char driver_name[CRYPTO_MAX_ALG_NAME]; |
| 891 | unsigned int blocksize; |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame^] | 892 | u32 type; |
| 893 | union { |
| 894 | struct ablkcipher_alg ablkcipher; |
| 895 | struct aead_alg aead; |
| 896 | struct blkcipher_alg blkcipher; |
| 897 | struct cipher_alg cipher; |
| 898 | struct compress_alg compress; |
| 899 | struct rng_alg rng; |
| 900 | } template_u; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 901 | u32 class1_alg_type; |
| 902 | u32 class2_alg_type; |
| 903 | u32 alg_op; |
| 904 | }; |
| 905 | |
| 906 | static struct caam_alg_template driver_algs[] = { |
| 907 | /* single-pass ipsec_esp descriptor */ |
| 908 | { |
| 909 | .name = "authenc(hmac(sha1),cbc(aes))", |
| 910 | .driver_name = "authenc-hmac-sha1-cbc-aes-caam", |
| 911 | .blocksize = AES_BLOCK_SIZE, |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame^] | 912 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 913 | .template_aead = { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 914 | .setkey = aead_setkey, |
| 915 | .setauthsize = aead_setauthsize, |
| 916 | .encrypt = aead_encrypt, |
| 917 | .decrypt = aead_decrypt, |
| 918 | .givencrypt = aead_givencrypt, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 919 | .geniv = "<built-in>", |
| 920 | .ivsize = AES_BLOCK_SIZE, |
| 921 | .maxauthsize = SHA1_DIGEST_SIZE, |
| 922 | }, |
| 923 | .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC, |
| 924 | .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP, |
| 925 | .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC, |
| 926 | }, |
| 927 | { |
| 928 | .name = "authenc(hmac(sha256),cbc(aes))", |
| 929 | .driver_name = "authenc-hmac-sha256-cbc-aes-caam", |
| 930 | .blocksize = AES_BLOCK_SIZE, |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame^] | 931 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 932 | .template_aead = { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 933 | .setkey = aead_setkey, |
| 934 | .setauthsize = aead_setauthsize, |
| 935 | .encrypt = aead_encrypt, |
| 936 | .decrypt = aead_decrypt, |
| 937 | .givencrypt = aead_givencrypt, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 938 | .geniv = "<built-in>", |
| 939 | .ivsize = AES_BLOCK_SIZE, |
| 940 | .maxauthsize = SHA256_DIGEST_SIZE, |
| 941 | }, |
| 942 | .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC, |
| 943 | .class2_alg_type = OP_ALG_ALGSEL_SHA256 | |
| 944 | OP_ALG_AAI_HMAC_PRECOMP, |
| 945 | .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC, |
| 946 | }, |
| 947 | { |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 948 | .name = "authenc(hmac(sha512),cbc(aes))", |
| 949 | .driver_name = "authenc-hmac-sha512-cbc-aes-caam", |
| 950 | .blocksize = AES_BLOCK_SIZE, |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame^] | 951 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 952 | .template_aead = { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 953 | .setkey = aead_setkey, |
| 954 | .setauthsize = aead_setauthsize, |
| 955 | .encrypt = aead_encrypt, |
| 956 | .decrypt = aead_decrypt, |
| 957 | .givencrypt = aead_givencrypt, |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 958 | .geniv = "<built-in>", |
| 959 | .ivsize = AES_BLOCK_SIZE, |
| 960 | .maxauthsize = SHA512_DIGEST_SIZE, |
| 961 | }, |
| 962 | .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC, |
| 963 | .class2_alg_type = OP_ALG_ALGSEL_SHA512 | |
| 964 | OP_ALG_AAI_HMAC_PRECOMP, |
| 965 | .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC, |
| 966 | }, |
| 967 | { |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 968 | .name = "authenc(hmac(sha1),cbc(des3_ede))", |
| 969 | .driver_name = "authenc-hmac-sha1-cbc-des3_ede-caam", |
| 970 | .blocksize = DES3_EDE_BLOCK_SIZE, |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame^] | 971 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 972 | .template_aead = { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 973 | .setkey = aead_setkey, |
| 974 | .setauthsize = aead_setauthsize, |
| 975 | .encrypt = aead_encrypt, |
| 976 | .decrypt = aead_decrypt, |
| 977 | .givencrypt = aead_givencrypt, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 978 | .geniv = "<built-in>", |
| 979 | .ivsize = DES3_EDE_BLOCK_SIZE, |
| 980 | .maxauthsize = SHA1_DIGEST_SIZE, |
| 981 | }, |
| 982 | .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC, |
| 983 | .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP, |
| 984 | .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC, |
| 985 | }, |
| 986 | { |
| 987 | .name = "authenc(hmac(sha256),cbc(des3_ede))", |
| 988 | .driver_name = "authenc-hmac-sha256-cbc-des3_ede-caam", |
| 989 | .blocksize = DES3_EDE_BLOCK_SIZE, |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame^] | 990 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 991 | .template_aead = { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 992 | .setkey = aead_setkey, |
| 993 | .setauthsize = aead_setauthsize, |
| 994 | .encrypt = aead_encrypt, |
| 995 | .decrypt = aead_decrypt, |
| 996 | .givencrypt = aead_givencrypt, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 997 | .geniv = "<built-in>", |
| 998 | .ivsize = DES3_EDE_BLOCK_SIZE, |
| 999 | .maxauthsize = SHA256_DIGEST_SIZE, |
| 1000 | }, |
| 1001 | .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC, |
| 1002 | .class2_alg_type = OP_ALG_ALGSEL_SHA256 | |
| 1003 | OP_ALG_AAI_HMAC_PRECOMP, |
| 1004 | .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC, |
| 1005 | }, |
| 1006 | { |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 1007 | .name = "authenc(hmac(sha512),cbc(des3_ede))", |
| 1008 | .driver_name = "authenc-hmac-sha512-cbc-des3_ede-caam", |
| 1009 | .blocksize = DES3_EDE_BLOCK_SIZE, |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame^] | 1010 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 1011 | .template_aead = { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1012 | .setkey = aead_setkey, |
| 1013 | .setauthsize = aead_setauthsize, |
| 1014 | .encrypt = aead_encrypt, |
| 1015 | .decrypt = aead_decrypt, |
| 1016 | .givencrypt = aead_givencrypt, |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 1017 | .geniv = "<built-in>", |
| 1018 | .ivsize = DES3_EDE_BLOCK_SIZE, |
| 1019 | .maxauthsize = SHA512_DIGEST_SIZE, |
| 1020 | }, |
| 1021 | .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC, |
| 1022 | .class2_alg_type = OP_ALG_ALGSEL_SHA512 | |
| 1023 | OP_ALG_AAI_HMAC_PRECOMP, |
| 1024 | .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC, |
| 1025 | }, |
| 1026 | { |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1027 | .name = "authenc(hmac(sha1),cbc(des))", |
| 1028 | .driver_name = "authenc-hmac-sha1-cbc-des-caam", |
| 1029 | .blocksize = DES_BLOCK_SIZE, |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame^] | 1030 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 1031 | .template_aead = { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1032 | .setkey = aead_setkey, |
| 1033 | .setauthsize = aead_setauthsize, |
| 1034 | .encrypt = aead_encrypt, |
| 1035 | .decrypt = aead_decrypt, |
| 1036 | .givencrypt = aead_givencrypt, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1037 | .geniv = "<built-in>", |
| 1038 | .ivsize = DES_BLOCK_SIZE, |
| 1039 | .maxauthsize = SHA1_DIGEST_SIZE, |
| 1040 | }, |
| 1041 | .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC, |
| 1042 | .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP, |
| 1043 | .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC, |
| 1044 | }, |
| 1045 | { |
| 1046 | .name = "authenc(hmac(sha256),cbc(des))", |
| 1047 | .driver_name = "authenc-hmac-sha256-cbc-des-caam", |
| 1048 | .blocksize = DES_BLOCK_SIZE, |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame^] | 1049 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 1050 | .template_aead = { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1051 | .setkey = aead_setkey, |
| 1052 | .setauthsize = aead_setauthsize, |
| 1053 | .encrypt = aead_encrypt, |
| 1054 | .decrypt = aead_decrypt, |
| 1055 | .givencrypt = aead_givencrypt, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1056 | .geniv = "<built-in>", |
| 1057 | .ivsize = DES_BLOCK_SIZE, |
| 1058 | .maxauthsize = SHA256_DIGEST_SIZE, |
| 1059 | }, |
| 1060 | .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC, |
| 1061 | .class2_alg_type = OP_ALG_ALGSEL_SHA256 | |
| 1062 | OP_ALG_AAI_HMAC_PRECOMP, |
| 1063 | .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC, |
| 1064 | }, |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 1065 | { |
| 1066 | .name = "authenc(hmac(sha512),cbc(des))", |
| 1067 | .driver_name = "authenc-hmac-sha512-cbc-des-caam", |
| 1068 | .blocksize = DES_BLOCK_SIZE, |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame^] | 1069 | .type = CRYPTO_ALG_TYPE_AEAD, |
| 1070 | .template_aead = { |
Yuan Kang | 0e47930 | 2011-07-15 11:21:41 +0800 | [diff] [blame] | 1071 | .setkey = aead_setkey, |
| 1072 | .setauthsize = aead_setauthsize, |
| 1073 | .encrypt = aead_encrypt, |
| 1074 | .decrypt = aead_decrypt, |
| 1075 | .givencrypt = aead_givencrypt, |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 1076 | .geniv = "<built-in>", |
| 1077 | .ivsize = DES_BLOCK_SIZE, |
| 1078 | .maxauthsize = SHA512_DIGEST_SIZE, |
| 1079 | }, |
| 1080 | .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC, |
| 1081 | .class2_alg_type = OP_ALG_ALGSEL_SHA512 | |
| 1082 | OP_ALG_AAI_HMAC_PRECOMP, |
| 1083 | .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC, |
| 1084 | }, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1085 | }; |
| 1086 | |
| 1087 | struct caam_crypto_alg { |
| 1088 | struct list_head entry; |
| 1089 | struct device *ctrldev; |
| 1090 | int class1_alg_type; |
| 1091 | int class2_alg_type; |
| 1092 | int alg_op; |
| 1093 | struct crypto_alg crypto_alg; |
| 1094 | }; |
| 1095 | |
| 1096 | static int caam_cra_init(struct crypto_tfm *tfm) |
| 1097 | { |
| 1098 | struct crypto_alg *alg = tfm->__crt_alg; |
| 1099 | struct caam_crypto_alg *caam_alg = |
| 1100 | container_of(alg, struct caam_crypto_alg, crypto_alg); |
| 1101 | struct caam_ctx *ctx = crypto_tfm_ctx(tfm); |
| 1102 | struct caam_drv_private *priv = dev_get_drvdata(caam_alg->ctrldev); |
| 1103 | int tgt_jr = atomic_inc_return(&priv->tfm_count); |
| 1104 | |
| 1105 | /* |
| 1106 | * distribute tfms across job rings to ensure in-order |
| 1107 | * crypto request processing per tfm |
| 1108 | */ |
| 1109 | ctx->jrdev = priv->algapi_jr[(tgt_jr / 2) % priv->num_jrs_for_algapi]; |
| 1110 | |
| 1111 | /* copy descriptor header template value */ |
| 1112 | ctx->class1_alg_type = OP_TYPE_CLASS1_ALG | caam_alg->class1_alg_type; |
| 1113 | ctx->class2_alg_type = OP_TYPE_CLASS2_ALG | caam_alg->class2_alg_type; |
| 1114 | ctx->alg_op = OP_TYPE_CLASS2_ALG | caam_alg->alg_op; |
| 1115 | |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
| 1119 | static void caam_cra_exit(struct crypto_tfm *tfm) |
| 1120 | { |
| 1121 | struct caam_ctx *ctx = crypto_tfm_ctx(tfm); |
| 1122 | |
| 1123 | if (!dma_mapping_error(ctx->jrdev, ctx->shared_desc_phys)) |
| 1124 | dma_unmap_single(ctx->jrdev, ctx->shared_desc_phys, |
| 1125 | desc_bytes(ctx->sh_desc), DMA_TO_DEVICE); |
| 1126 | kfree(ctx->sh_desc); |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 1127 | |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame^] | 1128 | if (!dma_mapping_error(ctx->jrdev, ctx->key_dma)) |
| 1129 | dma_unmap_single(ctx->jrdev, ctx->key_dma, |
Kim Phillips | 4427b1b | 2011-05-14 22:08:17 -0500 | [diff] [blame] | 1130 | ctx->split_key_pad_len + ctx->enckeylen, |
| 1131 | DMA_TO_DEVICE); |
| 1132 | kfree(ctx->key); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | static void __exit caam_algapi_exit(void) |
| 1136 | { |
| 1137 | |
| 1138 | struct device_node *dev_node; |
| 1139 | struct platform_device *pdev; |
| 1140 | struct device *ctrldev; |
| 1141 | struct caam_drv_private *priv; |
| 1142 | struct caam_crypto_alg *t_alg, *n; |
| 1143 | int i, err; |
| 1144 | |
Kim Phillips | 54e198d | 2011-03-23 21:15:44 +0800 | [diff] [blame] | 1145 | dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0"); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1146 | if (!dev_node) |
| 1147 | return; |
| 1148 | |
| 1149 | pdev = of_find_device_by_node(dev_node); |
| 1150 | if (!pdev) |
| 1151 | return; |
| 1152 | |
| 1153 | ctrldev = &pdev->dev; |
| 1154 | of_node_put(dev_node); |
| 1155 | priv = dev_get_drvdata(ctrldev); |
| 1156 | |
| 1157 | if (!priv->alg_list.next) |
| 1158 | return; |
| 1159 | |
| 1160 | list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) { |
| 1161 | crypto_unregister_alg(&t_alg->crypto_alg); |
| 1162 | list_del(&t_alg->entry); |
| 1163 | kfree(t_alg); |
| 1164 | } |
| 1165 | |
| 1166 | for (i = 0; i < priv->total_jobrs; i++) { |
| 1167 | err = caam_jr_deregister(priv->algapi_jr[i]); |
| 1168 | if (err < 0) |
| 1169 | break; |
| 1170 | } |
| 1171 | kfree(priv->algapi_jr); |
| 1172 | } |
| 1173 | |
| 1174 | static struct caam_crypto_alg *caam_alg_alloc(struct device *ctrldev, |
| 1175 | struct caam_alg_template |
| 1176 | *template) |
| 1177 | { |
| 1178 | struct caam_crypto_alg *t_alg; |
| 1179 | struct crypto_alg *alg; |
| 1180 | |
| 1181 | t_alg = kzalloc(sizeof(struct caam_crypto_alg), GFP_KERNEL); |
| 1182 | if (!t_alg) { |
| 1183 | dev_err(ctrldev, "failed to allocate t_alg\n"); |
| 1184 | return ERR_PTR(-ENOMEM); |
| 1185 | } |
| 1186 | |
| 1187 | alg = &t_alg->crypto_alg; |
| 1188 | |
| 1189 | snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", template->name); |
| 1190 | snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s", |
| 1191 | template->driver_name); |
| 1192 | alg->cra_module = THIS_MODULE; |
| 1193 | alg->cra_init = caam_cra_init; |
| 1194 | alg->cra_exit = caam_cra_exit; |
| 1195 | alg->cra_priority = CAAM_CRA_PRIORITY; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1196 | alg->cra_blocksize = template->blocksize; |
| 1197 | alg->cra_alignmask = 0; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1198 | alg->cra_ctxsize = sizeof(struct caam_ctx); |
Yuan Kang | 885e9e2 | 2011-07-15 11:21:41 +0800 | [diff] [blame^] | 1199 | alg->cra_flags = CRYPTO_ALG_ASYNC | template->type; |
| 1200 | switch (template->type) { |
| 1201 | case CRYPTO_ALG_TYPE_AEAD: |
| 1202 | alg->cra_type = &crypto_aead_type; |
| 1203 | alg->cra_aead = template->template_aead; |
| 1204 | break; |
| 1205 | } |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1206 | |
| 1207 | t_alg->class1_alg_type = template->class1_alg_type; |
| 1208 | t_alg->class2_alg_type = template->class2_alg_type; |
| 1209 | t_alg->alg_op = template->alg_op; |
| 1210 | t_alg->ctrldev = ctrldev; |
| 1211 | |
| 1212 | return t_alg; |
| 1213 | } |
| 1214 | |
| 1215 | static int __init caam_algapi_init(void) |
| 1216 | { |
| 1217 | struct device_node *dev_node; |
| 1218 | struct platform_device *pdev; |
| 1219 | struct device *ctrldev, **jrdev; |
| 1220 | struct caam_drv_private *priv; |
| 1221 | int i = 0, err = 0; |
| 1222 | |
Kim Phillips | 54e198d | 2011-03-23 21:15:44 +0800 | [diff] [blame] | 1223 | dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0"); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1224 | if (!dev_node) |
| 1225 | return -ENODEV; |
| 1226 | |
| 1227 | pdev = of_find_device_by_node(dev_node); |
| 1228 | if (!pdev) |
| 1229 | return -ENODEV; |
| 1230 | |
| 1231 | ctrldev = &pdev->dev; |
| 1232 | priv = dev_get_drvdata(ctrldev); |
| 1233 | of_node_put(dev_node); |
| 1234 | |
| 1235 | INIT_LIST_HEAD(&priv->alg_list); |
| 1236 | |
| 1237 | jrdev = kmalloc(sizeof(*jrdev) * priv->total_jobrs, GFP_KERNEL); |
| 1238 | if (!jrdev) |
| 1239 | return -ENOMEM; |
| 1240 | |
| 1241 | for (i = 0; i < priv->total_jobrs; i++) { |
| 1242 | err = caam_jr_register(ctrldev, &jrdev[i]); |
| 1243 | if (err < 0) |
| 1244 | break; |
| 1245 | } |
| 1246 | if (err < 0 && i == 0) { |
| 1247 | dev_err(ctrldev, "algapi error in job ring registration: %d\n", |
| 1248 | err); |
Julia Lawall | b3b7f05 | 2011-04-08 20:39:23 +0800 | [diff] [blame] | 1249 | kfree(jrdev); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1250 | return err; |
| 1251 | } |
| 1252 | |
| 1253 | priv->num_jrs_for_algapi = i; |
| 1254 | priv->algapi_jr = jrdev; |
| 1255 | atomic_set(&priv->tfm_count, -1); |
| 1256 | |
| 1257 | /* register crypto algorithms the device supports */ |
| 1258 | for (i = 0; i < ARRAY_SIZE(driver_algs); i++) { |
| 1259 | /* TODO: check if h/w supports alg */ |
| 1260 | struct caam_crypto_alg *t_alg; |
| 1261 | |
| 1262 | t_alg = caam_alg_alloc(ctrldev, &driver_algs[i]); |
| 1263 | if (IS_ERR(t_alg)) { |
| 1264 | err = PTR_ERR(t_alg); |
| 1265 | dev_warn(ctrldev, "%s alg allocation failed\n", |
Dan Carpenter | cdc712d | 2011-03-23 21:20:27 +0800 | [diff] [blame] | 1266 | driver_algs[i].driver_name); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 1267 | continue; |
| 1268 | } |
| 1269 | |
| 1270 | err = crypto_register_alg(&t_alg->crypto_alg); |
| 1271 | if (err) { |
| 1272 | dev_warn(ctrldev, "%s alg registration failed\n", |
| 1273 | t_alg->crypto_alg.cra_driver_name); |
| 1274 | kfree(t_alg); |
| 1275 | } else { |
| 1276 | list_add_tail(&t_alg->entry, &priv->alg_list); |
| 1277 | dev_info(ctrldev, "%s\n", |
| 1278 | t_alg->crypto_alg.cra_driver_name); |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | return err; |
| 1283 | } |
| 1284 | |
| 1285 | module_init(caam_algapi_init); |
| 1286 | module_exit(caam_algapi_exit); |
| 1287 | |
| 1288 | MODULE_LICENSE("GPL"); |
| 1289 | MODULE_DESCRIPTION("FSL CAAM support for crypto API"); |
| 1290 | MODULE_AUTHOR("Freescale Semiconductor - NMG/STC"); |