crypto4xx_core.c 39 KB

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