crypto4xx_core.c 33 KB

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