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