crypto4xx_core.c 40 KB

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