14c3f9727SGilad Ben-Yossef // SPDX-License-Identifier: GPL-2.0
203963caeSGilad Ben-Yossef /* Copyright (C) 2012-2019 ARM Limited (or its affiliates). */
34c3f9727SGilad Ben-Yossef
4ff27e85aSGilad Ben-Yossef #include <crypto/internal/aead.h>
54c3f9727SGilad Ben-Yossef #include <crypto/authenc.h>
64c3f9727SGilad Ben-Yossef #include <crypto/scatterwalk.h>
74c3f9727SGilad Ben-Yossef #include <linux/dmapool.h>
84c3f9727SGilad Ben-Yossef #include <linux/dma-mapping.h>
94c3f9727SGilad Ben-Yossef
104c3f9727SGilad Ben-Yossef #include "cc_buffer_mgr.h"
114c3f9727SGilad Ben-Yossef #include "cc_lli_defs.h"
1263ee04c8SGilad Ben-Yossef #include "cc_cipher.h"
1363893811SGilad Ben-Yossef #include "cc_hash.h"
14ff27e85aSGilad Ben-Yossef #include "cc_aead.h"
154c3f9727SGilad Ben-Yossef
164c3f9727SGilad Ben-Yossef union buffer_array_entry {
174c3f9727SGilad Ben-Yossef struct scatterlist *sgl;
184c3f9727SGilad Ben-Yossef dma_addr_t buffer_dma;
194c3f9727SGilad Ben-Yossef };
204c3f9727SGilad Ben-Yossef
214c3f9727SGilad Ben-Yossef struct buffer_array {
224c3f9727SGilad Ben-Yossef unsigned int num_of_buffers;
234c3f9727SGilad Ben-Yossef union buffer_array_entry entry[MAX_NUM_OF_BUFFERS_IN_MLLI];
244c3f9727SGilad Ben-Yossef unsigned int offset[MAX_NUM_OF_BUFFERS_IN_MLLI];
254c3f9727SGilad Ben-Yossef int nents[MAX_NUM_OF_BUFFERS_IN_MLLI];
264c3f9727SGilad Ben-Yossef int total_data_len[MAX_NUM_OF_BUFFERS_IN_MLLI];
274c3f9727SGilad Ben-Yossef bool is_last[MAX_NUM_OF_BUFFERS_IN_MLLI];
284c3f9727SGilad Ben-Yossef u32 *mlli_nents[MAX_NUM_OF_BUFFERS_IN_MLLI];
294c3f9727SGilad Ben-Yossef };
304c3f9727SGilad Ben-Yossef
cc_dma_buf_type(enum cc_req_dma_buf_type type)314c3f9727SGilad Ben-Yossef static inline char *cc_dma_buf_type(enum cc_req_dma_buf_type type)
324c3f9727SGilad Ben-Yossef {
334c3f9727SGilad Ben-Yossef switch (type) {
344c3f9727SGilad Ben-Yossef case CC_DMA_BUF_NULL:
354c3f9727SGilad Ben-Yossef return "BUF_NULL";
364c3f9727SGilad Ben-Yossef case CC_DMA_BUF_DLLI:
374c3f9727SGilad Ben-Yossef return "BUF_DLLI";
384c3f9727SGilad Ben-Yossef case CC_DMA_BUF_MLLI:
394c3f9727SGilad Ben-Yossef return "BUF_MLLI";
404c3f9727SGilad Ben-Yossef default:
414c3f9727SGilad Ben-Yossef return "BUF_INVALID";
424c3f9727SGilad Ben-Yossef }
434c3f9727SGilad Ben-Yossef }
444c3f9727SGilad Ben-Yossef
454c3f9727SGilad Ben-Yossef /**
46ff27e85aSGilad Ben-Yossef * cc_copy_mac() - Copy MAC to temporary location
47ff27e85aSGilad Ben-Yossef *
48ff27e85aSGilad Ben-Yossef * @dev: device object
49ff27e85aSGilad Ben-Yossef * @req: aead request object
50ff27e85aSGilad Ben-Yossef * @dir: [IN] copy from/to sgl
51ff27e85aSGilad Ben-Yossef */
cc_copy_mac(struct device * dev,struct aead_request * req,enum cc_sg_cpy_direct dir)52ff27e85aSGilad Ben-Yossef static void cc_copy_mac(struct device *dev, struct aead_request *req,
53ff27e85aSGilad Ben-Yossef enum cc_sg_cpy_direct dir)
54ff27e85aSGilad Ben-Yossef {
55*07547fa7SHerbert Xu struct aead_req_ctx *areq_ctx = aead_request_ctx_dma(req);
560eae14a0SGilad Ben-Yossef u32 skip = req->assoclen + req->cryptlen;
57ff27e85aSGilad Ben-Yossef
58ff27e85aSGilad Ben-Yossef cc_copy_sg_portion(dev, areq_ctx->backup_mac, req->src,
59ff27e85aSGilad Ben-Yossef (skip - areq_ctx->req_authsize), skip, dir);
60ff27e85aSGilad Ben-Yossef }
61ff27e85aSGilad Ben-Yossef
62ff27e85aSGilad Ben-Yossef /**
634c3f9727SGilad Ben-Yossef * cc_get_sgl_nents() - Get scatterlist number of entries.
644c3f9727SGilad Ben-Yossef *
65ae02fcfeSGeert Uytterhoeven * @dev: Device object
664c3f9727SGilad Ben-Yossef * @sg_list: SG list
674c3f9727SGilad Ben-Yossef * @nbytes: [IN] Total SGL data bytes.
684c3f9727SGilad Ben-Yossef * @lbytes: [OUT] Returns the amount of bytes at the last entry
69ae02fcfeSGeert Uytterhoeven *
70ae02fcfeSGeert Uytterhoeven * Return:
71ae02fcfeSGeert Uytterhoeven * Number of entries in the scatterlist
724c3f9727SGilad Ben-Yossef */
cc_get_sgl_nents(struct device * dev,struct scatterlist * sg_list,unsigned int nbytes,u32 * lbytes)734c3f9727SGilad Ben-Yossef static unsigned int cc_get_sgl_nents(struct device *dev,
744c3f9727SGilad Ben-Yossef struct scatterlist *sg_list,
75c4b22bf5SGilad Ben-Yossef unsigned int nbytes, u32 *lbytes)
764c3f9727SGilad Ben-Yossef {
774c3f9727SGilad Ben-Yossef unsigned int nents = 0;
784c3f9727SGilad Ben-Yossef
79ce0fc6dbSGilad Ben-Yossef *lbytes = 0;
80ce0fc6dbSGilad Ben-Yossef
814c3f9727SGilad Ben-Yossef while (nbytes && sg_list) {
824c3f9727SGilad Ben-Yossef nents++;
834c3f9727SGilad Ben-Yossef /* get the number of bytes in the last entry */
844c3f9727SGilad Ben-Yossef *lbytes = nbytes;
854c3f9727SGilad Ben-Yossef nbytes -= (sg_list->length > nbytes) ?
864c3f9727SGilad Ben-Yossef nbytes : sg_list->length;
874c3f9727SGilad Ben-Yossef sg_list = sg_next(sg_list);
884c3f9727SGilad Ben-Yossef }
89ce0fc6dbSGilad Ben-Yossef
904c3f9727SGilad Ben-Yossef dev_dbg(dev, "nents %d last bytes %d\n", nents, *lbytes);
914c3f9727SGilad Ben-Yossef return nents;
924c3f9727SGilad Ben-Yossef }
934c3f9727SGilad Ben-Yossef
944c3f9727SGilad Ben-Yossef /**
954c3f9727SGilad Ben-Yossef * cc_copy_sg_portion() - Copy scatter list data,
964c3f9727SGilad Ben-Yossef * from to_skip to end, to dest and vice versa
974c3f9727SGilad Ben-Yossef *
98ae02fcfeSGeert Uytterhoeven * @dev: Device object
99ae02fcfeSGeert Uytterhoeven * @dest: Buffer to copy to/from
100ae02fcfeSGeert Uytterhoeven * @sg: SG list
101ae02fcfeSGeert Uytterhoeven * @to_skip: Number of bytes to skip before copying
102ae02fcfeSGeert Uytterhoeven * @end: Offset of last byte to copy
103ae02fcfeSGeert Uytterhoeven * @direct: Transfer direction (true == from SG list to buffer, false == from
104ae02fcfeSGeert Uytterhoeven * buffer to SG list)
1054c3f9727SGilad Ben-Yossef */
cc_copy_sg_portion(struct device * dev,u8 * dest,struct scatterlist * sg,u32 to_skip,u32 end,enum cc_sg_cpy_direct direct)1064c3f9727SGilad Ben-Yossef void cc_copy_sg_portion(struct device *dev, u8 *dest, struct scatterlist *sg,
1074c3f9727SGilad Ben-Yossef u32 to_skip, u32 end, enum cc_sg_cpy_direct direct)
1084c3f9727SGilad Ben-Yossef {
109b7ec8530SGilad Ben-Yossef u32 nents;
1104c3f9727SGilad Ben-Yossef
111b7ec8530SGilad Ben-Yossef nents = sg_nents_for_len(sg, end);
112f4274eecSGeert Uytterhoeven sg_copy_buffer(sg, nents, dest, (end - to_skip + 1), to_skip,
1134c3f9727SGilad Ben-Yossef (direct == CC_SG_TO_BUF));
1144c3f9727SGilad Ben-Yossef }
1154c3f9727SGilad Ben-Yossef
cc_render_buff_to_mlli(struct device * dev,dma_addr_t buff_dma,u32 buff_size,u32 * curr_nents,u32 ** mlli_entry_pp)1164c3f9727SGilad Ben-Yossef static int cc_render_buff_to_mlli(struct device *dev, dma_addr_t buff_dma,
1174c3f9727SGilad Ben-Yossef u32 buff_size, u32 *curr_nents,
1184c3f9727SGilad Ben-Yossef u32 **mlli_entry_pp)
1194c3f9727SGilad Ben-Yossef {
1204c3f9727SGilad Ben-Yossef u32 *mlli_entry_p = *mlli_entry_pp;
1214c3f9727SGilad Ben-Yossef u32 new_nents;
1224c3f9727SGilad Ben-Yossef
1234c3f9727SGilad Ben-Yossef /* Verify there is no memory overflow*/
1244c3f9727SGilad Ben-Yossef new_nents = (*curr_nents + buff_size / CC_MAX_MLLI_ENTRY_SIZE + 1);
12532be4c5bSHadar Gat if (new_nents > MAX_NUM_OF_TOTAL_MLLI_ENTRIES) {
12632be4c5bSHadar Gat dev_err(dev, "Too many mlli entries. current %d max %d\n",
12732be4c5bSHadar Gat new_nents, MAX_NUM_OF_TOTAL_MLLI_ENTRIES);
1284c3f9727SGilad Ben-Yossef return -ENOMEM;
12932be4c5bSHadar Gat }
1304c3f9727SGilad Ben-Yossef
1314c3f9727SGilad Ben-Yossef /*handle buffer longer than 64 kbytes */
1324c3f9727SGilad Ben-Yossef while (buff_size > CC_MAX_MLLI_ENTRY_SIZE) {
1334c3f9727SGilad Ben-Yossef cc_lli_set_addr(mlli_entry_p, buff_dma);
1344c3f9727SGilad Ben-Yossef cc_lli_set_size(mlli_entry_p, CC_MAX_MLLI_ENTRY_SIZE);
1354c3f9727SGilad Ben-Yossef dev_dbg(dev, "entry[%d]: single_buff=0x%08X size=%08X\n",
1364c3f9727SGilad Ben-Yossef *curr_nents, mlli_entry_p[LLI_WORD0_OFFSET],
1374c3f9727SGilad Ben-Yossef mlli_entry_p[LLI_WORD1_OFFSET]);
1384c3f9727SGilad Ben-Yossef buff_dma += CC_MAX_MLLI_ENTRY_SIZE;
1394c3f9727SGilad Ben-Yossef buff_size -= CC_MAX_MLLI_ENTRY_SIZE;
1404c3f9727SGilad Ben-Yossef mlli_entry_p = mlli_entry_p + 2;
1414c3f9727SGilad Ben-Yossef (*curr_nents)++;
1424c3f9727SGilad Ben-Yossef }
1434c3f9727SGilad Ben-Yossef /*Last entry */
1444c3f9727SGilad Ben-Yossef cc_lli_set_addr(mlli_entry_p, buff_dma);
1454c3f9727SGilad Ben-Yossef cc_lli_set_size(mlli_entry_p, buff_size);
1464c3f9727SGilad Ben-Yossef dev_dbg(dev, "entry[%d]: single_buff=0x%08X size=%08X\n",
1474c3f9727SGilad Ben-Yossef *curr_nents, mlli_entry_p[LLI_WORD0_OFFSET],
1484c3f9727SGilad Ben-Yossef mlli_entry_p[LLI_WORD1_OFFSET]);
1494c3f9727SGilad Ben-Yossef mlli_entry_p = mlli_entry_p + 2;
1504c3f9727SGilad Ben-Yossef *mlli_entry_pp = mlli_entry_p;
1514c3f9727SGilad Ben-Yossef (*curr_nents)++;
1524c3f9727SGilad Ben-Yossef return 0;
1534c3f9727SGilad Ben-Yossef }
1544c3f9727SGilad Ben-Yossef
cc_render_sg_to_mlli(struct device * dev,struct scatterlist * sgl,u32 sgl_data_len,u32 sgl_offset,u32 * curr_nents,u32 ** mlli_entry_pp)1554c3f9727SGilad Ben-Yossef static int cc_render_sg_to_mlli(struct device *dev, struct scatterlist *sgl,
1564c3f9727SGilad Ben-Yossef u32 sgl_data_len, u32 sgl_offset,
1574c3f9727SGilad Ben-Yossef u32 *curr_nents, u32 **mlli_entry_pp)
1584c3f9727SGilad Ben-Yossef {
1594c3f9727SGilad Ben-Yossef struct scatterlist *curr_sgl = sgl;
1604c3f9727SGilad Ben-Yossef u32 *mlli_entry_p = *mlli_entry_pp;
1614c3f9727SGilad Ben-Yossef s32 rc = 0;
1624c3f9727SGilad Ben-Yossef
1634c3f9727SGilad Ben-Yossef for ( ; (curr_sgl && sgl_data_len);
1644c3f9727SGilad Ben-Yossef curr_sgl = sg_next(curr_sgl)) {
1654c3f9727SGilad Ben-Yossef u32 entry_data_len =
1664c3f9727SGilad Ben-Yossef (sgl_data_len > sg_dma_len(curr_sgl) - sgl_offset) ?
1674c3f9727SGilad Ben-Yossef sg_dma_len(curr_sgl) - sgl_offset :
1684c3f9727SGilad Ben-Yossef sgl_data_len;
1694c3f9727SGilad Ben-Yossef sgl_data_len -= entry_data_len;
1704c3f9727SGilad Ben-Yossef rc = cc_render_buff_to_mlli(dev, sg_dma_address(curr_sgl) +
1714c3f9727SGilad Ben-Yossef sgl_offset, entry_data_len,
1724c3f9727SGilad Ben-Yossef curr_nents, &mlli_entry_p);
1734c3f9727SGilad Ben-Yossef if (rc)
1744c3f9727SGilad Ben-Yossef return rc;
1754c3f9727SGilad Ben-Yossef
1764c3f9727SGilad Ben-Yossef sgl_offset = 0;
1774c3f9727SGilad Ben-Yossef }
1784c3f9727SGilad Ben-Yossef *mlli_entry_pp = mlli_entry_p;
1794c3f9727SGilad Ben-Yossef return 0;
1804c3f9727SGilad Ben-Yossef }
1814c3f9727SGilad Ben-Yossef
cc_generate_mlli(struct device * dev,struct buffer_array * sg_data,struct mlli_params * mlli_params,gfp_t flags)1824c3f9727SGilad Ben-Yossef static int cc_generate_mlli(struct device *dev, struct buffer_array *sg_data,
1834c3f9727SGilad Ben-Yossef struct mlli_params *mlli_params, gfp_t flags)
1844c3f9727SGilad Ben-Yossef {
1854c3f9727SGilad Ben-Yossef u32 *mlli_p;
1864c3f9727SGilad Ben-Yossef u32 total_nents = 0, prev_total_nents = 0;
1874c3f9727SGilad Ben-Yossef int rc = 0, i;
1884c3f9727SGilad Ben-Yossef
1894c3f9727SGilad Ben-Yossef dev_dbg(dev, "NUM of SG's = %d\n", sg_data->num_of_buffers);
1904c3f9727SGilad Ben-Yossef
1914c3f9727SGilad Ben-Yossef /* Allocate memory from the pointed pool */
1924c3f9727SGilad Ben-Yossef mlli_params->mlli_virt_addr =
1934c3f9727SGilad Ben-Yossef dma_pool_alloc(mlli_params->curr_pool, flags,
1944c3f9727SGilad Ben-Yossef &mlli_params->mlli_dma_addr);
1954c3f9727SGilad Ben-Yossef if (!mlli_params->mlli_virt_addr) {
1964c3f9727SGilad Ben-Yossef dev_err(dev, "dma_pool_alloc() failed\n");
1974c3f9727SGilad Ben-Yossef rc = -ENOMEM;
1984c3f9727SGilad Ben-Yossef goto build_mlli_exit;
1994c3f9727SGilad Ben-Yossef }
2004c3f9727SGilad Ben-Yossef /* Point to start of MLLI */
201ba99b6f9SGeert Uytterhoeven mlli_p = mlli_params->mlli_virt_addr;
2024c3f9727SGilad Ben-Yossef /* go over all SG's and link it to one MLLI table */
2034c3f9727SGilad Ben-Yossef for (i = 0; i < sg_data->num_of_buffers; i++) {
2044c3f9727SGilad Ben-Yossef union buffer_array_entry *entry = &sg_data->entry[i];
2054c3f9727SGilad Ben-Yossef u32 tot_len = sg_data->total_data_len[i];
2064c3f9727SGilad Ben-Yossef u32 offset = sg_data->offset[i];
2074c3f9727SGilad Ben-Yossef
2080eae14a0SGilad Ben-Yossef rc = cc_render_sg_to_mlli(dev, entry->sgl, tot_len, offset,
2090eae14a0SGilad Ben-Yossef &total_nents, &mlli_p);
2104c3f9727SGilad Ben-Yossef if (rc)
2114c3f9727SGilad Ben-Yossef return rc;
2124c3f9727SGilad Ben-Yossef
2134c3f9727SGilad Ben-Yossef /* set last bit in the current table */
2144c3f9727SGilad Ben-Yossef if (sg_data->mlli_nents[i]) {
2154c3f9727SGilad Ben-Yossef /*Calculate the current MLLI table length for the
2164c3f9727SGilad Ben-Yossef *length field in the descriptor
2174c3f9727SGilad Ben-Yossef */
2184c3f9727SGilad Ben-Yossef *sg_data->mlli_nents[i] +=
2194c3f9727SGilad Ben-Yossef (total_nents - prev_total_nents);
2204c3f9727SGilad Ben-Yossef prev_total_nents = total_nents;
2214c3f9727SGilad Ben-Yossef }
2224c3f9727SGilad Ben-Yossef }
2234c3f9727SGilad Ben-Yossef
2244c3f9727SGilad Ben-Yossef /* Set MLLI size for the bypass operation */
2254c3f9727SGilad Ben-Yossef mlli_params->mlli_len = (total_nents * LLI_ENTRY_BYTE_SIZE);
2264c3f9727SGilad Ben-Yossef
2274c3f9727SGilad Ben-Yossef dev_dbg(dev, "MLLI params: virt_addr=%pK dma_addr=%pad mlli_len=0x%X\n",
2284c3f9727SGilad Ben-Yossef mlli_params->mlli_virt_addr, &mlli_params->mlli_dma_addr,
2294c3f9727SGilad Ben-Yossef mlli_params->mlli_len);
2304c3f9727SGilad Ben-Yossef
2314c3f9727SGilad Ben-Yossef build_mlli_exit:
2324c3f9727SGilad Ben-Yossef return rc;
2334c3f9727SGilad Ben-Yossef }
2344c3f9727SGilad Ben-Yossef
cc_add_sg_entry(struct device * dev,struct buffer_array * sgl_data,unsigned int nents,struct scatterlist * sgl,unsigned int data_len,unsigned int data_offset,bool is_last_table,u32 * mlli_nents)2354c3f9727SGilad Ben-Yossef static void cc_add_sg_entry(struct device *dev, struct buffer_array *sgl_data,
2364c3f9727SGilad Ben-Yossef unsigned int nents, struct scatterlist *sgl,
2374c3f9727SGilad Ben-Yossef unsigned int data_len, unsigned int data_offset,
2384c3f9727SGilad Ben-Yossef bool is_last_table, u32 *mlli_nents)
2394c3f9727SGilad Ben-Yossef {
2404c3f9727SGilad Ben-Yossef unsigned int index = sgl_data->num_of_buffers;
2414c3f9727SGilad Ben-Yossef
2424c3f9727SGilad Ben-Yossef dev_dbg(dev, "index=%u nents=%u sgl=%pK data_len=0x%08X is_last=%d\n",
2434c3f9727SGilad Ben-Yossef index, nents, sgl, data_len, is_last_table);
2444c3f9727SGilad Ben-Yossef sgl_data->nents[index] = nents;
2454c3f9727SGilad Ben-Yossef sgl_data->entry[index].sgl = sgl;
2464c3f9727SGilad Ben-Yossef sgl_data->offset[index] = data_offset;
2474c3f9727SGilad Ben-Yossef sgl_data->total_data_len[index] = data_len;
2484c3f9727SGilad Ben-Yossef sgl_data->is_last[index] = is_last_table;
2494c3f9727SGilad Ben-Yossef sgl_data->mlli_nents[index] = mlli_nents;
2504c3f9727SGilad Ben-Yossef if (sgl_data->mlli_nents[index])
2514c3f9727SGilad Ben-Yossef *sgl_data->mlli_nents[index] = 0;
2524c3f9727SGilad Ben-Yossef sgl_data->num_of_buffers++;
2534c3f9727SGilad Ben-Yossef }
2544c3f9727SGilad Ben-Yossef
cc_map_sg(struct device * dev,struct scatterlist * sg,unsigned int nbytes,int direction,u32 * nents,u32 max_sg_nents,u32 * lbytes,u32 * mapped_nents)2554c3f9727SGilad Ben-Yossef static int cc_map_sg(struct device *dev, struct scatterlist *sg,
2564c3f9727SGilad Ben-Yossef unsigned int nbytes, int direction, u32 *nents,
2574c3f9727SGilad Ben-Yossef u32 max_sg_nents, u32 *lbytes, u32 *mapped_nents)
2584c3f9727SGilad Ben-Yossef {
259ce0fc6dbSGilad Ben-Yossef int ret = 0;
260ce0fc6dbSGilad Ben-Yossef
2611fb37b56SGilad Ben-Yossef if (!nbytes) {
2621fb37b56SGilad Ben-Yossef *mapped_nents = 0;
2631fb37b56SGilad Ben-Yossef *lbytes = 0;
2641fb37b56SGilad Ben-Yossef *nents = 0;
2651fb37b56SGilad Ben-Yossef return 0;
2661fb37b56SGilad Ben-Yossef }
2671fb37b56SGilad Ben-Yossef
268c4b22bf5SGilad Ben-Yossef *nents = cc_get_sgl_nents(dev, sg, nbytes, lbytes);
2694c3f9727SGilad Ben-Yossef if (*nents > max_sg_nents) {
2704c3f9727SGilad Ben-Yossef *nents = 0;
2714c3f9727SGilad Ben-Yossef dev_err(dev, "Too many fragments. current %d max %d\n",
2724c3f9727SGilad Ben-Yossef *nents, max_sg_nents);
2734c3f9727SGilad Ben-Yossef return -ENOMEM;
2744c3f9727SGilad Ben-Yossef }
275ce0fc6dbSGilad Ben-Yossef
276ce0fc6dbSGilad Ben-Yossef ret = dma_map_sg(dev, sg, *nents, direction);
2779b32fed8SJack Wang if (!ret) {
2784c3f9727SGilad Ben-Yossef *nents = 0;
279ce0fc6dbSGilad Ben-Yossef dev_err(dev, "dma_map_sg() sg buffer failed %d\n", ret);
2804c3f9727SGilad Ben-Yossef return -ENOMEM;
2814c3f9727SGilad Ben-Yossef }
282ce0fc6dbSGilad Ben-Yossef
283ce0fc6dbSGilad Ben-Yossef *mapped_nents = ret;
2844c3f9727SGilad Ben-Yossef
2854c3f9727SGilad Ben-Yossef return 0;
2864c3f9727SGilad Ben-Yossef }
2874c3f9727SGilad Ben-Yossef
288ff27e85aSGilad Ben-Yossef static int
cc_set_aead_conf_buf(struct device * dev,struct aead_req_ctx * areq_ctx,u8 * config_data,struct buffer_array * sg_data,unsigned int assoclen)289ff27e85aSGilad Ben-Yossef cc_set_aead_conf_buf(struct device *dev, struct aead_req_ctx *areq_ctx,
290ff27e85aSGilad Ben-Yossef u8 *config_data, struct buffer_array *sg_data,
291ff27e85aSGilad Ben-Yossef unsigned int assoclen)
292ff27e85aSGilad Ben-Yossef {
293ff27e85aSGilad Ben-Yossef dev_dbg(dev, " handle additional data config set to DLLI\n");
294ff27e85aSGilad Ben-Yossef /* create sg for the current buffer */
295ff27e85aSGilad Ben-Yossef sg_init_one(&areq_ctx->ccm_adata_sg, config_data,
296ff27e85aSGilad Ben-Yossef AES_BLOCK_SIZE + areq_ctx->ccm_hdr_size);
297ff27e85aSGilad Ben-Yossef if (dma_map_sg(dev, &areq_ctx->ccm_adata_sg, 1, DMA_TO_DEVICE) != 1) {
298ff27e85aSGilad Ben-Yossef dev_err(dev, "dma_map_sg() config buffer failed\n");
299ff27e85aSGilad Ben-Yossef return -ENOMEM;
300ff27e85aSGilad Ben-Yossef }
301ff27e85aSGilad Ben-Yossef dev_dbg(dev, "Mapped curr_buff: dma_address=%pad page=%p addr=%pK offset=%u length=%u\n",
302ff27e85aSGilad Ben-Yossef &sg_dma_address(&areq_ctx->ccm_adata_sg),
303ff27e85aSGilad Ben-Yossef sg_page(&areq_ctx->ccm_adata_sg),
304ff27e85aSGilad Ben-Yossef sg_virt(&areq_ctx->ccm_adata_sg),
305ff27e85aSGilad Ben-Yossef areq_ctx->ccm_adata_sg.offset, areq_ctx->ccm_adata_sg.length);
306ff27e85aSGilad Ben-Yossef /* prepare for case of MLLI */
307ff27e85aSGilad Ben-Yossef if (assoclen > 0) {
308ff27e85aSGilad Ben-Yossef cc_add_sg_entry(dev, sg_data, 1, &areq_ctx->ccm_adata_sg,
309ff27e85aSGilad Ben-Yossef (AES_BLOCK_SIZE + areq_ctx->ccm_hdr_size),
310ff27e85aSGilad Ben-Yossef 0, false, NULL);
311ff27e85aSGilad Ben-Yossef }
312ff27e85aSGilad Ben-Yossef return 0;
313ff27e85aSGilad Ben-Yossef }
314ff27e85aSGilad Ben-Yossef
cc_set_hash_buf(struct device * dev,struct ahash_req_ctx * areq_ctx,u8 * curr_buff,u32 curr_buff_cnt,struct buffer_array * sg_data)31563893811SGilad Ben-Yossef static int cc_set_hash_buf(struct device *dev, struct ahash_req_ctx *areq_ctx,
31663893811SGilad Ben-Yossef u8 *curr_buff, u32 curr_buff_cnt,
31763893811SGilad Ben-Yossef struct buffer_array *sg_data)
31863893811SGilad Ben-Yossef {
31963893811SGilad Ben-Yossef dev_dbg(dev, " handle curr buff %x set to DLLI\n", curr_buff_cnt);
32063893811SGilad Ben-Yossef /* create sg for the current buffer */
32163893811SGilad Ben-Yossef sg_init_one(areq_ctx->buff_sg, curr_buff, curr_buff_cnt);
32263893811SGilad Ben-Yossef if (dma_map_sg(dev, areq_ctx->buff_sg, 1, DMA_TO_DEVICE) != 1) {
32363893811SGilad Ben-Yossef dev_err(dev, "dma_map_sg() src buffer failed\n");
32463893811SGilad Ben-Yossef return -ENOMEM;
32563893811SGilad Ben-Yossef }
32663893811SGilad Ben-Yossef dev_dbg(dev, "Mapped curr_buff: dma_address=%pad page=%p addr=%pK offset=%u length=%u\n",
32763893811SGilad Ben-Yossef &sg_dma_address(areq_ctx->buff_sg), sg_page(areq_ctx->buff_sg),
32863893811SGilad Ben-Yossef sg_virt(areq_ctx->buff_sg), areq_ctx->buff_sg->offset,
32963893811SGilad Ben-Yossef areq_ctx->buff_sg->length);
33063893811SGilad Ben-Yossef areq_ctx->data_dma_buf_type = CC_DMA_BUF_DLLI;
33163893811SGilad Ben-Yossef areq_ctx->curr_sg = areq_ctx->buff_sg;
33263893811SGilad Ben-Yossef areq_ctx->in_nents = 0;
33363893811SGilad Ben-Yossef /* prepare for case of MLLI */
33463893811SGilad Ben-Yossef cc_add_sg_entry(dev, sg_data, 1, areq_ctx->buff_sg, curr_buff_cnt, 0,
33563893811SGilad Ben-Yossef false, NULL);
33663893811SGilad Ben-Yossef return 0;
33763893811SGilad Ben-Yossef }
33863893811SGilad Ben-Yossef
cc_unmap_cipher_request(struct device * dev,void * ctx,unsigned int ivsize,struct scatterlist * src,struct scatterlist * dst)33963ee04c8SGilad Ben-Yossef void cc_unmap_cipher_request(struct device *dev, void *ctx,
34063ee04c8SGilad Ben-Yossef unsigned int ivsize, struct scatterlist *src,
34163ee04c8SGilad Ben-Yossef struct scatterlist *dst)
34263ee04c8SGilad Ben-Yossef {
34363ee04c8SGilad Ben-Yossef struct cipher_req_ctx *req_ctx = (struct cipher_req_ctx *)ctx;
34463ee04c8SGilad Ben-Yossef
34563ee04c8SGilad Ben-Yossef if (req_ctx->gen_ctx.iv_dma_addr) {
34663ee04c8SGilad Ben-Yossef dev_dbg(dev, "Unmapped iv: iv_dma_addr=%pad iv_size=%u\n",
34763ee04c8SGilad Ben-Yossef &req_ctx->gen_ctx.iv_dma_addr, ivsize);
34863ee04c8SGilad Ben-Yossef dma_unmap_single(dev, req_ctx->gen_ctx.iv_dma_addr,
3496f17e00fSGilad Ben-Yossef ivsize, DMA_BIDIRECTIONAL);
35063ee04c8SGilad Ben-Yossef }
35163ee04c8SGilad Ben-Yossef /* Release pool */
35263ee04c8SGilad Ben-Yossef if (req_ctx->dma_buf_type == CC_DMA_BUF_MLLI &&
35363ee04c8SGilad Ben-Yossef req_ctx->mlli_params.mlli_virt_addr) {
35463ee04c8SGilad Ben-Yossef dma_pool_free(req_ctx->mlli_params.curr_pool,
35563ee04c8SGilad Ben-Yossef req_ctx->mlli_params.mlli_virt_addr,
35663ee04c8SGilad Ben-Yossef req_ctx->mlli_params.mlli_dma_addr);
35763ee04c8SGilad Ben-Yossef }
35863ee04c8SGilad Ben-Yossef
359a260436cSGilad Ben-Yossef if (src != dst) {
360a260436cSGilad Ben-Yossef dma_unmap_sg(dev, src, req_ctx->in_nents, DMA_TO_DEVICE);
361a260436cSGilad Ben-Yossef dma_unmap_sg(dev, dst, req_ctx->out_nents, DMA_FROM_DEVICE);
362a260436cSGilad Ben-Yossef dev_dbg(dev, "Unmapped req->dst=%pK\n", sg_virt(dst));
363a260436cSGilad Ben-Yossef dev_dbg(dev, "Unmapped req->src=%pK\n", sg_virt(src));
364a260436cSGilad Ben-Yossef } else {
36563ee04c8SGilad Ben-Yossef dma_unmap_sg(dev, src, req_ctx->in_nents, DMA_BIDIRECTIONAL);
36663ee04c8SGilad Ben-Yossef dev_dbg(dev, "Unmapped req->src=%pK\n", sg_virt(src));
36763ee04c8SGilad Ben-Yossef }
36863ee04c8SGilad Ben-Yossef }
36963ee04c8SGilad Ben-Yossef
cc_map_cipher_request(struct cc_drvdata * drvdata,void * ctx,unsigned int ivsize,unsigned int nbytes,void * info,struct scatterlist * src,struct scatterlist * dst,gfp_t flags)37063ee04c8SGilad Ben-Yossef int cc_map_cipher_request(struct cc_drvdata *drvdata, void *ctx,
37163ee04c8SGilad Ben-Yossef unsigned int ivsize, unsigned int nbytes,
37263ee04c8SGilad Ben-Yossef void *info, struct scatterlist *src,
37363ee04c8SGilad Ben-Yossef struct scatterlist *dst, gfp_t flags)
37463ee04c8SGilad Ben-Yossef {
37563ee04c8SGilad Ben-Yossef struct cipher_req_ctx *req_ctx = (struct cipher_req_ctx *)ctx;
37663ee04c8SGilad Ben-Yossef struct mlli_params *mlli_params = &req_ctx->mlli_params;
37763ee04c8SGilad Ben-Yossef struct device *dev = drvdata_to_dev(drvdata);
37863ee04c8SGilad Ben-Yossef struct buffer_array sg_data;
37963ee04c8SGilad Ben-Yossef u32 dummy = 0;
38063ee04c8SGilad Ben-Yossef int rc = 0;
38163ee04c8SGilad Ben-Yossef u32 mapped_nents = 0;
382a260436cSGilad Ben-Yossef int src_direction = (src != dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL);
38363ee04c8SGilad Ben-Yossef
38463ee04c8SGilad Ben-Yossef req_ctx->dma_buf_type = CC_DMA_BUF_DLLI;
38563ee04c8SGilad Ben-Yossef mlli_params->curr_pool = NULL;
38663ee04c8SGilad Ben-Yossef sg_data.num_of_buffers = 0;
38763ee04c8SGilad Ben-Yossef
38863ee04c8SGilad Ben-Yossef /* Map IV buffer */
38963ee04c8SGilad Ben-Yossef if (ivsize) {
390f4274eecSGeert Uytterhoeven dump_byte_array("iv", info, ivsize);
39163ee04c8SGilad Ben-Yossef req_ctx->gen_ctx.iv_dma_addr =
392f4274eecSGeert Uytterhoeven dma_map_single(dev, info, ivsize, DMA_BIDIRECTIONAL);
39363ee04c8SGilad Ben-Yossef if (dma_mapping_error(dev, req_ctx->gen_ctx.iv_dma_addr)) {
39463ee04c8SGilad Ben-Yossef dev_err(dev, "Mapping iv %u B at va=%pK for DMA failed\n",
39563ee04c8SGilad Ben-Yossef ivsize, info);
39663ee04c8SGilad Ben-Yossef return -ENOMEM;
39763ee04c8SGilad Ben-Yossef }
39863ee04c8SGilad Ben-Yossef dev_dbg(dev, "Mapped iv %u B at va=%pK to dma=%pad\n",
39963ee04c8SGilad Ben-Yossef ivsize, info, &req_ctx->gen_ctx.iv_dma_addr);
40063ee04c8SGilad Ben-Yossef } else {
40163ee04c8SGilad Ben-Yossef req_ctx->gen_ctx.iv_dma_addr = 0;
40263ee04c8SGilad Ben-Yossef }
40363ee04c8SGilad Ben-Yossef
40463ee04c8SGilad Ben-Yossef /* Map the src SGL */
405a260436cSGilad Ben-Yossef rc = cc_map_sg(dev, src, nbytes, src_direction, &req_ctx->in_nents,
40663ee04c8SGilad Ben-Yossef LLI_MAX_NUM_OF_DATA_ENTRIES, &dummy, &mapped_nents);
407ccba2f11SHadar Gat if (rc)
40863ee04c8SGilad Ben-Yossef goto cipher_exit;
40963ee04c8SGilad Ben-Yossef if (mapped_nents > 1)
41063ee04c8SGilad Ben-Yossef req_ctx->dma_buf_type = CC_DMA_BUF_MLLI;
41163ee04c8SGilad Ben-Yossef
41263ee04c8SGilad Ben-Yossef if (src == dst) {
41363ee04c8SGilad Ben-Yossef /* Handle inplace operation */
41463ee04c8SGilad Ben-Yossef if (req_ctx->dma_buf_type == CC_DMA_BUF_MLLI) {
41563ee04c8SGilad Ben-Yossef req_ctx->out_nents = 0;
41663ee04c8SGilad Ben-Yossef cc_add_sg_entry(dev, &sg_data, req_ctx->in_nents, src,
41763ee04c8SGilad Ben-Yossef nbytes, 0, true,
41863ee04c8SGilad Ben-Yossef &req_ctx->in_mlli_nents);
41963ee04c8SGilad Ben-Yossef }
42063ee04c8SGilad Ben-Yossef } else {
42163ee04c8SGilad Ben-Yossef /* Map the dst sg */
422a260436cSGilad Ben-Yossef rc = cc_map_sg(dev, dst, nbytes, DMA_FROM_DEVICE,
42363ee04c8SGilad Ben-Yossef &req_ctx->out_nents, LLI_MAX_NUM_OF_DATA_ENTRIES,
424ccba2f11SHadar Gat &dummy, &mapped_nents);
425ccba2f11SHadar Gat if (rc)
42663ee04c8SGilad Ben-Yossef goto cipher_exit;
42763ee04c8SGilad Ben-Yossef if (mapped_nents > 1)
42863ee04c8SGilad Ben-Yossef req_ctx->dma_buf_type = CC_DMA_BUF_MLLI;
42963ee04c8SGilad Ben-Yossef
43063ee04c8SGilad Ben-Yossef if (req_ctx->dma_buf_type == CC_DMA_BUF_MLLI) {
43163ee04c8SGilad Ben-Yossef cc_add_sg_entry(dev, &sg_data, req_ctx->in_nents, src,
43263ee04c8SGilad Ben-Yossef nbytes, 0, true,
43363ee04c8SGilad Ben-Yossef &req_ctx->in_mlli_nents);
43463ee04c8SGilad Ben-Yossef cc_add_sg_entry(dev, &sg_data, req_ctx->out_nents, dst,
43563ee04c8SGilad Ben-Yossef nbytes, 0, true,
43663ee04c8SGilad Ben-Yossef &req_ctx->out_mlli_nents);
43763ee04c8SGilad Ben-Yossef }
43863ee04c8SGilad Ben-Yossef }
43963ee04c8SGilad Ben-Yossef
44063ee04c8SGilad Ben-Yossef if (req_ctx->dma_buf_type == CC_DMA_BUF_MLLI) {
441040187a0SGeert Uytterhoeven mlli_params->curr_pool = drvdata->mlli_buffs_pool;
44263ee04c8SGilad Ben-Yossef rc = cc_generate_mlli(dev, &sg_data, mlli_params, flags);
44363ee04c8SGilad Ben-Yossef if (rc)
44463ee04c8SGilad Ben-Yossef goto cipher_exit;
44563ee04c8SGilad Ben-Yossef }
44663ee04c8SGilad Ben-Yossef
44763ee04c8SGilad Ben-Yossef dev_dbg(dev, "areq_ctx->dma_buf_type = %s\n",
44863ee04c8SGilad Ben-Yossef cc_dma_buf_type(req_ctx->dma_buf_type));
44963ee04c8SGilad Ben-Yossef
45063ee04c8SGilad Ben-Yossef return 0;
45163ee04c8SGilad Ben-Yossef
45263ee04c8SGilad Ben-Yossef cipher_exit:
45363ee04c8SGilad Ben-Yossef cc_unmap_cipher_request(dev, req_ctx, ivsize, src, dst);
45463ee04c8SGilad Ben-Yossef return rc;
45563ee04c8SGilad Ben-Yossef }
45663ee04c8SGilad Ben-Yossef
cc_unmap_aead_request(struct device * dev,struct aead_request * req)457ff27e85aSGilad Ben-Yossef void cc_unmap_aead_request(struct device *dev, struct aead_request *req)
458ff27e85aSGilad Ben-Yossef {
459*07547fa7SHerbert Xu struct aead_req_ctx *areq_ctx = aead_request_ctx_dma(req);
460ff27e85aSGilad Ben-Yossef unsigned int hw_iv_size = areq_ctx->hw_iv_size;
461ff27e85aSGilad Ben-Yossef struct cc_drvdata *drvdata = dev_get_drvdata(dev);
462a260436cSGilad Ben-Yossef int src_direction = (req->src != req->dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL);
463ff27e85aSGilad Ben-Yossef
464ff27e85aSGilad Ben-Yossef if (areq_ctx->mac_buf_dma_addr) {
465ff27e85aSGilad Ben-Yossef dma_unmap_single(dev, areq_ctx->mac_buf_dma_addr,
466ff27e85aSGilad Ben-Yossef MAX_MAC_SIZE, DMA_BIDIRECTIONAL);
467ff27e85aSGilad Ben-Yossef }
468ff27e85aSGilad Ben-Yossef
469ff27e85aSGilad Ben-Yossef if (areq_ctx->cipher_mode == DRV_CIPHER_GCTR) {
470ff27e85aSGilad Ben-Yossef if (areq_ctx->hkey_dma_addr) {
471ff27e85aSGilad Ben-Yossef dma_unmap_single(dev, areq_ctx->hkey_dma_addr,
472ff27e85aSGilad Ben-Yossef AES_BLOCK_SIZE, DMA_BIDIRECTIONAL);
473ff27e85aSGilad Ben-Yossef }
474ff27e85aSGilad Ben-Yossef
475ff27e85aSGilad Ben-Yossef if (areq_ctx->gcm_block_len_dma_addr) {
476ff27e85aSGilad Ben-Yossef dma_unmap_single(dev, areq_ctx->gcm_block_len_dma_addr,
477ff27e85aSGilad Ben-Yossef AES_BLOCK_SIZE, DMA_TO_DEVICE);
478ff27e85aSGilad Ben-Yossef }
479ff27e85aSGilad Ben-Yossef
480ff27e85aSGilad Ben-Yossef if (areq_ctx->gcm_iv_inc1_dma_addr) {
481ff27e85aSGilad Ben-Yossef dma_unmap_single(dev, areq_ctx->gcm_iv_inc1_dma_addr,
482ff27e85aSGilad Ben-Yossef AES_BLOCK_SIZE, DMA_TO_DEVICE);
483ff27e85aSGilad Ben-Yossef }
484ff27e85aSGilad Ben-Yossef
485ff27e85aSGilad Ben-Yossef if (areq_ctx->gcm_iv_inc2_dma_addr) {
486ff27e85aSGilad Ben-Yossef dma_unmap_single(dev, areq_ctx->gcm_iv_inc2_dma_addr,
487ff27e85aSGilad Ben-Yossef AES_BLOCK_SIZE, DMA_TO_DEVICE);
488ff27e85aSGilad Ben-Yossef }
489ff27e85aSGilad Ben-Yossef }
490ff27e85aSGilad Ben-Yossef
491ff27e85aSGilad Ben-Yossef if (areq_ctx->ccm_hdr_size != ccm_header_size_null) {
492ff27e85aSGilad Ben-Yossef if (areq_ctx->ccm_iv0_dma_addr) {
493ff27e85aSGilad Ben-Yossef dma_unmap_single(dev, areq_ctx->ccm_iv0_dma_addr,
494ff27e85aSGilad Ben-Yossef AES_BLOCK_SIZE, DMA_TO_DEVICE);
495ff27e85aSGilad Ben-Yossef }
496ff27e85aSGilad Ben-Yossef
497ff27e85aSGilad Ben-Yossef dma_unmap_sg(dev, &areq_ctx->ccm_adata_sg, 1, DMA_TO_DEVICE);
498ff27e85aSGilad Ben-Yossef }
499ff27e85aSGilad Ben-Yossef if (areq_ctx->gen_ctx.iv_dma_addr) {
500ff27e85aSGilad Ben-Yossef dma_unmap_single(dev, areq_ctx->gen_ctx.iv_dma_addr,
501ff27e85aSGilad Ben-Yossef hw_iv_size, DMA_BIDIRECTIONAL);
502453431a5SWaiman Long kfree_sensitive(areq_ctx->gen_ctx.iv);
503ff27e85aSGilad Ben-Yossef }
504ff27e85aSGilad Ben-Yossef
505a4941195SHadar Gat /* Release pool */
506a4941195SHadar Gat if ((areq_ctx->assoc_buff_type == CC_DMA_BUF_MLLI ||
507a4941195SHadar Gat areq_ctx->data_buff_type == CC_DMA_BUF_MLLI) &&
508a4941195SHadar Gat (areq_ctx->mlli_params.mlli_virt_addr)) {
509ff27e85aSGilad Ben-Yossef dev_dbg(dev, "free MLLI buffer: dma=%pad virt=%pK\n",
510ff27e85aSGilad Ben-Yossef &areq_ctx->mlli_params.mlli_dma_addr,
511ff27e85aSGilad Ben-Yossef areq_ctx->mlli_params.mlli_virt_addr);
512ff27e85aSGilad Ben-Yossef dma_pool_free(areq_ctx->mlli_params.curr_pool,
513ff27e85aSGilad Ben-Yossef areq_ctx->mlli_params.mlli_virt_addr,
514ff27e85aSGilad Ben-Yossef areq_ctx->mlli_params.mlli_dma_addr);
515ff27e85aSGilad Ben-Yossef }
516ff27e85aSGilad Ben-Yossef
517ff27e85aSGilad Ben-Yossef dev_dbg(dev, "Unmapping src sgl: req->src=%pK areq_ctx->src.nents=%u areq_ctx->assoc.nents=%u assoclen:%u cryptlen=%u\n",
518ff27e85aSGilad Ben-Yossef sg_virt(req->src), areq_ctx->src.nents, areq_ctx->assoc.nents,
519da3cf67fSGilad Ben-Yossef areq_ctx->assoclen, req->cryptlen);
520ff27e85aSGilad Ben-Yossef
521a260436cSGilad Ben-Yossef dma_unmap_sg(dev, req->src, areq_ctx->src.mapped_nents, src_direction);
522ff27e85aSGilad Ben-Yossef if (req->src != req->dst) {
523ff27e85aSGilad Ben-Yossef dev_dbg(dev, "Unmapping dst sgl: req->dst=%pK\n",
524ff27e85aSGilad Ben-Yossef sg_virt(req->dst));
525a260436cSGilad Ben-Yossef dma_unmap_sg(dev, req->dst, areq_ctx->dst.mapped_nents, DMA_FROM_DEVICE);
526ff27e85aSGilad Ben-Yossef }
527ff27e85aSGilad Ben-Yossef if (drvdata->coherent &&
528ff27e85aSGilad Ben-Yossef areq_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_DECRYPT &&
529ff27e85aSGilad Ben-Yossef req->src == req->dst) {
530ff27e85aSGilad Ben-Yossef /* copy back mac from temporary location to deal with possible
531ff27e85aSGilad Ben-Yossef * data memory overriding that caused by cache coherence
532ff27e85aSGilad Ben-Yossef * problem.
533ff27e85aSGilad Ben-Yossef */
534ff27e85aSGilad Ben-Yossef cc_copy_mac(dev, req, CC_SG_FROM_BUF);
535ff27e85aSGilad Ben-Yossef }
536ff27e85aSGilad Ben-Yossef }
537ff27e85aSGilad Ben-Yossef
cc_is_icv_frag(unsigned int sgl_nents,unsigned int authsize,u32 last_entry_data_size)5386825cfd6SGilad Ben-Yossef static bool cc_is_icv_frag(unsigned int sgl_nents, unsigned int authsize,
5396825cfd6SGilad Ben-Yossef u32 last_entry_data_size)
540ff27e85aSGilad Ben-Yossef {
5416825cfd6SGilad Ben-Yossef return ((sgl_nents > 1) && (last_entry_data_size < authsize));
542ff27e85aSGilad Ben-Yossef }
543ff27e85aSGilad Ben-Yossef
cc_aead_chain_iv(struct cc_drvdata * drvdata,struct aead_request * req,struct buffer_array * sg_data,bool is_last,bool do_chain)544ff27e85aSGilad Ben-Yossef static int cc_aead_chain_iv(struct cc_drvdata *drvdata,
545ff27e85aSGilad Ben-Yossef struct aead_request *req,
546ff27e85aSGilad Ben-Yossef struct buffer_array *sg_data,
547ff27e85aSGilad Ben-Yossef bool is_last, bool do_chain)
548ff27e85aSGilad Ben-Yossef {
549*07547fa7SHerbert Xu struct aead_req_ctx *areq_ctx = aead_request_ctx_dma(req);
550ff27e85aSGilad Ben-Yossef unsigned int hw_iv_size = areq_ctx->hw_iv_size;
551ff27e85aSGilad Ben-Yossef struct device *dev = drvdata_to_dev(drvdata);
552e8662a6aSGilad Ben-Yossef gfp_t flags = cc_gfp_flags(&req->base);
553ff27e85aSGilad Ben-Yossef int rc = 0;
554ff27e85aSGilad Ben-Yossef
555ff27e85aSGilad Ben-Yossef if (!req->iv) {
556ff27e85aSGilad Ben-Yossef areq_ctx->gen_ctx.iv_dma_addr = 0;
557e8662a6aSGilad Ben-Yossef areq_ctx->gen_ctx.iv = NULL;
558ff27e85aSGilad Ben-Yossef goto chain_iv_exit;
559ff27e85aSGilad Ben-Yossef }
560ff27e85aSGilad Ben-Yossef
561e8662a6aSGilad Ben-Yossef areq_ctx->gen_ctx.iv = kmemdup(req->iv, hw_iv_size, flags);
562e8662a6aSGilad Ben-Yossef if (!areq_ctx->gen_ctx.iv)
563e8662a6aSGilad Ben-Yossef return -ENOMEM;
564e8662a6aSGilad Ben-Yossef
565e8662a6aSGilad Ben-Yossef areq_ctx->gen_ctx.iv_dma_addr =
566e8662a6aSGilad Ben-Yossef dma_map_single(dev, areq_ctx->gen_ctx.iv, hw_iv_size,
567ff27e85aSGilad Ben-Yossef DMA_BIDIRECTIONAL);
568ff27e85aSGilad Ben-Yossef if (dma_mapping_error(dev, areq_ctx->gen_ctx.iv_dma_addr)) {
569ff27e85aSGilad Ben-Yossef dev_err(dev, "Mapping iv %u B at va=%pK for DMA failed\n",
570ff27e85aSGilad Ben-Yossef hw_iv_size, req->iv);
571453431a5SWaiman Long kfree_sensitive(areq_ctx->gen_ctx.iv);
572e8662a6aSGilad Ben-Yossef areq_ctx->gen_ctx.iv = NULL;
573ff27e85aSGilad Ben-Yossef rc = -ENOMEM;
574ff27e85aSGilad Ben-Yossef goto chain_iv_exit;
575ff27e85aSGilad Ben-Yossef }
576ff27e85aSGilad Ben-Yossef
577ff27e85aSGilad Ben-Yossef dev_dbg(dev, "Mapped iv %u B at va=%pK to dma=%pad\n",
578ff27e85aSGilad Ben-Yossef hw_iv_size, req->iv, &areq_ctx->gen_ctx.iv_dma_addr);
579ff27e85aSGilad Ben-Yossef
580ff27e85aSGilad Ben-Yossef chain_iv_exit:
581ff27e85aSGilad Ben-Yossef return rc;
582ff27e85aSGilad Ben-Yossef }
583ff27e85aSGilad Ben-Yossef
cc_aead_chain_assoc(struct cc_drvdata * drvdata,struct aead_request * req,struct buffer_array * sg_data,bool is_last,bool do_chain)584ff27e85aSGilad Ben-Yossef static int cc_aead_chain_assoc(struct cc_drvdata *drvdata,
585ff27e85aSGilad Ben-Yossef struct aead_request *req,
586ff27e85aSGilad Ben-Yossef struct buffer_array *sg_data,
587ff27e85aSGilad Ben-Yossef bool is_last, bool do_chain)
588ff27e85aSGilad Ben-Yossef {
589*07547fa7SHerbert Xu struct aead_req_ctx *areq_ctx = aead_request_ctx_dma(req);
590ff27e85aSGilad Ben-Yossef int rc = 0;
591c776f7d3SGilad Ben-Yossef int mapped_nents = 0;
592ff27e85aSGilad Ben-Yossef struct device *dev = drvdata_to_dev(drvdata);
593ff27e85aSGilad Ben-Yossef
594ff27e85aSGilad Ben-Yossef if (!sg_data) {
595ff27e85aSGilad Ben-Yossef rc = -EINVAL;
596ff27e85aSGilad Ben-Yossef goto chain_assoc_exit;
597ff27e85aSGilad Ben-Yossef }
598ff27e85aSGilad Ben-Yossef
599da3cf67fSGilad Ben-Yossef if (areq_ctx->assoclen == 0) {
600ff27e85aSGilad Ben-Yossef areq_ctx->assoc_buff_type = CC_DMA_BUF_NULL;
601ff27e85aSGilad Ben-Yossef areq_ctx->assoc.nents = 0;
602ff27e85aSGilad Ben-Yossef areq_ctx->assoc.mlli_nents = 0;
603ff27e85aSGilad Ben-Yossef dev_dbg(dev, "Chain assoc of length 0: buff_type=%s nents=%u\n",
604ff27e85aSGilad Ben-Yossef cc_dma_buf_type(areq_ctx->assoc_buff_type),
605ff27e85aSGilad Ben-Yossef areq_ctx->assoc.nents);
606ff27e85aSGilad Ben-Yossef goto chain_assoc_exit;
607ff27e85aSGilad Ben-Yossef }
608ff27e85aSGilad Ben-Yossef
6090eae14a0SGilad Ben-Yossef mapped_nents = sg_nents_for_len(req->src, areq_ctx->assoclen);
610c776f7d3SGilad Ben-Yossef if (mapped_nents < 0)
611c776f7d3SGilad Ben-Yossef return mapped_nents;
612c776f7d3SGilad Ben-Yossef
613ff27e85aSGilad Ben-Yossef if (mapped_nents > LLI_MAX_NUM_OF_ASSOC_DATA_ENTRIES) {
614ff27e85aSGilad Ben-Yossef dev_err(dev, "Too many fragments. current %d max %d\n",
615ff27e85aSGilad Ben-Yossef mapped_nents, LLI_MAX_NUM_OF_ASSOC_DATA_ENTRIES);
616ff27e85aSGilad Ben-Yossef return -ENOMEM;
617ff27e85aSGilad Ben-Yossef }
618ff27e85aSGilad Ben-Yossef areq_ctx->assoc.nents = mapped_nents;
619ff27e85aSGilad Ben-Yossef
620ff27e85aSGilad Ben-Yossef /* in CCM case we have additional entry for
621ff27e85aSGilad Ben-Yossef * ccm header configurations
622ff27e85aSGilad Ben-Yossef */
623ff27e85aSGilad Ben-Yossef if (areq_ctx->ccm_hdr_size != ccm_header_size_null) {
624ff27e85aSGilad Ben-Yossef if ((mapped_nents + 1) > LLI_MAX_NUM_OF_ASSOC_DATA_ENTRIES) {
625ff27e85aSGilad Ben-Yossef dev_err(dev, "CCM case.Too many fragments. Current %d max %d\n",
626ff27e85aSGilad Ben-Yossef (areq_ctx->assoc.nents + 1),
627ff27e85aSGilad Ben-Yossef LLI_MAX_NUM_OF_ASSOC_DATA_ENTRIES);
628ff27e85aSGilad Ben-Yossef rc = -ENOMEM;
629ff27e85aSGilad Ben-Yossef goto chain_assoc_exit;
630ff27e85aSGilad Ben-Yossef }
631ff27e85aSGilad Ben-Yossef }
632ff27e85aSGilad Ben-Yossef
633ff27e85aSGilad Ben-Yossef if (mapped_nents == 1 && areq_ctx->ccm_hdr_size == ccm_header_size_null)
634ff27e85aSGilad Ben-Yossef areq_ctx->assoc_buff_type = CC_DMA_BUF_DLLI;
635ff27e85aSGilad Ben-Yossef else
636ff27e85aSGilad Ben-Yossef areq_ctx->assoc_buff_type = CC_DMA_BUF_MLLI;
637ff27e85aSGilad Ben-Yossef
638ff27e85aSGilad Ben-Yossef if (do_chain || areq_ctx->assoc_buff_type == CC_DMA_BUF_MLLI) {
639ff27e85aSGilad Ben-Yossef dev_dbg(dev, "Chain assoc: buff_type=%s nents=%u\n",
640ff27e85aSGilad Ben-Yossef cc_dma_buf_type(areq_ctx->assoc_buff_type),
641ff27e85aSGilad Ben-Yossef areq_ctx->assoc.nents);
642ff27e85aSGilad Ben-Yossef cc_add_sg_entry(dev, sg_data, areq_ctx->assoc.nents, req->src,
643da3cf67fSGilad Ben-Yossef areq_ctx->assoclen, 0, is_last,
644ff27e85aSGilad Ben-Yossef &areq_ctx->assoc.mlli_nents);
645ff27e85aSGilad Ben-Yossef areq_ctx->assoc_buff_type = CC_DMA_BUF_MLLI;
646ff27e85aSGilad Ben-Yossef }
647ff27e85aSGilad Ben-Yossef
648ff27e85aSGilad Ben-Yossef chain_assoc_exit:
649ff27e85aSGilad Ben-Yossef return rc;
650ff27e85aSGilad Ben-Yossef }
651ff27e85aSGilad Ben-Yossef
cc_prepare_aead_data_dlli(struct aead_request * req,u32 * src_last_bytes,u32 * dst_last_bytes)652ff27e85aSGilad Ben-Yossef static void cc_prepare_aead_data_dlli(struct aead_request *req,
653ff27e85aSGilad Ben-Yossef u32 *src_last_bytes, u32 *dst_last_bytes)
654ff27e85aSGilad Ben-Yossef {
655*07547fa7SHerbert Xu struct aead_req_ctx *areq_ctx = aead_request_ctx_dma(req);
656ff27e85aSGilad Ben-Yossef enum drv_crypto_direction direct = areq_ctx->gen_ctx.op_type;
657ff27e85aSGilad Ben-Yossef unsigned int authsize = areq_ctx->req_authsize;
658c9877cbcSGilad Ben-Yossef struct scatterlist *sg;
659c9877cbcSGilad Ben-Yossef ssize_t offset;
660ff27e85aSGilad Ben-Yossef
661ff27e85aSGilad Ben-Yossef areq_ctx->is_icv_fragmented = false;
662c9877cbcSGilad Ben-Yossef
663c9877cbcSGilad Ben-Yossef if ((req->src == req->dst) || direct == DRV_CRYPTO_DIRECTION_DECRYPT) {
664c9877cbcSGilad Ben-Yossef sg = areq_ctx->src_sgl;
665c9877cbcSGilad Ben-Yossef offset = *src_last_bytes - authsize;
666ff27e85aSGilad Ben-Yossef } else {
667c9877cbcSGilad Ben-Yossef sg = areq_ctx->dst_sgl;
668c9877cbcSGilad Ben-Yossef offset = *dst_last_bytes - authsize;
669ff27e85aSGilad Ben-Yossef }
670c9877cbcSGilad Ben-Yossef
671c9877cbcSGilad Ben-Yossef areq_ctx->icv_dma_addr = sg_dma_address(sg) + offset;
672c9877cbcSGilad Ben-Yossef areq_ctx->icv_virt_addr = sg_virt(sg) + offset;
673ff27e85aSGilad Ben-Yossef }
674ff27e85aSGilad Ben-Yossef
cc_prepare_aead_data_mlli(struct cc_drvdata * drvdata,struct aead_request * req,struct buffer_array * sg_data,u32 * src_last_bytes,u32 * dst_last_bytes,bool is_last_table)6756825cfd6SGilad Ben-Yossef static void cc_prepare_aead_data_mlli(struct cc_drvdata *drvdata,
676ff27e85aSGilad Ben-Yossef struct aead_request *req,
677ff27e85aSGilad Ben-Yossef struct buffer_array *sg_data,
678ff27e85aSGilad Ben-Yossef u32 *src_last_bytes, u32 *dst_last_bytes,
679ff27e85aSGilad Ben-Yossef bool is_last_table)
680ff27e85aSGilad Ben-Yossef {
681*07547fa7SHerbert Xu struct aead_req_ctx *areq_ctx = aead_request_ctx_dma(req);
682ff27e85aSGilad Ben-Yossef enum drv_crypto_direction direct = areq_ctx->gen_ctx.op_type;
683ff27e85aSGilad Ben-Yossef unsigned int authsize = areq_ctx->req_authsize;
684ff27e85aSGilad Ben-Yossef struct device *dev = drvdata_to_dev(drvdata);
685ff27e85aSGilad Ben-Yossef struct scatterlist *sg;
686ff27e85aSGilad Ben-Yossef
687ff27e85aSGilad Ben-Yossef if (req->src == req->dst) {
688ff27e85aSGilad Ben-Yossef /*INPLACE*/
689ff27e85aSGilad Ben-Yossef cc_add_sg_entry(dev, sg_data, areq_ctx->src.nents,
690ff27e85aSGilad Ben-Yossef areq_ctx->src_sgl, areq_ctx->cryptlen,
691ff27e85aSGilad Ben-Yossef areq_ctx->src_offset, is_last_table,
692ff27e85aSGilad Ben-Yossef &areq_ctx->src.mlli_nents);
693ff27e85aSGilad Ben-Yossef
6946825cfd6SGilad Ben-Yossef areq_ctx->is_icv_fragmented =
6956825cfd6SGilad Ben-Yossef cc_is_icv_frag(areq_ctx->src.nents, authsize,
6966825cfd6SGilad Ben-Yossef *src_last_bytes);
697ff27e85aSGilad Ben-Yossef
698ff27e85aSGilad Ben-Yossef if (areq_ctx->is_icv_fragmented) {
699ff27e85aSGilad Ben-Yossef /* Backup happens only when ICV is fragmented, ICV
700ff27e85aSGilad Ben-Yossef * verification is made by CPU compare in order to
701ff27e85aSGilad Ben-Yossef * simplify MAC verification upon request completion
702ff27e85aSGilad Ben-Yossef */
703ff27e85aSGilad Ben-Yossef if (direct == DRV_CRYPTO_DIRECTION_DECRYPT) {
704ff27e85aSGilad Ben-Yossef /* In coherent platforms (e.g. ACP)
705ff27e85aSGilad Ben-Yossef * already copying ICV for any
706ff27e85aSGilad Ben-Yossef * INPLACE-DECRYPT operation, hence
707ff27e85aSGilad Ben-Yossef * we must neglect this code.
708ff27e85aSGilad Ben-Yossef */
709ff27e85aSGilad Ben-Yossef if (!drvdata->coherent)
710ff27e85aSGilad Ben-Yossef cc_copy_mac(dev, req, CC_SG_TO_BUF);
711ff27e85aSGilad Ben-Yossef
712ff27e85aSGilad Ben-Yossef areq_ctx->icv_virt_addr = areq_ctx->backup_mac;
713ff27e85aSGilad Ben-Yossef } else {
714ff27e85aSGilad Ben-Yossef areq_ctx->icv_virt_addr = areq_ctx->mac_buf;
715ff27e85aSGilad Ben-Yossef areq_ctx->icv_dma_addr =
716ff27e85aSGilad Ben-Yossef areq_ctx->mac_buf_dma_addr;
717ff27e85aSGilad Ben-Yossef }
718ff27e85aSGilad Ben-Yossef } else { /* Contig. ICV */
719ff27e85aSGilad Ben-Yossef sg = &areq_ctx->src_sgl[areq_ctx->src.nents - 1];
720ff27e85aSGilad Ben-Yossef /*Should hanlde if the sg is not contig.*/
721ff27e85aSGilad Ben-Yossef areq_ctx->icv_dma_addr = sg_dma_address(sg) +
722ff27e85aSGilad Ben-Yossef (*src_last_bytes - authsize);
723ff27e85aSGilad Ben-Yossef areq_ctx->icv_virt_addr = sg_virt(sg) +
724ff27e85aSGilad Ben-Yossef (*src_last_bytes - authsize);
725ff27e85aSGilad Ben-Yossef }
726ff27e85aSGilad Ben-Yossef
727ff27e85aSGilad Ben-Yossef } else if (direct == DRV_CRYPTO_DIRECTION_DECRYPT) {
728ff27e85aSGilad Ben-Yossef /*NON-INPLACE and DECRYPT*/
729ff27e85aSGilad Ben-Yossef cc_add_sg_entry(dev, sg_data, areq_ctx->src.nents,
730ff27e85aSGilad Ben-Yossef areq_ctx->src_sgl, areq_ctx->cryptlen,
731ff27e85aSGilad Ben-Yossef areq_ctx->src_offset, is_last_table,
732ff27e85aSGilad Ben-Yossef &areq_ctx->src.mlli_nents);
733ff27e85aSGilad Ben-Yossef cc_add_sg_entry(dev, sg_data, areq_ctx->dst.nents,
734ff27e85aSGilad Ben-Yossef areq_ctx->dst_sgl, areq_ctx->cryptlen,
735ff27e85aSGilad Ben-Yossef areq_ctx->dst_offset, is_last_table,
736ff27e85aSGilad Ben-Yossef &areq_ctx->dst.mlli_nents);
737ff27e85aSGilad Ben-Yossef
7386825cfd6SGilad Ben-Yossef areq_ctx->is_icv_fragmented =
7396825cfd6SGilad Ben-Yossef cc_is_icv_frag(areq_ctx->src.nents, authsize,
7406825cfd6SGilad Ben-Yossef *src_last_bytes);
741ff27e85aSGilad Ben-Yossef /* Backup happens only when ICV is fragmented, ICV
7426825cfd6SGilad Ben-Yossef
743ff27e85aSGilad Ben-Yossef * verification is made by CPU compare in order to simplify
744ff27e85aSGilad Ben-Yossef * MAC verification upon request completion
745ff27e85aSGilad Ben-Yossef */
746ff27e85aSGilad Ben-Yossef if (areq_ctx->is_icv_fragmented) {
747ff27e85aSGilad Ben-Yossef cc_copy_mac(dev, req, CC_SG_TO_BUF);
748ff27e85aSGilad Ben-Yossef areq_ctx->icv_virt_addr = areq_ctx->backup_mac;
749ff27e85aSGilad Ben-Yossef
750ff27e85aSGilad Ben-Yossef } else { /* Contig. ICV */
751ff27e85aSGilad Ben-Yossef sg = &areq_ctx->src_sgl[areq_ctx->src.nents - 1];
752ff27e85aSGilad Ben-Yossef /*Should hanlde if the sg is not contig.*/
753ff27e85aSGilad Ben-Yossef areq_ctx->icv_dma_addr = sg_dma_address(sg) +
754ff27e85aSGilad Ben-Yossef (*src_last_bytes - authsize);
755ff27e85aSGilad Ben-Yossef areq_ctx->icv_virt_addr = sg_virt(sg) +
756ff27e85aSGilad Ben-Yossef (*src_last_bytes - authsize);
757ff27e85aSGilad Ben-Yossef }
758ff27e85aSGilad Ben-Yossef
759ff27e85aSGilad Ben-Yossef } else {
760ff27e85aSGilad Ben-Yossef /*NON-INPLACE and ENCRYPT*/
761ff27e85aSGilad Ben-Yossef cc_add_sg_entry(dev, sg_data, areq_ctx->dst.nents,
762ff27e85aSGilad Ben-Yossef areq_ctx->dst_sgl, areq_ctx->cryptlen,
763ff27e85aSGilad Ben-Yossef areq_ctx->dst_offset, is_last_table,
764ff27e85aSGilad Ben-Yossef &areq_ctx->dst.mlli_nents);
765ff27e85aSGilad Ben-Yossef cc_add_sg_entry(dev, sg_data, areq_ctx->src.nents,
766ff27e85aSGilad Ben-Yossef areq_ctx->src_sgl, areq_ctx->cryptlen,
767ff27e85aSGilad Ben-Yossef areq_ctx->src_offset, is_last_table,
768ff27e85aSGilad Ben-Yossef &areq_ctx->src.mlli_nents);
769ff27e85aSGilad Ben-Yossef
7706825cfd6SGilad Ben-Yossef areq_ctx->is_icv_fragmented =
7716825cfd6SGilad Ben-Yossef cc_is_icv_frag(areq_ctx->dst.nents, authsize,
7726825cfd6SGilad Ben-Yossef *dst_last_bytes);
773ff27e85aSGilad Ben-Yossef
774ff27e85aSGilad Ben-Yossef if (!areq_ctx->is_icv_fragmented) {
775ff27e85aSGilad Ben-Yossef sg = &areq_ctx->dst_sgl[areq_ctx->dst.nents - 1];
776ff27e85aSGilad Ben-Yossef /* Contig. ICV */
777ff27e85aSGilad Ben-Yossef areq_ctx->icv_dma_addr = sg_dma_address(sg) +
778ff27e85aSGilad Ben-Yossef (*dst_last_bytes - authsize);
779ff27e85aSGilad Ben-Yossef areq_ctx->icv_virt_addr = sg_virt(sg) +
780ff27e85aSGilad Ben-Yossef (*dst_last_bytes - authsize);
781ff27e85aSGilad Ben-Yossef } else {
782ff27e85aSGilad Ben-Yossef areq_ctx->icv_dma_addr = areq_ctx->mac_buf_dma_addr;
783ff27e85aSGilad Ben-Yossef areq_ctx->icv_virt_addr = areq_ctx->mac_buf;
784ff27e85aSGilad Ben-Yossef }
785ff27e85aSGilad Ben-Yossef }
786ff27e85aSGilad Ben-Yossef }
787ff27e85aSGilad Ben-Yossef
cc_aead_chain_data(struct cc_drvdata * drvdata,struct aead_request * req,struct buffer_array * sg_data,bool is_last_table,bool do_chain)788ff27e85aSGilad Ben-Yossef static int cc_aead_chain_data(struct cc_drvdata *drvdata,
789ff27e85aSGilad Ben-Yossef struct aead_request *req,
790ff27e85aSGilad Ben-Yossef struct buffer_array *sg_data,
791ff27e85aSGilad Ben-Yossef bool is_last_table, bool do_chain)
792ff27e85aSGilad Ben-Yossef {
793*07547fa7SHerbert Xu struct aead_req_ctx *areq_ctx = aead_request_ctx_dma(req);
794ff27e85aSGilad Ben-Yossef struct device *dev = drvdata_to_dev(drvdata);
795ff27e85aSGilad Ben-Yossef enum drv_crypto_direction direct = areq_ctx->gen_ctx.op_type;
796ff27e85aSGilad Ben-Yossef unsigned int authsize = areq_ctx->req_authsize;
797ff27e85aSGilad Ben-Yossef unsigned int src_last_bytes = 0, dst_last_bytes = 0;
798ff27e85aSGilad Ben-Yossef int rc = 0;
799ff27e85aSGilad Ben-Yossef u32 src_mapped_nents = 0, dst_mapped_nents = 0;
800ff27e85aSGilad Ben-Yossef u32 offset = 0;
801ff27e85aSGilad Ben-Yossef /* non-inplace mode */
8020eae14a0SGilad Ben-Yossef unsigned int size_for_map = req->assoclen + req->cryptlen;
803ff27e85aSGilad Ben-Yossef u32 sg_index = 0;
8040eae14a0SGilad Ben-Yossef u32 size_to_skip = req->assoclen;
805c776f7d3SGilad Ben-Yossef struct scatterlist *sgl;
806ff27e85aSGilad Ben-Yossef
807ff27e85aSGilad Ben-Yossef offset = size_to_skip;
808ff27e85aSGilad Ben-Yossef
809ff27e85aSGilad Ben-Yossef if (!sg_data)
810ff27e85aSGilad Ben-Yossef return -EINVAL;
811ff27e85aSGilad Ben-Yossef
812ff27e85aSGilad Ben-Yossef areq_ctx->src_sgl = req->src;
813ff27e85aSGilad Ben-Yossef areq_ctx->dst_sgl = req->dst;
814ff27e85aSGilad Ben-Yossef
815ff27e85aSGilad Ben-Yossef size_for_map += (direct == DRV_CRYPTO_DIRECTION_ENCRYPT) ?
816ff27e85aSGilad Ben-Yossef authsize : 0;
817ff27e85aSGilad Ben-Yossef src_mapped_nents = cc_get_sgl_nents(dev, req->src, size_for_map,
818c4b22bf5SGilad Ben-Yossef &src_last_bytes);
819ff27e85aSGilad Ben-Yossef sg_index = areq_ctx->src_sgl->length;
820ff27e85aSGilad Ben-Yossef //check where the data starts
821ce0fc6dbSGilad Ben-Yossef while (src_mapped_nents && (sg_index <= size_to_skip)) {
822ff27e85aSGilad Ben-Yossef src_mapped_nents--;
823c776f7d3SGilad Ben-Yossef offset -= areq_ctx->src_sgl->length;
824c776f7d3SGilad Ben-Yossef sgl = sg_next(areq_ctx->src_sgl);
825c776f7d3SGilad Ben-Yossef if (!sgl)
826c776f7d3SGilad Ben-Yossef break;
827c776f7d3SGilad Ben-Yossef areq_ctx->src_sgl = sgl;
828c776f7d3SGilad Ben-Yossef sg_index += areq_ctx->src_sgl->length;
829ff27e85aSGilad Ben-Yossef }
830ff27e85aSGilad Ben-Yossef if (src_mapped_nents > LLI_MAX_NUM_OF_DATA_ENTRIES) {
831ff27e85aSGilad Ben-Yossef dev_err(dev, "Too many fragments. current %d max %d\n",
832ff27e85aSGilad Ben-Yossef src_mapped_nents, LLI_MAX_NUM_OF_DATA_ENTRIES);
833ff27e85aSGilad Ben-Yossef return -ENOMEM;
834ff27e85aSGilad Ben-Yossef }
835ff27e85aSGilad Ben-Yossef
836ff27e85aSGilad Ben-Yossef areq_ctx->src.nents = src_mapped_nents;
837ff27e85aSGilad Ben-Yossef
838ff27e85aSGilad Ben-Yossef areq_ctx->src_offset = offset;
839ff27e85aSGilad Ben-Yossef
840ff27e85aSGilad Ben-Yossef if (req->src != req->dst) {
8410eae14a0SGilad Ben-Yossef size_for_map = req->assoclen + req->cryptlen;
8428962c6d2SGilad Ben-Yossef
8438962c6d2SGilad Ben-Yossef if (direct == DRV_CRYPTO_DIRECTION_ENCRYPT)
8448962c6d2SGilad Ben-Yossef size_for_map += authsize;
8458962c6d2SGilad Ben-Yossef else
8468962c6d2SGilad Ben-Yossef size_for_map -= authsize;
8478962c6d2SGilad Ben-Yossef
848a260436cSGilad Ben-Yossef rc = cc_map_sg(dev, req->dst, size_for_map, DMA_FROM_DEVICE,
849ce0fc6dbSGilad Ben-Yossef &areq_ctx->dst.mapped_nents,
850ff27e85aSGilad Ben-Yossef LLI_MAX_NUM_OF_DATA_ENTRIES, &dst_last_bytes,
851ff27e85aSGilad Ben-Yossef &dst_mapped_nents);
852ccba2f11SHadar Gat if (rc)
853ff27e85aSGilad Ben-Yossef goto chain_data_exit;
854ff27e85aSGilad Ben-Yossef }
855ff27e85aSGilad Ben-Yossef
856ff27e85aSGilad Ben-Yossef dst_mapped_nents = cc_get_sgl_nents(dev, req->dst, size_for_map,
857c4b22bf5SGilad Ben-Yossef &dst_last_bytes);
858ff27e85aSGilad Ben-Yossef sg_index = areq_ctx->dst_sgl->length;
859ff27e85aSGilad Ben-Yossef offset = size_to_skip;
860ff27e85aSGilad Ben-Yossef
861ff27e85aSGilad Ben-Yossef //check where the data starts
862ce0fc6dbSGilad Ben-Yossef while (dst_mapped_nents && sg_index <= size_to_skip) {
863ff27e85aSGilad Ben-Yossef dst_mapped_nents--;
864c776f7d3SGilad Ben-Yossef offset -= areq_ctx->dst_sgl->length;
865c776f7d3SGilad Ben-Yossef sgl = sg_next(areq_ctx->dst_sgl);
866c776f7d3SGilad Ben-Yossef if (!sgl)
867c776f7d3SGilad Ben-Yossef break;
868c776f7d3SGilad Ben-Yossef areq_ctx->dst_sgl = sgl;
869c776f7d3SGilad Ben-Yossef sg_index += areq_ctx->dst_sgl->length;
870ff27e85aSGilad Ben-Yossef }
871ff27e85aSGilad Ben-Yossef if (dst_mapped_nents > LLI_MAX_NUM_OF_DATA_ENTRIES) {
872ff27e85aSGilad Ben-Yossef dev_err(dev, "Too many fragments. current %d max %d\n",
873ff27e85aSGilad Ben-Yossef dst_mapped_nents, LLI_MAX_NUM_OF_DATA_ENTRIES);
874ff27e85aSGilad Ben-Yossef return -ENOMEM;
875ff27e85aSGilad Ben-Yossef }
876ff27e85aSGilad Ben-Yossef areq_ctx->dst.nents = dst_mapped_nents;
877ff27e85aSGilad Ben-Yossef areq_ctx->dst_offset = offset;
878ff27e85aSGilad Ben-Yossef if (src_mapped_nents > 1 ||
879ff27e85aSGilad Ben-Yossef dst_mapped_nents > 1 ||
880ff27e85aSGilad Ben-Yossef do_chain) {
881ff27e85aSGilad Ben-Yossef areq_ctx->data_buff_type = CC_DMA_BUF_MLLI;
8826825cfd6SGilad Ben-Yossef cc_prepare_aead_data_mlli(drvdata, req, sg_data,
8836825cfd6SGilad Ben-Yossef &src_last_bytes, &dst_last_bytes,
8846825cfd6SGilad Ben-Yossef is_last_table);
885ff27e85aSGilad Ben-Yossef } else {
886ff27e85aSGilad Ben-Yossef areq_ctx->data_buff_type = CC_DMA_BUF_DLLI;
887ff27e85aSGilad Ben-Yossef cc_prepare_aead_data_dlli(req, &src_last_bytes,
888ff27e85aSGilad Ben-Yossef &dst_last_bytes);
889ff27e85aSGilad Ben-Yossef }
890ff27e85aSGilad Ben-Yossef
891ff27e85aSGilad Ben-Yossef chain_data_exit:
892ff27e85aSGilad Ben-Yossef return rc;
893ff27e85aSGilad Ben-Yossef }
894ff27e85aSGilad Ben-Yossef
cc_update_aead_mlli_nents(struct cc_drvdata * drvdata,struct aead_request * req)895ff27e85aSGilad Ben-Yossef static void cc_update_aead_mlli_nents(struct cc_drvdata *drvdata,
896ff27e85aSGilad Ben-Yossef struct aead_request *req)
897ff27e85aSGilad Ben-Yossef {
898*07547fa7SHerbert Xu struct aead_req_ctx *areq_ctx = aead_request_ctx_dma(req);
899ff27e85aSGilad Ben-Yossef u32 curr_mlli_size = 0;
900ff27e85aSGilad Ben-Yossef
901ff27e85aSGilad Ben-Yossef if (areq_ctx->assoc_buff_type == CC_DMA_BUF_MLLI) {
902ff27e85aSGilad Ben-Yossef areq_ctx->assoc.sram_addr = drvdata->mlli_sram_addr;
903ff27e85aSGilad Ben-Yossef curr_mlli_size = areq_ctx->assoc.mlli_nents *
904ff27e85aSGilad Ben-Yossef LLI_ENTRY_BYTE_SIZE;
905ff27e85aSGilad Ben-Yossef }
906ff27e85aSGilad Ben-Yossef
907ff27e85aSGilad Ben-Yossef if (areq_ctx->data_buff_type == CC_DMA_BUF_MLLI) {
908ff27e85aSGilad Ben-Yossef /*Inplace case dst nents equal to src nents*/
909ff27e85aSGilad Ben-Yossef if (req->src == req->dst) {
910ff27e85aSGilad Ben-Yossef areq_ctx->dst.mlli_nents = areq_ctx->src.mlli_nents;
911ff27e85aSGilad Ben-Yossef areq_ctx->src.sram_addr = drvdata->mlli_sram_addr +
912ff27e85aSGilad Ben-Yossef curr_mlli_size;
913ff27e85aSGilad Ben-Yossef areq_ctx->dst.sram_addr = areq_ctx->src.sram_addr;
914ff27e85aSGilad Ben-Yossef if (!areq_ctx->is_single_pass)
915ff27e85aSGilad Ben-Yossef areq_ctx->assoc.mlli_nents +=
916ff27e85aSGilad Ben-Yossef areq_ctx->src.mlli_nents;
917ff27e85aSGilad Ben-Yossef } else {
918ff27e85aSGilad Ben-Yossef if (areq_ctx->gen_ctx.op_type ==
919ff27e85aSGilad Ben-Yossef DRV_CRYPTO_DIRECTION_DECRYPT) {
920ff27e85aSGilad Ben-Yossef areq_ctx->src.sram_addr =
921ff27e85aSGilad Ben-Yossef drvdata->mlli_sram_addr +
922ff27e85aSGilad Ben-Yossef curr_mlli_size;
923ff27e85aSGilad Ben-Yossef areq_ctx->dst.sram_addr =
924ff27e85aSGilad Ben-Yossef areq_ctx->src.sram_addr +
925ff27e85aSGilad Ben-Yossef areq_ctx->src.mlli_nents *
926ff27e85aSGilad Ben-Yossef LLI_ENTRY_BYTE_SIZE;
927ff27e85aSGilad Ben-Yossef if (!areq_ctx->is_single_pass)
928ff27e85aSGilad Ben-Yossef areq_ctx->assoc.mlli_nents +=
929ff27e85aSGilad Ben-Yossef areq_ctx->src.mlli_nents;
930ff27e85aSGilad Ben-Yossef } else {
931ff27e85aSGilad Ben-Yossef areq_ctx->dst.sram_addr =
932ff27e85aSGilad Ben-Yossef drvdata->mlli_sram_addr +
933ff27e85aSGilad Ben-Yossef curr_mlli_size;
934ff27e85aSGilad Ben-Yossef areq_ctx->src.sram_addr =
935ff27e85aSGilad Ben-Yossef areq_ctx->dst.sram_addr +
936ff27e85aSGilad Ben-Yossef areq_ctx->dst.mlli_nents *
937ff27e85aSGilad Ben-Yossef LLI_ENTRY_BYTE_SIZE;
938ff27e85aSGilad Ben-Yossef if (!areq_ctx->is_single_pass)
939ff27e85aSGilad Ben-Yossef areq_ctx->assoc.mlli_nents +=
940ff27e85aSGilad Ben-Yossef areq_ctx->dst.mlli_nents;
941ff27e85aSGilad Ben-Yossef }
942ff27e85aSGilad Ben-Yossef }
943ff27e85aSGilad Ben-Yossef }
944ff27e85aSGilad Ben-Yossef }
945ff27e85aSGilad Ben-Yossef
cc_map_aead_request(struct cc_drvdata * drvdata,struct aead_request * req)946ff27e85aSGilad Ben-Yossef int cc_map_aead_request(struct cc_drvdata *drvdata, struct aead_request *req)
947ff27e85aSGilad Ben-Yossef {
948*07547fa7SHerbert Xu struct aead_req_ctx *areq_ctx = aead_request_ctx_dma(req);
949ff27e85aSGilad Ben-Yossef struct mlli_params *mlli_params = &areq_ctx->mlli_params;
950ff27e85aSGilad Ben-Yossef struct device *dev = drvdata_to_dev(drvdata);
951ff27e85aSGilad Ben-Yossef struct buffer_array sg_data;
952ff27e85aSGilad Ben-Yossef unsigned int authsize = areq_ctx->req_authsize;
953ff27e85aSGilad Ben-Yossef int rc = 0;
954ff27e85aSGilad Ben-Yossef dma_addr_t dma_addr;
955ff27e85aSGilad Ben-Yossef u32 mapped_nents = 0;
956ff27e85aSGilad Ben-Yossef u32 dummy = 0; /*used for the assoc data fragments */
9570eae14a0SGilad Ben-Yossef u32 size_to_map;
958ff27e85aSGilad Ben-Yossef gfp_t flags = cc_gfp_flags(&req->base);
959ff27e85aSGilad Ben-Yossef
960ff27e85aSGilad Ben-Yossef mlli_params->curr_pool = NULL;
961ff27e85aSGilad Ben-Yossef sg_data.num_of_buffers = 0;
962ff27e85aSGilad Ben-Yossef
963ff27e85aSGilad Ben-Yossef /* copy mac to a temporary location to deal with possible
964ff27e85aSGilad Ben-Yossef * data memory overriding that caused by cache coherence problem.
965ff27e85aSGilad Ben-Yossef */
966ff27e85aSGilad Ben-Yossef if (drvdata->coherent &&
967ff27e85aSGilad Ben-Yossef areq_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_DECRYPT &&
968ff27e85aSGilad Ben-Yossef req->src == req->dst)
969ff27e85aSGilad Ben-Yossef cc_copy_mac(dev, req, CC_SG_TO_BUF);
970ff27e85aSGilad Ben-Yossef
971ff27e85aSGilad Ben-Yossef /* cacluate the size for cipher remove ICV in decrypt*/
972ff27e85aSGilad Ben-Yossef areq_ctx->cryptlen = (areq_ctx->gen_ctx.op_type ==
973ff27e85aSGilad Ben-Yossef DRV_CRYPTO_DIRECTION_ENCRYPT) ?
974ff27e85aSGilad Ben-Yossef req->cryptlen :
975ff27e85aSGilad Ben-Yossef (req->cryptlen - authsize);
976ff27e85aSGilad Ben-Yossef
977ff27e85aSGilad Ben-Yossef dma_addr = dma_map_single(dev, areq_ctx->mac_buf, MAX_MAC_SIZE,
978ff27e85aSGilad Ben-Yossef DMA_BIDIRECTIONAL);
979ff27e85aSGilad Ben-Yossef if (dma_mapping_error(dev, dma_addr)) {
980ff27e85aSGilad Ben-Yossef dev_err(dev, "Mapping mac_buf %u B at va=%pK for DMA failed\n",
981ff27e85aSGilad Ben-Yossef MAX_MAC_SIZE, areq_ctx->mac_buf);
982ff27e85aSGilad Ben-Yossef rc = -ENOMEM;
983ff27e85aSGilad Ben-Yossef goto aead_map_failure;
984ff27e85aSGilad Ben-Yossef }
985ff27e85aSGilad Ben-Yossef areq_ctx->mac_buf_dma_addr = dma_addr;
986ff27e85aSGilad Ben-Yossef
987ff27e85aSGilad Ben-Yossef if (areq_ctx->ccm_hdr_size != ccm_header_size_null) {
988ff27e85aSGilad Ben-Yossef void *addr = areq_ctx->ccm_config + CCM_CTR_COUNT_0_OFFSET;
989ff27e85aSGilad Ben-Yossef
990ff27e85aSGilad Ben-Yossef dma_addr = dma_map_single(dev, addr, AES_BLOCK_SIZE,
991ff27e85aSGilad Ben-Yossef DMA_TO_DEVICE);
992ff27e85aSGilad Ben-Yossef
993ff27e85aSGilad Ben-Yossef if (dma_mapping_error(dev, dma_addr)) {
994ff27e85aSGilad Ben-Yossef dev_err(dev, "Mapping mac_buf %u B at va=%pK for DMA failed\n",
995ff27e85aSGilad Ben-Yossef AES_BLOCK_SIZE, addr);
996ff27e85aSGilad Ben-Yossef areq_ctx->ccm_iv0_dma_addr = 0;
997ff27e85aSGilad Ben-Yossef rc = -ENOMEM;
998ff27e85aSGilad Ben-Yossef goto aead_map_failure;
999ff27e85aSGilad Ben-Yossef }
1000ff27e85aSGilad Ben-Yossef areq_ctx->ccm_iv0_dma_addr = dma_addr;
1001ff27e85aSGilad Ben-Yossef
1002ccba2f11SHadar Gat rc = cc_set_aead_conf_buf(dev, areq_ctx, areq_ctx->ccm_config,
1003da3cf67fSGilad Ben-Yossef &sg_data, areq_ctx->assoclen);
1004ccba2f11SHadar Gat if (rc)
1005ff27e85aSGilad Ben-Yossef goto aead_map_failure;
1006ff27e85aSGilad Ben-Yossef }
1007ff27e85aSGilad Ben-Yossef
1008ff27e85aSGilad Ben-Yossef if (areq_ctx->cipher_mode == DRV_CIPHER_GCTR) {
1009ff27e85aSGilad Ben-Yossef dma_addr = dma_map_single(dev, areq_ctx->hkey, AES_BLOCK_SIZE,
1010ff27e85aSGilad Ben-Yossef DMA_BIDIRECTIONAL);
1011ff27e85aSGilad Ben-Yossef if (dma_mapping_error(dev, dma_addr)) {
1012ff27e85aSGilad Ben-Yossef dev_err(dev, "Mapping hkey %u B at va=%pK for DMA failed\n",
1013ff27e85aSGilad Ben-Yossef AES_BLOCK_SIZE, areq_ctx->hkey);
1014ff27e85aSGilad Ben-Yossef rc = -ENOMEM;
1015ff27e85aSGilad Ben-Yossef goto aead_map_failure;
1016ff27e85aSGilad Ben-Yossef }
1017ff27e85aSGilad Ben-Yossef areq_ctx->hkey_dma_addr = dma_addr;
1018ff27e85aSGilad Ben-Yossef
1019ff27e85aSGilad Ben-Yossef dma_addr = dma_map_single(dev, &areq_ctx->gcm_len_block,
1020ff27e85aSGilad Ben-Yossef AES_BLOCK_SIZE, DMA_TO_DEVICE);
1021ff27e85aSGilad Ben-Yossef if (dma_mapping_error(dev, dma_addr)) {
1022ff27e85aSGilad Ben-Yossef dev_err(dev, "Mapping gcm_len_block %u B at va=%pK for DMA failed\n",
1023ff27e85aSGilad Ben-Yossef AES_BLOCK_SIZE, &areq_ctx->gcm_len_block);
1024ff27e85aSGilad Ben-Yossef rc = -ENOMEM;
1025ff27e85aSGilad Ben-Yossef goto aead_map_failure;
1026ff27e85aSGilad Ben-Yossef }
1027ff27e85aSGilad Ben-Yossef areq_ctx->gcm_block_len_dma_addr = dma_addr;
1028ff27e85aSGilad Ben-Yossef
1029ff27e85aSGilad Ben-Yossef dma_addr = dma_map_single(dev, areq_ctx->gcm_iv_inc1,
1030ff27e85aSGilad Ben-Yossef AES_BLOCK_SIZE, DMA_TO_DEVICE);
1031ff27e85aSGilad Ben-Yossef
1032ff27e85aSGilad Ben-Yossef if (dma_mapping_error(dev, dma_addr)) {
1033ff27e85aSGilad Ben-Yossef dev_err(dev, "Mapping gcm_iv_inc1 %u B at va=%pK for DMA failed\n",
1034ff27e85aSGilad Ben-Yossef AES_BLOCK_SIZE, (areq_ctx->gcm_iv_inc1));
1035ff27e85aSGilad Ben-Yossef areq_ctx->gcm_iv_inc1_dma_addr = 0;
1036ff27e85aSGilad Ben-Yossef rc = -ENOMEM;
1037ff27e85aSGilad Ben-Yossef goto aead_map_failure;
1038ff27e85aSGilad Ben-Yossef }
1039ff27e85aSGilad Ben-Yossef areq_ctx->gcm_iv_inc1_dma_addr = dma_addr;
1040ff27e85aSGilad Ben-Yossef
1041ff27e85aSGilad Ben-Yossef dma_addr = dma_map_single(dev, areq_ctx->gcm_iv_inc2,
1042ff27e85aSGilad Ben-Yossef AES_BLOCK_SIZE, DMA_TO_DEVICE);
1043ff27e85aSGilad Ben-Yossef
1044ff27e85aSGilad Ben-Yossef if (dma_mapping_error(dev, dma_addr)) {
1045ff27e85aSGilad Ben-Yossef dev_err(dev, "Mapping gcm_iv_inc2 %u B at va=%pK for DMA failed\n",
1046ff27e85aSGilad Ben-Yossef AES_BLOCK_SIZE, (areq_ctx->gcm_iv_inc2));
1047ff27e85aSGilad Ben-Yossef areq_ctx->gcm_iv_inc2_dma_addr = 0;
1048ff27e85aSGilad Ben-Yossef rc = -ENOMEM;
1049ff27e85aSGilad Ben-Yossef goto aead_map_failure;
1050ff27e85aSGilad Ben-Yossef }
1051ff27e85aSGilad Ben-Yossef areq_ctx->gcm_iv_inc2_dma_addr = dma_addr;
1052ff27e85aSGilad Ben-Yossef }
1053ff27e85aSGilad Ben-Yossef
10540eae14a0SGilad Ben-Yossef size_to_map = req->cryptlen + req->assoclen;
1055504e84abSGilad Ben-Yossef /* If we do in-place encryption, we also need the auth tag */
1056504e84abSGilad Ben-Yossef if ((areq_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_ENCRYPT) &&
1057504e84abSGilad Ben-Yossef (req->src == req->dst)) {
1058ff27e85aSGilad Ben-Yossef size_to_map += authsize;
1059504e84abSGilad Ben-Yossef }
10600eae14a0SGilad Ben-Yossef
1061a260436cSGilad Ben-Yossef rc = cc_map_sg(dev, req->src, size_to_map,
1062a260436cSGilad Ben-Yossef (req->src != req->dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL),
1063ce0fc6dbSGilad Ben-Yossef &areq_ctx->src.mapped_nents,
1064ff27e85aSGilad Ben-Yossef (LLI_MAX_NUM_OF_ASSOC_DATA_ENTRIES +
1065ff27e85aSGilad Ben-Yossef LLI_MAX_NUM_OF_DATA_ENTRIES),
1066ff27e85aSGilad Ben-Yossef &dummy, &mapped_nents);
1067ccba2f11SHadar Gat if (rc)
1068ff27e85aSGilad Ben-Yossef goto aead_map_failure;
1069ff27e85aSGilad Ben-Yossef
1070ff27e85aSGilad Ben-Yossef if (areq_ctx->is_single_pass) {
1071ff27e85aSGilad Ben-Yossef /*
1072ff27e85aSGilad Ben-Yossef * Create MLLI table for:
1073ff27e85aSGilad Ben-Yossef * (1) Assoc. data
1074ff27e85aSGilad Ben-Yossef * (2) Src/Dst SGLs
1075ff27e85aSGilad Ben-Yossef * Note: IV is contg. buffer (not an SGL)
1076ff27e85aSGilad Ben-Yossef */
1077ff27e85aSGilad Ben-Yossef rc = cc_aead_chain_assoc(drvdata, req, &sg_data, true, false);
1078ff27e85aSGilad Ben-Yossef if (rc)
1079ff27e85aSGilad Ben-Yossef goto aead_map_failure;
1080ff27e85aSGilad Ben-Yossef rc = cc_aead_chain_iv(drvdata, req, &sg_data, true, false);
1081ff27e85aSGilad Ben-Yossef if (rc)
1082ff27e85aSGilad Ben-Yossef goto aead_map_failure;
1083ff27e85aSGilad Ben-Yossef rc = cc_aead_chain_data(drvdata, req, &sg_data, true, false);
1084ff27e85aSGilad Ben-Yossef if (rc)
1085ff27e85aSGilad Ben-Yossef goto aead_map_failure;
1086ff27e85aSGilad Ben-Yossef } else { /* DOUBLE-PASS flow */
1087ff27e85aSGilad Ben-Yossef /*
1088ff27e85aSGilad Ben-Yossef * Prepare MLLI table(s) in this order:
1089ff27e85aSGilad Ben-Yossef *
1090ff27e85aSGilad Ben-Yossef * If ENCRYPT/DECRYPT (inplace):
1091ff27e85aSGilad Ben-Yossef * (1) MLLI table for assoc
1092ff27e85aSGilad Ben-Yossef * (2) IV entry (chained right after end of assoc)
1093ff27e85aSGilad Ben-Yossef * (3) MLLI for src/dst (inplace operation)
1094ff27e85aSGilad Ben-Yossef *
1095ff27e85aSGilad Ben-Yossef * If ENCRYPT (non-inplace)
1096ff27e85aSGilad Ben-Yossef * (1) MLLI table for assoc
1097ff27e85aSGilad Ben-Yossef * (2) IV entry (chained right after end of assoc)
1098ff27e85aSGilad Ben-Yossef * (3) MLLI for dst
1099ff27e85aSGilad Ben-Yossef * (4) MLLI for src
1100ff27e85aSGilad Ben-Yossef *
1101ff27e85aSGilad Ben-Yossef * If DECRYPT (non-inplace)
1102ff27e85aSGilad Ben-Yossef * (1) MLLI table for assoc
1103ff27e85aSGilad Ben-Yossef * (2) IV entry (chained right after end of assoc)
1104ff27e85aSGilad Ben-Yossef * (3) MLLI for src
1105ff27e85aSGilad Ben-Yossef * (4) MLLI for dst
1106ff27e85aSGilad Ben-Yossef */
1107ff27e85aSGilad Ben-Yossef rc = cc_aead_chain_assoc(drvdata, req, &sg_data, false, true);
1108ff27e85aSGilad Ben-Yossef if (rc)
1109ff27e85aSGilad Ben-Yossef goto aead_map_failure;
1110ff27e85aSGilad Ben-Yossef rc = cc_aead_chain_iv(drvdata, req, &sg_data, false, true);
1111ff27e85aSGilad Ben-Yossef if (rc)
1112ff27e85aSGilad Ben-Yossef goto aead_map_failure;
1113ff27e85aSGilad Ben-Yossef rc = cc_aead_chain_data(drvdata, req, &sg_data, true, true);
1114ff27e85aSGilad Ben-Yossef if (rc)
1115ff27e85aSGilad Ben-Yossef goto aead_map_failure;
1116ff27e85aSGilad Ben-Yossef }
1117ff27e85aSGilad Ben-Yossef
1118ff27e85aSGilad Ben-Yossef /* Mlli support -start building the MLLI according to the above
1119ff27e85aSGilad Ben-Yossef * results
1120ff27e85aSGilad Ben-Yossef */
1121ff27e85aSGilad Ben-Yossef if (areq_ctx->assoc_buff_type == CC_DMA_BUF_MLLI ||
1122ff27e85aSGilad Ben-Yossef areq_ctx->data_buff_type == CC_DMA_BUF_MLLI) {
1123040187a0SGeert Uytterhoeven mlli_params->curr_pool = drvdata->mlli_buffs_pool;
1124ff27e85aSGilad Ben-Yossef rc = cc_generate_mlli(dev, &sg_data, mlli_params, flags);
1125ff27e85aSGilad Ben-Yossef if (rc)
1126ff27e85aSGilad Ben-Yossef goto aead_map_failure;
1127ff27e85aSGilad Ben-Yossef
1128ff27e85aSGilad Ben-Yossef cc_update_aead_mlli_nents(drvdata, req);
1129ff27e85aSGilad Ben-Yossef dev_dbg(dev, "assoc params mn %d\n",
1130ff27e85aSGilad Ben-Yossef areq_ctx->assoc.mlli_nents);
1131ff27e85aSGilad Ben-Yossef dev_dbg(dev, "src params mn %d\n", areq_ctx->src.mlli_nents);
1132ff27e85aSGilad Ben-Yossef dev_dbg(dev, "dst params mn %d\n", areq_ctx->dst.mlli_nents);
1133ff27e85aSGilad Ben-Yossef }
1134ff27e85aSGilad Ben-Yossef return 0;
1135ff27e85aSGilad Ben-Yossef
1136ff27e85aSGilad Ben-Yossef aead_map_failure:
1137ff27e85aSGilad Ben-Yossef cc_unmap_aead_request(dev, req);
1138ff27e85aSGilad Ben-Yossef return rc;
1139ff27e85aSGilad Ben-Yossef }
1140ff27e85aSGilad Ben-Yossef
cc_map_hash_request_final(struct cc_drvdata * drvdata,void * ctx,struct scatterlist * src,unsigned int nbytes,bool do_update,gfp_t flags)114163893811SGilad Ben-Yossef int cc_map_hash_request_final(struct cc_drvdata *drvdata, void *ctx,
114263893811SGilad Ben-Yossef struct scatterlist *src, unsigned int nbytes,
114363893811SGilad Ben-Yossef bool do_update, gfp_t flags)
114463893811SGilad Ben-Yossef {
114563893811SGilad Ben-Yossef struct ahash_req_ctx *areq_ctx = (struct ahash_req_ctx *)ctx;
114663893811SGilad Ben-Yossef struct device *dev = drvdata_to_dev(drvdata);
114763893811SGilad Ben-Yossef u8 *curr_buff = cc_hash_buf(areq_ctx);
114863893811SGilad Ben-Yossef u32 *curr_buff_cnt = cc_hash_buf_cnt(areq_ctx);
114963893811SGilad Ben-Yossef struct mlli_params *mlli_params = &areq_ctx->mlli_params;
115063893811SGilad Ben-Yossef struct buffer_array sg_data;
1151ccba2f11SHadar Gat int rc = 0;
115263893811SGilad Ben-Yossef u32 dummy = 0;
115363893811SGilad Ben-Yossef u32 mapped_nents = 0;
115463893811SGilad Ben-Yossef
115563893811SGilad Ben-Yossef dev_dbg(dev, "final params : curr_buff=%pK curr_buff_cnt=0x%X nbytes = 0x%X src=%pK curr_index=%u\n",
115663893811SGilad Ben-Yossef curr_buff, *curr_buff_cnt, nbytes, src, areq_ctx->buff_index);
115763893811SGilad Ben-Yossef /* Init the type of the dma buffer */
115863893811SGilad Ben-Yossef areq_ctx->data_dma_buf_type = CC_DMA_BUF_NULL;
115963893811SGilad Ben-Yossef mlli_params->curr_pool = NULL;
116063893811SGilad Ben-Yossef sg_data.num_of_buffers = 0;
116163893811SGilad Ben-Yossef areq_ctx->in_nents = 0;
116263893811SGilad Ben-Yossef
116363893811SGilad Ben-Yossef if (nbytes == 0 && *curr_buff_cnt == 0) {
116463893811SGilad Ben-Yossef /* nothing to do */
116563893811SGilad Ben-Yossef return 0;
116663893811SGilad Ben-Yossef }
116763893811SGilad Ben-Yossef
116863893811SGilad Ben-Yossef /* map the previous buffer */
116963893811SGilad Ben-Yossef if (*curr_buff_cnt) {
1170ccba2f11SHadar Gat rc = cc_set_hash_buf(dev, areq_ctx, curr_buff, *curr_buff_cnt,
1171ccba2f11SHadar Gat &sg_data);
1172ccba2f11SHadar Gat if (rc)
1173ccba2f11SHadar Gat return rc;
117463893811SGilad Ben-Yossef }
117563893811SGilad Ben-Yossef
117663893811SGilad Ben-Yossef if (src && nbytes > 0 && do_update) {
1177ccba2f11SHadar Gat rc = cc_map_sg(dev, src, nbytes, DMA_TO_DEVICE,
117863893811SGilad Ben-Yossef &areq_ctx->in_nents, LLI_MAX_NUM_OF_DATA_ENTRIES,
1179ccba2f11SHadar Gat &dummy, &mapped_nents);
1180ccba2f11SHadar Gat if (rc)
118163893811SGilad Ben-Yossef goto unmap_curr_buff;
118263893811SGilad Ben-Yossef if (src && mapped_nents == 1 &&
118363893811SGilad Ben-Yossef areq_ctx->data_dma_buf_type == CC_DMA_BUF_NULL) {
118463893811SGilad Ben-Yossef memcpy(areq_ctx->buff_sg, src,
118563893811SGilad Ben-Yossef sizeof(struct scatterlist));
118663893811SGilad Ben-Yossef areq_ctx->buff_sg->length = nbytes;
118763893811SGilad Ben-Yossef areq_ctx->curr_sg = areq_ctx->buff_sg;
118863893811SGilad Ben-Yossef areq_ctx->data_dma_buf_type = CC_DMA_BUF_DLLI;
118963893811SGilad Ben-Yossef } else {
119063893811SGilad Ben-Yossef areq_ctx->data_dma_buf_type = CC_DMA_BUF_MLLI;
119163893811SGilad Ben-Yossef }
119263893811SGilad Ben-Yossef }
119363893811SGilad Ben-Yossef
119463893811SGilad Ben-Yossef /*build mlli */
119563893811SGilad Ben-Yossef if (areq_ctx->data_dma_buf_type == CC_DMA_BUF_MLLI) {
1196040187a0SGeert Uytterhoeven mlli_params->curr_pool = drvdata->mlli_buffs_pool;
119763893811SGilad Ben-Yossef /* add the src data to the sg_data */
119863893811SGilad Ben-Yossef cc_add_sg_entry(dev, &sg_data, areq_ctx->in_nents, src, nbytes,
119963893811SGilad Ben-Yossef 0, true, &areq_ctx->mlli_nents);
1200ccba2f11SHadar Gat rc = cc_generate_mlli(dev, &sg_data, mlli_params, flags);
1201ccba2f11SHadar Gat if (rc)
120263893811SGilad Ben-Yossef goto fail_unmap_din;
120363893811SGilad Ben-Yossef }
120463893811SGilad Ben-Yossef /* change the buffer index for the unmap function */
120563893811SGilad Ben-Yossef areq_ctx->buff_index = (areq_ctx->buff_index ^ 1);
120663893811SGilad Ben-Yossef dev_dbg(dev, "areq_ctx->data_dma_buf_type = %s\n",
120763893811SGilad Ben-Yossef cc_dma_buf_type(areq_ctx->data_dma_buf_type));
120863893811SGilad Ben-Yossef return 0;
120963893811SGilad Ben-Yossef
121063893811SGilad Ben-Yossef fail_unmap_din:
121163893811SGilad Ben-Yossef dma_unmap_sg(dev, src, areq_ctx->in_nents, DMA_TO_DEVICE);
121263893811SGilad Ben-Yossef
121363893811SGilad Ben-Yossef unmap_curr_buff:
121463893811SGilad Ben-Yossef if (*curr_buff_cnt)
121563893811SGilad Ben-Yossef dma_unmap_sg(dev, areq_ctx->buff_sg, 1, DMA_TO_DEVICE);
121663893811SGilad Ben-Yossef
1217ccba2f11SHadar Gat return rc;
121863893811SGilad Ben-Yossef }
121963893811SGilad Ben-Yossef
cc_map_hash_request_update(struct cc_drvdata * drvdata,void * ctx,struct scatterlist * src,unsigned int nbytes,unsigned int block_size,gfp_t flags)122063893811SGilad Ben-Yossef int cc_map_hash_request_update(struct cc_drvdata *drvdata, void *ctx,
122163893811SGilad Ben-Yossef struct scatterlist *src, unsigned int nbytes,
122263893811SGilad Ben-Yossef unsigned int block_size, gfp_t flags)
122363893811SGilad Ben-Yossef {
122463893811SGilad Ben-Yossef struct ahash_req_ctx *areq_ctx = (struct ahash_req_ctx *)ctx;
122563893811SGilad Ben-Yossef struct device *dev = drvdata_to_dev(drvdata);
122663893811SGilad Ben-Yossef u8 *curr_buff = cc_hash_buf(areq_ctx);
122763893811SGilad Ben-Yossef u32 *curr_buff_cnt = cc_hash_buf_cnt(areq_ctx);
122863893811SGilad Ben-Yossef u8 *next_buff = cc_next_buf(areq_ctx);
122963893811SGilad Ben-Yossef u32 *next_buff_cnt = cc_next_buf_cnt(areq_ctx);
123063893811SGilad Ben-Yossef struct mlli_params *mlli_params = &areq_ctx->mlli_params;
123163893811SGilad Ben-Yossef unsigned int update_data_len;
123263893811SGilad Ben-Yossef u32 total_in_len = nbytes + *curr_buff_cnt;
123363893811SGilad Ben-Yossef struct buffer_array sg_data;
123463893811SGilad Ben-Yossef unsigned int swap_index = 0;
1235ccba2f11SHadar Gat int rc = 0;
123663893811SGilad Ben-Yossef u32 dummy = 0;
123763893811SGilad Ben-Yossef u32 mapped_nents = 0;
123863893811SGilad Ben-Yossef
123963893811SGilad Ben-Yossef dev_dbg(dev, " update params : curr_buff=%pK curr_buff_cnt=0x%X nbytes=0x%X src=%pK curr_index=%u\n",
124063893811SGilad Ben-Yossef curr_buff, *curr_buff_cnt, nbytes, src, areq_ctx->buff_index);
124163893811SGilad Ben-Yossef /* Init the type of the dma buffer */
124263893811SGilad Ben-Yossef areq_ctx->data_dma_buf_type = CC_DMA_BUF_NULL;
124363893811SGilad Ben-Yossef mlli_params->curr_pool = NULL;
124463893811SGilad Ben-Yossef areq_ctx->curr_sg = NULL;
124563893811SGilad Ben-Yossef sg_data.num_of_buffers = 0;
124663893811SGilad Ben-Yossef areq_ctx->in_nents = 0;
124763893811SGilad Ben-Yossef
124863893811SGilad Ben-Yossef if (total_in_len < block_size) {
124963893811SGilad Ben-Yossef dev_dbg(dev, " less than one block: curr_buff=%pK *curr_buff_cnt=0x%X copy_to=%pK\n",
125063893811SGilad Ben-Yossef curr_buff, *curr_buff_cnt, &curr_buff[*curr_buff_cnt]);
1251b7ec8530SGilad Ben-Yossef areq_ctx->in_nents = sg_nents_for_len(src, nbytes);
125263893811SGilad Ben-Yossef sg_copy_to_buffer(src, areq_ctx->in_nents,
125363893811SGilad Ben-Yossef &curr_buff[*curr_buff_cnt], nbytes);
125463893811SGilad Ben-Yossef *curr_buff_cnt += nbytes;
125563893811SGilad Ben-Yossef return 1;
125663893811SGilad Ben-Yossef }
125763893811SGilad Ben-Yossef
125863893811SGilad Ben-Yossef /* Calculate the residue size*/
125963893811SGilad Ben-Yossef *next_buff_cnt = total_in_len & (block_size - 1);
126063893811SGilad Ben-Yossef /* update data len */
126163893811SGilad Ben-Yossef update_data_len = total_in_len - *next_buff_cnt;
126263893811SGilad Ben-Yossef
126363893811SGilad Ben-Yossef dev_dbg(dev, " temp length : *next_buff_cnt=0x%X update_data_len=0x%X\n",
126463893811SGilad Ben-Yossef *next_buff_cnt, update_data_len);
126563893811SGilad Ben-Yossef
126663893811SGilad Ben-Yossef /* Copy the new residue to next buffer */
126763893811SGilad Ben-Yossef if (*next_buff_cnt) {
126863893811SGilad Ben-Yossef dev_dbg(dev, " handle residue: next buff %pK skip data %u residue %u\n",
126963893811SGilad Ben-Yossef next_buff, (update_data_len - *curr_buff_cnt),
127063893811SGilad Ben-Yossef *next_buff_cnt);
127163893811SGilad Ben-Yossef cc_copy_sg_portion(dev, next_buff, src,
127263893811SGilad Ben-Yossef (update_data_len - *curr_buff_cnt),
127363893811SGilad Ben-Yossef nbytes, CC_SG_TO_BUF);
127463893811SGilad Ben-Yossef /* change the buffer index for next operation */
127563893811SGilad Ben-Yossef swap_index = 1;
127663893811SGilad Ben-Yossef }
127763893811SGilad Ben-Yossef
127863893811SGilad Ben-Yossef if (*curr_buff_cnt) {
1279ccba2f11SHadar Gat rc = cc_set_hash_buf(dev, areq_ctx, curr_buff, *curr_buff_cnt,
1280ccba2f11SHadar Gat &sg_data);
1281ccba2f11SHadar Gat if (rc)
1282ccba2f11SHadar Gat return rc;
128363893811SGilad Ben-Yossef /* change the buffer index for next operation */
128463893811SGilad Ben-Yossef swap_index = 1;
128563893811SGilad Ben-Yossef }
128663893811SGilad Ben-Yossef
128763893811SGilad Ben-Yossef if (update_data_len > *curr_buff_cnt) {
1288ccba2f11SHadar Gat rc = cc_map_sg(dev, src, (update_data_len - *curr_buff_cnt),
128963893811SGilad Ben-Yossef DMA_TO_DEVICE, &areq_ctx->in_nents,
129063893811SGilad Ben-Yossef LLI_MAX_NUM_OF_DATA_ENTRIES, &dummy,
1291ccba2f11SHadar Gat &mapped_nents);
1292ccba2f11SHadar Gat if (rc)
129363893811SGilad Ben-Yossef goto unmap_curr_buff;
129463893811SGilad Ben-Yossef if (mapped_nents == 1 &&
129563893811SGilad Ben-Yossef areq_ctx->data_dma_buf_type == CC_DMA_BUF_NULL) {
129663893811SGilad Ben-Yossef /* only one entry in the SG and no previous data */
129763893811SGilad Ben-Yossef memcpy(areq_ctx->buff_sg, src,
129863893811SGilad Ben-Yossef sizeof(struct scatterlist));
129963893811SGilad Ben-Yossef areq_ctx->buff_sg->length = update_data_len;
130063893811SGilad Ben-Yossef areq_ctx->data_dma_buf_type = CC_DMA_BUF_DLLI;
130163893811SGilad Ben-Yossef areq_ctx->curr_sg = areq_ctx->buff_sg;
130263893811SGilad Ben-Yossef } else {
130363893811SGilad Ben-Yossef areq_ctx->data_dma_buf_type = CC_DMA_BUF_MLLI;
130463893811SGilad Ben-Yossef }
130563893811SGilad Ben-Yossef }
130663893811SGilad Ben-Yossef
130763893811SGilad Ben-Yossef if (areq_ctx->data_dma_buf_type == CC_DMA_BUF_MLLI) {
1308040187a0SGeert Uytterhoeven mlli_params->curr_pool = drvdata->mlli_buffs_pool;
130963893811SGilad Ben-Yossef /* add the src data to the sg_data */
131063893811SGilad Ben-Yossef cc_add_sg_entry(dev, &sg_data, areq_ctx->in_nents, src,
131163893811SGilad Ben-Yossef (update_data_len - *curr_buff_cnt), 0, true,
131263893811SGilad Ben-Yossef &areq_ctx->mlli_nents);
1313ccba2f11SHadar Gat rc = cc_generate_mlli(dev, &sg_data, mlli_params, flags);
1314ccba2f11SHadar Gat if (rc)
131563893811SGilad Ben-Yossef goto fail_unmap_din;
131663893811SGilad Ben-Yossef }
131763893811SGilad Ben-Yossef areq_ctx->buff_index = (areq_ctx->buff_index ^ swap_index);
131863893811SGilad Ben-Yossef
131963893811SGilad Ben-Yossef return 0;
132063893811SGilad Ben-Yossef
132163893811SGilad Ben-Yossef fail_unmap_din:
132263893811SGilad Ben-Yossef dma_unmap_sg(dev, src, areq_ctx->in_nents, DMA_TO_DEVICE);
132363893811SGilad Ben-Yossef
132463893811SGilad Ben-Yossef unmap_curr_buff:
132563893811SGilad Ben-Yossef if (*curr_buff_cnt)
132663893811SGilad Ben-Yossef dma_unmap_sg(dev, areq_ctx->buff_sg, 1, DMA_TO_DEVICE);
132763893811SGilad Ben-Yossef
1328ccba2f11SHadar Gat return rc;
132963893811SGilad Ben-Yossef }
133063893811SGilad Ben-Yossef
cc_unmap_hash_request(struct device * dev,void * ctx,struct scatterlist * src,bool do_revert)133163893811SGilad Ben-Yossef void cc_unmap_hash_request(struct device *dev, void *ctx,
133263893811SGilad Ben-Yossef struct scatterlist *src, bool do_revert)
133363893811SGilad Ben-Yossef {
133463893811SGilad Ben-Yossef struct ahash_req_ctx *areq_ctx = (struct ahash_req_ctx *)ctx;
133563893811SGilad Ben-Yossef u32 *prev_len = cc_next_buf_cnt(areq_ctx);
133663893811SGilad Ben-Yossef
133763893811SGilad Ben-Yossef /*In case a pool was set, a table was
133863893811SGilad Ben-Yossef *allocated and should be released
133963893811SGilad Ben-Yossef */
134063893811SGilad Ben-Yossef if (areq_ctx->mlli_params.curr_pool) {
134163893811SGilad Ben-Yossef dev_dbg(dev, "free MLLI buffer: dma=%pad virt=%pK\n",
134263893811SGilad Ben-Yossef &areq_ctx->mlli_params.mlli_dma_addr,
134363893811SGilad Ben-Yossef areq_ctx->mlli_params.mlli_virt_addr);
134463893811SGilad Ben-Yossef dma_pool_free(areq_ctx->mlli_params.curr_pool,
134563893811SGilad Ben-Yossef areq_ctx->mlli_params.mlli_virt_addr,
134663893811SGilad Ben-Yossef areq_ctx->mlli_params.mlli_dma_addr);
134763893811SGilad Ben-Yossef }
134863893811SGilad Ben-Yossef
134963893811SGilad Ben-Yossef if (src && areq_ctx->in_nents) {
135063893811SGilad Ben-Yossef dev_dbg(dev, "Unmapped sg src: virt=%pK dma=%pad len=0x%X\n",
135163893811SGilad Ben-Yossef sg_virt(src), &sg_dma_address(src), sg_dma_len(src));
135263893811SGilad Ben-Yossef dma_unmap_sg(dev, src,
135363893811SGilad Ben-Yossef areq_ctx->in_nents, DMA_TO_DEVICE);
135463893811SGilad Ben-Yossef }
135563893811SGilad Ben-Yossef
135663893811SGilad Ben-Yossef if (*prev_len) {
135763893811SGilad Ben-Yossef dev_dbg(dev, "Unmapped buffer: areq_ctx->buff_sg=%pK dma=%pad len 0x%X\n",
135863893811SGilad Ben-Yossef sg_virt(areq_ctx->buff_sg),
135963893811SGilad Ben-Yossef &sg_dma_address(areq_ctx->buff_sg),
136063893811SGilad Ben-Yossef sg_dma_len(areq_ctx->buff_sg));
136163893811SGilad Ben-Yossef dma_unmap_sg(dev, areq_ctx->buff_sg, 1, DMA_TO_DEVICE);
136263893811SGilad Ben-Yossef if (!do_revert) {
136363893811SGilad Ben-Yossef /* clean the previous data length for update
136463893811SGilad Ben-Yossef * operation
136563893811SGilad Ben-Yossef */
136663893811SGilad Ben-Yossef *prev_len = 0;
136763893811SGilad Ben-Yossef } else {
136863893811SGilad Ben-Yossef areq_ctx->buff_index ^= 1;
136963893811SGilad Ben-Yossef }
137063893811SGilad Ben-Yossef }
137163893811SGilad Ben-Yossef }
137263893811SGilad Ben-Yossef
cc_buffer_mgr_init(struct cc_drvdata * drvdata)13734c3f9727SGilad Ben-Yossef int cc_buffer_mgr_init(struct cc_drvdata *drvdata)
13744c3f9727SGilad Ben-Yossef {
13754c3f9727SGilad Ben-Yossef struct device *dev = drvdata_to_dev(drvdata);
13764c3f9727SGilad Ben-Yossef
1377040187a0SGeert Uytterhoeven drvdata->mlli_buffs_pool =
13784c3f9727SGilad Ben-Yossef dma_pool_create("dx_single_mlli_tables", dev,
13794c3f9727SGilad Ben-Yossef MAX_NUM_OF_TOTAL_MLLI_ENTRIES *
13804c3f9727SGilad Ben-Yossef LLI_ENTRY_BYTE_SIZE,
13814c3f9727SGilad Ben-Yossef MLLI_TABLE_MIN_ALIGNMENT, 0);
13824c3f9727SGilad Ben-Yossef
1383040187a0SGeert Uytterhoeven if (!drvdata->mlli_buffs_pool)
1384040187a0SGeert Uytterhoeven return -ENOMEM;
13854c3f9727SGilad Ben-Yossef
13864c3f9727SGilad Ben-Yossef return 0;
13874c3f9727SGilad Ben-Yossef }
13884c3f9727SGilad Ben-Yossef
cc_buffer_mgr_fini(struct cc_drvdata * drvdata)13894c3f9727SGilad Ben-Yossef int cc_buffer_mgr_fini(struct cc_drvdata *drvdata)
13904c3f9727SGilad Ben-Yossef {
1391040187a0SGeert Uytterhoeven dma_pool_destroy(drvdata->mlli_buffs_pool);
13924c3f9727SGilad Ben-Yossef return 0;
13934c3f9727SGilad Ben-Yossef }
1394