hifn_795x.c 78 KB

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