hifn7751.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954
  1. /* $OpenBSD: hifn7751.c,v 1.120 2002/05/17 00:33:34 deraadt Exp $ */
  2. /*-
  3. * Invertex AEON / Hifn 7751 driver
  4. * Copyright (c) 1999 Invertex Inc. All rights reserved.
  5. * Copyright (c) 1999 Theo de Raadt
  6. * Copyright (c) 2000-2001 Network Security Technologies, Inc.
  7. * http://www.netsec.net
  8. * Copyright (c) 2003 Hifn Inc.
  9. *
  10. * This driver is based on a previous driver by Invertex, for which they
  11. * requested: Please send any comments, feedback, bug-fixes, or feature
  12. * requests to software@invertex.com.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * 3. The name of the author may not be used to endorse or promote products
  24. * derived from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  27. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  28. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  29. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  31. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  35. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * Effort sponsored in part by the Defense Advanced Research Projects
  38. * Agency (DARPA) and Air Force Research Laboratory, Air Force
  39. * Materiel Command, USAF, under agreement number F30602-01-2-0537.
  40. *
  41. *
  42. __FBSDID("$FreeBSD: src/sys/dev/hifn/hifn7751.c,v 1.40 2007/03/21 03:42:49 sam Exp $");
  43. */
  44. /*
  45. * Driver for various Hifn encryption processors.
  46. */
  47. #include <linux/version.h>
  48. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,38) && !defined(AUTOCONF_INCLUDED)
  49. #include <linux/config.h>
  50. #endif
  51. #include <linux/module.h>
  52. #include <linux/init.h>
  53. #include <linux/list.h>
  54. #include <linux/slab.h>
  55. #include <linux/wait.h>
  56. #include <linux/sched.h>
  57. #include <linux/pci.h>
  58. #include <linux/delay.h>
  59. #include <linux/interrupt.h>
  60. #include <linux/spinlock.h>
  61. #include <linux/random.h>
  62. #include <linux/skbuff.h>
  63. #include <asm/io.h>
  64. #include <cryptodev.h>
  65. #include <uio.h>
  66. #include <hifn/hifn7751reg.h>
  67. #include <hifn/hifn7751var.h>
  68. #if 1
  69. #define DPRINTF(a...) if (hifn_debug) { \
  70. printk("%s: ", sc ? \
  71. device_get_nameunit(sc->sc_dev) : "hifn"); \
  72. printk(a); \
  73. } else
  74. #else
  75. #define DPRINTF(a...)
  76. #endif
  77. static inline int
  78. pci_get_revid(struct pci_dev *dev)
  79. {
  80. u8 rid = 0;
  81. pci_read_config_byte(dev, PCI_REVISION_ID, &rid);
  82. return rid;
  83. }
  84. static struct hifn_stats hifnstats;
  85. #define debug hifn_debug
  86. int hifn_debug = 0;
  87. module_param(hifn_debug, int, 0644);
  88. MODULE_PARM_DESC(hifn_debug, "Enable debug");
  89. int hifn_maxbatch = 1;
  90. module_param(hifn_maxbatch, int, 0644);
  91. MODULE_PARM_DESC(hifn_maxbatch, "max ops to batch w/o interrupt");
  92. int hifn_cache_linesize = 0x10;
  93. module_param(hifn_cache_linesize, int, 0444);
  94. MODULE_PARM_DESC(hifn_cache_linesize, "PCI config cache line size");
  95. #ifdef MODULE_PARM
  96. char *hifn_pllconfig = NULL;
  97. MODULE_PARM(hifn_pllconfig, "s");
  98. #else
  99. char hifn_pllconfig[32]; /* This setting is RO after loading */
  100. module_param_string(hifn_pllconfig, hifn_pllconfig, 32, 0444);
  101. #endif
  102. MODULE_PARM_DESC(hifn_pllconfig, "PLL config, ie., pci66, ext33, ...");
  103. #ifdef HIFN_VULCANDEV
  104. #include <sys/conf.h>
  105. #include <sys/uio.h>
  106. static struct cdevsw vulcanpk_cdevsw; /* forward declaration */
  107. #endif
  108. /*
  109. * Prototypes and count for the pci_device structure
  110. */
  111. static int hifn_probe(struct pci_dev *dev, const struct pci_device_id *ent);
  112. static void hifn_remove(struct pci_dev *dev);
  113. static int hifn_newsession(device_t, u_int32_t *, struct cryptoini *);
  114. static int hifn_freesession(device_t, u_int64_t);
  115. static int hifn_process(device_t, struct cryptop *, int);
  116. static device_method_t hifn_methods = {
  117. /* crypto device methods */
  118. DEVMETHOD(cryptodev_newsession, hifn_newsession),
  119. DEVMETHOD(cryptodev_freesession,hifn_freesession),
  120. DEVMETHOD(cryptodev_process, hifn_process),
  121. };
  122. static void hifn_reset_board(struct hifn_softc *, int);
  123. static void hifn_reset_puc(struct hifn_softc *);
  124. static void hifn_puc_wait(struct hifn_softc *);
  125. static int hifn_enable_crypto(struct hifn_softc *);
  126. static void hifn_set_retry(struct hifn_softc *sc);
  127. static void hifn_init_dma(struct hifn_softc *);
  128. static void hifn_init_pci_registers(struct hifn_softc *);
  129. static int hifn_sramsize(struct hifn_softc *);
  130. static int hifn_dramsize(struct hifn_softc *);
  131. static int hifn_ramtype(struct hifn_softc *);
  132. static void hifn_sessions(struct hifn_softc *);
  133. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
  134. static irqreturn_t hifn_intr(int irq, void *arg);
  135. #else
  136. static irqreturn_t hifn_intr(int irq, void *arg, struct pt_regs *regs);
  137. #endif
  138. static u_int hifn_write_command(struct hifn_command *, u_int8_t *);
  139. static u_int32_t hifn_next_signature(u_int32_t a, u_int cnt);
  140. static void hifn_callback(struct hifn_softc *, struct hifn_command *, u_int8_t *);
  141. static int hifn_crypto(struct hifn_softc *, struct hifn_command *, struct cryptop *, int);
  142. static int hifn_readramaddr(struct hifn_softc *, int, u_int8_t *);
  143. static int hifn_writeramaddr(struct hifn_softc *, int, u_int8_t *);
  144. static int hifn_dmamap_load_src(struct hifn_softc *, struct hifn_command *);
  145. static int hifn_dmamap_load_dst(struct hifn_softc *, struct hifn_command *);
  146. static int hifn_init_pubrng(struct hifn_softc *);
  147. static void hifn_tick(unsigned long arg);
  148. static void hifn_abort(struct hifn_softc *);
  149. static void hifn_alloc_slot(struct hifn_softc *, int *, int *, int *, int *);
  150. static void hifn_write_reg_0(struct hifn_softc *, bus_size_t, u_int32_t);
  151. static void hifn_write_reg_1(struct hifn_softc *, bus_size_t, u_int32_t);
  152. #ifdef CONFIG_OCF_RANDOMHARVEST
  153. static int hifn_read_random(void *arg, u_int32_t *buf, int len);
  154. #endif
  155. #define HIFN_MAX_CHIPS 8
  156. static struct hifn_softc *hifn_chip_idx[HIFN_MAX_CHIPS];
  157. static __inline u_int32_t
  158. READ_REG_0(struct hifn_softc *sc, bus_size_t reg)
  159. {
  160. u_int32_t v = readl(sc->sc_bar0 + reg);
  161. sc->sc_bar0_lastreg = (bus_size_t) -1;
  162. return (v);
  163. }
  164. #define WRITE_REG_0(sc, reg, val) hifn_write_reg_0(sc, reg, val)
  165. static __inline u_int32_t
  166. READ_REG_1(struct hifn_softc *sc, bus_size_t reg)
  167. {
  168. u_int32_t v = readl(sc->sc_bar1 + reg);
  169. sc->sc_bar1_lastreg = (bus_size_t) -1;
  170. return (v);
  171. }
  172. #define WRITE_REG_1(sc, reg, val) hifn_write_reg_1(sc, reg, val)
  173. /*
  174. * map in a given buffer (great on some arches :-)
  175. */
  176. static int
  177. pci_map_uio(struct hifn_softc *sc, struct hifn_operand *buf, struct uio *uio)
  178. {
  179. struct iovec *iov = uio->uio_iov;
  180. DPRINTF("%s()\n", __FUNCTION__);
  181. buf->mapsize = 0;
  182. for (buf->nsegs = 0; buf->nsegs < uio->uio_iovcnt; ) {
  183. buf->segs[buf->nsegs].ds_addr = pci_map_single(sc->sc_pcidev,
  184. iov->iov_base, iov->iov_len,
  185. PCI_DMA_BIDIRECTIONAL);
  186. buf->segs[buf->nsegs].ds_len = iov->iov_len;
  187. buf->mapsize += iov->iov_len;
  188. iov++;
  189. buf->nsegs++;
  190. }
  191. /* identify this buffer by the first segment */
  192. buf->map = (void *) buf->segs[0].ds_addr;
  193. return(0);
  194. }
  195. /*
  196. * map in a given sk_buff
  197. */
  198. static int
  199. pci_map_skb(struct hifn_softc *sc,struct hifn_operand *buf,struct sk_buff *skb)
  200. {
  201. int i;
  202. DPRINTF("%s()\n", __FUNCTION__);
  203. buf->mapsize = 0;
  204. buf->segs[0].ds_addr = pci_map_single(sc->sc_pcidev,
  205. skb->data, skb_headlen(skb), PCI_DMA_BIDIRECTIONAL);
  206. buf->segs[0].ds_len = skb_headlen(skb);
  207. buf->mapsize += buf->segs[0].ds_len;
  208. buf->nsegs = 1;
  209. for (i = 0; i < skb_shinfo(skb)->nr_frags; ) {
  210. buf->segs[buf->nsegs].ds_len = skb_shinfo(skb)->frags[i].size;
  211. buf->segs[buf->nsegs].ds_addr = pci_map_single(sc->sc_pcidev,
  212. page_address(skb_frag_page(&skb_shinfo(skb)->frags[i])) +
  213. skb_shinfo(skb)->frags[i].page_offset,
  214. buf->segs[buf->nsegs].ds_len, PCI_DMA_BIDIRECTIONAL);
  215. buf->mapsize += buf->segs[buf->nsegs].ds_len;
  216. buf->nsegs++;
  217. }
  218. /* identify this buffer by the first segment */
  219. buf->map = (void *) buf->segs[0].ds_addr;
  220. return(0);
  221. }
  222. /*
  223. * map in a given contiguous buffer
  224. */
  225. static int
  226. pci_map_buf(struct hifn_softc *sc,struct hifn_operand *buf, void *b, int len)
  227. {
  228. DPRINTF("%s()\n", __FUNCTION__);
  229. buf->mapsize = 0;
  230. buf->segs[0].ds_addr = pci_map_single(sc->sc_pcidev,
  231. b, len, PCI_DMA_BIDIRECTIONAL);
  232. buf->segs[0].ds_len = len;
  233. buf->mapsize += buf->segs[0].ds_len;
  234. buf->nsegs = 1;
  235. /* identify this buffer by the first segment */
  236. buf->map = (void *) buf->segs[0].ds_addr;
  237. return(0);
  238. }
  239. #if 0 /* not needed at this time */
  240. static void
  241. pci_sync_iov(struct hifn_softc *sc, struct hifn_operand *buf)
  242. {
  243. int i;
  244. DPRINTF("%s()\n", __FUNCTION__);
  245. for (i = 0; i < buf->nsegs; i++)
  246. pci_dma_sync_single_for_cpu(sc->sc_pcidev, buf->segs[i].ds_addr,
  247. buf->segs[i].ds_len, PCI_DMA_BIDIRECTIONAL);
  248. }
  249. #endif
  250. static void
  251. pci_unmap_buf(struct hifn_softc *sc, struct hifn_operand *buf)
  252. {
  253. int i;
  254. DPRINTF("%s()\n", __FUNCTION__);
  255. for (i = 0; i < buf->nsegs; i++) {
  256. pci_unmap_single(sc->sc_pcidev, buf->segs[i].ds_addr,
  257. buf->segs[i].ds_len, PCI_DMA_BIDIRECTIONAL);
  258. buf->segs[i].ds_addr = 0;
  259. buf->segs[i].ds_len = 0;
  260. }
  261. buf->nsegs = 0;
  262. buf->mapsize = 0;
  263. buf->map = 0;
  264. }
  265. static const char*
  266. hifn_partname(struct hifn_softc *sc)
  267. {
  268. /* XXX sprintf numbers when not decoded */
  269. switch (pci_get_vendor(sc->sc_pcidev)) {
  270. case PCI_VENDOR_HIFN:
  271. switch (pci_get_device(sc->sc_pcidev)) {
  272. case PCI_PRODUCT_HIFN_6500: return "Hifn 6500";
  273. case PCI_PRODUCT_HIFN_7751: return "Hifn 7751";
  274. case PCI_PRODUCT_HIFN_7811: return "Hifn 7811";
  275. case PCI_PRODUCT_HIFN_7951: return "Hifn 7951";
  276. case PCI_PRODUCT_HIFN_7955: return "Hifn 7955";
  277. case PCI_PRODUCT_HIFN_7956: return "Hifn 7956";
  278. }
  279. return "Hifn unknown-part";
  280. case PCI_VENDOR_INVERTEX:
  281. switch (pci_get_device(sc->sc_pcidev)) {
  282. case PCI_PRODUCT_INVERTEX_AEON: return "Invertex AEON";
  283. }
  284. return "Invertex unknown-part";
  285. case PCI_VENDOR_NETSEC:
  286. switch (pci_get_device(sc->sc_pcidev)) {
  287. case PCI_PRODUCT_NETSEC_7751: return "NetSec 7751";
  288. }
  289. return "NetSec unknown-part";
  290. }
  291. return "Unknown-vendor unknown-part";
  292. }
  293. static u_int
  294. checkmaxmin(struct pci_dev *dev, const char *what, u_int v, u_int min, u_int max)
  295. {
  296. struct hifn_softc *sc = pci_get_drvdata(dev);
  297. if (v > max) {
  298. device_printf(sc->sc_dev, "Warning, %s %u out of range, "
  299. "using max %u\n", what, v, max);
  300. v = max;
  301. } else if (v < min) {
  302. device_printf(sc->sc_dev, "Warning, %s %u out of range, "
  303. "using min %u\n", what, v, min);
  304. v = min;
  305. }
  306. return v;
  307. }
  308. /*
  309. * Select PLL configuration for 795x parts. This is complicated in
  310. * that we cannot determine the optimal parameters without user input.
  311. * The reference clock is derived from an external clock through a
  312. * multiplier. The external clock is either the host bus (i.e. PCI)
  313. * or an external clock generator. When using the PCI bus we assume
  314. * the clock is either 33 or 66 MHz; for an external source we cannot
  315. * tell the speed.
  316. *
  317. * PLL configuration is done with a string: "pci" for PCI bus, or "ext"
  318. * for an external source, followed by the frequency. We calculate
  319. * the appropriate multiplier and PLL register contents accordingly.
  320. * When no configuration is given we default to "pci66" since that
  321. * always will allow the card to work. If a card is using the PCI
  322. * bus clock and in a 33MHz slot then it will be operating at half
  323. * speed until the correct information is provided.
  324. *
  325. * We use a default setting of "ext66" because according to Mike Ham
  326. * of HiFn, almost every board in existence has an external crystal
  327. * populated at 66Mhz. Using PCI can be a problem on modern motherboards,
  328. * because PCI33 can have clocks from 0 to 33Mhz, and some have
  329. * non-PCI-compliant spread-spectrum clocks, which can confuse the pll.
  330. */
  331. static void
  332. hifn_getpllconfig(struct pci_dev *dev, u_int *pll)
  333. {
  334. const char *pllspec = hifn_pllconfig;
  335. u_int freq, mul, fl, fh;
  336. u_int32_t pllconfig;
  337. char *nxt;
  338. if (pllspec == NULL)
  339. pllspec = "ext66";
  340. fl = 33, fh = 66;
  341. pllconfig = 0;
  342. if (strncmp(pllspec, "ext", 3) == 0) {
  343. pllspec += 3;
  344. pllconfig |= HIFN_PLL_REF_SEL;
  345. switch (pci_get_device(dev)) {
  346. case PCI_PRODUCT_HIFN_7955:
  347. case PCI_PRODUCT_HIFN_7956:
  348. fl = 20, fh = 100;
  349. break;
  350. #ifdef notyet
  351. case PCI_PRODUCT_HIFN_7954:
  352. fl = 20, fh = 66;
  353. break;
  354. #endif
  355. }
  356. } else if (strncmp(pllspec, "pci", 3) == 0)
  357. pllspec += 3;
  358. freq = strtoul(pllspec, &nxt, 10);
  359. if (nxt == pllspec)
  360. freq = 66;
  361. else
  362. freq = checkmaxmin(dev, "frequency", freq, fl, fh);
  363. /*
  364. * Calculate multiplier. We target a Fck of 266 MHz,
  365. * allowing only even values, possibly rounded down.
  366. * Multipliers > 8 must set the charge pump current.
  367. */
  368. mul = checkmaxmin(dev, "PLL divisor", (266 / freq) &~ 1, 2, 12);
  369. pllconfig |= (mul / 2 - 1) << HIFN_PLL_ND_SHIFT;
  370. if (mul > 8)
  371. pllconfig |= HIFN_PLL_IS;
  372. *pll = pllconfig;
  373. }
  374. /*
  375. * Attach an interface that successfully probed.
  376. */
  377. static int
  378. hifn_probe(struct pci_dev *dev, const struct pci_device_id *ent)
  379. {
  380. struct hifn_softc *sc = NULL;
  381. char rbase;
  382. u_int16_t ena, rev;
  383. int rseg, rc;
  384. unsigned long mem_start, mem_len;
  385. static int num_chips = 0;
  386. DPRINTF("%s()\n", __FUNCTION__);
  387. if (pci_enable_device(dev) < 0)
  388. return(-ENODEV);
  389. if (pci_set_mwi(dev))
  390. return(-ENODEV);
  391. if (!dev->irq) {
  392. printk("hifn: found device with no IRQ assigned. check BIOS settings!");
  393. pci_disable_device(dev);
  394. return(-ENODEV);
  395. }
  396. sc = (struct hifn_softc *) kmalloc(sizeof(*sc), GFP_KERNEL);
  397. if (!sc)
  398. return(-ENOMEM);
  399. memset(sc, 0, sizeof(*sc));
  400. softc_device_init(sc, "hifn", num_chips, hifn_methods);
  401. sc->sc_pcidev = dev;
  402. sc->sc_irq = -1;
  403. sc->sc_cid = -1;
  404. sc->sc_num = num_chips++;
  405. if (sc->sc_num < HIFN_MAX_CHIPS)
  406. hifn_chip_idx[sc->sc_num] = sc;
  407. pci_set_drvdata(sc->sc_pcidev, sc);
  408. spin_lock_init(&sc->sc_mtx);
  409. /* XXX handle power management */
  410. /*
  411. * The 7951 and 795x have a random number generator and
  412. * public key support; note this.
  413. */
  414. if (pci_get_vendor(dev) == PCI_VENDOR_HIFN &&
  415. (pci_get_device(dev) == PCI_PRODUCT_HIFN_7951 ||
  416. pci_get_device(dev) == PCI_PRODUCT_HIFN_7955 ||
  417. pci_get_device(dev) == PCI_PRODUCT_HIFN_7956))
  418. sc->sc_flags = HIFN_HAS_RNG | HIFN_HAS_PUBLIC;
  419. /*
  420. * The 7811 has a random number generator and
  421. * we also note it's identity 'cuz of some quirks.
  422. */
  423. if (pci_get_vendor(dev) == PCI_VENDOR_HIFN &&
  424. pci_get_device(dev) == PCI_PRODUCT_HIFN_7811)
  425. sc->sc_flags |= HIFN_IS_7811 | HIFN_HAS_RNG;
  426. /*
  427. * The 795x parts support AES.
  428. */
  429. if (pci_get_vendor(dev) == PCI_VENDOR_HIFN &&
  430. (pci_get_device(dev) == PCI_PRODUCT_HIFN_7955 ||
  431. pci_get_device(dev) == PCI_PRODUCT_HIFN_7956)) {
  432. sc->sc_flags |= HIFN_IS_7956 | HIFN_HAS_AES;
  433. /*
  434. * Select PLL configuration. This depends on the
  435. * bus and board design and must be manually configured
  436. * if the default setting is unacceptable.
  437. */
  438. hifn_getpllconfig(dev, &sc->sc_pllconfig);
  439. }
  440. /*
  441. * Setup PCI resources. Note that we record the bus
  442. * tag and handle for each register mapping, this is
  443. * used by the READ_REG_0, WRITE_REG_0, READ_REG_1,
  444. * and WRITE_REG_1 macros throughout the driver.
  445. */
  446. mem_start = pci_resource_start(sc->sc_pcidev, 0);
  447. mem_len = pci_resource_len(sc->sc_pcidev, 0);
  448. sc->sc_bar0 = (ocf_iomem_t) ioremap(mem_start, mem_len);
  449. if (!sc->sc_bar0) {
  450. device_printf(sc->sc_dev, "cannot map bar%d register space\n", 0);
  451. goto fail;
  452. }
  453. sc->sc_bar0_lastreg = (bus_size_t) -1;
  454. mem_start = pci_resource_start(sc->sc_pcidev, 1);
  455. mem_len = pci_resource_len(sc->sc_pcidev, 1);
  456. sc->sc_bar1 = (ocf_iomem_t) ioremap(mem_start, mem_len);
  457. if (!sc->sc_bar1) {
  458. device_printf(sc->sc_dev, "cannot map bar%d register space\n", 1);
  459. goto fail;
  460. }
  461. sc->sc_bar1_lastreg = (bus_size_t) -1;
  462. /* fix up the bus size */
  463. if (pci_set_dma_mask(dev, DMA_32BIT_MASK)) {
  464. device_printf(sc->sc_dev, "No usable DMA configuration, aborting.\n");
  465. goto fail;
  466. }
  467. if (pci_set_consistent_dma_mask(dev, DMA_32BIT_MASK)) {
  468. device_printf(sc->sc_dev,
  469. "No usable consistent DMA configuration, aborting.\n");
  470. goto fail;
  471. }
  472. hifn_set_retry(sc);
  473. /*
  474. * Setup the area where the Hifn DMA's descriptors
  475. * and associated data structures.
  476. */
  477. sc->sc_dma = (struct hifn_dma *) pci_alloc_consistent(dev,
  478. sizeof(*sc->sc_dma),
  479. &sc->sc_dma_physaddr);
  480. if (!sc->sc_dma) {
  481. device_printf(sc->sc_dev, "cannot alloc sc_dma\n");
  482. goto fail;
  483. }
  484. bzero(sc->sc_dma, sizeof(*sc->sc_dma));
  485. /*
  486. * Reset the board and do the ``secret handshake''
  487. * to enable the crypto support. Then complete the
  488. * initialization procedure by setting up the interrupt
  489. * and hooking in to the system crypto support so we'll
  490. * get used for system services like the crypto device,
  491. * IPsec, RNG device, etc.
  492. */
  493. hifn_reset_board(sc, 0);
  494. if (hifn_enable_crypto(sc) != 0) {
  495. device_printf(sc->sc_dev, "crypto enabling failed\n");
  496. goto fail;
  497. }
  498. hifn_reset_puc(sc);
  499. hifn_init_dma(sc);
  500. hifn_init_pci_registers(sc);
  501. pci_set_master(sc->sc_pcidev);
  502. /* XXX can't dynamically determine ram type for 795x; force dram */
  503. if (sc->sc_flags & HIFN_IS_7956)
  504. sc->sc_drammodel = 1;
  505. else if (hifn_ramtype(sc))
  506. goto fail;
  507. if (sc->sc_drammodel == 0)
  508. hifn_sramsize(sc);
  509. else
  510. hifn_dramsize(sc);
  511. /*
  512. * Workaround for NetSec 7751 rev A: half ram size because two
  513. * of the address lines were left floating
  514. */
  515. if (pci_get_vendor(dev) == PCI_VENDOR_NETSEC &&
  516. pci_get_device(dev) == PCI_PRODUCT_NETSEC_7751 &&
  517. pci_get_revid(dev) == 0x61) /*XXX???*/
  518. sc->sc_ramsize >>= 1;
  519. /*
  520. * Arrange the interrupt line.
  521. */
  522. rc = request_irq(dev->irq, hifn_intr, IRQF_SHARED, "hifn", sc);
  523. if (rc) {
  524. device_printf(sc->sc_dev, "could not map interrupt: %d\n", rc);
  525. goto fail;
  526. }
  527. sc->sc_irq = dev->irq;
  528. hifn_sessions(sc);
  529. /*
  530. * NB: Keep only the low 16 bits; this masks the chip id
  531. * from the 7951.
  532. */
  533. rev = READ_REG_1(sc, HIFN_1_REVID) & 0xffff;
  534. rseg = sc->sc_ramsize / 1024;
  535. rbase = 'K';
  536. if (sc->sc_ramsize >= (1024 * 1024)) {
  537. rbase = 'M';
  538. rseg /= 1024;
  539. }
  540. device_printf(sc->sc_dev, "%s, rev %u, %d%cB %cram",
  541. hifn_partname(sc), rev,
  542. rseg, rbase, sc->sc_drammodel ? 'd' : 's');
  543. if (sc->sc_flags & HIFN_IS_7956)
  544. printf(", pll=0x%x<%s clk, %ux mult>",
  545. sc->sc_pllconfig,
  546. sc->sc_pllconfig & HIFN_PLL_REF_SEL ? "ext" : "pci",
  547. 2 + 2*((sc->sc_pllconfig & HIFN_PLL_ND) >> 11));
  548. printf("\n");
  549. sc->sc_cid = crypto_get_driverid(softc_get_device(sc),CRYPTOCAP_F_HARDWARE);
  550. if (sc->sc_cid < 0) {
  551. device_printf(sc->sc_dev, "could not get crypto driver id\n");
  552. goto fail;
  553. }
  554. WRITE_REG_0(sc, HIFN_0_PUCNFG,
  555. READ_REG_0(sc, HIFN_0_PUCNFG) | HIFN_PUCNFG_CHIPID);
  556. ena = READ_REG_0(sc, HIFN_0_PUSTAT) & HIFN_PUSTAT_CHIPENA;
  557. switch (ena) {
  558. case HIFN_PUSTAT_ENA_2:
  559. crypto_register(sc->sc_cid, CRYPTO_3DES_CBC, 0, 0);
  560. crypto_register(sc->sc_cid, CRYPTO_ARC4, 0, 0);
  561. if (sc->sc_flags & HIFN_HAS_AES)
  562. crypto_register(sc->sc_cid, CRYPTO_AES_CBC, 0, 0);
  563. /*FALLTHROUGH*/
  564. case HIFN_PUSTAT_ENA_1:
  565. crypto_register(sc->sc_cid, CRYPTO_MD5, 0, 0);
  566. crypto_register(sc->sc_cid, CRYPTO_SHA1, 0, 0);
  567. crypto_register(sc->sc_cid, CRYPTO_MD5_HMAC, 0, 0);
  568. crypto_register(sc->sc_cid, CRYPTO_SHA1_HMAC, 0, 0);
  569. crypto_register(sc->sc_cid, CRYPTO_DES_CBC, 0, 0);
  570. break;
  571. }
  572. if (sc->sc_flags & (HIFN_HAS_PUBLIC | HIFN_HAS_RNG))
  573. hifn_init_pubrng(sc);
  574. init_timer(&sc->sc_tickto);
  575. sc->sc_tickto.function = hifn_tick;
  576. sc->sc_tickto.data = (unsigned long) sc->sc_num;
  577. mod_timer(&sc->sc_tickto, jiffies + HZ);
  578. return (0);
  579. fail:
  580. if (sc->sc_cid >= 0)
  581. crypto_unregister_all(sc->sc_cid);
  582. if (sc->sc_irq != -1)
  583. free_irq(sc->sc_irq, sc);
  584. if (sc->sc_dma) {
  585. /* Turn off DMA polling */
  586. WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
  587. HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
  588. pci_free_consistent(sc->sc_pcidev,
  589. sizeof(*sc->sc_dma),
  590. sc->sc_dma, sc->sc_dma_physaddr);
  591. }
  592. kfree(sc);
  593. return (-ENXIO);
  594. }
  595. /*
  596. * Detach an interface that successfully probed.
  597. */
  598. static void
  599. hifn_remove(struct pci_dev *dev)
  600. {
  601. struct hifn_softc *sc = pci_get_drvdata(dev);
  602. unsigned long l_flags;
  603. DPRINTF("%s()\n", __FUNCTION__);
  604. KASSERT(sc != NULL, ("hifn_detach: null software carrier!"));
  605. /* disable interrupts */
  606. HIFN_LOCK(sc);
  607. WRITE_REG_1(sc, HIFN_1_DMA_IER, 0);
  608. HIFN_UNLOCK(sc);
  609. /*XXX other resources */
  610. del_timer_sync(&sc->sc_tickto);
  611. /* Turn off DMA polling */
  612. WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
  613. HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
  614. crypto_unregister_all(sc->sc_cid);
  615. free_irq(sc->sc_irq, sc);
  616. pci_free_consistent(sc->sc_pcidev, sizeof(*sc->sc_dma),
  617. sc->sc_dma, sc->sc_dma_physaddr);
  618. }
  619. static int
  620. hifn_init_pubrng(struct hifn_softc *sc)
  621. {
  622. int i;
  623. DPRINTF("%s()\n", __FUNCTION__);
  624. if ((sc->sc_flags & HIFN_IS_7811) == 0) {
  625. /* Reset 7951 public key/rng engine */
  626. WRITE_REG_1(sc, HIFN_1_PUB_RESET,
  627. READ_REG_1(sc, HIFN_1_PUB_RESET) | HIFN_PUBRST_RESET);
  628. for (i = 0; i < 100; i++) {
  629. DELAY(1000);
  630. if ((READ_REG_1(sc, HIFN_1_PUB_RESET) &
  631. HIFN_PUBRST_RESET) == 0)
  632. break;
  633. }
  634. if (i == 100) {
  635. device_printf(sc->sc_dev, "public key init failed\n");
  636. return (1);
  637. }
  638. }
  639. /* Enable the rng, if available */
  640. #ifdef CONFIG_OCF_RANDOMHARVEST
  641. if (sc->sc_flags & HIFN_HAS_RNG) {
  642. if (sc->sc_flags & HIFN_IS_7811) {
  643. u_int32_t r;
  644. r = READ_REG_1(sc, HIFN_1_7811_RNGENA);
  645. if (r & HIFN_7811_RNGENA_ENA) {
  646. r &= ~HIFN_7811_RNGENA_ENA;
  647. WRITE_REG_1(sc, HIFN_1_7811_RNGENA, r);
  648. }
  649. WRITE_REG_1(sc, HIFN_1_7811_RNGCFG,
  650. HIFN_7811_RNGCFG_DEFL);
  651. r |= HIFN_7811_RNGENA_ENA;
  652. WRITE_REG_1(sc, HIFN_1_7811_RNGENA, r);
  653. } else
  654. WRITE_REG_1(sc, HIFN_1_RNG_CONFIG,
  655. READ_REG_1(sc, HIFN_1_RNG_CONFIG) |
  656. HIFN_RNGCFG_ENA);
  657. sc->sc_rngfirst = 1;
  658. crypto_rregister(sc->sc_cid, hifn_read_random, sc);
  659. }
  660. #endif
  661. /* Enable public key engine, if available */
  662. if (sc->sc_flags & HIFN_HAS_PUBLIC) {
  663. WRITE_REG_1(sc, HIFN_1_PUB_IEN, HIFN_PUBIEN_DONE);
  664. sc->sc_dmaier |= HIFN_DMAIER_PUBDONE;
  665. WRITE_REG_1(sc, HIFN_1_DMA_IER, sc->sc_dmaier);
  666. #ifdef HIFN_VULCANDEV
  667. sc->sc_pkdev = make_dev(&vulcanpk_cdevsw, 0,
  668. UID_ROOT, GID_WHEEL, 0666,
  669. "vulcanpk");
  670. sc->sc_pkdev->si_drv1 = sc;
  671. #endif
  672. }
  673. return (0);
  674. }
  675. #ifdef CONFIG_OCF_RANDOMHARVEST
  676. static int
  677. hifn_read_random(void *arg, u_int32_t *buf, int len)
  678. {
  679. struct hifn_softc *sc = (struct hifn_softc *) arg;
  680. u_int32_t sts;
  681. int i, rc = 0;
  682. if (len <= 0)
  683. return rc;
  684. if (sc->sc_flags & HIFN_IS_7811) {
  685. /* ONLY VALID ON 7811!!!! */
  686. for (i = 0; i < 5; i++) {
  687. sts = READ_REG_1(sc, HIFN_1_7811_RNGSTS);
  688. if (sts & HIFN_7811_RNGSTS_UFL) {
  689. device_printf(sc->sc_dev,
  690. "RNG underflow: disabling\n");
  691. /* DAVIDM perhaps return -1 */
  692. break;
  693. }
  694. if ((sts & HIFN_7811_RNGSTS_RDY) == 0)
  695. break;
  696. /*
  697. * There are at least two words in the RNG FIFO
  698. * at this point.
  699. */
  700. if (rc < len)
  701. buf[rc++] = READ_REG_1(sc, HIFN_1_7811_RNGDAT);
  702. if (rc < len)
  703. buf[rc++] = READ_REG_1(sc, HIFN_1_7811_RNGDAT);
  704. }
  705. } else
  706. buf[rc++] = READ_REG_1(sc, HIFN_1_RNG_DATA);
  707. /* NB: discard first data read */
  708. if (sc->sc_rngfirst) {
  709. sc->sc_rngfirst = 0;
  710. rc = 0;
  711. }
  712. return(rc);
  713. }
  714. #endif /* CONFIG_OCF_RANDOMHARVEST */
  715. static void
  716. hifn_puc_wait(struct hifn_softc *sc)
  717. {
  718. int i;
  719. int reg = HIFN_0_PUCTRL;
  720. if (sc->sc_flags & HIFN_IS_7956) {
  721. reg = HIFN_0_PUCTRL2;
  722. }
  723. for (i = 5000; i > 0; i--) {
  724. DELAY(1);
  725. if (!(READ_REG_0(sc, reg) & HIFN_PUCTRL_RESET))
  726. break;
  727. }
  728. if (!i)
  729. device_printf(sc->sc_dev, "proc unit did not reset(0x%x)\n",
  730. READ_REG_0(sc, HIFN_0_PUCTRL));
  731. }
  732. /*
  733. * Reset the processing unit.
  734. */
  735. static void
  736. hifn_reset_puc(struct hifn_softc *sc)
  737. {
  738. /* Reset processing unit */
  739. int reg = HIFN_0_PUCTRL;
  740. if (sc->sc_flags & HIFN_IS_7956) {
  741. reg = HIFN_0_PUCTRL2;
  742. }
  743. WRITE_REG_0(sc, reg, HIFN_PUCTRL_DMAENA);
  744. hifn_puc_wait(sc);
  745. }
  746. /*
  747. * Set the Retry and TRDY registers; note that we set them to
  748. * zero because the 7811 locks up when forced to retry (section
  749. * 3.6 of "Specification Update SU-0014-04". Not clear if we
  750. * should do this for all Hifn parts, but it doesn't seem to hurt.
  751. */
  752. static void
  753. hifn_set_retry(struct hifn_softc *sc)
  754. {
  755. DPRINTF("%s()\n", __FUNCTION__);
  756. /* NB: RETRY only responds to 8-bit reads/writes */
  757. pci_write_config_byte(sc->sc_pcidev, HIFN_RETRY_TIMEOUT, 0);
  758. pci_write_config_byte(sc->sc_pcidev, HIFN_TRDY_TIMEOUT, 0);
  759. /* piggy back the cache line setting here */
  760. pci_write_config_byte(sc->sc_pcidev, PCI_CACHE_LINE_SIZE, hifn_cache_linesize);
  761. }
  762. /*
  763. * Resets the board. Values in the regesters are left as is
  764. * from the reset (i.e. initial values are assigned elsewhere).
  765. */
  766. static void
  767. hifn_reset_board(struct hifn_softc *sc, int full)
  768. {
  769. u_int32_t reg;
  770. DPRINTF("%s()\n", __FUNCTION__);
  771. /*
  772. * Set polling in the DMA configuration register to zero. 0x7 avoids
  773. * resetting the board and zeros out the other fields.
  774. */
  775. WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
  776. HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
  777. /*
  778. * Now that polling has been disabled, we have to wait 1 ms
  779. * before resetting the board.
  780. */
  781. DELAY(1000);
  782. /* Reset the DMA unit */
  783. if (full) {
  784. WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MODE);
  785. DELAY(1000);
  786. } else {
  787. WRITE_REG_1(sc, HIFN_1_DMA_CNFG,
  788. HIFN_DMACNFG_MODE | HIFN_DMACNFG_MSTRESET);
  789. hifn_reset_puc(sc);
  790. }
  791. KASSERT(sc->sc_dma != NULL, ("hifn_reset_board: null DMA tag!"));
  792. bzero(sc->sc_dma, sizeof(*sc->sc_dma));
  793. /* Bring dma unit out of reset */
  794. WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
  795. HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
  796. hifn_puc_wait(sc);
  797. hifn_set_retry(sc);
  798. if (sc->sc_flags & HIFN_IS_7811) {
  799. for (reg = 0; reg < 1000; reg++) {
  800. if (READ_REG_1(sc, HIFN_1_7811_MIPSRST) &
  801. HIFN_MIPSRST_CRAMINIT)
  802. break;
  803. DELAY(1000);
  804. }
  805. if (reg == 1000)
  806. device_printf(sc->sc_dev, ": cram init timeout\n");
  807. } else {
  808. /* set up DMA configuration register #2 */
  809. /* turn off all PK and BAR0 swaps */
  810. WRITE_REG_1(sc, HIFN_1_DMA_CNFG2,
  811. (3 << HIFN_DMACNFG2_INIT_WRITE_BURST_SHIFT)|
  812. (3 << HIFN_DMACNFG2_INIT_READ_BURST_SHIFT)|
  813. (2 << HIFN_DMACNFG2_TGT_WRITE_BURST_SHIFT)|
  814. (2 << HIFN_DMACNFG2_TGT_READ_BURST_SHIFT));
  815. }
  816. }
  817. static u_int32_t
  818. hifn_next_signature(u_int32_t a, u_int cnt)
  819. {
  820. int i;
  821. u_int32_t v;
  822. for (i = 0; i < cnt; i++) {
  823. /* get the parity */
  824. v = a & 0x80080125;
  825. v ^= v >> 16;
  826. v ^= v >> 8;
  827. v ^= v >> 4;
  828. v ^= v >> 2;
  829. v ^= v >> 1;
  830. a = (v & 1) ^ (a << 1);
  831. }
  832. return a;
  833. }
  834. /*
  835. * Checks to see if crypto is already enabled. If crypto isn't enable,
  836. * "hifn_enable_crypto" is called to enable it. The check is important,
  837. * as enabling crypto twice will lock the board.
  838. */
  839. static int
  840. hifn_enable_crypto(struct hifn_softc *sc)
  841. {
  842. u_int32_t dmacfg, ramcfg, encl, addr, i;
  843. char offtbl[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  844. 0x00, 0x00, 0x00, 0x00 };
  845. DPRINTF("%s()\n", __FUNCTION__);
  846. ramcfg = READ_REG_0(sc, HIFN_0_PUCNFG);
  847. dmacfg = READ_REG_1(sc, HIFN_1_DMA_CNFG);
  848. /*
  849. * The RAM config register's encrypt level bit needs to be set before
  850. * every read performed on the encryption level register.
  851. */
  852. WRITE_REG_0(sc, HIFN_0_PUCNFG, ramcfg | HIFN_PUCNFG_CHIPID);
  853. encl = READ_REG_0(sc, HIFN_0_PUSTAT) & HIFN_PUSTAT_CHIPENA;
  854. /*
  855. * Make sure we don't re-unlock. Two unlocks kills chip until the
  856. * next reboot.
  857. */
  858. if (encl == HIFN_PUSTAT_ENA_1 || encl == HIFN_PUSTAT_ENA_2) {
  859. #ifdef HIFN_DEBUG
  860. if (hifn_debug)
  861. device_printf(sc->sc_dev,
  862. "Strong crypto already enabled!\n");
  863. #endif
  864. goto report;
  865. }
  866. if (encl != 0 && encl != HIFN_PUSTAT_ENA_0) {
  867. #ifdef HIFN_DEBUG
  868. if (hifn_debug)
  869. device_printf(sc->sc_dev,
  870. "Unknown encryption level 0x%x\n", encl);
  871. #endif
  872. return 1;
  873. }
  874. WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_UNLOCK |
  875. HIFN_DMACNFG_MSTRESET | HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE);
  876. DELAY(1000);
  877. addr = READ_REG_1(sc, HIFN_UNLOCK_SECRET1);
  878. DELAY(1000);
  879. WRITE_REG_1(sc, HIFN_UNLOCK_SECRET2, 0);
  880. DELAY(1000);
  881. for (i = 0; i <= 12; i++) {
  882. addr = hifn_next_signature(addr, offtbl[i] + 0x101);
  883. WRITE_REG_1(sc, HIFN_UNLOCK_SECRET2, addr);
  884. DELAY(1000);
  885. }
  886. WRITE_REG_0(sc, HIFN_0_PUCNFG, ramcfg | HIFN_PUCNFG_CHIPID);
  887. encl = READ_REG_0(sc, HIFN_0_PUSTAT) & HIFN_PUSTAT_CHIPENA;
  888. #ifdef HIFN_DEBUG
  889. if (hifn_debug) {
  890. if (encl != HIFN_PUSTAT_ENA_1 && encl != HIFN_PUSTAT_ENA_2)
  891. device_printf(sc->sc_dev, "Engine is permanently "
  892. "locked until next system reset!\n");
  893. else
  894. device_printf(sc->sc_dev, "Engine enabled "
  895. "successfully!\n");
  896. }
  897. #endif
  898. report:
  899. WRITE_REG_0(sc, HIFN_0_PUCNFG, ramcfg);
  900. WRITE_REG_1(sc, HIFN_1_DMA_CNFG, dmacfg);
  901. switch (encl) {
  902. case HIFN_PUSTAT_ENA_1:
  903. case HIFN_PUSTAT_ENA_2:
  904. break;
  905. case HIFN_PUSTAT_ENA_0:
  906. default:
  907. device_printf(sc->sc_dev, "disabled\n");
  908. break;
  909. }
  910. return 0;
  911. }
  912. /*
  913. * Give initial values to the registers listed in the "Register Space"
  914. * section of the HIFN Software Development reference manual.
  915. */
  916. static void
  917. hifn_init_pci_registers(struct hifn_softc *sc)
  918. {
  919. DPRINTF("%s()\n", __FUNCTION__);
  920. /* write fixed values needed by the Initialization registers */
  921. WRITE_REG_0(sc, HIFN_0_PUCTRL, HIFN_PUCTRL_DMAENA);
  922. WRITE_REG_0(sc, HIFN_0_FIFOCNFG, HIFN_FIFOCNFG_THRESHOLD);
  923. WRITE_REG_0(sc, HIFN_0_PUIER, HIFN_PUIER_DSTOVER);
  924. /* write all 4 ring address registers */
  925. WRITE_REG_1(sc, HIFN_1_DMA_CRAR, sc->sc_dma_physaddr +
  926. offsetof(struct hifn_dma, cmdr[0]));
  927. WRITE_REG_1(sc, HIFN_1_DMA_SRAR, sc->sc_dma_physaddr +
  928. offsetof(struct hifn_dma, srcr[0]));
  929. WRITE_REG_1(sc, HIFN_1_DMA_DRAR, sc->sc_dma_physaddr +
  930. offsetof(struct hifn_dma, dstr[0]));
  931. WRITE_REG_1(sc, HIFN_1_DMA_RRAR, sc->sc_dma_physaddr +
  932. offsetof(struct hifn_dma, resr[0]));
  933. DELAY(2000);
  934. /* write status register */
  935. WRITE_REG_1(sc, HIFN_1_DMA_CSR,
  936. HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS |
  937. HIFN_DMACSR_S_CTRL_DIS | HIFN_DMACSR_C_CTRL_DIS |
  938. HIFN_DMACSR_D_ABORT | HIFN_DMACSR_D_DONE | HIFN_DMACSR_D_LAST |
  939. HIFN_DMACSR_D_WAIT | HIFN_DMACSR_D_OVER |
  940. HIFN_DMACSR_R_ABORT | HIFN_DMACSR_R_DONE | HIFN_DMACSR_R_LAST |
  941. HIFN_DMACSR_R_WAIT | HIFN_DMACSR_R_OVER |
  942. HIFN_DMACSR_S_ABORT | HIFN_DMACSR_S_DONE | HIFN_DMACSR_S_LAST |
  943. HIFN_DMACSR_S_WAIT |
  944. HIFN_DMACSR_C_ABORT | HIFN_DMACSR_C_DONE | HIFN_DMACSR_C_LAST |
  945. HIFN_DMACSR_C_WAIT |
  946. HIFN_DMACSR_ENGINE |
  947. ((sc->sc_flags & HIFN_HAS_PUBLIC) ?
  948. HIFN_DMACSR_PUBDONE : 0) |
  949. ((sc->sc_flags & HIFN_IS_7811) ?
  950. HIFN_DMACSR_ILLW | HIFN_DMACSR_ILLR : 0));
  951. sc->sc_d_busy = sc->sc_r_busy = sc->sc_s_busy = sc->sc_c_busy = 0;
  952. sc->sc_dmaier |= HIFN_DMAIER_R_DONE | HIFN_DMAIER_C_ABORT |
  953. HIFN_DMAIER_D_OVER | HIFN_DMAIER_R_OVER |
  954. HIFN_DMAIER_S_ABORT | HIFN_DMAIER_D_ABORT | HIFN_DMAIER_R_ABORT |
  955. ((sc->sc_flags & HIFN_IS_7811) ?
  956. HIFN_DMAIER_ILLW | HIFN_DMAIER_ILLR : 0);
  957. sc->sc_dmaier &= ~HIFN_DMAIER_C_WAIT;
  958. WRITE_REG_1(sc, HIFN_1_DMA_IER, sc->sc_dmaier);
  959. if (sc->sc_flags & HIFN_IS_7956) {
  960. u_int32_t pll;
  961. WRITE_REG_0(sc, HIFN_0_PUCNFG, HIFN_PUCNFG_COMPSING |
  962. HIFN_PUCNFG_TCALLPHASES |
  963. HIFN_PUCNFG_TCDRVTOTEM | HIFN_PUCNFG_BUS32);
  964. /* turn off the clocks and insure bypass is set */
  965. pll = READ_REG_1(sc, HIFN_1_PLL);
  966. pll = (pll &~ (HIFN_PLL_PK_CLK_SEL | HIFN_PLL_PE_CLK_SEL))
  967. | HIFN_PLL_BP | HIFN_PLL_MBSET;
  968. WRITE_REG_1(sc, HIFN_1_PLL, pll);
  969. DELAY(10*1000); /* 10ms */
  970. /* change configuration */
  971. pll = (pll &~ HIFN_PLL_CONFIG) | sc->sc_pllconfig;
  972. WRITE_REG_1(sc, HIFN_1_PLL, pll);
  973. DELAY(10*1000); /* 10ms */
  974. /* disable bypass */
  975. pll &= ~HIFN_PLL_BP;
  976. WRITE_REG_1(sc, HIFN_1_PLL, pll);
  977. /* enable clocks with new configuration */
  978. pll |= HIFN_PLL_PK_CLK_SEL | HIFN_PLL_PE_CLK_SEL;
  979. WRITE_REG_1(sc, HIFN_1_PLL, pll);
  980. } else {
  981. WRITE_REG_0(sc, HIFN_0_PUCNFG, HIFN_PUCNFG_COMPSING |
  982. HIFN_PUCNFG_DRFR_128 | HIFN_PUCNFG_TCALLPHASES |
  983. HIFN_PUCNFG_TCDRVTOTEM | HIFN_PUCNFG_BUS32 |
  984. (sc->sc_drammodel ? HIFN_PUCNFG_DRAM : HIFN_PUCNFG_SRAM));
  985. }
  986. WRITE_REG_0(sc, HIFN_0_PUISR, HIFN_PUISR_DSTOVER);
  987. WRITE_REG_1(sc, HIFN_1_DMA_CNFG, HIFN_DMACNFG_MSTRESET |
  988. HIFN_DMACNFG_DMARESET | HIFN_DMACNFG_MODE | HIFN_DMACNFG_LAST |
  989. ((HIFN_POLL_FREQUENCY << 16 ) & HIFN_DMACNFG_POLLFREQ) |
  990. ((HIFN_POLL_SCALAR << 8) & HIFN_DMACNFG_POLLINVAL));
  991. }
  992. /*
  993. * The maximum number of sessions supported by the card
  994. * is dependent on the amount of context ram, which
  995. * encryption algorithms are enabled, and how compression
  996. * is configured. This should be configured before this
  997. * routine is called.
  998. */
  999. static void
  1000. hifn_sessions(struct hifn_softc *sc)
  1001. {
  1002. u_int32_t pucnfg;
  1003. int ctxsize;
  1004. DPRINTF("%s()\n", __FUNCTION__);
  1005. pucnfg = READ_REG_0(sc, HIFN_0_PUCNFG);
  1006. if (pucnfg & HIFN_PUCNFG_COMPSING) {
  1007. if (pucnfg & HIFN_PUCNFG_ENCCNFG)
  1008. ctxsize = 128;
  1009. else
  1010. ctxsize = 512;
  1011. /*
  1012. * 7955/7956 has internal context memory of 32K
  1013. */
  1014. if (sc->sc_flags & HIFN_IS_7956)
  1015. sc->sc_maxses = 32768 / ctxsize;
  1016. else
  1017. sc->sc_maxses = 1 +
  1018. ((sc->sc_ramsize - 32768) / ctxsize);
  1019. } else
  1020. sc->sc_maxses = sc->sc_ramsize / 16384;
  1021. if (sc->sc_maxses > 2048)
  1022. sc->sc_maxses = 2048;
  1023. }
  1024. /*
  1025. * Determine ram type (sram or dram). Board should be just out of a reset
  1026. * state when this is called.
  1027. */
  1028. static int
  1029. hifn_ramtype(struct hifn_softc *sc)
  1030. {
  1031. u_int8_t data[8], dataexpect[8];
  1032. int i;
  1033. for (i = 0; i < sizeof(data); i++)
  1034. data[i] = dataexpect[i] = 0x55;
  1035. if (hifn_writeramaddr(sc, 0, data))
  1036. return (-1);
  1037. if (hifn_readramaddr(sc, 0, data))
  1038. return (-1);
  1039. if (bcmp(data, dataexpect, sizeof(data)) != 0) {
  1040. sc->sc_drammodel = 1;
  1041. return (0);
  1042. }
  1043. for (i = 0; i < sizeof(data); i++)
  1044. data[i] = dataexpect[i] = 0xaa;
  1045. if (hifn_writeramaddr(sc, 0, data))
  1046. return (-1);
  1047. if (hifn_readramaddr(sc, 0, data))
  1048. return (-1);
  1049. if (bcmp(data, dataexpect, sizeof(data)) != 0) {
  1050. sc->sc_drammodel = 1;
  1051. return (0);
  1052. }
  1053. return (0);
  1054. }
  1055. #define HIFN_SRAM_MAX (32 << 20)
  1056. #define HIFN_SRAM_STEP_SIZE 16384
  1057. #define HIFN_SRAM_GRANULARITY (HIFN_SRAM_MAX / HIFN_SRAM_STEP_SIZE)
  1058. static int
  1059. hifn_sramsize(struct hifn_softc *sc)
  1060. {
  1061. u_int32_t a;
  1062. u_int8_t data[8];
  1063. u_int8_t dataexpect[sizeof(data)];
  1064. int32_t i;
  1065. for (i = 0; i < sizeof(data); i++)
  1066. data[i] = dataexpect[i] = i ^ 0x5a;
  1067. for (i = HIFN_SRAM_GRANULARITY - 1; i >= 0; i--) {
  1068. a = i * HIFN_SRAM_STEP_SIZE;
  1069. bcopy(&i, data, sizeof(i));
  1070. hifn_writeramaddr(sc, a, data);
  1071. }
  1072. for (i = 0; i < HIFN_SRAM_GRANULARITY; i++) {
  1073. a = i * HIFN_SRAM_STEP_SIZE;
  1074. bcopy(&i, dataexpect, sizeof(i));
  1075. if (hifn_readramaddr(sc, a, data) < 0)
  1076. return (0);
  1077. if (bcmp(data, dataexpect, sizeof(data)) != 0)
  1078. return (0);
  1079. sc->sc_ramsize = a + HIFN_SRAM_STEP_SIZE;
  1080. }
  1081. return (0);
  1082. }
  1083. /*
  1084. * XXX For dram boards, one should really try all of the
  1085. * HIFN_PUCNFG_DSZ_*'s. This just assumes that PUCNFG
  1086. * is already set up correctly.
  1087. */
  1088. static int
  1089. hifn_dramsize(struct hifn_softc *sc)
  1090. {
  1091. u_int32_t cnfg;
  1092. if (sc->sc_flags & HIFN_IS_7956) {
  1093. /*
  1094. * 7955/7956 have a fixed internal ram of only 32K.
  1095. */
  1096. sc->sc_ramsize = 32768;
  1097. } else {
  1098. cnfg = READ_REG_0(sc, HIFN_0_PUCNFG) &
  1099. HIFN_PUCNFG_DRAMMASK;
  1100. sc->sc_ramsize = 1 << ((cnfg >> 13) + 18);
  1101. }
  1102. return (0);
  1103. }
  1104. static void
  1105. hifn_alloc_slot(struct hifn_softc *sc, int *cmdp, int *srcp, int *dstp, int *resp)
  1106. {
  1107. struct hifn_dma *dma = sc->sc_dma;
  1108. DPRINTF("%s()\n", __FUNCTION__);
  1109. if (dma->cmdi == HIFN_D_CMD_RSIZE) {
  1110. dma->cmdi = 0;
  1111. dma->cmdr[HIFN_D_CMD_RSIZE].l = htole32(HIFN_D_JUMP|HIFN_D_MASKDONEIRQ);
  1112. wmb();
  1113. dma->cmdr[HIFN_D_CMD_RSIZE].l |= htole32(HIFN_D_VALID);
  1114. HIFN_CMDR_SYNC(sc, HIFN_D_CMD_RSIZE,
  1115. BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
  1116. }
  1117. *cmdp = dma->cmdi++;
  1118. dma->cmdk = dma->cmdi;
  1119. if (dma->srci == HIFN_D_SRC_RSIZE) {
  1120. dma->srci = 0;
  1121. dma->srcr[HIFN_D_SRC_RSIZE].l = htole32(HIFN_D_JUMP|HIFN_D_MASKDONEIRQ);
  1122. wmb();
  1123. dma->srcr[HIFN_D_SRC_RSIZE].l |= htole32(HIFN_D_VALID);
  1124. HIFN_SRCR_SYNC(sc, HIFN_D_SRC_RSIZE,
  1125. BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
  1126. }
  1127. *srcp = dma->srci++;
  1128. dma->srck = dma->srci;
  1129. if (dma->dsti == HIFN_D_DST_RSIZE) {
  1130. dma->dsti = 0;
  1131. dma->dstr[HIFN_D_DST_RSIZE].l = htole32(HIFN_D_JUMP|HIFN_D_MASKDONEIRQ);
  1132. wmb();
  1133. dma->dstr[HIFN_D_DST_RSIZE].l |= htole32(HIFN_D_VALID);
  1134. HIFN_DSTR_SYNC(sc, HIFN_D_DST_RSIZE,
  1135. BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
  1136. }
  1137. *dstp = dma->dsti++;
  1138. dma->dstk = dma->dsti;
  1139. if (dma->resi == HIFN_D_RES_RSIZE) {
  1140. dma->resi = 0;
  1141. dma->resr[HIFN_D_RES_RSIZE].l = htole32(HIFN_D_JUMP|HIFN_D_MASKDONEIRQ);
  1142. wmb();
  1143. dma->resr[HIFN_D_RES_RSIZE].l |= htole32(HIFN_D_VALID);
  1144. HIFN_RESR_SYNC(sc, HIFN_D_RES_RSIZE,
  1145. BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
  1146. }
  1147. *resp = dma->resi++;
  1148. dma->resk = dma->resi;
  1149. }
  1150. static int
  1151. hifn_writeramaddr(struct hifn_softc *sc, int addr, u_int8_t *data)
  1152. {
  1153. struct hifn_dma *dma = sc->sc_dma;
  1154. hifn_base_command_t wc;
  1155. const u_int32_t masks = HIFN_D_VALID | HIFN_D_LAST | HIFN_D_MASKDONEIRQ;
  1156. int r, cmdi, resi, srci, dsti;
  1157. DPRINTF("%s()\n", __FUNCTION__);
  1158. wc.masks = htole16(3 << 13);
  1159. wc.session_num = htole16(addr >> 14);
  1160. wc.total_source_count = htole16(8);
  1161. wc.total_dest_count = htole16(addr & 0x3fff);
  1162. hifn_alloc_slot(sc, &cmdi, &srci, &dsti, &resi);
  1163. WRITE_REG_1(sc, HIFN_1_DMA_CSR,
  1164. HIFN_DMACSR_C_CTRL_ENA | HIFN_DMACSR_S_CTRL_ENA |
  1165. HIFN_DMACSR_D_CTRL_ENA | HIFN_DMACSR_R_CTRL_ENA);
  1166. /* build write command */
  1167. bzero(dma->command_bufs[cmdi], HIFN_MAX_COMMAND);
  1168. *(hifn_base_command_t *)dma->command_bufs[cmdi] = wc;
  1169. bcopy(data, &dma->test_src, sizeof(dma->test_src));
  1170. dma->srcr[srci].p = htole32(sc->sc_dma_physaddr
  1171. + offsetof(struct hifn_dma, test_src));
  1172. dma->dstr[dsti].p = htole32(sc->sc_dma_physaddr
  1173. + offsetof(struct hifn_dma, test_dst));
  1174. dma->cmdr[cmdi].l = htole32(16 | masks);
  1175. dma->srcr[srci].l = htole32(8 | masks);
  1176. dma->dstr[dsti].l = htole32(4 | masks);
  1177. dma->resr[resi].l = htole32(4 | masks);
  1178. for (r = 10000; r >= 0; r--) {
  1179. DELAY(10);
  1180. if ((dma->resr[resi].l & htole32(HIFN_D_VALID)) == 0)
  1181. break;
  1182. }
  1183. if (r == 0) {
  1184. device_printf(sc->sc_dev, "writeramaddr -- "
  1185. "result[%d](addr %d) still valid\n", resi, addr);
  1186. r = -1;
  1187. return (-1);
  1188. } else
  1189. r = 0;
  1190. WRITE_REG_1(sc, HIFN_1_DMA_CSR,
  1191. HIFN_DMACSR_C_CTRL_DIS | HIFN_DMACSR_S_CTRL_DIS |
  1192. HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS);
  1193. return (r);
  1194. }
  1195. static int
  1196. hifn_readramaddr(struct hifn_softc *sc, int addr, u_int8_t *data)
  1197. {
  1198. struct hifn_dma *dma = sc->sc_dma;
  1199. hifn_base_command_t rc;
  1200. const u_int32_t masks = HIFN_D_VALID | HIFN_D_LAST | HIFN_D_MASKDONEIRQ;
  1201. int r, cmdi, srci, dsti, resi;
  1202. DPRINTF("%s()\n", __FUNCTION__);
  1203. rc.masks = htole16(2 << 13);
  1204. rc.session_num = htole16(addr >> 14);
  1205. rc.total_source_count = htole16(addr & 0x3fff);
  1206. rc.total_dest_count = htole16(8);
  1207. hifn_alloc_slot(sc, &cmdi, &srci, &dsti, &resi);
  1208. WRITE_REG_1(sc, HIFN_1_DMA_CSR,
  1209. HIFN_DMACSR_C_CTRL_ENA | HIFN_DMACSR_S_CTRL_ENA |
  1210. HIFN_DMACSR_D_CTRL_ENA | HIFN_DMACSR_R_CTRL_ENA);
  1211. bzero(dma->command_bufs[cmdi], HIFN_MAX_COMMAND);
  1212. *(hifn_base_command_t *)dma->command_bufs[cmdi] = rc;
  1213. dma->srcr[srci].p = htole32(sc->sc_dma_physaddr +
  1214. offsetof(struct hifn_dma, test_src));
  1215. dma->test_src = 0;
  1216. dma->dstr[dsti].p = htole32(sc->sc_dma_physaddr +
  1217. offsetof(struct hifn_dma, test_dst));
  1218. dma->test_dst = 0;
  1219. dma->cmdr[cmdi].l = htole32(8 | masks);
  1220. dma->srcr[srci].l = htole32(8 | masks);
  1221. dma->dstr[dsti].l = htole32(8 | masks);
  1222. dma->resr[resi].l = htole32(HIFN_MAX_RESULT | masks);
  1223. for (r = 10000; r >= 0; r--) {
  1224. DELAY(10);
  1225. if ((dma->resr[resi].l & htole32(HIFN_D_VALID)) == 0)
  1226. break;
  1227. }
  1228. if (r == 0) {
  1229. device_printf(sc->sc_dev, "readramaddr -- "
  1230. "result[%d](addr %d) still valid\n", resi, addr);
  1231. r = -1;
  1232. } else {
  1233. r = 0;
  1234. bcopy(&dma->test_dst, data, sizeof(dma->test_dst));
  1235. }
  1236. WRITE_REG_1(sc, HIFN_1_DMA_CSR,
  1237. HIFN_DMACSR_C_CTRL_DIS | HIFN_DMACSR_S_CTRL_DIS |
  1238. HIFN_DMACSR_D_CTRL_DIS | HIFN_DMACSR_R_CTRL_DIS);
  1239. return (r);
  1240. }
  1241. /*
  1242. * Initialize the descriptor rings.
  1243. */
  1244. static void
  1245. hifn_init_dma(struct hifn_softc *sc)
  1246. {
  1247. struct hifn_dma *dma = sc->sc_dma;
  1248. int i;
  1249. DPRINTF("%s()\n", __FUNCTION__);
  1250. hifn_set_retry(sc);
  1251. /* initialize static pointer values */
  1252. for (i = 0; i < HIFN_D_CMD_RSIZE; i++)
  1253. dma->cmdr[i].p = htole32(sc->sc_dma_physaddr +
  1254. offsetof(struct hifn_dma, command_bufs[i][0]));
  1255. for (i = 0; i < HIFN_D_RES_RSIZE; i++)
  1256. dma->resr[i].p = htole32(sc->sc_dma_physaddr +
  1257. offsetof(struct hifn_dma, result_bufs[i][0]));
  1258. dma->cmdr[HIFN_D_CMD_RSIZE].p =
  1259. htole32(sc->sc_dma_physaddr + offsetof(struct hifn_dma, cmdr[0]));
  1260. dma->srcr[HIFN_D_SRC_RSIZE].p =
  1261. htole32(sc->sc_dma_physaddr + offsetof(struct hifn_dma, srcr[0]));
  1262. dma->dstr[HIFN_D_DST_RSIZE].p =
  1263. htole32(sc->sc_dma_physaddr + offsetof(struct hifn_dma, dstr[0]));
  1264. dma->resr[HIFN_D_RES_RSIZE].p =
  1265. htole32(sc->sc_dma_physaddr + offsetof(struct hifn_dma, resr[0]));
  1266. dma->cmdu = dma->srcu = dma->dstu = dma->resu = 0;
  1267. dma->cmdi = dma->srci = dma->dsti = dma->resi = 0;
  1268. dma->cmdk = dma->srck = dma->dstk = dma->resk = 0;
  1269. }
  1270. /*
  1271. * Writes out the raw command buffer space. Returns the
  1272. * command buffer size.
  1273. */
  1274. static u_int
  1275. hifn_write_command(struct hifn_command *cmd, u_int8_t *buf)
  1276. {
  1277. struct hifn_softc *sc = NULL;
  1278. u_int8_t *buf_pos;
  1279. hifn_base_command_t *base_cmd;
  1280. hifn_mac_command_t *mac_cmd;
  1281. hifn_crypt_command_t *cry_cmd;
  1282. int using_mac, using_crypt, len, ivlen;
  1283. u_int32_t dlen, slen;
  1284. DPRINTF("%s()\n", __FUNCTION__);
  1285. buf_pos = buf;
  1286. using_mac = cmd->base_masks & HIFN_BASE_CMD_MAC;
  1287. using_crypt = cmd->base_masks & HIFN_BASE_CMD_CRYPT;
  1288. base_cmd = (hifn_base_command_t *)buf_pos;
  1289. base_cmd->masks = htole16(cmd->base_masks);
  1290. slen = cmd->src_mapsize;
  1291. if (cmd->sloplen)
  1292. dlen = cmd->dst_mapsize - cmd->sloplen + sizeof(u_int32_t);
  1293. else
  1294. dlen = cmd->dst_mapsize;
  1295. base_cmd->total_source_count = htole16(slen & HIFN_BASE_CMD_LENMASK_LO);
  1296. base_cmd->total_dest_count = htole16(dlen & HIFN_BASE_CMD_LENMASK_LO);
  1297. dlen >>= 16;
  1298. slen >>= 16;
  1299. base_cmd->session_num = htole16(
  1300. ((slen << HIFN_BASE_CMD_SRCLEN_S) & HIFN_BASE_CMD_SRCLEN_M) |
  1301. ((dlen << HIFN_BASE_CMD_DSTLEN_S) & HIFN_BASE_CMD_DSTLEN_M));
  1302. buf_pos += sizeof(hifn_base_command_t);
  1303. if (using_mac) {
  1304. mac_cmd = (hifn_mac_command_t *)buf_pos;
  1305. dlen = cmd->maccrd->crd_len;
  1306. mac_cmd->source_count = htole16(dlen & 0xffff);
  1307. dlen >>= 16;
  1308. mac_cmd->masks = htole16(cmd->mac_masks |
  1309. ((dlen << HIFN_MAC_CMD_SRCLEN_S) & HIFN_MAC_CMD_SRCLEN_M));
  1310. mac_cmd->header_skip = htole16(cmd->maccrd->crd_skip);
  1311. mac_cmd->reserved = 0;
  1312. buf_pos += sizeof(hifn_mac_command_t);
  1313. }
  1314. if (using_crypt) {
  1315. cry_cmd = (hifn_crypt_command_t *)buf_pos;
  1316. dlen = cmd->enccrd->crd_len;
  1317. cry_cmd->source_count = htole16(dlen & 0xffff);
  1318. dlen >>= 16;
  1319. cry_cmd->masks = htole16(cmd->cry_masks |
  1320. ((dlen << HIFN_CRYPT_CMD_SRCLEN_S) & HIFN_CRYPT_CMD_SRCLEN_M));
  1321. cry_cmd->header_skip = htole16(cmd->enccrd->crd_skip);
  1322. cry_cmd->reserved = 0;
  1323. buf_pos += sizeof(hifn_crypt_command_t);
  1324. }
  1325. if (using_mac && cmd->mac_masks & HIFN_MAC_CMD_NEW_KEY) {
  1326. bcopy(cmd->mac, buf_pos, HIFN_MAC_KEY_LENGTH);
  1327. buf_pos += HIFN_MAC_KEY_LENGTH;
  1328. }
  1329. if (using_crypt && cmd->cry_masks & HIFN_CRYPT_CMD_NEW_KEY) {
  1330. switch (cmd->cry_masks & HIFN_CRYPT_CMD_ALG_MASK) {
  1331. case HIFN_CRYPT_CMD_ALG_3DES:
  1332. bcopy(cmd->ck, buf_pos, HIFN_3DES_KEY_LENGTH);
  1333. buf_pos += HIFN_3DES_KEY_LENGTH;
  1334. break;
  1335. case HIFN_CRYPT_CMD_ALG_DES:
  1336. bcopy(cmd->ck, buf_pos, HIFN_DES_KEY_LENGTH);
  1337. buf_pos += HIFN_DES_KEY_LENGTH;
  1338. break;
  1339. case HIFN_CRYPT_CMD_ALG_RC4:
  1340. len = 256;
  1341. do {
  1342. int clen;
  1343. clen = MIN(cmd->cklen, len);
  1344. bcopy(cmd->ck, buf_pos, clen);
  1345. len -= clen;
  1346. buf_pos += clen;
  1347. } while (len > 0);
  1348. bzero(buf_pos, 4);
  1349. buf_pos += 4;
  1350. break;
  1351. case HIFN_CRYPT_CMD_ALG_AES:
  1352. /*
  1353. * AES keys are variable 128, 192 and
  1354. * 256 bits (16, 24 and 32 bytes).
  1355. */
  1356. bcopy(cmd->ck, buf_pos, cmd->cklen);
  1357. buf_pos += cmd->cklen;
  1358. break;
  1359. }
  1360. }
  1361. if (using_crypt && cmd->cry_masks & HIFN_CRYPT_CMD_NEW_IV) {
  1362. switch (cmd->cry_masks & HIFN_CRYPT_CMD_ALG_MASK) {
  1363. case HIFN_CRYPT_CMD_ALG_AES:
  1364. ivlen = HIFN_AES_IV_LENGTH;
  1365. break;
  1366. default:
  1367. ivlen = HIFN_IV_LENGTH;
  1368. break;
  1369. }
  1370. bcopy(cmd->iv, buf_pos, ivlen);
  1371. buf_pos += ivlen;
  1372. }
  1373. if ((cmd->base_masks & (HIFN_BASE_CMD_MAC|HIFN_BASE_CMD_CRYPT)) == 0) {
  1374. bzero(buf_pos, 8);
  1375. buf_pos += 8;
  1376. }
  1377. return (buf_pos - buf);
  1378. }
  1379. static int
  1380. hifn_dmamap_aligned(struct hifn_operand *op)
  1381. {
  1382. struct hifn_softc *sc = NULL;
  1383. int i;
  1384. DPRINTF("%s()\n", __FUNCTION__);
  1385. for (i = 0; i < op->nsegs; i++) {
  1386. if (op->segs[i].ds_addr & 3)
  1387. return (0);
  1388. if ((i != (op->nsegs - 1)) && (op->segs[i].ds_len & 3))
  1389. return (0);
  1390. }
  1391. return (1);
  1392. }
  1393. static __inline int
  1394. hifn_dmamap_dstwrap(struct hifn_softc *sc, int idx)
  1395. {
  1396. struct hifn_dma *dma = sc->sc_dma;
  1397. if (++idx == HIFN_D_DST_RSIZE) {
  1398. dma->dstr[idx].l = htole32(HIFN_D_VALID | HIFN_D_JUMP |
  1399. HIFN_D_MASKDONEIRQ);
  1400. HIFN_DSTR_SYNC(sc, idx,
  1401. BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  1402. idx = 0;
  1403. }
  1404. return (idx);
  1405. }
  1406. static int
  1407. hifn_dmamap_load_dst(struct hifn_softc *sc, struct hifn_command *cmd)
  1408. {
  1409. struct hifn_dma *dma = sc->sc_dma;
  1410. struct hifn_operand *dst = &cmd->dst;
  1411. u_int32_t p, l;
  1412. int idx, used = 0, i;
  1413. DPRINTF("%s()\n", __FUNCTION__);
  1414. idx = dma->dsti;
  1415. for (i = 0; i < dst->nsegs - 1; i++) {
  1416. dma->dstr[idx].p = htole32(dst->segs[i].ds_addr);
  1417. dma->dstr[idx].l = htole32(HIFN_D_MASKDONEIRQ | dst->segs[i].ds_len);
  1418. wmb();
  1419. dma->dstr[idx].l |= htole32(HIFN_D_VALID);
  1420. HIFN_DSTR_SYNC(sc, idx,
  1421. BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  1422. used++;
  1423. idx = hifn_dmamap_dstwrap(sc, idx);
  1424. }
  1425. if (cmd->sloplen == 0) {
  1426. p = dst->segs[i].ds_addr;
  1427. l = HIFN_D_MASKDONEIRQ | HIFN_D_LAST |
  1428. dst->segs[i].ds_len;
  1429. } else {
  1430. p = sc->sc_dma_physaddr +
  1431. offsetof(struct hifn_dma, slop[cmd->slopidx]);
  1432. l = HIFN_D_MASKDONEIRQ | HIFN_D_LAST |
  1433. sizeof(u_int32_t);
  1434. if ((dst->segs[i].ds_len - cmd->sloplen) != 0) {
  1435. dma->dstr[idx].p = htole32(dst->segs[i].ds_addr);
  1436. dma->dstr[idx].l = htole32(HIFN_D_MASKDONEIRQ |
  1437. (dst->segs[i].ds_len - cmd->sloplen));
  1438. wmb();
  1439. dma->dstr[idx].l |= htole32(HIFN_D_VALID);
  1440. HIFN_DSTR_SYNC(sc, idx,
  1441. BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  1442. used++;
  1443. idx = hifn_dmamap_dstwrap(sc, idx);
  1444. }
  1445. }
  1446. dma->dstr[idx].p = htole32(p);
  1447. dma->dstr[idx].l = htole32(l);
  1448. wmb();
  1449. dma->dstr[idx].l |= htole32(HIFN_D_VALID);
  1450. HIFN_DSTR_SYNC(sc, idx, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  1451. used++;
  1452. idx = hifn_dmamap_dstwrap(sc, idx);
  1453. dma->dsti = idx;
  1454. dma->dstu += used;
  1455. return (idx);
  1456. }
  1457. static __inline int
  1458. hifn_dmamap_srcwrap(struct hifn_softc *sc, int idx)
  1459. {
  1460. struct hifn_dma *dma = sc->sc_dma;
  1461. if (++idx == HIFN_D_SRC_RSIZE) {
  1462. dma->srcr[idx].l = htole32(HIFN_D_VALID |
  1463. HIFN_D_JUMP | HIFN_D_MASKDONEIRQ);
  1464. HIFN_SRCR_SYNC(sc, HIFN_D_SRC_RSIZE,
  1465. BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
  1466. idx = 0;
  1467. }
  1468. return (idx);
  1469. }
  1470. static int
  1471. hifn_dmamap_load_src(struct hifn_softc *sc, struct hifn_command *cmd)
  1472. {
  1473. struct hifn_dma *dma = sc->sc_dma;
  1474. struct hifn_operand *src = &cmd->src;
  1475. int idx, i;
  1476. u_int32_t last = 0;
  1477. DPRINTF("%s()\n", __FUNCTION__);
  1478. idx = dma->srci;
  1479. for (i = 0; i < src->nsegs; i++) {
  1480. if (i == src->nsegs - 1)
  1481. last = HIFN_D_LAST;
  1482. dma->srcr[idx].p = htole32(src->segs[i].ds_addr);
  1483. dma->srcr[idx].l = htole32(src->segs[i].ds_len |
  1484. HIFN_D_MASKDONEIRQ | last);
  1485. wmb();
  1486. dma->srcr[idx].l |= htole32(HIFN_D_VALID);
  1487. HIFN_SRCR_SYNC(sc, idx,
  1488. BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
  1489. idx = hifn_dmamap_srcwrap(sc, idx);
  1490. }
  1491. dma->srci = idx;
  1492. dma->srcu += src->nsegs;
  1493. return (idx);
  1494. }
  1495. static int
  1496. hifn_crypto(
  1497. struct hifn_softc *sc,
  1498. struct hifn_command *cmd,
  1499. struct cryptop *crp,
  1500. int hint)
  1501. {
  1502. struct hifn_dma *dma = sc->sc_dma;
  1503. u_int32_t cmdlen, csr;
  1504. int cmdi, resi, err = 0;
  1505. unsigned long l_flags;
  1506. DPRINTF("%s()\n", __FUNCTION__);
  1507. /*
  1508. * need 1 cmd, and 1 res
  1509. *
  1510. * NB: check this first since it's easy.
  1511. */
  1512. HIFN_LOCK(sc);
  1513. if ((dma->cmdu + 1) > HIFN_D_CMD_RSIZE ||
  1514. (dma->resu + 1) > HIFN_D_RES_RSIZE) {
  1515. #ifdef HIFN_DEBUG
  1516. if (hifn_debug) {
  1517. device_printf(sc->sc_dev,
  1518. "cmd/result exhaustion, cmdu %u resu %u\n",
  1519. dma->cmdu, dma->resu);
  1520. }
  1521. #endif
  1522. hifnstats.hst_nomem_cr++;
  1523. sc->sc_needwakeup |= CRYPTO_SYMQ;
  1524. HIFN_UNLOCK(sc);
  1525. return (ERESTART);
  1526. }
  1527. if (crp->crp_flags & CRYPTO_F_SKBUF) {
  1528. if (pci_map_skb(sc, &cmd->src, cmd->src_skb)) {
  1529. hifnstats.hst_nomem_load++;
  1530. err = ENOMEM;
  1531. goto err_srcmap1;
  1532. }
  1533. } else if (crp->crp_flags & CRYPTO_F_IOV) {
  1534. if (pci_map_uio(sc, &cmd->src, cmd->src_io)) {
  1535. hifnstats.hst_nomem_load++;
  1536. err = ENOMEM;
  1537. goto err_srcmap1;
  1538. }
  1539. } else {
  1540. if (pci_map_buf(sc, &cmd->src, cmd->src_buf, crp->crp_ilen)) {
  1541. hifnstats.hst_nomem_load++;
  1542. err = ENOMEM;
  1543. goto err_srcmap1;
  1544. }
  1545. }
  1546. if (hifn_dmamap_aligned(&cmd->src)) {
  1547. cmd->sloplen = cmd->src_mapsize & 3;
  1548. cmd->dst = cmd->src;
  1549. } else {
  1550. if (crp->crp_flags & CRYPTO_F_IOV) {
  1551. DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  1552. err = EINVAL;
  1553. goto err_srcmap;
  1554. } else if (crp->crp_flags & CRYPTO_F_SKBUF) {
  1555. #ifdef NOTYET
  1556. int totlen, len;
  1557. struct mbuf *m, *m0, *mlast;
  1558. KASSERT(cmd->dst_m == cmd->src_m,
  1559. ("hifn_crypto: dst_m initialized improperly"));
  1560. hifnstats.hst_unaligned++;
  1561. /*
  1562. * Source is not aligned on a longword boundary.
  1563. * Copy the data to insure alignment. If we fail
  1564. * to allocate mbufs or clusters while doing this
  1565. * we return ERESTART so the operation is requeued
  1566. * at the crypto later, but only if there are
  1567. * ops already posted to the hardware; otherwise we
  1568. * have no guarantee that we'll be re-entered.
  1569. */
  1570. totlen = cmd->src_mapsize;
  1571. if (cmd->src_m->m_flags & M_PKTHDR) {
  1572. len = MHLEN;
  1573. MGETHDR(m0, M_DONTWAIT, MT_DATA);
  1574. if (m0 && !m_dup_pkthdr(m0, cmd->src_m, M_DONTWAIT)) {
  1575. m_free(m0);
  1576. m0 = NULL;
  1577. }
  1578. } else {
  1579. len = MLEN;
  1580. MGET(m0, M_DONTWAIT, MT_DATA);
  1581. }
  1582. if (m0 == NULL) {
  1583. hifnstats.hst_nomem_mbuf++;
  1584. err = dma->cmdu ? ERESTART : ENOMEM;
  1585. goto err_srcmap;
  1586. }
  1587. if (totlen >= MINCLSIZE) {
  1588. MCLGET(m0, M_DONTWAIT);
  1589. if ((m0->m_flags & M_EXT) == 0) {
  1590. hifnstats.hst_nomem_mcl++;
  1591. err = dma->cmdu ? ERESTART : ENOMEM;
  1592. m_freem(m0);
  1593. goto err_srcmap;
  1594. }
  1595. len = MCLBYTES;
  1596. }
  1597. totlen -= len;
  1598. m0->m_pkthdr.len = m0->m_len = len;
  1599. mlast = m0;
  1600. while (totlen > 0) {
  1601. MGET(m, M_DONTWAIT, MT_DATA);
  1602. if (m == NULL) {
  1603. hifnstats.hst_nomem_mbuf++;
  1604. err = dma->cmdu ? ERESTART : ENOMEM;
  1605. m_freem(m0);
  1606. goto err_srcmap;
  1607. }
  1608. len = MLEN;
  1609. if (totlen >= MINCLSIZE) {
  1610. MCLGET(m, M_DONTWAIT);
  1611. if ((m->m_flags & M_EXT) == 0) {
  1612. hifnstats.hst_nomem_mcl++;
  1613. err = dma->cmdu ? ERESTART : ENOMEM;
  1614. mlast->m_next = m;
  1615. m_freem(m0);
  1616. goto err_srcmap;
  1617. }
  1618. len = MCLBYTES;
  1619. }
  1620. m->m_len = len;
  1621. m0->m_pkthdr.len += len;
  1622. totlen -= len;
  1623. mlast->m_next = m;
  1624. mlast = m;
  1625. }
  1626. cmd->dst_m = m0;
  1627. #else
  1628. device_printf(sc->sc_dev,
  1629. "%s,%d: CRYPTO_F_SKBUF unaligned not implemented\n",
  1630. __FILE__, __LINE__);
  1631. err = EINVAL;
  1632. goto err_srcmap;
  1633. #endif
  1634. } else {
  1635. device_printf(sc->sc_dev,
  1636. "%s,%d: unaligned contig buffers not implemented\n",
  1637. __FILE__, __LINE__);
  1638. err = EINVAL;
  1639. goto err_srcmap;
  1640. }
  1641. }
  1642. if (cmd->dst_map == NULL) {
  1643. if (crp->crp_flags & CRYPTO_F_SKBUF) {
  1644. if (pci_map_skb(sc, &cmd->dst, cmd->dst_skb)) {
  1645. hifnstats.hst_nomem_map++;
  1646. err = ENOMEM;
  1647. goto err_dstmap1;
  1648. }
  1649. } else if (crp->crp_flags & CRYPTO_F_IOV) {
  1650. if (pci_map_uio(sc, &cmd->dst, cmd->dst_io)) {
  1651. hifnstats.hst_nomem_load++;
  1652. err = ENOMEM;
  1653. goto err_dstmap1;
  1654. }
  1655. } else {
  1656. if (pci_map_buf(sc, &cmd->dst, cmd->dst_buf, crp->crp_ilen)) {
  1657. hifnstats.hst_nomem_load++;
  1658. err = ENOMEM;
  1659. goto err_dstmap1;
  1660. }
  1661. }
  1662. }
  1663. #ifdef HIFN_DEBUG
  1664. if (hifn_debug) {
  1665. device_printf(sc->sc_dev,
  1666. "Entering cmd: stat %8x ien %8x u %d/%d/%d/%d n %d/%d\n",
  1667. READ_REG_1(sc, HIFN_1_DMA_CSR),
  1668. READ_REG_1(sc, HIFN_1_DMA_IER),
  1669. dma->cmdu, dma->srcu, dma->dstu, dma->resu,
  1670. cmd->src_nsegs, cmd->dst_nsegs);
  1671. }
  1672. #endif
  1673. #if 0
  1674. if (cmd->src_map == cmd->dst_map) {
  1675. bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
  1676. BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
  1677. } else {
  1678. bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
  1679. BUS_DMASYNC_PREWRITE);
  1680. bus_dmamap_sync(sc->sc_dmat, cmd->dst_map,
  1681. BUS_DMASYNC_PREREAD);
  1682. }
  1683. #endif
  1684. /*
  1685. * need N src, and N dst
  1686. */
  1687. if ((dma->srcu + cmd->src_nsegs) > HIFN_D_SRC_RSIZE ||
  1688. (dma->dstu + cmd->dst_nsegs + 1) > HIFN_D_DST_RSIZE) {
  1689. #ifdef HIFN_DEBUG
  1690. if (hifn_debug) {
  1691. device_printf(sc->sc_dev,
  1692. "src/dst exhaustion, srcu %u+%u dstu %u+%u\n",
  1693. dma->srcu, cmd->src_nsegs,
  1694. dma->dstu, cmd->dst_nsegs);
  1695. }
  1696. #endif
  1697. hifnstats.hst_nomem_sd++;
  1698. err = ERESTART;
  1699. goto err_dstmap;
  1700. }
  1701. if (dma->cmdi == HIFN_D_CMD_RSIZE) {
  1702. dma->cmdi = 0;
  1703. dma->cmdr[HIFN_D_CMD_RSIZE].l = htole32(HIFN_D_JUMP|HIFN_D_MASKDONEIRQ);
  1704. wmb();
  1705. dma->cmdr[HIFN_D_CMD_RSIZE].l |= htole32(HIFN_D_VALID);
  1706. HIFN_CMDR_SYNC(sc, HIFN_D_CMD_RSIZE,
  1707. BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
  1708. }
  1709. cmdi = dma->cmdi++;
  1710. cmdlen = hifn_write_command(cmd, dma->command_bufs[cmdi]);
  1711. HIFN_CMD_SYNC(sc, cmdi, BUS_DMASYNC_PREWRITE);
  1712. /* .p for command/result already set */
  1713. dma->cmdr[cmdi].l = htole32(cmdlen | HIFN_D_LAST |
  1714. HIFN_D_MASKDONEIRQ);
  1715. wmb();
  1716. dma->cmdr[cmdi].l |= htole32(HIFN_D_VALID);
  1717. HIFN_CMDR_SYNC(sc, cmdi,
  1718. BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
  1719. dma->cmdu++;
  1720. /*
  1721. * We don't worry about missing an interrupt (which a "command wait"
  1722. * interrupt salvages us from), unless there is more than one command
  1723. * in the queue.
  1724. */
  1725. if (dma->cmdu > 1) {
  1726. sc->sc_dmaier |= HIFN_DMAIER_C_WAIT;
  1727. WRITE_REG_1(sc, HIFN_1_DMA_IER, sc->sc_dmaier);
  1728. }
  1729. hifnstats.hst_ipackets++;
  1730. hifnstats.hst_ibytes += cmd->src_mapsize;
  1731. hifn_dmamap_load_src(sc, cmd);
  1732. /*
  1733. * Unlike other descriptors, we don't mask done interrupt from
  1734. * result descriptor.
  1735. */
  1736. #ifdef HIFN_DEBUG
  1737. if (hifn_debug)
  1738. device_printf(sc->sc_dev, "load res\n");
  1739. #endif
  1740. if (dma->resi == HIFN_D_RES_RSIZE) {
  1741. dma->resi = 0;
  1742. dma->resr[HIFN_D_RES_RSIZE].l = htole32(HIFN_D_JUMP|HIFN_D_MASKDONEIRQ);
  1743. wmb();
  1744. dma->resr[HIFN_D_RES_RSIZE].l |= htole32(HIFN_D_VALID);
  1745. HIFN_RESR_SYNC(sc, HIFN_D_RES_RSIZE,
  1746. BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  1747. }
  1748. resi = dma->resi++;
  1749. KASSERT(dma->hifn_commands[resi] == NULL,
  1750. ("hifn_crypto: command slot %u busy", resi));
  1751. dma->hifn_commands[resi] = cmd;
  1752. HIFN_RES_SYNC(sc, resi, BUS_DMASYNC_PREREAD);
  1753. if ((hint & CRYPTO_HINT_MORE) && sc->sc_curbatch < hifn_maxbatch) {
  1754. dma->resr[resi].l = htole32(HIFN_MAX_RESULT |
  1755. HIFN_D_LAST | HIFN_D_MASKDONEIRQ);
  1756. wmb();
  1757. dma->resr[resi].l |= htole32(HIFN_D_VALID);
  1758. sc->sc_curbatch++;
  1759. if (sc->sc_curbatch > hifnstats.hst_maxbatch)
  1760. hifnstats.hst_maxbatch = sc->sc_curbatch;
  1761. hifnstats.hst_totbatch++;
  1762. } else {
  1763. dma->resr[resi].l = htole32(HIFN_MAX_RESULT | HIFN_D_LAST);
  1764. wmb();
  1765. dma->resr[resi].l |= htole32(HIFN_D_VALID);
  1766. sc->sc_curbatch = 0;
  1767. }
  1768. HIFN_RESR_SYNC(sc, resi,
  1769. BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  1770. dma->resu++;
  1771. if (cmd->sloplen)
  1772. cmd->slopidx = resi;
  1773. hifn_dmamap_load_dst(sc, cmd);
  1774. csr = 0;
  1775. if (sc->sc_c_busy == 0) {
  1776. csr |= HIFN_DMACSR_C_CTRL_ENA;
  1777. sc->sc_c_busy = 1;
  1778. }
  1779. if (sc->sc_s_busy == 0) {
  1780. csr |= HIFN_DMACSR_S_CTRL_ENA;
  1781. sc->sc_s_busy = 1;
  1782. }
  1783. if (sc->sc_r_busy == 0) {
  1784. csr |= HIFN_DMACSR_R_CTRL_ENA;
  1785. sc->sc_r_busy = 1;
  1786. }
  1787. if (sc->sc_d_busy == 0) {
  1788. csr |= HIFN_DMACSR_D_CTRL_ENA;
  1789. sc->sc_d_busy = 1;
  1790. }
  1791. if (csr)
  1792. WRITE_REG_1(sc, HIFN_1_DMA_CSR, csr);
  1793. #ifdef HIFN_DEBUG
  1794. if (hifn_debug) {
  1795. device_printf(sc->sc_dev, "command: stat %8x ier %8x\n",
  1796. READ_REG_1(sc, HIFN_1_DMA_CSR),
  1797. READ_REG_1(sc, HIFN_1_DMA_IER));
  1798. }
  1799. #endif
  1800. sc->sc_active = 5;
  1801. HIFN_UNLOCK(sc);
  1802. KASSERT(err == 0, ("hifn_crypto: success with error %u", err));
  1803. return (err); /* success */
  1804. err_dstmap:
  1805. if (cmd->src_map != cmd->dst_map)
  1806. pci_unmap_buf(sc, &cmd->dst);
  1807. err_dstmap1:
  1808. err_srcmap:
  1809. if (crp->crp_flags & CRYPTO_F_SKBUF) {
  1810. if (cmd->src_skb != cmd->dst_skb)
  1811. #ifdef NOTYET
  1812. m_freem(cmd->dst_m);
  1813. #else
  1814. device_printf(sc->sc_dev,
  1815. "%s,%d: CRYPTO_F_SKBUF src != dst not implemented\n",
  1816. __FILE__, __LINE__);
  1817. #endif
  1818. }
  1819. pci_unmap_buf(sc, &cmd->src);
  1820. err_srcmap1:
  1821. HIFN_UNLOCK(sc);
  1822. return (err);
  1823. }
  1824. static void
  1825. hifn_tick(unsigned long arg)
  1826. {
  1827. struct hifn_softc *sc;
  1828. unsigned long l_flags;
  1829. if (arg >= HIFN_MAX_CHIPS)
  1830. return;
  1831. sc = hifn_chip_idx[arg];
  1832. if (!sc)
  1833. return;
  1834. HIFN_LOCK(sc);
  1835. if (sc->sc_active == 0) {
  1836. struct hifn_dma *dma = sc->sc_dma;
  1837. u_int32_t r = 0;
  1838. if (dma->cmdu == 0 && sc->sc_c_busy) {
  1839. sc->sc_c_busy = 0;
  1840. r |= HIFN_DMACSR_C_CTRL_DIS;
  1841. }
  1842. if (dma->srcu == 0 && sc->sc_s_busy) {
  1843. sc->sc_s_busy = 0;
  1844. r |= HIFN_DMACSR_S_CTRL_DIS;
  1845. }
  1846. if (dma->dstu == 0 && sc->sc_d_busy) {
  1847. sc->sc_d_busy = 0;
  1848. r |= HIFN_DMACSR_D_CTRL_DIS;
  1849. }
  1850. if (dma->resu == 0 && sc->sc_r_busy) {
  1851. sc->sc_r_busy = 0;
  1852. r |= HIFN_DMACSR_R_CTRL_DIS;
  1853. }
  1854. if (r)
  1855. WRITE_REG_1(sc, HIFN_1_DMA_CSR, r);
  1856. } else
  1857. sc->sc_active--;
  1858. HIFN_UNLOCK(sc);
  1859. mod_timer(&sc->sc_tickto, jiffies + HZ);
  1860. }
  1861. static irqreturn_t
  1862. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
  1863. hifn_intr(int irq, void *arg)
  1864. #else
  1865. hifn_intr(int irq, void *arg, struct pt_regs *regs)
  1866. #endif
  1867. {
  1868. struct hifn_softc *sc = arg;
  1869. struct hifn_dma *dma;
  1870. u_int32_t dmacsr, restart;
  1871. int i, u;
  1872. unsigned long l_flags;
  1873. dmacsr = READ_REG_1(sc, HIFN_1_DMA_CSR);
  1874. /* Nothing in the DMA unit interrupted */
  1875. if ((dmacsr & sc->sc_dmaier) == 0)
  1876. return IRQ_NONE;
  1877. HIFN_LOCK(sc);
  1878. dma = sc->sc_dma;
  1879. #ifdef HIFN_DEBUG
  1880. if (hifn_debug) {
  1881. device_printf(sc->sc_dev,
  1882. "irq: stat %08x ien %08x damier %08x i %d/%d/%d/%d k %d/%d/%d/%d u %d/%d/%d/%d\n",
  1883. dmacsr, READ_REG_1(sc, HIFN_1_DMA_IER), sc->sc_dmaier,
  1884. dma->cmdi, dma->srci, dma->dsti, dma->resi,
  1885. dma->cmdk, dma->srck, dma->dstk, dma->resk,
  1886. dma->cmdu, dma->srcu, dma->dstu, dma->resu);
  1887. }
  1888. #endif
  1889. WRITE_REG_1(sc, HIFN_1_DMA_CSR, dmacsr & sc->sc_dmaier);
  1890. if ((sc->sc_flags & HIFN_HAS_PUBLIC) &&
  1891. (dmacsr & HIFN_DMACSR_PUBDONE))
  1892. WRITE_REG_1(sc, HIFN_1_PUB_STATUS,
  1893. READ_REG_1(sc, HIFN_1_PUB_STATUS) | HIFN_PUBSTS_DONE);
  1894. restart = dmacsr & (HIFN_DMACSR_D_OVER | HIFN_DMACSR_R_OVER);
  1895. if (restart)
  1896. device_printf(sc->sc_dev, "overrun %x\n", dmacsr);
  1897. if (sc->sc_flags & HIFN_IS_7811) {
  1898. if (dmacsr & HIFN_DMACSR_ILLR)
  1899. device_printf(sc->sc_dev, "illegal read\n");
  1900. if (dmacsr & HIFN_DMACSR_ILLW)
  1901. device_printf(sc->sc_dev, "illegal write\n");
  1902. }
  1903. restart = dmacsr & (HIFN_DMACSR_C_ABORT | HIFN_DMACSR_S_ABORT |
  1904. HIFN_DMACSR_D_ABORT | HIFN_DMACSR_R_ABORT);
  1905. if (restart) {
  1906. device_printf(sc->sc_dev, "abort, resetting.\n");
  1907. hifnstats.hst_abort++;
  1908. hifn_abort(sc);
  1909. HIFN_UNLOCK(sc);
  1910. return IRQ_HANDLED;
  1911. }
  1912. if ((dmacsr & HIFN_DMACSR_C_WAIT) && (dma->cmdu == 0)) {
  1913. /*
  1914. * If no slots to process and we receive a "waiting on
  1915. * command" interrupt, we disable the "waiting on command"
  1916. * (by clearing it).
  1917. */
  1918. sc->sc_dmaier &= ~HIFN_DMAIER_C_WAIT;
  1919. WRITE_REG_1(sc, HIFN_1_DMA_IER, sc->sc_dmaier);
  1920. }
  1921. /* clear the rings */
  1922. i = dma->resk; u = dma->resu;
  1923. while (u != 0) {
  1924. HIFN_RESR_SYNC(sc, i,
  1925. BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  1926. if (dma->resr[i].l & htole32(HIFN_D_VALID)) {
  1927. HIFN_RESR_SYNC(sc, i,
  1928. BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  1929. break;
  1930. }
  1931. if (i != HIFN_D_RES_RSIZE) {
  1932. struct hifn_command *cmd;
  1933. u_int8_t *macbuf = NULL;
  1934. HIFN_RES_SYNC(sc, i, BUS_DMASYNC_POSTREAD);
  1935. cmd = dma->hifn_commands[i];
  1936. KASSERT(cmd != NULL,
  1937. ("hifn_intr: null command slot %u", i));
  1938. dma->hifn_commands[i] = NULL;
  1939. if (cmd->base_masks & HIFN_BASE_CMD_MAC) {
  1940. macbuf = dma->result_bufs[i];
  1941. macbuf += 12;
  1942. }
  1943. hifn_callback(sc, cmd, macbuf);
  1944. hifnstats.hst_opackets++;
  1945. u--;
  1946. }
  1947. if (++i == (HIFN_D_RES_RSIZE + 1))
  1948. i = 0;
  1949. }
  1950. dma->resk = i; dma->resu = u;
  1951. i = dma->srck; u = dma->srcu;
  1952. while (u != 0) {
  1953. if (i == HIFN_D_SRC_RSIZE)
  1954. i = 0;
  1955. HIFN_SRCR_SYNC(sc, i,
  1956. BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  1957. if (dma->srcr[i].l & htole32(HIFN_D_VALID)) {
  1958. HIFN_SRCR_SYNC(sc, i,
  1959. BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  1960. break;
  1961. }
  1962. i++, u--;
  1963. }
  1964. dma->srck = i; dma->srcu = u;
  1965. i = dma->cmdk; u = dma->cmdu;
  1966. while (u != 0) {
  1967. HIFN_CMDR_SYNC(sc, i,
  1968. BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  1969. if (dma->cmdr[i].l & htole32(HIFN_D_VALID)) {
  1970. HIFN_CMDR_SYNC(sc, i,
  1971. BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  1972. break;
  1973. }
  1974. if (i != HIFN_D_CMD_RSIZE) {
  1975. u--;
  1976. HIFN_CMD_SYNC(sc, i, BUS_DMASYNC_POSTWRITE);
  1977. }
  1978. if (++i == (HIFN_D_CMD_RSIZE + 1))
  1979. i = 0;
  1980. }
  1981. dma->cmdk = i; dma->cmdu = u;
  1982. HIFN_UNLOCK(sc);
  1983. if (sc->sc_needwakeup) { /* XXX check high watermark */
  1984. int wakeup = sc->sc_needwakeup & (CRYPTO_SYMQ|CRYPTO_ASYMQ);
  1985. #ifdef HIFN_DEBUG
  1986. if (hifn_debug)
  1987. device_printf(sc->sc_dev,
  1988. "wakeup crypto (%x) u %d/%d/%d/%d\n",
  1989. sc->sc_needwakeup,
  1990. dma->cmdu, dma->srcu, dma->dstu, dma->resu);
  1991. #endif
  1992. sc->sc_needwakeup &= ~wakeup;
  1993. crypto_unblock(sc->sc_cid, wakeup);
  1994. }
  1995. return IRQ_HANDLED;
  1996. }
  1997. /*
  1998. * Allocate a new 'session' and return an encoded session id. 'sidp'
  1999. * contains our registration id, and should contain an encoded session
  2000. * id on successful allocation.
  2001. */
  2002. static int
  2003. hifn_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri)
  2004. {
  2005. struct hifn_softc *sc = device_get_softc(dev);
  2006. struct cryptoini *c;
  2007. int mac = 0, cry = 0, sesn;
  2008. struct hifn_session *ses = NULL;
  2009. unsigned long l_flags;
  2010. DPRINTF("%s()\n", __FUNCTION__);
  2011. KASSERT(sc != NULL, ("hifn_newsession: null softc"));
  2012. if (sidp == NULL || cri == NULL || sc == NULL) {
  2013. DPRINTF("%s,%d: %s - EINVAL\n", __FILE__, __LINE__, __FUNCTION__);
  2014. return (EINVAL);
  2015. }
  2016. HIFN_LOCK(sc);
  2017. if (sc->sc_sessions == NULL) {
  2018. ses = sc->sc_sessions = (struct hifn_session *)kmalloc(sizeof(*ses),
  2019. SLAB_ATOMIC);
  2020. if (ses == NULL) {
  2021. HIFN_UNLOCK(sc);
  2022. return (ENOMEM);
  2023. }
  2024. sesn = 0;
  2025. sc->sc_nsessions = 1;
  2026. } else {
  2027. for (sesn = 0; sesn < sc->sc_nsessions; sesn++) {
  2028. if (!sc->sc_sessions[sesn].hs_used) {
  2029. ses = &sc->sc_sessions[sesn];
  2030. break;
  2031. }
  2032. }
  2033. if (ses == NULL) {
  2034. sesn = sc->sc_nsessions;
  2035. ses = (struct hifn_session *)kmalloc((sesn + 1) * sizeof(*ses),
  2036. SLAB_ATOMIC);
  2037. if (ses == NULL) {
  2038. HIFN_UNLOCK(sc);
  2039. return (ENOMEM);
  2040. }
  2041. bcopy(sc->sc_sessions, ses, sesn * sizeof(*ses));
  2042. bzero(sc->sc_sessions, sesn * sizeof(*ses));
  2043. kfree(sc->sc_sessions);
  2044. sc->sc_sessions = ses;
  2045. ses = &sc->sc_sessions[sesn];
  2046. sc->sc_nsessions++;
  2047. }
  2048. }
  2049. HIFN_UNLOCK(sc);
  2050. bzero(ses, sizeof(*ses));
  2051. ses->hs_used = 1;
  2052. for (c = cri; c != NULL; c = c->cri_next) {
  2053. switch (c->cri_alg) {
  2054. case CRYPTO_MD5:
  2055. case CRYPTO_SHA1:
  2056. case CRYPTO_MD5_HMAC:
  2057. case CRYPTO_SHA1_HMAC:
  2058. if (mac) {
  2059. DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  2060. return (EINVAL);
  2061. }
  2062. mac = 1;
  2063. ses->hs_mlen = c->cri_mlen;
  2064. if (ses->hs_mlen == 0) {
  2065. switch (c->cri_alg) {
  2066. case CRYPTO_MD5:
  2067. case CRYPTO_MD5_HMAC:
  2068. ses->hs_mlen = 16;
  2069. break;
  2070. case CRYPTO_SHA1:
  2071. case CRYPTO_SHA1_HMAC:
  2072. ses->hs_mlen = 20;
  2073. break;
  2074. }
  2075. }
  2076. break;
  2077. case CRYPTO_DES_CBC:
  2078. case CRYPTO_3DES_CBC:
  2079. case CRYPTO_AES_CBC:
  2080. case CRYPTO_ARC4:
  2081. if (cry) {
  2082. DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  2083. return (EINVAL);
  2084. }
  2085. cry = 1;
  2086. break;
  2087. default:
  2088. DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  2089. return (EINVAL);
  2090. }
  2091. }
  2092. if (mac == 0 && cry == 0) {
  2093. DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  2094. return (EINVAL);
  2095. }
  2096. *sidp = HIFN_SID(device_get_unit(sc->sc_dev), sesn);
  2097. return (0);
  2098. }
  2099. /*
  2100. * Deallocate a session.
  2101. * XXX this routine should run a zero'd mac/encrypt key into context ram.
  2102. * XXX to blow away any keys already stored there.
  2103. */
  2104. static int
  2105. hifn_freesession(device_t dev, u_int64_t tid)
  2106. {
  2107. struct hifn_softc *sc = device_get_softc(dev);
  2108. int session, error;
  2109. u_int32_t sid = CRYPTO_SESID2LID(tid);
  2110. unsigned long l_flags;
  2111. DPRINTF("%s()\n", __FUNCTION__);
  2112. KASSERT(sc != NULL, ("hifn_freesession: null softc"));
  2113. if (sc == NULL) {
  2114. DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  2115. return (EINVAL);
  2116. }
  2117. HIFN_LOCK(sc);
  2118. session = HIFN_SESSION(sid);
  2119. if (session < sc->sc_nsessions) {
  2120. bzero(&sc->sc_sessions[session], sizeof(struct hifn_session));
  2121. error = 0;
  2122. } else {
  2123. DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  2124. error = EINVAL;
  2125. }
  2126. HIFN_UNLOCK(sc);
  2127. return (error);
  2128. }
  2129. static int
  2130. hifn_process(device_t dev, struct cryptop *crp, int hint)
  2131. {
  2132. struct hifn_softc *sc = device_get_softc(dev);
  2133. struct hifn_command *cmd = NULL;
  2134. int session, err, ivlen;
  2135. struct cryptodesc *crd1, *crd2, *maccrd, *enccrd;
  2136. DPRINTF("%s()\n", __FUNCTION__);
  2137. if (crp == NULL || crp->crp_callback == NULL) {
  2138. hifnstats.hst_invalid++;
  2139. DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  2140. return (EINVAL);
  2141. }
  2142. session = HIFN_SESSION(crp->crp_sid);
  2143. if (sc == NULL || session >= sc->sc_nsessions) {
  2144. DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  2145. err = EINVAL;
  2146. goto errout;
  2147. }
  2148. cmd = kmalloc(sizeof(struct hifn_command), SLAB_ATOMIC);
  2149. if (cmd == NULL) {
  2150. hifnstats.hst_nomem++;
  2151. err = ENOMEM;
  2152. goto errout;
  2153. }
  2154. memset(cmd, 0, sizeof(*cmd));
  2155. if (crp->crp_flags & CRYPTO_F_SKBUF) {
  2156. cmd->src_skb = (struct sk_buff *)crp->crp_buf;
  2157. cmd->dst_skb = (struct sk_buff *)crp->crp_buf;
  2158. } else if (crp->crp_flags & CRYPTO_F_IOV) {
  2159. cmd->src_io = (struct uio *)crp->crp_buf;
  2160. cmd->dst_io = (struct uio *)crp->crp_buf;
  2161. } else {
  2162. cmd->src_buf = crp->crp_buf;
  2163. cmd->dst_buf = crp->crp_buf;
  2164. }
  2165. crd1 = crp->crp_desc;
  2166. if (crd1 == NULL) {
  2167. DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  2168. err = EINVAL;
  2169. goto errout;
  2170. }
  2171. crd2 = crd1->crd_next;
  2172. if (crd2 == NULL) {
  2173. if (crd1->crd_alg == CRYPTO_MD5_HMAC ||
  2174. crd1->crd_alg == CRYPTO_SHA1_HMAC ||
  2175. crd1->crd_alg == CRYPTO_SHA1 ||
  2176. crd1->crd_alg == CRYPTO_MD5) {
  2177. maccrd = crd1;
  2178. enccrd = NULL;
  2179. } else if (crd1->crd_alg == CRYPTO_DES_CBC ||
  2180. crd1->crd_alg == CRYPTO_3DES_CBC ||
  2181. crd1->crd_alg == CRYPTO_AES_CBC ||
  2182. crd1->crd_alg == CRYPTO_ARC4) {
  2183. if ((crd1->crd_flags & CRD_F_ENCRYPT) == 0)
  2184. cmd->base_masks |= HIFN_BASE_CMD_DECODE;
  2185. maccrd = NULL;
  2186. enccrd = crd1;
  2187. } else {
  2188. DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  2189. err = EINVAL;
  2190. goto errout;
  2191. }
  2192. } else {
  2193. if ((crd1->crd_alg == CRYPTO_MD5_HMAC ||
  2194. crd1->crd_alg == CRYPTO_SHA1_HMAC ||
  2195. crd1->crd_alg == CRYPTO_MD5 ||
  2196. crd1->crd_alg == CRYPTO_SHA1) &&
  2197. (crd2->crd_alg == CRYPTO_DES_CBC ||
  2198. crd2->crd_alg == CRYPTO_3DES_CBC ||
  2199. crd2->crd_alg == CRYPTO_AES_CBC ||
  2200. crd2->crd_alg == CRYPTO_ARC4) &&
  2201. ((crd2->crd_flags & CRD_F_ENCRYPT) == 0)) {
  2202. cmd->base_masks = HIFN_BASE_CMD_DECODE;
  2203. maccrd = crd1;
  2204. enccrd = crd2;
  2205. } else if ((crd1->crd_alg == CRYPTO_DES_CBC ||
  2206. crd1->crd_alg == CRYPTO_ARC4 ||
  2207. crd1->crd_alg == CRYPTO_3DES_CBC ||
  2208. crd1->crd_alg == CRYPTO_AES_CBC) &&
  2209. (crd2->crd_alg == CRYPTO_MD5_HMAC ||
  2210. crd2->crd_alg == CRYPTO_SHA1_HMAC ||
  2211. crd2->crd_alg == CRYPTO_MD5 ||
  2212. crd2->crd_alg == CRYPTO_SHA1) &&
  2213. (crd1->crd_flags & CRD_F_ENCRYPT)) {
  2214. enccrd = crd1;
  2215. maccrd = crd2;
  2216. } else {
  2217. /*
  2218. * We cannot order the 7751 as requested
  2219. */
  2220. DPRINTF("%s,%d: %s %d,%d,%d - EINVAL\n",__FILE__,__LINE__,__FUNCTION__, crd1->crd_alg, crd2->crd_alg, crd1->crd_flags & CRD_F_ENCRYPT);
  2221. err = EINVAL;
  2222. goto errout;
  2223. }
  2224. }
  2225. if (enccrd) {
  2226. cmd->enccrd = enccrd;
  2227. cmd->base_masks |= HIFN_BASE_CMD_CRYPT;
  2228. switch (enccrd->crd_alg) {
  2229. case CRYPTO_ARC4:
  2230. cmd->cry_masks |= HIFN_CRYPT_CMD_ALG_RC4;
  2231. break;
  2232. case CRYPTO_DES_CBC:
  2233. cmd->cry_masks |= HIFN_CRYPT_CMD_ALG_DES |
  2234. HIFN_CRYPT_CMD_MODE_CBC |
  2235. HIFN_CRYPT_CMD_NEW_IV;
  2236. break;
  2237. case CRYPTO_3DES_CBC:
  2238. cmd->cry_masks |= HIFN_CRYPT_CMD_ALG_3DES |
  2239. HIFN_CRYPT_CMD_MODE_CBC |
  2240. HIFN_CRYPT_CMD_NEW_IV;
  2241. break;
  2242. case CRYPTO_AES_CBC:
  2243. cmd->cry_masks |= HIFN_CRYPT_CMD_ALG_AES |
  2244. HIFN_CRYPT_CMD_MODE_CBC |
  2245. HIFN_CRYPT_CMD_NEW_IV;
  2246. break;
  2247. default:
  2248. DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  2249. err = EINVAL;
  2250. goto errout;
  2251. }
  2252. if (enccrd->crd_alg != CRYPTO_ARC4) {
  2253. ivlen = ((enccrd->crd_alg == CRYPTO_AES_CBC) ?
  2254. HIFN_AES_IV_LENGTH : HIFN_IV_LENGTH);
  2255. if (enccrd->crd_flags & CRD_F_ENCRYPT) {
  2256. if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
  2257. bcopy(enccrd->crd_iv, cmd->iv, ivlen);
  2258. else
  2259. read_random(cmd->iv, ivlen);
  2260. if ((enccrd->crd_flags & CRD_F_IV_PRESENT)
  2261. == 0) {
  2262. crypto_copyback(crp->crp_flags,
  2263. crp->crp_buf, enccrd->crd_inject,
  2264. ivlen, cmd->iv);
  2265. }
  2266. } else {
  2267. if (enccrd->crd_flags & CRD_F_IV_EXPLICIT)
  2268. bcopy(enccrd->crd_iv, cmd->iv, ivlen);
  2269. else {
  2270. crypto_copydata(crp->crp_flags,
  2271. crp->crp_buf, enccrd->crd_inject,
  2272. ivlen, cmd->iv);
  2273. }
  2274. }
  2275. }
  2276. if (enccrd->crd_flags & CRD_F_KEY_EXPLICIT)
  2277. cmd->cry_masks |= HIFN_CRYPT_CMD_NEW_KEY;
  2278. cmd->ck = enccrd->crd_key;
  2279. cmd->cklen = enccrd->crd_klen >> 3;
  2280. cmd->cry_masks |= HIFN_CRYPT_CMD_NEW_KEY;
  2281. /*
  2282. * Need to specify the size for the AES key in the masks.
  2283. */
  2284. if ((cmd->cry_masks & HIFN_CRYPT_CMD_ALG_MASK) ==
  2285. HIFN_CRYPT_CMD_ALG_AES) {
  2286. switch (cmd->cklen) {
  2287. case 16:
  2288. cmd->cry_masks |= HIFN_CRYPT_CMD_KSZ_128;
  2289. break;
  2290. case 24:
  2291. cmd->cry_masks |= HIFN_CRYPT_CMD_KSZ_192;
  2292. break;
  2293. case 32:
  2294. cmd->cry_masks |= HIFN_CRYPT_CMD_KSZ_256;
  2295. break;
  2296. default:
  2297. DPRINTF("%s,%d: %s - EINVAL\n",__FILE__,__LINE__,__FUNCTION__);
  2298. err = EINVAL;
  2299. goto errout;
  2300. }
  2301. }
  2302. }
  2303. if (maccrd) {
  2304. cmd->maccrd = maccrd;
  2305. cmd->base_masks |= HIFN_BASE_CMD_MAC;
  2306. switch (maccrd->crd_alg) {
  2307. case CRYPTO_MD5:
  2308. cmd->mac_masks |= HIFN_MAC_CMD_ALG_MD5 |
  2309. HIFN_MAC_CMD_RESULT | HIFN_MAC_CMD_MODE_HASH |
  2310. HIFN_MAC_CMD_POS_IPSEC;
  2311. break;
  2312. case CRYPTO_MD5_HMAC:
  2313. cmd->mac_masks |= HIFN_MAC_CMD_ALG_MD5 |
  2314. HIFN_MAC_CMD_RESULT | HIFN_MAC_CMD_MODE_HMAC |
  2315. HIFN_MAC_CMD_POS_IPSEC | HIFN_MAC_CMD_TRUNC;
  2316. break;
  2317. case CRYPTO_SHA1:
  2318. cmd->mac_masks |= HIFN_MAC_CMD_ALG_SHA1 |
  2319. HIFN_MAC_CMD_RESULT | HIFN_MAC_CMD_MODE_HASH |
  2320. HIFN_MAC_CMD_POS_IPSEC;
  2321. break;
  2322. case CRYPTO_SHA1_HMAC:
  2323. cmd->mac_masks |= HIFN_MAC_CMD_ALG_SHA1 |
  2324. HIFN_MAC_CMD_RESULT | HIFN_MAC_CMD_MODE_HMAC |
  2325. HIFN_MAC_CMD_POS_IPSEC | HIFN_MAC_CMD_TRUNC;
  2326. break;
  2327. }
  2328. if (maccrd->crd_alg == CRYPTO_SHA1_HMAC ||
  2329. maccrd->crd_alg == CRYPTO_MD5_HMAC) {
  2330. cmd->mac_masks |= HIFN_MAC_CMD_NEW_KEY;
  2331. bcopy(maccrd->crd_key, cmd->mac, maccrd->crd_klen >> 3);
  2332. bzero(cmd->mac + (maccrd->crd_klen >> 3),
  2333. HIFN_MAC_KEY_LENGTH - (maccrd->crd_klen >> 3));
  2334. }
  2335. }
  2336. cmd->crp = crp;
  2337. cmd->session_num = session;
  2338. cmd->softc = sc;
  2339. err = hifn_crypto(sc, cmd, crp, hint);
  2340. if (!err) {
  2341. return 0;
  2342. } else if (err == ERESTART) {
  2343. /*
  2344. * There weren't enough resources to dispatch the request
  2345. * to the part. Notify the caller so they'll requeue this
  2346. * request and resubmit it again soon.
  2347. */
  2348. #ifdef HIFN_DEBUG
  2349. if (hifn_debug)
  2350. device_printf(sc->sc_dev, "requeue request\n");
  2351. #endif
  2352. kfree(cmd);
  2353. sc->sc_needwakeup |= CRYPTO_SYMQ;
  2354. return (err);
  2355. }
  2356. errout:
  2357. if (cmd != NULL)
  2358. kfree(cmd);
  2359. if (err == EINVAL)
  2360. hifnstats.hst_invalid++;
  2361. else
  2362. hifnstats.hst_nomem++;
  2363. crp->crp_etype = err;
  2364. crypto_done(crp);
  2365. return (err);
  2366. }
  2367. static void
  2368. hifn_abort(struct hifn_softc *sc)
  2369. {
  2370. struct hifn_dma *dma = sc->sc_dma;
  2371. struct hifn_command *cmd;
  2372. struct cryptop *crp;
  2373. int i, u;
  2374. DPRINTF("%s()\n", __FUNCTION__);
  2375. i = dma->resk; u = dma->resu;
  2376. while (u != 0) {
  2377. cmd = dma->hifn_commands[i];
  2378. KASSERT(cmd != NULL, ("hifn_abort: null command slot %u", i));
  2379. dma->hifn_commands[i] = NULL;
  2380. crp = cmd->crp;
  2381. if ((dma->resr[i].l & htole32(HIFN_D_VALID)) == 0) {
  2382. /* Salvage what we can. */
  2383. u_int8_t *macbuf;
  2384. if (cmd->base_masks & HIFN_BASE_CMD_MAC) {
  2385. macbuf = dma->result_bufs[i];
  2386. macbuf += 12;
  2387. } else
  2388. macbuf = NULL;
  2389. hifnstats.hst_opackets++;
  2390. hifn_callback(sc, cmd, macbuf);
  2391. } else {
  2392. #if 0
  2393. if (cmd->src_map == cmd->dst_map) {
  2394. bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
  2395. BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
  2396. } else {
  2397. bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
  2398. BUS_DMASYNC_POSTWRITE);
  2399. bus_dmamap_sync(sc->sc_dmat, cmd->dst_map,
  2400. BUS_DMASYNC_POSTREAD);
  2401. }
  2402. #endif
  2403. if (cmd->src_skb != cmd->dst_skb) {
  2404. #ifdef NOTYET
  2405. m_freem(cmd->src_m);
  2406. crp->crp_buf = (caddr_t)cmd->dst_m;
  2407. #else
  2408. device_printf(sc->sc_dev,
  2409. "%s,%d: CRYPTO_F_SKBUF src != dst not implemented\n",
  2410. __FILE__, __LINE__);
  2411. #endif
  2412. }
  2413. /* non-shared buffers cannot be restarted */
  2414. if (cmd->src_map != cmd->dst_map) {
  2415. /*
  2416. * XXX should be EAGAIN, delayed until
  2417. * after the reset.
  2418. */
  2419. crp->crp_etype = ENOMEM;
  2420. pci_unmap_buf(sc, &cmd->dst);
  2421. } else
  2422. crp->crp_etype = ENOMEM;
  2423. pci_unmap_buf(sc, &cmd->src);
  2424. kfree(cmd);
  2425. if (crp->crp_etype != EAGAIN)
  2426. crypto_done(crp);
  2427. }
  2428. if (++i == HIFN_D_RES_RSIZE)
  2429. i = 0;
  2430. u--;
  2431. }
  2432. dma->resk = i; dma->resu = u;
  2433. hifn_reset_board(sc, 1);
  2434. hifn_init_dma(sc);
  2435. hifn_init_pci_registers(sc);
  2436. }
  2437. static void
  2438. hifn_callback(struct hifn_softc *sc, struct hifn_command *cmd, u_int8_t *macbuf)
  2439. {
  2440. struct hifn_dma *dma = sc->sc_dma;
  2441. struct cryptop *crp = cmd->crp;
  2442. struct cryptodesc *crd;
  2443. int i, u;
  2444. DPRINTF("%s()\n", __FUNCTION__);
  2445. #if 0
  2446. if (cmd->src_map == cmd->dst_map) {
  2447. bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
  2448. BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
  2449. } else {
  2450. bus_dmamap_sync(sc->sc_dmat, cmd->src_map,
  2451. BUS_DMASYNC_POSTWRITE);
  2452. bus_dmamap_sync(sc->sc_dmat, cmd->dst_map,
  2453. BUS_DMASYNC_POSTREAD);
  2454. }
  2455. #endif
  2456. if (crp->crp_flags & CRYPTO_F_SKBUF) {
  2457. if (cmd->src_skb != cmd->dst_skb) {
  2458. #ifdef NOTYET
  2459. crp->crp_buf = (caddr_t)cmd->dst_m;
  2460. totlen = cmd->src_mapsize;
  2461. for (m = cmd->dst_m; m != NULL; m = m->m_next) {
  2462. if (totlen < m->m_len) {
  2463. m->m_len = totlen;
  2464. totlen = 0;
  2465. } else
  2466. totlen -= m->m_len;
  2467. }
  2468. cmd->dst_m->m_pkthdr.len = cmd->src_m->m_pkthdr.len;
  2469. m_freem(cmd->src_m);
  2470. #else
  2471. device_printf(sc->sc_dev,
  2472. "%s,%d: CRYPTO_F_SKBUF src != dst not implemented\n",
  2473. __FILE__, __LINE__);
  2474. #endif
  2475. }
  2476. }
  2477. if (cmd->sloplen != 0) {
  2478. crypto_copyback(crp->crp_flags, crp->crp_buf,
  2479. cmd->src_mapsize - cmd->sloplen, cmd->sloplen,
  2480. (caddr_t)&dma->slop[cmd->slopidx]);
  2481. }
  2482. i = dma->dstk; u = dma->dstu;
  2483. while (u != 0) {
  2484. if (i == HIFN_D_DST_RSIZE)
  2485. i = 0;
  2486. #if 0
  2487. bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
  2488. BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  2489. #endif
  2490. if (dma->dstr[i].l & htole32(HIFN_D_VALID)) {
  2491. #if 0
  2492. bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
  2493. BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  2494. #endif
  2495. break;
  2496. }
  2497. i++, u--;
  2498. }
  2499. dma->dstk = i; dma->dstu = u;
  2500. hifnstats.hst_obytes += cmd->dst_mapsize;
  2501. if (macbuf != NULL) {
  2502. for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
  2503. int len;
  2504. if (crd->crd_alg != CRYPTO_MD5 &&
  2505. crd->crd_alg != CRYPTO_SHA1 &&
  2506. crd->crd_alg != CRYPTO_MD5_HMAC &&
  2507. crd->crd_alg != CRYPTO_SHA1_HMAC) {
  2508. continue;
  2509. }
  2510. len = cmd->softc->sc_sessions[cmd->session_num].hs_mlen;
  2511. crypto_copyback(crp->crp_flags, crp->crp_buf,
  2512. crd->crd_inject, len, macbuf);
  2513. break;
  2514. }
  2515. }
  2516. if (cmd->src_map != cmd->dst_map)
  2517. pci_unmap_buf(sc, &cmd->dst);
  2518. pci_unmap_buf(sc, &cmd->src);
  2519. kfree(cmd);
  2520. crypto_done(crp);
  2521. }
  2522. /*
  2523. * 7811 PB3 rev/2 parts lock-up on burst writes to Group 0
  2524. * and Group 1 registers; avoid conditions that could create
  2525. * burst writes by doing a read in between the writes.
  2526. *
  2527. * NB: The read we interpose is always to the same register;
  2528. * we do this because reading from an arbitrary (e.g. last)
  2529. * register may not always work.
  2530. */
  2531. static void
  2532. hifn_write_reg_0(struct hifn_softc *sc, bus_size_t reg, u_int32_t val)
  2533. {
  2534. if (sc->sc_flags & HIFN_IS_7811) {
  2535. if (sc->sc_bar0_lastreg == reg - 4)
  2536. readl(sc->sc_bar0 + HIFN_0_PUCNFG);
  2537. sc->sc_bar0_lastreg = reg;
  2538. }
  2539. writel(val, sc->sc_bar0 + reg);
  2540. }
  2541. static void
  2542. hifn_write_reg_1(struct hifn_softc *sc, bus_size_t reg, u_int32_t val)
  2543. {
  2544. if (sc->sc_flags & HIFN_IS_7811) {
  2545. if (sc->sc_bar1_lastreg == reg - 4)
  2546. readl(sc->sc_bar1 + HIFN_1_REVID);
  2547. sc->sc_bar1_lastreg = reg;
  2548. }
  2549. writel(val, sc->sc_bar1 + reg);
  2550. }
  2551. static struct pci_device_id hifn_pci_tbl[] = {
  2552. { PCI_VENDOR_HIFN, PCI_PRODUCT_HIFN_7951,
  2553. PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  2554. { PCI_VENDOR_HIFN, PCI_PRODUCT_HIFN_7955,
  2555. PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  2556. { PCI_VENDOR_HIFN, PCI_PRODUCT_HIFN_7956,
  2557. PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  2558. { PCI_VENDOR_NETSEC, PCI_PRODUCT_NETSEC_7751,
  2559. PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  2560. { PCI_VENDOR_INVERTEX, PCI_PRODUCT_INVERTEX_AEON,
  2561. PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  2562. { PCI_VENDOR_HIFN, PCI_PRODUCT_HIFN_7811,
  2563. PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  2564. /*
  2565. * Other vendors share this PCI ID as well, such as
  2566. * http://www.powercrypt.com, and obviously they also
  2567. * use the same key.
  2568. */
  2569. { PCI_VENDOR_HIFN, PCI_PRODUCT_HIFN_7751,
  2570. PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
  2571. { 0, 0, 0, 0, 0, 0, }
  2572. };
  2573. MODULE_DEVICE_TABLE(pci, hifn_pci_tbl);
  2574. static struct pci_driver hifn_driver = {
  2575. .name = "hifn",
  2576. .id_table = hifn_pci_tbl,
  2577. .probe = hifn_probe,
  2578. .remove = hifn_remove,
  2579. /* add PM stuff here one day */
  2580. };
  2581. static int __init hifn_init (void)
  2582. {
  2583. struct hifn_softc *sc = NULL;
  2584. int rc;
  2585. DPRINTF("%s(%p)\n", __FUNCTION__, hifn_init);
  2586. rc = pci_register_driver(&hifn_driver);
  2587. pci_register_driver_compat(&hifn_driver, rc);
  2588. return rc;
  2589. }
  2590. static void __exit hifn_exit (void)
  2591. {
  2592. pci_unregister_driver(&hifn_driver);
  2593. }
  2594. module_init(hifn_init);
  2595. module_exit(hifn_exit);
  2596. MODULE_LICENSE("Dual BSD/GPL");
  2597. MODULE_AUTHOR("David McCullough <david_mccullough@mcafee.com>");
  2598. MODULE_DESCRIPTION("OCF driver for hifn PCI crypto devices");