blob: 46be101ede567f77ffafd25d9c8c073c58abbe97 [file] [log] [blame]
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +00001// SPDX-License-Identifier: GPL-2.0
2/* Copyright (C) 2012-2018 ARM Limited or its affiliates. */
3
4#include <crypto/authenc.h>
5#include <crypto/scatterwalk.h>
6#include <linux/dmapool.h>
7#include <linux/dma-mapping.h>
8
9#include "cc_buffer_mgr.h"
10#include "cc_lli_defs.h"
Gilad Ben-Yossef63ee04c2018-01-22 09:27:01 +000011#include "cc_cipher.h"
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +000012
13enum dma_buffer_type {
14 DMA_NULL_TYPE = -1,
15 DMA_SGL_TYPE = 1,
16 DMA_BUFF_TYPE = 2,
17};
18
19struct buff_mgr_handle {
20 struct dma_pool *mlli_buffs_pool;
21};
22
23union buffer_array_entry {
24 struct scatterlist *sgl;
25 dma_addr_t buffer_dma;
26};
27
28struct buffer_array {
29 unsigned int num_of_buffers;
30 union buffer_array_entry entry[MAX_NUM_OF_BUFFERS_IN_MLLI];
31 unsigned int offset[MAX_NUM_OF_BUFFERS_IN_MLLI];
32 int nents[MAX_NUM_OF_BUFFERS_IN_MLLI];
33 int total_data_len[MAX_NUM_OF_BUFFERS_IN_MLLI];
34 enum dma_buffer_type type[MAX_NUM_OF_BUFFERS_IN_MLLI];
35 bool is_last[MAX_NUM_OF_BUFFERS_IN_MLLI];
36 u32 *mlli_nents[MAX_NUM_OF_BUFFERS_IN_MLLI];
37};
38
39static inline char *cc_dma_buf_type(enum cc_req_dma_buf_type type)
40{
41 switch (type) {
42 case CC_DMA_BUF_NULL:
43 return "BUF_NULL";
44 case CC_DMA_BUF_DLLI:
45 return "BUF_DLLI";
46 case CC_DMA_BUF_MLLI:
47 return "BUF_MLLI";
48 default:
49 return "BUF_INVALID";
50 }
51}
52
53/**
54 * cc_get_sgl_nents() - Get scatterlist number of entries.
55 *
56 * @sg_list: SG list
57 * @nbytes: [IN] Total SGL data bytes.
58 * @lbytes: [OUT] Returns the amount of bytes at the last entry
59 */
60static unsigned int cc_get_sgl_nents(struct device *dev,
61 struct scatterlist *sg_list,
62 unsigned int nbytes, u32 *lbytes,
63 bool *is_chained)
64{
65 unsigned int nents = 0;
66
67 while (nbytes && sg_list) {
68 if (sg_list->length) {
69 nents++;
70 /* get the number of bytes in the last entry */
71 *lbytes = nbytes;
72 nbytes -= (sg_list->length > nbytes) ?
73 nbytes : sg_list->length;
74 sg_list = sg_next(sg_list);
75 } else {
76 sg_list = (struct scatterlist *)sg_page(sg_list);
77 if (is_chained)
78 *is_chained = true;
79 }
80 }
81 dev_dbg(dev, "nents %d last bytes %d\n", nents, *lbytes);
82 return nents;
83}
84
85/**
86 * cc_zero_sgl() - Zero scatter scatter list data.
87 *
88 * @sgl:
89 */
90void cc_zero_sgl(struct scatterlist *sgl, u32 data_len)
91{
92 struct scatterlist *current_sg = sgl;
93 int sg_index = 0;
94
95 while (sg_index <= data_len) {
96 if (!current_sg) {
97 /* reached the end of the sgl --> just return back */
98 return;
99 }
100 memset(sg_virt(current_sg), 0, current_sg->length);
101 sg_index += current_sg->length;
102 current_sg = sg_next(current_sg);
103 }
104}
105
106/**
107 * cc_copy_sg_portion() - Copy scatter list data,
108 * from to_skip to end, to dest and vice versa
109 *
110 * @dest:
111 * @sg:
112 * @to_skip:
113 * @end:
114 * @direct:
115 */
116void cc_copy_sg_portion(struct device *dev, u8 *dest, struct scatterlist *sg,
117 u32 to_skip, u32 end, enum cc_sg_cpy_direct direct)
118{
119 u32 nents, lbytes;
120
121 nents = cc_get_sgl_nents(dev, sg, end, &lbytes, NULL);
122 sg_copy_buffer(sg, nents, (void *)dest, (end - to_skip + 1), to_skip,
123 (direct == CC_SG_TO_BUF));
124}
125
126static int cc_render_buff_to_mlli(struct device *dev, dma_addr_t buff_dma,
127 u32 buff_size, u32 *curr_nents,
128 u32 **mlli_entry_pp)
129{
130 u32 *mlli_entry_p = *mlli_entry_pp;
131 u32 new_nents;
132
133 /* Verify there is no memory overflow*/
134 new_nents = (*curr_nents + buff_size / CC_MAX_MLLI_ENTRY_SIZE + 1);
135 if (new_nents > MAX_NUM_OF_TOTAL_MLLI_ENTRIES)
136 return -ENOMEM;
137
138 /*handle buffer longer than 64 kbytes */
139 while (buff_size > CC_MAX_MLLI_ENTRY_SIZE) {
140 cc_lli_set_addr(mlli_entry_p, buff_dma);
141 cc_lli_set_size(mlli_entry_p, CC_MAX_MLLI_ENTRY_SIZE);
142 dev_dbg(dev, "entry[%d]: single_buff=0x%08X size=%08X\n",
143 *curr_nents, mlli_entry_p[LLI_WORD0_OFFSET],
144 mlli_entry_p[LLI_WORD1_OFFSET]);
145 buff_dma += CC_MAX_MLLI_ENTRY_SIZE;
146 buff_size -= CC_MAX_MLLI_ENTRY_SIZE;
147 mlli_entry_p = mlli_entry_p + 2;
148 (*curr_nents)++;
149 }
150 /*Last entry */
151 cc_lli_set_addr(mlli_entry_p, buff_dma);
152 cc_lli_set_size(mlli_entry_p, buff_size);
153 dev_dbg(dev, "entry[%d]: single_buff=0x%08X size=%08X\n",
154 *curr_nents, mlli_entry_p[LLI_WORD0_OFFSET],
155 mlli_entry_p[LLI_WORD1_OFFSET]);
156 mlli_entry_p = mlli_entry_p + 2;
157 *mlli_entry_pp = mlli_entry_p;
158 (*curr_nents)++;
159 return 0;
160}
161
162static int cc_render_sg_to_mlli(struct device *dev, struct scatterlist *sgl,
163 u32 sgl_data_len, u32 sgl_offset,
164 u32 *curr_nents, u32 **mlli_entry_pp)
165{
166 struct scatterlist *curr_sgl = sgl;
167 u32 *mlli_entry_p = *mlli_entry_pp;
168 s32 rc = 0;
169
170 for ( ; (curr_sgl && sgl_data_len);
171 curr_sgl = sg_next(curr_sgl)) {
172 u32 entry_data_len =
173 (sgl_data_len > sg_dma_len(curr_sgl) - sgl_offset) ?
174 sg_dma_len(curr_sgl) - sgl_offset :
175 sgl_data_len;
176 sgl_data_len -= entry_data_len;
177 rc = cc_render_buff_to_mlli(dev, sg_dma_address(curr_sgl) +
178 sgl_offset, entry_data_len,
179 curr_nents, &mlli_entry_p);
180 if (rc)
181 return rc;
182
183 sgl_offset = 0;
184 }
185 *mlli_entry_pp = mlli_entry_p;
186 return 0;
187}
188
189static int cc_generate_mlli(struct device *dev, struct buffer_array *sg_data,
190 struct mlli_params *mlli_params, gfp_t flags)
191{
192 u32 *mlli_p;
193 u32 total_nents = 0, prev_total_nents = 0;
194 int rc = 0, i;
195
196 dev_dbg(dev, "NUM of SG's = %d\n", sg_data->num_of_buffers);
197
198 /* Allocate memory from the pointed pool */
199 mlli_params->mlli_virt_addr =
200 dma_pool_alloc(mlli_params->curr_pool, flags,
201 &mlli_params->mlli_dma_addr);
202 if (!mlli_params->mlli_virt_addr) {
203 dev_err(dev, "dma_pool_alloc() failed\n");
204 rc = -ENOMEM;
205 goto build_mlli_exit;
206 }
207 /* Point to start of MLLI */
208 mlli_p = (u32 *)mlli_params->mlli_virt_addr;
209 /* go over all SG's and link it to one MLLI table */
210 for (i = 0; i < sg_data->num_of_buffers; i++) {
211 union buffer_array_entry *entry = &sg_data->entry[i];
212 u32 tot_len = sg_data->total_data_len[i];
213 u32 offset = sg_data->offset[i];
214
215 if (sg_data->type[i] == DMA_SGL_TYPE)
216 rc = cc_render_sg_to_mlli(dev, entry->sgl, tot_len,
217 offset, &total_nents,
218 &mlli_p);
219 else /*DMA_BUFF_TYPE*/
220 rc = cc_render_buff_to_mlli(dev, entry->buffer_dma,
221 tot_len, &total_nents,
222 &mlli_p);
223 if (rc)
224 return rc;
225
226 /* set last bit in the current table */
227 if (sg_data->mlli_nents[i]) {
228 /*Calculate the current MLLI table length for the
229 *length field in the descriptor
230 */
231 *sg_data->mlli_nents[i] +=
232 (total_nents - prev_total_nents);
233 prev_total_nents = total_nents;
234 }
235 }
236
237 /* Set MLLI size for the bypass operation */
238 mlli_params->mlli_len = (total_nents * LLI_ENTRY_BYTE_SIZE);
239
240 dev_dbg(dev, "MLLI params: virt_addr=%pK dma_addr=%pad mlli_len=0x%X\n",
241 mlli_params->mlli_virt_addr, &mlli_params->mlli_dma_addr,
242 mlli_params->mlli_len);
243
244build_mlli_exit:
245 return rc;
246}
247
248static void cc_add_sg_entry(struct device *dev, struct buffer_array *sgl_data,
249 unsigned int nents, struct scatterlist *sgl,
250 unsigned int data_len, unsigned int data_offset,
251 bool is_last_table, u32 *mlli_nents)
252{
253 unsigned int index = sgl_data->num_of_buffers;
254
255 dev_dbg(dev, "index=%u nents=%u sgl=%pK data_len=0x%08X is_last=%d\n",
256 index, nents, sgl, data_len, is_last_table);
257 sgl_data->nents[index] = nents;
258 sgl_data->entry[index].sgl = sgl;
259 sgl_data->offset[index] = data_offset;
260 sgl_data->total_data_len[index] = data_len;
261 sgl_data->type[index] = DMA_SGL_TYPE;
262 sgl_data->is_last[index] = is_last_table;
263 sgl_data->mlli_nents[index] = mlli_nents;
264 if (sgl_data->mlli_nents[index])
265 *sgl_data->mlli_nents[index] = 0;
266 sgl_data->num_of_buffers++;
267}
268
269static int cc_dma_map_sg(struct device *dev, struct scatterlist *sg, u32 nents,
270 enum dma_data_direction direction)
271{
272 u32 i, j;
273 struct scatterlist *l_sg = sg;
274
275 for (i = 0; i < nents; i++) {
276 if (!l_sg)
277 break;
278 if (dma_map_sg(dev, l_sg, 1, direction) != 1) {
279 dev_err(dev, "dma_map_page() sg buffer failed\n");
280 goto err;
281 }
282 l_sg = sg_next(l_sg);
283 }
284 return nents;
285
286err:
287 /* Restore mapped parts */
288 for (j = 0; j < i; j++) {
289 if (!sg)
290 break;
291 dma_unmap_sg(dev, sg, 1, direction);
292 sg = sg_next(sg);
293 }
294 return 0;
295}
296
297static int cc_map_sg(struct device *dev, struct scatterlist *sg,
298 unsigned int nbytes, int direction, u32 *nents,
299 u32 max_sg_nents, u32 *lbytes, u32 *mapped_nents)
300{
301 bool is_chained = false;
302
303 if (sg_is_last(sg)) {
304 /* One entry only case -set to DLLI */
305 if (dma_map_sg(dev, sg, 1, direction) != 1) {
306 dev_err(dev, "dma_map_sg() single buffer failed\n");
307 return -ENOMEM;
308 }
309 dev_dbg(dev, "Mapped sg: dma_address=%pad page=%p addr=%pK offset=%u length=%u\n",
310 &sg_dma_address(sg), sg_page(sg), sg_virt(sg),
311 sg->offset, sg->length);
312 *lbytes = nbytes;
313 *nents = 1;
314 *mapped_nents = 1;
315 } else { /*sg_is_last*/
316 *nents = cc_get_sgl_nents(dev, sg, nbytes, lbytes,
317 &is_chained);
318 if (*nents > max_sg_nents) {
319 *nents = 0;
320 dev_err(dev, "Too many fragments. current %d max %d\n",
321 *nents, max_sg_nents);
322 return -ENOMEM;
323 }
324 if (!is_chained) {
325 /* In case of mmu the number of mapped nents might
326 * be changed from the original sgl nents
327 */
328 *mapped_nents = dma_map_sg(dev, sg, *nents, direction);
329 if (*mapped_nents == 0) {
330 *nents = 0;
331 dev_err(dev, "dma_map_sg() sg buffer failed\n");
332 return -ENOMEM;
333 }
334 } else {
335 /*In this case the driver maps entry by entry so it
336 * must have the same nents before and after map
337 */
338 *mapped_nents = cc_dma_map_sg(dev, sg, *nents,
339 direction);
340 if (*mapped_nents != *nents) {
341 *nents = *mapped_nents;
342 dev_err(dev, "dma_map_sg() sg buffer failed\n");
343 return -ENOMEM;
344 }
345 }
346 }
347
348 return 0;
349}
350
Gilad Ben-Yossef63ee04c2018-01-22 09:27:01 +0000351void cc_unmap_cipher_request(struct device *dev, void *ctx,
352 unsigned int ivsize, struct scatterlist *src,
353 struct scatterlist *dst)
354{
355 struct cipher_req_ctx *req_ctx = (struct cipher_req_ctx *)ctx;
356
357 if (req_ctx->gen_ctx.iv_dma_addr) {
358 dev_dbg(dev, "Unmapped iv: iv_dma_addr=%pad iv_size=%u\n",
359 &req_ctx->gen_ctx.iv_dma_addr, ivsize);
360 dma_unmap_single(dev, req_ctx->gen_ctx.iv_dma_addr,
361 ivsize,
362 req_ctx->is_giv ? DMA_BIDIRECTIONAL :
363 DMA_TO_DEVICE);
364 }
365 /* Release pool */
366 if (req_ctx->dma_buf_type == CC_DMA_BUF_MLLI &&
367 req_ctx->mlli_params.mlli_virt_addr) {
368 dma_pool_free(req_ctx->mlli_params.curr_pool,
369 req_ctx->mlli_params.mlli_virt_addr,
370 req_ctx->mlli_params.mlli_dma_addr);
371 }
372
373 dma_unmap_sg(dev, src, req_ctx->in_nents, DMA_BIDIRECTIONAL);
374 dev_dbg(dev, "Unmapped req->src=%pK\n", sg_virt(src));
375
376 if (src != dst) {
377 dma_unmap_sg(dev, dst, req_ctx->out_nents, DMA_BIDIRECTIONAL);
378 dev_dbg(dev, "Unmapped req->dst=%pK\n", sg_virt(dst));
379 }
380}
381
382int cc_map_cipher_request(struct cc_drvdata *drvdata, void *ctx,
383 unsigned int ivsize, unsigned int nbytes,
384 void *info, struct scatterlist *src,
385 struct scatterlist *dst, gfp_t flags)
386{
387 struct cipher_req_ctx *req_ctx = (struct cipher_req_ctx *)ctx;
388 struct mlli_params *mlli_params = &req_ctx->mlli_params;
389 struct buff_mgr_handle *buff_mgr = drvdata->buff_mgr_handle;
390 struct device *dev = drvdata_to_dev(drvdata);
391 struct buffer_array sg_data;
392 u32 dummy = 0;
393 int rc = 0;
394 u32 mapped_nents = 0;
395
396 req_ctx->dma_buf_type = CC_DMA_BUF_DLLI;
397 mlli_params->curr_pool = NULL;
398 sg_data.num_of_buffers = 0;
399
400 /* Map IV buffer */
401 if (ivsize) {
402 dump_byte_array("iv", (u8 *)info, ivsize);
403 req_ctx->gen_ctx.iv_dma_addr =
404 dma_map_single(dev, (void *)info,
405 ivsize,
406 req_ctx->is_giv ? DMA_BIDIRECTIONAL :
407 DMA_TO_DEVICE);
408 if (dma_mapping_error(dev, req_ctx->gen_ctx.iv_dma_addr)) {
409 dev_err(dev, "Mapping iv %u B at va=%pK for DMA failed\n",
410 ivsize, info);
411 return -ENOMEM;
412 }
413 dev_dbg(dev, "Mapped iv %u B at va=%pK to dma=%pad\n",
414 ivsize, info, &req_ctx->gen_ctx.iv_dma_addr);
415 } else {
416 req_ctx->gen_ctx.iv_dma_addr = 0;
417 }
418
419 /* Map the src SGL */
420 rc = cc_map_sg(dev, src, nbytes, DMA_BIDIRECTIONAL, &req_ctx->in_nents,
421 LLI_MAX_NUM_OF_DATA_ENTRIES, &dummy, &mapped_nents);
422 if (rc) {
423 rc = -ENOMEM;
424 goto cipher_exit;
425 }
426 if (mapped_nents > 1)
427 req_ctx->dma_buf_type = CC_DMA_BUF_MLLI;
428
429 if (src == dst) {
430 /* Handle inplace operation */
431 if (req_ctx->dma_buf_type == CC_DMA_BUF_MLLI) {
432 req_ctx->out_nents = 0;
433 cc_add_sg_entry(dev, &sg_data, req_ctx->in_nents, src,
434 nbytes, 0, true,
435 &req_ctx->in_mlli_nents);
436 }
437 } else {
438 /* Map the dst sg */
439 if (cc_map_sg(dev, dst, nbytes, DMA_BIDIRECTIONAL,
440 &req_ctx->out_nents, LLI_MAX_NUM_OF_DATA_ENTRIES,
441 &dummy, &mapped_nents)) {
442 rc = -ENOMEM;
443 goto cipher_exit;
444 }
445 if (mapped_nents > 1)
446 req_ctx->dma_buf_type = CC_DMA_BUF_MLLI;
447
448 if (req_ctx->dma_buf_type == CC_DMA_BUF_MLLI) {
449 cc_add_sg_entry(dev, &sg_data, req_ctx->in_nents, src,
450 nbytes, 0, true,
451 &req_ctx->in_mlli_nents);
452 cc_add_sg_entry(dev, &sg_data, req_ctx->out_nents, dst,
453 nbytes, 0, true,
454 &req_ctx->out_mlli_nents);
455 }
456 }
457
458 if (req_ctx->dma_buf_type == CC_DMA_BUF_MLLI) {
459 mlli_params->curr_pool = buff_mgr->mlli_buffs_pool;
460 rc = cc_generate_mlli(dev, &sg_data, mlli_params, flags);
461 if (rc)
462 goto cipher_exit;
463 }
464
465 dev_dbg(dev, "areq_ctx->dma_buf_type = %s\n",
466 cc_dma_buf_type(req_ctx->dma_buf_type));
467
468 return 0;
469
470cipher_exit:
471 cc_unmap_cipher_request(dev, req_ctx, ivsize, src, dst);
472 return rc;
473}
474
Gilad Ben-Yossef4c3f9722018-01-22 09:27:00 +0000475int cc_buffer_mgr_init(struct cc_drvdata *drvdata)
476{
477 struct buff_mgr_handle *buff_mgr_handle;
478 struct device *dev = drvdata_to_dev(drvdata);
479
480 buff_mgr_handle = kmalloc(sizeof(*buff_mgr_handle), GFP_KERNEL);
481 if (!buff_mgr_handle)
482 return -ENOMEM;
483
484 drvdata->buff_mgr_handle = buff_mgr_handle;
485
486 buff_mgr_handle->mlli_buffs_pool =
487 dma_pool_create("dx_single_mlli_tables", dev,
488 MAX_NUM_OF_TOTAL_MLLI_ENTRIES *
489 LLI_ENTRY_BYTE_SIZE,
490 MLLI_TABLE_MIN_ALIGNMENT, 0);
491
492 if (!buff_mgr_handle->mlli_buffs_pool)
493 goto error;
494
495 return 0;
496
497error:
498 cc_buffer_mgr_fini(drvdata);
499 return -ENOMEM;
500}
501
502int cc_buffer_mgr_fini(struct cc_drvdata *drvdata)
503{
504 struct buff_mgr_handle *buff_mgr_handle = drvdata->buff_mgr_handle;
505
506 if (buff_mgr_handle) {
507 dma_pool_destroy(buff_mgr_handle->mlli_buffs_pool);
508 kfree(drvdata->buff_mgr_handle);
509 drvdata->buff_mgr_handle = NULL;
510 }
511 return 0;
512}