blob: dce30ae2b704070ac7a599486dd567208dea94ea [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/* Copyright (C) 2012-2019 ARM Limited (or its affiliates). */
3
4#include <crypto/internal/aead.h>
5#include <crypto/authenc.h>
6#include <crypto/scatterwalk.h>
7#include <linux/dmapool.h>
8#include <linux/dma-mapping.h>
9
10#include "cc_buffer_mgr.h"
11#include "cc_lli_defs.h"
12#include "cc_cipher.h"
13#include "cc_hash.h"
14#include "cc_aead.h"
15
16enum dma_buffer_type {
17 DMA_NULL_TYPE = -1,
18 DMA_SGL_TYPE = 1,
19 DMA_BUFF_TYPE = 2,
20};
21
22struct buff_mgr_handle {
23 struct dma_pool *mlli_buffs_pool;
24};
25
26union buffer_array_entry {
27 struct scatterlist *sgl;
28 dma_addr_t buffer_dma;
29};
30
31struct buffer_array {
32 unsigned int num_of_buffers;
33 union buffer_array_entry entry[MAX_NUM_OF_BUFFERS_IN_MLLI];
34 unsigned int offset[MAX_NUM_OF_BUFFERS_IN_MLLI];
35 int nents[MAX_NUM_OF_BUFFERS_IN_MLLI];
36 int total_data_len[MAX_NUM_OF_BUFFERS_IN_MLLI];
37 enum dma_buffer_type type[MAX_NUM_OF_BUFFERS_IN_MLLI];
38 bool is_last[MAX_NUM_OF_BUFFERS_IN_MLLI];
39 u32 *mlli_nents[MAX_NUM_OF_BUFFERS_IN_MLLI];
40};
41
42static inline char *cc_dma_buf_type(enum cc_req_dma_buf_type type)
43{
44 switch (type) {
45 case CC_DMA_BUF_NULL:
46 return "BUF_NULL";
47 case CC_DMA_BUF_DLLI:
48 return "BUF_DLLI";
49 case CC_DMA_BUF_MLLI:
50 return "BUF_MLLI";
51 default:
52 return "BUF_INVALID";
53 }
54}
55
56/**
57 * cc_copy_mac() - Copy MAC to temporary location
58 *
59 * @dev: device object
60 * @req: aead request object
61 * @dir: [IN] copy from/to sgl
62 */
63static void cc_copy_mac(struct device *dev, struct aead_request *req,
64 enum cc_sg_cpy_direct dir)
65{
66 struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
67 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
68 u32 skip = areq_ctx->assoclen + req->cryptlen;
69
70 if (areq_ctx->is_gcm4543)
71 skip += crypto_aead_ivsize(tfm);
72
73 cc_copy_sg_portion(dev, areq_ctx->backup_mac, req->src,
74 (skip - areq_ctx->req_authsize), skip, dir);
75}
76
77/**
78 * cc_get_sgl_nents() - Get scatterlist number of entries.
79 *
80 * @sg_list: SG list
81 * @nbytes: [IN] Total SGL data bytes.
82 * @lbytes: [OUT] Returns the amount of bytes at the last entry
83 */
84static unsigned int cc_get_sgl_nents(struct device *dev,
85 struct scatterlist *sg_list,
86 unsigned int nbytes, u32 *lbytes)
87{
88 unsigned int nents = 0;
89
90 *lbytes = 0;
91
92 while (nbytes && sg_list) {
93 nents++;
94 /* get the number of bytes in the last entry */
95 *lbytes = nbytes;
96 nbytes -= (sg_list->length > nbytes) ?
97 nbytes : sg_list->length;
98 sg_list = sg_next(sg_list);
99 }
100
101 dev_dbg(dev, "nents %d last bytes %d\n", nents, *lbytes);
102 return nents;
103}
104
105/**
106 * cc_copy_sg_portion() - Copy scatter list data,
107 * from to_skip to end, to dest and vice versa
108 *
109 * @dest:
110 * @sg:
111 * @to_skip:
112 * @end:
113 * @direct:
114 */
115void cc_copy_sg_portion(struct device *dev, u8 *dest, struct scatterlist *sg,
116 u32 to_skip, u32 end, enum cc_sg_cpy_direct direct)
117{
118 u32 nents;
119
120 nents = sg_nents_for_len(sg, end);
121 sg_copy_buffer(sg, nents, (void *)dest, (end - to_skip + 1), to_skip,
122 (direct == CC_SG_TO_BUF));
123}
124
125static int cc_render_buff_to_mlli(struct device *dev, dma_addr_t buff_dma,
126 u32 buff_size, u32 *curr_nents,
127 u32 **mlli_entry_pp)
128{
129 u32 *mlli_entry_p = *mlli_entry_pp;
130 u32 new_nents;
131
132 /* Verify there is no memory overflow*/
133 new_nents = (*curr_nents + buff_size / CC_MAX_MLLI_ENTRY_SIZE + 1);
134 if (new_nents > MAX_NUM_OF_TOTAL_MLLI_ENTRIES) {
135 dev_err(dev, "Too many mlli entries. current %d max %d\n",
136 new_nents, MAX_NUM_OF_TOTAL_MLLI_ENTRIES);
137 return -ENOMEM;
138 }
139
140 /*handle buffer longer than 64 kbytes */
141 while (buff_size > CC_MAX_MLLI_ENTRY_SIZE) {
142 cc_lli_set_addr(mlli_entry_p, buff_dma);
143 cc_lli_set_size(mlli_entry_p, CC_MAX_MLLI_ENTRY_SIZE);
144 dev_dbg(dev, "entry[%d]: single_buff=0x%08X size=%08X\n",
145 *curr_nents, mlli_entry_p[LLI_WORD0_OFFSET],
146 mlli_entry_p[LLI_WORD1_OFFSET]);
147 buff_dma += CC_MAX_MLLI_ENTRY_SIZE;
148 buff_size -= CC_MAX_MLLI_ENTRY_SIZE;
149 mlli_entry_p = mlli_entry_p + 2;
150 (*curr_nents)++;
151 }
152 /*Last entry */
153 cc_lli_set_addr(mlli_entry_p, buff_dma);
154 cc_lli_set_size(mlli_entry_p, buff_size);
155 dev_dbg(dev, "entry[%d]: single_buff=0x%08X size=%08X\n",
156 *curr_nents, mlli_entry_p[LLI_WORD0_OFFSET],
157 mlli_entry_p[LLI_WORD1_OFFSET]);
158 mlli_entry_p = mlli_entry_p + 2;
159 *mlli_entry_pp = mlli_entry_p;
160 (*curr_nents)++;
161 return 0;
162}
163
164static int cc_render_sg_to_mlli(struct device *dev, struct scatterlist *sgl,
165 u32 sgl_data_len, u32 sgl_offset,
166 u32 *curr_nents, u32 **mlli_entry_pp)
167{
168 struct scatterlist *curr_sgl = sgl;
169 u32 *mlli_entry_p = *mlli_entry_pp;
170 s32 rc = 0;
171
172 for ( ; (curr_sgl && sgl_data_len);
173 curr_sgl = sg_next(curr_sgl)) {
174 u32 entry_data_len =
175 (sgl_data_len > sg_dma_len(curr_sgl) - sgl_offset) ?
176 sg_dma_len(curr_sgl) - sgl_offset :
177 sgl_data_len;
178 sgl_data_len -= entry_data_len;
179 rc = cc_render_buff_to_mlli(dev, sg_dma_address(curr_sgl) +
180 sgl_offset, entry_data_len,
181 curr_nents, &mlli_entry_p);
182 if (rc)
183 return rc;
184
185 sgl_offset = 0;
186 }
187 *mlli_entry_pp = mlli_entry_p;
188 return 0;
189}
190
191static int cc_generate_mlli(struct device *dev, struct buffer_array *sg_data,
192 struct mlli_params *mlli_params, gfp_t flags)
193{
194 u32 *mlli_p;
195 u32 total_nents = 0, prev_total_nents = 0;
196 int rc = 0, i;
197
198 dev_dbg(dev, "NUM of SG's = %d\n", sg_data->num_of_buffers);
199
200 /* Allocate memory from the pointed pool */
201 mlli_params->mlli_virt_addr =
202 dma_pool_alloc(mlli_params->curr_pool, flags,
203 &mlli_params->mlli_dma_addr);
204 if (!mlli_params->mlli_virt_addr) {
205 dev_err(dev, "dma_pool_alloc() failed\n");
206 rc = -ENOMEM;
207 goto build_mlli_exit;
208 }
209 /* Point to start of MLLI */
210 mlli_p = (u32 *)mlli_params->mlli_virt_addr;
211 /* go over all SG's and link it to one MLLI table */
212 for (i = 0; i < sg_data->num_of_buffers; i++) {
213 union buffer_array_entry *entry = &sg_data->entry[i];
214 u32 tot_len = sg_data->total_data_len[i];
215 u32 offset = sg_data->offset[i];
216
217 if (sg_data->type[i] == DMA_SGL_TYPE)
218 rc = cc_render_sg_to_mlli(dev, entry->sgl, tot_len,
219 offset, &total_nents,
220 &mlli_p);
221 else /*DMA_BUFF_TYPE*/
222 rc = cc_render_buff_to_mlli(dev, entry->buffer_dma,
223 tot_len, &total_nents,
224 &mlli_p);
225 if (rc)
226 return rc;
227
228 /* set last bit in the current table */
229 if (sg_data->mlli_nents[i]) {
230 /*Calculate the current MLLI table length for the
231 *length field in the descriptor
232 */
233 *sg_data->mlli_nents[i] +=
234 (total_nents - prev_total_nents);
235 prev_total_nents = total_nents;
236 }
237 }
238
239 /* Set MLLI size for the bypass operation */
240 mlli_params->mlli_len = (total_nents * LLI_ENTRY_BYTE_SIZE);
241
242 dev_dbg(dev, "MLLI params: virt_addr=%pK dma_addr=%pad mlli_len=0x%X\n",
243 mlli_params->mlli_virt_addr, &mlli_params->mlli_dma_addr,
244 mlli_params->mlli_len);
245
246build_mlli_exit:
247 return rc;
248}
249
250static void cc_add_buffer_entry(struct device *dev,
251 struct buffer_array *sgl_data,
252 dma_addr_t buffer_dma, unsigned int buffer_len,
253 bool is_last_entry, u32 *mlli_nents)
254{
255 unsigned int index = sgl_data->num_of_buffers;
256
257 dev_dbg(dev, "index=%u single_buff=%pad buffer_len=0x%08X is_last=%d\n",
258 index, &buffer_dma, buffer_len, is_last_entry);
259 sgl_data->nents[index] = 1;
260 sgl_data->entry[index].buffer_dma = buffer_dma;
261 sgl_data->offset[index] = 0;
262 sgl_data->total_data_len[index] = buffer_len;
263 sgl_data->type[index] = DMA_BUFF_TYPE;
264 sgl_data->is_last[index] = is_last_entry;
265 sgl_data->mlli_nents[index] = mlli_nents;
266 if (sgl_data->mlli_nents[index])
267 *sgl_data->mlli_nents[index] = 0;
268 sgl_data->num_of_buffers++;
269}
270
271static void cc_add_sg_entry(struct device *dev, struct buffer_array *sgl_data,
272 unsigned int nents, struct scatterlist *sgl,
273 unsigned int data_len, unsigned int data_offset,
274 bool is_last_table, u32 *mlli_nents)
275{
276 unsigned int index = sgl_data->num_of_buffers;
277
278 dev_dbg(dev, "index=%u nents=%u sgl=%pK data_len=0x%08X is_last=%d\n",
279 index, nents, sgl, data_len, is_last_table);
280 sgl_data->nents[index] = nents;
281 sgl_data->entry[index].sgl = sgl;
282 sgl_data->offset[index] = data_offset;
283 sgl_data->total_data_len[index] = data_len;
284 sgl_data->type[index] = DMA_SGL_TYPE;
285 sgl_data->is_last[index] = is_last_table;
286 sgl_data->mlli_nents[index] = mlli_nents;
287 if (sgl_data->mlli_nents[index])
288 *sgl_data->mlli_nents[index] = 0;
289 sgl_data->num_of_buffers++;
290}
291
292static int cc_map_sg(struct device *dev, struct scatterlist *sg,
293 unsigned int nbytes, int direction, u32 *nents,
294 u32 max_sg_nents, u32 *lbytes, u32 *mapped_nents)
295{
296 int ret = 0;
297
298 if (!nbytes) {
299 *mapped_nents = 0;
300 *lbytes = 0;
301 *nents = 0;
302 return 0;
303 }
304
305 *nents = cc_get_sgl_nents(dev, sg, nbytes, lbytes);
306 if (*nents > max_sg_nents) {
307 *nents = 0;
308 dev_err(dev, "Too many fragments. current %d max %d\n",
309 *nents, max_sg_nents);
310 return -ENOMEM;
311 }
312
313 ret = dma_map_sg(dev, sg, *nents, direction);
314 if (dma_mapping_error(dev, ret)) {
315 *nents = 0;
316 dev_err(dev, "dma_map_sg() sg buffer failed %d\n", ret);
317 return -ENOMEM;
318 }
319
320 *mapped_nents = ret;
321
322 return 0;
323}
324
325static int
326cc_set_aead_conf_buf(struct device *dev, struct aead_req_ctx *areq_ctx,
327 u8 *config_data, struct buffer_array *sg_data,
328 unsigned int assoclen)
329{
330 dev_dbg(dev, " handle additional data config set to DLLI\n");
331 /* create sg for the current buffer */
332 sg_init_one(&areq_ctx->ccm_adata_sg, config_data,
333 AES_BLOCK_SIZE + areq_ctx->ccm_hdr_size);
334 if (dma_map_sg(dev, &areq_ctx->ccm_adata_sg, 1, DMA_TO_DEVICE) != 1) {
335 dev_err(dev, "dma_map_sg() config buffer failed\n");
336 return -ENOMEM;
337 }
338 dev_dbg(dev, "Mapped curr_buff: dma_address=%pad page=%p addr=%pK offset=%u length=%u\n",
339 &sg_dma_address(&areq_ctx->ccm_adata_sg),
340 sg_page(&areq_ctx->ccm_adata_sg),
341 sg_virt(&areq_ctx->ccm_adata_sg),
342 areq_ctx->ccm_adata_sg.offset, areq_ctx->ccm_adata_sg.length);
343 /* prepare for case of MLLI */
344 if (assoclen > 0) {
345 cc_add_sg_entry(dev, sg_data, 1, &areq_ctx->ccm_adata_sg,
346 (AES_BLOCK_SIZE + areq_ctx->ccm_hdr_size),
347 0, false, NULL);
348 }
349 return 0;
350}
351
352static int cc_set_hash_buf(struct device *dev, struct ahash_req_ctx *areq_ctx,
353 u8 *curr_buff, u32 curr_buff_cnt,
354 struct buffer_array *sg_data)
355{
356 dev_dbg(dev, " handle curr buff %x set to DLLI\n", curr_buff_cnt);
357 /* create sg for the current buffer */
358 sg_init_one(areq_ctx->buff_sg, curr_buff, curr_buff_cnt);
359 if (dma_map_sg(dev, areq_ctx->buff_sg, 1, DMA_TO_DEVICE) != 1) {
360 dev_err(dev, "dma_map_sg() src buffer failed\n");
361 return -ENOMEM;
362 }
363 dev_dbg(dev, "Mapped curr_buff: dma_address=%pad page=%p addr=%pK offset=%u length=%u\n",
364 &sg_dma_address(areq_ctx->buff_sg), sg_page(areq_ctx->buff_sg),
365 sg_virt(areq_ctx->buff_sg), areq_ctx->buff_sg->offset,
366 areq_ctx->buff_sg->length);
367 areq_ctx->data_dma_buf_type = CC_DMA_BUF_DLLI;
368 areq_ctx->curr_sg = areq_ctx->buff_sg;
369 areq_ctx->in_nents = 0;
370 /* prepare for case of MLLI */
371 cc_add_sg_entry(dev, sg_data, 1, areq_ctx->buff_sg, curr_buff_cnt, 0,
372 false, NULL);
373 return 0;
374}
375
376void cc_unmap_cipher_request(struct device *dev, void *ctx,
377 unsigned int ivsize, struct scatterlist *src,
378 struct scatterlist *dst)
379{
380 struct cipher_req_ctx *req_ctx = (struct cipher_req_ctx *)ctx;
381
382 if (req_ctx->gen_ctx.iv_dma_addr) {
383 dev_dbg(dev, "Unmapped iv: iv_dma_addr=%pad iv_size=%u\n",
384 &req_ctx->gen_ctx.iv_dma_addr, ivsize);
385 dma_unmap_single(dev, req_ctx->gen_ctx.iv_dma_addr,
386 ivsize, DMA_BIDIRECTIONAL);
387 }
388 /* Release pool */
389 if (req_ctx->dma_buf_type == CC_DMA_BUF_MLLI &&
390 req_ctx->mlli_params.mlli_virt_addr) {
391 dma_pool_free(req_ctx->mlli_params.curr_pool,
392 req_ctx->mlli_params.mlli_virt_addr,
393 req_ctx->mlli_params.mlli_dma_addr);
394 }
395
396 dma_unmap_sg(dev, src, req_ctx->in_nents, DMA_BIDIRECTIONAL);
397 dev_dbg(dev, "Unmapped req->src=%pK\n", sg_virt(src));
398
399 if (src != dst) {
400 dma_unmap_sg(dev, dst, req_ctx->out_nents, DMA_BIDIRECTIONAL);
401 dev_dbg(dev, "Unmapped req->dst=%pK\n", sg_virt(dst));
402 }
403}
404
405int cc_map_cipher_request(struct cc_drvdata *drvdata, void *ctx,
406 unsigned int ivsize, unsigned int nbytes,
407 void *info, struct scatterlist *src,
408 struct scatterlist *dst, gfp_t flags)
409{
410 struct cipher_req_ctx *req_ctx = (struct cipher_req_ctx *)ctx;
411 struct mlli_params *mlli_params = &req_ctx->mlli_params;
412 struct buff_mgr_handle *buff_mgr = drvdata->buff_mgr_handle;
413 struct device *dev = drvdata_to_dev(drvdata);
414 struct buffer_array sg_data;
415 u32 dummy = 0;
416 int rc = 0;
417 u32 mapped_nents = 0;
418
419 req_ctx->dma_buf_type = CC_DMA_BUF_DLLI;
420 mlli_params->curr_pool = NULL;
421 sg_data.num_of_buffers = 0;
422
423 /* Map IV buffer */
424 if (ivsize) {
425 dump_byte_array("iv", (u8 *)info, ivsize);
426 req_ctx->gen_ctx.iv_dma_addr =
427 dma_map_single(dev, (void *)info,
428 ivsize, DMA_BIDIRECTIONAL);
429 if (dma_mapping_error(dev, req_ctx->gen_ctx.iv_dma_addr)) {
430 dev_err(dev, "Mapping iv %u B at va=%pK for DMA failed\n",
431 ivsize, info);
432 return -ENOMEM;
433 }
434 dev_dbg(dev, "Mapped iv %u B at va=%pK to dma=%pad\n",
435 ivsize, info, &req_ctx->gen_ctx.iv_dma_addr);
436 } else {
437 req_ctx->gen_ctx.iv_dma_addr = 0;
438 }
439
440 /* Map the src SGL */
441 rc = cc_map_sg(dev, src, nbytes, DMA_BIDIRECTIONAL, &req_ctx->in_nents,
442 LLI_MAX_NUM_OF_DATA_ENTRIES, &dummy, &mapped_nents);
443 if (rc)
444 goto cipher_exit;
445 if (mapped_nents > 1)
446 req_ctx->dma_buf_type = CC_DMA_BUF_MLLI;
447
448 if (src == dst) {
449 /* Handle inplace operation */
450 if (req_ctx->dma_buf_type == CC_DMA_BUF_MLLI) {
451 req_ctx->out_nents = 0;
452 cc_add_sg_entry(dev, &sg_data, req_ctx->in_nents, src,
453 nbytes, 0, true,
454 &req_ctx->in_mlli_nents);
455 }
456 } else {
457 /* Map the dst sg */
458 rc = cc_map_sg(dev, dst, nbytes, DMA_BIDIRECTIONAL,
459 &req_ctx->out_nents, LLI_MAX_NUM_OF_DATA_ENTRIES,
460 &dummy, &mapped_nents);
461 if (rc)
462 goto cipher_exit;
463 if (mapped_nents > 1)
464 req_ctx->dma_buf_type = CC_DMA_BUF_MLLI;
465
466 if (req_ctx->dma_buf_type == CC_DMA_BUF_MLLI) {
467 cc_add_sg_entry(dev, &sg_data, req_ctx->in_nents, src,
468 nbytes, 0, true,
469 &req_ctx->in_mlli_nents);
470 cc_add_sg_entry(dev, &sg_data, req_ctx->out_nents, dst,
471 nbytes, 0, true,
472 &req_ctx->out_mlli_nents);
473 }
474 }
475
476 if (req_ctx->dma_buf_type == CC_DMA_BUF_MLLI) {
477 mlli_params->curr_pool = buff_mgr->mlli_buffs_pool;
478 rc = cc_generate_mlli(dev, &sg_data, mlli_params, flags);
479 if (rc)
480 goto cipher_exit;
481 }
482
483 dev_dbg(dev, "areq_ctx->dma_buf_type = %s\n",
484 cc_dma_buf_type(req_ctx->dma_buf_type));
485
486 return 0;
487
488cipher_exit:
489 cc_unmap_cipher_request(dev, req_ctx, ivsize, src, dst);
490 return rc;
491}
492
493void cc_unmap_aead_request(struct device *dev, struct aead_request *req)
494{
495 struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
496 unsigned int hw_iv_size = areq_ctx->hw_iv_size;
497 struct cc_drvdata *drvdata = dev_get_drvdata(dev);
498
499 if (areq_ctx->mac_buf_dma_addr) {
500 dma_unmap_single(dev, areq_ctx->mac_buf_dma_addr,
501 MAX_MAC_SIZE, DMA_BIDIRECTIONAL);
502 }
503
504 if (areq_ctx->cipher_mode == DRV_CIPHER_GCTR) {
505 if (areq_ctx->hkey_dma_addr) {
506 dma_unmap_single(dev, areq_ctx->hkey_dma_addr,
507 AES_BLOCK_SIZE, DMA_BIDIRECTIONAL);
508 }
509
510 if (areq_ctx->gcm_block_len_dma_addr) {
511 dma_unmap_single(dev, areq_ctx->gcm_block_len_dma_addr,
512 AES_BLOCK_SIZE, DMA_TO_DEVICE);
513 }
514
515 if (areq_ctx->gcm_iv_inc1_dma_addr) {
516 dma_unmap_single(dev, areq_ctx->gcm_iv_inc1_dma_addr,
517 AES_BLOCK_SIZE, DMA_TO_DEVICE);
518 }
519
520 if (areq_ctx->gcm_iv_inc2_dma_addr) {
521 dma_unmap_single(dev, areq_ctx->gcm_iv_inc2_dma_addr,
522 AES_BLOCK_SIZE, DMA_TO_DEVICE);
523 }
524 }
525
526 if (areq_ctx->ccm_hdr_size != ccm_header_size_null) {
527 if (areq_ctx->ccm_iv0_dma_addr) {
528 dma_unmap_single(dev, areq_ctx->ccm_iv0_dma_addr,
529 AES_BLOCK_SIZE, DMA_TO_DEVICE);
530 }
531
532 dma_unmap_sg(dev, &areq_ctx->ccm_adata_sg, 1, DMA_TO_DEVICE);
533 }
534 if (areq_ctx->gen_ctx.iv_dma_addr) {
535 dma_unmap_single(dev, areq_ctx->gen_ctx.iv_dma_addr,
536 hw_iv_size, DMA_BIDIRECTIONAL);
537 kzfree(areq_ctx->gen_ctx.iv);
538 }
539
540 /* Release pool */
541 if ((areq_ctx->assoc_buff_type == CC_DMA_BUF_MLLI ||
542 areq_ctx->data_buff_type == CC_DMA_BUF_MLLI) &&
543 (areq_ctx->mlli_params.mlli_virt_addr)) {
544 dev_dbg(dev, "free MLLI buffer: dma=%pad virt=%pK\n",
545 &areq_ctx->mlli_params.mlli_dma_addr,
546 areq_ctx->mlli_params.mlli_virt_addr);
547 dma_pool_free(areq_ctx->mlli_params.curr_pool,
548 areq_ctx->mlli_params.mlli_virt_addr,
549 areq_ctx->mlli_params.mlli_dma_addr);
550 }
551
552 dev_dbg(dev, "Unmapping src sgl: req->src=%pK areq_ctx->src.nents=%u areq_ctx->assoc.nents=%u assoclen:%u cryptlen=%u\n",
553 sg_virt(req->src), areq_ctx->src.nents, areq_ctx->assoc.nents,
554 areq_ctx->assoclen, req->cryptlen);
555
556 dma_unmap_sg(dev, req->src, areq_ctx->src.mapped_nents,
557 DMA_BIDIRECTIONAL);
558 if (req->src != req->dst) {
559 dev_dbg(dev, "Unmapping dst sgl: req->dst=%pK\n",
560 sg_virt(req->dst));
561 dma_unmap_sg(dev, req->dst, areq_ctx->dst.mapped_nents,
562 DMA_BIDIRECTIONAL);
563 }
564 if (drvdata->coherent &&
565 areq_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_DECRYPT &&
566 req->src == req->dst) {
567 /* copy back mac from temporary location to deal with possible
568 * data memory overriding that caused by cache coherence
569 * problem.
570 */
571 cc_copy_mac(dev, req, CC_SG_FROM_BUF);
572 }
573}
574
575static bool cc_is_icv_frag(unsigned int sgl_nents, unsigned int authsize,
576 u32 last_entry_data_size)
577{
578 return ((sgl_nents > 1) && (last_entry_data_size < authsize));
579}
580
581static int cc_aead_chain_iv(struct cc_drvdata *drvdata,
582 struct aead_request *req,
583 struct buffer_array *sg_data,
584 bool is_last, bool do_chain)
585{
586 struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
587 unsigned int hw_iv_size = areq_ctx->hw_iv_size;
588 struct device *dev = drvdata_to_dev(drvdata);
589 gfp_t flags = cc_gfp_flags(&req->base);
590 int rc = 0;
591
592 if (!req->iv) {
593 areq_ctx->gen_ctx.iv_dma_addr = 0;
594 areq_ctx->gen_ctx.iv = NULL;
595 goto chain_iv_exit;
596 }
597
598 areq_ctx->gen_ctx.iv = kmemdup(req->iv, hw_iv_size, flags);
599 if (!areq_ctx->gen_ctx.iv)
600 return -ENOMEM;
601
602 areq_ctx->gen_ctx.iv_dma_addr =
603 dma_map_single(dev, areq_ctx->gen_ctx.iv, hw_iv_size,
604 DMA_BIDIRECTIONAL);
605 if (dma_mapping_error(dev, areq_ctx->gen_ctx.iv_dma_addr)) {
606 dev_err(dev, "Mapping iv %u B at va=%pK for DMA failed\n",
607 hw_iv_size, req->iv);
608 kzfree(areq_ctx->gen_ctx.iv);
609 areq_ctx->gen_ctx.iv = NULL;
610 rc = -ENOMEM;
611 goto chain_iv_exit;
612 }
613
614 dev_dbg(dev, "Mapped iv %u B at va=%pK to dma=%pad\n",
615 hw_iv_size, req->iv, &areq_ctx->gen_ctx.iv_dma_addr);
616 // TODO: what about CTR?? ask Ron
617 if (do_chain && areq_ctx->plaintext_authenticate_only) {
618 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
619 unsigned int iv_size_to_authenc = crypto_aead_ivsize(tfm);
620 unsigned int iv_ofs = GCM_BLOCK_RFC4_IV_OFFSET;
621 /* Chain to given list */
622 cc_add_buffer_entry(dev, sg_data,
623 (areq_ctx->gen_ctx.iv_dma_addr + iv_ofs),
624 iv_size_to_authenc, is_last,
625 &areq_ctx->assoc.mlli_nents);
626 areq_ctx->assoc_buff_type = CC_DMA_BUF_MLLI;
627 }
628
629chain_iv_exit:
630 return rc;
631}
632
633static int cc_aead_chain_assoc(struct cc_drvdata *drvdata,
634 struct aead_request *req,
635 struct buffer_array *sg_data,
636 bool is_last, bool do_chain)
637{
638 struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
639 int rc = 0;
640 int mapped_nents = 0;
641 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
642 unsigned int size_of_assoc = areq_ctx->assoclen;
643 struct device *dev = drvdata_to_dev(drvdata);
644
645 if (areq_ctx->is_gcm4543)
646 size_of_assoc += crypto_aead_ivsize(tfm);
647
648 if (!sg_data) {
649 rc = -EINVAL;
650 goto chain_assoc_exit;
651 }
652
653 if (areq_ctx->assoclen == 0) {
654 areq_ctx->assoc_buff_type = CC_DMA_BUF_NULL;
655 areq_ctx->assoc.nents = 0;
656 areq_ctx->assoc.mlli_nents = 0;
657 dev_dbg(dev, "Chain assoc of length 0: buff_type=%s nents=%u\n",
658 cc_dma_buf_type(areq_ctx->assoc_buff_type),
659 areq_ctx->assoc.nents);
660 goto chain_assoc_exit;
661 }
662
663 mapped_nents = sg_nents_for_len(req->src, size_of_assoc);
664 if (mapped_nents < 0)
665 return mapped_nents;
666
667 if (mapped_nents > LLI_MAX_NUM_OF_ASSOC_DATA_ENTRIES) {
668 dev_err(dev, "Too many fragments. current %d max %d\n",
669 mapped_nents, LLI_MAX_NUM_OF_ASSOC_DATA_ENTRIES);
670 return -ENOMEM;
671 }
672 areq_ctx->assoc.nents = mapped_nents;
673
674 /* in CCM case we have additional entry for
675 * ccm header configurations
676 */
677 if (areq_ctx->ccm_hdr_size != ccm_header_size_null) {
678 if ((mapped_nents + 1) > LLI_MAX_NUM_OF_ASSOC_DATA_ENTRIES) {
679 dev_err(dev, "CCM case.Too many fragments. Current %d max %d\n",
680 (areq_ctx->assoc.nents + 1),
681 LLI_MAX_NUM_OF_ASSOC_DATA_ENTRIES);
682 rc = -ENOMEM;
683 goto chain_assoc_exit;
684 }
685 }
686
687 if (mapped_nents == 1 && areq_ctx->ccm_hdr_size == ccm_header_size_null)
688 areq_ctx->assoc_buff_type = CC_DMA_BUF_DLLI;
689 else
690 areq_ctx->assoc_buff_type = CC_DMA_BUF_MLLI;
691
692 if (do_chain || areq_ctx->assoc_buff_type == CC_DMA_BUF_MLLI) {
693 dev_dbg(dev, "Chain assoc: buff_type=%s nents=%u\n",
694 cc_dma_buf_type(areq_ctx->assoc_buff_type),
695 areq_ctx->assoc.nents);
696 cc_add_sg_entry(dev, sg_data, areq_ctx->assoc.nents, req->src,
697 areq_ctx->assoclen, 0, is_last,
698 &areq_ctx->assoc.mlli_nents);
699 areq_ctx->assoc_buff_type = CC_DMA_BUF_MLLI;
700 }
701
702chain_assoc_exit:
703 return rc;
704}
705
706static void cc_prepare_aead_data_dlli(struct aead_request *req,
707 u32 *src_last_bytes, u32 *dst_last_bytes)
708{
709 struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
710 enum drv_crypto_direction direct = areq_ctx->gen_ctx.op_type;
711 unsigned int authsize = areq_ctx->req_authsize;
712 struct scatterlist *sg;
713 ssize_t offset;
714
715 areq_ctx->is_icv_fragmented = false;
716
717 if ((req->src == req->dst) || direct == DRV_CRYPTO_DIRECTION_DECRYPT) {
718 sg = areq_ctx->src_sgl;
719 offset = *src_last_bytes - authsize;
720 } else {
721 sg = areq_ctx->dst_sgl;
722 offset = *dst_last_bytes - authsize;
723 }
724
725 areq_ctx->icv_dma_addr = sg_dma_address(sg) + offset;
726 areq_ctx->icv_virt_addr = sg_virt(sg) + offset;
727}
728
729static void cc_prepare_aead_data_mlli(struct cc_drvdata *drvdata,
730 struct aead_request *req,
731 struct buffer_array *sg_data,
732 u32 *src_last_bytes, u32 *dst_last_bytes,
733 bool is_last_table)
734{
735 struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
736 enum drv_crypto_direction direct = areq_ctx->gen_ctx.op_type;
737 unsigned int authsize = areq_ctx->req_authsize;
738 struct device *dev = drvdata_to_dev(drvdata);
739 struct scatterlist *sg;
740
741 if (req->src == req->dst) {
742 /*INPLACE*/
743 cc_add_sg_entry(dev, sg_data, areq_ctx->src.nents,
744 areq_ctx->src_sgl, areq_ctx->cryptlen,
745 areq_ctx->src_offset, is_last_table,
746 &areq_ctx->src.mlli_nents);
747
748 areq_ctx->is_icv_fragmented =
749 cc_is_icv_frag(areq_ctx->src.nents, authsize,
750 *src_last_bytes);
751
752 if (areq_ctx->is_icv_fragmented) {
753 /* Backup happens only when ICV is fragmented, ICV
754 * verification is made by CPU compare in order to
755 * simplify MAC verification upon request completion
756 */
757 if (direct == DRV_CRYPTO_DIRECTION_DECRYPT) {
758 /* In coherent platforms (e.g. ACP)
759 * already copying ICV for any
760 * INPLACE-DECRYPT operation, hence
761 * we must neglect this code.
762 */
763 if (!drvdata->coherent)
764 cc_copy_mac(dev, req, CC_SG_TO_BUF);
765
766 areq_ctx->icv_virt_addr = areq_ctx->backup_mac;
767 } else {
768 areq_ctx->icv_virt_addr = areq_ctx->mac_buf;
769 areq_ctx->icv_dma_addr =
770 areq_ctx->mac_buf_dma_addr;
771 }
772 } else { /* Contig. ICV */
773 sg = &areq_ctx->src_sgl[areq_ctx->src.nents - 1];
774 /*Should hanlde if the sg is not contig.*/
775 areq_ctx->icv_dma_addr = sg_dma_address(sg) +
776 (*src_last_bytes - authsize);
777 areq_ctx->icv_virt_addr = sg_virt(sg) +
778 (*src_last_bytes - authsize);
779 }
780
781 } else if (direct == DRV_CRYPTO_DIRECTION_DECRYPT) {
782 /*NON-INPLACE and DECRYPT*/
783 cc_add_sg_entry(dev, sg_data, areq_ctx->src.nents,
784 areq_ctx->src_sgl, areq_ctx->cryptlen,
785 areq_ctx->src_offset, is_last_table,
786 &areq_ctx->src.mlli_nents);
787 cc_add_sg_entry(dev, sg_data, areq_ctx->dst.nents,
788 areq_ctx->dst_sgl, areq_ctx->cryptlen,
789 areq_ctx->dst_offset, is_last_table,
790 &areq_ctx->dst.mlli_nents);
791
792 areq_ctx->is_icv_fragmented =
793 cc_is_icv_frag(areq_ctx->src.nents, authsize,
794 *src_last_bytes);
795 /* Backup happens only when ICV is fragmented, ICV
796
797 * verification is made by CPU compare in order to simplify
798 * MAC verification upon request completion
799 */
800 if (areq_ctx->is_icv_fragmented) {
801 cc_copy_mac(dev, req, CC_SG_TO_BUF);
802 areq_ctx->icv_virt_addr = areq_ctx->backup_mac;
803
804 } else { /* Contig. ICV */
805 sg = &areq_ctx->src_sgl[areq_ctx->src.nents - 1];
806 /*Should hanlde if the sg is not contig.*/
807 areq_ctx->icv_dma_addr = sg_dma_address(sg) +
808 (*src_last_bytes - authsize);
809 areq_ctx->icv_virt_addr = sg_virt(sg) +
810 (*src_last_bytes - authsize);
811 }
812
813 } else {
814 /*NON-INPLACE and ENCRYPT*/
815 cc_add_sg_entry(dev, sg_data, areq_ctx->dst.nents,
816 areq_ctx->dst_sgl, areq_ctx->cryptlen,
817 areq_ctx->dst_offset, is_last_table,
818 &areq_ctx->dst.mlli_nents);
819 cc_add_sg_entry(dev, sg_data, areq_ctx->src.nents,
820 areq_ctx->src_sgl, areq_ctx->cryptlen,
821 areq_ctx->src_offset, is_last_table,
822 &areq_ctx->src.mlli_nents);
823
824 areq_ctx->is_icv_fragmented =
825 cc_is_icv_frag(areq_ctx->dst.nents, authsize,
826 *dst_last_bytes);
827
828 if (!areq_ctx->is_icv_fragmented) {
829 sg = &areq_ctx->dst_sgl[areq_ctx->dst.nents - 1];
830 /* Contig. ICV */
831 areq_ctx->icv_dma_addr = sg_dma_address(sg) +
832 (*dst_last_bytes - authsize);
833 areq_ctx->icv_virt_addr = sg_virt(sg) +
834 (*dst_last_bytes - authsize);
835 } else {
836 areq_ctx->icv_dma_addr = areq_ctx->mac_buf_dma_addr;
837 areq_ctx->icv_virt_addr = areq_ctx->mac_buf;
838 }
839 }
840}
841
842static int cc_aead_chain_data(struct cc_drvdata *drvdata,
843 struct aead_request *req,
844 struct buffer_array *sg_data,
845 bool is_last_table, bool do_chain)
846{
847 struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
848 struct device *dev = drvdata_to_dev(drvdata);
849 enum drv_crypto_direction direct = areq_ctx->gen_ctx.op_type;
850 unsigned int authsize = areq_ctx->req_authsize;
851 unsigned int src_last_bytes = 0, dst_last_bytes = 0;
852 int rc = 0;
853 u32 src_mapped_nents = 0, dst_mapped_nents = 0;
854 u32 offset = 0;
855 /* non-inplace mode */
856 unsigned int size_for_map = areq_ctx->assoclen + req->cryptlen;
857 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
858 u32 sg_index = 0;
859 bool is_gcm4543 = areq_ctx->is_gcm4543;
860 u32 size_to_skip = areq_ctx->assoclen;
861 struct scatterlist *sgl;
862
863 if (is_gcm4543)
864 size_to_skip += crypto_aead_ivsize(tfm);
865
866 offset = size_to_skip;
867
868 if (!sg_data)
869 return -EINVAL;
870
871 areq_ctx->src_sgl = req->src;
872 areq_ctx->dst_sgl = req->dst;
873
874 if (is_gcm4543)
875 size_for_map += crypto_aead_ivsize(tfm);
876
877 size_for_map += (direct == DRV_CRYPTO_DIRECTION_ENCRYPT) ?
878 authsize : 0;
879 src_mapped_nents = cc_get_sgl_nents(dev, req->src, size_for_map,
880 &src_last_bytes);
881 sg_index = areq_ctx->src_sgl->length;
882 //check where the data starts
883 while (src_mapped_nents && (sg_index <= size_to_skip)) {
884 src_mapped_nents--;
885 offset -= areq_ctx->src_sgl->length;
886 sgl = sg_next(areq_ctx->src_sgl);
887 if (!sgl)
888 break;
889 areq_ctx->src_sgl = sgl;
890 sg_index += areq_ctx->src_sgl->length;
891 }
892 if (src_mapped_nents > LLI_MAX_NUM_OF_DATA_ENTRIES) {
893 dev_err(dev, "Too many fragments. current %d max %d\n",
894 src_mapped_nents, LLI_MAX_NUM_OF_DATA_ENTRIES);
895 return -ENOMEM;
896 }
897
898 areq_ctx->src.nents = src_mapped_nents;
899
900 areq_ctx->src_offset = offset;
901
902 if (req->src != req->dst) {
903 size_for_map = areq_ctx->assoclen + req->cryptlen;
904
905 if (direct == DRV_CRYPTO_DIRECTION_ENCRYPT)
906 size_for_map += authsize;
907 else
908 size_for_map -= authsize;
909
910 if (is_gcm4543)
911 size_for_map += crypto_aead_ivsize(tfm);
912
913 rc = cc_map_sg(dev, req->dst, size_for_map, DMA_BIDIRECTIONAL,
914 &areq_ctx->dst.mapped_nents,
915 LLI_MAX_NUM_OF_DATA_ENTRIES, &dst_last_bytes,
916 &dst_mapped_nents);
917 if (rc)
918 goto chain_data_exit;
919 }
920
921 dst_mapped_nents = cc_get_sgl_nents(dev, req->dst, size_for_map,
922 &dst_last_bytes);
923 sg_index = areq_ctx->dst_sgl->length;
924 offset = size_to_skip;
925
926 //check where the data starts
927 while (dst_mapped_nents && sg_index <= size_to_skip) {
928 dst_mapped_nents--;
929 offset -= areq_ctx->dst_sgl->length;
930 sgl = sg_next(areq_ctx->dst_sgl);
931 if (!sgl)
932 break;
933 areq_ctx->dst_sgl = sgl;
934 sg_index += areq_ctx->dst_sgl->length;
935 }
936 if (dst_mapped_nents > LLI_MAX_NUM_OF_DATA_ENTRIES) {
937 dev_err(dev, "Too many fragments. current %d max %d\n",
938 dst_mapped_nents, LLI_MAX_NUM_OF_DATA_ENTRIES);
939 return -ENOMEM;
940 }
941 areq_ctx->dst.nents = dst_mapped_nents;
942 areq_ctx->dst_offset = offset;
943 if (src_mapped_nents > 1 ||
944 dst_mapped_nents > 1 ||
945 do_chain) {
946 areq_ctx->data_buff_type = CC_DMA_BUF_MLLI;
947 cc_prepare_aead_data_mlli(drvdata, req, sg_data,
948 &src_last_bytes, &dst_last_bytes,
949 is_last_table);
950 } else {
951 areq_ctx->data_buff_type = CC_DMA_BUF_DLLI;
952 cc_prepare_aead_data_dlli(req, &src_last_bytes,
953 &dst_last_bytes);
954 }
955
956chain_data_exit:
957 return rc;
958}
959
960static void cc_update_aead_mlli_nents(struct cc_drvdata *drvdata,
961 struct aead_request *req)
962{
963 struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
964 u32 curr_mlli_size = 0;
965
966 if (areq_ctx->assoc_buff_type == CC_DMA_BUF_MLLI) {
967 areq_ctx->assoc.sram_addr = drvdata->mlli_sram_addr;
968 curr_mlli_size = areq_ctx->assoc.mlli_nents *
969 LLI_ENTRY_BYTE_SIZE;
970 }
971
972 if (areq_ctx->data_buff_type == CC_DMA_BUF_MLLI) {
973 /*Inplace case dst nents equal to src nents*/
974 if (req->src == req->dst) {
975 areq_ctx->dst.mlli_nents = areq_ctx->src.mlli_nents;
976 areq_ctx->src.sram_addr = drvdata->mlli_sram_addr +
977 curr_mlli_size;
978 areq_ctx->dst.sram_addr = areq_ctx->src.sram_addr;
979 if (!areq_ctx->is_single_pass)
980 areq_ctx->assoc.mlli_nents +=
981 areq_ctx->src.mlli_nents;
982 } else {
983 if (areq_ctx->gen_ctx.op_type ==
984 DRV_CRYPTO_DIRECTION_DECRYPT) {
985 areq_ctx->src.sram_addr =
986 drvdata->mlli_sram_addr +
987 curr_mlli_size;
988 areq_ctx->dst.sram_addr =
989 areq_ctx->src.sram_addr +
990 areq_ctx->src.mlli_nents *
991 LLI_ENTRY_BYTE_SIZE;
992 if (!areq_ctx->is_single_pass)
993 areq_ctx->assoc.mlli_nents +=
994 areq_ctx->src.mlli_nents;
995 } else {
996 areq_ctx->dst.sram_addr =
997 drvdata->mlli_sram_addr +
998 curr_mlli_size;
999 areq_ctx->src.sram_addr =
1000 areq_ctx->dst.sram_addr +
1001 areq_ctx->dst.mlli_nents *
1002 LLI_ENTRY_BYTE_SIZE;
1003 if (!areq_ctx->is_single_pass)
1004 areq_ctx->assoc.mlli_nents +=
1005 areq_ctx->dst.mlli_nents;
1006 }
1007 }
1008 }
1009}
1010
1011int cc_map_aead_request(struct cc_drvdata *drvdata, struct aead_request *req)
1012{
1013 struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
1014 struct mlli_params *mlli_params = &areq_ctx->mlli_params;
1015 struct device *dev = drvdata_to_dev(drvdata);
1016 struct buffer_array sg_data;
1017 unsigned int authsize = areq_ctx->req_authsize;
1018 struct buff_mgr_handle *buff_mgr = drvdata->buff_mgr_handle;
1019 int rc = 0;
1020 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1021 bool is_gcm4543 = areq_ctx->is_gcm4543;
1022 dma_addr_t dma_addr;
1023 u32 mapped_nents = 0;
1024 u32 dummy = 0; /*used for the assoc data fragments */
1025 u32 size_to_map = 0;
1026 gfp_t flags = cc_gfp_flags(&req->base);
1027
1028 mlli_params->curr_pool = NULL;
1029 sg_data.num_of_buffers = 0;
1030
1031 /* copy mac to a temporary location to deal with possible
1032 * data memory overriding that caused by cache coherence problem.
1033 */
1034 if (drvdata->coherent &&
1035 areq_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_DECRYPT &&
1036 req->src == req->dst)
1037 cc_copy_mac(dev, req, CC_SG_TO_BUF);
1038
1039 /* cacluate the size for cipher remove ICV in decrypt*/
1040 areq_ctx->cryptlen = (areq_ctx->gen_ctx.op_type ==
1041 DRV_CRYPTO_DIRECTION_ENCRYPT) ?
1042 req->cryptlen :
1043 (req->cryptlen - authsize);
1044
1045 dma_addr = dma_map_single(dev, areq_ctx->mac_buf, MAX_MAC_SIZE,
1046 DMA_BIDIRECTIONAL);
1047 if (dma_mapping_error(dev, dma_addr)) {
1048 dev_err(dev, "Mapping mac_buf %u B at va=%pK for DMA failed\n",
1049 MAX_MAC_SIZE, areq_ctx->mac_buf);
1050 rc = -ENOMEM;
1051 goto aead_map_failure;
1052 }
1053 areq_ctx->mac_buf_dma_addr = dma_addr;
1054
1055 if (areq_ctx->ccm_hdr_size != ccm_header_size_null) {
1056 void *addr = areq_ctx->ccm_config + CCM_CTR_COUNT_0_OFFSET;
1057
1058 dma_addr = dma_map_single(dev, addr, AES_BLOCK_SIZE,
1059 DMA_TO_DEVICE);
1060
1061 if (dma_mapping_error(dev, dma_addr)) {
1062 dev_err(dev, "Mapping mac_buf %u B at va=%pK for DMA failed\n",
1063 AES_BLOCK_SIZE, addr);
1064 areq_ctx->ccm_iv0_dma_addr = 0;
1065 rc = -ENOMEM;
1066 goto aead_map_failure;
1067 }
1068 areq_ctx->ccm_iv0_dma_addr = dma_addr;
1069
1070 rc = cc_set_aead_conf_buf(dev, areq_ctx, areq_ctx->ccm_config,
1071 &sg_data, areq_ctx->assoclen);
1072 if (rc)
1073 goto aead_map_failure;
1074 }
1075
1076 if (areq_ctx->cipher_mode == DRV_CIPHER_GCTR) {
1077 dma_addr = dma_map_single(dev, areq_ctx->hkey, AES_BLOCK_SIZE,
1078 DMA_BIDIRECTIONAL);
1079 if (dma_mapping_error(dev, dma_addr)) {
1080 dev_err(dev, "Mapping hkey %u B at va=%pK for DMA failed\n",
1081 AES_BLOCK_SIZE, areq_ctx->hkey);
1082 rc = -ENOMEM;
1083 goto aead_map_failure;
1084 }
1085 areq_ctx->hkey_dma_addr = dma_addr;
1086
1087 dma_addr = dma_map_single(dev, &areq_ctx->gcm_len_block,
1088 AES_BLOCK_SIZE, DMA_TO_DEVICE);
1089 if (dma_mapping_error(dev, dma_addr)) {
1090 dev_err(dev, "Mapping gcm_len_block %u B at va=%pK for DMA failed\n",
1091 AES_BLOCK_SIZE, &areq_ctx->gcm_len_block);
1092 rc = -ENOMEM;
1093 goto aead_map_failure;
1094 }
1095 areq_ctx->gcm_block_len_dma_addr = dma_addr;
1096
1097 dma_addr = dma_map_single(dev, areq_ctx->gcm_iv_inc1,
1098 AES_BLOCK_SIZE, DMA_TO_DEVICE);
1099
1100 if (dma_mapping_error(dev, dma_addr)) {
1101 dev_err(dev, "Mapping gcm_iv_inc1 %u B at va=%pK for DMA failed\n",
1102 AES_BLOCK_SIZE, (areq_ctx->gcm_iv_inc1));
1103 areq_ctx->gcm_iv_inc1_dma_addr = 0;
1104 rc = -ENOMEM;
1105 goto aead_map_failure;
1106 }
1107 areq_ctx->gcm_iv_inc1_dma_addr = dma_addr;
1108
1109 dma_addr = dma_map_single(dev, areq_ctx->gcm_iv_inc2,
1110 AES_BLOCK_SIZE, DMA_TO_DEVICE);
1111
1112 if (dma_mapping_error(dev, dma_addr)) {
1113 dev_err(dev, "Mapping gcm_iv_inc2 %u B at va=%pK for DMA failed\n",
1114 AES_BLOCK_SIZE, (areq_ctx->gcm_iv_inc2));
1115 areq_ctx->gcm_iv_inc2_dma_addr = 0;
1116 rc = -ENOMEM;
1117 goto aead_map_failure;
1118 }
1119 areq_ctx->gcm_iv_inc2_dma_addr = dma_addr;
1120 }
1121
1122 size_to_map = req->cryptlen + areq_ctx->assoclen;
1123 /* If we do in-place encryption, we also need the auth tag */
1124 if ((areq_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_ENCRYPT) &&
1125 (req->src == req->dst)) {
1126 size_to_map += authsize;
1127 }
1128 if (is_gcm4543)
1129 size_to_map += crypto_aead_ivsize(tfm);
1130 rc = cc_map_sg(dev, req->src, size_to_map, DMA_BIDIRECTIONAL,
1131 &areq_ctx->src.mapped_nents,
1132 (LLI_MAX_NUM_OF_ASSOC_DATA_ENTRIES +
1133 LLI_MAX_NUM_OF_DATA_ENTRIES),
1134 &dummy, &mapped_nents);
1135 if (rc)
1136 goto aead_map_failure;
1137
1138 if (areq_ctx->is_single_pass) {
1139 /*
1140 * Create MLLI table for:
1141 * (1) Assoc. data
1142 * (2) Src/Dst SGLs
1143 * Note: IV is contg. buffer (not an SGL)
1144 */
1145 rc = cc_aead_chain_assoc(drvdata, req, &sg_data, true, false);
1146 if (rc)
1147 goto aead_map_failure;
1148 rc = cc_aead_chain_iv(drvdata, req, &sg_data, true, false);
1149 if (rc)
1150 goto aead_map_failure;
1151 rc = cc_aead_chain_data(drvdata, req, &sg_data, true, false);
1152 if (rc)
1153 goto aead_map_failure;
1154 } else { /* DOUBLE-PASS flow */
1155 /*
1156 * Prepare MLLI table(s) in this order:
1157 *
1158 * If ENCRYPT/DECRYPT (inplace):
1159 * (1) MLLI table for assoc
1160 * (2) IV entry (chained right after end of assoc)
1161 * (3) MLLI for src/dst (inplace operation)
1162 *
1163 * If ENCRYPT (non-inplace)
1164 * (1) MLLI table for assoc
1165 * (2) IV entry (chained right after end of assoc)
1166 * (3) MLLI for dst
1167 * (4) MLLI for src
1168 *
1169 * If DECRYPT (non-inplace)
1170 * (1) MLLI table for assoc
1171 * (2) IV entry (chained right after end of assoc)
1172 * (3) MLLI for src
1173 * (4) MLLI for dst
1174 */
1175 rc = cc_aead_chain_assoc(drvdata, req, &sg_data, false, true);
1176 if (rc)
1177 goto aead_map_failure;
1178 rc = cc_aead_chain_iv(drvdata, req, &sg_data, false, true);
1179 if (rc)
1180 goto aead_map_failure;
1181 rc = cc_aead_chain_data(drvdata, req, &sg_data, true, true);
1182 if (rc)
1183 goto aead_map_failure;
1184 }
1185
1186 /* Mlli support -start building the MLLI according to the above
1187 * results
1188 */
1189 if (areq_ctx->assoc_buff_type == CC_DMA_BUF_MLLI ||
1190 areq_ctx->data_buff_type == CC_DMA_BUF_MLLI) {
1191 mlli_params->curr_pool = buff_mgr->mlli_buffs_pool;
1192 rc = cc_generate_mlli(dev, &sg_data, mlli_params, flags);
1193 if (rc)
1194 goto aead_map_failure;
1195
1196 cc_update_aead_mlli_nents(drvdata, req);
1197 dev_dbg(dev, "assoc params mn %d\n",
1198 areq_ctx->assoc.mlli_nents);
1199 dev_dbg(dev, "src params mn %d\n", areq_ctx->src.mlli_nents);
1200 dev_dbg(dev, "dst params mn %d\n", areq_ctx->dst.mlli_nents);
1201 }
1202 return 0;
1203
1204aead_map_failure:
1205 cc_unmap_aead_request(dev, req);
1206 return rc;
1207}
1208
1209int cc_map_hash_request_final(struct cc_drvdata *drvdata, void *ctx,
1210 struct scatterlist *src, unsigned int nbytes,
1211 bool do_update, gfp_t flags)
1212{
1213 struct ahash_req_ctx *areq_ctx = (struct ahash_req_ctx *)ctx;
1214 struct device *dev = drvdata_to_dev(drvdata);
1215 u8 *curr_buff = cc_hash_buf(areq_ctx);
1216 u32 *curr_buff_cnt = cc_hash_buf_cnt(areq_ctx);
1217 struct mlli_params *mlli_params = &areq_ctx->mlli_params;
1218 struct buffer_array sg_data;
1219 struct buff_mgr_handle *buff_mgr = drvdata->buff_mgr_handle;
1220 int rc = 0;
1221 u32 dummy = 0;
1222 u32 mapped_nents = 0;
1223
1224 dev_dbg(dev, "final params : curr_buff=%pK curr_buff_cnt=0x%X nbytes = 0x%X src=%pK curr_index=%u\n",
1225 curr_buff, *curr_buff_cnt, nbytes, src, areq_ctx->buff_index);
1226 /* Init the type of the dma buffer */
1227 areq_ctx->data_dma_buf_type = CC_DMA_BUF_NULL;
1228 mlli_params->curr_pool = NULL;
1229 sg_data.num_of_buffers = 0;
1230 areq_ctx->in_nents = 0;
1231
1232 if (nbytes == 0 && *curr_buff_cnt == 0) {
1233 /* nothing to do */
1234 return 0;
1235 }
1236
1237 /*TODO: copy data in case that buffer is enough for operation */
1238 /* map the previous buffer */
1239 if (*curr_buff_cnt) {
1240 rc = cc_set_hash_buf(dev, areq_ctx, curr_buff, *curr_buff_cnt,
1241 &sg_data);
1242 if (rc)
1243 return rc;
1244 }
1245
1246 if (src && nbytes > 0 && do_update) {
1247 rc = cc_map_sg(dev, src, nbytes, DMA_TO_DEVICE,
1248 &areq_ctx->in_nents, LLI_MAX_NUM_OF_DATA_ENTRIES,
1249 &dummy, &mapped_nents);
1250 if (rc)
1251 goto unmap_curr_buff;
1252 if (src && mapped_nents == 1 &&
1253 areq_ctx->data_dma_buf_type == CC_DMA_BUF_NULL) {
1254 memcpy(areq_ctx->buff_sg, src,
1255 sizeof(struct scatterlist));
1256 areq_ctx->buff_sg->length = nbytes;
1257 areq_ctx->curr_sg = areq_ctx->buff_sg;
1258 areq_ctx->data_dma_buf_type = CC_DMA_BUF_DLLI;
1259 } else {
1260 areq_ctx->data_dma_buf_type = CC_DMA_BUF_MLLI;
1261 }
1262 }
1263
1264 /*build mlli */
1265 if (areq_ctx->data_dma_buf_type == CC_DMA_BUF_MLLI) {
1266 mlli_params->curr_pool = buff_mgr->mlli_buffs_pool;
1267 /* add the src data to the sg_data */
1268 cc_add_sg_entry(dev, &sg_data, areq_ctx->in_nents, src, nbytes,
1269 0, true, &areq_ctx->mlli_nents);
1270 rc = cc_generate_mlli(dev, &sg_data, mlli_params, flags);
1271 if (rc)
1272 goto fail_unmap_din;
1273 }
1274 /* change the buffer index for the unmap function */
1275 areq_ctx->buff_index = (areq_ctx->buff_index ^ 1);
1276 dev_dbg(dev, "areq_ctx->data_dma_buf_type = %s\n",
1277 cc_dma_buf_type(areq_ctx->data_dma_buf_type));
1278 return 0;
1279
1280fail_unmap_din:
1281 dma_unmap_sg(dev, src, areq_ctx->in_nents, DMA_TO_DEVICE);
1282
1283unmap_curr_buff:
1284 if (*curr_buff_cnt)
1285 dma_unmap_sg(dev, areq_ctx->buff_sg, 1, DMA_TO_DEVICE);
1286
1287 return rc;
1288}
1289
1290int cc_map_hash_request_update(struct cc_drvdata *drvdata, void *ctx,
1291 struct scatterlist *src, unsigned int nbytes,
1292 unsigned int block_size, gfp_t flags)
1293{
1294 struct ahash_req_ctx *areq_ctx = (struct ahash_req_ctx *)ctx;
1295 struct device *dev = drvdata_to_dev(drvdata);
1296 u8 *curr_buff = cc_hash_buf(areq_ctx);
1297 u32 *curr_buff_cnt = cc_hash_buf_cnt(areq_ctx);
1298 u8 *next_buff = cc_next_buf(areq_ctx);
1299 u32 *next_buff_cnt = cc_next_buf_cnt(areq_ctx);
1300 struct mlli_params *mlli_params = &areq_ctx->mlli_params;
1301 unsigned int update_data_len;
1302 u32 total_in_len = nbytes + *curr_buff_cnt;
1303 struct buffer_array sg_data;
1304 struct buff_mgr_handle *buff_mgr = drvdata->buff_mgr_handle;
1305 unsigned int swap_index = 0;
1306 int rc = 0;
1307 u32 dummy = 0;
1308 u32 mapped_nents = 0;
1309
1310 dev_dbg(dev, " update params : curr_buff=%pK curr_buff_cnt=0x%X nbytes=0x%X src=%pK curr_index=%u\n",
1311 curr_buff, *curr_buff_cnt, nbytes, src, areq_ctx->buff_index);
1312 /* Init the type of the dma buffer */
1313 areq_ctx->data_dma_buf_type = CC_DMA_BUF_NULL;
1314 mlli_params->curr_pool = NULL;
1315 areq_ctx->curr_sg = NULL;
1316 sg_data.num_of_buffers = 0;
1317 areq_ctx->in_nents = 0;
1318
1319 if (total_in_len < block_size) {
1320 dev_dbg(dev, " less than one block: curr_buff=%pK *curr_buff_cnt=0x%X copy_to=%pK\n",
1321 curr_buff, *curr_buff_cnt, &curr_buff[*curr_buff_cnt]);
1322 areq_ctx->in_nents = sg_nents_for_len(src, nbytes);
1323 sg_copy_to_buffer(src, areq_ctx->in_nents,
1324 &curr_buff[*curr_buff_cnt], nbytes);
1325 *curr_buff_cnt += nbytes;
1326 return 1;
1327 }
1328
1329 /* Calculate the residue size*/
1330 *next_buff_cnt = total_in_len & (block_size - 1);
1331 /* update data len */
1332 update_data_len = total_in_len - *next_buff_cnt;
1333
1334 dev_dbg(dev, " temp length : *next_buff_cnt=0x%X update_data_len=0x%X\n",
1335 *next_buff_cnt, update_data_len);
1336
1337 /* Copy the new residue to next buffer */
1338 if (*next_buff_cnt) {
1339 dev_dbg(dev, " handle residue: next buff %pK skip data %u residue %u\n",
1340 next_buff, (update_data_len - *curr_buff_cnt),
1341 *next_buff_cnt);
1342 cc_copy_sg_portion(dev, next_buff, src,
1343 (update_data_len - *curr_buff_cnt),
1344 nbytes, CC_SG_TO_BUF);
1345 /* change the buffer index for next operation */
1346 swap_index = 1;
1347 }
1348
1349 if (*curr_buff_cnt) {
1350 rc = cc_set_hash_buf(dev, areq_ctx, curr_buff, *curr_buff_cnt,
1351 &sg_data);
1352 if (rc)
1353 return rc;
1354 /* change the buffer index for next operation */
1355 swap_index = 1;
1356 }
1357
1358 if (update_data_len > *curr_buff_cnt) {
1359 rc = cc_map_sg(dev, src, (update_data_len - *curr_buff_cnt),
1360 DMA_TO_DEVICE, &areq_ctx->in_nents,
1361 LLI_MAX_NUM_OF_DATA_ENTRIES, &dummy,
1362 &mapped_nents);
1363 if (rc)
1364 goto unmap_curr_buff;
1365 if (mapped_nents == 1 &&
1366 areq_ctx->data_dma_buf_type == CC_DMA_BUF_NULL) {
1367 /* only one entry in the SG and no previous data */
1368 memcpy(areq_ctx->buff_sg, src,
1369 sizeof(struct scatterlist));
1370 areq_ctx->buff_sg->length = update_data_len;
1371 areq_ctx->data_dma_buf_type = CC_DMA_BUF_DLLI;
1372 areq_ctx->curr_sg = areq_ctx->buff_sg;
1373 } else {
1374 areq_ctx->data_dma_buf_type = CC_DMA_BUF_MLLI;
1375 }
1376 }
1377
1378 if (areq_ctx->data_dma_buf_type == CC_DMA_BUF_MLLI) {
1379 mlli_params->curr_pool = buff_mgr->mlli_buffs_pool;
1380 /* add the src data to the sg_data */
1381 cc_add_sg_entry(dev, &sg_data, areq_ctx->in_nents, src,
1382 (update_data_len - *curr_buff_cnt), 0, true,
1383 &areq_ctx->mlli_nents);
1384 rc = cc_generate_mlli(dev, &sg_data, mlli_params, flags);
1385 if (rc)
1386 goto fail_unmap_din;
1387 }
1388 areq_ctx->buff_index = (areq_ctx->buff_index ^ swap_index);
1389
1390 return 0;
1391
1392fail_unmap_din:
1393 dma_unmap_sg(dev, src, areq_ctx->in_nents, DMA_TO_DEVICE);
1394
1395unmap_curr_buff:
1396 if (*curr_buff_cnt)
1397 dma_unmap_sg(dev, areq_ctx->buff_sg, 1, DMA_TO_DEVICE);
1398
1399 return rc;
1400}
1401
1402void cc_unmap_hash_request(struct device *dev, void *ctx,
1403 struct scatterlist *src, bool do_revert)
1404{
1405 struct ahash_req_ctx *areq_ctx = (struct ahash_req_ctx *)ctx;
1406 u32 *prev_len = cc_next_buf_cnt(areq_ctx);
1407
1408 /*In case a pool was set, a table was
1409 *allocated and should be released
1410 */
1411 if (areq_ctx->mlli_params.curr_pool) {
1412 dev_dbg(dev, "free MLLI buffer: dma=%pad virt=%pK\n",
1413 &areq_ctx->mlli_params.mlli_dma_addr,
1414 areq_ctx->mlli_params.mlli_virt_addr);
1415 dma_pool_free(areq_ctx->mlli_params.curr_pool,
1416 areq_ctx->mlli_params.mlli_virt_addr,
1417 areq_ctx->mlli_params.mlli_dma_addr);
1418 }
1419
1420 if (src && areq_ctx->in_nents) {
1421 dev_dbg(dev, "Unmapped sg src: virt=%pK dma=%pad len=0x%X\n",
1422 sg_virt(src), &sg_dma_address(src), sg_dma_len(src));
1423 dma_unmap_sg(dev, src,
1424 areq_ctx->in_nents, DMA_TO_DEVICE);
1425 }
1426
1427 if (*prev_len) {
1428 dev_dbg(dev, "Unmapped buffer: areq_ctx->buff_sg=%pK dma=%pad len 0x%X\n",
1429 sg_virt(areq_ctx->buff_sg),
1430 &sg_dma_address(areq_ctx->buff_sg),
1431 sg_dma_len(areq_ctx->buff_sg));
1432 dma_unmap_sg(dev, areq_ctx->buff_sg, 1, DMA_TO_DEVICE);
1433 if (!do_revert) {
1434 /* clean the previous data length for update
1435 * operation
1436 */
1437 *prev_len = 0;
1438 } else {
1439 areq_ctx->buff_index ^= 1;
1440 }
1441 }
1442}
1443
1444int cc_buffer_mgr_init(struct cc_drvdata *drvdata)
1445{
1446 struct buff_mgr_handle *buff_mgr_handle;
1447 struct device *dev = drvdata_to_dev(drvdata);
1448
1449 buff_mgr_handle = kmalloc(sizeof(*buff_mgr_handle), GFP_KERNEL);
1450 if (!buff_mgr_handle)
1451 return -ENOMEM;
1452
1453 drvdata->buff_mgr_handle = buff_mgr_handle;
1454
1455 buff_mgr_handle->mlli_buffs_pool =
1456 dma_pool_create("dx_single_mlli_tables", dev,
1457 MAX_NUM_OF_TOTAL_MLLI_ENTRIES *
1458 LLI_ENTRY_BYTE_SIZE,
1459 MLLI_TABLE_MIN_ALIGNMENT, 0);
1460
1461 if (!buff_mgr_handle->mlli_buffs_pool)
1462 goto error;
1463
1464 return 0;
1465
1466error:
1467 cc_buffer_mgr_fini(drvdata);
1468 return -ENOMEM;
1469}
1470
1471int cc_buffer_mgr_fini(struct cc_drvdata *drvdata)
1472{
1473 struct buff_mgr_handle *buff_mgr_handle = drvdata->buff_mgr_handle;
1474
1475 if (buff_mgr_handle) {
1476 dma_pool_destroy(buff_mgr_handle->mlli_buffs_pool);
1477 kfree(drvdata->buff_mgr_handle);
1478 drvdata->buff_mgr_handle = NULL;
1479 }
1480 return 0;
1481}