hifn_795x.c 78 KB

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