hifn_795x.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * 2007+ Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
  4. * All rights reserved.
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/moduleparam.h>
  9. #include <linux/mod_devicetable.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/pci.h>
  12. #include <linux/slab.h>
  13. #include <linux/delay.h>
  14. #include <linux/mm.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/highmem.h>
  18. #include <linux/crypto.h>
  19. #include <linux/hw_random.h>
  20. #include <linux/ktime.h>
  21. #include <crypto/algapi.h>
  22. #include <crypto/internal/des.h>
  23. #include <crypto/internal/skcipher.h>
  24. static char hifn_pll_ref[sizeof("extNNN")] = "ext";
  25. module_param_string(hifn_pll_ref, hifn_pll_ref, sizeof(hifn_pll_ref), 0444);
  26. MODULE_PARM_DESC(hifn_pll_ref,
  27. "PLL reference clock (pci[freq] or ext[freq], default ext)");
  28. static atomic_t hifn_dev_number;
  29. #define ACRYPTO_OP_DECRYPT 0
  30. #define ACRYPTO_OP_ENCRYPT 1
  31. #define ACRYPTO_OP_HMAC 2
  32. #define ACRYPTO_OP_RNG 3
  33. #define ACRYPTO_MODE_ECB 0
  34. #define ACRYPTO_MODE_CBC 1
  35. #define ACRYPTO_MODE_CFB 2
  36. #define ACRYPTO_MODE_OFB 3
  37. #define ACRYPTO_TYPE_AES_128 0
  38. #define ACRYPTO_TYPE_AES_192 1
  39. #define ACRYPTO_TYPE_AES_256 2
  40. #define ACRYPTO_TYPE_3DES 3
  41. #define ACRYPTO_TYPE_DES 4
  42. #define PCI_VENDOR_ID_HIFN 0x13A3
  43. #define PCI_DEVICE_ID_HIFN_7955 0x0020
  44. #define PCI_DEVICE_ID_HIFN_7956 0x001d
  45. /* I/O region sizes */
  46. #define HIFN_BAR0_SIZE 0x1000
  47. #define HIFN_BAR1_SIZE 0x2000
  48. #define HIFN_BAR2_SIZE 0x8000
  49. /* DMA registres */
  50. #define HIFN_DMA_CRA 0x0C /* DMA Command Ring Address */
  51. #define HIFN_DMA_SDRA 0x1C /* DMA Source Data Ring Address */
  52. #define HIFN_DMA_RRA 0x2C /* DMA Result Ring Address */
  53. #define HIFN_DMA_DDRA 0x3C /* DMA Destination Data Ring Address */
  54. #define HIFN_DMA_STCTL 0x40 /* DMA Status and Control */
  55. #define HIFN_DMA_INTREN 0x44 /* DMA Interrupt Enable */
  56. #define HIFN_DMA_CFG1 0x48 /* DMA Configuration #1 */
  57. #define HIFN_DMA_CFG2 0x6C /* DMA Configuration #2 */
  58. #define HIFN_CHIP_ID 0x98 /* Chip ID */
  59. /*
  60. * Processing Unit Registers (offset from BASEREG0)
  61. */
  62. #define HIFN_0_PUDATA 0x00 /* Processing Unit Data */
  63. #define HIFN_0_PUCTRL 0x04 /* Processing Unit Control */
  64. #define HIFN_0_PUISR 0x08 /* Processing Unit Interrupt Status */
  65. #define HIFN_0_PUCNFG 0x0c /* Processing Unit Configuration */
  66. #define HIFN_0_PUIER 0x10 /* Processing Unit Interrupt Enable */
  67. #define HIFN_0_PUSTAT 0x14 /* Processing Unit Status/Chip ID */
  68. #define HIFN_0_FIFOSTAT 0x18 /* FIFO Status */
  69. #define HIFN_0_FIFOCNFG 0x1c /* FIFO Configuration */
  70. #define HIFN_0_SPACESIZE 0x20 /* Register space size */
  71. /* Processing Unit Control Register (HIFN_0_PUCTRL) */
  72. #define HIFN_PUCTRL_CLRSRCFIFO 0x0010 /* clear source fifo */
  73. #define HIFN_PUCTRL_STOP 0x0008 /* stop pu */
  74. #define HIFN_PUCTRL_LOCKRAM 0x0004 /* lock ram */
  75. #define HIFN_PUCTRL_DMAENA 0x0002 /* enable dma */
  76. #define HIFN_PUCTRL_RESET 0x0001 /* Reset processing unit */
  77. /* Processing Unit Interrupt Status Register (HIFN_0_PUISR) */
  78. #define HIFN_PUISR_CMDINVAL 0x8000 /* Invalid command interrupt */
  79. #define HIFN_PUISR_DATAERR 0x4000 /* Data error interrupt */
  80. #define HIFN_PUISR_SRCFIFO 0x2000 /* Source FIFO ready interrupt */
  81. #define HIFN_PUISR_DSTFIFO 0x1000 /* Destination FIFO ready interrupt */
  82. #define HIFN_PUISR_DSTOVER 0x0200 /* Destination overrun interrupt */
  83. #define HIFN_PUISR_SRCCMD 0x0080 /* Source command interrupt */
  84. #define HIFN_PUISR_SRCCTX 0x0040 /* Source context interrupt */
  85. #define HIFN_PUISR_SRCDATA 0x0020 /* Source data interrupt */
  86. #define HIFN_PUISR_DSTDATA 0x0010 /* Destination data interrupt */
  87. #define HIFN_PUISR_DSTRESULT 0x0004 /* Destination result interrupt */
  88. /* Processing Unit Configuration Register (HIFN_0_PUCNFG) */
  89. #define HIFN_PUCNFG_DRAMMASK 0xe000 /* DRAM size mask */
  90. #define HIFN_PUCNFG_DSZ_256K 0x0000 /* 256k dram */
  91. #define HIFN_PUCNFG_DSZ_512K 0x2000 /* 512k dram */
  92. #define HIFN_PUCNFG_DSZ_1M 0x4000 /* 1m dram */
  93. #define HIFN_PUCNFG_DSZ_2M 0x6000 /* 2m dram */
  94. #define HIFN_PUCNFG_DSZ_4M 0x8000 /* 4m dram */
  95. #define HIFN_PUCNFG_DSZ_8M 0xa000 /* 8m dram */
  96. #define HIFN_PUNCFG_DSZ_16M 0xc000 /* 16m dram */
  97. #define HIFN_PUCNFG_DSZ_32M 0xe000 /* 32m dram */
  98. #define HIFN_PUCNFG_DRAMREFRESH 0x1800 /* DRAM refresh rate mask */
  99. #define HIFN_PUCNFG_DRFR_512 0x0000 /* 512 divisor of ECLK */
  100. #define HIFN_PUCNFG_DRFR_256 0x0800 /* 256 divisor of ECLK */
  101. #define HIFN_PUCNFG_DRFR_128 0x1000 /* 128 divisor of ECLK */
  102. #define HIFN_PUCNFG_TCALLPHASES 0x0200 /* your guess is as good as mine... */
  103. #define HIFN_PUCNFG_TCDRVTOTEM 0x0100 /* your guess is as good as mine... */
  104. #define HIFN_PUCNFG_BIGENDIAN 0x0080 /* DMA big endian mode */
  105. #define HIFN_PUCNFG_BUS32 0x0040 /* Bus width 32bits */
  106. #define HIFN_PUCNFG_BUS16 0x0000 /* Bus width 16 bits */
  107. #define HIFN_PUCNFG_CHIPID 0x0020 /* Allow chipid from PUSTAT */
  108. #define HIFN_PUCNFG_DRAM 0x0010 /* Context RAM is DRAM */
  109. #define HIFN_PUCNFG_SRAM 0x0000 /* Context RAM is SRAM */
  110. #define HIFN_PUCNFG_COMPSING 0x0004 /* Enable single compression context */
  111. #define HIFN_PUCNFG_ENCCNFG 0x0002 /* Encryption configuration */
  112. /* Processing Unit Interrupt Enable Register (HIFN_0_PUIER) */
  113. #define HIFN_PUIER_CMDINVAL 0x8000 /* Invalid command interrupt */
  114. #define HIFN_PUIER_DATAERR 0x4000 /* Data error interrupt */
  115. #define HIFN_PUIER_SRCFIFO 0x2000 /* Source FIFO ready interrupt */
  116. #define HIFN_PUIER_DSTFIFO 0x1000 /* Destination FIFO ready interrupt */
  117. #define HIFN_PUIER_DSTOVER 0x0200 /* Destination overrun interrupt */
  118. #define HIFN_PUIER_SRCCMD 0x0080 /* Source command interrupt */
  119. #define HIFN_PUIER_SRCCTX 0x0040 /* Source context interrupt */
  120. #define HIFN_PUIER_SRCDATA 0x0020 /* Source data interrupt */
  121. #define HIFN_PUIER_DSTDATA 0x0010 /* Destination data interrupt */
  122. #define HIFN_PUIER_DSTRESULT 0x0004 /* Destination result interrupt */
  123. /* Processing Unit Status Register/Chip ID (HIFN_0_PUSTAT) */
  124. #define HIFN_PUSTAT_CMDINVAL 0x8000 /* Invalid command interrupt */
  125. #define HIFN_PUSTAT_DATAERR 0x4000 /* Data error interrupt */
  126. #define HIFN_PUSTAT_SRCFIFO 0x2000 /* Source FIFO ready interrupt */
  127. #define HIFN_PUSTAT_DSTFIFO 0x1000 /* Destination FIFO ready interrupt */
  128. #define HIFN_PUSTAT_DSTOVER 0x0200 /* Destination overrun interrupt */
  129. #define HIFN_PUSTAT_SRCCMD 0x0080 /* Source command interrupt */
  130. #define HIFN_PUSTAT_SRCCTX 0x0040 /* Source context interrupt */
  131. #define HIFN_PUSTAT_SRCDATA 0x0020 /* Source data interrupt */
  132. #define HIFN_PUSTAT_DSTDATA 0x0010 /* Destination data interrupt */
  133. #define HIFN_PUSTAT_DSTRESULT 0x0004 /* Destination result interrupt */
  134. #define HIFN_PUSTAT_CHIPREV 0x00ff /* Chip revision mask */
  135. #define HIFN_PUSTAT_CHIPENA 0xff00 /* Chip enabled mask */
  136. #define HIFN_PUSTAT_ENA_2 0x1100 /* Level 2 enabled */
  137. #define HIFN_PUSTAT_ENA_1 0x1000 /* Level 1 enabled */
  138. #define HIFN_PUSTAT_ENA_0 0x3000 /* Level 0 enabled */
  139. #define HIFN_PUSTAT_REV_2 0x0020 /* 7751 PT6/2 */
  140. #define HIFN_PUSTAT_REV_3 0x0030 /* 7751 PT6/3 */
  141. /* FIFO Status Register (HIFN_0_FIFOSTAT) */
  142. #define HIFN_FIFOSTAT_SRC 0x7f00 /* Source FIFO available */
  143. #define HIFN_FIFOSTAT_DST 0x007f /* Destination FIFO available */
  144. /* FIFO Configuration Register (HIFN_0_FIFOCNFG) */
  145. #define HIFN_FIFOCNFG_THRESHOLD 0x0400 /* must be written as 1 */
  146. /*
  147. * DMA Interface Registers (offset from BASEREG1)
  148. */
  149. #define HIFN_1_DMA_CRAR 0x0c /* DMA Command Ring Address */
  150. #define HIFN_1_DMA_SRAR 0x1c /* DMA Source Ring Address */
  151. #define HIFN_1_DMA_RRAR 0x2c /* DMA Result Ring Address */
  152. #define HIFN_1_DMA_DRAR 0x3c /* DMA Destination Ring Address */
  153. #define HIFN_1_DMA_CSR 0x40 /* DMA Status and Control */
  154. #define HIFN_1_DMA_IER 0x44 /* DMA Interrupt Enable */
  155. #define HIFN_1_DMA_CNFG 0x48 /* DMA Configuration */
  156. #define HIFN_1_PLL 0x4c /* 795x: PLL config */
  157. #define HIFN_1_7811_RNGENA 0x60 /* 7811: rng enable */
  158. #define HIFN_1_7811_RNGCFG 0x64 /* 7811: rng config */
  159. #define HIFN_1_7811_RNGDAT 0x68 /* 7811: rng data */
  160. #define HIFN_1_7811_RNGSTS 0x6c /* 7811: rng status */
  161. #define HIFN_1_7811_MIPSRST 0x94 /* 7811: MIPS reset */
  162. #define HIFN_1_REVID 0x98 /* Revision ID */
  163. #define HIFN_1_UNLOCK_SECRET1 0xf4
  164. #define HIFN_1_UNLOCK_SECRET2 0xfc
  165. #define HIFN_1_PUB_RESET 0x204 /* Public/RNG Reset */
  166. #define HIFN_1_PUB_BASE 0x300 /* Public Base Address */
  167. #define HIFN_1_PUB_OPLEN 0x304 /* Public Operand Length */
  168. #define HIFN_1_PUB_OP 0x308 /* Public Operand */
  169. #define HIFN_1_PUB_STATUS 0x30c /* Public Status */
  170. #define HIFN_1_PUB_IEN 0x310 /* Public Interrupt enable */
  171. #define HIFN_1_RNG_CONFIG 0x314 /* RNG config */
  172. #define HIFN_1_RNG_DATA 0x318 /* RNG data */
  173. #define HIFN_1_PUB_MEM 0x400 /* start of Public key memory */
  174. #define HIFN_1_PUB_MEMEND 0xbff /* end of Public key memory */
  175. /* DMA Status and Control Register (HIFN_1_DMA_CSR) */
  176. #define HIFN_DMACSR_D_CTRLMASK 0xc0000000 /* Destinition Ring Control */
  177. #define HIFN_DMACSR_D_CTRL_NOP 0x00000000 /* Dest. Control: no-op */
  178. #define HIFN_DMACSR_D_CTRL_DIS 0x40000000 /* Dest. Control: disable */
  179. #define HIFN_DMACSR_D_CTRL_ENA 0x80000000 /* Dest. Control: enable */
  180. #define HIFN_DMACSR_D_ABORT 0x20000000 /* Destinition Ring PCIAbort */
  181. #define HIFN_DMACSR_D_DONE 0x10000000 /* Destinition Ring Done */
  182. #define HIFN_DMACSR_D_LAST 0x08000000 /* Destinition Ring Last */
  183. #define HIFN_DMACSR_D_WAIT 0x04000000 /* Destinition Ring Waiting */
  184. #define HIFN_DMACSR_D_OVER 0x02000000 /* Destinition Ring Overflow */
  185. #define HIFN_DMACSR_R_CTRL 0x00c00000 /* Result Ring Control */
  186. #define HIFN_DMACSR_R_CTRL_NOP 0x00000000 /* Result Control: no-op */
  187. #define HIFN_DMACSR_R_CTRL_DIS 0x00400000 /* Result Control: disable */
  188. #define HIFN_DMACSR_R_CTRL_ENA 0x00800000 /* Result Control: enable */
  189. #define HIFN_DMACSR_R_ABORT 0x00200000 /* Result Ring PCI Abort */
  190. #define HIFN_DMACSR_R_DONE 0x00100000 /* Result Ring Done */
  191. #define HIFN_DMACSR_R_LAST 0x00080000 /* Result Ring Last */
  192. #define HIFN_DMACSR_R_WAIT 0x00040000 /* Result Ring Waiting */
  193. #define HIFN_DMACSR_R_OVER 0x00020000 /* Result Ring Overflow */
  194. #define HIFN_DMACSR_S_CTRL 0x0000c000 /* Source Ring Control */
  195. #define HIFN_DMACSR_S_CTRL_NOP 0x00000000 /* Source Control: no-op */
  196. #define HIFN_DMACSR_S_CTRL_DIS 0x00004000 /* Source Control: disable */
  197. #define HIFN_DMACSR_S_CTRL_ENA 0x00008000 /* Source Control: enable */
  198. #define HIFN_DMACSR_S_ABORT 0x00002000 /* Source Ring PCI Abort */
  199. #define HIFN_DMACSR_S_DONE 0x00001000 /* Source Ring Done */
  200. #define HIFN_DMACSR_S_LAST 0x00000800 /* Source Ring Last */
  201. #define HIFN_DMACSR_S_WAIT 0x00000400 /* Source Ring Waiting */
  202. #define HIFN_DMACSR_ILLW 0x00000200 /* Illegal write (7811 only) */
  203. #define HIFN_DMACSR_ILLR 0x00000100 /* Illegal read (7811 only) */
  204. #define HIFN_DMACSR_C_CTRL 0x000000c0 /* Command Ring Control */
  205. #define HIFN_DMACSR_C_CTRL_NOP 0x00000000 /* Command Control: no-op */
  206. #define HIFN_DMACSR_C_CTRL_DIS 0x00000040 /* Command Control: disable */
  207. #define HIFN_DMACSR_C_CTRL_ENA 0x00000080 /* Command Control: enable */
  208. #define HIFN_DMACSR_C_ABORT 0x00000020 /* Command Ring PCI Abort */
  209. #define HIFN_DMACSR_C_DONE 0x00000010 /* Command Ring Done */
  210. #define HIFN_DMACSR_C_LAST 0x00000008 /* Command Ring Last */
  211. #define HIFN_DMACSR_C_WAIT 0x00000004 /* Command Ring Waiting */
  212. #define HIFN_DMACSR_PUBDONE 0x00000002 /* Public op done (7951 only) */
  213. #define HIFN_DMACSR_ENGINE 0x00000001 /* Command Ring Engine IRQ */
  214. /* DMA Interrupt Enable Register (HIFN_1_DMA_IER) */
  215. #define HIFN_DMAIER_D_ABORT 0x20000000 /* Destination Ring PCIAbort */
  216. #define HIFN_DMAIER_D_DONE 0x10000000 /* Destination Ring Done */
  217. #define HIFN_DMAIER_D_LAST 0x08000000 /* Destination Ring Last */
  218. #define HIFN_DMAIER_D_WAIT 0x04000000 /* Destination Ring Waiting */
  219. #define HIFN_DMAIER_D_OVER 0x02000000 /* Destination Ring Overflow */
  220. #define HIFN_DMAIER_R_ABORT 0x00200000 /* Result Ring PCI Abort */
  221. #define HIFN_DMAIER_R_DONE 0x00100000 /* Result Ring Done */
  222. #define HIFN_DMAIER_R_LAST 0x00080000 /* Result Ring Last */
  223. #define HIFN_DMAIER_R_WAIT 0x00040000 /* Result Ring Waiting */
  224. #define HIFN_DMAIER_R_OVER 0x00020000 /* Result Ring Overflow */
  225. #define HIFN_DMAIER_S_ABORT 0x00002000 /* Source Ring PCI Abort */
  226. #define HIFN_DMAIER_S_DONE 0x00001000 /* Source Ring Done */
  227. #define HIFN_DMAIER_S_LAST 0x00000800 /* Source Ring Last */
  228. #define HIFN_DMAIER_S_WAIT 0x00000400 /* Source Ring Waiting */
  229. #define HIFN_DMAIER_ILLW 0x00000200 /* Illegal write (7811 only) */
  230. #define HIFN_DMAIER_ILLR 0x00000100 /* Illegal read (7811 only) */
  231. #define HIFN_DMAIER_C_ABORT 0x00000020 /* Command Ring PCI Abort */
  232. #define HIFN_DMAIER_C_DONE 0x00000010 /* Command Ring Done */
  233. #define HIFN_DMAIER_C_LAST 0x00000008 /* Command Ring Last */
  234. #define HIFN_DMAIER_C_WAIT 0x00000004 /* Command Ring Waiting */
  235. #define HIFN_DMAIER_PUBDONE 0x00000002 /* public op done (7951 only) */
  236. #define HIFN_DMAIER_ENGINE 0x00000001 /* Engine IRQ */
  237. /* DMA Configuration Register (HIFN_1_DMA_CNFG) */
  238. #define HIFN_DMACNFG_BIGENDIAN 0x10000000 /* big endian mode */
  239. #define HIFN_DMACNFG_POLLFREQ 0x00ff0000 /* Poll frequency mask */
  240. #define HIFN_DMACNFG_UNLOCK 0x00000800
  241. #define HIFN_DMACNFG_POLLINVAL 0x00000700 /* Invalid Poll Scalar */
  242. #define HIFN_DMACNFG_LAST 0x00000010 /* Host control LAST bit */
  243. #define HIFN_DMACNFG_MODE 0x00000004 /* DMA mode */
  244. #define HIFN_DMACNFG_DMARESET 0x00000002 /* DMA Reset # */
  245. #define HIFN_DMACNFG_MSTRESET 0x00000001 /* Master Reset # */
  246. /* PLL configuration register */
  247. #define HIFN_PLL_REF_CLK_HBI 0x00000000 /* HBI reference clock */
  248. #define HIFN_PLL_REF_CLK_PLL 0x00000001 /* PLL reference clock */
  249. #define HIFN_PLL_BP 0x00000002 /* Reference clock bypass */
  250. #define HIFN_PLL_PK_CLK_HBI 0x00000000 /* PK engine HBI clock */
  251. #define HIFN_PLL_PK_CLK_PLL 0x00000008 /* PK engine PLL clock */
  252. #define HIFN_PLL_PE_CLK_HBI 0x00000000 /* PE engine HBI clock */
  253. #define HIFN_PLL_PE_CLK_PLL 0x00000010 /* PE engine PLL clock */
  254. #define HIFN_PLL_RESERVED_1 0x00000400 /* Reserved bit, must be 1 */
  255. #define HIFN_PLL_ND_SHIFT 11 /* Clock multiplier shift */
  256. #define HIFN_PLL_ND_MULT_2 0x00000000 /* PLL clock multiplier 2 */
  257. #define HIFN_PLL_ND_MULT_4 0x00000800 /* PLL clock multiplier 4 */
  258. #define HIFN_PLL_ND_MULT_6 0x00001000 /* PLL clock multiplier 6 */
  259. #define HIFN_PLL_ND_MULT_8 0x00001800 /* PLL clock multiplier 8 */
  260. #define HIFN_PLL_ND_MULT_10 0x00002000 /* PLL clock multiplier 10 */
  261. #define HIFN_PLL_ND_MULT_12 0x00002800 /* PLL clock multiplier 12 */
  262. #define HIFN_PLL_IS_1_8 0x00000000 /* charge pump (mult. 1-8) */
  263. #define HIFN_PLL_IS_9_12 0x00010000 /* charge pump (mult. 9-12) */
  264. #define HIFN_PLL_FCK_MAX 266 /* Maximum PLL frequency */
  265. /* Public key reset register (HIFN_1_PUB_RESET) */
  266. #define HIFN_PUBRST_RESET 0x00000001 /* reset public/rng unit */
  267. /* Public base address register (HIFN_1_PUB_BASE) */
  268. #define HIFN_PUBBASE_ADDR 0x00003fff /* base address */
  269. /* Public operand length register (HIFN_1_PUB_OPLEN) */
  270. #define HIFN_PUBOPLEN_MOD_M 0x0000007f /* modulus length mask */
  271. #define HIFN_PUBOPLEN_MOD_S 0 /* modulus length shift */
  272. #define HIFN_PUBOPLEN_EXP_M 0x0003ff80 /* exponent length mask */
  273. #define HIFN_PUBOPLEN_EXP_S 7 /* exponent length shift */
  274. #define HIFN_PUBOPLEN_RED_M 0x003c0000 /* reducend length mask */
  275. #define HIFN_PUBOPLEN_RED_S 18 /* reducend length shift */
  276. /* Public operation register (HIFN_1_PUB_OP) */
  277. #define HIFN_PUBOP_AOFFSET_M 0x0000007f /* A offset mask */
  278. #define HIFN_PUBOP_AOFFSET_S 0 /* A offset shift */
  279. #define HIFN_PUBOP_BOFFSET_M 0x00000f80 /* B offset mask */
  280. #define HIFN_PUBOP_BOFFSET_S 7 /* B offset shift */
  281. #define HIFN_PUBOP_MOFFSET_M 0x0003f000 /* M offset mask */
  282. #define HIFN_PUBOP_MOFFSET_S 12 /* M offset shift */
  283. #define HIFN_PUBOP_OP_MASK 0x003c0000 /* Opcode: */
  284. #define HIFN_PUBOP_OP_NOP 0x00000000 /* NOP */
  285. #define HIFN_PUBOP_OP_ADD 0x00040000 /* ADD */
  286. #define HIFN_PUBOP_OP_ADDC 0x00080000 /* ADD w/carry */
  287. #define HIFN_PUBOP_OP_SUB 0x000c0000 /* SUB */
  288. #define HIFN_PUBOP_OP_SUBC 0x00100000 /* SUB w/carry */
  289. #define HIFN_PUBOP_OP_MODADD 0x00140000 /* Modular ADD */
  290. #define HIFN_PUBOP_OP_MODSUB 0x00180000 /* Modular SUB */
  291. #define HIFN_PUBOP_OP_INCA 0x001c0000 /* INC A */
  292. #define HIFN_PUBOP_OP_DECA 0x00200000 /* DEC A */
  293. #define HIFN_PUBOP_OP_MULT 0x00240000 /* MULT */
  294. #define HIFN_PUBOP_OP_MODMULT 0x00280000 /* Modular MULT */
  295. #define HIFN_PUBOP_OP_MODRED 0x002c0000 /* Modular RED */
  296. #define HIFN_PUBOP_OP_MODEXP 0x00300000 /* Modular EXP */
  297. /* Public status register (HIFN_1_PUB_STATUS) */
  298. #define HIFN_PUBSTS_DONE 0x00000001 /* operation done */
  299. #define HIFN_PUBSTS_CARRY 0x00000002 /* carry */
  300. /* Public interrupt enable register (HIFN_1_PUB_IEN) */
  301. #define HIFN_PUBIEN_DONE 0x00000001 /* operation done interrupt */
  302. /* Random number generator config register (HIFN_1_RNG_CONFIG) */
  303. #define HIFN_RNGCFG_ENA 0x00000001 /* enable rng */
  304. #define HIFN_NAMESIZE 32
  305. #define HIFN_MAX_RESULT_ORDER 5
  306. #define HIFN_D_CMD_RSIZE (24 * 1)
  307. #define HIFN_D_SRC_RSIZE (80 * 1)
  308. #define HIFN_D_DST_RSIZE (80 * 1)
  309. #define HIFN_D_RES_RSIZE (24 * 1)
  310. #define HIFN_D_DST_DALIGN 4
  311. #define HIFN_QUEUE_LENGTH (HIFN_D_CMD_RSIZE - 1)
  312. #define AES_MIN_KEY_SIZE 16
  313. #define AES_MAX_KEY_SIZE 32
  314. #define HIFN_DES_KEY_LENGTH 8
  315. #define HIFN_3DES_KEY_LENGTH 24
  316. #define HIFN_MAX_CRYPT_KEY_LENGTH AES_MAX_KEY_SIZE
  317. #define HIFN_IV_LENGTH 8
  318. #define HIFN_AES_IV_LENGTH 16
  319. #define HIFN_MAX_IV_LENGTH HIFN_AES_IV_LENGTH
  320. #define HIFN_MAC_KEY_LENGTH 64
  321. #define HIFN_MD5_LENGTH 16
  322. #define HIFN_SHA1_LENGTH 20
  323. #define HIFN_MAC_TRUNC_LENGTH 12
  324. #define HIFN_MAX_COMMAND (8 + 8 + 8 + 64 + 260)
  325. #define HIFN_MAX_RESULT (8 + 4 + 4 + 20 + 4)
  326. #define HIFN_USED_RESULT 12
  327. struct hifn_desc {
  328. volatile __le32 l;
  329. volatile __le32 p;
  330. };
  331. struct hifn_dma {
  332. struct hifn_desc cmdr[HIFN_D_CMD_RSIZE + 1];
  333. struct hifn_desc srcr[HIFN_D_SRC_RSIZE + 1];
  334. struct hifn_desc dstr[HIFN_D_DST_RSIZE + 1];
  335. struct hifn_desc resr[HIFN_D_RES_RSIZE + 1];
  336. u8 command_bufs[HIFN_D_CMD_RSIZE][HIFN_MAX_COMMAND];
  337. u8 result_bufs[HIFN_D_CMD_RSIZE][HIFN_MAX_RESULT];
  338. /*
  339. * Our current positions for insertion and removal from the descriptor
  340. * rings.
  341. */
  342. volatile int cmdi, srci, dsti, resi;
  343. volatile int cmdu, srcu, dstu, resu;
  344. int cmdk, srck, dstk, resk;
  345. };
  346. #define HIFN_FLAG_CMD_BUSY (1 << 0)
  347. #define HIFN_FLAG_SRC_BUSY (1 << 1)
  348. #define HIFN_FLAG_DST_BUSY (1 << 2)
  349. #define HIFN_FLAG_RES_BUSY (1 << 3)
  350. #define HIFN_FLAG_OLD_KEY (1 << 4)
  351. #define HIFN_DEFAULT_ACTIVE_NUM 5
  352. struct hifn_device {
  353. char name[HIFN_NAMESIZE];
  354. int irq;
  355. struct pci_dev *pdev;
  356. void __iomem *bar[3];
  357. void *desc_virt;
  358. dma_addr_t desc_dma;
  359. u32 dmareg;
  360. void *sa[HIFN_D_RES_RSIZE];
  361. spinlock_t lock;
  362. u32 flags;
  363. int active, started;
  364. struct delayed_work work;
  365. unsigned long reset;
  366. unsigned long success;
  367. unsigned long prev_success;
  368. u8 snum;
  369. struct tasklet_struct tasklet;
  370. struct crypto_queue queue;
  371. struct list_head alg_list;
  372. unsigned int pk_clk_freq;
  373. #ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
  374. unsigned int rng_wait_time;
  375. ktime_t rngtime;
  376. struct hwrng rng;
  377. #endif
  378. };
  379. #define HIFN_D_LENGTH 0x0000ffff
  380. #define HIFN_D_NOINVALID 0x01000000
  381. #define HIFN_D_MASKDONEIRQ 0x02000000
  382. #define HIFN_D_DESTOVER 0x04000000
  383. #define HIFN_D_OVER 0x08000000
  384. #define HIFN_D_LAST 0x20000000
  385. #define HIFN_D_JUMP 0x40000000
  386. #define HIFN_D_VALID 0x80000000
  387. struct hifn_base_command {
  388. volatile __le16 masks;
  389. volatile __le16 session_num;
  390. volatile __le16 total_source_count;
  391. volatile __le16 total_dest_count;
  392. };
  393. #define HIFN_BASE_CMD_COMP 0x0100 /* enable compression engine */
  394. #define HIFN_BASE_CMD_PAD 0x0200 /* enable padding engine */
  395. #define HIFN_BASE_CMD_MAC 0x0400 /* enable MAC engine */
  396. #define HIFN_BASE_CMD_CRYPT 0x0800 /* enable crypt engine */
  397. #define HIFN_BASE_CMD_DECODE 0x2000
  398. #define HIFN_BASE_CMD_SRCLEN_M 0xc000
  399. #define HIFN_BASE_CMD_SRCLEN_S 14
  400. #define HIFN_BASE_CMD_DSTLEN_M 0x3000
  401. #define HIFN_BASE_CMD_DSTLEN_S 12
  402. #define HIFN_BASE_CMD_LENMASK_HI 0x30000
  403. #define HIFN_BASE_CMD_LENMASK_LO 0x0ffff
  404. /*
  405. * Structure to help build up the command data structure.
  406. */
  407. struct hifn_crypt_command {
  408. volatile __le16 masks;
  409. volatile __le16 header_skip;
  410. volatile __le16 source_count;
  411. volatile __le16 reserved;
  412. };
  413. #define HIFN_CRYPT_CMD_ALG_MASK 0x0003 /* algorithm: */
  414. #define HIFN_CRYPT_CMD_ALG_DES 0x0000 /* DES */
  415. #define HIFN_CRYPT_CMD_ALG_3DES 0x0001 /* 3DES */
  416. #define HIFN_CRYPT_CMD_ALG_RC4 0x0002 /* RC4 */
  417. #define HIFN_CRYPT_CMD_ALG_AES 0x0003 /* AES */
  418. #define HIFN_CRYPT_CMD_MODE_MASK 0x0018 /* Encrypt mode: */
  419. #define HIFN_CRYPT_CMD_MODE_ECB 0x0000 /* ECB */
  420. #define HIFN_CRYPT_CMD_MODE_CBC 0x0008 /* CBC */
  421. #define HIFN_CRYPT_CMD_MODE_CFB 0x0010 /* CFB */
  422. #define HIFN_CRYPT_CMD_MODE_OFB 0x0018 /* OFB */
  423. #define HIFN_CRYPT_CMD_CLR_CTX 0x0040 /* clear context */
  424. #define HIFN_CRYPT_CMD_KSZ_MASK 0x0600 /* AES key size: */
  425. #define HIFN_CRYPT_CMD_KSZ_128 0x0000 /* 128 bit */
  426. #define HIFN_CRYPT_CMD_KSZ_192 0x0200 /* 192 bit */
  427. #define HIFN_CRYPT_CMD_KSZ_256 0x0400 /* 256 bit */
  428. #define HIFN_CRYPT_CMD_NEW_KEY 0x0800 /* expect new key */
  429. #define HIFN_CRYPT_CMD_NEW_IV 0x1000 /* expect new iv */
  430. #define HIFN_CRYPT_CMD_SRCLEN_M 0xc000
  431. #define HIFN_CRYPT_CMD_SRCLEN_S 14
  432. #define HIFN_MAC_CMD_ALG_MASK 0x0001
  433. #define HIFN_MAC_CMD_ALG_SHA1 0x0000
  434. #define HIFN_MAC_CMD_ALG_MD5 0x0001
  435. #define HIFN_MAC_CMD_MODE_MASK 0x000c
  436. #define HIFN_MAC_CMD_MODE_HMAC 0x0000
  437. #define HIFN_MAC_CMD_MODE_SSL_MAC 0x0004
  438. #define HIFN_MAC_CMD_MODE_HASH 0x0008
  439. #define HIFN_MAC_CMD_MODE_FULL 0x0004
  440. #define HIFN_MAC_CMD_TRUNC 0x0010
  441. #define HIFN_MAC_CMD_RESULT 0x0020
  442. #define HIFN_MAC_CMD_APPEND 0x0040
  443. #define HIFN_MAC_CMD_SRCLEN_M 0xc000
  444. #define HIFN_MAC_CMD_SRCLEN_S 14
  445. /*
  446. * MAC POS IPsec initiates authentication after encryption on encodes
  447. * and before decryption on decodes.
  448. */
  449. #define HIFN_MAC_CMD_POS_IPSEC 0x0200
  450. #define HIFN_MAC_CMD_NEW_KEY 0x0800
  451. #define HIFN_COMP_CMD_SRCLEN_M 0xc000
  452. #define HIFN_COMP_CMD_SRCLEN_S 14
  453. #define HIFN_COMP_CMD_ONE 0x0100 /* must be one */
  454. #define HIFN_COMP_CMD_CLEARHIST 0x0010 /* clear history */
  455. #define HIFN_COMP_CMD_UPDATEHIST 0x0008 /* update history */
  456. #define HIFN_COMP_CMD_LZS_STRIP0 0x0004 /* LZS: strip zero */
  457. #define HIFN_COMP_CMD_MPPC_RESTART 0x0004 /* MPPC: restart */
  458. #define HIFN_COMP_CMD_ALG_MASK 0x0001 /* compression mode: */
  459. #define HIFN_COMP_CMD_ALG_MPPC 0x0001 /* MPPC */
  460. #define HIFN_COMP_CMD_ALG_LZS 0x0000 /* LZS */
  461. struct hifn_base_result {
  462. volatile __le16 flags;
  463. volatile __le16 session;
  464. volatile __le16 src_cnt; /* 15:0 of source count */
  465. volatile __le16 dst_cnt; /* 15:0 of dest count */
  466. };
  467. #define HIFN_BASE_RES_DSTOVERRUN 0x0200 /* destination overrun */
  468. #define HIFN_BASE_RES_SRCLEN_M 0xc000 /* 17:16 of source count */
  469. #define HIFN_BASE_RES_SRCLEN_S 14
  470. #define HIFN_BASE_RES_DSTLEN_M 0x3000 /* 17:16 of dest count */
  471. #define HIFN_BASE_RES_DSTLEN_S 12
  472. struct hifn_comp_result {
  473. volatile __le16 flags;
  474. volatile __le16 crc;
  475. };
  476. #define HIFN_COMP_RES_LCB_M 0xff00 /* longitudinal check byte */
  477. #define HIFN_COMP_RES_LCB_S 8
  478. #define HIFN_COMP_RES_RESTART 0x0004 /* MPPC: restart */
  479. #define HIFN_COMP_RES_ENDMARKER 0x0002 /* LZS: end marker seen */
  480. #define HIFN_COMP_RES_SRC_NOTZERO 0x0001 /* source expired */
  481. struct hifn_mac_result {
  482. volatile __le16 flags;
  483. volatile __le16 reserved;
  484. /* followed by 0, 6, 8, or 10 u16's of the MAC, then crypt */
  485. };
  486. #define HIFN_MAC_RES_MISCOMPARE 0x0002 /* compare failed */
  487. #define HIFN_MAC_RES_SRC_NOTZERO 0x0001 /* source expired */
  488. struct hifn_crypt_result {
  489. volatile __le16 flags;
  490. volatile __le16 reserved;
  491. };
  492. #define HIFN_CRYPT_RES_SRC_NOTZERO 0x0001 /* source expired */
  493. #ifndef HIFN_POLL_FREQUENCY
  494. #define HIFN_POLL_FREQUENCY 0x1
  495. #endif
  496. #ifndef HIFN_POLL_SCALAR
  497. #define HIFN_POLL_SCALAR 0x0
  498. #endif
  499. #define HIFN_MAX_SEGLEN 0xffff /* maximum dma segment len */
  500. #define HIFN_MAX_DMALEN 0x3ffff /* maximum dma length */
  501. struct hifn_crypto_alg {
  502. struct list_head entry;
  503. struct skcipher_alg alg;
  504. struct hifn_device *dev;
  505. };
  506. #define ASYNC_SCATTERLIST_CACHE 16
  507. #define ASYNC_FLAGS_MISALIGNED (1 << 0)
  508. struct hifn_cipher_walk {
  509. struct scatterlist cache[ASYNC_SCATTERLIST_CACHE];
  510. u32 flags;
  511. int num;
  512. };
  513. struct hifn_context {
  514. u8 key[HIFN_MAX_CRYPT_KEY_LENGTH];
  515. struct hifn_device *dev;
  516. unsigned int keysize;
  517. };
  518. struct hifn_request_context {
  519. u8 *iv;
  520. unsigned int ivsize;
  521. u8 op, type, mode, unused;
  522. struct hifn_cipher_walk walk;
  523. };
  524. #define crypto_alg_to_hifn(a) container_of(a, struct hifn_crypto_alg, alg)
  525. static inline u32 hifn_read_0(struct hifn_device *dev, u32 reg)
  526. {
  527. return readl(dev->bar[0] + reg);
  528. }
  529. static inline u32 hifn_read_1(struct hifn_device *dev, u32 reg)
  530. {
  531. return readl(dev->bar[1] + reg);
  532. }
  533. static inline void hifn_write_0(struct hifn_device *dev, u32 reg, u32 val)
  534. {
  535. writel((__force u32)cpu_to_le32(val), dev->bar[0] + reg);
  536. }
  537. static inline void hifn_write_1(struct hifn_device *dev, u32 reg, u32 val)
  538. {
  539. writel((__force u32)cpu_to_le32(val), dev->bar[1] + reg);
  540. }
  541. static void hifn_wait_puc(struct hifn_device *dev)
  542. {
  543. int i;
  544. u32 ret;
  545. for (i = 10000; i > 0; --i) {
  546. ret = hifn_read_0(dev, HIFN_0_PUCTRL);
  547. if (!(ret & HIFN_PUCTRL_RESET))
  548. break;
  549. udelay(1);
  550. }
  551. if (!i)
  552. dev_err(&dev->pdev->dev, "Failed to reset PUC unit.\n");
  553. }
  554. static void hifn_reset_puc(struct hifn_device *dev)
  555. {
  556. hifn_write_0(dev, HIFN_0_PUCTRL, HIFN_PUCTRL_DMAENA);
  557. hifn_wait_puc(dev);
  558. }
  559. static void hifn_stop_device(struct hifn_device *dev)
  560. {
  561. hifn_write_1(dev, HIFN_1_DMA_CSR,
  562. HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS |
  563. HIFN_DMACSR_S_CTRL_DIS | HIFN_DMACSR_C_CTRL_DIS);
  564. hifn_write_0(dev, HIFN_0_PUIER, 0);
  565. hifn_write_1(dev, HIFN_1_DMA_IER, 0);
  566. }
  567. static void hifn_reset_dma(struct hifn_device *dev, int full)
  568. {
  569. hifn_stop_device(dev);
  570. /*
  571. * Setting poll frequency and others to 0.
  572. */
  573. hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
  574. HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
  575. mdelay(1);
  576. /*
  577. * Reset DMA.
  578. */
  579. if (full) {
  580. hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MODE);
  581. mdelay(1);
  582. } else {
  583. hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MODE |
  584. HIFN_DMACNFG_MSTRESET);
  585. hifn_reset_puc(dev);
  586. }
  587. hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
  588. HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
  589. hifn_reset_puc(dev);
  590. }
  591. static u32 hifn_next_signature(u32 a, u_int cnt)
  592. {
  593. int i;
  594. u32 v;
  595. for (i = 0; i < cnt; i++) {
  596. /* get the parity */
  597. v = a & 0x80080125;
  598. v ^= v >> 16;
  599. v ^= v >> 8;
  600. v ^= v >> 4;
  601. v ^= v >> 2;
  602. v ^= v >> 1;
  603. a = (v & 1) ^ (a << 1);
  604. }
  605. return a;
  606. }
  607. static struct pci2id {
  608. u_short pci_vendor;
  609. u_short pci_prod;
  610. char card_id[13];
  611. } pci2id[] = {
  612. {
  613. PCI_VENDOR_ID_HIFN,
  614. PCI_DEVICE_ID_HIFN_7955,
  615. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  616. 0x00, 0x00, 0x00, 0x00, 0x00 }
  617. },
  618. {
  619. PCI_VENDOR_ID_HIFN,
  620. PCI_DEVICE_ID_HIFN_7956,
  621. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  622. 0x00, 0x00, 0x00, 0x00, 0x00 }
  623. }
  624. };
  625. #ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
  626. static int hifn_rng_data_present(struct hwrng *rng, int wait)
  627. {
  628. struct hifn_device *dev = (struct hifn_device *)rng->priv;
  629. s64 nsec;
  630. nsec = ktime_to_ns(ktime_sub(ktime_get(), dev->rngtime));
  631. nsec -= dev->rng_wait_time;
  632. if (nsec <= 0)
  633. return 1;
  634. if (!wait)
  635. return 0;
  636. ndelay(nsec);
  637. return 1;
  638. }
  639. static int hifn_rng_data_read(struct hwrng *rng, u32 *data)
  640. {
  641. struct hifn_device *dev = (struct hifn_device *)rng->priv;
  642. *data = hifn_read_1(dev, HIFN_1_RNG_DATA);
  643. dev->rngtime = ktime_get();
  644. return 4;
  645. }
  646. static int hifn_register_rng(struct hifn_device *dev)
  647. {
  648. /*
  649. * We must wait at least 256 Pk_clk cycles between two reads of the rng.
  650. */
  651. dev->rng_wait_time = DIV_ROUND_UP_ULL(NSEC_PER_SEC,
  652. dev->pk_clk_freq) * 256;
  653. dev->rng.name = dev->name;
  654. dev->rng.data_present = hifn_rng_data_present;
  655. dev->rng.data_read = hifn_rng_data_read;
  656. dev->rng.priv = (unsigned long)dev;
  657. return hwrng_register(&dev->rng);
  658. }
  659. static void hifn_unregister_rng(struct hifn_device *dev)
  660. {
  661. hwrng_unregister(&dev->rng);
  662. }
  663. #else
  664. #define hifn_register_rng(dev) 0
  665. #define hifn_unregister_rng(dev)
  666. #endif
  667. static int hifn_init_pubrng(struct hifn_device *dev)
  668. {
  669. int i;
  670. hifn_write_1(dev, HIFN_1_PUB_RESET, hifn_read_1(dev, HIFN_1_PUB_RESET) |
  671. HIFN_PUBRST_RESET);
  672. for (i = 100; i > 0; --i) {
  673. mdelay(1);
  674. if ((hifn_read_1(dev, HIFN_1_PUB_RESET) & HIFN_PUBRST_RESET) == 0)
  675. break;
  676. }
  677. if (!i) {
  678. dev_err(&dev->pdev->dev, "Failed to initialise public key engine.\n");
  679. } else {
  680. hifn_write_1(dev, HIFN_1_PUB_IEN, HIFN_PUBIEN_DONE);
  681. dev->dmareg |= HIFN_DMAIER_PUBDONE;
  682. hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
  683. dev_dbg(&dev->pdev->dev, "Public key engine has been successfully initialised.\n");
  684. }
  685. /* Enable RNG engine. */
  686. hifn_write_1(dev, HIFN_1_RNG_CONFIG,
  687. hifn_read_1(dev, HIFN_1_RNG_CONFIG) | HIFN_RNGCFG_ENA);
  688. dev_dbg(&dev->pdev->dev, "RNG engine has been successfully initialised.\n");
  689. #ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
  690. /* First value must be discarded */
  691. hifn_read_1(dev, HIFN_1_RNG_DATA);
  692. dev->rngtime = ktime_get();
  693. #endif
  694. return 0;
  695. }
  696. static int hifn_enable_crypto(struct hifn_device *dev)
  697. {
  698. u32 dmacfg, addr;
  699. char *offtbl = NULL;
  700. int i;
  701. for (i = 0; i < ARRAY_SIZE(pci2id); i++) {
  702. if (pci2id[i].pci_vendor == dev->pdev->vendor &&
  703. pci2id[i].pci_prod == dev->pdev->device) {
  704. offtbl = pci2id[i].card_id;
  705. break;
  706. }
  707. }
  708. if (!offtbl) {
  709. dev_err(&dev->pdev->dev, "Unknown card!\n");
  710. return -ENODEV;
  711. }
  712. dmacfg = hifn_read_1(dev, HIFN_1_DMA_CNFG);
  713. hifn_write_1(dev, HIFN_1_DMA_CNFG,
  714. HIFN_DMACNFG_UNLOCK | HIFN_DMACNFG_MSTRESET |
  715. HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
  716. mdelay(1);
  717. addr = hifn_read_1(dev, HIFN_1_UNLOCK_SECRET1);
  718. mdelay(1);
  719. hifn_write_1(dev, HIFN_1_UNLOCK_SECRET2, 0);
  720. mdelay(1);
  721. for (i = 0; i < 12; ++i) {
  722. addr = hifn_next_signature(addr, offtbl[i] + 0x101);
  723. hifn_write_1(dev, HIFN_1_UNLOCK_SECRET2, addr);
  724. mdelay(1);
  725. }
  726. hifn_write_1(dev, HIFN_1_DMA_CNFG, dmacfg);
  727. dev_dbg(&dev->pdev->dev, "%s %s.\n", dev->name, pci_name(dev->pdev));
  728. return 0;
  729. }
  730. static void hifn_init_dma(struct hifn_device *dev)
  731. {
  732. struct hifn_dma *dma = dev->desc_virt;
  733. u32 dptr = dev->desc_dma;
  734. int i;
  735. for (i = 0; i < HIFN_D_CMD_RSIZE; ++i)
  736. dma->cmdr[i].p = __cpu_to_le32(dptr +
  737. offsetof(struct hifn_dma, command_bufs[i][0]));
  738. for (i = 0; i < HIFN_D_RES_RSIZE; ++i)
  739. dma->resr[i].p = __cpu_to_le32(dptr +
  740. offsetof(struct hifn_dma, result_bufs[i][0]));
  741. /* Setup LAST descriptors. */
  742. dma->cmdr[HIFN_D_CMD_RSIZE].p = __cpu_to_le32(dptr +
  743. offsetof(struct hifn_dma, cmdr[0]));
  744. dma->srcr[HIFN_D_SRC_RSIZE].p = __cpu_to_le32(dptr +
  745. offsetof(struct hifn_dma, srcr[0]));
  746. dma->dstr[HIFN_D_DST_RSIZE].p = __cpu_to_le32(dptr +
  747. offsetof(struct hifn_dma, dstr[0]));
  748. dma->resr[HIFN_D_RES_RSIZE].p = __cpu_to_le32(dptr +
  749. offsetof(struct hifn_dma, resr[0]));
  750. dma->cmdu = dma->srcu = dma->dstu = dma->resu = 0;
  751. dma->cmdi = dma->srci = dma->dsti = dma->resi = 0;
  752. dma->cmdk = dma->srck = dma->dstk = dma->resk = 0;
  753. }
  754. /*
  755. * Initialize the PLL. We need to know the frequency of the reference clock
  756. * to calculate the optimal multiplier. For PCI we assume 66MHz, since that
  757. * allows us to operate without the risk of overclocking the chip. If it
  758. * actually uses 33MHz, the chip will operate at half the speed, this can be
  759. * overridden by specifying the frequency as module parameter (pci33).
  760. *
  761. * Unfortunately the PCI clock is not very suitable since the HIFN needs a
  762. * stable clock and the PCI clock frequency may vary, so the default is the
  763. * external clock. There is no way to find out its frequency, we default to
  764. * 66MHz since according to Mike Ham of HiFn, almost every board in existence
  765. * has an external crystal populated at 66MHz.
  766. */
  767. static void hifn_init_pll(struct hifn_device *dev)
  768. {
  769. unsigned int freq, m;
  770. u32 pllcfg;
  771. pllcfg = HIFN_1_PLL | HIFN_PLL_RESERVED_1;
  772. if (strncmp(hifn_pll_ref, "ext", 3) == 0)
  773. pllcfg |= HIFN_PLL_REF_CLK_PLL;
  774. else
  775. pllcfg |= HIFN_PLL_REF_CLK_HBI;
  776. if (hifn_pll_ref[3] == '\0' ||
  777. kstrtouint(hifn_pll_ref + 3, 10, &freq)) {
  778. freq = 66;
  779. dev_info(&dev->pdev->dev, "assuming %u MHz clock speed, override with hifn_pll_ref=%.3s<frequency>\n",
  780. freq, hifn_pll_ref);
  781. }
  782. m = HIFN_PLL_FCK_MAX / freq;
  783. pllcfg |= (m / 2 - 1) << HIFN_PLL_ND_SHIFT;
  784. if (m <= 8)
  785. pllcfg |= HIFN_PLL_IS_1_8;
  786. else
  787. pllcfg |= HIFN_PLL_IS_9_12;
  788. /* Select clock source and enable clock bypass */
  789. hifn_write_1(dev, HIFN_1_PLL, pllcfg |
  790. HIFN_PLL_PK_CLK_HBI | HIFN_PLL_PE_CLK_HBI | HIFN_PLL_BP);
  791. /* Let the chip lock to the input clock */
  792. mdelay(10);
  793. /* Disable clock bypass */
  794. hifn_write_1(dev, HIFN_1_PLL, pllcfg |
  795. HIFN_PLL_PK_CLK_HBI | HIFN_PLL_PE_CLK_HBI);
  796. /* Switch the engines to the PLL */
  797. hifn_write_1(dev, HIFN_1_PLL, pllcfg |
  798. HIFN_PLL_PK_CLK_PLL | HIFN_PLL_PE_CLK_PLL);
  799. /*
  800. * The Fpk_clk runs at half the total speed. Its frequency is needed to
  801. * calculate the minimum time between two reads of the rng. Since 33MHz
  802. * is actually 33.333... we overestimate the frequency here, resulting
  803. * in slightly larger intervals.
  804. */
  805. dev->pk_clk_freq = 1000000 * (freq + 1) * m / 2;
  806. }
  807. static void hifn_init_registers(struct hifn_device *dev)
  808. {
  809. u32 dptr = dev->desc_dma;
  810. /* Initialization magic... */
  811. hifn_write_0(dev, HIFN_0_PUCTRL, HIFN_PUCTRL_DMAENA);
  812. hifn_write_0(dev, HIFN_0_FIFOCNFG, HIFN_FIFOCNFG_THRESHOLD);
  813. hifn_write_0(dev, HIFN_0_PUIER, HIFN_PUIER_DSTOVER);
  814. /* write all 4 ring address registers */
  815. hifn_write_1(dev, HIFN_1_DMA_CRAR, dptr +
  816. offsetof(struct hifn_dma, cmdr[0]));
  817. hifn_write_1(dev, HIFN_1_DMA_SRAR, dptr +
  818. offsetof(struct hifn_dma, srcr[0]));
  819. hifn_write_1(dev, HIFN_1_DMA_DRAR, dptr +
  820. offsetof(struct hifn_dma, dstr[0]));
  821. hifn_write_1(dev, HIFN_1_DMA_RRAR, dptr +
  822. offsetof(struct hifn_dma, resr[0]));
  823. mdelay(2);
  824. #if 0
  825. hifn_write_1(dev, HIFN_1_DMA_CSR,
  826. HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS |
  827. HIFN_DMACSR_S_CTRL_DIS | HIFN_DMACSR_C_CTRL_DIS |
  828. HIFN_DMACSR_D_ABORT | HIFN_DMACSR_D_DONE | HIFN_DMACSR_D_LAST |
  829. HIFN_DMACSR_D_WAIT | HIFN_DMACSR_D_OVER |
  830. HIFN_DMACSR_R_ABORT | HIFN_DMACSR_R_DONE | HIFN_DMACSR_R_LAST |
  831. HIFN_DMACSR_R_WAIT | HIFN_DMACSR_R_OVER |
  832. HIFN_DMACSR_S_ABORT | HIFN_DMACSR_S_DONE | HIFN_DMACSR_S_LAST |
  833. HIFN_DMACSR_S_WAIT |
  834. HIFN_DMACSR_C_ABORT | HIFN_DMACSR_C_DONE | HIFN_DMACSR_C_LAST |
  835. HIFN_DMACSR_C_WAIT |
  836. HIFN_DMACSR_ENGINE |
  837. HIFN_DMACSR_PUBDONE);
  838. #else
  839. hifn_write_1(dev, HIFN_1_DMA_CSR,
  840. HIFN_DMACSR_C_CTRL_ENA | HIFN_DMACSR_S_CTRL_ENA |
  841. HIFN_DMACSR_D_CTRL_ENA | HIFN_DMACSR_R_CTRL_ENA |
  842. HIFN_DMACSR_D_ABORT | HIFN_DMACSR_D_DONE | HIFN_DMACSR_D_LAST |
  843. HIFN_DMACSR_D_WAIT | HIFN_DMACSR_D_OVER |
  844. HIFN_DMACSR_R_ABORT | HIFN_DMACSR_R_DONE | HIFN_DMACSR_R_LAST |
  845. HIFN_DMACSR_R_WAIT | HIFN_DMACSR_R_OVER |
  846. HIFN_DMACSR_S_ABORT | HIFN_DMACSR_S_DONE | HIFN_DMACSR_S_LAST |
  847. HIFN_DMACSR_S_WAIT |
  848. HIFN_DMACSR_C_ABORT | HIFN_DMACSR_C_DONE | HIFN_DMACSR_C_LAST |
  849. HIFN_DMACSR_C_WAIT |
  850. HIFN_DMACSR_ENGINE |
  851. HIFN_DMACSR_PUBDONE);
  852. #endif
  853. hifn_read_1(dev, HIFN_1_DMA_CSR);
  854. dev->dmareg |= HIFN_DMAIER_R_DONE | HIFN_DMAIER_C_ABORT |
  855. HIFN_DMAIER_D_OVER | HIFN_DMAIER_R_OVER |
  856. HIFN_DMAIER_S_ABORT | HIFN_DMAIER_D_ABORT | HIFN_DMAIER_R_ABORT |
  857. HIFN_DMAIER_ENGINE;
  858. dev->dmareg &= ~HIFN_DMAIER_C_WAIT;
  859. hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
  860. hifn_read_1(dev, HIFN_1_DMA_IER);
  861. #if 0
  862. hifn_write_0(dev, HIFN_0_PUCNFG, HIFN_PUCNFG_ENCCNFG |
  863. HIFN_PUCNFG_DRFR_128 | HIFN_PUCNFG_TCALLPHASES |
  864. HIFN_PUCNFG_TCDRVTOTEM | HIFN_PUCNFG_BUS32 |
  865. HIFN_PUCNFG_DRAM);
  866. #else
  867. hifn_write_0(dev, HIFN_0_PUCNFG, 0x10342);
  868. #endif
  869. hifn_init_pll(dev);
  870. hifn_write_0(dev, HIFN_0_PUISR, HIFN_PUISR_DSTOVER);
  871. hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
  872. HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE | HIFN_DMACNFG_LAST |
  873. ((HIFN_POLL_FREQUENCY << 16 ) & HIFN_DMACNFG_POLLFREQ) |
  874. ((HIFN_POLL_SCALAR << 8) & HIFN_DMACNFG_POLLINVAL));
  875. }
  876. static int hifn_setup_base_command(struct hifn_device *dev, u8 *buf,
  877. unsigned dlen, unsigned slen, u16 mask, u8 snum)
  878. {
  879. struct hifn_base_command *base_cmd;
  880. u8 *buf_pos = buf;
  881. base_cmd = (struct hifn_base_command *)buf_pos;
  882. base_cmd->masks = __cpu_to_le16(mask);
  883. base_cmd->total_source_count =
  884. __cpu_to_le16(slen & HIFN_BASE_CMD_LENMASK_LO);
  885. base_cmd->total_dest_count =
  886. __cpu_to_le16(dlen & HIFN_BASE_CMD_LENMASK_LO);
  887. dlen >>= 16;
  888. slen >>= 16;
  889. base_cmd->session_num = __cpu_to_le16(snum |
  890. ((slen << HIFN_BASE_CMD_SRCLEN_S) & HIFN_BASE_CMD_SRCLEN_M) |
  891. ((dlen << HIFN_BASE_CMD_DSTLEN_S) & HIFN_BASE_CMD_DSTLEN_M));
  892. return sizeof(struct hifn_base_command);
  893. }
  894. static int hifn_setup_crypto_command(struct hifn_device *dev,
  895. u8 *buf, unsigned dlen, unsigned slen,
  896. u8 *key, int keylen, u8 *iv, int ivsize, u16 mode)
  897. {
  898. struct hifn_dma *dma = dev->desc_virt;
  899. struct hifn_crypt_command *cry_cmd;
  900. u8 *buf_pos = buf;
  901. u16 cmd_len;
  902. cry_cmd = (struct hifn_crypt_command *)buf_pos;
  903. cry_cmd->source_count = __cpu_to_le16(dlen & 0xffff);
  904. dlen >>= 16;
  905. cry_cmd->masks = __cpu_to_le16(mode |
  906. ((dlen << HIFN_CRYPT_CMD_SRCLEN_S) &
  907. HIFN_CRYPT_CMD_SRCLEN_M));
  908. cry_cmd->header_skip = 0;
  909. cry_cmd->reserved = 0;
  910. buf_pos += sizeof(struct hifn_crypt_command);
  911. dma->cmdu++;
  912. if (dma->cmdu > 1) {
  913. dev->dmareg |= HIFN_DMAIER_C_WAIT;
  914. hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
  915. }
  916. if (keylen) {
  917. memcpy(buf_pos, key, keylen);
  918. buf_pos += keylen;
  919. }
  920. if (ivsize) {
  921. memcpy(buf_pos, iv, ivsize);
  922. buf_pos += ivsize;
  923. }
  924. cmd_len = buf_pos - buf;
  925. return cmd_len;
  926. }
  927. static int hifn_setup_cmd_desc(struct hifn_device *dev,
  928. struct hifn_context *ctx, struct hifn_request_context *rctx,
  929. void *priv, unsigned int nbytes)
  930. {
  931. struct hifn_dma *dma = dev->desc_virt;
  932. int cmd_len, sa_idx;
  933. u8 *buf, *buf_pos;
  934. u16 mask;
  935. sa_idx = dma->cmdi;
  936. buf_pos = buf = dma->command_bufs[dma->cmdi];
  937. mask = 0;
  938. switch (rctx->op) {
  939. case ACRYPTO_OP_DECRYPT:
  940. mask = HIFN_BASE_CMD_CRYPT | HIFN_BASE_CMD_DECODE;
  941. break;
  942. case ACRYPTO_OP_ENCRYPT:
  943. mask = HIFN_BASE_CMD_CRYPT;
  944. break;
  945. case ACRYPTO_OP_HMAC:
  946. mask = HIFN_BASE_CMD_MAC;
  947. break;
  948. default:
  949. goto err_out;
  950. }
  951. buf_pos += hifn_setup_base_command(dev, buf_pos, nbytes,
  952. nbytes, mask, dev->snum);
  953. if (rctx->op == ACRYPTO_OP_ENCRYPT || rctx->op == ACRYPTO_OP_DECRYPT) {
  954. u16 md = 0;
  955. if (ctx->keysize)
  956. md |= HIFN_CRYPT_CMD_NEW_KEY;
  957. if (rctx->iv && rctx->mode != ACRYPTO_MODE_ECB)
  958. md |= HIFN_CRYPT_CMD_NEW_IV;
  959. switch (rctx->mode) {
  960. case ACRYPTO_MODE_ECB:
  961. md |= HIFN_CRYPT_CMD_MODE_ECB;
  962. break;
  963. case ACRYPTO_MODE_CBC:
  964. md |= HIFN_CRYPT_CMD_MODE_CBC;
  965. break;
  966. case ACRYPTO_MODE_CFB:
  967. md |= HIFN_CRYPT_CMD_MODE_CFB;
  968. break;
  969. case ACRYPTO_MODE_OFB:
  970. md |= HIFN_CRYPT_CMD_MODE_OFB;
  971. break;
  972. default:
  973. goto err_out;
  974. }
  975. switch (rctx->type) {
  976. case ACRYPTO_TYPE_AES_128:
  977. if (ctx->keysize != 16)
  978. goto err_out;
  979. md |= HIFN_CRYPT_CMD_KSZ_128 |
  980. HIFN_CRYPT_CMD_ALG_AES;
  981. break;
  982. case ACRYPTO_TYPE_AES_192:
  983. if (ctx->keysize != 24)
  984. goto err_out;
  985. md |= HIFN_CRYPT_CMD_KSZ_192 |
  986. HIFN_CRYPT_CMD_ALG_AES;
  987. break;
  988. case ACRYPTO_TYPE_AES_256:
  989. if (ctx->keysize != 32)
  990. goto err_out;
  991. md |= HIFN_CRYPT_CMD_KSZ_256 |
  992. HIFN_CRYPT_CMD_ALG_AES;
  993. break;
  994. case ACRYPTO_TYPE_3DES:
  995. if (ctx->keysize != 24)
  996. goto err_out;
  997. md |= HIFN_CRYPT_CMD_ALG_3DES;
  998. break;
  999. case ACRYPTO_TYPE_DES:
  1000. if (ctx->keysize != 8)
  1001. goto err_out;
  1002. md |= HIFN_CRYPT_CMD_ALG_DES;
  1003. break;
  1004. default:
  1005. goto err_out;
  1006. }
  1007. buf_pos += hifn_setup_crypto_command(dev, buf_pos,
  1008. nbytes, nbytes, ctx->key, ctx->keysize,
  1009. rctx->iv, rctx->ivsize, md);
  1010. }
  1011. dev->sa[sa_idx] = priv;
  1012. dev->started++;
  1013. cmd_len = buf_pos - buf;
  1014. dma->cmdr[dma->cmdi].l = __cpu_to_le32(cmd_len | HIFN_D_VALID |
  1015. HIFN_D_LAST | HIFN_D_MASKDONEIRQ);
  1016. if (++dma->cmdi == HIFN_D_CMD_RSIZE) {
  1017. dma->cmdr[dma->cmdi].l = __cpu_to_le32(
  1018. HIFN_D_VALID | HIFN_D_LAST |
  1019. HIFN_D_MASKDONEIRQ | HIFN_D_JUMP);
  1020. dma->cmdi = 0;
  1021. } else {
  1022. dma->cmdr[dma->cmdi - 1].l |= __cpu_to_le32(HIFN_D_VALID);
  1023. }
  1024. if (!(dev->flags & HIFN_FLAG_CMD_BUSY)) {
  1025. hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_C_CTRL_ENA);
  1026. dev->flags |= HIFN_FLAG_CMD_BUSY;
  1027. }
  1028. return 0;
  1029. err_out:
  1030. return -EINVAL;
  1031. }
  1032. static int hifn_setup_src_desc(struct hifn_device *dev, struct page *page,
  1033. unsigned int offset, unsigned int size, int last)
  1034. {
  1035. struct hifn_dma *dma = dev->desc_virt;
  1036. int idx;
  1037. dma_addr_t addr;
  1038. addr = dma_map_page(&dev->pdev->dev, page, offset, size,
  1039. DMA_TO_DEVICE);
  1040. idx = dma->srci;
  1041. dma->srcr[idx].p = __cpu_to_le32(addr);
  1042. dma->srcr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
  1043. HIFN_D_MASKDONEIRQ | (last ? HIFN_D_LAST : 0));
  1044. if (++idx == HIFN_D_SRC_RSIZE) {
  1045. dma->srcr[idx].l = __cpu_to_le32(HIFN_D_VALID |
  1046. HIFN_D_JUMP | HIFN_D_MASKDONEIRQ |
  1047. (last ? HIFN_D_LAST : 0));
  1048. idx = 0;
  1049. }
  1050. dma->srci = idx;
  1051. dma->srcu++;
  1052. if (!(dev->flags & HIFN_FLAG_SRC_BUSY)) {
  1053. hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_S_CTRL_ENA);
  1054. dev->flags |= HIFN_FLAG_SRC_BUSY;
  1055. }
  1056. return size;
  1057. }
  1058. static void hifn_setup_res_desc(struct hifn_device *dev)
  1059. {
  1060. struct hifn_dma *dma = dev->desc_virt;
  1061. dma->resr[dma->resi].l = __cpu_to_le32(HIFN_USED_RESULT |
  1062. HIFN_D_VALID | HIFN_D_LAST);
  1063. /*
  1064. * dma->resr[dma->resi].l = __cpu_to_le32(HIFN_MAX_RESULT | HIFN_D_VALID |
  1065. * HIFN_D_LAST);
  1066. */
  1067. if (++dma->resi == HIFN_D_RES_RSIZE) {
  1068. dma->resr[HIFN_D_RES_RSIZE].l = __cpu_to_le32(HIFN_D_VALID |
  1069. HIFN_D_JUMP | HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
  1070. dma->resi = 0;
  1071. }
  1072. dma->resu++;
  1073. if (!(dev->flags & HIFN_FLAG_RES_BUSY)) {
  1074. hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_R_CTRL_ENA);
  1075. dev->flags |= HIFN_FLAG_RES_BUSY;
  1076. }
  1077. }
  1078. static void hifn_setup_dst_desc(struct hifn_device *dev, struct page *page,
  1079. unsigned offset, unsigned size, int last)
  1080. {
  1081. struct hifn_dma *dma = dev->desc_virt;
  1082. int idx;
  1083. dma_addr_t addr;
  1084. addr = dma_map_page(&dev->pdev->dev, page, offset, size,
  1085. DMA_FROM_DEVICE);
  1086. idx = dma->dsti;
  1087. dma->dstr[idx].p = __cpu_to_le32(addr);
  1088. dma->dstr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
  1089. HIFN_D_MASKDONEIRQ | (last ? HIFN_D_LAST : 0));
  1090. if (++idx == HIFN_D_DST_RSIZE) {
  1091. dma->dstr[idx].l = __cpu_to_le32(HIFN_D_VALID |
  1092. HIFN_D_JUMP | HIFN_D_MASKDONEIRQ |
  1093. (last ? HIFN_D_LAST : 0));
  1094. idx = 0;
  1095. }
  1096. dma->dsti = idx;
  1097. dma->dstu++;
  1098. if (!(dev->flags & HIFN_FLAG_DST_BUSY)) {
  1099. hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_D_CTRL_ENA);
  1100. dev->flags |= HIFN_FLAG_DST_BUSY;
  1101. }
  1102. }
  1103. static int hifn_setup_dma(struct hifn_device *dev,
  1104. struct hifn_context *ctx, struct hifn_request_context *rctx,
  1105. struct scatterlist *src, struct scatterlist *dst,
  1106. unsigned int nbytes, void *priv)
  1107. {
  1108. struct scatterlist *t;
  1109. struct page *spage, *dpage;
  1110. unsigned int soff, doff;
  1111. unsigned int n, len;
  1112. n = nbytes;
  1113. while (n) {
  1114. spage = sg_page(src);
  1115. soff = src->offset;
  1116. len = min(src->length, n);
  1117. hifn_setup_src_desc(dev, spage, soff, len, n - len == 0);
  1118. src++;
  1119. n -= len;
  1120. }
  1121. t = &rctx->walk.cache[0];
  1122. n = nbytes;
  1123. while (n) {
  1124. if (t->length && rctx->walk.flags & ASYNC_FLAGS_MISALIGNED) {
  1125. BUG_ON(!sg_page(t));
  1126. dpage = sg_page(t);
  1127. doff = 0;
  1128. len = t->length;
  1129. } else {
  1130. BUG_ON(!sg_page(dst));
  1131. dpage = sg_page(dst);
  1132. doff = dst->offset;
  1133. len = dst->length;
  1134. }
  1135. len = min(len, n);
  1136. hifn_setup_dst_desc(dev, dpage, doff, len, n - len == 0);
  1137. dst++;
  1138. t++;
  1139. n -= len;
  1140. }
  1141. hifn_setup_cmd_desc(dev, ctx, rctx, priv, nbytes);
  1142. hifn_setup_res_desc(dev);
  1143. return 0;
  1144. }
  1145. static int hifn_cipher_walk_init(struct hifn_cipher_walk *w,
  1146. int num, gfp_t gfp_flags)
  1147. {
  1148. int i;
  1149. num = min(ASYNC_SCATTERLIST_CACHE, num);
  1150. sg_init_table(w->cache, num);
  1151. w->num = 0;
  1152. for (i = 0; i < num; ++i) {
  1153. struct page *page = alloc_page(gfp_flags);
  1154. struct scatterlist *s;
  1155. if (!page)
  1156. break;
  1157. s = &w->cache[i];
  1158. sg_set_page(s, page, PAGE_SIZE, 0);
  1159. w->num++;
  1160. }
  1161. return i;
  1162. }
  1163. static void hifn_cipher_walk_exit(struct hifn_cipher_walk *w)
  1164. {
  1165. int i;
  1166. for (i = 0; i < w->num; ++i) {
  1167. struct scatterlist *s = &w->cache[i];
  1168. __free_page(sg_page(s));
  1169. s->length = 0;
  1170. }
  1171. w->num = 0;
  1172. }
  1173. static int skcipher_add(unsigned int *drestp, struct scatterlist *dst,
  1174. unsigned int size, unsigned int *nbytesp)
  1175. {
  1176. unsigned int copy, drest = *drestp, nbytes = *nbytesp;
  1177. int idx = 0;
  1178. if (drest < size || size > nbytes)
  1179. return -EINVAL;
  1180. while (size) {
  1181. copy = min3(drest, size, dst->length);
  1182. size -= copy;
  1183. drest -= copy;
  1184. nbytes -= copy;
  1185. pr_debug("%s: copy: %u, size: %u, drest: %u, nbytes: %u.\n",
  1186. __func__, copy, size, drest, nbytes);
  1187. dst++;
  1188. idx++;
  1189. }
  1190. *nbytesp = nbytes;
  1191. *drestp = drest;
  1192. return idx;
  1193. }
  1194. static int hifn_cipher_walk(struct skcipher_request *req,
  1195. struct hifn_cipher_walk *w)
  1196. {
  1197. struct scatterlist *dst, *t;
  1198. unsigned int nbytes = req->cryptlen, offset, copy, diff;
  1199. int idx, tidx, err;
  1200. tidx = idx = 0;
  1201. offset = 0;
  1202. while (nbytes) {
  1203. if (idx >= w->num && (w->flags & ASYNC_FLAGS_MISALIGNED))
  1204. return -EINVAL;
  1205. dst = &req->dst[idx];
  1206. pr_debug("\n%s: dlen: %u, doff: %u, offset: %u, nbytes: %u.\n",
  1207. __func__, dst->length, dst->offset, offset, nbytes);
  1208. if (!IS_ALIGNED(dst->offset, HIFN_D_DST_DALIGN) ||
  1209. !IS_ALIGNED(dst->length, HIFN_D_DST_DALIGN) ||
  1210. offset) {
  1211. unsigned slen = min(dst->length - offset, nbytes);
  1212. unsigned dlen = PAGE_SIZE;
  1213. t = &w->cache[idx];
  1214. err = skcipher_add(&dlen, dst, slen, &nbytes);
  1215. if (err < 0)
  1216. return err;
  1217. idx += err;
  1218. copy = slen & ~(HIFN_D_DST_DALIGN - 1);
  1219. diff = slen & (HIFN_D_DST_DALIGN - 1);
  1220. if (dlen < nbytes) {
  1221. /*
  1222. * Destination page does not have enough space
  1223. * to put there additional blocksized chunk,
  1224. * so we mark that page as containing only
  1225. * blocksize aligned chunks:
  1226. * t->length = (slen & ~(HIFN_D_DST_DALIGN - 1));
  1227. * and increase number of bytes to be processed
  1228. * in next chunk:
  1229. * nbytes += diff;
  1230. */
  1231. nbytes += diff;
  1232. /*
  1233. * Temporary of course...
  1234. * Kick author if you will catch this one.
  1235. */
  1236. pr_err("%s: dlen: %u, nbytes: %u, slen: %u, offset: %u.\n",
  1237. __func__, dlen, nbytes, slen, offset);
  1238. pr_err("%s: please contact author to fix this "
  1239. "issue, generally you should not catch "
  1240. "this path under any condition but who "
  1241. "knows how did you use crypto code.\n"
  1242. "Thank you.\n", __func__);
  1243. BUG();
  1244. } else {
  1245. copy += diff + nbytes;
  1246. dst = &req->dst[idx];
  1247. err = skcipher_add(&dlen, dst, nbytes, &nbytes);
  1248. if (err < 0)
  1249. return err;
  1250. idx += err;
  1251. }
  1252. t->length = copy;
  1253. t->offset = offset;
  1254. } else {
  1255. nbytes -= min(dst->length, nbytes);
  1256. idx++;
  1257. }
  1258. tidx++;
  1259. }
  1260. return tidx;
  1261. }
  1262. static int hifn_setup_session(struct skcipher_request *req)
  1263. {
  1264. struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
  1265. struct hifn_request_context *rctx = skcipher_request_ctx(req);
  1266. struct hifn_device *dev = ctx->dev;
  1267. unsigned long dlen, flags;
  1268. unsigned int nbytes = req->cryptlen, idx = 0;
  1269. int err = -EINVAL, sg_num;
  1270. struct scatterlist *dst;
  1271. if (rctx->iv && !rctx->ivsize && rctx->mode != ACRYPTO_MODE_ECB)
  1272. goto err_out_exit;
  1273. rctx->walk.flags = 0;
  1274. while (nbytes) {
  1275. dst = &req->dst[idx];
  1276. dlen = min(dst->length, nbytes);
  1277. if (!IS_ALIGNED(dst->offset, HIFN_D_DST_DALIGN) ||
  1278. !IS_ALIGNED(dlen, HIFN_D_DST_DALIGN))
  1279. rctx->walk.flags |= ASYNC_FLAGS_MISALIGNED;
  1280. nbytes -= dlen;
  1281. idx++;
  1282. }
  1283. if (rctx->walk.flags & ASYNC_FLAGS_MISALIGNED) {
  1284. err = hifn_cipher_walk_init(&rctx->walk, idx, GFP_ATOMIC);
  1285. if (err < 0)
  1286. return err;
  1287. }
  1288. sg_num = hifn_cipher_walk(req, &rctx->walk);
  1289. if (sg_num < 0) {
  1290. err = sg_num;
  1291. goto err_out_exit;
  1292. }
  1293. spin_lock_irqsave(&dev->lock, flags);
  1294. if (dev->started + sg_num > HIFN_QUEUE_LENGTH) {
  1295. err = -EAGAIN;
  1296. goto err_out;
  1297. }
  1298. err = hifn_setup_dma(dev, ctx, rctx, req->src, req->dst, req->cryptlen, req);
  1299. if (err)
  1300. goto err_out;
  1301. dev->snum++;
  1302. dev->active = HIFN_DEFAULT_ACTIVE_NUM;
  1303. spin_unlock_irqrestore(&dev->lock, flags);
  1304. return 0;
  1305. err_out:
  1306. spin_unlock_irqrestore(&dev->lock, flags);
  1307. err_out_exit:
  1308. if (err) {
  1309. dev_info(&dev->pdev->dev, "iv: %p [%d], key: %p [%d], mode: %u, op: %u, "
  1310. "type: %u, err: %d.\n",
  1311. rctx->iv, rctx->ivsize,
  1312. ctx->key, ctx->keysize,
  1313. rctx->mode, rctx->op, rctx->type, err);
  1314. }
  1315. return err;
  1316. }
  1317. static int hifn_start_device(struct hifn_device *dev)
  1318. {
  1319. int err;
  1320. dev->started = dev->active = 0;
  1321. hifn_reset_dma(dev, 1);
  1322. err = hifn_enable_crypto(dev);
  1323. if (err)
  1324. return err;
  1325. hifn_reset_puc(dev);
  1326. hifn_init_dma(dev);
  1327. hifn_init_registers(dev);
  1328. hifn_init_pubrng(dev);
  1329. return 0;
  1330. }
  1331. static int skcipher_get(void *saddr, unsigned int *srestp, unsigned int offset,
  1332. struct scatterlist *dst, unsigned int size, unsigned int *nbytesp)
  1333. {
  1334. unsigned int srest = *srestp, nbytes = *nbytesp, copy;
  1335. void *daddr;
  1336. int idx = 0;
  1337. if (srest < size || size > nbytes)
  1338. return -EINVAL;
  1339. while (size) {
  1340. copy = min3(srest, dst->length, size);
  1341. daddr = kmap_atomic(sg_page(dst));
  1342. memcpy(daddr + dst->offset + offset, saddr, copy);
  1343. kunmap_atomic(daddr);
  1344. nbytes -= copy;
  1345. size -= copy;
  1346. srest -= copy;
  1347. saddr += copy;
  1348. offset = 0;
  1349. pr_debug("%s: copy: %u, size: %u, srest: %u, nbytes: %u.\n",
  1350. __func__, copy, size, srest, nbytes);
  1351. dst++;
  1352. idx++;
  1353. }
  1354. *nbytesp = nbytes;
  1355. *srestp = srest;
  1356. return idx;
  1357. }
  1358. static inline void hifn_complete_sa(struct hifn_device *dev, int i)
  1359. {
  1360. unsigned long flags;
  1361. spin_lock_irqsave(&dev->lock, flags);
  1362. dev->sa[i] = NULL;
  1363. dev->started--;
  1364. if (dev->started < 0)
  1365. dev_info(&dev->pdev->dev, "%s: started: %d.\n", __func__,
  1366. dev->started);
  1367. spin_unlock_irqrestore(&dev->lock, flags);
  1368. BUG_ON(dev->started < 0);
  1369. }
  1370. static void hifn_process_ready(struct skcipher_request *req, int error)
  1371. {
  1372. struct hifn_request_context *rctx = skcipher_request_ctx(req);
  1373. if (rctx->walk.flags & ASYNC_FLAGS_MISALIGNED) {
  1374. unsigned int nbytes = req->cryptlen;
  1375. int idx = 0, err;
  1376. struct scatterlist *dst, *t;
  1377. void *saddr;
  1378. while (nbytes) {
  1379. t = &rctx->walk.cache[idx];
  1380. dst = &req->dst[idx];
  1381. pr_debug("\n%s: sg_page(t): %p, t->length: %u, "
  1382. "sg_page(dst): %p, dst->length: %u, "
  1383. "nbytes: %u.\n",
  1384. __func__, sg_page(t), t->length,
  1385. sg_page(dst), dst->length, nbytes);
  1386. if (!t->length) {
  1387. nbytes -= min(dst->length, nbytes);
  1388. idx++;
  1389. continue;
  1390. }
  1391. saddr = kmap_atomic(sg_page(t));
  1392. err = skcipher_get(saddr, &t->length, t->offset,
  1393. dst, nbytes, &nbytes);
  1394. if (err < 0) {
  1395. kunmap_atomic(saddr);
  1396. break;
  1397. }
  1398. idx += err;
  1399. kunmap_atomic(saddr);
  1400. }
  1401. hifn_cipher_walk_exit(&rctx->walk);
  1402. }
  1403. skcipher_request_complete(req, error);
  1404. }
  1405. static void hifn_clear_rings(struct hifn_device *dev, int error)
  1406. {
  1407. struct hifn_dma *dma = dev->desc_virt;
  1408. int i, u;
  1409. dev_dbg(&dev->pdev->dev, "ring cleanup 1: i: %d.%d.%d.%d, u: %d.%d.%d.%d, "
  1410. "k: %d.%d.%d.%d.\n",
  1411. dma->cmdi, dma->srci, dma->dsti, dma->resi,
  1412. dma->cmdu, dma->srcu, dma->dstu, dma->resu,
  1413. dma->cmdk, dma->srck, dma->dstk, dma->resk);
  1414. i = dma->resk; u = dma->resu;
  1415. while (u != 0) {
  1416. if (dma->resr[i].l & __cpu_to_le32(HIFN_D_VALID))
  1417. break;
  1418. if (dev->sa[i]) {
  1419. dev->success++;
  1420. dev->reset = 0;
  1421. hifn_process_ready(dev->sa[i], error);
  1422. hifn_complete_sa(dev, i);
  1423. }
  1424. if (++i == HIFN_D_RES_RSIZE)
  1425. i = 0;
  1426. u--;
  1427. }
  1428. dma->resk = i; dma->resu = u;
  1429. i = dma->srck; u = dma->srcu;
  1430. while (u != 0) {
  1431. if (dma->srcr[i].l & __cpu_to_le32(HIFN_D_VALID))
  1432. break;
  1433. if (++i == HIFN_D_SRC_RSIZE)
  1434. i = 0;
  1435. u--;
  1436. }
  1437. dma->srck = i; dma->srcu = u;
  1438. i = dma->cmdk; u = dma->cmdu;
  1439. while (u != 0) {
  1440. if (dma->cmdr[i].l & __cpu_to_le32(HIFN_D_VALID))
  1441. break;
  1442. if (++i == HIFN_D_CMD_RSIZE)
  1443. i = 0;
  1444. u--;
  1445. }
  1446. dma->cmdk = i; dma->cmdu = u;
  1447. i = dma->dstk; u = dma->dstu;
  1448. while (u != 0) {
  1449. if (dma->dstr[i].l & __cpu_to_le32(HIFN_D_VALID))
  1450. break;
  1451. if (++i == HIFN_D_DST_RSIZE)
  1452. i = 0;
  1453. u--;
  1454. }
  1455. dma->dstk = i; dma->dstu = u;
  1456. dev_dbg(&dev->pdev->dev, "ring cleanup 2: i: %d.%d.%d.%d, u: %d.%d.%d.%d, "
  1457. "k: %d.%d.%d.%d.\n",
  1458. dma->cmdi, dma->srci, dma->dsti, dma->resi,
  1459. dma->cmdu, dma->srcu, dma->dstu, dma->resu,
  1460. dma->cmdk, dma->srck, dma->dstk, dma->resk);
  1461. }
  1462. static void hifn_work(struct work_struct *work)
  1463. {
  1464. struct delayed_work *dw = to_delayed_work(work);
  1465. struct hifn_device *dev = container_of(dw, struct hifn_device, work);
  1466. unsigned long flags;
  1467. int reset = 0;
  1468. u32 r = 0;
  1469. spin_lock_irqsave(&dev->lock, flags);
  1470. if (dev->active == 0) {
  1471. struct hifn_dma *dma = dev->desc_virt;
  1472. if (dma->cmdu == 0 && (dev->flags & HIFN_FLAG_CMD_BUSY)) {
  1473. dev->flags &= ~HIFN_FLAG_CMD_BUSY;
  1474. r |= HIFN_DMACSR_C_CTRL_DIS;
  1475. }
  1476. if (dma->srcu == 0 && (dev->flags & HIFN_FLAG_SRC_BUSY)) {
  1477. dev->flags &= ~HIFN_FLAG_SRC_BUSY;
  1478. r |= HIFN_DMACSR_S_CTRL_DIS;
  1479. }
  1480. if (dma->dstu == 0 && (dev->flags & HIFN_FLAG_DST_BUSY)) {
  1481. dev->flags &= ~HIFN_FLAG_DST_BUSY;
  1482. r |= HIFN_DMACSR_D_CTRL_DIS;
  1483. }
  1484. if (dma->resu == 0 && (dev->flags & HIFN_FLAG_RES_BUSY)) {
  1485. dev->flags &= ~HIFN_FLAG_RES_BUSY;
  1486. r |= HIFN_DMACSR_R_CTRL_DIS;
  1487. }
  1488. if (r)
  1489. hifn_write_1(dev, HIFN_1_DMA_CSR, r);
  1490. } else
  1491. dev->active--;
  1492. if ((dev->prev_success == dev->success) && dev->started)
  1493. reset = 1;
  1494. dev->prev_success = dev->success;
  1495. spin_unlock_irqrestore(&dev->lock, flags);
  1496. if (reset) {
  1497. if (++dev->reset >= 5) {
  1498. int i;
  1499. struct hifn_dma *dma = dev->desc_virt;
  1500. dev_info(&dev->pdev->dev,
  1501. "r: %08x, active: %d, started: %d, "
  1502. "success: %lu: qlen: %u/%u, reset: %d.\n",
  1503. r, dev->active, dev->started,
  1504. dev->success, dev->queue.qlen, dev->queue.max_qlen,
  1505. reset);
  1506. dev_info(&dev->pdev->dev, "%s: res: ", __func__);
  1507. for (i = 0; i < HIFN_D_RES_RSIZE; ++i) {
  1508. pr_info("%x.%p ", dma->resr[i].l, dev->sa[i]);
  1509. if (dev->sa[i]) {
  1510. hifn_process_ready(dev->sa[i], -ENODEV);
  1511. hifn_complete_sa(dev, i);
  1512. }
  1513. }
  1514. pr_info("\n");
  1515. hifn_reset_dma(dev, 1);
  1516. hifn_stop_device(dev);
  1517. hifn_start_device(dev);
  1518. dev->reset = 0;
  1519. }
  1520. tasklet_schedule(&dev->tasklet);
  1521. }
  1522. schedule_delayed_work(&dev->work, HZ);
  1523. }
  1524. static irqreturn_t hifn_interrupt(int irq, void *data)
  1525. {
  1526. struct hifn_device *dev = data;
  1527. struct hifn_dma *dma = dev->desc_virt;
  1528. u32 dmacsr, restart;
  1529. dmacsr = hifn_read_1(dev, HIFN_1_DMA_CSR);
  1530. dev_dbg(&dev->pdev->dev, "1 dmacsr: %08x, dmareg: %08x, res: %08x [%d], "
  1531. "i: %d.%d.%d.%d, u: %d.%d.%d.%d.\n",
  1532. dmacsr, dev->dmareg, dmacsr & dev->dmareg, dma->cmdi,
  1533. dma->cmdi, dma->srci, dma->dsti, dma->resi,
  1534. dma->cmdu, dma->srcu, dma->dstu, dma->resu);
  1535. if ((dmacsr & dev->dmareg) == 0)
  1536. return IRQ_NONE;
  1537. hifn_write_1(dev, HIFN_1_DMA_CSR, dmacsr & dev->dmareg);
  1538. if (dmacsr & HIFN_DMACSR_ENGINE)
  1539. hifn_write_0(dev, HIFN_0_PUISR, hifn_read_0(dev, HIFN_0_PUISR));
  1540. if (dmacsr & HIFN_DMACSR_PUBDONE)
  1541. hifn_write_1(dev, HIFN_1_PUB_STATUS,
  1542. hifn_read_1(dev, HIFN_1_PUB_STATUS) | HIFN_PUBSTS_DONE);
  1543. restart = dmacsr & (HIFN_DMACSR_R_OVER | HIFN_DMACSR_D_OVER);
  1544. if (restart) {
  1545. u32 puisr = hifn_read_0(dev, HIFN_0_PUISR);
  1546. dev_warn(&dev->pdev->dev, "overflow: r: %d, d: %d, puisr: %08x, d: %u.\n",
  1547. !!(dmacsr & HIFN_DMACSR_R_OVER),
  1548. !!(dmacsr & HIFN_DMACSR_D_OVER),
  1549. puisr, !!(puisr & HIFN_PUISR_DSTOVER));
  1550. if (!!(puisr & HIFN_PUISR_DSTOVER))
  1551. hifn_write_0(dev, HIFN_0_PUISR, HIFN_PUISR_DSTOVER);
  1552. hifn_write_1(dev, HIFN_1_DMA_CSR, dmacsr & (HIFN_DMACSR_R_OVER |
  1553. HIFN_DMACSR_D_OVER));
  1554. }
  1555. restart = dmacsr & (HIFN_DMACSR_C_ABORT | HIFN_DMACSR_S_ABORT |
  1556. HIFN_DMACSR_D_ABORT | HIFN_DMACSR_R_ABORT);
  1557. if (restart) {
  1558. dev_warn(&dev->pdev->dev, "abort: c: %d, s: %d, d: %d, r: %d.\n",
  1559. !!(dmacsr & HIFN_DMACSR_C_ABORT),
  1560. !!(dmacsr & HIFN_DMACSR_S_ABORT),
  1561. !!(dmacsr & HIFN_DMACSR_D_ABORT),
  1562. !!(dmacsr & HIFN_DMACSR_R_ABORT));
  1563. hifn_reset_dma(dev, 1);
  1564. hifn_init_dma(dev);
  1565. hifn_init_registers(dev);
  1566. }
  1567. if ((dmacsr & HIFN_DMACSR_C_WAIT) && (dma->cmdu == 0)) {
  1568. dev_dbg(&dev->pdev->dev, "wait on command.\n");
  1569. dev->dmareg &= ~(HIFN_DMAIER_C_WAIT);
  1570. hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
  1571. }
  1572. tasklet_schedule(&dev->tasklet);
  1573. return IRQ_HANDLED;
  1574. }
  1575. static void hifn_flush(struct hifn_device *dev)
  1576. {
  1577. unsigned long flags;
  1578. struct crypto_async_request *async_req;
  1579. struct skcipher_request *req;
  1580. struct hifn_dma *dma = dev->desc_virt;
  1581. int i;
  1582. for (i = 0; i < HIFN_D_RES_RSIZE; ++i) {
  1583. struct hifn_desc *d = &dma->resr[i];
  1584. if (dev->sa[i]) {
  1585. hifn_process_ready(dev->sa[i],
  1586. (d->l & __cpu_to_le32(HIFN_D_VALID)) ? -ENODEV : 0);
  1587. hifn_complete_sa(dev, i);
  1588. }
  1589. }
  1590. spin_lock_irqsave(&dev->lock, flags);
  1591. while ((async_req = crypto_dequeue_request(&dev->queue))) {
  1592. req = skcipher_request_cast(async_req);
  1593. spin_unlock_irqrestore(&dev->lock, flags);
  1594. hifn_process_ready(req, -ENODEV);
  1595. spin_lock_irqsave(&dev->lock, flags);
  1596. }
  1597. spin_unlock_irqrestore(&dev->lock, flags);
  1598. }
  1599. static int hifn_setkey(struct crypto_skcipher *cipher, const u8 *key,
  1600. unsigned int len)
  1601. {
  1602. struct hifn_context *ctx = crypto_skcipher_ctx(cipher);
  1603. struct hifn_device *dev = ctx->dev;
  1604. int err;
  1605. err = verify_skcipher_des_key(cipher, key);
  1606. if (err)
  1607. return err;
  1608. dev->flags &= ~HIFN_FLAG_OLD_KEY;
  1609. memcpy(ctx->key, key, len);
  1610. ctx->keysize = len;
  1611. return 0;
  1612. }
  1613. static int hifn_des3_setkey(struct crypto_skcipher *cipher, const u8 *key,
  1614. unsigned int len)
  1615. {
  1616. struct hifn_context *ctx = crypto_skcipher_ctx(cipher);
  1617. struct hifn_device *dev = ctx->dev;
  1618. int err;
  1619. err = verify_skcipher_des3_key(cipher, key);
  1620. if (err)
  1621. return err;
  1622. dev->flags &= ~HIFN_FLAG_OLD_KEY;
  1623. memcpy(ctx->key, key, len);
  1624. ctx->keysize = len;
  1625. return 0;
  1626. }
  1627. static int hifn_handle_req(struct skcipher_request *req)
  1628. {
  1629. struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
  1630. struct hifn_device *dev = ctx->dev;
  1631. int err = -EAGAIN;
  1632. if (dev->started + DIV_ROUND_UP(req->cryptlen, PAGE_SIZE) <= HIFN_QUEUE_LENGTH)
  1633. err = hifn_setup_session(req);
  1634. if (err == -EAGAIN) {
  1635. unsigned long flags;
  1636. spin_lock_irqsave(&dev->lock, flags);
  1637. err = crypto_enqueue_request(&dev->queue, &req->base);
  1638. spin_unlock_irqrestore(&dev->lock, flags);
  1639. }
  1640. return err;
  1641. }
  1642. static int hifn_setup_crypto_req(struct skcipher_request *req, u8 op,
  1643. u8 type, u8 mode)
  1644. {
  1645. struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
  1646. struct hifn_request_context *rctx = skcipher_request_ctx(req);
  1647. unsigned ivsize;
  1648. ivsize = crypto_skcipher_ivsize(crypto_skcipher_reqtfm(req));
  1649. if (req->iv && mode != ACRYPTO_MODE_ECB) {
  1650. if (type == ACRYPTO_TYPE_AES_128)
  1651. ivsize = HIFN_AES_IV_LENGTH;
  1652. else if (type == ACRYPTO_TYPE_DES)
  1653. ivsize = HIFN_DES_KEY_LENGTH;
  1654. else if (type == ACRYPTO_TYPE_3DES)
  1655. ivsize = HIFN_3DES_KEY_LENGTH;
  1656. }
  1657. if (ctx->keysize != 16 && type == ACRYPTO_TYPE_AES_128) {
  1658. if (ctx->keysize == 24)
  1659. type = ACRYPTO_TYPE_AES_192;
  1660. else if (ctx->keysize == 32)
  1661. type = ACRYPTO_TYPE_AES_256;
  1662. }
  1663. rctx->op = op;
  1664. rctx->mode = mode;
  1665. rctx->type = type;
  1666. rctx->iv = req->iv;
  1667. rctx->ivsize = ivsize;
  1668. /*
  1669. * HEAVY TODO: needs to kick Herbert XU to write documentation.
  1670. * HEAVY TODO: needs to kick Herbert XU to write documentation.
  1671. * HEAVY TODO: needs to kick Herbert XU to write documentation.
  1672. */
  1673. return hifn_handle_req(req);
  1674. }
  1675. static int hifn_process_queue(struct hifn_device *dev)
  1676. {
  1677. struct crypto_async_request *async_req, *backlog;
  1678. struct skcipher_request *req;
  1679. unsigned long flags;
  1680. int err = 0;
  1681. while (dev->started < HIFN_QUEUE_LENGTH) {
  1682. spin_lock_irqsave(&dev->lock, flags);
  1683. backlog = crypto_get_backlog(&dev->queue);
  1684. async_req = crypto_dequeue_request(&dev->queue);
  1685. spin_unlock_irqrestore(&dev->lock, flags);
  1686. if (!async_req)
  1687. break;
  1688. if (backlog)
  1689. crypto_request_complete(backlog, -EINPROGRESS);
  1690. req = skcipher_request_cast(async_req);
  1691. err = hifn_handle_req(req);
  1692. if (err)
  1693. break;
  1694. }
  1695. return err;
  1696. }
  1697. static int hifn_setup_crypto(struct skcipher_request *req, u8 op,
  1698. u8 type, u8 mode)
  1699. {
  1700. int err;
  1701. struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
  1702. struct hifn_device *dev = ctx->dev;
  1703. err = hifn_setup_crypto_req(req, op, type, mode);
  1704. if (err)
  1705. return err;
  1706. if (dev->started < HIFN_QUEUE_LENGTH && dev->queue.qlen)
  1707. hifn_process_queue(dev);
  1708. return -EINPROGRESS;
  1709. }
  1710. /*
  1711. * AES ecryption functions.
  1712. */
  1713. static inline int hifn_encrypt_aes_ecb(struct skcipher_request *req)
  1714. {
  1715. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1716. ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_ECB);
  1717. }
  1718. static inline int hifn_encrypt_aes_cbc(struct skcipher_request *req)
  1719. {
  1720. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1721. ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CBC);
  1722. }
  1723. /*
  1724. * AES decryption functions.
  1725. */
  1726. static inline int hifn_decrypt_aes_ecb(struct skcipher_request *req)
  1727. {
  1728. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1729. ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_ECB);
  1730. }
  1731. static inline int hifn_decrypt_aes_cbc(struct skcipher_request *req)
  1732. {
  1733. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1734. ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CBC);
  1735. }
  1736. /*
  1737. * DES ecryption functions.
  1738. */
  1739. static inline int hifn_encrypt_des_ecb(struct skcipher_request *req)
  1740. {
  1741. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1742. ACRYPTO_TYPE_DES, ACRYPTO_MODE_ECB);
  1743. }
  1744. static inline int hifn_encrypt_des_cbc(struct skcipher_request *req)
  1745. {
  1746. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1747. ACRYPTO_TYPE_DES, ACRYPTO_MODE_CBC);
  1748. }
  1749. /*
  1750. * DES decryption functions.
  1751. */
  1752. static inline int hifn_decrypt_des_ecb(struct skcipher_request *req)
  1753. {
  1754. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1755. ACRYPTO_TYPE_DES, ACRYPTO_MODE_ECB);
  1756. }
  1757. static inline int hifn_decrypt_des_cbc(struct skcipher_request *req)
  1758. {
  1759. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1760. ACRYPTO_TYPE_DES, ACRYPTO_MODE_CBC);
  1761. }
  1762. /*
  1763. * 3DES ecryption functions.
  1764. */
  1765. static inline int hifn_encrypt_3des_ecb(struct skcipher_request *req)
  1766. {
  1767. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1768. ACRYPTO_TYPE_3DES, ACRYPTO_MODE_ECB);
  1769. }
  1770. static inline int hifn_encrypt_3des_cbc(struct skcipher_request *req)
  1771. {
  1772. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1773. ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CBC);
  1774. }
  1775. /* 3DES decryption functions. */
  1776. static inline int hifn_decrypt_3des_ecb(struct skcipher_request *req)
  1777. {
  1778. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1779. ACRYPTO_TYPE_3DES, ACRYPTO_MODE_ECB);
  1780. }
  1781. static inline int hifn_decrypt_3des_cbc(struct skcipher_request *req)
  1782. {
  1783. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1784. ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CBC);
  1785. }
  1786. struct hifn_alg_template {
  1787. char name[CRYPTO_MAX_ALG_NAME];
  1788. char drv_name[CRYPTO_MAX_ALG_NAME];
  1789. unsigned int bsize;
  1790. struct skcipher_alg skcipher;
  1791. };
  1792. static const struct hifn_alg_template hifn_alg_templates[] = {
  1793. /*
  1794. * 3DES ECB and CBC modes.
  1795. */
  1796. {
  1797. .name = "cbc(des3_ede)", .drv_name = "cbc-3des", .bsize = 8,
  1798. .skcipher = {
  1799. .ivsize = HIFN_IV_LENGTH,
  1800. .min_keysize = HIFN_3DES_KEY_LENGTH,
  1801. .max_keysize = HIFN_3DES_KEY_LENGTH,
  1802. .setkey = hifn_des3_setkey,
  1803. .encrypt = hifn_encrypt_3des_cbc,
  1804. .decrypt = hifn_decrypt_3des_cbc,
  1805. },
  1806. },
  1807. {
  1808. .name = "ecb(des3_ede)", .drv_name = "ecb-3des", .bsize = 8,
  1809. .skcipher = {
  1810. .min_keysize = HIFN_3DES_KEY_LENGTH,
  1811. .max_keysize = HIFN_3DES_KEY_LENGTH,
  1812. .setkey = hifn_des3_setkey,
  1813. .encrypt = hifn_encrypt_3des_ecb,
  1814. .decrypt = hifn_decrypt_3des_ecb,
  1815. },
  1816. },
  1817. /*
  1818. * DES ECB and CBC modes.
  1819. */
  1820. {
  1821. .name = "cbc(des)", .drv_name = "cbc-des", .bsize = 8,
  1822. .skcipher = {
  1823. .ivsize = HIFN_IV_LENGTH,
  1824. .min_keysize = HIFN_DES_KEY_LENGTH,
  1825. .max_keysize = HIFN_DES_KEY_LENGTH,
  1826. .setkey = hifn_setkey,
  1827. .encrypt = hifn_encrypt_des_cbc,
  1828. .decrypt = hifn_decrypt_des_cbc,
  1829. },
  1830. },
  1831. {
  1832. .name = "ecb(des)", .drv_name = "ecb-des", .bsize = 8,
  1833. .skcipher = {
  1834. .min_keysize = HIFN_DES_KEY_LENGTH,
  1835. .max_keysize = HIFN_DES_KEY_LENGTH,
  1836. .setkey = hifn_setkey,
  1837. .encrypt = hifn_encrypt_des_ecb,
  1838. .decrypt = hifn_decrypt_des_ecb,
  1839. },
  1840. },
  1841. /*
  1842. * AES ECB and CBC modes.
  1843. */
  1844. {
  1845. .name = "ecb(aes)", .drv_name = "ecb-aes", .bsize = 16,
  1846. .skcipher = {
  1847. .min_keysize = AES_MIN_KEY_SIZE,
  1848. .max_keysize = AES_MAX_KEY_SIZE,
  1849. .setkey = hifn_setkey,
  1850. .encrypt = hifn_encrypt_aes_ecb,
  1851. .decrypt = hifn_decrypt_aes_ecb,
  1852. },
  1853. },
  1854. {
  1855. .name = "cbc(aes)", .drv_name = "cbc-aes", .bsize = 16,
  1856. .skcipher = {
  1857. .ivsize = HIFN_AES_IV_LENGTH,
  1858. .min_keysize = AES_MIN_KEY_SIZE,
  1859. .max_keysize = AES_MAX_KEY_SIZE,
  1860. .setkey = hifn_setkey,
  1861. .encrypt = hifn_encrypt_aes_cbc,
  1862. .decrypt = hifn_decrypt_aes_cbc,
  1863. },
  1864. },
  1865. };
  1866. static int hifn_init_tfm(struct crypto_skcipher *tfm)
  1867. {
  1868. struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
  1869. struct hifn_crypto_alg *ha = crypto_alg_to_hifn(alg);
  1870. struct hifn_context *ctx = crypto_skcipher_ctx(tfm);
  1871. ctx->dev = ha->dev;
  1872. crypto_skcipher_set_reqsize(tfm, sizeof(struct hifn_request_context));
  1873. return 0;
  1874. }
  1875. static int hifn_alg_alloc(struct hifn_device *dev, const struct hifn_alg_template *t)
  1876. {
  1877. struct hifn_crypto_alg *alg;
  1878. int err;
  1879. alg = kzalloc_obj(*alg);
  1880. if (!alg)
  1881. return -ENOMEM;
  1882. alg->alg = t->skcipher;
  1883. alg->alg.init = hifn_init_tfm;
  1884. err = -EINVAL;
  1885. if (snprintf(alg->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  1886. "%s", t->name) >= CRYPTO_MAX_ALG_NAME)
  1887. goto out_free_alg;
  1888. if (snprintf(alg->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  1889. "%s-%s", t->drv_name, dev->name) >= CRYPTO_MAX_ALG_NAME)
  1890. goto out_free_alg;
  1891. alg->alg.base.cra_priority = 300;
  1892. alg->alg.base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC;
  1893. alg->alg.base.cra_blocksize = t->bsize;
  1894. alg->alg.base.cra_ctxsize = sizeof(struct hifn_context);
  1895. alg->alg.base.cra_alignmask = 0;
  1896. alg->alg.base.cra_module = THIS_MODULE;
  1897. alg->dev = dev;
  1898. list_add_tail(&alg->entry, &dev->alg_list);
  1899. err = crypto_register_skcipher(&alg->alg);
  1900. if (err) {
  1901. list_del(&alg->entry);
  1902. out_free_alg:
  1903. kfree(alg);
  1904. }
  1905. return err;
  1906. }
  1907. static void hifn_unregister_alg(struct hifn_device *dev)
  1908. {
  1909. struct hifn_crypto_alg *a, *n;
  1910. list_for_each_entry_safe(a, n, &dev->alg_list, entry) {
  1911. list_del(&a->entry);
  1912. crypto_unregister_skcipher(&a->alg);
  1913. kfree(a);
  1914. }
  1915. }
  1916. static int hifn_register_alg(struct hifn_device *dev)
  1917. {
  1918. int i, err;
  1919. for (i = 0; i < ARRAY_SIZE(hifn_alg_templates); ++i) {
  1920. err = hifn_alg_alloc(dev, &hifn_alg_templates[i]);
  1921. if (err)
  1922. goto err_out_exit;
  1923. }
  1924. return 0;
  1925. err_out_exit:
  1926. hifn_unregister_alg(dev);
  1927. return err;
  1928. }
  1929. static void hifn_tasklet_callback(unsigned long data)
  1930. {
  1931. struct hifn_device *dev = (struct hifn_device *)data;
  1932. /*
  1933. * This is ok to call this without lock being held,
  1934. * althogh it modifies some parameters used in parallel,
  1935. * (like dev->success), but they are used in process
  1936. * context or update is atomic (like setting dev->sa[i] to NULL).
  1937. */
  1938. hifn_clear_rings(dev, 0);
  1939. if (dev->started < HIFN_QUEUE_LENGTH && dev->queue.qlen)
  1940. hifn_process_queue(dev);
  1941. }
  1942. static int hifn_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1943. {
  1944. int err, i;
  1945. struct hifn_device *dev;
  1946. char name[8];
  1947. err = pci_enable_device(pdev);
  1948. if (err)
  1949. return err;
  1950. pci_set_master(pdev);
  1951. err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  1952. if (err)
  1953. goto err_out_disable_pci_device;
  1954. snprintf(name, sizeof(name), "hifn%d",
  1955. atomic_inc_return(&hifn_dev_number) - 1);
  1956. err = pci_request_regions(pdev, name);
  1957. if (err)
  1958. goto err_out_disable_pci_device;
  1959. if (pci_resource_len(pdev, 0) < HIFN_BAR0_SIZE ||
  1960. pci_resource_len(pdev, 1) < HIFN_BAR1_SIZE ||
  1961. pci_resource_len(pdev, 2) < HIFN_BAR2_SIZE) {
  1962. dev_err(&pdev->dev, "Broken hardware - I/O regions are too small.\n");
  1963. err = -ENODEV;
  1964. goto err_out_free_regions;
  1965. }
  1966. dev = kzalloc(sizeof(struct hifn_device) + sizeof(struct crypto_alg),
  1967. GFP_KERNEL);
  1968. if (!dev) {
  1969. err = -ENOMEM;
  1970. goto err_out_free_regions;
  1971. }
  1972. INIT_LIST_HEAD(&dev->alg_list);
  1973. snprintf(dev->name, sizeof(dev->name), "%s", name);
  1974. spin_lock_init(&dev->lock);
  1975. for (i = 0; i < 3; ++i) {
  1976. unsigned long addr, size;
  1977. addr = pci_resource_start(pdev, i);
  1978. size = pci_resource_len(pdev, i);
  1979. dev->bar[i] = ioremap(addr, size);
  1980. if (!dev->bar[i]) {
  1981. err = -ENOMEM;
  1982. goto err_out_unmap_bars;
  1983. }
  1984. }
  1985. dev->desc_virt = dma_alloc_coherent(&pdev->dev,
  1986. sizeof(struct hifn_dma),
  1987. &dev->desc_dma, GFP_KERNEL);
  1988. if (!dev->desc_virt) {
  1989. dev_err(&pdev->dev, "Failed to allocate descriptor rings.\n");
  1990. err = -ENOMEM;
  1991. goto err_out_unmap_bars;
  1992. }
  1993. dev->pdev = pdev;
  1994. dev->irq = pdev->irq;
  1995. for (i = 0; i < HIFN_D_RES_RSIZE; ++i)
  1996. dev->sa[i] = NULL;
  1997. pci_set_drvdata(pdev, dev);
  1998. tasklet_init(&dev->tasklet, hifn_tasklet_callback, (unsigned long)dev);
  1999. crypto_init_queue(&dev->queue, 1);
  2000. err = request_irq(dev->irq, hifn_interrupt, IRQF_SHARED, dev->name, dev);
  2001. if (err) {
  2002. dev_err(&pdev->dev, "Failed to request IRQ%d: err: %d.\n",
  2003. dev->irq, err);
  2004. dev->irq = 0;
  2005. goto err_out_free_desc;
  2006. }
  2007. err = hifn_start_device(dev);
  2008. if (err)
  2009. goto err_out_free_irq;
  2010. err = hifn_register_rng(dev);
  2011. if (err)
  2012. goto err_out_stop_device;
  2013. err = hifn_register_alg(dev);
  2014. if (err)
  2015. goto err_out_unregister_rng;
  2016. INIT_DELAYED_WORK(&dev->work, hifn_work);
  2017. schedule_delayed_work(&dev->work, HZ);
  2018. dev_dbg(&pdev->dev, "HIFN crypto accelerator card at %s has been "
  2019. "successfully registered as %s.\n",
  2020. pci_name(pdev), dev->name);
  2021. return 0;
  2022. err_out_unregister_rng:
  2023. hifn_unregister_rng(dev);
  2024. err_out_stop_device:
  2025. hifn_reset_dma(dev, 1);
  2026. hifn_stop_device(dev);
  2027. err_out_free_irq:
  2028. free_irq(dev->irq, dev);
  2029. tasklet_kill(&dev->tasklet);
  2030. err_out_free_desc:
  2031. dma_free_coherent(&pdev->dev, sizeof(struct hifn_dma), dev->desc_virt,
  2032. dev->desc_dma);
  2033. err_out_unmap_bars:
  2034. for (i = 0; i < 3; ++i)
  2035. if (dev->bar[i])
  2036. iounmap(dev->bar[i]);
  2037. kfree(dev);
  2038. err_out_free_regions:
  2039. pci_release_regions(pdev);
  2040. err_out_disable_pci_device:
  2041. pci_disable_device(pdev);
  2042. return err;
  2043. }
  2044. static void hifn_remove(struct pci_dev *pdev)
  2045. {
  2046. int i;
  2047. struct hifn_device *dev;
  2048. dev = pci_get_drvdata(pdev);
  2049. if (dev) {
  2050. cancel_delayed_work_sync(&dev->work);
  2051. hifn_unregister_rng(dev);
  2052. hifn_unregister_alg(dev);
  2053. hifn_reset_dma(dev, 1);
  2054. hifn_stop_device(dev);
  2055. free_irq(dev->irq, dev);
  2056. tasklet_kill(&dev->tasklet);
  2057. hifn_flush(dev);
  2058. dma_free_coherent(&pdev->dev, sizeof(struct hifn_dma),
  2059. dev->desc_virt, dev->desc_dma);
  2060. for (i = 0; i < 3; ++i)
  2061. if (dev->bar[i])
  2062. iounmap(dev->bar[i]);
  2063. kfree(dev);
  2064. }
  2065. pci_release_regions(pdev);
  2066. pci_disable_device(pdev);
  2067. }
  2068. static struct pci_device_id hifn_pci_tbl[] = {
  2069. { PCI_DEVICE(PCI_VENDOR_ID_HIFN, PCI_DEVICE_ID_HIFN_7955) },
  2070. { PCI_DEVICE(PCI_VENDOR_ID_HIFN, PCI_DEVICE_ID_HIFN_7956) },
  2071. { 0 }
  2072. };
  2073. MODULE_DEVICE_TABLE(pci, hifn_pci_tbl);
  2074. static struct pci_driver hifn_pci_driver = {
  2075. .name = "hifn795x",
  2076. .id_table = hifn_pci_tbl,
  2077. .probe = hifn_probe,
  2078. .remove = hifn_remove,
  2079. };
  2080. static int __init hifn_init(void)
  2081. {
  2082. unsigned int freq;
  2083. int err;
  2084. if (strncmp(hifn_pll_ref, "ext", 3) &&
  2085. strncmp(hifn_pll_ref, "pci", 3)) {
  2086. pr_err("hifn795x: invalid hifn_pll_ref clock, must be pci or ext");
  2087. return -EINVAL;
  2088. }
  2089. /*
  2090. * For the 7955/7956 the reference clock frequency must be in the
  2091. * range of 20MHz-100MHz. For the 7954 the upper bound is 66.67MHz,
  2092. * but this chip is currently not supported.
  2093. */
  2094. if (hifn_pll_ref[3] != '\0') {
  2095. freq = simple_strtoul(hifn_pll_ref + 3, NULL, 10);
  2096. if (freq < 20 || freq > 100) {
  2097. pr_err("hifn795x: invalid hifn_pll_ref frequency, must"
  2098. "be in the range of 20-100");
  2099. return -EINVAL;
  2100. }
  2101. }
  2102. err = pci_register_driver(&hifn_pci_driver);
  2103. if (err < 0) {
  2104. pr_err("Failed to register PCI driver for %s device.\n",
  2105. hifn_pci_driver.name);
  2106. return -ENODEV;
  2107. }
  2108. pr_info("Driver for HIFN 795x crypto accelerator chip "
  2109. "has been successfully registered.\n");
  2110. return 0;
  2111. }
  2112. static void __exit hifn_fini(void)
  2113. {
  2114. pci_unregister_driver(&hifn_pci_driver);
  2115. pr_info("Driver for HIFN 795x crypto accelerator chip "
  2116. "has been successfully unregistered.\n");
  2117. }
  2118. module_init(hifn_init);
  2119. module_exit(hifn_fini);
  2120. MODULE_LICENSE("GPL");
  2121. MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
  2122. MODULE_DESCRIPTION("Driver for HIFN 795x crypto accelerator chip.");