hifn_795x.c 75 KB

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