crypto4xx_core.c 34 KB

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