crypto4xx_core.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  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/aead.h>
  38. #include <crypto/aes.h>
  39. #include <crypto/ctr.h>
  40. #include <crypto/gcm.h>
  41. #include <crypto/sha.h>
  42. #include <crypto/scatterwalk.h>
  43. #include <crypto/skcipher.h>
  44. #include <crypto/internal/aead.h>
  45. #include <crypto/internal/skcipher.h>
  46. #include "crypto4xx_reg_def.h"
  47. #include "crypto4xx_core.h"
  48. #include "crypto4xx_sa.h"
  49. #include "crypto4xx_trng.h"
  50. #define PPC4XX_SEC_VERSION_STR "0.5"
  51. /**
  52. * PPC4xx Crypto Engine Initialization Routine
  53. */
  54. static void crypto4xx_hw_init(struct crypto4xx_device *dev)
  55. {
  56. union ce_ring_size ring_size;
  57. union ce_ring_control ring_ctrl;
  58. union ce_part_ring_size part_ring_size;
  59. union ce_io_threshold io_threshold;
  60. u32 rand_num;
  61. union ce_pe_dma_cfg pe_dma_cfg;
  62. u32 device_ctrl;
  63. writel(PPC4XX_BYTE_ORDER, dev->ce_base + CRYPTO4XX_BYTE_ORDER_CFG);
  64. /* setup pe dma, include reset sg, pdr and pe, then release reset */
  65. pe_dma_cfg.w = 0;
  66. pe_dma_cfg.bf.bo_sgpd_en = 1;
  67. pe_dma_cfg.bf.bo_data_en = 0;
  68. pe_dma_cfg.bf.bo_sa_en = 1;
  69. pe_dma_cfg.bf.bo_pd_en = 1;
  70. pe_dma_cfg.bf.dynamic_sa_en = 1;
  71. pe_dma_cfg.bf.reset_sg = 1;
  72. pe_dma_cfg.bf.reset_pdr = 1;
  73. pe_dma_cfg.bf.reset_pe = 1;
  74. writel(pe_dma_cfg.w, dev->ce_base + CRYPTO4XX_PE_DMA_CFG);
  75. /* un reset pe,sg and pdr */
  76. pe_dma_cfg.bf.pe_mode = 0;
  77. pe_dma_cfg.bf.reset_sg = 0;
  78. pe_dma_cfg.bf.reset_pdr = 0;
  79. pe_dma_cfg.bf.reset_pe = 0;
  80. pe_dma_cfg.bf.bo_td_en = 0;
  81. writel(pe_dma_cfg.w, dev->ce_base + CRYPTO4XX_PE_DMA_CFG);
  82. writel(dev->pdr_pa, dev->ce_base + CRYPTO4XX_PDR_BASE);
  83. writel(dev->pdr_pa, dev->ce_base + CRYPTO4XX_RDR_BASE);
  84. writel(PPC4XX_PRNG_CTRL_AUTO_EN, dev->ce_base + CRYPTO4XX_PRNG_CTRL);
  85. get_random_bytes(&rand_num, sizeof(rand_num));
  86. writel(rand_num, dev->ce_base + CRYPTO4XX_PRNG_SEED_L);
  87. get_random_bytes(&rand_num, sizeof(rand_num));
  88. writel(rand_num, dev->ce_base + CRYPTO4XX_PRNG_SEED_H);
  89. ring_size.w = 0;
  90. ring_size.bf.ring_offset = PPC4XX_PD_SIZE;
  91. ring_size.bf.ring_size = PPC4XX_NUM_PD;
  92. writel(ring_size.w, dev->ce_base + CRYPTO4XX_RING_SIZE);
  93. ring_ctrl.w = 0;
  94. writel(ring_ctrl.w, dev->ce_base + CRYPTO4XX_RING_CTRL);
  95. device_ctrl = readl(dev->ce_base + CRYPTO4XX_DEVICE_CTRL);
  96. device_ctrl |= PPC4XX_DC_3DES_EN;
  97. writel(device_ctrl, dev->ce_base + CRYPTO4XX_DEVICE_CTRL);
  98. writel(dev->gdr_pa, dev->ce_base + CRYPTO4XX_GATH_RING_BASE);
  99. writel(dev->sdr_pa, dev->ce_base + CRYPTO4XX_SCAT_RING_BASE);
  100. part_ring_size.w = 0;
  101. part_ring_size.bf.sdr_size = PPC4XX_SDR_SIZE;
  102. part_ring_size.bf.gdr_size = PPC4XX_GDR_SIZE;
  103. writel(part_ring_size.w, dev->ce_base + CRYPTO4XX_PART_RING_SIZE);
  104. writel(PPC4XX_SD_BUFFER_SIZE, dev->ce_base + CRYPTO4XX_PART_RING_CFG);
  105. io_threshold.w = 0;
  106. io_threshold.bf.output_threshold = PPC4XX_OUTPUT_THRESHOLD;
  107. io_threshold.bf.input_threshold = PPC4XX_INPUT_THRESHOLD;
  108. writel(io_threshold.w, dev->ce_base + CRYPTO4XX_IO_THRESHOLD);
  109. writel(0, dev->ce_base + CRYPTO4XX_PDR_BASE_UADDR);
  110. writel(0, dev->ce_base + CRYPTO4XX_RDR_BASE_UADDR);
  111. writel(0, dev->ce_base + CRYPTO4XX_PKT_SRC_UADDR);
  112. writel(0, dev->ce_base + CRYPTO4XX_PKT_DEST_UADDR);
  113. writel(0, dev->ce_base + CRYPTO4XX_SA_UADDR);
  114. writel(0, dev->ce_base + CRYPTO4XX_GATH_RING_BASE_UADDR);
  115. writel(0, dev->ce_base + CRYPTO4XX_SCAT_RING_BASE_UADDR);
  116. /* un reset pe,sg and pdr */
  117. pe_dma_cfg.bf.pe_mode = 1;
  118. pe_dma_cfg.bf.reset_sg = 0;
  119. pe_dma_cfg.bf.reset_pdr = 0;
  120. pe_dma_cfg.bf.reset_pe = 0;
  121. pe_dma_cfg.bf.bo_td_en = 0;
  122. writel(pe_dma_cfg.w, dev->ce_base + CRYPTO4XX_PE_DMA_CFG);
  123. /*clear all pending interrupt*/
  124. writel(PPC4XX_INTERRUPT_CLR, dev->ce_base + CRYPTO4XX_INT_CLR);
  125. writel(PPC4XX_INT_DESCR_CNT, dev->ce_base + CRYPTO4XX_INT_DESCR_CNT);
  126. writel(PPC4XX_INT_DESCR_CNT, dev->ce_base + CRYPTO4XX_INT_DESCR_CNT);
  127. writel(PPC4XX_INT_CFG, dev->ce_base + CRYPTO4XX_INT_CFG);
  128. if (dev->is_revb) {
  129. writel(PPC4XX_INT_TIMEOUT_CNT_REVB << 10,
  130. dev->ce_base + CRYPTO4XX_INT_TIMEOUT_CNT);
  131. writel(PPC4XX_PD_DONE_INT | PPC4XX_TMO_ERR_INT,
  132. dev->ce_base + CRYPTO4XX_INT_EN);
  133. } else {
  134. writel(PPC4XX_PD_DONE_INT, dev->ce_base + CRYPTO4XX_INT_EN);
  135. }
  136. }
  137. int crypto4xx_alloc_sa(struct crypto4xx_ctx *ctx, u32 size)
  138. {
  139. ctx->sa_in = kcalloc(size, 4, GFP_ATOMIC);
  140. if (ctx->sa_in == NULL)
  141. return -ENOMEM;
  142. ctx->sa_out = kcalloc(size, 4, GFP_ATOMIC);
  143. if (ctx->sa_out == NULL) {
  144. kfree(ctx->sa_in);
  145. ctx->sa_in = NULL;
  146. return -ENOMEM;
  147. }
  148. ctx->sa_len = size;
  149. return 0;
  150. }
  151. void crypto4xx_free_sa(struct crypto4xx_ctx *ctx)
  152. {
  153. kfree(ctx->sa_in);
  154. ctx->sa_in = NULL;
  155. kfree(ctx->sa_out);
  156. ctx->sa_out = NULL;
  157. ctx->sa_len = 0;
  158. }
  159. /**
  160. * alloc memory for the gather ring
  161. * no need to alloc buf for the ring
  162. * gdr_tail, gdr_head and gdr_count are initialized by this function
  163. */
  164. static u32 crypto4xx_build_pdr(struct crypto4xx_device *dev)
  165. {
  166. int i;
  167. dev->pdr = dma_alloc_coherent(dev->core_dev->device,
  168. sizeof(struct ce_pd) * PPC4XX_NUM_PD,
  169. &dev->pdr_pa, GFP_ATOMIC);
  170. if (!dev->pdr)
  171. return -ENOMEM;
  172. dev->pdr_uinfo = kcalloc(PPC4XX_NUM_PD, sizeof(struct pd_uinfo),
  173. GFP_KERNEL);
  174. if (!dev->pdr_uinfo) {
  175. dma_free_coherent(dev->core_dev->device,
  176. sizeof(struct ce_pd) * PPC4XX_NUM_PD,
  177. dev->pdr,
  178. dev->pdr_pa);
  179. return -ENOMEM;
  180. }
  181. memset(dev->pdr, 0, sizeof(struct ce_pd) * PPC4XX_NUM_PD);
  182. dev->shadow_sa_pool = dma_alloc_coherent(dev->core_dev->device,
  183. sizeof(union shadow_sa_buf) * PPC4XX_NUM_PD,
  184. &dev->shadow_sa_pool_pa,
  185. GFP_ATOMIC);
  186. if (!dev->shadow_sa_pool)
  187. return -ENOMEM;
  188. dev->shadow_sr_pool = dma_alloc_coherent(dev->core_dev->device,
  189. sizeof(struct sa_state_record) * PPC4XX_NUM_PD,
  190. &dev->shadow_sr_pool_pa, GFP_ATOMIC);
  191. if (!dev->shadow_sr_pool)
  192. return -ENOMEM;
  193. for (i = 0; i < PPC4XX_NUM_PD; i++) {
  194. struct ce_pd *pd = &dev->pdr[i];
  195. struct pd_uinfo *pd_uinfo = &dev->pdr_uinfo[i];
  196. pd->sa = dev->shadow_sa_pool_pa +
  197. sizeof(union shadow_sa_buf) * i;
  198. /* alloc 256 bytes which is enough for any kind of dynamic sa */
  199. pd_uinfo->sa_va = &dev->shadow_sa_pool[i].sa;
  200. /* alloc state record */
  201. pd_uinfo->sr_va = &dev->shadow_sr_pool[i];
  202. pd_uinfo->sr_pa = dev->shadow_sr_pool_pa +
  203. sizeof(struct sa_state_record) * i;
  204. }
  205. return 0;
  206. }
  207. static void crypto4xx_destroy_pdr(struct crypto4xx_device *dev)
  208. {
  209. if (dev->pdr)
  210. dma_free_coherent(dev->core_dev->device,
  211. sizeof(struct ce_pd) * PPC4XX_NUM_PD,
  212. dev->pdr, dev->pdr_pa);
  213. if (dev->shadow_sa_pool)
  214. dma_free_coherent(dev->core_dev->device,
  215. sizeof(union shadow_sa_buf) * PPC4XX_NUM_PD,
  216. dev->shadow_sa_pool, dev->shadow_sa_pool_pa);
  217. if (dev->shadow_sr_pool)
  218. dma_free_coherent(dev->core_dev->device,
  219. sizeof(struct sa_state_record) * PPC4XX_NUM_PD,
  220. dev->shadow_sr_pool, dev->shadow_sr_pool_pa);
  221. kfree(dev->pdr_uinfo);
  222. }
  223. static u32 crypto4xx_get_pd_from_pdr_nolock(struct crypto4xx_device *dev)
  224. {
  225. u32 retval;
  226. u32 tmp;
  227. retval = dev->pdr_head;
  228. tmp = (dev->pdr_head + 1) % PPC4XX_NUM_PD;
  229. if (tmp == dev->pdr_tail)
  230. return ERING_WAS_FULL;
  231. dev->pdr_head = tmp;
  232. return retval;
  233. }
  234. static u32 crypto4xx_put_pd_to_pdr(struct crypto4xx_device *dev, u32 idx)
  235. {
  236. struct pd_uinfo *pd_uinfo = &dev->pdr_uinfo[idx];
  237. u32 tail;
  238. unsigned long flags;
  239. spin_lock_irqsave(&dev->core_dev->lock, flags);
  240. pd_uinfo->state = PD_ENTRY_FREE;
  241. if (dev->pdr_tail != PPC4XX_LAST_PD)
  242. dev->pdr_tail++;
  243. else
  244. dev->pdr_tail = 0;
  245. tail = dev->pdr_tail;
  246. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  247. return tail;
  248. }
  249. /**
  250. * alloc memory for the gather ring
  251. * no need to alloc buf for the ring
  252. * gdr_tail, gdr_head and gdr_count are initialized by this function
  253. */
  254. static u32 crypto4xx_build_gdr(struct crypto4xx_device *dev)
  255. {
  256. dev->gdr = dma_zalloc_coherent(dev->core_dev->device,
  257. sizeof(struct ce_gd) * PPC4XX_NUM_GD,
  258. &dev->gdr_pa, GFP_ATOMIC);
  259. if (!dev->gdr)
  260. return -ENOMEM;
  261. return 0;
  262. }
  263. static inline void crypto4xx_destroy_gdr(struct crypto4xx_device *dev)
  264. {
  265. dma_free_coherent(dev->core_dev->device,
  266. sizeof(struct ce_gd) * PPC4XX_NUM_GD,
  267. dev->gdr, dev->gdr_pa);
  268. }
  269. /*
  270. * when this function is called.
  271. * preemption or interrupt must be disabled
  272. */
  273. static u32 crypto4xx_get_n_gd(struct crypto4xx_device *dev, int n)
  274. {
  275. u32 retval;
  276. u32 tmp;
  277. if (n >= PPC4XX_NUM_GD)
  278. return ERING_WAS_FULL;
  279. retval = dev->gdr_head;
  280. tmp = (dev->gdr_head + n) % PPC4XX_NUM_GD;
  281. if (dev->gdr_head > dev->gdr_tail) {
  282. if (tmp < dev->gdr_head && tmp >= dev->gdr_tail)
  283. return ERING_WAS_FULL;
  284. } else if (dev->gdr_head < dev->gdr_tail) {
  285. if (tmp < dev->gdr_head || tmp >= dev->gdr_tail)
  286. return ERING_WAS_FULL;
  287. }
  288. dev->gdr_head = tmp;
  289. return retval;
  290. }
  291. static u32 crypto4xx_put_gd_to_gdr(struct crypto4xx_device *dev)
  292. {
  293. unsigned long flags;
  294. spin_lock_irqsave(&dev->core_dev->lock, flags);
  295. if (dev->gdr_tail == dev->gdr_head) {
  296. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  297. return 0;
  298. }
  299. if (dev->gdr_tail != PPC4XX_LAST_GD)
  300. dev->gdr_tail++;
  301. else
  302. dev->gdr_tail = 0;
  303. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  304. return 0;
  305. }
  306. static inline struct ce_gd *crypto4xx_get_gdp(struct crypto4xx_device *dev,
  307. dma_addr_t *gd_dma, u32 idx)
  308. {
  309. *gd_dma = dev->gdr_pa + sizeof(struct ce_gd) * idx;
  310. return &dev->gdr[idx];
  311. }
  312. /**
  313. * alloc memory for the scatter ring
  314. * need to alloc buf for the ring
  315. * sdr_tail, sdr_head and sdr_count are initialized by this function
  316. */
  317. static u32 crypto4xx_build_sdr(struct crypto4xx_device *dev)
  318. {
  319. int i;
  320. /* alloc memory for scatter descriptor ring */
  321. dev->sdr = dma_alloc_coherent(dev->core_dev->device,
  322. sizeof(struct ce_sd) * PPC4XX_NUM_SD,
  323. &dev->sdr_pa, GFP_ATOMIC);
  324. if (!dev->sdr)
  325. return -ENOMEM;
  326. dev->scatter_buffer_va =
  327. dma_alloc_coherent(dev->core_dev->device,
  328. PPC4XX_SD_BUFFER_SIZE * PPC4XX_NUM_SD,
  329. &dev->scatter_buffer_pa, GFP_ATOMIC);
  330. if (!dev->scatter_buffer_va) {
  331. dma_free_coherent(dev->core_dev->device,
  332. sizeof(struct ce_sd) * PPC4XX_NUM_SD,
  333. dev->sdr, dev->sdr_pa);
  334. return -ENOMEM;
  335. }
  336. for (i = 0; i < PPC4XX_NUM_SD; i++) {
  337. dev->sdr[i].ptr = dev->scatter_buffer_pa +
  338. PPC4XX_SD_BUFFER_SIZE * i;
  339. }
  340. return 0;
  341. }
  342. static void crypto4xx_destroy_sdr(struct crypto4xx_device *dev)
  343. {
  344. if (dev->sdr)
  345. dma_free_coherent(dev->core_dev->device,
  346. sizeof(struct ce_sd) * PPC4XX_NUM_SD,
  347. dev->sdr, dev->sdr_pa);
  348. if (dev->scatter_buffer_va)
  349. dma_free_coherent(dev->core_dev->device,
  350. PPC4XX_SD_BUFFER_SIZE * PPC4XX_NUM_SD,
  351. dev->scatter_buffer_va,
  352. dev->scatter_buffer_pa);
  353. }
  354. /*
  355. * when this function is called.
  356. * preemption or interrupt must be disabled
  357. */
  358. static u32 crypto4xx_get_n_sd(struct crypto4xx_device *dev, int n)
  359. {
  360. u32 retval;
  361. u32 tmp;
  362. if (n >= PPC4XX_NUM_SD)
  363. return ERING_WAS_FULL;
  364. retval = dev->sdr_head;
  365. tmp = (dev->sdr_head + n) % PPC4XX_NUM_SD;
  366. if (dev->sdr_head > dev->gdr_tail) {
  367. if (tmp < dev->sdr_head && tmp >= dev->sdr_tail)
  368. return ERING_WAS_FULL;
  369. } else if (dev->sdr_head < dev->sdr_tail) {
  370. if (tmp < dev->sdr_head || tmp >= dev->sdr_tail)
  371. return ERING_WAS_FULL;
  372. } /* the head = tail, or empty case is already take cared */
  373. dev->sdr_head = tmp;
  374. return retval;
  375. }
  376. static u32 crypto4xx_put_sd_to_sdr(struct crypto4xx_device *dev)
  377. {
  378. unsigned long flags;
  379. spin_lock_irqsave(&dev->core_dev->lock, flags);
  380. if (dev->sdr_tail == dev->sdr_head) {
  381. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  382. return 0;
  383. }
  384. if (dev->sdr_tail != PPC4XX_LAST_SD)
  385. dev->sdr_tail++;
  386. else
  387. dev->sdr_tail = 0;
  388. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  389. return 0;
  390. }
  391. static inline struct ce_sd *crypto4xx_get_sdp(struct crypto4xx_device *dev,
  392. dma_addr_t *sd_dma, u32 idx)
  393. {
  394. *sd_dma = dev->sdr_pa + sizeof(struct ce_sd) * idx;
  395. return &dev->sdr[idx];
  396. }
  397. static void crypto4xx_copy_pkt_to_dst(struct crypto4xx_device *dev,
  398. struct ce_pd *pd,
  399. struct pd_uinfo *pd_uinfo,
  400. u32 nbytes,
  401. struct scatterlist *dst)
  402. {
  403. unsigned int first_sd = pd_uinfo->first_sd;
  404. unsigned int last_sd;
  405. unsigned int overflow = 0;
  406. unsigned int to_copy;
  407. unsigned int dst_start = 0;
  408. /*
  409. * Because the scatter buffers are all neatly organized in one
  410. * big continuous ringbuffer; scatterwalk_map_and_copy() can
  411. * be instructed to copy a range of buffers in one go.
  412. */
  413. last_sd = (first_sd + pd_uinfo->num_sd);
  414. if (last_sd > PPC4XX_LAST_SD) {
  415. last_sd = PPC4XX_LAST_SD;
  416. overflow = last_sd % PPC4XX_NUM_SD;
  417. }
  418. while (nbytes) {
  419. void *buf = dev->scatter_buffer_va +
  420. first_sd * PPC4XX_SD_BUFFER_SIZE;
  421. to_copy = min(nbytes, PPC4XX_SD_BUFFER_SIZE *
  422. (1 + last_sd - first_sd));
  423. scatterwalk_map_and_copy(buf, dst, dst_start, to_copy, 1);
  424. nbytes -= to_copy;
  425. if (overflow) {
  426. first_sd = 0;
  427. last_sd = overflow;
  428. dst_start += to_copy;
  429. overflow = 0;
  430. }
  431. }
  432. }
  433. static void crypto4xx_copy_digest_to_dst(void *dst,
  434. struct pd_uinfo *pd_uinfo,
  435. struct crypto4xx_ctx *ctx)
  436. {
  437. struct dynamic_sa_ctl *sa = (struct dynamic_sa_ctl *) ctx->sa_in;
  438. if (sa->sa_command_0.bf.hash_alg == SA_HASH_ALG_SHA1) {
  439. memcpy(dst, pd_uinfo->sr_va->save_digest,
  440. SA_HASH_ALG_SHA1_DIGEST_SIZE);
  441. }
  442. }
  443. static void crypto4xx_ret_sg_desc(struct crypto4xx_device *dev,
  444. struct pd_uinfo *pd_uinfo)
  445. {
  446. int i;
  447. if (pd_uinfo->num_gd) {
  448. for (i = 0; i < pd_uinfo->num_gd; i++)
  449. crypto4xx_put_gd_to_gdr(dev);
  450. pd_uinfo->first_gd = 0xffffffff;
  451. pd_uinfo->num_gd = 0;
  452. }
  453. if (pd_uinfo->num_sd) {
  454. for (i = 0; i < pd_uinfo->num_sd; i++)
  455. crypto4xx_put_sd_to_sdr(dev);
  456. pd_uinfo->first_sd = 0xffffffff;
  457. pd_uinfo->num_sd = 0;
  458. }
  459. }
  460. static void crypto4xx_cipher_done(struct crypto4xx_device *dev,
  461. struct pd_uinfo *pd_uinfo,
  462. struct ce_pd *pd)
  463. {
  464. struct skcipher_request *req;
  465. struct scatterlist *dst;
  466. dma_addr_t addr;
  467. req = skcipher_request_cast(pd_uinfo->async_req);
  468. if (pd_uinfo->using_sd) {
  469. crypto4xx_copy_pkt_to_dst(dev, pd, pd_uinfo,
  470. req->cryptlen, req->dst);
  471. } else {
  472. dst = pd_uinfo->dest_va;
  473. addr = dma_map_page(dev->core_dev->device, sg_page(dst),
  474. dst->offset, dst->length, DMA_FROM_DEVICE);
  475. }
  476. if (pd_uinfo->sa_va->sa_command_0.bf.save_iv == SA_SAVE_IV) {
  477. struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  478. crypto4xx_memcpy_from_le32((u32 *)req->iv,
  479. pd_uinfo->sr_va->save_iv,
  480. crypto_skcipher_ivsize(skcipher));
  481. }
  482. crypto4xx_ret_sg_desc(dev, pd_uinfo);
  483. if (pd_uinfo->state & PD_ENTRY_BUSY)
  484. skcipher_request_complete(req, -EINPROGRESS);
  485. skcipher_request_complete(req, 0);
  486. }
  487. static void crypto4xx_ahash_done(struct crypto4xx_device *dev,
  488. struct pd_uinfo *pd_uinfo)
  489. {
  490. struct crypto4xx_ctx *ctx;
  491. struct ahash_request *ahash_req;
  492. ahash_req = ahash_request_cast(pd_uinfo->async_req);
  493. ctx = crypto_tfm_ctx(ahash_req->base.tfm);
  494. crypto4xx_copy_digest_to_dst(ahash_req->result, pd_uinfo,
  495. crypto_tfm_ctx(ahash_req->base.tfm));
  496. crypto4xx_ret_sg_desc(dev, pd_uinfo);
  497. if (pd_uinfo->state & PD_ENTRY_BUSY)
  498. ahash_request_complete(ahash_req, -EINPROGRESS);
  499. ahash_request_complete(ahash_req, 0);
  500. }
  501. static void crypto4xx_aead_done(struct crypto4xx_device *dev,
  502. struct pd_uinfo *pd_uinfo,
  503. struct ce_pd *pd)
  504. {
  505. struct aead_request *aead_req = container_of(pd_uinfo->async_req,
  506. struct aead_request, base);
  507. struct scatterlist *dst = pd_uinfo->dest_va;
  508. size_t cp_len = crypto_aead_authsize(
  509. crypto_aead_reqtfm(aead_req));
  510. u32 icv[AES_BLOCK_SIZE];
  511. int err = 0;
  512. if (pd_uinfo->using_sd) {
  513. crypto4xx_copy_pkt_to_dst(dev, pd, pd_uinfo,
  514. pd->pd_ctl_len.bf.pkt_len,
  515. dst);
  516. } else {
  517. __dma_sync_page(sg_page(dst), dst->offset, dst->length,
  518. DMA_FROM_DEVICE);
  519. }
  520. if (pd_uinfo->sa_va->sa_command_0.bf.dir == DIR_OUTBOUND) {
  521. /* append icv at the end */
  522. crypto4xx_memcpy_from_le32(icv, pd_uinfo->sr_va->save_digest,
  523. sizeof(icv));
  524. scatterwalk_map_and_copy(icv, dst, aead_req->cryptlen,
  525. cp_len, 1);
  526. } else {
  527. /* check icv at the end */
  528. scatterwalk_map_and_copy(icv, aead_req->src,
  529. aead_req->assoclen + aead_req->cryptlen -
  530. cp_len, cp_len, 0);
  531. crypto4xx_memcpy_from_le32(icv, icv, sizeof(icv));
  532. if (crypto_memneq(icv, pd_uinfo->sr_va->save_digest, cp_len))
  533. err = -EBADMSG;
  534. }
  535. crypto4xx_ret_sg_desc(dev, pd_uinfo);
  536. if (pd->pd_ctl.bf.status & 0xff) {
  537. if (!__ratelimit(&dev->aead_ratelimit)) {
  538. if (pd->pd_ctl.bf.status & 2)
  539. pr_err("pad fail error\n");
  540. if (pd->pd_ctl.bf.status & 4)
  541. pr_err("seqnum fail\n");
  542. if (pd->pd_ctl.bf.status & 8)
  543. pr_err("error _notify\n");
  544. pr_err("aead return err status = 0x%02x\n",
  545. pd->pd_ctl.bf.status & 0xff);
  546. pr_err("pd pad_ctl = 0x%08x\n",
  547. pd->pd_ctl.bf.pd_pad_ctl);
  548. }
  549. err = -EINVAL;
  550. }
  551. if (pd_uinfo->state & PD_ENTRY_BUSY)
  552. aead_request_complete(aead_req, -EINPROGRESS);
  553. aead_request_complete(aead_req, err);
  554. }
  555. static void crypto4xx_pd_done(struct crypto4xx_device *dev, u32 idx)
  556. {
  557. struct ce_pd *pd = &dev->pdr[idx];
  558. struct pd_uinfo *pd_uinfo = &dev->pdr_uinfo[idx];
  559. switch (crypto_tfm_alg_type(pd_uinfo->async_req->tfm)) {
  560. case CRYPTO_ALG_TYPE_SKCIPHER:
  561. crypto4xx_cipher_done(dev, pd_uinfo, pd);
  562. break;
  563. case CRYPTO_ALG_TYPE_AEAD:
  564. crypto4xx_aead_done(dev, pd_uinfo, pd);
  565. break;
  566. case CRYPTO_ALG_TYPE_AHASH:
  567. crypto4xx_ahash_done(dev, pd_uinfo);
  568. break;
  569. }
  570. }
  571. static void crypto4xx_stop_all(struct crypto4xx_core_device *core_dev)
  572. {
  573. crypto4xx_destroy_pdr(core_dev->dev);
  574. crypto4xx_destroy_gdr(core_dev->dev);
  575. crypto4xx_destroy_sdr(core_dev->dev);
  576. iounmap(core_dev->dev->ce_base);
  577. kfree(core_dev->dev);
  578. kfree(core_dev);
  579. }
  580. static u32 get_next_gd(u32 current)
  581. {
  582. if (current != PPC4XX_LAST_GD)
  583. return current + 1;
  584. else
  585. return 0;
  586. }
  587. static u32 get_next_sd(u32 current)
  588. {
  589. if (current != PPC4XX_LAST_SD)
  590. return current + 1;
  591. else
  592. return 0;
  593. }
  594. int crypto4xx_build_pd(struct crypto_async_request *req,
  595. struct crypto4xx_ctx *ctx,
  596. struct scatterlist *src,
  597. struct scatterlist *dst,
  598. const unsigned int datalen,
  599. const __le32 *iv, const u32 iv_len,
  600. const struct dynamic_sa_ctl *req_sa,
  601. const unsigned int sa_len,
  602. const unsigned int assoclen,
  603. struct scatterlist *_dst)
  604. {
  605. struct crypto4xx_device *dev = ctx->dev;
  606. struct dynamic_sa_ctl *sa;
  607. struct ce_gd *gd;
  608. struct ce_pd *pd;
  609. u32 num_gd, num_sd;
  610. u32 fst_gd = 0xffffffff;
  611. u32 fst_sd = 0xffffffff;
  612. u32 pd_entry;
  613. unsigned long flags;
  614. struct pd_uinfo *pd_uinfo;
  615. unsigned int nbytes = datalen;
  616. size_t offset_to_sr_ptr;
  617. u32 gd_idx = 0;
  618. int tmp;
  619. bool is_busy;
  620. /* figure how many gd are needed */
  621. tmp = sg_nents_for_len(src, assoclen + datalen);
  622. if (tmp < 0) {
  623. dev_err(dev->core_dev->device, "Invalid number of src SG.\n");
  624. return tmp;
  625. }
  626. if (tmp == 1)
  627. tmp = 0;
  628. num_gd = tmp;
  629. if (assoclen) {
  630. nbytes += assoclen;
  631. dst = scatterwalk_ffwd(_dst, dst, assoclen);
  632. }
  633. /* figure how many sd are needed */
  634. if (sg_is_last(dst)) {
  635. num_sd = 0;
  636. } else {
  637. if (datalen > PPC4XX_SD_BUFFER_SIZE) {
  638. num_sd = datalen / PPC4XX_SD_BUFFER_SIZE;
  639. if (datalen % PPC4XX_SD_BUFFER_SIZE)
  640. num_sd++;
  641. } else {
  642. num_sd = 1;
  643. }
  644. }
  645. /*
  646. * The follow section of code needs to be protected
  647. * The gather ring and scatter ring needs to be consecutive
  648. * In case of run out of any kind of descriptor, the descriptor
  649. * already got must be return the original place.
  650. */
  651. spin_lock_irqsave(&dev->core_dev->lock, flags);
  652. /*
  653. * Let the caller know to slow down, once more than 13/16ths = 81%
  654. * of the available data contexts are being used simultaneously.
  655. *
  656. * With PPC4XX_NUM_PD = 256, this will leave a "backlog queue" for
  657. * 31 more contexts. Before new requests have to be rejected.
  658. */
  659. if (req->flags & CRYPTO_TFM_REQ_MAY_BACKLOG) {
  660. is_busy = ((dev->pdr_head - dev->pdr_tail) % PPC4XX_NUM_PD) >=
  661. ((PPC4XX_NUM_PD * 13) / 16);
  662. } else {
  663. /*
  664. * To fix contention issues between ipsec (no blacklog) and
  665. * dm-crypto (backlog) reserve 32 entries for "no backlog"
  666. * data contexts.
  667. */
  668. is_busy = ((dev->pdr_head - dev->pdr_tail) % PPC4XX_NUM_PD) >=
  669. ((PPC4XX_NUM_PD * 15) / 16);
  670. if (is_busy) {
  671. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  672. return -EBUSY;
  673. }
  674. }
  675. if (num_gd) {
  676. fst_gd = crypto4xx_get_n_gd(dev, num_gd);
  677. if (fst_gd == ERING_WAS_FULL) {
  678. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  679. return -EAGAIN;
  680. }
  681. }
  682. if (num_sd) {
  683. fst_sd = crypto4xx_get_n_sd(dev, num_sd);
  684. if (fst_sd == ERING_WAS_FULL) {
  685. if (num_gd)
  686. dev->gdr_head = fst_gd;
  687. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  688. return -EAGAIN;
  689. }
  690. }
  691. pd_entry = crypto4xx_get_pd_from_pdr_nolock(dev);
  692. if (pd_entry == ERING_WAS_FULL) {
  693. if (num_gd)
  694. dev->gdr_head = fst_gd;
  695. if (num_sd)
  696. dev->sdr_head = fst_sd;
  697. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  698. return -EAGAIN;
  699. }
  700. spin_unlock_irqrestore(&dev->core_dev->lock, flags);
  701. pd = &dev->pdr[pd_entry];
  702. pd->sa_len = sa_len;
  703. pd_uinfo = &dev->pdr_uinfo[pd_entry];
  704. pd_uinfo->async_req = req;
  705. pd_uinfo->num_gd = num_gd;
  706. pd_uinfo->num_sd = num_sd;
  707. if (iv_len)
  708. memcpy(pd_uinfo->sr_va->save_iv, iv, iv_len);
  709. sa = pd_uinfo->sa_va;
  710. memcpy(sa, req_sa, sa_len * 4);
  711. sa->sa_command_1.bf.hash_crypto_offset = (assoclen >> 2);
  712. offset_to_sr_ptr = get_dynamic_sa_offset_state_ptr_field(sa);
  713. *(u32 *)((unsigned long)sa + offset_to_sr_ptr) = pd_uinfo->sr_pa;
  714. if (num_gd) {
  715. dma_addr_t gd_dma;
  716. struct scatterlist *sg;
  717. /* get first gd we are going to use */
  718. gd_idx = fst_gd;
  719. pd_uinfo->first_gd = fst_gd;
  720. pd_uinfo->num_gd = num_gd;
  721. gd = crypto4xx_get_gdp(dev, &gd_dma, gd_idx);
  722. pd->src = gd_dma;
  723. /* enable gather */
  724. sa->sa_command_0.bf.gather = 1;
  725. /* walk the sg, and setup gather array */
  726. sg = src;
  727. while (nbytes) {
  728. size_t len;
  729. len = min(sg->length, nbytes);
  730. gd->ptr = dma_map_page(dev->core_dev->device,
  731. sg_page(sg), sg->offset, len, DMA_TO_DEVICE);
  732. gd->ctl_len.len = len;
  733. gd->ctl_len.done = 0;
  734. gd->ctl_len.ready = 1;
  735. if (len >= nbytes)
  736. break;
  737. nbytes -= sg->length;
  738. gd_idx = get_next_gd(gd_idx);
  739. gd = crypto4xx_get_gdp(dev, &gd_dma, gd_idx);
  740. sg = sg_next(sg);
  741. }
  742. } else {
  743. pd->src = (u32)dma_map_page(dev->core_dev->device, sg_page(src),
  744. src->offset, min(nbytes, src->length),
  745. DMA_TO_DEVICE);
  746. /*
  747. * Disable gather in sa command
  748. */
  749. sa->sa_command_0.bf.gather = 0;
  750. /*
  751. * Indicate gather array is not used
  752. */
  753. pd_uinfo->first_gd = 0xffffffff;
  754. pd_uinfo->num_gd = 0;
  755. }
  756. if (sg_is_last(dst)) {
  757. /*
  758. * we know application give us dst a whole piece of memory
  759. * no need to use scatter ring.
  760. */
  761. pd_uinfo->using_sd = 0;
  762. pd_uinfo->first_sd = 0xffffffff;
  763. pd_uinfo->num_sd = 0;
  764. pd_uinfo->dest_va = dst;
  765. sa->sa_command_0.bf.scatter = 0;
  766. pd->dest = (u32)dma_map_page(dev->core_dev->device,
  767. sg_page(dst), dst->offset,
  768. min(datalen, dst->length),
  769. DMA_TO_DEVICE);
  770. } else {
  771. dma_addr_t sd_dma;
  772. struct ce_sd *sd = NULL;
  773. u32 sd_idx = fst_sd;
  774. nbytes = datalen;
  775. sa->sa_command_0.bf.scatter = 1;
  776. pd_uinfo->using_sd = 1;
  777. pd_uinfo->dest_va = dst;
  778. pd_uinfo->first_sd = fst_sd;
  779. pd_uinfo->num_sd = num_sd;
  780. sd = crypto4xx_get_sdp(dev, &sd_dma, sd_idx);
  781. pd->dest = sd_dma;
  782. /* setup scatter descriptor */
  783. sd->ctl.done = 0;
  784. sd->ctl.rdy = 1;
  785. /* sd->ptr should be setup by sd_init routine*/
  786. if (nbytes >= PPC4XX_SD_BUFFER_SIZE)
  787. nbytes -= PPC4XX_SD_BUFFER_SIZE;
  788. else
  789. nbytes = 0;
  790. while (nbytes) {
  791. sd_idx = get_next_sd(sd_idx);
  792. sd = crypto4xx_get_sdp(dev, &sd_dma, sd_idx);
  793. /* setup scatter descriptor */
  794. sd->ctl.done = 0;
  795. sd->ctl.rdy = 1;
  796. if (nbytes >= PPC4XX_SD_BUFFER_SIZE) {
  797. nbytes -= PPC4XX_SD_BUFFER_SIZE;
  798. } else {
  799. /*
  800. * SD entry can hold PPC4XX_SD_BUFFER_SIZE,
  801. * which is more than nbytes, so done.
  802. */
  803. nbytes = 0;
  804. }
  805. }
  806. }
  807. pd->pd_ctl.w = PD_CTL_HOST_READY |
  808. ((crypto_tfm_alg_type(req->tfm) == CRYPTO_ALG_TYPE_AHASH) |
  809. (crypto_tfm_alg_type(req->tfm) == CRYPTO_ALG_TYPE_AEAD) ?
  810. PD_CTL_HASH_FINAL : 0);
  811. pd->pd_ctl_len.w = 0x00400000 | (assoclen + datalen);
  812. pd_uinfo->state = PD_ENTRY_INUSE | (is_busy ? PD_ENTRY_BUSY : 0);
  813. wmb();
  814. /* write any value to push engine to read a pd */
  815. writel(0, dev->ce_base + CRYPTO4XX_INT_DESCR_RD);
  816. writel(1, dev->ce_base + CRYPTO4XX_INT_DESCR_RD);
  817. return is_busy ? -EBUSY : -EINPROGRESS;
  818. }
  819. /**
  820. * Algorithm Registration Functions
  821. */
  822. static void crypto4xx_ctx_init(struct crypto4xx_alg *amcc_alg,
  823. struct crypto4xx_ctx *ctx)
  824. {
  825. ctx->dev = amcc_alg->dev;
  826. ctx->sa_in = NULL;
  827. ctx->sa_out = NULL;
  828. ctx->sa_len = 0;
  829. }
  830. static int crypto4xx_sk_init(struct crypto_skcipher *sk)
  831. {
  832. struct skcipher_alg *alg = crypto_skcipher_alg(sk);
  833. struct crypto4xx_alg *amcc_alg;
  834. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(sk);
  835. if (alg->base.cra_flags & CRYPTO_ALG_NEED_FALLBACK) {
  836. ctx->sw_cipher.cipher =
  837. crypto_alloc_skcipher(alg->base.cra_name, 0,
  838. CRYPTO_ALG_NEED_FALLBACK |
  839. CRYPTO_ALG_ASYNC);
  840. if (IS_ERR(ctx->sw_cipher.cipher))
  841. return PTR_ERR(ctx->sw_cipher.cipher);
  842. crypto_skcipher_set_reqsize(sk,
  843. sizeof(struct skcipher_request) + 32 +
  844. crypto_skcipher_reqsize(ctx->sw_cipher.cipher));
  845. }
  846. amcc_alg = container_of(alg, struct crypto4xx_alg, alg.u.cipher);
  847. crypto4xx_ctx_init(amcc_alg, ctx);
  848. return 0;
  849. }
  850. static void crypto4xx_common_exit(struct crypto4xx_ctx *ctx)
  851. {
  852. crypto4xx_free_sa(ctx);
  853. }
  854. static void crypto4xx_sk_exit(struct crypto_skcipher *sk)
  855. {
  856. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(sk);
  857. crypto4xx_common_exit(ctx);
  858. if (ctx->sw_cipher.cipher)
  859. crypto_free_skcipher(ctx->sw_cipher.cipher);
  860. }
  861. static int crypto4xx_aead_init(struct crypto_aead *tfm)
  862. {
  863. struct aead_alg *alg = crypto_aead_alg(tfm);
  864. struct crypto4xx_ctx *ctx = crypto_aead_ctx(tfm);
  865. struct crypto4xx_alg *amcc_alg;
  866. ctx->sw_cipher.aead = crypto_alloc_aead(alg->base.cra_name, 0,
  867. CRYPTO_ALG_NEED_FALLBACK |
  868. CRYPTO_ALG_ASYNC);
  869. if (IS_ERR(ctx->sw_cipher.aead))
  870. return PTR_ERR(ctx->sw_cipher.aead);
  871. amcc_alg = container_of(alg, struct crypto4xx_alg, alg.u.aead);
  872. crypto4xx_ctx_init(amcc_alg, ctx);
  873. crypto_aead_set_reqsize(tfm, max(sizeof(struct aead_request) + 32 +
  874. crypto_aead_reqsize(ctx->sw_cipher.aead),
  875. sizeof(struct crypto4xx_aead_reqctx)));
  876. return 0;
  877. }
  878. static void crypto4xx_aead_exit(struct crypto_aead *tfm)
  879. {
  880. struct crypto4xx_ctx *ctx = crypto_aead_ctx(tfm);
  881. crypto4xx_common_exit(ctx);
  882. crypto_free_aead(ctx->sw_cipher.aead);
  883. }
  884. static int crypto4xx_register_alg(struct crypto4xx_device *sec_dev,
  885. struct crypto4xx_alg_common *crypto_alg,
  886. int array_size)
  887. {
  888. struct crypto4xx_alg *alg;
  889. int i;
  890. int rc = 0;
  891. for (i = 0; i < array_size; i++) {
  892. alg = kzalloc(sizeof(struct crypto4xx_alg), GFP_KERNEL);
  893. if (!alg)
  894. return -ENOMEM;
  895. alg->alg = crypto_alg[i];
  896. alg->dev = sec_dev;
  897. switch (alg->alg.type) {
  898. case CRYPTO_ALG_TYPE_AEAD:
  899. rc = crypto_register_aead(&alg->alg.u.aead);
  900. break;
  901. case CRYPTO_ALG_TYPE_AHASH:
  902. rc = crypto_register_ahash(&alg->alg.u.hash);
  903. break;
  904. default:
  905. rc = crypto_register_skcipher(&alg->alg.u.cipher);
  906. break;
  907. }
  908. if (rc)
  909. kfree(alg);
  910. else
  911. list_add_tail(&alg->entry, &sec_dev->alg_list);
  912. }
  913. return 0;
  914. }
  915. static void crypto4xx_unregister_alg(struct crypto4xx_device *sec_dev)
  916. {
  917. struct crypto4xx_alg *alg, *tmp;
  918. list_for_each_entry_safe(alg, tmp, &sec_dev->alg_list, entry) {
  919. list_del(&alg->entry);
  920. switch (alg->alg.type) {
  921. case CRYPTO_ALG_TYPE_AHASH:
  922. crypto_unregister_ahash(&alg->alg.u.hash);
  923. break;
  924. case CRYPTO_ALG_TYPE_AEAD:
  925. crypto_unregister_aead(&alg->alg.u.aead);
  926. break;
  927. default:
  928. crypto_unregister_skcipher(&alg->alg.u.cipher);
  929. }
  930. kfree(alg);
  931. }
  932. }
  933. static void crypto4xx_bh_tasklet_cb(unsigned long data)
  934. {
  935. struct device *dev = (struct device *)data;
  936. struct crypto4xx_core_device *core_dev = dev_get_drvdata(dev);
  937. struct pd_uinfo *pd_uinfo;
  938. struct ce_pd *pd;
  939. u32 tail = core_dev->dev->pdr_tail;
  940. u32 head = core_dev->dev->pdr_head;
  941. do {
  942. pd_uinfo = &core_dev->dev->pdr_uinfo[tail];
  943. pd = &core_dev->dev->pdr[tail];
  944. if ((pd_uinfo->state & PD_ENTRY_INUSE) &&
  945. ((READ_ONCE(pd->pd_ctl.w) &
  946. (PD_CTL_PE_DONE | PD_CTL_HOST_READY)) ==
  947. PD_CTL_PE_DONE)) {
  948. crypto4xx_pd_done(core_dev->dev, tail);
  949. tail = crypto4xx_put_pd_to_pdr(core_dev->dev, tail);
  950. } else {
  951. /* if tail not done, break */
  952. break;
  953. }
  954. } while (head != tail);
  955. }
  956. /**
  957. * Top Half of isr.
  958. */
  959. static inline irqreturn_t crypto4xx_interrupt_handler(int irq, void *data,
  960. u32 clr_val)
  961. {
  962. struct device *dev = (struct device *)data;
  963. struct crypto4xx_core_device *core_dev = dev_get_drvdata(dev);
  964. writel(clr_val, core_dev->dev->ce_base + CRYPTO4XX_INT_CLR);
  965. tasklet_schedule(&core_dev->tasklet);
  966. return IRQ_HANDLED;
  967. }
  968. static irqreturn_t crypto4xx_ce_interrupt_handler(int irq, void *data)
  969. {
  970. return crypto4xx_interrupt_handler(irq, data, PPC4XX_INTERRUPT_CLR);
  971. }
  972. static irqreturn_t crypto4xx_ce_interrupt_handler_revb(int irq, void *data)
  973. {
  974. return crypto4xx_interrupt_handler(irq, data, PPC4XX_INTERRUPT_CLR |
  975. PPC4XX_TMO_ERR_INT);
  976. }
  977. /**
  978. * Supported Crypto Algorithms
  979. */
  980. static struct crypto4xx_alg_common crypto4xx_alg[] = {
  981. /* Crypto AES modes */
  982. { .type = CRYPTO_ALG_TYPE_SKCIPHER, .u.cipher = {
  983. .base = {
  984. .cra_name = "cbc(aes)",
  985. .cra_driver_name = "cbc-aes-ppc4xx",
  986. .cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
  987. .cra_flags = CRYPTO_ALG_ASYNC |
  988. CRYPTO_ALG_KERN_DRIVER_ONLY,
  989. .cra_blocksize = AES_BLOCK_SIZE,
  990. .cra_ctxsize = sizeof(struct crypto4xx_ctx),
  991. .cra_module = THIS_MODULE,
  992. },
  993. .min_keysize = AES_MIN_KEY_SIZE,
  994. .max_keysize = AES_MAX_KEY_SIZE,
  995. .ivsize = AES_IV_SIZE,
  996. .setkey = crypto4xx_setkey_aes_cbc,
  997. .encrypt = crypto4xx_encrypt_iv,
  998. .decrypt = crypto4xx_decrypt_iv,
  999. .init = crypto4xx_sk_init,
  1000. .exit = crypto4xx_sk_exit,
  1001. } },
  1002. { .type = CRYPTO_ALG_TYPE_SKCIPHER, .u.cipher = {
  1003. .base = {
  1004. .cra_name = "cfb(aes)",
  1005. .cra_driver_name = "cfb-aes-ppc4xx",
  1006. .cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
  1007. .cra_flags = CRYPTO_ALG_ASYNC |
  1008. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1009. .cra_blocksize = AES_BLOCK_SIZE,
  1010. .cra_ctxsize = sizeof(struct crypto4xx_ctx),
  1011. .cra_module = THIS_MODULE,
  1012. },
  1013. .min_keysize = AES_MIN_KEY_SIZE,
  1014. .max_keysize = AES_MAX_KEY_SIZE,
  1015. .ivsize = AES_IV_SIZE,
  1016. .setkey = crypto4xx_setkey_aes_cfb,
  1017. .encrypt = crypto4xx_encrypt_iv,
  1018. .decrypt = crypto4xx_decrypt_iv,
  1019. .init = crypto4xx_sk_init,
  1020. .exit = crypto4xx_sk_exit,
  1021. } },
  1022. { .type = CRYPTO_ALG_TYPE_SKCIPHER, .u.cipher = {
  1023. .base = {
  1024. .cra_name = "ctr(aes)",
  1025. .cra_driver_name = "ctr-aes-ppc4xx",
  1026. .cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
  1027. .cra_flags = CRYPTO_ALG_NEED_FALLBACK |
  1028. CRYPTO_ALG_ASYNC |
  1029. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1030. .cra_blocksize = AES_BLOCK_SIZE,
  1031. .cra_ctxsize = sizeof(struct crypto4xx_ctx),
  1032. .cra_module = THIS_MODULE,
  1033. },
  1034. .min_keysize = AES_MIN_KEY_SIZE,
  1035. .max_keysize = AES_MAX_KEY_SIZE,
  1036. .ivsize = AES_IV_SIZE,
  1037. .setkey = crypto4xx_setkey_aes_ctr,
  1038. .encrypt = crypto4xx_encrypt_ctr,
  1039. .decrypt = crypto4xx_decrypt_ctr,
  1040. .init = crypto4xx_sk_init,
  1041. .exit = crypto4xx_sk_exit,
  1042. } },
  1043. { .type = CRYPTO_ALG_TYPE_SKCIPHER, .u.cipher = {
  1044. .base = {
  1045. .cra_name = "rfc3686(ctr(aes))",
  1046. .cra_driver_name = "rfc3686-ctr-aes-ppc4xx",
  1047. .cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
  1048. .cra_flags = CRYPTO_ALG_ASYNC |
  1049. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1050. .cra_blocksize = AES_BLOCK_SIZE,
  1051. .cra_ctxsize = sizeof(struct crypto4xx_ctx),
  1052. .cra_module = THIS_MODULE,
  1053. },
  1054. .min_keysize = AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
  1055. .max_keysize = AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
  1056. .ivsize = CTR_RFC3686_IV_SIZE,
  1057. .setkey = crypto4xx_setkey_rfc3686,
  1058. .encrypt = crypto4xx_rfc3686_encrypt,
  1059. .decrypt = crypto4xx_rfc3686_decrypt,
  1060. .init = crypto4xx_sk_init,
  1061. .exit = crypto4xx_sk_exit,
  1062. } },
  1063. { .type = CRYPTO_ALG_TYPE_SKCIPHER, .u.cipher = {
  1064. .base = {
  1065. .cra_name = "ecb(aes)",
  1066. .cra_driver_name = "ecb-aes-ppc4xx",
  1067. .cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
  1068. .cra_flags = CRYPTO_ALG_ASYNC |
  1069. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1070. .cra_blocksize = AES_BLOCK_SIZE,
  1071. .cra_ctxsize = sizeof(struct crypto4xx_ctx),
  1072. .cra_module = THIS_MODULE,
  1073. },
  1074. .min_keysize = AES_MIN_KEY_SIZE,
  1075. .max_keysize = AES_MAX_KEY_SIZE,
  1076. .setkey = crypto4xx_setkey_aes_ecb,
  1077. .encrypt = crypto4xx_encrypt_noiv,
  1078. .decrypt = crypto4xx_decrypt_noiv,
  1079. .init = crypto4xx_sk_init,
  1080. .exit = crypto4xx_sk_exit,
  1081. } },
  1082. { .type = CRYPTO_ALG_TYPE_SKCIPHER, .u.cipher = {
  1083. .base = {
  1084. .cra_name = "ofb(aes)",
  1085. .cra_driver_name = "ofb-aes-ppc4xx",
  1086. .cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
  1087. .cra_flags = CRYPTO_ALG_ASYNC |
  1088. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1089. .cra_blocksize = AES_BLOCK_SIZE,
  1090. .cra_ctxsize = sizeof(struct crypto4xx_ctx),
  1091. .cra_module = THIS_MODULE,
  1092. },
  1093. .min_keysize = AES_MIN_KEY_SIZE,
  1094. .max_keysize = AES_MAX_KEY_SIZE,
  1095. .ivsize = AES_IV_SIZE,
  1096. .setkey = crypto4xx_setkey_aes_ofb,
  1097. .encrypt = crypto4xx_encrypt_iv,
  1098. .decrypt = crypto4xx_decrypt_iv,
  1099. .init = crypto4xx_sk_init,
  1100. .exit = crypto4xx_sk_exit,
  1101. } },
  1102. /* AEAD */
  1103. { .type = CRYPTO_ALG_TYPE_AEAD, .u.aead = {
  1104. .setkey = crypto4xx_setkey_aes_ccm,
  1105. .setauthsize = crypto4xx_setauthsize_aead,
  1106. .encrypt = crypto4xx_encrypt_aes_ccm,
  1107. .decrypt = crypto4xx_decrypt_aes_ccm,
  1108. .init = crypto4xx_aead_init,
  1109. .exit = crypto4xx_aead_exit,
  1110. .ivsize = AES_BLOCK_SIZE,
  1111. .maxauthsize = 16,
  1112. .base = {
  1113. .cra_name = "ccm(aes)",
  1114. .cra_driver_name = "ccm-aes-ppc4xx",
  1115. .cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
  1116. .cra_flags = CRYPTO_ALG_ASYNC |
  1117. CRYPTO_ALG_NEED_FALLBACK |
  1118. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1119. .cra_blocksize = 1,
  1120. .cra_ctxsize = sizeof(struct crypto4xx_ctx),
  1121. .cra_module = THIS_MODULE,
  1122. },
  1123. } },
  1124. { .type = CRYPTO_ALG_TYPE_AEAD, .u.aead = {
  1125. .setkey = crypto4xx_setkey_aes_gcm,
  1126. .setauthsize = crypto4xx_setauthsize_aead,
  1127. .encrypt = crypto4xx_encrypt_aes_gcm,
  1128. .decrypt = crypto4xx_decrypt_aes_gcm,
  1129. .init = crypto4xx_aead_init,
  1130. .exit = crypto4xx_aead_exit,
  1131. .ivsize = GCM_AES_IV_SIZE,
  1132. .maxauthsize = 16,
  1133. .base = {
  1134. .cra_name = "gcm(aes)",
  1135. .cra_driver_name = "gcm-aes-ppc4xx",
  1136. .cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
  1137. .cra_flags = CRYPTO_ALG_ASYNC |
  1138. CRYPTO_ALG_NEED_FALLBACK |
  1139. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1140. .cra_blocksize = 1,
  1141. .cra_ctxsize = sizeof(struct crypto4xx_ctx),
  1142. .cra_module = THIS_MODULE,
  1143. },
  1144. } },
  1145. };
  1146. /**
  1147. * Module Initialization Routine
  1148. */
  1149. static int crypto4xx_probe(struct platform_device *ofdev)
  1150. {
  1151. int rc;
  1152. struct resource res;
  1153. struct device *dev = &ofdev->dev;
  1154. struct crypto4xx_core_device *core_dev;
  1155. u32 pvr;
  1156. bool is_revb = true;
  1157. rc = of_address_to_resource(ofdev->dev.of_node, 0, &res);
  1158. if (rc)
  1159. return -ENODEV;
  1160. if (of_find_compatible_node(NULL, NULL, "amcc,ppc460ex-crypto")) {
  1161. mtdcri(SDR0, PPC460EX_SDR0_SRST,
  1162. mfdcri(SDR0, PPC460EX_SDR0_SRST) | PPC460EX_CE_RESET);
  1163. mtdcri(SDR0, PPC460EX_SDR0_SRST,
  1164. mfdcri(SDR0, PPC460EX_SDR0_SRST) & ~PPC460EX_CE_RESET);
  1165. } else if (of_find_compatible_node(NULL, NULL,
  1166. "amcc,ppc405ex-crypto")) {
  1167. mtdcri(SDR0, PPC405EX_SDR0_SRST,
  1168. mfdcri(SDR0, PPC405EX_SDR0_SRST) | PPC405EX_CE_RESET);
  1169. mtdcri(SDR0, PPC405EX_SDR0_SRST,
  1170. mfdcri(SDR0, PPC405EX_SDR0_SRST) & ~PPC405EX_CE_RESET);
  1171. is_revb = false;
  1172. } else if (of_find_compatible_node(NULL, NULL,
  1173. "amcc,ppc460sx-crypto")) {
  1174. mtdcri(SDR0, PPC460SX_SDR0_SRST,
  1175. mfdcri(SDR0, PPC460SX_SDR0_SRST) | PPC460SX_CE_RESET);
  1176. mtdcri(SDR0, PPC460SX_SDR0_SRST,
  1177. mfdcri(SDR0, PPC460SX_SDR0_SRST) & ~PPC460SX_CE_RESET);
  1178. } else {
  1179. printk(KERN_ERR "Crypto Function Not supported!\n");
  1180. return -EINVAL;
  1181. }
  1182. core_dev = kzalloc(sizeof(struct crypto4xx_core_device), GFP_KERNEL);
  1183. if (!core_dev)
  1184. return -ENOMEM;
  1185. dev_set_drvdata(dev, core_dev);
  1186. core_dev->ofdev = ofdev;
  1187. core_dev->dev = kzalloc(sizeof(struct crypto4xx_device), GFP_KERNEL);
  1188. rc = -ENOMEM;
  1189. if (!core_dev->dev)
  1190. goto err_alloc_dev;
  1191. /*
  1192. * Older version of 460EX/GT have a hardware bug.
  1193. * Hence they do not support H/W based security intr coalescing
  1194. */
  1195. pvr = mfspr(SPRN_PVR);
  1196. if (is_revb && ((pvr >> 4) == 0x130218A)) {
  1197. u32 min = PVR_MIN(pvr);
  1198. if (min < 4) {
  1199. dev_info(dev, "RevA detected - disable interrupt coalescing\n");
  1200. is_revb = false;
  1201. }
  1202. }
  1203. core_dev->dev->core_dev = core_dev;
  1204. core_dev->dev->is_revb = is_revb;
  1205. core_dev->device = dev;
  1206. spin_lock_init(&core_dev->lock);
  1207. INIT_LIST_HEAD(&core_dev->dev->alg_list);
  1208. ratelimit_default_init(&core_dev->dev->aead_ratelimit);
  1209. rc = crypto4xx_build_pdr(core_dev->dev);
  1210. if (rc)
  1211. goto err_build_pdr;
  1212. rc = crypto4xx_build_gdr(core_dev->dev);
  1213. if (rc)
  1214. goto err_build_pdr;
  1215. rc = crypto4xx_build_sdr(core_dev->dev);
  1216. if (rc)
  1217. goto err_build_sdr;
  1218. /* Init tasklet for bottom half processing */
  1219. tasklet_init(&core_dev->tasklet, crypto4xx_bh_tasklet_cb,
  1220. (unsigned long) dev);
  1221. core_dev->dev->ce_base = of_iomap(ofdev->dev.of_node, 0);
  1222. if (!core_dev->dev->ce_base) {
  1223. dev_err(dev, "failed to of_iomap\n");
  1224. rc = -ENOMEM;
  1225. goto err_iomap;
  1226. }
  1227. /* Register for Crypto isr, Crypto Engine IRQ */
  1228. core_dev->irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
  1229. rc = request_irq(core_dev->irq, is_revb ?
  1230. crypto4xx_ce_interrupt_handler_revb :
  1231. crypto4xx_ce_interrupt_handler, 0,
  1232. KBUILD_MODNAME, dev);
  1233. if (rc)
  1234. goto err_request_irq;
  1235. /* need to setup pdr, rdr, gdr and sdr before this */
  1236. crypto4xx_hw_init(core_dev->dev);
  1237. /* Register security algorithms with Linux CryptoAPI */
  1238. rc = crypto4xx_register_alg(core_dev->dev, crypto4xx_alg,
  1239. ARRAY_SIZE(crypto4xx_alg));
  1240. if (rc)
  1241. goto err_start_dev;
  1242. ppc4xx_trng_probe(core_dev);
  1243. return 0;
  1244. err_start_dev:
  1245. free_irq(core_dev->irq, dev);
  1246. err_request_irq:
  1247. irq_dispose_mapping(core_dev->irq);
  1248. iounmap(core_dev->dev->ce_base);
  1249. err_iomap:
  1250. tasklet_kill(&core_dev->tasklet);
  1251. err_build_sdr:
  1252. crypto4xx_destroy_sdr(core_dev->dev);
  1253. crypto4xx_destroy_gdr(core_dev->dev);
  1254. err_build_pdr:
  1255. crypto4xx_destroy_pdr(core_dev->dev);
  1256. kfree(core_dev->dev);
  1257. err_alloc_dev:
  1258. kfree(core_dev);
  1259. return rc;
  1260. }
  1261. static int crypto4xx_remove(struct platform_device *ofdev)
  1262. {
  1263. struct device *dev = &ofdev->dev;
  1264. struct crypto4xx_core_device *core_dev = dev_get_drvdata(dev);
  1265. ppc4xx_trng_remove(core_dev);
  1266. free_irq(core_dev->irq, dev);
  1267. irq_dispose_mapping(core_dev->irq);
  1268. tasklet_kill(&core_dev->tasklet);
  1269. /* Un-register with Linux CryptoAPI */
  1270. crypto4xx_unregister_alg(core_dev->dev);
  1271. /* Free all allocated memory */
  1272. crypto4xx_stop_all(core_dev);
  1273. return 0;
  1274. }
  1275. static const struct of_device_id crypto4xx_match[] = {
  1276. { .compatible = "amcc,ppc4xx-crypto",},
  1277. { },
  1278. };
  1279. MODULE_DEVICE_TABLE(of, crypto4xx_match);
  1280. static struct platform_driver crypto4xx_driver = {
  1281. .driver = {
  1282. .name = KBUILD_MODNAME,
  1283. .of_match_table = crypto4xx_match,
  1284. },
  1285. .probe = crypto4xx_probe,
  1286. .remove = crypto4xx_remove,
  1287. };
  1288. module_platform_driver(crypto4xx_driver);
  1289. MODULE_LICENSE("GPL");
  1290. MODULE_AUTHOR("James Hsiao <jhsiao@amcc.com>");
  1291. MODULE_DESCRIPTION("Driver for AMCC PPC4xx crypto accelerator");