cc_buffer_mgr.c 44 KB

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