2 * Copyright (C) 2012-2017 ARM Limited or its affiliates.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 #include <linux/crypto.h>
18 #include <linux/version.h>
19 #include <crypto/algapi.h>
20 #include <crypto/internal/aead.h>
21 #include <crypto/hash.h>
22 #include <crypto/authenc.h>
23 #include <crypto/scatterwalk.h>
24 #include <linux/dmapool.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/crypto.h>
27 #include <linux/module.h>
28 #include <linux/platform_device.h>
30 #include "ssi_buffer_mgr.h"
31 #include "cc_lli_defs.h"
32 #include "ssi_cipher.h"
36 #define LLI_MAX_NUM_OF_DATA_ENTRIES 128
37 #define LLI_MAX_NUM_OF_ASSOC_DATA_ENTRIES 4
38 #define MLLI_TABLE_MIN_ALIGNMENT 4 /*Force the MLLI table to be align to uint32 */
39 #define MAX_NUM_OF_BUFFERS_IN_MLLI 4
40 #define MAX_NUM_OF_TOTAL_MLLI_ENTRIES (2*LLI_MAX_NUM_OF_DATA_ENTRIES + \
41 LLI_MAX_NUM_OF_ASSOC_DATA_ENTRIES )
44 #define DUMP_SGL(sg) \
46 SSI_LOG_DEBUG("page=%lu offset=%u length=%u (dma_len=%u) " \
47 "dma_addr=%08x\n", (sg)->page_link, (sg)->offset, \
48 (sg)->length, sg_dma_len(sg), (sg)->dma_address); \
51 #define DUMP_MLLI_TABLE(mlli_p, nents) \
53 SSI_LOG_DEBUG("mlli=%pK nents=%u\n", (mlli_p), (nents)); \
55 SSI_LOG_DEBUG("addr=0x%08X size=0x%08X\n", \
56 (mlli_p)[LLI_WORD0_OFFSET], \
57 (mlli_p)[LLI_WORD1_OFFSET]); \
58 (mlli_p) += LLI_ENTRY_WORD_SIZE; \
61 #define GET_DMA_BUFFER_TYPE(buff_type) ( \
62 ((buff_type) == SSI_DMA_BUF_NULL) ? "BUF_NULL" : \
63 ((buff_type) == SSI_DMA_BUF_DLLI) ? "BUF_DLLI" : \
64 ((buff_type) == SSI_DMA_BUF_MLLI) ? "BUF_MLLI" : "BUF_INVALID")
66 #define DX_BUFFER_MGR_DUMP_SGL(sg)
67 #define DX_BUFFER_MGR_DUMP_MLLI_TABLE(mlli_p, nents)
68 #define GET_DMA_BUFFER_TYPE(buff_type)
72 enum dma_buffer_type {
78 struct buff_mgr_handle {
79 struct dma_pool *mlli_buffs_pool;
82 union buffer_array_entry {
83 struct scatterlist *sgl;
84 dma_addr_t buffer_dma;
88 unsigned int num_of_buffers;
89 union buffer_array_entry entry[MAX_NUM_OF_BUFFERS_IN_MLLI];
90 unsigned int offset[MAX_NUM_OF_BUFFERS_IN_MLLI];
91 int nents[MAX_NUM_OF_BUFFERS_IN_MLLI];
92 int total_data_len[MAX_NUM_OF_BUFFERS_IN_MLLI];
93 enum dma_buffer_type type[MAX_NUM_OF_BUFFERS_IN_MLLI];
94 bool is_last[MAX_NUM_OF_BUFFERS_IN_MLLI];
95 uint32_t * mlli_nents[MAX_NUM_OF_BUFFERS_IN_MLLI];
98 #ifdef CC_DMA_48BIT_SIM
99 dma_addr_t ssi_buff_mgr_update_dma_addr(dma_addr_t orig_addr, uint32_t data_len)
101 dma_addr_t tmp_dma_addr;
102 #ifdef CC_DMA_48BIT_SIM_FULL
103 /* With this code all addresses will be switched to 48 bits. */
104 /* The if condition protects from double expention */
105 if((((orig_addr >> 16) & 0xFFFF) != 0xFFFF) &&
106 (data_len <= CC_MAX_MLLI_ENTRY_SIZE)) {
108 if((!(((orig_addr >> 16) & 0xFF) % 2)) &&
109 (data_len <= CC_MAX_MLLI_ENTRY_SIZE)) {
111 tmp_dma_addr = ((orig_addr<<16) | 0xFFFF0000 |
112 (orig_addr & UINT16_MAX));
113 SSI_LOG_DEBUG("MAP DMA: orig address=0x%llX "
114 "dma_address=0x%llX\n",
115 orig_addr, tmp_dma_addr);
121 dma_addr_t ssi_buff_mgr_restore_dma_addr(dma_addr_t orig_addr)
123 dma_addr_t tmp_dma_addr;
124 #ifdef CC_DMA_48BIT_SIM_FULL
125 /* With this code all addresses will be restored from 48 bits. */
126 /* The if condition protects from double restoring */
127 if((orig_addr >> 32) & 0xFFFF ) {
129 if(((orig_addr >> 32) & 0xFFFF) &&
130 !(((orig_addr >> 32) & 0xFF) % 2) ) {
132 /*return high 16 bits*/
133 tmp_dma_addr = ((orig_addr >> 16));
134 /*clean the 0xFFFF in the lower bits (set in the add expansion)*/
135 tmp_dma_addr &= 0xFFFF0000;
136 /* Set the original 16 bits */
137 tmp_dma_addr |= (orig_addr & UINT16_MAX);
138 SSI_LOG_DEBUG("Release DMA: orig address=0x%llX "
139 "dma_address=0x%llX\n",
140 orig_addr, tmp_dma_addr);
147 * ssi_buffer_mgr_get_sgl_nents() - Get scatterlist number of entries.
150 * @nbytes: [IN] Total SGL data bytes.
151 * @lbytes: [OUT] Returns the amount of bytes at the last entry
153 static unsigned int ssi_buffer_mgr_get_sgl_nents(
154 struct scatterlist *sg_list, unsigned int nbytes, uint32_t *lbytes, bool *is_chained)
156 unsigned int nents = 0;
157 while (nbytes != 0) {
158 if (sg_is_chain(sg_list)) {
159 SSI_LOG_ERR("Unexpected chanined entry "
160 "in sg (entry =0x%X) \n", nents);
163 if (sg_list->length != 0) {
165 /* get the number of bytes in the last entry */
167 nbytes -= ( sg_list->length > nbytes ) ? nbytes : sg_list->length;
168 sg_list = sg_next(sg_list);
170 sg_list = (struct scatterlist *)sg_page(sg_list);
171 if (is_chained != NULL) {
176 SSI_LOG_DEBUG("nents %d last bytes %d\n",nents, *lbytes);
181 * ssi_buffer_mgr_zero_sgl() - Zero scatter scatter list data.
185 void ssi_buffer_mgr_zero_sgl(struct scatterlist *sgl, uint32_t data_len)
187 struct scatterlist *current_sg = sgl;
190 while (sg_index <= data_len) {
191 if (current_sg == NULL) {
192 /* reached the end of the sgl --> just return back */
195 memset(sg_virt(current_sg), 0, current_sg->length);
196 sg_index += current_sg->length;
197 current_sg = sg_next(current_sg);
202 * ssi_buffer_mgr_copy_scatterlist_portion() - Copy scatter list data,
203 * from to_skip to end, to dest and vice versa
211 void ssi_buffer_mgr_copy_scatterlist_portion(
212 u8 *dest, struct scatterlist *sg,
213 uint32_t to_skip, uint32_t end,
214 enum ssi_sg_cpy_direct direct)
216 uint32_t nents, lbytes;
218 nents = ssi_buffer_mgr_get_sgl_nents(sg, end, &lbytes, NULL);
219 sg_copy_buffer(sg, nents, (void *)dest, (end - to_skip), 0, (direct == SSI_SG_TO_BUF));
222 static inline int ssi_buffer_mgr_render_buff_to_mlli(
223 dma_addr_t buff_dma, uint32_t buff_size, uint32_t *curr_nents,
224 uint32_t **mlli_entry_pp)
226 uint32_t *mlli_entry_p = *mlli_entry_pp;
229 /* Verify there is no memory overflow*/
230 new_nents = (*curr_nents + buff_size/CC_MAX_MLLI_ENTRY_SIZE + 1);
231 if (new_nents > MAX_NUM_OF_TOTAL_MLLI_ENTRIES ) {
235 /*handle buffer longer than 64 kbytes */
236 while (buff_size > CC_MAX_MLLI_ENTRY_SIZE ) {
237 SSI_UPDATE_DMA_ADDR_TO_48BIT(buff_dma, CC_MAX_MLLI_ENTRY_SIZE);
238 LLI_SET_ADDR(mlli_entry_p,buff_dma);
239 LLI_SET_SIZE(mlli_entry_p, CC_MAX_MLLI_ENTRY_SIZE);
240 SSI_LOG_DEBUG("entry[%d]: single_buff=0x%08X size=%08X\n",*curr_nents,
241 mlli_entry_p[LLI_WORD0_OFFSET],
242 mlli_entry_p[LLI_WORD1_OFFSET]);
243 SSI_RESTORE_DMA_ADDR_TO_48BIT(buff_dma);
244 buff_dma += CC_MAX_MLLI_ENTRY_SIZE;
245 buff_size -= CC_MAX_MLLI_ENTRY_SIZE;
246 mlli_entry_p = mlli_entry_p + 2;
250 SSI_UPDATE_DMA_ADDR_TO_48BIT(buff_dma, buff_size);
251 LLI_SET_ADDR(mlli_entry_p,buff_dma);
252 LLI_SET_SIZE(mlli_entry_p, buff_size);
253 SSI_LOG_DEBUG("entry[%d]: single_buff=0x%08X size=%08X\n",*curr_nents,
254 mlli_entry_p[LLI_WORD0_OFFSET],
255 mlli_entry_p[LLI_WORD1_OFFSET]);
256 mlli_entry_p = mlli_entry_p + 2;
257 *mlli_entry_pp = mlli_entry_p;
263 static inline int ssi_buffer_mgr_render_scatterlist_to_mlli(
264 struct scatterlist *sgl, uint32_t sgl_data_len, uint32_t sglOffset, uint32_t *curr_nents,
265 uint32_t **mlli_entry_pp)
267 struct scatterlist *curr_sgl = sgl;
268 uint32_t *mlli_entry_p = *mlli_entry_pp;
271 for ( ; (curr_sgl != NULL) && (sgl_data_len != 0);
272 curr_sgl = sg_next(curr_sgl)) {
273 uint32_t entry_data_len =
274 (sgl_data_len > sg_dma_len(curr_sgl) - sglOffset) ?
275 sg_dma_len(curr_sgl) - sglOffset : sgl_data_len ;
276 sgl_data_len -= entry_data_len;
277 rc = ssi_buffer_mgr_render_buff_to_mlli(
278 sg_dma_address(curr_sgl) + sglOffset, entry_data_len, curr_nents,
285 *mlli_entry_pp = mlli_entry_p;
289 static int ssi_buffer_mgr_generate_mlli(
291 struct buffer_array *sg_data,
292 struct mlli_params *mlli_params)
295 uint32_t total_nents = 0,prev_total_nents = 0;
298 SSI_LOG_DEBUG("NUM of SG's = %d\n", sg_data->num_of_buffers);
300 /* Allocate memory from the pointed pool */
301 mlli_params->mlli_virt_addr = dma_pool_alloc(
302 mlli_params->curr_pool, GFP_KERNEL,
303 &(mlli_params->mlli_dma_addr));
304 if (unlikely(mlli_params->mlli_virt_addr == NULL)) {
305 SSI_LOG_ERR("dma_pool_alloc() failed\n");
307 goto build_mlli_exit;
309 SSI_UPDATE_DMA_ADDR_TO_48BIT(mlli_params->mlli_dma_addr,
310 (MAX_NUM_OF_TOTAL_MLLI_ENTRIES*
311 LLI_ENTRY_BYTE_SIZE));
312 /* Point to start of MLLI */
313 mlli_p = (uint32_t *)mlli_params->mlli_virt_addr;
314 /* go over all SG's and link it to one MLLI table */
315 for (i = 0; i < sg_data->num_of_buffers; i++) {
316 if (sg_data->type[i] == DMA_SGL_TYPE)
317 rc = ssi_buffer_mgr_render_scatterlist_to_mlli(
318 sg_data->entry[i].sgl,
319 sg_data->total_data_len[i], sg_data->offset[i], &total_nents,
321 else /*DMA_BUFF_TYPE*/
322 rc = ssi_buffer_mgr_render_buff_to_mlli(
323 sg_data->entry[i].buffer_dma,
324 sg_data->total_data_len[i], &total_nents,
330 /* set last bit in the current table */
331 if (sg_data->mlli_nents[i] != NULL) {
332 /*Calculate the current MLLI table length for the
333 length field in the descriptor*/
334 *(sg_data->mlli_nents[i]) +=
335 (total_nents - prev_total_nents);
336 prev_total_nents = total_nents;
340 /* Set MLLI size for the bypass operation */
341 mlli_params->mlli_len = (total_nents * LLI_ENTRY_BYTE_SIZE);
343 SSI_LOG_DEBUG("MLLI params: "
344 "virt_addr=%pK dma_addr=0x%llX mlli_len=0x%X\n",
345 mlli_params->mlli_virt_addr,
346 (unsigned long long)mlli_params->mlli_dma_addr,
347 mlli_params->mlli_len);
353 static inline void ssi_buffer_mgr_add_buffer_entry(
354 struct buffer_array *sgl_data,
355 dma_addr_t buffer_dma, unsigned int buffer_len,
356 bool is_last_entry, uint32_t *mlli_nents)
358 unsigned int index = sgl_data->num_of_buffers;
360 SSI_LOG_DEBUG("index=%u single_buff=0x%llX "
361 "buffer_len=0x%08X is_last=%d\n",
362 index, (unsigned long long)buffer_dma, buffer_len, is_last_entry);
363 sgl_data->nents[index] = 1;
364 sgl_data->entry[index].buffer_dma = buffer_dma;
365 sgl_data->offset[index] = 0;
366 sgl_data->total_data_len[index] = buffer_len;
367 sgl_data->type[index] = DMA_BUFF_TYPE;
368 sgl_data->is_last[index] = is_last_entry;
369 sgl_data->mlli_nents[index] = mlli_nents;
370 if (sgl_data->mlli_nents[index] != NULL)
371 *sgl_data->mlli_nents[index] = 0;
372 sgl_data->num_of_buffers++;
375 static inline void ssi_buffer_mgr_add_scatterlist_entry(
376 struct buffer_array *sgl_data,
378 struct scatterlist *sgl,
379 unsigned int data_len,
380 unsigned int data_offset,
382 uint32_t *mlli_nents)
384 unsigned int index = sgl_data->num_of_buffers;
386 SSI_LOG_DEBUG("index=%u nents=%u sgl=%pK data_len=0x%08X is_last=%d\n",
387 index, nents, sgl, data_len, is_last_table);
388 sgl_data->nents[index] = nents;
389 sgl_data->entry[index].sgl = sgl;
390 sgl_data->offset[index] = data_offset;
391 sgl_data->total_data_len[index] = data_len;
392 sgl_data->type[index] = DMA_SGL_TYPE;
393 sgl_data->is_last[index] = is_last_table;
394 sgl_data->mlli_nents[index] = mlli_nents;
395 if (sgl_data->mlli_nents[index] != NULL)
396 *sgl_data->mlli_nents[index] = 0;
397 sgl_data->num_of_buffers++;
401 ssi_buffer_mgr_dma_map_sg(struct device *dev, struct scatterlist *sg, uint32_t nents,
402 enum dma_data_direction direction)
405 struct scatterlist *l_sg = sg;
406 for (i = 0; i < nents; i++) {
410 if (unlikely(dma_map_sg(dev, l_sg, 1, direction) != 1)){
411 SSI_LOG_ERR("dma_map_page() sg buffer failed\n");
414 l_sg = sg_next(l_sg);
419 /* Restore mapped parts */
420 for (j = 0; j < i; j++) {
424 dma_unmap_sg(dev,sg,1,direction);
430 static int ssi_buffer_mgr_map_scatterlist(
431 struct device *dev, struct scatterlist *sg,
432 unsigned int nbytes, int direction,
433 uint32_t *nents, uint32_t max_sg_nents,
434 uint32_t *lbytes, uint32_t *mapped_nents)
436 bool is_chained = false;
438 if (sg_is_last(sg)) {
439 /* One entry only case -set to DLLI */
440 if (unlikely(dma_map_sg(dev, sg, 1, direction) != 1)) {
441 SSI_LOG_ERR("dma_map_sg() single buffer failed\n");
444 SSI_LOG_DEBUG("Mapped sg: dma_address=0x%llX "
445 "page_link=0x%08lX addr=%pK offset=%u "
447 (unsigned long long)sg_dma_address(sg),
450 sg->offset, sg->length);
454 SSI_UPDATE_DMA_ADDR_TO_48BIT(sg_dma_address(sg), sg_dma_len(sg));
455 } else { /*sg_is_last*/
456 *nents = ssi_buffer_mgr_get_sgl_nents(sg, nbytes, lbytes,
458 if (*nents > max_sg_nents) {
460 SSI_LOG_ERR("Too many fragments. current %d max %d\n",
461 *nents, max_sg_nents);
465 /* In case of mmu the number of mapped nents might
466 be changed from the original sgl nents */
467 *mapped_nents = dma_map_sg(dev, sg, *nents, direction);
468 if (unlikely(*mapped_nents == 0)){
470 SSI_LOG_ERR("dma_map_sg() sg buffer failed\n");
474 /*In this case the driver maps entry by entry so it
475 must have the same nents before and after map */
476 *mapped_nents = ssi_buffer_mgr_dma_map_sg(dev,
480 if (unlikely(*mapped_nents != *nents)){
481 *nents = *mapped_nents;
482 SSI_LOG_ERR("dma_map_sg() sg buffer failed\n");
492 ssi_aead_handle_config_buf(struct device *dev,
493 struct aead_req_ctx *areq_ctx,
494 uint8_t* config_data,
495 struct buffer_array *sg_data,
496 unsigned int assoclen)
498 SSI_LOG_DEBUG(" handle additional data config set to DLLI \n");
499 /* create sg for the current buffer */
500 sg_init_one(&areq_ctx->ccm_adata_sg, config_data, AES_BLOCK_SIZE + areq_ctx->ccm_hdr_size);
501 if (unlikely(dma_map_sg(dev, &areq_ctx->ccm_adata_sg, 1,
502 DMA_TO_DEVICE) != 1)) {
503 SSI_LOG_ERR("dma_map_sg() "
504 "config buffer failed\n");
507 SSI_LOG_DEBUG("Mapped curr_buff: dma_address=0x%llX "
508 "page_link=0x%08lX addr=%pK "
509 "offset=%u length=%u\n",
510 (unsigned long long)sg_dma_address(&areq_ctx->ccm_adata_sg),
511 areq_ctx->ccm_adata_sg.page_link,
512 sg_virt(&areq_ctx->ccm_adata_sg),
513 areq_ctx->ccm_adata_sg.offset,
514 areq_ctx->ccm_adata_sg.length);
515 /* prepare for case of MLLI */
517 ssi_buffer_mgr_add_scatterlist_entry(sg_data, 1,
518 &areq_ctx->ccm_adata_sg,
520 areq_ctx->ccm_hdr_size), 0,
527 static inline int ssi_ahash_handle_curr_buf(struct device *dev,
528 struct ahash_req_ctx *areq_ctx,
530 uint32_t curr_buff_cnt,
531 struct buffer_array *sg_data)
533 SSI_LOG_DEBUG(" handle curr buff %x set to DLLI \n", curr_buff_cnt);
534 /* create sg for the current buffer */
535 sg_init_one(areq_ctx->buff_sg,curr_buff, curr_buff_cnt);
536 if (unlikely(dma_map_sg(dev, areq_ctx->buff_sg, 1,
537 DMA_TO_DEVICE) != 1)) {
538 SSI_LOG_ERR("dma_map_sg() "
539 "src buffer failed\n");
542 SSI_LOG_DEBUG("Mapped curr_buff: dma_address=0x%llX "
543 "page_link=0x%08lX addr=%pK "
544 "offset=%u length=%u\n",
545 (unsigned long long)sg_dma_address(areq_ctx->buff_sg),
546 areq_ctx->buff_sg->page_link,
547 sg_virt(areq_ctx->buff_sg),
548 areq_ctx->buff_sg->offset,
549 areq_ctx->buff_sg->length);
550 areq_ctx->data_dma_buf_type = SSI_DMA_BUF_DLLI;
551 areq_ctx->curr_sg = areq_ctx->buff_sg;
552 areq_ctx->in_nents = 0;
553 /* prepare for case of MLLI */
554 ssi_buffer_mgr_add_scatterlist_entry(sg_data, 1, areq_ctx->buff_sg,
555 curr_buff_cnt, 0, false, NULL);
559 void ssi_buffer_mgr_unmap_blkcipher_request(
563 struct scatterlist *src,
564 struct scatterlist *dst)
566 struct blkcipher_req_ctx *req_ctx = (struct blkcipher_req_ctx *)ctx;
568 if (likely(req_ctx->gen_ctx.iv_dma_addr != 0)) {
569 SSI_LOG_DEBUG("Unmapped iv: iv_dma_addr=0x%llX iv_size=%u\n",
570 (unsigned long long)req_ctx->gen_ctx.iv_dma_addr,
572 SSI_RESTORE_DMA_ADDR_TO_48BIT(req_ctx->gen_ctx.iv_dma_addr);
573 dma_unmap_single(dev, req_ctx->gen_ctx.iv_dma_addr,
575 req_ctx->is_giv ? DMA_BIDIRECTIONAL :
579 if (req_ctx->dma_buf_type == SSI_DMA_BUF_MLLI) {
580 SSI_RESTORE_DMA_ADDR_TO_48BIT(req_ctx->mlli_params.mlli_dma_addr);
581 dma_pool_free(req_ctx->mlli_params.curr_pool,
582 req_ctx->mlli_params.mlli_virt_addr,
583 req_ctx->mlli_params.mlli_dma_addr);
586 SSI_RESTORE_DMA_ADDR_TO_48BIT(sg_dma_address(src));
587 dma_unmap_sg(dev, src, req_ctx->in_nents,
589 SSI_LOG_DEBUG("Unmapped req->src=%pK\n",
593 SSI_RESTORE_DMA_ADDR_TO_48BIT(sg_dma_address(dst));
594 dma_unmap_sg(dev, dst, req_ctx->out_nents,
596 SSI_LOG_DEBUG("Unmapped req->dst=%pK\n",
601 int ssi_buffer_mgr_map_blkcipher_request(
602 struct ssi_drvdata *drvdata,
607 struct scatterlist *src,
608 struct scatterlist *dst)
610 struct blkcipher_req_ctx *req_ctx = (struct blkcipher_req_ctx *)ctx;
611 struct mlli_params *mlli_params = &req_ctx->mlli_params;
612 struct buff_mgr_handle *buff_mgr = drvdata->buff_mgr_handle;
613 struct device *dev = &drvdata->plat_dev->dev;
614 struct buffer_array sg_data;
617 uint32_t mapped_nents = 0;
619 req_ctx->dma_buf_type = SSI_DMA_BUF_DLLI;
620 mlli_params->curr_pool = NULL;
621 sg_data.num_of_buffers = 0;
624 if (likely(ivsize != 0) ) {
625 dump_byte_array("iv", (uint8_t *)info, ivsize);
626 req_ctx->gen_ctx.iv_dma_addr =
627 dma_map_single(dev, (void *)info,
629 req_ctx->is_giv ? DMA_BIDIRECTIONAL:
631 if (unlikely(dma_mapping_error(dev,
632 req_ctx->gen_ctx.iv_dma_addr))) {
633 SSI_LOG_ERR("Mapping iv %u B at va=%pK "
634 "for DMA failed\n", ivsize, info);
637 SSI_UPDATE_DMA_ADDR_TO_48BIT(req_ctx->gen_ctx.iv_dma_addr,
639 SSI_LOG_DEBUG("Mapped iv %u B at va=%pK to dma=0x%llX\n",
641 (unsigned long long)req_ctx->gen_ctx.iv_dma_addr);
643 req_ctx->gen_ctx.iv_dma_addr = 0;
645 /* Map the src SGL */
646 rc = ssi_buffer_mgr_map_scatterlist(dev, src,
647 nbytes, DMA_BIDIRECTIONAL, &req_ctx->in_nents,
648 LLI_MAX_NUM_OF_DATA_ENTRIES, &dummy, &mapped_nents);
649 if (unlikely(rc != 0)) {
651 goto ablkcipher_exit;
653 if (mapped_nents > 1)
654 req_ctx->dma_buf_type = SSI_DMA_BUF_MLLI;
656 if (unlikely(src == dst)) {
657 /* Handle inplace operation */
658 if (unlikely(req_ctx->dma_buf_type == SSI_DMA_BUF_MLLI)) {
659 req_ctx->out_nents = 0;
660 ssi_buffer_mgr_add_scatterlist_entry(&sg_data,
661 req_ctx->in_nents, src,
662 nbytes, 0, true, &req_ctx->in_mlli_nents);
666 if (unlikely(ssi_buffer_mgr_map_scatterlist(
668 DMA_BIDIRECTIONAL, &req_ctx->out_nents,
669 LLI_MAX_NUM_OF_DATA_ENTRIES, &dummy,
672 goto ablkcipher_exit;
674 if (mapped_nents > 1)
675 req_ctx->dma_buf_type = SSI_DMA_BUF_MLLI;
677 if (unlikely((req_ctx->dma_buf_type == SSI_DMA_BUF_MLLI))) {
678 ssi_buffer_mgr_add_scatterlist_entry(&sg_data,
679 req_ctx->in_nents, src,
681 &req_ctx->in_mlli_nents);
682 ssi_buffer_mgr_add_scatterlist_entry(&sg_data,
683 req_ctx->out_nents, dst,
685 &req_ctx->out_mlli_nents);
689 if (unlikely(req_ctx->dma_buf_type == SSI_DMA_BUF_MLLI)) {
690 mlli_params->curr_pool = buff_mgr->mlli_buffs_pool;
691 rc = ssi_buffer_mgr_generate_mlli(dev, &sg_data, mlli_params);
692 if (unlikely(rc!= 0))
693 goto ablkcipher_exit;
697 SSI_LOG_DEBUG("areq_ctx->dma_buf_type = %s\n",
698 GET_DMA_BUFFER_TYPE(req_ctx->dma_buf_type));
703 ssi_buffer_mgr_unmap_blkcipher_request(dev, req_ctx, ivsize, src, dst);
707 void ssi_buffer_mgr_unmap_aead_request(
708 struct device *dev, struct aead_request *req)
710 struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
711 unsigned int hw_iv_size = areq_ctx->hw_iv_size;
712 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
715 uint32_t size_to_unmap = 0;
717 if (areq_ctx->mac_buf_dma_addr != 0) {
718 SSI_RESTORE_DMA_ADDR_TO_48BIT(areq_ctx->mac_buf_dma_addr);
719 dma_unmap_single(dev, areq_ctx->mac_buf_dma_addr,
720 MAX_MAC_SIZE, DMA_BIDIRECTIONAL);
723 #if SSI_CC_HAS_AES_GCM
724 if (areq_ctx->cipher_mode == DRV_CIPHER_GCTR) {
725 if (areq_ctx->hkey_dma_addr != 0) {
726 SSI_RESTORE_DMA_ADDR_TO_48BIT(areq_ctx->hkey_dma_addr);
727 dma_unmap_single(dev, areq_ctx->hkey_dma_addr,
728 AES_BLOCK_SIZE, DMA_BIDIRECTIONAL);
731 if (areq_ctx->gcm_block_len_dma_addr != 0) {
732 SSI_RESTORE_DMA_ADDR_TO_48BIT(areq_ctx->gcm_block_len_dma_addr);
733 dma_unmap_single(dev, areq_ctx->gcm_block_len_dma_addr,
734 AES_BLOCK_SIZE, DMA_TO_DEVICE);
737 if (areq_ctx->gcm_iv_inc1_dma_addr != 0) {
738 SSI_RESTORE_DMA_ADDR_TO_48BIT(areq_ctx->gcm_iv_inc1_dma_addr);
739 dma_unmap_single(dev, areq_ctx->gcm_iv_inc1_dma_addr,
740 AES_BLOCK_SIZE, DMA_TO_DEVICE);
743 if (areq_ctx->gcm_iv_inc2_dma_addr != 0) {
744 SSI_RESTORE_DMA_ADDR_TO_48BIT(areq_ctx->gcm_iv_inc2_dma_addr);
745 dma_unmap_single(dev, areq_ctx->gcm_iv_inc2_dma_addr,
746 AES_BLOCK_SIZE, DMA_TO_DEVICE);
751 if (areq_ctx->ccm_hdr_size != ccm_header_size_null) {
752 if (areq_ctx->ccm_iv0_dma_addr != 0) {
753 SSI_RESTORE_DMA_ADDR_TO_48BIT(areq_ctx->ccm_iv0_dma_addr);
754 dma_unmap_single(dev, areq_ctx->ccm_iv0_dma_addr,
755 AES_BLOCK_SIZE, DMA_TO_DEVICE);
758 if (&areq_ctx->ccm_adata_sg != NULL)
759 dma_unmap_sg(dev, &areq_ctx->ccm_adata_sg,
762 if (areq_ctx->gen_ctx.iv_dma_addr != 0) {
763 SSI_RESTORE_DMA_ADDR_TO_48BIT(areq_ctx->gen_ctx.iv_dma_addr);
764 dma_unmap_single(dev, areq_ctx->gen_ctx.iv_dma_addr,
765 hw_iv_size, DMA_BIDIRECTIONAL);
768 /*In case a pool was set, a table was
769 allocated and should be released */
770 if (areq_ctx->mlli_params.curr_pool != NULL) {
771 SSI_LOG_DEBUG("free MLLI buffer: dma=0x%08llX virt=%pK\n",
772 (unsigned long long)areq_ctx->mlli_params.mlli_dma_addr,
773 areq_ctx->mlli_params.mlli_virt_addr);
774 SSI_RESTORE_DMA_ADDR_TO_48BIT(areq_ctx->mlli_params.mlli_dma_addr);
775 dma_pool_free(areq_ctx->mlli_params.curr_pool,
776 areq_ctx->mlli_params.mlli_virt_addr,
777 areq_ctx->mlli_params.mlli_dma_addr);
780 SSI_LOG_DEBUG("Unmapping src sgl: req->src=%pK areq_ctx->src.nents=%u areq_ctx->assoc.nents=%u assoclen:%u cryptlen=%u\n", sg_virt(req->src),areq_ctx->src.nents,areq_ctx->assoc.nents,req->assoclen,req->cryptlen);
781 SSI_RESTORE_DMA_ADDR_TO_48BIT(sg_dma_address(req->src));
782 size_to_unmap = req->assoclen+req->cryptlen;
783 if(areq_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_ENCRYPT){
784 size_to_unmap += areq_ctx->req_authsize;
786 if (areq_ctx->is_gcm4543)
787 size_to_unmap += crypto_aead_ivsize(tfm);
789 dma_unmap_sg(dev, req->src, ssi_buffer_mgr_get_sgl_nents(req->src,size_to_unmap,&dummy,&chained) , DMA_BIDIRECTIONAL);
790 if (unlikely(req->src != req->dst)) {
791 SSI_LOG_DEBUG("Unmapping dst sgl: req->dst=%pK\n",
793 SSI_RESTORE_DMA_ADDR_TO_48BIT(sg_dma_address(req->dst));
794 dma_unmap_sg(dev, req->dst, ssi_buffer_mgr_get_sgl_nents(req->dst,size_to_unmap,&dummy,&chained),
798 if ((areq_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_DECRYPT) &&
799 likely(req->src == req->dst))
801 uint32_t size_to_skip = req->assoclen;
802 if (areq_ctx->is_gcm4543) {
803 size_to_skip += crypto_aead_ivsize(tfm);
805 /* copy mac to a temporary location to deal with possible
806 data memory overriding that caused by cache coherence problem. */
807 ssi_buffer_mgr_copy_scatterlist_portion(
808 areq_ctx->backup_mac, req->src,
809 size_to_skip+ req->cryptlen - areq_ctx->req_authsize,
810 size_to_skip+ req->cryptlen, SSI_SG_FROM_BUF);
815 static inline int ssi_buffer_mgr_get_aead_icv_nents(
816 struct scatterlist *sgl,
817 unsigned int sgl_nents,
818 unsigned int authsize,
819 uint32_t last_entry_data_size,
820 bool *is_icv_fragmented)
822 unsigned int icv_max_size = 0;
823 unsigned int icv_required_size = authsize > last_entry_data_size ? (authsize - last_entry_data_size) : authsize;
827 if (sgl_nents < MAX_ICV_NENTS_SUPPORTED) {
828 *is_icv_fragmented = false;
832 for( i = 0 ; i < (sgl_nents - MAX_ICV_NENTS_SUPPORTED) ; i++) {
840 icv_max_size = sgl->length;
843 if (last_entry_data_size > authsize) {
844 nents = 0; /* ICV attached to data in last entry (not fragmented!) */
845 *is_icv_fragmented = false;
846 } else if (last_entry_data_size == authsize) {
847 nents = 1; /* ICV placed in whole last entry (not fragmented!) */
848 *is_icv_fragmented = false;
849 } else if (icv_max_size > icv_required_size) {
851 *is_icv_fragmented = true;
852 } else if (icv_max_size == icv_required_size) {
854 *is_icv_fragmented = true;
856 SSI_LOG_ERR("Unsupported num. of ICV fragments (> %d)\n",
857 MAX_ICV_NENTS_SUPPORTED);
858 nents = -1; /*unsupported*/
860 SSI_LOG_DEBUG("is_frag=%s icv_nents=%u\n",
861 (*is_icv_fragmented ? "true" : "false"), nents);
866 static inline int ssi_buffer_mgr_aead_chain_iv(
867 struct ssi_drvdata *drvdata,
868 struct aead_request *req,
869 struct buffer_array *sg_data,
870 bool is_last, bool do_chain)
872 struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
873 unsigned int hw_iv_size = areq_ctx->hw_iv_size;
874 struct device *dev = &drvdata->plat_dev->dev;
877 if (unlikely(req->iv == NULL)) {
878 areq_ctx->gen_ctx.iv_dma_addr = 0;
882 areq_ctx->gen_ctx.iv_dma_addr = dma_map_single(dev, req->iv,
883 hw_iv_size, DMA_BIDIRECTIONAL);
884 if (unlikely(dma_mapping_error(dev, areq_ctx->gen_ctx.iv_dma_addr))) {
885 SSI_LOG_ERR("Mapping iv %u B at va=%pK for DMA failed\n",
886 hw_iv_size, req->iv);
890 SSI_UPDATE_DMA_ADDR_TO_48BIT(areq_ctx->gen_ctx.iv_dma_addr, hw_iv_size);
892 SSI_LOG_DEBUG("Mapped iv %u B at va=%pK to dma=0x%llX\n",
894 (unsigned long long)areq_ctx->gen_ctx.iv_dma_addr);
895 if (do_chain == true && areq_ctx->plaintext_authenticate_only == true){ // TODO: what about CTR?? ask Ron
896 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
897 unsigned int iv_size_to_authenc = crypto_aead_ivsize(tfm);
898 unsigned int iv_ofs = GCM_BLOCK_RFC4_IV_OFFSET;
899 /* Chain to given list */
900 ssi_buffer_mgr_add_buffer_entry(
901 sg_data, areq_ctx->gen_ctx.iv_dma_addr + iv_ofs,
902 iv_size_to_authenc, is_last,
903 &areq_ctx->assoc.mlli_nents);
904 areq_ctx->assoc_buff_type = SSI_DMA_BUF_MLLI;
911 static inline int ssi_buffer_mgr_aead_chain_assoc(
912 struct ssi_drvdata *drvdata,
913 struct aead_request *req,
914 struct buffer_array *sg_data,
915 bool is_last, bool do_chain)
917 struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
919 uint32_t mapped_nents = 0;
920 struct scatterlist *current_sg = req->src;
921 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
922 unsigned int sg_index = 0;
923 uint32_t size_of_assoc = req->assoclen;
925 if (areq_ctx->is_gcm4543) {
926 size_of_assoc += crypto_aead_ivsize(tfm);
929 if (sg_data == NULL) {
931 goto chain_assoc_exit;
934 if (unlikely(req->assoclen == 0)) {
935 areq_ctx->assoc_buff_type = SSI_DMA_BUF_NULL;
936 areq_ctx->assoc.nents = 0;
937 areq_ctx->assoc.mlli_nents = 0;
938 SSI_LOG_DEBUG("Chain assoc of length 0: buff_type=%s nents=%u\n",
939 GET_DMA_BUFFER_TYPE(areq_ctx->assoc_buff_type),
940 areq_ctx->assoc.nents);
941 goto chain_assoc_exit;
944 //iterate over the sgl to see how many entries are for associated data
945 //it is assumed that if we reach here , the sgl is already mapped
946 sg_index = current_sg->length;
947 if (sg_index > size_of_assoc) { //the first entry in the scatter list contains all the associated data
951 while (sg_index <= size_of_assoc) {
952 current_sg = sg_next(current_sg);
953 //if have reached the end of the sgl, then this is unexpected
954 if (current_sg == NULL) {
955 SSI_LOG_ERR("reached end of sg list. unexpected \n");
958 sg_index += current_sg->length;
962 if (unlikely(mapped_nents > LLI_MAX_NUM_OF_ASSOC_DATA_ENTRIES)) {
963 SSI_LOG_ERR("Too many fragments. current %d max %d\n",
964 mapped_nents, LLI_MAX_NUM_OF_ASSOC_DATA_ENTRIES);
967 areq_ctx->assoc.nents = mapped_nents;
969 /* in CCM case we have additional entry for
970 * ccm header configurations */
971 if (areq_ctx->ccm_hdr_size != ccm_header_size_null) {
972 if (unlikely((mapped_nents + 1) >
973 LLI_MAX_NUM_OF_ASSOC_DATA_ENTRIES)) {
975 SSI_LOG_ERR("CCM case.Too many fragments. "
976 "Current %d max %d\n",
977 (areq_ctx->assoc.nents + 1),
978 LLI_MAX_NUM_OF_ASSOC_DATA_ENTRIES);
980 goto chain_assoc_exit;
984 if (likely(mapped_nents == 1) &&
985 (areq_ctx->ccm_hdr_size == ccm_header_size_null))
986 areq_ctx->assoc_buff_type = SSI_DMA_BUF_DLLI;
988 areq_ctx->assoc_buff_type = SSI_DMA_BUF_MLLI;
990 if (unlikely((do_chain == true) ||
991 (areq_ctx->assoc_buff_type == SSI_DMA_BUF_MLLI))) {
993 SSI_LOG_DEBUG("Chain assoc: buff_type=%s nents=%u\n",
994 GET_DMA_BUFFER_TYPE(areq_ctx->assoc_buff_type),
995 areq_ctx->assoc.nents);
996 ssi_buffer_mgr_add_scatterlist_entry(
997 sg_data, areq_ctx->assoc.nents,
998 req->src, req->assoclen, 0, is_last,
999 &areq_ctx->assoc.mlli_nents);
1000 areq_ctx->assoc_buff_type = SSI_DMA_BUF_MLLI;
1007 static inline void ssi_buffer_mgr_prepare_aead_data_dlli(
1008 struct aead_request *req,
1009 uint32_t *src_last_bytes, uint32_t *dst_last_bytes)
1011 struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
1012 enum drv_crypto_direction direct = areq_ctx->gen_ctx.op_type;
1013 unsigned int authsize = areq_ctx->req_authsize;
1015 areq_ctx->is_icv_fragmented = false;
1016 if (likely(req->src == req->dst)) {
1018 areq_ctx->icv_dma_addr = sg_dma_address(
1020 (*src_last_bytes - authsize);
1021 areq_ctx->icv_virt_addr = sg_virt(
1023 (*src_last_bytes - authsize);
1024 } else if (direct == DRV_CRYPTO_DIRECTION_DECRYPT) {
1025 /*NON-INPLACE and DECRYPT*/
1026 areq_ctx->icv_dma_addr = sg_dma_address(
1028 (*src_last_bytes - authsize);
1029 areq_ctx->icv_virt_addr = sg_virt(
1031 (*src_last_bytes - authsize);
1033 /*NON-INPLACE and ENCRYPT*/
1034 areq_ctx->icv_dma_addr = sg_dma_address(
1036 (*dst_last_bytes - authsize);
1037 areq_ctx->icv_virt_addr = sg_virt(
1039 (*dst_last_bytes - authsize);
1043 static inline int ssi_buffer_mgr_prepare_aead_data_mlli(
1044 struct ssi_drvdata *drvdata,
1045 struct aead_request *req,
1046 struct buffer_array *sg_data,
1047 uint32_t *src_last_bytes, uint32_t *dst_last_bytes,
1050 struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
1051 enum drv_crypto_direction direct = areq_ctx->gen_ctx.op_type;
1052 unsigned int authsize = areq_ctx->req_authsize;
1053 int rc = 0, icv_nents;
1054 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1056 if (likely(req->src == req->dst)) {
1058 ssi_buffer_mgr_add_scatterlist_entry(sg_data,
1059 areq_ctx->src.nents, areq_ctx->srcSgl,
1060 areq_ctx->cryptlen,areq_ctx->srcOffset, is_last_table,
1061 &areq_ctx->src.mlli_nents);
1063 icv_nents = ssi_buffer_mgr_get_aead_icv_nents(areq_ctx->srcSgl,
1064 areq_ctx->src.nents, authsize, *src_last_bytes,
1065 &areq_ctx->is_icv_fragmented);
1066 if (unlikely(icv_nents < 0)) {
1068 goto prepare_data_mlli_exit;
1071 if (unlikely(areq_ctx->is_icv_fragmented == true)) {
1072 /* Backup happens only when ICV is fragmented, ICV
1073 verification is made by CPU compare in order to simplify
1074 MAC verification upon request completion */
1075 if (direct == DRV_CRYPTO_DIRECTION_DECRYPT) {
1077 /* In ACP platform we already copying ICV
1078 for any INPLACE-DECRYPT operation, hence
1079 we must neglect this code. */
1080 uint32_t size_to_skip = req->assoclen;
1081 if (areq_ctx->is_gcm4543) {
1082 size_to_skip += crypto_aead_ivsize(tfm);
1084 ssi_buffer_mgr_copy_scatterlist_portion(
1085 areq_ctx->backup_mac, req->src,
1086 size_to_skip+ req->cryptlen - areq_ctx->req_authsize,
1087 size_to_skip+ req->cryptlen, SSI_SG_TO_BUF);
1089 areq_ctx->icv_virt_addr = areq_ctx->backup_mac;
1091 areq_ctx->icv_virt_addr = areq_ctx->mac_buf;
1092 areq_ctx->icv_dma_addr = areq_ctx->mac_buf_dma_addr;
1094 } else { /* Contig. ICV */
1095 /*Should hanlde if the sg is not contig.*/
1096 areq_ctx->icv_dma_addr = sg_dma_address(
1097 &areq_ctx->srcSgl[areq_ctx->src.nents - 1]) +
1098 (*src_last_bytes - authsize);
1099 areq_ctx->icv_virt_addr = sg_virt(
1100 &areq_ctx->srcSgl[areq_ctx->src.nents - 1]) +
1101 (*src_last_bytes - authsize);
1104 } else if (direct == DRV_CRYPTO_DIRECTION_DECRYPT) {
1105 /*NON-INPLACE and DECRYPT*/
1106 ssi_buffer_mgr_add_scatterlist_entry(sg_data,
1107 areq_ctx->src.nents, areq_ctx->srcSgl,
1108 areq_ctx->cryptlen, areq_ctx->srcOffset,is_last_table,
1109 &areq_ctx->src.mlli_nents);
1110 ssi_buffer_mgr_add_scatterlist_entry(sg_data,
1111 areq_ctx->dst.nents, areq_ctx->dstSgl,
1112 areq_ctx->cryptlen,areq_ctx->dstOffset, is_last_table,
1113 &areq_ctx->dst.mlli_nents);
1115 icv_nents = ssi_buffer_mgr_get_aead_icv_nents(areq_ctx->srcSgl,
1116 areq_ctx->src.nents, authsize, *src_last_bytes,
1117 &areq_ctx->is_icv_fragmented);
1118 if (unlikely(icv_nents < 0)) {
1120 goto prepare_data_mlli_exit;
1123 if (unlikely(areq_ctx->is_icv_fragmented == true)) {
1124 /* Backup happens only when ICV is fragmented, ICV
1125 verification is made by CPU compare in order to simplify
1126 MAC verification upon request completion */
1127 uint32_t size_to_skip = req->assoclen;
1128 if (areq_ctx->is_gcm4543) {
1129 size_to_skip += crypto_aead_ivsize(tfm);
1131 ssi_buffer_mgr_copy_scatterlist_portion(
1132 areq_ctx->backup_mac, req->src,
1133 size_to_skip+ req->cryptlen - areq_ctx->req_authsize,
1134 size_to_skip+ req->cryptlen, SSI_SG_TO_BUF);
1135 areq_ctx->icv_virt_addr = areq_ctx->backup_mac;
1136 } else { /* Contig. ICV */
1137 /*Should hanlde if the sg is not contig.*/
1138 areq_ctx->icv_dma_addr = sg_dma_address(
1139 &areq_ctx->srcSgl[areq_ctx->src.nents - 1]) +
1140 (*src_last_bytes - authsize);
1141 areq_ctx->icv_virt_addr = sg_virt(
1142 &areq_ctx->srcSgl[areq_ctx->src.nents - 1]) +
1143 (*src_last_bytes - authsize);
1147 /*NON-INPLACE and ENCRYPT*/
1148 ssi_buffer_mgr_add_scatterlist_entry(sg_data,
1149 areq_ctx->dst.nents, areq_ctx->dstSgl,
1150 areq_ctx->cryptlen,areq_ctx->dstOffset, is_last_table,
1151 &areq_ctx->dst.mlli_nents);
1152 ssi_buffer_mgr_add_scatterlist_entry(sg_data,
1153 areq_ctx->src.nents, areq_ctx->srcSgl,
1154 areq_ctx->cryptlen, areq_ctx->srcOffset,is_last_table,
1155 &areq_ctx->src.mlli_nents);
1157 icv_nents = ssi_buffer_mgr_get_aead_icv_nents(areq_ctx->dstSgl,
1158 areq_ctx->dst.nents, authsize, *dst_last_bytes,
1159 &areq_ctx->is_icv_fragmented);
1160 if (unlikely(icv_nents < 0)) {
1162 goto prepare_data_mlli_exit;
1165 if (likely(areq_ctx->is_icv_fragmented == false)) {
1167 areq_ctx->icv_dma_addr = sg_dma_address(
1168 &areq_ctx->dstSgl[areq_ctx->dst.nents - 1]) +
1169 (*dst_last_bytes - authsize);
1170 areq_ctx->icv_virt_addr = sg_virt(
1171 &areq_ctx->dstSgl[areq_ctx->dst.nents - 1]) +
1172 (*dst_last_bytes - authsize);
1174 areq_ctx->icv_dma_addr = areq_ctx->mac_buf_dma_addr;
1175 areq_ctx->icv_virt_addr = areq_ctx->mac_buf;
1179 prepare_data_mlli_exit:
1183 static inline int ssi_buffer_mgr_aead_chain_data(
1184 struct ssi_drvdata *drvdata,
1185 struct aead_request *req,
1186 struct buffer_array *sg_data,
1187 bool is_last_table, bool do_chain)
1189 struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
1190 struct device *dev = &drvdata->plat_dev->dev;
1191 enum drv_crypto_direction direct = areq_ctx->gen_ctx.op_type;
1192 unsigned int authsize = areq_ctx->req_authsize;
1193 int src_last_bytes = 0, dst_last_bytes = 0;
1195 uint32_t src_mapped_nents = 0, dst_mapped_nents = 0;
1196 uint32_t offset = 0;
1197 unsigned int size_for_map = req->assoclen +req->cryptlen; /*non-inplace mode*/
1198 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1199 uint32_t sg_index = 0;
1200 bool chained = false;
1201 bool is_gcm4543 = areq_ctx->is_gcm4543;
1202 uint32_t size_to_skip = req->assoclen;
1204 size_to_skip += crypto_aead_ivsize(tfm);
1206 offset = size_to_skip;
1208 if (sg_data == NULL) {
1210 goto chain_data_exit;
1212 areq_ctx->srcSgl = req->src;
1213 areq_ctx->dstSgl = req->dst;
1216 size_for_map += crypto_aead_ivsize(tfm);
1219 size_for_map += (direct == DRV_CRYPTO_DIRECTION_ENCRYPT) ? authsize:0;
1220 src_mapped_nents = ssi_buffer_mgr_get_sgl_nents(req->src,size_for_map,&src_last_bytes, &chained);
1221 sg_index = areq_ctx->srcSgl->length;
1222 //check where the data starts
1223 while (sg_index <= size_to_skip) {
1224 offset -= areq_ctx->srcSgl->length;
1225 areq_ctx->srcSgl = sg_next(areq_ctx->srcSgl);
1226 //if have reached the end of the sgl, then this is unexpected
1227 if (areq_ctx->srcSgl == NULL) {
1228 SSI_LOG_ERR("reached end of sg list. unexpected \n");
1231 sg_index += areq_ctx->srcSgl->length;
1234 if (unlikely(src_mapped_nents > LLI_MAX_NUM_OF_DATA_ENTRIES))
1236 SSI_LOG_ERR("Too many fragments. current %d max %d\n",
1237 src_mapped_nents, LLI_MAX_NUM_OF_DATA_ENTRIES);
1241 areq_ctx->src.nents = src_mapped_nents;
1243 areq_ctx->srcOffset = offset;
1245 if (req->src != req->dst) {
1246 size_for_map = req->assoclen +req->cryptlen;
1247 size_for_map += (direct == DRV_CRYPTO_DIRECTION_ENCRYPT) ? authsize : 0;
1249 size_for_map += crypto_aead_ivsize(tfm);
1252 rc = ssi_buffer_mgr_map_scatterlist(dev, req->dst, size_for_map,
1253 DMA_BIDIRECTIONAL, &(areq_ctx->dst.nents),
1254 LLI_MAX_NUM_OF_DATA_ENTRIES, &dst_last_bytes,
1256 if (unlikely(rc != 0)) {
1258 goto chain_data_exit;
1262 dst_mapped_nents = ssi_buffer_mgr_get_sgl_nents(req->dst,size_for_map,&dst_last_bytes, &chained);
1263 sg_index = areq_ctx->dstSgl->length;
1264 offset = size_to_skip;
1266 //check where the data starts
1267 while (sg_index <= size_to_skip) {
1269 offset -= areq_ctx->dstSgl->length;
1270 areq_ctx->dstSgl = sg_next(areq_ctx->dstSgl);
1271 //if have reached the end of the sgl, then this is unexpected
1272 if (areq_ctx->dstSgl == NULL) {
1273 SSI_LOG_ERR("reached end of sg list. unexpected \n");
1276 sg_index += areq_ctx->dstSgl->length;
1279 if (unlikely(dst_mapped_nents > LLI_MAX_NUM_OF_DATA_ENTRIES))
1281 SSI_LOG_ERR("Too many fragments. current %d max %d\n",
1282 dst_mapped_nents, LLI_MAX_NUM_OF_DATA_ENTRIES);
1285 areq_ctx->dst.nents = dst_mapped_nents;
1286 areq_ctx->dstOffset = offset;
1287 if ((src_mapped_nents > 1) ||
1288 (dst_mapped_nents > 1) ||
1289 (do_chain == true)) {
1290 areq_ctx->data_buff_type = SSI_DMA_BUF_MLLI;
1291 rc = ssi_buffer_mgr_prepare_aead_data_mlli(drvdata, req, sg_data,
1292 &src_last_bytes, &dst_last_bytes, is_last_table);
1294 areq_ctx->data_buff_type = SSI_DMA_BUF_DLLI;
1295 ssi_buffer_mgr_prepare_aead_data_dlli(
1296 req, &src_last_bytes, &dst_last_bytes);
1303 static void ssi_buffer_mgr_update_aead_mlli_nents( struct ssi_drvdata *drvdata,
1304 struct aead_request *req)
1306 struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
1307 uint32_t curr_mlli_size = 0;
1309 if (areq_ctx->assoc_buff_type == SSI_DMA_BUF_MLLI) {
1310 areq_ctx->assoc.sram_addr = drvdata->mlli_sram_addr;
1311 curr_mlli_size = areq_ctx->assoc.mlli_nents *
1312 LLI_ENTRY_BYTE_SIZE;
1315 if (areq_ctx->data_buff_type == SSI_DMA_BUF_MLLI) {
1316 /*Inplace case dst nents equal to src nents*/
1317 if (req->src == req->dst) {
1318 areq_ctx->dst.mlli_nents = areq_ctx->src.mlli_nents;
1319 areq_ctx->src.sram_addr = drvdata->mlli_sram_addr +
1321 areq_ctx->dst.sram_addr = areq_ctx->src.sram_addr;
1322 if (areq_ctx->is_single_pass == false)
1323 areq_ctx->assoc.mlli_nents +=
1324 areq_ctx->src.mlli_nents;
1326 if (areq_ctx->gen_ctx.op_type ==
1327 DRV_CRYPTO_DIRECTION_DECRYPT) {
1328 areq_ctx->src.sram_addr =
1329 drvdata->mlli_sram_addr +
1331 areq_ctx->dst.sram_addr =
1332 areq_ctx->src.sram_addr +
1333 areq_ctx->src.mlli_nents *
1334 LLI_ENTRY_BYTE_SIZE;
1335 if (areq_ctx->is_single_pass == false)
1336 areq_ctx->assoc.mlli_nents +=
1337 areq_ctx->src.mlli_nents;
1339 areq_ctx->dst.sram_addr =
1340 drvdata->mlli_sram_addr +
1342 areq_ctx->src.sram_addr =
1343 areq_ctx->dst.sram_addr +
1344 areq_ctx->dst.mlli_nents *
1345 LLI_ENTRY_BYTE_SIZE;
1346 if (areq_ctx->is_single_pass == false)
1347 areq_ctx->assoc.mlli_nents +=
1348 areq_ctx->dst.mlli_nents;
1354 int ssi_buffer_mgr_map_aead_request(
1355 struct ssi_drvdata *drvdata, struct aead_request *req)
1357 struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
1358 struct mlli_params *mlli_params = &areq_ctx->mlli_params;
1359 struct device *dev = &drvdata->plat_dev->dev;
1360 struct buffer_array sg_data;
1361 unsigned int authsize = areq_ctx->req_authsize;
1362 struct buff_mgr_handle *buff_mgr = drvdata->buff_mgr_handle;
1364 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1365 bool is_gcm4543 = areq_ctx->is_gcm4543;
1367 uint32_t mapped_nents = 0;
1368 uint32_t dummy = 0; /*used for the assoc data fragments */
1369 uint32_t size_to_map = 0;
1371 mlli_params->curr_pool = NULL;
1372 sg_data.num_of_buffers = 0;
1375 if ((areq_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_DECRYPT) &&
1376 likely(req->src == req->dst))
1378 uint32_t size_to_skip = req->assoclen;
1380 size_to_skip += crypto_aead_ivsize(tfm);
1382 /* copy mac to a temporary location to deal with possible
1383 data memory overriding that caused by cache coherence problem. */
1384 ssi_buffer_mgr_copy_scatterlist_portion(
1385 areq_ctx->backup_mac, req->src,
1386 size_to_skip+ req->cryptlen - areq_ctx->req_authsize,
1387 size_to_skip+ req->cryptlen, SSI_SG_TO_BUF);
1391 /* cacluate the size for cipher remove ICV in decrypt*/
1392 areq_ctx->cryptlen = (areq_ctx->gen_ctx.op_type ==
1393 DRV_CRYPTO_DIRECTION_ENCRYPT) ?
1395 (req->cryptlen - authsize);
1397 areq_ctx->mac_buf_dma_addr = dma_map_single(dev,
1398 areq_ctx->mac_buf, MAX_MAC_SIZE, DMA_BIDIRECTIONAL);
1399 if (unlikely(dma_mapping_error(dev, areq_ctx->mac_buf_dma_addr))) {
1400 SSI_LOG_ERR("Mapping mac_buf %u B at va=%pK for DMA failed\n",
1401 MAX_MAC_SIZE, areq_ctx->mac_buf);
1403 goto aead_map_failure;
1405 SSI_UPDATE_DMA_ADDR_TO_48BIT(areq_ctx->mac_buf_dma_addr, MAX_MAC_SIZE);
1407 if (areq_ctx->ccm_hdr_size != ccm_header_size_null) {
1408 areq_ctx->ccm_iv0_dma_addr = dma_map_single(dev,
1409 (areq_ctx->ccm_config + CCM_CTR_COUNT_0_OFFSET),
1410 AES_BLOCK_SIZE, DMA_TO_DEVICE);
1412 if (unlikely(dma_mapping_error(dev, areq_ctx->ccm_iv0_dma_addr))) {
1413 SSI_LOG_ERR("Mapping mac_buf %u B at va=%pK "
1414 "for DMA failed\n", AES_BLOCK_SIZE,
1415 (areq_ctx->ccm_config + CCM_CTR_COUNT_0_OFFSET));
1416 areq_ctx->ccm_iv0_dma_addr = 0;
1418 goto aead_map_failure;
1420 SSI_UPDATE_DMA_ADDR_TO_48BIT(areq_ctx->ccm_iv0_dma_addr,
1422 if (ssi_aead_handle_config_buf(dev, areq_ctx,
1423 areq_ctx->ccm_config, &sg_data, req->assoclen) != 0) {
1425 goto aead_map_failure;
1429 #if SSI_CC_HAS_AES_GCM
1430 if (areq_ctx->cipher_mode == DRV_CIPHER_GCTR) {
1431 areq_ctx->hkey_dma_addr = dma_map_single(dev,
1432 areq_ctx->hkey, AES_BLOCK_SIZE, DMA_BIDIRECTIONAL);
1433 if (unlikely(dma_mapping_error(dev, areq_ctx->hkey_dma_addr))) {
1434 SSI_LOG_ERR("Mapping hkey %u B at va=%pK for DMA failed\n",
1435 AES_BLOCK_SIZE, areq_ctx->hkey);
1437 goto aead_map_failure;
1439 SSI_UPDATE_DMA_ADDR_TO_48BIT(areq_ctx->hkey_dma_addr, AES_BLOCK_SIZE);
1441 areq_ctx->gcm_block_len_dma_addr = dma_map_single(dev,
1442 &areq_ctx->gcm_len_block, AES_BLOCK_SIZE, DMA_TO_DEVICE);
1443 if (unlikely(dma_mapping_error(dev, areq_ctx->gcm_block_len_dma_addr))) {
1444 SSI_LOG_ERR("Mapping gcm_len_block %u B at va=%pK for DMA failed\n",
1445 AES_BLOCK_SIZE, &areq_ctx->gcm_len_block);
1447 goto aead_map_failure;
1449 SSI_UPDATE_DMA_ADDR_TO_48BIT(areq_ctx->gcm_block_len_dma_addr, AES_BLOCK_SIZE);
1451 areq_ctx->gcm_iv_inc1_dma_addr = dma_map_single(dev,
1452 areq_ctx->gcm_iv_inc1,
1453 AES_BLOCK_SIZE, DMA_TO_DEVICE);
1455 if (unlikely(dma_mapping_error(dev, areq_ctx->gcm_iv_inc1_dma_addr))) {
1456 SSI_LOG_ERR("Mapping gcm_iv_inc1 %u B at va=%pK "
1457 "for DMA failed\n", AES_BLOCK_SIZE,
1458 (areq_ctx->gcm_iv_inc1));
1459 areq_ctx->gcm_iv_inc1_dma_addr = 0;
1461 goto aead_map_failure;
1463 SSI_UPDATE_DMA_ADDR_TO_48BIT(areq_ctx->gcm_iv_inc1_dma_addr,
1466 areq_ctx->gcm_iv_inc2_dma_addr = dma_map_single(dev,
1467 areq_ctx->gcm_iv_inc2,
1468 AES_BLOCK_SIZE, DMA_TO_DEVICE);
1470 if (unlikely(dma_mapping_error(dev, areq_ctx->gcm_iv_inc2_dma_addr))) {
1471 SSI_LOG_ERR("Mapping gcm_iv_inc2 %u B at va=%pK "
1472 "for DMA failed\n", AES_BLOCK_SIZE,
1473 (areq_ctx->gcm_iv_inc2));
1474 areq_ctx->gcm_iv_inc2_dma_addr = 0;
1476 goto aead_map_failure;
1478 SSI_UPDATE_DMA_ADDR_TO_48BIT(areq_ctx->gcm_iv_inc2_dma_addr,
1481 #endif /*SSI_CC_HAS_AES_GCM*/
1483 size_to_map = req->cryptlen + req->assoclen;
1484 if (areq_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_ENCRYPT) {
1485 size_to_map += authsize;
1488 size_to_map += crypto_aead_ivsize(tfm);
1489 rc = ssi_buffer_mgr_map_scatterlist(dev, req->src,
1490 size_to_map, DMA_BIDIRECTIONAL, &(areq_ctx->src.nents),
1491 LLI_MAX_NUM_OF_ASSOC_DATA_ENTRIES+LLI_MAX_NUM_OF_DATA_ENTRIES, &dummy, &mapped_nents);
1492 if (unlikely(rc != 0)) {
1494 goto aead_map_failure;
1497 if (likely(areq_ctx->is_single_pass == true)) {
1499 * Create MLLI table for:
1502 * Note: IV is contg. buffer (not an SGL)
1504 rc = ssi_buffer_mgr_aead_chain_assoc(drvdata, req, &sg_data, true, false);
1505 if (unlikely(rc != 0))
1506 goto aead_map_failure;
1507 rc = ssi_buffer_mgr_aead_chain_iv(drvdata, req, &sg_data, true, false);
1508 if (unlikely(rc != 0))
1509 goto aead_map_failure;
1510 rc = ssi_buffer_mgr_aead_chain_data(drvdata, req, &sg_data, true, false);
1511 if (unlikely(rc != 0))
1512 goto aead_map_failure;
1513 } else { /* DOUBLE-PASS flow */
1515 * Prepare MLLI table(s) in this order:
1517 * If ENCRYPT/DECRYPT (inplace):
1518 * (1) MLLI table for assoc
1519 * (2) IV entry (chained right after end of assoc)
1520 * (3) MLLI for src/dst (inplace operation)
1522 * If ENCRYPT (non-inplace)
1523 * (1) MLLI table for assoc
1524 * (2) IV entry (chained right after end of assoc)
1528 * If DECRYPT (non-inplace)
1529 * (1) MLLI table for assoc
1530 * (2) IV entry (chained right after end of assoc)
1534 rc = ssi_buffer_mgr_aead_chain_assoc(drvdata, req, &sg_data, false, true);
1535 if (unlikely(rc != 0))
1536 goto aead_map_failure;
1537 rc = ssi_buffer_mgr_aead_chain_iv(drvdata, req, &sg_data, false, true);
1538 if (unlikely(rc != 0))
1539 goto aead_map_failure;
1540 rc = ssi_buffer_mgr_aead_chain_data(drvdata, req, &sg_data, true, true);
1541 if (unlikely(rc != 0))
1542 goto aead_map_failure;
1545 /* Mlli support -start building the MLLI according to the above results */
1547 (areq_ctx->assoc_buff_type == SSI_DMA_BUF_MLLI) ||
1548 (areq_ctx->data_buff_type == SSI_DMA_BUF_MLLI))) {
1550 mlli_params->curr_pool = buff_mgr->mlli_buffs_pool;
1551 rc = ssi_buffer_mgr_generate_mlli(dev, &sg_data, mlli_params);
1552 if (unlikely(rc != 0)) {
1553 goto aead_map_failure;
1556 ssi_buffer_mgr_update_aead_mlli_nents(drvdata, req);
1557 SSI_LOG_DEBUG("assoc params mn %d\n",areq_ctx->assoc.mlli_nents);
1558 SSI_LOG_DEBUG("src params mn %d\n",areq_ctx->src.mlli_nents);
1559 SSI_LOG_DEBUG("dst params mn %d\n",areq_ctx->dst.mlli_nents);
1564 ssi_buffer_mgr_unmap_aead_request(dev, req);
1568 int ssi_buffer_mgr_map_hash_request_final(
1569 struct ssi_drvdata *drvdata, void *ctx, struct scatterlist *src, unsigned int nbytes, bool do_update)
1571 struct ahash_req_ctx *areq_ctx = (struct ahash_req_ctx *)ctx;
1572 struct device *dev = &drvdata->plat_dev->dev;
1573 uint8_t* curr_buff = areq_ctx->buff_index ? areq_ctx->buff1 :
1575 uint32_t *curr_buff_cnt = areq_ctx->buff_index ? &areq_ctx->buff1_cnt :
1576 &areq_ctx->buff0_cnt;
1577 struct mlli_params *mlli_params = &areq_ctx->mlli_params;
1578 struct buffer_array sg_data;
1579 struct buff_mgr_handle *buff_mgr = drvdata->buff_mgr_handle;
1581 uint32_t mapped_nents = 0;
1583 SSI_LOG_DEBUG(" final params : curr_buff=%pK "
1584 "curr_buff_cnt=0x%X nbytes = 0x%X "
1585 "src=%pK curr_index=%u\n",
1586 curr_buff, *curr_buff_cnt, nbytes,
1587 src, areq_ctx->buff_index);
1588 /* Init the type of the dma buffer */
1589 areq_ctx->data_dma_buf_type = SSI_DMA_BUF_NULL;
1590 mlli_params->curr_pool = NULL;
1591 sg_data.num_of_buffers = 0;
1592 areq_ctx->in_nents = 0;
1594 if (unlikely(nbytes == 0 && *curr_buff_cnt == 0)) {
1599 /*TODO: copy data in case that buffer is enough for operation */
1600 /* map the previous buffer */
1601 if (*curr_buff_cnt != 0 ) {
1602 if (ssi_ahash_handle_curr_buf(dev, areq_ctx, curr_buff,
1603 *curr_buff_cnt, &sg_data) != 0) {
1608 if (src && (nbytes > 0) && do_update) {
1609 if ( unlikely( ssi_buffer_mgr_map_scatterlist( dev,src,
1612 &areq_ctx->in_nents,
1613 LLI_MAX_NUM_OF_DATA_ENTRIES,
1614 &dummy, &mapped_nents))){
1615 goto unmap_curr_buff;
1617 if ( src && (mapped_nents == 1)
1618 && (areq_ctx->data_dma_buf_type == SSI_DMA_BUF_NULL) ) {
1619 memcpy(areq_ctx->buff_sg,src,
1620 sizeof(struct scatterlist));
1621 areq_ctx->buff_sg->length = nbytes;
1622 areq_ctx->curr_sg = areq_ctx->buff_sg;
1623 areq_ctx->data_dma_buf_type = SSI_DMA_BUF_DLLI;
1625 areq_ctx->data_dma_buf_type = SSI_DMA_BUF_MLLI;
1631 if (unlikely(areq_ctx->data_dma_buf_type == SSI_DMA_BUF_MLLI)) {
1632 mlli_params->curr_pool = buff_mgr->mlli_buffs_pool;
1633 /* add the src data to the sg_data */
1634 ssi_buffer_mgr_add_scatterlist_entry(&sg_data,
1638 true, &areq_ctx->mlli_nents);
1639 if (unlikely(ssi_buffer_mgr_generate_mlli(dev, &sg_data,
1640 mlli_params) != 0)) {
1641 goto fail_unmap_din;
1644 /* change the buffer index for the unmap function */
1645 areq_ctx->buff_index = (areq_ctx->buff_index^1);
1646 SSI_LOG_DEBUG("areq_ctx->data_dma_buf_type = %s\n",
1647 GET_DMA_BUFFER_TYPE(areq_ctx->data_dma_buf_type));
1651 dma_unmap_sg(dev, src, areq_ctx->in_nents, DMA_TO_DEVICE);
1654 if (*curr_buff_cnt != 0 ) {
1655 dma_unmap_sg(dev, areq_ctx->buff_sg, 1, DMA_TO_DEVICE);
1660 int ssi_buffer_mgr_map_hash_request_update(
1661 struct ssi_drvdata *drvdata, void *ctx, struct scatterlist *src, unsigned int nbytes, unsigned int block_size)
1663 struct ahash_req_ctx *areq_ctx = (struct ahash_req_ctx *)ctx;
1664 struct device *dev = &drvdata->plat_dev->dev;
1665 uint8_t* curr_buff = areq_ctx->buff_index ? areq_ctx->buff1 :
1667 uint32_t *curr_buff_cnt = areq_ctx->buff_index ? &areq_ctx->buff1_cnt :
1668 &areq_ctx->buff0_cnt;
1669 uint8_t* next_buff = areq_ctx->buff_index ? areq_ctx->buff0 :
1671 uint32_t *next_buff_cnt = areq_ctx->buff_index ? &areq_ctx->buff0_cnt :
1672 &areq_ctx->buff1_cnt;
1673 struct mlli_params *mlli_params = &areq_ctx->mlli_params;
1674 unsigned int update_data_len;
1675 uint32_t total_in_len = nbytes + *curr_buff_cnt;
1676 struct buffer_array sg_data;
1677 struct buff_mgr_handle *buff_mgr = drvdata->buff_mgr_handle;
1678 unsigned int swap_index = 0;
1680 uint32_t mapped_nents = 0;
1682 SSI_LOG_DEBUG(" update params : curr_buff=%pK "
1683 "curr_buff_cnt=0x%X nbytes=0x%X "
1684 "src=%pK curr_index=%u \n",
1685 curr_buff, *curr_buff_cnt, nbytes,
1686 src, areq_ctx->buff_index);
1687 /* Init the type of the dma buffer */
1688 areq_ctx->data_dma_buf_type = SSI_DMA_BUF_NULL;
1689 mlli_params->curr_pool = NULL;
1690 areq_ctx->curr_sg = NULL;
1691 sg_data.num_of_buffers = 0;
1692 areq_ctx->in_nents = 0;
1694 if (unlikely(total_in_len < block_size)) {
1695 SSI_LOG_DEBUG(" less than one block: curr_buff=%pK "
1696 "*curr_buff_cnt=0x%X copy_to=%pK\n",
1697 curr_buff, *curr_buff_cnt,
1698 &curr_buff[*curr_buff_cnt]);
1699 areq_ctx->in_nents =
1700 ssi_buffer_mgr_get_sgl_nents(src,
1703 sg_copy_to_buffer(src, areq_ctx->in_nents,
1704 &curr_buff[*curr_buff_cnt], nbytes);
1705 *curr_buff_cnt += nbytes;
1709 /* Calculate the residue size*/
1710 *next_buff_cnt = total_in_len & (block_size - 1);
1711 /* update data len */
1712 update_data_len = total_in_len - *next_buff_cnt;
1714 SSI_LOG_DEBUG(" temp length : *next_buff_cnt=0x%X "
1715 "update_data_len=0x%X\n",
1716 *next_buff_cnt, update_data_len);
1718 /* Copy the new residue to next buffer */
1719 if (*next_buff_cnt != 0) {
1720 SSI_LOG_DEBUG(" handle residue: next buff %pK skip data %u"
1721 " residue %u \n", next_buff,
1722 (update_data_len - *curr_buff_cnt),
1724 ssi_buffer_mgr_copy_scatterlist_portion(next_buff, src,
1725 (update_data_len -*curr_buff_cnt),
1726 nbytes,SSI_SG_TO_BUF);
1727 /* change the buffer index for next operation */
1731 if (*curr_buff_cnt != 0) {
1732 if (ssi_ahash_handle_curr_buf(dev, areq_ctx, curr_buff,
1733 *curr_buff_cnt, &sg_data) != 0) {
1736 /* change the buffer index for next operation */
1740 if ( update_data_len > *curr_buff_cnt ) {
1741 if ( unlikely( ssi_buffer_mgr_map_scatterlist( dev,src,
1742 (update_data_len -*curr_buff_cnt),
1744 &areq_ctx->in_nents,
1745 LLI_MAX_NUM_OF_DATA_ENTRIES,
1746 &dummy, &mapped_nents))){
1747 goto unmap_curr_buff;
1749 if ( (mapped_nents == 1)
1750 && (areq_ctx->data_dma_buf_type == SSI_DMA_BUF_NULL) ) {
1751 /* only one entry in the SG and no previous data */
1752 memcpy(areq_ctx->buff_sg,src,
1753 sizeof(struct scatterlist));
1754 areq_ctx->buff_sg->length = update_data_len;
1755 areq_ctx->data_dma_buf_type = SSI_DMA_BUF_DLLI;
1756 areq_ctx->curr_sg = areq_ctx->buff_sg;
1758 areq_ctx->data_dma_buf_type = SSI_DMA_BUF_MLLI;
1762 if (unlikely(areq_ctx->data_dma_buf_type == SSI_DMA_BUF_MLLI)) {
1763 mlli_params->curr_pool = buff_mgr->mlli_buffs_pool;
1764 /* add the src data to the sg_data */
1765 ssi_buffer_mgr_add_scatterlist_entry(&sg_data,
1768 (update_data_len - *curr_buff_cnt), 0,
1769 true, &areq_ctx->mlli_nents);
1770 if (unlikely(ssi_buffer_mgr_generate_mlli(dev, &sg_data,
1771 mlli_params) != 0)) {
1772 goto fail_unmap_din;
1776 areq_ctx->buff_index = (areq_ctx->buff_index^swap_index);
1781 dma_unmap_sg(dev, src, areq_ctx->in_nents, DMA_TO_DEVICE);
1784 if (*curr_buff_cnt != 0 ) {
1785 dma_unmap_sg(dev, areq_ctx->buff_sg, 1, DMA_TO_DEVICE);
1790 void ssi_buffer_mgr_unmap_hash_request(
1791 struct device *dev, void *ctx, struct scatterlist *src, bool do_revert)
1793 struct ahash_req_ctx *areq_ctx = (struct ahash_req_ctx *)ctx;
1794 uint32_t *prev_len = areq_ctx->buff_index ? &areq_ctx->buff0_cnt :
1795 &areq_ctx->buff1_cnt;
1797 /*In case a pool was set, a table was
1798 allocated and should be released */
1799 if (areq_ctx->mlli_params.curr_pool != NULL) {
1800 SSI_LOG_DEBUG("free MLLI buffer: dma=0x%llX virt=%pK\n",
1801 (unsigned long long)areq_ctx->mlli_params.mlli_dma_addr,
1802 areq_ctx->mlli_params.mlli_virt_addr);
1803 SSI_RESTORE_DMA_ADDR_TO_48BIT(areq_ctx->mlli_params.mlli_dma_addr);
1804 dma_pool_free(areq_ctx->mlli_params.curr_pool,
1805 areq_ctx->mlli_params.mlli_virt_addr,
1806 areq_ctx->mlli_params.mlli_dma_addr);
1809 if ((src) && likely(areq_ctx->in_nents != 0)) {
1810 SSI_LOG_DEBUG("Unmapped sg src: virt=%pK dma=0x%llX len=0x%X\n",
1812 (unsigned long long)sg_dma_address(src),
1814 SSI_RESTORE_DMA_ADDR_TO_48BIT(sg_dma_address(src));
1815 dma_unmap_sg(dev, src,
1816 areq_ctx->in_nents, DMA_TO_DEVICE);
1819 if (*prev_len != 0) {
1820 SSI_LOG_DEBUG("Unmapped buffer: areq_ctx->buff_sg=%pK"
1821 "dma=0x%llX len 0x%X\n",
1822 sg_virt(areq_ctx->buff_sg),
1823 (unsigned long long)sg_dma_address(areq_ctx->buff_sg),
1824 sg_dma_len(areq_ctx->buff_sg));
1825 dma_unmap_sg(dev, areq_ctx->buff_sg, 1, DMA_TO_DEVICE);
1827 /* clean the previous data length for update operation */
1830 areq_ctx->buff_index ^= 1;
1835 int ssi_buffer_mgr_init(struct ssi_drvdata *drvdata)
1837 struct buff_mgr_handle *buff_mgr_handle;
1838 struct device *dev = &drvdata->plat_dev->dev;
1840 buff_mgr_handle = (struct buff_mgr_handle *)
1841 kmalloc(sizeof(struct buff_mgr_handle), GFP_KERNEL);
1842 if (buff_mgr_handle == NULL)
1845 drvdata->buff_mgr_handle = buff_mgr_handle;
1847 buff_mgr_handle->mlli_buffs_pool = dma_pool_create(
1848 "dx_single_mlli_tables", dev,
1849 MAX_NUM_OF_TOTAL_MLLI_ENTRIES *
1850 LLI_ENTRY_BYTE_SIZE,
1851 MLLI_TABLE_MIN_ALIGNMENT, 0);
1853 if (unlikely(buff_mgr_handle->mlli_buffs_pool == NULL))
1859 ssi_buffer_mgr_fini(drvdata);
1863 int ssi_buffer_mgr_fini(struct ssi_drvdata *drvdata)
1865 struct buff_mgr_handle *buff_mgr_handle = drvdata->buff_mgr_handle;
1867 if (buff_mgr_handle != NULL) {
1868 if (buff_mgr_handle->mlli_buffs_pool != NULL)
1869 dma_pool_destroy(buff_mgr_handle->mlli_buffs_pool);
1870 kfree(drvdata->buff_mgr_handle);
1871 drvdata->buff_mgr_handle = NULL;