hifn_795x.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701
  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. u32 ret;
  551. ret = readl(dev->bar[0] + reg);
  552. return ret;
  553. }
  554. static inline u32 hifn_read_1(struct hifn_device *dev, u32 reg)
  555. {
  556. u32 ret;
  557. ret = readl(dev->bar[1] + reg);
  558. return ret;
  559. }
  560. static inline void hifn_write_0(struct hifn_device *dev, u32 reg, u32 val)
  561. {
  562. writel((__force u32)cpu_to_le32(val), dev->bar[0] + reg);
  563. }
  564. static inline void hifn_write_1(struct hifn_device *dev, u32 reg, u32 val)
  565. {
  566. writel((__force u32)cpu_to_le32(val), dev->bar[1] + reg);
  567. }
  568. static void hifn_wait_puc(struct hifn_device *dev)
  569. {
  570. int i;
  571. u32 ret;
  572. for (i = 10000; i > 0; --i) {
  573. ret = hifn_read_0(dev, HIFN_0_PUCTRL);
  574. if (!(ret & HIFN_PUCTRL_RESET))
  575. break;
  576. udelay(1);
  577. }
  578. if (!i)
  579. dev_err(&dev->pdev->dev, "Failed to reset PUC unit.\n");
  580. }
  581. static void hifn_reset_puc(struct hifn_device *dev)
  582. {
  583. hifn_write_0(dev, HIFN_0_PUCTRL, HIFN_PUCTRL_DMAENA);
  584. hifn_wait_puc(dev);
  585. }
  586. static void hifn_stop_device(struct hifn_device *dev)
  587. {
  588. hifn_write_1(dev, HIFN_1_DMA_CSR,
  589. HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS |
  590. HIFN_DMACSR_S_CTRL_DIS | HIFN_DMACSR_C_CTRL_DIS);
  591. hifn_write_0(dev, HIFN_0_PUIER, 0);
  592. hifn_write_1(dev, HIFN_1_DMA_IER, 0);
  593. }
  594. static void hifn_reset_dma(struct hifn_device *dev, int full)
  595. {
  596. hifn_stop_device(dev);
  597. /*
  598. * Setting poll frequency and others to 0.
  599. */
  600. hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
  601. HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
  602. mdelay(1);
  603. /*
  604. * Reset DMA.
  605. */
  606. if (full) {
  607. hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MODE);
  608. mdelay(1);
  609. } else {
  610. hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MODE |
  611. HIFN_DMACNFG_MSTRESET);
  612. hifn_reset_puc(dev);
  613. }
  614. hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
  615. HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
  616. hifn_reset_puc(dev);
  617. }
  618. static u32 hifn_next_signature(u32 a, u_int cnt)
  619. {
  620. int i;
  621. u32 v;
  622. for (i = 0; i < cnt; i++) {
  623. /* get the parity */
  624. v = a & 0x80080125;
  625. v ^= v >> 16;
  626. v ^= v >> 8;
  627. v ^= v >> 4;
  628. v ^= v >> 2;
  629. v ^= v >> 1;
  630. a = (v & 1) ^ (a << 1);
  631. }
  632. return a;
  633. }
  634. static struct pci2id {
  635. u_short pci_vendor;
  636. u_short pci_prod;
  637. char card_id[13];
  638. } pci2id[] = {
  639. {
  640. PCI_VENDOR_ID_HIFN,
  641. PCI_DEVICE_ID_HIFN_7955,
  642. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  643. 0x00, 0x00, 0x00, 0x00, 0x00 }
  644. },
  645. {
  646. PCI_VENDOR_ID_HIFN,
  647. PCI_DEVICE_ID_HIFN_7956,
  648. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  649. 0x00, 0x00, 0x00, 0x00, 0x00 }
  650. }
  651. };
  652. #ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
  653. static int hifn_rng_data_present(struct hwrng *rng, int wait)
  654. {
  655. struct hifn_device *dev = (struct hifn_device *)rng->priv;
  656. s64 nsec;
  657. nsec = ktime_to_ns(ktime_sub(ktime_get(), dev->rngtime));
  658. nsec -= dev->rng_wait_time;
  659. if (nsec <= 0)
  660. return 1;
  661. if (!wait)
  662. return 0;
  663. ndelay(nsec);
  664. return 1;
  665. }
  666. static int hifn_rng_data_read(struct hwrng *rng, u32 *data)
  667. {
  668. struct hifn_device *dev = (struct hifn_device *)rng->priv;
  669. *data = hifn_read_1(dev, HIFN_1_RNG_DATA);
  670. dev->rngtime = ktime_get();
  671. return 4;
  672. }
  673. static int hifn_register_rng(struct hifn_device *dev)
  674. {
  675. /*
  676. * We must wait at least 256 Pk_clk cycles between two reads of the rng.
  677. */
  678. dev->rng_wait_time = DIV_ROUND_UP_ULL(NSEC_PER_SEC,
  679. dev->pk_clk_freq) * 256;
  680. dev->rng.name = dev->name;
  681. dev->rng.data_present = hifn_rng_data_present,
  682. dev->rng.data_read = hifn_rng_data_read,
  683. dev->rng.priv = (unsigned long)dev;
  684. return hwrng_register(&dev->rng);
  685. }
  686. static void hifn_unregister_rng(struct hifn_device *dev)
  687. {
  688. hwrng_unregister(&dev->rng);
  689. }
  690. #else
  691. #define hifn_register_rng(dev) 0
  692. #define hifn_unregister_rng(dev)
  693. #endif
  694. static int hifn_init_pubrng(struct hifn_device *dev)
  695. {
  696. int i;
  697. hifn_write_1(dev, HIFN_1_PUB_RESET, hifn_read_1(dev, HIFN_1_PUB_RESET) |
  698. HIFN_PUBRST_RESET);
  699. for (i = 100; i > 0; --i) {
  700. mdelay(1);
  701. if ((hifn_read_1(dev, HIFN_1_PUB_RESET) & HIFN_PUBRST_RESET) == 0)
  702. break;
  703. }
  704. if (!i) {
  705. dev_err(&dev->pdev->dev, "Failed to initialise public key engine.\n");
  706. } else {
  707. hifn_write_1(dev, HIFN_1_PUB_IEN, HIFN_PUBIEN_DONE);
  708. dev->dmareg |= HIFN_DMAIER_PUBDONE;
  709. hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
  710. dev_dbg(&dev->pdev->dev, "Public key engine has been successfully initialised.\n");
  711. }
  712. /* Enable RNG engine. */
  713. hifn_write_1(dev, HIFN_1_RNG_CONFIG,
  714. hifn_read_1(dev, HIFN_1_RNG_CONFIG) | HIFN_RNGCFG_ENA);
  715. dev_dbg(&dev->pdev->dev, "RNG engine has been successfully initialised.\n");
  716. #ifdef CONFIG_CRYPTO_DEV_HIFN_795X_RNG
  717. /* First value must be discarded */
  718. hifn_read_1(dev, HIFN_1_RNG_DATA);
  719. dev->rngtime = ktime_get();
  720. #endif
  721. return 0;
  722. }
  723. static int hifn_enable_crypto(struct hifn_device *dev)
  724. {
  725. u32 dmacfg, addr;
  726. char *offtbl = NULL;
  727. int i;
  728. for (i = 0; i < ARRAY_SIZE(pci2id); i++) {
  729. if (pci2id[i].pci_vendor == dev->pdev->vendor &&
  730. pci2id[i].pci_prod == dev->pdev->device) {
  731. offtbl = pci2id[i].card_id;
  732. break;
  733. }
  734. }
  735. if (!offtbl) {
  736. dev_err(&dev->pdev->dev, "Unknown card!\n");
  737. return -ENODEV;
  738. }
  739. dmacfg = hifn_read_1(dev, HIFN_1_DMA_CNFG);
  740. hifn_write_1(dev, HIFN_1_DMA_CNFG,
  741. HIFN_DMACNFG_UNLOCK | HIFN_DMACNFG_MSTRESET |
  742. HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
  743. mdelay(1);
  744. addr = hifn_read_1(dev, HIFN_1_UNLOCK_SECRET1);
  745. mdelay(1);
  746. hifn_write_1(dev, HIFN_1_UNLOCK_SECRET2, 0);
  747. mdelay(1);
  748. for (i = 0; i < 12; ++i) {
  749. addr = hifn_next_signature(addr, offtbl[i] + 0x101);
  750. hifn_write_1(dev, HIFN_1_UNLOCK_SECRET2, addr);
  751. mdelay(1);
  752. }
  753. hifn_write_1(dev, HIFN_1_DMA_CNFG, dmacfg);
  754. dev_dbg(&dev->pdev->dev, "%s %s.\n", dev->name, pci_name(dev->pdev));
  755. return 0;
  756. }
  757. static void hifn_init_dma(struct hifn_device *dev)
  758. {
  759. struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
  760. u32 dptr = dev->desc_dma;
  761. int i;
  762. for (i = 0; i < HIFN_D_CMD_RSIZE; ++i)
  763. dma->cmdr[i].p = __cpu_to_le32(dptr +
  764. offsetof(struct hifn_dma, command_bufs[i][0]));
  765. for (i = 0; i < HIFN_D_RES_RSIZE; ++i)
  766. dma->resr[i].p = __cpu_to_le32(dptr +
  767. offsetof(struct hifn_dma, result_bufs[i][0]));
  768. /* Setup LAST descriptors. */
  769. dma->cmdr[HIFN_D_CMD_RSIZE].p = __cpu_to_le32(dptr +
  770. offsetof(struct hifn_dma, cmdr[0]));
  771. dma->srcr[HIFN_D_SRC_RSIZE].p = __cpu_to_le32(dptr +
  772. offsetof(struct hifn_dma, srcr[0]));
  773. dma->dstr[HIFN_D_DST_RSIZE].p = __cpu_to_le32(dptr +
  774. offsetof(struct hifn_dma, dstr[0]));
  775. dma->resr[HIFN_D_RES_RSIZE].p = __cpu_to_le32(dptr +
  776. offsetof(struct hifn_dma, resr[0]));
  777. dma->cmdu = dma->srcu = dma->dstu = dma->resu = 0;
  778. dma->cmdi = dma->srci = dma->dsti = dma->resi = 0;
  779. dma->cmdk = dma->srck = dma->dstk = dma->resk = 0;
  780. }
  781. /*
  782. * Initialize the PLL. We need to know the frequency of the reference clock
  783. * to calculate the optimal multiplier. For PCI we assume 66MHz, since that
  784. * allows us to operate without the risk of overclocking the chip. If it
  785. * actually uses 33MHz, the chip will operate at half the speed, this can be
  786. * overridden by specifying the frequency as module parameter (pci33).
  787. *
  788. * Unfortunately the PCI clock is not very suitable since the HIFN needs a
  789. * stable clock and the PCI clock frequency may vary, so the default is the
  790. * external clock. There is no way to find out its frequency, we default to
  791. * 66MHz since according to Mike Ham of HiFn, almost every board in existence
  792. * has an external crystal populated at 66MHz.
  793. */
  794. static void hifn_init_pll(struct hifn_device *dev)
  795. {
  796. unsigned int freq, m;
  797. u32 pllcfg;
  798. pllcfg = HIFN_1_PLL | HIFN_PLL_RESERVED_1;
  799. if (strncmp(hifn_pll_ref, "ext", 3) == 0)
  800. pllcfg |= HIFN_PLL_REF_CLK_PLL;
  801. else
  802. pllcfg |= HIFN_PLL_REF_CLK_HBI;
  803. if (hifn_pll_ref[3] != '\0')
  804. freq = simple_strtoul(hifn_pll_ref + 3, NULL, 10);
  805. else {
  806. freq = 66;
  807. dev_info(&dev->pdev->dev, "assuming %uMHz clock speed, override with hifn_pll_ref=%.3s<frequency>\n",
  808. freq, hifn_pll_ref);
  809. }
  810. m = HIFN_PLL_FCK_MAX / freq;
  811. pllcfg |= (m / 2 - 1) << HIFN_PLL_ND_SHIFT;
  812. if (m <= 8)
  813. pllcfg |= HIFN_PLL_IS_1_8;
  814. else
  815. pllcfg |= HIFN_PLL_IS_9_12;
  816. /* Select clock source and enable clock bypass */
  817. hifn_write_1(dev, HIFN_1_PLL, pllcfg |
  818. HIFN_PLL_PK_CLK_HBI | HIFN_PLL_PE_CLK_HBI | HIFN_PLL_BP);
  819. /* Let the chip lock to the input clock */
  820. mdelay(10);
  821. /* Disable clock bypass */
  822. hifn_write_1(dev, HIFN_1_PLL, pllcfg |
  823. HIFN_PLL_PK_CLK_HBI | HIFN_PLL_PE_CLK_HBI);
  824. /* Switch the engines to the PLL */
  825. hifn_write_1(dev, HIFN_1_PLL, pllcfg |
  826. HIFN_PLL_PK_CLK_PLL | HIFN_PLL_PE_CLK_PLL);
  827. /*
  828. * The Fpk_clk runs at half the total speed. Its frequency is needed to
  829. * calculate the minimum time between two reads of the rng. Since 33MHz
  830. * is actually 33.333... we overestimate the frequency here, resulting
  831. * in slightly larger intervals.
  832. */
  833. dev->pk_clk_freq = 1000000 * (freq + 1) * m / 2;
  834. }
  835. static void hifn_init_registers(struct hifn_device *dev)
  836. {
  837. u32 dptr = dev->desc_dma;
  838. /* Initialization magic... */
  839. hifn_write_0(dev, HIFN_0_PUCTRL, HIFN_PUCTRL_DMAENA);
  840. hifn_write_0(dev, HIFN_0_FIFOCNFG, HIFN_FIFOCNFG_THRESHOLD);
  841. hifn_write_0(dev, HIFN_0_PUIER, HIFN_PUIER_DSTOVER);
  842. /* write all 4 ring address registers */
  843. hifn_write_1(dev, HIFN_1_DMA_CRAR, dptr +
  844. offsetof(struct hifn_dma, cmdr[0]));
  845. hifn_write_1(dev, HIFN_1_DMA_SRAR, dptr +
  846. offsetof(struct hifn_dma, srcr[0]));
  847. hifn_write_1(dev, HIFN_1_DMA_DRAR, dptr +
  848. offsetof(struct hifn_dma, dstr[0]));
  849. hifn_write_1(dev, HIFN_1_DMA_RRAR, dptr +
  850. offsetof(struct hifn_dma, resr[0]));
  851. mdelay(2);
  852. #if 0
  853. hifn_write_1(dev, HIFN_1_DMA_CSR,
  854. HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS |
  855. HIFN_DMACSR_S_CTRL_DIS | HIFN_DMACSR_C_CTRL_DIS |
  856. HIFN_DMACSR_D_ABORT | HIFN_DMACSR_D_DONE | HIFN_DMACSR_D_LAST |
  857. HIFN_DMACSR_D_WAIT | HIFN_DMACSR_D_OVER |
  858. HIFN_DMACSR_R_ABORT | HIFN_DMACSR_R_DONE | HIFN_DMACSR_R_LAST |
  859. HIFN_DMACSR_R_WAIT | HIFN_DMACSR_R_OVER |
  860. HIFN_DMACSR_S_ABORT | HIFN_DMACSR_S_DONE | HIFN_DMACSR_S_LAST |
  861. HIFN_DMACSR_S_WAIT |
  862. HIFN_DMACSR_C_ABORT | HIFN_DMACSR_C_DONE | HIFN_DMACSR_C_LAST |
  863. HIFN_DMACSR_C_WAIT |
  864. HIFN_DMACSR_ENGINE |
  865. HIFN_DMACSR_PUBDONE);
  866. #else
  867. hifn_write_1(dev, HIFN_1_DMA_CSR,
  868. HIFN_DMACSR_C_CTRL_ENA | HIFN_DMACSR_S_CTRL_ENA |
  869. HIFN_DMACSR_D_CTRL_ENA | HIFN_DMACSR_R_CTRL_ENA |
  870. HIFN_DMACSR_D_ABORT | HIFN_DMACSR_D_DONE | HIFN_DMACSR_D_LAST |
  871. HIFN_DMACSR_D_WAIT | HIFN_DMACSR_D_OVER |
  872. HIFN_DMACSR_R_ABORT | HIFN_DMACSR_R_DONE | HIFN_DMACSR_R_LAST |
  873. HIFN_DMACSR_R_WAIT | HIFN_DMACSR_R_OVER |
  874. HIFN_DMACSR_S_ABORT | HIFN_DMACSR_S_DONE | HIFN_DMACSR_S_LAST |
  875. HIFN_DMACSR_S_WAIT |
  876. HIFN_DMACSR_C_ABORT | HIFN_DMACSR_C_DONE | HIFN_DMACSR_C_LAST |
  877. HIFN_DMACSR_C_WAIT |
  878. HIFN_DMACSR_ENGINE |
  879. HIFN_DMACSR_PUBDONE);
  880. #endif
  881. hifn_read_1(dev, HIFN_1_DMA_CSR);
  882. dev->dmareg |= HIFN_DMAIER_R_DONE | HIFN_DMAIER_C_ABORT |
  883. HIFN_DMAIER_D_OVER | HIFN_DMAIER_R_OVER |
  884. HIFN_DMAIER_S_ABORT | HIFN_DMAIER_D_ABORT | HIFN_DMAIER_R_ABORT |
  885. HIFN_DMAIER_ENGINE;
  886. dev->dmareg &= ~HIFN_DMAIER_C_WAIT;
  887. hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
  888. hifn_read_1(dev, HIFN_1_DMA_IER);
  889. #if 0
  890. hifn_write_0(dev, HIFN_0_PUCNFG, HIFN_PUCNFG_ENCCNFG |
  891. HIFN_PUCNFG_DRFR_128 | HIFN_PUCNFG_TCALLPHASES |
  892. HIFN_PUCNFG_TCDRVTOTEM | HIFN_PUCNFG_BUS32 |
  893. HIFN_PUCNFG_DRAM);
  894. #else
  895. hifn_write_0(dev, HIFN_0_PUCNFG, 0x10342);
  896. #endif
  897. hifn_init_pll(dev);
  898. hifn_write_0(dev, HIFN_0_PUISR, HIFN_PUISR_DSTOVER);
  899. hifn_write_1(dev, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
  900. HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE | HIFN_DMACNFG_LAST |
  901. ((HIFN_POLL_FREQUENCY << 16 ) & HIFN_DMACNFG_POLLFREQ) |
  902. ((HIFN_POLL_SCALAR << 8) & HIFN_DMACNFG_POLLINVAL));
  903. }
  904. static int hifn_setup_base_command(struct hifn_device *dev, u8 *buf,
  905. unsigned dlen, unsigned slen, u16 mask, u8 snum)
  906. {
  907. struct hifn_base_command *base_cmd;
  908. u8 *buf_pos = buf;
  909. base_cmd = (struct hifn_base_command *)buf_pos;
  910. base_cmd->masks = __cpu_to_le16(mask);
  911. base_cmd->total_source_count =
  912. __cpu_to_le16(slen & HIFN_BASE_CMD_LENMASK_LO);
  913. base_cmd->total_dest_count =
  914. __cpu_to_le16(dlen & HIFN_BASE_CMD_LENMASK_LO);
  915. dlen >>= 16;
  916. slen >>= 16;
  917. base_cmd->session_num = __cpu_to_le16(snum |
  918. ((slen << HIFN_BASE_CMD_SRCLEN_S) & HIFN_BASE_CMD_SRCLEN_M) |
  919. ((dlen << HIFN_BASE_CMD_DSTLEN_S) & HIFN_BASE_CMD_DSTLEN_M));
  920. return sizeof(struct hifn_base_command);
  921. }
  922. static int hifn_setup_crypto_command(struct hifn_device *dev,
  923. u8 *buf, unsigned dlen, unsigned slen,
  924. u8 *key, int keylen, u8 *iv, int ivsize, u16 mode)
  925. {
  926. struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
  927. struct hifn_crypt_command *cry_cmd;
  928. u8 *buf_pos = buf;
  929. u16 cmd_len;
  930. cry_cmd = (struct hifn_crypt_command *)buf_pos;
  931. cry_cmd->source_count = __cpu_to_le16(dlen & 0xffff);
  932. dlen >>= 16;
  933. cry_cmd->masks = __cpu_to_le16(mode |
  934. ((dlen << HIFN_CRYPT_CMD_SRCLEN_S) &
  935. HIFN_CRYPT_CMD_SRCLEN_M));
  936. cry_cmd->header_skip = 0;
  937. cry_cmd->reserved = 0;
  938. buf_pos += sizeof(struct hifn_crypt_command);
  939. dma->cmdu++;
  940. if (dma->cmdu > 1) {
  941. dev->dmareg |= HIFN_DMAIER_C_WAIT;
  942. hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
  943. }
  944. if (keylen) {
  945. memcpy(buf_pos, key, keylen);
  946. buf_pos += keylen;
  947. }
  948. if (ivsize) {
  949. memcpy(buf_pos, iv, ivsize);
  950. buf_pos += ivsize;
  951. }
  952. cmd_len = buf_pos - buf;
  953. return cmd_len;
  954. }
  955. static int hifn_setup_cmd_desc(struct hifn_device *dev,
  956. struct hifn_context *ctx, struct hifn_request_context *rctx,
  957. void *priv, unsigned int nbytes)
  958. {
  959. struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
  960. int cmd_len, sa_idx;
  961. u8 *buf, *buf_pos;
  962. u16 mask;
  963. sa_idx = dma->cmdi;
  964. buf_pos = buf = dma->command_bufs[dma->cmdi];
  965. mask = 0;
  966. switch (rctx->op) {
  967. case ACRYPTO_OP_DECRYPT:
  968. mask = HIFN_BASE_CMD_CRYPT | HIFN_BASE_CMD_DECODE;
  969. break;
  970. case ACRYPTO_OP_ENCRYPT:
  971. mask = HIFN_BASE_CMD_CRYPT;
  972. break;
  973. case ACRYPTO_OP_HMAC:
  974. mask = HIFN_BASE_CMD_MAC;
  975. break;
  976. default:
  977. goto err_out;
  978. }
  979. buf_pos += hifn_setup_base_command(dev, buf_pos, nbytes,
  980. nbytes, mask, dev->snum);
  981. if (rctx->op == ACRYPTO_OP_ENCRYPT || rctx->op == ACRYPTO_OP_DECRYPT) {
  982. u16 md = 0;
  983. if (ctx->keysize)
  984. md |= HIFN_CRYPT_CMD_NEW_KEY;
  985. if (rctx->iv && rctx->mode != ACRYPTO_MODE_ECB)
  986. md |= HIFN_CRYPT_CMD_NEW_IV;
  987. switch (rctx->mode) {
  988. case ACRYPTO_MODE_ECB:
  989. md |= HIFN_CRYPT_CMD_MODE_ECB;
  990. break;
  991. case ACRYPTO_MODE_CBC:
  992. md |= HIFN_CRYPT_CMD_MODE_CBC;
  993. break;
  994. case ACRYPTO_MODE_CFB:
  995. md |= HIFN_CRYPT_CMD_MODE_CFB;
  996. break;
  997. case ACRYPTO_MODE_OFB:
  998. md |= HIFN_CRYPT_CMD_MODE_OFB;
  999. break;
  1000. default:
  1001. goto err_out;
  1002. }
  1003. switch (rctx->type) {
  1004. case ACRYPTO_TYPE_AES_128:
  1005. if (ctx->keysize != 16)
  1006. goto err_out;
  1007. md |= HIFN_CRYPT_CMD_KSZ_128 |
  1008. HIFN_CRYPT_CMD_ALG_AES;
  1009. break;
  1010. case ACRYPTO_TYPE_AES_192:
  1011. if (ctx->keysize != 24)
  1012. goto err_out;
  1013. md |= HIFN_CRYPT_CMD_KSZ_192 |
  1014. HIFN_CRYPT_CMD_ALG_AES;
  1015. break;
  1016. case ACRYPTO_TYPE_AES_256:
  1017. if (ctx->keysize != 32)
  1018. goto err_out;
  1019. md |= HIFN_CRYPT_CMD_KSZ_256 |
  1020. HIFN_CRYPT_CMD_ALG_AES;
  1021. break;
  1022. case ACRYPTO_TYPE_3DES:
  1023. if (ctx->keysize != 24)
  1024. goto err_out;
  1025. md |= HIFN_CRYPT_CMD_ALG_3DES;
  1026. break;
  1027. case ACRYPTO_TYPE_DES:
  1028. if (ctx->keysize != 8)
  1029. goto err_out;
  1030. md |= HIFN_CRYPT_CMD_ALG_DES;
  1031. break;
  1032. default:
  1033. goto err_out;
  1034. }
  1035. buf_pos += hifn_setup_crypto_command(dev, buf_pos,
  1036. nbytes, nbytes, ctx->key, ctx->keysize,
  1037. rctx->iv, rctx->ivsize, md);
  1038. }
  1039. dev->sa[sa_idx] = priv;
  1040. dev->started++;
  1041. cmd_len = buf_pos - buf;
  1042. dma->cmdr[dma->cmdi].l = __cpu_to_le32(cmd_len | HIFN_D_VALID |
  1043. HIFN_D_LAST | HIFN_D_MASKDONEIRQ);
  1044. if (++dma->cmdi == HIFN_D_CMD_RSIZE) {
  1045. dma->cmdr[dma->cmdi].l = __cpu_to_le32(
  1046. HIFN_D_VALID | HIFN_D_LAST |
  1047. HIFN_D_MASKDONEIRQ | HIFN_D_JUMP);
  1048. dma->cmdi = 0;
  1049. } else {
  1050. dma->cmdr[dma->cmdi - 1].l |= __cpu_to_le32(HIFN_D_VALID);
  1051. }
  1052. if (!(dev->flags & HIFN_FLAG_CMD_BUSY)) {
  1053. hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_C_CTRL_ENA);
  1054. dev->flags |= HIFN_FLAG_CMD_BUSY;
  1055. }
  1056. return 0;
  1057. err_out:
  1058. return -EINVAL;
  1059. }
  1060. static int hifn_setup_src_desc(struct hifn_device *dev, struct page *page,
  1061. unsigned int offset, unsigned int size, int last)
  1062. {
  1063. struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
  1064. int idx;
  1065. dma_addr_t addr;
  1066. addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_TODEVICE);
  1067. idx = dma->srci;
  1068. dma->srcr[idx].p = __cpu_to_le32(addr);
  1069. dma->srcr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
  1070. HIFN_D_MASKDONEIRQ | (last ? HIFN_D_LAST : 0));
  1071. if (++idx == HIFN_D_SRC_RSIZE) {
  1072. dma->srcr[idx].l = __cpu_to_le32(HIFN_D_VALID |
  1073. HIFN_D_JUMP | HIFN_D_MASKDONEIRQ |
  1074. (last ? HIFN_D_LAST : 0));
  1075. idx = 0;
  1076. }
  1077. dma->srci = idx;
  1078. dma->srcu++;
  1079. if (!(dev->flags & HIFN_FLAG_SRC_BUSY)) {
  1080. hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_S_CTRL_ENA);
  1081. dev->flags |= HIFN_FLAG_SRC_BUSY;
  1082. }
  1083. return size;
  1084. }
  1085. static void hifn_setup_res_desc(struct hifn_device *dev)
  1086. {
  1087. struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
  1088. dma->resr[dma->resi].l = __cpu_to_le32(HIFN_USED_RESULT |
  1089. HIFN_D_VALID | HIFN_D_LAST);
  1090. /*
  1091. * dma->resr[dma->resi].l = __cpu_to_le32(HIFN_MAX_RESULT | HIFN_D_VALID |
  1092. * HIFN_D_LAST);
  1093. */
  1094. if (++dma->resi == HIFN_D_RES_RSIZE) {
  1095. dma->resr[HIFN_D_RES_RSIZE].l = __cpu_to_le32(HIFN_D_VALID |
  1096. HIFN_D_JUMP | HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
  1097. dma->resi = 0;
  1098. }
  1099. dma->resu++;
  1100. if (!(dev->flags & HIFN_FLAG_RES_BUSY)) {
  1101. hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_R_CTRL_ENA);
  1102. dev->flags |= HIFN_FLAG_RES_BUSY;
  1103. }
  1104. }
  1105. static void hifn_setup_dst_desc(struct hifn_device *dev, struct page *page,
  1106. unsigned offset, unsigned size, int last)
  1107. {
  1108. struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
  1109. int idx;
  1110. dma_addr_t addr;
  1111. addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_FROMDEVICE);
  1112. idx = dma->dsti;
  1113. dma->dstr[idx].p = __cpu_to_le32(addr);
  1114. dma->dstr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
  1115. HIFN_D_MASKDONEIRQ | (last ? HIFN_D_LAST : 0));
  1116. if (++idx == HIFN_D_DST_RSIZE) {
  1117. dma->dstr[idx].l = __cpu_to_le32(HIFN_D_VALID |
  1118. HIFN_D_JUMP | HIFN_D_MASKDONEIRQ |
  1119. (last ? HIFN_D_LAST : 0));
  1120. idx = 0;
  1121. }
  1122. dma->dsti = idx;
  1123. dma->dstu++;
  1124. if (!(dev->flags & HIFN_FLAG_DST_BUSY)) {
  1125. hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_D_CTRL_ENA);
  1126. dev->flags |= HIFN_FLAG_DST_BUSY;
  1127. }
  1128. }
  1129. static int hifn_setup_dma(struct hifn_device *dev,
  1130. struct hifn_context *ctx, struct hifn_request_context *rctx,
  1131. struct scatterlist *src, struct scatterlist *dst,
  1132. unsigned int nbytes, void *priv)
  1133. {
  1134. struct scatterlist *t;
  1135. struct page *spage, *dpage;
  1136. unsigned int soff, doff;
  1137. unsigned int n, len;
  1138. n = nbytes;
  1139. while (n) {
  1140. spage = sg_page(src);
  1141. soff = src->offset;
  1142. len = min(src->length, n);
  1143. hifn_setup_src_desc(dev, spage, soff, len, n - len == 0);
  1144. src++;
  1145. n -= len;
  1146. }
  1147. t = &rctx->walk.cache[0];
  1148. n = nbytes;
  1149. while (n) {
  1150. if (t->length && rctx->walk.flags & ASYNC_FLAGS_MISALIGNED) {
  1151. BUG_ON(!sg_page(t));
  1152. dpage = sg_page(t);
  1153. doff = 0;
  1154. len = t->length;
  1155. } else {
  1156. BUG_ON(!sg_page(dst));
  1157. dpage = sg_page(dst);
  1158. doff = dst->offset;
  1159. len = dst->length;
  1160. }
  1161. len = min(len, n);
  1162. hifn_setup_dst_desc(dev, dpage, doff, len, n - len == 0);
  1163. dst++;
  1164. t++;
  1165. n -= len;
  1166. }
  1167. hifn_setup_cmd_desc(dev, ctx, rctx, priv, nbytes);
  1168. hifn_setup_res_desc(dev);
  1169. return 0;
  1170. }
  1171. static int hifn_cipher_walk_init(struct hifn_cipher_walk *w,
  1172. int num, gfp_t gfp_flags)
  1173. {
  1174. int i;
  1175. num = min(ASYNC_SCATTERLIST_CACHE, num);
  1176. sg_init_table(w->cache, num);
  1177. w->num = 0;
  1178. for (i = 0; i < num; ++i) {
  1179. struct page *page = alloc_page(gfp_flags);
  1180. struct scatterlist *s;
  1181. if (!page)
  1182. break;
  1183. s = &w->cache[i];
  1184. sg_set_page(s, page, PAGE_SIZE, 0);
  1185. w->num++;
  1186. }
  1187. return i;
  1188. }
  1189. static void hifn_cipher_walk_exit(struct hifn_cipher_walk *w)
  1190. {
  1191. int i;
  1192. for (i = 0; i < w->num; ++i) {
  1193. struct scatterlist *s = &w->cache[i];
  1194. __free_page(sg_page(s));
  1195. s->length = 0;
  1196. }
  1197. w->num = 0;
  1198. }
  1199. static int ablkcipher_add(unsigned int *drestp, struct scatterlist *dst,
  1200. unsigned int size, unsigned int *nbytesp)
  1201. {
  1202. unsigned int copy, drest = *drestp, nbytes = *nbytesp;
  1203. int idx = 0;
  1204. if (drest < size || size > nbytes)
  1205. return -EINVAL;
  1206. while (size) {
  1207. copy = min3(drest, size, dst->length);
  1208. size -= copy;
  1209. drest -= copy;
  1210. nbytes -= copy;
  1211. pr_debug("%s: copy: %u, size: %u, drest: %u, nbytes: %u.\n",
  1212. __func__, copy, size, drest, nbytes);
  1213. dst++;
  1214. idx++;
  1215. }
  1216. *nbytesp = nbytes;
  1217. *drestp = drest;
  1218. return idx;
  1219. }
  1220. static int hifn_cipher_walk(struct ablkcipher_request *req,
  1221. struct hifn_cipher_walk *w)
  1222. {
  1223. struct scatterlist *dst, *t;
  1224. unsigned int nbytes = req->nbytes, offset, copy, diff;
  1225. int idx, tidx, err;
  1226. tidx = idx = 0;
  1227. offset = 0;
  1228. while (nbytes) {
  1229. if (idx >= w->num && (w->flags & ASYNC_FLAGS_MISALIGNED))
  1230. return -EINVAL;
  1231. dst = &req->dst[idx];
  1232. pr_debug("\n%s: dlen: %u, doff: %u, offset: %u, nbytes: %u.\n",
  1233. __func__, dst->length, dst->offset, offset, nbytes);
  1234. if (!IS_ALIGNED(dst->offset, HIFN_D_DST_DALIGN) ||
  1235. !IS_ALIGNED(dst->length, HIFN_D_DST_DALIGN) ||
  1236. offset) {
  1237. unsigned slen = min(dst->length - offset, nbytes);
  1238. unsigned dlen = PAGE_SIZE;
  1239. t = &w->cache[idx];
  1240. err = ablkcipher_add(&dlen, dst, slen, &nbytes);
  1241. if (err < 0)
  1242. return err;
  1243. idx += err;
  1244. copy = slen & ~(HIFN_D_DST_DALIGN - 1);
  1245. diff = slen & (HIFN_D_DST_DALIGN - 1);
  1246. if (dlen < nbytes) {
  1247. /*
  1248. * Destination page does not have enough space
  1249. * to put there additional blocksized chunk,
  1250. * so we mark that page as containing only
  1251. * blocksize aligned chunks:
  1252. * t->length = (slen & ~(HIFN_D_DST_DALIGN - 1));
  1253. * and increase number of bytes to be processed
  1254. * in next chunk:
  1255. * nbytes += diff;
  1256. */
  1257. nbytes += diff;
  1258. /*
  1259. * Temporary of course...
  1260. * Kick author if you will catch this one.
  1261. */
  1262. pr_err("%s: dlen: %u, nbytes: %u, slen: %u, offset: %u.\n",
  1263. __func__, dlen, nbytes, slen, offset);
  1264. pr_err("%s: please contact author to fix this "
  1265. "issue, generally you should not catch "
  1266. "this path under any condition but who "
  1267. "knows how did you use crypto code.\n"
  1268. "Thank you.\n", __func__);
  1269. BUG();
  1270. } else {
  1271. copy += diff + nbytes;
  1272. dst = &req->dst[idx];
  1273. err = ablkcipher_add(&dlen, dst, nbytes, &nbytes);
  1274. if (err < 0)
  1275. return err;
  1276. idx += err;
  1277. }
  1278. t->length = copy;
  1279. t->offset = offset;
  1280. } else {
  1281. nbytes -= min(dst->length, nbytes);
  1282. idx++;
  1283. }
  1284. tidx++;
  1285. }
  1286. return tidx;
  1287. }
  1288. static int hifn_setup_session(struct ablkcipher_request *req)
  1289. {
  1290. struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
  1291. struct hifn_request_context *rctx = ablkcipher_request_ctx(req);
  1292. struct hifn_device *dev = ctx->dev;
  1293. unsigned long dlen, flags;
  1294. unsigned int nbytes = req->nbytes, idx = 0;
  1295. int err = -EINVAL, sg_num;
  1296. struct scatterlist *dst;
  1297. if (rctx->iv && !rctx->ivsize && rctx->mode != ACRYPTO_MODE_ECB)
  1298. goto err_out_exit;
  1299. rctx->walk.flags = 0;
  1300. while (nbytes) {
  1301. dst = &req->dst[idx];
  1302. dlen = min(dst->length, nbytes);
  1303. if (!IS_ALIGNED(dst->offset, HIFN_D_DST_DALIGN) ||
  1304. !IS_ALIGNED(dlen, HIFN_D_DST_DALIGN))
  1305. rctx->walk.flags |= ASYNC_FLAGS_MISALIGNED;
  1306. nbytes -= dlen;
  1307. idx++;
  1308. }
  1309. if (rctx->walk.flags & ASYNC_FLAGS_MISALIGNED) {
  1310. err = hifn_cipher_walk_init(&rctx->walk, idx, GFP_ATOMIC);
  1311. if (err < 0)
  1312. return err;
  1313. }
  1314. sg_num = hifn_cipher_walk(req, &rctx->walk);
  1315. if (sg_num < 0) {
  1316. err = sg_num;
  1317. goto err_out_exit;
  1318. }
  1319. spin_lock_irqsave(&dev->lock, flags);
  1320. if (dev->started + sg_num > HIFN_QUEUE_LENGTH) {
  1321. err = -EAGAIN;
  1322. goto err_out;
  1323. }
  1324. err = hifn_setup_dma(dev, ctx, rctx, req->src, req->dst, req->nbytes, req);
  1325. if (err)
  1326. goto err_out;
  1327. dev->snum++;
  1328. dev->active = HIFN_DEFAULT_ACTIVE_NUM;
  1329. spin_unlock_irqrestore(&dev->lock, flags);
  1330. return 0;
  1331. err_out:
  1332. spin_unlock_irqrestore(&dev->lock, flags);
  1333. err_out_exit:
  1334. if (err) {
  1335. dev_info(&dev->pdev->dev, "iv: %p [%d], key: %p [%d], mode: %u, op: %u, "
  1336. "type: %u, err: %d.\n",
  1337. rctx->iv, rctx->ivsize,
  1338. ctx->key, ctx->keysize,
  1339. rctx->mode, rctx->op, rctx->type, err);
  1340. }
  1341. return err;
  1342. }
  1343. static int hifn_start_device(struct hifn_device *dev)
  1344. {
  1345. int err;
  1346. dev->started = dev->active = 0;
  1347. hifn_reset_dma(dev, 1);
  1348. err = hifn_enable_crypto(dev);
  1349. if (err)
  1350. return err;
  1351. hifn_reset_puc(dev);
  1352. hifn_init_dma(dev);
  1353. hifn_init_registers(dev);
  1354. hifn_init_pubrng(dev);
  1355. return 0;
  1356. }
  1357. static int ablkcipher_get(void *saddr, unsigned int *srestp, unsigned int offset,
  1358. struct scatterlist *dst, unsigned int size, unsigned int *nbytesp)
  1359. {
  1360. unsigned int srest = *srestp, nbytes = *nbytesp, copy;
  1361. void *daddr;
  1362. int idx = 0;
  1363. if (srest < size || size > nbytes)
  1364. return -EINVAL;
  1365. while (size) {
  1366. copy = min3(srest, dst->length, size);
  1367. daddr = kmap_atomic(sg_page(dst));
  1368. memcpy(daddr + dst->offset + offset, saddr, copy);
  1369. kunmap_atomic(daddr);
  1370. nbytes -= copy;
  1371. size -= copy;
  1372. srest -= copy;
  1373. saddr += copy;
  1374. offset = 0;
  1375. pr_debug("%s: copy: %u, size: %u, srest: %u, nbytes: %u.\n",
  1376. __func__, copy, size, srest, nbytes);
  1377. dst++;
  1378. idx++;
  1379. }
  1380. *nbytesp = nbytes;
  1381. *srestp = srest;
  1382. return idx;
  1383. }
  1384. static inline void hifn_complete_sa(struct hifn_device *dev, int i)
  1385. {
  1386. unsigned long flags;
  1387. spin_lock_irqsave(&dev->lock, flags);
  1388. dev->sa[i] = NULL;
  1389. dev->started--;
  1390. if (dev->started < 0)
  1391. dev_info(&dev->pdev->dev, "%s: started: %d.\n", __func__,
  1392. dev->started);
  1393. spin_unlock_irqrestore(&dev->lock, flags);
  1394. BUG_ON(dev->started < 0);
  1395. }
  1396. static void hifn_process_ready(struct ablkcipher_request *req, int error)
  1397. {
  1398. struct hifn_request_context *rctx = ablkcipher_request_ctx(req);
  1399. if (rctx->walk.flags & ASYNC_FLAGS_MISALIGNED) {
  1400. unsigned int nbytes = req->nbytes;
  1401. int idx = 0, err;
  1402. struct scatterlist *dst, *t;
  1403. void *saddr;
  1404. while (nbytes) {
  1405. t = &rctx->walk.cache[idx];
  1406. dst = &req->dst[idx];
  1407. pr_debug("\n%s: sg_page(t): %p, t->length: %u, "
  1408. "sg_page(dst): %p, dst->length: %u, "
  1409. "nbytes: %u.\n",
  1410. __func__, sg_page(t), t->length,
  1411. sg_page(dst), dst->length, nbytes);
  1412. if (!t->length) {
  1413. nbytes -= min(dst->length, nbytes);
  1414. idx++;
  1415. continue;
  1416. }
  1417. saddr = kmap_atomic(sg_page(t));
  1418. err = ablkcipher_get(saddr, &t->length, t->offset,
  1419. dst, nbytes, &nbytes);
  1420. if (err < 0) {
  1421. kunmap_atomic(saddr);
  1422. break;
  1423. }
  1424. idx += err;
  1425. kunmap_atomic(saddr);
  1426. }
  1427. hifn_cipher_walk_exit(&rctx->walk);
  1428. }
  1429. req->base.complete(&req->base, error);
  1430. }
  1431. static void hifn_clear_rings(struct hifn_device *dev, int error)
  1432. {
  1433. struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
  1434. int i, u;
  1435. dev_dbg(&dev->pdev->dev, "ring cleanup 1: i: %d.%d.%d.%d, u: %d.%d.%d.%d, "
  1436. "k: %d.%d.%d.%d.\n",
  1437. dma->cmdi, dma->srci, dma->dsti, dma->resi,
  1438. dma->cmdu, dma->srcu, dma->dstu, dma->resu,
  1439. dma->cmdk, dma->srck, dma->dstk, dma->resk);
  1440. i = dma->resk; u = dma->resu;
  1441. while (u != 0) {
  1442. if (dma->resr[i].l & __cpu_to_le32(HIFN_D_VALID))
  1443. break;
  1444. if (dev->sa[i]) {
  1445. dev->success++;
  1446. dev->reset = 0;
  1447. hifn_process_ready(dev->sa[i], error);
  1448. hifn_complete_sa(dev, i);
  1449. }
  1450. if (++i == HIFN_D_RES_RSIZE)
  1451. i = 0;
  1452. u--;
  1453. }
  1454. dma->resk = i; dma->resu = u;
  1455. i = dma->srck; u = dma->srcu;
  1456. while (u != 0) {
  1457. if (dma->srcr[i].l & __cpu_to_le32(HIFN_D_VALID))
  1458. break;
  1459. if (++i == HIFN_D_SRC_RSIZE)
  1460. i = 0;
  1461. u--;
  1462. }
  1463. dma->srck = i; dma->srcu = u;
  1464. i = dma->cmdk; u = dma->cmdu;
  1465. while (u != 0) {
  1466. if (dma->cmdr[i].l & __cpu_to_le32(HIFN_D_VALID))
  1467. break;
  1468. if (++i == HIFN_D_CMD_RSIZE)
  1469. i = 0;
  1470. u--;
  1471. }
  1472. dma->cmdk = i; dma->cmdu = u;
  1473. i = dma->dstk; u = dma->dstu;
  1474. while (u != 0) {
  1475. if (dma->dstr[i].l & __cpu_to_le32(HIFN_D_VALID))
  1476. break;
  1477. if (++i == HIFN_D_DST_RSIZE)
  1478. i = 0;
  1479. u--;
  1480. }
  1481. dma->dstk = i; dma->dstu = u;
  1482. dev_dbg(&dev->pdev->dev, "ring cleanup 2: i: %d.%d.%d.%d, u: %d.%d.%d.%d, "
  1483. "k: %d.%d.%d.%d.\n",
  1484. dma->cmdi, dma->srci, dma->dsti, dma->resi,
  1485. dma->cmdu, dma->srcu, dma->dstu, dma->resu,
  1486. dma->cmdk, dma->srck, dma->dstk, dma->resk);
  1487. }
  1488. static void hifn_work(struct work_struct *work)
  1489. {
  1490. struct delayed_work *dw = to_delayed_work(work);
  1491. struct hifn_device *dev = container_of(dw, struct hifn_device, work);
  1492. unsigned long flags;
  1493. int reset = 0;
  1494. u32 r = 0;
  1495. spin_lock_irqsave(&dev->lock, flags);
  1496. if (dev->active == 0) {
  1497. struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
  1498. if (dma->cmdu == 0 && (dev->flags & HIFN_FLAG_CMD_BUSY)) {
  1499. dev->flags &= ~HIFN_FLAG_CMD_BUSY;
  1500. r |= HIFN_DMACSR_C_CTRL_DIS;
  1501. }
  1502. if (dma->srcu == 0 && (dev->flags & HIFN_FLAG_SRC_BUSY)) {
  1503. dev->flags &= ~HIFN_FLAG_SRC_BUSY;
  1504. r |= HIFN_DMACSR_S_CTRL_DIS;
  1505. }
  1506. if (dma->dstu == 0 && (dev->flags & HIFN_FLAG_DST_BUSY)) {
  1507. dev->flags &= ~HIFN_FLAG_DST_BUSY;
  1508. r |= HIFN_DMACSR_D_CTRL_DIS;
  1509. }
  1510. if (dma->resu == 0 && (dev->flags & HIFN_FLAG_RES_BUSY)) {
  1511. dev->flags &= ~HIFN_FLAG_RES_BUSY;
  1512. r |= HIFN_DMACSR_R_CTRL_DIS;
  1513. }
  1514. if (r)
  1515. hifn_write_1(dev, HIFN_1_DMA_CSR, r);
  1516. } else
  1517. dev->active--;
  1518. if ((dev->prev_success == dev->success) && dev->started)
  1519. reset = 1;
  1520. dev->prev_success = dev->success;
  1521. spin_unlock_irqrestore(&dev->lock, flags);
  1522. if (reset) {
  1523. if (++dev->reset >= 5) {
  1524. int i;
  1525. struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
  1526. dev_info(&dev->pdev->dev,
  1527. "r: %08x, active: %d, started: %d, "
  1528. "success: %lu: qlen: %u/%u, reset: %d.\n",
  1529. r, dev->active, dev->started,
  1530. dev->success, dev->queue.qlen, dev->queue.max_qlen,
  1531. reset);
  1532. dev_info(&dev->pdev->dev, "%s: res: ", __func__);
  1533. for (i = 0; i < HIFN_D_RES_RSIZE; ++i) {
  1534. pr_info("%x.%p ", dma->resr[i].l, dev->sa[i]);
  1535. if (dev->sa[i]) {
  1536. hifn_process_ready(dev->sa[i], -ENODEV);
  1537. hifn_complete_sa(dev, i);
  1538. }
  1539. }
  1540. pr_info("\n");
  1541. hifn_reset_dma(dev, 1);
  1542. hifn_stop_device(dev);
  1543. hifn_start_device(dev);
  1544. dev->reset = 0;
  1545. }
  1546. tasklet_schedule(&dev->tasklet);
  1547. }
  1548. schedule_delayed_work(&dev->work, HZ);
  1549. }
  1550. static irqreturn_t hifn_interrupt(int irq, void *data)
  1551. {
  1552. struct hifn_device *dev = (struct hifn_device *)data;
  1553. struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
  1554. u32 dmacsr, restart;
  1555. dmacsr = hifn_read_1(dev, HIFN_1_DMA_CSR);
  1556. dev_dbg(&dev->pdev->dev, "1 dmacsr: %08x, dmareg: %08x, res: %08x [%d], "
  1557. "i: %d.%d.%d.%d, u: %d.%d.%d.%d.\n",
  1558. dmacsr, dev->dmareg, dmacsr & dev->dmareg, dma->cmdi,
  1559. dma->cmdi, dma->srci, dma->dsti, dma->resi,
  1560. dma->cmdu, dma->srcu, dma->dstu, dma->resu);
  1561. if ((dmacsr & dev->dmareg) == 0)
  1562. return IRQ_NONE;
  1563. hifn_write_1(dev, HIFN_1_DMA_CSR, dmacsr & dev->dmareg);
  1564. if (dmacsr & HIFN_DMACSR_ENGINE)
  1565. hifn_write_0(dev, HIFN_0_PUISR, hifn_read_0(dev, HIFN_0_PUISR));
  1566. if (dmacsr & HIFN_DMACSR_PUBDONE)
  1567. hifn_write_1(dev, HIFN_1_PUB_STATUS,
  1568. hifn_read_1(dev, HIFN_1_PUB_STATUS) | HIFN_PUBSTS_DONE);
  1569. restart = dmacsr & (HIFN_DMACSR_R_OVER | HIFN_DMACSR_D_OVER);
  1570. if (restart) {
  1571. u32 puisr = hifn_read_0(dev, HIFN_0_PUISR);
  1572. dev_warn(&dev->pdev->dev, "overflow: r: %d, d: %d, puisr: %08x, d: %u.\n",
  1573. !!(dmacsr & HIFN_DMACSR_R_OVER),
  1574. !!(dmacsr & HIFN_DMACSR_D_OVER),
  1575. puisr, !!(puisr & HIFN_PUISR_DSTOVER));
  1576. if (!!(puisr & HIFN_PUISR_DSTOVER))
  1577. hifn_write_0(dev, HIFN_0_PUISR, HIFN_PUISR_DSTOVER);
  1578. hifn_write_1(dev, HIFN_1_DMA_CSR, dmacsr & (HIFN_DMACSR_R_OVER |
  1579. HIFN_DMACSR_D_OVER));
  1580. }
  1581. restart = dmacsr & (HIFN_DMACSR_C_ABORT | HIFN_DMACSR_S_ABORT |
  1582. HIFN_DMACSR_D_ABORT | HIFN_DMACSR_R_ABORT);
  1583. if (restart) {
  1584. dev_warn(&dev->pdev->dev, "abort: c: %d, s: %d, d: %d, r: %d.\n",
  1585. !!(dmacsr & HIFN_DMACSR_C_ABORT),
  1586. !!(dmacsr & HIFN_DMACSR_S_ABORT),
  1587. !!(dmacsr & HIFN_DMACSR_D_ABORT),
  1588. !!(dmacsr & HIFN_DMACSR_R_ABORT));
  1589. hifn_reset_dma(dev, 1);
  1590. hifn_init_dma(dev);
  1591. hifn_init_registers(dev);
  1592. }
  1593. if ((dmacsr & HIFN_DMACSR_C_WAIT) && (dma->cmdu == 0)) {
  1594. dev_dbg(&dev->pdev->dev, "wait on command.\n");
  1595. dev->dmareg &= ~(HIFN_DMAIER_C_WAIT);
  1596. hifn_write_1(dev, HIFN_1_DMA_IER, dev->dmareg);
  1597. }
  1598. tasklet_schedule(&dev->tasklet);
  1599. return IRQ_HANDLED;
  1600. }
  1601. static void hifn_flush(struct hifn_device *dev)
  1602. {
  1603. unsigned long flags;
  1604. struct crypto_async_request *async_req;
  1605. struct ablkcipher_request *req;
  1606. struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
  1607. int i;
  1608. for (i = 0; i < HIFN_D_RES_RSIZE; ++i) {
  1609. struct hifn_desc *d = &dma->resr[i];
  1610. if (dev->sa[i]) {
  1611. hifn_process_ready(dev->sa[i],
  1612. (d->l & __cpu_to_le32(HIFN_D_VALID)) ? -ENODEV : 0);
  1613. hifn_complete_sa(dev, i);
  1614. }
  1615. }
  1616. spin_lock_irqsave(&dev->lock, flags);
  1617. while ((async_req = crypto_dequeue_request(&dev->queue))) {
  1618. req = ablkcipher_request_cast(async_req);
  1619. spin_unlock_irqrestore(&dev->lock, flags);
  1620. hifn_process_ready(req, -ENODEV);
  1621. spin_lock_irqsave(&dev->lock, flags);
  1622. }
  1623. spin_unlock_irqrestore(&dev->lock, flags);
  1624. }
  1625. static int hifn_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
  1626. unsigned int len)
  1627. {
  1628. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  1629. struct hifn_context *ctx = crypto_tfm_ctx(tfm);
  1630. struct hifn_device *dev = ctx->dev;
  1631. if (len > HIFN_MAX_CRYPT_KEY_LENGTH) {
  1632. crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1633. return -1;
  1634. }
  1635. if (len == HIFN_DES_KEY_LENGTH) {
  1636. u32 tmp[DES_EXPKEY_WORDS];
  1637. int ret = des_ekey(tmp, key);
  1638. if (unlikely(ret == 0) && (tfm->crt_flags & CRYPTO_TFM_REQ_WEAK_KEY)) {
  1639. tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
  1640. return -EINVAL;
  1641. }
  1642. }
  1643. dev->flags &= ~HIFN_FLAG_OLD_KEY;
  1644. memcpy(ctx->key, key, len);
  1645. ctx->keysize = len;
  1646. return 0;
  1647. }
  1648. static int hifn_handle_req(struct ablkcipher_request *req)
  1649. {
  1650. struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
  1651. struct hifn_device *dev = ctx->dev;
  1652. int err = -EAGAIN;
  1653. if (dev->started + DIV_ROUND_UP(req->nbytes, PAGE_SIZE) <= HIFN_QUEUE_LENGTH)
  1654. err = hifn_setup_session(req);
  1655. if (err == -EAGAIN) {
  1656. unsigned long flags;
  1657. spin_lock_irqsave(&dev->lock, flags);
  1658. err = ablkcipher_enqueue_request(&dev->queue, req);
  1659. spin_unlock_irqrestore(&dev->lock, flags);
  1660. }
  1661. return err;
  1662. }
  1663. static int hifn_setup_crypto_req(struct ablkcipher_request *req, u8 op,
  1664. u8 type, u8 mode)
  1665. {
  1666. struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
  1667. struct hifn_request_context *rctx = ablkcipher_request_ctx(req);
  1668. unsigned ivsize;
  1669. ivsize = crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(req));
  1670. if (req->info && mode != ACRYPTO_MODE_ECB) {
  1671. if (type == ACRYPTO_TYPE_AES_128)
  1672. ivsize = HIFN_AES_IV_LENGTH;
  1673. else if (type == ACRYPTO_TYPE_DES)
  1674. ivsize = HIFN_DES_KEY_LENGTH;
  1675. else if (type == ACRYPTO_TYPE_3DES)
  1676. ivsize = HIFN_3DES_KEY_LENGTH;
  1677. }
  1678. if (ctx->keysize != 16 && type == ACRYPTO_TYPE_AES_128) {
  1679. if (ctx->keysize == 24)
  1680. type = ACRYPTO_TYPE_AES_192;
  1681. else if (ctx->keysize == 32)
  1682. type = ACRYPTO_TYPE_AES_256;
  1683. }
  1684. rctx->op = op;
  1685. rctx->mode = mode;
  1686. rctx->type = type;
  1687. rctx->iv = req->info;
  1688. rctx->ivsize = ivsize;
  1689. /*
  1690. * HEAVY TODO: needs to kick Herbert XU to write documentation.
  1691. * HEAVY TODO: needs to kick Herbert XU to write documentation.
  1692. * HEAVY TODO: needs to kick Herbert XU to write documentation.
  1693. */
  1694. return hifn_handle_req(req);
  1695. }
  1696. static int hifn_process_queue(struct hifn_device *dev)
  1697. {
  1698. struct crypto_async_request *async_req, *backlog;
  1699. struct ablkcipher_request *req;
  1700. unsigned long flags;
  1701. int err = 0;
  1702. while (dev->started < HIFN_QUEUE_LENGTH) {
  1703. spin_lock_irqsave(&dev->lock, flags);
  1704. backlog = crypto_get_backlog(&dev->queue);
  1705. async_req = crypto_dequeue_request(&dev->queue);
  1706. spin_unlock_irqrestore(&dev->lock, flags);
  1707. if (!async_req)
  1708. break;
  1709. if (backlog)
  1710. backlog->complete(backlog, -EINPROGRESS);
  1711. req = ablkcipher_request_cast(async_req);
  1712. err = hifn_handle_req(req);
  1713. if (err)
  1714. break;
  1715. }
  1716. return err;
  1717. }
  1718. static int hifn_setup_crypto(struct ablkcipher_request *req, u8 op,
  1719. u8 type, u8 mode)
  1720. {
  1721. int err;
  1722. struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
  1723. struct hifn_device *dev = ctx->dev;
  1724. err = hifn_setup_crypto_req(req, op, type, mode);
  1725. if (err)
  1726. return err;
  1727. if (dev->started < HIFN_QUEUE_LENGTH && dev->queue.qlen)
  1728. hifn_process_queue(dev);
  1729. return -EINPROGRESS;
  1730. }
  1731. /*
  1732. * AES ecryption functions.
  1733. */
  1734. static inline int hifn_encrypt_aes_ecb(struct ablkcipher_request *req)
  1735. {
  1736. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1737. ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_ECB);
  1738. }
  1739. static inline int hifn_encrypt_aes_cbc(struct ablkcipher_request *req)
  1740. {
  1741. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1742. ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CBC);
  1743. }
  1744. static inline int hifn_encrypt_aes_cfb(struct ablkcipher_request *req)
  1745. {
  1746. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1747. ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CFB);
  1748. }
  1749. static inline int hifn_encrypt_aes_ofb(struct ablkcipher_request *req)
  1750. {
  1751. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1752. ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_OFB);
  1753. }
  1754. /*
  1755. * AES decryption functions.
  1756. */
  1757. static inline int hifn_decrypt_aes_ecb(struct ablkcipher_request *req)
  1758. {
  1759. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1760. ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_ECB);
  1761. }
  1762. static inline int hifn_decrypt_aes_cbc(struct ablkcipher_request *req)
  1763. {
  1764. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1765. ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CBC);
  1766. }
  1767. static inline int hifn_decrypt_aes_cfb(struct ablkcipher_request *req)
  1768. {
  1769. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1770. ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_CFB);
  1771. }
  1772. static inline int hifn_decrypt_aes_ofb(struct ablkcipher_request *req)
  1773. {
  1774. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1775. ACRYPTO_TYPE_AES_128, ACRYPTO_MODE_OFB);
  1776. }
  1777. /*
  1778. * DES ecryption functions.
  1779. */
  1780. static inline int hifn_encrypt_des_ecb(struct ablkcipher_request *req)
  1781. {
  1782. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1783. ACRYPTO_TYPE_DES, ACRYPTO_MODE_ECB);
  1784. }
  1785. static inline int hifn_encrypt_des_cbc(struct ablkcipher_request *req)
  1786. {
  1787. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1788. ACRYPTO_TYPE_DES, ACRYPTO_MODE_CBC);
  1789. }
  1790. static inline int hifn_encrypt_des_cfb(struct ablkcipher_request *req)
  1791. {
  1792. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1793. ACRYPTO_TYPE_DES, ACRYPTO_MODE_CFB);
  1794. }
  1795. static inline int hifn_encrypt_des_ofb(struct ablkcipher_request *req)
  1796. {
  1797. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1798. ACRYPTO_TYPE_DES, ACRYPTO_MODE_OFB);
  1799. }
  1800. /*
  1801. * DES decryption functions.
  1802. */
  1803. static inline int hifn_decrypt_des_ecb(struct ablkcipher_request *req)
  1804. {
  1805. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1806. ACRYPTO_TYPE_DES, ACRYPTO_MODE_ECB);
  1807. }
  1808. static inline int hifn_decrypt_des_cbc(struct ablkcipher_request *req)
  1809. {
  1810. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1811. ACRYPTO_TYPE_DES, ACRYPTO_MODE_CBC);
  1812. }
  1813. static inline int hifn_decrypt_des_cfb(struct ablkcipher_request *req)
  1814. {
  1815. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1816. ACRYPTO_TYPE_DES, ACRYPTO_MODE_CFB);
  1817. }
  1818. static inline int hifn_decrypt_des_ofb(struct ablkcipher_request *req)
  1819. {
  1820. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1821. ACRYPTO_TYPE_DES, ACRYPTO_MODE_OFB);
  1822. }
  1823. /*
  1824. * 3DES ecryption functions.
  1825. */
  1826. static inline int hifn_encrypt_3des_ecb(struct ablkcipher_request *req)
  1827. {
  1828. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1829. ACRYPTO_TYPE_3DES, ACRYPTO_MODE_ECB);
  1830. }
  1831. static inline int hifn_encrypt_3des_cbc(struct ablkcipher_request *req)
  1832. {
  1833. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1834. ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CBC);
  1835. }
  1836. static inline int hifn_encrypt_3des_cfb(struct ablkcipher_request *req)
  1837. {
  1838. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1839. ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CFB);
  1840. }
  1841. static inline int hifn_encrypt_3des_ofb(struct ablkcipher_request *req)
  1842. {
  1843. return hifn_setup_crypto(req, ACRYPTO_OP_ENCRYPT,
  1844. ACRYPTO_TYPE_3DES, ACRYPTO_MODE_OFB);
  1845. }
  1846. /* 3DES decryption functions. */
  1847. static inline int hifn_decrypt_3des_ecb(struct ablkcipher_request *req)
  1848. {
  1849. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1850. ACRYPTO_TYPE_3DES, ACRYPTO_MODE_ECB);
  1851. }
  1852. static inline int hifn_decrypt_3des_cbc(struct ablkcipher_request *req)
  1853. {
  1854. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1855. ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CBC);
  1856. }
  1857. static inline int hifn_decrypt_3des_cfb(struct ablkcipher_request *req)
  1858. {
  1859. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1860. ACRYPTO_TYPE_3DES, ACRYPTO_MODE_CFB);
  1861. }
  1862. static inline int hifn_decrypt_3des_ofb(struct ablkcipher_request *req)
  1863. {
  1864. return hifn_setup_crypto(req, ACRYPTO_OP_DECRYPT,
  1865. ACRYPTO_TYPE_3DES, ACRYPTO_MODE_OFB);
  1866. }
  1867. struct hifn_alg_template {
  1868. char name[CRYPTO_MAX_ALG_NAME];
  1869. char drv_name[CRYPTO_MAX_ALG_NAME];
  1870. unsigned int bsize;
  1871. struct ablkcipher_alg ablkcipher;
  1872. };
  1873. static struct hifn_alg_template hifn_alg_templates[] = {
  1874. /*
  1875. * 3DES ECB, CBC, CFB and OFB modes.
  1876. */
  1877. {
  1878. .name = "cfb(des3_ede)", .drv_name = "cfb-3des", .bsize = 8,
  1879. .ablkcipher = {
  1880. .min_keysize = HIFN_3DES_KEY_LENGTH,
  1881. .max_keysize = HIFN_3DES_KEY_LENGTH,
  1882. .setkey = hifn_setkey,
  1883. .encrypt = hifn_encrypt_3des_cfb,
  1884. .decrypt = hifn_decrypt_3des_cfb,
  1885. },
  1886. },
  1887. {
  1888. .name = "ofb(des3_ede)", .drv_name = "ofb-3des", .bsize = 8,
  1889. .ablkcipher = {
  1890. .min_keysize = HIFN_3DES_KEY_LENGTH,
  1891. .max_keysize = HIFN_3DES_KEY_LENGTH,
  1892. .setkey = hifn_setkey,
  1893. .encrypt = hifn_encrypt_3des_ofb,
  1894. .decrypt = hifn_decrypt_3des_ofb,
  1895. },
  1896. },
  1897. {
  1898. .name = "cbc(des3_ede)", .drv_name = "cbc-3des", .bsize = 8,
  1899. .ablkcipher = {
  1900. .ivsize = HIFN_IV_LENGTH,
  1901. .min_keysize = HIFN_3DES_KEY_LENGTH,
  1902. .max_keysize = HIFN_3DES_KEY_LENGTH,
  1903. .setkey = hifn_setkey,
  1904. .encrypt = hifn_encrypt_3des_cbc,
  1905. .decrypt = hifn_decrypt_3des_cbc,
  1906. },
  1907. },
  1908. {
  1909. .name = "ecb(des3_ede)", .drv_name = "ecb-3des", .bsize = 8,
  1910. .ablkcipher = {
  1911. .min_keysize = HIFN_3DES_KEY_LENGTH,
  1912. .max_keysize = HIFN_3DES_KEY_LENGTH,
  1913. .setkey = hifn_setkey,
  1914. .encrypt = hifn_encrypt_3des_ecb,
  1915. .decrypt = hifn_decrypt_3des_ecb,
  1916. },
  1917. },
  1918. /*
  1919. * DES ECB, CBC, CFB and OFB modes.
  1920. */
  1921. {
  1922. .name = "cfb(des)", .drv_name = "cfb-des", .bsize = 8,
  1923. .ablkcipher = {
  1924. .min_keysize = HIFN_DES_KEY_LENGTH,
  1925. .max_keysize = HIFN_DES_KEY_LENGTH,
  1926. .setkey = hifn_setkey,
  1927. .encrypt = hifn_encrypt_des_cfb,
  1928. .decrypt = hifn_decrypt_des_cfb,
  1929. },
  1930. },
  1931. {
  1932. .name = "ofb(des)", .drv_name = "ofb-des", .bsize = 8,
  1933. .ablkcipher = {
  1934. .min_keysize = HIFN_DES_KEY_LENGTH,
  1935. .max_keysize = HIFN_DES_KEY_LENGTH,
  1936. .setkey = hifn_setkey,
  1937. .encrypt = hifn_encrypt_des_ofb,
  1938. .decrypt = hifn_decrypt_des_ofb,
  1939. },
  1940. },
  1941. {
  1942. .name = "cbc(des)", .drv_name = "cbc-des", .bsize = 8,
  1943. .ablkcipher = {
  1944. .ivsize = HIFN_IV_LENGTH,
  1945. .min_keysize = HIFN_DES_KEY_LENGTH,
  1946. .max_keysize = HIFN_DES_KEY_LENGTH,
  1947. .setkey = hifn_setkey,
  1948. .encrypt = hifn_encrypt_des_cbc,
  1949. .decrypt = hifn_decrypt_des_cbc,
  1950. },
  1951. },
  1952. {
  1953. .name = "ecb(des)", .drv_name = "ecb-des", .bsize = 8,
  1954. .ablkcipher = {
  1955. .min_keysize = HIFN_DES_KEY_LENGTH,
  1956. .max_keysize = HIFN_DES_KEY_LENGTH,
  1957. .setkey = hifn_setkey,
  1958. .encrypt = hifn_encrypt_des_ecb,
  1959. .decrypt = hifn_decrypt_des_ecb,
  1960. },
  1961. },
  1962. /*
  1963. * AES ECB, CBC, CFB and OFB modes.
  1964. */
  1965. {
  1966. .name = "ecb(aes)", .drv_name = "ecb-aes", .bsize = 16,
  1967. .ablkcipher = {
  1968. .min_keysize = AES_MIN_KEY_SIZE,
  1969. .max_keysize = AES_MAX_KEY_SIZE,
  1970. .setkey = hifn_setkey,
  1971. .encrypt = hifn_encrypt_aes_ecb,
  1972. .decrypt = hifn_decrypt_aes_ecb,
  1973. },
  1974. },
  1975. {
  1976. .name = "cbc(aes)", .drv_name = "cbc-aes", .bsize = 16,
  1977. .ablkcipher = {
  1978. .ivsize = HIFN_AES_IV_LENGTH,
  1979. .min_keysize = AES_MIN_KEY_SIZE,
  1980. .max_keysize = AES_MAX_KEY_SIZE,
  1981. .setkey = hifn_setkey,
  1982. .encrypt = hifn_encrypt_aes_cbc,
  1983. .decrypt = hifn_decrypt_aes_cbc,
  1984. },
  1985. },
  1986. {
  1987. .name = "cfb(aes)", .drv_name = "cfb-aes", .bsize = 16,
  1988. .ablkcipher = {
  1989. .min_keysize = AES_MIN_KEY_SIZE,
  1990. .max_keysize = AES_MAX_KEY_SIZE,
  1991. .setkey = hifn_setkey,
  1992. .encrypt = hifn_encrypt_aes_cfb,
  1993. .decrypt = hifn_decrypt_aes_cfb,
  1994. },
  1995. },
  1996. {
  1997. .name = "ofb(aes)", .drv_name = "ofb-aes", .bsize = 16,
  1998. .ablkcipher = {
  1999. .min_keysize = AES_MIN_KEY_SIZE,
  2000. .max_keysize = AES_MAX_KEY_SIZE,
  2001. .setkey = hifn_setkey,
  2002. .encrypt = hifn_encrypt_aes_ofb,
  2003. .decrypt = hifn_decrypt_aes_ofb,
  2004. },
  2005. },
  2006. };
  2007. static int hifn_cra_init(struct crypto_tfm *tfm)
  2008. {
  2009. struct crypto_alg *alg = tfm->__crt_alg;
  2010. struct hifn_crypto_alg *ha = crypto_alg_to_hifn(alg);
  2011. struct hifn_context *ctx = crypto_tfm_ctx(tfm);
  2012. ctx->dev = ha->dev;
  2013. tfm->crt_ablkcipher.reqsize = sizeof(struct hifn_request_context);
  2014. return 0;
  2015. }
  2016. static int hifn_alg_alloc(struct hifn_device *dev, struct hifn_alg_template *t)
  2017. {
  2018. struct hifn_crypto_alg *alg;
  2019. int err;
  2020. alg = kzalloc(sizeof(*alg), GFP_KERNEL);
  2021. if (!alg)
  2022. return -ENOMEM;
  2023. snprintf(alg->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s", t->name);
  2024. snprintf(alg->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s-%s",
  2025. t->drv_name, dev->name);
  2026. alg->alg.cra_priority = 300;
  2027. alg->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  2028. CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC;
  2029. alg->alg.cra_blocksize = t->bsize;
  2030. alg->alg.cra_ctxsize = sizeof(struct hifn_context);
  2031. alg->alg.cra_alignmask = 0;
  2032. alg->alg.cra_type = &crypto_ablkcipher_type;
  2033. alg->alg.cra_module = THIS_MODULE;
  2034. alg->alg.cra_u.ablkcipher = t->ablkcipher;
  2035. alg->alg.cra_init = hifn_cra_init;
  2036. alg->dev = dev;
  2037. list_add_tail(&alg->entry, &dev->alg_list);
  2038. err = crypto_register_alg(&alg->alg);
  2039. if (err) {
  2040. list_del(&alg->entry);
  2041. kfree(alg);
  2042. }
  2043. return err;
  2044. }
  2045. static void hifn_unregister_alg(struct hifn_device *dev)
  2046. {
  2047. struct hifn_crypto_alg *a, *n;
  2048. list_for_each_entry_safe(a, n, &dev->alg_list, entry) {
  2049. list_del(&a->entry);
  2050. crypto_unregister_alg(&a->alg);
  2051. kfree(a);
  2052. }
  2053. }
  2054. static int hifn_register_alg(struct hifn_device *dev)
  2055. {
  2056. int i, err;
  2057. for (i = 0; i < ARRAY_SIZE(hifn_alg_templates); ++i) {
  2058. err = hifn_alg_alloc(dev, &hifn_alg_templates[i]);
  2059. if (err)
  2060. goto err_out_exit;
  2061. }
  2062. return 0;
  2063. err_out_exit:
  2064. hifn_unregister_alg(dev);
  2065. return err;
  2066. }
  2067. static void hifn_tasklet_callback(unsigned long data)
  2068. {
  2069. struct hifn_device *dev = (struct hifn_device *)data;
  2070. /*
  2071. * This is ok to call this without lock being held,
  2072. * althogh it modifies some parameters used in parallel,
  2073. * (like dev->success), but they are used in process
  2074. * context or update is atomic (like setting dev->sa[i] to NULL).
  2075. */
  2076. hifn_clear_rings(dev, 0);
  2077. if (dev->started < HIFN_QUEUE_LENGTH && dev->queue.qlen)
  2078. hifn_process_queue(dev);
  2079. }
  2080. static int hifn_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  2081. {
  2082. int err, i;
  2083. struct hifn_device *dev;
  2084. char name[8];
  2085. err = pci_enable_device(pdev);
  2086. if (err)
  2087. return err;
  2088. pci_set_master(pdev);
  2089. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2090. if (err)
  2091. goto err_out_disable_pci_device;
  2092. snprintf(name, sizeof(name), "hifn%d",
  2093. atomic_inc_return(&hifn_dev_number) - 1);
  2094. err = pci_request_regions(pdev, name);
  2095. if (err)
  2096. goto err_out_disable_pci_device;
  2097. if (pci_resource_len(pdev, 0) < HIFN_BAR0_SIZE ||
  2098. pci_resource_len(pdev, 1) < HIFN_BAR1_SIZE ||
  2099. pci_resource_len(pdev, 2) < HIFN_BAR2_SIZE) {
  2100. dev_err(&pdev->dev, "Broken hardware - I/O regions are too small.\n");
  2101. err = -ENODEV;
  2102. goto err_out_free_regions;
  2103. }
  2104. dev = kzalloc(sizeof(struct hifn_device) + sizeof(struct crypto_alg),
  2105. GFP_KERNEL);
  2106. if (!dev) {
  2107. err = -ENOMEM;
  2108. goto err_out_free_regions;
  2109. }
  2110. INIT_LIST_HEAD(&dev->alg_list);
  2111. snprintf(dev->name, sizeof(dev->name), "%s", name);
  2112. spin_lock_init(&dev->lock);
  2113. for (i = 0; i < 3; ++i) {
  2114. unsigned long addr, size;
  2115. addr = pci_resource_start(pdev, i);
  2116. size = pci_resource_len(pdev, i);
  2117. dev->bar[i] = ioremap_nocache(addr, size);
  2118. if (!dev->bar[i]) {
  2119. err = -ENOMEM;
  2120. goto err_out_unmap_bars;
  2121. }
  2122. }
  2123. dev->desc_virt = pci_zalloc_consistent(pdev, sizeof(struct hifn_dma),
  2124. &dev->desc_dma);
  2125. if (!dev->desc_virt) {
  2126. dev_err(&pdev->dev, "Failed to allocate descriptor rings.\n");
  2127. err = -ENOMEM;
  2128. goto err_out_unmap_bars;
  2129. }
  2130. dev->pdev = pdev;
  2131. dev->irq = pdev->irq;
  2132. for (i = 0; i < HIFN_D_RES_RSIZE; ++i)
  2133. dev->sa[i] = NULL;
  2134. pci_set_drvdata(pdev, dev);
  2135. tasklet_init(&dev->tasklet, hifn_tasklet_callback, (unsigned long)dev);
  2136. crypto_init_queue(&dev->queue, 1);
  2137. err = request_irq(dev->irq, hifn_interrupt, IRQF_SHARED, dev->name, dev);
  2138. if (err) {
  2139. dev_err(&pdev->dev, "Failed to request IRQ%d: err: %d.\n",
  2140. dev->irq, err);
  2141. dev->irq = 0;
  2142. goto err_out_free_desc;
  2143. }
  2144. err = hifn_start_device(dev);
  2145. if (err)
  2146. goto err_out_free_irq;
  2147. err = hifn_register_rng(dev);
  2148. if (err)
  2149. goto err_out_stop_device;
  2150. err = hifn_register_alg(dev);
  2151. if (err)
  2152. goto err_out_unregister_rng;
  2153. INIT_DELAYED_WORK(&dev->work, hifn_work);
  2154. schedule_delayed_work(&dev->work, HZ);
  2155. dev_dbg(&pdev->dev, "HIFN crypto accelerator card at %s has been "
  2156. "successfully registered as %s.\n",
  2157. pci_name(pdev), dev->name);
  2158. return 0;
  2159. err_out_unregister_rng:
  2160. hifn_unregister_rng(dev);
  2161. err_out_stop_device:
  2162. hifn_reset_dma(dev, 1);
  2163. hifn_stop_device(dev);
  2164. err_out_free_irq:
  2165. free_irq(dev->irq, dev);
  2166. tasklet_kill(&dev->tasklet);
  2167. err_out_free_desc:
  2168. pci_free_consistent(pdev, sizeof(struct hifn_dma),
  2169. dev->desc_virt, dev->desc_dma);
  2170. err_out_unmap_bars:
  2171. for (i = 0; i < 3; ++i)
  2172. if (dev->bar[i])
  2173. iounmap(dev->bar[i]);
  2174. err_out_free_regions:
  2175. pci_release_regions(pdev);
  2176. err_out_disable_pci_device:
  2177. pci_disable_device(pdev);
  2178. return err;
  2179. }
  2180. static void hifn_remove(struct pci_dev *pdev)
  2181. {
  2182. int i;
  2183. struct hifn_device *dev;
  2184. dev = pci_get_drvdata(pdev);
  2185. if (dev) {
  2186. cancel_delayed_work_sync(&dev->work);
  2187. hifn_unregister_rng(dev);
  2188. hifn_unregister_alg(dev);
  2189. hifn_reset_dma(dev, 1);
  2190. hifn_stop_device(dev);
  2191. free_irq(dev->irq, dev);
  2192. tasklet_kill(&dev->tasklet);
  2193. hifn_flush(dev);
  2194. pci_free_consistent(pdev, sizeof(struct hifn_dma),
  2195. dev->desc_virt, dev->desc_dma);
  2196. for (i = 0; i < 3; ++i)
  2197. if (dev->bar[i])
  2198. iounmap(dev->bar[i]);
  2199. kfree(dev);
  2200. }
  2201. pci_release_regions(pdev);
  2202. pci_disable_device(pdev);
  2203. }
  2204. static struct pci_device_id hifn_pci_tbl[] = {
  2205. { PCI_DEVICE(PCI_VENDOR_ID_HIFN, PCI_DEVICE_ID_HIFN_7955) },
  2206. { PCI_DEVICE(PCI_VENDOR_ID_HIFN, PCI_DEVICE_ID_HIFN_7956) },
  2207. { 0 }
  2208. };
  2209. MODULE_DEVICE_TABLE(pci, hifn_pci_tbl);
  2210. static struct pci_driver hifn_pci_driver = {
  2211. .name = "hifn795x",
  2212. .id_table = hifn_pci_tbl,
  2213. .probe = hifn_probe,
  2214. .remove = hifn_remove,
  2215. };
  2216. static int __init hifn_init(void)
  2217. {
  2218. unsigned int freq;
  2219. int err;
  2220. /* HIFN supports only 32-bit addresses */
  2221. BUILD_BUG_ON(sizeof(dma_addr_t) != 4);
  2222. if (strncmp(hifn_pll_ref, "ext", 3) &&
  2223. strncmp(hifn_pll_ref, "pci", 3)) {
  2224. pr_err("hifn795x: invalid hifn_pll_ref clock, must be pci or ext");
  2225. return -EINVAL;
  2226. }
  2227. /*
  2228. * For the 7955/7956 the reference clock frequency must be in the
  2229. * range of 20MHz-100MHz. For the 7954 the upper bound is 66.67MHz,
  2230. * but this chip is currently not supported.
  2231. */
  2232. if (hifn_pll_ref[3] != '\0') {
  2233. freq = simple_strtoul(hifn_pll_ref + 3, NULL, 10);
  2234. if (freq < 20 || freq > 100) {
  2235. pr_err("hifn795x: invalid hifn_pll_ref frequency, must"
  2236. "be in the range of 20-100");
  2237. return -EINVAL;
  2238. }
  2239. }
  2240. err = pci_register_driver(&hifn_pci_driver);
  2241. if (err < 0) {
  2242. pr_err("Failed to register PCI driver for %s device.\n",
  2243. hifn_pci_driver.name);
  2244. return -ENODEV;
  2245. }
  2246. pr_info("Driver for HIFN 795x crypto accelerator chip "
  2247. "has been successfully registered.\n");
  2248. return 0;
  2249. }
  2250. static void __exit hifn_fini(void)
  2251. {
  2252. pci_unregister_driver(&hifn_pci_driver);
  2253. pr_info("Driver for HIFN 795x crypto accelerator chip "
  2254. "has been successfully unregistered.\n");
  2255. }
  2256. module_init(hifn_init);
  2257. module_exit(hifn_fini);
  2258. MODULE_LICENSE("GPL");
  2259. MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
  2260. MODULE_DESCRIPTION("Driver for HIFN 795x crypto accelerator chip.");