crypto4xx_core.c 40 KB

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