hifn_795x.c 78 KB

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