crypto4xx_core.c 34 KB

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