crypto4xx_core.c 34 KB

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