hifn_795x.c 77 KB

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