cc_buffer_mgr.c 49 KB

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