crypto4xx_core.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  1. /**
  2. * AMCC SoC PPC4xx Crypto Driver
  3. *
  4. * Copyright (c) 2008 Applied Micro Circuits Corporation.
  5. * All rights reserved. James Hsiao <jhsiao@amcc.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * This file implements AMCC crypto offload Linux device driver for use with
  18. * Linux CryptoAPI.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/spinlock_types.h>
  23. #include <linux/random.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/crypto.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/init.h>
  29. #include <linux/module.h>
  30. #include <linux/of_address.h>
  31. #include <linux/of_irq.h>
  32. #include <linux/of_platform.h>
  33. #include <linux/slab.h>
  34. #include <asm/dcr.h>
  35. #include <asm/dcr-regs.h>
  36. #include <asm/cacheflush.h>
  37. #include <crypto/aes.h>
  38. #include <crypto/sha.h>
  39. #include "crypto4xx_reg_def.h"
  40. #include "crypto4xx_core.h"
  41. #include "crypto4xx_sa.h"
  42. #include "crypto4xx_trng.h"
  43. #define PPC4XX_SEC_VERSION_STR "0.5"
  44. /**
  45. * PPC4xx Crypto Engine Initialization Routine
  46. */
  47. static void crypto4xx_hw_init(struct crypto4xx_device *dev)
  48. {
  49. union ce_ring_size ring_size;
  50. union ce_ring_contol ring_ctrl;
  51. union ce_part_ring_size part_ring_size;
  52. union ce_io_threshold io_threshold;
  53. u32 rand_num;
  54. union ce_pe_dma_cfg pe_dma_cfg;
  55. u32 device_ctrl;
  56. writel(PPC4XX_BYTE_ORDER, dev->ce_base + CRYPTO4XX_BYTE_ORDER_CFG);
  57. /* setup pe dma, include reset sg, pdr and pe, then release reset */
  58. pe_dma_cfg.w = 0;
  59. pe_dma_cfg.bf.bo_sgpd_en = 1;
  60. pe_dma_cfg.bf.bo_data_en = 0;
  61. pe_dma_cfg.bf.bo_sa_en = 1;
  62. pe_dma_cfg.bf.bo_pd_en = 1;
  63. pe_dma_cfg.bf.dynamic_sa_en = 1;
  64. pe_dma_cfg.bf.reset_sg = 1;
  65. pe_dma_cfg.bf.reset_pdr = 1;
  66. pe_dma_cfg.bf.reset_pe = 1;
  67. writel(pe_dma_cfg.w, dev->ce_base + CRYPTO4XX_PE_DMA_CFG);
  68. /* un reset pe,sg and pdr */
  69. pe_dma_cfg.bf.pe_mode = 0;
  70. pe_dma_cfg.bf.reset_sg = 0;
  71. pe_dma_cfg.bf.reset_pdr = 0;
  72. pe_dma_cfg.bf.reset_pe = 0;
  73. pe_dma_cfg.bf.bo_td_en = 0;
  74. writel(pe_dma_cfg.w, dev->ce_base + CRYPTO4XX_PE_DMA_CFG);
  75. writel(dev->pdr_pa, dev->ce_base + CRYPTO4XX_PDR_BASE);
  76. writel(dev->pdr_pa, dev->ce_base + CRYPTO4XX_RDR_BASE);
  77. writel(PPC4XX_PRNG_CTRL_AUTO_EN, dev->ce_base + CRYPTO4XX_PRNG_CTRL);
  78. get_random_bytes(&rand_num, sizeof(rand_num));
  79. writel(rand_num, dev->ce_base + CRYPTO4XX_PRNG_SEED_L);
  80. get_random_bytes(&rand_num, sizeof(rand_num));
  81. writel(rand_num, dev->ce_base + CRYPTO4XX_PRNG_SEED_H);
  82. ring_size.w = 0;
  83. ring_size.bf.ring_offset = PPC4XX_PD_SIZE;
  84. ring_size.bf.ring_size = PPC4XX_NUM_PD;
  85. writel(ring_size.w, dev->ce_base + CRYPTO4XX_RING_SIZE);
  86. ring_ctrl.w = 0;
  87. writel(ring_ctrl.w, dev->ce_base + CRYPTO4XX_RING_CTRL);
  88. device_ctrl = readl(dev->ce_base + CRYPTO4XX_DEVICE_CTRL);
  89. device_ctrl |= PPC4XX_DC_3DES_EN;
  90. writel(device_ctrl, dev->ce_base + CRYPTO4XX_DEVICE_CTRL);
  91. writel(dev->gdr_pa, dev->ce_base + CRYPTO4XX_GATH_RING_BASE);
  92. writel(dev->sdr_pa, dev->ce_base + CRYPTO4XX_SCAT_RING_BASE);
  93. part_ring_size.w = 0;
  94. part_ring_size.bf.sdr_size = PPC4XX_SDR_SIZE;
  95. part_ring_size.bf.gdr_size = PPC4XX_GDR_SIZE;
  96. writel(part_ring_size.w, dev->ce_base + CRYPTO4XX_PART_RING_SIZE);
  97. writel(PPC4XX_SD_BUFFER_SIZE, dev->ce_base + CRYPTO4XX_PART_RING_CFG);
  98. io_threshold.w = 0;
  99. io_threshold.bf.output_threshold = PPC4XX_OUTPUT_THRESHOLD;
  100. io_threshold.bf.input_threshold = PPC4XX_INPUT_THRESHOLD;
  101. writel(io_threshold.w, dev->ce_base + CRYPTO4XX_IO_THRESHOLD);
  102. writel(0, dev->ce_base + CRYPTO4XX_PDR_BASE_UADDR);
  103. writel(0, dev->ce_base + CRYPTO4XX_RDR_BASE_UADDR);
  104. writel(0, dev->ce_base + CRYPTO4XX_PKT_SRC_UADDR);
  105. writel(0, dev->ce_base + CRYPTO4XX_PKT_DEST_UADDR);
  106. writel(0, dev->ce_base + CRYPTO4XX_SA_UADDR);
  107. writel(0, dev->ce_base + CRYPTO4XX_GATH_RING_BASE_UADDR);
  108. writel(0, dev->ce_base + CRYPTO4XX_SCAT_RING_BASE_UADDR);
  109. /* un reset pe,sg and pdr */
  110. pe_dma_cfg.bf.pe_mode = 1;
  111. pe_dma_cfg.bf.reset_sg = 0;
  112. pe_dma_cfg.bf.reset_pdr = 0;
  113. pe_dma_cfg.bf.reset_pe = 0;
  114. pe_dma_cfg.bf.bo_td_en = 0;
  115. writel(pe_dma_cfg.w, dev->ce_base + CRYPTO4XX_PE_DMA_CFG);
  116. /*clear all pending interrupt*/
  117. writel(PPC4XX_INTERRUPT_CLR, dev->ce_base + CRYPTO4XX_INT_CLR);
  118. writel(PPC4XX_INT_DESCR_CNT, dev->ce_base + CRYPTO4XX_INT_DESCR_CNT);
  119. writel(PPC4XX_INT_DESCR_CNT, dev->ce_base + CRYPTO4XX_INT_DESCR_CNT);
  120. writel(PPC4XX_INT_CFG, dev->ce_base + CRYPTO4XX_INT_CFG);
  121. writel(PPC4XX_PD_DONE_INT, dev->ce_base + CRYPTO4XX_INT_EN);
  122. }
  123. int crypto4xx_alloc_sa(struct crypto4xx_ctx *ctx, u32 size)
  124. {
  125. ctx->sa_in = dma_alloc_coherent(ctx->dev->core_dev->device, size * 4,
  126. &ctx->sa_in_dma_addr, GFP_ATOMIC);
  127. if (ctx->sa_in == NULL)
  128. return -ENOMEM;
  129. ctx->sa_out = dma_alloc_coherent(ctx->dev->core_dev->device, size * 4,
  130. &ctx->sa_out_dma_addr, GFP_ATOMIC);
  131. if (ctx->sa_out == NULL) {
  132. dma_free_coherent(ctx->dev->core_dev->device, size * 4,
  133. ctx->sa_in, ctx->sa_in_dma_addr);
  134. return -ENOMEM;
  135. }
  136. memset(ctx->sa_in, 0, size * 4);
  137. memset(ctx->sa_out, 0, size * 4);
  138. ctx->sa_len = size;
  139. return 0;
  140. }
  141. void crypto4xx_free_sa(struct crypto4xx_ctx *ctx)
  142. {
  143. if (ctx->sa_in != NULL)
  144. dma_free_coherent(ctx->dev->core_dev->device, ctx->sa_len * 4,
  145. ctx->sa_in, ctx->sa_in_dma_addr);
  146. if (ctx->sa_out != NULL)
  147. dma_free_coherent(ctx->dev->core_dev->device, ctx->sa_len * 4,
  148. ctx->sa_out, ctx->sa_out_dma_addr);
  149. ctx->sa_in_dma_addr = 0;
  150. ctx->sa_out_dma_addr = 0;
  151. ctx->sa_len = 0;
  152. }
  153. u32 crypto4xx_alloc_state_record(struct crypto4xx_ctx *ctx)
  154. {
  155. ctx->state_record = dma_alloc_coherent(ctx->dev->core_dev->device,
  156. sizeof(struct sa_state_record),
  157. &ctx->state_record_dma_addr, GFP_ATOMIC);
  158. if (!ctx->state_record_dma_addr)
  159. return -ENOMEM;
  160. memset(ctx->state_record, 0, sizeof(struct sa_state_record));
  161. return 0;
  162. }
  163. void crypto4xx_free_state_record(struct crypto4xx_ctx *ctx)
  164. {
  165. if (ctx->state_record != NULL)
  166. dma_free_coherent(ctx->dev->core_dev->device,
  167. sizeof(struct sa_state_record),
  168. ctx->state_record,
  169. ctx->state_record_dma_addr);
  170. ctx->state_record_dma_addr = 0;
  171. }
  172. /**
  173. * alloc memory for the gather ring
  174. * no need to alloc buf for the ring
  175. * gdr_tail, gdr_head and gdr_count are initialized by this function
  176. */
  177. static u32 crypto4xx_build_pdr(struct crypto4xx_device *dev)
  178. {
  179. int i;
  180. struct pd_uinfo *pd_uinfo;
  181. dev->pdr = dma_alloc_coherent(dev->core_dev->device,
  182. sizeof(struct ce_pd) * PPC4XX_NUM_PD,
  183. &dev->pdr_pa, GFP_ATOMIC);
  184. if (!dev->pdr)
  185. return -ENOMEM;
  186. dev->pdr_uinfo = kzalloc(sizeof(struct pd_uinfo) * PPC4XX_NUM_PD,
  187. GFP_KERNEL);
  188. if (!dev->pdr_uinfo) {
  189. dma_free_coherent(dev->core_dev->device,
  190. sizeof(struct ce_pd) * PPC4XX_NUM_PD,
  191. dev->pdr,
  192. dev->pdr_pa);
  193. return -ENOMEM;
  194. }
  195. memset(dev->pdr, 0, sizeof(struct ce_pd) * PPC4XX_NUM_PD);
  196. dev->shadow_sa_pool = dma_alloc_coherent(dev->core_dev->device,
  197. 256 * PPC4XX_NUM_PD,
  198. &dev->shadow_sa_pool_pa,
  199. GFP_ATOMIC);
  200. if (!dev->shadow_sa_pool)
  201. return -ENOMEM;
  202. dev->shadow_sr_pool = dma_alloc_coherent(dev->core_dev->device,
  203. sizeof(struct sa_state_record) * PPC4XX_NUM_PD,
  204. &dev->shadow_sr_pool_pa, GFP_ATOMIC);
  205. if (!dev->shadow_sr_pool)
  206. return -ENOMEM;
  207. for (i = 0; i < PPC4XX_NUM_PD; i++) {
  208. pd_uinfo = (struct pd_uinfo *) (dev->pdr_uinfo +
  209. sizeof(struct pd_uinfo) * i);
  210. /* alloc 256 bytes which is enough for any kind of dynamic sa */
  211. pd_uinfo->sa_va = dev->shadow_sa_pool + 256 * i;
  212. pd_uinfo->sa_pa = dev->shadow_sa_pool_pa + 256 * i;
  213. /* alloc state record */
  214. pd_uinfo->sr_va = dev->shadow_sr_pool +
  215. sizeof(struct sa_state_record) * i;
  216. pd_uinfo->sr_pa = dev->shadow_sr_pool_pa +
  217. sizeof(struct sa_state_record) * i;
  218. }
  219. return 0;
  220. }
  221. static void crypto4xx_destroy_pdr(struct crypto4xx_device *dev)
  222. {
  223. if (dev->pdr != NULL)
  224. dma_free_coherent(dev->core_dev->device,
  225. sizeof(struct ce_pd) * PPC4XX_NUM_PD,
  226. dev->pdr, dev->pdr_pa);
  227. if (dev->shadow_sa_pool)
  228. dma_free_coherent(dev->core_dev->device, 256 * PPC4XX_NUM_PD,
  229. dev->shadow_sa_pool, dev->shadow_sa_pool_pa);
  230. if (dev->shadow_sr_pool)
  231. dma_free_coherent(dev->core_dev->device,
  232. sizeof(struct sa_state_record) * PPC4XX_NUM_PD,
  233. dev->shadow_sr_pool, dev->shadow_sr_pool_pa);
  234. kfree(dev->pdr_uinfo);
  235. }
  236. static u32 crypto4xx_get_pd_from_pdr_nolock(struct crypto4xx_device *dev)
  237. {
  238. u32 retval;
  239. u32 tmp;
  240. retval = dev->pdr_head;
  241. tmp = (dev->pdr_head + 1) % PPC4XX_NUM_PD;
  242. if (tmp == dev->pdr_tail)
  243. return ERING_WAS_FULL;
  244. dev->pdr_head = tmp;
  245. return retval;
  246. }
  247. static u32 crypto4xx_put_pd_to_pdr(struct crypto4xx_device *dev, u32 idx)
  248. {
  249. struct pd_uinfo *pd_uinfo;
  250. unsigned long flags;
  251. pd_uinfo = (struct pd_uinfo *)(dev->pdr_uinfo +
  252. sizeof(struct pd_uinfo) * idx);
  253. spin_lock_irqsave(&dev->core_dev->lock, flags);
  254. if (dev->pdr_tail != PPC4XX_LAST_PD)
  255. dev->pdr_tail++;
  256. else
  257. dev->pdr_tail = 0;
  258. pd_uinfo->state = PD_ENTRY_FREE;
  259. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  260. return 0;
  261. }
  262. static struct ce_pd *crypto4xx_get_pdp(struct crypto4xx_device *dev,
  263. dma_addr_t *pd_dma, u32 idx)
  264. {
  265. *pd_dma = dev->pdr_pa + sizeof(struct ce_pd) * idx;
  266. return dev->pdr + sizeof(struct ce_pd) * idx;
  267. }
  268. /**
  269. * alloc memory for the gather ring
  270. * no need to alloc buf for the ring
  271. * gdr_tail, gdr_head and gdr_count are initialized by this function
  272. */
  273. static u32 crypto4xx_build_gdr(struct crypto4xx_device *dev)
  274. {
  275. dev->gdr = dma_alloc_coherent(dev->core_dev->device,
  276. sizeof(struct ce_gd) * PPC4XX_NUM_GD,
  277. &dev->gdr_pa, GFP_ATOMIC);
  278. if (!dev->gdr)
  279. return -ENOMEM;
  280. memset(dev->gdr, 0, sizeof(struct ce_gd) * PPC4XX_NUM_GD);
  281. return 0;
  282. }
  283. static inline void crypto4xx_destroy_gdr(struct crypto4xx_device *dev)
  284. {
  285. dma_free_coherent(dev->core_dev->device,
  286. sizeof(struct ce_gd) * PPC4XX_NUM_GD,
  287. dev->gdr, dev->gdr_pa);
  288. }
  289. /*
  290. * when this function is called.
  291. * preemption or interrupt must be disabled
  292. */
  293. u32 crypto4xx_get_n_gd(struct crypto4xx_device *dev, int n)
  294. {
  295. u32 retval;
  296. u32 tmp;
  297. if (n >= PPC4XX_NUM_GD)
  298. return ERING_WAS_FULL;
  299. retval = dev->gdr_head;
  300. tmp = (dev->gdr_head + n) % PPC4XX_NUM_GD;
  301. if (dev->gdr_head > dev->gdr_tail) {
  302. if (tmp < dev->gdr_head && tmp >= dev->gdr_tail)
  303. return ERING_WAS_FULL;
  304. } else if (dev->gdr_head < dev->gdr_tail) {
  305. if (tmp < dev->gdr_head || tmp >= dev->gdr_tail)
  306. return ERING_WAS_FULL;
  307. }
  308. dev->gdr_head = tmp;
  309. return retval;
  310. }
  311. static u32 crypto4xx_put_gd_to_gdr(struct crypto4xx_device *dev)
  312. {
  313. unsigned long flags;
  314. spin_lock_irqsave(&dev->core_dev->lock, flags);
  315. if (dev->gdr_tail == dev->gdr_head) {
  316. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  317. return 0;
  318. }
  319. if (dev->gdr_tail != PPC4XX_LAST_GD)
  320. dev->gdr_tail++;
  321. else
  322. dev->gdr_tail = 0;
  323. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  324. return 0;
  325. }
  326. static inline struct ce_gd *crypto4xx_get_gdp(struct crypto4xx_device *dev,
  327. dma_addr_t *gd_dma, u32 idx)
  328. {
  329. *gd_dma = dev->gdr_pa + sizeof(struct ce_gd) * idx;
  330. return (struct ce_gd *) (dev->gdr + sizeof(struct ce_gd) * idx);
  331. }
  332. /**
  333. * alloc memory for the scatter ring
  334. * need to alloc buf for the ring
  335. * sdr_tail, sdr_head and sdr_count are initialized by this function
  336. */
  337. static u32 crypto4xx_build_sdr(struct crypto4xx_device *dev)
  338. {
  339. int i;
  340. struct ce_sd *sd_array;
  341. /* alloc memory for scatter descriptor ring */
  342. dev->sdr = dma_alloc_coherent(dev->core_dev->device,
  343. sizeof(struct ce_sd) * PPC4XX_NUM_SD,
  344. &dev->sdr_pa, GFP_ATOMIC);
  345. if (!dev->sdr)
  346. return -ENOMEM;
  347. dev->scatter_buffer_size = PPC4XX_SD_BUFFER_SIZE;
  348. dev->scatter_buffer_va =
  349. dma_alloc_coherent(dev->core_dev->device,
  350. dev->scatter_buffer_size * PPC4XX_NUM_SD,
  351. &dev->scatter_buffer_pa, GFP_ATOMIC);
  352. if (!dev->scatter_buffer_va) {
  353. dma_free_coherent(dev->core_dev->device,
  354. sizeof(struct ce_sd) * PPC4XX_NUM_SD,
  355. dev->sdr, dev->sdr_pa);
  356. return -ENOMEM;
  357. }
  358. sd_array = dev->sdr;
  359. for (i = 0; i < PPC4XX_NUM_SD; i++) {
  360. sd_array[i].ptr = dev->scatter_buffer_pa +
  361. dev->scatter_buffer_size * i;
  362. }
  363. return 0;
  364. }
  365. static void crypto4xx_destroy_sdr(struct crypto4xx_device *dev)
  366. {
  367. if (dev->sdr != NULL)
  368. dma_free_coherent(dev->core_dev->device,
  369. sizeof(struct ce_sd) * PPC4XX_NUM_SD,
  370. dev->sdr, dev->sdr_pa);
  371. if (dev->scatter_buffer_va != NULL)
  372. dma_free_coherent(dev->core_dev->device,
  373. dev->scatter_buffer_size * PPC4XX_NUM_SD,
  374. dev->scatter_buffer_va,
  375. dev->scatter_buffer_pa);
  376. }
  377. /*
  378. * when this function is called.
  379. * preemption or interrupt must be disabled
  380. */
  381. static u32 crypto4xx_get_n_sd(struct crypto4xx_device *dev, int n)
  382. {
  383. u32 retval;
  384. u32 tmp;
  385. if (n >= PPC4XX_NUM_SD)
  386. return ERING_WAS_FULL;
  387. retval = dev->sdr_head;
  388. tmp = (dev->sdr_head + n) % PPC4XX_NUM_SD;
  389. if (dev->sdr_head > dev->gdr_tail) {
  390. if (tmp < dev->sdr_head && tmp >= dev->sdr_tail)
  391. return ERING_WAS_FULL;
  392. } else if (dev->sdr_head < dev->sdr_tail) {
  393. if (tmp < dev->sdr_head || tmp >= dev->sdr_tail)
  394. return ERING_WAS_FULL;
  395. } /* the head = tail, or empty case is already take cared */
  396. dev->sdr_head = tmp;
  397. return retval;
  398. }
  399. static u32 crypto4xx_put_sd_to_sdr(struct crypto4xx_device *dev)
  400. {
  401. unsigned long flags;
  402. spin_lock_irqsave(&dev->core_dev->lock, flags);
  403. if (dev->sdr_tail == dev->sdr_head) {
  404. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  405. return 0;
  406. }
  407. if (dev->sdr_tail != PPC4XX_LAST_SD)
  408. dev->sdr_tail++;
  409. else
  410. dev->sdr_tail = 0;
  411. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  412. return 0;
  413. }
  414. static inline struct ce_sd *crypto4xx_get_sdp(struct crypto4xx_device *dev,
  415. dma_addr_t *sd_dma, u32 idx)
  416. {
  417. *sd_dma = dev->sdr_pa + sizeof(struct ce_sd) * idx;
  418. return (struct ce_sd *)(dev->sdr + sizeof(struct ce_sd) * idx);
  419. }
  420. static u32 crypto4xx_fill_one_page(struct crypto4xx_device *dev,
  421. dma_addr_t *addr, u32 *length,
  422. u32 *idx, u32 *offset, u32 *nbytes)
  423. {
  424. u32 len;
  425. if (*length > dev->scatter_buffer_size) {
  426. memcpy(phys_to_virt(*addr),
  427. dev->scatter_buffer_va +
  428. *idx * dev->scatter_buffer_size + *offset,
  429. dev->scatter_buffer_size);
  430. *offset = 0;
  431. *length -= dev->scatter_buffer_size;
  432. *nbytes -= dev->scatter_buffer_size;
  433. if (*idx == PPC4XX_LAST_SD)
  434. *idx = 0;
  435. else
  436. (*idx)++;
  437. *addr = *addr + dev->scatter_buffer_size;
  438. return 1;
  439. } else if (*length < dev->scatter_buffer_size) {
  440. memcpy(phys_to_virt(*addr),
  441. dev->scatter_buffer_va +
  442. *idx * dev->scatter_buffer_size + *offset, *length);
  443. if ((*offset + *length) == dev->scatter_buffer_size) {
  444. if (*idx == PPC4XX_LAST_SD)
  445. *idx = 0;
  446. else
  447. (*idx)++;
  448. *nbytes -= *length;
  449. *offset = 0;
  450. } else {
  451. *nbytes -= *length;
  452. *offset += *length;
  453. }
  454. return 0;
  455. } else {
  456. len = (*nbytes <= dev->scatter_buffer_size) ?
  457. (*nbytes) : dev->scatter_buffer_size;
  458. memcpy(phys_to_virt(*addr),
  459. dev->scatter_buffer_va +
  460. *idx * dev->scatter_buffer_size + *offset,
  461. len);
  462. *offset = 0;
  463. *nbytes -= len;
  464. if (*idx == PPC4XX_LAST_SD)
  465. *idx = 0;
  466. else
  467. (*idx)++;
  468. return 0;
  469. }
  470. }
  471. static void crypto4xx_copy_pkt_to_dst(struct crypto4xx_device *dev,
  472. struct ce_pd *pd,
  473. struct pd_uinfo *pd_uinfo,
  474. u32 nbytes,
  475. struct scatterlist *dst)
  476. {
  477. dma_addr_t addr;
  478. u32 this_sd;
  479. u32 offset;
  480. u32 len;
  481. u32 i;
  482. u32 sg_len;
  483. struct scatterlist *sg;
  484. this_sd = pd_uinfo->first_sd;
  485. offset = 0;
  486. i = 0;
  487. while (nbytes) {
  488. sg = &dst[i];
  489. sg_len = sg->length;
  490. addr = dma_map_page(dev->core_dev->device, sg_page(sg),
  491. sg->offset, sg->length, DMA_TO_DEVICE);
  492. if (offset == 0) {
  493. len = (nbytes <= sg->length) ? nbytes : sg->length;
  494. while (crypto4xx_fill_one_page(dev, &addr, &len,
  495. &this_sd, &offset, &nbytes))
  496. ;
  497. if (!nbytes)
  498. return;
  499. i++;
  500. } else {
  501. len = (nbytes <= (dev->scatter_buffer_size - offset)) ?
  502. nbytes : (dev->scatter_buffer_size - offset);
  503. len = (sg->length < len) ? sg->length : len;
  504. while (crypto4xx_fill_one_page(dev, &addr, &len,
  505. &this_sd, &offset, &nbytes))
  506. ;
  507. if (!nbytes)
  508. return;
  509. sg_len -= len;
  510. if (sg_len) {
  511. addr += len;
  512. while (crypto4xx_fill_one_page(dev, &addr,
  513. &sg_len, &this_sd, &offset, &nbytes))
  514. ;
  515. }
  516. i++;
  517. }
  518. }
  519. }
  520. static u32 crypto4xx_copy_digest_to_dst(struct pd_uinfo *pd_uinfo,
  521. struct crypto4xx_ctx *ctx)
  522. {
  523. struct dynamic_sa_ctl *sa = (struct dynamic_sa_ctl *) ctx->sa_in;
  524. struct sa_state_record *state_record =
  525. (struct sa_state_record *) pd_uinfo->sr_va;
  526. if (sa->sa_command_0.bf.hash_alg == SA_HASH_ALG_SHA1) {
  527. memcpy((void *) pd_uinfo->dest_va, state_record->save_digest,
  528. SA_HASH_ALG_SHA1_DIGEST_SIZE);
  529. }
  530. return 0;
  531. }
  532. static void crypto4xx_ret_sg_desc(struct crypto4xx_device *dev,
  533. struct pd_uinfo *pd_uinfo)
  534. {
  535. int i;
  536. if (pd_uinfo->num_gd) {
  537. for (i = 0; i < pd_uinfo->num_gd; i++)
  538. crypto4xx_put_gd_to_gdr(dev);
  539. pd_uinfo->first_gd = 0xffffffff;
  540. pd_uinfo->num_gd = 0;
  541. }
  542. if (pd_uinfo->num_sd) {
  543. for (i = 0; i < pd_uinfo->num_sd; i++)
  544. crypto4xx_put_sd_to_sdr(dev);
  545. pd_uinfo->first_sd = 0xffffffff;
  546. pd_uinfo->num_sd = 0;
  547. }
  548. }
  549. static u32 crypto4xx_ablkcipher_done(struct crypto4xx_device *dev,
  550. struct pd_uinfo *pd_uinfo,
  551. struct ce_pd *pd)
  552. {
  553. struct crypto4xx_ctx *ctx;
  554. struct ablkcipher_request *ablk_req;
  555. struct scatterlist *dst;
  556. dma_addr_t addr;
  557. ablk_req = ablkcipher_request_cast(pd_uinfo->async_req);
  558. ctx = crypto_tfm_ctx(ablk_req->base.tfm);
  559. if (pd_uinfo->using_sd) {
  560. crypto4xx_copy_pkt_to_dst(dev, pd, pd_uinfo, ablk_req->nbytes,
  561. ablk_req->dst);
  562. } else {
  563. dst = pd_uinfo->dest_va;
  564. addr = dma_map_page(dev->core_dev->device, sg_page(dst),
  565. dst->offset, dst->length, DMA_FROM_DEVICE);
  566. }
  567. crypto4xx_ret_sg_desc(dev, pd_uinfo);
  568. if (ablk_req->base.complete != NULL)
  569. ablk_req->base.complete(&ablk_req->base, 0);
  570. return 0;
  571. }
  572. static u32 crypto4xx_ahash_done(struct crypto4xx_device *dev,
  573. struct pd_uinfo *pd_uinfo)
  574. {
  575. struct crypto4xx_ctx *ctx;
  576. struct ahash_request *ahash_req;
  577. ahash_req = ahash_request_cast(pd_uinfo->async_req);
  578. ctx = crypto_tfm_ctx(ahash_req->base.tfm);
  579. crypto4xx_copy_digest_to_dst(pd_uinfo,
  580. crypto_tfm_ctx(ahash_req->base.tfm));
  581. crypto4xx_ret_sg_desc(dev, pd_uinfo);
  582. /* call user provided callback function x */
  583. if (ahash_req->base.complete != NULL)
  584. ahash_req->base.complete(&ahash_req->base, 0);
  585. return 0;
  586. }
  587. static u32 crypto4xx_pd_done(struct crypto4xx_device *dev, u32 idx)
  588. {
  589. struct ce_pd *pd;
  590. struct pd_uinfo *pd_uinfo;
  591. pd = dev->pdr + sizeof(struct ce_pd)*idx;
  592. pd_uinfo = dev->pdr_uinfo + sizeof(struct pd_uinfo)*idx;
  593. if (crypto_tfm_alg_type(pd_uinfo->async_req->tfm) ==
  594. CRYPTO_ALG_TYPE_ABLKCIPHER)
  595. return crypto4xx_ablkcipher_done(dev, pd_uinfo, pd);
  596. else
  597. return crypto4xx_ahash_done(dev, pd_uinfo);
  598. }
  599. /**
  600. * Note: Only use this function to copy items that is word aligned.
  601. */
  602. void crypto4xx_memcpy_le(unsigned int *dst,
  603. const unsigned char *buf,
  604. int len)
  605. {
  606. u8 *tmp;
  607. for (; len >= 4; buf += 4, len -= 4)
  608. *dst++ = cpu_to_le32(*(unsigned int *) buf);
  609. tmp = (u8 *)dst;
  610. switch (len) {
  611. case 3:
  612. *tmp++ = 0;
  613. *tmp++ = *(buf+2);
  614. *tmp++ = *(buf+1);
  615. *tmp++ = *buf;
  616. break;
  617. case 2:
  618. *tmp++ = 0;
  619. *tmp++ = 0;
  620. *tmp++ = *(buf+1);
  621. *tmp++ = *buf;
  622. break;
  623. case 1:
  624. *tmp++ = 0;
  625. *tmp++ = 0;
  626. *tmp++ = 0;
  627. *tmp++ = *buf;
  628. break;
  629. default:
  630. break;
  631. }
  632. }
  633. static void crypto4xx_stop_all(struct crypto4xx_core_device *core_dev)
  634. {
  635. crypto4xx_destroy_pdr(core_dev->dev);
  636. crypto4xx_destroy_gdr(core_dev->dev);
  637. crypto4xx_destroy_sdr(core_dev->dev);
  638. iounmap(core_dev->dev->ce_base);
  639. kfree(core_dev->dev);
  640. kfree(core_dev);
  641. }
  642. void crypto4xx_return_pd(struct crypto4xx_device *dev,
  643. u32 pd_entry, struct ce_pd *pd,
  644. struct pd_uinfo *pd_uinfo)
  645. {
  646. /* irq should be already disabled */
  647. dev->pdr_head = pd_entry;
  648. pd->pd_ctl.w = 0;
  649. pd->pd_ctl_len.w = 0;
  650. pd_uinfo->state = PD_ENTRY_FREE;
  651. }
  652. static u32 get_next_gd(u32 current)
  653. {
  654. if (current != PPC4XX_LAST_GD)
  655. return current + 1;
  656. else
  657. return 0;
  658. }
  659. static u32 get_next_sd(u32 current)
  660. {
  661. if (current != PPC4XX_LAST_SD)
  662. return current + 1;
  663. else
  664. return 0;
  665. }
  666. u32 crypto4xx_build_pd(struct crypto_async_request *req,
  667. struct crypto4xx_ctx *ctx,
  668. struct scatterlist *src,
  669. struct scatterlist *dst,
  670. unsigned int datalen,
  671. void *iv, u32 iv_len)
  672. {
  673. struct crypto4xx_device *dev = ctx->dev;
  674. dma_addr_t addr, pd_dma, sd_dma, gd_dma;
  675. struct dynamic_sa_ctl *sa;
  676. struct scatterlist *sg;
  677. struct ce_gd *gd;
  678. struct ce_pd *pd;
  679. u32 num_gd, num_sd;
  680. u32 fst_gd = 0xffffffff;
  681. u32 fst_sd = 0xffffffff;
  682. u32 pd_entry;
  683. unsigned long flags;
  684. struct pd_uinfo *pd_uinfo = NULL;
  685. unsigned int nbytes = datalen, idx;
  686. unsigned int ivlen = 0;
  687. u32 gd_idx = 0;
  688. /* figure how many gd is needed */
  689. num_gd = sg_nents_for_len(src, datalen);
  690. if ((int)num_gd < 0) {
  691. dev_err(dev->core_dev->device, "Invalid number of src SG.\n");
  692. return -EINVAL;
  693. }
  694. if (num_gd == 1)
  695. num_gd = 0;
  696. /* figure how many sd is needed */
  697. if (sg_is_last(dst) || ctx->is_hash) {
  698. num_sd = 0;
  699. } else {
  700. if (datalen > PPC4XX_SD_BUFFER_SIZE) {
  701. num_sd = datalen / PPC4XX_SD_BUFFER_SIZE;
  702. if (datalen % PPC4XX_SD_BUFFER_SIZE)
  703. num_sd++;
  704. } else {
  705. num_sd = 1;
  706. }
  707. }
  708. /*
  709. * The follow section of code needs to be protected
  710. * The gather ring and scatter ring needs to be consecutive
  711. * In case of run out of any kind of descriptor, the descriptor
  712. * already got must be return the original place.
  713. */
  714. spin_lock_irqsave(&dev->core_dev->lock, flags);
  715. if (num_gd) {
  716. fst_gd = crypto4xx_get_n_gd(dev, num_gd);
  717. if (fst_gd == ERING_WAS_FULL) {
  718. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  719. return -EAGAIN;
  720. }
  721. }
  722. if (num_sd) {
  723. fst_sd = crypto4xx_get_n_sd(dev, num_sd);
  724. if (fst_sd == ERING_WAS_FULL) {
  725. if (num_gd)
  726. dev->gdr_head = fst_gd;
  727. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  728. return -EAGAIN;
  729. }
  730. }
  731. pd_entry = crypto4xx_get_pd_from_pdr_nolock(dev);
  732. if (pd_entry == ERING_WAS_FULL) {
  733. if (num_gd)
  734. dev->gdr_head = fst_gd;
  735. if (num_sd)
  736. dev->sdr_head = fst_sd;
  737. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  738. return -EAGAIN;
  739. }
  740. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  741. pd_uinfo = (struct pd_uinfo *)(dev->pdr_uinfo +
  742. sizeof(struct pd_uinfo) * pd_entry);
  743. pd = crypto4xx_get_pdp(dev, &pd_dma, pd_entry);
  744. pd_uinfo->async_req = req;
  745. pd_uinfo->num_gd = num_gd;
  746. pd_uinfo->num_sd = num_sd;
  747. if (iv_len || ctx->is_hash) {
  748. ivlen = iv_len;
  749. pd->sa = pd_uinfo->sa_pa;
  750. sa = (struct dynamic_sa_ctl *) pd_uinfo->sa_va;
  751. if (ctx->direction == DIR_INBOUND)
  752. memcpy(sa, ctx->sa_in, ctx->sa_len * 4);
  753. else
  754. memcpy(sa, ctx->sa_out, ctx->sa_len * 4);
  755. memcpy((void *) sa + ctx->offset_to_sr_ptr,
  756. &pd_uinfo->sr_pa, 4);
  757. if (iv_len)
  758. crypto4xx_memcpy_le(pd_uinfo->sr_va, iv, iv_len);
  759. } else {
  760. if (ctx->direction == DIR_INBOUND) {
  761. pd->sa = ctx->sa_in_dma_addr;
  762. sa = (struct dynamic_sa_ctl *) ctx->sa_in;
  763. } else {
  764. pd->sa = ctx->sa_out_dma_addr;
  765. sa = (struct dynamic_sa_ctl *) ctx->sa_out;
  766. }
  767. }
  768. pd->sa_len = ctx->sa_len;
  769. if (num_gd) {
  770. /* get first gd we are going to use */
  771. gd_idx = fst_gd;
  772. pd_uinfo->first_gd = fst_gd;
  773. pd_uinfo->num_gd = num_gd;
  774. gd = crypto4xx_get_gdp(dev, &gd_dma, gd_idx);
  775. pd->src = gd_dma;
  776. /* enable gather */
  777. sa->sa_command_0.bf.gather = 1;
  778. idx = 0;
  779. src = &src[0];
  780. /* walk the sg, and setup gather array */
  781. while (nbytes) {
  782. sg = &src[idx];
  783. addr = dma_map_page(dev->core_dev->device, sg_page(sg),
  784. sg->offset, sg->length, DMA_TO_DEVICE);
  785. gd->ptr = addr;
  786. gd->ctl_len.len = sg->length;
  787. gd->ctl_len.done = 0;
  788. gd->ctl_len.ready = 1;
  789. if (sg->length >= nbytes)
  790. break;
  791. nbytes -= sg->length;
  792. gd_idx = get_next_gd(gd_idx);
  793. gd = crypto4xx_get_gdp(dev, &gd_dma, gd_idx);
  794. idx++;
  795. }
  796. } else {
  797. pd->src = (u32)dma_map_page(dev->core_dev->device, sg_page(src),
  798. src->offset, src->length, DMA_TO_DEVICE);
  799. /*
  800. * Disable gather in sa command
  801. */
  802. sa->sa_command_0.bf.gather = 0;
  803. /*
  804. * Indicate gather array is not used
  805. */
  806. pd_uinfo->first_gd = 0xffffffff;
  807. pd_uinfo->num_gd = 0;
  808. }
  809. if (ctx->is_hash || sg_is_last(dst)) {
  810. /*
  811. * we know application give us dst a whole piece of memory
  812. * no need to use scatter ring.
  813. * In case of is_hash, the icv is always at end of src data.
  814. */
  815. pd_uinfo->using_sd = 0;
  816. pd_uinfo->first_sd = 0xffffffff;
  817. pd_uinfo->num_sd = 0;
  818. pd_uinfo->dest_va = dst;
  819. sa->sa_command_0.bf.scatter = 0;
  820. if (ctx->is_hash)
  821. pd->dest = virt_to_phys((void *)dst);
  822. else
  823. pd->dest = (u32)dma_map_page(dev->core_dev->device,
  824. sg_page(dst), dst->offset,
  825. dst->length, DMA_TO_DEVICE);
  826. } else {
  827. struct ce_sd *sd = NULL;
  828. u32 sd_idx = fst_sd;
  829. nbytes = datalen;
  830. sa->sa_command_0.bf.scatter = 1;
  831. pd_uinfo->using_sd = 1;
  832. pd_uinfo->dest_va = dst;
  833. pd_uinfo->first_sd = fst_sd;
  834. pd_uinfo->num_sd = num_sd;
  835. sd = crypto4xx_get_sdp(dev, &sd_dma, sd_idx);
  836. pd->dest = sd_dma;
  837. /* setup scatter descriptor */
  838. sd->ctl.done = 0;
  839. sd->ctl.rdy = 1;
  840. /* sd->ptr should be setup by sd_init routine*/
  841. idx = 0;
  842. if (nbytes >= PPC4XX_SD_BUFFER_SIZE)
  843. nbytes -= PPC4XX_SD_BUFFER_SIZE;
  844. else
  845. nbytes = 0;
  846. while (nbytes) {
  847. sd_idx = get_next_sd(sd_idx);
  848. sd = crypto4xx_get_sdp(dev, &sd_dma, sd_idx);
  849. /* setup scatter descriptor */
  850. sd->ctl.done = 0;
  851. sd->ctl.rdy = 1;
  852. if (nbytes >= PPC4XX_SD_BUFFER_SIZE)
  853. nbytes -= PPC4XX_SD_BUFFER_SIZE;
  854. else
  855. /*
  856. * SD entry can hold PPC4XX_SD_BUFFER_SIZE,
  857. * which is more than nbytes, so done.
  858. */
  859. nbytes = 0;
  860. }
  861. }
  862. sa->sa_command_1.bf.hash_crypto_offset = 0;
  863. pd->pd_ctl.w = ctx->pd_ctl;
  864. pd->pd_ctl_len.w = 0x00400000 | (ctx->bypass << 24) | datalen;
  865. pd_uinfo->state = PD_ENTRY_INUSE;
  866. wmb();
  867. /* write any value to push engine to read a pd */
  868. writel(1, dev->ce_base + CRYPTO4XX_INT_DESCR_RD);
  869. return -EINPROGRESS;
  870. }
  871. /**
  872. * Algorithm Registration Functions
  873. */
  874. static int crypto4xx_alg_init(struct crypto_tfm *tfm)
  875. {
  876. struct crypto_alg *alg = tfm->__crt_alg;
  877. struct crypto4xx_alg *amcc_alg = crypto_alg_to_crypto4xx_alg(alg);
  878. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  879. ctx->dev = amcc_alg->dev;
  880. ctx->sa_in = NULL;
  881. ctx->sa_out = NULL;
  882. ctx->sa_in_dma_addr = 0;
  883. ctx->sa_out_dma_addr = 0;
  884. ctx->sa_len = 0;
  885. switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
  886. default:
  887. tfm->crt_ablkcipher.reqsize = sizeof(struct crypto4xx_ctx);
  888. break;
  889. case CRYPTO_ALG_TYPE_AHASH:
  890. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  891. sizeof(struct crypto4xx_ctx));
  892. break;
  893. }
  894. return 0;
  895. }
  896. static void crypto4xx_alg_exit(struct crypto_tfm *tfm)
  897. {
  898. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  899. crypto4xx_free_sa(ctx);
  900. crypto4xx_free_state_record(ctx);
  901. }
  902. int crypto4xx_register_alg(struct crypto4xx_device *sec_dev,
  903. struct crypto4xx_alg_common *crypto_alg,
  904. int array_size)
  905. {
  906. struct crypto4xx_alg *alg;
  907. int i;
  908. int rc = 0;
  909. for (i = 0; i < array_size; i++) {
  910. alg = kzalloc(sizeof(struct crypto4xx_alg), GFP_KERNEL);
  911. if (!alg)
  912. return -ENOMEM;
  913. alg->alg = crypto_alg[i];
  914. alg->dev = sec_dev;
  915. switch (alg->alg.type) {
  916. case CRYPTO_ALG_TYPE_AHASH:
  917. rc = crypto_register_ahash(&alg->alg.u.hash);
  918. break;
  919. default:
  920. rc = crypto_register_alg(&alg->alg.u.cipher);
  921. break;
  922. }
  923. if (rc) {
  924. list_del(&alg->entry);
  925. kfree(alg);
  926. } else {
  927. list_add_tail(&alg->entry, &sec_dev->alg_list);
  928. }
  929. }
  930. return 0;
  931. }
  932. static void crypto4xx_unregister_alg(struct crypto4xx_device *sec_dev)
  933. {
  934. struct crypto4xx_alg *alg, *tmp;
  935. list_for_each_entry_safe(alg, tmp, &sec_dev->alg_list, entry) {
  936. list_del(&alg->entry);
  937. switch (alg->alg.type) {
  938. case CRYPTO_ALG_TYPE_AHASH:
  939. crypto_unregister_ahash(&alg->alg.u.hash);
  940. break;
  941. default:
  942. crypto_unregister_alg(&alg->alg.u.cipher);
  943. }
  944. kfree(alg);
  945. }
  946. }
  947. static void crypto4xx_bh_tasklet_cb(unsigned long data)
  948. {
  949. struct device *dev = (struct device *)data;
  950. struct crypto4xx_core_device *core_dev = dev_get_drvdata(dev);
  951. struct pd_uinfo *pd_uinfo;
  952. struct ce_pd *pd;
  953. u32 tail;
  954. while (core_dev->dev->pdr_head != core_dev->dev->pdr_tail) {
  955. tail = core_dev->dev->pdr_tail;
  956. pd_uinfo = core_dev->dev->pdr_uinfo +
  957. sizeof(struct pd_uinfo)*tail;
  958. pd = core_dev->dev->pdr + sizeof(struct ce_pd) * tail;
  959. if ((pd_uinfo->state == PD_ENTRY_INUSE) &&
  960. pd->pd_ctl.bf.pe_done &&
  961. !pd->pd_ctl.bf.host_ready) {
  962. pd->pd_ctl.bf.pe_done = 0;
  963. crypto4xx_pd_done(core_dev->dev, tail);
  964. crypto4xx_put_pd_to_pdr(core_dev->dev, tail);
  965. pd_uinfo->state = PD_ENTRY_FREE;
  966. } else {
  967. /* if tail not done, break */
  968. break;
  969. }
  970. }
  971. }
  972. /**
  973. * Top Half of isr.
  974. */
  975. static irqreturn_t crypto4xx_ce_interrupt_handler(int irq, void *data)
  976. {
  977. struct device *dev = (struct device *)data;
  978. struct crypto4xx_core_device *core_dev = dev_get_drvdata(dev);
  979. if (!core_dev->dev->ce_base)
  980. return 0;
  981. writel(PPC4XX_INTERRUPT_CLR,
  982. core_dev->dev->ce_base + CRYPTO4XX_INT_CLR);
  983. tasklet_schedule(&core_dev->tasklet);
  984. return IRQ_HANDLED;
  985. }
  986. /**
  987. * Supported Crypto Algorithms
  988. */
  989. struct crypto4xx_alg_common crypto4xx_alg[] = {
  990. /* Crypto AES modes */
  991. { .type = CRYPTO_ALG_TYPE_ABLKCIPHER, .u.cipher = {
  992. .cra_name = "cbc(aes)",
  993. .cra_driver_name = "cbc-aes-ppc4xx",
  994. .cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
  995. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  996. .cra_blocksize = AES_BLOCK_SIZE,
  997. .cra_ctxsize = sizeof(struct crypto4xx_ctx),
  998. .cra_type = &crypto_ablkcipher_type,
  999. .cra_init = crypto4xx_alg_init,
  1000. .cra_exit = crypto4xx_alg_exit,
  1001. .cra_module = THIS_MODULE,
  1002. .cra_u = {
  1003. .ablkcipher = {
  1004. .min_keysize = AES_MIN_KEY_SIZE,
  1005. .max_keysize = AES_MAX_KEY_SIZE,
  1006. .ivsize = AES_IV_SIZE,
  1007. .setkey = crypto4xx_setkey_aes_cbc,
  1008. .encrypt = crypto4xx_encrypt,
  1009. .decrypt = crypto4xx_decrypt,
  1010. }
  1011. }
  1012. }},
  1013. };
  1014. /**
  1015. * Module Initialization Routine
  1016. */
  1017. static int crypto4xx_probe(struct platform_device *ofdev)
  1018. {
  1019. int rc;
  1020. struct resource res;
  1021. struct device *dev = &ofdev->dev;
  1022. struct crypto4xx_core_device *core_dev;
  1023. rc = of_address_to_resource(ofdev->dev.of_node, 0, &res);
  1024. if (rc)
  1025. return -ENODEV;
  1026. if (of_find_compatible_node(NULL, NULL, "amcc,ppc460ex-crypto")) {
  1027. mtdcri(SDR0, PPC460EX_SDR0_SRST,
  1028. mfdcri(SDR0, PPC460EX_SDR0_SRST) | PPC460EX_CE_RESET);
  1029. mtdcri(SDR0, PPC460EX_SDR0_SRST,
  1030. mfdcri(SDR0, PPC460EX_SDR0_SRST) & ~PPC460EX_CE_RESET);
  1031. } else if (of_find_compatible_node(NULL, NULL,
  1032. "amcc,ppc405ex-crypto")) {
  1033. mtdcri(SDR0, PPC405EX_SDR0_SRST,
  1034. mfdcri(SDR0, PPC405EX_SDR0_SRST) | PPC405EX_CE_RESET);
  1035. mtdcri(SDR0, PPC405EX_SDR0_SRST,
  1036. mfdcri(SDR0, PPC405EX_SDR0_SRST) & ~PPC405EX_CE_RESET);
  1037. } else if (of_find_compatible_node(NULL, NULL,
  1038. "amcc,ppc460sx-crypto")) {
  1039. mtdcri(SDR0, PPC460SX_SDR0_SRST,
  1040. mfdcri(SDR0, PPC460SX_SDR0_SRST) | PPC460SX_CE_RESET);
  1041. mtdcri(SDR0, PPC460SX_SDR0_SRST,
  1042. mfdcri(SDR0, PPC460SX_SDR0_SRST) & ~PPC460SX_CE_RESET);
  1043. } else {
  1044. printk(KERN_ERR "Crypto Function Not supported!\n");
  1045. return -EINVAL;
  1046. }
  1047. core_dev = kzalloc(sizeof(struct crypto4xx_core_device), GFP_KERNEL);
  1048. if (!core_dev)
  1049. return -ENOMEM;
  1050. dev_set_drvdata(dev, core_dev);
  1051. core_dev->ofdev = ofdev;
  1052. core_dev->dev = kzalloc(sizeof(struct crypto4xx_device), GFP_KERNEL);
  1053. if (!core_dev->dev)
  1054. goto err_alloc_dev;
  1055. core_dev->dev->core_dev = core_dev;
  1056. core_dev->device = dev;
  1057. spin_lock_init(&core_dev->lock);
  1058. INIT_LIST_HEAD(&core_dev->dev->alg_list);
  1059. rc = crypto4xx_build_pdr(core_dev->dev);
  1060. if (rc)
  1061. goto err_build_pdr;
  1062. rc = crypto4xx_build_gdr(core_dev->dev);
  1063. if (rc)
  1064. goto err_build_gdr;
  1065. rc = crypto4xx_build_sdr(core_dev->dev);
  1066. if (rc)
  1067. goto err_build_sdr;
  1068. /* Init tasklet for bottom half processing */
  1069. tasklet_init(&core_dev->tasklet, crypto4xx_bh_tasklet_cb,
  1070. (unsigned long) dev);
  1071. /* Register for Crypto isr, Crypto Engine IRQ */
  1072. core_dev->irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
  1073. rc = request_irq(core_dev->irq, crypto4xx_ce_interrupt_handler, 0,
  1074. core_dev->dev->name, dev);
  1075. if (rc)
  1076. goto err_request_irq;
  1077. core_dev->dev->ce_base = of_iomap(ofdev->dev.of_node, 0);
  1078. if (!core_dev->dev->ce_base) {
  1079. dev_err(dev, "failed to of_iomap\n");
  1080. rc = -ENOMEM;
  1081. goto err_iomap;
  1082. }
  1083. /* need to setup pdr, rdr, gdr and sdr before this */
  1084. crypto4xx_hw_init(core_dev->dev);
  1085. /* Register security algorithms with Linux CryptoAPI */
  1086. rc = crypto4xx_register_alg(core_dev->dev, crypto4xx_alg,
  1087. ARRAY_SIZE(crypto4xx_alg));
  1088. if (rc)
  1089. goto err_start_dev;
  1090. ppc4xx_trng_probe(core_dev);
  1091. return 0;
  1092. err_start_dev:
  1093. iounmap(core_dev->dev->ce_base);
  1094. err_iomap:
  1095. free_irq(core_dev->irq, dev);
  1096. err_request_irq:
  1097. irq_dispose_mapping(core_dev->irq);
  1098. tasklet_kill(&core_dev->tasklet);
  1099. crypto4xx_destroy_sdr(core_dev->dev);
  1100. err_build_sdr:
  1101. crypto4xx_destroy_gdr(core_dev->dev);
  1102. err_build_gdr:
  1103. crypto4xx_destroy_pdr(core_dev->dev);
  1104. err_build_pdr:
  1105. kfree(core_dev->dev);
  1106. err_alloc_dev:
  1107. kfree(core_dev);
  1108. return rc;
  1109. }
  1110. static int crypto4xx_remove(struct platform_device *ofdev)
  1111. {
  1112. struct device *dev = &ofdev->dev;
  1113. struct crypto4xx_core_device *core_dev = dev_get_drvdata(dev);
  1114. ppc4xx_trng_remove(core_dev);
  1115. free_irq(core_dev->irq, dev);
  1116. irq_dispose_mapping(core_dev->irq);
  1117. tasklet_kill(&core_dev->tasklet);
  1118. /* Un-register with Linux CryptoAPI */
  1119. crypto4xx_unregister_alg(core_dev->dev);
  1120. /* Free all allocated memory */
  1121. crypto4xx_stop_all(core_dev);
  1122. return 0;
  1123. }
  1124. static const struct of_device_id crypto4xx_match[] = {
  1125. { .compatible = "amcc,ppc4xx-crypto",},
  1126. { },
  1127. };
  1128. MODULE_DEVICE_TABLE(of, crypto4xx_match);
  1129. static struct platform_driver crypto4xx_driver = {
  1130. .driver = {
  1131. .name = MODULE_NAME,
  1132. .of_match_table = crypto4xx_match,
  1133. },
  1134. .probe = crypto4xx_probe,
  1135. .remove = crypto4xx_remove,
  1136. };
  1137. module_platform_driver(crypto4xx_driver);
  1138. MODULE_LICENSE("GPL");
  1139. MODULE_AUTHOR("James Hsiao <jhsiao@amcc.com>");
  1140. MODULE_DESCRIPTION("Driver for AMCC PPC4xx crypto accelerator");