hifn_795x.c 72 KB

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