hifn_795x.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562
  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. freq = simple_strtoul(hifn_pll_ref + 3, NULL, 10);
  778. else {
  779. freq = 66;
  780. dev_info(&dev->pdev->dev, "assuming %uMHz clock speed, override with hifn_pll_ref=%.3s<frequency>\n",
  781. freq, hifn_pll_ref);
  782. }
  783. m = HIFN_PLL_FCK_MAX / freq;
  784. pllcfg |= (m / 2 - 1) << HIFN_PLL_ND_SHIFT;
  785. if (m <= 8)
  786. pllcfg |= HIFN_PLL_IS_1_8;
  787. else
  788. pllcfg |= HIFN_PLL_IS_9_12;
  789. /* Select clock source and enable clock bypass */
  790. hifn_write_1(dev, HIFN_1_PLL, pllcfg |
  791. HIFN_PLL_PK_CLK_HBI | HIFN_PLL_PE_CLK_HBI | HIFN_PLL_BP);
  792. /* Let the chip lock to the input clock */
  793. mdelay(10);
  794. /* Disable clock bypass */
  795. hifn_write_1(dev, HIFN_1_PLL, pllcfg |
  796. HIFN_PLL_PK_CLK_HBI | HIFN_PLL_PE_CLK_HBI);
  797. /* Switch the engines to the PLL */
  798. hifn_write_1(dev, HIFN_1_PLL, pllcfg |
  799. HIFN_PLL_PK_CLK_PLL | HIFN_PLL_PE_CLK_PLL);
  800. /*
  801. * The Fpk_clk runs at half the total speed. Its frequency is needed to
  802. * calculate the minimum time between two reads of the rng. Since 33MHz
  803. * is actually 33.333... we overestimate the frequency here, resulting
  804. * in slightly larger intervals.
  805. */
  806. dev->pk_clk_freq = 1000000 * (freq + 1) * m / 2;
  807. }
  808. static void hifn_init_registers(struct hifn_device *dev)
  809. {
  810. u32 dptr = dev->desc_dma;
  811. /* Initialization magic... */
  812. hifn_write_0(dev, HIFN_0_PUCTRL, HIFN_PUCTRL_DMAENA);
  813. hifn_write_0(dev, HIFN_0_FIFOCNFG, HIFN_FIFOCNFG_THRESHOLD);
  814. hifn_write_0(dev, HIFN_0_PUIER, HIFN_PUIER_DSTOVER);
  815. /* write all 4 ring address registers */
  816. hifn_write_1(dev, HIFN_1_DMA_CRAR, dptr +
  817. offsetof(struct hifn_dma, cmdr[0]));
  818. hifn_write_1(dev, HIFN_1_DMA_SRAR, dptr +
  819. offsetof(struct hifn_dma, srcr[0]));
  820. hifn_write_1(dev, HIFN_1_DMA_DRAR, dptr +
  821. offsetof(struct hifn_dma, dstr[0]));
  822. hifn_write_1(dev, HIFN_1_DMA_RRAR, dptr +
  823. offsetof(struct hifn_dma, resr[0]));
  824. mdelay(2);
  825. #if 0
  826. hifn_write_1(dev, HIFN_1_DMA_CSR,
  827. HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS |
  828. HIFN_DMACSR_S_CTRL_DIS | HIFN_DMACSR_C_CTRL_DIS |
  829. HIFN_DMACSR_D_ABORT | HIFN_DMACSR_D_DONE | HIFN_DMACSR_D_LAST |
  830. HIFN_DMACSR_D_WAIT | HIFN_DMACSR_D_OVER |
  831. HIFN_DMACSR_R_ABORT | HIFN_DMACSR_R_DONE | HIFN_DMACSR_R_LAST |
  832. HIFN_DMACSR_R_WAIT | HIFN_DMACSR_R_OVER |
  833. HIFN_DMACSR_S_ABORT | HIFN_DMACSR_S_DONE | HIFN_DMACSR_S_LAST |
  834. HIFN_DMACSR_S_WAIT |
  835. HIFN_DMACSR_C_ABORT | HIFN_DMACSR_C_DONE | HIFN_DMACSR_C_LAST |
  836. HIFN_DMACSR_C_WAIT |
  837. HIFN_DMACSR_ENGINE |
  838. HIFN_DMACSR_PUBDONE);
  839. #else
  840. hifn_write_1(dev, HIFN_1_DMA_CSR,
  841. HIFN_DMACSR_C_CTRL_ENA | HIFN_DMACSR_S_CTRL_ENA |
  842. HIFN_DMACSR_D_CTRL_ENA | HIFN_DMACSR_R_CTRL_ENA |
  843. HIFN_DMACSR_D_ABORT | HIFN_DMACSR_D_DONE | HIFN_DMACSR_D_LAST |
  844. HIFN_DMACSR_D_WAIT | HIFN_DMACSR_D_OVER |
  845. HIFN_DMACSR_R_ABORT | HIFN_DMACSR_R_DONE | HIFN_DMACSR_R_LAST |
  846. HIFN_DMACSR_R_WAIT | HIFN_DMACSR_R_OVER |
  847. HIFN_DMACSR_S_ABORT | HIFN_DMACSR_S_DONE | HIFN_DMACSR_S_LAST |
  848. HIFN_DMACSR_S_WAIT |
  849. HIFN_DMACSR_C_ABORT | HIFN_DMACSR_C_DONE | HIFN_DMACSR_C_LAST |
  850. HIFN_DMACSR_C_WAIT |
  851. HIFN_DMACSR_ENGINE |
  852. HIFN_DMACSR_PUBDONE);
  853. #endif
  854. hifn_read_1(dev, HIFN_1_DMA_CSR);
  855. dev->dmareg |= HIFN_DMAIER_R_DONE | HIFN_DMAIER_C_ABORT |
  856. HIFN_DMAIER_D_OVER | HIFN_DMAIER_R_OVER |
  857. HIFN_DMAIER_S_ABORT | HIFN_DMAIER_D_ABORT | HIFN_DMAIER_R_ABORT |
  858. HIFN_DMAIER_ENGINE;
  859. dev->dmareg &= ~HIFN_DMAIER_C_WAIT;
  860. hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
  861. hifn_read_1(dev, HIFN_1_DMA_IER);
  862. #if 0
  863. hifn_write_0(dev, HIFN_0_PUCNFG, HIFN_PUCNFG_ENCCNFG |
  864. HIFN_PUCNFG_DRFR_128 | HIFN_PUCNFG_TCALLPHASES |
  865. HIFN_PUCNFG_TCDRVTOTEM | HIFN_PUCNFG_BUS32 |
  866. HIFN_PUCNFG_DRAM);
  867. #else
  868. hifn_write_0(dev, HIFN_0_PUCNFG, 0x10342);
  869. #endif
  870. hifn_init_pll(dev);
  871. hifn_write_0(dev, HIFN_0_PUISR, HIFN_PUISR_DSTOVER);
  872. hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
  873. HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE | HIFN_DMACNFG_LAST |
  874. ((HIFN_POLL_FREQUENCY << 16 ) & HIFN_DMACNFG_POLLFREQ) |
  875. ((HIFN_POLL_SCALAR << 8) & HIFN_DMACNFG_POLLINVAL));
  876. }
  877. static int hifn_setup_base_command(struct hifn_device *dev, u8 *buf,
  878. unsigned dlen, unsigned slen, u16 mask, u8 snum)
  879. {
  880. struct hifn_base_command *base_cmd;
  881. u8 *buf_pos = buf;
  882. base_cmd = (struct hifn_base_command *)buf_pos;
  883. base_cmd->masks = __cpu_to_le16(mask);
  884. base_cmd->total_source_count =
  885. __cpu_to_le16(slen & HIFN_BASE_CMD_LENMASK_LO);
  886. base_cmd->total_dest_count =
  887. __cpu_to_le16(dlen & HIFN_BASE_CMD_LENMASK_LO);
  888. dlen >>= 16;
  889. slen >>= 16;
  890. base_cmd->session_num = __cpu_to_le16(snum |
  891. ((slen << HIFN_BASE_CMD_SRCLEN_S) & HIFN_BASE_CMD_SRCLEN_M) |
  892. ((dlen << HIFN_BASE_CMD_DSTLEN_S) & HIFN_BASE_CMD_DSTLEN_M));
  893. return sizeof(struct hifn_base_command);
  894. }
  895. static int hifn_setup_crypto_command(struct hifn_device *dev,
  896. u8 *buf, unsigned dlen, unsigned slen,
  897. u8 *key, int keylen, u8 *iv, int ivsize, u16 mode)
  898. {
  899. struct hifn_dma *dma = dev->desc_virt;
  900. struct hifn_crypt_command *cry_cmd;
  901. u8 *buf_pos = buf;
  902. u16 cmd_len;
  903. cry_cmd = (struct hifn_crypt_command *)buf_pos;
  904. cry_cmd->source_count = __cpu_to_le16(dlen & 0xffff);
  905. dlen >>= 16;
  906. cry_cmd->masks = __cpu_to_le16(mode |
  907. ((dlen << HIFN_CRYPT_CMD_SRCLEN_S) &
  908. HIFN_CRYPT_CMD_SRCLEN_M));
  909. cry_cmd->header_skip = 0;
  910. cry_cmd->reserved = 0;
  911. buf_pos += sizeof(struct hifn_crypt_command);
  912. dma->cmdu++;
  913. if (dma->cmdu > 1) {
  914. dev->dmareg |= HIFN_DMAIER_C_WAIT;
  915. hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
  916. }
  917. if (keylen) {
  918. memcpy(buf_pos, key, keylen);
  919. buf_pos += keylen;
  920. }
  921. if (ivsize) {
  922. memcpy(buf_pos, iv, ivsize);
  923. buf_pos += ivsize;
  924. }
  925. cmd_len = buf_pos - buf;
  926. return cmd_len;
  927. }
  928. static int hifn_setup_cmd_desc(struct hifn_device *dev,
  929. struct hifn_context *ctx, struct hifn_request_context *rctx,
  930. void *priv, unsigned int nbytes)
  931. {
  932. struct hifn_dma *dma = dev->desc_virt;
  933. int cmd_len, sa_idx;
  934. u8 *buf, *buf_pos;
  935. u16 mask;
  936. sa_idx = dma->cmdi;
  937. buf_pos = buf = dma->command_bufs[dma->cmdi];
  938. mask = 0;
  939. switch (rctx->op) {
  940. case ACRYPTO_OP_DECRYPT:
  941. mask = HIFN_BASE_CMD_CRYPT | HIFN_BASE_CMD_DECODE;
  942. break;
  943. case ACRYPTO_OP_ENCRYPT:
  944. mask = HIFN_BASE_CMD_CRYPT;
  945. break;
  946. case ACRYPTO_OP_HMAC:
  947. mask = HIFN_BASE_CMD_MAC;
  948. break;
  949. default:
  950. goto err_out;
  951. }
  952. buf_pos += hifn_setup_base_command(dev, buf_pos, nbytes,
  953. nbytes, mask, dev->snum);
  954. if (rctx->op == ACRYPTO_OP_ENCRYPT || rctx->op == ACRYPTO_OP_DECRYPT) {
  955. u16 md = 0;
  956. if (ctx->keysize)
  957. md |= HIFN_CRYPT_CMD_NEW_KEY;
  958. if (rctx->iv && rctx->mode != ACRYPTO_MODE_ECB)
  959. md |= HIFN_CRYPT_CMD_NEW_IV;
  960. switch (rctx->mode) {
  961. case ACRYPTO_MODE_ECB:
  962. md |= HIFN_CRYPT_CMD_MODE_ECB;
  963. break;
  964. case ACRYPTO_MODE_CBC:
  965. md |= HIFN_CRYPT_CMD_MODE_CBC;
  966. break;
  967. case ACRYPTO_MODE_CFB:
  968. md |= HIFN_CRYPT_CMD_MODE_CFB;
  969. break;
  970. case ACRYPTO_MODE_OFB:
  971. md |= HIFN_CRYPT_CMD_MODE_OFB;
  972. break;
  973. default:
  974. goto err_out;
  975. }
  976. switch (rctx->type) {
  977. case ACRYPTO_TYPE_AES_128:
  978. if (ctx->keysize != 16)
  979. goto err_out;
  980. md |= HIFN_CRYPT_CMD_KSZ_128 |
  981. HIFN_CRYPT_CMD_ALG_AES;
  982. break;
  983. case ACRYPTO_TYPE_AES_192:
  984. if (ctx->keysize != 24)
  985. goto err_out;
  986. md |= HIFN_CRYPT_CMD_KSZ_192 |
  987. HIFN_CRYPT_CMD_ALG_AES;
  988. break;
  989. case ACRYPTO_TYPE_AES_256:
  990. if (ctx->keysize != 32)
  991. goto err_out;
  992. md |= HIFN_CRYPT_CMD_KSZ_256 |
  993. HIFN_CRYPT_CMD_ALG_AES;
  994. break;
  995. case ACRYPTO_TYPE_3DES:
  996. if (ctx->keysize != 24)
  997. goto err_out;
  998. md |= HIFN_CRYPT_CMD_ALG_3DES;
  999. break;
  1000. case ACRYPTO_TYPE_DES:
  1001. if (ctx->keysize != 8)
  1002. goto err_out;
  1003. md |= HIFN_CRYPT_CMD_ALG_DES;
  1004. break;
  1005. default:
  1006. goto err_out;
  1007. }
  1008. buf_pos += hifn_setup_crypto_command(dev, buf_pos,
  1009. nbytes, nbytes, ctx->key, ctx->keysize,
  1010. rctx->iv, rctx->ivsize, md);
  1011. }
  1012. dev->sa[sa_idx] = priv;
  1013. dev->started++;
  1014. cmd_len = buf_pos - buf;
  1015. dma->cmdr[dma->cmdi].l = __cpu_to_le32(cmd_len | HIFN_D_VALID |
  1016. HIFN_D_LAST | HIFN_D_MASKDONEIRQ);
  1017. if (++dma->cmdi == HIFN_D_CMD_RSIZE) {
  1018. dma->cmdr[dma->cmdi].l = __cpu_to_le32(
  1019. HIFN_D_VALID | HIFN_D_LAST |
  1020. HIFN_D_MASKDONEIRQ | HIFN_D_JUMP);
  1021. dma->cmdi = 0;
  1022. } else {
  1023. dma->cmdr[dma->cmdi - 1].l |= __cpu_to_le32(HIFN_D_VALID);
  1024. }
  1025. if (!(dev->flags & HIFN_FLAG_CMD_BUSY)) {
  1026. hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_C_CTRL_ENA);
  1027. dev->flags |= HIFN_FLAG_CMD_BUSY;
  1028. }
  1029. return 0;
  1030. err_out:
  1031. return -EINVAL;
  1032. }
  1033. static int hifn_setup_src_desc(struct hifn_device *dev, struct page *page,
  1034. unsigned int offset, unsigned int size, int last)
  1035. {
  1036. struct hifn_dma *dma = dev->desc_virt;
  1037. int idx;
  1038. dma_addr_t addr;
  1039. addr = dma_map_page(&dev->pdev->dev, page, offset, size,
  1040. DMA_TO_DEVICE);
  1041. idx = dma->srci;
  1042. dma->srcr[idx].p = __cpu_to_le32(addr);
  1043. dma->srcr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
  1044. HIFN_D_MASKDONEIRQ | (last ? HIFN_D_LAST : 0));
  1045. if (++idx == HIFN_D_SRC_RSIZE) {
  1046. dma->srcr[idx].l = __cpu_to_le32(HIFN_D_VALID |
  1047. HIFN_D_JUMP | HIFN_D_MASKDONEIRQ |
  1048. (last ? HIFN_D_LAST : 0));
  1049. idx = 0;
  1050. }
  1051. dma->srci = idx;
  1052. dma->srcu++;
  1053. if (!(dev->flags & HIFN_FLAG_SRC_BUSY)) {
  1054. hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_S_CTRL_ENA);
  1055. dev->flags |= HIFN_FLAG_SRC_BUSY;
  1056. }
  1057. return size;
  1058. }
  1059. static void hifn_setup_res_desc(struct hifn_device *dev)
  1060. {
  1061. struct hifn_dma *dma = dev->desc_virt;
  1062. dma->resr[dma->resi].l = __cpu_to_le32(HIFN_USED_RESULT |
  1063. HIFN_D_VALID | HIFN_D_LAST);
  1064. /*
  1065. * dma->resr[dma->resi].l = __cpu_to_le32(HIFN_MAX_RESULT | HIFN_D_VALID |
  1066. * HIFN_D_LAST);
  1067. */
  1068. if (++dma->resi == HIFN_D_RES_RSIZE) {
  1069. dma->resr[HIFN_D_RES_RSIZE].l = __cpu_to_le32(HIFN_D_VALID |
  1070. HIFN_D_JUMP | HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
  1071. dma->resi = 0;
  1072. }
  1073. dma->resu++;
  1074. if (!(dev->flags & HIFN_FLAG_RES_BUSY)) {
  1075. hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_R_CTRL_ENA);
  1076. dev->flags |= HIFN_FLAG_RES_BUSY;
  1077. }
  1078. }
  1079. static void hifn_setup_dst_desc(struct hifn_device *dev, struct page *page,
  1080. unsigned offset, unsigned size, int last)
  1081. {
  1082. struct hifn_dma *dma = dev->desc_virt;
  1083. int idx;
  1084. dma_addr_t addr;
  1085. addr = dma_map_page(&dev->pdev->dev, page, offset, size,
  1086. DMA_FROM_DEVICE);
  1087. idx = dma->dsti;
  1088. dma->dstr[idx].p = __cpu_to_le32(addr);
  1089. dma->dstr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
  1090. HIFN_D_MASKDONEIRQ | (last ? HIFN_D_LAST : 0));
  1091. if (++idx == HIFN_D_DST_RSIZE) {
  1092. dma->dstr[idx].l = __cpu_to_le32(HIFN_D_VALID |
  1093. HIFN_D_JUMP | HIFN_D_MASKDONEIRQ |
  1094. (last ? HIFN_D_LAST : 0));
  1095. idx = 0;
  1096. }
  1097. dma->dsti = idx;
  1098. dma->dstu++;
  1099. if (!(dev->flags & HIFN_FLAG_DST_BUSY)) {
  1100. hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_D_CTRL_ENA);
  1101. dev->flags |= HIFN_FLAG_DST_BUSY;
  1102. }
  1103. }
  1104. static int hifn_setup_dma(struct hifn_device *dev,
  1105. struct hifn_context *ctx, struct hifn_request_context *rctx,
  1106. struct scatterlist *src, struct scatterlist *dst,
  1107. unsigned int nbytes, void *priv)
  1108. {
  1109. struct scatterlist *t;
  1110. struct page *spage, *dpage;
  1111. unsigned int soff, doff;
  1112. unsigned int n, len;
  1113. n = nbytes;
  1114. while (n) {
  1115. spage = sg_page(src);
  1116. soff = src->offset;
  1117. len = min(src->length, n);
  1118. hifn_setup_src_desc(dev, spage, soff, len, n - len == 0);
  1119. src++;
  1120. n -= len;
  1121. }
  1122. t = &rctx->walk.cache[0];
  1123. n = nbytes;
  1124. while (n) {
  1125. if (t->length && rctx->walk.flags & ASYNC_FLAGS_MISALIGNED) {
  1126. BUG_ON(!sg_page(t));
  1127. dpage = sg_page(t);
  1128. doff = 0;
  1129. len = t->length;
  1130. } else {
  1131. BUG_ON(!sg_page(dst));
  1132. dpage = sg_page(dst);
  1133. doff = dst->offset;
  1134. len = dst->length;
  1135. }
  1136. len = min(len, n);
  1137. hifn_setup_dst_desc(dev, dpage, doff, len, n - len == 0);
  1138. dst++;
  1139. t++;
  1140. n -= len;
  1141. }
  1142. hifn_setup_cmd_desc(dev, ctx, rctx, priv, nbytes);
  1143. hifn_setup_res_desc(dev);
  1144. return 0;
  1145. }
  1146. static int hifn_cipher_walk_init(struct hifn_cipher_walk *w,
  1147. int num, gfp_t gfp_flags)
  1148. {
  1149. int i;
  1150. num = min(ASYNC_SCATTERLIST_CACHE, num);
  1151. sg_init_table(w->cache, num);
  1152. w->num = 0;
  1153. for (i = 0; i < num; ++i) {
  1154. struct page *page = alloc_page(gfp_flags);
  1155. struct scatterlist *s;
  1156. if (!page)
  1157. break;
  1158. s = &w->cache[i];
  1159. sg_set_page(s, page, PAGE_SIZE, 0);
  1160. w->num++;
  1161. }
  1162. return i;
  1163. }
  1164. static void hifn_cipher_walk_exit(struct hifn_cipher_walk *w)
  1165. {
  1166. int i;
  1167. for (i = 0; i < w->num; ++i) {
  1168. struct scatterlist *s = &w->cache[i];
  1169. __free_page(sg_page(s));
  1170. s->length = 0;
  1171. }
  1172. w->num = 0;
  1173. }
  1174. static int skcipher_add(unsigned int *drestp, struct scatterlist *dst,
  1175. unsigned int size, unsigned int *nbytesp)
  1176. {
  1177. unsigned int copy, drest = *drestp, nbytes = *nbytesp;
  1178. int idx = 0;
  1179. if (drest < size || size > nbytes)
  1180. return -EINVAL;
  1181. while (size) {
  1182. copy = min3(drest, size, dst->length);
  1183. size -= copy;
  1184. drest -= copy;
  1185. nbytes -= copy;
  1186. pr_debug("%s: copy: %u, size: %u, drest: %u, nbytes: %u.\n",
  1187. __func__, copy, size, drest, nbytes);
  1188. dst++;
  1189. idx++;
  1190. }
  1191. *nbytesp = nbytes;
  1192. *drestp = drest;
  1193. return idx;
  1194. }
  1195. static int hifn_cipher_walk(struct skcipher_request *req,
  1196. struct hifn_cipher_walk *w)
  1197. {
  1198. struct scatterlist *dst, *t;
  1199. unsigned int nbytes = req->cryptlen, offset, copy, diff;
  1200. int idx, tidx, err;
  1201. tidx = idx = 0;
  1202. offset = 0;
  1203. while (nbytes) {
  1204. if (idx >= w->num && (w->flags & ASYNC_FLAGS_MISALIGNED))
  1205. return -EINVAL;
  1206. dst = &req->dst[idx];
  1207. pr_debug("\n%s: dlen: %u, doff: %u, offset: %u, nbytes: %u.\n",
  1208. __func__, dst->length, dst->offset, offset, nbytes);
  1209. if (!IS_ALIGNED(dst->offset, HIFN_D_DST_DALIGN) ||
  1210. !IS_ALIGNED(dst->length, HIFN_D_DST_DALIGN) ||
  1211. offset) {
  1212. unsigned slen = min(dst->length - offset, nbytes);
  1213. unsigned dlen = PAGE_SIZE;
  1214. t = &w->cache[idx];
  1215. err = skcipher_add(&dlen, dst, slen, &nbytes);
  1216. if (err < 0)
  1217. return err;
  1218. idx += err;
  1219. copy = slen & ~(HIFN_D_DST_DALIGN - 1);
  1220. diff = slen & (HIFN_D_DST_DALIGN - 1);
  1221. if (dlen < nbytes) {
  1222. /*
  1223. * Destination page does not have enough space
  1224. * to put there additional blocksized chunk,
  1225. * so we mark that page as containing only
  1226. * blocksize aligned chunks:
  1227. * t->length = (slen & ~(HIFN_D_DST_DALIGN - 1));
  1228. * and increase number of bytes to be processed
  1229. * in next chunk:
  1230. * nbytes += diff;
  1231. */
  1232. nbytes += diff;
  1233. /*
  1234. * Temporary of course...
  1235. * Kick author if you will catch this one.
  1236. */
  1237. pr_err("%s: dlen: %u, nbytes: %u, slen: %u, offset: %u.\n",
  1238. __func__, dlen, nbytes, slen, offset);
  1239. pr_err("%s: please contact author to fix this "
  1240. "issue, generally you should not catch "
  1241. "this path under any condition but who "
  1242. "knows how did you use crypto code.\n"
  1243. "Thank you.\n", __func__);
  1244. BUG();
  1245. } else {
  1246. copy += diff + nbytes;
  1247. dst = &req->dst[idx];
  1248. err = skcipher_add(&dlen, dst, nbytes, &nbytes);
  1249. if (err < 0)
  1250. return err;
  1251. idx += err;
  1252. }
  1253. t->length = copy;
  1254. t->offset = offset;
  1255. } else {
  1256. nbytes -= min(dst->length, nbytes);
  1257. idx++;
  1258. }
  1259. tidx++;
  1260. }
  1261. return tidx;
  1262. }
  1263. static int hifn_setup_session(struct skcipher_request *req)
  1264. {
  1265. struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
  1266. struct hifn_request_context *rctx = skcipher_request_ctx(req);
  1267. struct hifn_device *dev = ctx->dev;
  1268. unsigned long dlen, flags;
  1269. unsigned int nbytes = req->cryptlen, idx = 0;
  1270. int err = -EINVAL, sg_num;
  1271. struct scatterlist *dst;
  1272. if (rctx->iv && !rctx->ivsize && rctx->mode != ACRYPTO_MODE_ECB)
  1273. goto err_out_exit;
  1274. rctx->walk.flags = 0;
  1275. while (nbytes) {
  1276. dst = &req->dst[idx];
  1277. dlen = min(dst->length, nbytes);
  1278. if (!IS_ALIGNED(dst->offset, HIFN_D_DST_DALIGN) ||
  1279. !IS_ALIGNED(dlen, HIFN_D_DST_DALIGN))
  1280. rctx->walk.flags |= ASYNC_FLAGS_MISALIGNED;
  1281. nbytes -= dlen;
  1282. idx++;
  1283. }
  1284. if (rctx->walk.flags & ASYNC_FLAGS_MISALIGNED) {
  1285. err = hifn_cipher_walk_init(&rctx->walk, idx, GFP_ATOMIC);
  1286. if (err < 0)
  1287. return err;
  1288. }
  1289. sg_num = hifn_cipher_walk(req, &rctx->walk);
  1290. if (sg_num < 0) {
  1291. err = sg_num;
  1292. goto err_out_exit;
  1293. }
  1294. spin_lock_irqsave(&dev->lock, flags);
  1295. if (dev->started + sg_num > HIFN_QUEUE_LENGTH) {
  1296. err = -EAGAIN;
  1297. goto err_out;
  1298. }
  1299. err = hifn_setup_dma(dev, ctx, rctx, req->src, req->dst, req->cryptlen, req);
  1300. if (err)
  1301. goto err_out;
  1302. dev->snum++;
  1303. dev->active = HIFN_DEFAULT_ACTIVE_NUM;
  1304. spin_unlock_irqrestore(&dev->lock, flags);
  1305. return 0;
  1306. err_out:
  1307. spin_unlock_irqrestore(&dev->lock, flags);
  1308. err_out_exit:
  1309. if (err) {
  1310. dev_info(&dev->pdev->dev, "iv: %p [%d], key: %p [%d], mode: %u, op: %u, "
  1311. "type: %u, err: %d.\n",
  1312. rctx->iv, rctx->ivsize,
  1313. ctx->key, ctx->keysize,
  1314. rctx->mode, rctx->op, rctx->type, err);
  1315. }
  1316. return err;
  1317. }
  1318. static int hifn_start_device(struct hifn_device *dev)
  1319. {
  1320. int err;
  1321. dev->started = dev->active = 0;
  1322. hifn_reset_dma(dev, 1);
  1323. err = hifn_enable_crypto(dev);
  1324. if (err)
  1325. return err;
  1326. hifn_reset_puc(dev);
  1327. hifn_init_dma(dev);
  1328. hifn_init_registers(dev);
  1329. hifn_init_pubrng(dev);
  1330. return 0;
  1331. }
  1332. static int skcipher_get(void *saddr, unsigned int *srestp, unsigned int offset,
  1333. struct scatterlist *dst, unsigned int size, unsigned int *nbytesp)
  1334. {
  1335. unsigned int srest = *srestp, nbytes = *nbytesp, copy;
  1336. void *daddr;
  1337. int idx = 0;
  1338. if (srest < size || size > nbytes)
  1339. return -EINVAL;
  1340. while (size) {
  1341. copy = min3(srest, dst->length, size);
  1342. daddr = kmap_atomic(sg_page(dst));
  1343. memcpy(daddr + dst->offset + offset, saddr, copy);
  1344. kunmap_atomic(daddr);
  1345. nbytes -= copy;
  1346. size -= copy;
  1347. srest -= copy;
  1348. saddr += copy;
  1349. offset = 0;
  1350. pr_debug("%s: copy: %u, size: %u, srest: %u, nbytes: %u.\n",
  1351. __func__, copy, size, srest, nbytes);
  1352. dst++;
  1353. idx++;
  1354. }
  1355. *nbytesp = nbytes;
  1356. *srestp = srest;
  1357. return idx;
  1358. }
  1359. static inline void hifn_complete_sa(struct hifn_device *dev, int i)
  1360. {
  1361. unsigned long flags;
  1362. spin_lock_irqsave(&dev->lock, flags);
  1363. dev->sa[i] = NULL;
  1364. dev->started--;
  1365. if (dev->started < 0)
  1366. dev_info(&dev->pdev->dev, "%s: started: %d.\n", __func__,
  1367. dev->started);
  1368. spin_unlock_irqrestore(&dev->lock, flags);
  1369. BUG_ON(dev->started < 0);
  1370. }
  1371. static void hifn_process_ready(struct skcipher_request *req, int error)
  1372. {
  1373. struct hifn_request_context *rctx = skcipher_request_ctx(req);
  1374. if (rctx->walk.flags & ASYNC_FLAGS_MISALIGNED) {
  1375. unsigned int nbytes = req->cryptlen;
  1376. int idx = 0, err;
  1377. struct scatterlist *dst, *t;
  1378. void *saddr;
  1379. while (nbytes) {
  1380. t = &rctx->walk.cache[idx];
  1381. dst = &req->dst[idx];
  1382. pr_debug("\n%s: sg_page(t): %p, t->length: %u, "
  1383. "sg_page(dst): %p, dst->length: %u, "
  1384. "nbytes: %u.\n",
  1385. __func__, sg_page(t), t->length,
  1386. sg_page(dst), dst->length, nbytes);
  1387. if (!t->length) {
  1388. nbytes -= min(dst->length, nbytes);
  1389. idx++;
  1390. continue;
  1391. }
  1392. saddr = kmap_atomic(sg_page(t));
  1393. err = skcipher_get(saddr, &t->length, t->offset,
  1394. dst, nbytes, &nbytes);
  1395. if (err < 0) {
  1396. kunmap_atomic(saddr);
  1397. break;
  1398. }
  1399. idx += err;
  1400. kunmap_atomic(saddr);
  1401. }
  1402. hifn_cipher_walk_exit(&rctx->walk);
  1403. }
  1404. skcipher_request_complete(req, error);
  1405. }
  1406. static void hifn_clear_rings(struct hifn_device *dev, int error)
  1407. {
  1408. struct hifn_dma *dma = dev->desc_virt;
  1409. int i, u;
  1410. dev_dbg(&dev->pdev->dev, "ring cleanup 1: i: %d.%d.%d.%d, u: %d.%d.%d.%d, "
  1411. "k: %d.%d.%d.%d.\n",
  1412. dma->cmdi, dma->srci, dma->dsti, dma->resi,
  1413. dma->cmdu, dma->srcu, dma->dstu, dma->resu,
  1414. dma->cmdk, dma->srck, dma->dstk, dma->resk);
  1415. i = dma->resk; u = dma->resu;
  1416. while (u != 0) {
  1417. if (dma->resr[i].l & __cpu_to_le32(HIFN_D_VALID))
  1418. break;
  1419. if (dev->sa[i]) {
  1420. dev->success++;
  1421. dev->reset = 0;
  1422. hifn_process_ready(dev->sa[i], error);
  1423. hifn_complete_sa(dev, i);
  1424. }
  1425. if (++i == HIFN_D_RES_RSIZE)
  1426. i = 0;
  1427. u--;
  1428. }
  1429. dma->resk = i; dma->resu = u;
  1430. i = dma->srck; u = dma->srcu;
  1431. while (u != 0) {
  1432. if (dma->srcr[i].l & __cpu_to_le32(HIFN_D_VALID))
  1433. break;
  1434. if (++i == HIFN_D_SRC_RSIZE)
  1435. i = 0;
  1436. u--;
  1437. }
  1438. dma->srck = i; dma->srcu = u;
  1439. i = dma->cmdk; u = dma->cmdu;
  1440. while (u != 0) {
  1441. if (dma->cmdr[i].l & __cpu_to_le32(HIFN_D_VALID))
  1442. break;
  1443. if (++i == HIFN_D_CMD_RSIZE)
  1444. i = 0;
  1445. u--;
  1446. }
  1447. dma->cmdk = i; dma->cmdu = u;
  1448. i = dma->dstk; u = dma->dstu;
  1449. while (u != 0) {
  1450. if (dma->dstr[i].l & __cpu_to_le32(HIFN_D_VALID))
  1451. break;
  1452. if (++i == HIFN_D_DST_RSIZE)
  1453. i = 0;
  1454. u--;
  1455. }
  1456. dma->dstk = i; dma->dstu = u;
  1457. dev_dbg(&dev->pdev->dev, "ring cleanup 2: i: %d.%d.%d.%d, u: %d.%d.%d.%d, "
  1458. "k: %d.%d.%d.%d.\n",
  1459. dma->cmdi, dma->srci, dma->dsti, dma->resi,
  1460. dma->cmdu, dma->srcu, dma->dstu, dma->resu,
  1461. dma->cmdk, dma->srck, dma->dstk, dma->resk);
  1462. }
  1463. static void hifn_work(struct work_struct *work)
  1464. {
  1465. struct delayed_work *dw = to_delayed_work(work);
  1466. struct hifn_device *dev = container_of(dw, struct hifn_device, work);
  1467. unsigned long flags;
  1468. int reset = 0;
  1469. u32 r = 0;
  1470. spin_lock_irqsave(&dev->lock, flags);
  1471. if (dev->active == 0) {
  1472. struct hifn_dma *dma = dev->desc_virt;
  1473. if (dma->cmdu == 0 && (dev->flags & HIFN_FLAG_CMD_BUSY)) {
  1474. dev->flags &= ~HIFN_FLAG_CMD_BUSY;
  1475. r |= HIFN_DMACSR_C_CTRL_DIS;
  1476. }
  1477. if (dma->srcu == 0 && (dev->flags & HIFN_FLAG_SRC_BUSY)) {
  1478. dev->flags &= ~HIFN_FLAG_SRC_BUSY;
  1479. r |= HIFN_DMACSR_S_CTRL_DIS;
  1480. }
  1481. if (dma->dstu == 0 && (dev->flags & HIFN_FLAG_DST_BUSY)) {
  1482. dev->flags &= ~HIFN_FLAG_DST_BUSY;
  1483. r |= HIFN_DMACSR_D_CTRL_DIS;
  1484. }
  1485. if (dma->resu == 0 && (dev->flags & HIFN_FLAG_RES_BUSY)) {
  1486. dev->flags &= ~HIFN_FLAG_RES_BUSY;
  1487. r |= HIFN_DMACSR_R_CTRL_DIS;
  1488. }
  1489. if (r)
  1490. hifn_write_1(dev, HIFN_1_DMA_CSR, r);
  1491. } else
  1492. dev->active--;
  1493. if ((dev->prev_success == dev->success) && dev->started)
  1494. reset = 1;
  1495. dev->prev_success = dev->success;
  1496. spin_unlock_irqrestore(&dev->lock, flags);
  1497. if (reset) {
  1498. if (++dev->reset >= 5) {
  1499. int i;
  1500. struct hifn_dma *dma = dev->desc_virt;
  1501. dev_info(&dev->pdev->dev,
  1502. "r: %08x, active: %d, started: %d, "
  1503. "success: %lu: qlen: %u/%u, reset: %d.\n",
  1504. r, dev->active, dev->started,
  1505. dev->success, dev->queue.qlen, dev->queue.max_qlen,
  1506. reset);
  1507. dev_info(&dev->pdev->dev, "%s: res: ", __func__);
  1508. for (i = 0; i < HIFN_D_RES_RSIZE; ++i) {
  1509. pr_info("%x.%p ", dma->resr[i].l, dev->sa[i]);
  1510. if (dev->sa[i]) {
  1511. hifn_process_ready(dev->sa[i], -ENODEV);
  1512. hifn_complete_sa(dev, i);
  1513. }
  1514. }
  1515. pr_info("\n");
  1516. hifn_reset_dma(dev, 1);
  1517. hifn_stop_device(dev);
  1518. hifn_start_device(dev);
  1519. dev->reset = 0;
  1520. }
  1521. tasklet_schedule(&dev->tasklet);
  1522. }
  1523. schedule_delayed_work(&dev->work, HZ);
  1524. }
  1525. static irqreturn_t hifn_interrupt(int irq, void *data)
  1526. {
  1527. struct hifn_device *dev = data;
  1528. struct hifn_dma *dma = dev->desc_virt;
  1529. u32 dmacsr, restart;
  1530. dmacsr = hifn_read_1(dev, HIFN_1_DMA_CSR);
  1531. dev_dbg(&dev->pdev->dev, "1 dmacsr: %08x, dmareg: %08x, res: %08x [%d], "
  1532. "i: %d.%d.%d.%d, u: %d.%d.%d.%d.\n",
  1533. dmacsr, dev->dmareg, dmacsr & dev->dmareg, dma->cmdi,
  1534. dma->cmdi, dma->srci, dma->dsti, dma->resi,
  1535. dma->cmdu, dma->srcu, dma->dstu, dma->resu);
  1536. if ((dmacsr & dev->dmareg) == 0)
  1537. return IRQ_NONE;
  1538. hifn_write_1(dev, HIFN_1_DMA_CSR, dmacsr & dev->dmareg);
  1539. if (dmacsr & HIFN_DMACSR_ENGINE)
  1540. hifn_write_0(dev, HIFN_0_PUISR, hifn_read_0(dev, HIFN_0_PUISR));
  1541. if (dmacsr & HIFN_DMACSR_PUBDONE)
  1542. hifn_write_1(dev, HIFN_1_PUB_STATUS,
  1543. hifn_read_1(dev, HIFN_1_PUB_STATUS) | HIFN_PUBSTS_DONE);
  1544. restart = dmacsr & (HIFN_DMACSR_R_OVER | HIFN_DMACSR_D_OVER);
  1545. if (restart) {
  1546. u32 puisr = hifn_read_0(dev, HIFN_0_PUISR);
  1547. dev_warn(&dev->pdev->dev, "overflow: r: %d, d: %d, puisr: %08x, d: %u.\n",
  1548. !!(dmacsr & HIFN_DMACSR_R_OVER),
  1549. !!(dmacsr & HIFN_DMACSR_D_OVER),
  1550. puisr, !!(puisr & HIFN_PUISR_DSTOVER));
  1551. if (!!(puisr & HIFN_PUISR_DSTOVER))
  1552. hifn_write_0(dev, HIFN_0_PUISR, HIFN_PUISR_DSTOVER);
  1553. hifn_write_1(dev, HIFN_1_DMA_CSR, dmacsr & (HIFN_DMACSR_R_OVER |
  1554. HIFN_DMACSR_D_OVER));
  1555. }
  1556. restart = dmacsr & (HIFN_DMACSR_C_ABORT | HIFN_DMACSR_S_ABORT |
  1557. HIFN_DMACSR_D_ABORT | HIFN_DMACSR_R_ABORT);
  1558. if (restart) {
  1559. dev_warn(&dev->pdev->dev, "abort: c: %d, s: %d, d: %d, r: %d.\n",
  1560. !!(dmacsr & HIFN_DMACSR_C_ABORT),
  1561. !!(dmacsr & HIFN_DMACSR_S_ABORT),
  1562. !!(dmacsr & HIFN_DMACSR_D_ABORT),
  1563. !!(dmacsr & HIFN_DMACSR_R_ABORT));
  1564. hifn_reset_dma(dev, 1);
  1565. hifn_init_dma(dev);
  1566. hifn_init_registers(dev);
  1567. }
  1568. if ((dmacsr & HIFN_DMACSR_C_WAIT) && (dma->cmdu == 0)) {
  1569. dev_dbg(&dev->pdev->dev, "wait on command.\n");
  1570. dev->dmareg &= ~(HIFN_DMAIER_C_WAIT);
  1571. hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
  1572. }
  1573. tasklet_schedule(&dev->tasklet);
  1574. return IRQ_HANDLED;
  1575. }
  1576. static void hifn_flush(struct hifn_device *dev)
  1577. {
  1578. unsigned long flags;
  1579. struct crypto_async_request *async_req;
  1580. struct skcipher_request *req;
  1581. struct hifn_dma *dma = dev->desc_virt;
  1582. int i;
  1583. for (i = 0; i < HIFN_D_RES_RSIZE; ++i) {
  1584. struct hifn_desc *d = &dma->resr[i];
  1585. if (dev->sa[i]) {
  1586. hifn_process_ready(dev->sa[i],
  1587. (d->l & __cpu_to_le32(HIFN_D_VALID)) ? -ENODEV : 0);
  1588. hifn_complete_sa(dev, i);
  1589. }
  1590. }
  1591. spin_lock_irqsave(&dev->lock, flags);
  1592. while ((async_req = crypto_dequeue_request(&dev->queue))) {
  1593. req = skcipher_request_cast(async_req);
  1594. spin_unlock_irqrestore(&dev->lock, flags);
  1595. hifn_process_ready(req, -ENODEV);
  1596. spin_lock_irqsave(&dev->lock, flags);
  1597. }
  1598. spin_unlock_irqrestore(&dev->lock, flags);
  1599. }
  1600. static int hifn_setkey(struct crypto_skcipher *cipher, const u8 *key,
  1601. unsigned int len)
  1602. {
  1603. struct hifn_context *ctx = crypto_skcipher_ctx(cipher);
  1604. struct hifn_device *dev = ctx->dev;
  1605. int err;
  1606. err = verify_skcipher_des_key(cipher, key);
  1607. if (err)
  1608. return err;
  1609. dev->flags &= ~HIFN_FLAG_OLD_KEY;
  1610. memcpy(ctx->key, key, len);
  1611. ctx->keysize = len;
  1612. return 0;
  1613. }
  1614. static int hifn_des3_setkey(struct crypto_skcipher *cipher, const u8 *key,
  1615. unsigned int len)
  1616. {
  1617. struct hifn_context *ctx = crypto_skcipher_ctx(cipher);
  1618. struct hifn_device *dev = ctx->dev;
  1619. int err;
  1620. err = verify_skcipher_des3_key(cipher, key);
  1621. if (err)
  1622. return err;
  1623. dev->flags &= ~HIFN_FLAG_OLD_KEY;
  1624. memcpy(ctx->key, key, len);
  1625. ctx->keysize = len;
  1626. return 0;
  1627. }
  1628. static int hifn_handle_req(struct skcipher_request *req)
  1629. {
  1630. struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
  1631. struct hifn_device *dev = ctx->dev;
  1632. int err = -EAGAIN;
  1633. if (dev->started + DIV_ROUND_UP(req->cryptlen, PAGE_SIZE) <= HIFN_QUEUE_LENGTH)
  1634. err = hifn_setup_session(req);
  1635. if (err == -EAGAIN) {
  1636. unsigned long flags;
  1637. spin_lock_irqsave(&dev->lock, flags);
  1638. err = crypto_enqueue_request(&dev->queue, &req->base);
  1639. spin_unlock_irqrestore(&dev->lock, flags);
  1640. }
  1641. return err;
  1642. }
  1643. static int hifn_setup_crypto_req(struct skcipher_request *req, u8 op,
  1644. u8 type, u8 mode)
  1645. {
  1646. struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
  1647. struct hifn_request_context *rctx = skcipher_request_ctx(req);
  1648. unsigned ivsize;
  1649. ivsize = crypto_skcipher_ivsize(crypto_skcipher_reqtfm(req));
  1650. if (req->iv && mode != ACRYPTO_MODE_ECB) {
  1651. if (type == ACRYPTO_TYPE_AES_128)
  1652. ivsize = HIFN_AES_IV_LENGTH;
  1653. else if (type == ACRYPTO_TYPE_DES)
  1654. ivsize = HIFN_DES_KEY_LENGTH;
  1655. else if (type == ACRYPTO_TYPE_3DES)
  1656. ivsize = HIFN_3DES_KEY_LENGTH;
  1657. }
  1658. if (ctx->keysize != 16 && type == ACRYPTO_TYPE_AES_128) {
  1659. if (ctx->keysize == 24)
  1660. type = ACRYPTO_TYPE_AES_192;
  1661. else if (ctx->keysize == 32)
  1662. type = ACRYPTO_TYPE_AES_256;
  1663. }
  1664. rctx->op = op;
  1665. rctx->mode = mode;
  1666. rctx->type = type;
  1667. rctx->iv = req->iv;
  1668. rctx->ivsize = ivsize;
  1669. /*
  1670. * HEAVY TODO: needs to kick Herbert XU to write documentation.
  1671. * HEAVY TODO: needs to kick Herbert XU to write documentation.
  1672. * HEAVY TODO: needs to kick Herbert XU to write documentation.
  1673. */
  1674. return hifn_handle_req(req);
  1675. }
  1676. static int hifn_process_queue(struct hifn_device *dev)
  1677. {
  1678. struct crypto_async_request *async_req, *backlog;
  1679. struct skcipher_request *req;
  1680. unsigned long flags;
  1681. int err = 0;
  1682. while (dev->started < HIFN_QUEUE_LENGTH) {
  1683. spin_lock_irqsave(&dev->lock, flags);
  1684. backlog = crypto_get_backlog(&dev->queue);
  1685. async_req = crypto_dequeue_request(&dev->queue);
  1686. spin_unlock_irqrestore(&dev->lock, flags);
  1687. if (!async_req)
  1688. break;
  1689. if (backlog)
  1690. crypto_request_complete(backlog, -EINPROGRESS);
  1691. req = skcipher_request_cast(async_req);
  1692. err = hifn_handle_req(req);
  1693. if (err)
  1694. break;
  1695. }
  1696. return err;
  1697. }
  1698. static int hifn_setup_crypto(struct skcipher_request *req, u8 op,
  1699. u8 type, u8 mode)
  1700. {
  1701. int err;
  1702. struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
  1703. struct hifn_device *dev = ctx->dev;
  1704. err = hifn_setup_crypto_req(req, op, type, mode);
  1705. if (err)
  1706. return err;
  1707. if (dev->started < HIFN_QUEUE_LENGTH && dev->queue.qlen)
  1708. hifn_process_queue(dev);
  1709. return -EINPROGRESS;
  1710. }
  1711. /*
  1712. * AES ecryption functions.
  1713. */
  1714. static inline int hifn_encrypt_aes_ecb(struct skcipher_request *req)
  1715. {
  1716. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1717. ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_ECB);
  1718. }
  1719. static inline int hifn_encrypt_aes_cbc(struct skcipher_request *req)
  1720. {
  1721. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1722. ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CBC);
  1723. }
  1724. /*
  1725. * AES decryption functions.
  1726. */
  1727. static inline int hifn_decrypt_aes_ecb(struct skcipher_request *req)
  1728. {
  1729. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1730. ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_ECB);
  1731. }
  1732. static inline int hifn_decrypt_aes_cbc(struct skcipher_request *req)
  1733. {
  1734. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1735. ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CBC);
  1736. }
  1737. /*
  1738. * DES ecryption functions.
  1739. */
  1740. static inline int hifn_encrypt_des_ecb(struct skcipher_request *req)
  1741. {
  1742. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1743. ACRYPTO_TYPE_DES, ACRYPTO_MODE_ECB);
  1744. }
  1745. static inline int hifn_encrypt_des_cbc(struct skcipher_request *req)
  1746. {
  1747. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1748. ACRYPTO_TYPE_DES, ACRYPTO_MODE_CBC);
  1749. }
  1750. /*
  1751. * DES decryption functions.
  1752. */
  1753. static inline int hifn_decrypt_des_ecb(struct skcipher_request *req)
  1754. {
  1755. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1756. ACRYPTO_TYPE_DES, ACRYPTO_MODE_ECB);
  1757. }
  1758. static inline int hifn_decrypt_des_cbc(struct skcipher_request *req)
  1759. {
  1760. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1761. ACRYPTO_TYPE_DES, ACRYPTO_MODE_CBC);
  1762. }
  1763. /*
  1764. * 3DES ecryption functions.
  1765. */
  1766. static inline int hifn_encrypt_3des_ecb(struct skcipher_request *req)
  1767. {
  1768. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1769. ACRYPTO_TYPE_3DES, ACRYPTO_MODE_ECB);
  1770. }
  1771. static inline int hifn_encrypt_3des_cbc(struct skcipher_request *req)
  1772. {
  1773. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1774. ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CBC);
  1775. }
  1776. /* 3DES decryption functions. */
  1777. static inline int hifn_decrypt_3des_ecb(struct skcipher_request *req)
  1778. {
  1779. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1780. ACRYPTO_TYPE_3DES, ACRYPTO_MODE_ECB);
  1781. }
  1782. static inline int hifn_decrypt_3des_cbc(struct skcipher_request *req)
  1783. {
  1784. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1785. ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CBC);
  1786. }
  1787. struct hifn_alg_template {
  1788. char name[CRYPTO_MAX_ALG_NAME];
  1789. char drv_name[CRYPTO_MAX_ALG_NAME];
  1790. unsigned int bsize;
  1791. struct skcipher_alg skcipher;
  1792. };
  1793. static const struct hifn_alg_template hifn_alg_templates[] = {
  1794. /*
  1795. * 3DES ECB and CBC modes.
  1796. */
  1797. {
  1798. .name = "cbc(des3_ede)", .drv_name = "cbc-3des", .bsize = 8,
  1799. .skcipher = {
  1800. .ivsize = HIFN_IV_LENGTH,
  1801. .min_keysize = HIFN_3DES_KEY_LENGTH,
  1802. .max_keysize = HIFN_3DES_KEY_LENGTH,
  1803. .setkey = hifn_des3_setkey,
  1804. .encrypt = hifn_encrypt_3des_cbc,
  1805. .decrypt = hifn_decrypt_3des_cbc,
  1806. },
  1807. },
  1808. {
  1809. .name = "ecb(des3_ede)", .drv_name = "ecb-3des", .bsize = 8,
  1810. .skcipher = {
  1811. .min_keysize = HIFN_3DES_KEY_LENGTH,
  1812. .max_keysize = HIFN_3DES_KEY_LENGTH,
  1813. .setkey = hifn_des3_setkey,
  1814. .encrypt = hifn_encrypt_3des_ecb,
  1815. .decrypt = hifn_decrypt_3des_ecb,
  1816. },
  1817. },
  1818. /*
  1819. * DES ECB and CBC modes.
  1820. */
  1821. {
  1822. .name = "cbc(des)", .drv_name = "cbc-des", .bsize = 8,
  1823. .skcipher = {
  1824. .ivsize = HIFN_IV_LENGTH,
  1825. .min_keysize = HIFN_DES_KEY_LENGTH,
  1826. .max_keysize = HIFN_DES_KEY_LENGTH,
  1827. .setkey = hifn_setkey,
  1828. .encrypt = hifn_encrypt_des_cbc,
  1829. .decrypt = hifn_decrypt_des_cbc,
  1830. },
  1831. },
  1832. {
  1833. .name = "ecb(des)", .drv_name = "ecb-des", .bsize = 8,
  1834. .skcipher = {
  1835. .min_keysize = HIFN_DES_KEY_LENGTH,
  1836. .max_keysize = HIFN_DES_KEY_LENGTH,
  1837. .setkey = hifn_setkey,
  1838. .encrypt = hifn_encrypt_des_ecb,
  1839. .decrypt = hifn_decrypt_des_ecb,
  1840. },
  1841. },
  1842. /*
  1843. * AES ECB and CBC modes.
  1844. */
  1845. {
  1846. .name = "ecb(aes)", .drv_name = "ecb-aes", .bsize = 16,
  1847. .skcipher = {
  1848. .min_keysize = AES_MIN_KEY_SIZE,
  1849. .max_keysize = AES_MAX_KEY_SIZE,
  1850. .setkey = hifn_setkey,
  1851. .encrypt = hifn_encrypt_aes_ecb,
  1852. .decrypt = hifn_decrypt_aes_ecb,
  1853. },
  1854. },
  1855. {
  1856. .name = "cbc(aes)", .drv_name = "cbc-aes", .bsize = 16,
  1857. .skcipher = {
  1858. .ivsize = HIFN_AES_IV_LENGTH,
  1859. .min_keysize = AES_MIN_KEY_SIZE,
  1860. .max_keysize = AES_MAX_KEY_SIZE,
  1861. .setkey = hifn_setkey,
  1862. .encrypt = hifn_encrypt_aes_cbc,
  1863. .decrypt = hifn_decrypt_aes_cbc,
  1864. },
  1865. },
  1866. };
  1867. static int hifn_init_tfm(struct crypto_skcipher *tfm)
  1868. {
  1869. struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
  1870. struct hifn_crypto_alg *ha = crypto_alg_to_hifn(alg);
  1871. struct hifn_context *ctx = crypto_skcipher_ctx(tfm);
  1872. ctx->dev = ha->dev;
  1873. crypto_skcipher_set_reqsize(tfm, sizeof(struct hifn_request_context));
  1874. return 0;
  1875. }
  1876. static int hifn_alg_alloc(struct hifn_device *dev, const struct hifn_alg_template *t)
  1877. {
  1878. struct hifn_crypto_alg *alg;
  1879. int err;
  1880. alg = kzalloc(sizeof(*alg), GFP_KERNEL);
  1881. if (!alg)
  1882. return -ENOMEM;
  1883. alg->alg = t->skcipher;
  1884. alg->alg.init = hifn_init_tfm;
  1885. err = -EINVAL;
  1886. if (snprintf(alg->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  1887. "%s", t->name) >= CRYPTO_MAX_ALG_NAME)
  1888. goto out_free_alg;
  1889. if (snprintf(alg->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  1890. "%s-%s", t->drv_name, dev->name) >= CRYPTO_MAX_ALG_NAME)
  1891. goto out_free_alg;
  1892. alg->alg.base.cra_priority = 300;
  1893. alg->alg.base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC;
  1894. alg->alg.base.cra_blocksize = t->bsize;
  1895. alg->alg.base.cra_ctxsize = sizeof(struct hifn_context);
  1896. alg->alg.base.cra_alignmask = 0;
  1897. alg->alg.base.cra_module = THIS_MODULE;
  1898. alg->dev = dev;
  1899. list_add_tail(&alg->entry, &dev->alg_list);
  1900. err = crypto_register_skcipher(&alg->alg);
  1901. if (err) {
  1902. list_del(&alg->entry);
  1903. out_free_alg:
  1904. kfree(alg);
  1905. }
  1906. return err;
  1907. }
  1908. static void hifn_unregister_alg(struct hifn_device *dev)
  1909. {
  1910. struct hifn_crypto_alg *a, *n;
  1911. list_for_each_entry_safe(a, n, &dev->alg_list, entry) {
  1912. list_del(&a->entry);
  1913. crypto_unregister_skcipher(&a->alg);
  1914. kfree(a);
  1915. }
  1916. }
  1917. static int hifn_register_alg(struct hifn_device *dev)
  1918. {
  1919. int i, err;
  1920. for (i = 0; i < ARRAY_SIZE(hifn_alg_templates); ++i) {
  1921. err = hifn_alg_alloc(dev, &hifn_alg_templates[i]);
  1922. if (err)
  1923. goto err_out_exit;
  1924. }
  1925. return 0;
  1926. err_out_exit:
  1927. hifn_unregister_alg(dev);
  1928. return err;
  1929. }
  1930. static void hifn_tasklet_callback(unsigned long data)
  1931. {
  1932. struct hifn_device *dev = (struct hifn_device *)data;
  1933. /*
  1934. * This is ok to call this without lock being held,
  1935. * althogh it modifies some parameters used in parallel,
  1936. * (like dev->success), but they are used in process
  1937. * context or update is atomic (like setting dev->sa[i] to NULL).
  1938. */
  1939. hifn_clear_rings(dev, 0);
  1940. if (dev->started < HIFN_QUEUE_LENGTH && dev->queue.qlen)
  1941. hifn_process_queue(dev);
  1942. }
  1943. static int hifn_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1944. {
  1945. int err, i;
  1946. struct hifn_device *dev;
  1947. char name[8];
  1948. err = pci_enable_device(pdev);
  1949. if (err)
  1950. return err;
  1951. pci_set_master(pdev);
  1952. err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  1953. if (err)
  1954. goto err_out_disable_pci_device;
  1955. snprintf(name, sizeof(name), "hifn%d",
  1956. atomic_inc_return(&hifn_dev_number) - 1);
  1957. err = pci_request_regions(pdev, name);
  1958. if (err)
  1959. goto err_out_disable_pci_device;
  1960. if (pci_resource_len(pdev, 0) < HIFN_BAR0_SIZE ||
  1961. pci_resource_len(pdev, 1) < HIFN_BAR1_SIZE ||
  1962. pci_resource_len(pdev, 2) < HIFN_BAR2_SIZE) {
  1963. dev_err(&pdev->dev, "Broken hardware - I/O regions are too small.\n");
  1964. err = -ENODEV;
  1965. goto err_out_free_regions;
  1966. }
  1967. dev = kzalloc(sizeof(struct hifn_device) + sizeof(struct crypto_alg),
  1968. GFP_KERNEL);
  1969. if (!dev) {
  1970. err = -ENOMEM;
  1971. goto err_out_free_regions;
  1972. }
  1973. INIT_LIST_HEAD(&dev->alg_list);
  1974. snprintf(dev->name, sizeof(dev->name), "%s", name);
  1975. spin_lock_init(&dev->lock);
  1976. for (i = 0; i < 3; ++i) {
  1977. unsigned long addr, size;
  1978. addr = pci_resource_start(pdev, i);
  1979. size = pci_resource_len(pdev, i);
  1980. dev->bar[i] = ioremap(addr, size);
  1981. if (!dev->bar[i]) {
  1982. err = -ENOMEM;
  1983. goto err_out_unmap_bars;
  1984. }
  1985. }
  1986. dev->desc_virt = dma_alloc_coherent(&pdev->dev,
  1987. sizeof(struct hifn_dma),
  1988. &dev->desc_dma, GFP_KERNEL);
  1989. if (!dev->desc_virt) {
  1990. dev_err(&pdev->dev, "Failed to allocate descriptor rings.\n");
  1991. err = -ENOMEM;
  1992. goto err_out_unmap_bars;
  1993. }
  1994. dev->pdev = pdev;
  1995. dev->irq = pdev->irq;
  1996. for (i = 0; i < HIFN_D_RES_RSIZE; ++i)
  1997. dev->sa[i] = NULL;
  1998. pci_set_drvdata(pdev, dev);
  1999. tasklet_init(&dev->tasklet, hifn_tasklet_callback, (unsigned long)dev);
  2000. crypto_init_queue(&dev->queue, 1);
  2001. err = request_irq(dev->irq, hifn_interrupt, IRQF_SHARED, dev->name, dev);
  2002. if (err) {
  2003. dev_err(&pdev->dev, "Failed to request IRQ%d: err: %d.\n",
  2004. dev->irq, err);
  2005. dev->irq = 0;
  2006. goto err_out_free_desc;
  2007. }
  2008. err = hifn_start_device(dev);
  2009. if (err)
  2010. goto err_out_free_irq;
  2011. err = hifn_register_rng(dev);
  2012. if (err)
  2013. goto err_out_stop_device;
  2014. err = hifn_register_alg(dev);
  2015. if (err)
  2016. goto err_out_unregister_rng;
  2017. INIT_DELAYED_WORK(&dev->work, hifn_work);
  2018. schedule_delayed_work(&dev->work, HZ);
  2019. dev_dbg(&pdev->dev, "HIFN crypto accelerator card at %s has been "
  2020. "successfully registered as %s.\n",
  2021. pci_name(pdev), dev->name);
  2022. return 0;
  2023. err_out_unregister_rng:
  2024. hifn_unregister_rng(dev);
  2025. err_out_stop_device:
  2026. hifn_reset_dma(dev, 1);
  2027. hifn_stop_device(dev);
  2028. err_out_free_irq:
  2029. free_irq(dev->irq, dev);
  2030. tasklet_kill(&dev->tasklet);
  2031. err_out_free_desc:
  2032. dma_free_coherent(&pdev->dev, sizeof(struct hifn_dma), dev->desc_virt,
  2033. dev->desc_dma);
  2034. err_out_unmap_bars:
  2035. for (i = 0; i < 3; ++i)
  2036. if (dev->bar[i])
  2037. iounmap(dev->bar[i]);
  2038. kfree(dev);
  2039. err_out_free_regions:
  2040. pci_release_regions(pdev);
  2041. err_out_disable_pci_device:
  2042. pci_disable_device(pdev);
  2043. return err;
  2044. }
  2045. static void hifn_remove(struct pci_dev *pdev)
  2046. {
  2047. int i;
  2048. struct hifn_device *dev;
  2049. dev = pci_get_drvdata(pdev);
  2050. if (dev) {
  2051. cancel_delayed_work_sync(&dev->work);
  2052. hifn_unregister_rng(dev);
  2053. hifn_unregister_alg(dev);
  2054. hifn_reset_dma(dev, 1);
  2055. hifn_stop_device(dev);
  2056. free_irq(dev->irq, dev);
  2057. tasklet_kill(&dev->tasklet);
  2058. hifn_flush(dev);
  2059. dma_free_coherent(&pdev->dev, sizeof(struct hifn_dma),
  2060. dev->desc_virt, dev->desc_dma);
  2061. for (i = 0; i < 3; ++i)
  2062. if (dev->bar[i])
  2063. iounmap(dev->bar[i]);
  2064. kfree(dev);
  2065. }
  2066. pci_release_regions(pdev);
  2067. pci_disable_device(pdev);
  2068. }
  2069. static struct pci_device_id hifn_pci_tbl[] = {
  2070. { PCI_DEVICE(PCI_VENDOR_ID_HIFN, PCI_DEVICE_ID_HIFN_7955) },
  2071. { PCI_DEVICE(PCI_VENDOR_ID_HIFN, PCI_DEVICE_ID_HIFN_7956) },
  2072. { 0 }
  2073. };
  2074. MODULE_DEVICE_TABLE(pci, hifn_pci_tbl);
  2075. static struct pci_driver hifn_pci_driver = {
  2076. .name = "hifn795x",
  2077. .id_table = hifn_pci_tbl,
  2078. .probe = hifn_probe,
  2079. .remove = hifn_remove,
  2080. };
  2081. static int __init hifn_init(void)
  2082. {
  2083. unsigned int freq;
  2084. int err;
  2085. if (strncmp(hifn_pll_ref, "ext", 3) &&
  2086. strncmp(hifn_pll_ref, "pci", 3)) {
  2087. pr_err("hifn795x: invalid hifn_pll_ref clock, must be pci or ext");
  2088. return -EINVAL;
  2089. }
  2090. /*
  2091. * For the 7955/7956 the reference clock frequency must be in the
  2092. * range of 20MHz-100MHz. For the 7954 the upper bound is 66.67MHz,
  2093. * but this chip is currently not supported.
  2094. */
  2095. if (hifn_pll_ref[3] != '\0') {
  2096. freq = simple_strtoul(hifn_pll_ref + 3, NULL, 10);
  2097. if (freq < 20 || freq > 100) {
  2098. pr_err("hifn795x: invalid hifn_pll_ref frequency, must"
  2099. "be in the range of 20-100");
  2100. return -EINVAL;
  2101. }
  2102. }
  2103. err = pci_register_driver(&hifn_pci_driver);
  2104. if (err < 0) {
  2105. pr_err("Failed to register PCI driver for %s device.\n",
  2106. hifn_pci_driver.name);
  2107. return -ENODEV;
  2108. }
  2109. pr_info("Driver for HIFN 795x crypto accelerator chip "
  2110. "has been successfully registered.\n");
  2111. return 0;
  2112. }
  2113. static void __exit hifn_fini(void)
  2114. {
  2115. pci_unregister_driver(&hifn_pci_driver);
  2116. pr_info("Driver for HIFN 795x crypto accelerator chip "
  2117. "has been successfully unregistered.\n");
  2118. }
  2119. module_init(hifn_init);
  2120. module_exit(hifn_fini);
  2121. MODULE_LICENSE("GPL");
  2122. MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
  2123. MODULE_DESCRIPTION("Driver for HIFN 795x crypto accelerator chip.");