cc_buffer_mgr.c 48 KB

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