hifn_795x.c 71 KB

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