cc_buffer_mgr.c 48 KB

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